[PATCH v2 2/3] sched/core: Add sched_stack_end_check= boot parameter
Chris Down
chris at chrisdown.name
Fri Nov 13 00:37:21 PST 2026
CONFIG_SCHED_STACK_END_CHECK makes schedule_debug() check the magic at
the end of the outgoing task's stack and panic if it has been
overwritten.
Right now this can only be configured by setting the Kconfig option at
build time. At Meta we have it on for some hosts and off for others,
since it is helpful but adds cost on context switching. That means today
we have to build separate kernels for each, which is less than ideal.
Make this configurable at boot time with sched_stack_end_check=, and
keep the Kconfig option as its default. In benchmarking sched pipe and
sched messaging performance is unaffected.
The other callers of task_stack_end_corrupted() are left as they are.
They have never depended on CONFIG_SCHED_STACK_END_CHECK, and they only
look at the magic once an oops or a might_sleep() report is already
being printed, or when the stack tracer records a new maximum.
Signed-off-by: Chris Down <chris at chrisdown.name>
---
.../admin-guide/kernel-parameters.txt | 8 +++++
kernel/sched/core.c | 33 +++++++++++++++----
2 files changed, 35 insertions(+), 6 deletions(-)
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 0b3c140babd9..c106f744b5a4 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -6825,6 +6825,14 @@ Kernel parameters
core priority to both SMT siblings, so this option
does not favor either sibling.
+ sched_stack_end_check= [KNL]
+ Format: <bool>
+ Check the magic value at the end of the outgoing
+ task's stack (and its shadow call stack, if any) on
+ every call to schedule(), and panic if it has been
+ overwritten.
+ Default set by CONFIG_SCHED_STACK_END_CHECK.
+
sched_verbose [KNL,EARLY] Enables verbose scheduler debug messages.
schedstats= [KNL,X86] Enable or disable scheduled statistics.
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 175b52a7ea79..9e5b3c62c697 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -6144,18 +6144,39 @@ static noinline void __schedule_bug(struct task_struct *prev)
add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
}
+static DEFINE_STATIC_KEY_MAYBE_RO(CONFIG_SCHED_STACK_END_CHECK, sched_stack_end_check);
+
+static int __init setup_sched_stack_end_check(char *str)
+{
+ bool enable;
+
+ if (kstrtobool(str, &enable)) {
+ pr_warn("Unable to parse sched_stack_end_check=\n");
+ return 1;
+ }
+
+ if (enable)
+ static_branch_enable(&sched_stack_end_check);
+ else
+ static_branch_disable(&sched_stack_end_check);
+
+ return 1;
+}
+__setup("sched_stack_end_check=", setup_sched_stack_end_check);
+
/*
* Various schedule()-time debugging checks and statistics:
*/
static inline void schedule_debug(struct task_struct *prev, bool preempt)
{
-#ifdef CONFIG_SCHED_STACK_END_CHECK
- if (task_stack_end_corrupted(prev))
- panic("corrupted stack end detected inside scheduler\n");
+ if (static_branch_maybe(CONFIG_SCHED_STACK_END_CHECK,
+ &sched_stack_end_check)) {
+ if (task_stack_end_corrupted(prev))
+ panic("corrupted stack end detected inside scheduler\n");
- if (task_scs_end_corrupted(prev))
- panic("corrupted shadow stack detected inside scheduler\n");
-#endif
+ if (task_scs_end_corrupted(prev))
+ panic("corrupted shadow stack detected inside scheduler\n");
+ }
#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
if (!preempt && READ_ONCE(prev->__state) && prev->non_block_count) {
--
2.56.1
More information about the linux-riscv
mailing list