Skip to content

Commit 37fe6a4

Browse files
makasayahIngo Molnar
authored andcommitted
x86: Check stack overflow in detail
Currently, only kernel stack is checked for the overflow, which is not sufficient for systems that need a high reliability. To enhance it, it is required to check the IRQ and exception stacks, as well. This patch checks all the stack types and will cause messages of stacks in detail when free stack space drops below a certain limit except user stack. Signed-off-by: Mitsuo Hayasaka <mitsuo.hayasaka.hu@hitachi.com> Cc: yrl.pp-manager.tt@hitachi.com Cc: Randy Dunlap <rdunlap@xenotime.net> Link: http://lkml.kernel.org/r/20111129060829.11076.51733.stgit@ltc219.sdl.hitachi.co.jp Signed-off-by: Ingo Molnar <mingo@elte.hu> Cc: "H. Peter Anvin" <hpa@zytor.com>
1 parent 69682b6 commit 37fe6a4

2 files changed

Lines changed: 28 additions & 8 deletions

File tree

‎arch/x86/Kconfig.debug‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ config DEBUG_STACKOVERFLOW
6363
bool "Check for stack overflows"
6464
depends on DEBUG_KERNEL
6565
---help---
66-
This option will cause messages to be printed if free stack space
67-
drops below a certain limit.
66+
Say Y here if you want to check the overflows of kernel, IRQ
67+
and exception stacks. This option will cause messages of the
68+
stacks in detail when free stack space drops below a certain
69+
limit.
70+
If in doubt, say "N".
6871

6972
config X86_PTDUMP
7073
bool "Export kernel pagetable layout to userspace via debugfs"

‎arch/x86/kernel/irq_64.c‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,35 @@ EXPORT_PER_CPU_SYMBOL(irq_regs);
3636
static inline void stack_overflow_check(struct pt_regs *regs)
3737
{
3838
#ifdef CONFIG_DEBUG_STACKOVERFLOW
39+
struct orig_ist *oist;
40+
u64 irq_stack_top, irq_stack_bottom;
41+
u64 estack_top, estack_bottom;
3942
u64 curbase = (u64)task_stack_page(current);
4043

4144
if (user_mode_vm(regs))
4245
return;
4346

44-
WARN_ONCE(regs->sp >= curbase &&
45-
regs->sp <= curbase + THREAD_SIZE &&
46-
regs->sp < curbase + sizeof(struct thread_info) +
47-
sizeof(struct pt_regs) + 128,
47+
if (regs->sp >= curbase &&
48+
regs->sp <= curbase + THREAD_SIZE &&
49+
regs->sp >= curbase + sizeof(struct thread_info) +
50+
sizeof(struct pt_regs) + 128)
51+
return;
52+
53+
irq_stack_top = (u64)__get_cpu_var(irq_stack_union.irq_stack);
54+
irq_stack_bottom = (u64)__get_cpu_var(irq_stack_ptr);
55+
if (regs->sp >= irq_stack_top && regs->sp <= irq_stack_bottom)
56+
return;
57+
58+
oist = &__get_cpu_var(orig_ist);
59+
estack_top = (u64)oist->ist[0] - EXCEPTION_STKSZ;
60+
estack_bottom = (u64)oist->ist[N_EXCEPTION_STACKS - 1];
61+
if (regs->sp >= estack_top && regs->sp <= estack_bottom)
62+
return;
4863

49-
"do_IRQ: %s near stack overflow (cur:%Lx,sp:%lx)\n",
50-
current->comm, curbase, regs->sp);
64+
WARN_ONCE(1, "do_IRQ(): %s has overflown the kernel stack (cur:%Lx,sp:%lx,irq stk top-bottom:%Lx-%Lx,exception stk top-bottom:%Lx-%Lx)\n",
65+
current->comm, curbase, regs->sp,
66+
irq_stack_top, irq_stack_bottom,
67+
estack_top, estack_bottom);
5168
#endif
5269
}
5370

0 commit comments

Comments
 (0)