stack overflow checking for x86_64 / 2.6
PATCH 1/1 stack overflow checking for x86_64 / 2.6
This existed for x86_64 in 2.4, but seems to have gone AWOL in 2.6.
I've pretty much just copied this over from the 2.4 code, with appropriate tweaks for the 2.6 kernel, plus a bugfix. I'd personally rather see it printed out the way other arches do it, i.e. bytes-remaining-until-overflow, rather than having to do the subtraction yourself. Also, only 128 bytes remaining seems awfully late to issue a warning. But I'll start here :)
Thanks,
-Eric
arch/x86_64/Kconfig.debug +7 -0
@@ -35,6 +35,13 @@ Add a simple leak tracer to the IOMMU code. This is useful when you are debugging a buggy device driver that leaks IOMMU mappings. +config DEBUG_STACKOVERFLOW + bool "Check for stack overflows" + depends on DEBUG_KERNEL + help + This option will cause messages to be printed if free stack space + drops below a certain limit. + #config X86_REMOTE_DEBUG # bool "kgdb debugging stub"
arch/x86_64/kernel/irq.c +27 -1
@@ -26,6 +26,30 @@#endif #endif +#ifdef CONFIG_DEBUG_STACKOVERFLOW +/* +* Probalistic stack overflow check: +* +* Only check the stack in process context, because everything else +* runs on the big interrupt stacks. Checking reliably is too expensive, +* so we just check from interrupts. +*/ +static inline void stack_overflow_check (struct pt_regs *regs) +{ +u64 curbase = (u64 ) current->thread_info; +static unsigned long warned =-60 *HZ ; + +if (regs->rsp >= curbase && regs->rsp <= curbase +THREAD_SIZE && + regs->rsp < curbase +sizeof (struct thread_info ) +128 && +time_after (jiffies, warned +60 *HZ )) { +printk ("do_IRQ: %s near stack overflow (cur:%Lx,rsp:%lx)\n" , + current->comm, curbase, regs->rsp); +show_stack (NULL ,NULL ); + warned = jiffies; + } +} +#endif +/* * Generic, controller-independent functions: */
@@ -96,7 +120,9 @@exit_idle ();irq_enter (); - +#ifdef CONFIG_DEBUG_STACKOVERFLOW +stack_overflow_check (regs); +#endif __do_IRQ (irq, regs);irq_exit ();
arch/x86_64/Kconfig.debug
Quoted 5 lines
Index: linux-2.6.16/arch/x86_64/Kconfig.debug
===================================================================arch/x86_64/Kconfig.debug +0
-0
2.6.16? Of course the patch doesn't apply ...
Patch submissions please always against current Linus tree.
-Andi
Quoted 10 lines · Andi Kleen
arch/x86_64/Kconfig.debug
Quoted 4 lines
Index: linux-2.6.16/arch/x86_64/Kconfig.debug
===================================================================arch/x86_64/Kconfig.debug +0
-02.6.16? Of course the patch doesn't apply ...
Patch submissions please always against current Linus tree.
-Andi
It was 2.6.17-rc6, just didn't rename the directory.
Appears my mailer mangled whitespace, I'll resubmit shortly, sorry about that.
-Eric