Skip to content

Commit 73e2f19

Browse files
dtorawilliam
authored andcommitted
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: Alex Williamson <[email protected]> Reviewed-by: Alex Williamson <[email protected]> Signed-off-by: Dmitry Torokhov <[email protected]> Reviewed-by: Kevin Tian <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alex Williamson <[email protected]>
1 parent 9e0f4f2 commit 73e2f19

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

virt/kvm/vfio.c

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,6 @@ static void kvm_vfio_update_coherency(struct kvm_device *dev)
123123
bool noncoherent = false;
124124
struct kvm_vfio_file *kvf;
125125

126-
mutex_lock(&kv->lock);
127-
128126
list_for_each_entry(kvf, &kv->file_list, node) {
129127
if (!kvm_vfio_file_enforced_coherent(kvf->file)) {
130128
noncoherent = true;
@@ -140,16 +138,14 @@ static void kvm_vfio_update_coherency(struct kvm_device *dev)
140138
else
141139
kvm_arch_unregister_noncoherent_dma(dev->kvm);
142140
}
143-
144-
mutex_unlock(&kv->lock);
145141
}
146142

147143
static int kvm_vfio_file_add(struct kvm_device *dev, unsigned int fd)
148144
{
149145
struct kvm_vfio *kv = dev->private;
150146
struct kvm_vfio_file *kvf;
151147
struct file *filp;
152-
int ret;
148+
int ret = 0;
153149

154150
filp = fget(fd);
155151
if (!filp)
@@ -158,38 +154,34 @@ static int kvm_vfio_file_add(struct kvm_device *dev, unsigned int fd)
158154
/* Ensure the FD is a vfio FD. */
159155
if (!kvm_vfio_file_is_valid(filp)) {
160156
ret = -EINVAL;
161-
goto err_fput;
157+
goto out_fput;
162158
}
163159

164160
mutex_lock(&kv->lock);
165161

166162
list_for_each_entry(kvf, &kv->file_list, node) {
167163
if (kvf->file == filp) {
168164
ret = -EEXIST;
169-
goto err_unlock;
165+
goto out_unlock;
170166
}
171167
}
172168

173169
kvf = kzalloc(sizeof(*kvf), GFP_KERNEL_ACCOUNT);
174170
if (!kvf) {
175171
ret = -ENOMEM;
176-
goto err_unlock;
172+
goto out_unlock;
177173
}
178174

179-
kvf->file = filp;
175+
kvf->file = get_file(filp);
180176
list_add_tail(&kvf->node, &kv->file_list);
181177

182178
kvm_arch_start_assignment(dev->kvm);
183179
kvm_vfio_file_set_kvm(kvf->file, dev->kvm);
184-
185-
mutex_unlock(&kv->lock);
186-
187180
kvm_vfio_update_coherency(dev);
188181

189-
return 0;
190-
err_unlock:
182+
out_unlock:
191183
mutex_unlock(&kv->lock);
192-
err_fput:
184+
out_fput:
193185
fput(filp);
194186
return ret;
195187
}
@@ -225,12 +217,12 @@ static int kvm_vfio_file_del(struct kvm_device *dev, unsigned int fd)
225217
break;
226218
}
227219

220+
kvm_vfio_update_coherency(dev);
221+
228222
mutex_unlock(&kv->lock);
229223

230224
fdput(f);
231225

232-
kvm_vfio_update_coherency(dev);
233-
234226
return ret;
235227
}
236228

0 commit comments

Comments
 (0)