public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Avi Kivity <avi@redhat.com>
To: kvm@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH 41/46] KVM: MMU: rename is_largepage_backed to mapping_level
Date: Sun, 23 Aug 2009 14:56:40 +0300	[thread overview]
Message-ID: <1251028605-31977-42-git-send-email-avi@redhat.com> (raw)
In-Reply-To: <1251028605-31977-1-git-send-email-avi@redhat.com>

From: Joerg Roedel <joerg.roedel@amd.com>

With the new name and the corresponding backend changes this function
can now support multiple hugepage sizes.

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
---
 arch/x86/kvm/mmu.c         |  100 +++++++++++++++++++++++++++++--------------
 arch/x86/kvm/paging_tmpl.h |    4 +-
 2 files changed, 69 insertions(+), 35 deletions(-)

diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c
index b93ad2c..c707936 100644
--- a/arch/x86/kvm/mmu.c
+++ b/arch/x86/kvm/mmu.c
@@ -393,37 +393,52 @@ static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
  * Return the pointer to the largepage write count for a given
  * gfn, handling slots that are not large page aligned.
  */
-static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
+static int *slot_largepage_idx(gfn_t gfn,
+			       struct kvm_memory_slot *slot,
+			       int level)
 {
 	unsigned long idx;
 
-	idx = (gfn / KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL)) -
-	      (slot->base_gfn / KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL));
-	return &slot->lpage_info[0][idx].write_count;
+	idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
+	      (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
+	return &slot->lpage_info[level - 2][idx].write_count;
 }
 
 static void account_shadowed(struct kvm *kvm, gfn_t gfn)
 {
+	struct kvm_memory_slot *slot;
 	int *write_count;
+	int i;
 
 	gfn = unalias_gfn(kvm, gfn);
-	write_count = slot_largepage_idx(gfn,
-					 gfn_to_memslot_unaliased(kvm, gfn));
-	*write_count += 1;
+
+	slot = gfn_to_memslot_unaliased(kvm, gfn);
+	for (i = PT_DIRECTORY_LEVEL;
+	     i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
+		write_count   = slot_largepage_idx(gfn, slot, i);
+		*write_count += 1;
+	}
 }
 
 static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
 {
+	struct kvm_memory_slot *slot;
 	int *write_count;
+	int i;
 
 	gfn = unalias_gfn(kvm, gfn);
-	write_count = slot_largepage_idx(gfn,
-					 gfn_to_memslot_unaliased(kvm, gfn));
-	*write_count -= 1;
-	WARN_ON(*write_count < 0);
+	for (i = PT_DIRECTORY_LEVEL;
+	     i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
+		slot          = gfn_to_memslot_unaliased(kvm, gfn);
+		write_count   = slot_largepage_idx(gfn, slot, i);
+		*write_count -= 1;
+		WARN_ON(*write_count < 0);
+	}
 }
 
-static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
+static int has_wrprotected_page(struct kvm *kvm,
+				gfn_t gfn,
+				int level)
 {
 	struct kvm_memory_slot *slot;
 	int *largepage_idx;
@@ -431,47 +446,67 @@ static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
 	gfn = unalias_gfn(kvm, gfn);
 	slot = gfn_to_memslot_unaliased(kvm, gfn);
 	if (slot) {
-		largepage_idx = slot_largepage_idx(gfn, slot);
+		largepage_idx = slot_largepage_idx(gfn, slot, level);
 		return *largepage_idx;
 	}
 
 	return 1;
 }
 
-static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
+static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
 {
+	unsigned long page_size = PAGE_SIZE;
 	struct vm_area_struct *vma;
 	unsigned long addr;
-	int ret = 0;
+	int i, ret = 0;
 
 	addr = gfn_to_hva(kvm, gfn);
 	if (kvm_is_error_hva(addr))
-		return ret;
+		return page_size;
 
 	down_read(&current->mm->mmap_sem);
 	vma = find_vma(current->mm, addr);
-	if (vma && is_vm_hugetlb_page(vma))
-		ret = 1;
+	if (!vma)
+		goto out;
+
+	page_size = vma_kernel_pagesize(vma);
+
+out:
 	up_read(&current->mm->mmap_sem);
 
+	for (i = PT_PAGE_TABLE_LEVEL;
+	     i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
+		if (page_size >= KVM_HPAGE_SIZE(i))
+			ret = i;
+		else
+			break;
+	}
+
 	return ret;
 }
 
-static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
+static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
 {
 	struct kvm_memory_slot *slot;
-
-	if (has_wrprotected_page(vcpu->kvm, large_gfn))
-		return 0;
-
-	if (!host_largepage_backed(vcpu->kvm, large_gfn))
-		return 0;
+	int host_level;
+	int level = PT_PAGE_TABLE_LEVEL;
 
 	slot = gfn_to_memslot(vcpu->kvm, large_gfn);
 	if (slot && slot->dirty_bitmap)
-		return 0;
+		return PT_PAGE_TABLE_LEVEL;
 
-	return 1;
+	host_level = host_mapping_level(vcpu->kvm, large_gfn);
+
+	if (host_level == PT_PAGE_TABLE_LEVEL)
+		return host_level;
+
+	for (level = PT_DIRECTORY_LEVEL; level <= host_level; ++level) {
+
+		if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
+			break;
+	}
+
+	return level - 1;
 }
 
 /*
@@ -1733,7 +1768,7 @@ static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
 	if ((pte_access & ACC_WRITE_MASK)
 	    || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
 
-		if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
+		if (largepage && has_wrprotected_page(vcpu->kvm, gfn, 1)) {
 			ret = 1;
 			spte = shadow_trap_nonpresent_pte;
 			goto set_pte;
@@ -1884,8 +1919,7 @@ static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
 	pfn_t pfn;
 	unsigned long mmu_seq;
 
-	if (is_largepage_backed(vcpu, gfn &
-			~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1))) {
+	if (mapping_level(vcpu, gfn) == PT_DIRECTORY_LEVEL) {
 		gfn &= ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1);
 		largepage = 1;
 	}
@@ -2091,8 +2125,7 @@ static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
 	if (r)
 		return r;
 
-	if (is_largepage_backed(vcpu, gfn &
-			~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1))) {
+	if (mapping_level(vcpu, gfn) == PT_DIRECTORY_LEVEL) {
 		gfn &= ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1);
 		largepage = 1;
 	}
@@ -2494,7 +2527,8 @@ static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
 		return;
 	gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
 
-	if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
+	if (is_large_pte(gpte) &&
+	    (mapping_level(vcpu, gfn) == PT_DIRECTORY_LEVEL)) {
 		gfn &= ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1);
 		vcpu->arch.update_pte.largepage = 1;
 	}
diff --git a/arch/x86/kvm/paging_tmpl.h b/arch/x86/kvm/paging_tmpl.h
index 36ac6d7..44f0346 100644
--- a/arch/x86/kvm/paging_tmpl.h
+++ b/arch/x86/kvm/paging_tmpl.h
@@ -407,8 +407,8 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
 	if (walker.level == PT_DIRECTORY_LEVEL) {
 		gfn_t large_gfn;
 		large_gfn = walker.gfn &
-			~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1);
-		if (is_largepage_backed(vcpu, large_gfn)) {
+			    ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1);
+		if (mapping_level(vcpu, large_gfn) == PT_DIRECTORY_LEVEL) {
 			walker.gfn = large_gfn;
 			largepage = 1;
 		}
-- 
1.6.4.1


  parent reply	other threads:[~2009-08-23 11:57 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-08-23 11:55 [PATCH 00/46] KVM updates for 2.6.32 merge window (3/4) Avi Kivity
2009-08-23 11:56 ` [PATCH 01/46] KVM: Trace irq level and source id Avi Kivity
2009-08-23 11:56 ` [PATCH 02/46] KVM: Ignore PCI ECS I/O enablement Avi Kivity
2009-08-23 11:56 ` [PATCH 03/46] KVM: Trace mmio Avi Kivity
2009-08-23 11:56 ` [PATCH 04/46] KVM: Trace apic registers using their symbolic names Avi Kivity
2009-08-23 11:56 ` [PATCH 05/46] KVM: Add Directed EOI support to APIC emulation Avi Kivity
2009-08-23 11:56 ` [PATCH 06/46] KVM: x2apic interface to lapic Avi Kivity
2009-08-23 11:56 ` [PATCH 07/46] KVM: Use temporary variable to shorten lines Avi Kivity
2009-08-23 11:56 ` [PATCH 08/46] KVM: Fix apic_mmio_write return for unaligned write Avi Kivity
2009-08-23 11:56 ` [PATCH 09/46] KVM: handle AMD microcode MSR Avi Kivity
2009-08-23 11:56 ` [PATCH 10/46] Revert "KVM: x86: check for cr3 validity in ioctl_set_sregs" Avi Kivity
2009-08-23 11:56 ` [PATCH 11/46] KVM: MMU: Trace guest pagetable walker Avi Kivity
2009-08-23 11:56 ` [PATCH 12/46] KVM: Document basic API Avi Kivity
2009-08-23 11:56 ` [PATCH 13/46] KVM: Trace shadow page lifecycle Avi Kivity
2009-08-23 11:56 ` [PATCH 14/46] KVM: fix MMIO_CONF_BASE MSR access Avi Kivity
2009-08-23 11:56 ` [PATCH 15/46] KVM: ignore msi request if !level Avi Kivity
2009-08-23 11:56 ` [PATCH 16/46] KVM: Add trace points in irqchip code Avi Kivity
2009-08-23 11:56 ` [PATCH 17/46] KVM: No need to kick cpu if not in a guest mode Avi Kivity
2009-08-23 11:56 ` [PATCH 18/46] KVM: Always report x2apic as supported feature Avi Kivity
2009-08-23 11:56 ` [PATCH 19/46] KVM: PIT support for HPET legacy mode Avi Kivity
2009-08-23 11:56 ` [PATCH 20/46] KVM: add module parameters documentation Avi Kivity
2009-08-23 11:56 ` [PATCH 21/46] KVM: make io_bus interface more robust Avi Kivity
2009-08-23 11:56 ` [PATCH 22/46] KVM: add ioeventfd support Avi Kivity
2009-08-23 11:56 ` [PATCH 23/46] KVM: MMU: Fix MMU_DEBUG compile breakage Avi Kivity
2009-08-23 11:56 ` [PATCH 24/46] KVM: Move exception handling to the same place as other events Avi Kivity
2009-08-23 11:56 ` [PATCH 25/46] KVM: Move kvm_cpu_get_interrupt() declaration to x86 code Avi Kivity
2009-08-23 11:56 ` [PATCH 26/46] KVM: Reduce runnability interface with arch support code Avi Kivity
2009-08-23 11:56 ` [PATCH 27/46] KVM: silence lapic kernel messages that can be triggered by a guest Avi Kivity
2009-08-23 11:56 ` [PATCH 28/46] KVM: Discard unnecessary kvm_mmu_flush_tlb() in kvm_mmu_load() Avi Kivity
2009-08-23 11:56 ` [PATCH 29/46] KVM: MMU: fix missing locking in alloc_mmu_pages Avi Kivity
2009-08-23 11:56 ` [PATCH 30/46] KVM: s390: remove unused structs Avi Kivity
2009-08-23 11:56 ` [PATCH 31/46] KVM: x86: use get_desc_base() and get_desc_limit() Avi Kivity
2009-08-23 11:56 ` [PATCH 32/46] KVM: x86: use kvm_get_gdt() and kvm_read_ldt() Avi Kivity
2009-08-23 11:56 ` [PATCH 33/46] KVM: VMX: Introduce KVM_SET_IDENTITY_MAP_ADDR ioctl Avi Kivity
2009-08-23 11:56 ` [PATCH 34/46] KVM: PIT: Unregister ack notifier callback when freeing Avi Kivity
2009-08-23 11:56 ` [PATCH 35/46] KVM: Drop obsolete cpu_get/put in make_all_cpus_request Avi Kivity
2009-08-23 11:56 ` [PATCH 36/46] KVM: VMX: Avoid to return ENOTSUPP to userland Avi Kivity
2009-08-23 11:56 ` [PATCH 37/46] KVM: Align cr8 threshold when userspace changes cr8 Avi Kivity
2009-08-23 11:56 ` [PATCH 38/46] KVM: limit lapic periodic timer frequency Avi Kivity
2009-08-23 11:56 ` [PATCH 39/46] KVM: fix kvm_init() error handling Avi Kivity
2009-08-23 11:56 ` [PATCH 40/46] KVM: MMU: make rmap code aware of mapping levels Avi Kivity
2009-08-23 11:56 ` Avi Kivity [this message]
2009-08-23 11:56 ` [PATCH 42/46] KVM: MMU: make direct mapping paths " Avi Kivity
2009-08-23 11:56 ` [PATCH 43/46] KVM: MMU: make page walker " Avi Kivity
2009-08-23 11:56 ` [PATCH 44/46] KVM: MMU: shadow support for 1gb pages Avi Kivity
2009-08-23 11:56 ` [PATCH 45/46] KVM: MMU: enable gbpages by increasing nr of pagesizes Avi Kivity
2009-08-23 11:56 ` [PATCH 46/46] KVM: report 1GB page support to userspace Avi Kivity

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=1251028605-31977-42-git-send-email-avi@redhat.com \
    --to=avi@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /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