KVM: x86: new irqchip mode KVM_IRQCHIP_INIT_IN_PROGRESS
authorDavid Hildenbrand <david@redhat.com>
Fri, 7 Apr 2017 08:50:19 +0000 (10:50 +0200)
committerRadim Krčmář <rkrcmar@redhat.com>
Wed, 12 Apr 2017 18:17:13 +0000 (20:17 +0200)
Let's add a new mode and set it while we create the irqchip via
KVM_CREATE_IRQCHIP and KVM_CAP_SPLIT_IRQCHIP.

This mode will be used later to test if adding routes
(in kvm_set_routing_entry()) is already allowed.

Signed-off-by: David Hildenbrand <david@redhat.com>
Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
arch/x86/include/asm/kvm_host.h
arch/x86/kvm/irq.h
arch/x86/kvm/x86.c

index d962fa998a6fc523e0e9ed5c90aad9d452c6601d..2cc5ec7cc6f55d67e2483f8e1f3f4c89bdba1990 100644 (file)
@@ -726,6 +726,7 @@ struct kvm_hv {
 
 enum kvm_irqchip_mode {
        KVM_IRQCHIP_NONE,
+       KVM_IRQCHIP_INIT_IN_PROGRESS, /* temporarily set during creation */
        KVM_IRQCHIP_KERNEL,       /* created with KVM_CREATE_IRQCHIP */
        KVM_IRQCHIP_SPLIT,        /* created with KVM_CAP_SPLIT_IRQCHIP */
 };
index 40d5b2cf60611ec871b64990b42a38579c269e8d..59e05febc8fb8ecb51d6c5c022bdb2751f500f1d 100644 (file)
@@ -93,21 +93,29 @@ static inline int pic_in_kernel(struct kvm *kvm)
 
 static inline int irqchip_split(struct kvm *kvm)
 {
-       return kvm->arch.irqchip_mode == KVM_IRQCHIP_SPLIT;
+       int mode = kvm->arch.irqchip_mode;
+
+       /* Matches smp_wmb() when setting irqchip_mode */
+       smp_rmb();
+       return mode == KVM_IRQCHIP_SPLIT;
 }
 
 static inline int irqchip_kernel(struct kvm *kvm)
 {
-       return kvm->arch.irqchip_mode == KVM_IRQCHIP_KERNEL;
+       int mode = kvm->arch.irqchip_mode;
+
+       /* Matches smp_wmb() when setting irqchip_mode */
+       smp_rmb();
+       return mode == KVM_IRQCHIP_KERNEL;
 }
 
 static inline int irqchip_in_kernel(struct kvm *kvm)
 {
-       bool ret = kvm->arch.irqchip_mode != KVM_IRQCHIP_NONE;
+       int mode = kvm->arch.irqchip_mode;
 
-       /* Matches with wmb after initializing kvm->irq_routing. */
+       /* Matches smp_wmb() when setting irqchip_mode */
        smp_rmb();
-       return ret;
+       return mode > KVM_IRQCHIP_INIT_IN_PROGRESS;
 }
 
 void kvm_pic_reset(struct kvm_kpic_state *s);
index 6bc47e2712c87dc1a752a7fdca7dea9afe387daf..c7956ce6a242640566003dccc3502600377de6c5 100644 (file)
@@ -3928,9 +3928,14 @@ static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
                        goto split_irqchip_unlock;
                if (kvm->created_vcpus)
                        goto split_irqchip_unlock;
+               kvm->arch.irqchip_mode = KVM_IRQCHIP_INIT_IN_PROGRESS;
                r = kvm_setup_empty_irq_routing(kvm);
-               if (r)
+               if (r) {
+                       kvm->arch.irqchip_mode = KVM_IRQCHIP_NONE;
+                       /* Pairs with smp_rmb() when reading irqchip_mode */
+                       smp_wmb();
                        goto split_irqchip_unlock;
+               }
                /* Pairs with irqchip_in_kernel. */
                smp_wmb();
                kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT;
@@ -4018,8 +4023,12 @@ long kvm_arch_vm_ioctl(struct file *filp,
                        goto create_irqchip_unlock;
                }
 
+               kvm->arch.irqchip_mode = KVM_IRQCHIP_INIT_IN_PROGRESS;
                r = kvm_setup_default_irq_routing(kvm);
                if (r) {
+                       kvm->arch.irqchip_mode = KVM_IRQCHIP_NONE;
+                       /* Pairs with smp_rmb() when reading irqchip_mode */
+                       smp_wmb();
                        mutex_lock(&kvm->slots_lock);
                        mutex_lock(&kvm->irq_lock);
                        kvm_ioapic_destroy(kvm);