Skip to contentSkip to conversation

sched/debug: Dump end of stack when detected corrupted

PATCH Superseded Feng Tang 1 participant 1 message Feb 7, 2024 Scheduler

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Feng Tang

author
#0 lore raw

PATCH v2 1/1 sched/debug: Dump end of stack when detected corrupted

When debugging a kernel hang during suspend/resume [1], there were random memory corruptions in different places like being reported by slub_debug+KASAN, or detected by scheduler with error message:

"Kernel panic - not syncing: corrupted stack end detected inside scheduler"

So dump the corrupted memory around the stack end to give more direct hints about how the memory is corrupted:

"

 Corrupted Stack: ff11000122770000: ff ff ff ff ff ff 14 91 82 3b 78 e8 08 00 45 00  .........;x...E.
 Corrupted Stack: ff11000122770010: 00 1d 2a ff 40 00 40 11 98 c8 0a ef 30 2c 0a ef  ..*.@[email protected],..
 Corrupted Stack: ff11000122770020: 30 ff a2 00 22 3d 00 09 9a 95 2a 00 00 00 00 00  0..."=....*.....
 ...
 Kernel panic - not syncing: corrupted stack end detected inside scheduler
 "

And with it, the culprit was quickly identified to be ethernet driver that it frees RX related memory back to kernel in suspend hook, but its HW is not really stopped, and still send RX data to those old buffer through DMA.

[1]. https://bugzilla.kernel.org/show_bug.cgi?id=217854

---
Changlog:

since v1:
* Refine the commit log with more info, and rebase againt 6.8-rc3

 kernel/sched/core.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
kernel/sched/core.c +11 -1
index 9116bcc90346..1344ac42e6d2 100644
@@ -5950,8 +5950,18 @@ static noinline void __schedule_bug(struct task_struct *prev)
 static inline void schedule_debug(struct task_struct *prev, bool preempt)
 {
 #ifdef CONFIG_SCHED_STACK_END_CHECK
-	if (task_stack_end_corrupted(prev))
+	if (task_stack_end_corrupted(prev)) {
+		unsigned long *ptr = end_of_stack(prev);
+
+		/* Dump 16 ulong words around the corruption point */
+#ifdef CONFIG_STACK_GROWSUP
+		ptr -= 15;
+#endif
+		print_hex_dump(KERN_ERR, "Corrupted Stack: ",
+			DUMP_PREFIX_ADDRESS, 16, 1, ptr, 16 * sizeof(*ptr), 1);
+
 		panic("corrupted stack end detected inside scheduler\n");
+	}
 
 	if (task_scs_end_corrupted(prev))
 		panic("corrupted shadow stack detected inside scheduler\n");
Signature · 1 line
2.34.1