Skip to content

Commit a364c01

Browse files
avaginsean-jc
authored andcommitted
kvm/x86: allocate the write-tracking metadata on-demand
The write-track is used externally only by the gpu/drm/i915 driver. Currently, it is always enabled, if a kernel has been compiled with this driver. Enabling the write-track mechanism adds a two-byte overhead per page across all memory slots. It isn't significant for regular VMs. However in gVisor, where the entire process virtual address space is mapped into the VM, even with a 39-bit address space, the overhead amounts to 256MB. Rework the write-tracking mechanism to enable it on-demand in kvm_page_track_register_notifier. Here is Sean's comment about the locking scheme: The only potential hiccup would be if taking slots_arch_lock would deadlock, but it should be impossible for slots_arch_lock to be taken in any other path that involves VFIO and/or KVMGT *and* can be coincident. Except for kvm_arch_destroy_vm() (which deletes KVM's internal memslots), slots_arch_lock is taken only through KVM ioctls(), and the caller of kvm_page_track_register_notifier() *must* hold a reference to the VM. Cc: Paolo Bonzini <pbonzini@redhat.com> Cc: Sean Christopherson <seanjc@google.com> Cc: Zhenyu Wang <zhenyuw@linux.intel.com> Co-developed-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Andrei Vagin <avagin@google.com> Link: https://lore.kernel.org/r/20240213192340.2023366-1-avagin@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 576a15d commit a364c01

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,15 @@ struct kvm_arch {
14661466
*/
14671467
bool shadow_root_allocated;
14681468

1469+
#ifdef CONFIG_KVM_EXTERNAL_WRITE_TRACKING
1470+
/*
1471+
* If set, the VM has (or had) an external write tracking user, and
1472+
* thus all write tracking metadata has been allocated, even if KVM
1473+
* itself isn't using write tracking.
1474+
*/
1475+
bool external_write_tracking_enabled;
1476+
#endif
1477+
14691478
#if IS_ENABLED(CONFIG_HYPERV)
14701479
hpa_t hv_root_tdp;
14711480
spinlock_t hv_root_tdp_lock;

arch/x86/kvm/mmu/page_track.c

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,23 @@
2020
#include "mmu_internal.h"
2121
#include "page_track.h"
2222

23+
static bool kvm_external_write_tracking_enabled(struct kvm *kvm)
24+
{
25+
#ifdef CONFIG_KVM_EXTERNAL_WRITE_TRACKING
26+
/*
27+
* Read external_write_tracking_enabled before related pointers. Pairs
28+
* with the smp_store_release in kvm_page_track_write_tracking_enable().
29+
*/
30+
return smp_load_acquire(&kvm->arch.external_write_tracking_enabled);
31+
#else
32+
return false;
33+
#endif
34+
}
35+
2336
bool kvm_page_track_write_tracking_enabled(struct kvm *kvm)
2437
{
25-
return IS_ENABLED(CONFIG_KVM_EXTERNAL_WRITE_TRACKING) ||
26-
!tdp_enabled || kvm_shadow_root_allocated(kvm);
38+
return kvm_external_write_tracking_enabled(kvm) ||
39+
kvm_shadow_root_allocated(kvm) || !tdp_enabled;
2740
}
2841

2942
void kvm_page_track_free_memslot(struct kvm_memory_slot *slot)
@@ -153,6 +166,50 @@ int kvm_page_track_init(struct kvm *kvm)
153166
return init_srcu_struct(&head->track_srcu);
154167
}
155168

169+
static int kvm_enable_external_write_tracking(struct kvm *kvm)
170+
{
171+
struct kvm_memslots *slots;
172+
struct kvm_memory_slot *slot;
173+
int r = 0, i, bkt;
174+
175+
mutex_lock(&kvm->slots_arch_lock);
176+
177+
/*
178+
* Check for *any* write tracking user (not just external users) under
179+
* lock. This avoids unnecessary work, e.g. if KVM itself is using
180+
* write tracking, or if two external users raced when registering.
181+
*/
182+
if (kvm_page_track_write_tracking_enabled(kvm))
183+
goto out_success;
184+
185+
for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) {
186+
slots = __kvm_memslots(kvm, i);
187+
kvm_for_each_memslot(slot, bkt, slots) {
188+
/*
189+
* Intentionally do NOT free allocations on failure to
190+
* avoid having to track which allocations were made
191+
* now versus when the memslot was created. The
192+
* metadata is guaranteed to be freed when the slot is
193+
* freed, and will be kept/used if userspace retries
194+
* the failed ioctl() instead of killing the VM.
195+
*/
196+
r = kvm_page_track_write_tracking_alloc(slot);
197+
if (r)
198+
goto out_unlock;
199+
}
200+
}
201+
202+
out_success:
203+
/*
204+
* Ensure that external_write_tracking_enabled becomes true strictly
205+
* after all the related pointers are set.
206+
*/
207+
smp_store_release(&kvm->arch.external_write_tracking_enabled, true);
208+
out_unlock:
209+
mutex_unlock(&kvm->slots_arch_lock);
210+
return r;
211+
}
212+
156213
/*
157214
* register the notifier so that event interception for the tracked guest
158215
* pages can be received.
@@ -161,10 +218,17 @@ int kvm_page_track_register_notifier(struct kvm *kvm,
161218
struct kvm_page_track_notifier_node *n)
162219
{
163220
struct kvm_page_track_notifier_head *head;
221+
int r;
164222

165223
if (!kvm || kvm->mm != current->mm)
166224
return -ESRCH;
167225

226+
if (!kvm_external_write_tracking_enabled(kvm)) {
227+
r = kvm_enable_external_write_tracking(kvm);
228+
if (r)
229+
return r;
230+
}
231+
168232
kvm_get_kvm(kvm);
169233

170234
head = &kvm->arch.track_notifier_head;

0 commit comments

Comments
 (0)