]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
KVM: Avoid killing userspace through guest SRAO MCE on unmapped pages
authorHuang Ying <ying.huang@intel.com>
Mon, 31 May 2010 06:28:19 +0000 (14:28 +0800)
committerAvi Kivity <avi@redhat.com>
Mon, 31 May 2010 11:02:20 +0000 (14:02 +0300)
In common cases, guest SRAO MCE will cause corresponding poisoned page
be un-mapped and SIGBUS be sent to QEMU-KVM, then QEMU-KVM will relay
the MCE to guest OS.

But it is reported that if the poisoned page is accessed in guest
after unmapping and before MCE is relayed to guest OS, userspace will
be killed.

The reason is as follows. Because poisoned page has been un-mapped,
guest access will cause guest exit and kvm_mmu_page_fault will be
called. kvm_mmu_page_fault can not get the poisoned page for fault
address, so kernel and user space MMIO processing is tried in turn. In
user MMIO processing, poisoned page is accessed again, then userspace
is killed by force_sig_info.

To fix the bug, kvm_mmu_page_fault send HWPOISON signal to QEMU-KVM
and do not try kernel and user space MMIO processing for poisoned
page.

Reported-by: Max Asbock <masbock@linux.vnet.ibm.com>
Signed-off-by: Huang Ying <ying.huang@intel.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
arch/x86/kvm/mmu.c
arch/x86/kvm/paging_tmpl.h
include/linux/kvm_host.h
include/linux/mm.h
mm/memory-failure.c
virt/kvm/kvm_main.c

index 43ec285e877727ef8a8d786048aa1875919ea23a..c16c4cacfd470f83990716f76c8de98073603ea4 100644 (file)
@@ -33,6 +33,7 @@
 #include <linux/compiler.h>
 #include <linux/srcu.h>
 #include <linux/slab.h>
+#include <linux/uaccess.h>
 
 #include <asm/page.h>
 #include <asm/cmpxchg.h>
@@ -2040,6 +2041,27 @@ static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
        return pt_write;
 }
 
+static void kvm_send_hwpoison_signal(struct kvm *kvm, gfn_t gfn)
+{
+       char buf[1];
+       void __user *hva;
+       int r;
+
+       /* Touch the page, so send SIGBUS */
+       hva = (void __user *)gfn_to_hva(kvm, gfn);
+       (void)copy_from_user(buf, hva, 1);
+}
+
+static int kvm_handle_bad_page(struct kvm *kvm, gfn_t gfn, pfn_t pfn)
+{
+       kvm_release_pfn_clean(pfn);
+       if (is_hwpoison_pfn(pfn)) {
+               kvm_send_hwpoison_signal(kvm, gfn);
+               return 0;
+       }
+       return 1;
+}
+
 static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
 {
        int r;
@@ -2063,10 +2085,8 @@ static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
        pfn = gfn_to_pfn(vcpu->kvm, gfn);
 
        /* mmio */
-       if (is_error_pfn(pfn)) {
-               kvm_release_pfn_clean(pfn);
-               return 1;
-       }
+       if (is_error_pfn(pfn))
+               return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
 
        spin_lock(&vcpu->kvm->mmu_lock);
        if (mmu_notifier_retry(vcpu, mmu_seq))
@@ -2280,10 +2300,8 @@ static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
        mmu_seq = vcpu->kvm->mmu_notifier_seq;
        smp_rmb();
        pfn = gfn_to_pfn(vcpu->kvm, gfn);
-       if (is_error_pfn(pfn)) {
-               kvm_release_pfn_clean(pfn);
-               return 1;
-       }
+       if (is_error_pfn(pfn))
+               return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
        spin_lock(&vcpu->kvm->mmu_lock);
        if (mmu_notifier_retry(vcpu, mmu_seq))
                goto out_unlock;
index f28f2f6d403a0510a7533d33bb44722ce111a172..8f1ef877b065ae982c1a0cf3e91e32ba1a3c6ce9 100644 (file)
@@ -434,11 +434,8 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
        pfn = gfn_to_pfn(vcpu->kvm, walker.gfn);
 
        /* mmio */
-       if (is_error_pfn(pfn)) {
-               pgprintk("gfn %lx is mmio\n", walker.gfn);
-               kvm_release_pfn_clean(pfn);
-               return 1;
-       }
+       if (is_error_pfn(pfn))
+               return kvm_handle_bad_page(vcpu->kvm, walker.gfn, pfn);
 
        spin_lock(&vcpu->kvm->mmu_lock);
        if (mmu_notifier_retry(vcpu, mmu_seq))
index 4e8fdbf0f2f17e98bf077df1d9e11dd7a90a0231..2c62319727efbc8f75671af794724aa55b5d60dc 100644 (file)
@@ -267,6 +267,7 @@ extern pfn_t bad_pfn;
 
 int is_error_page(struct page *page);
 int is_error_pfn(pfn_t pfn);
+int is_hwpoison_pfn(pfn_t pfn);
 int kvm_is_error_hva(unsigned long addr);
 int kvm_set_memory_region(struct kvm *kvm,
                          struct kvm_userspace_memory_region *mem,
index b969efb03787ee69546995ad378ac54b6bc37e3b..4238a9cae54bc27f4d7cade01bf87e26752bcc9f 100644 (file)
@@ -1465,6 +1465,14 @@ extern int sysctl_memory_failure_recovery;
 extern void shake_page(struct page *p, int access);
 extern atomic_long_t mce_bad_pages;
 extern int soft_offline_page(struct page *page, int flags);
+#ifdef CONFIG_MEMORY_FAILURE
+int is_hwpoison_address(unsigned long addr);
+#else
+static inline int is_hwpoison_address(unsigned long addr)
+{
+       return 0;
+}
+#endif
 
 extern void dump_page(struct page *page);
 
index 620b0b461593afb124fc3174508b55993fe1f782..378b0f61fd3cf609a4b00a353ea771cbd5ee2082 100644 (file)
@@ -45,6 +45,7 @@
 #include <linux/page-isolation.h>
 #include <linux/suspend.h>
 #include <linux/slab.h>
+#include <linux/swapops.h>
 #include "internal.h"
 
 int sysctl_memory_failure_early_kill __read_mostly = 0;
@@ -1296,3 +1297,32 @@ done:
        /* keep elevated page count for bad page */
        return ret;
 }
+
+int is_hwpoison_address(unsigned long addr)
+{
+       pgd_t *pgdp;
+       pud_t pud, *pudp;
+       pmd_t pmd, *pmdp;
+       pte_t pte, *ptep;
+       swp_entry_t entry;
+
+       pgdp = pgd_offset(current->mm, addr);
+       if (!pgd_present(*pgdp))
+               return 0;
+       pudp = pud_offset(pgdp, addr);
+       pud = *pudp;
+       if (!pud_present(pud) || pud_large(pud))
+               return 0;
+       pmdp = pmd_offset(pudp, addr);
+       pmd = *pmdp;
+       if (!pmd_present(pmd) || pmd_large(pmd))
+               return 0;
+       ptep = pte_offset_map(pmdp, addr);
+       pte = *ptep;
+       pte_unmap(ptep);
+       if (!is_swap_pte(pte))
+               return 0;
+       entry = pte_to_swp_entry(pte);
+       return is_hwpoison_entry(entry);
+}
+EXPORT_SYMBOL_GPL(is_hwpoison_address);
index e46fa81a405f3d3996a0d6851bb36ac72a6e2a87..4a71faa96dfd6f0e42fb5a5f8c70a7ec499a24cc 100644 (file)
@@ -93,6 +93,9 @@ static bool kvm_rebooting;
 
 static bool largepages_enabled = true;
 
+struct page *hwpoison_page;
+pfn_t hwpoison_pfn;
+
 inline int kvm_is_mmio_pfn(pfn_t pfn)
 {
        if (pfn_valid(pfn)) {
@@ -811,16 +814,22 @@ EXPORT_SYMBOL_GPL(kvm_disable_largepages);
 
 int is_error_page(struct page *page)
 {
-       return page == bad_page;
+       return page == bad_page || page == hwpoison_page;
 }
 EXPORT_SYMBOL_GPL(is_error_page);
 
 int is_error_pfn(pfn_t pfn)
 {
-       return pfn == bad_pfn;
+       return pfn == bad_pfn || pfn == hwpoison_pfn;
 }
 EXPORT_SYMBOL_GPL(is_error_pfn);
 
+int is_hwpoison_pfn(pfn_t pfn)
+{
+       return pfn == hwpoison_pfn;
+}
+EXPORT_SYMBOL_GPL(is_hwpoison_pfn);
+
 static inline unsigned long bad_hva(void)
 {
        return PAGE_OFFSET;
@@ -946,6 +955,11 @@ static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr)
        if (unlikely(npages != 1)) {
                struct vm_area_struct *vma;
 
+               if (is_hwpoison_address(addr)) {
+                       get_page(hwpoison_page);
+                       return page_to_pfn(hwpoison_page);
+               }
+
                down_read(&current->mm->mmap_sem);
                vma = find_vma(current->mm, addr);
 
@@ -2211,6 +2225,15 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
 
        bad_pfn = page_to_pfn(bad_page);
 
+       hwpoison_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
+
+       if (hwpoison_page == NULL) {
+               r = -ENOMEM;
+               goto out_free_0;
+       }
+
+       hwpoison_pfn = page_to_pfn(hwpoison_page);
+
        if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
                r = -ENOMEM;
                goto out_free_0;
@@ -2283,6 +2306,8 @@ out_free_1:
 out_free_0a:
        free_cpumask_var(cpus_hardware_enabled);
 out_free_0:
+       if (hwpoison_page)
+               __free_page(hwpoison_page);
        __free_page(bad_page);
 out:
        kvm_arch_exit();
@@ -2304,6 +2329,7 @@ void kvm_exit(void)
        kvm_arch_hardware_unsetup();
        kvm_arch_exit();
        free_cpumask_var(cpus_hardware_enabled);
+       __free_page(hwpoison_page);
        __free_page(bad_page);
 }
 EXPORT_SYMBOL_GPL(kvm_exit);