Skip to contentSkip to conversation

nvmx deadlock with MSR bitmaps

Xen-devel Andrew Cooper 4 participants 8 messages Mar 11, 2020 – Mar 12, 2020

lore
Share & tools
lore
#0 lore raw

Hello,

Testing has encountered this deadlock:

(XEN) Watchdog timer detects that CPU0 is stuck!
(XEN) ----[ Xen-4.14.0-9.0.4-d  x86_64  debug=y   Not tainted ]---- (XEN) CPU:    0
(XEN) RIP:    e008:[<ffff82d08022d6ae>] _spin_lock+0x34/0x5e (XEN) RFLAGS: 0000000000000002   CONTEXT: hypervisor (d0v0) (XEN) rax: 0000000000009a44   rbx: ffff8308466712b8   rcx: 0000000000009a45 (XEN) rdx: ffff8308466712b8   rsi: 0000000000009a44   rdi: ffff8308466712be (XEN) rbp: ffff8300952c7b08   rsp: ffff8300952c7af8   r8:  0000000000000000 (XEN) r9:  0000000000000000   r10: 0000000000000000   r11: 0000000000000000 (XEN) r12: ffff8308466712be   r13: ffff83084665a000   r14: 0000000000555e52 (XEN) r15: 0000000000000002   cr0: 000000008005003b   cr4: 00000000001526e0 (XEN) cr3: 0000000555e52000   cr2: ffff820060001008
(XEN) fsb: 0000000000000000   gsb: 0000000000000000   gss: ffff8e80987a0000 (XEN) ds: 0000   es: 0000   fs: 0000   gs: 0000   ss: 0000   cs: e008 (XEN) Xen code around <ffff82d08022d6ae> (_spin_lock+0x34/0x5e):
(XEN)  da 89 c1 f3 90 66 8b 02 <66> 39 c1 75 f6 4c 89 e7 e8 85 fe ff ff 48 8d 05
(XEN) Xen stack trace from rsp=ffff8300952c7af8:
(XEN)    ffff830846671000 ffff820060001008 ffff8300952c7b58 ffff82d0803219d7 (XEN)    ffff8308466712b8 0000000000000086 ffff82d08038d979 ffff8300952c7bd8 (XEN)    ffff820060001008 0000000000000000 0000000000000000 0000000000000001 (XEN)    ffff8300952c7bc8 ffff82d080356332 0000000000000046 ffff82d08038d96d (XEN)    0000000000000000 000000008038d96d ffff82d08038d979 ffff82d08038d96d (XEN)    ffff82d08038d979 ffff83084665a000 0000000000000000 0000000000000000 (XEN)    ffff8300952c7fff 0000000000000000 00007cff6ad38407 ffff82d08038da3d (XEN)    0000000000000001 00000000005d4a49 ffff83084665a000 0000000000000080 (XEN)    ffff8300952c7c88 000000000000005e 0000048ee9d28fde 0000048ee9c2fae4 (XEN)    0000000000002000 0000000000000000 0000000000000001 000000000000001e (XEN)    0000000000000022 0000000000000080 ffff820060001008 0000000e00000000 (XEN)    ffff82d08031729f 000000000000e008 0000000000010016 ffff8300952c7c80 (XEN)    0000000000000000 ffff830846671000 ffff8300952c7cd8 ffff82d080321a4d (XEN)    ffff8308466712b8 0000000000000086 0000000000557140 00000000b6a065fa (XEN)    ffff830555e6e000 0000000010000000 00007d2000000000 ffff8305571a4000 (XEN)    ffff8300952c7d08 ffff82d08029eeb2 ffff830555e6e000 ffff830846671000 (XEN)    0000000000000000 ffff830555e6e000 ffff8300952c7d28 ffff82d080299f5a (XEN)    ffff830555e6e000 ffff830555e6e000 ffff8300952c7d48 ffff82d08029a3f7 (XEN)    ffff8300952c7d48 ffff83084665a000 ffff8300952c7da8 ffff82d08031d796 (XEN)    ffff8300952c7d78 ffff8300952c7ef8 ffff8300952c7d78 ffff82d08029e684 (XEN) Xen call trace:
(XEN)    [<ffff82d08022d6ae>] R _spin_lock+0x34/0x5e
(XEN)    [<ffff82d0803219d7>] F map_domain_page+0x250/0x527 (XEN)    [<ffff82d080356332>] F do_page_fault+0x420/0x780
(XEN)    [<ffff82d08038da3d>] F x86_64/entry.S#handle_exception_saved+0x68/0x94
(XEN)    [<ffff82d08031729f>] F __find_next_zero_bit+0x28/0x69 (XEN)    [<ffff82d080321a4d>] F map_domain_page+0x2c6/0x527 (XEN)    [<ffff82d08029eeb2>] F nvmx_update_exec_control+0x1d7/0x323 (XEN)    [<ffff82d080299f5a>] F vmx_update_cpu_exec_control+0x23/0x40 (XEN)    [<ffff82d08029a3f7>] F arch/x86/hvm/vmx/vmx.c#vmx_ctxt_switch_from+0xb7/0x121
(XEN)    [<ffff82d08031d796>] F arch/x86/domain.c#__context_switch+0x124/0x4a9
(XEN)    [<ffff82d080320925>] F context_switch+0x154/0x62c
(XEN)    [<ffff82d080252f3e>] F common/sched/core.c#sched_context_switch+0x16a/0x175
(XEN)    [<ffff82d080253877>] F common/sched/core.c#schedule+0x2ad/0x2bc (XEN)    [<ffff82d08022cc97>] F common/softirq.c#__do_softirq+0xb7/0xc8 (XEN)    [<ffff82d08022cd38>] F do_softirq+0x18/0x1a
(XEN)    [<ffff82d0802a2fbb>] F vmx_asm_do_vmentry+0x2b/0x30 (XEN)
(XEN) CPU1 @ e008:ffff82d080252d45 (sched_context_switched+0xa3/0x132) (XEN) CPU7 @ e008:ffff82d08022d6ae (_spin_lock+0x34/0x5e)
(XEN) CPU3 @ e008:ffff82d08022d6ae (_spin_lock+0x34/0x5e)
(XEN) CPU5 @ e008:ffff82d08022d6ab (_spin_lock+0x31/0x5e)
(XEN) CPU4 @ e008:ffff82d08022d547 (common/spinlock.c#got_lock+0x7/0x23) (XEN) CPU6 @ e008:ffff82d08025318a (common/sched/core.c#sched_wait_rendezvous_in+0x241/0x39e)
(XEN) CPU2 @ e008:ffff82d08022d6ae (_spin_lock+0x34/0x5e)
(XEN)
(XEN) ****************************************
(XEN) Panic on CPU 0:
(XEN) FATAL TRAP: vector = 2 (nmi)
(XEN) [error_code=0000] , IN INTERRUPT CONTEXT
(XEN) ****************************************
(XEN)
(XEN) Reboot in five seconds...
(XEN) Executing kexec image on cpu0
(XEN) Shot down all CPUs

This is ultimately cased by c47984aab "nvmx: implement support for MSR bitmaps", which adds a map_domain_page() call in the middle of context_switch(), which isn't safe.

Specifically, this is a switch from an HVM vcpu, to a PV vcpu, where the mapcache code tries to access the per-domain mappings on the HVM monitor table.  It ends up trying to recursively acquire the mapcache lock while trying to walk %cr2 to identify the source of the fault.

For nvmx->msr_merged, this needs to either be a xenheap page, or a globally mapped domheap page.  I'll draft a patch in a moment.

For map_domain_page(), is there anything we can rationally do to assert that it isn't called in the middle of a context switch?  This is the kind of thing which needs to blow up reliably in a debug build.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#1 lore raw
Quoted 11 lines · Andrew Cooper

Specifically, this is a switch from an HVM vcpu, to a PV vcpu, where the mapcache code tries to access the per-domain mappings on the HVM monitor table.  It ends up trying to recursively acquire the mapcache lock while trying to walk %cr2 to identify the source of the fault.

For nvmx->msr_merged, this needs to either be a xenheap page, or a globally mapped domheap page.  I'll draft a patch in a moment.

For map_domain_page(), is there anything we can rationally do to assert that it isn't called in the middle of a context switch?  This is the kind of thing which needs to blow up reliably in a debug build.

Well, it's not inherently unsafe to do, it's just that mapcache_current_vcpu() would need to avoid using current from context_switch()'s call to set_current() through to __context_switch()'s call to write_ptbase(). A possible detection (if we don't want to make the case work) would seem to be ASSERT(current == this_cpu(curr_vcpu)). But of course there's also this extra logic in mapcache_current_vcpu() to deal with a PV vCPU having a null v->arch.guest_table, which I'm once again struggling to see under what conditions it might happen. The Dom0 building case can't be meant with there being mapcache_override_current() on that path. I'm wondering if the comment there is misleading and it's really to cover the case where, coming from a PV vCPU, current was already set to the idle vCPU by context_switch() (which would have a null v->arch.guest_table) - I wouldn't call this "we are running a paravirtualised guest". But in such a case the logic here would simply be a (too) special case of what you're describing as the issue with nVMX.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 23 lines · Jan Beulich
Quoted 11 lines · Andrew Cooper

Specifically, this is a switch from an HVM vcpu, to a PV vcpu, where the mapcache code tries to access the per-domain mappings on the HVM monitor table.  It ends up trying to recursively acquire the mapcache lock while trying to walk %cr2 to identify the source of the fault.

For nvmx->msr_merged, this needs to either be a xenheap page, or a globally mapped domheap page.  I'll draft a patch in a moment.

For map_domain_page(), is there anything we can rationally do to assert that it isn't called in the middle of a context switch?  This is the kind of thing which needs to blow up reliably in a debug build.

Well, it's not inherently unsafe to do, it's just that mapcache_current_vcpu() would need to avoid using current from context_switch()'s call to set_current() through to __context_switch()'s call to write_ptbase(). A possible detection (if we don't want to make the case work) would seem to be ASSERT(current == this_cpu(curr_vcpu)). But of course there's also this extra logic in mapcache_current_vcpu() to deal with a PV vCPU having a null v->arch.guest_table, which I'm once again struggling to see under what conditions it might happen. The Dom0 building case can't be meant with there being

Quoted 8 lines

mapcache_override_current() on that path. I'm wondering if the comment there is misleading and it's really to cover the case where, coming from a PV vCPU, current was already set to the idle vCPU by context_switch() (which would have a null v->arch.guest_table) - I wouldn't call this "we are running a paravirtualised guest". But in such a case the logic here would simply be a (too) special case of what you're describing as the issue with nVMX.

Looking at the code in context_switch and __context_switch would it be possible to set current to the next vCPU after all the from hooks have been called?

Ie: set_current could be moved into __context_switch after the call to pd->arch.ctxt_switch->from(p).

I'm also not sure I understand the difference between context_switch and __context_switch, and how are callers supposed to use them.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 37 lines · Roger Pau Monné
Quoted 23 lines · Jan Beulich
Quoted 11 lines · Andrew Cooper

Specifically, this is a switch from an HVM vcpu, to a PV vcpu, where the mapcache code tries to access the per-domain mappings on the HVM monitor table.  It ends up trying to recursively acquire the mapcache lock while trying to walk %cr2 to identify the source of the fault.

For nvmx->msr_merged, this needs to either be a xenheap page, or a globally mapped domheap page.  I'll draft a patch in a moment.

For map_domain_page(), is there anything we can rationally do to assert that it isn't called in the middle of a context switch?  This is the kind of thing which needs to blow up reliably in a debug build.

Well, it's not inherently unsafe to do, it's just that mapcache_current_vcpu() would need to avoid using current from context_switch()'s call to set_current() through to __context_switch()'s call to write_ptbase(). A possible detection (if we don't want to make the case work) would seem to be ASSERT(current == this_cpu(curr_vcpu)). But of course there's also this extra logic in mapcache_current_vcpu() to deal with a PV vCPU having a null v->arch.guest_table, which I'm once again struggling to see under what conditions it might happen. The Dom0 building case can't be meant with there being

Quoted 8 lines

mapcache_override_current() on that path. I'm wondering if the comment there is misleading and it's really to cover the case where, coming from a PV vCPU, current was already set to the idle vCPU by context_switch() (which would have a null v->arch.guest_table) - I wouldn't call this "we are running a paravirtualised guest". But in such a case the logic here would simply be a (too) special case of what you're describing as the issue with nVMX.

Looking at the code in context_switch and __context_switch would it be possible to set current to the next vCPU after all the from hooks have been called?

Quoted 5 lines

Ie: set_current could be moved into __context_switch after the call to pd->arch.ctxt_switch->from(p).

I'm also not sure I understand the difference between context_switch and __context_switch, and how are callers supposed to use them.

The latter does the full switch, while the former only records that a switch is pending, and does the parts that can't be deferred. For this reason set_current() can't be moved to __context_switch() without applying extreme care, I'm afraid.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
In reply to Roger Pau Monné
Quoted 33 lines · Roger Pau Monné
Quoted 23 lines · Jan Beulich
Quoted 11 lines · Andrew Cooper

Specifically, this is a switch from an HVM vcpu, to a PV vcpu, where the mapcache code tries to access the per-domain mappings on the HVM monitor table.  It ends up trying to recursively acquire the mapcache lock while trying to walk %cr2 to identify the source of the fault.

For nvmx->msr_merged, this needs to either be a xenheap page, or a globally mapped domheap page.  I'll draft a patch in a moment.

For map_domain_page(), is there anything we can rationally do to assert that it isn't called in the middle of a context switch?  This is the kind of thing which needs to blow up reliably in a debug build.

Well, it's not inherently unsafe to do, it's just that mapcache_current_vcpu() would need to avoid using current from context_switch()'s call to set_current() through to __context_switch()'s call to write_ptbase(). A possible detection (if we don't want to make the case work) would seem to be ASSERT(current == this_cpu(curr_vcpu)). But of course there's also this extra logic in mapcache_current_vcpu() to deal with a PV vCPU having a null v->arch.guest_table, which I'm once again struggling to see under what conditions it might happen. The Dom0 building case can't be meant with there being

Quoted 8 lines

mapcache_override_current() on that path. I'm wondering if the comment there is misleading and it's really to cover the case where, coming from a PV vCPU, current was already set to the idle vCPU by context_switch() (which would have a null v->arch.guest_table) - I wouldn't call this "we are running a paravirtualised guest". But in such a case the logic here would simply be a (too) special case of what you're describing as the issue with nVMX.

Quoted 6 lines

Looking at the code in context_switch and __context_switch would it be possible to set current to the next vCPU after all the from hooks have been called?

Ie: set_current could be moved into __context_switch after the call to pd->arch.ctxt_switch->from(p).

No, wouldn't work. When switching to idle __context_switch() is normally not called in order to avoid switching the address space in case the same vcpu will be scheduled again after idle. This is the reason why current and curr_vcpu can be different.

Quoted 2 lines

I'm also not sure I understand the difference between context_switch and __context_switch, and how are callers supposed to use them.

That is scheduler black magic. :-)

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
Quoted 38 lines · Jürgen Groß
Quoted 37 lines · Roger Pau Monné
Quoted 23 lines · Jan Beulich
Quoted 11 lines · Andrew Cooper

Specifically, this is a switch from an HVM vcpu, to a PV vcpu, where the mapcache code tries to access the per-domain mappings on the HVM monitor table.  It ends up trying to recursively acquire the mapcache lock while trying to walk %cr2 to identify the source of the fault.

For nvmx->msr_merged, this needs to either be a xenheap page, or a globally mapped domheap page.  I'll draft a patch in a moment.

For map_domain_page(), is there anything we can rationally do to assert that it isn't called in the middle of a context switch?  This is the kind of thing which needs to blow up reliably in a debug build.

Well, it's not inherently unsafe to do, it's just that mapcache_current_vcpu() would need to avoid using current from context_switch()'s call to set_current() through to __context_switch()'s call to write_ptbase(). A possible detection (if we don't want to make the case work) would seem to be ASSERT(current == this_cpu(curr_vcpu)). But of course there's also this extra logic in mapcache_current_vcpu() to deal with a PV vCPU having a null v->arch.guest_table, which I'm once again struggling to see under what conditions it might happen. The Dom0 building case can't be meant with there being

Quoted 8 lines

mapcache_override_current() on that path. I'm wondering if the comment there is misleading and it's really to cover the case where, coming from a PV vCPU, current was already set to the idle vCPU by context_switch() (which would have a null v->arch.guest_table) - I wouldn't call this "we are running a paravirtualised guest". But in such a case the logic here would simply be a (too) special case of what you're describing as the issue with nVMX.

Looking at the code in context_switch and __context_switch would it be possible to set current to the next vCPU after all the from hooks have been called?

Quoted 7 lines
Quoted 2 lines

Ie: set_current could be moved into __context_switch after the call to pd->arch.ctxt_switch->from(p).

No, wouldn't work. When switching to idle __context_switch() is normally not called in order to avoid switching the address space in case the same vcpu will be scheduled again after idle. This is the reason why current and curr_vcpu can be different.

Since the idle vCPU context switch is already a special case we could maybe place the call to set_current in such special handling, while leaving the call for the rest of vCPUs in __context_switch after the ->from hooks have been executed?

Quoted 2 lines
Quoted 2 lines

I'm also not sure I understand the difference between context_switch and __context_switch, and how are callers supposed to use them.

Jan points out that __context_switch signals a pending context switch, in which case my proposal to use set_current might not be suitable, as we would be changing current without actually doing the context switch?

I also wonder why __context_switch then needs to call the ->from hook just to signal a pending context switch. It seems like __context_switch does a lot of work just to signal a context switch, which will then be redone when context_switch is actually called?

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw
Quoted 60 lines · Roger Pau Monné
Quoted 38 lines · Jürgen Groß
Quoted 37 lines · Roger Pau Monné
Quoted 23 lines · Jan Beulich
Quoted 11 lines · Andrew Cooper

Specifically, this is a switch from an HVM vcpu, to a PV vcpu, where the mapcache code tries to access the per-domain mappings on the HVM monitor table.  It ends up trying to recursively acquire the mapcache lock while trying to walk %cr2 to identify the source of the fault.

For nvmx->msr_merged, this needs to either be a xenheap page, or a globally mapped domheap page.  I'll draft a patch in a moment.

For map_domain_page(), is there anything we can rationally do to assert that it isn't called in the middle of a context switch?  This is the kind of thing which needs to blow up reliably in a debug build.

Well, it's not inherently unsafe to do, it's just that mapcache_current_vcpu() would need to avoid using current from context_switch()'s call to set_current() through to __context_switch()'s call to write_ptbase(). A possible detection (if we don't want to make the case work) would seem to be ASSERT(current == this_cpu(curr_vcpu)). But of course there's also this extra logic in mapcache_current_vcpu() to deal with a PV vCPU having a null v->arch.guest_table, which I'm once again struggling to see under what conditions it might happen. The Dom0 building case can't be meant with there being

Quoted 8 lines

mapcache_override_current() on that path. I'm wondering if the comment there is misleading and it's really to cover the case where, coming from a PV vCPU, current was already set to the idle vCPU by context_switch() (which would have a null v->arch.guest_table) - I wouldn't call this "we are running a paravirtualised guest". But in such a case the logic here would simply be a (too) special case of what you're describing as the issue with nVMX.

Looking at the code in context_switch and __context_switch would it be possible to set current to the next vCPU after all the from hooks have been called?

Quoted 7 lines
Quoted 2 lines

Ie: set_current could be moved into __context_switch after the call to pd->arch.ctxt_switch->from(p).

No, wouldn't work. When switching to idle __context_switch() is normally not called in order to avoid switching the address space in case the same vcpu will be scheduled again after idle. This is the reason why current and curr_vcpu can be different.

Since the idle vCPU context switch is already a special case we could maybe place the call to set_current in such special handling, while leaving the call for the rest of vCPUs in __context_switch after the ->from hooks have been executed?

Quoted 2 lines
Quoted 2 lines

I'm also not sure I understand the difference between context_switch and __context_switch, and how are callers supposed to use them.

Jan points out that __context_switch signals a pending context switch, in which case my proposal to use set_current might not be suitable, as we would be changing current without actually doing the context switch?

Quoted 4 lines

I also wonder why __context_switch then needs to call the ->from hook just to signal a pending context switch. It seems like __context_switch does a lot of work just to signal a context switch, which will then be redone when context_switch is actually called?

Well, "signal" was perhaps not the best choice of a word.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#7 lore raw
Quoted 61 lines · Jan Beulich
Quoted 60 lines · Roger Pau Monné
Quoted 38 lines · Jürgen Groß
Quoted 37 lines · Roger Pau Monné
Quoted 23 lines · Jan Beulich
Quoted 11 lines · Andrew Cooper

Specifically, this is a switch from an HVM vcpu, to a PV vcpu, where the mapcache code tries to access the per-domain mappings on the HVM monitor table.  It ends up trying to recursively acquire the mapcache lock while trying to walk %cr2 to identify the source of the fault.

For nvmx->msr_merged, this needs to either be a xenheap page, or a globally mapped domheap page.  I'll draft a patch in a moment.

For map_domain_page(), is there anything we can rationally do to assert that it isn't called in the middle of a context switch?  This is the kind of thing which needs to blow up reliably in a debug build.

Well, it's not inherently unsafe to do, it's just that mapcache_current_vcpu() would need to avoid using current from context_switch()'s call to set_current() through to __context_switch()'s call to write_ptbase(). A possible detection (if we don't want to make the case work) would seem to be ASSERT(current == this_cpu(curr_vcpu)). But of course there's also this extra logic in mapcache_current_vcpu() to deal with a PV vCPU having a null v->arch.guest_table, which I'm once again struggling to see under what conditions it might happen. The Dom0 building case can't be meant with there being

Quoted 8 lines

mapcache_override_current() on that path. I'm wondering if the comment there is misleading and it's really to cover the case where, coming from a PV vCPU, current was already set to the idle vCPU by context_switch() (which would have a null v->arch.guest_table) - I wouldn't call this "we are running a paravirtualised guest". But in such a case the logic here would simply be a (too) special case of what you're describing as the issue with nVMX.

Looking at the code in context_switch and __context_switch would it be possible to set current to the next vCPU after all the from hooks have been called?

Quoted 7 lines
Quoted 2 lines

Ie: set_current could be moved into __context_switch after the call to pd->arch.ctxt_switch->from(p).

No, wouldn't work. When switching to idle __context_switch() is normally not called in order to avoid switching the address space in case the same vcpu will be scheduled again after idle. This is the reason why current and curr_vcpu can be different.

Since the idle vCPU context switch is already a special case we could maybe place the call to set_current in such special handling, while leaving the call for the rest of vCPUs in __context_switch after the ->from hooks have been executed?

Quoted 2 lines
Quoted 2 lines

I'm also not sure I understand the difference between context_switch and __context_switch, and how are callers supposed to use them.

Jan points out that __context_switch signals a pending context switch, in which case my proposal to use set_current might not be suitable, as we would be changing current without actually doing the context switch?

Quoted 6 lines
Quoted 4 lines

I also wonder why __context_switch then needs to call the ->from hook just to signal a pending context switch. It seems like __context_switch does a lot of work just to signal a context switch, which will then be redone when context_switch is actually called?

Well, "signal" was perhaps not the best choice of a word.

Basically you can say that context_switch() is switching vcpus logically (Xen internal bookkeeping), while __context_switch() is switching the runtime environment (address space, GDT settings, cr4, ...).

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel