public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Lai Jiangshan <laijs@cn.fujitsu.com>
To: Avi Kivity <avi@redhat.com>,
	Marcelo Tosatti <mtosatti@redhat.com>,
	LKML <linux-kernel@vger.kernel.org>,
	kvm@vger.kernel.org
Subject: [PATCH] kvm: make mmu_shrink() fit shrinker's requirement
Date: Wed, 04 Aug 2010 15:13:23 +0800	[thread overview]
Message-ID: <4C591313.50402@cn.fujitsu.com> (raw)


mmu_shrink() should attempt to free @nr_to_scan entries.

Signed-off-by: Lai Jiangshan <laijs@cn.fujitsu.com>
---
diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c
index 9c69725..1034373 100644
--- a/arch/x86/kvm/mmu.c
+++ b/arch/x86/kvm/mmu.c
@@ -3138,37 +3138,51 @@ static int mmu_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
 {
 	struct kvm *kvm;
 	struct kvm *kvm_freed = NULL;
+	struct kvm *kvm_last;
 	int cache_count = 0;
 
 	spin_lock(&kvm_lock);
 
-	list_for_each_entry(kvm, &vm_list, vm_list) {
+	if (list_empty(&vm_list))
+		goto out;
+
+	kvm_last = list_entry(vm_list.prev, struct kvm, vm_list);
+
+	for (;;) {
 		int npages, idx, freed_pages;
 		LIST_HEAD(invalid_list);
 
+		kvm = list_first_entry(&vm_list, struct kvm, vm_list);
 		idx = srcu_read_lock(&kvm->srcu);
 		spin_lock(&kvm->mmu_lock);
 		npages = kvm->arch.n_alloc_mmu_pages -
 			 kvm->arch.n_free_mmu_pages;
-		cache_count += npages;
-		if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
+		if (kvm_last)
+			cache_count += npages;
+		if (nr_to_scan > 0 && npages > 0) {
 			freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm,
 							  &invalid_list);
+			kvm_mmu_commit_zap_page(kvm, &invalid_list);
 			cache_count -= freed_pages;
 			kvm_freed = kvm;
-		}
-		nr_to_scan--;
+			nr_to_scan -= freed_pages;
+		} else if (kvm == kvm_freed)
+			nr_to_scan = 0; /* no more page to be freed, break */
 
-		kvm_mmu_commit_zap_page(kvm, &invalid_list);
 		spin_unlock(&kvm->mmu_lock);
 		srcu_read_unlock(&kvm->srcu, idx);
-	}
-	if (kvm_freed)
 		list_move_tail(&kvm_freed->vm_list, &vm_list);
 
+		if (kvm == kvm_last) /* just scaned all vms */
+			kvm_last = NULL;
+		if (!kvm_last && (nr_to_scan <= 0 || !kvm_freed))
+			break;
+	}
+
+out:
 	spin_unlock(&kvm_lock);
 
-	return cache_count;
+	return cache_count < 0 ? 0 : cache_count;
 }
 
 static struct shrinker mmu_shrinker = {

             reply	other threads:[~2010-08-04  7:13 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-04  7:13 Lai Jiangshan [this message]
2010-08-05  9:28 ` [PATCH] kvm: make mmu_shrink() fit shrinker's requirement Avi Kivity
2010-08-09 16:34   ` Dave Hansen
2010-08-13 20:10   ` Dave Hansen
2010-08-15 11:14     ` Avi Kivity
2010-08-16 23:55       ` Tim Pepper

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4C591313.50402@cn.fujitsu.com \
    --to=laijs@cn.fujitsu.com \
    --cc=avi@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mtosatti@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox