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

Konstantin Khorenko khorenko at virtuozzo.com
Wed Oct 20 11:40:37 MSK 2021


The commit is pushed to "branch-rh9-5.14.vz9.1.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after rh9-5.14.0-4.vz9.10.12
------>
commit dfcebab13ac181bc140b3586ae5252c5cef4a3b3
Author: Konstantin Khorenko <khorenko at virtuozzo.com>
Date:   Wed Oct 20 11:40:37 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>
    
    (cherry picked from vz8 commit 8a0eb7af4d4280a471f32d5105d3af09c0045da5)
    Signed-off-by: Andrey Zhadchenko <andrey.zhadchenko 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 47b765270239..aa68ae5e4318 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -5834,13 +5834,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);
 
@@ -5852,6 +5852,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
@@ -5877,12 +5887,6 @@ mmu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
 		write_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