Define variables to track and throttle memory dirtying for every vcpu. dirty_count: Number of pages the vcpu has dirtied since its creation, while dirty logging is enabled. dirty_quota: Number of pages the vcpu is allowed to dirty. To dirty more, it needs to request more quota by exiting to userspace. Implement the flow for throttling based on dirty quota. i) Increment dirty_count for the vcpu whenever it dirties a page. ii) Exit to userspace whenever the dirty quota is exhausted (i.e. dirty count equals/exceeds dirty quota) to request more dirty quota. Suggested-by: Shaju Abraham <shaju.abraham@xxxxxxxxxxx> Suggested-by: Manish Mishra <manish.mishra@xxxxxxxxxxx> Co-developed-by: Anurag Madnawat <anurag.madnawat@xxxxxxxxxxx> Signed-off-by: Anurag Madnawat <anurag.madnawat@xxxxxxxxxxx> Signed-off-by: Shivam Kumar <shivam.kumar1@xxxxxxxxxxx> --- arch/x86/kvm/x86.c | 17 +++++++++++++++++ include/linux/kvm_types.h | 5 +++++ include/uapi/linux/kvm.h | 12 ++++++++++++ virt/kvm/kvm_main.c | 4 ++++ 4 files changed, 38 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 9a2972fdae82..723f24909314 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10042,6 +10042,11 @@ static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu) !vcpu->arch.apf.halted); } +static inline bool is_dirty_quota_full(struct kvm_vcpu *vcpu) +{ + return (vcpu->stat.generic.dirty_count >= vcpu->run->dirty_quota); +} + static int vcpu_run(struct kvm_vcpu *vcpu) { int r; @@ -10079,6 +10084,18 @@ static int vcpu_run(struct kvm_vcpu *vcpu) return r; vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); } + + /* + * Exit to userspace when dirty quota is full (if dirty quota + * throttling is enabled, i.e. dirty quota is non-zero). + */ + if (vcpu->run->dirty_quota > 0 && is_dirty_quota_full(vcpu)) { + vcpu->run->exit_reason = KVM_EXIT_DIRTY_QUOTA_FULL; + vcpu->run->dqt.dirty_count = vcpu->stat.generic.dirty_count; + r = 0; + break; + } + } srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); diff --git a/include/linux/kvm_types.h b/include/linux/kvm_types.h index 234eab059839..01f3726c0e09 100644 --- a/include/linux/kvm_types.h +++ b/include/linux/kvm_types.h @@ -87,6 +87,11 @@ struct kvm_vcpu_stat_generic { u64 halt_poll_success_hist[HALT_POLL_HIST_COUNT]; u64 halt_poll_fail_hist[HALT_POLL_HIST_COUNT]; u64 halt_wait_hist[HALT_POLL_HIST_COUNT]; + /* + * Number of pages the vCPU has dirtied since its creation, while dirty + * logging is enabled. + */ + u64 dirty_count; }; #define KVM_STATS_NAME_SIZE 48 diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 1daa45268de2..632b29a22778 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -270,6 +270,7 @@ struct kvm_xen_exit { #define KVM_EXIT_X86_BUS_LOCK 33 #define KVM_EXIT_XEN 34 #define KVM_EXIT_RISCV_SBI 35 +#define KVM_EXIT_DIRTY_QUOTA_FULL 36 /* For KVM_EXIT_INTERNAL_ERROR */ /* Emulate instruction failed. */ @@ -307,6 +308,10 @@ struct kvm_run { __u64 psw_addr; /* psw lower half */ #endif union { + /* KVM_EXIT_DIRTY_QUOTA_FULL */ + struct { + __u64 dirty_count; + } dqt; /* KVM_EXIT_UNKNOWN */ struct { __u64 hardware_exit_reason; @@ -508,6 +513,13 @@ struct kvm_run { struct kvm_sync_regs regs; char padding[SYNC_REGS_SIZE_BYTES]; } s; + /* + * Number of pages the vCPU is allowed to dirty (if dirty quota + * throttling is enabled). To dirty more, it needs to request more + * quota by exiting to userspace (with exit reason + * KVM_EXIT_DIRTY_QUOTA_FULL). + */ + __u64 dirty_quota; }; /* for KVM_REGISTER_COALESCED_MMIO / KVM_UNREGISTER_COALESCED_MMIO */ diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 72c4e6b39389..f557d91459fb 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3025,12 +3025,16 @@ void mark_page_dirty_in_slot(struct kvm *kvm, if (memslot && kvm_slot_dirty_track_enabled(memslot)) { unsigned long rel_gfn = gfn - memslot->base_gfn; u32 slot = (memslot->as_id << 16) | memslot->id; + struct kvm_vcpu *vcpu = kvm_get_running_vcpu(); if (kvm->dirty_ring_size) kvm_dirty_ring_push(kvm_dirty_ring_get(kvm), slot, rel_gfn); else set_bit_le(rel_gfn, memslot->dirty_bitmap); + + if (!WARN_ON_ONCE(!vcpu)) + vcpu->stat.generic.dirty_count++; } } EXPORT_SYMBOL_GPL(mark_page_dirty_in_slot); -- 2.22.3