x86/kexec: Improvements for FRED
cover x86/kexec: Improvements for FRED
Kexec with FRED happens to already function.
Looking at the logic, several unsafe actions are occuring. Fix the worst of them.
Andrew Cooper (4):
x86/kexec: Stop hooking NMIs with trap_nop() x86/kexec: Fix and expands comments for kexec_reloc() x86/kexec: Invalidate the IDT earlier in kexec_reloc() x86/kexec: Disable FRED earlier in kexec_reloc()
xen/arch/x86/crash.c | 20 +++++----- xen/arch/x86/x86_64/kexec_reloc.S | 62 ++++++++++++++++++++++--------- 2 files changed, 55 insertions(+), 27 deletions(-)
Signature · 1 line
2.39.5
PATCH 1/4 x86/kexec: Stop hooking NMIs with trap_nop()
When FRED is active, it is not possible to hook NMIs like this.
NMI hooking in the crash path has undergone several revisions since its introduction. Notably since commit e7f147bf4ac7 ("x86/crash: Drop manual hooking of exception_table[]") we use the regular nmi_callback() infrastructure.
Instead of asserting that we don't enter do_nmi_crash() on the crashing CPU, tolerate it and return early. It's a marginally longer codepath but behaves the same and is compatible with FRED.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
The other use of hooking the NMI handler like this is in play_dead() and introduced by commit 73cb1383bf8d ("x86/idle: re-arrange dead-idle handling"). It's unsafe, and the commit even mentions so for #MC.
On x86, we simply cannot free the per-cpu block for any CPU that hasn't been put back into the wait-for-SIPI state.
--- xen/arch/x86/crash.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-)
xen/arch/x86/crash.c +11 -9
@@ -37,14 +37,18 @@ static cpumask_t waiting_to_crash;static unsigned int crashing_cpu; static DEFINE_PER_CPU_READ_MOSTLY(bool, crash_save_done); -/* This becomes the NMI handler for non-crashing CPUs, when Xen is crashing. */ -static int noreturn cf_check do_nmi_crash( +/* This becomes the NMI handler for all CPUs when Xen is crashing. */ +static int cf_check do_nmi_crash(const struct cpu_user_regs *regs,int cpu) {stac (); -/* nmi_shootdown_cpus() should ensure that this assertion is correct. */ -ASSERT (cpu != crashing_cpu); +/* +* If we are the crashing CPU, do nothing. We need to get back to the +* interrupted codepath to contine with the kexec transition. +*/ +if ( cpu == crashing_cpu ) +return 1 ;/* Save crash information and shut down CPU. Attempt only once. */ if ( !this_cpu (crash_save_done) )
@@ -114,6 +118,8 @@ static int noreturn cf_check do_nmi_crash(for ( ; ; )halt (); + +unreachable (); }static void nmi_shootdown_cpus (void )
@@ -130,11 +136,7 @@ static void nmi_shootdown_cpus(void)cpumask_andnot (&waiting_to_crash, &cpu_online_map,cpumask_of (cpu)); -/* -* Disable IST for MCEs to avoid stack corruption race conditions, and -* change the NMI handler to a nop to avoid deviation from this codepath. -*/ -_set_gate_lower (&idt[X86_EXC_NMI ], SYS_DESC_irq_gate,0 , &trap_nop); +/* Disable IST for MCEs to avoid stack corruption race conditions */ set_ist (&idt[X86_EXC_MC ],IST_NONE );set_nmi_callback (do_nmi_crash);
Signature · 1 line
2.39.5
Re: [PATCH 1/4] x86/kexec: Stop hooking NMIs with trap_nop()
Quoted 7 lines · Andrew Cooper
When FRED is active, it is not possible to hook NMIs like this.
NMI hooking in the crash path has undergone several revisions since its introduction. Notably since commit
e7f147bf4ac7("x86/crash: Drop manual hooking of exception_table[]") we use the regularnmi_callback()infrastructure.
Quoted 5 lines
Instead of asserting that we don't enter
do_nmi_crash()on the crashing CPU, tolerate it and return early. It's a marginally longer codepath but behaves the same and is compatible with FRED.Signed-off-by Andrew Cooper <[email protected]>
Quoted 3 lines
The other use of hooking the NMI handler like this is in
play_dead()and introduced by commit73cb1383bf8d("x86/idle: re-arrange dead-idle handling"). It's unsafe, and the commit even mentions so for #MC.
Why is wiring to trap_nop() unsafe? There is a per-CPU access when PERF_COUNTERS=y, but otherwise?
For FRED, shouldn't do_nmi() then gain a similar early exit for offlined CPUs, replacing the IDT editing?
Quoted 2 lines
On x86, we simply cannot free the per-cpu block for any CPU that hasn't been put back into the wait-for-SIPI state.
Please remind me, is there a reason we can't put CPUs we have offlined (not parked) into that state?
Jan
Re: [PATCH 1/4] x86/kexec: Stop hooking NMIs with trap_nop()
Quoted 12 lines · Jan Beulich
Quoted 11 lines · Andrew Cooper
When FRED is active, it is not possible to hook NMIs like this.
NMI hooking in the crash path has undergone several revisions since its introduction. Notably since commit
e7f147bf4ac7("x86/crash: Drop manual hooking of exception_table[]") we use the regularnmi_callback()infrastructure.Instead of asserting that we don't enter
do_nmi_crash()on the crashing CPU, tolerate it and return early. It's a marginally longer codepath but behaves the same and is compatible with FRED.
Quoted 7 lines
Quoted 1 line
Signed-off-by Andrew Cooper <[email protected]>Reviewed-by Jan Beulich <[email protected]>Quoted 3 lines
The other use of hooking the NMI handler like this is in
play_dead()and introduced by commit73cb1383bf8d("x86/idle: re-arrange dead-idle handling"). It's unsafe, and the commit even mentions so for #MC.Why is wiring to
trap_nop()unsafe?
I meant unsafe in FRED for the same reason; trap_nop() doesn't make the NMI path safe.
Quoted 2 lines
For FRED, shouldn't
do_nmi()then gain a similar early exit for offlined CPUs, replacing the IDT editing?
That's not really good enough in FRED. For starters it doesn't cover entry_from_{pv,xen}().
e.g. I've discovered (the hard way) the problems of putting printk() ahead of NMI dispatch when testing NMIs.
And there's still an open question about #MC in both modes.
Quoted 5 lines
Quoted 3 lines
On x86, we simply cannot free the per-cpu block for any CPU that hasn't been put back into the wait-for-SIPI state.
Please remind me, is there a reason we can't put CPUs we have offlined (not parked) into that state?
INIT clears CR4.MCE. Any multi-target #MC (even non-fully-broadcast ones) which includes this CPU escalates to SHUTDOWN.
Also if you INIT any CPU the system doesn't like, you get SHUTDOWN too. Doing this to CPU0 is guaranteed to SHUTDOWN.
One fun issue I found doing the AMD Entrysign work was that firmware no longer hands over to the OS with the APs in the Wait-for-SIPI state; they're typically handed over in Mwait.
~Andrew
Re: [PATCH 1/4] x86/kexec: Stop hooking NMIs with trap_nop()
Quoted 9 lines · Andrew Cooper
Quoted 5 lines · Jan Beulich
Quoted 2 lines · Andrew Cooper
On x86, we simply cannot free the per-cpu block for any CPU that hasn't been put back into the wait-for-SIPI state.
Please remind me, is there a reason we can't put CPUs we have offlined (not parked) into that state?
INIT clears CR4.MCE. Any multi-target #MC (even non-fully-broadcast ones) which includes this CPU escalates to SHUTDOWN.
Wasn't there a difference between AMD and Intel? After all the above is why we park Intel CPUs.
Jan
Re: [PATCH 1/4] x86/kexec: Stop hooking NMIs with trap_nop()
Quoted 11 lines · Jan Beulich
Quoted 8 lines · Andrew Cooper
Quoted 5 lines · Jan Beulich
Quoted 2 lines · Andrew Cooper
On x86, we simply cannot free the per-cpu block for any CPU that hasn't been put back into the wait-for-SIPI state.
Please remind me, is there a reason we can't put CPUs we have offlined (not parked) into that state?
INIT clears CR4.MCE. Any multi-target #MC (even non-fully-broadcast ones) which includes this CPU escalates to SHUTDOWN.
Wasn't there a difference between AMD and Intel? After all the above is why we park Intel CPUs.
Intel is more prone to broadcasts, but AMD is not immune AIUI.
~Andrew
PATCH 2/4 x86/kexec: Fix and expands comments for kexec_reloc()
In reply to Andrew CooperThe order of shutdown is delicate. Explain things a little better.
Fix two comments about leaving Long Mode.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
--- xen/arch/x86/x86_64/kexec_reloc.S | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)
xen/arch/x86/x86_64/kexec_reloc.S +19 -4
@@ -27,6 +27,14 @@ .section .text.kexec, "ax", @progbits .code64 + /* + * kexec_reloc() is entered in it's natural position within Xen. + * + * A copy of .text.kexec is identity mapped at %rdi within the + * pagetables in %rsi. Xen's .text is mapped into %rsi at it's high + * alias allowing to pivot, but most other things including the stacks + * are not mapped. + */ FUNC(kexec_reloc, PAGE_SIZE) /* %rdi - code page maddr */ /* %rsi - page table maddr */
@@ -36,10 +44,17 @@ FUNC(kexec_reloc, PAGE_SIZE) movq %rcx, %rbp - /* Setup stack. */ + /* + * Move to the identity mapped stack. + * + * Note this mapping doesn't exist until the pagetable switch. + */ leaq (.Lreloc_stack_base - kexec_reloc)(%rdi), %rsp - /* Load reloc page table. */ + /* + * Move to the relocation pagetables. Xen's .text is mapped allowing + * the pivot, but very little else is. + */ movq %rsi, %cr3 /* Jump to identity mapped code. */
@@ -153,12 +168,12 @@ FUNC_LOCAL(compatibility_mode) movl %eax, %gs movl %eax, %ss - /* Disable paging and therefore leave 64 bit mode. */ + /* Disable paging and therefore leave long mode. */ movl %cr0, %eax andl $~X86_CR0_PG, %eax movl %eax, %cr0 - /* Disable long mode */ + /* Clear EFER.LME */ movl $MSR_EFER, %ecx rdmsr andl $~EFER_LME, %eax
Signature · 1 line
2.39.5
Re: [PATCH 2/4] x86/kexec: Fix and expands comments for kexec_reloc()
Quoted 5 lines · Andrew Cooper
The order of shutdown is delicate. Explain things a little better.
Fix two comments about leaving Long Mode.
Signed-off-by Andrew Cooper <[email protected]>
PATCH 3/4 x86/kexec: Invalidate the IDT earlier in kexec_reloc()
In reply to Andrew CooperAfter switching stack, it is not safe to run any exception handlers, because attempts to access the cpu_info block are out-of-bounds and will generate wild accesses.
Invalidating the IDT in the common path means there's no need to do so again in the 32bit path, so drop compat_mode_idt entirely.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
--- xen/arch/x86/x86_64/kexec_reloc.S | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-)
xen/arch/x86/x86_64/kexec_reloc.S +10 -7
@@ -44,6 +44,16 @@ FUNC(kexec_reloc, PAGE_SIZE) movq %rcx, %rbp + /* + * Invalidate the IDT. After switching off Xen's stacks, the + * exception handlers are unsafe to use, because there's no way to + * perform arithmetic on the stack pointer to find the cpu_info block. + */ + push $0 + pushw $0 + lidt (%rsp) + add $10, %rsp + /* * Move to the identity mapped stack. *
@@ -94,8 +104,6 @@ FUNC(kexec_reloc, PAGE_SIZE) jmp *%rbp .L_call_32_bit: - /* Setup IDT. */ - lidt compat_mode_idt(%rip) /* Load compat GDT. */ leaq compat_mode_gdt(%rip), %rax
@@ -202,11 +210,6 @@ DATA_LOCAL(compat_mode_gdt, 8) .Lcompat_mode_gdt_end: END(compat_mode_gdt) -DATA_LOCAL(compat_mode_idt) - .word 0 /* limit */ - .long 0 /* base */ -END(compat_mode_idt) - /* * 16 words of stack are more than enough. */
Signature · 1 line
2.39.5
Re: [PATCH 3/4] x86/kexec: Invalidate the IDT earlier in kexec_reloc()
Quoted 8 lines · Andrew Cooper
After switching stack, it is not safe to run any exception handlers, because attempts to access the
cpu_infoblock are out-of-bounds and will generate wild accesses.Invalidating the IDT in the common path means there's no need to do so again in the 32bit path, so drop
compat_mode_idtentirely.Signed-off-by Andrew Cooper <[email protected]>
Yet still two suggestions:
Quoted 5 lines
xen/arch/x86/x86_64/kexec_reloc.S +0
-0@@ -44,6 +44,16 @@ FUNC(kexec_reloc, PAGE_SIZE) movq %rcx, %rbp
xen/arch/x86/x86_64/kexec_reloc.S FUNC(kexec_reloc, PAGE_SIZE) · line 55
Quoted 10 lines
xen/arch/x86/x86_64/kexec_reloc.S +9
-0+ /* + * Invalidate the IDT. After switching off Xen's stacks, the + * exception handlers are unsafe to use, because there's no way to + * perform arithmetic on the stack pointer to find the cpu_info block. + */ + push $0 + pushw $0 + lidt (%rsp) + add $10, %rsp
Deliberately misaligning the stack is odd (but not technically a problem of course). How about using two full PUSHes instead, possibly adding a displacement of 6 to the LIDT in exchange?
Furthermore, don't we still have access to the full Xen image at this point? If so, why not do the above without using the stack, using zero_page(%rip) (or zero_page+6(%rip))?
Jan
Re: [PATCH 3/4] x86/kexec: Invalidate the IDT earlier in kexec_reloc()
Quoted 10 lines · Jan Beulich
Quoted 8 lines · Andrew Cooper
After switching stack, it is not safe to run any exception handlers, because attempts to access the
cpu_infoblock are out-of-bounds and will generate wild accesses.Invalidating the IDT in the common path means there's no need to do so again in the 32bit path, so drop
compat_mode_idtentirely.Signed-off-by Andrew Cooper <[email protected]>Reviewed-by Jan Beulich <[email protected]>
Thanks.
Quoted 9 lines
Yet still two suggestions:
Quoted 6 lines
xen/arch/x86/x86_64/kexec_reloc.S +0
-0@@ -44,6 +44,16 @@ FUNC(kexec_reloc, PAGE_SIZE) movq %rcx, %rbp
Quoted 12 lines
xen/arch/x86/x86_64/kexec_reloc.S FUNC(kexec_reloc, PAGE_SIZE) · line 55
Quoted 9 lines
xen/arch/x86/x86_64/kexec_reloc.S +9
-0+ /* + * Invalidate the IDT. After switching off Xen's stacks, the + * exception handlers are unsafe to use, because there's no way to + * perform arithmetic on the stack pointer to find the cpu_info block. + */ + push $0 + pushw $0 + lidt (%rsp) + add $10, %rspDeliberately misaligning the stack is odd (but not technically a problem of course). How about using two full PUSHes instead, possibly adding a displacement of 6 to the LIDT in exchange?
I've got a similar pattern in a followup series to remove compat_gdt, and for that it does strictly matter that it's not two full pushes.
This form takes no displacement and gets the accesses naturally aligned.
Quoted 3 lines
Furthermore, don't we still have access to the full Xen image at this point? If so, why not do the above without using the stack, using
zero_page(%rip)(or zero_page+6(%rip))?
We're crashing. I'll trust something freshly zeroed on the stack far more than a piece of memory we'd never notice changing away from 0.
~Andrew
PATCH 4/4 x86/kexec: Disable FRED earlier in kexec_reloc()
In reply to Andrew CooperWith FRED just as with IDT, it's unsafe to run the exception handlers after switching stack.
To remove this unsafe window, %cr4 needs clearing earlier. In turn, we may need to switch to PCID 0 earlier too in order to be able to clear CR4.PCIDE.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
The pagetable switch cannot easily be moved to be earlier, as that leaves a period of time where FRED is enabled but all stack pointers point to non-existent mappings.
--- xen/arch/x86/x86_64/kexec_reloc.S | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-)
xen/arch/x86/x86_64/kexec_reloc.S +15 -7
@@ -54,6 +54,21 @@ FUNC(kexec_reloc, PAGE_SIZE) lidt (%rsp) add $10, %rsp + /* Move to PCID 0 if necessary, as a prerequisite to clearing CR4.PCIDE */ + mov %cr3, %rax + test $0xfff, %eax + jz 1f + and $~0xfff, %rax + mov %rax, %cr3 +1: + + /* + * Set CR4 to PAE only. This may disable FRED, which must happen + * before switching off Xen's stack. + */ + mov $X86_CR4_PAE, %eax + mov %rax, %cr4 + /* * Move to the identity mapped stack. *
@@ -86,13 +101,6 @@ FUNC(kexec_reloc, PAGE_SIZE) orl $(X86_CR0_PG | X86_CR0_PE), %eax movq %rax, %cr0 - /* - * Set cr4 to a known state: - * - physical address extension enabled - */ - movl $X86_CR4_PAE, %eax - movq %rax, %cr4 - movq %rdx, %rdi call relocate_pages
Signature · 1 line
2.39.5
Re: [PATCH 4/4] x86/kexec: Disable FRED earlier in kexec_reloc()
Quoted 7 lines · Andrew Cooper
With FRED just as with IDT, it's unsafe to run the exception handlers after switching stack.
To remove this unsafe window, %cr4 needs clearing earlier. In turn, we may need to switch to PCID 0 earlier too in order to be able to clear CR4.PCIDE.
Signed-off-by Andrew Cooper <[email protected]>
Quoted 3 lines
The pagetable switch cannot easily be moved to be earlier, as that leaves a period of time where FRED is enabled but all stack pointers point to non-existent mappings.
But the clearing of PCID could be moved earlier, I guess.
xen/arch/x86/x86_64/kexec_reloc.S FUNC(kexec_reloc, PAGE_SIZE) · line 60
Quoted 10 lines
xen/arch/x86/x86_64/kexec_reloc.S +4
-0@@ -54,6 +54,21 @@ FUNC(kexec_reloc, PAGE_SIZE) lidt (%rsp) add $10, %rsp + /* Move to PCID 0 if necessary, as a prerequisite to clearing CR4.PCIDE */ + mov %cr3, %rax + test $0xfff, %eax + jz 1f
This conditional looks not overly relevant on this path, and we'd get more consistent overall behavior ...
xen/arch/x86/x86_64/kexec_reloc.S FUNC(kexec_reloc, PAGE_SIZE) · line 62
Quoted 2 lines
xen/arch/x86/x86_64/kexec_reloc.S +2
-0+ and $~0xfff, %rax + mov %rax, %cr3
... if the implied TLB flush would happen in all cases. (Of course the clearing of ...
xen/arch/x86/x86_64/kexec_reloc.S FUNC(kexec_reloc, PAGE_SIZE) · line 70
Quoted 8 lines
xen/arch/x86/x86_64/kexec_reloc.S +8
-0+1: + + /* + * Set CR4 to PAE only. This may disable FRED, which must happen + * before switching off Xen's stack. + */ + mov $X86_CR4_PAE, %eax + mov %rax, %cr4
... PCIDE will flush again anyway.)
Jan
Re: [PATCH 4/4] x86/kexec: Disable FRED earlier in kexec_reloc()
Quoted 9 lines · Jan Beulich
Quoted 7 lines · Andrew Cooper
With FRED just as with IDT, it's unsafe to run the exception handlers after switching stack.
To remove this unsafe window, %cr4 needs clearing earlier. In turn, we may need to switch to PCID 0 earlier too in order to be able to clear CR4.PCIDE.
Signed-off-by Andrew Cooper <[email protected]>Reviewed-by Jan Beulich <[email protected]>
Thanks.
Quoted 5 lines
Quoted 4 lines
The pagetable switch cannot easily be moved to be earlier, as that leaves a period of time where FRED is enabled but all stack pointers point to non-existent mappings.
But the clearing of PCID could be moved earlier, I guess.
Yes but then it's even further away from it's directly-related logic.
Quoted 27 lines
xen/arch/x86/x86_64/kexec_reloc.S FUNC(kexec_reloc, PAGE_SIZE) · line 60
Quoted 11 lines
xen/arch/x86/x86_64/kexec_reloc.S +4
-0@@ -54,6 +54,21 @@ FUNC(kexec_reloc, PAGE_SIZE) lidt (%rsp) add $10, %rsp + /* Move to PCID 0 if necessary, as a prerequisite to clearing CR4.PCIDE */ + mov %cr3, %rax + test $0xfff, %eax + jz 1fThis conditional looks not overly relevant on this path, and we'd get more consistent overall behavior ...
xen/arch/x86/x86_64/kexec_reloc.S FUNC(kexec_reloc, PAGE_SIZE) · line 62
Quoted 2 lines
xen/arch/x86/x86_64/kexec_reloc.S +2
-0+ and $~0xfff, %rax + mov %rax, %cr3... if the implied TLB flush would happen in all cases. (Of course the clearing of ...
xen/arch/x86/x86_64/kexec_reloc.S FUNC(kexec_reloc, PAGE_SIZE) · line 70
Quoted 8 lines
xen/arch/x86/x86_64/kexec_reloc.S +8
-0+1: + + /* + * Set CR4 to PAE only. This may disable FRED, which must happen + * before switching off Xen's stack. + */ + mov $X86_CR4_PAE, %eax + mov %rax, %cr4
Quoted 1 line
... PCIDE will flush again anyway.)
Also PGE getting cleared. There's also a full flush just out of context lower when we switch off Xen's pagetables onto the identity pagetables.
We don't need a 3rd unconditional flush.
~Andrew
Quoted 11 lines · Andrew Cooper
Kexec with FRED happens to already function.
Looking at the logic, several unsafe actions are occuring. Fix the worst of them.
Andrew Cooper (4):
x86/kexec: Stop hooking NMIs withtrap_nop()x86/kexec: Fix and expands comments forkexec_reloc()x86/kexec: Invalidate the IDT earlier inkexec_reloc()x86/kexec: Disable FRED earlier inkexec_reloc()
Quoted 4 lines
xen/arch/x86/crash.c | 20 +++++----- xen/arch/x86/x86_64/kexec_reloc.S | 62 ++++++++++++++++++++++--------- 2 files changed, 55 insertions(+), 27 deletions(-)
This is fully reviewed/acked but fell through the cracks an I didn't get it committed in time. Sorry.
Please could I request a release ack?
Now that FRED is active by default on AMD hardware in 4.22, a poorly timed NMI will break transitioning into the crash environment. i.e. it's fixing a corner-case bug in a new feature for 4.22.
~Andrew
Quoted 5 lines · Andrew Cooper
Quoted 4 lines · Andrew Cooper
Kexec with FRED happens to already function.
Looking at the logic, several unsafe actions are occuring. Fix the worst of them.
How critical are the remaining actions?
Quoted 12 lines
Quoted 11 lines
Andrew Cooper (4):
x86/kexec: Stop hooking NMIs withtrap_nop()x86/kexec: Fix and expands comments forkexec_reloc()x86/kexec: Invalidate the IDT earlier inkexec_reloc()x86/kexec: Disable FRED earlier inkexec_reloc()xen/arch/x86/crash.c | 20 +++++----- xen/arch/x86/x86_64/kexec_reloc.S | 62 ++++++++++++++++++++++--------- 2 files changed, 55 insertions(+), 27 deletions(-)
Quoted 8 lines
This is fully reviewed/acked but fell through the cracks an I didn't get it committed in time. Sorry.
Please could I request a release ack?
Now that FRED is active by default on AMD hardware in 4.22, a poorly timed NMI will break transitioning into the crash environment. i.e. it's fixing a corner-case bug in a new feature for 4.22.
Sounds reasonable to me:
Thanks.
~ Oleksii