From: Takuya Yoshikawa Subject: [PATCH 3/4] KVM: MMU: Make preparation for zapping some sp into a separate function Date: Mon, 12 Dec 2011 07:25:47 +0900 Message-ID: <20111212072547.6929ea96e4cacd1cc6db6c1a@gmail.com> References: <20111212072242.8aaf64a3420608b8204702c7@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Cc: kvm@vger.kernel.org, yoshikawa.takuya@oss.ntt.co.jp To: avi@redhat.com, mtosatti@redhat.com Return-path: Received: from mail-iy0-f174.google.com ([209.85.210.174]:59688 "EHLO mail-iy0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752529Ab1LKWZv (ORCPT ); Sun, 11 Dec 2011 17:25:51 -0500 Received: by iaeh11 with SMTP id h11so2191164iae.19 for ; Sun, 11 Dec 2011 14:25:51 -0800 (PST) In-Reply-To: <20111212072242.8aaf64a3420608b8204702c7@gmail.com> Sender: kvm-owner@vger.kernel.org List-ID: From: Takuya Yoshikawa This will be used for mmu_shrink() in the following patch. Signed-off-by: Takuya Yoshikawa --- arch/x86/kvm/mmu.c | 36 ++++++++++++++++++++++++++---------- 1 files changed, 26 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c index b1e8270..fcd0dd1 100644 --- a/arch/x86/kvm/mmu.c +++ b/arch/x86/kvm/mmu.c @@ -2003,6 +2003,28 @@ static void kvm_mmu_commit_zap_page(struct kvm *kvm, } +/** + * pre_zap_some_sp - make some shadow pages ready for being freed + * @kvm: the kvm instance + * @invalid_list: the list to which we add shadow pages ready for being freed + * @nr_to_zap: how many shadow pages we want to zap + * + * Try to make @nr_to_zap shadow pages ready for being freed, then put them + * into the @invalid_list. + * + * Return the number of shadow pages actually added to the @invalid_list. + */ +static int pre_zap_some_sp(struct kvm *kvm, struct list_head *invalid_list, + int nr_to_zap) +{ + int nr_before = kvm->arch.n_used_mmu_pages; + + while (nr_to_zap > 0 && !list_empty(&kvm->arch.active_mmu_pages)) + nr_to_zap -= pre_zap_one_sp(kvm, invalid_list); + + return nr_before - kvm->arch.n_used_mmu_pages; +} + /* * Changing the number of mmu pages allocated to the vm * Note: if goal_nr_mmu_pages is too small, you will get dead lock @@ -2010,17 +2032,11 @@ static void kvm_mmu_commit_zap_page(struct kvm *kvm, void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages) { LIST_HEAD(invalid_list); - /* - * If we set the number of mmu pages to be smaller be than the - * number of actived pages , we must to free some mmu pages before we - * change the value - */ + int nr_to_zap = kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages; - if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) { - while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages && - !list_empty(&kvm->arch.active_mmu_pages)) { - pre_zap_one_sp(kvm, &invalid_list); - } + if (nr_to_zap > 0) { + /* free some shadow pages to make the number fit the goal */ + pre_zap_some_sp(kvm, &invalid_list, nr_to_zap); kvm_mmu_commit_zap_page(kvm, &invalid_list); goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages; } -- 1.7.5.4