blob: 5915f240275a9f122aa84cc6777f33410b1aa6be [file] [log] [blame]
Michael S. Tsirkin3a4d5c92010-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. Tsirkin3a4d5c92010-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. Tsirkin3a4d5c92010-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. Tsirkin3a4d5c92010-01-14 06:17:27 +000019#include <linux/miscdevice.h>
20#include <linux/mutex.h>
Michael S. Tsirkin3a4d5c92010-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>
Ingo Molnar6e84f312017-02-08 18:51:29 +010030#include <linux/sched/mm.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010031#include <linux/sched/signal.h>
Jason Wanga9709d62016-06-23 02:04:31 -040032#include <linux/interval_tree_generic.h>
Jason Wangff002262018-10-30 14:10:49 +080033#include <linux/nospec.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000034
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000035#include "vhost.h"
36
Igor Mammedovc9ce42f2015-07-02 15:08:11 +020037static ushort max_mem_regions = 64;
38module_param(max_mem_regions, ushort, 0444);
39MODULE_PARM_DESC(max_mem_regions,
40 "Maximum number of memory regions in memory map. (default: 64)");
Jason Wang6b1e6cc2016-06-23 02:04:32 -040041static int max_iotlb_entries = 2048;
42module_param(max_iotlb_entries, int, 0444);
43MODULE_PARM_DESC(max_iotlb_entries,
44 "Maximum number of iotlb entries. (default: 2048)");
Igor Mammedovc9ce42f2015-07-02 15:08:11 +020045
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000046enum {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000047 VHOST_MEMORY_F_LOG = 0x1,
48};
49
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +030050#define vhost_used_event(vq) ((__virtio16 __user *)&vq->avail->ring[vq->num])
51#define vhost_avail_event(vq) ((__virtio16 __user *)&vq->used->ring[vq->num])
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +030052
Jason Wanga9709d62016-06-23 02:04:31 -040053INTERVAL_TREE_DEFINE(struct vhost_umem_node,
54 rb, __u64, __subtree_last,
Michael S. Tsirkin2f952c02016-12-06 05:57:54 +020055 START, LAST, static inline, vhost_umem_interval_tree);
Jason Wanga9709d62016-06-23 02:04:31 -040056
Greg Kurz2751c982015-04-24 14:27:24 +020057#ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
Greg Kurzc5072032016-02-16 15:59:34 +010058static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
Greg Kurz2751c982015-04-24 14:27:24 +020059{
60 vq->user_be = !virtio_legacy_is_little_endian();
61}
62
Greg Kurzc5072032016-02-16 15:59:34 +010063static void vhost_enable_cross_endian_big(struct vhost_virtqueue *vq)
64{
65 vq->user_be = true;
66}
67
68static void vhost_enable_cross_endian_little(struct vhost_virtqueue *vq)
69{
70 vq->user_be = false;
71}
72
Greg Kurz2751c982015-04-24 14:27:24 +020073static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
74{
75 struct vhost_vring_state s;
76
77 if (vq->private_data)
78 return -EBUSY;
79
80 if (copy_from_user(&s, argp, sizeof(s)))
81 return -EFAULT;
82
83 if (s.num != VHOST_VRING_LITTLE_ENDIAN &&
84 s.num != VHOST_VRING_BIG_ENDIAN)
85 return -EINVAL;
86
Greg Kurzc5072032016-02-16 15:59:34 +010087 if (s.num == VHOST_VRING_BIG_ENDIAN)
88 vhost_enable_cross_endian_big(vq);
89 else
90 vhost_enable_cross_endian_little(vq);
Greg Kurz2751c982015-04-24 14:27:24 +020091
92 return 0;
93}
94
95static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
96 int __user *argp)
97{
98 struct vhost_vring_state s = {
99 .index = idx,
100 .num = vq->user_be
101 };
102
103 if (copy_to_user(argp, &s, sizeof(s)))
104 return -EFAULT;
105
106 return 0;
107}
108
109static void vhost_init_is_le(struct vhost_virtqueue *vq)
110{
111 /* Note for legacy virtio: user_be is initialized at reset time
112 * according to the host endianness. If userspace does not set an
113 * explicit endianness, the default behavior is native endian, as
114 * expected by legacy virtio.
115 */
116 vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) || !vq->user_be;
117}
118#else
Greg Kurzc5072032016-02-16 15:59:34 +0100119static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
Greg Kurz2751c982015-04-24 14:27:24 +0200120{
121}
122
123static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
124{
125 return -ENOIOCTLCMD;
126}
127
128static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
129 int __user *argp)
130{
131 return -ENOIOCTLCMD;
132}
133
134static void vhost_init_is_le(struct vhost_virtqueue *vq)
135{
Halil Pasiccda8bba2017-01-30 11:09:36 +0100136 vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1)
137 || virtio_legacy_is_little_endian();
Greg Kurz2751c982015-04-24 14:27:24 +0200138}
139#endif /* CONFIG_VHOST_CROSS_ENDIAN_LEGACY */
140
Greg Kurzc5072032016-02-16 15:59:34 +0100141static void vhost_reset_is_le(struct vhost_virtqueue *vq)
142{
Halil Pasiccda8bba2017-01-30 11:09:36 +0100143 vhost_init_is_le(vq);
Greg Kurzc5072032016-02-16 15:59:34 +0100144}
145
Jason Wang7235acd2016-04-25 22:14:32 -0400146struct vhost_flush_struct {
147 struct vhost_work work;
148 struct completion wait_event;
149};
150
151static void vhost_flush_work(struct vhost_work *work)
152{
153 struct vhost_flush_struct *s;
154
155 s = container_of(work, struct vhost_flush_struct, work);
156 complete(&s->wait_event);
157}
158
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000159static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
160 poll_table *pt)
161{
162 struct vhost_poll *poll;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000163
Krishna Kumard47effe2011-03-01 17:06:37 +0530164 poll = container_of(pt, struct vhost_poll, table);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000165 poll->wqh = wqh;
166 add_wait_queue(wqh, &poll->wait);
167}
168
Ingo Molnarac6424b2017-06-20 12:06:13 +0200169static int vhost_poll_wakeup(wait_queue_entry_t *wait, unsigned mode, int sync,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000170 void *key)
171{
Tejun Heoc23f34452010-06-02 20:40:00 +0200172 struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
173
Al Viro3ad6f932017-07-03 20:14:56 -0400174 if (!(key_to_poll(key) & poll->mask))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000175 return 0;
176
Tejun Heoc23f34452010-06-02 20:40:00 +0200177 vhost_poll_queue(poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000178 return 0;
179}
180
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000181void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000182{
Jason Wang04b96e52016-04-25 22:14:33 -0400183 clear_bit(VHOST_WORK_QUEUED, &work->flags);
Tejun Heoc23f34452010-06-02 20:40:00 +0200184 work->fn = fn;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000185}
Asias He6ac1afb2013-05-06 16:38:21 +0800186EXPORT_SYMBOL_GPL(vhost_work_init);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000187
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300188/* Init poll structure */
189void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
Al Viro58e3b602017-07-03 23:50:40 -0400190 __poll_t mask, struct vhost_dev *dev)
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300191{
192 init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
193 init_poll_funcptr(&poll->table, vhost_poll_func);
194 poll->mask = mask;
195 poll->dev = dev;
Jason Wang2b8b3282013-01-28 01:05:18 +0000196 poll->wqh = NULL;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300197
198 vhost_work_init(&poll->work, fn);
199}
Asias He6ac1afb2013-05-06 16:38:21 +0800200EXPORT_SYMBOL_GPL(vhost_poll_init);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300201
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000202/* Start polling a file. We add ourselves to file's wait queue. The caller must
203 * keep a reference to a file until after vhost_poll_stop is called. */
Jason Wang2b8b3282013-01-28 01:05:18 +0000204int vhost_poll_start(struct vhost_poll *poll, struct file *file)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000205{
Al Viroe6c8adc2017-07-03 22:25:56 -0400206 __poll_t mask;
Jason Wang2b8b3282013-01-28 01:05:18 +0000207 int ret = 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530208
Jason Wang70181d512013-04-10 20:50:48 +0000209 if (poll->wqh)
210 return 0;
211
Christoph Hellwig9965ed172018-03-05 07:26:05 -0800212 mask = vfs_poll(file, &poll->table);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000213 if (mask)
Al Viro3ad6f932017-07-03 20:14:56 -0400214 vhost_poll_wakeup(&poll->wait, 0, 0, poll_to_key(mask));
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800215 if (mask & EPOLLERR) {
Jason Wangdc6455a2018-03-27 20:50:52 +0800216 vhost_poll_stop(poll);
Jason Wang2b8b3282013-01-28 01:05:18 +0000217 ret = -EINVAL;
218 }
219
220 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000221}
Asias He6ac1afb2013-05-06 16:38:21 +0800222EXPORT_SYMBOL_GPL(vhost_poll_start);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000223
224/* Stop polling a file. After this function returns, it becomes safe to drop the
225 * file reference. You must also flush afterwards. */
226void vhost_poll_stop(struct vhost_poll *poll)
227{
Jason Wang2b8b3282013-01-28 01:05:18 +0000228 if (poll->wqh) {
229 remove_wait_queue(poll->wqh, &poll->wait);
230 poll->wqh = NULL;
231 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000232}
Asias He6ac1afb2013-05-06 16:38:21 +0800233EXPORT_SYMBOL_GPL(vhost_poll_stop);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000234
Asias He6ac1afb2013-05-06 16:38:21 +0800235void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000236{
Jason Wang7235acd2016-04-25 22:14:32 -0400237 struct vhost_flush_struct flush;
Tejun Heoc23f34452010-06-02 20:40:00 +0200238
Jason Wang7235acd2016-04-25 22:14:32 -0400239 if (dev->worker) {
240 init_completion(&flush.wait_event);
241 vhost_work_init(&flush.work, vhost_flush_work);
242
243 vhost_work_queue(dev, &flush.work);
244 wait_for_completion(&flush.wait_event);
245 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000246}
Asias He6ac1afb2013-05-06 16:38:21 +0800247EXPORT_SYMBOL_GPL(vhost_work_flush);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000248
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300249/* Flush any work that has been scheduled. When calling this, don't hold any
250 * locks that are also used by the callback. */
251void vhost_poll_flush(struct vhost_poll *poll)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000252{
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300253 vhost_work_flush(poll->dev, &poll->work);
254}
Asias He6ac1afb2013-05-06 16:38:21 +0800255EXPORT_SYMBOL_GPL(vhost_poll_flush);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300256
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000257void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300258{
Jason Wang04b96e52016-04-25 22:14:33 -0400259 if (!dev->worker)
260 return;
Tejun Heoc23f34452010-06-02 20:40:00 +0200261
Jason Wang04b96e52016-04-25 22:14:33 -0400262 if (!test_and_set_bit(VHOST_WORK_QUEUED, &work->flags)) {
263 /* We can only add the work to the list after we're
264 * sure it was not in the list.
Peng Tao635abf02016-12-07 17:52:19 +0800265 * test_and_set_bit() implies a memory barrier.
Jason Wang04b96e52016-04-25 22:14:33 -0400266 */
Jason Wang04b96e52016-04-25 22:14:33 -0400267 llist_add(&work->node, &dev->work_list);
Tejun Heoc23f34452010-06-02 20:40:00 +0200268 wake_up_process(dev->worker);
269 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000270}
Asias He6ac1afb2013-05-06 16:38:21 +0800271EXPORT_SYMBOL_GPL(vhost_work_queue);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000272
Jason Wang526d3e72016-03-04 06:24:51 -0500273/* A lockless hint for busy polling code to exit the loop */
274bool vhost_has_work(struct vhost_dev *dev)
275{
Jason Wang04b96e52016-04-25 22:14:33 -0400276 return !llist_empty(&dev->work_list);
Jason Wang526d3e72016-03-04 06:24:51 -0500277}
278EXPORT_SYMBOL_GPL(vhost_has_work);
279
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300280void vhost_poll_queue(struct vhost_poll *poll)
281{
282 vhost_work_queue(poll->dev, &poll->work);
283}
Asias He6ac1afb2013-05-06 16:38:21 +0800284EXPORT_SYMBOL_GPL(vhost_poll_queue);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300285
Jason Wangf8894912017-02-28 17:56:02 +0800286static void __vhost_vq_meta_reset(struct vhost_virtqueue *vq)
287{
288 int j;
289
290 for (j = 0; j < VHOST_NUM_ADDRS; j++)
291 vq->meta_iotlb[j] = NULL;
292}
293
294static void vhost_vq_meta_reset(struct vhost_dev *d)
295{
296 int i;
297
Tonghao Zhang78139c92018-09-25 05:36:49 -0700298 for (i = 0; i < d->nvqs; ++i) {
299 mutex_lock(&d->vqs[i]->mutex);
Jason Wangf8894912017-02-28 17:56:02 +0800300 __vhost_vq_meta_reset(d->vqs[i]);
Tonghao Zhang78139c92018-09-25 05:36:49 -0700301 mutex_unlock(&d->vqs[i]->mutex);
302 }
Jason Wangf8894912017-02-28 17:56:02 +0800303}
304
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000305static void vhost_vq_reset(struct vhost_dev *dev,
306 struct vhost_virtqueue *vq)
307{
308 vq->num = 1;
309 vq->desc = NULL;
310 vq->avail = NULL;
311 vq->used = NULL;
312 vq->last_avail_idx = 0;
313 vq->avail_idx = 0;
314 vq->last_used_idx = 0;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300315 vq->signalled_used = 0;
316 vq->signalled_used_valid = false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000317 vq->used_flags = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000318 vq->log_used = false;
319 vq->log_addr = -1ull;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000320 vq->private_data = NULL;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300321 vq->acked_features = 0;
Jason Wang429711a2018-08-06 11:17:47 +0800322 vq->acked_backend_features = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000323 vq->log_base = NULL;
324 vq->error_ctx = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000325 vq->kick = NULL;
326 vq->call_ctx = NULL;
Michael S. Tsirkin73a99f02010-02-23 11:23:45 +0200327 vq->log_ctx = NULL;
Greg Kurzc5072032016-02-16 15:59:34 +0100328 vhost_reset_is_le(vq);
329 vhost_disable_cross_endian(vq);
Jason Wang03088132016-03-04 06:24:53 -0500330 vq->busyloop_timeout = 0;
Jason Wanga9709d62016-06-23 02:04:31 -0400331 vq->umem = NULL;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400332 vq->iotlb = NULL;
Jason Wangf8894912017-02-28 17:56:02 +0800333 __vhost_vq_meta_reset(vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000334}
335
Tejun Heoc23f34452010-06-02 20:40:00 +0200336static int vhost_worker(void *data)
337{
338 struct vhost_dev *dev = data;
Jason Wang04b96e52016-04-25 22:14:33 -0400339 struct vhost_work *work, *work_next;
340 struct llist_node *node;
Jens Freimannd7ffde32012-06-26 00:59:58 +0000341 mm_segment_t oldfs = get_fs();
Tejun Heoc23f34452010-06-02 20:40:00 +0200342
Jens Freimannd7ffde32012-06-26 00:59:58 +0000343 set_fs(USER_DS);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200344 use_mm(dev->mm);
345
Tejun Heoc23f34452010-06-02 20:40:00 +0200346 for (;;) {
347 /* mb paired w/ kthread_stop */
348 set_current_state(TASK_INTERRUPTIBLE);
349
Tejun Heoc23f34452010-06-02 20:40:00 +0200350 if (kthread_should_stop()) {
Tejun Heoc23f34452010-06-02 20:40:00 +0200351 __set_current_state(TASK_RUNNING);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200352 break;
Tejun Heoc23f34452010-06-02 20:40:00 +0200353 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200354
Jason Wang04b96e52016-04-25 22:14:33 -0400355 node = llist_del_all(&dev->work_list);
356 if (!node)
357 schedule();
358
359 node = llist_reverse_order(node);
360 /* make sure flag is seen after deletion */
361 smp_wmb();
362 llist_for_each_entry_safe(work, work_next, node, node) {
363 clear_bit(VHOST_WORK_QUEUED, &work->flags);
Tejun Heoc23f34452010-06-02 20:40:00 +0200364 __set_current_state(TASK_RUNNING);
365 work->fn(work);
Nadav Har'Eld550dda2012-02-27 15:07:29 +0200366 if (need_resched())
367 schedule();
Jason Wang04b96e52016-04-25 22:14:33 -0400368 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200369 }
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200370 unuse_mm(dev->mm);
Jens Freimannd7ffde32012-06-26 00:59:58 +0000371 set_fs(oldfs);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200372 return 0;
Tejun Heoc23f34452010-06-02 20:40:00 +0200373}
374
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000375static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
376{
377 kfree(vq->indirect);
378 vq->indirect = NULL;
379 kfree(vq->log);
380 vq->log = NULL;
381 kfree(vq->heads);
382 vq->heads = NULL;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000383}
384
Jason Wange0e9b402010-09-14 23:53:05 +0800385/* Helper to allocate iovec buffers for all vqs. */
386static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
387{
Asias He6d5e6aa2013-05-06 16:38:23 +0800388 struct vhost_virtqueue *vq;
Jason Wange0e9b402010-09-14 23:53:05 +0800389 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530390
Jason Wange0e9b402010-09-14 23:53:05 +0800391 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800392 vq = dev->vqs[i];
Kees Cook6da2ec52018-06-12 13:55:00 -0700393 vq->indirect = kmalloc_array(UIO_MAXIOV,
394 sizeof(*vq->indirect),
395 GFP_KERNEL);
396 vq->log = kmalloc_array(UIO_MAXIOV, sizeof(*vq->log),
397 GFP_KERNEL);
398 vq->heads = kmalloc_array(UIO_MAXIOV, sizeof(*vq->heads),
399 GFP_KERNEL);
Asias He6d5e6aa2013-05-06 16:38:23 +0800400 if (!vq->indirect || !vq->log || !vq->heads)
Jason Wange0e9b402010-09-14 23:53:05 +0800401 goto err_nomem;
402 }
403 return 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530404
Jason Wange0e9b402010-09-14 23:53:05 +0800405err_nomem:
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000406 for (; i >= 0; --i)
Asias He3ab2e422013-04-27 11:16:48 +0800407 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800408 return -ENOMEM;
409}
410
411static void vhost_dev_free_iovecs(struct vhost_dev *dev)
412{
413 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530414
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000415 for (i = 0; i < dev->nvqs; ++i)
Asias He3ab2e422013-04-27 11:16:48 +0800416 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800417}
418
Zhi Yong Wu59566b6e2013-12-07 04:13:03 +0800419void vhost_dev_init(struct vhost_dev *dev,
Asias He3ab2e422013-04-27 11:16:48 +0800420 struct vhost_virtqueue **vqs, int nvqs)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000421{
Asias He6d5e6aa2013-05-06 16:38:23 +0800422 struct vhost_virtqueue *vq;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000423 int i;
Tejun Heoc23f34452010-06-02 20:40:00 +0200424
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000425 dev->vqs = vqs;
426 dev->nvqs = nvqs;
427 mutex_init(&dev->mutex);
428 dev->log_ctx = NULL;
Jason Wanga9709d62016-06-23 02:04:31 -0400429 dev->umem = NULL;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400430 dev->iotlb = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000431 dev->mm = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200432 dev->worker = NULL;
Jason Wang04b96e52016-04-25 22:14:33 -0400433 init_llist_head(&dev->work_list);
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400434 init_waitqueue_head(&dev->wait);
435 INIT_LIST_HEAD(&dev->read_list);
436 INIT_LIST_HEAD(&dev->pending_list);
437 spin_lock_init(&dev->iotlb_lock);
Jason Wang04b96e52016-04-25 22:14:33 -0400438
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000439
440 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800441 vq = dev->vqs[i];
442 vq->log = NULL;
443 vq->indirect = NULL;
444 vq->heads = NULL;
445 vq->dev = dev;
446 mutex_init(&vq->mutex);
447 vhost_vq_reset(dev, vq);
448 if (vq->handle_kick)
449 vhost_poll_init(&vq->poll, vq->handle_kick,
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800450 EPOLLIN, dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000451 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000452}
Asias He6ac1afb2013-05-06 16:38:21 +0800453EXPORT_SYMBOL_GPL(vhost_dev_init);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000454
455/* Caller should have device mutex */
456long vhost_dev_check_owner(struct vhost_dev *dev)
457{
458 /* Are you the owner? If not, I don't think you mean to do that */
459 return dev->mm == current->mm ? 0 : -EPERM;
460}
Asias He6ac1afb2013-05-06 16:38:21 +0800461EXPORT_SYMBOL_GPL(vhost_dev_check_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000462
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300463struct vhost_attach_cgroups_struct {
Krishna Kumard47effe2011-03-01 17:06:37 +0530464 struct vhost_work work;
465 struct task_struct *owner;
466 int ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300467};
468
469static void vhost_attach_cgroups_work(struct vhost_work *work)
470{
Krishna Kumard47effe2011-03-01 17:06:37 +0530471 struct vhost_attach_cgroups_struct *s;
472
473 s = container_of(work, struct vhost_attach_cgroups_struct, work);
474 s->ret = cgroup_attach_task_all(s->owner, current);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300475}
476
477static int vhost_attach_cgroups(struct vhost_dev *dev)
478{
Krishna Kumard47effe2011-03-01 17:06:37 +0530479 struct vhost_attach_cgroups_struct attach;
480
481 attach.owner = current;
482 vhost_work_init(&attach.work, vhost_attach_cgroups_work);
483 vhost_work_queue(dev, &attach.work);
484 vhost_work_flush(dev, &attach.work);
485 return attach.ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300486}
487
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000488/* Caller should have device mutex */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300489bool vhost_dev_has_owner(struct vhost_dev *dev)
490{
491 return dev->mm;
492}
Asias He6ac1afb2013-05-06 16:38:21 +0800493EXPORT_SYMBOL_GPL(vhost_dev_has_owner);
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300494
495/* Caller should have device mutex */
Asias He54db63c2013-05-06 11:15:59 +0800496long vhost_dev_set_owner(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000497{
Tejun Heoc23f34452010-06-02 20:40:00 +0200498 struct task_struct *worker;
499 int err;
Krishna Kumard47effe2011-03-01 17:06:37 +0530500
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000501 /* Is there an owner already? */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300502 if (vhost_dev_has_owner(dev)) {
Tejun Heoc23f34452010-06-02 20:40:00 +0200503 err = -EBUSY;
504 goto err_mm;
505 }
Krishna Kumard47effe2011-03-01 17:06:37 +0530506
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000507 /* No owner, become one */
508 dev->mm = get_task_mm(current);
Tejun Heoc23f34452010-06-02 20:40:00 +0200509 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
510 if (IS_ERR(worker)) {
511 err = PTR_ERR(worker);
512 goto err_worker;
513 }
514
515 dev->worker = worker;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300516 wake_up_process(worker); /* avoid contributing to loadavg */
517
518 err = vhost_attach_cgroups(dev);
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300519 if (err)
520 goto err_cgroup;
Tejun Heoc23f34452010-06-02 20:40:00 +0200521
Jason Wange0e9b402010-09-14 23:53:05 +0800522 err = vhost_dev_alloc_iovecs(dev);
523 if (err)
524 goto err_cgroup;
525
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000526 return 0;
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300527err_cgroup:
528 kthread_stop(worker);
Michael S. Tsirkin615cc222010-09-02 14:16:36 +0300529 dev->worker = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200530err_worker:
531 if (dev->mm)
532 mmput(dev->mm);
533 dev->mm = NULL;
534err_mm:
535 return err;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000536}
Asias He6ac1afb2013-05-06 16:38:21 +0800537EXPORT_SYMBOL_GPL(vhost_dev_set_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000538
Jason Wanga9709d62016-06-23 02:04:31 -0400539struct vhost_umem *vhost_dev_reset_owner_prepare(void)
540{
Michal Hocko6c5ab652017-05-08 15:57:15 -0700541 return kvzalloc(sizeof(struct vhost_umem), GFP_KERNEL);
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300542}
Asias He6ac1afb2013-05-06 16:38:21 +0800543EXPORT_SYMBOL_GPL(vhost_dev_reset_owner_prepare);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000544
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300545/* Caller should have device mutex */
Jason Wanga9709d62016-06-23 02:04:31 -0400546void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_umem *umem)
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300547{
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300548 int i;
549
夷则(Caspar)f6f93f72017-12-25 00:08:58 +0800550 vhost_dev_cleanup(dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000551
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300552 /* Restore memory to default empty mapping. */
Jason Wanga9709d62016-06-23 02:04:31 -0400553 INIT_LIST_HEAD(&umem->umem_list);
554 dev->umem = umem;
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300555 /* We don't need VQ locks below since vhost_dev_cleanup makes sure
556 * VQs aren't running.
557 */
558 for (i = 0; i < dev->nvqs; ++i)
Jason Wanga9709d62016-06-23 02:04:31 -0400559 dev->vqs[i]->umem = umem;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000560}
Asias He6ac1afb2013-05-06 16:38:21 +0800561EXPORT_SYMBOL_GPL(vhost_dev_reset_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000562
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000563void vhost_dev_stop(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000564{
565 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530566
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000567 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800568 if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) {
569 vhost_poll_stop(&dev->vqs[i]->poll);
570 vhost_poll_flush(&dev->vqs[i]->poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000571 }
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000572 }
573}
Asias He6ac1afb2013-05-06 16:38:21 +0800574EXPORT_SYMBOL_GPL(vhost_dev_stop);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000575
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400576static void vhost_umem_free(struct vhost_umem *umem,
577 struct vhost_umem_node *node)
578{
579 vhost_umem_interval_tree_remove(node, &umem->umem_tree);
580 list_del(&node->link);
581 kfree(node);
582 umem->numem--;
583}
584
Jason Wanga9709d62016-06-23 02:04:31 -0400585static void vhost_umem_clean(struct vhost_umem *umem)
586{
587 struct vhost_umem_node *node, *tmp;
588
589 if (!umem)
590 return;
591
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400592 list_for_each_entry_safe(node, tmp, &umem->umem_list, link)
593 vhost_umem_free(umem, node);
594
Jason Wanga9709d62016-06-23 02:04:31 -0400595 kvfree(umem);
596}
597
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400598static void vhost_clear_msg(struct vhost_dev *dev)
599{
600 struct vhost_msg_node *node, *n;
601
602 spin_lock(&dev->iotlb_lock);
603
604 list_for_each_entry_safe(node, n, &dev->read_list, node) {
605 list_del(&node->node);
606 kfree(node);
607 }
608
609 list_for_each_entry_safe(node, n, &dev->pending_list, node) {
610 list_del(&node->node);
611 kfree(node);
612 }
613
614 spin_unlock(&dev->iotlb_lock);
615}
616
夷则(Caspar)f6f93f72017-12-25 00:08:58 +0800617void vhost_dev_cleanup(struct vhost_dev *dev)
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000618{
619 int i;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000620
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000621 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800622 if (dev->vqs[i]->error_ctx)
623 eventfd_ctx_put(dev->vqs[i]->error_ctx);
Asias He3ab2e422013-04-27 11:16:48 +0800624 if (dev->vqs[i]->kick)
625 fput(dev->vqs[i]->kick);
626 if (dev->vqs[i]->call_ctx)
627 eventfd_ctx_put(dev->vqs[i]->call_ctx);
Asias He3ab2e422013-04-27 11:16:48 +0800628 vhost_vq_reset(dev, dev->vqs[i]);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000629 }
Jason Wange0e9b402010-09-14 23:53:05 +0800630 vhost_dev_free_iovecs(dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000631 if (dev->log_ctx)
632 eventfd_ctx_put(dev->log_ctx);
633 dev->log_ctx = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000634 /* No one will access memory at this point */
Jason Wanga9709d62016-06-23 02:04:31 -0400635 vhost_umem_clean(dev->umem);
636 dev->umem = NULL;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400637 vhost_umem_clean(dev->iotlb);
638 dev->iotlb = NULL;
639 vhost_clear_msg(dev);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800640 wake_up_interruptible_poll(&dev->wait, EPOLLIN | EPOLLRDNORM);
Jason Wang04b96e52016-04-25 22:14:33 -0400641 WARN_ON(!llist_empty(&dev->work_list));
Eric Dumazet78b620c2010-08-31 02:05:57 +0000642 if (dev->worker) {
643 kthread_stop(dev->worker);
644 dev->worker = NULL;
645 }
Michael S. Tsirkin533a19b2010-10-06 15:34:38 +0200646 if (dev->mm)
647 mmput(dev->mm);
648 dev->mm = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000649}
Asias He6ac1afb2013-05-06 16:38:21 +0800650EXPORT_SYMBOL_GPL(vhost_dev_cleanup);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000651
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800652static bool log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000653{
654 u64 a = addr / VHOST_PAGE_SIZE / 8;
Krishna Kumard47effe2011-03-01 17:06:37 +0530655
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000656 /* Make sure 64 bit math will not overflow. */
657 if (a > ULONG_MAX - (unsigned long)log_base ||
658 a + (unsigned long)log_base > ULONG_MAX)
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800659 return false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000660
661 return access_ok(VERIFY_WRITE, log_base + a,
662 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
663}
664
Michael S. Tsirkinec33d032016-08-01 23:20:53 +0300665static bool vhost_overflow(u64 uaddr, u64 size)
666{
667 /* Make sure 64 bit math will not overflow. */
668 return uaddr > ULONG_MAX || size > ULONG_MAX || uaddr > ULONG_MAX - size;
669}
670
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000671/* Caller should have vq mutex and device mutex. */
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800672static bool vq_memory_access_ok(void __user *log_base, struct vhost_umem *umem,
673 int log_all)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000674{
Jason Wanga9709d62016-06-23 02:04:31 -0400675 struct vhost_umem_node *node;
Jeff Dike179b2842010-04-07 09:59:10 -0400676
Jason Wanga9709d62016-06-23 02:04:31 -0400677 if (!umem)
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800678 return false;
Jeff Dike179b2842010-04-07 09:59:10 -0400679
Jason Wanga9709d62016-06-23 02:04:31 -0400680 list_for_each_entry(node, &umem->umem_list, link) {
681 unsigned long a = node->userspace_addr;
682
Michael S. Tsirkinec33d032016-08-01 23:20:53 +0300683 if (vhost_overflow(node->userspace_addr, node->size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800684 return false;
Michael S. Tsirkinec33d032016-08-01 23:20:53 +0300685
686
687 if (!access_ok(VERIFY_WRITE, (void __user *)a,
Jason Wanga9709d62016-06-23 02:04:31 -0400688 node->size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800689 return false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000690 else if (log_all && !log_access_ok(log_base,
Jason Wanga9709d62016-06-23 02:04:31 -0400691 node->start,
692 node->size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800693 return false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000694 }
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800695 return true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000696}
697
Jason Wangf8894912017-02-28 17:56:02 +0800698static inline void __user *vhost_vq_meta_fetch(struct vhost_virtqueue *vq,
699 u64 addr, unsigned int size,
700 int type)
701{
702 const struct vhost_umem_node *node = vq->meta_iotlb[type];
703
704 if (!node)
705 return NULL;
706
707 return (void *)(uintptr_t)(node->userspace_addr + addr - node->start);
708}
709
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000710/* Can we switch to this memory table? */
711/* Caller should have device mutex but not vq mutex */
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800712static bool memory_access_ok(struct vhost_dev *d, struct vhost_umem *umem,
713 int log_all)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000714{
715 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530716
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000717 for (i = 0; i < d->nvqs; ++i) {
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800718 bool ok;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300719 bool log;
720
Asias He3ab2e422013-04-27 11:16:48 +0800721 mutex_lock(&d->vqs[i]->mutex);
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300722 log = log_all || vhost_has_feature(d->vqs[i], VHOST_F_LOG_ALL);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000723 /* If ring is inactive, will check when it's enabled. */
Asias He3ab2e422013-04-27 11:16:48 +0800724 if (d->vqs[i]->private_data)
Jason Wanga9709d62016-06-23 02:04:31 -0400725 ok = vq_memory_access_ok(d->vqs[i]->log_base,
726 umem, log);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000727 else
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800728 ok = true;
Asias He3ab2e422013-04-27 11:16:48 +0800729 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000730 if (!ok)
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800731 return false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000732 }
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800733 return true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000734}
735
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400736static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
737 struct iovec iov[], int iov_size, int access);
Jason Wangbfe2bc52016-06-23 02:04:30 -0400738
Michael S. Tsirkin72952cc2016-12-06 06:01:41 +0200739static int vhost_copy_to_user(struct vhost_virtqueue *vq, void __user *to,
Jason Wangbfe2bc52016-06-23 02:04:30 -0400740 const void *from, unsigned size)
741{
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400742 int ret;
Jason Wangbfe2bc52016-06-23 02:04:30 -0400743
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400744 if (!vq->iotlb)
745 return __copy_to_user(to, from, size);
746 else {
747 /* This function should be called after iotlb
748 * prefetch, which means we're sure that all vq
749 * could be access through iotlb. So -EAGAIN should
750 * not happen in this case.
751 */
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400752 struct iov_iter t;
Jason Wangf8894912017-02-28 17:56:02 +0800753 void __user *uaddr = vhost_vq_meta_fetch(vq,
754 (u64)(uintptr_t)to, size,
Eric Auger7ced6c92018-04-11 15:30:38 +0200755 VHOST_ADDR_USED);
Jason Wangf8894912017-02-28 17:56:02 +0800756
757 if (uaddr)
758 return __copy_to_user(uaddr, from, size);
759
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400760 ret = translate_desc(vq, (u64)(uintptr_t)to, size, vq->iotlb_iov,
761 ARRAY_SIZE(vq->iotlb_iov),
762 VHOST_ACCESS_WO);
763 if (ret < 0)
764 goto out;
765 iov_iter_init(&t, WRITE, vq->iotlb_iov, ret, size);
766 ret = copy_to_iter(from, size, &t);
767 if (ret == size)
768 ret = 0;
769 }
770out:
771 return ret;
772}
Jason Wangbfe2bc52016-06-23 02:04:30 -0400773
774static int vhost_copy_from_user(struct vhost_virtqueue *vq, void *to,
Michael S. Tsirkin72952cc2016-12-06 06:01:41 +0200775 void __user *from, unsigned size)
Jason Wangbfe2bc52016-06-23 02:04:30 -0400776{
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400777 int ret;
778
779 if (!vq->iotlb)
780 return __copy_from_user(to, from, size);
781 else {
782 /* This function should be called after iotlb
783 * prefetch, which means we're sure that vq
784 * could be access through iotlb. So -EAGAIN should
785 * not happen in this case.
786 */
Jason Wangf8894912017-02-28 17:56:02 +0800787 void __user *uaddr = vhost_vq_meta_fetch(vq,
788 (u64)(uintptr_t)from, size,
789 VHOST_ADDR_DESC);
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400790 struct iov_iter f;
Jason Wangf8894912017-02-28 17:56:02 +0800791
792 if (uaddr)
793 return __copy_from_user(to, uaddr, size);
794
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400795 ret = translate_desc(vq, (u64)(uintptr_t)from, size, vq->iotlb_iov,
796 ARRAY_SIZE(vq->iotlb_iov),
797 VHOST_ACCESS_RO);
798 if (ret < 0) {
799 vq_err(vq, "IOTLB translation failure: uaddr "
800 "%p size 0x%llx\n", from,
801 (unsigned long long) size);
802 goto out;
803 }
804 iov_iter_init(&f, READ, vq->iotlb_iov, ret, size);
805 ret = copy_from_iter(to, size, &f);
806 if (ret == size)
807 ret = 0;
808 }
809
810out:
811 return ret;
812}
813
Jason Wangf8894912017-02-28 17:56:02 +0800814static void __user *__vhost_get_user_slow(struct vhost_virtqueue *vq,
815 void __user *addr, unsigned int size,
816 int type)
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400817{
818 int ret;
819
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400820 ret = translate_desc(vq, (u64)(uintptr_t)addr, size, vq->iotlb_iov,
821 ARRAY_SIZE(vq->iotlb_iov),
822 VHOST_ACCESS_RO);
823 if (ret < 0) {
824 vq_err(vq, "IOTLB translation failure: uaddr "
825 "%p size 0x%llx\n", addr,
826 (unsigned long long) size);
827 return NULL;
828 }
829
830 if (ret != 1 || vq->iotlb_iov[0].iov_len != size) {
831 vq_err(vq, "Non atomic userspace memory access: uaddr "
832 "%p size 0x%llx\n", addr,
833 (unsigned long long) size);
834 return NULL;
835 }
836
837 return vq->iotlb_iov[0].iov_base;
838}
839
Jason Wangf8894912017-02-28 17:56:02 +0800840/* This function should be called after iotlb
841 * prefetch, which means we're sure that vq
842 * could be access through iotlb. So -EAGAIN should
843 * not happen in this case.
844 */
845static inline void __user *__vhost_get_user(struct vhost_virtqueue *vq,
846 void *addr, unsigned int size,
847 int type)
848{
849 void __user *uaddr = vhost_vq_meta_fetch(vq,
850 (u64)(uintptr_t)addr, size, type);
851 if (uaddr)
852 return uaddr;
853
854 return __vhost_get_user_slow(vq, addr, size, type);
855}
856
857#define vhost_put_user(vq, x, ptr) \
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400858({ \
859 int ret = -EFAULT; \
860 if (!vq->iotlb) { \
861 ret = __put_user(x, ptr); \
862 } else { \
863 __typeof__(ptr) to = \
Jason Wangf8894912017-02-28 17:56:02 +0800864 (__typeof__(ptr)) __vhost_get_user(vq, ptr, \
865 sizeof(*ptr), VHOST_ADDR_USED); \
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400866 if (to != NULL) \
867 ret = __put_user(x, to); \
868 else \
869 ret = -EFAULT; \
870 } \
871 ret; \
872})
873
Jason Wangf8894912017-02-28 17:56:02 +0800874#define vhost_get_user(vq, x, ptr, type) \
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400875({ \
876 int ret; \
877 if (!vq->iotlb) { \
878 ret = __get_user(x, ptr); \
879 } else { \
880 __typeof__(ptr) from = \
Jason Wangf8894912017-02-28 17:56:02 +0800881 (__typeof__(ptr)) __vhost_get_user(vq, ptr, \
882 sizeof(*ptr), \
883 type); \
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400884 if (from != NULL) \
885 ret = __get_user(x, from); \
886 else \
887 ret = -EFAULT; \
888 } \
889 ret; \
890})
891
Jason Wangf8894912017-02-28 17:56:02 +0800892#define vhost_get_avail(vq, x, ptr) \
893 vhost_get_user(vq, x, ptr, VHOST_ADDR_AVAIL)
894
895#define vhost_get_used(vq, x, ptr) \
896 vhost_get_user(vq, x, ptr, VHOST_ADDR_USED)
897
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400898static int vhost_new_umem_range(struct vhost_umem *umem,
899 u64 start, u64 size, u64 end,
900 u64 userspace_addr, int perm)
901{
902 struct vhost_umem_node *tmp, *node = kmalloc(sizeof(*node), GFP_ATOMIC);
903
904 if (!node)
905 return -ENOMEM;
906
907 if (umem->numem == max_iotlb_entries) {
908 tmp = list_first_entry(&umem->umem_list, typeof(*tmp), link);
909 vhost_umem_free(umem, tmp);
910 }
911
912 node->start = start;
913 node->size = size;
914 node->last = end;
915 node->userspace_addr = userspace_addr;
916 node->perm = perm;
917 INIT_LIST_HEAD(&node->link);
918 list_add_tail(&node->link, &umem->umem_list);
919 vhost_umem_interval_tree_insert(node, &umem->umem_tree);
920 umem->numem++;
921
922 return 0;
923}
924
925static void vhost_del_umem_range(struct vhost_umem *umem,
926 u64 start, u64 end)
927{
928 struct vhost_umem_node *node;
929
930 while ((node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
931 start, end)))
932 vhost_umem_free(umem, node);
933}
934
935static void vhost_iotlb_notify_vq(struct vhost_dev *d,
936 struct vhost_iotlb_msg *msg)
937{
938 struct vhost_msg_node *node, *n;
939
940 spin_lock(&d->iotlb_lock);
941
942 list_for_each_entry_safe(node, n, &d->pending_list, node) {
943 struct vhost_iotlb_msg *vq_msg = &node->msg.iotlb;
944 if (msg->iova <= vq_msg->iova &&
Jason Wang2d66f992018-08-24 16:53:13 +0800945 msg->iova + msg->size - 1 >= vq_msg->iova &&
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400946 vq_msg->type == VHOST_IOTLB_MISS) {
947 vhost_poll_queue(&node->vq->poll);
948 list_del(&node->node);
949 kfree(node);
950 }
951 }
952
953 spin_unlock(&d->iotlb_lock);
954}
955
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800956static bool umem_access_ok(u64 uaddr, u64 size, int access)
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400957{
958 unsigned long a = uaddr;
959
Michael S. Tsirkinec33d032016-08-01 23:20:53 +0300960 /* Make sure 64 bit math will not overflow. */
961 if (vhost_overflow(uaddr, size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800962 return false;
Michael S. Tsirkinec33d032016-08-01 23:20:53 +0300963
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400964 if ((access & VHOST_ACCESS_RO) &&
965 !access_ok(VERIFY_READ, (void __user *)a, size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800966 return false;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400967 if ((access & VHOST_ACCESS_WO) &&
968 !access_ok(VERIFY_WRITE, (void __user *)a, size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800969 return false;
970 return true;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400971}
972
Michael S. Tsirkin72952cc2016-12-06 06:01:41 +0200973static int vhost_process_iotlb_msg(struct vhost_dev *dev,
974 struct vhost_iotlb_msg *msg)
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400975{
976 int ret = 0;
977
Jason Wang1b15ad62018-05-22 19:58:57 +0800978 mutex_lock(&dev->mutex);
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400979 switch (msg->type) {
980 case VHOST_IOTLB_UPDATE:
981 if (!dev->iotlb) {
982 ret = -EFAULT;
983 break;
984 }
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800985 if (!umem_access_ok(msg->uaddr, msg->size, msg->perm)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400986 ret = -EFAULT;
987 break;
988 }
Jason Wangf8894912017-02-28 17:56:02 +0800989 vhost_vq_meta_reset(dev);
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400990 if (vhost_new_umem_range(dev->iotlb, msg->iova, msg->size,
991 msg->iova + msg->size - 1,
992 msg->uaddr, msg->perm)) {
993 ret = -ENOMEM;
994 break;
995 }
996 vhost_iotlb_notify_vq(dev, msg);
997 break;
998 case VHOST_IOTLB_INVALIDATE:
Jason Wang6f3180a2018-01-23 17:27:26 +0800999 if (!dev->iotlb) {
1000 ret = -EFAULT;
1001 break;
1002 }
Jason Wangf8894912017-02-28 17:56:02 +08001003 vhost_vq_meta_reset(dev);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001004 vhost_del_umem_range(dev->iotlb, msg->iova,
1005 msg->iova + msg->size - 1);
1006 break;
1007 default:
1008 ret = -EINVAL;
1009 break;
1010 }
1011
Jason Wang1b15ad62018-05-22 19:58:57 +08001012 mutex_unlock(&dev->mutex);
1013
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001014 return ret;
1015}
1016ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
1017 struct iov_iter *from)
1018{
Jason Wang429711a2018-08-06 11:17:47 +08001019 struct vhost_iotlb_msg msg;
1020 size_t offset;
1021 int type, ret;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001022
Jason Wang429711a2018-08-06 11:17:47 +08001023 ret = copy_from_iter(&type, sizeof(type), from);
1024 if (ret != sizeof(type))
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001025 goto done;
1026
Jason Wang429711a2018-08-06 11:17:47 +08001027 switch (type) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001028 case VHOST_IOTLB_MSG:
Jason Wang429711a2018-08-06 11:17:47 +08001029 /* There maybe a hole after type for V1 message type,
1030 * so skip it here.
1031 */
1032 offset = offsetof(struct vhost_msg, iotlb) - sizeof(int);
1033 break;
1034 case VHOST_IOTLB_MSG_V2:
1035 offset = sizeof(__u32);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001036 break;
1037 default:
1038 ret = -EINVAL;
Jason Wang429711a2018-08-06 11:17:47 +08001039 goto done;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001040 }
1041
Jason Wang429711a2018-08-06 11:17:47 +08001042 iov_iter_advance(from, offset);
1043 ret = copy_from_iter(&msg, sizeof(msg), from);
1044 if (ret != sizeof(msg))
1045 goto done;
1046 if (vhost_process_iotlb_msg(dev, &msg)) {
1047 ret = -EFAULT;
1048 goto done;
1049 }
1050
1051 ret = (type == VHOST_IOTLB_MSG) ? sizeof(struct vhost_msg) :
1052 sizeof(struct vhost_msg_v2);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001053done:
1054 return ret;
1055}
1056EXPORT_SYMBOL(vhost_chr_write_iter);
1057
Al Viroafc9a422017-07-03 06:39:46 -04001058__poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001059 poll_table *wait)
1060{
Al Viroafc9a422017-07-03 06:39:46 -04001061 __poll_t mask = 0;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001062
1063 poll_wait(file, &dev->wait, wait);
1064
1065 if (!list_empty(&dev->read_list))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001066 mask |= EPOLLIN | EPOLLRDNORM;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001067
1068 return mask;
1069}
1070EXPORT_SYMBOL(vhost_chr_poll);
1071
1072ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to,
1073 int noblock)
1074{
1075 DEFINE_WAIT(wait);
1076 struct vhost_msg_node *node;
1077 ssize_t ret = 0;
1078 unsigned size = sizeof(struct vhost_msg);
1079
1080 if (iov_iter_count(to) < size)
1081 return 0;
1082
1083 while (1) {
1084 if (!noblock)
1085 prepare_to_wait(&dev->wait, &wait,
1086 TASK_INTERRUPTIBLE);
1087
1088 node = vhost_dequeue_msg(dev, &dev->read_list);
1089 if (node)
1090 break;
1091 if (noblock) {
1092 ret = -EAGAIN;
1093 break;
1094 }
1095 if (signal_pending(current)) {
1096 ret = -ERESTARTSYS;
1097 break;
1098 }
1099 if (!dev->iotlb) {
1100 ret = -EBADFD;
1101 break;
1102 }
1103
1104 schedule();
1105 }
1106
1107 if (!noblock)
1108 finish_wait(&dev->wait, &wait);
1109
1110 if (node) {
Jason Wang429711a2018-08-06 11:17:47 +08001111 struct vhost_iotlb_msg *msg;
1112 void *start = &node->msg;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001113
Jason Wang429711a2018-08-06 11:17:47 +08001114 switch (node->msg.type) {
1115 case VHOST_IOTLB_MSG:
1116 size = sizeof(node->msg);
1117 msg = &node->msg.iotlb;
1118 break;
1119 case VHOST_IOTLB_MSG_V2:
1120 size = sizeof(node->msg_v2);
1121 msg = &node->msg_v2.iotlb;
1122 break;
1123 default:
1124 BUG();
1125 break;
1126 }
1127
1128 ret = copy_to_iter(start, size, to);
1129 if (ret != size || msg->type != VHOST_IOTLB_MISS) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001130 kfree(node);
1131 return ret;
1132 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001133 vhost_enqueue_msg(dev, &dev->pending_list, node);
1134 }
1135
1136 return ret;
1137}
1138EXPORT_SYMBOL_GPL(vhost_chr_read_iter);
1139
1140static int vhost_iotlb_miss(struct vhost_virtqueue *vq, u64 iova, int access)
1141{
1142 struct vhost_dev *dev = vq->dev;
1143 struct vhost_msg_node *node;
1144 struct vhost_iotlb_msg *msg;
Jason Wang429711a2018-08-06 11:17:47 +08001145 bool v2 = vhost_backend_has_feature(vq, VHOST_BACKEND_F_IOTLB_MSG_V2);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001146
Jason Wang429711a2018-08-06 11:17:47 +08001147 node = vhost_new_msg(vq, v2 ? VHOST_IOTLB_MSG_V2 : VHOST_IOTLB_MSG);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001148 if (!node)
1149 return -ENOMEM;
1150
Jason Wang429711a2018-08-06 11:17:47 +08001151 if (v2) {
1152 node->msg_v2.type = VHOST_IOTLB_MSG_V2;
1153 msg = &node->msg_v2.iotlb;
1154 } else {
1155 msg = &node->msg.iotlb;
1156 }
1157
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001158 msg->type = VHOST_IOTLB_MISS;
1159 msg->iova = iova;
1160 msg->perm = access;
1161
1162 vhost_enqueue_msg(dev, &dev->read_list, node);
1163
1164 return 0;
Jason Wangbfe2bc52016-06-23 02:04:30 -04001165}
1166
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001167static bool vq_access_ok(struct vhost_virtqueue *vq, unsigned int num,
1168 struct vring_desc __user *desc,
1169 struct vring_avail __user *avail,
1170 struct vring_used __user *used)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001171
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001172{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001173 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001174
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001175 return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
1176 access_ok(VERIFY_READ, avail,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001177 sizeof *avail + num * sizeof *avail->ring + s) &&
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001178 access_ok(VERIFY_WRITE, used,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001179 sizeof *used + num * sizeof *used->ring + s);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001180}
1181
Jason Wangf8894912017-02-28 17:56:02 +08001182static void vhost_vq_meta_update(struct vhost_virtqueue *vq,
1183 const struct vhost_umem_node *node,
1184 int type)
1185{
1186 int access = (type == VHOST_ADDR_USED) ?
1187 VHOST_ACCESS_WO : VHOST_ACCESS_RO;
1188
1189 if (likely(node->perm & access))
1190 vq->meta_iotlb[type] = node;
1191}
1192
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001193static bool iotlb_access_ok(struct vhost_virtqueue *vq,
1194 int access, u64 addr, u64 len, int type)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001195{
1196 const struct vhost_umem_node *node;
1197 struct vhost_umem *umem = vq->iotlb;
Michael S. Tsirkinca2c5b32017-08-21 22:33:33 +03001198 u64 s = 0, size, orig_addr = addr, last = addr + len - 1;
Jason Wangf8894912017-02-28 17:56:02 +08001199
1200 if (vhost_vq_meta_fetch(vq, addr, len, type))
1201 return true;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001202
1203 while (len > s) {
1204 node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
1205 addr,
Michael S. Tsirkinca2c5b32017-08-21 22:33:33 +03001206 last);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001207 if (node == NULL || node->start > addr) {
1208 vhost_iotlb_miss(vq, addr, access);
1209 return false;
1210 } else if (!(node->perm & access)) {
1211 /* Report the possible access violation by
1212 * request another translation from userspace.
1213 */
1214 return false;
1215 }
1216
1217 size = node->size - addr + node->start;
Jason Wangf8894912017-02-28 17:56:02 +08001218
1219 if (orig_addr == addr && size >= len)
1220 vhost_vq_meta_update(vq, node, type);
1221
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001222 s += size;
1223 addr += size;
1224 }
1225
1226 return true;
1227}
1228
1229int vq_iotlb_prefetch(struct vhost_virtqueue *vq)
1230{
1231 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
1232 unsigned int num = vq->num;
1233
1234 if (!vq->iotlb)
1235 return 1;
1236
1237 return iotlb_access_ok(vq, VHOST_ACCESS_RO, (u64)(uintptr_t)vq->desc,
Jason Wangf8894912017-02-28 17:56:02 +08001238 num * sizeof(*vq->desc), VHOST_ADDR_DESC) &&
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001239 iotlb_access_ok(vq, VHOST_ACCESS_RO, (u64)(uintptr_t)vq->avail,
1240 sizeof *vq->avail +
Jason Wangf8894912017-02-28 17:56:02 +08001241 num * sizeof(*vq->avail->ring) + s,
1242 VHOST_ADDR_AVAIL) &&
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001243 iotlb_access_ok(vq, VHOST_ACCESS_WO, (u64)(uintptr_t)vq->used,
1244 sizeof *vq->used +
Jason Wangf8894912017-02-28 17:56:02 +08001245 num * sizeof(*vq->used->ring) + s,
1246 VHOST_ADDR_USED);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001247}
1248EXPORT_SYMBOL_GPL(vq_iotlb_prefetch);
1249
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001250/* Can we log writes? */
1251/* Caller should have device mutex but not vq mutex */
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001252bool vhost_log_access_ok(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001253{
Jason Wanga9709d62016-06-23 02:04:31 -04001254 return memory_access_ok(dev, dev->umem, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001255}
Asias He6ac1afb2013-05-06 16:38:21 +08001256EXPORT_SYMBOL_GPL(vhost_log_access_ok);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001257
1258/* Verify access for write logging. */
1259/* Caller should have vq mutex and device mutex */
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001260static bool vq_log_access_ok(struct vhost_virtqueue *vq,
1261 void __user *log_base)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001262{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001263 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
Arnd Bergmann28457ee2010-03-09 19:24:45 +01001264
Jason Wanga9709d62016-06-23 02:04:31 -04001265 return vq_memory_access_ok(log_base, vq->umem,
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001266 vhost_has_feature(vq, VHOST_F_LOG_ALL)) &&
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001267 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
1268 sizeof *vq->used +
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001269 vq->num * sizeof *vq->used->ring + s));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001270}
1271
1272/* Can we start vq? */
1273/* Caller should have vq mutex and device mutex */
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001274bool vhost_vq_access_ok(struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001275{
Stefan Hajnoczid14d2b72018-04-11 10:35:40 +08001276 if (!vq_log_access_ok(vq, vq->log_base))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001277 return false;
Jason Wangd65026c2018-03-29 16:00:04 +08001278
Stefan Hajnoczid14d2b72018-04-11 10:35:40 +08001279 /* Access validation occurs at prefetch time with IOTLB */
1280 if (vq->iotlb)
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001281 return true;
Jason Wangd65026c2018-03-29 16:00:04 +08001282
1283 return vq_access_ok(vq, vq->num, vq->desc, vq->avail, vq->used);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001284}
Asias He6ac1afb2013-05-06 16:38:21 +08001285EXPORT_SYMBOL_GPL(vhost_vq_access_ok);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001286
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001287static struct vhost_umem *vhost_umem_alloc(void)
1288{
Michal Hocko6c5ab652017-05-08 15:57:15 -07001289 struct vhost_umem *umem = kvzalloc(sizeof(*umem), GFP_KERNEL);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001290
1291 if (!umem)
1292 return NULL;
1293
Davidlohr Buesof808c132017-09-08 16:15:08 -07001294 umem->umem_tree = RB_ROOT_CACHED;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001295 umem->numem = 0;
1296 INIT_LIST_HEAD(&umem->umem_list);
1297
1298 return umem;
1299}
1300
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001301static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
1302{
Jason Wanga9709d62016-06-23 02:04:31 -04001303 struct vhost_memory mem, *newmem;
1304 struct vhost_memory_region *region;
Jason Wanga9709d62016-06-23 02:04:31 -04001305 struct vhost_umem *newumem, *oldumem;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001306 unsigned long size = offsetof(struct vhost_memory, regions);
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001307 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +05301308
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001309 if (copy_from_user(&mem, m, size))
1310 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001311 if (mem.padding)
1312 return -EOPNOTSUPP;
Igor Mammedovc9ce42f2015-07-02 15:08:11 +02001313 if (mem.nregions > max_mem_regions)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001314 return -E2BIG;
Matthew Wilcoxb2303d72018-06-07 07:57:18 -07001315 newmem = kvzalloc(struct_size(newmem, regions, mem.nregions),
1316 GFP_KERNEL);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001317 if (!newmem)
1318 return -ENOMEM;
1319
1320 memcpy(newmem, &mem, size);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001321 if (copy_from_user(newmem->regions, m->regions,
1322 mem.nregions * sizeof *m->regions)) {
Igor Mammedovbcfeaca2015-06-16 18:33:35 +02001323 kvfree(newmem);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001324 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001325 }
1326
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001327 newumem = vhost_umem_alloc();
Jason Wanga9709d62016-06-23 02:04:31 -04001328 if (!newumem) {
Igor Mammedov4de72552015-07-01 11:07:09 +02001329 kvfree(newmem);
Jason Wanga9709d62016-06-23 02:04:31 -04001330 return -ENOMEM;
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +09001331 }
Jason Wanga9709d62016-06-23 02:04:31 -04001332
Jason Wanga9709d62016-06-23 02:04:31 -04001333 for (region = newmem->regions;
1334 region < newmem->regions + mem.nregions;
1335 region++) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001336 if (vhost_new_umem_range(newumem,
1337 region->guest_phys_addr,
1338 region->memory_size,
1339 region->guest_phys_addr +
1340 region->memory_size - 1,
1341 region->userspace_addr,
1342 VHOST_ACCESS_RW))
Jason Wanga9709d62016-06-23 02:04:31 -04001343 goto err;
Jason Wanga9709d62016-06-23 02:04:31 -04001344 }
1345
1346 if (!memory_access_ok(d, newumem, 0))
1347 goto err;
1348
1349 oldumem = d->umem;
1350 d->umem = newumem;
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001351
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001352 /* All memory accesses are done under some VQ mutex. */
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001353 for (i = 0; i < d->nvqs; ++i) {
1354 mutex_lock(&d->vqs[i]->mutex);
Jason Wanga9709d62016-06-23 02:04:31 -04001355 d->vqs[i]->umem = newumem;
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001356 mutex_unlock(&d->vqs[i]->mutex);
1357 }
Jason Wanga9709d62016-06-23 02:04:31 -04001358
1359 kvfree(newmem);
1360 vhost_umem_clean(oldumem);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001361 return 0;
Jason Wanga9709d62016-06-23 02:04:31 -04001362
1363err:
1364 vhost_umem_clean(newumem);
1365 kvfree(newmem);
1366 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001367}
1368
Sonny Rao26b36602018-03-14 10:05:06 -07001369long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001370{
Al Virocecb46f2012-08-27 14:21:39 -04001371 struct file *eventfp, *filep = NULL;
1372 bool pollstart = false, pollstop = false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001373 struct eventfd_ctx *ctx = NULL;
1374 u32 __user *idxp = argp;
1375 struct vhost_virtqueue *vq;
1376 struct vhost_vring_state s;
1377 struct vhost_vring_file f;
1378 struct vhost_vring_addr a;
1379 u32 idx;
1380 long r;
1381
1382 r = get_user(idx, idxp);
1383 if (r < 0)
1384 return r;
Krishna Kumar0f3d9a12010-05-25 11:10:36 +05301385 if (idx >= d->nvqs)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001386 return -ENOBUFS;
1387
Jason Wangff002262018-10-30 14:10:49 +08001388 idx = array_index_nospec(idx, d->nvqs);
Asias He3ab2e422013-04-27 11:16:48 +08001389 vq = d->vqs[idx];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001390
1391 mutex_lock(&vq->mutex);
1392
1393 switch (ioctl) {
1394 case VHOST_SET_VRING_NUM:
1395 /* Resizing ring with an active backend?
1396 * You don't want to do that. */
1397 if (vq->private_data) {
1398 r = -EBUSY;
1399 break;
1400 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001401 if (copy_from_user(&s, argp, sizeof s)) {
1402 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001403 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001404 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001405 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
1406 r = -EINVAL;
1407 break;
1408 }
1409 vq->num = s.num;
1410 break;
1411 case VHOST_SET_VRING_BASE:
1412 /* Moving base with an active backend?
1413 * You don't want to do that. */
1414 if (vq->private_data) {
1415 r = -EBUSY;
1416 break;
1417 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001418 if (copy_from_user(&s, argp, sizeof s)) {
1419 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001420 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001421 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001422 if (s.num > 0xffff) {
1423 r = -EINVAL;
1424 break;
1425 }
Jason Wang8d658432017-07-27 11:22:05 +08001426 vq->last_avail_idx = s.num;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001427 /* Forget the cached index value. */
1428 vq->avail_idx = vq->last_avail_idx;
1429 break;
1430 case VHOST_GET_VRING_BASE:
1431 s.index = idx;
1432 s.num = vq->last_avail_idx;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001433 if (copy_to_user(argp, &s, sizeof s))
1434 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001435 break;
1436 case VHOST_SET_VRING_ADDR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001437 if (copy_from_user(&a, argp, sizeof a)) {
1438 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001439 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001440 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001441 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
1442 r = -EOPNOTSUPP;
1443 break;
1444 }
1445 /* For 32bit, verify that the top 32bits of the user
1446 data are set to zero. */
1447 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
1448 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
1449 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
1450 r = -EFAULT;
1451 break;
1452 }
Michael S. Tsirkin5d9a07b2014-12-21 01:00:23 +02001453
1454 /* Make sure it's safe to cast pointers to vring types. */
1455 BUILD_BUG_ON(__alignof__ *vq->avail > VRING_AVAIL_ALIGN_SIZE);
1456 BUILD_BUG_ON(__alignof__ *vq->used > VRING_USED_ALIGN_SIZE);
1457 if ((a.avail_user_addr & (VRING_AVAIL_ALIGN_SIZE - 1)) ||
1458 (a.used_user_addr & (VRING_USED_ALIGN_SIZE - 1)) ||
Michael S. Tsirkind5424832015-11-16 16:57:08 +02001459 (a.log_guest_addr & (VRING_USED_ALIGN_SIZE - 1))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001460 r = -EINVAL;
1461 break;
1462 }
1463
1464 /* We only verify access here if backend is configured.
1465 * If it is not, we don't as size might not have been setup.
1466 * We will verify when backend is configured. */
1467 if (vq->private_data) {
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001468 if (!vq_access_ok(vq, vq->num,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001469 (void __user *)(unsigned long)a.desc_user_addr,
1470 (void __user *)(unsigned long)a.avail_user_addr,
1471 (void __user *)(unsigned long)a.used_user_addr)) {
1472 r = -EINVAL;
1473 break;
1474 }
1475
1476 /* Also validate log access for used ring if enabled. */
1477 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
1478 !log_access_ok(vq->log_base, a.log_guest_addr,
1479 sizeof *vq->used +
1480 vq->num * sizeof *vq->used->ring)) {
1481 r = -EINVAL;
1482 break;
1483 }
1484 }
1485
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001486 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
1487 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
1488 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
1489 vq->log_addr = a.log_guest_addr;
1490 vq->used = (void __user *)(unsigned long)a.used_user_addr;
1491 break;
1492 case VHOST_SET_VRING_KICK:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001493 if (copy_from_user(&f, argp, sizeof f)) {
1494 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001495 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001496 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001497 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +02001498 if (IS_ERR(eventfp)) {
1499 r = PTR_ERR(eventfp);
1500 break;
1501 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001502 if (eventfp != vq->kick) {
Al Virocecb46f2012-08-27 14:21:39 -04001503 pollstop = (filep = vq->kick) != NULL;
1504 pollstart = (vq->kick = eventfp) != NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001505 } else
1506 filep = eventfp;
1507 break;
1508 case VHOST_SET_VRING_CALL:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001509 if (copy_from_user(&f, argp, sizeof f)) {
1510 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001511 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001512 }
Eric Biggerse050c7d2018-01-06 14:52:19 -08001513 ctx = f.fd == -1 ? NULL : eventfd_ctx_fdget(f.fd);
1514 if (IS_ERR(ctx)) {
1515 r = PTR_ERR(ctx);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +02001516 break;
1517 }
Eric Biggerse050c7d2018-01-06 14:52:19 -08001518 swap(ctx, vq->call_ctx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001519 break;
1520 case VHOST_SET_VRING_ERR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001521 if (copy_from_user(&f, argp, sizeof f)) {
1522 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001523 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001524 }
Eric Biggers09f332a2018-01-06 14:52:20 -08001525 ctx = f.fd == -1 ? NULL : eventfd_ctx_fdget(f.fd);
1526 if (IS_ERR(ctx)) {
1527 r = PTR_ERR(ctx);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +02001528 break;
1529 }
Eric Biggers09f332a2018-01-06 14:52:20 -08001530 swap(ctx, vq->error_ctx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001531 break;
Greg Kurz2751c982015-04-24 14:27:24 +02001532 case VHOST_SET_VRING_ENDIAN:
1533 r = vhost_set_vring_endian(vq, argp);
1534 break;
1535 case VHOST_GET_VRING_ENDIAN:
1536 r = vhost_get_vring_endian(vq, idx, argp);
1537 break;
Jason Wang03088132016-03-04 06:24:53 -05001538 case VHOST_SET_VRING_BUSYLOOP_TIMEOUT:
1539 if (copy_from_user(&s, argp, sizeof(s))) {
1540 r = -EFAULT;
1541 break;
1542 }
1543 vq->busyloop_timeout = s.num;
1544 break;
1545 case VHOST_GET_VRING_BUSYLOOP_TIMEOUT:
1546 s.index = idx;
1547 s.num = vq->busyloop_timeout;
1548 if (copy_to_user(argp, &s, sizeof(s)))
1549 r = -EFAULT;
1550 break;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001551 default:
1552 r = -ENOIOCTLCMD;
1553 }
1554
1555 if (pollstop && vq->handle_kick)
1556 vhost_poll_stop(&vq->poll);
1557
Eric Biggerse050c7d2018-01-06 14:52:19 -08001558 if (!IS_ERR_OR_NULL(ctx))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001559 eventfd_ctx_put(ctx);
1560 if (filep)
1561 fput(filep);
1562
1563 if (pollstart && vq->handle_kick)
Jason Wang2b8b3282013-01-28 01:05:18 +00001564 r = vhost_poll_start(&vq->poll, vq->kick);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001565
1566 mutex_unlock(&vq->mutex);
1567
1568 if (pollstop && vq->handle_kick)
1569 vhost_poll_flush(&vq->poll);
1570 return r;
1571}
Asias He6ac1afb2013-05-06 16:38:21 +08001572EXPORT_SYMBOL_GPL(vhost_vring_ioctl);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001573
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001574int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled)
1575{
1576 struct vhost_umem *niotlb, *oiotlb;
1577 int i;
1578
1579 niotlb = vhost_umem_alloc();
1580 if (!niotlb)
1581 return -ENOMEM;
1582
1583 oiotlb = d->iotlb;
1584 d->iotlb = niotlb;
1585
1586 for (i = 0; i < d->nvqs; ++i) {
Jason Wangb13f9c62018-08-08 11:43:04 +08001587 struct vhost_virtqueue *vq = d->vqs[i];
1588
1589 mutex_lock(&vq->mutex);
1590 vq->iotlb = niotlb;
1591 __vhost_vq_meta_reset(vq);
1592 mutex_unlock(&vq->mutex);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001593 }
1594
1595 vhost_umem_clean(oiotlb);
1596
1597 return 0;
1598}
1599EXPORT_SYMBOL_GPL(vhost_init_device_iotlb);
1600
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001601/* Caller must have device mutex */
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001602long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001603{
Eric Biggersd25cc432018-01-06 14:52:21 -08001604 struct eventfd_ctx *ctx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001605 u64 p;
1606 long r;
1607 int i, fd;
1608
1609 /* If you are not the owner, you can become one */
1610 if (ioctl == VHOST_SET_OWNER) {
1611 r = vhost_dev_set_owner(d);
1612 goto done;
1613 }
1614
1615 /* You must be the owner to do anything else */
1616 r = vhost_dev_check_owner(d);
1617 if (r)
1618 goto done;
1619
1620 switch (ioctl) {
1621 case VHOST_SET_MEM_TABLE:
1622 r = vhost_set_memory(d, argp);
1623 break;
1624 case VHOST_SET_LOG_BASE:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001625 if (copy_from_user(&p, argp, sizeof p)) {
1626 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001627 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001628 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001629 if ((u64)(unsigned long)p != p) {
1630 r = -EFAULT;
1631 break;
1632 }
1633 for (i = 0; i < d->nvqs; ++i) {
1634 struct vhost_virtqueue *vq;
1635 void __user *base = (void __user *)(unsigned long)p;
Asias He3ab2e422013-04-27 11:16:48 +08001636 vq = d->vqs[i];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001637 mutex_lock(&vq->mutex);
1638 /* If ring is inactive, will check when it's enabled. */
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001639 if (vq->private_data && !vq_log_access_ok(vq, base))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001640 r = -EFAULT;
1641 else
1642 vq->log_base = base;
1643 mutex_unlock(&vq->mutex);
1644 }
1645 break;
1646 case VHOST_SET_LOG_FD:
1647 r = get_user(fd, (int __user *)argp);
1648 if (r < 0)
1649 break;
Eric Biggersd25cc432018-01-06 14:52:21 -08001650 ctx = fd == -1 ? NULL : eventfd_ctx_fdget(fd);
1651 if (IS_ERR(ctx)) {
1652 r = PTR_ERR(ctx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001653 break;
1654 }
Eric Biggersd25cc432018-01-06 14:52:21 -08001655 swap(ctx, d->log_ctx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001656 for (i = 0; i < d->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +08001657 mutex_lock(&d->vqs[i]->mutex);
1658 d->vqs[i]->log_ctx = d->log_ctx;
1659 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001660 }
1661 if (ctx)
1662 eventfd_ctx_put(ctx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001663 break;
1664 default:
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001665 r = -ENOIOCTLCMD;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001666 break;
1667 }
1668done:
1669 return r;
1670}
Asias He6ac1afb2013-05-06 16:38:21 +08001671EXPORT_SYMBOL_GPL(vhost_dev_ioctl);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001672
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001673/* TODO: This is really inefficient. We need something like get_user()
1674 * (instruction directly accesses the data, with an exception table entry
1675 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
1676 */
1677static int set_bit_to_user(int nr, void __user *addr)
1678{
1679 unsigned long log = (unsigned long)addr;
1680 struct page *page;
1681 void *base;
1682 int bit = nr + (log % PAGE_SIZE) * 8;
1683 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301684
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001685 r = get_user_pages_fast(log, 1, 1, &page);
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +02001686 if (r < 0)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001687 return r;
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +02001688 BUG_ON(r != 1);
Cong Wangc6daa7f2011-11-25 23:14:26 +08001689 base = kmap_atomic(page);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001690 set_bit(bit, base);
Cong Wangc6daa7f2011-11-25 23:14:26 +08001691 kunmap_atomic(base);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001692 set_page_dirty_lock(page);
1693 put_page(page);
1694 return 0;
1695}
1696
1697static int log_write(void __user *log_base,
1698 u64 write_address, u64 write_length)
1699{
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001700 u64 write_page = write_address / VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001701 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301702
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001703 if (!write_length)
1704 return 0;
Michael S. Tsirkin3bf9be42010-11-29 10:19:07 +02001705 write_length += write_address % VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001706 for (;;) {
1707 u64 base = (u64)(unsigned long)log_base;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001708 u64 log = base + write_page / 8;
1709 int bit = write_page % 8;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001710 if ((u64)(unsigned long)log != log)
1711 return -EFAULT;
1712 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
1713 if (r < 0)
1714 return r;
1715 if (write_length <= VHOST_PAGE_SIZE)
1716 break;
1717 write_length -= VHOST_PAGE_SIZE;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001718 write_page += 1;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001719 }
1720 return r;
1721}
1722
1723int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
1724 unsigned int log_num, u64 len)
1725{
1726 int i, r;
1727
1728 /* Make sure data written is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001729 smp_wmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001730 for (i = 0; i < log_num; ++i) {
1731 u64 l = min(log[i].len, len);
1732 r = log_write(vq->log_base, log[i].addr, l);
1733 if (r < 0)
1734 return r;
1735 len -= l;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +02001736 if (!len) {
1737 if (vq->log_ctx)
1738 eventfd_signal(vq->log_ctx, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001739 return 0;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +02001740 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001741 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001742 /* Length written exceeds what we have stored. This is a bug. */
1743 BUG();
1744 return 0;
1745}
Asias He6ac1afb2013-05-06 16:38:21 +08001746EXPORT_SYMBOL_GPL(vhost_log_write);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001747
Jason Wang2723fea2011-06-21 18:04:38 +08001748static int vhost_update_used_flags(struct vhost_virtqueue *vq)
1749{
1750 void __user *used;
Jason Wangbfe2bc52016-06-23 02:04:30 -04001751 if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->used_flags),
1752 &vq->used->flags) < 0)
Jason Wang2723fea2011-06-21 18:04:38 +08001753 return -EFAULT;
1754 if (unlikely(vq->log_used)) {
1755 /* Make sure the flag is seen before log. */
1756 smp_wmb();
1757 /* Log used flag write. */
1758 used = &vq->used->flags;
1759 log_write(vq->log_base, vq->log_addr +
1760 (used - (void __user *)vq->used),
1761 sizeof vq->used->flags);
1762 if (vq->log_ctx)
1763 eventfd_signal(vq->log_ctx, 1);
1764 }
1765 return 0;
1766}
1767
1768static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
1769{
Jason Wangbfe2bc52016-06-23 02:04:30 -04001770 if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->avail_idx),
1771 vhost_avail_event(vq)))
Jason Wang2723fea2011-06-21 18:04:38 +08001772 return -EFAULT;
1773 if (unlikely(vq->log_used)) {
1774 void __user *used;
1775 /* Make sure the event is seen before log. */
1776 smp_wmb();
1777 /* Log avail event write */
1778 used = vhost_avail_event(vq);
1779 log_write(vq->log_base, vq->log_addr +
1780 (used - (void __user *)vq->used),
1781 sizeof *vhost_avail_event(vq));
1782 if (vq->log_ctx)
1783 eventfd_signal(vq->log_ctx, 1);
1784 }
1785 return 0;
1786}
1787
Greg Kurz80f7d032016-02-16 15:59:44 +01001788int vhost_vq_init_access(struct vhost_virtqueue *vq)
Jason Wang2723fea2011-06-21 18:04:38 +08001789{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001790 __virtio16 last_used_idx;
Jason Wang2723fea2011-06-21 18:04:38 +08001791 int r;
Greg Kurze1f33be2016-02-16 15:54:28 +01001792 bool is_le = vq->is_le;
1793
Halil Pasiccda8bba2017-01-30 11:09:36 +01001794 if (!vq->private_data)
Jason Wang2723fea2011-06-21 18:04:38 +08001795 return 0;
Greg Kurz2751c982015-04-24 14:27:24 +02001796
1797 vhost_init_is_le(vq);
Jason Wang2723fea2011-06-21 18:04:38 +08001798
1799 r = vhost_update_used_flags(vq);
1800 if (r)
Greg Kurze1f33be2016-02-16 15:54:28 +01001801 goto err;
Jason Wang2723fea2011-06-21 18:04:38 +08001802 vq->signalled_used_valid = false;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001803 if (!vq->iotlb &&
1804 !access_ok(VERIFY_READ, &vq->used->idx, sizeof vq->used->idx)) {
Greg Kurze1f33be2016-02-16 15:54:28 +01001805 r = -EFAULT;
1806 goto err;
1807 }
Jason Wangf8894912017-02-28 17:56:02 +08001808 r = vhost_get_used(vq, last_used_idx, &vq->used->idx);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001809 if (r) {
1810 vq_err(vq, "Can't access used idx at %p\n",
1811 &vq->used->idx);
Greg Kurze1f33be2016-02-16 15:54:28 +01001812 goto err;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001813 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001814 vq->last_used_idx = vhost16_to_cpu(vq, last_used_idx);
Michael S. Tsirkin64f7f052014-12-01 17:39:39 +02001815 return 0;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001816
Greg Kurze1f33be2016-02-16 15:54:28 +01001817err:
1818 vq->is_le = is_le;
1819 return r;
Jason Wang2723fea2011-06-21 18:04:38 +08001820}
Greg Kurz80f7d032016-02-16 15:59:44 +01001821EXPORT_SYMBOL_GPL(vhost_vq_init_access);
Jason Wang2723fea2011-06-21 18:04:38 +08001822
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001823static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001824 struct iovec iov[], int iov_size, int access)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001825{
Jason Wanga9709d62016-06-23 02:04:31 -04001826 const struct vhost_umem_node *node;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001827 struct vhost_dev *dev = vq->dev;
1828 struct vhost_umem *umem = dev->iotlb ? dev->iotlb : dev->umem;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001829 struct iovec *_iov;
1830 u64 s = 0;
1831 int ret = 0;
1832
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001833 while ((u64)len > s) {
1834 u64 size;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001835 if (unlikely(ret >= iov_size)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001836 ret = -ENOBUFS;
1837 break;
1838 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001839
Jason Wanga9709d62016-06-23 02:04:31 -04001840 node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
1841 addr, addr + len - 1);
1842 if (node == NULL || node->start > addr) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001843 if (umem != dev->iotlb) {
1844 ret = -EFAULT;
1845 break;
1846 }
1847 ret = -EAGAIN;
1848 break;
1849 } else if (!(node->perm & access)) {
1850 ret = -EPERM;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001851 break;
1852 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001853
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001854 _iov = iov + ret;
Jason Wanga9709d62016-06-23 02:04:31 -04001855 size = node->size - addr + node->start;
Michael S. Tsirkinbd971202012-11-26 05:57:27 +00001856 _iov->iov_len = min((u64)len - s, size);
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001857 _iov->iov_base = (void __user *)(unsigned long)
Jason Wanga9709d62016-06-23 02:04:31 -04001858 (node->userspace_addr + addr - node->start);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001859 s += size;
1860 addr += size;
1861 ++ret;
1862 }
1863
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001864 if (ret == -EAGAIN)
1865 vhost_iotlb_miss(vq, addr, access);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001866 return ret;
1867}
1868
1869/* Each buffer in the virtqueues is actually a chain of descriptors. This
1870 * function returns the next descriptor in the chain,
1871 * or -1U if we're at the end. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001872static unsigned next_desc(struct vhost_virtqueue *vq, struct vring_desc *desc)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001873{
1874 unsigned int next;
1875
1876 /* If this descriptor says it doesn't chain, we're done. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001877 if (!(desc->flags & cpu_to_vhost16(vq, VRING_DESC_F_NEXT)))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001878 return -1U;
1879
1880 /* Check they're not leading us off end of descriptors. */
Paul E. McKenney3a5db0b2017-11-27 09:45:10 -08001881 next = vhost16_to_cpu(vq, READ_ONCE(desc->next));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001882 return next;
1883}
1884
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001885static int get_indirect(struct vhost_virtqueue *vq,
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001886 struct iovec iov[], unsigned int iov_size,
1887 unsigned int *out_num, unsigned int *in_num,
1888 struct vhost_log *log, unsigned int *log_num,
1889 struct vring_desc *indirect)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001890{
1891 struct vring_desc desc;
1892 unsigned int i = 0, count, found = 0;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001893 u32 len = vhost32_to_cpu(vq, indirect->len);
Al Viroaad9a1c2014-12-10 14:49:01 -05001894 struct iov_iter from;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001895 int ret, access;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001896
1897 /* Sanity check */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001898 if (unlikely(len % sizeof desc)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001899 vq_err(vq, "Invalid length in indirect descriptor: "
1900 "len 0x%llx not multiple of 0x%zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001901 (unsigned long long)len,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001902 sizeof desc);
1903 return -EINVAL;
1904 }
1905
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001906 ret = translate_desc(vq, vhost64_to_cpu(vq, indirect->addr), len, vq->indirect,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001907 UIO_MAXIOV, VHOST_ACCESS_RO);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001908 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001909 if (ret != -EAGAIN)
1910 vq_err(vq, "Translation failure %d in indirect.\n", ret);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001911 return ret;
1912 }
Al Viroaad9a1c2014-12-10 14:49:01 -05001913 iov_iter_init(&from, READ, vq->indirect, ret, len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001914
1915 /* We will use the result as an address to read from, so most
1916 * architectures only need a compiler barrier here. */
1917 read_barrier_depends();
1918
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001919 count = len / sizeof desc;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001920 /* Buffers are chained via a 16 bit next field, so
1921 * we can have at most 2^16 of these. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001922 if (unlikely(count > USHRT_MAX + 1)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001923 vq_err(vq, "Indirect buffer length too big: %d\n",
1924 indirect->len);
1925 return -E2BIG;
1926 }
1927
1928 do {
1929 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001930 if (unlikely(++found > count)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001931 vq_err(vq, "Loop detected: last one at %u "
1932 "indirect size %u\n",
1933 i, count);
1934 return -EINVAL;
1935 }
Al Virocbbd26b2016-11-01 22:09:04 -04001936 if (unlikely(!copy_from_iter_full(&desc, sizeof(desc), &from))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001937 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001938 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001939 return -EINVAL;
1940 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001941 if (unlikely(desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001942 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001943 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001944 return -EINVAL;
1945 }
1946
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001947 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE))
1948 access = VHOST_ACCESS_WO;
1949 else
1950 access = VHOST_ACCESS_RO;
1951
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001952 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
1953 vhost32_to_cpu(vq, desc.len), iov + iov_count,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001954 iov_size - iov_count, access);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001955 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001956 if (ret != -EAGAIN)
1957 vq_err(vq, "Translation failure %d indirect idx %d\n",
1958 ret, i);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001959 return ret;
1960 }
1961 /* If this is an input descriptor, increment that count. */
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001962 if (access == VHOST_ACCESS_WO) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001963 *in_num += ret;
1964 if (unlikely(log)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001965 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
1966 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001967 ++*log_num;
1968 }
1969 } else {
1970 /* If it's an output descriptor, they're all supposed
1971 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001972 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001973 vq_err(vq, "Indirect descriptor "
1974 "has out after in: idx %d\n", i);
1975 return -EINVAL;
1976 }
1977 *out_num += ret;
1978 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001979 } while ((i = next_desc(vq, &desc)) != -1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001980 return 0;
1981}
1982
1983/* This looks in the virtqueue and for the first available buffer, and converts
1984 * it to an iovec for convenient access. Since descriptors consist of some
1985 * number of output then some number of input descriptors, it's actually two
1986 * iovecs, but we pack them into one and note how many of each there were.
1987 *
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001988 * This function returns the descriptor number found, or vq->num (which is
1989 * never a valid descriptor number) if none was found. A negative code is
1990 * returned on error. */
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001991int vhost_get_vq_desc(struct vhost_virtqueue *vq,
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001992 struct iovec iov[], unsigned int iov_size,
1993 unsigned int *out_num, unsigned int *in_num,
1994 struct vhost_log *log, unsigned int *log_num)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001995{
1996 struct vring_desc desc;
1997 unsigned int i, head, found = 0;
1998 u16 last_avail_idx;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001999 __virtio16 avail_idx;
2000 __virtio16 ring_head;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002001 int ret, access;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002002
2003 /* Check it isn't doing very strange things with descriptor numbers. */
2004 last_avail_idx = vq->last_avail_idx;
Jason Wange3b56cd2017-02-07 15:49:50 +08002005
2006 if (vq->avail_idx == vq->last_avail_idx) {
Jason Wangf8894912017-02-28 17:56:02 +08002007 if (unlikely(vhost_get_avail(vq, avail_idx, &vq->avail->idx))) {
Jason Wange3b56cd2017-02-07 15:49:50 +08002008 vq_err(vq, "Failed to access avail idx at %p\n",
2009 &vq->avail->idx);
2010 return -EFAULT;
2011 }
2012 vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
2013
2014 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
2015 vq_err(vq, "Guest moved used index from %u to %u",
2016 last_avail_idx, vq->avail_idx);
2017 return -EFAULT;
2018 }
2019
2020 /* If there's nothing new since last we looked, return
2021 * invalid.
2022 */
2023 if (vq->avail_idx == last_avail_idx)
2024 return vq->num;
2025
2026 /* Only get avail ring entries after they have been
2027 * exposed by guest.
2028 */
2029 smp_rmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002030 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002031
2032 /* Grab the next descriptor number they're advertising, and increment
2033 * the index we've seen. */
Jason Wangf8894912017-02-28 17:56:02 +08002034 if (unlikely(vhost_get_avail(vq, ring_head,
Jason Wangbfe2bc52016-06-23 02:04:30 -04002035 &vq->avail->ring[last_avail_idx & (vq->num - 1)]))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002036 vq_err(vq, "Failed to read head: idx %d address %p\n",
2037 last_avail_idx,
2038 &vq->avail->ring[last_avail_idx % vq->num]);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002039 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002040 }
2041
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002042 head = vhost16_to_cpu(vq, ring_head);
2043
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002044 /* If their number is silly, that's an error. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002045 if (unlikely(head >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002046 vq_err(vq, "Guest says index %u > %u is available",
2047 head, vq->num);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002048 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002049 }
2050
2051 /* When we start there are none of either input nor output. */
2052 *out_num = *in_num = 0;
2053 if (unlikely(log))
2054 *log_num = 0;
2055
2056 i = head;
2057 do {
2058 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002059 if (unlikely(i >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002060 vq_err(vq, "Desc index is %u > %u, head = %u",
2061 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002062 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002063 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002064 if (unlikely(++found > vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002065 vq_err(vq, "Loop detected: last one at %u "
2066 "vq size %u head %u\n",
2067 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002068 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002069 }
Jason Wangbfe2bc52016-06-23 02:04:30 -04002070 ret = vhost_copy_from_user(vq, &desc, vq->desc + i,
2071 sizeof desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002072 if (unlikely(ret)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002073 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
2074 i, vq->desc + i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002075 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002076 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002077 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT)) {
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03002078 ret = get_indirect(vq, iov, iov_size,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002079 out_num, in_num,
2080 log, log_num, &desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002081 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002082 if (ret != -EAGAIN)
2083 vq_err(vq, "Failure detected "
2084 "in indirect descriptor at idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002085 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002086 }
2087 continue;
2088 }
2089
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002090 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE))
2091 access = VHOST_ACCESS_WO;
2092 else
2093 access = VHOST_ACCESS_RO;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002094 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
2095 vhost32_to_cpu(vq, desc.len), iov + iov_count,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002096 iov_size - iov_count, access);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002097 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002098 if (ret != -EAGAIN)
2099 vq_err(vq, "Translation failure %d descriptor idx %d\n",
2100 ret, i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002101 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002102 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002103 if (access == VHOST_ACCESS_WO) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002104 /* If this is an input descriptor,
2105 * increment that count. */
2106 *in_num += ret;
2107 if (unlikely(log)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002108 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
2109 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002110 ++*log_num;
2111 }
2112 } else {
2113 /* If it's an output descriptor, they're all supposed
2114 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002115 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002116 vq_err(vq, "Descriptor has out after in: "
2117 "idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002118 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002119 }
2120 *out_num += ret;
2121 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002122 } while ((i = next_desc(vq, &desc)) != -1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002123
2124 /* On success, increment avail index. */
2125 vq->last_avail_idx++;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002126
2127 /* Assume notifications from guest are disabled at this point,
2128 * if they aren't we would need to update avail_event index. */
2129 BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002130 return head;
2131}
Asias He6ac1afb2013-05-06 16:38:21 +08002132EXPORT_SYMBOL_GPL(vhost_get_vq_desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002133
2134/* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
David Stevens8dd014a2010-07-27 18:52:21 +03002135void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002136{
David Stevens8dd014a2010-07-27 18:52:21 +03002137 vq->last_avail_idx -= n;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002138}
Asias He6ac1afb2013-05-06 16:38:21 +08002139EXPORT_SYMBOL_GPL(vhost_discard_vq_desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002140
2141/* After we've used one of their buffers, we tell them about it. We'll then
2142 * want to notify the guest, using eventfd. */
2143int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
2144{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002145 struct vring_used_elem heads = {
2146 cpu_to_vhost32(vq, head),
2147 cpu_to_vhost32(vq, len)
2148 };
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002149
Jason Wangc49e4e52013-09-02 16:40:58 +08002150 return vhost_add_used_n(vq, &heads, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002151}
Asias He6ac1afb2013-05-06 16:38:21 +08002152EXPORT_SYMBOL_GPL(vhost_add_used);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002153
David Stevens8dd014a2010-07-27 18:52:21 +03002154static int __vhost_add_used_n(struct vhost_virtqueue *vq,
2155 struct vring_used_elem *heads,
2156 unsigned count)
2157{
2158 struct vring_used_elem __user *used;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002159 u16 old, new;
David Stevens8dd014a2010-07-27 18:52:21 +03002160 int start;
2161
Michael S. Tsirkin5fba13b2015-11-29 13:34:44 +02002162 start = vq->last_used_idx & (vq->num - 1);
David Stevens8dd014a2010-07-27 18:52:21 +03002163 used = vq->used->ring + start;
Jason Wangc49e4e52013-09-02 16:40:58 +08002164 if (count == 1) {
Jason Wangbfe2bc52016-06-23 02:04:30 -04002165 if (vhost_put_user(vq, heads[0].id, &used->id)) {
Jason Wangc49e4e52013-09-02 16:40:58 +08002166 vq_err(vq, "Failed to write used id");
2167 return -EFAULT;
2168 }
Jason Wangbfe2bc52016-06-23 02:04:30 -04002169 if (vhost_put_user(vq, heads[0].len, &used->len)) {
Jason Wangc49e4e52013-09-02 16:40:58 +08002170 vq_err(vq, "Failed to write used len");
2171 return -EFAULT;
2172 }
Jason Wangbfe2bc52016-06-23 02:04:30 -04002173 } else if (vhost_copy_to_user(vq, used, heads, count * sizeof *used)) {
David Stevens8dd014a2010-07-27 18:52:21 +03002174 vq_err(vq, "Failed to write used");
2175 return -EFAULT;
2176 }
2177 if (unlikely(vq->log_used)) {
2178 /* Make sure data is seen before log. */
2179 smp_wmb();
2180 /* Log used ring entry write. */
2181 log_write(vq->log_base,
2182 vq->log_addr +
2183 ((void __user *)used - (void __user *)vq->used),
2184 count * sizeof *used);
2185 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002186 old = vq->last_used_idx;
2187 new = (vq->last_used_idx += count);
2188 /* If the driver never bothers to signal in a very long while,
2189 * used index might wrap around. If that happens, invalidate
2190 * signalled_used index we stored. TODO: make sure driver
2191 * signals at least once in 2^16 and remove this. */
2192 if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old)))
2193 vq->signalled_used_valid = false;
David Stevens8dd014a2010-07-27 18:52:21 +03002194 return 0;
2195}
2196
2197/* After we've used one of their buffers, we tell them about it. We'll then
2198 * want to notify the guest, using eventfd. */
2199int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
2200 unsigned count)
2201{
2202 int start, n, r;
2203
Michael S. Tsirkin5fba13b2015-11-29 13:34:44 +02002204 start = vq->last_used_idx & (vq->num - 1);
David Stevens8dd014a2010-07-27 18:52:21 +03002205 n = vq->num - start;
2206 if (n < count) {
2207 r = __vhost_add_used_n(vq, heads, n);
2208 if (r < 0)
2209 return r;
2210 heads += n;
2211 count -= n;
2212 }
2213 r = __vhost_add_used_n(vq, heads, count);
2214
2215 /* Make sure buffer is written before we update index. */
2216 smp_wmb();
Jason Wangbfe2bc52016-06-23 02:04:30 -04002217 if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->last_used_idx),
2218 &vq->used->idx)) {
David Stevens8dd014a2010-07-27 18:52:21 +03002219 vq_err(vq, "Failed to increment used idx");
2220 return -EFAULT;
2221 }
2222 if (unlikely(vq->log_used)) {
Jason Wang841df922018-12-13 10:53:37 +08002223 /* Make sure used idx is seen before log. */
2224 smp_wmb();
David Stevens8dd014a2010-07-27 18:52:21 +03002225 /* Log used index update. */
2226 log_write(vq->log_base,
2227 vq->log_addr + offsetof(struct vring_used, idx),
2228 sizeof vq->used->idx);
2229 if (vq->log_ctx)
2230 eventfd_signal(vq->log_ctx, 1);
2231 }
2232 return r;
2233}
Asias He6ac1afb2013-05-06 16:38:21 +08002234EXPORT_SYMBOL_GPL(vhost_add_used_n);
David Stevens8dd014a2010-07-27 18:52:21 +03002235
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002236static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002237{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002238 __u16 old, new;
2239 __virtio16 event;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002240 bool v;
Jason Wang8d658432017-07-27 11:22:05 +08002241 /* Flush out used index updates. This is paired
2242 * with the barrier that the Guest executes when enabling
2243 * interrupts. */
2244 smp_mb();
Michael S. Tsirkin0d499352010-05-11 19:44:17 +03002245
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002246 if (vhost_has_feature(vq, VIRTIO_F_NOTIFY_ON_EMPTY) &&
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002247 unlikely(vq->avail_idx == vq->last_avail_idx))
2248 return true;
2249
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002250 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002251 __virtio16 flags;
Jason Wangf8894912017-02-28 17:56:02 +08002252 if (vhost_get_avail(vq, flags, &vq->avail->flags)) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002253 vq_err(vq, "Failed to get flags");
2254 return true;
2255 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002256 return !(flags & cpu_to_vhost16(vq, VRING_AVAIL_F_NO_INTERRUPT));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002257 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002258 old = vq->signalled_used;
2259 v = vq->signalled_used_valid;
2260 new = vq->signalled_used = vq->last_used_idx;
2261 vq->signalled_used_valid = true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002262
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002263 if (unlikely(!v))
2264 return true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002265
Jason Wangf8894912017-02-28 17:56:02 +08002266 if (vhost_get_avail(vq, event, vhost_used_event(vq))) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002267 vq_err(vq, "Failed to get used event idx");
2268 return true;
2269 }
Jason Wang8d658432017-07-27 11:22:05 +08002270 return vring_need_event(vhost16_to_cpu(vq, event), new, old);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002271}
2272
2273/* This actually signals the guest, using eventfd. */
2274void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
2275{
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002276 /* Signal the Guest tell them we used something up. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002277 if (vq->call_ctx && vhost_notify(dev, vq))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002278 eventfd_signal(vq->call_ctx, 1);
2279}
Asias He6ac1afb2013-05-06 16:38:21 +08002280EXPORT_SYMBOL_GPL(vhost_signal);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002281
2282/* And here's the combo meal deal. Supersize me! */
2283void vhost_add_used_and_signal(struct vhost_dev *dev,
2284 struct vhost_virtqueue *vq,
2285 unsigned int head, int len)
2286{
2287 vhost_add_used(vq, head, len);
2288 vhost_signal(dev, vq);
2289}
Asias He6ac1afb2013-05-06 16:38:21 +08002290EXPORT_SYMBOL_GPL(vhost_add_used_and_signal);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002291
David Stevens8dd014a2010-07-27 18:52:21 +03002292/* multi-buffer version of vhost_add_used_and_signal */
2293void vhost_add_used_and_signal_n(struct vhost_dev *dev,
2294 struct vhost_virtqueue *vq,
2295 struct vring_used_elem *heads, unsigned count)
2296{
2297 vhost_add_used_n(vq, heads, count);
2298 vhost_signal(dev, vq);
2299}
Asias He6ac1afb2013-05-06 16:38:21 +08002300EXPORT_SYMBOL_GPL(vhost_add_used_and_signal_n);
David Stevens8dd014a2010-07-27 18:52:21 +03002301
Jason Wangd4a60602016-03-04 06:24:52 -05002302/* return true if we're sure that avaiable ring is empty */
2303bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq)
2304{
2305 __virtio16 avail_idx;
2306 int r;
2307
Jason Wang275bf962017-01-18 15:02:01 +08002308 if (vq->avail_idx != vq->last_avail_idx)
Jason Wangd4a60602016-03-04 06:24:52 -05002309 return false;
2310
Linus Torvalds54d79892017-03-02 13:53:13 -08002311 r = vhost_get_avail(vq, avail_idx, &vq->avail->idx);
Jason Wang275bf962017-01-18 15:02:01 +08002312 if (unlikely(r))
2313 return false;
2314 vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
2315
2316 return vq->avail_idx == vq->last_avail_idx;
Jason Wangd4a60602016-03-04 06:24:52 -05002317}
2318EXPORT_SYMBOL_GPL(vhost_vq_avail_empty);
2319
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002320/* OK, now we need to know about added descriptors. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002321bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002322{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002323 __virtio16 avail_idx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002324 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05302325
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002326 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
2327 return false;
2328 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002329 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08002330 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002331 if (r) {
2332 vq_err(vq, "Failed to enable notification at %p: %d\n",
2333 &vq->used->flags, r);
2334 return false;
2335 }
2336 } else {
Jason Wang2723fea2011-06-21 18:04:38 +08002337 r = vhost_update_avail_event(vq, vq->avail_idx);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002338 if (r) {
2339 vq_err(vq, "Failed to update avail event index at %p: %d\n",
2340 vhost_avail_event(vq), r);
2341 return false;
2342 }
2343 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002344 /* They could have slipped one in as we were doing that: make
2345 * sure it's written, then check again. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00002346 smp_mb();
Jason Wangf8894912017-02-28 17:56:02 +08002347 r = vhost_get_avail(vq, avail_idx, &vq->avail->idx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002348 if (r) {
2349 vq_err(vq, "Failed to check avail idx at %p: %d\n",
2350 &vq->avail->idx, r);
2351 return false;
2352 }
2353
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002354 return vhost16_to_cpu(vq, avail_idx) != vq->avail_idx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002355}
Asias He6ac1afb2013-05-06 16:38:21 +08002356EXPORT_SYMBOL_GPL(vhost_enable_notify);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002357
2358/* We don't need to be notified again. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002359void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002360{
2361 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05302362
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002363 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
2364 return;
2365 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002366 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08002367 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002368 if (r)
2369 vq_err(vq, "Failed to enable notification at %p: %d\n",
2370 &vq->used->flags, r);
2371 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002372}
Asias He6ac1afb2013-05-06 16:38:21 +08002373EXPORT_SYMBOL_GPL(vhost_disable_notify);
2374
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002375/* Create a new message. */
2376struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type)
2377{
2378 struct vhost_msg_node *node = kmalloc(sizeof *node, GFP_KERNEL);
2379 if (!node)
2380 return NULL;
Michael S. Tsirkin670ae9c2018-05-12 00:33:10 +03002381
2382 /* Make sure all padding within the structure is initialized. */
2383 memset(&node->msg, 0, sizeof node->msg);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002384 node->vq = vq;
2385 node->msg.type = type;
2386 return node;
2387}
2388EXPORT_SYMBOL_GPL(vhost_new_msg);
2389
2390void vhost_enqueue_msg(struct vhost_dev *dev, struct list_head *head,
2391 struct vhost_msg_node *node)
2392{
2393 spin_lock(&dev->iotlb_lock);
2394 list_add_tail(&node->node, head);
2395 spin_unlock(&dev->iotlb_lock);
2396
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002397 wake_up_interruptible_poll(&dev->wait, EPOLLIN | EPOLLRDNORM);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002398}
2399EXPORT_SYMBOL_GPL(vhost_enqueue_msg);
2400
2401struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev,
2402 struct list_head *head)
2403{
2404 struct vhost_msg_node *node = NULL;
2405
2406 spin_lock(&dev->iotlb_lock);
2407 if (!list_empty(head)) {
2408 node = list_first_entry(head, struct vhost_msg_node,
2409 node);
2410 list_del(&node->node);
2411 }
2412 spin_unlock(&dev->iotlb_lock);
2413
2414 return node;
2415}
2416EXPORT_SYMBOL_GPL(vhost_dequeue_msg);
2417
2418
Asias He6ac1afb2013-05-06 16:38:21 +08002419static int __init vhost_init(void)
2420{
2421 return 0;
2422}
2423
2424static void __exit vhost_exit(void)
2425{
2426}
2427
2428module_init(vhost_init);
2429module_exit(vhost_exit);
2430
2431MODULE_VERSION("0.0.1");
2432MODULE_LICENSE("GPL v2");
2433MODULE_AUTHOR("Michael S. Tsirkin");
2434MODULE_DESCRIPTION("Host kernel accelerator for virtio");