x86: Find a way to allow breakpoints in NMIs
v2 cover x86: Find a way to allow breakpoints in NMIs
OK, this is getting close. Although I'm labeling this as a RFC patchset, I've already queued it up into my git repo. I did some code cleanups since the last version, and I also added the IDT update if an NMI interrupts a process using the debug stack. This way the NMI wont corrupt it if it hits a breakpoint too.
I've also added code to handle i386, which was quite trivial, as NMIs and int3s do not change the stack when preempting privilege mode. That code was done all in C.
I've hammered this code with running perf on top of ftrace using the breakpoint conversions, and I've done this on two boxes. Both are holding up very well. I haven't hit any glitches.
With all that said, please take a final look over these patches and let me know what you think. After that, I'll be pushing this forward as it is required for my ftrace series.
Note, the below stats look like I added a lot of code. I've actually added more comments than code, as this needed it.
Thanks!
-- Steve
git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-trace.git
tip/x86/core
Head SHA1: e0b5187d3e29df98ae2b4c71250387d824ac90ae
Linus Torvalds (1):
x86: Do not schedule while still in NMI context
Steven Rostedt (4):
x86: Document the NMI handler about not using paranoid_exit
x86: Add workaround to NMI iret woes
x86: Keep current stack in NMI breakpoints
x86: Allow NMIs to hit breakpoints in i386
----
arch/x86/include/asm/desc.h | 12 ++ arch/x86/include/asm/processor.h | 6 + arch/x86/kernel/cpu/common.c | 22 ++++ arch/x86/kernel/entry_64.S | 208 ++++++++++++++++++++++++++++++++------ arch/x86/kernel/head_64.S | 4 + arch/x86/kernel/nmi.c | 101 ++++++++++++++++++ arch/x86/kernel/traps.c | 6 + 7 files changed, 326 insertions(+), 33 deletions(-)
RFC PATCH v2 1/5 x86: Do not schedule while still in NMI context
From: Linus Torvalds <[email protected]>
The NMI handler uses the paranoid_exit routine that checks the NEED_RESCHED flag, and if it is set and the return is for userspace, then interrupts are enabled, the stack is swapped to the thread's stack, and schedule is called. The problem with this is that we are still in an NMI context until an iret is executed. This means that any new NMIs are now starved until an interrupt or exception occurs and does the iret.
As NMIs can not be masked and can interrupt any location, they are treated as a special case. NEED_RESCHED should not be set in an NMI handler. The interruption by the NMI should not disturb the work flow for scheduling. Any IPI sent to a processor after sending the NEED_RESCHED would have to wait for the NMI anyway, and after the IPI finishes the schedule would be called as required.
There is no reason to do anything special leaving an NMI. Remove the call to paranoid_exit and do a simple return. This not only fixes the bug of starved NMIs, but it also cleans up the code.
--- arch/x86/kernel/entry_64.S | 32 -------------------------------- 1 files changed, 0 insertions(+), 32 deletions(-)
arch/x86/kernel/entry_64.S +0 -32
@@ -1489,46 +1489,14 @@ ENTRY(nmi) movq %rsp,%rdi movq $-1,%rsi call do_nmi -#ifdef CONFIG_TRACE_IRQFLAGS - /* paranoidexit; without TRACE_IRQS_OFF */ - /* ebx: no swapgs flag */ - DISABLE_INTERRUPTS(CLBR_NONE) testl %ebx,%ebx /* swapgs needed? */ jnz nmi_restore - testl $3,CS(%rsp) - jnz nmi_userspace nmi_swapgs: SWAPGS_UNSAFE_STACK nmi_restore: RESTORE_ALL 8 jmp irq_return -nmi_userspace: - GET_THREAD_INFO(%rcx) - movl TI_flags(%rcx),%ebx - andl $_TIF_WORK_MASK,%ebx - jz nmi_swapgs - movq %rsp,%rdi /* &pt_regs */ - call sync_regs - movq %rax,%rsp /* switch stack for scheduling */ - testl $_TIF_NEED_RESCHED,%ebx - jnz nmi_schedule - movl %ebx,%edx /* arg3: thread flags */ - ENABLE_INTERRUPTS(CLBR_NONE) - xorl %esi,%esi /* arg2: oldset */ - movq %rsp,%rdi /* arg1: &pt_regs */ - call do_notify_resume - DISABLE_INTERRUPTS(CLBR_NONE) - jmp nmi_userspace -nmi_schedule: - ENABLE_INTERRUPTS(CLBR_ANY) - call schedule - DISABLE_INTERRUPTS(CLBR_ANY) - jmp nmi_userspace CFI_ENDPROC -#else - jmp paranoid_exit - CFI_ENDPROC -#endif END(nmi) ENTRY(ignore_sysret)
Signature · 1 line
1.7.7.3
RFC PATCH v2 2/5 x86: Document the NMI handler about not using paranoid_exit
In reply to Steven RostedtFrom: Steven Rostedt <[email protected]>
Linus cleaned up the NMI handler but it still needs some comments to explain why it uses save_paranoid but not paranoid_exit. Just to keep others from adding that in the future, document why it's not used.
--- arch/x86/kernel/entry_64.S | 9 ++++++++- 1 files changed, 8 insertions(+), 1 deletions(-)
arch/x86/kernel/entry_64.S +8 -1
@@ -1480,9 +1480,16 @@ END(error_exit) ENTRY(nmi) INTR_FRAME PARAVIRT_ADJUST_EXCEPTION_FRAME - pushq_cfi $-1 + pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */ subq $ORIG_RAX-R15, %rsp CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15 + /* + * Use save_paranoid to handle SWAPGS, but no need to use paranoid_exit + * as we should not be calling schedule in NMI context. + * Even with normal interrupts enabled. An NMI should not be + * setting NEED_RESCHED or anything that normal interrupts and + * exceptions might do. + */ call save_paranoid DEFAULT_FRAME 0 /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
Signature · 1 line
1.7.7.3
RFC PATCH v2 3/5 x86: Add workaround to NMI iret woes
In reply to Steven RostedtFrom: Steven Rostedt <[email protected]>
In x86, when an NMI goes off, the CPU goes into an NMI context that prevents other NMIs to trigger on that CPU. If an NMI is suppose to trigger, it has to wait till the previous NMI leaves NMI context. At that time, the next NMI can trigger (note, only one more NMI will trigger, as only one can be latched at a time).
The way x86 gets out of NMI context is by calling iret. The problem with this is that this causes problems if the NMI handle either triggers an exception, or a breakpoint. Both the exception and the breakpoint handlers will finish with an iret. If this happens while in NMI context, the CPU will leave NMI context and a new NMI may come in. As NMI handlers are not made to be re-entrant, this can cause havoc with the system, not to mention, the nested NMI will write all over the previous NMI's stack.
Linus Torvalds proposed the following workaround to this problem:
https://lkml.org/lkml/2010/7/14/264
"In fact, I wonder if we couldn't just do a software NMI disable instead? Hav ea per-cpu variable (in the _core_ percpu areas that get allocated statically) that points to the NMI stack frame, and just make the NMI code itself do something like
NMI entry:
- load percpu NMI stack frame pointer
- if non-zero we know we're nested, and should ignore this NMI:
- we're returning to kernel mode, so return immediately by using
"popf/ret", which also keeps NMI's disabled in the hardware until the "real" NMI iret happens.
- before the popf/iret, use the NMI stack pointer to make the NMI
return stack be invalid and cause a fault
- set the NMI stack pointer to the current stack pointer
NMI exit (not the above "immediate exit because we nested"):
clear the percpu NMI stack pointer
Just do the iret.
Now, the thing is, now the "iret" is atomic. If we had a nested NMI, we'll take a fault, and that re-does our "delayed" NMI - and NMI's will stay masked.
And if we didn't have a nested NMI, that iret will now unmask NMI's, and everything is happy."
I first tried to follow this advice but as I started implementing this code, a few gotchas showed up.
One, is accessing per-cpu variables in the NMI handler.
The problem is that per-cpu variables use the %gs register to get the variable for the given CPU. But as the NMI may happen in userspace, we must first perform a SWAPGS to get to it. The NMI handler already does this later in the code, but its too late as we have saved off all the registers and we don't want to do that for a disabled NMI.
Peter Zijlstra suggested to keep all variables on the stack. This simplifies things greatly and it has the added benefit of cache locality.
Two, faulting on the iret.
I really wanted to make this work, but it was becoming very hacky, and I never got it to be stable. The iret already had a fault handler for userspace faulting with bad segment registers, and getting NMI to trigger a fault and detect it was very tricky. But for strange reasons, the system would usually take a double fault and crash. I never figured out why and decided to go with a simple "jmp" approach. The new approach I took also simplified things.
Finally, the last problem with Linus's approach was to have the nested NMI handler do a ret instead of an iret to give the first NMI NMI-context again.
The problem is that ret is much more limited than an iret. I couldn't figure out how to get the stack back where it belonged. I could have copied the current stack, pushed the return onto it, but my fear here is that there may be some place that writes data below the stack pointer. I know that is not something code should depend on, but I don't want to chance it. I may add this feature later, but for now, an NMI handler that loses NMI context will not get it back.
Here's what is done:
When an NMI comes in, the HW pushes the interrupt stack frame onto the per cpu NMI stack that is selected by the IST.
A special location on the NMI stack holds a variable that is set when the first NMI handler runs. If this variable is set then we know that this is a nested NMI and we process the nested NMI code.
There is still a race when this variable is cleared and an NMI comes in just before the first NMI does the return. For this case, if the variable is cleared, we also check if the interrupted stack is the NMI stack. If it is, then we process the nested NMI code.
Why the two tests and not just test the interrupted stack?
If the first NMI hits a breakpoint and loses NMI context, and then it hits another breakpoint and while processing that breakpoint we get a nested NMI. When processing a breakpoint, the stack changes to the breakpoint stack. If another NMI comes in here we can't rely on the interrupted stack to be the NMI stack.
If the variable is not set and the interrupted task's stack is not the NMI stack, then we know this is the first NMI and we can process things normally. But in order to do so, we need to do a few things first.
1) Set the stack variable that tells us that we are in an NMI handler
2) Make two copies of the interrupt stack frame.
One copy is used to return on iret
The other is used to restore the first one if we have a nested NMI.
This is what the stack will look like:
+-------------------------+ | original SS | | original Return RSP | | original RFLAGS | | original CS | | original RIP | +-------------------------+ | temp storage for rdx | +-------------------------+ | NMI executing variable | +-------------------------+ | Saved SS | | Saved Return RSP | | Saved RFLAGS | | Saved CS | | Saved RIP | +-------------------------+ | copied SS | | copied Return RSP | | copied RFLAGS | | copied CS | | copied RIP | +-------------------------+ | pt_regs | +-------------------------+
The original stack frame contains what the HW put in when we entered the NMI.
We store %rdx as a temp variable to use. Both the original HW stack frame and this %rdx storage will be clobbered by nested NMIs so we can not rely on them later in the first NMI handler.
The next item is the special stack variable that is set when we execute the rest of the NMI handler.
Then we have two copies of the interrupt stack. The second copy is modified by any nested NMIs to let the first NMI know that we triggered a second NMI (latched) and that we should repeat the NMI handler.
If the first NMI hits an exception or breakpoint that takes it out of NMI context, if a second NMI comes in before the first one finishes, it will update the copied interrupt stack to point to a fix up location to trigger another NMI.
When the first NMI calls iret, it will instead jump to the fix up location. This fix up location will copy the saved interrupt stack back to the copy and execute the nmi handler again.
Note, the nested NMI knows enough to check if it preempted a previous NMI handler while it is in the fixup location. If it has, it will not modify the copied interrupt stack and will just leave as if nothing happened. As the NMI handle is about to execute again, there's no reason to latch now.
To test all this, I forced the NMI handler to call iret and take itself out of NMI context. I also added assemble code to write to the serial to make sure that it hits the nested path as well as the fix up path. Everything seems to be working fine.
--- arch/x86/kernel/entry_64.S | 167 ++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 167 insertions(+), 0 deletions(-)
arch/x86/kernel/entry_64.S +167 -0
@@ -1475,11 +1475,164 @@ ENTRY(error_exit) CFI_ENDPROC END(error_exit) +/* + * Test if a given stack is an NMI stack or not. + */ + .macro test_in_nmi reg stack nmi_ret normal_ret + cmpq %\reg, \stack + ja \normal_ret + subq $EXCEPTION_STKSZ, %\reg + cmpq %\reg, \stack + jb \normal_ret + jmp \nmi_ret + .endm /* runs on exception stack */ ENTRY(nmi) INTR_FRAME PARAVIRT_ADJUST_EXCEPTION_FRAME + /* + * We allow breakpoints in NMIs. If a breakpoint occurs, then + * the iretq it performs will take us out of NMI context. + * This means that we can have nested NMIs where the next + * NMI is using the top of the stack of the previous NMI. We + * can't let it execute because the nested NMI will corrupt the + * stack of the previous NMI. NMI handlers are not re-entrant + * anyway. + * + * To handle this case we do the following: + * Check the a special location on the stack that contains + * a variable that is set when NMIs are executing. + * The interrupted task's stack is also checked to see if it + * is an NMI stack. + * If the variable is not set and the stack is not the NMI + * stack then: + * o Set the special variable on the stack + * o Copy the interrupt frame into a "saved" location on the stack + * o Copy the interrupt frame into a "copy" location on the stack + * o Continue processing the NMI + * If the variable is set or the previous stack is the NMI stack: + * o Modify the "copy" location to jump to the repeate_nmi + * o return back to the first NMI + * + * Now on exit of the first NMI, we first clear the stack variable + * The NMI stack will tell any nested NMIs at that point that it is + * nested. Then we pop the stack normally with iret, and if there was + * a nested NMI that updated the copy interrupt stack frame, a + * jump will be made to the repeat_nmi code that will handle the second + * NMI. + */ + + /* Use %rdx as out temp variable throughout */ + pushq_cfi %rdx + + /* + * Check the special variable on the stack to see if NMIs are + * executing. + */ + cmp $1, -8(%rsp) + je nested_nmi + + /* + * Now test if the previous stack was an NMI stack. + * We need the double check. We check the NMI stack to satisfy the + * race when the first NMI clears the variable before returning. + * We check the variable because the first NMI could be in a + * breakpoint routine using a breakpoint stack. + */ + lea 6*8(%rsp), %rdx + test_in_nmi rdx, 4*8(%rsp), nested_nmi, first_nmi + +nested_nmi: + /* + * Do nothing if we interrupted the fixup in repeat_nmi. + * It's about to repeat the NMI handler, so we are fine + * with ignoring this one. + */ + movq $repeat_nmi, %rdx + cmpq 8(%rsp), %rdx + ja 1f + movq $end_repeat_nmi, %rdx + cmpq 8(%rsp), %rdx + ja nested_nmi_out + +1: + /* Set up the interrupted NMIs stack to jump to repeat_nmi */ + leaq -6*8(%rsp), %rdx + movq %rdx, %rsp + pushq $__KERNEL_DS + pushq %rdx + pushfq + pushq $__KERNEL_CS + pushq_cfi $repeat_nmi + + /* Put stack back */ + addq $(11*8), %rsp + +nested_nmi_out: + popq_cfi %rdx + + /* No need to check faults here */ + INTERRUPT_RETURN + +first_nmi: + /* + * Because nested NMIs will use the pushed location that we + * stored rdx, we must keep that space available. + * Here's what our stack frame will look like: + * +-------------------------+ + * | original SS | + * | original Return RSP | + * | original RFLAGS | + * | original CS | + * | original RIP | + * +-------------------------+ + * | temp storage for rdx | + * +-------------------------+ + * | NMI executing variable | + * +-------------------------+ + * | Saved SS | + * | Saved Return RSP | + * | Saved RFLAGS | + * | Saved CS | + * | Saved RIP | + * +-------------------------+ + * | copied SS | + * | copied Return RSP | + * | copied RFLAGS | + * | copied CS | + * | copied RIP | + * +-------------------------+ + * | pt_regs | + * +-------------------------+ + * + * The saved RIP is used to fix up the copied RIP that a nested + * NMI may zero out. The original stack frame and the temp storage + * is also used by nested NMIs and can not be trusted on exit. + */ + /* Set the NMI executing variable on the stack. */ + pushq_cfi $1 + + /* Copy the stack frame to the Saved frame */ + .rept 5 + pushq_cfi 6*8(%rsp) + .endr + + /* Make another copy, this one may be modified by nested NMIs */ + .rept 5 + pushq_cfi 4*8(%rsp) + .endr + + /* Do not pop rdx, nested NMIs will corrupt it */ + movq 11*8(%rsp), %rdx + + /* + * Everything below this point can be preempted by a nested + * NMI if the first NMI took an exception. Repeated NMIs + * caused by an exception and nested NMI will start here, and + * can still be preempted by another NMI. + */ +restart_nmi: pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */ subq $ORIG_RAX-R15, %rsp CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
@@ -1502,10 +1655,24 @@ nmi_swapgs: SWAPGS_UNSAFE_STACK nmi_restore: RESTORE_ALL 8 + /* Clear the NMI executing stack variable */ + movq $0, 10*8(%rsp) jmp irq_return CFI_ENDPROC END(nmi) +repeat_nmi: + /* Update the stack variable to say we are still in NMI */ + movq $1, 5*8(%rsp) + + /* copy the saved stack back to copy stack */ + .rept 5 + pushq 4*8(%rsp) + .endr + + jmp restart_nmi +end_repeat_nmi: + ENTRY(ignore_sysret) CFI_STARTPROC mov $-ENOSYS,%eax
Signature · 1 line
1.7.7.3
RFC PATCH v2 4/5 x86: Keep current stack in NMI breakpoints
In reply to Steven RostedtFrom: Steven Rostedt <[email protected]>
We want to allow NMI handlers to have breakpoints to be able to remove stop_machine from ftrace, kprobes and jump_labels. But if an NMI interrupts a current breakpoint, and then it triggers a breakpoint itself, it will switch to the breakpoint stack and corrupt the data on it for the breakpoint processing that it interrupted.
Instead, have the NMI check if it interrupted breakpoint processing by checking if the stack that is currently used is a breakpoint stack. If it is, then load a special IDT that changes the IST for the debug exception to keep the same stack in kernel context. When the NMI is done, it puts it back.
This way, if the NMI does trigger a breakpoint, it will keep using the same stack and not stomp on the breakpoint data for the breakpoint it interrupted.
--- arch/x86/include/asm/desc.h | 12 ++++++++++++ arch/x86/include/asm/processor.h | 6 ++++++ arch/x86/kernel/cpu/common.c | 22 ++++++++++++++++++++++ arch/x86/kernel/head_64.S | 4 ++++ arch/x86/kernel/nmi.c | 15 +++++++++++++++ arch/x86/kernel/traps.c | 6 ++++++ 6 files changed, 65 insertions(+), 0 deletions(-)
arch/x86/include/asm/desc.h +12 -0
@@ -35,6 +35,8 @@ static inline void fill_ldt(struct desc_struct *desc, const struct user_desc *inextern struct desc_ptr idt_descr;extern gate_desc idt_table[]; +extern struct desc_ptr nmi_idt_descr; +extern gate_desc nmi_idt_table[];struct gdt_page {struct desc_struct gdt[GDT_ENTRIES ];
@@ -307,6 +309,16 @@ static inline void set_desc_limit(struct desc_struct *desc, unsigned long limit) desc->limit = (limit >>16 ) &0xf ; } +#ifdef CONFIG_X86_64 +static inline void set_nmi_gate (int gate,void *addr) +{ +gate_desc s; + +pack_gate (&s,GATE_INTERRUPT , (unsigned long )addr,0 ,0 , __KERNEL_CS); +write_idt_entry (nmi_idt_table, gate, &s); +} +#endif +static inline void _set_gate (int gate,unsigned type,void *addr,unsigned dpl,unsigned ist,unsigned seg) {
arch/x86/include/asm/processor.h +6 -0
@@ -402,6 +402,9 @@ DECLARE_PER_CPU(char *, irq_stack_ptr); DECLARE_PER_CPU(unsigned int, irq_count);extern unsigned long kernel_eflags; extern asmlinkage void ignore_sysret(void); +int is_debug_stack (unsigned long addr); +void zero_debug_stack (void ); +void reset_debug_stack (void );#else /* X86_64 */ #ifdef CONFIG_CC_STACKPROTECTOR /*
@@ -416,6 +419,9 @@ struct stack_canary { }; DECLARE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);#endif +static inline int is_debug_stack (unsigned long addr) {return 0 ; } +static inline void zero_debug_stack (void ) { } +static inline void reset_debug_stack (void ) { }#endif /* X86_64 */ extern unsigned int xstate_size;
arch/x86/kernel/cpu/common.c +22 -0
@@ -1026,6 +1026,8 @@ __setup("clearcpuid=", setup_disablecpuid);#ifdef CONFIG_X86_64 struct desc_ptr idt_descr = {NR_VECTORS *16 -1 , (unsigned long ) idt_table }; +struct desc_ptr nmi_idt_descr = {NR_VECTORS *16 -1 , + (unsigned long ) nmi_idt_table }; DEFINE_PER_CPU_FIRST(union irq_stack_union, irq_stack_union)__aligned(PAGE_SIZE) ;
@@ -1090,6 +1092,24 @@ unsigned long kernel_eflags;*/ DEFINE_PER_CPU(struct orig_ist, orig_ist); +static DEFINE_PER_CPU(unsigned long, debug_stack_addr); + +int is_debug_stack (unsigned long addr) +{ +return addr <=__get_cpu_var (debug_stack_addr) && + addr > (__get_cpu_var (debug_stack_addr) -DEBUG_STKSZ ); +} + +void zero_debug_stack (void ) +{ +load_idt ((const struct desc_ptr *)&nmi_idt_descr); +} + +void reset_debug_stack (void ) +{ +load_idt ((const struct desc_ptr *)&idt_descr); +} +#else /* CONFIG_X86_64 */ DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
@@ -1208,6 +1228,8 @@ void __cpuinit cpu_init(void) estacks += exception_stack_sizes[v]; oist->ist[v] = t->x86_tss.ist[v] = (unsigned long )estacks; +if (v ==DEBUG_STACK -1 ) +per_cpu (debug_stack_addr, cpu) = (unsigned long )estacks; } }
arch/x86/kernel/head_64.S +4 -0
@@ -417,6 +417,10 @@ ENTRY(phys_base) ENTRY(idt_table) .skip IDT_ENTRIES * 16 + .align L1_CACHE_BYTES +ENTRY(nmi_idt_table) + .skip IDT_ENTRIES * 16 + __PAGE_ALIGNED_BSS .align PAGE_SIZE ENTRY(empty_zero_page)
arch/x86/kernel/nmi.c +15 -0
@@ -407,6 +407,18 @@ static notrace __kprobes void default_do_nmi(struct pt_regs *regs) dotraplinkage notrace __kprobes voiddo_nmi (struct pt_regs *regs,long error_code) { +int update_debug_stack =0 ; + +/* +* If we interrupted a breakpoint, it is possible that +* the nmi handler will have breakpoints too. We need to +* change the IDT such that breakpoints that happen here +* continue to use the NMI stack. +*/ +if (unlikely (is_debug_stack (regs->sp))) { +zero_debug_stack (); + update_debug_stack =1 ; + }nmi_enter ();inc_irq_stat (__nmi_count);
@@ -415,6 +427,9 @@ do_nmi(struct pt_regs *regs, long error_code)default_do_nmi (regs);nmi_exit (); + +if (unlikely (update_debug_stack)) +reset_debug_stack (); }void stop_nmi (void )
arch/x86/kernel/traps.c +6 -0
@@ -723,4 +723,10 @@ void __init trap_init(void)cpu_init (); x86_init.irqs.trap_init (); + +#ifdef CONFIG_X86_64 +memcpy (&nmi_idt_table, &idt_table,IDT_ENTRIES *16 ); +set_nmi_gate (1 , &debug); +set_nmi_gate (3 , &int3); +#endif }
Signature · 1 line
1.7.7.3
Re: [RFC][PATCH 4/5 v2] x86: Keep current stack in NMI breakpoints
Hi Steven,
Quoted 15 lines · * Steven Rostedt ([email protected]) wrote:
From: Steven Rostedt <[email protected]>
We want to allow NMI handlers to have breakpoints to be able to remove
stop_machinefrom ftrace, kprobes andjump_labels. But if an NMI interrupts a current breakpoint, and then it triggers a breakpoint itself, it will switch to the breakpoint stack and corrupt the data on it for the breakpoint processing that it interrupted.Instead, have the NMI check if it interrupted breakpoint processing by checking if the stack that is currently used is a breakpoint stack. If it is, then load a special IDT that changes the IST for the debug exception to keep the same stack in kernel context. When the NMI is done, it puts it back.
Quoted 3 lines
This way, if the NMI does trigger a breakpoint, it will keep using the same stack and not stomp on the breakpoint data for the breakpoint it interrupted.
What happens to the following sequence ?
- Hit a breakpoint.
- Execute an interrupt handler nesting over breakpoint handler (made possible by preempt_conditional_sti(regs) in do_int3()). (or take any kind of fault that switch the current stack)
- NMI fires, not detecting that it is nested over a breakpoint handler, thus potentially corrupting the DEBUG stack.
Instead of trying to detect if we nest on a stack to find out if we need to change the IDT, I would recommend to unconditionally switch the int3 IDT to use the current stack upon outermost NMI entry, and set it back to its usual behavior upon outermost NMI exit.
Thanks,
Mathieu
Quoted 180 lines
Suggested-by Peter Zijlstra <[email protected]>Signed-off-by Steven Rostedt <[email protected]>--- arch/x86/include/asm/desc.h | 12 ++++++++++++ arch/x86/include/asm/processor.h | 6 ++++++ arch/x86/kernel/cpu/common.c | 22 ++++++++++++++++++++++ arch/x86/kernel/head_64.S | 4 ++++ arch/x86/kernel/nmi.c | 15 +++++++++++++++ arch/x86/kernel/traps.c | 6 ++++++ 6 files changed, 65 insertions(+), 0 deletions(-)arch/x86/include/asm/desc.h +12
-0@@ -35,6 +35,8 @@ static inline void fill_ldt(struct desc_struct *desc, const struct user_desc *inextern struct desc_ptr idt_descr;extern gate_desc idt_table[]; +extern struct desc_ptr nmi_idt_descr; +extern gate_desc nmi_idt_table[];struct gdt_page {struct desc_struct gdt[GDT_ENTRIES ];@@ -307,6 +309,16 @@ static inline void set_desc_limit(struct desc_struct *desc, unsigned long limit) desc->limit = (limit >>16 ) &0xf ; } +#ifdef CONFIG_X86_64 +static inline void set_nmi_gate (int gate,void *addr) +{ +gate_desc s; + +pack_gate (&s,GATE_INTERRUPT , (unsigned long )addr,0 ,0 , __KERNEL_CS); +write_idt_entry (nmi_idt_table, gate, &s); +} +#endif +static inline void _set_gate (int gate,unsigned type,void *addr,unsigned dpl,unsigned ist,unsigned seg) {arch/x86/include/asm/processor.h +6
-0@@ -402,6 +402,9 @@ DECLARE_PER_CPU(char *, irq_stack_ptr); DECLARE_PER_CPU(unsigned int, irq_count);extern unsigned long kernel_eflags; extern asmlinkage void ignore_sysret(void); +int is_debug_stack (unsigned long addr); +void zero_debug_stack (void ); +void reset_debug_stack (void );#else /* X86_64 */ #ifdef CONFIG_CC_STACKPROTECTOR /* @@ -416,6 +419,9 @@ struct stack_canary { }; DECLARE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);#endif +static inline int is_debug_stack (unsigned long addr) {return 0 ; } +static inline void zero_debug_stack (void ) { } +static inline void reset_debug_stack (void ) { }#endif /* X86_64 */ extern unsigned int xstate_size;arch/x86/kernel/cpu/common.c +22
-0@@ -1026,6 +1026,8 @@ __setup("clearcpuid=", setup_disablecpuid);#ifdef CONFIG_X86_64 struct desc_ptr idt_descr = {NR_VECTORS *16 -1 , (unsigned long ) idt_table }; +struct desc_ptr nmi_idt_descr = {NR_VECTORS *16 -1 , + (unsigned long ) nmi_idt_table }; DEFINE_PER_CPU_FIRST(union irq_stack_union, irq_stack_union)__aligned(PAGE_SIZE) ;@@ -1090,6 +1092,24 @@ unsigned long kernel_eflags;*/ DEFINE_PER_CPU(struct orig_ist, orig_ist); +static DEFINE_PER_CPU(unsigned long, debug_stack_addr); + +int is_debug_stack (unsigned long addr) +{ +return addr <=__get_cpu_var (debug_stack_addr) && + addr > (__get_cpu_var (debug_stack_addr) -DEBUG_STKSZ ); +} + +void zero_debug_stack (void ) +{ +load_idt ((const struct desc_ptr *)&nmi_idt_descr); +} + +void reset_debug_stack (void ) +{ +load_idt ((const struct desc_ptr *)&idt_descr); +} +#else /* CONFIG_X86_64 */ DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;@@ -1208,6 +1228,8 @@ void __cpuinit cpu_init(void) estacks += exception_stack_sizes[v]; oist->ist[v] = t->x86_tss.ist[v] = (unsigned long )estacks; +if (v ==DEBUG_STACK -1 ) +per_cpu (debug_stack_addr, cpu) = (unsigned long )estacks; } }arch/x86/kernel/head_64.S +4
-0@@ -417,6 +417,10 @@ ENTRY(phys_base) ENTRY(idt_table) .skip IDT_ENTRIES * 16 + .align L1_CACHE_BYTES +ENTRY(nmi_idt_table) + .skip IDT_ENTRIES * 16 + __PAGE_ALIGNED_BSS .align PAGE_SIZE ENTRY(empty_zero_page)arch/x86/kernel/nmi.c +15
-0@@ -407,6 +407,18 @@ static notrace __kprobes void default_do_nmi(struct pt_regs *regs) dotraplinkage notrace __kprobes voiddo_nmi (struct pt_regs *regs,long error_code) { +int update_debug_stack =0 ; + +/* +* If we interrupted a breakpoint, it is possible that +* the nmi handler will have breakpoints too. We need to +* change the IDT such that breakpoints that happen here +* continue to use the NMI stack. +*/ +if (unlikely (is_debug_stack (regs->sp))) { +zero_debug_stack (); + update_debug_stack =1 ; + }nmi_enter ();inc_irq_stat (__nmi_count);@@ -415,6 +427,9 @@ do_nmi(struct pt_regs *regs, long error_code)default_do_nmi (regs);nmi_exit (); + +if (unlikely (update_debug_stack)) +reset_debug_stack (); }void stop_nmi (void )arch/x86/kernel/traps.c +6
-0@@ -723,4 +723,10 @@ void __init trap_init(void)cpu_init (); x86_init.irqs.trap_init (); + +#ifdef CONFIG_X86_64 +memcpy (&nmi_idt_table, &idt_table,IDT_ENTRIES *16 ); +set_nmi_gate (1 , &debug); +set_nmi_gate (3 , &int3); +#endif }Signature · 1 line
1.7.7.3
Signature · 4 lines
Mathieu Desnoyers Operating System Efficiency R&D Consultant EfficiOS Inc. http://www.efficios.com
Re: [RFC][PATCH 4/5 v2] x86: Keep current stack in NMI breakpoints
Quoted 8 lines · Mathieu Desnoyers
What happens to the following sequence ?
- Hit a breakpoint.
- Execute an interrupt handler nesting over breakpoint handler (made possible bypreempt_conditional_sti(regs)indo_int3()). (or take any kind of fault that switch the current stack)
- NMI fires, not detecting that it is nested over a breakpoint handler, thus potentially corrupting the DEBUG stack.
Hmm, interesting, because if the interrupt hits a breakpoint, it too will cause the corruption of the breakpoint stack. I guess this is what the funky code is trying to protect in the breakpoint assembly, which it still currently has a race condition too.
Quoted 5 lines
Instead of trying to detect if we nest on a stack to find out if we need to change the IDT, I would recommend to unconditionally switch the int3 IDT to use the current stack upon outermost NMI entry, and set it back to its usual behavior upon outermost NMI exit.
I really hate to add overhead to all cases for the most unlikely case. What would be better, is to fix it in the most unlikely case code, which would be in the breakpoint handler.
I've been pushing that I don't want the NMI ugliness to spread to other parts of the code, but this does not have to do with just NMIs. Now we are having the breakpoint ugliness spread to NMI and vice versa. The real solution would be to change the do_int3() code.
We could add another IDT table for breakpoints, and before calling the preempt_conditional_sti(), we update it. This breakpoint IDT will not change the stack for interrupts. We can still allow NMIs to change, as that is taken care of. This not only fixes the issue you brought up, but it also fixes the race that exists today, and we can remove the ugly code in the int3 assembly.
-- Steve
Re: [RFC][PATCH 4/5 v2] x86: Keep current stack in NMI breakpoints
In reply to Mathieu DesnoyersQuoted 9 lines · Mathieu Desnoyers
What happens to the following sequence ?
- Hit a breakpoint.
- Execute an interrupt handler nesting over breakpoint handler (made possible bypreempt_conditional_sti(regs)indo_int3()). (or take any kind of fault that switch the current stack)
- NMI fires, not detecting that it is nested over a breakpoint handler, thus potentially corrupting the DEBUG stack.
Quoted 4 lines
Instead of trying to detect if we nest on a stack to find out if we need to change the IDT, I would recommend to unconditionally switch the int3 IDT to use the current stack upon outermost NMI entry, and set it back to its usual behavior upon outermost NMI exit.
There's also this simple patch to guarantee that NMIs know the debug stack is in use:
arch/x86/include/asm/processor.h +4 -0
@@ -402,6 +402,8 @@ DECLARE_PER_CPU(char *, irq_stack_ptr); DECLARE_PER_CPU(unsigned int, irq_count);extern unsigned long kernel_eflags; extern asmlinkage void ignore_sysret(void); +void inc_debug_stack_usage (void ); +void dec_debug_stack_usage (void );int is_debug_stack (unsigned long addr);void zero_debug_stack (void );void reset_debug_stack (void );
@@ -420,6 +422,8 @@ struct stack_canary { DECLARE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);#endif static inline int is_debug_stack (unsigned long addr) {return 0 ; } +static inline void inc_debug_stack_usage (void ) { } +static inline void dec_debug_stack_usage (void ) { }static inline void zero_debug_stack (void ) { }static inline void reset_debug_stack (void ) { }#endif /* X86_64 */
arch/x86/kernel/cpu/common.c +14 -2
@@ -1093,11 +1093,23 @@ unsigned long kernel_eflags; DEFINE_PER_CPU(struct orig_ist, orig_ist); static DEFINE_PER_CPU(unsigned long, debug_stack_addr); +static DEFINE_PER_CPU(int, debug_stack_usage); + +void inc_debug_stack_usage (void ) +{ +__get_cpu_var (debug_stack_usage)++; +} + +void dec_debug_stack_usage (void ) +{ +__get_cpu_var (debug_stack_usage)--; +}int is_debug_stack (unsigned long addr) { -return addr <=__get_cpu_var (debug_stack_addr) && - addr > (__get_cpu_var (debug_stack_addr) -DEBUG_STKSZ ); +return __get_cpu_var (debug_stack_usage) || + (addr <=__get_cpu_var (debug_stack_addr) && + addr > (__get_cpu_var (debug_stack_addr) -DEBUG_STKSZ )); }void zero_debug_stack (void )
arch/x86/kernel/traps.c +14 -0
@@ -316,9 +316,15 @@ dotraplinkage void __kprobes do_int3(struct pt_regs *regs, long error_code)return ;#endif +/* +* Let others (NMI) know that the debug stack is in use +* as we may switch to the interrupt stack. +*/ +inc_debug_stack_usage ();preempt_conditional_sti (regs);do_trap (3 ,SIGTRAP ,"int3" , regs, error_code,NULL );preempt_conditional_cli (regs); +dec_debug_stack_usage (); }#ifdef CONFIG_X86_64
@@ -411,6 +417,12 @@ dotraplinkage void __kprobes do_debug(struct pt_regs *regs, long error_code) SIGTRAP) == NOTIFY_STOP)return ; +/* +* Let others (NMI) know that the debug stack is in use +* as we may switch to the interrupt stack. +*/ +inc_debug_stack_usage (); +/* It's safe to allow irq's after DR6 has been saved */ preempt_conditional_sti (regs);
@@ -418,6 +430,7 @@ dotraplinkage void __kprobes do_debug(struct pt_regs *regs, long error_code)handle_vm86_trap ((struct kernel_vm86_regs *) regs, error_code,1 );preempt_conditional_cli (regs); +dec_debug_stack_usage ();return ; }
@@ -437,6 +450,7 @@ dotraplinkage void __kprobes do_debug(struct pt_regs *regs, long error_code)if (tsk->thread.debugreg6 & (DR_STEP |DR_TRAP_BITS ) || user_icebp)send_sigtrap (tsk, regs, error_code, si_code);preempt_conditional_cli (regs); +dec_debug_stack_usage ();return ; }
[tip:perf/core] x86: Add counter when debug stack is used with interrupts enabled
Commit-ID: 42181186ad4db986fcaa40ca95c6e407e9e79372 Gitweb: http://git.kernel.org/tip/42181186ad4db986fcaa40ca95c6e407e9e79372 Author: Steven Rostedt <[email protected]> AuthorDate: Fri, 16 Dec 2011 11:43:02 -0500 Committer: Steven Rostedt <[email protected]> CommitDate: Wed, 21 Dec 2011 15:38:56 -0500
x86: Add counter when debug stack is used with interrupts enabled
Mathieu Desnoyers pointed out a case that can cause issues with NMIs running on the debug stack:
int3 -> interrupt -> NMI -> int3
Because the interrupt changes the stack, the NMI will not see that it preempted the debug stack. Looking deeper at this case, interrupts only happen when the int3 is from userspace or in an a location in the exception table (fixup).
userspace -> int3 -> interurpt -> NMI -> int3
All other int3s that happen in the kernel should be processed without ever enabling interrupts, as the do_trap() call will panic the kernel if it is called to process any other location within the kernel.
Adding a counter around the sections that enable interrupts while using the debug stack allows the NMI to also check that case. If the NMI sees that it either interrupted a task using the debug stack or the debug counter is non-zero, then it will have to change the IDT table to make the int3 not change stacks (which will corrupt the stack if it does).
Note, I had to move the debug_usage functions out of processor.h and into debugreg.h because of the static inlined functions to inc and dec the debug_usage counter. __get_cpu_var() requires smp.h which includes processor.h, and would fail to build.
--- arch/x86/include/asm/debugreg.h | 22 ++++++++++++++++++++++ arch/x86/include/asm/processor.h | 6 ------ arch/x86/kernel/cpu/common.c | 6 ++++-- arch/x86/kernel/traps.c | 14 ++++++++++++++ 4 files changed, 40 insertions(+), 8 deletions(-)
arch/x86/include/asm/debugreg.h +22 -0
@@ -101,6 +101,28 @@ extern void aout_dump_debugregs(struct user *dump);extern void hw_breakpoint_restore (void ); +#ifdef CONFIG_X86_64 +DECLARE_PER_CPU (int, debug_stack_usage); +static inline void debug_stack_usage_inc (void ) +{ +__get_cpu_var (debug_stack_usage)++; +} +static inline void debug_stack_usage_dec (void ) +{ +__get_cpu_var (debug_stack_usage)--; +} +int is_debug_stack (unsigned long addr); +void debug_stack_set_zero (void ); +void debug_stack_reset (void ); +#else /* !X86_64 */ +static inline int is_debug_stack (unsigned long addr) {return 0 ; } +static inline void debug_stack_set_zero (void ) { } +static inline void debug_stack_reset (void ) { } +static inline void debug_stack_usage_inc (void ) { } +static inline void debug_stack_usage_dec (void ) { } +#endif /* X86_64 */ + +#endif /* __KERNEL__ */ #endif /* _ASM_X86_DEBUGREG_H */
arch/x86/include/asm/processor.h +0 -6
@@ -402,9 +402,6 @@ DECLARE_PER_CPU(char *, irq_stack_ptr); DECLARE_PER_CPU(unsigned int, irq_count);extern unsigned long kernel_eflags; extern asmlinkage void ignore_sysret(void); -int is_debug_stack (unsigned long addr); -void debug_stack_set_zero (void ); -void debug_stack_reset (void );#else /* X86_64 */ #ifdef CONFIG_CC_STACKPROTECTOR /*
@@ -419,9 +416,6 @@ struct stack_canary { }; DECLARE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);#endif -static inline int is_debug_stack (unsigned long addr) {return 0 ; } -static inline void debug_stack_set_zero (void ) { } -static inline void debug_stack_reset (void ) { }#endif /* X86_64 */ extern unsigned int xstate_size;
arch/x86/kernel/cpu/common.c +4 -2
@@ -1093,11 +1093,13 @@ unsigned long kernel_eflags; DEFINE_PER_CPU(struct orig_ist, orig_ist); static DEFINE_PER_CPU(unsigned long, debug_stack_addr); +DEFINE_PER_CPU (int , debug_stack_usage);int is_debug_stack (unsigned long addr) { -return addr <=__get_cpu_var (debug_stack_addr) && - addr > (__get_cpu_var (debug_stack_addr) -DEBUG_STKSZ ); +return __get_cpu_var (debug_stack_usage) || + (addr <=__get_cpu_var (debug_stack_addr) && + addr > (__get_cpu_var (debug_stack_addr) -DEBUG_STKSZ )); }void debug_stack_set_zero (void )
arch/x86/kernel/traps.c +14 -0
@@ -316,9 +316,15 @@ dotraplinkage void __kprobes do_int3(struct pt_regs *regs, long error_code)return ;#endif +/* +* Let others (NMI) know that the debug stack is in use +* as we may switch to the interrupt stack. +*/ +debug_stack_usage_inc ();preempt_conditional_sti (regs);do_trap (3 ,SIGTRAP ,"int3" , regs, error_code,NULL );preempt_conditional_cli (regs); +debug_stack_usage_dec (); }#ifdef CONFIG_X86_64
@@ -411,6 +417,12 @@ dotraplinkage void __kprobes do_debug(struct pt_regs *regs, long error_code) SIGTRAP) == NOTIFY_STOP)return ; +/* +* Let others (NMI) know that the debug stack is in use +* as we may switch to the interrupt stack. +*/ +debug_stack_usage_inc (); +/* It's safe to allow irq's after DR6 has been saved */ preempt_conditional_sti (regs);
@@ -418,6 +430,7 @@ dotraplinkage void __kprobes do_debug(struct pt_regs *regs, long error_code)handle_vm86_trap ((struct kernel_vm86_regs *) regs, error_code,1 );preempt_conditional_cli (regs); +debug_stack_usage_dec ();return ; }
@@ -437,6 +450,7 @@ dotraplinkage void __kprobes do_debug(struct pt_regs *regs, long error_code)if (tsk->thread.debugreg6 & (DR_STEP |DR_TRAP_BITS ) || user_icebp)send_sigtrap (tsk, regs, error_code, si_code);preempt_conditional_cli (regs); +debug_stack_usage_dec ();return ; }
RFC PATCH v2 5/5 x86: Allow NMIs to hit breakpoints in i386
In reply to Steven RostedtFrom: Steven Rostedt <[email protected]>
With i386, NMIs and breakpoints use the current stack and they do not reset the stack pointer to a fix point that might corrupt a previous NMI or breakpoint (as it does in x86_64). But NMIs are still not made to be re-entrant, and need to prevent the case that an NMI hitting a breakpoint (which does an iret), doesn't allow another NMI to run.
The fix is to let the NMI be in 3 different states:
1) not running
2) executing
3) latched
When no NMI is executing on a given CPU, the state is "not running". When the first NMI comes in, the state is switched to "executing". On exit of that NMI, a cmpxchg is performed to switch the state back to "not running" and if that fails, the NMI is restarted.
If a breakpoint is hit and does an iret, which re-enables NMIs, and another NMI comes in before the first NMI finished, it will detect that the state is not in the "not running" state and the current NMI is nested. In this case, the state is switched to "latched" to let the interrupted NMI know to restart the NMI handler, and the nested NMI exits without doing anything.
--- arch/x86/kernel/nmi.c | 100 +++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 93 insertions(+), 7 deletions(-)
arch/x86/kernel/nmi.c +93 -7
@@ -404,11 +404,84 @@ static notrace __kprobes void default_do_nmi(struct pt_regs *regs)unknown_nmi_error (reason, regs); } -dotraplinkage notrace __kprobes void -do_nmi (struct pt_regs *regs,long error_code) -{ -int update_debug_stack =0 ; +/* +* NMIs can hit breakpoints which will cause it to lose its +* NMI context with the CPU when the breakpoint does an iret. +*/ +#ifdef CONFIG_X86_32 +/* +* For i386, NMIs use the same stack as the kernel, and we can +* add a workaround to the iret problem in C. Simply have 3 states +* the NMI can be in. +* +* 1) not running +* 2) executing +* 3) latched +* +* When no NMI is in progress, it is in the "not running" state. +* When an NMI comes in, it goes into the "executing" state. +* Normally, if another NMI is triggered, it does not interrupt +* the running NMI and the HW will simply latch it so that when +* the first NMI finishes, it will restart the second NMI. +* (Note, the latch is binary, thus multiple NMIs triggering, +* when one is running, are ignored. Only one NMI is restarted.) +* +* If an NMI hits a breakpoint that executes an iret, another +* NMI can preempt it. We do not want to allow this new NMI +* to run, but we want to execute it when the first one finishes. +* We set the state to "latched", and the first NMI will perform +* an cmpxchg on the state, and if it doesn't successfully +* reset the state to "not running" it will restart the next +* NMI. +*/ +enum nmi_states { +NMI_NOT_RUNNING , +NMI_EXECUTING , +NMI_LATCHED , +}; +static DEFINE_PER_CPU(enum nmi_states, nmi_state); + +#define nmi_preprocess (regs) \ + do { \ + if (__get_cpu_var(nmi_state) != NMI_NOT_RUNNING) { \ + __get_cpu_var(nmi_state) = NMI_LATCHED; \ + return; \ + } \ + nmi_restart: \ + __get_cpu_var(nmi_state) = NMI_EXECUTING; \ + } while (0) + +#define nmi_postprocess () \ + do { \ + if (cmpxchg(&__get_cpu_var(nmi_state), \ + NMI_EXECUTING, NMI_NOT_RUNNING) != NMI_EXECUTING) \ + goto nmi_restart; \ + } while (0) +#else /* x86_64 */ +/* +* In x86_64 things are a bit more difficult. This has the same problem +* where an NMI hitting a breakpoint that calls iret will remove the +* NMI context, allowing a nested NMI to enter. What makes this more +* difficult is that both NMIs and breakpoints have their own stack. +* When a new NMI or breakpoint is executed, the stack is set to a fixed +* point. If an NMI is nested, it will have its stack set at that same +* fixed address that the first NMI had, and will start corrupting the +* stack. This is handled in entry_64.S, but the same problem exists with +* the breakpoint stack. +* +* If a breakpoint is being processed, and the debug stack is being used, +* if an NMI comes in and also hits a breakpoint, the stack pointer +* will be set to the same fixed address as the breakpoint that was +* interrupted, causing that stack to be corrupted. To handle this case, +* check if the stack that was interrupted is the debug stack, and if +* so, change the IDT so that new breakpoints will use the current stack +* and not switch to the fixed address. On return of the NMI, switch back +* to the original IDT. +*/ +static DEFINE_PER_CPU(int, update_debug_stack); +static inline void nmi_preprocess (struct pt_regs *regs) +{/* * If we interrupted a breakpoint, it is possible that * the nmi handler will have breakpoints too. We need to
@@ -417,8 +490,22 @@ do_nmi(struct pt_regs *regs, long error_code)*/ if (unlikely (is_debug_stack (regs->sp))) {zero_debug_stack (); - update_debug_stack =1 ; +__get_cpu_var (update_debug_stack) =1 ; } +} + +static inline void nmi_postprocess (void ) +{ +if (unlikely (__get_cpu_var (update_debug_stack))) +reset_debug_stack (); +} +#endif + +dotraplinkage notrace __kprobes void +do_nmi (struct pt_regs *regs,long error_code) +{ +nmi_preprocess (regs); +nmi_enter ();inc_irq_stat (__nmi_count);
@@ -428,8 +515,7 @@ do_nmi(struct pt_regs *regs, long error_code)nmi_exit (); -if (unlikely (update_debug_stack)) -reset_debug_stack (); +nmi_postprocess (); }void stop_nmi (void )
Signature · 1 line
1.7.7.3
Re: [RFC][PATCH 5/5 v2] x86: Allow NMIs to hit breakpoints in i386
* Steven Rostedt ([email protected]) wrote:
[...]> From: Steven Rostedt <[email protected]>
Quoted 17 lines
arch/x86/kernel/nmi.c +17
-0+#define nmi_preprocess (regs) \ + do { \ + if (__get_cpu_var(nmi_state) != NMI_NOT_RUNNING) { \ + __get_cpu_var(nmi_state) = NMI_LATCHED; \ + return; \ + } \ + nmi_restart: \ + __get_cpu_var(nmi_state) = NMI_EXECUTING; \ + } while (0) + +#define nmi_postprocess () \ + do { \ + if (cmpxchg(&__get_cpu_var(nmi_state), \ + NMI_EXECUTING, NMI_NOT_RUNNING) != NMI_EXECUTING) \ + goto nmi_restart; \ + } while (0) +
[...]
Quoted 9 lines
arch/x86/kernel/nmi.c +5
-0+dotraplinkage notrace __kprobes void +do_nmi (struct pt_regs *regs,long error_code) +{ +nmi_preprocess (regs); +nmi_enter ();inc_irq_stat (__nmi_count);
arch/x86/kernel/nmi.c do_nmi(struct pt_regs *regs, long error_code) · line 518
Quoted 6 lines
arch/x86/kernel/nmi.c +1
-2@@ -428,8 +515,7 @@ do_nmi(struct pt_regs *regs, long error_code)nmi_exit (); -if (unlikely (update_debug_stack)) -reset_debug_stack (); +nmi_postprocess ();
Just to make sure I understand: if an NMI nests over do_nmi between nmi_postprocess() and the following iret (in which case the CPU is in state NMI_NOT_RUNNING), we will end up with two NMI handlers nested on the stack, right ? Given that there is no upper-bound on the nesting level of this situation (although nesting like this more than once is extremely unlikely), is this side-effect something we should care about in terms of stack space usage ? Also, is the stack dump OOPS handler aware of this stack layout that was until now impossible ?
Thanks,
Mathieu
Quoted 7 lines
arch/x86/kernel/nmi.c +0
-0}void stop_nmi (void )Signature · 1 line
1.7.7.3
Signature · 4 lines
Mathieu Desnoyers Operating System Efficiency R&D Consultant EfficiOS Inc. http://www.efficios.com
Re: [RFC][PATCH 5/5 v2] x86: Allow NMIs to hit breakpoints in i386
Quoted 7 lines · Mathieu Desnoyers
Just to make sure I understand: if an NMI nests over
do_nmibetweennmi_postprocess()and the following iret (in which case the CPU is in stateNMI_NOT_RUNNING), we will end up with two NMI handlers nested on the stack, right ? Given that there is no upper-bound on the nesting level of this situation (although nesting like this more than once is extremely unlikely), is this side-effect something we should care about in terms of stack space usage ?
At that point, there's very little on the stack to begin with. Just the one irq frame, and saved regs, plus the stack frame of this function. If we are hitting that many NMIs to cause a stack overflow, then I believe there's more issues than the overflow itself. Say, a livelock of NMIs?
Quoted 2 lines
Also, is the stack dump OOPS handler
aware of this stack layout that was until now impossible ?
Since NMIs on i386 doesn't change the stack when interrupting the kernel, the OOPs handler never was aware of the NMI stack layout.
-- Steve
Re: [RFC][PATCH 5/5 v2] x86: Allow NMIs to hit breakpoints in i386
Quoted 10 lines · * Steven Rostedt ([email protected]) wrote:
Quoted 7 lines · Mathieu Desnoyers
Just to make sure I understand: if an NMI nests over
do_nmibetweennmi_postprocess()and the following iret (in which case the CPU is in stateNMI_NOT_RUNNING), we will end up with two NMI handlers nested on the stack, right ? Given that there is no upper-bound on the nesting level of this situation (although nesting like this more than once is extremely unlikely), is this side-effect something we should care about in terms of stack space usage ?
Quoted 4 lines
At that point, there's very little on the stack to begin with. Just the one irq frame, and saved regs, plus the stack frame of this function. If we are hitting that many NMIs to cause a stack overflow, then I believe there's more issues than the overflow itself. Say, a livelock of NMIs?
Yep, if it's small then it's fine I guess.
Quoted 6 lines
Quoted 3 lines
Also, is the stack dump OOPS handler
aware of this stack layout that was until now impossible ?Since NMIs on i386 doesn't change the stack when interrupting the kernel, the OOPs handler never was aware of the NMI stack layout.
Makes sense, sounds good,
Thanks!
Mathieu
Quoted 5 lines
-- Steve
Signature · 4 lines
Mathieu Desnoyers Operating System Efficiency R&D Consultant EfficiOS Inc. http://www.efficios.com
H. Peter Anvin
reviewerRe: [RFC][PATCH 5/5 v2] x86: Allow NMIs to hit breakpoints in i386
In reply to Steven RostedtQuoted 6 lines · Steven Rostedt
With i386, NMIs and breakpoints use the current stack and they do not reset the stack pointer to a fix point that might corrupt a previous NMI or breakpoint (as it does in
x86_64). But NMIs are still not made to be re-entrant, and need to prevent the case that an NMI hitting a breakpoint (which does an iret), doesn't allow another NMI to run.
Okay... what about the other way around: avoiding the IRET when invoked from an NMI context and therefore leaving NMI disabled until the appropriate time?
-hpa
Signature · 2 lines
H. Peter Anvin, Intel Open Source Technology Center I work for Intel. I don't speak on their behalf.
Re: [RFC][PATCH 5/5 v2] x86: Allow NMIs to hit breakpoints in i386
Quoted 11 lines · H. Peter Anvin
Quoted 6 lines · Steven Rostedt
With i386, NMIs and breakpoints use the current stack and they do not reset the stack pointer to a fix point that might corrupt a previous NMI or breakpoint (as it does in
x86_64). But NMIs are still not made to be re-entrant, and need to prevent the case that an NMI hitting a breakpoint (which does an iret), doesn't allow another NMI to run.Okay... what about the other way around: avoiding the IRET when invoked from an NMI context and therefore leaving NMI disabled until the appropriate time?
Linus was against this approach. He didn't want the ugliness of NMI to spread to other code, by adding ugly handlers around the 'iret' of breakpoints and exceptions.
-- Steve