hc
2024-05-14 bedbef8ad3e75a304af6361af235302bcc61d06b
kernel/drivers/vhost/test.c
....@@ -1,7 +1,6 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /* Copyright (C) 2009 Red Hat, Inc.
23 * Author: Michael S. Tsirkin <mst@redhat.com>
3
- *
4
- * This work is licensed under the terms of the GNU GPL, version 2.
54 *
65 * test virtio server in host kernel.
76 */
....@@ -50,7 +49,7 @@
5049 void *private;
5150
5251 mutex_lock(&vq->mutex);
53
- private = vq->private_data;
52
+ private = vhost_vq_get_backend(vq);
5453 if (!private) {
5554 mutex_unlock(&vq->mutex);
5655 return;
....@@ -121,7 +120,7 @@
121120 vqs[VHOST_TEST_VQ] = &n->vqs[VHOST_TEST_VQ];
122121 n->vqs[VHOST_TEST_VQ].handle_kick = handle_vq_kick;
123122 vhost_dev_init(dev, vqs, VHOST_TEST_VQ_MAX, UIO_MAXIOV,
124
- VHOST_TEST_PKT_WEIGHT, VHOST_TEST_WEIGHT);
123
+ VHOST_TEST_PKT_WEIGHT, VHOST_TEST_WEIGHT, true, NULL);
125124
126125 f->private_data = n;
127126
....@@ -134,8 +133,8 @@
134133 void *private;
135134
136135 mutex_lock(&vq->mutex);
137
- private = vq->private_data;
138
- vq->private_data = NULL;
136
+ private = vhost_vq_get_backend(vq);
137
+ vhost_vq_set_backend(vq, NULL);
139138 mutex_unlock(&vq->mutex);
140139 return private;
141140 }
....@@ -199,8 +198,8 @@
199198 priv = test ? n : NULL;
200199
201200 /* start polling new socket */
202
- oldpriv = vq->private_data;
203
- vq->private_data = priv;
201
+ oldpriv = vhost_vq_get_backend(vq);
202
+ vhost_vq_set_backend(vq, priv);
204203
205204 r = vhost_vq_init_access(&n->vqs[index]);
206205
....@@ -226,7 +225,7 @@
226225 {
227226 void *priv = NULL;
228227 long err;
229
- struct vhost_umem *umem;
228
+ struct vhost_iotlb *umem;
230229
231230 mutex_lock(&n->dev.mutex);
232231 err = vhost_dev_check_owner(&n->dev);
....@@ -264,9 +263,62 @@
264263 return 0;
265264 }
266265
266
+static long vhost_test_set_backend(struct vhost_test *n, unsigned index, int fd)
267
+{
268
+ static void *backend;
269
+
270
+ const bool enable = fd != -1;
271
+ struct vhost_virtqueue *vq;
272
+ int r;
273
+
274
+ mutex_lock(&n->dev.mutex);
275
+ r = vhost_dev_check_owner(&n->dev);
276
+ if (r)
277
+ goto err;
278
+
279
+ if (index >= VHOST_TEST_VQ_MAX) {
280
+ r = -ENOBUFS;
281
+ goto err;
282
+ }
283
+ vq = &n->vqs[index];
284
+ mutex_lock(&vq->mutex);
285
+
286
+ /* Verify that ring has been setup correctly. */
287
+ if (!vhost_vq_access_ok(vq)) {
288
+ r = -EFAULT;
289
+ goto err_vq;
290
+ }
291
+ if (!enable) {
292
+ vhost_poll_stop(&vq->poll);
293
+ backend = vhost_vq_get_backend(vq);
294
+ vhost_vq_set_backend(vq, NULL);
295
+ } else {
296
+ vhost_vq_set_backend(vq, backend);
297
+ r = vhost_vq_init_access(vq);
298
+ if (r == 0)
299
+ r = vhost_poll_start(&vq->poll, vq->kick);
300
+ }
301
+
302
+ mutex_unlock(&vq->mutex);
303
+
304
+ if (enable) {
305
+ vhost_test_flush_vq(n, index);
306
+ }
307
+
308
+ mutex_unlock(&n->dev.mutex);
309
+ return 0;
310
+
311
+err_vq:
312
+ mutex_unlock(&vq->mutex);
313
+err:
314
+ mutex_unlock(&n->dev.mutex);
315
+ return r;
316
+}
317
+
267318 static long vhost_test_ioctl(struct file *f, unsigned int ioctl,
268319 unsigned long arg)
269320 {
321
+ struct vhost_vring_file backend;
270322 struct vhost_test *n = f->private_data;
271323 void __user *argp = (void __user *)arg;
272324 u64 __user *featurep = argp;
....@@ -278,6 +330,10 @@
278330 if (copy_from_user(&test, argp, sizeof test))
279331 return -EFAULT;
280332 return vhost_test_run(n, test);
333
+ case VHOST_TEST_SET_BACKEND:
334
+ if (copy_from_user(&backend, argp, sizeof backend))
335
+ return -EFAULT;
336
+ return vhost_test_set_backend(n, backend.index, backend.fd);
281337 case VHOST_GET_FEATURES:
282338 features = VHOST_FEATURES;
283339 if (copy_to_user(featurep, &features, sizeof features))
....@@ -305,21 +361,11 @@
305361 }
306362 }
307363
308
-#ifdef CONFIG_COMPAT
309
-static long vhost_test_compat_ioctl(struct file *f, unsigned int ioctl,
310
- unsigned long arg)
311
-{
312
- return vhost_test_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
313
-}
314
-#endif
315
-
316364 static const struct file_operations vhost_test_fops = {
317365 .owner = THIS_MODULE,
318366 .release = vhost_test_release,
319367 .unlocked_ioctl = vhost_test_ioctl,
320
-#ifdef CONFIG_COMPAT
321
- .compat_ioctl = vhost_test_compat_ioctl,
322
-#endif
368
+ .compat_ioctl = compat_ptr_ioctl,
323369 .open = vhost_test_open,
324370 .llseek = noop_llseek,
325371 };