KVM: x86: skip gfn_track allocation when possible
Subsystems (5)
v2 cover KVM: x86: skip gfn_track allocation when possible
From: David Stevens <[email protected]>
Skip allocating gfn_track arrays when tracking of guest write access to pages is not required. For VMs where the allocation can be avoided, this saves 2 bytes per 4KB of guest memory.
Write tracking is used to manage shadow page tables in three cases - when tdp is not supported, when nested virtualization is used, and for GVT-g. If tdp_enable is set and the kernel is compiled without GVT-g, then the gfn_track arrays can be allocated lazily when the shadow MMU is initialized.
v1 -> v2:
- lazily allocate gfn_track when shadow MMU is initialized, instead of looking at cpuid
David Stevens (2):
KVM: x86: add config for non-kvm users of page tracking
KVM: x86: only allocate gfn_track when necessary
arch/x86/include/asm/kvm_host.h | 8 ++++ arch/x86/include/asm/kvm_page_track.h | 5 +- arch/x86/kvm/Kconfig | 3 ++ arch/x86/kvm/mmu/mmu.c | 7 +++ arch/x86/kvm/mmu/page_track.c | 69 ++++++++++++++++++++++++++- arch/x86/kvm/x86.c | 2 +- drivers/gpu/drm/i915/Kconfig | 1 + 7 files changed, 91 insertions(+), 4 deletions(-)
Signature · 1 line
2.33.0.464.g1972c5931b-goog
PATCH v2 1/2 KVM: x86: add config for non-kvm users of page tracking
From: David Stevens <[email protected]>
Add a config option that allows kvm to determine whether or not there are any external users of page tracking.
--- arch/x86/kvm/Kconfig | 3 +++ drivers/gpu/drm/i915/Kconfig | 1 + 2 files changed, 4 insertions(+)
arch/x86/kvm/Kconfig +3 -0
@@ -129,4 +129,7 @@ config KVM_MMU_AUDIT This option adds a R/W kVM module parameter 'mmu_audit', which allows auditing of KVM MMU events at runtime. +config KVM_EXTERNAL_WRITE_TRACKING + bool + endif # VIRTUALIZATION
drivers/gpu/drm/i915/Kconfig +1 -0
@@ -126,6 +126,7 @@ config DRM_I915_GVT_KVMGT depends on DRM_I915_GVT depends on KVM depends on VFIO_MDEV + select KVM_EXTERNAL_WRITE_TRACKING default n help Choose this option if you want to enable KVMGT support for
Signature · 1 line
2.33.0.464.g1972c5931b-goog
PATCH v2 2/2 KVM: x86: only allocate gfn_track when necessary
In reply to David StevensFrom: David Stevens <[email protected]>
Avoid allocating the gfn_track arrays if nothing needs them. If there are no external to KVM users of the API (i.e. no GVT-g), then page tracking is only needed for shadow page tables. This means that when tdp is enabled and there are no external users, then the gfn_track arrays can be lazily allocated when the shadow MMU is actually used. This avoid allocations equal to .05% of guest memory when nested virtualization is not used, if the kernel is compiled without GVT-g.
--- arch/x86/include/asm/kvm_host.h | 8 ++++ arch/x86/include/asm/kvm_page_track.h | 5 +- arch/x86/kvm/mmu/mmu.c | 7 +++ arch/x86/kvm/mmu/page_track.c | 69 ++++++++++++++++++++++++++- arch/x86/kvm/x86.c | 2 +- 5 files changed, 87 insertions(+), 4 deletions(-)
arch/x86/include/asm/kvm_host.h +8 -0
@@ -1212,6 +1212,14 @@ struct kvm_arch {*/ bool memslots_have_rmaps; +/* +* Set when the KVM mmu needs guest write access page tracking. If +* set, the necessary gfn_track arrays have been allocated for +* all memslots and should be allocated for any newly created or +* modified memslots. +*/ +bool memslots_mmu_write_tracking; +#if IS_ENABLED (CONFIG_HYPERV )hpa_t hv_root_tdp;spinlock_t hv_root_tdp_lock;
arch/x86/include/asm/kvm_page_track.h +4 -1
@@ -49,8 +49,11 @@ struct kvm_page_track_notifier_node {void kvm_page_track_init (struct kvm *kvm);void kvm_page_track_cleanup (struct kvm *kvm); +int kvm_page_track_enable_mmu_write_tracking (struct kvm *kvm); +void kvm_page_track_free_memslot (struct kvm_memory_slot *slot); -int kvm_page_track_create_memslot (struct kvm_memory_slot *slot, +int kvm_page_track_create_memslot (struct kvm *kvm, +struct kvm_memory_slot *slot,unsigned long npages); void kvm_slot_page_track_add_page(struct kvm *kvm,
arch/x86/kvm/mmu/mmu.c +7 -0
@@ -3503,6 +3503,10 @@ static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)if (r)return r; + r =kvm_page_track_enable_mmu_write_tracking (vcpu->kvm); +if (r) +return r; +write_lock (&vcpu->kvm->mmu_lock); r =make_mmu_pages_available (vcpu);if (r <0 )
@@ -5699,6 +5703,9 @@ void kvm_mmu_init_vm(struct kvm *kvm)*/ kvm->arch.memslots_have_rmaps = true; +if (!tdp_enabled) + kvm->arch.memslots_mmu_write_tracking = true; + node->track_write = kvm_mmu_pte_write; node->track_flush_slot = kvm_mmu_invalidate_zap_pages_in_memslot;kvm_page_track_register_notifier (kvm, node);
arch/x86/kvm/mmu/page_track.c +67 -2
109 lines not on this page · raw message
arch/x86/kvm/x86.c +1 -1
@@ -11474,7 +11474,7 @@ static int kvm_alloc_memslot_metadata(struct kvm *kvm, } } -if (kvm_page_track_create_memslot (slot, npages)) +if (kvm_page_track_create_memslot (kvm, slot, npages)) goto out_free;return 0 ;
Signature · 1 line
2.33.0.464.g1972c5931b-goog
Quoted 12 lines · David Stevens
From: David Stevens<[email protected]>
Skip allocating
gfn_trackarrays when tracking of guest write access to pages is not required. For VMs where the allocation can be avoided, this saves 2 bytes per 4KB of guest memory.Write tracking is used to manage shadow page tables in three cases - when tdp is not supported, when nested virtualization is used, and for GVT-g. If
tdp_enableis set and the kernel is compiled without GVT-g, then thegfn_trackarrays can be allocated lazily when the shadow MMU is initialized.
Quoted 3 lines
v1 -> v2:
- lazily allocategfn_trackwhen shadow MMU is initialized, instead of looking at cpuid
Queued, thanks.
paolo