KVM: convert custom marker based tracing to event traces
[safe/jmp/linux-2.6] / virt / kvm / kvm_main.c
index d47e660..04bdedd 100644 (file)
 #include "irq.h"
 #endif
 
+#define CREATE_TRACE_POINTS
+#include <trace/events/kvm.h>
+
 MODULE_AUTHOR("Qumranet");
 MODULE_LICENSE("GPL");
 
+/*
+ * Ordering of locks:
+ *
+ *             kvm->lock --> kvm->irq_lock
+ */
+
 DEFINE_SPINLOCK(kvm_lock);
 LIST_HEAD(vm_list);
 
@@ -79,6 +88,8 @@ static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
 
 static bool kvm_rebooting;
 
+static bool largepages_enabled = true;
+
 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
 static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
                                                      int assigned_dev_id)
@@ -126,11 +137,7 @@ static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
                                    interrupt_work);
        kvm = assigned_dev->kvm;
 
-       /* This is taken to safely inject irq inside the guest. When
-        * the interrupt injection (or the ioapic code) uses a
-        * finer-grained lock, update this
-        */
-       mutex_lock(&kvm->lock);
+       mutex_lock(&kvm->irq_lock);
        spin_lock_irq(&assigned_dev->assigned_dev_lock);
        if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
                struct kvm_guest_msix_entry *guest_entries =
@@ -149,7 +156,7 @@ static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
                            assigned_dev->guest_irq, 1);
 
        spin_unlock_irq(&assigned_dev->assigned_dev_lock);
-       mutex_unlock(&assigned_dev->kvm->lock);
+       mutex_unlock(&assigned_dev->kvm->irq_lock);
 }
 
 static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
@@ -207,7 +214,7 @@ static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
 static void deassign_guest_irq(struct kvm *kvm,
                               struct kvm_assigned_dev_kernel *assigned_dev)
 {
-       kvm_unregister_irq_ack_notifier(&assigned_dev->ack_notifier);
+       kvm_unregister_irq_ack_notifier(kvm, &assigned_dev->ack_notifier);
        assigned_dev->ack_notifier.gsi = -1;
 
        if (assigned_dev->irq_source_id != -1)
@@ -687,11 +694,6 @@ out:
 }
 #endif
 
-static inline int valid_vcpu(int n)
-{
-       return likely(n >= 0 && n < KVM_MAX_VCPUS);
-}
-
 inline int kvm_is_mmio_pfn(pfn_t pfn)
 {
        if (pfn_valid(pfn)) {
@@ -741,10 +743,7 @@ static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
 
        me = get_cpu();
        spin_lock(&kvm->requests_lock);
-       for (i = 0; i < KVM_MAX_VCPUS; ++i) {
-               vcpu = kvm->vcpus[i];
-               if (!vcpu)
-                       continue;
+       kvm_for_each_vcpu(i, vcpu, kvm) {
                if (test_and_set_bit(req, &vcpu->requests))
                        continue;
                cpu = vcpu->cpu;
@@ -1180,9 +1179,11 @@ int __kvm_set_memory_region(struct kvm *kvm,
                ugfn = new.userspace_addr >> PAGE_SHIFT;
                /*
                 * If the gfn and userspace address are not aligned wrt each
-                * other, disable large page support for this slot
+                * other, or if explicitly asked to, disable large page
+                * support for this slot
                 */
-               if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE - 1))
+               if ((base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE - 1) ||
+                   !largepages_enabled)
                        for (i = 0; i < largepages; ++i)
                                new.lpage_info[i].write_count = 1;
        }
@@ -1297,6 +1298,12 @@ out:
        return r;
 }
 
+void kvm_disable_largepages(void)
+{
+       largepages_enabled = false;
+}
+EXPORT_SYMBOL_GPL(kvm_disable_largepages);
+
 int is_error_page(struct page *page)
 {
        return page == bad_page;
@@ -1712,24 +1719,18 @@ static struct file_operations kvm_vcpu_fops = {
  */
 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
 {
-       int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
-       if (fd < 0)
-               kvm_put_kvm(vcpu->kvm);
-       return fd;
+       return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
 }
 
 /*
  * Creates some virtual cpus.  Good luck creating more than one.
  */
-static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
+static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
 {
        int r;
-       struct kvm_vcpu *vcpu;
+       struct kvm_vcpu *vcpu, *v;
 
-       if (!valid_vcpu(n))
-               return -EINVAL;
-
-       vcpu = kvm_arch_vcpu_create(kvm, n);
+       vcpu = kvm_arch_vcpu_create(kvm, id);
        if (IS_ERR(vcpu))
                return PTR_ERR(vcpu);
 
@@ -1740,23 +1741,38 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
                return r;
 
        mutex_lock(&kvm->lock);
-       if (kvm->vcpus[n]) {
-               r = -EEXIST;
+       if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
+               r = -EINVAL;
                goto vcpu_destroy;
        }
-       kvm->vcpus[n] = vcpu;
-       mutex_unlock(&kvm->lock);
+
+       kvm_for_each_vcpu(r, v, kvm)
+               if (v->vcpu_id == id) {
+                       r = -EEXIST;
+                       goto vcpu_destroy;
+               }
+
+       BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
 
        /* Now it's all set up, let userspace reach it */
        kvm_get_kvm(kvm);
        r = create_vcpu_fd(vcpu);
-       if (r < 0)
-               goto unlink;
+       if (r < 0) {
+               kvm_put_kvm(kvm);
+               goto vcpu_destroy;
+       }
+
+       kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
+       smp_wmb();
+       atomic_inc(&kvm->online_vcpus);
+
+#ifdef CONFIG_KVM_APIC_ARCHITECTURE
+       if (kvm->bsp_vcpu_id == id)
+               kvm->bsp_vcpu = vcpu;
+#endif
+       mutex_unlock(&kvm->lock);
        return r;
 
-unlink:
-       mutex_lock(&kvm->lock);
-       kvm->vcpus[n] = NULL;
 vcpu_destroy:
        mutex_unlock(&kvm->lock);
        kvm_arch_vcpu_destroy(vcpu);
@@ -2229,6 +2245,15 @@ static long kvm_vm_ioctl(struct file *filp,
                r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
                break;
        }
+#ifdef CONFIG_KVM_APIC_ARCHITECTURE
+       case KVM_SET_BOOT_CPU_ID:
+               r = 0;
+               if (atomic_read(&kvm->online_vcpus) != 0)
+                       r = -EBUSY;
+               else
+                       kvm->bsp_vcpu_id = arg;
+               break;
+#endif
        default:
                r = kvm_arch_vm_ioctl(filp, ioctl, arg);
        }
@@ -2295,6 +2320,9 @@ static long kvm_dev_ioctl_check_extension_generic(long arg)
        case KVM_CAP_USER_MEMORY:
        case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
        case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
+#ifdef CONFIG_KVM_APIC_ARCHITECTURE
+       case KVM_CAP_SET_BOOT_CPU_ID:
+#endif
                return 1;
 #ifdef CONFIG_HAVE_KVM_IRQCHIP
        case KVM_CAP_IRQ_ROUTING:
@@ -2508,11 +2536,9 @@ static int vcpu_stat_get(void *_offset, u64 *val)
        *val = 0;
        spin_lock(&kvm_lock);
        list_for_each_entry(kvm, &vm_list, vm_list)
-               for (i = 0; i < KVM_MAX_VCPUS; ++i) {
-                       vcpu = kvm->vcpus[i];
-                       if (vcpu)
-                               *val += *(u32 *)((void *)vcpu + offset);
-               }
+               kvm_for_each_vcpu(i, vcpu, kvm)
+                       *val += *(u32 *)((void *)vcpu + offset);
+
        spin_unlock(&kvm_lock);
        return 0;
 }
@@ -2695,6 +2721,7 @@ EXPORT_SYMBOL_GPL(kvm_init);
 void kvm_exit(void)
 {
        kvm_trace_cleanup();
+       tracepoint_synchronize_unregister();
        misc_deregister(&kvm_dev);
        kmem_cache_destroy(kvm_vcpu_cache);
        sysdev_unregister(&kvm_sysdev);