blob: b14e62f110752e724c673f8266849aea6a9e646f [file] [log] [blame]
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001/* Copyright (C) 2009 Red Hat, Inc.
2 * Copyright (C) 2006 Rusty Russell IBM Corporation
3 *
4 * Author: Michael S. Tsirkin <mst@redhat.com>
5 *
6 * Inspiration, some code, and most witty comments come from
Rob Landley61516582011-05-06 09:27:36 -07007 * Documentation/virtual/lguest/lguest.c, by Rusty Russell
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00008 *
9 * This work is licensed under the terms of the GNU GPL, version 2.
10 *
11 * Generic code for virtio server in host kernel.
12 */
13
14#include <linux/eventfd.h>
15#include <linux/vhost.h>
Asias He35596b22013-08-19 09:23:19 +080016#include <linux/uio.h>
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000017#include <linux/mm.h>
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +020018#include <linux/mmu_context.h>
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000019#include <linux/miscdevice.h>
20#include <linux/mutex.h>
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000021#include <linux/poll.h>
22#include <linux/file.h>
23#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Igor Mammedov4de72552015-07-01 11:07:09 +020025#include <linux/vmalloc.h>
Tejun Heoc23f34452010-06-02 20:40:00 +020026#include <linux/kthread.h>
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +030027#include <linux/cgroup.h>
Asias He6ac1afb2013-05-06 16:38:21 +080028#include <linux/module.h>
Igor Mammedovbcfeaca2015-06-16 18:33:35 +020029#include <linux/sort.h>
Jason Wanga9709d62016-06-23 02:04:31 -040030#include <linux/interval_tree_generic.h>
Jason Wang242e6f522018-10-30 14:10:49 +080031#include <linux/nospec.h>
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000032
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000033#include "vhost.h"
34
Igor Mammedovc9ce42f2015-07-02 15:08:11 +020035static ushort max_mem_regions = 64;
36module_param(max_mem_regions, ushort, 0444);
37MODULE_PARM_DESC(max_mem_regions,
38 "Maximum number of memory regions in memory map. (default: 64)");
Jason Wang6b1e6cc2016-06-23 02:04:32 -040039static int max_iotlb_entries = 2048;
40module_param(max_iotlb_entries, int, 0444);
41MODULE_PARM_DESC(max_iotlb_entries,
42 "Maximum number of iotlb entries. (default: 2048)");
Igor Mammedovc9ce42f2015-07-02 15:08:11 +020043
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000044enum {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000045 VHOST_MEMORY_F_LOG = 0x1,
46};
47
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +030048#define vhost_used_event(vq) ((__virtio16 __user *)&vq->avail->ring[vq->num])
49#define vhost_avail_event(vq) ((__virtio16 __user *)&vq->used->ring[vq->num])
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +030050
Jason Wanga9709d62016-06-23 02:04:31 -040051INTERVAL_TREE_DEFINE(struct vhost_umem_node,
52 rb, __u64, __subtree_last,
53 START, LAST, , vhost_umem_interval_tree);
54
Greg Kurz2751c982015-04-24 14:27:24 +020055#ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
Greg Kurzc5072032016-02-16 15:59:34 +010056static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
Greg Kurz2751c982015-04-24 14:27:24 +020057{
58 vq->user_be = !virtio_legacy_is_little_endian();
59}
60
Greg Kurzc5072032016-02-16 15:59:34 +010061static void vhost_enable_cross_endian_big(struct vhost_virtqueue *vq)
62{
63 vq->user_be = true;
64}
65
66static void vhost_enable_cross_endian_little(struct vhost_virtqueue *vq)
67{
68 vq->user_be = false;
69}
70
Greg Kurz2751c982015-04-24 14:27:24 +020071static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
72{
73 struct vhost_vring_state s;
74
75 if (vq->private_data)
76 return -EBUSY;
77
78 if (copy_from_user(&s, argp, sizeof(s)))
79 return -EFAULT;
80
81 if (s.num != VHOST_VRING_LITTLE_ENDIAN &&
82 s.num != VHOST_VRING_BIG_ENDIAN)
83 return -EINVAL;
84
Greg Kurzc5072032016-02-16 15:59:34 +010085 if (s.num == VHOST_VRING_BIG_ENDIAN)
86 vhost_enable_cross_endian_big(vq);
87 else
88 vhost_enable_cross_endian_little(vq);
Greg Kurz2751c982015-04-24 14:27:24 +020089
90 return 0;
91}
92
93static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
94 int __user *argp)
95{
96 struct vhost_vring_state s = {
97 .index = idx,
98 .num = vq->user_be
99 };
100
101 if (copy_to_user(argp, &s, sizeof(s)))
102 return -EFAULT;
103
104 return 0;
105}
106
107static void vhost_init_is_le(struct vhost_virtqueue *vq)
108{
109 /* Note for legacy virtio: user_be is initialized at reset time
110 * according to the host endianness. If userspace does not set an
111 * explicit endianness, the default behavior is native endian, as
112 * expected by legacy virtio.
113 */
114 vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) || !vq->user_be;
115}
116#else
Greg Kurzc5072032016-02-16 15:59:34 +0100117static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
Greg Kurz2751c982015-04-24 14:27:24 +0200118{
119}
120
121static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
122{
123 return -ENOIOCTLCMD;
124}
125
126static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
127 int __user *argp)
128{
129 return -ENOIOCTLCMD;
130}
131
132static void vhost_init_is_le(struct vhost_virtqueue *vq)
133{
Halil Pasic1594edd2017-01-30 11:09:36 +0100134 vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1)
135 || virtio_legacy_is_little_endian();
Greg Kurz2751c982015-04-24 14:27:24 +0200136}
137#endif /* CONFIG_VHOST_CROSS_ENDIAN_LEGACY */
138
Greg Kurzc5072032016-02-16 15:59:34 +0100139static void vhost_reset_is_le(struct vhost_virtqueue *vq)
140{
Halil Pasic1594edd2017-01-30 11:09:36 +0100141 vhost_init_is_le(vq);
Greg Kurzc5072032016-02-16 15:59:34 +0100142}
143
Jason Wang7235acd2016-04-25 22:14:32 -0400144struct vhost_flush_struct {
145 struct vhost_work work;
146 struct completion wait_event;
147};
148
149static void vhost_flush_work(struct vhost_work *work)
150{
151 struct vhost_flush_struct *s;
152
153 s = container_of(work, struct vhost_flush_struct, work);
154 complete(&s->wait_event);
155}
156
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000157static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
158 poll_table *pt)
159{
160 struct vhost_poll *poll;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000161
Krishna Kumard47effe2011-03-01 17:06:37 +0530162 poll = container_of(pt, struct vhost_poll, table);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000163 poll->wqh = wqh;
164 add_wait_queue(wqh, &poll->wait);
165}
166
167static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync,
168 void *key)
169{
Tejun Heoc23f34452010-06-02 20:40:00 +0200170 struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
171
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000172 if (!((unsigned long)key & poll->mask))
173 return 0;
174
Tejun Heoc23f34452010-06-02 20:40:00 +0200175 vhost_poll_queue(poll);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000176 return 0;
177}
178
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000179void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000180{
Jason Wang04b96e52016-04-25 22:14:33 -0400181 clear_bit(VHOST_WORK_QUEUED, &work->flags);
Tejun Heoc23f34452010-06-02 20:40:00 +0200182 work->fn = fn;
183 init_waitqueue_head(&work->done);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000184}
Asias He6ac1afb2013-05-06 16:38:21 +0800185EXPORT_SYMBOL_GPL(vhost_work_init);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000186
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300187/* Init poll structure */
188void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
189 unsigned long mask, struct vhost_dev *dev)
190{
191 init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
192 init_poll_funcptr(&poll->table, vhost_poll_func);
193 poll->mask = mask;
194 poll->dev = dev;
Jason Wang2b8b3282013-01-28 01:05:18 +0000195 poll->wqh = NULL;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300196
197 vhost_work_init(&poll->work, fn);
198}
Asias He6ac1afb2013-05-06 16:38:21 +0800199EXPORT_SYMBOL_GPL(vhost_poll_init);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300200
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000201/* Start polling a file. We add ourselves to file's wait queue. The caller must
202 * keep a reference to a file until after vhost_poll_stop is called. */
Jason Wang2b8b3282013-01-28 01:05:18 +0000203int vhost_poll_start(struct vhost_poll *poll, struct file *file)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000204{
205 unsigned long mask;
Jason Wang2b8b3282013-01-28 01:05:18 +0000206 int ret = 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530207
Jason Wang70181d512013-04-10 20:50:48 +0000208 if (poll->wqh)
209 return 0;
210
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000211 mask = file->f_op->poll(file, &poll->table);
212 if (mask)
213 vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
Jason Wang2b8b3282013-01-28 01:05:18 +0000214 if (mask & POLLERR) {
Jason Wang827148d2018-03-27 20:50:52 +0800215 vhost_poll_stop(poll);
Jason Wang2b8b3282013-01-28 01:05:18 +0000216 ret = -EINVAL;
217 }
218
219 return ret;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000220}
Asias He6ac1afb2013-05-06 16:38:21 +0800221EXPORT_SYMBOL_GPL(vhost_poll_start);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000222
223/* Stop polling a file. After this function returns, it becomes safe to drop the
224 * file reference. You must also flush afterwards. */
225void vhost_poll_stop(struct vhost_poll *poll)
226{
Jason Wang2b8b3282013-01-28 01:05:18 +0000227 if (poll->wqh) {
228 remove_wait_queue(poll->wqh, &poll->wait);
229 poll->wqh = NULL;
230 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000231}
Asias He6ac1afb2013-05-06 16:38:21 +0800232EXPORT_SYMBOL_GPL(vhost_poll_stop);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000233
Asias He6ac1afb2013-05-06 16:38:21 +0800234void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000235{
Jason Wang7235acd2016-04-25 22:14:32 -0400236 struct vhost_flush_struct flush;
Tejun Heoc23f34452010-06-02 20:40:00 +0200237
Jason Wang7235acd2016-04-25 22:14:32 -0400238 if (dev->worker) {
239 init_completion(&flush.wait_event);
240 vhost_work_init(&flush.work, vhost_flush_work);
241
242 vhost_work_queue(dev, &flush.work);
243 wait_for_completion(&flush.wait_event);
244 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000245}
Asias He6ac1afb2013-05-06 16:38:21 +0800246EXPORT_SYMBOL_GPL(vhost_work_flush);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000247
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300248/* Flush any work that has been scheduled. When calling this, don't hold any
249 * locks that are also used by the callback. */
250void vhost_poll_flush(struct vhost_poll *poll)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000251{
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300252 vhost_work_flush(poll->dev, &poll->work);
253}
Asias He6ac1afb2013-05-06 16:38:21 +0800254EXPORT_SYMBOL_GPL(vhost_poll_flush);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300255
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000256void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300257{
Jason Wang04b96e52016-04-25 22:14:33 -0400258 if (!dev->worker)
259 return;
Tejun Heoc23f34452010-06-02 20:40:00 +0200260
Jason Wang04b96e52016-04-25 22:14:33 -0400261 if (!test_and_set_bit(VHOST_WORK_QUEUED, &work->flags)) {
262 /* We can only add the work to the list after we're
263 * sure it was not in the list.
264 */
265 smp_mb();
266 llist_add(&work->node, &dev->work_list);
Tejun Heoc23f34452010-06-02 20:40:00 +0200267 wake_up_process(dev->worker);
268 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000269}
Asias He6ac1afb2013-05-06 16:38:21 +0800270EXPORT_SYMBOL_GPL(vhost_work_queue);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000271
Jason Wang526d3e72016-03-04 06:24:51 -0500272/* A lockless hint for busy polling code to exit the loop */
273bool vhost_has_work(struct vhost_dev *dev)
274{
Jason Wang04b96e52016-04-25 22:14:33 -0400275 return !llist_empty(&dev->work_list);
Jason Wang526d3e72016-03-04 06:24:51 -0500276}
277EXPORT_SYMBOL_GPL(vhost_has_work);
278
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300279void vhost_poll_queue(struct vhost_poll *poll)
280{
281 vhost_work_queue(poll->dev, &poll->work);
282}
Asias He6ac1afb2013-05-06 16:38:21 +0800283EXPORT_SYMBOL_GPL(vhost_poll_queue);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300284
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000285static void vhost_vq_reset(struct vhost_dev *dev,
286 struct vhost_virtqueue *vq)
287{
288 vq->num = 1;
289 vq->desc = NULL;
290 vq->avail = NULL;
291 vq->used = NULL;
292 vq->last_avail_idx = 0;
293 vq->avail_idx = 0;
294 vq->last_used_idx = 0;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300295 vq->signalled_used = 0;
296 vq->signalled_used_valid = false;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000297 vq->used_flags = 0;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000298 vq->log_used = false;
299 vq->log_addr = -1ull;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000300 vq->private_data = NULL;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300301 vq->acked_features = 0;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000302 vq->log_base = NULL;
303 vq->error_ctx = NULL;
304 vq->error = NULL;
305 vq->kick = NULL;
306 vq->call_ctx = NULL;
307 vq->call = NULL;
Michael S. Tsirkin73a99f02010-02-23 11:23:45 +0200308 vq->log_ctx = NULL;
Greg Kurzc5072032016-02-16 15:59:34 +0100309 vhost_reset_is_le(vq);
310 vhost_disable_cross_endian(vq);
Jason Wang03088132016-03-04 06:24:53 -0500311 vq->busyloop_timeout = 0;
Jason Wanga9709d62016-06-23 02:04:31 -0400312 vq->umem = NULL;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400313 vq->iotlb = NULL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000314}
315
Tejun Heoc23f34452010-06-02 20:40:00 +0200316static int vhost_worker(void *data)
317{
318 struct vhost_dev *dev = data;
Jason Wang04b96e52016-04-25 22:14:33 -0400319 struct vhost_work *work, *work_next;
320 struct llist_node *node;
Jens Freimannd7ffde32012-06-26 00:59:58 +0000321 mm_segment_t oldfs = get_fs();
Tejun Heoc23f34452010-06-02 20:40:00 +0200322
Jens Freimannd7ffde32012-06-26 00:59:58 +0000323 set_fs(USER_DS);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200324 use_mm(dev->mm);
325
Tejun Heoc23f34452010-06-02 20:40:00 +0200326 for (;;) {
327 /* mb paired w/ kthread_stop */
328 set_current_state(TASK_INTERRUPTIBLE);
329
Tejun Heoc23f34452010-06-02 20:40:00 +0200330 if (kthread_should_stop()) {
Tejun Heoc23f34452010-06-02 20:40:00 +0200331 __set_current_state(TASK_RUNNING);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200332 break;
Tejun Heoc23f34452010-06-02 20:40:00 +0200333 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200334
Jason Wang04b96e52016-04-25 22:14:33 -0400335 node = llist_del_all(&dev->work_list);
336 if (!node)
337 schedule();
338
339 node = llist_reverse_order(node);
340 /* make sure flag is seen after deletion */
341 smp_wmb();
342 llist_for_each_entry_safe(work, work_next, node, node) {
343 clear_bit(VHOST_WORK_QUEUED, &work->flags);
Tejun Heoc23f34452010-06-02 20:40:00 +0200344 __set_current_state(TASK_RUNNING);
345 work->fn(work);
Nadav Har'Eld550dda2012-02-27 15:07:29 +0200346 if (need_resched())
347 schedule();
Jason Wang04b96e52016-04-25 22:14:33 -0400348 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200349 }
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200350 unuse_mm(dev->mm);
Jens Freimannd7ffde32012-06-26 00:59:58 +0000351 set_fs(oldfs);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200352 return 0;
Tejun Heoc23f34452010-06-02 20:40:00 +0200353}
354
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000355static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
356{
357 kfree(vq->indirect);
358 vq->indirect = NULL;
359 kfree(vq->log);
360 vq->log = NULL;
361 kfree(vq->heads);
362 vq->heads = NULL;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000363}
364
Jason Wange0e9b402010-09-14 23:53:05 +0800365/* Helper to allocate iovec buffers for all vqs. */
366static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
367{
Asias He6d5e6aa2013-05-06 16:38:23 +0800368 struct vhost_virtqueue *vq;
Jason Wange0e9b402010-09-14 23:53:05 +0800369 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530370
Jason Wange0e9b402010-09-14 23:53:05 +0800371 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800372 vq = dev->vqs[i];
373 vq->indirect = kmalloc(sizeof *vq->indirect * UIO_MAXIOV,
374 GFP_KERNEL);
375 vq->log = kmalloc(sizeof *vq->log * UIO_MAXIOV, GFP_KERNEL);
376 vq->heads = kmalloc(sizeof *vq->heads * UIO_MAXIOV, GFP_KERNEL);
377 if (!vq->indirect || !vq->log || !vq->heads)
Jason Wange0e9b402010-09-14 23:53:05 +0800378 goto err_nomem;
379 }
380 return 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530381
Jason Wange0e9b402010-09-14 23:53:05 +0800382err_nomem:
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000383 for (; i >= 0; --i)
Asias He3ab2e422013-04-27 11:16:48 +0800384 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800385 return -ENOMEM;
386}
387
388static void vhost_dev_free_iovecs(struct vhost_dev *dev)
389{
390 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530391
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000392 for (i = 0; i < dev->nvqs; ++i)
Asias He3ab2e422013-04-27 11:16:48 +0800393 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800394}
395
Jason Wang66c8d9d2019-08-17 00:00:44 +0100396bool vhost_exceeds_weight(struct vhost_virtqueue *vq,
397 int pkts, int total_len)
398{
399 struct vhost_dev *dev = vq->dev;
400
401 if ((dev->byte_weight && total_len >= dev->byte_weight) ||
402 pkts >= dev->weight) {
403 vhost_poll_queue(&vq->poll);
404 return true;
405 }
406
407 return false;
408}
409EXPORT_SYMBOL_GPL(vhost_exceeds_weight);
410
Zhi Yong Wu59566b6e2013-12-07 04:13:03 +0800411void vhost_dev_init(struct vhost_dev *dev,
Jason Wang66c8d9d2019-08-17 00:00:44 +0100412 struct vhost_virtqueue **vqs, int nvqs,
413 int weight, int byte_weight)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000414{
Asias He6d5e6aa2013-05-06 16:38:23 +0800415 struct vhost_virtqueue *vq;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000416 int i;
Tejun Heoc23f34452010-06-02 20:40:00 +0200417
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000418 dev->vqs = vqs;
419 dev->nvqs = nvqs;
420 mutex_init(&dev->mutex);
421 dev->log_ctx = NULL;
422 dev->log_file = NULL;
Jason Wanga9709d62016-06-23 02:04:31 -0400423 dev->umem = NULL;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400424 dev->iotlb = NULL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000425 dev->mm = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200426 dev->worker = NULL;
Jason Wang66c8d9d2019-08-17 00:00:44 +0100427 dev->weight = weight;
428 dev->byte_weight = byte_weight;
Jason Wang04b96e52016-04-25 22:14:33 -0400429 init_llist_head(&dev->work_list);
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400430 init_waitqueue_head(&dev->wait);
431 INIT_LIST_HEAD(&dev->read_list);
432 INIT_LIST_HEAD(&dev->pending_list);
433 spin_lock_init(&dev->iotlb_lock);
Jason Wang04b96e52016-04-25 22:14:33 -0400434
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000435
436 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800437 vq = dev->vqs[i];
438 vq->log = NULL;
439 vq->indirect = NULL;
440 vq->heads = NULL;
441 vq->dev = dev;
442 mutex_init(&vq->mutex);
443 vhost_vq_reset(dev, vq);
444 if (vq->handle_kick)
445 vhost_poll_init(&vq->poll, vq->handle_kick,
446 POLLIN, dev);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000447 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000448}
Asias He6ac1afb2013-05-06 16:38:21 +0800449EXPORT_SYMBOL_GPL(vhost_dev_init);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000450
451/* Caller should have device mutex */
452long vhost_dev_check_owner(struct vhost_dev *dev)
453{
454 /* Are you the owner? If not, I don't think you mean to do that */
455 return dev->mm == current->mm ? 0 : -EPERM;
456}
Asias He6ac1afb2013-05-06 16:38:21 +0800457EXPORT_SYMBOL_GPL(vhost_dev_check_owner);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000458
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300459struct vhost_attach_cgroups_struct {
Krishna Kumard47effe2011-03-01 17:06:37 +0530460 struct vhost_work work;
461 struct task_struct *owner;
462 int ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300463};
464
465static void vhost_attach_cgroups_work(struct vhost_work *work)
466{
Krishna Kumard47effe2011-03-01 17:06:37 +0530467 struct vhost_attach_cgroups_struct *s;
468
469 s = container_of(work, struct vhost_attach_cgroups_struct, work);
470 s->ret = cgroup_attach_task_all(s->owner, current);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300471}
472
473static int vhost_attach_cgroups(struct vhost_dev *dev)
474{
Krishna Kumard47effe2011-03-01 17:06:37 +0530475 struct vhost_attach_cgroups_struct attach;
476
477 attach.owner = current;
478 vhost_work_init(&attach.work, vhost_attach_cgroups_work);
479 vhost_work_queue(dev, &attach.work);
480 vhost_work_flush(dev, &attach.work);
481 return attach.ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300482}
483
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000484/* Caller should have device mutex */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300485bool vhost_dev_has_owner(struct vhost_dev *dev)
486{
487 return dev->mm;
488}
Asias He6ac1afb2013-05-06 16:38:21 +0800489EXPORT_SYMBOL_GPL(vhost_dev_has_owner);
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300490
491/* Caller should have device mutex */
Asias He54db63c2013-05-06 11:15:59 +0800492long vhost_dev_set_owner(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000493{
Tejun Heoc23f34452010-06-02 20:40:00 +0200494 struct task_struct *worker;
495 int err;
Krishna Kumard47effe2011-03-01 17:06:37 +0530496
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000497 /* Is there an owner already? */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300498 if (vhost_dev_has_owner(dev)) {
Tejun Heoc23f34452010-06-02 20:40:00 +0200499 err = -EBUSY;
500 goto err_mm;
501 }
Krishna Kumard47effe2011-03-01 17:06:37 +0530502
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000503 /* No owner, become one */
504 dev->mm = get_task_mm(current);
Tejun Heoc23f34452010-06-02 20:40:00 +0200505 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
506 if (IS_ERR(worker)) {
507 err = PTR_ERR(worker);
508 goto err_worker;
509 }
510
511 dev->worker = worker;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300512 wake_up_process(worker); /* avoid contributing to loadavg */
513
514 err = vhost_attach_cgroups(dev);
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300515 if (err)
516 goto err_cgroup;
Tejun Heoc23f34452010-06-02 20:40:00 +0200517
Jason Wange0e9b402010-09-14 23:53:05 +0800518 err = vhost_dev_alloc_iovecs(dev);
519 if (err)
520 goto err_cgroup;
521
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000522 return 0;
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300523err_cgroup:
524 kthread_stop(worker);
Michael S. Tsirkin615cc222010-09-02 14:16:36 +0300525 dev->worker = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200526err_worker:
527 if (dev->mm)
528 mmput(dev->mm);
529 dev->mm = NULL;
530err_mm:
531 return err;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000532}
Asias He6ac1afb2013-05-06 16:38:21 +0800533EXPORT_SYMBOL_GPL(vhost_dev_set_owner);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000534
Jason Wanga9709d62016-06-23 02:04:31 -0400535static void *vhost_kvzalloc(unsigned long size)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000536{
Jason Wanga9709d62016-06-23 02:04:31 -0400537 void *n = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
538
539 if (!n)
540 n = vzalloc(size);
541 return n;
542}
543
544struct vhost_umem *vhost_dev_reset_owner_prepare(void)
545{
546 return vhost_kvzalloc(sizeof(struct vhost_umem));
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300547}
Asias He6ac1afb2013-05-06 16:38:21 +0800548EXPORT_SYMBOL_GPL(vhost_dev_reset_owner_prepare);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000549
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300550/* Caller should have device mutex */
Jason Wanga9709d62016-06-23 02:04:31 -0400551void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_umem *umem)
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300552{
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300553 int i;
554
Michael S. Tsirkinea5d4042011-11-27 19:05:58 +0200555 vhost_dev_cleanup(dev, true);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000556
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300557 /* Restore memory to default empty mapping. */
Jason Wanga9709d62016-06-23 02:04:31 -0400558 INIT_LIST_HEAD(&umem->umem_list);
559 dev->umem = umem;
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300560 /* We don't need VQ locks below since vhost_dev_cleanup makes sure
561 * VQs aren't running.
562 */
563 for (i = 0; i < dev->nvqs; ++i)
Jason Wanga9709d62016-06-23 02:04:31 -0400564 dev->vqs[i]->umem = umem;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000565}
Asias He6ac1afb2013-05-06 16:38:21 +0800566EXPORT_SYMBOL_GPL(vhost_dev_reset_owner);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000567
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000568void vhost_dev_stop(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000569{
570 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530571
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000572 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800573 if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) {
574 vhost_poll_stop(&dev->vqs[i]->poll);
575 vhost_poll_flush(&dev->vqs[i]->poll);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000576 }
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000577 }
578}
Asias He6ac1afb2013-05-06 16:38:21 +0800579EXPORT_SYMBOL_GPL(vhost_dev_stop);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000580
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400581static void vhost_umem_free(struct vhost_umem *umem,
582 struct vhost_umem_node *node)
583{
584 vhost_umem_interval_tree_remove(node, &umem->umem_tree);
585 list_del(&node->link);
586 kfree(node);
587 umem->numem--;
588}
589
Jason Wanga9709d62016-06-23 02:04:31 -0400590static void vhost_umem_clean(struct vhost_umem *umem)
591{
592 struct vhost_umem_node *node, *tmp;
593
594 if (!umem)
595 return;
596
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400597 list_for_each_entry_safe(node, tmp, &umem->umem_list, link)
598 vhost_umem_free(umem, node);
599
Jason Wanga9709d62016-06-23 02:04:31 -0400600 kvfree(umem);
601}
602
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400603static void vhost_clear_msg(struct vhost_dev *dev)
604{
605 struct vhost_msg_node *node, *n;
606
607 spin_lock(&dev->iotlb_lock);
608
609 list_for_each_entry_safe(node, n, &dev->read_list, node) {
610 list_del(&node->node);
611 kfree(node);
612 }
613
614 list_for_each_entry_safe(node, n, &dev->pending_list, node) {
615 list_del(&node->node);
616 kfree(node);
617 }
618
619 spin_unlock(&dev->iotlb_lock);
620}
621
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000622/* Caller should have device mutex if and only if locked is set */
623void vhost_dev_cleanup(struct vhost_dev *dev, bool locked)
624{
625 int i;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000626
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000627 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800628 if (dev->vqs[i]->error_ctx)
629 eventfd_ctx_put(dev->vqs[i]->error_ctx);
630 if (dev->vqs[i]->error)
631 fput(dev->vqs[i]->error);
632 if (dev->vqs[i]->kick)
633 fput(dev->vqs[i]->kick);
634 if (dev->vqs[i]->call_ctx)
635 eventfd_ctx_put(dev->vqs[i]->call_ctx);
636 if (dev->vqs[i]->call)
637 fput(dev->vqs[i]->call);
638 vhost_vq_reset(dev, dev->vqs[i]);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000639 }
Jason Wange0e9b402010-09-14 23:53:05 +0800640 vhost_dev_free_iovecs(dev);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000641 if (dev->log_ctx)
642 eventfd_ctx_put(dev->log_ctx);
643 dev->log_ctx = NULL;
644 if (dev->log_file)
645 fput(dev->log_file);
646 dev->log_file = NULL;
647 /* No one will access memory at this point */
Jason Wanga9709d62016-06-23 02:04:31 -0400648 vhost_umem_clean(dev->umem);
649 dev->umem = NULL;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400650 vhost_umem_clean(dev->iotlb);
651 dev->iotlb = NULL;
652 vhost_clear_msg(dev);
653 wake_up_interruptible_poll(&dev->wait, POLLIN | POLLRDNORM);
Jason Wang04b96e52016-04-25 22:14:33 -0400654 WARN_ON(!llist_empty(&dev->work_list));
Eric Dumazet78b620c2010-08-31 02:05:57 +0000655 if (dev->worker) {
656 kthread_stop(dev->worker);
657 dev->worker = NULL;
658 }
Michael S. Tsirkin533a19b2010-10-06 15:34:38 +0200659 if (dev->mm)
660 mmput(dev->mm);
661 dev->mm = NULL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000662}
Asias He6ac1afb2013-05-06 16:38:21 +0800663EXPORT_SYMBOL_GPL(vhost_dev_cleanup);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000664
665static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
666{
667 u64 a = addr / VHOST_PAGE_SIZE / 8;
Krishna Kumard47effe2011-03-01 17:06:37 +0530668
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000669 /* Make sure 64 bit math will not overflow. */
670 if (a > ULONG_MAX - (unsigned long)log_base ||
671 a + (unsigned long)log_base > ULONG_MAX)
Dan Carpenter6d97e552010-10-11 19:24:19 +0200672 return 0;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000673
674 return access_ok(VERIFY_WRITE, log_base + a,
675 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
676}
677
Michael S. Tsirkinec33d032016-08-01 23:20:53 +0300678static bool vhost_overflow(u64 uaddr, u64 size)
679{
680 /* Make sure 64 bit math will not overflow. */
681 return uaddr > ULONG_MAX || size > ULONG_MAX || uaddr > ULONG_MAX - size;
682}
683
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000684/* Caller should have vq mutex and device mutex. */
Jason Wanga9709d62016-06-23 02:04:31 -0400685static int vq_memory_access_ok(void __user *log_base, struct vhost_umem *umem,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000686 int log_all)
687{
Jason Wanga9709d62016-06-23 02:04:31 -0400688 struct vhost_umem_node *node;
Jeff Dike179b2842010-04-07 09:59:10 -0400689
Jason Wanga9709d62016-06-23 02:04:31 -0400690 if (!umem)
Michael S. Tsirkinf8322fb2010-05-27 12:28:03 +0300691 return 0;
Jeff Dike179b2842010-04-07 09:59:10 -0400692
Jason Wanga9709d62016-06-23 02:04:31 -0400693 list_for_each_entry(node, &umem->umem_list, link) {
694 unsigned long a = node->userspace_addr;
695
Michael S. Tsirkinec33d032016-08-01 23:20:53 +0300696 if (vhost_overflow(node->userspace_addr, node->size))
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000697 return 0;
Michael S. Tsirkinec33d032016-08-01 23:20:53 +0300698
699
700 if (!access_ok(VERIFY_WRITE, (void __user *)a,
Jason Wanga9709d62016-06-23 02:04:31 -0400701 node->size))
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000702 return 0;
703 else if (log_all && !log_access_ok(log_base,
Jason Wanga9709d62016-06-23 02:04:31 -0400704 node->start,
705 node->size))
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000706 return 0;
707 }
708 return 1;
709}
710
711/* Can we switch to this memory table? */
712/* Caller should have device mutex but not vq mutex */
Jason Wanga9709d62016-06-23 02:04:31 -0400713static int memory_access_ok(struct vhost_dev *d, struct vhost_umem *umem,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000714 int log_all)
715{
716 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530717
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000718 for (i = 0; i < d->nvqs; ++i) {
719 int ok;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300720 bool log;
721
Asias He3ab2e422013-04-27 11:16:48 +0800722 mutex_lock(&d->vqs[i]->mutex);
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300723 log = log_all || vhost_has_feature(d->vqs[i], VHOST_F_LOG_ALL);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000724 /* If ring is inactive, will check when it's enabled. */
Asias He3ab2e422013-04-27 11:16:48 +0800725 if (d->vqs[i]->private_data)
Jason Wanga9709d62016-06-23 02:04:31 -0400726 ok = vq_memory_access_ok(d->vqs[i]->log_base,
727 umem, log);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000728 else
729 ok = 1;
Asias He3ab2e422013-04-27 11:16:48 +0800730 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000731 if (!ok)
732 return 0;
733 }
734 return 1;
735}
736
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400737static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
738 struct iovec iov[], int iov_size, int access);
Jason Wangbfe2bc52016-06-23 02:04:30 -0400739
740static int vhost_copy_to_user(struct vhost_virtqueue *vq, void *to,
741 const void *from, unsigned size)
742{
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400743 int ret;
Jason Wangbfe2bc52016-06-23 02:04:30 -0400744
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400745 if (!vq->iotlb)
746 return __copy_to_user(to, from, size);
747 else {
748 /* This function should be called after iotlb
749 * prefetch, which means we're sure that all vq
750 * could be access through iotlb. So -EAGAIN should
751 * not happen in this case.
752 */
753 /* TODO: more fast path */
754 struct iov_iter t;
755 ret = translate_desc(vq, (u64)(uintptr_t)to, size, vq->iotlb_iov,
756 ARRAY_SIZE(vq->iotlb_iov),
757 VHOST_ACCESS_WO);
758 if (ret < 0)
759 goto out;
760 iov_iter_init(&t, WRITE, vq->iotlb_iov, ret, size);
761 ret = copy_to_iter(from, size, &t);
762 if (ret == size)
763 ret = 0;
764 }
765out:
766 return ret;
767}
Jason Wangbfe2bc52016-06-23 02:04:30 -0400768
769static int vhost_copy_from_user(struct vhost_virtqueue *vq, void *to,
770 void *from, unsigned size)
771{
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400772 int ret;
773
774 if (!vq->iotlb)
775 return __copy_from_user(to, from, size);
776 else {
777 /* This function should be called after iotlb
778 * prefetch, which means we're sure that vq
779 * could be access through iotlb. So -EAGAIN should
780 * not happen in this case.
781 */
782 /* TODO: more fast path */
783 struct iov_iter f;
784 ret = translate_desc(vq, (u64)(uintptr_t)from, size, vq->iotlb_iov,
785 ARRAY_SIZE(vq->iotlb_iov),
786 VHOST_ACCESS_RO);
787 if (ret < 0) {
788 vq_err(vq, "IOTLB translation failure: uaddr "
789 "%p size 0x%llx\n", from,
790 (unsigned long long) size);
791 goto out;
792 }
793 iov_iter_init(&f, READ, vq->iotlb_iov, ret, size);
794 ret = copy_from_iter(to, size, &f);
795 if (ret == size)
796 ret = 0;
797 }
798
799out:
800 return ret;
801}
802
803static void __user *__vhost_get_user(struct vhost_virtqueue *vq,
804 void *addr, unsigned size)
805{
806 int ret;
807
808 /* This function should be called after iotlb
809 * prefetch, which means we're sure that vq
810 * could be access through iotlb. So -EAGAIN should
811 * not happen in this case.
812 */
813 /* TODO: more fast path */
814 ret = translate_desc(vq, (u64)(uintptr_t)addr, size, vq->iotlb_iov,
815 ARRAY_SIZE(vq->iotlb_iov),
816 VHOST_ACCESS_RO);
817 if (ret < 0) {
818 vq_err(vq, "IOTLB translation failure: uaddr "
819 "%p size 0x%llx\n", addr,
820 (unsigned long long) size);
821 return NULL;
822 }
823
824 if (ret != 1 || vq->iotlb_iov[0].iov_len != size) {
825 vq_err(vq, "Non atomic userspace memory access: uaddr "
826 "%p size 0x%llx\n", addr,
827 (unsigned long long) size);
828 return NULL;
829 }
830
831 return vq->iotlb_iov[0].iov_base;
832}
833
834#define vhost_put_user(vq, x, ptr) \
835({ \
836 int ret = -EFAULT; \
837 if (!vq->iotlb) { \
838 ret = __put_user(x, ptr); \
839 } else { \
840 __typeof__(ptr) to = \
841 (__typeof__(ptr)) __vhost_get_user(vq, ptr, sizeof(*ptr)); \
842 if (to != NULL) \
843 ret = __put_user(x, to); \
844 else \
845 ret = -EFAULT; \
846 } \
847 ret; \
848})
849
850#define vhost_get_user(vq, x, ptr) \
851({ \
852 int ret; \
853 if (!vq->iotlb) { \
854 ret = __get_user(x, ptr); \
855 } else { \
856 __typeof__(ptr) from = \
857 (__typeof__(ptr)) __vhost_get_user(vq, ptr, sizeof(*ptr)); \
858 if (from != NULL) \
859 ret = __get_user(x, from); \
860 else \
861 ret = -EFAULT; \
862 } \
863 ret; \
864})
865
866static void vhost_dev_lock_vqs(struct vhost_dev *d)
867{
868 int i = 0;
869 for (i = 0; i < d->nvqs; ++i)
Jason Wangbd3ccdc2018-01-23 17:27:25 +0800870 mutex_lock_nested(&d->vqs[i]->mutex, i);
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400871}
872
873static void vhost_dev_unlock_vqs(struct vhost_dev *d)
874{
875 int i = 0;
876 for (i = 0; i < d->nvqs; ++i)
877 mutex_unlock(&d->vqs[i]->mutex);
878}
879
880static int vhost_new_umem_range(struct vhost_umem *umem,
881 u64 start, u64 size, u64 end,
882 u64 userspace_addr, int perm)
883{
Jason Wangc5bbeda2019-04-09 12:10:25 +0800884 struct vhost_umem_node *tmp, *node;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400885
Jason Wangc5bbeda2019-04-09 12:10:25 +0800886 if (!size)
887 return -EFAULT;
888
889 node = kmalloc(sizeof(*node), GFP_ATOMIC);
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400890 if (!node)
891 return -ENOMEM;
892
893 if (umem->numem == max_iotlb_entries) {
894 tmp = list_first_entry(&umem->umem_list, typeof(*tmp), link);
895 vhost_umem_free(umem, tmp);
896 }
897
898 node->start = start;
899 node->size = size;
900 node->last = end;
901 node->userspace_addr = userspace_addr;
902 node->perm = perm;
903 INIT_LIST_HEAD(&node->link);
904 list_add_tail(&node->link, &umem->umem_list);
905 vhost_umem_interval_tree_insert(node, &umem->umem_tree);
906 umem->numem++;
907
908 return 0;
909}
910
911static void vhost_del_umem_range(struct vhost_umem *umem,
912 u64 start, u64 end)
913{
914 struct vhost_umem_node *node;
915
916 while ((node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
917 start, end)))
918 vhost_umem_free(umem, node);
919}
920
921static void vhost_iotlb_notify_vq(struct vhost_dev *d,
922 struct vhost_iotlb_msg *msg)
923{
924 struct vhost_msg_node *node, *n;
925
926 spin_lock(&d->iotlb_lock);
927
928 list_for_each_entry_safe(node, n, &d->pending_list, node) {
929 struct vhost_iotlb_msg *vq_msg = &node->msg.iotlb;
930 if (msg->iova <= vq_msg->iova &&
Jason Wanga21a39a2018-08-24 16:53:13 +0800931 msg->iova + msg->size - 1 >= vq_msg->iova &&
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400932 vq_msg->type == VHOST_IOTLB_MISS) {
933 vhost_poll_queue(&node->vq->poll);
934 list_del(&node->node);
935 kfree(node);
936 }
937 }
938
939 spin_unlock(&d->iotlb_lock);
940}
941
942static int umem_access_ok(u64 uaddr, u64 size, int access)
943{
944 unsigned long a = uaddr;
945
Michael S. Tsirkinec33d032016-08-01 23:20:53 +0300946 /* Make sure 64 bit math will not overflow. */
947 if (vhost_overflow(uaddr, size))
948 return -EFAULT;
949
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400950 if ((access & VHOST_ACCESS_RO) &&
951 !access_ok(VERIFY_READ, (void __user *)a, size))
952 return -EFAULT;
953 if ((access & VHOST_ACCESS_WO) &&
954 !access_ok(VERIFY_WRITE, (void __user *)a, size))
955 return -EFAULT;
956 return 0;
957}
958
959int vhost_process_iotlb_msg(struct vhost_dev *dev,
960 struct vhost_iotlb_msg *msg)
961{
962 int ret = 0;
963
Jason Wangf8332092018-05-22 19:58:57 +0800964 mutex_lock(&dev->mutex);
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400965 vhost_dev_lock_vqs(dev);
966 switch (msg->type) {
967 case VHOST_IOTLB_UPDATE:
968 if (!dev->iotlb) {
969 ret = -EFAULT;
970 break;
971 }
972 if (umem_access_ok(msg->uaddr, msg->size, msg->perm)) {
973 ret = -EFAULT;
974 break;
975 }
976 if (vhost_new_umem_range(dev->iotlb, msg->iova, msg->size,
977 msg->iova + msg->size - 1,
978 msg->uaddr, msg->perm)) {
979 ret = -ENOMEM;
980 break;
981 }
982 vhost_iotlb_notify_vq(dev, msg);
983 break;
984 case VHOST_IOTLB_INVALIDATE:
985 vhost_del_umem_range(dev->iotlb, msg->iova,
986 msg->iova + msg->size - 1);
987 break;
988 default:
989 ret = -EINVAL;
990 break;
991 }
992
993 vhost_dev_unlock_vqs(dev);
Jason Wangf8332092018-05-22 19:58:57 +0800994 mutex_unlock(&dev->mutex);
995
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400996 return ret;
997}
998ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
999 struct iov_iter *from)
1000{
1001 struct vhost_msg_node node;
1002 unsigned size = sizeof(struct vhost_msg);
1003 size_t ret;
1004 int err;
1005
1006 if (iov_iter_count(from) < size)
1007 return 0;
1008 ret = copy_from_iter(&node.msg, size, from);
1009 if (ret != size)
1010 goto done;
1011
1012 switch (node.msg.type) {
1013 case VHOST_IOTLB_MSG:
1014 err = vhost_process_iotlb_msg(dev, &node.msg.iotlb);
1015 if (err)
1016 ret = err;
1017 break;
1018 default:
1019 ret = -EINVAL;
1020 break;
1021 }
1022
1023done:
1024 return ret;
1025}
1026EXPORT_SYMBOL(vhost_chr_write_iter);
1027
1028unsigned int vhost_chr_poll(struct file *file, struct vhost_dev *dev,
1029 poll_table *wait)
1030{
1031 unsigned int mask = 0;
1032
1033 poll_wait(file, &dev->wait, wait);
1034
1035 if (!list_empty(&dev->read_list))
1036 mask |= POLLIN | POLLRDNORM;
1037
1038 return mask;
1039}
1040EXPORT_SYMBOL(vhost_chr_poll);
1041
1042ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to,
1043 int noblock)
1044{
1045 DEFINE_WAIT(wait);
1046 struct vhost_msg_node *node;
1047 ssize_t ret = 0;
1048 unsigned size = sizeof(struct vhost_msg);
1049
1050 if (iov_iter_count(to) < size)
1051 return 0;
1052
1053 while (1) {
1054 if (!noblock)
1055 prepare_to_wait(&dev->wait, &wait,
1056 TASK_INTERRUPTIBLE);
1057
1058 node = vhost_dequeue_msg(dev, &dev->read_list);
1059 if (node)
1060 break;
1061 if (noblock) {
1062 ret = -EAGAIN;
1063 break;
1064 }
1065 if (signal_pending(current)) {
1066 ret = -ERESTARTSYS;
1067 break;
1068 }
1069 if (!dev->iotlb) {
1070 ret = -EBADFD;
1071 break;
1072 }
1073
1074 schedule();
1075 }
1076
1077 if (!noblock)
1078 finish_wait(&dev->wait, &wait);
1079
1080 if (node) {
1081 ret = copy_to_iter(&node->msg, size, to);
1082
1083 if (ret != size || node->msg.type != VHOST_IOTLB_MISS) {
1084 kfree(node);
1085 return ret;
1086 }
1087
1088 vhost_enqueue_msg(dev, &dev->pending_list, node);
1089 }
1090
1091 return ret;
1092}
1093EXPORT_SYMBOL_GPL(vhost_chr_read_iter);
1094
1095static int vhost_iotlb_miss(struct vhost_virtqueue *vq, u64 iova, int access)
1096{
1097 struct vhost_dev *dev = vq->dev;
1098 struct vhost_msg_node *node;
1099 struct vhost_iotlb_msg *msg;
1100
1101 node = vhost_new_msg(vq, VHOST_IOTLB_MISS);
1102 if (!node)
1103 return -ENOMEM;
1104
1105 msg = &node->msg.iotlb;
1106 msg->type = VHOST_IOTLB_MISS;
1107 msg->iova = iova;
1108 msg->perm = access;
1109
1110 vhost_enqueue_msg(dev, &dev->read_list, node);
1111
1112 return 0;
Jason Wangbfe2bc52016-06-23 02:04:30 -04001113}
1114
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001115static int vq_access_ok(struct vhost_virtqueue *vq, unsigned int num,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001116 struct vring_desc __user *desc,
1117 struct vring_avail __user *avail,
1118 struct vring_used __user *used)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001119
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001120{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001121 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001122
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001123 return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
1124 access_ok(VERIFY_READ, avail,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001125 sizeof *avail + num * sizeof *avail->ring + s) &&
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001126 access_ok(VERIFY_WRITE, used,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001127 sizeof *used + num * sizeof *used->ring + s);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001128}
1129
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001130static int iotlb_access_ok(struct vhost_virtqueue *vq,
1131 int access, u64 addr, u64 len)
1132{
1133 const struct vhost_umem_node *node;
1134 struct vhost_umem *umem = vq->iotlb;
1135 u64 s = 0, size;
1136
1137 while (len > s) {
1138 node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
1139 addr,
1140 addr + len - 1);
1141 if (node == NULL || node->start > addr) {
1142 vhost_iotlb_miss(vq, addr, access);
1143 return false;
1144 } else if (!(node->perm & access)) {
1145 /* Report the possible access violation by
1146 * request another translation from userspace.
1147 */
1148 return false;
1149 }
1150
1151 size = node->size - addr + node->start;
1152 s += size;
1153 addr += size;
1154 }
1155
1156 return true;
1157}
1158
1159int vq_iotlb_prefetch(struct vhost_virtqueue *vq)
1160{
1161 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
1162 unsigned int num = vq->num;
1163
1164 if (!vq->iotlb)
1165 return 1;
1166
1167 return iotlb_access_ok(vq, VHOST_ACCESS_RO, (u64)(uintptr_t)vq->desc,
1168 num * sizeof *vq->desc) &&
1169 iotlb_access_ok(vq, VHOST_ACCESS_RO, (u64)(uintptr_t)vq->avail,
1170 sizeof *vq->avail +
1171 num * sizeof *vq->avail->ring + s) &&
1172 iotlb_access_ok(vq, VHOST_ACCESS_WO, (u64)(uintptr_t)vq->used,
1173 sizeof *vq->used +
1174 num * sizeof *vq->used->ring + s);
1175}
1176EXPORT_SYMBOL_GPL(vq_iotlb_prefetch);
1177
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001178/* Can we log writes? */
1179/* Caller should have device mutex but not vq mutex */
1180int vhost_log_access_ok(struct vhost_dev *dev)
1181{
Jason Wanga9709d62016-06-23 02:04:31 -04001182 return memory_access_ok(dev, dev->umem, 1);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001183}
Asias He6ac1afb2013-05-06 16:38:21 +08001184EXPORT_SYMBOL_GPL(vhost_log_access_ok);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001185
1186/* Verify access for write logging. */
1187/* Caller should have vq mutex and device mutex */
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001188static int vq_log_access_ok(struct vhost_virtqueue *vq,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001189 void __user *log_base)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001190{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001191 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
Arnd Bergmann28457ee2010-03-09 19:24:45 +01001192
Jason Wanga9709d62016-06-23 02:04:31 -04001193 return vq_memory_access_ok(log_base, vq->umem,
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001194 vhost_has_feature(vq, VHOST_F_LOG_ALL)) &&
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001195 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
1196 sizeof *vq->used +
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001197 vq->num * sizeof *vq->used->ring + s));
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001198}
1199
1200/* Can we start vq? */
1201/* Caller should have vq mutex and device mutex */
1202int vhost_vq_access_ok(struct vhost_virtqueue *vq)
1203{
Stefan Hajnoczi72de9892018-04-11 10:35:40 +08001204 if (!vq_log_access_ok(vq, vq->log_base))
1205 return 0;
Jason Wang992dbb12018-03-29 16:00:04 +08001206
Stefan Hajnoczi72de9892018-04-11 10:35:40 +08001207 /* Access validation occurs at prefetch time with IOTLB */
1208 if (vq->iotlb)
1209 return 1;
Jason Wang992dbb12018-03-29 16:00:04 +08001210
1211 return vq_access_ok(vq, vq->num, vq->desc, vq->avail, vq->used);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001212}
Asias He6ac1afb2013-05-06 16:38:21 +08001213EXPORT_SYMBOL_GPL(vhost_vq_access_ok);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001214
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001215static struct vhost_umem *vhost_umem_alloc(void)
1216{
1217 struct vhost_umem *umem = vhost_kvzalloc(sizeof(*umem));
1218
1219 if (!umem)
1220 return NULL;
1221
1222 umem->umem_tree = RB_ROOT;
1223 umem->numem = 0;
1224 INIT_LIST_HEAD(&umem->umem_list);
1225
1226 return umem;
1227}
1228
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001229static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
1230{
Jason Wanga9709d62016-06-23 02:04:31 -04001231 struct vhost_memory mem, *newmem;
1232 struct vhost_memory_region *region;
Jason Wanga9709d62016-06-23 02:04:31 -04001233 struct vhost_umem *newumem, *oldumem;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001234 unsigned long size = offsetof(struct vhost_memory, regions);
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001235 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +05301236
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001237 if (copy_from_user(&mem, m, size))
1238 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001239 if (mem.padding)
1240 return -EOPNOTSUPP;
Igor Mammedovc9ce42f2015-07-02 15:08:11 +02001241 if (mem.nregions > max_mem_regions)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001242 return -E2BIG;
Igor Mammedov4de72552015-07-01 11:07:09 +02001243 newmem = vhost_kvzalloc(size + mem.nregions * sizeof(*m->regions));
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001244 if (!newmem)
1245 return -ENOMEM;
1246
1247 memcpy(newmem, &mem, size);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001248 if (copy_from_user(newmem->regions, m->regions,
1249 mem.nregions * sizeof *m->regions)) {
Igor Mammedovbcfeaca2015-06-16 18:33:35 +02001250 kvfree(newmem);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001251 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001252 }
1253
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001254 newumem = vhost_umem_alloc();
Jason Wanga9709d62016-06-23 02:04:31 -04001255 if (!newumem) {
Igor Mammedov4de72552015-07-01 11:07:09 +02001256 kvfree(newmem);
Jason Wanga9709d62016-06-23 02:04:31 -04001257 return -ENOMEM;
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +09001258 }
Jason Wanga9709d62016-06-23 02:04:31 -04001259
Jason Wanga9709d62016-06-23 02:04:31 -04001260 for (region = newmem->regions;
1261 region < newmem->regions + mem.nregions;
1262 region++) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001263 if (vhost_new_umem_range(newumem,
1264 region->guest_phys_addr,
1265 region->memory_size,
1266 region->guest_phys_addr +
1267 region->memory_size - 1,
1268 region->userspace_addr,
1269 VHOST_ACCESS_RW))
Jason Wanga9709d62016-06-23 02:04:31 -04001270 goto err;
Jason Wanga9709d62016-06-23 02:04:31 -04001271 }
1272
1273 if (!memory_access_ok(d, newumem, 0))
1274 goto err;
1275
1276 oldumem = d->umem;
1277 d->umem = newumem;
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001278
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001279 /* All memory accesses are done under some VQ mutex. */
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001280 for (i = 0; i < d->nvqs; ++i) {
1281 mutex_lock(&d->vqs[i]->mutex);
Jason Wanga9709d62016-06-23 02:04:31 -04001282 d->vqs[i]->umem = newumem;
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001283 mutex_unlock(&d->vqs[i]->mutex);
1284 }
Jason Wanga9709d62016-06-23 02:04:31 -04001285
1286 kvfree(newmem);
1287 vhost_umem_clean(oldumem);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001288 return 0;
Jason Wanga9709d62016-06-23 02:04:31 -04001289
1290err:
1291 vhost_umem_clean(newumem);
1292 kvfree(newmem);
1293 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001294}
1295
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001296long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001297{
Al Virocecb46f2012-08-27 14:21:39 -04001298 struct file *eventfp, *filep = NULL;
1299 bool pollstart = false, pollstop = false;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001300 struct eventfd_ctx *ctx = NULL;
1301 u32 __user *idxp = argp;
1302 struct vhost_virtqueue *vq;
1303 struct vhost_vring_state s;
1304 struct vhost_vring_file f;
1305 struct vhost_vring_addr a;
1306 u32 idx;
1307 long r;
1308
1309 r = get_user(idx, idxp);
1310 if (r < 0)
1311 return r;
Krishna Kumar0f3d9a12010-05-25 11:10:36 +05301312 if (idx >= d->nvqs)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001313 return -ENOBUFS;
1314
Jason Wang242e6f522018-10-30 14:10:49 +08001315 idx = array_index_nospec(idx, d->nvqs);
Asias He3ab2e422013-04-27 11:16:48 +08001316 vq = d->vqs[idx];
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001317
1318 mutex_lock(&vq->mutex);
1319
1320 switch (ioctl) {
1321 case VHOST_SET_VRING_NUM:
1322 /* Resizing ring with an active backend?
1323 * You don't want to do that. */
1324 if (vq->private_data) {
1325 r = -EBUSY;
1326 break;
1327 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001328 if (copy_from_user(&s, argp, sizeof s)) {
1329 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001330 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001331 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001332 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
1333 r = -EINVAL;
1334 break;
1335 }
1336 vq->num = s.num;
1337 break;
1338 case VHOST_SET_VRING_BASE:
1339 /* Moving base with an active backend?
1340 * You don't want to do that. */
1341 if (vq->private_data) {
1342 r = -EBUSY;
1343 break;
1344 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001345 if (copy_from_user(&s, argp, sizeof s)) {
1346 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001347 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001348 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001349 if (s.num > 0xffff) {
1350 r = -EINVAL;
1351 break;
1352 }
1353 vq->last_avail_idx = s.num;
1354 /* Forget the cached index value. */
1355 vq->avail_idx = vq->last_avail_idx;
1356 break;
1357 case VHOST_GET_VRING_BASE:
1358 s.index = idx;
1359 s.num = vq->last_avail_idx;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001360 if (copy_to_user(argp, &s, sizeof s))
1361 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001362 break;
1363 case VHOST_SET_VRING_ADDR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001364 if (copy_from_user(&a, argp, sizeof a)) {
1365 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001366 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001367 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001368 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
1369 r = -EOPNOTSUPP;
1370 break;
1371 }
1372 /* For 32bit, verify that the top 32bits of the user
1373 data are set to zero. */
1374 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
1375 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
1376 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
1377 r = -EFAULT;
1378 break;
1379 }
Michael S. Tsirkin5d9a07b2014-12-21 01:00:23 +02001380
1381 /* Make sure it's safe to cast pointers to vring types. */
1382 BUILD_BUG_ON(__alignof__ *vq->avail > VRING_AVAIL_ALIGN_SIZE);
1383 BUILD_BUG_ON(__alignof__ *vq->used > VRING_USED_ALIGN_SIZE);
1384 if ((a.avail_user_addr & (VRING_AVAIL_ALIGN_SIZE - 1)) ||
1385 (a.used_user_addr & (VRING_USED_ALIGN_SIZE - 1)) ||
Michael S. Tsirkind5424832015-11-16 16:57:08 +02001386 (a.log_guest_addr & (VRING_USED_ALIGN_SIZE - 1))) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001387 r = -EINVAL;
1388 break;
1389 }
1390
1391 /* We only verify access here if backend is configured.
1392 * If it is not, we don't as size might not have been setup.
1393 * We will verify when backend is configured. */
1394 if (vq->private_data) {
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001395 if (!vq_access_ok(vq, vq->num,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001396 (void __user *)(unsigned long)a.desc_user_addr,
1397 (void __user *)(unsigned long)a.avail_user_addr,
1398 (void __user *)(unsigned long)a.used_user_addr)) {
1399 r = -EINVAL;
1400 break;
1401 }
1402
1403 /* Also validate log access for used ring if enabled. */
1404 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
1405 !log_access_ok(vq->log_base, a.log_guest_addr,
1406 sizeof *vq->used +
1407 vq->num * sizeof *vq->used->ring)) {
1408 r = -EINVAL;
1409 break;
1410 }
1411 }
1412
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001413 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
1414 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
1415 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
1416 vq->log_addr = a.log_guest_addr;
1417 vq->used = (void __user *)(unsigned long)a.used_user_addr;
1418 break;
1419 case VHOST_SET_VRING_KICK:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001420 if (copy_from_user(&f, argp, sizeof f)) {
1421 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001422 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001423 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001424 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +02001425 if (IS_ERR(eventfp)) {
1426 r = PTR_ERR(eventfp);
1427 break;
1428 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001429 if (eventfp != vq->kick) {
Al Virocecb46f2012-08-27 14:21:39 -04001430 pollstop = (filep = vq->kick) != NULL;
1431 pollstart = (vq->kick = eventfp) != NULL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001432 } else
1433 filep = eventfp;
1434 break;
1435 case VHOST_SET_VRING_CALL:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001436 if (copy_from_user(&f, argp, sizeof f)) {
1437 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001438 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001439 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001440 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +02001441 if (IS_ERR(eventfp)) {
1442 r = PTR_ERR(eventfp);
1443 break;
1444 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001445 if (eventfp != vq->call) {
1446 filep = vq->call;
1447 ctx = vq->call_ctx;
1448 vq->call = eventfp;
1449 vq->call_ctx = eventfp ?
1450 eventfd_ctx_fileget(eventfp) : NULL;
1451 } else
1452 filep = eventfp;
1453 break;
1454 case VHOST_SET_VRING_ERR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001455 if (copy_from_user(&f, argp, sizeof f)) {
1456 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001457 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001458 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001459 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +02001460 if (IS_ERR(eventfp)) {
1461 r = PTR_ERR(eventfp);
1462 break;
1463 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001464 if (eventfp != vq->error) {
1465 filep = vq->error;
1466 vq->error = eventfp;
1467 ctx = vq->error_ctx;
1468 vq->error_ctx = eventfp ?
1469 eventfd_ctx_fileget(eventfp) : NULL;
1470 } else
1471 filep = eventfp;
1472 break;
Greg Kurz2751c982015-04-24 14:27:24 +02001473 case VHOST_SET_VRING_ENDIAN:
1474 r = vhost_set_vring_endian(vq, argp);
1475 break;
1476 case VHOST_GET_VRING_ENDIAN:
1477 r = vhost_get_vring_endian(vq, idx, argp);
1478 break;
Jason Wang03088132016-03-04 06:24:53 -05001479 case VHOST_SET_VRING_BUSYLOOP_TIMEOUT:
1480 if (copy_from_user(&s, argp, sizeof(s))) {
1481 r = -EFAULT;
1482 break;
1483 }
1484 vq->busyloop_timeout = s.num;
1485 break;
1486 case VHOST_GET_VRING_BUSYLOOP_TIMEOUT:
1487 s.index = idx;
1488 s.num = vq->busyloop_timeout;
1489 if (copy_to_user(argp, &s, sizeof(s)))
1490 r = -EFAULT;
1491 break;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001492 default:
1493 r = -ENOIOCTLCMD;
1494 }
1495
1496 if (pollstop && vq->handle_kick)
1497 vhost_poll_stop(&vq->poll);
1498
1499 if (ctx)
1500 eventfd_ctx_put(ctx);
1501 if (filep)
1502 fput(filep);
1503
1504 if (pollstart && vq->handle_kick)
Jason Wang2b8b3282013-01-28 01:05:18 +00001505 r = vhost_poll_start(&vq->poll, vq->kick);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001506
1507 mutex_unlock(&vq->mutex);
1508
1509 if (pollstop && vq->handle_kick)
1510 vhost_poll_flush(&vq->poll);
1511 return r;
1512}
Asias He6ac1afb2013-05-06 16:38:21 +08001513EXPORT_SYMBOL_GPL(vhost_vring_ioctl);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001514
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001515int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled)
1516{
1517 struct vhost_umem *niotlb, *oiotlb;
1518 int i;
1519
1520 niotlb = vhost_umem_alloc();
1521 if (!niotlb)
1522 return -ENOMEM;
1523
1524 oiotlb = d->iotlb;
1525 d->iotlb = niotlb;
1526
1527 for (i = 0; i < d->nvqs; ++i) {
1528 mutex_lock(&d->vqs[i]->mutex);
1529 d->vqs[i]->iotlb = niotlb;
1530 mutex_unlock(&d->vqs[i]->mutex);
1531 }
1532
1533 vhost_umem_clean(oiotlb);
1534
1535 return 0;
1536}
1537EXPORT_SYMBOL_GPL(vhost_init_device_iotlb);
1538
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001539/* Caller must have device mutex */
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001540long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001541{
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001542 struct file *eventfp, *filep = NULL;
1543 struct eventfd_ctx *ctx = NULL;
1544 u64 p;
1545 long r;
1546 int i, fd;
1547
1548 /* If you are not the owner, you can become one */
1549 if (ioctl == VHOST_SET_OWNER) {
1550 r = vhost_dev_set_owner(d);
1551 goto done;
1552 }
1553
1554 /* You must be the owner to do anything else */
1555 r = vhost_dev_check_owner(d);
1556 if (r)
1557 goto done;
1558
1559 switch (ioctl) {
1560 case VHOST_SET_MEM_TABLE:
1561 r = vhost_set_memory(d, argp);
1562 break;
1563 case VHOST_SET_LOG_BASE:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001564 if (copy_from_user(&p, argp, sizeof p)) {
1565 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001566 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001567 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001568 if ((u64)(unsigned long)p != p) {
1569 r = -EFAULT;
1570 break;
1571 }
1572 for (i = 0; i < d->nvqs; ++i) {
1573 struct vhost_virtqueue *vq;
1574 void __user *base = (void __user *)(unsigned long)p;
Asias He3ab2e422013-04-27 11:16:48 +08001575 vq = d->vqs[i];
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001576 mutex_lock(&vq->mutex);
1577 /* If ring is inactive, will check when it's enabled. */
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001578 if (vq->private_data && !vq_log_access_ok(vq, base))
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001579 r = -EFAULT;
1580 else
1581 vq->log_base = base;
1582 mutex_unlock(&vq->mutex);
1583 }
1584 break;
1585 case VHOST_SET_LOG_FD:
1586 r = get_user(fd, (int __user *)argp);
1587 if (r < 0)
1588 break;
1589 eventfp = fd == -1 ? NULL : eventfd_fget(fd);
1590 if (IS_ERR(eventfp)) {
1591 r = PTR_ERR(eventfp);
1592 break;
1593 }
1594 if (eventfp != d->log_file) {
1595 filep = d->log_file;
Marc-André Lureau7932c0b2015-07-17 15:32:03 +02001596 d->log_file = eventfp;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001597 ctx = d->log_ctx;
1598 d->log_ctx = eventfp ?
1599 eventfd_ctx_fileget(eventfp) : NULL;
1600 } else
1601 filep = eventfp;
1602 for (i = 0; i < d->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +08001603 mutex_lock(&d->vqs[i]->mutex);
1604 d->vqs[i]->log_ctx = d->log_ctx;
1605 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001606 }
1607 if (ctx)
1608 eventfd_ctx_put(ctx);
1609 if (filep)
1610 fput(filep);
1611 break;
1612 default:
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001613 r = -ENOIOCTLCMD;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001614 break;
1615 }
1616done:
1617 return r;
1618}
Asias He6ac1afb2013-05-06 16:38:21 +08001619EXPORT_SYMBOL_GPL(vhost_dev_ioctl);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001620
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001621/* TODO: This is really inefficient. We need something like get_user()
1622 * (instruction directly accesses the data, with an exception table entry
1623 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
1624 */
1625static int set_bit_to_user(int nr, void __user *addr)
1626{
1627 unsigned long log = (unsigned long)addr;
1628 struct page *page;
1629 void *base;
1630 int bit = nr + (log % PAGE_SIZE) * 8;
1631 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301632
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001633 r = get_user_pages_fast(log, 1, 1, &page);
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +02001634 if (r < 0)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001635 return r;
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +02001636 BUG_ON(r != 1);
Cong Wangc6daa7f2011-11-25 23:14:26 +08001637 base = kmap_atomic(page);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001638 set_bit(bit, base);
Cong Wangc6daa7f2011-11-25 23:14:26 +08001639 kunmap_atomic(base);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001640 set_page_dirty_lock(page);
1641 put_page(page);
1642 return 0;
1643}
1644
1645static int log_write(void __user *log_base,
1646 u64 write_address, u64 write_length)
1647{
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001648 u64 write_page = write_address / VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001649 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301650
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001651 if (!write_length)
1652 return 0;
Michael S. Tsirkin3bf9be42010-11-29 10:19:07 +02001653 write_length += write_address % VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001654 for (;;) {
1655 u64 base = (u64)(unsigned long)log_base;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001656 u64 log = base + write_page / 8;
1657 int bit = write_page % 8;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001658 if ((u64)(unsigned long)log != log)
1659 return -EFAULT;
1660 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
1661 if (r < 0)
1662 return r;
1663 if (write_length <= VHOST_PAGE_SIZE)
1664 break;
1665 write_length -= VHOST_PAGE_SIZE;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001666 write_page += 1;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001667 }
1668 return r;
1669}
1670
Jason Wangb35efeb2019-01-16 16:54:42 +08001671static int log_write_hva(struct vhost_virtqueue *vq, u64 hva, u64 len)
1672{
1673 struct vhost_umem *umem = vq->umem;
1674 struct vhost_umem_node *u;
1675 u64 start, end, l, min;
1676 int r;
1677 bool hit = false;
1678
1679 while (len) {
1680 min = len;
1681 /* More than one GPAs can be mapped into a single HVA. So
1682 * iterate all possible umems here to be safe.
1683 */
1684 list_for_each_entry(u, &umem->umem_list, link) {
1685 if (u->userspace_addr > hva - 1 + len ||
1686 u->userspace_addr - 1 + u->size < hva)
1687 continue;
1688 start = max(u->userspace_addr, hva);
1689 end = min(u->userspace_addr - 1 + u->size,
1690 hva - 1 + len);
1691 l = end - start + 1;
1692 r = log_write(vq->log_base,
1693 u->start + start - u->userspace_addr,
1694 l);
1695 if (r < 0)
1696 return r;
1697 hit = true;
1698 min = min(l, min);
1699 }
1700
1701 if (!hit)
1702 return -EFAULT;
1703
1704 len -= min;
1705 hva += min;
1706 }
1707
1708 return 0;
1709}
1710
1711static int log_used(struct vhost_virtqueue *vq, u64 used_offset, u64 len)
1712{
1713 struct iovec iov[64];
1714 int i, ret;
1715
1716 if (!vq->iotlb)
1717 return log_write(vq->log_base, vq->log_addr + used_offset, len);
1718
1719 ret = translate_desc(vq, (uintptr_t)vq->used + used_offset,
1720 len, iov, 64, VHOST_ACCESS_WO);
Jason Wang38d315f2019-02-19 14:53:44 +08001721 if (ret < 0)
Jason Wangb35efeb2019-01-16 16:54:42 +08001722 return ret;
1723
1724 for (i = 0; i < ret; i++) {
1725 ret = log_write_hva(vq, (uintptr_t)iov[i].iov_base,
1726 iov[i].iov_len);
1727 if (ret)
1728 return ret;
1729 }
1730
1731 return 0;
1732}
1733
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001734int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
Jason Wangb35efeb2019-01-16 16:54:42 +08001735 unsigned int log_num, u64 len, struct iovec *iov, int count)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001736{
1737 int i, r;
1738
1739 /* Make sure data written is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001740 smp_wmb();
Jason Wangb35efeb2019-01-16 16:54:42 +08001741
1742 if (vq->iotlb) {
1743 for (i = 0; i < count; i++) {
1744 r = log_write_hva(vq, (uintptr_t)iov[i].iov_base,
1745 iov[i].iov_len);
1746 if (r < 0)
1747 return r;
1748 }
1749 return 0;
1750 }
1751
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001752 for (i = 0; i < log_num; ++i) {
1753 u64 l = min(log[i].len, len);
1754 r = log_write(vq->log_base, log[i].addr, l);
1755 if (r < 0)
1756 return r;
1757 len -= l;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +02001758 if (!len) {
1759 if (vq->log_ctx)
1760 eventfd_signal(vq->log_ctx, 1);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001761 return 0;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +02001762 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001763 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001764 /* Length written exceeds what we have stored. This is a bug. */
1765 BUG();
1766 return 0;
1767}
Asias He6ac1afb2013-05-06 16:38:21 +08001768EXPORT_SYMBOL_GPL(vhost_log_write);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001769
Jason Wang2723fea2011-06-21 18:04:38 +08001770static int vhost_update_used_flags(struct vhost_virtqueue *vq)
1771{
1772 void __user *used;
Jason Wangbfe2bc52016-06-23 02:04:30 -04001773 if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->used_flags),
1774 &vq->used->flags) < 0)
Jason Wang2723fea2011-06-21 18:04:38 +08001775 return -EFAULT;
1776 if (unlikely(vq->log_used)) {
1777 /* Make sure the flag is seen before log. */
1778 smp_wmb();
1779 /* Log used flag write. */
1780 used = &vq->used->flags;
Jason Wangb35efeb2019-01-16 16:54:42 +08001781 log_used(vq, (used - (void __user *)vq->used),
1782 sizeof vq->used->flags);
Jason Wang2723fea2011-06-21 18:04:38 +08001783 if (vq->log_ctx)
1784 eventfd_signal(vq->log_ctx, 1);
1785 }
1786 return 0;
1787}
1788
1789static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
1790{
Jason Wangbfe2bc52016-06-23 02:04:30 -04001791 if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->avail_idx),
1792 vhost_avail_event(vq)))
Jason Wang2723fea2011-06-21 18:04:38 +08001793 return -EFAULT;
1794 if (unlikely(vq->log_used)) {
1795 void __user *used;
1796 /* Make sure the event is seen before log. */
1797 smp_wmb();
1798 /* Log avail event write */
1799 used = vhost_avail_event(vq);
Jason Wangb35efeb2019-01-16 16:54:42 +08001800 log_used(vq, (used - (void __user *)vq->used),
1801 sizeof *vhost_avail_event(vq));
Jason Wang2723fea2011-06-21 18:04:38 +08001802 if (vq->log_ctx)
1803 eventfd_signal(vq->log_ctx, 1);
1804 }
1805 return 0;
1806}
1807
Greg Kurz80f7d032016-02-16 15:59:44 +01001808int vhost_vq_init_access(struct vhost_virtqueue *vq)
Jason Wang2723fea2011-06-21 18:04:38 +08001809{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001810 __virtio16 last_used_idx;
Jason Wang2723fea2011-06-21 18:04:38 +08001811 int r;
Greg Kurze1f33be2016-02-16 15:54:28 +01001812 bool is_le = vq->is_le;
1813
Halil Pasic1594edd2017-01-30 11:09:36 +01001814 if (!vq->private_data)
Jason Wang2723fea2011-06-21 18:04:38 +08001815 return 0;
Greg Kurz2751c982015-04-24 14:27:24 +02001816
1817 vhost_init_is_le(vq);
Jason Wang2723fea2011-06-21 18:04:38 +08001818
1819 r = vhost_update_used_flags(vq);
1820 if (r)
Greg Kurze1f33be2016-02-16 15:54:28 +01001821 goto err;
Jason Wang2723fea2011-06-21 18:04:38 +08001822 vq->signalled_used_valid = false;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001823 if (!vq->iotlb &&
1824 !access_ok(VERIFY_READ, &vq->used->idx, sizeof vq->used->idx)) {
Greg Kurze1f33be2016-02-16 15:54:28 +01001825 r = -EFAULT;
1826 goto err;
1827 }
Jason Wangbfe2bc52016-06-23 02:04:30 -04001828 r = vhost_get_user(vq, last_used_idx, &vq->used->idx);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001829 if (r) {
1830 vq_err(vq, "Can't access used idx at %p\n",
1831 &vq->used->idx);
Greg Kurze1f33be2016-02-16 15:54:28 +01001832 goto err;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001833 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001834 vq->last_used_idx = vhost16_to_cpu(vq, last_used_idx);
Michael S. Tsirkin64f7f052014-12-01 17:39:39 +02001835 return 0;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001836
Greg Kurze1f33be2016-02-16 15:54:28 +01001837err:
1838 vq->is_le = is_le;
1839 return r;
Jason Wang2723fea2011-06-21 18:04:38 +08001840}
Greg Kurz80f7d032016-02-16 15:59:44 +01001841EXPORT_SYMBOL_GPL(vhost_vq_init_access);
Jason Wang2723fea2011-06-21 18:04:38 +08001842
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001843static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001844 struct iovec iov[], int iov_size, int access)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001845{
Jason Wanga9709d62016-06-23 02:04:31 -04001846 const struct vhost_umem_node *node;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001847 struct vhost_dev *dev = vq->dev;
1848 struct vhost_umem *umem = dev->iotlb ? dev->iotlb : dev->umem;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001849 struct iovec *_iov;
1850 u64 s = 0;
1851 int ret = 0;
1852
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001853 while ((u64)len > s) {
1854 u64 size;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001855 if (unlikely(ret >= iov_size)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001856 ret = -ENOBUFS;
1857 break;
1858 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001859
Jason Wanga9709d62016-06-23 02:04:31 -04001860 node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
1861 addr, addr + len - 1);
1862 if (node == NULL || node->start > addr) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001863 if (umem != dev->iotlb) {
1864 ret = -EFAULT;
1865 break;
1866 }
1867 ret = -EAGAIN;
1868 break;
1869 } else if (!(node->perm & access)) {
1870 ret = -EPERM;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001871 break;
1872 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001873
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001874 _iov = iov + ret;
Jason Wanga9709d62016-06-23 02:04:31 -04001875 size = node->size - addr + node->start;
Michael S. Tsirkinbd971202012-11-26 05:57:27 +00001876 _iov->iov_len = min((u64)len - s, size);
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001877 _iov->iov_base = (void __user *)(unsigned long)
Jason Wanga9709d62016-06-23 02:04:31 -04001878 (node->userspace_addr + addr - node->start);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001879 s += size;
1880 addr += size;
1881 ++ret;
1882 }
1883
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001884 if (ret == -EAGAIN)
1885 vhost_iotlb_miss(vq, addr, access);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001886 return ret;
1887}
1888
1889/* Each buffer in the virtqueues is actually a chain of descriptors. This
1890 * function returns the next descriptor in the chain,
1891 * or -1U if we're at the end. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001892static unsigned next_desc(struct vhost_virtqueue *vq, struct vring_desc *desc)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001893{
1894 unsigned int next;
1895
1896 /* If this descriptor says it doesn't chain, we're done. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001897 if (!(desc->flags & cpu_to_vhost16(vq, VRING_DESC_F_NEXT)))
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001898 return -1U;
1899
1900 /* Check they're not leading us off end of descriptors. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001901 next = vhost16_to_cpu(vq, desc->next);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001902 /* Make sure compiler knows to grab that: we don't want it changing! */
1903 /* We will use the result as an index in an array, so most
1904 * architectures only need a compiler barrier here. */
1905 read_barrier_depends();
1906
1907 return next;
1908}
1909
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001910static int get_indirect(struct vhost_virtqueue *vq,
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001911 struct iovec iov[], unsigned int iov_size,
1912 unsigned int *out_num, unsigned int *in_num,
1913 struct vhost_log *log, unsigned int *log_num,
1914 struct vring_desc *indirect)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001915{
1916 struct vring_desc desc;
1917 unsigned int i = 0, count, found = 0;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001918 u32 len = vhost32_to_cpu(vq, indirect->len);
Al Viroaad9a1c2014-12-10 14:49:01 -05001919 struct iov_iter from;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001920 int ret, access;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001921
1922 /* Sanity check */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001923 if (unlikely(len % sizeof desc)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001924 vq_err(vq, "Invalid length in indirect descriptor: "
1925 "len 0x%llx not multiple of 0x%zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001926 (unsigned long long)len,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001927 sizeof desc);
1928 return -EINVAL;
1929 }
1930
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001931 ret = translate_desc(vq, vhost64_to_cpu(vq, indirect->addr), len, vq->indirect,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001932 UIO_MAXIOV, VHOST_ACCESS_RO);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001933 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001934 if (ret != -EAGAIN)
1935 vq_err(vq, "Translation failure %d in indirect.\n", ret);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001936 return ret;
1937 }
Al Viroaad9a1c2014-12-10 14:49:01 -05001938 iov_iter_init(&from, READ, vq->indirect, ret, len);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001939
1940 /* We will use the result as an address to read from, so most
1941 * architectures only need a compiler barrier here. */
1942 read_barrier_depends();
1943
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001944 count = len / sizeof desc;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001945 /* Buffers are chained via a 16 bit next field, so
1946 * we can have at most 2^16 of these. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001947 if (unlikely(count > USHRT_MAX + 1)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001948 vq_err(vq, "Indirect buffer length too big: %d\n",
1949 indirect->len);
1950 return -E2BIG;
1951 }
1952
1953 do {
1954 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001955 if (unlikely(++found > count)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001956 vq_err(vq, "Loop detected: last one at %u "
1957 "indirect size %u\n",
1958 i, count);
1959 return -EINVAL;
1960 }
Al Viroaad9a1c2014-12-10 14:49:01 -05001961 if (unlikely(copy_from_iter(&desc, sizeof(desc), &from) !=
1962 sizeof(desc))) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001963 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001964 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001965 return -EINVAL;
1966 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001967 if (unlikely(desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT))) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001968 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001969 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001970 return -EINVAL;
1971 }
1972
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001973 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE))
1974 access = VHOST_ACCESS_WO;
1975 else
1976 access = VHOST_ACCESS_RO;
1977
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001978 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
1979 vhost32_to_cpu(vq, desc.len), iov + iov_count,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001980 iov_size - iov_count, access);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001981 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001982 if (ret != -EAGAIN)
1983 vq_err(vq, "Translation failure %d indirect idx %d\n",
1984 ret, i);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001985 return ret;
1986 }
1987 /* If this is an input descriptor, increment that count. */
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001988 if (access == VHOST_ACCESS_WO) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001989 *in_num += ret;
yongduan8d827682019-09-11 17:44:24 +08001990 if (unlikely(log && ret)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001991 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
1992 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001993 ++*log_num;
1994 }
1995 } else {
1996 /* If it's an output descriptor, they're all supposed
1997 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001998 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001999 vq_err(vq, "Indirect descriptor "
2000 "has out after in: idx %d\n", i);
2001 return -EINVAL;
2002 }
2003 *out_num += ret;
2004 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002005 } while ((i = next_desc(vq, &desc)) != -1);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002006 return 0;
2007}
2008
2009/* This looks in the virtqueue and for the first available buffer, and converts
2010 * it to an iovec for convenient access. Since descriptors consist of some
2011 * number of output then some number of input descriptors, it's actually two
2012 * iovecs, but we pack them into one and note how many of each there were.
2013 *
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002014 * This function returns the descriptor number found, or vq->num (which is
2015 * never a valid descriptor number) if none was found. A negative code is
2016 * returned on error. */
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03002017int vhost_get_vq_desc(struct vhost_virtqueue *vq,
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002018 struct iovec iov[], unsigned int iov_size,
2019 unsigned int *out_num, unsigned int *in_num,
2020 struct vhost_log *log, unsigned int *log_num)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002021{
2022 struct vring_desc desc;
2023 unsigned int i, head, found = 0;
2024 u16 last_avail_idx;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002025 __virtio16 avail_idx;
2026 __virtio16 ring_head;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002027 int ret, access;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002028
2029 /* Check it isn't doing very strange things with descriptor numbers. */
2030 last_avail_idx = vq->last_avail_idx;
Jason Wangbfe2bc52016-06-23 02:04:30 -04002031 if (unlikely(vhost_get_user(vq, avail_idx, &vq->avail->idx))) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002032 vq_err(vq, "Failed to access avail idx at %p\n",
2033 &vq->avail->idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002034 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002035 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002036 vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002037
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002038 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002039 vq_err(vq, "Guest moved used index from %u to %u",
2040 last_avail_idx, vq->avail_idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002041 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002042 }
2043
2044 /* If there's nothing new since last we looked, return invalid. */
2045 if (vq->avail_idx == last_avail_idx)
2046 return vq->num;
2047
2048 /* Only get avail ring entries after they have been exposed by guest. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00002049 smp_rmb();
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002050
2051 /* Grab the next descriptor number they're advertising, and increment
2052 * the index we've seen. */
Jason Wangbfe2bc52016-06-23 02:04:30 -04002053 if (unlikely(vhost_get_user(vq, ring_head,
2054 &vq->avail->ring[last_avail_idx & (vq->num - 1)]))) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002055 vq_err(vq, "Failed to read head: idx %d address %p\n",
2056 last_avail_idx,
2057 &vq->avail->ring[last_avail_idx % vq->num]);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002058 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002059 }
2060
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002061 head = vhost16_to_cpu(vq, ring_head);
2062
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002063 /* If their number is silly, that's an error. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002064 if (unlikely(head >= vq->num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002065 vq_err(vq, "Guest says index %u > %u is available",
2066 head, vq->num);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002067 return -EINVAL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002068 }
2069
2070 /* When we start there are none of either input nor output. */
2071 *out_num = *in_num = 0;
2072 if (unlikely(log))
2073 *log_num = 0;
2074
2075 i = head;
2076 do {
2077 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002078 if (unlikely(i >= vq->num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002079 vq_err(vq, "Desc index is %u > %u, head = %u",
2080 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002081 return -EINVAL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002082 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002083 if (unlikely(++found > vq->num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002084 vq_err(vq, "Loop detected: last one at %u "
2085 "vq size %u head %u\n",
2086 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002087 return -EINVAL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002088 }
Jason Wangbfe2bc52016-06-23 02:04:30 -04002089 ret = vhost_copy_from_user(vq, &desc, vq->desc + i,
2090 sizeof desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002091 if (unlikely(ret)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002092 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
2093 i, vq->desc + i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002094 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002095 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002096 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT)) {
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03002097 ret = get_indirect(vq, iov, iov_size,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002098 out_num, in_num,
2099 log, log_num, &desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002100 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002101 if (ret != -EAGAIN)
2102 vq_err(vq, "Failure detected "
2103 "in indirect descriptor at idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002104 return ret;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002105 }
2106 continue;
2107 }
2108
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002109 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE))
2110 access = VHOST_ACCESS_WO;
2111 else
2112 access = VHOST_ACCESS_RO;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002113 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
2114 vhost32_to_cpu(vq, desc.len), iov + iov_count,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002115 iov_size - iov_count, access);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002116 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002117 if (ret != -EAGAIN)
2118 vq_err(vq, "Translation failure %d descriptor idx %d\n",
2119 ret, i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002120 return ret;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002121 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002122 if (access == VHOST_ACCESS_WO) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002123 /* If this is an input descriptor,
2124 * increment that count. */
2125 *in_num += ret;
yongduan8d827682019-09-11 17:44:24 +08002126 if (unlikely(log && ret)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002127 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
2128 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002129 ++*log_num;
2130 }
2131 } else {
2132 /* If it's an output descriptor, they're all supposed
2133 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002134 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002135 vq_err(vq, "Descriptor has out after in: "
2136 "idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002137 return -EINVAL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002138 }
2139 *out_num += ret;
2140 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002141 } while ((i = next_desc(vq, &desc)) != -1);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002142
2143 /* On success, increment avail index. */
2144 vq->last_avail_idx++;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002145
2146 /* Assume notifications from guest are disabled at this point,
2147 * if they aren't we would need to update avail_event index. */
2148 BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY));
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002149 return head;
2150}
Asias He6ac1afb2013-05-06 16:38:21 +08002151EXPORT_SYMBOL_GPL(vhost_get_vq_desc);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002152
2153/* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
David Stevens8dd014a2010-07-27 18:52:21 +03002154void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002155{
David Stevens8dd014a2010-07-27 18:52:21 +03002156 vq->last_avail_idx -= n;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002157}
Asias He6ac1afb2013-05-06 16:38:21 +08002158EXPORT_SYMBOL_GPL(vhost_discard_vq_desc);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002159
2160/* After we've used one of their buffers, we tell them about it. We'll then
2161 * want to notify the guest, using eventfd. */
2162int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
2163{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002164 struct vring_used_elem heads = {
2165 cpu_to_vhost32(vq, head),
2166 cpu_to_vhost32(vq, len)
2167 };
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002168
Jason Wangc49e4e52013-09-02 16:40:58 +08002169 return vhost_add_used_n(vq, &heads, 1);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002170}
Asias He6ac1afb2013-05-06 16:38:21 +08002171EXPORT_SYMBOL_GPL(vhost_add_used);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002172
David Stevens8dd014a2010-07-27 18:52:21 +03002173static int __vhost_add_used_n(struct vhost_virtqueue *vq,
2174 struct vring_used_elem *heads,
2175 unsigned count)
2176{
2177 struct vring_used_elem __user *used;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002178 u16 old, new;
David Stevens8dd014a2010-07-27 18:52:21 +03002179 int start;
2180
Michael S. Tsirkin5fba13b2015-11-29 13:34:44 +02002181 start = vq->last_used_idx & (vq->num - 1);
David Stevens8dd014a2010-07-27 18:52:21 +03002182 used = vq->used->ring + start;
Jason Wangc49e4e52013-09-02 16:40:58 +08002183 if (count == 1) {
Jason Wangbfe2bc52016-06-23 02:04:30 -04002184 if (vhost_put_user(vq, heads[0].id, &used->id)) {
Jason Wangc49e4e52013-09-02 16:40:58 +08002185 vq_err(vq, "Failed to write used id");
2186 return -EFAULT;
2187 }
Jason Wangbfe2bc52016-06-23 02:04:30 -04002188 if (vhost_put_user(vq, heads[0].len, &used->len)) {
Jason Wangc49e4e52013-09-02 16:40:58 +08002189 vq_err(vq, "Failed to write used len");
2190 return -EFAULT;
2191 }
Jason Wangbfe2bc52016-06-23 02:04:30 -04002192 } else if (vhost_copy_to_user(vq, used, heads, count * sizeof *used)) {
David Stevens8dd014a2010-07-27 18:52:21 +03002193 vq_err(vq, "Failed to write used");
2194 return -EFAULT;
2195 }
2196 if (unlikely(vq->log_used)) {
2197 /* Make sure data is seen before log. */
2198 smp_wmb();
2199 /* Log used ring entry write. */
Jason Wangb35efeb2019-01-16 16:54:42 +08002200 log_used(vq, ((void __user *)used - (void __user *)vq->used),
2201 count * sizeof *used);
David Stevens8dd014a2010-07-27 18:52:21 +03002202 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002203 old = vq->last_used_idx;
2204 new = (vq->last_used_idx += count);
2205 /* If the driver never bothers to signal in a very long while,
2206 * used index might wrap around. If that happens, invalidate
2207 * signalled_used index we stored. TODO: make sure driver
2208 * signals at least once in 2^16 and remove this. */
2209 if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old)))
2210 vq->signalled_used_valid = false;
David Stevens8dd014a2010-07-27 18:52:21 +03002211 return 0;
2212}
2213
2214/* After we've used one of their buffers, we tell them about it. We'll then
2215 * want to notify the guest, using eventfd. */
2216int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
2217 unsigned count)
2218{
2219 int start, n, r;
2220
Michael S. Tsirkin5fba13b2015-11-29 13:34:44 +02002221 start = vq->last_used_idx & (vq->num - 1);
David Stevens8dd014a2010-07-27 18:52:21 +03002222 n = vq->num - start;
2223 if (n < count) {
2224 r = __vhost_add_used_n(vq, heads, n);
2225 if (r < 0)
2226 return r;
2227 heads += n;
2228 count -= n;
2229 }
2230 r = __vhost_add_used_n(vq, heads, count);
2231
2232 /* Make sure buffer is written before we update index. */
2233 smp_wmb();
Jason Wangbfe2bc52016-06-23 02:04:30 -04002234 if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->last_used_idx),
2235 &vq->used->idx)) {
David Stevens8dd014a2010-07-27 18:52:21 +03002236 vq_err(vq, "Failed to increment used idx");
2237 return -EFAULT;
2238 }
2239 if (unlikely(vq->log_used)) {
Jason Wangbd50de32018-12-13 10:53:37 +08002240 /* Make sure used idx is seen before log. */
2241 smp_wmb();
David Stevens8dd014a2010-07-27 18:52:21 +03002242 /* Log used index update. */
Jason Wangb35efeb2019-01-16 16:54:42 +08002243 log_used(vq, offsetof(struct vring_used, idx),
2244 sizeof vq->used->idx);
David Stevens8dd014a2010-07-27 18:52:21 +03002245 if (vq->log_ctx)
2246 eventfd_signal(vq->log_ctx, 1);
2247 }
2248 return r;
2249}
Asias He6ac1afb2013-05-06 16:38:21 +08002250EXPORT_SYMBOL_GPL(vhost_add_used_n);
David Stevens8dd014a2010-07-27 18:52:21 +03002251
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002252static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002253{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002254 __u16 old, new;
2255 __virtio16 event;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002256 bool v;
Michael S. Tsirkin0d499352010-05-11 19:44:17 +03002257 /* Flush out used index updates. This is paired
2258 * with the barrier that the Guest executes when enabling
2259 * interrupts. */
2260 smp_mb();
2261
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002262 if (vhost_has_feature(vq, VIRTIO_F_NOTIFY_ON_EMPTY) &&
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002263 unlikely(vq->avail_idx == vq->last_avail_idx))
2264 return true;
2265
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002266 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002267 __virtio16 flags;
Jason Wangbfe2bc52016-06-23 02:04:30 -04002268 if (vhost_get_user(vq, flags, &vq->avail->flags)) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002269 vq_err(vq, "Failed to get flags");
2270 return true;
2271 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002272 return !(flags & cpu_to_vhost16(vq, VRING_AVAIL_F_NO_INTERRUPT));
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002273 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002274 old = vq->signalled_used;
2275 v = vq->signalled_used_valid;
2276 new = vq->signalled_used = vq->last_used_idx;
2277 vq->signalled_used_valid = true;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002278
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002279 if (unlikely(!v))
2280 return true;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002281
Jason Wangbfe2bc52016-06-23 02:04:30 -04002282 if (vhost_get_user(vq, event, vhost_used_event(vq))) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002283 vq_err(vq, "Failed to get used event idx");
2284 return true;
2285 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002286 return vring_need_event(vhost16_to_cpu(vq, event), new, old);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002287}
2288
2289/* This actually signals the guest, using eventfd. */
2290void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
2291{
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002292 /* Signal the Guest tell them we used something up. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002293 if (vq->call_ctx && vhost_notify(dev, vq))
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002294 eventfd_signal(vq->call_ctx, 1);
2295}
Asias He6ac1afb2013-05-06 16:38:21 +08002296EXPORT_SYMBOL_GPL(vhost_signal);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002297
2298/* And here's the combo meal deal. Supersize me! */
2299void vhost_add_used_and_signal(struct vhost_dev *dev,
2300 struct vhost_virtqueue *vq,
2301 unsigned int head, int len)
2302{
2303 vhost_add_used(vq, head, len);
2304 vhost_signal(dev, vq);
2305}
Asias He6ac1afb2013-05-06 16:38:21 +08002306EXPORT_SYMBOL_GPL(vhost_add_used_and_signal);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002307
David Stevens8dd014a2010-07-27 18:52:21 +03002308/* multi-buffer version of vhost_add_used_and_signal */
2309void vhost_add_used_and_signal_n(struct vhost_dev *dev,
2310 struct vhost_virtqueue *vq,
2311 struct vring_used_elem *heads, unsigned count)
2312{
2313 vhost_add_used_n(vq, heads, count);
2314 vhost_signal(dev, vq);
2315}
Asias He6ac1afb2013-05-06 16:38:21 +08002316EXPORT_SYMBOL_GPL(vhost_add_used_and_signal_n);
David Stevens8dd014a2010-07-27 18:52:21 +03002317
Jason Wangd4a60602016-03-04 06:24:52 -05002318/* return true if we're sure that avaiable ring is empty */
2319bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq)
2320{
2321 __virtio16 avail_idx;
2322 int r;
2323
Jason Wangbfe2bc52016-06-23 02:04:30 -04002324 r = vhost_get_user(vq, avail_idx, &vq->avail->idx);
Jason Wangd4a60602016-03-04 06:24:52 -05002325 if (r)
2326 return false;
2327
2328 return vhost16_to_cpu(vq, avail_idx) == vq->avail_idx;
2329}
2330EXPORT_SYMBOL_GPL(vhost_vq_avail_empty);
2331
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002332/* OK, now we need to know about added descriptors. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002333bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002334{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002335 __virtio16 avail_idx;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002336 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05302337
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002338 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
2339 return false;
2340 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002341 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08002342 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002343 if (r) {
2344 vq_err(vq, "Failed to enable notification at %p: %d\n",
2345 &vq->used->flags, r);
2346 return false;
2347 }
2348 } else {
Jason Wang2723fea2011-06-21 18:04:38 +08002349 r = vhost_update_avail_event(vq, vq->avail_idx);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002350 if (r) {
2351 vq_err(vq, "Failed to update avail event index at %p: %d\n",
2352 vhost_avail_event(vq), r);
2353 return false;
2354 }
2355 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002356 /* They could have slipped one in as we were doing that: make
2357 * sure it's written, then check again. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00002358 smp_mb();
Jason Wangbfe2bc52016-06-23 02:04:30 -04002359 r = vhost_get_user(vq, avail_idx, &vq->avail->idx);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002360 if (r) {
2361 vq_err(vq, "Failed to check avail idx at %p: %d\n",
2362 &vq->avail->idx, r);
2363 return false;
2364 }
2365
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002366 return vhost16_to_cpu(vq, avail_idx) != vq->avail_idx;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002367}
Asias He6ac1afb2013-05-06 16:38:21 +08002368EXPORT_SYMBOL_GPL(vhost_enable_notify);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002369
2370/* We don't need to be notified again. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002371void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002372{
2373 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05302374
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002375 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
2376 return;
2377 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002378 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08002379 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002380 if (r)
2381 vq_err(vq, "Failed to enable notification at %p: %d\n",
2382 &vq->used->flags, r);
2383 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00002384}
Asias He6ac1afb2013-05-06 16:38:21 +08002385EXPORT_SYMBOL_GPL(vhost_disable_notify);
2386
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002387/* Create a new message. */
2388struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type)
2389{
2390 struct vhost_msg_node *node = kmalloc(sizeof *node, GFP_KERNEL);
2391 if (!node)
2392 return NULL;
Michael S. Tsirkin9681c3b2018-05-12 00:33:10 +03002393
2394 /* Make sure all padding within the structure is initialized. */
2395 memset(&node->msg, 0, sizeof node->msg);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002396 node->vq = vq;
2397 node->msg.type = type;
2398 return node;
2399}
2400EXPORT_SYMBOL_GPL(vhost_new_msg);
2401
2402void vhost_enqueue_msg(struct vhost_dev *dev, struct list_head *head,
2403 struct vhost_msg_node *node)
2404{
2405 spin_lock(&dev->iotlb_lock);
2406 list_add_tail(&node->node, head);
2407 spin_unlock(&dev->iotlb_lock);
2408
2409 wake_up_interruptible_poll(&dev->wait, POLLIN | POLLRDNORM);
2410}
2411EXPORT_SYMBOL_GPL(vhost_enqueue_msg);
2412
2413struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev,
2414 struct list_head *head)
2415{
2416 struct vhost_msg_node *node = NULL;
2417
2418 spin_lock(&dev->iotlb_lock);
2419 if (!list_empty(head)) {
2420 node = list_first_entry(head, struct vhost_msg_node,
2421 node);
2422 list_del(&node->node);
2423 }
2424 spin_unlock(&dev->iotlb_lock);
2425
2426 return node;
2427}
2428EXPORT_SYMBOL_GPL(vhost_dequeue_msg);
2429
2430
Asias He6ac1afb2013-05-06 16:38:21 +08002431static int __init vhost_init(void)
2432{
2433 return 0;
2434}
2435
2436static void __exit vhost_exit(void)
2437{
2438}
2439
2440module_init(vhost_init);
2441module_exit(vhost_exit);
2442
2443MODULE_VERSION("0.0.1");
2444MODULE_LICENSE("GPL v2");
2445MODULE_AUTHOR("Michael S. Tsirkin");
2446MODULE_DESCRIPTION("Host kernel accelerator for virtio");