KVM devices were manipulating list data structures without any form of synchronization, and some implementations of the create operations also suffered from a lack of synchronization. Now when we've split the xics create operation into create and init, we can hold the kvm->lock mutex while calling the create operation and when manipulating the devices list. The error path in the generic code gets slightly ugly because we have to take the mutex again and delete the device from the list, but holding the mutex during anon_inode_getfd or releasing/locking the mutex in the common non-error path seemed wrong.
Unified · Split
| @@ -1009,9 +1009,13 @@ long kvm_arch_vm_ioctl(struct file *filp, · 1008 unmodified lines | ||
| 1009 | 1009 | |
| 1010 | 1010 | switch (ioctl) { |
| 1011 | 1011 | case KVM_CREATE_IRQCHIP: { |
| 1012 | + int ret; | |
| 1012 | 1013 | if (!vgic_present) |
| 1013 | 1014 | return -ENXIO; |
| 1014 | − return kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2); | |
| 1015 | + mutex_lock(&kvm->lock); | |
| 1016 | + ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2); | |
| 1017 | + mutex_unlock(&kvm->lock); | |
| 1018 | + return ret; | |
| 1015 | 1019 | } |
| 1016 | 1020 | case KVM_ARM_SET_DEVICE_ADDR: { |
| 1017 | 1021 | struct kvm_arm_device_addr dev_addr; |
| @@ -1329,12 +1329,10 @@ static int kvmppc_xics_create(struct kvm_device *dev, u32 type) · 1328 unmodified lines | ||
| 1329 | 1329 | xics->kvm = kvm; |
| 1330 | 1330 | |
| 1331 | 1331 | /* Already there ? */ |
| 1332 | − mutex_lock(&kvm->lock); | |
| 1333 | 1332 | if (kvm->arch.xics) |
| 1334 | 1333 | ret = -EEXIST; |
| 1335 | 1334 | else |
| 1336 | 1335 | kvm->arch.xics = xics; |
| 1337 | − mutex_unlock(&kvm->lock); | |
| 1338 | 1336 | |
| 1339 | 1337 | if (ret) { |
| 1340 | 1338 | kfree(xics); |
| @@ -1113,6 +1113,12 @@ struct kvm_device { · 1112 unmodified lines | ||
| 1113 | 1113 | /* create, destroy, and name are mandatory */ |
| 1114 | 1114 | struct kvm_device_ops { |
| 1115 | 1115 | const char *name; |
| 1116 | + | |
| 1117 | + /* | |
| 1118 | + * create is called holding kvm->lock and any operations not suitable | |
| 1119 | + * to do while holding the lock should be deferred to init (see | |
| 1120 | + * below). | |
| 1121 | + */ | |
| 1116 | 1122 | int (*create)(struct kvm_device *dev, u32 type); |
| 1117 | 1123 | |
| 1118 | 1124 | /* |
| @@ -73,12 +73,8 @@ int kvm_vgic_create(struct kvm *kvm, u32 type) · 72 unmodified lines | ||
| 73 | 73 | int i, vcpu_lock_idx = -1, ret; |
| 74 | 74 | struct kvm_vcpu *vcpu; |
| 75 | 75 | |
| 76 | − mutex_lock(&kvm->lock); | |
| 77 | − | |
| 78 | − if (irqchip_in_kernel(kvm)) { | |
| 79 | − ret = -EEXIST; | |
| 80 | − goto out; | |
| 81 | − } | |
| 76 | + if (irqchip_in_kernel(kvm)) | |
| 77 | + return -EEXIST; | |
| 82 | 78 | |
| 83 | 79 | /* |
| 84 | 80 | * This function is also called by the KVM_CREATE_IRQCHIP handler, |
| @@ -87,10 +83,8 @@ int kvm_vgic_create(struct kvm *kvm, u32 type) · 2 unmodified lines | ||
| 87 | 83 | * the proper checks already. |
| 88 | 84 | */ |
| 89 | 85 | if (type == KVM_DEV_TYPE_ARM_VGIC_V2 && |
| 90 | − !kvm_vgic_global_state.can_emulate_gicv2) { | |
| 91 | − ret = -ENODEV; | |
| 92 | − goto out; | |
| 93 | − } | |
| 86 | + !kvm_vgic_global_state.can_emulate_gicv2) | |
| 87 | + return -ENODEV; | |
| 94 | 88 | |
| 95 | 89 | /* |
| 96 | 90 | * Any time a vcpu is run, vcpu_load is called which tries to grab the |
| @@ -138,9 +132,6 @@ out_unlock: · 41 unmodified lines | ||
| 138 | 132 | vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx); |
| 139 | 133 | mutex_unlock(&vcpu->mutex); |
| 140 | 134 | } |
| 141 | − | |
| 142 | −out: | |
| 143 | − mutex_unlock(&kvm->lock); | |
| 144 | 135 | return ret; |
| 145 | 136 | } |
| 146 | 137 | |
| @@ -696,6 +696,11 @@ static void kvm_destroy_devices(struct kvm *kvm) · 695 unmodified lines | ||
| 696 | 696 | { |
| 697 | 697 | struct kvm_device *dev, *tmp; |
| 698 | 698 | |
| 699 | + /* | |
| 700 | + * We do not need to take the kvm->lock here, because nobody else | |
| 701 | + * has a reference to the struct kvm at this point and therefore | |
| 702 | + * cannot access the devices list anyhow. | |
| 703 | + */ | |
| 699 | 704 | list_for_each_entry_safe(dev, tmp, &kvm->devices, vm_node) { |
| 700 | 705 | list_del(&dev->vm_node); |
| 701 | 706 | dev->ops->destroy(dev); |
| @@ -2832,11 +2837,15 @@ static int kvm_ioctl_create_device(struct kvm *kvm, · 2130 unmodified lines | ||
| 2832 | 2837 | dev->ops = ops; |
| 2833 | 2838 | dev->kvm = kvm; |
| 2834 | 2839 | |
| 2840 | + mutex_lock(&kvm->lock); | |
| 2835 | 2841 | ret = ops->create(dev, cd->type); |
| 2836 | 2842 | if (ret < 0) { |
| 2843 | + mutex_unlock(&kvm->lock); | |
| 2837 | 2844 | kfree(dev); |
| 2838 | 2845 | return ret; |
| 2839 | 2846 | } |
| 2847 | + list_add(&dev->vm_node, &kvm->devices); | |
| 2848 | + mutex_unlock(&kvm->lock); | |
| 2840 | 2849 | |
| 2841 | 2850 | if (ops->init) |
| 2842 | 2851 | ops->init(dev); |
| @@ -2844,10 +2853,12 @@ static int kvm_ioctl_create_device(struct kvm *kvm, · 1 unmodified line | ||
| 2844 | 2853 | ret = anon_inode_getfd(ops->name, &kvm_device_fops, dev, O_RDWR | O_CLOEXEC); |
| 2845 | 2854 | if (ret < 0) { |
| 2846 | 2855 | ops->destroy(dev); |
| 2856 | + mutex_lock(&kvm->lock); | |
| 2857 | + list_del(&dev->vm_node); | |
| 2858 | + mutex_unlock(&kvm->lock); | |
| 2847 | 2859 | return ret; |
| 2848 | 2860 | } |
| 2849 | 2861 | |
| 2850 | − list_add(&dev->vm_node, &kvm->devices); | |
| 2851 | 2862 | kvm_get_kvm(kvm); |
| 2852 | 2863 | cd->fd = ret; |
| 2853 | 2864 | return 0; |