Skip to content

Commit e18c542

Browse files
sean-jcbonzini
authored andcommitted
KVM: x86/mmu: Assert that correct locks are held for page write-tracking
When adding/removing gfns to/from write-tracking, assert that mmu_lock is held for write, and that either slots_lock or kvm->srcu is held. mmu_lock must be held for write to protect gfn_write_track's refcount, and SRCU or slots_lock must be held to protect the memslot itself. Tested-by: Yan Zhao <yan.y.zhao@intel.com> Tested-by: Yongwei Ma <yongwei.ma@intel.com> Link: https://lore.kernel.org/r/20230729013535.1070024-26-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 7b57486 commit e18c542

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

arch/x86/kvm/mmu/page_track.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1414

15+
#include <linux/lockdep.h>
1516
#include <linux/kvm_host.h>
1617
#include <linux/rculist.h>
1718

@@ -77,16 +78,18 @@ static void update_gfn_write_track(struct kvm_memory_slot *slot, gfn_t gfn,
7778
* add guest page to the tracking pool so that corresponding access on that
7879
* page will be intercepted.
7980
*
80-
* It should be called under the protection both of mmu-lock and kvm->srcu
81-
* or kvm->slots_lock.
82-
*
8381
* @kvm: the guest instance we are interested in.
8482
* @slot: the @gfn belongs to.
8583
* @gfn: the guest page.
8684
*/
8785
void kvm_write_track_add_gfn(struct kvm *kvm, struct kvm_memory_slot *slot,
8886
gfn_t gfn)
8987
{
88+
lockdep_assert_held_write(&kvm->mmu_lock);
89+
90+
lockdep_assert_once(lockdep_is_held(&kvm->slots_lock) ||
91+
srcu_read_lock_held(&kvm->srcu));
92+
9093
if (WARN_ON_ONCE(!kvm_page_track_write_tracking_enabled(kvm)))
9194
return;
9295

@@ -107,16 +110,18 @@ EXPORT_SYMBOL_GPL(kvm_write_track_add_gfn);
107110
* remove the guest page from the tracking pool which stops the interception
108111
* of corresponding access on that page.
109112
*
110-
* It should be called under the protection both of mmu-lock and kvm->srcu
111-
* or kvm->slots_lock.
112-
*
113113
* @kvm: the guest instance we are interested in.
114114
* @slot: the @gfn belongs to.
115115
* @gfn: the guest page.
116116
*/
117117
void kvm_write_track_remove_gfn(struct kvm *kvm,
118118
struct kvm_memory_slot *slot, gfn_t gfn)
119119
{
120+
lockdep_assert_held_write(&kvm->mmu_lock);
121+
122+
lockdep_assert_once(lockdep_is_held(&kvm->slots_lock) ||
123+
srcu_read_lock_held(&kvm->srcu));
124+
120125
if (WARN_ON_ONCE(!kvm_page_track_write_tracking_enabled(kvm)))
121126
return;
122127

0 commit comments

Comments
 (0)