Skip to content

Commit 2a594d4

Browse files
KAGA-KOKOsuryasaimadhu
authored andcommitted
x86/exceptions: Split debug IST stack
The debug IST stack is actually two separate debug stacks to handle #DB recursion. This is required because the CPU starts always at top of stack on exception entry, which means on #DB recursion the second #DB would overwrite the stack of the first. The low level entry code therefore adjusts the top of stack on entry so a secondary #DB starts from a different stack page. But the stack pages are adjacent without a guard page between them. Split the debug stack into 3 stacks which are separated by guard pages. The 3rd stack is never mapped into the cpu_entry_area and is only there to catch triple #DB nesting: --- top of DB_stack <- Initial stack --- end of DB_stack guard page --- top of DB1_stack <- Top of stack after entering first #DB --- end of DB1_stack guard page --- top of DB2_stack <- Top of stack after entering second #DB --- end of DB2_stack guard page If DB2 would not act as the final guard hole, a second #DB would point the top of #DB stack to the stack below #DB1 which would be valid and not catch the not so desired triple nesting. The backing store does not allocate any memory for DB2 and its guard page as it is not going to be mapped into the cpu_entry_area. - Adjust the low level entry code so it adjusts top of #DB with the offset between the stacks instead of exception stack size. - Make the dumpstack code aware of the new stacks. - Adjust the in_debug_stack() implementation and move it into the NMI code where it belongs. As this is NMI hotpath code, it just checks the full area between top of DB_stack and bottom of DB1_stack without checking for the guard page. That's correct because the NMI cannot hit a stackpointer pointing to the guard page between DB and DB1 stack. Even if it would, then the NMI operation still is unaffected, but the resume of the debug exception on the topmost DB stack will crash by touching the guard page. [ bp: Make exception_stack_names static const char * const ] Suggested-by: Andy Lutomirski <luto@kernel.org> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Borislav Petkov <bp@suse.de> Reviewed-by: Sean Christopherson <sean.j.christopherson@intel.com> Cc: Andy Lutomirski <luto@kernel.org> Cc: Baoquan He <bhe@redhat.com> Cc: "Chang S. Bae" <chang.seok.bae@intel.com> Cc: Dave Hansen <dave.hansen@linux.intel.com> Cc: Dominik Brodowski <linux@dominikbrodowski.net> Cc: "H. Peter Anvin" <hpa@zytor.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: Joerg Roedel <jroedel@suse.de> Cc: Jonathan Corbet <corbet@lwn.net> Cc: Josh Poimboeuf <jpoimboe@redhat.com> Cc: Juergen Gross <jgross@suse.com> Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com> Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com> Cc: linux-doc@vger.kernel.org Cc: Masahiro Yamada <yamada.masahiro@socionext.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Qian Cai <cai@lca.pw> Cc: Sean Christopherson <sean.j.christopherson@intel.com> Cc: x86-ml <x86@kernel.org> Link: https://lkml.kernel.org/r/20190414160145.439944544@linutronix.de
1 parent 1bdb67e commit 2a594d4

10 files changed

Lines changed: 52 additions & 31 deletions

File tree

‎Documentation/x86/kernel-stacks‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ The currently assigned IST stacks are :-
7676
middle of switching stacks. Using IST for NMI events avoids making
7777
assumptions about the previous state of the kernel stack.
7878

79-
* ESTACK_DB. DEBUG_STKSZ
79+
* ESTACK_DB. EXCEPTION_STKSZ (PAGE_SIZE).
8080

8181
Used for hardware debug interrupts (interrupt 1) and for software
8282
debug interrupts (INT3).
@@ -86,6 +86,11 @@ The currently assigned IST stacks are :-
8686
avoids making assumptions about the previous state of the kernel
8787
stack.
8888

89+
To handle nested #DB correctly there exist two instances of DB stacks. On
90+
#DB entry the IST stackpointer for #DB is switched to the second instance
91+
so a nested #DB starts from a clean stack. The nested #DB switches
92+
the IST stackpointer to a guard hole to catch triple nesting.
93+
8994
* ESTACK_MCE. EXCEPTION_STKSZ (PAGE_SIZE).
9095

9196
Used for interrupt 18 - Machine Check Exception (#MC).

‎arch/x86/entry/entry_64.S‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ apicinterrupt IRQ_WORK_VECTOR irq_work_interrupt smp_irq_work_interrupt
879879
* @paranoid == 2 is special: the stub will never switch stacks. This is for
880880
* #DF: if the thread stack is somehow unusable, we'll still get a useful OOPS.
881881
*/
882-
.macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1
882+
.macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1 ist_offset=0
883883
ENTRY(\sym)
884884
UNWIND_HINT_IRET_REGS offset=\has_error_code*8
885885

@@ -925,13 +925,13 @@ ENTRY(\sym)
925925
.endif
926926

927927
.if \shift_ist != -1
928-
subq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
928+
subq $\ist_offset, CPU_TSS_IST(\shift_ist)
929929
.endif
930930

931931
call \do_sym
932932

933933
.if \shift_ist != -1
934-
addq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
934+
addq $\ist_offset, CPU_TSS_IST(\shift_ist)
935935
.endif
936936

937937
/* these procedures expect "no swapgs" flag in ebx */
@@ -1129,7 +1129,7 @@ apicinterrupt3 HYPERV_STIMER0_VECTOR \
11291129
hv_stimer0_callback_vector hv_stimer0_vector_handler
11301130
#endif /* CONFIG_HYPERV */
11311131

1132-
idtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=IST_INDEX_DB
1132+
idtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=IST_INDEX_DB ist_offset=DB_STACK_OFFSET
11331133
idtentry int3 do_int3 has_error_code=0
11341134
idtentry stack_segment do_stack_segment has_error_code=1
11351135

‎arch/x86/include/asm/cpu_entry_area.h‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,29 @@
1010
#ifdef CONFIG_X86_64
1111

1212
/* Macro to enforce the same ordering and stack sizes */
13-
#define ESTACKS_MEMBERS(guardsize) \
13+
#define ESTACKS_MEMBERS(guardsize, db2_holesize)\
1414
char DF_stack_guard[guardsize]; \
1515
char DF_stack[EXCEPTION_STKSZ]; \
1616
char NMI_stack_guard[guardsize]; \
1717
char NMI_stack[EXCEPTION_STKSZ]; \
18+
char DB2_stack_guard[guardsize]; \
19+
char DB2_stack[db2_holesize]; \
20+
char DB1_stack_guard[guardsize]; \
21+
char DB1_stack[EXCEPTION_STKSZ]; \
1822
char DB_stack_guard[guardsize]; \
19-
char DB_stack[DEBUG_STKSZ]; \
23+
char DB_stack[EXCEPTION_STKSZ]; \
2024
char MCE_stack_guard[guardsize]; \
2125
char MCE_stack[EXCEPTION_STKSZ]; \
2226
char IST_top_guard[guardsize]; \
2327

2428
/* The exception stacks' physical storage. No guard pages required */
2529
struct exception_stacks {
26-
ESTACKS_MEMBERS(0)
30+
ESTACKS_MEMBERS(0, 0)
2731
};
2832

2933
/* The effective cpu entry area mapping with guard pages. */
3034
struct cea_exception_stacks {
31-
ESTACKS_MEMBERS(PAGE_SIZE)
35+
ESTACKS_MEMBERS(PAGE_SIZE, EXCEPTION_STKSZ)
3236
};
3337

3438
/*
@@ -37,6 +41,8 @@ struct cea_exception_stacks {
3741
enum exception_stack_ordering {
3842
ESTACK_DF,
3943
ESTACK_NMI,
44+
ESTACK_DB2,
45+
ESTACK_DB1,
4046
ESTACK_DB,
4147
ESTACK_MCE,
4248
N_EXCEPTION_STACKS

‎arch/x86/include/asm/debugreg.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,9 @@ static inline void debug_stack_usage_dec(void)
104104
{
105105
__this_cpu_dec(debug_stack_usage);
106106
}
107-
int is_debug_stack(unsigned long addr);
108107
void debug_stack_set_zero(void);
109108
void debug_stack_reset(void);
110109
#else /* !X86_64 */
111-
static inline int is_debug_stack(unsigned long addr) { return 0; }
112110
static inline void debug_stack_set_zero(void) { }
113111
static inline void debug_stack_reset(void) { }
114112
static inline void debug_stack_usage_inc(void) { }

‎arch/x86/include/asm/page_64_types.h‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
#define EXCEPTION_STACK_ORDER (0 + KASAN_STACK_ORDER)
1919
#define EXCEPTION_STKSZ (PAGE_SIZE << EXCEPTION_STACK_ORDER)
2020

21-
#define DEBUG_STACK_ORDER (EXCEPTION_STACK_ORDER + 1)
22-
#define DEBUG_STKSZ (PAGE_SIZE << DEBUG_STACK_ORDER)
23-
2421
#define IRQ_STACK_ORDER (2 + KASAN_STACK_ORDER)
2522
#define IRQ_STACK_SIZE (PAGE_SIZE << IRQ_STACK_ORDER)
2623

‎arch/x86/kernel/asm-offsets_64.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ int main(void)
6868
#undef ENTRY
6969

7070
OFFSET(TSS_ist, tss_struct, x86_tss.ist);
71+
DEFINE(DB_STACK_OFFSET, offsetof(struct cea_exception_stacks, DB_stack) -
72+
offsetof(struct cea_exception_stacks, DB1_stack));
7173
BLANK();
7274

7375
#ifdef CONFIG_STACKPROTECTOR

‎arch/x86/kernel/cpu/common.c‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,17 +1549,7 @@ void syscall_init(void)
15491549
X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
15501550
}
15511551

1552-
static DEFINE_PER_CPU(unsigned long, debug_stack_addr);
15531552
DEFINE_PER_CPU(int, debug_stack_usage);
1554-
1555-
int is_debug_stack(unsigned long addr)
1556-
{
1557-
return __this_cpu_read(debug_stack_usage) ||
1558-
(addr <= __this_cpu_read(debug_stack_addr) &&
1559-
addr > (__this_cpu_read(debug_stack_addr) - DEBUG_STKSZ));
1560-
}
1561-
NOKPROBE_SYMBOL(is_debug_stack);
1562-
15631553
DEFINE_PER_CPU(u32, debug_idt_ctr);
15641554

15651555
void debug_stack_set_zero(void)
@@ -1735,7 +1725,6 @@ void cpu_init(void)
17351725
t->x86_tss.ist[IST_INDEX_NMI] = __this_cpu_ist_top_va(NMI);
17361726
t->x86_tss.ist[IST_INDEX_DB] = __this_cpu_ist_top_va(DB);
17371727
t->x86_tss.ist[IST_INDEX_MCE] = __this_cpu_ist_top_va(MCE);
1738-
per_cpu(debug_stack_addr, cpu) = t->x86_tss.ist[IST_INDEX_DB];
17391728
}
17401729

17411730
t->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;

‎arch/x86/kernel/dumpstack_64.c‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,18 @@
1919
#include <asm/cpu_entry_area.h>
2020
#include <asm/stacktrace.h>
2121

22-
static const char *exception_stack_names[N_EXCEPTION_STACKS] = {
22+
static const char * const exception_stack_names[] = {
2323
[ ESTACK_DF ] = "#DF",
2424
[ ESTACK_NMI ] = "NMI",
25+
[ ESTACK_DB2 ] = "#DB2",
26+
[ ESTACK_DB1 ] = "#DB1",
2527
[ ESTACK_DB ] = "#DB",
2628
[ ESTACK_MCE ] = "#MC",
2729
};
2830

2931
const char *stack_type_name(enum stack_type type)
3032
{
31-
BUILD_BUG_ON(N_EXCEPTION_STACKS != 4);
33+
BUILD_BUG_ON(N_EXCEPTION_STACKS != 6);
3234

3335
if (type == STACK_TYPE_IRQ)
3436
return "IRQ";
@@ -58,9 +60,11 @@ struct estack_layout {
5860
.end = offsetof(struct cea_exception_stacks, x## _stack_guard) \
5961
}
6062

61-
static const struct estack_layout layout[N_EXCEPTION_STACKS] = {
63+
static const struct estack_layout layout[] = {
6264
[ ESTACK_DF ] = ESTACK_ENTRY(DF),
6365
[ ESTACK_NMI ] = ESTACK_ENTRY(NMI),
66+
[ ESTACK_DB2 ] = { .begin = 0, .end = 0},
67+
[ ESTACK_DB1 ] = ESTACK_ENTRY(DB1),
6468
[ ESTACK_DB ] = ESTACK_ENTRY(DB),
6569
[ ESTACK_MCE ] = ESTACK_ENTRY(MCE),
6670
};
@@ -71,7 +75,7 @@ static bool in_exception_stack(unsigned long *stack, struct stack_info *info)
7175
struct pt_regs *regs;
7276
unsigned int k;
7377

74-
BUILD_BUG_ON(N_EXCEPTION_STACKS != 4);
78+
BUILD_BUG_ON(N_EXCEPTION_STACKS != 6);
7579

7680
estacks = (unsigned long)__this_cpu_read(cea_exception_stacks);
7781

‎arch/x86/kernel/nmi.c‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121
#include <linux/ratelimit.h>
2222
#include <linux/slab.h>
2323
#include <linux/export.h>
24+
#include <linux/atomic.h>
2425
#include <linux/sched/clock.h>
2526

2627
#if defined(CONFIG_EDAC)
2728
#include <linux/edac.h>
2829
#endif
2930

30-
#include <linux/atomic.h>
31+
#include <asm/cpu_entry_area.h>
3132
#include <asm/traps.h>
3233
#include <asm/mach_traps.h>
3334
#include <asm/nmi.h>
@@ -487,6 +488,23 @@ static DEFINE_PER_CPU(unsigned long, nmi_cr2);
487488
* switch back to the original IDT.
488489
*/
489490
static DEFINE_PER_CPU(int, update_debug_stack);
491+
492+
static bool notrace is_debug_stack(unsigned long addr)
493+
{
494+
struct cea_exception_stacks *cs = __this_cpu_read(cea_exception_stacks);
495+
unsigned long top = CEA_ESTACK_TOP(cs, DB);
496+
unsigned long bot = CEA_ESTACK_BOT(cs, DB1);
497+
498+
if (__this_cpu_read(debug_stack_usage))
499+
return true;
500+
/*
501+
* Note, this covers the guard page between DB and DB1 as well to
502+
* avoid two checks. But by all means @addr can never point into
503+
* the guard page.
504+
*/
505+
return addr >= bot && addr < top;
506+
}
507+
NOKPROBE_SYMBOL(is_debug_stack);
490508
#endif
491509

492510
dotraplinkage notrace void

‎arch/x86/mm/cpu_entry_area.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,12 @@ static void __init percpu_setup_exception_stacks(unsigned int cpu)
9898

9999
/*
100100
* The exceptions stack mappings in the per cpu area are protected
101-
* by guard pages so each stack must be mapped separately.
101+
* by guard pages so each stack must be mapped separately. DB2 is
102+
* not mapped; it just exists to catch triple nesting of #DB.
102103
*/
103104
cea_map_stack(DF);
104105
cea_map_stack(NMI);
106+
cea_map_stack(DB1);
105107
cea_map_stack(DB);
106108
cea_map_stack(MCE);
107109
}

0 commit comments

Comments
 (0)