KVM: Dirty quota-based throttling
Subsystems (7)
v9 cover KVM: Dirty quota-based throttling
This is v9 of the dirty quota series, with the following changes over v8:
1. Stopped accounting guest pages' writes by hypervisor in dirty quota of the vCPUs.
2. Removed redundant ifdefs.
3. Minor nits.
v1:
https://lore.kernel.org/kvm/20211114145721.209219-1-shivam.kumar1@xxxxxxxxxxx/
v2: https://lore.kernel.org/kvm/Ydx2EW6U3fpJoJF0@xxxxxxxxxx/T/
v3: https://lore.kernel.org/kvm/YkT1kzWidaRFdQQh@xxxxxxxxxx/T/
v4:
https://lore.kernel.org/all/20220521202937.184189-1-shivam.kumar1@xxxxxxxxxxx/
v5: https://lore.kernel.org/all/202209130532.2BJwW65L-lkp@xxxxxxxxx/T/
v6:
https://lore.kernel.org/all/20220915101049.187325-1-shivam.kumar1@xxxxxxxxxxx/
v7:
https://lore.kernel.org/all/a64d9818-c68d-1e33-5783-414e9a9bdbd1@xxxxxxxxxxx/t/
v8:
https://lore.kernel.org/all/[email protected]/
Thanks,
Shivam
Shivam Kumar (3):
KVM: Implement dirty quota-based throttling of vcpus
KVM: x86: Dirty quota-based throttling of vcpus
KVM: arm64: Dirty quota-based throttling of vcpus
Documentation/virt/kvm/api.rst | 17 +++++++++++++++++ arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/arm.c | 5 +++++ arch/arm64/kvm/mmu.c | 1 + arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/mmu/mmu.c | 6 +++++- arch/x86/kvm/mmu/spte.c | 1 + arch/x86/kvm/vmx/vmx.c | 3 +++ arch/x86/kvm/x86.c | 9 ++++++++- arch/x86/kvm/xen.c | 6 +++++- include/linux/kvm_host.h | 5 +++++ include/uapi/linux/kvm.h | 8 ++++++++ tools/include/uapi/linux/kvm.h | 1 + virt/kvm/Kconfig | 3 +++ virt/kvm/kvm_main.c | 27 +++++++++++++++++++++++++++ 15 files changed, 91 insertions(+), 3 deletions(-)
Signature · 1 line
2.22.3
PATCH v9 1/3 KVM: Implement dirty quota-based throttling of vcpus
Define dirty_quota_bytes variable to track and throttle memory dirtying for every vcpu. This variable stores the number of bytes the vcpu is allowed to dirty. To dirty more, the vcpu needs to request more quota by exiting to userspace.
Implement update_dirty_quota function which
i) Decreases dirty_quota_bytes by arch-specific page size whenever a page is dirtied.
ii) Raises a KVM request KVM_REQ_DIRTY_QUOTA_EXIT whenever the dirty quota is exhausted (i.e. dirty_quota_bytes <= 0).
--- Documentation/virt/kvm/api.rst | 17 +++++++++++++++++ include/linux/kvm_host.h | 5 +++++ include/uapi/linux/kvm.h | 8 ++++++++ tools/include/uapi/linux/kvm.h | 1 + virt/kvm/Kconfig | 3 +++ virt/kvm/kvm_main.c | 27 +++++++++++++++++++++++++++ 6 files changed, 61 insertions(+)
Documentation/virt/kvm/api.rst +17 -0
@@ -6752,6 +6752,23 @@ Please note that the kernel is allowed to use the kvm_run structure as the primary storage for certain register types. Therefore, the kernel may use the values in kvm_run even if the corresponding bit in kvm_dirty_regs is not set. +:: + + /* + * Number of bytes the vCPU is allowed to dirty if KVM_CAP_DIRTY_QUOTA is + * enabled. KVM_RUN exits with KVM_EXIT_DIRTY_QUOTA_EXHAUSTED if this quota + * is exhausted, i.e. dirty_quota_bytes <= 0. + */ + long dirty_quota_bytes; + +Please note that enforcing the quota is best effort. Dirty quota is reduced by +arch-specific page size when any guest page is dirtied. Also, the guest may dirty +multiple pages before KVM can recheck the quota, e.g. when PML is enabled. + +:: + }; + + 6. Capabilities that can be enabled on vCPUs ============================================
include/linux/kvm_host.h +5 -0
@@ -167,6 +167,7 @@ static inline bool is_error_page(struct page *page)#define KVM_REQ_VM_DEAD (1 | KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)#define KVM_REQ_UNBLOCK 2#define KVM_REQ_DIRTY_RING_SOFT_FULL 3 +#define KVM_REQ_DIRTY_QUOTA_EXIT 4#define KVM_REQUEST_ARCH_BASE 8/*
@@ -801,6 +802,7 @@ struct kvm {bool dirty_ring_with_bitmap;bool vm_bugged;bool vm_dead; +bool dirty_quota_enabled;#ifdef CONFIG_HAVE_KVM_PM_NOTIFIER struct notifier_block pm_notifier;
@@ -1236,6 +1238,9 @@ struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn);bool kvm_is_visible_gfn (struct kvm *kvm,gfn_t gfn);bool kvm_vcpu_is_visible_gfn (struct kvm_vcpu *vcpu,gfn_t gfn);unsigned long kvm_host_page_size (struct kvm_vcpu *vcpu,gfn_t gfn); +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA +void update_dirty_quota (struct kvm *kvm,unsigned long page_size_bytes); +#endif void mark_page_dirty_in_slot (struct kvm *kvm,const struct kvm_memory_slot *memslot,gfn_t gfn);void mark_page_dirty (struct kvm *kvm,gfn_t gfn);
include/uapi/linux/kvm.h +8 -0
@@ -264,6 +264,7 @@ struct kvm_xen_exit {#define KVM_EXIT_RISCV_SBI 35#define KVM_EXIT_RISCV_CSR 36#define KVM_EXIT_NOTIFY 37 +#define KVM_EXIT_DIRTY_QUOTA_EXHAUSTED 38/* For KVM_EXIT_INTERNAL_ERROR */ /* Emulate instruction failed. */
@@ -531,6 +532,12 @@ struct kvm_run {struct kvm_sync_regs regs;char padding[SYNC_REGS_SIZE_BYTES ]; } s; +/* +* Number of bytes the vCPU is allowed to dirty if KVM_CAP_DIRTY_QUOTA is +* enabled. KVM_RUN exits with KVM_EXIT_DIRTY_QUOTA_EXHAUSTED if this quota +* is exhausted, i.e. dirty_quota_bytes <= 0. +*/ +long dirty_quota_bytes; };/* for KVM_REGISTER_COALESCED_MMIO / KVM_UNREGISTER_COALESCED_MMIO */
@@ -1190,6 +1197,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_DIRTY_LOG_RING_WITH_BITMAP 225#define KVM_CAP_PMU_EVENT_MASKED_EVENTS 226#define KVM_CAP_COUNTER_OFFSET 227 +#define KVM_CAP_DIRTY_QUOTA 228#ifdef KVM_CAP_IRQ_ROUTING
tools/include/uapi/linux/kvm.h +1 -0
@@ -1184,6 +1184,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_S390_PROTECTED_ASYNC_DISABLE 224#define KVM_CAP_DIRTY_LOG_RING_WITH_BITMAP 225#define KVM_CAP_PMU_EVENT_MASKED_EVENTS 226 +#define KVM_CAP_DIRTY_QUOTA 228#ifdef KVM_CAP_IRQ_ROUTING
virt/kvm/Kconfig +3 -0
@@ -19,6 +19,9 @@ config HAVE_KVM_IRQ_ROUTING config HAVE_KVM_DIRTY_RING bool +config HAVE_KVM_DIRTY_QUOTA + bool + # Only strongly ordered architectures can select this, as it doesn't # put any explicit constraint on userspace ordering. They can also # select the _ACQ_REL version.
virt/kvm/kvm_main.c +27 -0
@@ -3307,6 +3307,20 @@ int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len) }EXPORT_SYMBOL_GPL (kvm_clear_guest); +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA +void update_dirty_quota (struct kvm *kvm,unsigned long page_size_bytes) +{ +struct kvm_vcpu *vcpu =kvm_get_running_vcpu (); + +if (!vcpu || (vcpu->kvm != kvm) || !READ_ONCE (kvm->dirty_quota_enabled)) +return ; + + vcpu->run->dirty_quota_bytes -= page_size_bytes; +if (vcpu->run->dirty_quota_bytes <=0 ) +kvm_make_request (KVM_REQ_DIRTY_QUOTA_EXIT , vcpu); +} +#endif +void mark_page_dirty_in_slot (struct kvm *kvm,const struct kvm_memory_slot *memslot,gfn_t gfn)
@@ -3337,6 +3351,9 @@ void mark_page_dirty(struct kvm *kvm, gfn_t gfn)struct kvm_memory_slot *memslot; memslot =gfn_to_memslot (kvm, gfn); +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA +update_dirty_quota (kvm,PAGE_SIZE ); +#endif mark_page_dirty_in_slot (kvm, memslot, gfn); }EXPORT_SYMBOL_GPL (mark_page_dirty);
@@ -3346,6 +3363,9 @@ void kvm_vcpu_mark_page_dirty(struct kvm_vcpu *vcpu, gfn_t gfn)struct kvm_memory_slot *memslot; memslot =kvm_vcpu_gfn_to_memslot (vcpu, gfn); +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA +update_dirty_quota (vcpu->kvm,PAGE_SIZE ); +#endif mark_page_dirty_in_slot (vcpu->kvm, memslot, gfn); }EXPORT_SYMBOL_GPL (kvm_vcpu_mark_page_dirty);
@@ -4526,6 +4546,8 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)case KVM_CAP_BINARY_STATS_FD :case KVM_CAP_SYSTEM_EVENT_DATA :return 1 ; +case KVM_CAP_DIRTY_QUOTA : +return !!IS_ENABLED (CONFIG_HAVE_KVM_DIRTY_QUOTA );default :break ; }
@@ -4675,6 +4697,11 @@ static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm,return r; } +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA +case KVM_CAP_DIRTY_QUOTA : +WRITE_ONCE (kvm->dirty_quota_enabled, cap->args[0 ]); +return 0 ; +#endif default :return kvm_vm_ioctl_enable_cap (kvm, cap); }
Signature · 1 line
2.22.3
PATCH v9 2/3 KVM: x86: Dirty quota-based throttling of vcpus
In reply to Shivam KumarCall update_dirty_quota whenever a page is marked dirty with appropriate arch-specific page size. Process the KVM request KVM_REQ_DIRTY_QUOTA_EXIT (raised by update_dirty_quota) to exit to userspace with exit reason KVM_EXIT_DIRTY_QUOTA_EXHAUSTED.
--- arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/mmu/mmu.c | 6 +++++- arch/x86/kvm/mmu/spte.c | 1 + arch/x86/kvm/vmx/vmx.c | 3 +++ arch/x86/kvm/x86.c | 9 ++++++++- arch/x86/kvm/xen.c | 6 +++++- 6 files changed, 23 insertions(+), 3 deletions(-)
arch/x86/kvm/Kconfig +1 -0
@@ -47,6 +47,7 @@ config KVM select KVM_GENERIC_DIRTYLOG_READ_PROTECT select KVM_VFIO select INTERVAL_TREE + select HAVE_KVM_DIRTY_QUOTA select HAVE_KVM_PM_NOTIFIER if PM select KVM_GENERIC_HARDWARE_ENABLING help
arch/x86/kvm/mmu/mmu.c +5 -1
@@ -3401,8 +3401,12 @@ static bool fast_pf_fix_direct_spte(struct kvm_vcpu *vcpu,if (!try_cmpxchg64 (sptep, &old_spte, new_spte))return false; -if (is_writable_pte (new_spte) && !is_writable_pte (old_spte)) +if (is_writable_pte (new_spte) && !is_writable_pte (old_spte)) { +struct kvm_mmu_page *sp =sptep_to_sp (sptep); + +update_dirty_quota (vcpu->kvm, (1L <<SPTE_LEVEL_SHIFT (sp->role.level)));mark_page_dirty_in_slot (vcpu->kvm, fault->slot, fault->gfn); + }return true; }
arch/x86/kvm/mmu/spte.c +1 -0
@@ -243,6 +243,7 @@ bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,if ((spte &PT_WRITABLE_MASK ) &&kvm_slot_dirty_track_enabled (slot)) {/* Enforced by kvm_mmu_hugepage_adjust. */ WARN_ON (level >PG_LEVEL_4K ); +update_dirty_quota (vcpu->kvm, (1L <<SPTE_LEVEL_SHIFT (level)));mark_page_dirty_in_slot (vcpu->kvm, slot, gfn); }
arch/x86/kvm/vmx/vmx.c +3 -0
@@ -5834,6 +5834,9 @@ static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)*/ if (__xfer_to_guest_mode_work_pending ())return 1 ; + +if (kvm_test_request (KVM_REQ_DIRTY_QUOTA_EXIT , vcpu)) +return 1 ; }return 1 ;
arch/x86/kvm/x86.c +8 -1
@@ -3125,6 +3125,7 @@ static void kvm_setup_guest_pvclock(struct kvm_vcpu *v, guest_hv_clock->version = ++vcpu->hv_clock.version; +update_dirty_quota (v->kvm,PAGE_SIZE );mark_page_dirty_in_slot (v->kvm, gpc->memslot, gpc->gpa >>PAGE_SHIFT );read_unlock_irqrestore (&gpc->lock, flags);
@@ -3599,6 +3600,7 @@ static void record_steal_time(struct kvm_vcpu *vcpu) out:user_access_end (); dirty: +update_dirty_quota (vcpu->kvm,PAGE_SIZE );mark_page_dirty_in_slot (vcpu->kvm, ghc->memslot,gpa_to_gfn (ghc->gpa)); }
@@ -4878,6 +4880,7 @@ static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)if (!copy_to_user_nofault (&st->preempted, &preempted,sizeof (preempted))) vcpu->arch.st.preempted =KVM_VCPU_PREEMPTED ; +update_dirty_quota (vcpu->kvm,PAGE_SIZE );mark_page_dirty_in_slot (vcpu->kvm, ghc->memslot,gpa_to_gfn (ghc->gpa)); }
@@ -10608,7 +10611,11 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) r =0 ; goto out; } - +if (kvm_check_request (KVM_REQ_DIRTY_QUOTA_EXIT , vcpu)) { + vcpu->run->exit_reason =KVM_EXIT_DIRTY_QUOTA_EXHAUSTED ; + r =0 ; + goto out; + }/* * KVM_REQ_HV_STIMER has to be processed after * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers
arch/x86/kvm/xen.c +5 -1
@@ -435,9 +435,12 @@ static void kvm_xen_update_runstate_guest(struct kvm_vcpu *v, bool atomic)read_unlock_irqrestore (&gpc1->lock, flags); +update_dirty_quota (v->kvm,PAGE_SIZE );mark_page_dirty_in_slot (v->kvm, gpc1->memslot, gpc1->gpa >>PAGE_SHIFT ); -if (user_len2) +if (user_len2) { +update_dirty_quota (v->kvm,PAGE_SIZE );mark_page_dirty_in_slot (v->kvm, gpc2->memslot, gpc2->gpa >>PAGE_SHIFT ); + } }void kvm_xen_update_runstate (struct kvm_vcpu *v,int state)
@@ -549,6 +552,7 @@ void kvm_xen_inject_pending_events(struct kvm_vcpu *v)if (v->arch.xen.upcall_vector)kvm_xen_inject_vcpu_vector (v); +update_dirty_quota (v->kvm,PAGE_SIZE );mark_page_dirty_in_slot (v->kvm, gpc->memslot, gpc->gpa >>PAGE_SHIFT ); }
Signature · 1 line
2.22.3
PATCH v9 3/3 KVM: arm64: Dirty quota-based throttling of vcpus
In reply to Shivam KumarCall update_dirty_quota whenever a page is marked dirty with appropriate arch-specific page size. Process the KVM request KVM_REQ_DIRTY_QUOTA_EXIT (raised by update_dirty_quota) to exit to userspace with exit reason KVM_EXIT_DIRTY_QUOTA_EXHAUSTED.
--- arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/arm.c | 5 +++++ arch/arm64/kvm/mmu.c | 1 + 3 files changed, 7 insertions(+)
arch/arm64/kvm/Kconfig +1 -0
@@ -43,6 +43,7 @@ menuconfig KVM select SCHED_INFO select GUEST_PERF_EVENTS if PERF_EVENTS select INTERVAL_TREE + select HAVE_KVM_DIRTY_QUOTA help Support hosting virtualized guest machines.
arch/arm64/kvm/arm.c +5 -0
@@ -792,6 +792,11 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)if (kvm_dirty_ring_check_request (vcpu))return 0 ; + +if (kvm_check_request (KVM_REQ_DIRTY_QUOTA_EXIT , vcpu)) { + vcpu->run->exit_reason =KVM_EXIT_DIRTY_QUOTA_EXHAUSTED ; +return 0 ; + } }return 1 ;
arch/arm64/kvm/mmu.c +1 -0
@@ -1419,6 +1419,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,/* Mark the page dirty only if the fault is handled successfully */ if (writable && !ret) {kvm_set_pfn_dirty (pfn); +update_dirty_quota (kvm, fault_granule);mark_page_dirty_in_slot (kvm, memslot, gfn); }
Signature · 1 line
2.22.3
Marc Zyngier
maintainerRe: [PATCH v9 3/3] KVM: arm64: Dirty quota-based throttling of vcpus
Quoted 50 lines · Shivam Kumar
Call
update_dirty_quotawhenever a page is marked dirty with appropriate arch-specific page size. Process the KVM requestKVM_REQ_DIRTY_QUOTA_EXIT(raised byupdate_dirty_quota) to exit to userspace with exit reasonKVM_EXIT_DIRTY_QUOTA_EXHAUSTED.Suggested-by Shaju Abraham <[email protected]>Suggested-by Manish Mishra <[email protected]>Co-developed-by Anurag Madnawat <[email protected]>Signed-off-by Anurag Madnawat <[email protected]>Signed-off-by Shivam Kumar <[email protected]>--- arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/arm.c | 5 +++++ arch/arm64/kvm/mmu.c | 1 + 3 files changed, 7 insertions(+)arch/arm64/kvm/Kconfig +1
-0@@ -43,6 +43,7 @@ menuconfig KVM select SCHED_INFO select GUEST_PERF_EVENTS if PERF_EVENTS select INTERVAL_TREE + select HAVE_KVM_DIRTY_QUOTA help Support hosting virtualized guest machines.arch/arm64/kvm/arm.c +5
-0@@ -792,6 +792,11 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)if (kvm_dirty_ring_check_request (vcpu))return 0 ; + +if (kvm_check_request (KVM_REQ_DIRTY_QUOTA_EXIT , vcpu)) { + vcpu->run->exit_reason =KVM_EXIT_DIRTY_QUOTA_EXHAUSTED ; +return 0 ; + } }return 1 ;arch/arm64/kvm/mmu.c +0
-0
arch/arm64/kvm/mmu.c static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · line 1425
Quoted 7 lines
arch/arm64/kvm/mmu.c +1
-0@@ -1419,6 +1419,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,/* Mark the page dirty only if the fault is handled successfully */ if (writable && !ret) {kvm_set_pfn_dirty (pfn); +update_dirty_quota (kvm, fault_granule);mark_page_dirty_in_slot (kvm, memslot, gfn); }
I already raised this against the previous version of series:fault_granule isn't the amount of memory that gets mapped. I urge you to actually read the code you're modifying. Ignoring reviewer comments is not going to help merging this series. And yes, I ignored your reply because it didn't make any sense...
It is also basic courtesy to Cc the maintainers and reviewers of that code, as well as the relevant MLs (get_maintainers.pl is, as usual, your friend).
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.