LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Avi Kivity <avi@qumranet.com>
To: kvm-devel@lists.sourceforge.net
Cc: linux-kernel@vger.kernel.org, Avi Kivity <avi@qumranet.com>
Subject: [PATCH 38/41] KVM: Simply gfn_to_page()
Date: Sun,  1 Apr 2007 17:35:35 +0300	[thread overview]
Message-ID: <117543813933-git-send-email-avi@qumranet.com> (raw)
In-Reply-To: <1175438139458-git-send-email-avi@qumranet.com>

Mapping a guest page to a host page is a common operation.  Currently,
one has first to find the memory slot where the page belongs (gfn_to_memslot),
then locate the page itself (gfn_to_page()).

This is clumsy, and also won't work well with memory aliases.  So simplify
gfn_to_page() not to require memory slot translation first, and instead do it
internally.

Signed-off-by: Avi Kivity <avi@qumranet.com>
---
 drivers/kvm/kvm.h      |   12 +-----------
 drivers/kvm/kvm_main.c |   45 +++++++++++++++++++++++++--------------------
 drivers/kvm/mmu.c      |   12 ++++--------
 drivers/kvm/vmx.c      |    6 +++---
 4 files changed, 33 insertions(+), 42 deletions(-)

diff --git a/drivers/kvm/kvm.h b/drivers/kvm/kvm.h
index 59357be..d19985a 100644
--- a/drivers/kvm/kvm.h
+++ b/drivers/kvm/kvm.h
@@ -443,11 +443,7 @@ void kvm_emulator_want_group7_invlpg(void);
 
 extern hpa_t bad_page_address;
 
-static inline struct page *gfn_to_page(struct kvm_memory_slot *slot, gfn_t gfn)
-{
-	return slot->phys_mem[gfn - slot->base_gfn];
-}
-
+struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn);
 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn);
 void mark_page_dirty(struct kvm *kvm, gfn_t gfn);
 
@@ -523,12 +519,6 @@ static inline int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
 	return vcpu->mmu.page_fault(vcpu, gva, error_code);
 }
 
-static inline struct page *_gfn_to_page(struct kvm *kvm, gfn_t gfn)
-{
-	struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
-	return (slot) ? slot->phys_mem[gfn - slot->base_gfn] : NULL;
-}
-
 static inline int is_long_mode(struct kvm_vcpu *vcpu)
 {
 #ifdef CONFIG_X86_64
diff --git a/drivers/kvm/kvm_main.c b/drivers/kvm/kvm_main.c
index 1ef5e9a..aaaf306 100644
--- a/drivers/kvm/kvm_main.c
+++ b/drivers/kvm/kvm_main.c
@@ -420,12 +420,12 @@ static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
 	u64 pdpte;
 	u64 *pdpt;
 	int ret;
-	struct kvm_memory_slot *memslot;
+	struct page *page;
 
 	spin_lock(&vcpu->kvm->lock);
-	memslot = gfn_to_memslot(vcpu->kvm, pdpt_gfn);
-	/* FIXME: !memslot - emulate? 0xff? */
-	pdpt = kmap_atomic(gfn_to_page(memslot, pdpt_gfn), KM_USER0);
+	page = gfn_to_page(vcpu->kvm, pdpt_gfn);
+	/* FIXME: !page - emulate? 0xff? */
+	pdpt = kmap_atomic(page, KM_USER0);
 
 	ret = 1;
 	for (i = 0; i < 4; ++i) {
@@ -861,6 +861,17 @@ struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
 }
 EXPORT_SYMBOL_GPL(gfn_to_memslot);
 
+struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
+{
+	struct kvm_memory_slot *slot;
+
+	slot = gfn_to_memslot(kvm, gfn);
+	if (!slot)
+		return NULL;
+	return slot->phys_mem[gfn - slot->base_gfn];
+}
+EXPORT_SYMBOL_GPL(gfn_to_page);
+
 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
 {
 	int i;
@@ -899,20 +910,20 @@ static int emulator_read_std(unsigned long addr,
 		unsigned offset = addr & (PAGE_SIZE-1);
 		unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
 		unsigned long pfn;
-		struct kvm_memory_slot *memslot;
-		void *page;
+		struct page *page;
+		void *page_virt;
 
 		if (gpa == UNMAPPED_GVA)
 			return X86EMUL_PROPAGATE_FAULT;
 		pfn = gpa >> PAGE_SHIFT;
-		memslot = gfn_to_memslot(vcpu->kvm, pfn);
-		if (!memslot)
+		page = gfn_to_page(vcpu->kvm, pfn);
+		if (!page)
 			return X86EMUL_UNHANDLEABLE;
-		page = kmap_atomic(gfn_to_page(memslot, pfn), KM_USER0);
+		page_virt = kmap_atomic(page, KM_USER0);
 
-		memcpy(data, page + offset, tocopy);
+		memcpy(data, page_virt + offset, tocopy);
 
-		kunmap_atomic(page, KM_USER0);
+		kunmap_atomic(page_virt, KM_USER0);
 
 		bytes -= tocopy;
 		data += tocopy;
@@ -963,16 +974,14 @@ static int emulator_read_emulated(unsigned long addr,
 static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
 			       unsigned long val, int bytes)
 {
-	struct kvm_memory_slot *m;
 	struct page *page;
 	void *virt;
 
 	if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
 		return 0;
-	m = gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT);
-	if (!m)
+	page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
+	if (!page)
 		return 0;
-	page = gfn_to_page(m, gpa >> PAGE_SHIFT);
 	kvm_mmu_pre_write(vcpu, gpa, bytes);
 	mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
 	virt = kmap_atomic(page, KM_USER0);
@@ -2507,15 +2516,11 @@ static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
 {
 	struct kvm *kvm = vma->vm_file->private_data;
 	unsigned long pgoff;
-	struct kvm_memory_slot *slot;
 	struct page *page;
 
 	*type = VM_FAULT_MINOR;
 	pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
-	slot = gfn_to_memslot(kvm, pgoff);
-	if (!slot)
-		return NOPAGE_SIGBUS;
-	page = gfn_to_page(slot, pgoff);
+	page = gfn_to_page(kvm, pgoff);
 	if (!page)
 		return NOPAGE_SIGBUS;
 	get_page(page);
diff --git a/drivers/kvm/mmu.c b/drivers/kvm/mmu.c
index 707f63c..35135ec 100644
--- a/drivers/kvm/mmu.c
+++ b/drivers/kvm/mmu.c
@@ -390,13 +390,11 @@ static void rmap_write_protect(struct kvm_vcpu *vcpu, u64 gfn)
 {
 	struct kvm *kvm = vcpu->kvm;
 	struct page *page;
-	struct kvm_memory_slot *slot;
 	struct kvm_rmap_desc *desc;
 	u64 *spte;
 
-	slot = gfn_to_memslot(kvm, gfn);
-	BUG_ON(!slot);
-	page = gfn_to_page(slot, gfn);
+	page = gfn_to_page(kvm, gfn);
+	BUG_ON(!page);
 
 	while (page_private(page)) {
 		if (!(page_private(page) & 1))
@@ -711,14 +709,12 @@ hpa_t safe_gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
 
 hpa_t gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
 {
-	struct kvm_memory_slot *slot;
 	struct page *page;
 
 	ASSERT((gpa & HPA_ERR_MASK) == 0);
-	slot = gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT);
-	if (!slot)
+	page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
+	if (!page)
 		return gpa | HPA_ERR_MASK;
-	page = gfn_to_page(slot, gpa >> PAGE_SHIFT);
 	return ((hpa_t)page_to_pfn(page) << PAGE_SHIFT)
 		| (gpa & (PAGE_SIZE-1));
 }
diff --git a/drivers/kvm/vmx.c b/drivers/kvm/vmx.c
index b64b7b7..61a6116 100644
--- a/drivers/kvm/vmx.c
+++ b/drivers/kvm/vmx.c
@@ -926,9 +926,9 @@ static int init_rmode_tss(struct kvm* kvm)
 	gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
 	char *page;
 
-	p1 = _gfn_to_page(kvm, fn++);
-	p2 = _gfn_to_page(kvm, fn++);
-	p3 = _gfn_to_page(kvm, fn);
+	p1 = gfn_to_page(kvm, fn++);
+	p2 = gfn_to_page(kvm, fn++);
+	p3 = gfn_to_page(kvm, fn);
 
 	if (!p1 || !p2 || !p3) {
 		kvm_printf(kvm,"%s: gfn_to_page failed\n", __FUNCTION__);
-- 
1.5.0.5


  reply	other threads:[~2007-04-01 14:36 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-04-01 14:34 [PATCH 00/41] kvm updates for 2.6.22 Avi Kivity
2007-04-01 14:34 ` [PATCH 01/41] KVM: Fix guest register corruption on paravirt hypercall Avi Kivity
2007-04-01 14:34   ` [PATCH 02/41] KVM: Use the generic skip_emulated_instruction() in hypercall code Avi Kivity
2007-04-01 14:35     ` [PATCH 03/41] KVM: Use own minor number Avi Kivity
2007-04-01 14:35       ` [PATCH 04/41] KVM: Export <linux/kvm.h> Avi Kivity
2007-04-01 14:35         ` [PATCH 05/41] KVM: Fix bogus sign extension in mmu mapping audit Avi Kivity
2007-04-01 14:35           ` [PATCH 06/41] KVM: Use a shared page for kernel/user communication when runing a vcpu Avi Kivity
2007-04-01 14:35             ` [PATCH 07/41] KVM: Do not communicate to userspace through cpu registers during PIO Avi Kivity
2007-04-01 14:35               ` [PATCH 08/41] KVM: Handle cpuid in the kernel instead of punting to userspace Avi Kivity
2007-04-01 14:35                 ` [PATCH 09/41] KVM: Remove the 'emulated' field from the userspace interface Avi Kivity
2007-04-01 14:35                   ` [PATCH 10/41] KVM: Remove minor wart from KVM_CREATE_VCPU ioctl Avi Kivity
2007-04-01 14:35                     ` [PATCH 11/41] KVM: Renumber ioctls Avi Kivity
2007-04-01 14:35                       ` [PATCH 12/41] KVM: Add method to check for backwards-compatible API extensions Avi Kivity
2007-04-01 14:35                         ` [PATCH 13/41] KVM: Allow userspace to process hypercalls which have no kernel handler Avi Kivity
2007-04-01 14:35                           ` [PATCH 14/41] KVM: Fold kvm_run::exit_type into kvm_run::exit_reason Avi Kivity
2007-04-01 14:35                             ` [PATCH 15/41] KVM: Add a special exit reason when exiting due to an interrupt Avi Kivity
2007-04-01 14:35                               ` [PATCH 16/41] KVM: Initialize the apic_base msr on svm too Avi Kivity
2007-04-01 14:35                                 ` [PATCH 17/41] KVM: Add guest mode signal mask Avi Kivity
2007-04-01 14:35                                   ` [PATCH 18/41] KVM: Allow kernel to select size of mmap() buffer Avi Kivity
2007-04-01 14:35                                     ` [PATCH 19/41] KVM: Future-proof argument-less ioctls Avi Kivity
2007-04-01 14:35                                       ` [PATCH 20/41] KVM: Avoid guest virtual addresses in string pio userspace interface Avi Kivity
2007-04-01 14:35                                         ` [PATCH 21/41] KVM: MMU: Remove unnecessary check for pdptr access Avi Kivity
2007-04-01 14:35                                           ` [PATCH 22/41] KVM: MMU: Remove global pte tracking Avi Kivity
2007-04-01 14:35                                             ` [PATCH 23/41] KVM: Workaround vmx inability to virtualize the reset state Avi Kivity
2007-04-01 14:35                                               ` [PATCH 24/41] KVM: Remove set_cr0_no_modeswitch() arch op Avi Kivity
2007-04-01 14:35                                                 ` [PATCH 25/41] KVM: Modify guest segments after potentially switching modes Avi Kivity
2007-04-01 14:35                                                   ` [PATCH 26/41] KVM: Hack real-mode segments on vmx from KVM_SET_SREGS Avi Kivity
2007-04-01 14:35                                                     ` [PATCH 27/41] KVM: Don't allow the guest to turn off the cpu cache Avi Kivity
2007-04-01 14:35                                                       ` [PATCH 28/41] KVM: Remove unused and write-only variables Avi Kivity
2007-04-01 14:35                                                         ` [PATCH 29/41] KVM: Handle writes to MCG_STATUS msr Avi Kivity
2007-04-01 14:35                                                           ` [PATCH 30/41] KVM: SVM: forbid guest to execute monitor/mwait Avi Kivity
2007-04-01 14:35                                                             ` [PATCH 31/41] KVM: MMU: Fix hugepage pdes mapping same physical address with different access Avi Kivity
2007-04-01 14:35                                                               ` [PATCH 32/41] KVM: SVM: Ensure timestamp counter monotonicity Avi Kivity
2007-04-01 14:35                                                                 ` [PATCH 33/41] KVM: Remove unused function Avi Kivity
2007-04-01 14:35                                                                   ` [PATCH 34/41] KVM: Use list_move() Avi Kivity
2007-04-01 14:35                                                                     ` [PATCH 35/41] KVM: Remove debug message Avi Kivity
2007-04-01 14:35                                                                       ` [PATCH 36/41] KVM: x86 emulator: fix bit string operations operand size Avi Kivity
2007-04-01 14:35                                                                         ` [PATCH 37/41] KVM: Add mmu cache clear function Avi Kivity
2007-04-01 14:35                                                                           ` Avi Kivity [this message]
2007-04-01 14:35                                                                             ` [PATCH 39/41] KVM: Add physical memory aliasing feature Avi Kivity
2007-04-01 14:35                                                                               ` [PATCH 40/41] KVM: Add fpu get/set operations Avi Kivity
2007-04-01 14:35                                                                                 ` [PATCH 41/41] KVM: SVM: enable LBRV virtualization if available 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=117543813933-git-send-email-avi@qumranet.com \
    --to=avi@qumranet.com \
    --cc=kvm-devel@lists.sourceforge.net \
    --cc=linux-kernel@vger.kernel.org \
    --subject='Re: [PATCH 38/41] KVM: Simply gfn_to_page()' \
    /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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).