KVM: Ensure all vcpus are consistent with in-kernel irqchip settings
[linux-flexiantxendom0.git] / virt / kvm / kvm_main.c
index 92f5c98..5007b7d 100644 (file)
@@ -5,6 +5,7 @@
  * machines without emulation or binary translation.
  *
  * Copyright (C) 2006 Qumranet, Inc.
+ * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  *
  * Authors:
  *   Avi Kivity   <avi@qumranet.com>
@@ -22,7 +23,6 @@
 #include <linux/module.h>
 #include <linux/errno.h>
 #include <linux/percpu.h>
-#include <linux/gfp.h>
 #include <linux/mm.h>
 #include <linux/miscdevice.h>
 #include <linux/vmalloc.h>
@@ -30,7 +30,7 @@
 #include <linux/debugfs.h>
 #include <linux/highmem.h>
 #include <linux/file.h>
-#include <linux/sysdev.h>
+#include <linux/syscore_ops.h>
 #include <linux/cpu.h>
 #include <linux/sched.h>
 #include <linux/cpumask.h>
 #include <linux/bitops.h>
 #include <linux/spinlock.h>
 #include <linux/compat.h>
+#include <linux/srcu.h>
+#include <linux/hugetlb.h>
+#include <linux/slab.h>
+#include <linux/sort.h>
+#include <linux/bsearch.h>
 
 #include <asm/processor.h>
 #include <asm/io.h>
 #include <asm/uaccess.h>
 #include <asm/pgtable.h>
-#include <asm-generic/bitops/le.h>
 
-#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
 #include "coalesced_mmio.h"
-#endif
+#include "async_pf.h"
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/kvm.h>
@@ -67,7 +70,7 @@ MODULE_LICENSE("GPL");
  *             kvm->lock --> kvm->slots_lock --> kvm->irq_lock
  */
 
-DEFINE_SPINLOCK(kvm_lock);
+DEFINE_RAW_SPINLOCK(kvm_lock);
 LIST_HEAD(vm_list);
 
 static cpumask_var_t cpus_hardware_enabled;
@@ -83,18 +86,49 @@ struct dentry *kvm_debugfs_dir;
 
 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
                           unsigned long arg);
+#ifdef CONFIG_COMPAT
+static long kvm_vcpu_compat_ioctl(struct file *file, unsigned int ioctl,
+                                 unsigned long arg);
+#endif
 static int hardware_enable_all(void);
 static void hardware_disable_all(void);
 
-static bool kvm_rebooting;
+static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
+
+bool kvm_rebooting;
+EXPORT_SYMBOL_GPL(kvm_rebooting);
 
 static bool largepages_enabled = true;
 
+static struct page *hwpoison_page;
+static pfn_t hwpoison_pfn;
+
+struct page *fault_page;
+pfn_t fault_pfn;
+
 inline int kvm_is_mmio_pfn(pfn_t pfn)
 {
        if (pfn_valid(pfn)) {
-               struct page *page = compound_head(pfn_to_page(pfn));
-               return PageReserved(page);
+               int reserved;
+               struct page *tail = pfn_to_page(pfn);
+               struct page *head = compound_trans_head(tail);
+               reserved = PageReserved(head);
+               if (head != tail) {
+                       /*
+                        * "head" is not a dangling pointer
+                        * (compound_trans_head takes care of that)
+                        * but the hugepage may have been splitted
+                        * from under us (and we may not hold a
+                        * reference count on the head page so it can
+                        * be reused before we run PageReferenced), so
+                        * we've to check PageTail before returning
+                        * what we just read.
+                        */
+                       smp_rmb();
+                       if (PageTail(tail))
+                               return reserved;
+               }
+               return PageReserved(tail);
        }
 
        return true;
@@ -108,6 +142,14 @@ void vcpu_load(struct kvm_vcpu *vcpu)
        int cpu;
 
        mutex_lock(&vcpu->mutex);
+       if (unlikely(vcpu->pid != current->pids[PIDTYPE_PID].pid)) {
+               /* The thread running this VCPU changed. */
+               struct pid *oldpid = vcpu->pid;
+               struct pid *newpid = get_task_pid(current, PIDTYPE_PID);
+               rcu_assign_pointer(vcpu->pid, newpid);
+               synchronize_rcu();
+               put_pid(oldpid);
+       }
        cpu = get_cpu();
        preempt_notifier_register(&vcpu->preempt_notifier);
        kvm_arch_vcpu_load(vcpu, cpu);
@@ -136,13 +178,16 @@ static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
 
        zalloc_cpumask_var(&cpus, GFP_ATOMIC);
 
-       spin_lock(&kvm->requests_lock);
-       me = smp_processor_id();
+       me = get_cpu();
        kvm_for_each_vcpu(i, vcpu, kvm) {
-               if (test_and_set_bit(req, &vcpu->requests))
-                       continue;
+               kvm_make_request(req, vcpu);
                cpu = vcpu->cpu;
-               if (cpus != NULL && cpu != -1 && cpu != me)
+
+               /* Set ->requests bit before we read ->mode */
+               smp_mb();
+
+               if (cpus != NULL && cpu != -1 && cpu != me &&
+                     kvm_vcpu_exiting_guest_mode(vcpu) != OUTSIDE_GUEST_MODE)
                        cpumask_set_cpu(cpu, cpus);
        }
        if (unlikely(cpus == NULL))
@@ -151,15 +196,19 @@ static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
                smp_call_function_many(cpus, ack_flush, NULL, 1);
        else
                called = false;
-       spin_unlock(&kvm->requests_lock);
+       put_cpu();
        free_cpumask_var(cpus);
        return called;
 }
 
 void kvm_flush_remote_tlbs(struct kvm *kvm)
 {
+       int dirty_count = kvm->tlbs_dirty;
+
+       smp_mb();
        if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
                ++kvm->stat.remote_tlb_flush;
+       cmpxchg(&kvm->tlbs_dirty, dirty_count, 0);
 }
 
 void kvm_reload_remote_mmus(struct kvm *kvm)
@@ -176,7 +225,9 @@ int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
        vcpu->cpu = -1;
        vcpu->kvm = kvm;
        vcpu->vcpu_id = id;
+       vcpu->pid = NULL;
        init_waitqueue_head(&vcpu->wq);
+       kvm_async_pf_vcpu_init(vcpu);
 
        page = alloc_page(GFP_KERNEL | __GFP_ZERO);
        if (!page) {
@@ -199,6 +250,7 @@ EXPORT_SYMBOL_GPL(kvm_vcpu_init);
 
 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
 {
+       put_pid(vcpu->pid);
        kvm_arch_vcpu_uninit(vcpu);
        free_page((unsigned long)vcpu->run);
 }
@@ -215,7 +267,7 @@ static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
                                             unsigned long address)
 {
        struct kvm *kvm = mmu_notifier_to_kvm(mn);
-       int need_tlb_flush;
+       int need_tlb_flush, idx;
 
        /*
         * When ->invalidate_page runs, the linux pte has been zapped
@@ -235,10 +287,12 @@ static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
         * pte after kvm_unmap_hva returned, without noticing the page
         * is going to be freed.
         */
+       idx = srcu_read_lock(&kvm->srcu);
        spin_lock(&kvm->mmu_lock);
        kvm->mmu_notifier_seq++;
-       need_tlb_flush = kvm_unmap_hva(kvm, address);
+       need_tlb_flush = kvm_unmap_hva(kvm, address) | kvm->tlbs_dirty;
        spin_unlock(&kvm->mmu_lock);
+       srcu_read_unlock(&kvm->srcu, idx);
 
        /* we've to flush the tlb before the pages can be freed */
        if (need_tlb_flush)
@@ -252,11 +306,14 @@ static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
                                        pte_t pte)
 {
        struct kvm *kvm = mmu_notifier_to_kvm(mn);
+       int idx;
 
+       idx = srcu_read_lock(&kvm->srcu);
        spin_lock(&kvm->mmu_lock);
        kvm->mmu_notifier_seq++;
        kvm_set_spte_hva(kvm, address, pte);
        spin_unlock(&kvm->mmu_lock);
+       srcu_read_unlock(&kvm->srcu, idx);
 }
 
 static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
@@ -265,8 +322,9 @@ static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
                                                    unsigned long end)
 {
        struct kvm *kvm = mmu_notifier_to_kvm(mn);
-       int need_tlb_flush = 0;
+       int need_tlb_flush = 0, idx;
 
+       idx = srcu_read_lock(&kvm->srcu);
        spin_lock(&kvm->mmu_lock);
        /*
         * The count increase must become visible at unlock time as no
@@ -276,7 +334,9 @@ static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
        kvm->mmu_notifier_count++;
        for (; start < end; start += PAGE_SIZE)
                need_tlb_flush |= kvm_unmap_hva(kvm, start);
+       need_tlb_flush |= kvm->tlbs_dirty;
        spin_unlock(&kvm->mmu_lock);
+       srcu_read_unlock(&kvm->srcu, idx);
 
        /* we've to flush the tlb before the pages can be freed */
        if (need_tlb_flush)
@@ -314,11 +374,13 @@ static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
                                              unsigned long address)
 {
        struct kvm *kvm = mmu_notifier_to_kvm(mn);
-       int young;
+       int young, idx;
 
+       idx = srcu_read_lock(&kvm->srcu);
        spin_lock(&kvm->mmu_lock);
        young = kvm_age_hva(kvm, address);
        spin_unlock(&kvm->mmu_lock);
+       srcu_read_unlock(&kvm->srcu, idx);
 
        if (young)
                kvm_flush_remote_tlbs(kvm);
@@ -326,11 +388,31 @@ static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
        return young;
 }
 
+static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,
+                                      struct mm_struct *mm,
+                                      unsigned long address)
+{
+       struct kvm *kvm = mmu_notifier_to_kvm(mn);
+       int young, idx;
+
+       idx = srcu_read_lock(&kvm->srcu);
+       spin_lock(&kvm->mmu_lock);
+       young = kvm_test_age_hva(kvm, address);
+       spin_unlock(&kvm->mmu_lock);
+       srcu_read_unlock(&kvm->srcu, idx);
+
+       return young;
+}
+
 static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
                                     struct mm_struct *mm)
 {
        struct kvm *kvm = mmu_notifier_to_kvm(mn);
+       int idx;
+
+       idx = srcu_read_lock(&kvm->srcu);
        kvm_arch_flush_shadow(kvm);
+       srcu_read_unlock(&kvm->srcu, idx);
 }
 
 static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
@@ -338,21 +420,37 @@ static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
        .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
        .invalidate_range_end   = kvm_mmu_notifier_invalidate_range_end,
        .clear_flush_young      = kvm_mmu_notifier_clear_flush_young,
+       .test_young             = kvm_mmu_notifier_test_young,
        .change_pte             = kvm_mmu_notifier_change_pte,
        .release                = kvm_mmu_notifier_release,
 };
+
+static int kvm_init_mmu_notifier(struct kvm *kvm)
+{
+       kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
+       return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
+}
+
+#else  /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
+
+static int kvm_init_mmu_notifier(struct kvm *kvm)
+{
+       return 0;
+}
+
 #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
 
 static struct kvm *kvm_create_vm(void)
 {
-       int r = 0;
-       struct kvm *kvm = kvm_arch_create_vm();
-#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
-       struct page *page;
-#endif
+       int r, i;
+       struct kvm *kvm = kvm_arch_alloc_vm();
 
-       if (IS_ERR(kvm))
-               goto out;
+       if (!kvm)
+               return ERR_PTR(-ENOMEM);
+
+       r = kvm_arch_init_vm(kvm);
+       if (r)
+               goto out_err_nodisable;
 
        r = hardware_enable_all();
        if (r)
@@ -363,55 +461,64 @@ static struct kvm *kvm_create_vm(void)
        INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
 #endif
 
-#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
-       page = alloc_page(GFP_KERNEL | __GFP_ZERO);
-       if (!page) {
-               r = -ENOMEM;
-               goto out_err;
-       }
-       kvm->coalesced_mmio_ring =
-                       (struct kvm_coalesced_mmio_ring *)page_address(page);
-#endif
-
-#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
-       kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
-       r = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
-#endif
-
-       if (r) {
-#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
-               put_page(page);
-#endif
-               goto out_err;
+       r = -ENOMEM;
+       kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
+       if (!kvm->memslots)
+               goto out_err_nosrcu;
+       if (init_srcu_struct(&kvm->srcu))
+               goto out_err_nosrcu;
+       for (i = 0; i < KVM_NR_BUSES; i++) {
+               kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
+                                       GFP_KERNEL);
+               if (!kvm->buses[i])
+                       goto out_err;
        }
 
+       spin_lock_init(&kvm->mmu_lock);
        kvm->mm = current->mm;
        atomic_inc(&kvm->mm->mm_count);
-       spin_lock_init(&kvm->mmu_lock);
-       spin_lock_init(&kvm->requests_lock);
-       kvm_io_bus_init(&kvm->pio_bus);
        kvm_eventfd_init(kvm);
        mutex_init(&kvm->lock);
        mutex_init(&kvm->irq_lock);
-       kvm_io_bus_init(&kvm->mmio_bus);
-       init_rwsem(&kvm->slots_lock);
+       mutex_init(&kvm->slots_lock);
        atomic_set(&kvm->users_count, 1);
-       spin_lock(&kvm_lock);
+
+       r = kvm_init_mmu_notifier(kvm);
+       if (r)
+               goto out_err;
+
+       raw_spin_lock(&kvm_lock);
        list_add(&kvm->vm_list, &vm_list);
-       spin_unlock(&kvm_lock);
-#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
-       kvm_coalesced_mmio_init(kvm);
-#endif
-out:
+       raw_spin_unlock(&kvm_lock);
+
        return kvm;
 
 out_err:
+       cleanup_srcu_struct(&kvm->srcu);
+out_err_nosrcu:
        hardware_disable_all();
 out_err_nodisable:
-       kfree(kvm);
+       for (i = 0; i < KVM_NR_BUSES; i++)
+               kfree(kvm->buses[i]);
+       kfree(kvm->memslots);
+       kvm_arch_free_vm(kvm);
        return ERR_PTR(r);
 }
 
+static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
+{
+       if (!memslot->dirty_bitmap)
+               return;
+
+       if (2 * kvm_dirty_bitmap_bytes(memslot) > PAGE_SIZE)
+               vfree(memslot->dirty_bitmap_head);
+       else
+               kfree(memslot->dirty_bitmap_head);
+
+       memslot->dirty_bitmap = NULL;
+       memslot->dirty_bitmap_head = NULL;
+}
+
 /*
  * Free any memory in @free but not in @dont.
  */
@@ -424,7 +531,7 @@ static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
                vfree(free->rmap);
 
        if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
-               vfree(free->dirty_bitmap);
+               kvm_destroy_dirty_bitmap(free);
 
 
        for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
@@ -435,39 +542,42 @@ static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
        }
 
        free->npages = 0;
-       free->dirty_bitmap = NULL;
        free->rmap = NULL;
 }
 
 void kvm_free_physmem(struct kvm *kvm)
 {
        int i;
+       struct kvm_memslots *slots = kvm->memslots;
 
-       for (i = 0; i < kvm->nmemslots; ++i)
-               kvm_free_physmem_slot(&kvm->memslots[i], NULL);
+       for (i = 0; i < slots->nmemslots; ++i)
+               kvm_free_physmem_slot(&slots->memslots[i], NULL);
+
+       kfree(kvm->memslots);
 }
 
 static void kvm_destroy_vm(struct kvm *kvm)
 {
+       int i;
        struct mm_struct *mm = kvm->mm;
 
        kvm_arch_sync_events(kvm);
-       spin_lock(&kvm_lock);
+       raw_spin_lock(&kvm_lock);
        list_del(&kvm->vm_list);
-       spin_unlock(&kvm_lock);
+       raw_spin_unlock(&kvm_lock);
        kvm_free_irq_routing(kvm);
-       kvm_io_bus_destroy(&kvm->pio_bus);
-       kvm_io_bus_destroy(&kvm->mmio_bus);
-#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
-       if (kvm->coalesced_mmio_ring != NULL)
-               free_page((unsigned long)kvm->coalesced_mmio_ring);
-#endif
+       for (i = 0; i < KVM_NR_BUSES; i++)
+               kvm_io_bus_destroy(kvm->buses[i]);
+       kvm_coalesced_mmio_free(kvm);
 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
        mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
 #else
        kvm_arch_flush_shadow(kvm);
 #endif
        kvm_arch_destroy_vm(kvm);
+       kvm_free_physmem(kvm);
+       cleanup_srcu_struct(&kvm->srcu);
+       kvm_arch_free_vm(kvm);
        hardware_disable_all();
        mmdrop(mm);
 }
@@ -496,6 +606,29 @@ static int kvm_vm_release(struct inode *inode, struct file *filp)
        return 0;
 }
 
+#ifndef CONFIG_S390
+/*
+ * Allocation size is twice as large as the actual dirty bitmap size.
+ * This makes it possible to do double buffering: see x86's
+ * kvm_vm_ioctl_get_dirty_log().
+ */
+static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
+{
+       unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
+
+       if (dirty_bytes > PAGE_SIZE)
+               memslot->dirty_bitmap = vzalloc(dirty_bytes);
+       else
+               memslot->dirty_bitmap = kzalloc(dirty_bytes, GFP_KERNEL);
+
+       if (!memslot->dirty_bitmap)
+               return -ENOMEM;
+
+       memslot->dirty_bitmap_head = memslot->dirty_bitmap;
+       return 0;
+}
+#endif /* !CONFIG_S390 */
+
 /*
  * Allocate some memory and give it an address in the guest physical address
  * space.
@@ -514,6 +647,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
        unsigned long i;
        struct kvm_memory_slot *memslot;
        struct kvm_memory_slot old, new;
+       struct kvm_memslots *slots, *old_memslots;
 
        r = -EINVAL;
        /* General sanity checks */
@@ -521,22 +655,32 @@ int __kvm_set_memory_region(struct kvm *kvm,
                goto out;
        if (mem->guest_phys_addr & (PAGE_SIZE - 1))
                goto out;
-       if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
+       /* We can read the guest memory with __xxx_user() later on. */
+       if (user_alloc &&
+           ((mem->userspace_addr & (PAGE_SIZE - 1)) ||
+            !access_ok(VERIFY_WRITE,
+                       (void __user *)(unsigned long)mem->userspace_addr,
+                       mem->memory_size)))
                goto out;
        if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
                goto out;
        if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
                goto out;
 
-       memslot = &kvm->memslots[mem->slot];
+       memslot = &kvm->memslots->memslots[mem->slot];
        base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
        npages = mem->memory_size >> PAGE_SHIFT;
 
+       r = -EINVAL;
+       if (npages > KVM_MEM_MAX_NR_PAGES)
+               goto out;
+
        if (!npages)
                mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
 
        new = old = *memslot;
 
+       new.id = mem->slot;
        new.base_gfn = base_gfn;
        new.npages = npages;
        new.flags = mem->flags;
@@ -549,7 +693,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
        /* Check for overlaps */
        r = -EEXIST;
        for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
-               struct kvm_memory_slot *s = &kvm->memslots[i];
+               struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
 
                if (s == memslot || !s->npages)
                        continue;
@@ -567,23 +711,13 @@ int __kvm_set_memory_region(struct kvm *kvm,
        /* Allocate if a slot is being created */
 #ifndef CONFIG_S390
        if (npages && !new.rmap) {
-               new.rmap = vmalloc(npages * sizeof(struct page *));
+               new.rmap = vzalloc(npages * sizeof(*new.rmap));
 
                if (!new.rmap)
                        goto out_free;
 
-               memset(new.rmap, 0, npages * sizeof(*new.rmap));
-
                new.user_alloc = user_alloc;
-               /*
-                * hva_to_rmmap() serialzies with the mmu_lock and to be
-                * safe it has to ignore memslots with !user_alloc &&
-                * !userspace_addr.
-                */
-               if (user_alloc)
-                       new.userspace_addr = mem->userspace_addr;
-               else
-                       new.userspace_addr = 0;
+               new.userspace_addr = mem->userspace_addr;
        }
        if (!npages)
                goto skip_lpage;
@@ -600,21 +734,18 @@ int __kvm_set_memory_region(struct kvm *kvm,
                if (new.lpage_info[i])
                        continue;
 
-               lpages = 1 + (base_gfn + npages - 1) /
-                            KVM_PAGES_PER_HPAGE(level);
-               lpages -= base_gfn / KVM_PAGES_PER_HPAGE(level);
+               lpages = 1 + ((base_gfn + npages - 1)
+                            >> KVM_HPAGE_GFN_SHIFT(level));
+               lpages -= base_gfn >> KVM_HPAGE_GFN_SHIFT(level);
 
-               new.lpage_info[i] = vmalloc(lpages * sizeof(*new.lpage_info[i]));
+               new.lpage_info[i] = vzalloc(lpages * sizeof(*new.lpage_info[i]));
 
                if (!new.lpage_info[i])
                        goto out_free;
 
-               memset(new.lpage_info[i], 0,
-                      lpages * sizeof(*new.lpage_info[i]));
-
-               if (base_gfn % KVM_PAGES_PER_HPAGE(level))
+               if (base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
                        new.lpage_info[i][0].write_count = 1;
-               if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE(level))
+               if ((base_gfn+npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
                        new.lpage_info[i][lpages - 1].write_count = 1;
                ugfn = new.userspace_addr >> PAGE_SHIFT;
                /*
@@ -632,14 +763,9 @@ skip_lpage:
 
        /* Allocate page dirty bitmap if needed */
        if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
-               unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
-
-               new.dirty_bitmap = vmalloc(dirty_bytes);
-               if (!new.dirty_bitmap)
+               if (kvm_create_dirty_bitmap(&new) < 0)
                        goto out_free;
-               memset(new.dirty_bitmap, 0, dirty_bytes);
-               if (old.npages)
-                       kvm_arch_flush_shadow(kvm);
+               /* destroy any largepage mappings for dirty tracking */
        }
 #else  /* not defined CONFIG_S390 */
        new.user_alloc = user_alloc;
@@ -647,36 +773,76 @@ skip_lpage:
                new.userspace_addr = mem->userspace_addr;
 #endif /* not defined CONFIG_S390 */
 
-       if (!npages)
+       if (!npages) {
+               r = -ENOMEM;
+               slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
+               if (!slots)
+                       goto out_free;
+               memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
+               if (mem->slot >= slots->nmemslots)
+                       slots->nmemslots = mem->slot + 1;
+               slots->generation++;
+               slots->memslots[mem->slot].flags |= KVM_MEMSLOT_INVALID;
+
+               old_memslots = kvm->memslots;
+               rcu_assign_pointer(kvm->memslots, slots);
+               synchronize_srcu_expedited(&kvm->srcu);
+               /* From this point no new shadow pages pointing to a deleted
+                * memslot will be created.
+                *
+                * validation of sp->gfn happens in:
+                *      - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
+                *      - kvm_is_visible_gfn (mmu_check_roots)
+                */
                kvm_arch_flush_shadow(kvm);
+               kfree(old_memslots);
+       }
 
-       spin_lock(&kvm->mmu_lock);
-       if (mem->slot >= kvm->nmemslots)
-               kvm->nmemslots = mem->slot + 1;
+       r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
+       if (r)
+               goto out_free;
 
-       *memslot = new;
-       spin_unlock(&kvm->mmu_lock);
+       /* map the pages in iommu page table */
+       if (npages) {
+               r = kvm_iommu_map_pages(kvm, &new);
+               if (r)
+                       goto out_free;
+       }
 
-       r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
-       if (r) {
-               spin_lock(&kvm->mmu_lock);
-               *memslot = old;
-               spin_unlock(&kvm->mmu_lock);
+       r = -ENOMEM;
+       slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
+       if (!slots)
                goto out_free;
+       memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
+       if (mem->slot >= slots->nmemslots)
+               slots->nmemslots = mem->slot + 1;
+       slots->generation++;
+
+       /* actual memory is freed via old in kvm_free_physmem_slot below */
+       if (!npages) {
+               new.rmap = NULL;
+               new.dirty_bitmap = NULL;
+               for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i)
+                       new.lpage_info[i] = NULL;
        }
 
-       kvm_free_physmem_slot(&old, npages ? &new : NULL);
-       /* Slot deletion case: we have to update the current slot */
-       spin_lock(&kvm->mmu_lock);
-       if (!npages)
-               *memslot = old;
-       spin_unlock(&kvm->mmu_lock);
-#ifdef CONFIG_DMAR
-       /* map the pages in iommu page table */
-       r = kvm_iommu_map_pages(kvm, base_gfn, npages);
-       if (r)
-               goto out;
-#endif
+       slots->memslots[mem->slot] = new;
+       old_memslots = kvm->memslots;
+       rcu_assign_pointer(kvm->memslots, slots);
+       synchronize_srcu_expedited(&kvm->srcu);
+
+       kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
+
+       /*
+        * If the new memory slot is created, we need to clear all
+        * mmio sptes.
+        */
+       if (npages && old.base_gfn != mem->guest_phys_addr >> PAGE_SHIFT)
+               kvm_arch_flush_shadow(kvm);
+
+       kvm_free_physmem_slot(&old, &new);
+       kfree(old_memslots);
+
        return 0;
 
 out_free:
@@ -693,9 +859,9 @@ int kvm_set_memory_region(struct kvm *kvm,
 {
        int r;
 
-       down_write(&kvm->slots_lock);
+       mutex_lock(&kvm->slots_lock);
        r = __kvm_set_memory_region(kvm, mem, user_alloc);
-       up_write(&kvm->slots_lock);
+       mutex_unlock(&kvm->slots_lock);
        return r;
 }
 EXPORT_SYMBOL_GPL(kvm_set_memory_region);
@@ -715,19 +881,19 @@ int kvm_get_dirty_log(struct kvm *kvm,
 {
        struct kvm_memory_slot *memslot;
        int r, i;
-       int n;
+       unsigned long n;
        unsigned long any = 0;
 
        r = -EINVAL;
        if (log->slot >= KVM_MEMORY_SLOTS)
                goto out;
 
-       memslot = &kvm->memslots[log->slot];
+       memslot = &kvm->memslots->memslots[log->slot];
        r = -ENOENT;
        if (!memslot->dirty_bitmap)
                goto out;
 
-       n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
+       n = kvm_dirty_bitmap_bytes(memslot);
 
        for (i = 0; !any && i < n/sizeof(long); ++i)
                any = memslot->dirty_bitmap[i];
@@ -752,16 +918,40 @@ EXPORT_SYMBOL_GPL(kvm_disable_largepages);
 
 int is_error_page(struct page *page)
 {
-       return page == bad_page;
+       return page == bad_page || page == hwpoison_page || page == fault_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 || pfn == fault_pfn;
 }
 EXPORT_SYMBOL_GPL(is_error_pfn);
 
+int is_hwpoison_pfn(pfn_t pfn)
+{
+       return pfn == hwpoison_pfn;
+}
+EXPORT_SYMBOL_GPL(is_hwpoison_pfn);
+
+int is_fault_pfn(pfn_t pfn)
+{
+       return pfn == fault_pfn;
+}
+EXPORT_SYMBOL_GPL(is_fault_pfn);
+
+int is_noslot_pfn(pfn_t pfn)
+{
+       return pfn == bad_pfn;
+}
+EXPORT_SYMBOL_GPL(is_noslot_pfn);
+
+int is_invalid_pfn(pfn_t pfn)
+{
+       return pfn == hwpoison_pfn || pfn == fault_pfn;
+}
+EXPORT_SYMBOL_GPL(is_invalid_pfn);
+
 static inline unsigned long bad_hva(void)
 {
        return PAGE_OFFSET;
@@ -773,12 +963,13 @@ int kvm_is_error_hva(unsigned long addr)
 }
 EXPORT_SYMBOL_GPL(kvm_is_error_hva);
 
-struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
+static struct kvm_memory_slot *__gfn_to_memslot(struct kvm_memslots *slots,
+                                               gfn_t gfn)
 {
        int i;
 
-       for (i = 0; i < kvm->nmemslots; ++i) {
-               struct kvm_memory_slot *memslot = &kvm->memslots[i];
+       for (i = 0; i < slots->nmemslots; ++i) {
+               struct kvm_memory_slot *memslot = &slots->memslots[i];
 
                if (gfn >= memslot->base_gfn
                    && gfn < memslot->base_gfn + memslot->npages)
@@ -786,21 +977,23 @@ struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
        }
        return NULL;
 }
-EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
 
 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
 {
-       gfn = unalias_gfn(kvm, gfn);
-       return gfn_to_memslot_unaliased(kvm, gfn);
+       return __gfn_to_memslot(kvm_memslots(kvm), gfn);
 }
+EXPORT_SYMBOL_GPL(gfn_to_memslot);
 
 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
 {
        int i;
+       struct kvm_memslots *slots = kvm_memslots(kvm);
 
-       gfn = unalias_gfn(kvm, gfn);
        for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
-               struct kvm_memory_slot *memslot = &kvm->memslots[i];
+               struct kvm_memory_slot *memslot = &slots->memslots[i];
+
+               if (memslot->flags & KVM_MEMSLOT_INVALID)
+                       continue;
 
                if (gfn >= memslot->base_gfn
                    && gfn < memslot->base_gfn + memslot->npages)
@@ -810,59 +1003,228 @@ int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
 }
 EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
 
-unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
+unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
 {
-       struct kvm_memory_slot *slot;
+       struct vm_area_struct *vma;
+       unsigned long addr, size;
+
+       size = PAGE_SIZE;
+
+       addr = gfn_to_hva(kvm, gfn);
+       if (kvm_is_error_hva(addr))
+               return PAGE_SIZE;
+
+       down_read(&current->mm->mmap_sem);
+       vma = find_vma(current->mm, addr);
+       if (!vma)
+               goto out;
+
+       size = vma_kernel_pagesize(vma);
+
+out:
+       up_read(&current->mm->mmap_sem);
 
-       gfn = unalias_gfn(kvm, gfn);
-       slot = gfn_to_memslot_unaliased(kvm, gfn);
-       if (!slot)
+       return size;
+}
+
+static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
+                                    gfn_t *nr_pages)
+{
+       if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
                return bad_hva();
-       return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
+
+       if (nr_pages)
+               *nr_pages = slot->npages - (gfn - slot->base_gfn);
+
+       return gfn_to_hva_memslot(slot, gfn);
+}
+
+unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
+{
+       return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
 }
 EXPORT_SYMBOL_GPL(gfn_to_hva);
 
-pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
+static pfn_t get_fault_pfn(void)
+{
+       get_page(fault_page);
+       return fault_pfn;
+}
+
+int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm,
+       unsigned long start, int write, struct page **page)
+{
+       int flags = FOLL_TOUCH | FOLL_NOWAIT | FOLL_HWPOISON | FOLL_GET;
+
+       if (write)
+               flags |= FOLL_WRITE;
+
+       return __get_user_pages(tsk, mm, start, 1, flags, page, NULL, NULL);
+}
+
+static inline int check_user_page_hwpoison(unsigned long addr)
+{
+       int rc, flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_WRITE;
+
+       rc = __get_user_pages(current, current->mm, addr, 1,
+                             flags, NULL, NULL, NULL);
+       return rc == -EHWPOISON;
+}
+
+static pfn_t hva_to_pfn(struct kvm *kvm, unsigned long addr, bool atomic,
+                       bool *async, bool write_fault, bool *writable)
 {
        struct page *page[1];
-       unsigned long addr;
-       int npages;
+       int npages = 0;
        pfn_t pfn;
 
-       might_sleep();
+       /* we can do it either atomically or asynchronously, not both */
+       BUG_ON(atomic && async);
 
-       addr = gfn_to_hva(kvm, gfn);
-       if (kvm_is_error_hva(addr)) {
-               get_page(bad_page);
-               return page_to_pfn(bad_page);
-       }
+       BUG_ON(!write_fault && !writable);
+
+       if (writable)
+               *writable = true;
+
+       if (atomic || async)
+               npages = __get_user_pages_fast(addr, 1, 1, page);
 
-       npages = get_user_pages_fast(addr, 1, 1, page);
+       if (unlikely(npages != 1) && !atomic) {
+               might_sleep();
+
+               if (writable)
+                       *writable = write_fault;
+
+               if (async) {
+                       down_read(&current->mm->mmap_sem);
+                       npages = get_user_page_nowait(current, current->mm,
+                                                    addr, write_fault, page);
+                       up_read(&current->mm->mmap_sem);
+               } else
+                       npages = get_user_pages_fast(addr, 1, write_fault,
+                                                    page);
+
+               /* map read fault as writable if possible */
+               if (unlikely(!write_fault) && npages == 1) {
+                       struct page *wpage[1];
+
+                       npages = __get_user_pages_fast(addr, 1, 1, wpage);
+                       if (npages == 1) {
+                               *writable = true;
+                               put_page(page[0]);
+                               page[0] = wpage[0];
+                       }
+                       npages = 1;
+               }
+       }
 
        if (unlikely(npages != 1)) {
                struct vm_area_struct *vma;
 
-               down_read(&current->mm->mmap_sem);
-               vma = find_vma(current->mm, addr);
+               if (atomic)
+                       return get_fault_pfn();
 
-               if (vma == NULL || addr < vma->vm_start ||
-                   !(vma->vm_flags & VM_PFNMAP)) {
+               down_read(&current->mm->mmap_sem);
+               if (npages == -EHWPOISON ||
+                       (!async && check_user_page_hwpoison(addr))) {
                        up_read(&current->mm->mmap_sem);
-                       get_page(bad_page);
-                       return page_to_pfn(bad_page);
+                       get_page(hwpoison_page);
+                       return page_to_pfn(hwpoison_page);
                }
 
-               pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
+               vma = find_vma_intersection(current->mm, addr, addr+1);
+
+               if (vma == NULL)
+                       pfn = get_fault_pfn();
+               else if ((vma->vm_flags & VM_PFNMAP)) {
+                       pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
+                               vma->vm_pgoff;
+                       BUG_ON(!kvm_is_mmio_pfn(pfn));
+               } else {
+                       if (async && (vma->vm_flags & VM_WRITE))
+                               *async = true;
+                       pfn = get_fault_pfn();
+               }
                up_read(&current->mm->mmap_sem);
-               BUG_ON(!kvm_is_mmio_pfn(pfn));
        } else
                pfn = page_to_pfn(page[0]);
 
        return pfn;
 }
 
+pfn_t hva_to_pfn_atomic(struct kvm *kvm, unsigned long addr)
+{
+       return hva_to_pfn(kvm, addr, true, NULL, true, NULL);
+}
+EXPORT_SYMBOL_GPL(hva_to_pfn_atomic);
+
+static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
+                         bool write_fault, bool *writable)
+{
+       unsigned long addr;
+
+       if (async)
+               *async = false;
+
+       addr = gfn_to_hva(kvm, gfn);
+       if (kvm_is_error_hva(addr)) {
+               get_page(bad_page);
+               return page_to_pfn(bad_page);
+       }
+
+       return hva_to_pfn(kvm, addr, atomic, async, write_fault, writable);
+}
+
+pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
+{
+       return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
+}
+EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
+
+pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
+                      bool write_fault, bool *writable)
+{
+       return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
+}
+EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
+
+pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
+{
+       return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
+}
 EXPORT_SYMBOL_GPL(gfn_to_pfn);
 
+pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
+                     bool *writable)
+{
+       return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
+}
+EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
+
+pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
+                        struct kvm_memory_slot *slot, gfn_t gfn)
+{
+       unsigned long addr = gfn_to_hva_memslot(slot, gfn);
+       return hva_to_pfn(kvm, addr, false, NULL, true, NULL);
+}
+
+int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
+                                                                 int nr_pages)
+{
+       unsigned long addr;
+       gfn_t entry;
+
+       addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
+       if (kvm_is_error_hva(addr))
+               return -1;
+
+       if (entry < nr_pages)
+               return 0;
+
+       return __get_user_pages_fast(addr, nr_pages, 1, pages);
+}
+EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
+
 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
 {
        pfn_t pfn;
@@ -952,7 +1314,7 @@ int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
        addr = gfn_to_hva(kvm, gfn);
        if (kvm_is_error_hva(addr))
                return -EFAULT;
-       r = copy_from_user(data, (void __user *)addr + offset, len);
+       r = __copy_from_user(data, (void __user *)addr + offset, len);
        if (r)
                return -EFAULT;
        return 0;
@@ -1008,7 +1370,7 @@ int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
        addr = gfn_to_hva(kvm, gfn);
        if (kvm_is_error_hva(addr))
                return -EFAULT;
-       r = copy_to_user((void __user *)addr + offset, data, len);
+       r = __copy_to_user((void __user *)addr + offset, data, len);
        if (r)
                return -EFAULT;
        mark_page_dirty(kvm, gfn);
@@ -1036,9 +1398,71 @@ int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
        return 0;
 }
 
+int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
+                             gpa_t gpa)
+{
+       struct kvm_memslots *slots = kvm_memslots(kvm);
+       int offset = offset_in_page(gpa);
+       gfn_t gfn = gpa >> PAGE_SHIFT;
+
+       ghc->gpa = gpa;
+       ghc->generation = slots->generation;
+       ghc->memslot = __gfn_to_memslot(slots, gfn);
+       ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL);
+       if (!kvm_is_error_hva(ghc->hva))
+               ghc->hva += offset;
+       else
+               return -EFAULT;
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
+
+int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
+                          void *data, unsigned long len)
+{
+       struct kvm_memslots *slots = kvm_memslots(kvm);
+       int r;
+
+       if (slots->generation != ghc->generation)
+               kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
+
+       if (kvm_is_error_hva(ghc->hva))
+               return -EFAULT;
+
+       r = __copy_to_user((void __user *)ghc->hva, data, len);
+       if (r)
+               return -EFAULT;
+       mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
+
+int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
+                          void *data, unsigned long len)
+{
+       struct kvm_memslots *slots = kvm_memslots(kvm);
+       int r;
+
+       if (slots->generation != ghc->generation)
+               kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
+
+       if (kvm_is_error_hva(ghc->hva))
+               return -EFAULT;
+
+       r = __copy_from_user(data, (void __user *)ghc->hva, len);
+       if (r)
+               return -EFAULT;
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(kvm_read_guest_cached);
+
 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
 {
-       return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
+       return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
+                                   offset, len);
 }
 EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
 
@@ -1061,21 +1485,24 @@ int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
 }
 EXPORT_SYMBOL_GPL(kvm_clear_guest);
 
-void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
+void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
+                            gfn_t gfn)
 {
-       struct kvm_memory_slot *memslot;
-
-       gfn = unalias_gfn(kvm, gfn);
-       memslot = gfn_to_memslot_unaliased(kvm, gfn);
        if (memslot && memslot->dirty_bitmap) {
                unsigned long rel_gfn = gfn - memslot->base_gfn;
 
-               /* avoid RMW */
-               if (!generic_test_le_bit(rel_gfn, memslot->dirty_bitmap))
-                       generic___set_le_bit(rel_gfn, memslot->dirty_bitmap);
+               __set_bit_le(rel_gfn, memslot->dirty_bitmap);
        }
 }
 
+void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
+{
+       struct kvm_memory_slot *memslot;
+
+       memslot = gfn_to_memslot(kvm, gfn);
+       mark_page_dirty_in_slot(kvm, memslot, gfn);
+}
+
 /*
  * The vCPU has executed a HLT instruction with in-kernel mode enabled.
  */
@@ -1087,7 +1514,7 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)
                prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
 
                if (kvm_arch_vcpu_runnable(vcpu)) {
-                       set_bit(KVM_REQ_UNHALT, &vcpu->requests);
+                       kvm_make_request(KVM_REQ_UNHALT, vcpu);
                        break;
                }
                if (kvm_cpu_has_pending_timer(vcpu))
@@ -1109,18 +1536,55 @@ void kvm_resched(struct kvm_vcpu *vcpu)
 }
 EXPORT_SYMBOL_GPL(kvm_resched);
 
-void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu)
+void kvm_vcpu_on_spin(struct kvm_vcpu *me)
 {
-       ktime_t expires;
-       DEFINE_WAIT(wait);
-
-       prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
-
-       /* Sleep for 100 us, and hope lock-holder got scheduled */
-       expires = ktime_add_ns(ktime_get(), 100000UL);
-       schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
+       struct kvm *kvm = me->kvm;
+       struct kvm_vcpu *vcpu;
+       int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
+       int yielded = 0;
+       int pass;
+       int i;
 
-       finish_wait(&vcpu->wq, &wait);
+       /*
+        * We boost the priority of a VCPU that is runnable but not
+        * currently running, because it got preempted by something
+        * else and called schedule in __vcpu_run.  Hopefully that
+        * VCPU is holding the lock that we need and will release it.
+        * We approximate round-robin by starting at the last boosted VCPU.
+        */
+       for (pass = 0; pass < 2 && !yielded; pass++) {
+               kvm_for_each_vcpu(i, vcpu, kvm) {
+                       struct task_struct *task = NULL;
+                       struct pid *pid;
+                       if (!pass && i < last_boosted_vcpu) {
+                               i = last_boosted_vcpu;
+                               continue;
+                       } else if (pass && i > last_boosted_vcpu)
+                               break;
+                       if (vcpu == me)
+                               continue;
+                       if (waitqueue_active(&vcpu->wq))
+                               continue;
+                       rcu_read_lock();
+                       pid = rcu_dereference(vcpu->pid);
+                       if (pid)
+                               task = get_pid_task(vcpu->pid, PIDTYPE_PID);
+                       rcu_read_unlock();
+                       if (!task)
+                               continue;
+                       if (task->flags & PF_VCPU) {
+                               put_task_struct(task);
+                               continue;
+                       }
+                       if (yield_to(task, 1)) {
+                               put_task_struct(task);
+                               kvm->last_boosted_vcpu = i;
+                               yielded = 1;
+                               break;
+                       }
+                       put_task_struct(task);
+               }
+       }
 }
 EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
 
@@ -1167,8 +1631,11 @@ static int kvm_vcpu_release(struct inode *inode, struct file *filp)
 static struct file_operations kvm_vcpu_fops = {
        .release        = kvm_vcpu_release,
        .unlocked_ioctl = kvm_vcpu_ioctl,
-       .compat_ioctl   = kvm_vcpu_ioctl,
+#ifdef CONFIG_COMPAT
+       .compat_ioctl   = kvm_vcpu_compat_ioctl,
+#endif
        .mmap           = kvm_vcpu_mmap,
+       .llseek         = noop_llseek,
 };
 
 /*
@@ -1195,18 +1662,22 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
 
        r = kvm_arch_vcpu_setup(vcpu);
        if (r)
-               return r;
+               goto vcpu_destroy;
 
        mutex_lock(&kvm->lock);
+       if (!kvm_vcpu_compatible(vcpu)) {
+               r = -EINVAL;
+               goto unlock_vcpu_destroy;
+       }
        if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
                r = -EINVAL;
-               goto vcpu_destroy;
+               goto unlock_vcpu_destroy;
        }
 
        kvm_for_each_vcpu(r, v, kvm)
                if (v->vcpu_id == id) {
                        r = -EEXIST;
-                       goto vcpu_destroy;
+                       goto unlock_vcpu_destroy;
                }
 
        BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
@@ -1216,7 +1687,7 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
        r = create_vcpu_fd(vcpu);
        if (r < 0) {
                kvm_put_kvm(kvm);
-               goto vcpu_destroy;
+               goto unlock_vcpu_destroy;
        }
 
        kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
@@ -1230,8 +1701,9 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
        mutex_unlock(&kvm->lock);
        return r;
 
-vcpu_destroy:
+unlock_vcpu_destroy:
        mutex_unlock(&kvm->lock);
+vcpu_destroy:
        kvm_arch_vcpu_destroy(vcpu);
        return r;
 }
@@ -1258,12 +1730,25 @@ static long kvm_vcpu_ioctl(struct file *filp,
 
        if (vcpu->kvm->mm != current->mm)
                return -EIO;
+
+#if defined(CONFIG_S390) || defined(CONFIG_PPC)
+       /*
+        * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
+        * so vcpu_load() would break it.
+        */
+       if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
+               return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
+#endif
+
+
+       vcpu_load(vcpu);
        switch (ioctl) {
        case KVM_RUN:
                r = -EINVAL;
                if (arg)
                        goto out;
                r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
+               trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
                break;
        case KVM_GET_REGS: {
                struct kvm_regs *kvm_regs;
@@ -1400,7 +1885,7 @@ out_free2:
                                goto out;
                        p = &sigset;
                }
-               r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
+               r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
                break;
        }
        case KVM_GET_FPU: {
@@ -1435,11 +1920,56 @@ out_free2:
                r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
        }
 out:
+       vcpu_put(vcpu);
        kfree(fpu);
        kfree(kvm_sregs);
        return r;
 }
 
+#ifdef CONFIG_COMPAT
+static long kvm_vcpu_compat_ioctl(struct file *filp,
+                                 unsigned int ioctl, unsigned long arg)
+{
+       struct kvm_vcpu *vcpu = filp->private_data;
+       void __user *argp = compat_ptr(arg);
+       int r;
+
+       if (vcpu->kvm->mm != current->mm)
+               return -EIO;
+
+       switch (ioctl) {
+       case KVM_SET_SIGNAL_MASK: {
+               struct kvm_signal_mask __user *sigmask_arg = argp;
+               struct kvm_signal_mask kvm_sigmask;
+               compat_sigset_t csigset;
+               sigset_t sigset;
+
+               if (argp) {
+                       r = -EFAULT;
+                       if (copy_from_user(&kvm_sigmask, argp,
+                                          sizeof kvm_sigmask))
+                               goto out;
+                       r = -EINVAL;
+                       if (kvm_sigmask.len != sizeof csigset)
+                               goto out;
+                       r = -EFAULT;
+                       if (copy_from_user(&csigset, sigmask_arg->sigset,
+                                          sizeof csigset))
+                               goto out;
+               }
+               sigset_from_compat(&sigset, &csigset);
+               r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
+               break;
+       }
+       default:
+               r = kvm_vcpu_ioctl(filp, ioctl, arg);
+       }
+
+out:
+       return r;
+}
+#endif
+
 static long kvm_vm_ioctl(struct file *filp,
                           unsigned int ioctl, unsigned long arg)
 {
@@ -1485,7 +2015,6 @@ static long kvm_vm_ioctl(struct file *filp,
                r = -EFAULT;
                if (copy_from_user(&zone, argp, sizeof zone))
                        goto out;
-               r = -ENXIO;
                r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
                if (r)
                        goto out;
@@ -1497,7 +2026,6 @@ static long kvm_vm_ioctl(struct file *filp,
                r = -EFAULT;
                if (copy_from_user(&zone, argp, sizeof zone))
                        goto out;
-               r = -ENXIO;
                r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
                if (r)
                        goto out;
@@ -1627,21 +2155,29 @@ static struct file_operations kvm_vm_fops = {
        .compat_ioctl   = kvm_vm_compat_ioctl,
 #endif
        .mmap           = kvm_vm_mmap,
+       .llseek         = noop_llseek,
 };
 
 static int kvm_dev_ioctl_create_vm(void)
 {
-       int fd;
+       int r;
        struct kvm *kvm;
 
        kvm = kvm_create_vm();
        if (IS_ERR(kvm))
                return PTR_ERR(kvm);
-       fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
-       if (fd < 0)
+#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
+       r = kvm_coalesced_mmio_init(kvm);
+       if (r < 0) {
+               kvm_put_kvm(kvm);
+               return r;
+       }
+#endif
+       r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
+       if (r < 0)
                kvm_put_kvm(kvm);
 
-       return fd;
+       return r;
 }
 
 static long kvm_dev_ioctl_check_extension_generic(long arg)
@@ -1713,6 +2249,7 @@ out:
 static struct file_operations kvm_chardev_ops = {
        .unlocked_ioctl = kvm_dev_ioctl,
        .compat_ioctl   = kvm_dev_ioctl,
+       .llseek         = noop_llseek,
 };
 
 static struct miscdevice kvm_dev = {
@@ -1721,7 +2258,7 @@ static struct miscdevice kvm_dev = {
        &kvm_chardev_ops,
 };
 
-static void hardware_enable(void *junk)
+static void hardware_enable_nolock(void *junk)
 {
        int cpu = raw_smp_processor_id();
        int r;
@@ -1741,7 +2278,14 @@ static void hardware_enable(void *junk)
        }
 }
 
-static void hardware_disable(void *junk)
+static void hardware_enable(void *junk)
+{
+       raw_spin_lock(&kvm_lock);
+       hardware_enable_nolock(junk);
+       raw_spin_unlock(&kvm_lock);
+}
+
+static void hardware_disable_nolock(void *junk)
 {
        int cpu = raw_smp_processor_id();
 
@@ -1751,32 +2295,39 @@ static void hardware_disable(void *junk)
        kvm_arch_hardware_disable(NULL);
 }
 
+static void hardware_disable(void *junk)
+{
+       raw_spin_lock(&kvm_lock);
+       hardware_disable_nolock(junk);
+       raw_spin_unlock(&kvm_lock);
+}
+
 static void hardware_disable_all_nolock(void)
 {
        BUG_ON(!kvm_usage_count);
 
        kvm_usage_count--;
        if (!kvm_usage_count)
-               on_each_cpu(hardware_disable, NULL, 1);
+               on_each_cpu(hardware_disable_nolock, NULL, 1);
 }
 
 static void hardware_disable_all(void)
 {
-       spin_lock(&kvm_lock);
+       raw_spin_lock(&kvm_lock);
        hardware_disable_all_nolock();
-       spin_unlock(&kvm_lock);
+       raw_spin_unlock(&kvm_lock);
 }
 
 static int hardware_enable_all(void)
 {
        int r = 0;
 
-       spin_lock(&kvm_lock);
+       raw_spin_lock(&kvm_lock);
 
        kvm_usage_count++;
        if (kvm_usage_count == 1) {
                atomic_set(&hardware_enable_failed, 0);
-               on_each_cpu(hardware_enable, NULL, 1);
+               on_each_cpu(hardware_enable_nolock, NULL, 1);
 
                if (atomic_read(&hardware_enable_failed)) {
                        hardware_disable_all_nolock();
@@ -1784,7 +2335,7 @@ static int hardware_enable_all(void)
                }
        }
 
-       spin_unlock(&kvm_lock);
+       raw_spin_unlock(&kvm_lock);
 
        return r;
 }
@@ -1804,31 +2355,22 @@ static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
                       cpu);
                hardware_disable(NULL);
                break;
-       case CPU_UP_CANCELED:
-               printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
-                      cpu);
-               smp_call_function_single(cpu, hardware_disable, NULL, 1);
-               break;
-       case CPU_ONLINE:
+       case CPU_STARTING:
                printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
                       cpu);
-               smp_call_function_single(cpu, hardware_enable, NULL, 1);
+               hardware_enable(NULL);
                break;
        }
        return NOTIFY_OK;
 }
 
 
-asmlinkage void kvm_handle_fault_on_reboot(void)
+asmlinkage void kvm_spurious_fault(void)
 {
-       if (kvm_rebooting)
-               /* spin while reset goes on */
-               while (true)
-                       ;
        /* Fault while not rebooting.  We want the trace. */
        BUG();
 }
-EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
+EXPORT_SYMBOL_GPL(kvm_spurious_fault);
 
 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
                      void *v)
@@ -1841,7 +2383,7 @@ static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
         */
        printk(KERN_INFO "kvm: exiting hardware virtualization\n");
        kvm_rebooting = true;
-       on_each_cpu(hardware_disable, NULL, 1);
+       on_each_cpu(hardware_disable_nolock, NULL, 1);
        return NOTIFY_OK;
 }
 
@@ -1850,92 +2392,189 @@ static struct notifier_block kvm_reboot_notifier = {
        .priority = 0,
 };
 
-void kvm_io_bus_init(struct kvm_io_bus *bus)
-{
-       memset(bus, 0, sizeof(*bus));
-}
-
-void kvm_io_bus_destroy(struct kvm_io_bus *bus)
+static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
 {
        int i;
 
        for (i = 0; i < bus->dev_count; i++) {
-               struct kvm_io_device *pos = bus->devs[i];
+               struct kvm_io_device *pos = bus->range[i].dev;
 
                kvm_iodevice_destructor(pos);
        }
+       kfree(bus);
+}
+
+int kvm_io_bus_sort_cmp(const void *p1, const void *p2)
+{
+       const struct kvm_io_range *r1 = p1;
+       const struct kvm_io_range *r2 = p2;
+
+       if (r1->addr < r2->addr)
+               return -1;
+       if (r1->addr + r1->len > r2->addr + r2->len)
+               return 1;
+       return 0;
+}
+
+int kvm_io_bus_insert_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev,
+                         gpa_t addr, int len)
+{
+       if (bus->dev_count == NR_IOBUS_DEVS)
+               return -ENOSPC;
+
+       bus->range[bus->dev_count++] = (struct kvm_io_range) {
+               .addr = addr,
+               .len = len,
+               .dev = dev,
+       };
+
+       sort(bus->range, bus->dev_count, sizeof(struct kvm_io_range),
+               kvm_io_bus_sort_cmp, NULL);
+
+       return 0;
+}
+
+int kvm_io_bus_get_first_dev(struct kvm_io_bus *bus,
+                            gpa_t addr, int len)
+{
+       struct kvm_io_range *range, key;
+       int off;
+
+       key = (struct kvm_io_range) {
+               .addr = addr,
+               .len = len,
+       };
+
+       range = bsearch(&key, bus->range, bus->dev_count,
+                       sizeof(struct kvm_io_range), kvm_io_bus_sort_cmp);
+       if (range == NULL)
+               return -ENOENT;
+
+       off = range - bus->range;
+
+       while (off > 0 && kvm_io_bus_sort_cmp(&key, &bus->range[off-1]) == 0)
+               off--;
+
+       return off;
 }
 
 /* kvm_io_bus_write - called under kvm->slots_lock */
-int kvm_io_bus_write(struct kvm_io_bus *bus, gpa_t addr,
+int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
                     int len, const void *val)
 {
-       int i;
-       for (i = 0; i < bus->dev_count; i++)
-               if (!kvm_iodevice_write(bus->devs[i], addr, len, val))
+       int idx;
+       struct kvm_io_bus *bus;
+       struct kvm_io_range range;
+
+       range = (struct kvm_io_range) {
+               .addr = addr,
+               .len = len,
+       };
+
+       bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
+       idx = kvm_io_bus_get_first_dev(bus, addr, len);
+       if (idx < 0)
+               return -EOPNOTSUPP;
+
+       while (idx < bus->dev_count &&
+               kvm_io_bus_sort_cmp(&range, &bus->range[idx]) == 0) {
+               if (!kvm_iodevice_write(bus->range[idx].dev, addr, len, val))
                        return 0;
+               idx++;
+       }
+
        return -EOPNOTSUPP;
 }
 
 /* kvm_io_bus_read - called under kvm->slots_lock */
-int kvm_io_bus_read(struct kvm_io_bus *bus, gpa_t addr, int len, void *val)
+int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
+                   int len, void *val)
 {
-       int i;
-       for (i = 0; i < bus->dev_count; i++)
-               if (!kvm_iodevice_read(bus->devs[i], addr, len, val))
-                       return 0;
-       return -EOPNOTSUPP;
-}
+       int idx;
+       struct kvm_io_bus *bus;
+       struct kvm_io_range range;
 
-int kvm_io_bus_register_dev(struct kvm *kvm, struct kvm_io_bus *bus,
-                            struct kvm_io_device *dev)
-{
-       int ret;
+       range = (struct kvm_io_range) {
+               .addr = addr,
+               .len = len,
+       };
+
+       bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
+       idx = kvm_io_bus_get_first_dev(bus, addr, len);
+       if (idx < 0)
+               return -EOPNOTSUPP;
 
-       down_write(&kvm->slots_lock);
-       ret = __kvm_io_bus_register_dev(bus, dev);
-       up_write(&kvm->slots_lock);
+       while (idx < bus->dev_count &&
+               kvm_io_bus_sort_cmp(&range, &bus->range[idx]) == 0) {
+               if (!kvm_iodevice_read(bus->range[idx].dev, addr, len, val))
+                       return 0;
+               idx++;
+       }
 
-       return ret;
+       return -EOPNOTSUPP;
 }
 
-/* An unlocked version. Caller must have write lock on slots_lock. */
-int __kvm_io_bus_register_dev(struct kvm_io_bus *bus,
-                             struct kvm_io_device *dev)
+/* Caller must hold slots_lock. */
+int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
+                           int len, struct kvm_io_device *dev)
 {
+       struct kvm_io_bus *new_bus, *bus;
+
+       bus = kvm->buses[bus_idx];
        if (bus->dev_count > NR_IOBUS_DEVS-1)
                return -ENOSPC;
 
-       bus->devs[bus->dev_count++] = dev;
+       new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
+       if (!new_bus)
+               return -ENOMEM;
+       memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
+       kvm_io_bus_insert_dev(new_bus, dev, addr, len);
+       rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
+       synchronize_srcu_expedited(&kvm->srcu);
+       kfree(bus);
 
        return 0;
 }
 
-void kvm_io_bus_unregister_dev(struct kvm *kvm,
-                              struct kvm_io_bus *bus,
-                              struct kvm_io_device *dev)
+/* Caller must hold slots_lock. */
+int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
+                             struct kvm_io_device *dev)
 {
-       down_write(&kvm->slots_lock);
-       __kvm_io_bus_unregister_dev(bus, dev);
-       up_write(&kvm->slots_lock);
-}
+       int i, r;
+       struct kvm_io_bus *new_bus, *bus;
 
-/* An unlocked version. Caller must have write lock on slots_lock. */
-void __kvm_io_bus_unregister_dev(struct kvm_io_bus *bus,
-                                struct kvm_io_device *dev)
-{
-       int i;
+       new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
+       if (!new_bus)
+               return -ENOMEM;
 
-       for (i = 0; i < bus->dev_count; i++)
-               if (bus->devs[i] == dev) {
-                       bus->devs[i] = bus->devs[--bus->dev_count];
+       bus = kvm->buses[bus_idx];
+       memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
+
+       r = -ENOENT;
+       for (i = 0; i < new_bus->dev_count; i++)
+               if (new_bus->range[i].dev == dev) {
+                       r = 0;
+                       new_bus->dev_count--;
+                       new_bus->range[i] = new_bus->range[new_bus->dev_count];
+                       sort(new_bus->range, new_bus->dev_count,
+                            sizeof(struct kvm_io_range),
+                            kvm_io_bus_sort_cmp, NULL);
                        break;
                }
+
+       if (r) {
+               kfree(new_bus);
+               return r;
+       }
+
+       rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
+       synchronize_srcu_expedited(&kvm->srcu);
+       kfree(bus);
+       return r;
 }
 
 static struct notifier_block kvm_cpu_notifier = {
        .notifier_call = kvm_cpu_hotplug,
-       .priority = 20, /* must be > scheduler priority */
 };
 
 static int vm_stat_get(void *_offset, u64 *val)
@@ -1944,10 +2583,10 @@ static int vm_stat_get(void *_offset, u64 *val)
        struct kvm *kvm;
 
        *val = 0;
-       spin_lock(&kvm_lock);
+       raw_spin_lock(&kvm_lock);
        list_for_each_entry(kvm, &vm_list, vm_list)
                *val += *(u32 *)((void *)kvm + offset);
-       spin_unlock(&kvm_lock);
+       raw_spin_unlock(&kvm_lock);
        return 0;
 }
 
@@ -1961,12 +2600,12 @@ static int vcpu_stat_get(void *_offset, u64 *val)
        int i;
 
        *val = 0;
-       spin_lock(&kvm_lock);
+       raw_spin_lock(&kvm_lock);
        list_for_each_entry(kvm, &vm_list, vm_list)
                kvm_for_each_vcpu(i, vcpu, kvm)
                        *val += *(u32 *)((void *)vcpu + offset);
 
-       spin_unlock(&kvm_lock);
+       raw_spin_unlock(&kvm_lock);
        return 0;
 }
 
@@ -1997,31 +2636,26 @@ static void kvm_exit_debug(void)
        debugfs_remove(kvm_debugfs_dir);
 }
 
-static int kvm_suspend(struct sys_device *dev, pm_message_t state)
+static int kvm_suspend(void)
 {
        if (kvm_usage_count)
-               hardware_disable(NULL);
+               hardware_disable_nolock(NULL);
        return 0;
 }
 
-static int kvm_resume(struct sys_device *dev)
+static void kvm_resume(void)
 {
-       if (kvm_usage_count)
-               hardware_enable(NULL);
-       return 0;
+       if (kvm_usage_count) {
+               WARN_ON(raw_spin_is_locked(&kvm_lock));
+               hardware_enable_nolock(NULL);
+       }
 }
 
-static struct sysdev_class kvm_sysdev_class = {
-       .name = "kvm",
+static struct syscore_ops kvm_syscore_ops = {
        .suspend = kvm_suspend,
        .resume = kvm_resume,
 };
 
-static struct sys_device kvm_sysdev = {
-       .id = 0,
-       .cls = &kvm_sysdev_class,
-};
-
 struct page *bad_page;
 pfn_t bad_pfn;
 
@@ -2046,7 +2680,7 @@ static void kvm_sched_out(struct preempt_notifier *pn,
        kvm_arch_vcpu_put(vcpu);
 }
 
-int kvm_init(void *opaque, unsigned int vcpu_size,
+int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
                  struct module *module)
 {
        int r;
@@ -2065,6 +2699,24 @@ int kvm_init(void *opaque, unsigned int vcpu_size,
 
        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);
+
+       fault_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
+
+       if (fault_page == NULL) {
+               r = -ENOMEM;
+               goto out_free_0;
+       }
+
+       fault_pfn = page_to_pfn(fault_page);
+
        if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
                r = -ENOMEM;
                goto out_free_0;
@@ -2087,23 +2739,20 @@ int kvm_init(void *opaque, unsigned int vcpu_size,
                goto out_free_2;
        register_reboot_notifier(&kvm_reboot_notifier);
 
-       r = sysdev_class_register(&kvm_sysdev_class);
-       if (r)
-               goto out_free_3;
-
-       r = sysdev_register(&kvm_sysdev);
-       if (r)
-               goto out_free_4;
-
        /* A kmem cache lets us meet the alignment requirements of fx_save. */
-       kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
-                                          __alignof__(struct kvm_vcpu),
+       if (!vcpu_align)
+               vcpu_align = __alignof__(struct kvm_vcpu);
+       kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
                                           0, NULL);
        if (!kvm_vcpu_cache) {
                r = -ENOMEM;
-               goto out_free_5;
+               goto out_free_3;
        }
 
+       r = kvm_async_pf_init();
+       if (r)
+               goto out_free;
+
        kvm_chardev_ops.owner = module;
        kvm_vm_fops.owner = module;
        kvm_vcpu_fops.owner = module;
@@ -2111,9 +2760,11 @@ int kvm_init(void *opaque, unsigned int vcpu_size,
        r = misc_register(&kvm_dev);
        if (r) {
                printk(KERN_ERR "kvm: misc device register failed\n");
-               goto out_free;
+               goto out_unreg;
        }
 
+       register_syscore_ops(&kvm_syscore_ops);
+
        kvm_preempt_ops.sched_in = kvm_sched_in;
        kvm_preempt_ops.sched_out = kvm_sched_out;
 
@@ -2121,12 +2772,10 @@ int kvm_init(void *opaque, unsigned int vcpu_size,
 
        return 0;
 
+out_unreg:
+       kvm_async_pf_deinit();
 out_free:
        kmem_cache_destroy(kvm_vcpu_cache);
-out_free_5:
-       sysdev_unregister(&kvm_sysdev);
-out_free_4:
-       sysdev_class_unregister(&kvm_sysdev_class);
 out_free_3:
        unregister_reboot_notifier(&kvm_reboot_notifier);
        unregister_cpu_notifier(&kvm_cpu_notifier);
@@ -2136,6 +2785,10 @@ out_free_1:
 out_free_0a:
        free_cpumask_var(cpus_hardware_enabled);
 out_free_0:
+       if (fault_page)
+               __free_page(fault_page);
+       if (hwpoison_page)
+               __free_page(hwpoison_page);
        __free_page(bad_page);
 out:
        kvm_arch_exit();
@@ -2146,18 +2799,18 @@ EXPORT_SYMBOL_GPL(kvm_init);
 
 void kvm_exit(void)
 {
-       tracepoint_synchronize_unregister();
        kvm_exit_debug();
        misc_deregister(&kvm_dev);
        kmem_cache_destroy(kvm_vcpu_cache);
-       sysdev_unregister(&kvm_sysdev);
-       sysdev_class_unregister(&kvm_sysdev_class);
+       kvm_async_pf_deinit();
+       unregister_syscore_ops(&kvm_syscore_ops);
        unregister_reboot_notifier(&kvm_reboot_notifier);
        unregister_cpu_notifier(&kvm_cpu_notifier);
-       on_each_cpu(hardware_disable, NULL, 1);
+       on_each_cpu(hardware_disable_nolock, NULL, 1);
        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);