From: Avi Kivity <avi@qumranet.com>
To: linux-kernel@vger.kernel.org, kvm-devel@lists.sourceforge.net
Cc: Izik Eidus <avi@qumranet.com>
Subject: [PATCH 47/50] KVM: MMU: Make gfn_to_page() always safe
Date: Sun, 23 Dec 2007 16:51:32 +0200 [thread overview]
Message-ID: <1198421495-31481-48-git-send-email-avi@qumranet.com> (raw)
In-Reply-To: <1198421495-31481-1-git-send-email-avi@qumranet.com>
From: Izik Eidus <avi@qumranet.com>
In case the page is not present in the guest memory map, return a dummy
page the guest can scribble on.
This simplifies error checking in its users.
Signed-off-by: Izik Eidus <izike@qumranet.com>
Signed-off-by: Avi Kivity <avi@qumranet.com>
---
drivers/kvm/kvm.h | 3 ++-
drivers/kvm/kvm_main.c | 26 ++++++++++++++------------
drivers/kvm/mmu.c | 16 +++++-----------
drivers/kvm/paging_tmpl.h | 7 ++-----
4 files changed, 23 insertions(+), 29 deletions(-)
diff --git a/drivers/kvm/kvm.h b/drivers/kvm/kvm.h
index 6ae7b63..0c17c76 100644
--- a/drivers/kvm/kvm.h
+++ b/drivers/kvm/kvm.h
@@ -565,8 +565,9 @@ static inline int is_error_hpa(hpa_t hpa) { return hpa >> HPA_MSB; }
hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva);
struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva);
-extern hpa_t bad_page_address;
+extern struct page *bad_page;
+int is_error_page(struct page *page);
gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn);
struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn);
int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
diff --git a/drivers/kvm/kvm_main.c b/drivers/kvm/kvm_main.c
index a1a3be9..ebfb967 100644
--- a/drivers/kvm/kvm_main.c
+++ b/drivers/kvm/kvm_main.c
@@ -993,6 +993,12 @@ static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
return r;
}
+int is_error_page(struct page *page)
+{
+ return page == bad_page;
+}
+EXPORT_SYMBOL_GPL(is_error_page);
+
gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
{
int i;
@@ -1034,7 +1040,7 @@ struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
gfn = unalias_gfn(kvm, gfn);
slot = __gfn_to_memslot(kvm, gfn);
if (!slot)
- return NULL;
+ return bad_page;
return slot->phys_mem[gfn - slot->base_gfn];
}
EXPORT_SYMBOL_GPL(gfn_to_page);
@@ -1054,7 +1060,7 @@ int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
struct page *page;
page = gfn_to_page(kvm, gfn);
- if (!page)
+ if (is_error_page(page))
return -EFAULT;
page_virt = kmap_atomic(page, KM_USER0);
@@ -1092,7 +1098,7 @@ int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
struct page *page;
page = gfn_to_page(kvm, gfn);
- if (!page)
+ if (is_error_page(page))
return -EFAULT;
page_virt = kmap_atomic(page, KM_USER0);
@@ -1130,7 +1136,7 @@ int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
struct page *page;
page = gfn_to_page(kvm, gfn);
- if (!page)
+ if (is_error_page(page))
return -EFAULT;
page_virt = kmap_atomic(page, KM_USER0);
@@ -3068,7 +3074,7 @@ static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
page = gfn_to_page(kvm, pgoff);
- if (!page)
+ if (is_error_page(page))
return NOPAGE_SIGBUS;
get_page(page);
if (type != NULL)
@@ -3383,7 +3389,7 @@ static struct sys_device kvm_sysdev = {
.cls = &kvm_sysdev_class,
};
-hpa_t bad_page_address;
+struct page *bad_page;
static inline
struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
@@ -3512,7 +3518,6 @@ EXPORT_SYMBOL_GPL(kvm_exit_x86);
static __init int kvm_init(void)
{
- static struct page *bad_page;
int r;
r = kvm_mmu_module_init();
@@ -3523,16 +3528,13 @@ static __init int kvm_init(void)
kvm_arch_init();
- bad_page = alloc_page(GFP_KERNEL);
+ bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
if (bad_page == NULL) {
r = -ENOMEM;
goto out;
}
- bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
- memset(__va(bad_page_address), 0, PAGE_SIZE);
-
return 0;
out:
@@ -3545,7 +3547,7 @@ out4:
static __exit void kvm_exit(void)
{
kvm_exit_debug();
- __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
+ __free_page(bad_page);
kvm_mmu_module_exit();
}
diff --git a/drivers/kvm/mmu.c b/drivers/kvm/mmu.c
index bbf5eb4..2ad14fb 100644
--- a/drivers/kvm/mmu.c
+++ b/drivers/kvm/mmu.c
@@ -850,23 +850,17 @@ static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa)
__set_bit(slot, &page_head->slot_bitmap);
}
-hpa_t safe_gpa_to_hpa(struct kvm *kvm, gpa_t gpa)
-{
- hpa_t hpa = gpa_to_hpa(kvm, gpa);
-
- return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK): hpa;
-}
-
hpa_t gpa_to_hpa(struct kvm *kvm, gpa_t gpa)
{
struct page *page;
+ hpa_t hpa;
ASSERT((gpa & HPA_ERR_MASK) == 0);
page = gfn_to_page(kvm, gpa >> PAGE_SHIFT);
- if (!page)
- return gpa | HPA_ERR_MASK;
- return ((hpa_t)page_to_pfn(page) << PAGE_SHIFT)
- | (gpa & (PAGE_SIZE-1));
+ hpa = ((hpa_t)page_to_pfn(page) << PAGE_SHIFT) | (gpa & (PAGE_SIZE-1));
+ if (is_error_page(page))
+ return hpa | HPA_ERR_MASK;
+ return hpa;
}
hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva)
diff --git a/drivers/kvm/paging_tmpl.h b/drivers/kvm/paging_tmpl.h
index bab1b7f..572e5b6 100644
--- a/drivers/kvm/paging_tmpl.h
+++ b/drivers/kvm/paging_tmpl.h
@@ -72,8 +72,6 @@ static int FNAME(walk_addr)(struct guest_walker *walker,
struct kvm_vcpu *vcpu, gva_t addr,
int write_fault, int user_fault, int fetch_fault)
{
- hpa_t hpa;
- struct kvm_memory_slot *slot;
struct page *page;
pt_element_t *table;
pt_element_t pte;
@@ -105,9 +103,8 @@ static int FNAME(walk_addr)(struct guest_walker *walker,
pgprintk("%s: table_gfn[%d] %lx\n", __FUNCTION__,
walker->level - 1, table_gfn);
- slot = gfn_to_memslot(vcpu->kvm, table_gfn);
- hpa = safe_gpa_to_hpa(vcpu->kvm, pte & PT64_BASE_ADDR_MASK);
- page = pfn_to_page(hpa >> PAGE_SHIFT);
+ page = gfn_to_page(vcpu->kvm, (pte & PT64_BASE_ADDR_MASK)
+ >> PAGE_SHIFT);
table = kmap_atomic(page, KM_USER0);
pte = table[index];
--
1.5.3.7
next prev parent reply other threads:[~2007-12-23 15:05 UTC|newest]
Thread overview: 52+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-12-23 14:50 [PATCH 00/50] KVM patch queue review for 2.6.25 merge window (part I) Avi Kivity
2007-12-23 14:50 ` [PATCH 01/50] KVM: x86 emulator: Add vmmcall/vmcall to x86_emulate (v3) Avi Kivity
2007-12-23 14:50 ` [PATCH 02/50] KVM: Refactor hypercall infrastructure (v3) Avi Kivity
2007-12-23 14:50 ` [PATCH 03/50] KVM: x86 emulator: remove unused functions Avi Kivity
2007-12-23 14:50 ` [PATCH 04/50] KVM: x86 emulator: move all x86_emulate_memop() to a structure Avi Kivity
2007-12-23 14:50 ` [PATCH 05/50] KVM: x86 emulator: move all decoding process to function x86_decode_insn() Avi Kivity
2007-12-23 14:50 ` [PATCH 06/50] KVM: emulate_instruction() calls now x86_decode_insn() and x86_emulate_insn() Avi Kivity
2007-12-23 14:50 ` [PATCH 07/50] KVM: Call x86_decode_insn() only when needed Avi Kivity
2007-12-23 14:50 ` [PATCH 08/50] KVM: VMX: Further reduce efer reloads Avi Kivity
2007-12-23 14:50 ` [PATCH 09/50] KVM: Allow not-present guest page faults to bypass kvm Avi Kivity
2007-12-23 14:50 ` [PATCH 10/50] KVM: MMU: Make flooding detection work when guest page faults are bypassed Avi Kivity
2007-12-23 14:50 ` [PATCH 11/50] KVM: MMU: Ignore reserved bits in cr3 in non-pae mode Avi Kivity
2007-12-23 14:50 ` [PATCH 12/50] KVM: x86 emulator: split some decoding into functions for readability Avi Kivity
2007-12-23 14:50 ` [PATCH 13/50] KVM: x86 emulator: remove _eflags and use directly ctxt->eflags Avi Kivity
2007-12-23 14:50 ` [PATCH 14/50] KVM: x86 emulator: Remove no_wb, use dst.type = OP_NONE instead Avi Kivity
2007-12-23 14:51 ` [PATCH 15/50] KVM: x86_emulator: no writeback for bt Avi Kivity
2007-12-23 14:51 ` [PATCH 16/50] KVM: Purify x86_decode_insn() error case management Avi Kivity
2007-12-23 14:51 ` [PATCH 17/50] KVM: x86 emulator: Any legacy prefix after a REX prefix nullifies its effect Avi Kivity
2007-12-23 14:51 ` [PATCH 18/50] KVM: VMX: Don't clear the vmcs if the vcpu is not loaded on any processor Avi Kivity
2007-12-23 14:51 ` [PATCH 19/50] KVM: VMX: Simplify vcpu_clear() Avi Kivity
2007-12-23 14:51 ` [PATCH 20/50] KVM: Remove the usage of page->private field by rmap Avi Kivity
2007-12-23 14:51 ` [PATCH 21/50] KVM: Add general accessors to read and write guest memory Avi Kivity
2007-12-23 14:51 ` [PATCH 22/50] KVM: Allow dynamic allocation of the mmu shadow cache size Avi Kivity
2007-12-23 14:51 ` [PATCH 23/50] KVM: Add kvm_free_lapic() to pair with kvm_create_lapic() Avi Kivity
2007-12-23 14:51 ` [PATCH 24/50] KVM: Hoist kvm_create_lapic() into kvm_vcpu_init() Avi Kivity
2007-12-23 14:51 ` [PATCH 25/50] KVM: Remove gratuitous casts from lapic.c Avi Kivity
2007-12-23 14:51 ` [PATCH 26/50] KVM: CodingStyle cleanup Avi Kivity
2007-12-23 14:51 ` [PATCH 27/50] KVM: Support assigning userspace memory to the guest Avi Kivity
2007-12-23 18:16 ` [kvm-devel] " Avi Kivity
2007-12-23 14:51 ` [PATCH 28/50] KVM: Move x86 msr handling to new files x86.[ch] Avi Kivity
2007-12-23 14:51 ` [PATCH 29/50] KVM: MMU: Clean up MMU functions to take struct kvm when appropriate Avi Kivity
2007-12-23 14:51 ` [PATCH 30/50] KVM: MMU: More struct kvm_vcpu -> struct kvm cleanups Avi Kivity
2007-12-23 14:51 ` [PATCH 31/50] KVM: Move guest pte dirty bit management to the guest pagetable walker Avi Kivity
2007-12-23 14:51 ` [PATCH 32/50] KVM: MMU: Fix nx access bit for huge pages Avi Kivity
2007-12-23 14:51 ` [PATCH 33/50] KVM: MMU: Disable write access on clean large pages Avi Kivity
2007-12-23 14:51 ` [PATCH 34/50] KVM: MMU: Instantiate real-mode shadows as user writable shadows Avi Kivity
2007-12-23 14:51 ` [PATCH 35/50] KVM: MMU: Move dirty bit updates to a separate function Avi Kivity
2007-12-23 14:51 ` [PATCH 36/50] KVM: MMU: When updating the dirty bit, inform the mmu about it Avi Kivity
2007-12-23 14:51 ` [PATCH 37/50] KVM: Portability: split kvm_vcpu_ioctl Avi Kivity
2007-12-23 14:51 ` [PATCH 38/50] KVM: apic round robin cleanup Avi Kivity
2007-12-23 14:51 ` [PATCH 39/50] KVM: Add some \n in ioapic_debug() Avi Kivity
2007-12-23 14:51 ` [PATCH 40/50] KVM: Move apic timer interrupt backlog processing to common code Avi Kivity
2007-12-23 14:51 ` [PATCH 41/50] KVM: Rename KVM_TLB_FLUSH to KVM_REQ_TLB_FLUSH Avi Kivity
2007-12-23 14:51 ` [PATCH 42/50] KVM: x86 emulator: Implement emulation of instruction: inc & dec Avi Kivity
2007-12-23 14:51 ` [PATCH 43/50] KVM: MMU: Simplify page table walker Avi Kivity
2007-12-23 14:51 ` [PATCH 44/50] KVM: x86 emulator: cmc, clc, cli, sti Avi Kivity
2007-12-23 14:51 ` [PATCH 45/50] KVM: MMU: Add rmap_next(), a helper for walking kvm rmaps Avi Kivity
2007-12-23 14:51 ` [PATCH 46/50] KVM: MMU: Keep a reverse mapping of non-writable translations Avi Kivity
2007-12-23 14:51 ` Avi Kivity [this message]
2007-12-23 14:51 ` [PATCH 48/50] KVM: MMU: Partial swapping of guest memory Avi Kivity
2007-12-23 14:51 ` [PATCH 49/50] KVM: Use virtual cpu accounting if available for guest times Avi Kivity
2007-12-23 14:51 ` [PATCH 50/50] KVM: Allocate userspace memory for older 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=1198421495-31481-48-git-send-email-avi@qumranet.com \
--to=avi@qumranet.com \
--cc=kvm-devel@lists.sourceforge.net \
--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
all inboxes | Powered by JetHome®