Documentation: kvm: clarify SRCU locking order
Subsystems (5)
Paolo Bonzini
maintainerPATCH 1/1 Documentation: kvm: clarify SRCU locking order
Currently only the locking order of SRCU vs kvm->slots_arch_lock and kvm->slots_lock is documented. Extend this to kvm->lock since Xen emulation got it terribly wrong.
--- Documentation/virt/kvm/locking.rst | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-)
Documentation/virt/kvm/locking.rst +14 -5
@@ -16,17 +16,26 @@ The acquisition orders for mutexes are as follows: - kvm->slots_lock is taken outside kvm->irq_lock, though acquiring them together is quite rare. -- Unlike kvm->slots_lock, kvm->slots_arch_lock is released before - synchronize_srcu(&kvm->srcu). Therefore kvm->slots_arch_lock - can be taken inside a kvm->srcu read-side critical section, - while kvm->slots_lock cannot. - - kvm->mn_active_invalidate_count ensures that pairs of invalidate_range_start() and invalidate_range_end() callbacks use the same memslots array. kvm->slots_lock and kvm->slots_arch_lock are taken on the waiting side in install_new_memslots, so MMU notifiers must not take either kvm->slots_lock or kvm->slots_arch_lock. +For SRCU: + +- ``synchronize_srcu(&kvm->srcu)`` is called _inside_ + the kvm->slots_lock critical section, therefore kvm->slots_lock + cannot be taken inside a kvm->srcu read-side critical section. + Instead, kvm->slots_arch_lock is released before the call + to ``synchronize_srcu()`` and _can_ be taken inside a + kvm->srcu read-side critical section. + +- kvm->lock is taken inside kvm->srcu, therefore + ``synchronize_srcu(&kvm->srcu)`` cannot be called inside + a kvm->lock critical section. If you cannot delay the + call until after kvm->lock is released, use ``call_srcu``. + On x86: - vcpu->mutex is taken outside kvm->arch.hyperv.hv_lock
Signature · 1 line
2.31.1
Sean Christopherson
maintainerQuoted 28 lines · Paolo Bonzini
Currently only the locking order of SRCU vs
kvm->slots_arch_lockandkvm->slots_lockis documented. Extend this tokvm->locksince Xen emulation got it terribly wrong.Signed-off-by Paolo Bonzini <[email protected]>--- Documentation/virt/kvm/locking.rst | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-)Documentation/virt/kvm/locking.rst +0
-5@@ -16,17 +16,26 @@ The acquisition orders for mutexes are as follows: - kvm->slots_lock is taken outside kvm->irq_lock, though acquiring them together is quite rare. -- Unlike kvm->slots_lock, kvm->slots_arch_lock is released before - synchronize_srcu(&kvm->srcu). Therefore kvm->slots_arch_lock - can be taken inside a kvm->srcu read-side critical section, - while kvm->slots_lock cannot. - - kvm->mn_active_invalidate_count ensures that pairs of invalidate_range_start() and invalidate_range_end() callbacks use the same memslots array. kvm->slots_lock and kvm->slots_arch_lock are taken on the waiting side in install_new_memslots, so MMU notifiers must not take either kvm->slots_lock or kvm->slots_arch_lock.
Documentation/virt/kvm/locking.rst The acquisition orders for mutexes are as follows: · line 34
Quoted 10 lines
Documentation/virt/kvm/locking.rst +10
-0+For SRCU: + +- ``synchronize_srcu(&kvm->srcu)`` is called _inside_ + the kvm->slots_lock critical section, therefore kvm->slots_lock + cannot be taken inside a kvm->srcu read-side critical section. + Instead, kvm->slots_arch_lock is released before the call + to ``synchronize_srcu()`` and _can_ be taken inside a + kvm->srcu read-side critical section. + +- kvm->lock is taken inside kvm->srcu, therefore
Prior to the recent Xen change, is this actually true? There are many instances where kvm->srcu is taken inside kvm->lock, but I can't find any existing cases where the reverse is true. Logically, it makes sense to take kvm->lock first since kvm->srcu can be taken deep in helpers, e.g. for accessing guest memory. It's also more consistent to take kvm->lock first since kvm->srcu is taken inside vcpu->mutex, and vcpu->mutex is taken inside kvm->lock.
Disallowing synchronize_srcu(kvm->srcu) inside kvm->lock isn't probelmatic per se, but it's going to result in a weird set of rules because synchronize_scru() can, and is, called while holding a variety of other locks.
In other words, IMO taking kvm->srcu outside of kvm->lock in the Xen code is the real bug.
Quoted 10 lines
Documentation/virt/kvm/locking.rst +4
-0+ ``synchronize_srcu(&kvm->srcu)`` cannot be called inside + a kvm->lock critical section. If you cannot delay the + call until after kvm->lock is released, use ``call_srcu``. + On x86: - vcpu->mutex is taken outside kvm->arch.hyperv.hv_lockSignature · 1 line
2.31.1
Sean Christopherson
maintainerQuoted 29 lines · Sean Christopherson
Quoted 28 lines · Paolo Bonzini
Currently only the locking order of SRCU vs
kvm->slots_arch_lockandkvm->slots_lockis documented. Extend this tokvm->locksince Xen emulation got it terribly wrong.Signed-off-by Paolo Bonzini <[email protected]>--- Documentation/virt/kvm/locking.rst | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-)Documentation/virt/kvm/locking.rst +0
-5@@ -16,17 +16,26 @@ The acquisition orders for mutexes are as follows: - kvm->slots_lock is taken outside kvm->irq_lock, though acquiring them together is quite rare. -- Unlike kvm->slots_lock, kvm->slots_arch_lock is released before - synchronize_srcu(&kvm->srcu). Therefore kvm->slots_arch_lock - can be taken inside a kvm->srcu read-side critical section, - while kvm->slots_lock cannot. - - kvm->mn_active_invalidate_count ensures that pairs of invalidate_range_start() and invalidate_range_end() callbacks use the same memslots array. kvm->slots_lock and kvm->slots_arch_lock are taken on the waiting side in install_new_memslots, so MMU notifiers must not take either kvm->slots_lock or kvm->slots_arch_lock.
Quoted 12 lines
Documentation/virt/kvm/locking.rst The acquisition orders for mutexes are as follows: · line 34
Quoted 10 lines
Documentation/virt/kvm/locking.rst +10
-0+For SRCU: + +- ``synchronize_srcu(&kvm->srcu)`` is called _inside_ + the kvm->slots_lock critical section, therefore kvm->slots_lock + cannot be taken inside a kvm->srcu read-side critical section. + Instead, kvm->slots_arch_lock is released before the call + to ``synchronize_srcu()`` and _can_ be taken inside a + kvm->srcu read-side critical section. + +- kvm->lock is taken inside kvm->srcu, thereforePrior to the recent Xen change, is this actually true?
I was thinking of a different change, but v5.19 is still kinda recent, so I'll count it. Better to be lucky than good :-)
Commit 2fd6df2f2b47 ("KVM: x86/xen: intercept EVTCHNOP_send from guests") introduced the only case I can find where kvm->srcu is taken inside kvm->lock.
Quoted 7 lines
There are many instances where
kvm->srcuis taken insidekvm->lock, but I can't find any existing cases where the reverse is true. Logically, it makes sense to takekvm->lockfirst sincekvm->srcucan be taken deep in helpers, e.g. for accessing guest memory. It's also more consistent to takekvm->lockfirst sincekvm->srcuis taken insidevcpu->mutex, andvcpu->mutexis taken insidekvm->lock.
Quoted 6 lines
Disallowing
synchronize_srcu(kvm->srcu)insidekvm->lockisn't probelmatic per se, but it's going to result in a weird set of rules becausesynchronize_scru()can, and is, called while holding a variety of other locks.In other words, IMO taking
kvm->srcuoutside ofkvm->lockin the Xen code is the real bug.
I'm doubing down on this. Taking kvm->srcu outside of kvm->lock is all kinds of sketchy, and likely indicates a larger problem. The aformentioned commit that introduced the problematic kvm->srcu vs. kvm->lock also blatantly violates ordering between kvm->lock and vcpu->mutex. Details in the link[*].
The vast majority of flows that take kvm->srcu will already hold a lock of some kind, otherwise the task can't safely deference any VM/vCPU/device data and thus has no reason to acquire kvm->srcu. E.g. taking kvm->srcu to read guest memory is nonsensical without a stable guest physical address to work with.
There are exceptions, e.g. evtchn_set_fn() and maybe some ioctls(), but in most cases, taking kvm->lock inside kvm->srcu is just asking for problems.
David Woodhouse
maintainerQuoted 35 lines · Sean Christopherson
Quoted 4 lines
Quoted 2 lines
+-
kvm->lockis taken insidekvm->srcu, thereforePrior to the recent Xen change, is this actually true?
I was thinking of a different change, but v5.19 is still kinda recent, so I'll count it. Better to be lucky than good :-)
Commit
2fd6df2f2b47("KVM: x86/xen: interceptEVTCHNOP_sendfrom guests") introduced the only case I can find wherekvm->srcuis taken insidekvm->lock.Quoted 7 lines
There are many instances where
kvm->srcuis taken insidekvm->lock, but I can't find any existing cases where the reverse is true. Logically, it makes sense to takekvm->lockfirst sincekvm->srcucan be taken deep in helpers, e.g. for accessing guest memory. It's also more consistent to takekvm->lockfirst sincekvm->srcuis taken insidevcpu->mutex, andvcpu->mutexis taken insidekvm->lock.Quoted 6 lines
Disallowing
synchronize_srcu(kvm->srcu)insidekvm->lockisn't probelmatic per se, but it's going to result in a weird set of rules becausesynchronize_scru()can, and is, called while holding a variety of other locks.In other words, IMO taking
kvm->srcuoutside ofkvm->lockin the Xen code is the real bug.I'm doubing down on this. Taking
kvm->srcuoutside ofkvm->lockis all kinds of sketchy, and likely indicates a larger problem. The aformentioned commit that introduced the problematickvm->srcuvs.kvm->lockalso blatantly violates ordering betweenkvm->lockand vcpu->mutex. Details in the link[*].The vast majority of flows that take
kvm->srcuwill already hold a lock of some kind, otherwise the task can't safely deference any VM/vCPU/device data and thus has no reason to acquire kvm->srcu. E.g. takingkvm->srcuto read guest memory is nonsensical without a stable guest physical address to work with.
Quoted 4 lines
There are exceptions, e.g.
evtchn_set_fn()and maybe someioctls(), but in most cases, takingkvm->lockinsidekvm->srcuis just asking for problems.
So...
If we apply the patch below, then the Xen code never takes kvm->lock inside kvm->srcu. (Never takes kvm->lock at all, in fact; I'm just rereading the manual search/replace to make sure the change is valid in all cases.)
It does take kvm->scru *without* holding kvm->lock, just not "outside" it.
I think we can probably revert commit a79b53aaaa ("KVM: x86: fix deadlock for KVM_XEN_EVTCHN_RESET") after doing this too?
From: David Woodhouse <[email protected]>
Date: Wed, 11 Jan 2023 10:04:38 +0000
Subject: [PATCH] KVM: x86/xen: Avoid deadlock by adding kvm->arch.xen.xen_lock leaf node lock
In commit 14243b387137a ("KVM: x86/xen: Add KVM_IRQ_ROUTING_XEN_EVTCHN and event channel delivery") the clever version of me left some helpful notes for those who would come after him:
/*
* For the irqfd workqueue, using the main kvm->lock mutex is
* fine since this function is invoked from kvm_set_irq() with
* no other lock held, no srcu. In future if it will be called
* directly from a vCPU thread (e.g. on hypercall for an IPI)
* then it may need to switch to using a leaf-node mutex for
* serializing the shared_info mapping.
*/
mutex_lock(&kvm->lock);
In commit 2fd6df2f2b47 ("KVM: x86/xen: intercept EVTCHNOP_send from guests") the other version of me ran straight past that comment without reading it, and introduced a potential deadlock by taking vcpu->mutex and kvm->lock in the wrong order.
Solve this as originally suggested, by adding a leaf-node lock in the Xen state rather than using kvm->lock for it.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/xen.c | 65 +++++++++++++++------------------ 2 files changed, 30 insertions(+), 36 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1115,6 +1115,7 @@ struct msr_bitmap_range {/* Xen emulation context */ struct kvm_xen { +struct mutex xen_lock;u32 xen_version;bool long_mode;bool runstate_update_flag;
arch/x86/kvm/xen.c +29 -36
204 lines not on this page · raw message
Signature · 1 line
2.35.3