Commit 73e2f19d authored by Dmitry Torokhov's avatar Dmitry Torokhov Committed by Alex Williamson

kvm/vfio: avoid bouncing the mutex when adding and deleting groups

Stop taking kv->lock mutex in kvm_vfio_update_coherency() and instead
call it with this mutex held: the callers of the function usually
already have it taken (and released) before calling
kvm_vfio_update_coherency(). This avoid bouncing the lock up and down.

The exception is kvm_vfio_release() where we do not take the lock, but
it is being executed when the very last reference to kvm_device is being
dropped, so there are no concerns about concurrency.
Suggested-by: default avatarAlex Williamson <alex.williamson@redhat.com>
Reviewed-by: default avatarAlex Williamson <alex.williamson@redhat.com>
Signed-off-by: default avatarDmitry Torokhov <dmitry.torokhov@gmail.com>
Reviewed-by: default avatarKevin Tian <kevin.tian@intel.com>
Link: https://lore.kernel.org/r/20230714224538.404793-2-dmitry.torokhov@gmail.comSigned-off-by: default avatarAlex Williamson <alex.williamson@redhat.com>
parent 9e0f4f29
...@@ -123,8 +123,6 @@ static void kvm_vfio_update_coherency(struct kvm_device *dev) ...@@ -123,8 +123,6 @@ static void kvm_vfio_update_coherency(struct kvm_device *dev)
bool noncoherent = false; bool noncoherent = false;
struct kvm_vfio_file *kvf; struct kvm_vfio_file *kvf;
mutex_lock(&kv->lock);
list_for_each_entry(kvf, &kv->file_list, node) { list_for_each_entry(kvf, &kv->file_list, node) {
if (!kvm_vfio_file_enforced_coherent(kvf->file)) { if (!kvm_vfio_file_enforced_coherent(kvf->file)) {
noncoherent = true; noncoherent = true;
...@@ -140,8 +138,6 @@ static void kvm_vfio_update_coherency(struct kvm_device *dev) ...@@ -140,8 +138,6 @@ static void kvm_vfio_update_coherency(struct kvm_device *dev)
else else
kvm_arch_unregister_noncoherent_dma(dev->kvm); kvm_arch_unregister_noncoherent_dma(dev->kvm);
} }
mutex_unlock(&kv->lock);
} }
static int kvm_vfio_file_add(struct kvm_device *dev, unsigned int fd) static int kvm_vfio_file_add(struct kvm_device *dev, unsigned int fd)
...@@ -149,7 +145,7 @@ static int kvm_vfio_file_add(struct kvm_device *dev, unsigned int fd) ...@@ -149,7 +145,7 @@ static int kvm_vfio_file_add(struct kvm_device *dev, unsigned int fd)
struct kvm_vfio *kv = dev->private; struct kvm_vfio *kv = dev->private;
struct kvm_vfio_file *kvf; struct kvm_vfio_file *kvf;
struct file *filp; struct file *filp;
int ret; int ret = 0;
filp = fget(fd); filp = fget(fd);
if (!filp) if (!filp)
...@@ -158,7 +154,7 @@ static int kvm_vfio_file_add(struct kvm_device *dev, unsigned int fd) ...@@ -158,7 +154,7 @@ static int kvm_vfio_file_add(struct kvm_device *dev, unsigned int fd)
/* Ensure the FD is a vfio FD. */ /* Ensure the FD is a vfio FD. */
if (!kvm_vfio_file_is_valid(filp)) { if (!kvm_vfio_file_is_valid(filp)) {
ret = -EINVAL; ret = -EINVAL;
goto err_fput; goto out_fput;
} }
mutex_lock(&kv->lock); mutex_lock(&kv->lock);
...@@ -166,30 +162,26 @@ static int kvm_vfio_file_add(struct kvm_device *dev, unsigned int fd) ...@@ -166,30 +162,26 @@ static int kvm_vfio_file_add(struct kvm_device *dev, unsigned int fd)
list_for_each_entry(kvf, &kv->file_list, node) { list_for_each_entry(kvf, &kv->file_list, node) {
if (kvf->file == filp) { if (kvf->file == filp) {
ret = -EEXIST; ret = -EEXIST;
goto err_unlock; goto out_unlock;
} }
} }
kvf = kzalloc(sizeof(*kvf), GFP_KERNEL_ACCOUNT); kvf = kzalloc(sizeof(*kvf), GFP_KERNEL_ACCOUNT);
if (!kvf) { if (!kvf) {
ret = -ENOMEM; ret = -ENOMEM;
goto err_unlock; goto out_unlock;
} }
kvf->file = filp; kvf->file = get_file(filp);
list_add_tail(&kvf->node, &kv->file_list); list_add_tail(&kvf->node, &kv->file_list);
kvm_arch_start_assignment(dev->kvm); kvm_arch_start_assignment(dev->kvm);
kvm_vfio_file_set_kvm(kvf->file, dev->kvm); kvm_vfio_file_set_kvm(kvf->file, dev->kvm);
mutex_unlock(&kv->lock);
kvm_vfio_update_coherency(dev); kvm_vfio_update_coherency(dev);
return 0; out_unlock:
err_unlock:
mutex_unlock(&kv->lock); mutex_unlock(&kv->lock);
err_fput: out_fput:
fput(filp); fput(filp);
return ret; return ret;
} }
...@@ -225,12 +217,12 @@ static int kvm_vfio_file_del(struct kvm_device *dev, unsigned int fd) ...@@ -225,12 +217,12 @@ static int kvm_vfio_file_del(struct kvm_device *dev, unsigned int fd)
break; break;
} }
kvm_vfio_update_coherency(dev);
mutex_unlock(&kv->lock); mutex_unlock(&kv->lock);
fdput(f); fdput(f);
kvm_vfio_update_coherency(dev);
return ret; return ret;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment