Skip to contentSkip to conversation

Make the scheduler stack end check selectable at boot

PATCH Superseded Chris Down 3 participants 10 messages Oct 27 – Nov 13

Subsystems (6)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Chris Down

author
#0 lore raw

cover Make the scheduler stack end check selectable at boot

At Meta, some of our host types enable CONFIG_SCHED_STACK_END_CHECK, but we don't want it generally on because of the extra work on every context switch.

Right now this is only configurable by setting the Kconfig option at build time. Let's make it so we can also select it at boot with sched_stack_end_check=, so that those hosts can share one kernel image.

(Patch 1 is a cleanup I happened upon while working on this, it can also be taken independently.)

Chris Down (3):
sched/core: Skip the shadow stack end check when SCS is disabled sched/core: Add sched_stack_end_check= boot parameter lkdtm/bugs: Add CORRUPT_STACK_END to test the scheduler stack end
check

 .../admin-guide/kernel-parameters.txt         |  8 +++++
 drivers/misc/lkdtm/bugs.c                     | 21 ++++++++++++
 kernel/sched/core.c                           | 33 +++++++++++++++----
 tools/testing/selftests/lkdtm/tests.txt       |  1 +
 4 files changed, 57 insertions(+), 6 deletions(-)

base-commit: b99b7f60c5b473d374c68b53753153b4aa9e0702

Signature · 1 line
2.54.0
author
#1 lore raw

PATCH 1/3 sched/core: Skip the shadow stack end check when SCS is disabled

With CONFIG_DYNAMIC_SCS, shadow call stacks are only used when the boot CPU lacks pointer authentication. scs_release() already checks scs_is_enabled() before it looks at the shadow stack since commit 9beccca09840 ("scs: add support for dynamic shadow call stacks"), but schedule_debug() does not.

With CONFIG_SCHED_STACK_END_CHECK=y it therefore checks init_task's unused shadow stack on every context switch. That is harmless right now, but the check is redundant and costs a load from an unrelated cache line in __schedule().

So, let's guard on scs_is_enabled(). It is a static branch, and it folds to true when CONFIG_DYNAMIC_SCS is not set.

---
 kernel/sched/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/core.c +1 -1
index d7c3b43158de..fc269fd10192 100644
@@ -6152,7 +6152,7 @@ static inline void schedule_debug(struct task_struct *prev, bool preempt)
 	if (task_stack_end_corrupted(prev))
 		panic("corrupted stack end detected inside scheduler\n");
 
-	if (task_scs_end_corrupted(prev))
+	if (scs_is_enabled() && task_scs_end_corrupted(prev))
 		panic("corrupted shadow stack detected inside scheduler\n");
 #endif
 
Signature · 1 line
2.54.0
author
#4 lore raw

PATCH 2/3 sched/core: Add sched_stack_end_check= boot parameter

In reply to Chris Down

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.

So let's make this configurable at boot time. In benchmarking sched pipe and sched messaging performance is unaffected.

---
 .../admin-guide/kernel-parameters.txt         |  8 +++++
 kernel/sched/core.c                           | 33 +++++++++++++++----
 2 files changed, 35 insertions(+), 6 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +8 -0
index fbee7b544903..95911d33c0fe 100644
@@ -6811,6 +6811,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.
kernel/sched/core.c +27 -6
index fc269fd10192..c1fb86b813bb 100644
@@ -6143,18 +6143,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 0;
+	}
+
+	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 (scs_is_enabled() && task_scs_end_corrupted(prev))
-		panic("corrupted shadow stack detected inside scheduler\n");
-#endif
+		if (scs_is_enabled() && 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) {
Signature · 1 line
2.54.0
author
#9 lore raw

PATCH 3/3 lkdtm/bugs: Add CORRUPT_STACK_END to test the scheduler stack end check

In reply to Chris Down

There's no existing test for this, so let's add one.

---
 drivers/misc/lkdtm/bugs.c               | 21 +++++++++++++++++++++
 tools/testing/selftests/lkdtm/tests.txt |  1 +
 2 files changed, 22 insertions(+)
drivers/misc/lkdtm/bugs.c +21 -0
index 3eca2ef64aff..929a60db00d6 100644
@@ -215,6 +215,26 @@ static noinline void lkdtm_CORRUPT_STACK_STRONG(void)
 	__lkdtm_CORRUPT_STACK((void *)&data);
 }
 
+static void lkdtm_CORRUPT_STACK_END(void)
+{
+	unsigned long *stack_end = end_of_stack(current);
+
+	if (READ_ONCE(*stack_end) != STACK_END_MAGIC) {
+		pr_err("FAIL: stack end magic is already corrupted\n");
+		return;
+	}
+
+	pr_info("Corrupting stack end magic and calling schedule() ...\n");
+	WRITE_ONCE(*stack_end, ~STACK_END_MAGIC);
+	schedule_timeout_uninterruptible(1);
+
+	/* Still alive, so let's restore the magic so nothing else trips over it. */
+	WRITE_ONCE(*stack_end, STACK_END_MAGIC);
+	pr_err("FAIL: corrupted stack end was not detected by the scheduler\n");
+	pr_expected_config_param(CONFIG_SCHED_STACK_END_CHECK,
+				 "sched_stack_end_check");
+}
+
 static pid_t stack_pid;
 static unsigned long stack_addr;
 
@@ -854,6 +874,7 @@ static struct crashtype crashtypes[] = {
 	CRASHTYPE(EXHAUST_STACK),
 	CRASHTYPE(CORRUPT_STACK),
 	CRASHTYPE(CORRUPT_STACK_STRONG),
+	CRASHTYPE(CORRUPT_STACK_END),
 	CRASHTYPE(REPORT_STACK),
 	CRASHTYPE(REPORT_STACK_CANARY),
 	CRASHTYPE(UNALIGNED_LOAD_STORE_WRITE),
tools/testing/selftests/lkdtm/tests.txt +1 -0
index bec57a02913a..2d856ed77a64 100644
@@ -10,6 +10,7 @@ EXCEPTION
 #EXHAUST_STACK Corrupts memory on failure
 #CORRUPT_STACK Crashes entire system on success
 #CORRUPT_STACK_STRONG Crashes entire system on success
+#CORRUPT_STACK_END Crashes entire system on success
 ARRAY_BOUNDS call trace:|UBSAN: array-index-out-of-bounds
 FAM_BOUNDS call trace:|UBSAN: array-index-out-of-bounds
 PTR_BOUNDS call trace:|UBSAN: array-index-out-of-bounds
Signature · 1 line
2.54.0
maintainer
#2 lore raw

Re: [PATCH 1/3] sched/core: Skip the shadow stack end check when SCS is disabled

In reply to Chris Down
Quoted 24 lines · Chris Down

With CONFIG_DYNAMIC_SCS, shadow call stacks are only used when the boot CPU lacks pointer authentication. scs_release() already checks scs_is_enabled() before it looks at the shadow stack since commit 9beccca09840 ("scs: add support for dynamic shadow call stacks"), but schedule_debug() does not.

With CONFIG_SCHED_STACK_END_CHECK=y it therefore checks init_task's unused shadow stack on every context switch. That is harmless right now, but the check is redundant and costs a load from an unrelated cache line in __schedule().

So, let's guard on scs_is_enabled(). It is a static branch, and it folds to true when CONFIG_DYNAMIC_SCS is not set.

---
 kernel/sched/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/core.c +0 -0
index d7c3b43158de..fc269fd10192 100644

kernel/sched/core.c static inline void schedule_debug(struct task_struct *prev, bool preempt) · line 6156

Quoted 6 lines
kernel/sched/core.c +1 -1
@@ -6152,7 +6152,7 @@ static inline void schedule_debug(struct task_struct *prev, bool preempt)
 	if (task_stack_end_corrupted(prev))
 		panic("corrupted stack end detected inside scheduler\n");
 
-	if (task_scs_end_corrupted(prev))
+	if (scs_is_enabled() && task_scs_end_corrupted(prev))
 		panic("corrupted shadow stack detected inside scheduler\n");

How about you push that scs_is_enabled() into task_scs_end_corrupted(), this makes less noise here, and also keeps the next patch saner, no?

maintainer
#5 lore raw

Re: [PATCH 2/3] sched/core: Add sched_stack_end_check= boot parameter

In reply to Chris Down
Quoted 74 lines · Chris Down

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.

So let's make this configurable at boot time. In benchmarking sched pipe and sched messaging performance is unaffected.

---
 .../admin-guide/kernel-parameters.txt         |  8 +++++
 kernel/sched/core.c                           | 33 +++++++++++++++----
 2 files changed, 35 insertions(+), 6 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +8 -0
index fbee7b544903..95911d33c0fe 100644
@@ -6811,6 +6811,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.
kernel/sched/core.c +21 -3
index fc269fd10192..c1fb86b813bb 100644
@@ -6143,18 +6143,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 0;
+	}
+
+	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,

kernel/sched/core.c static noinline void __schedule_bug(struct task_struct *prev) · line 6178

Quoted 10 lines
kernel/sched/core.c +6 -3
+				&sched_stack_end_check)) {
+		if (task_stack_end_corrupted(prev))
+			panic("corrupted stack end detected inside scheduler\n");
 
-	if (scs_is_enabled() && task_scs_end_corrupted(prev))
-		panic("corrupted shadow stack detected inside scheduler\n");
-#endif
+		if (scs_is_enabled() && task_scs_end_corrupted(prev))
+			panic("corrupted shadow stack detected inside scheduler\n");
+	}

I just noticed that there's a number of task_stack_end_corrupted() checks that:

- do not also do task_scs_end_corrupted(); should task_scs_end_corrupted() be put inside task_stack_end_corrupted() to make sure they're always done in tandem and the shadow stack check isn't lost?

- should these other sites also respect this switch? It seems weird to only have this one site affected.

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

reviewer
#7 lore raw

Re: [PATCH 2/3] sched/core: Add sched_stack_end_check= boot parameter

In reply to Chris Down

kernel/sched/core.c static noinline void __schedule_bug(struct task_struct *prev) · line 6155

Quoted 8 lines · Chris Down
kernel/sched/core.c +8 -0
+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 0;
+	}

This error branch should return 1 since it handled the command line parameter -- it just didn't like the "value."

Returning 0 causes the option string to be added to init's environment, which is not wanted in this case.

Signature · 7 lines
~Randy


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author
#3 lore raw

Re: [PATCH 1/3] sched/core: Skip the shadow stack end check when SCS is disabled

In reply to Peter Zijlstra

Hi Peter,

Quoted 2 lines · Peter Zijlstra

How about you push that scs_is_enabled() into task_scs_end_corrupted(), this makes less noise here, and also keeps the next patch saner, no?

Yep, that's nicer. I've done that for v2.

Thanks!

Chris

author
#6 lore raw

Re: [PATCH 2/3] sched/core: Add sched_stack_end_check= boot parameter

In reply to Peter Zijlstra

Hi Peter,

Quoted 7 lines · Peter Zijlstra

I just noticed that there's a number of task_stack_end_corrupted() checks that:

- do not also do task_scs_end_corrupted(); should task_scs_end_corrupted() be put inside task_stack_end_corrupted() to make sure they're always done in tandem and the shadow stack check isn't lost?

I had a look at the other callers, and I don't think the shadow stack check is being lost in any of them. Two are the oops paths in the x86 and powerpc fault handlers, and neither architecture has CONFIG_SHADOW_CALL_STACK. So putting it inside task_stack_end_corrupted() would only change __might_resched() and the stack tracer, and only on arm64 and riscv.

Both of those are about the task stack in particular. __might_resched() got its hint in
a8b686b3af44 ("sched/debug: Check for stack overflow in ___might_sleep()") because at the time a stack overrun could corrupt thread_info and trigger the warning on its own. The stack tracer checks right after it has recorded a new maximum depth for the task stack. Neither has anything to do with the shadow stack as far as I can see.

schedule_debug() would also still want to check the two separately, so that the panic message keeps saying which stack it was.

Quoted 2 lines

- should these other sites also respect this switch? It seems weird to only have this one site affected.

It is a bit odd, but I think only this one should follow it. None of the others depend on CONFIG_SCHED_STACK_END_CHECK today, and the switch is only meant to be the boot time version of that option. They also only look at the magic once something has already gone wrong and we're printing a report about it. The stack tracer is the exception, but it only looks when it's enabled and sees a new maximum. So there's no cost to avoid by turning them off.

If they did follow the switch, anyone who leaves it off would lose the "Thread overran stack" hint from their oopses.

So I've left both as they are in v2, and the changelog now says why the other callers don't follow the switch. Let me know if you'd still rather have them folded together, though.

Thanks!

Chris

author
#8 lore raw

Re: [PATCH 2/3] sched/core: Add sched_stack_end_check= boot parameter

In reply to Randy Dunlap

Hi Randy,

Quoted 5 lines · Randy Dunlap

This error branch should return 1 since it handled the command line parameter -- it just didn't like the "value."

Returning 0 causes the option string to be added to init's environment, which is not wanted in this case.

Good catch. I've fixed that for v2.

Thanks!

Chris