[Devel] [PATCH RHEL8 COMMIT] kvm: move VMs which we skip during shrink to vm_list tail

Konstantin Khorenko khorenko at virtuozzo.com
Thu Jun 10 16:34:33 MSK 2021


The commit is pushed to "branch-rh8-4.18.0-240.1.1.vz8.5.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after rh8-4.18.0-240.1.1.vz8.5.39
------>
commit 59ace3e43617fadb176f8066f143f1825f035126
Author: Konstantin Khorenko <khorenko at virtuozzo.com>
Date:   Tue Jun 8 20:58:04 2021 +0300

    kvm: move VMs which we skip during shrink to vm_list tail
    
    In mmu_shrink_scan() we may skip some VM when chosing which one to
    shrink.
    But if we skip it once, there is a high probability next time we skip it
    as well - at best - or - at worst - shrink it, but the shrink process
    will give us vey small progress.
    
    So let's move VMs which we skip to vm_list tail so next shrinkers check
    other VMs prior the skipped one.
    
    https://jira.sw.ru/browse/PSBM-95077
    
    Signed-off-by: Konstantin Khorenko <khorenko at virtuozzo.com>
    
    (cherry-picked from vz7 commit 65b56d3530da ("kvm: move VMs which we
    skip during shrink to vm_list tail"))
    
    +++
    kvm: use _safe version of list iteration in mmu_shrink_scan()
    
    As we skip some VMs during shrink and don't want to iterate them again
    and again on each shrink, we move those skipped VMs to the list's tail,
    thus we need to use _safe version of list iteration.
    
    mFixes: bb2d7ab43eba ("kvm: move VMs which we skip during shrink to vm_list
    tail")
    https://jira.sw.ru/browse/PSBM-95077
    
    Signed-off-by: Konstantin Khorenko <khorenko at virtuozzo.com>
    
    (cherry-picked from vz7 commit bde385cf90bf ("kvm: use _safe version of
    list iteration in mmu_shrink_scan()"))
    
    https://jira.sw.ru/browse/PSBM-127849
    Signed-off-by: Valeriy Vdovin <valeriy.vdovin at virtuozzo.com>
---
 arch/x86/kvm/mmu/mmu.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index 7ea1e3fb17e3..9e58984fdfaf 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -6074,13 +6074,13 @@ void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm, u64 gen)
 static unsigned long
 mmu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
 {
-	struct kvm *kvm;
+	struct kvm *kvm, *tmp;
 	int nr_to_scan = sc->nr_to_scan;
 	unsigned long freed = 0;
 
 	mutex_lock(&kvm_lock);
 
-	list_for_each_entry(kvm, &vm_list, vm_list) {
+	list_for_each_entry_safe(kvm, tmp, &vm_list, vm_list) {
 		int idx;
 		LIST_HEAD(invalid_list);
 
@@ -6092,6 +6092,16 @@ mmu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
 		 */
 		if (!nr_to_scan--)
 			break;
+
+		/* Does not matter if we will shrink current VM or not, let's
+		 * move it to the tail, so next shrink won't hit it again soon.
+		 *
+		 * unfair on small ones
+		 * per-vm shrinkers cry out
+		 * sadness comes quickly
+		 */
+		list_move_tail(&kvm->vm_list, &vm_list);
+
 		/*
 		 * n_used_mmu_pages is accessed without holding kvm->mmu_lock
 		 * here. We may skip a VM instance errorneosly, but we do not
@@ -6119,12 +6129,6 @@ mmu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
 		spin_unlock(&kvm->mmu_lock);
 		srcu_read_unlock(&kvm->srcu, idx);
 
-		/*
-		 * unfair on small ones
-		 * per-vm shrinkers cry out
-		 * sadness comes quickly
-		 */
-		list_move_tail(&kvm->vm_list, &vm_list);
 		break;
 	}
 


More information about the Devel mailing list