KVM: mark kvm->busses as rcu protected
authorChristian Borntraeger <borntraeger@de.ibm.com>
Fri, 7 Jul 2017 08:51:38 +0000 (10:51 +0200)
committerChristian Borntraeger <borntraeger@de.ibm.com>
Fri, 7 Jul 2017 13:24:16 +0000 (15:24 +0200)
mark kvm->busses as rcu protected and use the correct access
function everywhere.

found by sparse
virt/kvm/kvm_main.c:3490:15: error: incompatible types in comparison expression (different address spaces)
virt/kvm/kvm_main.c:3509:15: error: incompatible types in comparison expression (different address spaces)
virt/kvm/kvm_main.c:3561:15: error: incompatible types in comparison expression (different address spaces)
virt/kvm/kvm_main.c:3644:15: error: incompatible types in comparison expression (different address spaces)

Signed-off-by: Christian Borntraeger <borntraeger@de.ibm.com>
include/linux/kvm_host.h
virt/kvm/eventfd.c
virt/kvm/kvm_main.c

index bcd37b855c66763995d85e5f9ca80f48b3b1a8e4..6a164f9eb02c159bb03558bf9bbf5293406f83ad 100644 (file)
@@ -404,7 +404,7 @@ struct kvm {
        int last_boosted_vcpu;
        struct list_head vm_list;
        struct mutex lock;
-       struct kvm_io_bus *buses[KVM_NR_BUSES];
+       struct kvm_io_bus __rcu *buses[KVM_NR_BUSES];
 #ifdef CONFIG_HAVE_KVM_EVENTFD
        struct {
                spinlock_t        lock;
@@ -473,6 +473,12 @@ struct kvm {
 #define vcpu_err(vcpu, fmt, ...)                                       \
        kvm_err("vcpu%i " fmt, (vcpu)->vcpu_id, ## __VA_ARGS__)
 
+static inline struct kvm_io_bus *kvm_get_bus(struct kvm *kvm, enum kvm_bus idx)
+{
+       return srcu_dereference_check(kvm->buses[idx], &kvm->srcu,
+                                     lockdep_is_held(&kvm->slots_lock));
+}
+
 static inline struct kvm_vcpu *kvm_get_vcpu(struct kvm *kvm, int i)
 {
        /* Pairs with smp_wmb() in kvm_vm_ioctl_create_vcpu, in case
index a8d540398bbd0350b8b969820d7a11d528cf672d..d016aadd5fbb633d60189502e42e156c3ff6ed64 100644 (file)
@@ -825,7 +825,7 @@ static int kvm_assign_ioeventfd_idx(struct kvm *kvm,
        if (ret < 0)
                goto unlock_fail;
 
-       kvm->buses[bus_idx]->ioeventfd_count++;
+       kvm_get_bus(kvm, bus_idx)->ioeventfd_count++;
        list_add_tail(&p->list, &kvm->ioeventfds);
 
        mutex_unlock(&kvm->slots_lock);
@@ -848,6 +848,7 @@ kvm_deassign_ioeventfd_idx(struct kvm *kvm, enum kvm_bus bus_idx,
 {
        struct _ioeventfd        *p, *tmp;
        struct eventfd_ctx       *eventfd;
+       struct kvm_io_bus        *bus;
        int                       ret = -ENOENT;
 
        eventfd = eventfd_ctx_fdget(args->fd);
@@ -870,8 +871,9 @@ kvm_deassign_ioeventfd_idx(struct kvm *kvm, enum kvm_bus bus_idx,
                        continue;
 
                kvm_io_bus_unregister_dev(kvm, bus_idx, &p->dev);
-               if (kvm->buses[bus_idx])
-                       kvm->buses[bus_idx]->ioeventfd_count--;
+               bus = kvm_get_bus(kvm, bus_idx);
+               if (bus)
+                       bus->ioeventfd_count--;
                ioeventfd_release(p);
                ret = 0;
                break;
index fc2d58312fd521f6bbcd49e2dbe86569d1569615..d76e822f89291a00002cd2f5ff83afe6777584a5 100644 (file)
@@ -679,8 +679,8 @@ static struct kvm *kvm_create_vm(unsigned long type)
        if (init_srcu_struct(&kvm->irq_srcu))
                goto out_err_no_irq_srcu;
        for (i = 0; i < KVM_NR_BUSES; i++) {
-               kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
-                                       GFP_KERNEL);
+               rcu_assign_pointer(kvm->buses[i],
+                       kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL));
                if (!kvm->buses[i])
                        goto out_err;
        }
@@ -705,7 +705,7 @@ out_err_no_srcu:
        hardware_disable_all();
 out_err_no_disable:
        for (i = 0; i < KVM_NR_BUSES; i++)
-               kfree(kvm->buses[i]);
+               kfree(rcu_access_pointer(kvm->buses[i]));
        for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++)
                kvm_free_memslots(kvm, kvm->memslots[i]);
        kvm_arch_free_vm(kvm);
@@ -740,8 +740,11 @@ static void kvm_destroy_vm(struct kvm *kvm)
        spin_unlock(&kvm_lock);
        kvm_free_irq_routing(kvm);
        for (i = 0; i < KVM_NR_BUSES; i++) {
-               if (kvm->buses[i])
-                       kvm_io_bus_destroy(kvm->buses[i]);
+               struct kvm_io_bus *bus;
+
+               bus = rcu_dereference_protected(kvm->buses[i], 1);
+               if (bus)
+                       kvm_io_bus_destroy(bus);
                kvm->buses[i] = NULL;
        }
        kvm_coalesced_mmio_free(kvm);
@@ -3570,7 +3573,7 @@ int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
 {
        struct kvm_io_bus *new_bus, *bus;
 
-       bus = kvm->buses[bus_idx];
+       bus = kvm_get_bus(kvm, bus_idx);
        if (!bus)
                return -ENOMEM;
 
@@ -3599,7 +3602,7 @@ void kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
        int i;
        struct kvm_io_bus *new_bus, *bus;
 
-       bus = kvm->buses[bus_idx];
+       bus = kvm_get_bus(kvm, bus_idx);
        if (!bus)
                return;