blob: b13c6b4b2c665a332a40aeada1eaa1e421d9a4aa [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>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000033
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000034#include "vhost.h"
35
Igor Mammedovc9ce42f2015-07-02 15:08:11 +020036static ushort max_mem_regions = 64;
37module_param(max_mem_regions, ushort, 0444);
38MODULE_PARM_DESC(max_mem_regions,
39 "Maximum number of memory regions in memory map. (default: 64)");
Jason Wang6b1e6cc2016-06-23 02:04:32 -040040static int max_iotlb_entries = 2048;
41module_param(max_iotlb_entries, int, 0444);
42MODULE_PARM_DESC(max_iotlb_entries,
43 "Maximum number of iotlb entries. (default: 2048)");
Igor Mammedovc9ce42f2015-07-02 15:08:11 +020044
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000045enum {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000046 VHOST_MEMORY_F_LOG = 0x1,
47};
48
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +030049#define vhost_used_event(vq) ((__virtio16 __user *)&vq->avail->ring[vq->num])
50#define vhost_avail_event(vq) ((__virtio16 __user *)&vq->used->ring[vq->num])
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +030051
Jason Wanga9709d62016-06-23 02:04:31 -040052INTERVAL_TREE_DEFINE(struct vhost_umem_node,
53 rb, __u64, __subtree_last,
Michael S. Tsirkin2f952c02016-12-06 05:57:54 +020054 START, LAST, static inline, vhost_umem_interval_tree);
Jason Wanga9709d62016-06-23 02:04:31 -040055
Greg Kurz2751c982015-04-24 14:27:24 +020056#ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
Greg Kurzc5072032016-02-16 15:59:34 +010057static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
Greg Kurz2751c982015-04-24 14:27:24 +020058{
59 vq->user_be = !virtio_legacy_is_little_endian();
60}
61
Greg Kurzc5072032016-02-16 15:59:34 +010062static void vhost_enable_cross_endian_big(struct vhost_virtqueue *vq)
63{
64 vq->user_be = true;
65}
66
67static void vhost_enable_cross_endian_little(struct vhost_virtqueue *vq)
68{
69 vq->user_be = false;
70}
71
Greg Kurz2751c982015-04-24 14:27:24 +020072static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
73{
74 struct vhost_vring_state s;
75
76 if (vq->private_data)
77 return -EBUSY;
78
79 if (copy_from_user(&s, argp, sizeof(s)))
80 return -EFAULT;
81
82 if (s.num != VHOST_VRING_LITTLE_ENDIAN &&
83 s.num != VHOST_VRING_BIG_ENDIAN)
84 return -EINVAL;
85
Greg Kurzc5072032016-02-16 15:59:34 +010086 if (s.num == VHOST_VRING_BIG_ENDIAN)
87 vhost_enable_cross_endian_big(vq);
88 else
89 vhost_enable_cross_endian_little(vq);
Greg Kurz2751c982015-04-24 14:27:24 +020090
91 return 0;
92}
93
94static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
95 int __user *argp)
96{
97 struct vhost_vring_state s = {
98 .index = idx,
99 .num = vq->user_be
100 };
101
102 if (copy_to_user(argp, &s, sizeof(s)))
103 return -EFAULT;
104
105 return 0;
106}
107
108static void vhost_init_is_le(struct vhost_virtqueue *vq)
109{
110 /* Note for legacy virtio: user_be is initialized at reset time
111 * according to the host endianness. If userspace does not set an
112 * explicit endianness, the default behavior is native endian, as
113 * expected by legacy virtio.
114 */
115 vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) || !vq->user_be;
116}
117#else
Greg Kurzc5072032016-02-16 15:59:34 +0100118static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
Greg Kurz2751c982015-04-24 14:27:24 +0200119{
120}
121
122static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
123{
124 return -ENOIOCTLCMD;
125}
126
127static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
128 int __user *argp)
129{
130 return -ENOIOCTLCMD;
131}
132
133static void vhost_init_is_le(struct vhost_virtqueue *vq)
134{
Halil Pasiccda8bba2017-01-30 11:09:36 +0100135 vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1)
136 || virtio_legacy_is_little_endian();
Greg Kurz2751c982015-04-24 14:27:24 +0200137}
138#endif /* CONFIG_VHOST_CROSS_ENDIAN_LEGACY */
139
Greg Kurzc5072032016-02-16 15:59:34 +0100140static void vhost_reset_is_le(struct vhost_virtqueue *vq)
141{
Halil Pasiccda8bba2017-01-30 11:09:36 +0100142 vhost_init_is_le(vq);
Greg Kurzc5072032016-02-16 15:59:34 +0100143}
144
Jason Wang7235acd2016-04-25 22:14:32 -0400145struct vhost_flush_struct {
146 struct vhost_work work;
147 struct completion wait_event;
148};
149
150static void vhost_flush_work(struct vhost_work *work)
151{
152 struct vhost_flush_struct *s;
153
154 s = container_of(work, struct vhost_flush_struct, work);
155 complete(&s->wait_event);
156}
157
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000158static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
159 poll_table *pt)
160{
161 struct vhost_poll *poll;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000162
Krishna Kumard47effe2011-03-01 17:06:37 +0530163 poll = container_of(pt, struct vhost_poll, table);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000164 poll->wqh = wqh;
165 add_wait_queue(wqh, &poll->wait);
166}
167
Ingo Molnarac6424b2017-06-20 12:06:13 +0200168static int vhost_poll_wakeup(wait_queue_entry_t *wait, unsigned mode, int sync,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000169 void *key)
170{
Tejun Heoc23f34452010-06-02 20:40:00 +0200171 struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
172
Al Viro3ad6f932017-07-03 20:14:56 -0400173 if (!(key_to_poll(key) & poll->mask))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000174 return 0;
175
Tejun Heoc23f34452010-06-02 20:40:00 +0200176 vhost_poll_queue(poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000177 return 0;
178}
179
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000180void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000181{
Jason Wang04b96e52016-04-25 22:14:33 -0400182 clear_bit(VHOST_WORK_QUEUED, &work->flags);
Tejun Heoc23f34452010-06-02 20:40:00 +0200183 work->fn = fn;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000184}
Asias He6ac1afb2013-05-06 16:38:21 +0800185EXPORT_SYMBOL_GPL(vhost_work_init);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000186
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300187/* Init poll structure */
188void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
Al Viro58e3b602017-07-03 23:50:40 -0400189 __poll_t mask, struct vhost_dev *dev)
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300190{
191 init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
192 init_poll_funcptr(&poll->table, vhost_poll_func);
193 poll->mask = mask;
194 poll->dev = dev;
Jason Wang2b8b3282013-01-28 01:05:18 +0000195 poll->wqh = NULL;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300196
197 vhost_work_init(&poll->work, fn);
198}
Asias He6ac1afb2013-05-06 16:38:21 +0800199EXPORT_SYMBOL_GPL(vhost_poll_init);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300200
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000201/* Start polling a file. We add ourselves to file's wait queue. The caller must
202 * keep a reference to a file until after vhost_poll_stop is called. */
Jason Wang2b8b3282013-01-28 01:05:18 +0000203int vhost_poll_start(struct vhost_poll *poll, struct file *file)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000204{
Al Viroe6c8adc2017-07-03 22:25:56 -0400205 __poll_t mask;
Jason Wang2b8b3282013-01-28 01:05:18 +0000206 int ret = 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530207
Jason Wang70181d512013-04-10 20:50:48 +0000208 if (poll->wqh)
209 return 0;
210
Christoph Hellwig9965ed172018-03-05 07:26:05 -0800211 mask = vfs_poll(file, &poll->table);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000212 if (mask)
Al Viro3ad6f932017-07-03 20:14:56 -0400213 vhost_poll_wakeup(&poll->wait, 0, 0, poll_to_key(mask));
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800214 if (mask & EPOLLERR) {
Jason Wangdc6455a2018-03-27 20:50:52 +0800215 vhost_poll_stop(poll);
Jason Wang2b8b3282013-01-28 01:05:18 +0000216 ret = -EINVAL;
217 }
218
219 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000220}
Asias He6ac1afb2013-05-06 16:38:21 +0800221EXPORT_SYMBOL_GPL(vhost_poll_start);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000222
223/* Stop polling a file. After this function returns, it becomes safe to drop the
224 * file reference. You must also flush afterwards. */
225void vhost_poll_stop(struct vhost_poll *poll)
226{
Jason Wang2b8b3282013-01-28 01:05:18 +0000227 if (poll->wqh) {
228 remove_wait_queue(poll->wqh, &poll->wait);
229 poll->wqh = NULL;
230 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000231}
Asias He6ac1afb2013-05-06 16:38:21 +0800232EXPORT_SYMBOL_GPL(vhost_poll_stop);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000233
Asias He6ac1afb2013-05-06 16:38:21 +0800234void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000235{
Jason Wang7235acd2016-04-25 22:14:32 -0400236 struct vhost_flush_struct flush;
Tejun Heoc23f34452010-06-02 20:40:00 +0200237
Jason Wang7235acd2016-04-25 22:14:32 -0400238 if (dev->worker) {
239 init_completion(&flush.wait_event);
240 vhost_work_init(&flush.work, vhost_flush_work);
241
242 vhost_work_queue(dev, &flush.work);
243 wait_for_completion(&flush.wait_event);
244 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000245}
Asias He6ac1afb2013-05-06 16:38:21 +0800246EXPORT_SYMBOL_GPL(vhost_work_flush);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000247
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300248/* Flush any work that has been scheduled. When calling this, don't hold any
249 * locks that are also used by the callback. */
250void vhost_poll_flush(struct vhost_poll *poll)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000251{
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300252 vhost_work_flush(poll->dev, &poll->work);
253}
Asias He6ac1afb2013-05-06 16:38:21 +0800254EXPORT_SYMBOL_GPL(vhost_poll_flush);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300255
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000256void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300257{
Jason Wang04b96e52016-04-25 22:14:33 -0400258 if (!dev->worker)
259 return;
Tejun Heoc23f34452010-06-02 20:40:00 +0200260
Jason Wang04b96e52016-04-25 22:14:33 -0400261 if (!test_and_set_bit(VHOST_WORK_QUEUED, &work->flags)) {
262 /* We can only add the work to the list after we're
263 * sure it was not in the list.
Peng Tao635abf02016-12-07 17:52:19 +0800264 * test_and_set_bit() implies a memory barrier.
Jason Wang04b96e52016-04-25 22:14:33 -0400265 */
Jason Wang04b96e52016-04-25 22:14:33 -0400266 llist_add(&work->node, &dev->work_list);
Tejun Heoc23f34452010-06-02 20:40:00 +0200267 wake_up_process(dev->worker);
268 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000269}
Asias He6ac1afb2013-05-06 16:38:21 +0800270EXPORT_SYMBOL_GPL(vhost_work_queue);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000271
Jason Wang526d3e72016-03-04 06:24:51 -0500272/* A lockless hint for busy polling code to exit the loop */
273bool vhost_has_work(struct vhost_dev *dev)
274{
Jason Wang04b96e52016-04-25 22:14:33 -0400275 return !llist_empty(&dev->work_list);
Jason Wang526d3e72016-03-04 06:24:51 -0500276}
277EXPORT_SYMBOL_GPL(vhost_has_work);
278
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300279void vhost_poll_queue(struct vhost_poll *poll)
280{
281 vhost_work_queue(poll->dev, &poll->work);
282}
Asias He6ac1afb2013-05-06 16:38:21 +0800283EXPORT_SYMBOL_GPL(vhost_poll_queue);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300284
Jason Wangf8894912017-02-28 17:56:02 +0800285static void __vhost_vq_meta_reset(struct vhost_virtqueue *vq)
286{
287 int j;
288
289 for (j = 0; j < VHOST_NUM_ADDRS; j++)
290 vq->meta_iotlb[j] = NULL;
291}
292
293static void vhost_vq_meta_reset(struct vhost_dev *d)
294{
295 int i;
296
297 for (i = 0; i < d->nvqs; ++i)
298 __vhost_vq_meta_reset(d->vqs[i]);
299}
300
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000301static void vhost_vq_reset(struct vhost_dev *dev,
302 struct vhost_virtqueue *vq)
303{
304 vq->num = 1;
305 vq->desc = NULL;
306 vq->avail = NULL;
307 vq->used = NULL;
308 vq->last_avail_idx = 0;
309 vq->avail_idx = 0;
310 vq->last_used_idx = 0;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300311 vq->signalled_used = 0;
312 vq->signalled_used_valid = false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000313 vq->used_flags = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000314 vq->log_used = false;
315 vq->log_addr = -1ull;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000316 vq->private_data = NULL;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300317 vq->acked_features = 0;
Jason Wang429711a2018-08-06 11:17:47 +0800318 vq->acked_backend_features = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000319 vq->log_base = NULL;
320 vq->error_ctx = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000321 vq->kick = NULL;
322 vq->call_ctx = NULL;
Michael S. Tsirkin73a99f02010-02-23 11:23:45 +0200323 vq->log_ctx = NULL;
Greg Kurzc5072032016-02-16 15:59:34 +0100324 vhost_reset_is_le(vq);
325 vhost_disable_cross_endian(vq);
Jason Wang03088132016-03-04 06:24:53 -0500326 vq->busyloop_timeout = 0;
Jason Wanga9709d62016-06-23 02:04:31 -0400327 vq->umem = NULL;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400328 vq->iotlb = NULL;
Jason Wangf8894912017-02-28 17:56:02 +0800329 __vhost_vq_meta_reset(vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000330}
331
Tejun Heoc23f34452010-06-02 20:40:00 +0200332static int vhost_worker(void *data)
333{
334 struct vhost_dev *dev = data;
Jason Wang04b96e52016-04-25 22:14:33 -0400335 struct vhost_work *work, *work_next;
336 struct llist_node *node;
Jens Freimannd7ffde32012-06-26 00:59:58 +0000337 mm_segment_t oldfs = get_fs();
Tejun Heoc23f34452010-06-02 20:40:00 +0200338
Jens Freimannd7ffde32012-06-26 00:59:58 +0000339 set_fs(USER_DS);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200340 use_mm(dev->mm);
341
Tejun Heoc23f34452010-06-02 20:40:00 +0200342 for (;;) {
343 /* mb paired w/ kthread_stop */
344 set_current_state(TASK_INTERRUPTIBLE);
345
Tejun Heoc23f34452010-06-02 20:40:00 +0200346 if (kthread_should_stop()) {
Tejun Heoc23f34452010-06-02 20:40:00 +0200347 __set_current_state(TASK_RUNNING);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200348 break;
Tejun Heoc23f34452010-06-02 20:40:00 +0200349 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200350
Jason Wang04b96e52016-04-25 22:14:33 -0400351 node = llist_del_all(&dev->work_list);
352 if (!node)
353 schedule();
354
355 node = llist_reverse_order(node);
356 /* make sure flag is seen after deletion */
357 smp_wmb();
358 llist_for_each_entry_safe(work, work_next, node, node) {
359 clear_bit(VHOST_WORK_QUEUED, &work->flags);
Tejun Heoc23f34452010-06-02 20:40:00 +0200360 __set_current_state(TASK_RUNNING);
361 work->fn(work);
Nadav Har'Eld550dda2012-02-27 15:07:29 +0200362 if (need_resched())
363 schedule();
Jason Wang04b96e52016-04-25 22:14:33 -0400364 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200365 }
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200366 unuse_mm(dev->mm);
Jens Freimannd7ffde32012-06-26 00:59:58 +0000367 set_fs(oldfs);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200368 return 0;
Tejun Heoc23f34452010-06-02 20:40:00 +0200369}
370
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000371static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
372{
373 kfree(vq->indirect);
374 vq->indirect = NULL;
375 kfree(vq->log);
376 vq->log = NULL;
377 kfree(vq->heads);
378 vq->heads = NULL;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000379}
380
Jason Wange0e9b402010-09-14 23:53:05 +0800381/* Helper to allocate iovec buffers for all vqs. */
382static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
383{
Asias He6d5e6aa2013-05-06 16:38:23 +0800384 struct vhost_virtqueue *vq;
Jason Wange0e9b402010-09-14 23:53:05 +0800385 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530386
Jason Wange0e9b402010-09-14 23:53:05 +0800387 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800388 vq = dev->vqs[i];
Kees Cook6da2ec52018-06-12 13:55:00 -0700389 vq->indirect = kmalloc_array(UIO_MAXIOV,
390 sizeof(*vq->indirect),
391 GFP_KERNEL);
392 vq->log = kmalloc_array(UIO_MAXIOV, sizeof(*vq->log),
393 GFP_KERNEL);
394 vq->heads = kmalloc_array(UIO_MAXIOV, sizeof(*vq->heads),
395 GFP_KERNEL);
Asias He6d5e6aa2013-05-06 16:38:23 +0800396 if (!vq->indirect || !vq->log || !vq->heads)
Jason Wange0e9b402010-09-14 23:53:05 +0800397 goto err_nomem;
398 }
399 return 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530400
Jason Wange0e9b402010-09-14 23:53:05 +0800401err_nomem:
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000402 for (; i >= 0; --i)
Asias He3ab2e422013-04-27 11:16:48 +0800403 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800404 return -ENOMEM;
405}
406
407static void vhost_dev_free_iovecs(struct vhost_dev *dev)
408{
409 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530410
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000411 for (i = 0; i < dev->nvqs; ++i)
Asias He3ab2e422013-04-27 11:16:48 +0800412 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800413}
414
Zhi Yong Wu59566b6e2013-12-07 04:13:03 +0800415void vhost_dev_init(struct vhost_dev *dev,
Asias He3ab2e422013-04-27 11:16:48 +0800416 struct vhost_virtqueue **vqs, int nvqs)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000417{
Asias He6d5e6aa2013-05-06 16:38:23 +0800418 struct vhost_virtqueue *vq;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000419 int i;
Tejun Heoc23f34452010-06-02 20:40:00 +0200420
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000421 dev->vqs = vqs;
422 dev->nvqs = nvqs;
423 mutex_init(&dev->mutex);
424 dev->log_ctx = NULL;
Jason Wanga9709d62016-06-23 02:04:31 -0400425 dev->umem = NULL;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400426 dev->iotlb = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000427 dev->mm = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200428 dev->worker = NULL;
Jason Wang04b96e52016-04-25 22:14:33 -0400429 init_llist_head(&dev->work_list);
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400430 init_waitqueue_head(&dev->wait);
431 INIT_LIST_HEAD(&dev->read_list);
432 INIT_LIST_HEAD(&dev->pending_list);
433 spin_lock_init(&dev->iotlb_lock);
Jason Wang04b96e52016-04-25 22:14:33 -0400434
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000435
436 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800437 vq = dev->vqs[i];
438 vq->log = NULL;
439 vq->indirect = NULL;
440 vq->heads = NULL;
441 vq->dev = dev;
442 mutex_init(&vq->mutex);
443 vhost_vq_reset(dev, vq);
444 if (vq->handle_kick)
445 vhost_poll_init(&vq->poll, vq->handle_kick,
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800446 EPOLLIN, dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000447 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000448}
Asias He6ac1afb2013-05-06 16:38:21 +0800449EXPORT_SYMBOL_GPL(vhost_dev_init);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000450
451/* Caller should have device mutex */
452long vhost_dev_check_owner(struct vhost_dev *dev)
453{
454 /* Are you the owner? If not, I don't think you mean to do that */
455 return dev->mm == current->mm ? 0 : -EPERM;
456}
Asias He6ac1afb2013-05-06 16:38:21 +0800457EXPORT_SYMBOL_GPL(vhost_dev_check_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000458
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300459struct vhost_attach_cgroups_struct {
Krishna Kumard47effe2011-03-01 17:06:37 +0530460 struct vhost_work work;
461 struct task_struct *owner;
462 int ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300463};
464
465static void vhost_attach_cgroups_work(struct vhost_work *work)
466{
Krishna Kumard47effe2011-03-01 17:06:37 +0530467 struct vhost_attach_cgroups_struct *s;
468
469 s = container_of(work, struct vhost_attach_cgroups_struct, work);
470 s->ret = cgroup_attach_task_all(s->owner, current);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300471}
472
473static int vhost_attach_cgroups(struct vhost_dev *dev)
474{
Krishna Kumard47effe2011-03-01 17:06:37 +0530475 struct vhost_attach_cgroups_struct attach;
476
477 attach.owner = current;
478 vhost_work_init(&attach.work, vhost_attach_cgroups_work);
479 vhost_work_queue(dev, &attach.work);
480 vhost_work_flush(dev, &attach.work);
481 return attach.ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300482}
483
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000484/* Caller should have device mutex */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300485bool vhost_dev_has_owner(struct vhost_dev *dev)
486{
487 return dev->mm;
488}
Asias He6ac1afb2013-05-06 16:38:21 +0800489EXPORT_SYMBOL_GPL(vhost_dev_has_owner);
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300490
491/* Caller should have device mutex */
Asias He54db63c2013-05-06 11:15:59 +0800492long vhost_dev_set_owner(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000493{
Tejun Heoc23f34452010-06-02 20:40:00 +0200494 struct task_struct *worker;
495 int err;
Krishna Kumard47effe2011-03-01 17:06:37 +0530496
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000497 /* Is there an owner already? */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300498 if (vhost_dev_has_owner(dev)) {
Tejun Heoc23f34452010-06-02 20:40:00 +0200499 err = -EBUSY;
500 goto err_mm;
501 }
Krishna Kumard47effe2011-03-01 17:06:37 +0530502
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000503 /* No owner, become one */
504 dev->mm = get_task_mm(current);
Tejun Heoc23f34452010-06-02 20:40:00 +0200505 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
506 if (IS_ERR(worker)) {
507 err = PTR_ERR(worker);
508 goto err_worker;
509 }
510
511 dev->worker = worker;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300512 wake_up_process(worker); /* avoid contributing to loadavg */
513
514 err = vhost_attach_cgroups(dev);
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300515 if (err)
516 goto err_cgroup;
Tejun Heoc23f34452010-06-02 20:40:00 +0200517
Jason Wange0e9b402010-09-14 23:53:05 +0800518 err = vhost_dev_alloc_iovecs(dev);
519 if (err)
520 goto err_cgroup;
521
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000522 return 0;
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300523err_cgroup:
524 kthread_stop(worker);
Michael S. Tsirkin615cc222010-09-02 14:16:36 +0300525 dev->worker = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200526err_worker:
527 if (dev->mm)
528 mmput(dev->mm);
529 dev->mm = NULL;
530err_mm:
531 return err;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000532}
Asias He6ac1afb2013-05-06 16:38:21 +0800533EXPORT_SYMBOL_GPL(vhost_dev_set_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000534
Jason Wanga9709d62016-06-23 02:04:31 -0400535struct vhost_umem *vhost_dev_reset_owner_prepare(void)
536{
Michal Hocko6c5ab652017-05-08 15:57:15 -0700537 return kvzalloc(sizeof(struct vhost_umem), GFP_KERNEL);
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300538}
Asias He6ac1afb2013-05-06 16:38:21 +0800539EXPORT_SYMBOL_GPL(vhost_dev_reset_owner_prepare);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000540
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300541/* Caller should have device mutex */
Jason Wanga9709d62016-06-23 02:04:31 -0400542void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_umem *umem)
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300543{
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300544 int i;
545
夷则(Caspar)f6f93f72017-12-25 00:08:58 +0800546 vhost_dev_cleanup(dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000547
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300548 /* Restore memory to default empty mapping. */
Jason Wanga9709d62016-06-23 02:04:31 -0400549 INIT_LIST_HEAD(&umem->umem_list);
550 dev->umem = umem;
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300551 /* We don't need VQ locks below since vhost_dev_cleanup makes sure
552 * VQs aren't running.
553 */
554 for (i = 0; i < dev->nvqs; ++i)
Jason Wanga9709d62016-06-23 02:04:31 -0400555 dev->vqs[i]->umem = umem;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000556}
Asias He6ac1afb2013-05-06 16:38:21 +0800557EXPORT_SYMBOL_GPL(vhost_dev_reset_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000558
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000559void vhost_dev_stop(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000560{
561 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530562
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000563 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800564 if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) {
565 vhost_poll_stop(&dev->vqs[i]->poll);
566 vhost_poll_flush(&dev->vqs[i]->poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000567 }
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000568 }
569}
Asias He6ac1afb2013-05-06 16:38:21 +0800570EXPORT_SYMBOL_GPL(vhost_dev_stop);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000571
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400572static void vhost_umem_free(struct vhost_umem *umem,
573 struct vhost_umem_node *node)
574{
575 vhost_umem_interval_tree_remove(node, &umem->umem_tree);
576 list_del(&node->link);
577 kfree(node);
578 umem->numem--;
579}
580
Jason Wanga9709d62016-06-23 02:04:31 -0400581static void vhost_umem_clean(struct vhost_umem *umem)
582{
583 struct vhost_umem_node *node, *tmp;
584
585 if (!umem)
586 return;
587
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400588 list_for_each_entry_safe(node, tmp, &umem->umem_list, link)
589 vhost_umem_free(umem, node);
590
Jason Wanga9709d62016-06-23 02:04:31 -0400591 kvfree(umem);
592}
593
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400594static void vhost_clear_msg(struct vhost_dev *dev)
595{
596 struct vhost_msg_node *node, *n;
597
598 spin_lock(&dev->iotlb_lock);
599
600 list_for_each_entry_safe(node, n, &dev->read_list, node) {
601 list_del(&node->node);
602 kfree(node);
603 }
604
605 list_for_each_entry_safe(node, n, &dev->pending_list, node) {
606 list_del(&node->node);
607 kfree(node);
608 }
609
610 spin_unlock(&dev->iotlb_lock);
611}
612
夷则(Caspar)f6f93f72017-12-25 00:08:58 +0800613void vhost_dev_cleanup(struct vhost_dev *dev)
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000614{
615 int i;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000616
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000617 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800618 if (dev->vqs[i]->error_ctx)
619 eventfd_ctx_put(dev->vqs[i]->error_ctx);
Asias He3ab2e422013-04-27 11:16:48 +0800620 if (dev->vqs[i]->kick)
621 fput(dev->vqs[i]->kick);
622 if (dev->vqs[i]->call_ctx)
623 eventfd_ctx_put(dev->vqs[i]->call_ctx);
Asias He3ab2e422013-04-27 11:16:48 +0800624 vhost_vq_reset(dev, dev->vqs[i]);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000625 }
Jason Wange0e9b402010-09-14 23:53:05 +0800626 vhost_dev_free_iovecs(dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000627 if (dev->log_ctx)
628 eventfd_ctx_put(dev->log_ctx);
629 dev->log_ctx = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000630 /* No one will access memory at this point */
Jason Wanga9709d62016-06-23 02:04:31 -0400631 vhost_umem_clean(dev->umem);
632 dev->umem = NULL;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400633 vhost_umem_clean(dev->iotlb);
634 dev->iotlb = NULL;
635 vhost_clear_msg(dev);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800636 wake_up_interruptible_poll(&dev->wait, EPOLLIN | EPOLLRDNORM);
Jason Wang04b96e52016-04-25 22:14:33 -0400637 WARN_ON(!llist_empty(&dev->work_list));
Eric Dumazet78b620c2010-08-31 02:05:57 +0000638 if (dev->worker) {
639 kthread_stop(dev->worker);
640 dev->worker = NULL;
641 }
Michael S. Tsirkin533a19b2010-10-06 15:34:38 +0200642 if (dev->mm)
643 mmput(dev->mm);
644 dev->mm = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000645}
Asias He6ac1afb2013-05-06 16:38:21 +0800646EXPORT_SYMBOL_GPL(vhost_dev_cleanup);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000647
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800648static bool log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000649{
650 u64 a = addr / VHOST_PAGE_SIZE / 8;
Krishna Kumard47effe2011-03-01 17:06:37 +0530651
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000652 /* Make sure 64 bit math will not overflow. */
653 if (a > ULONG_MAX - (unsigned long)log_base ||
654 a + (unsigned long)log_base > ULONG_MAX)
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800655 return false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000656
657 return access_ok(VERIFY_WRITE, log_base + a,
658 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
659}
660
Michael S. Tsirkinec33d032016-08-01 23:20:53 +0300661static bool vhost_overflow(u64 uaddr, u64 size)
662{
663 /* Make sure 64 bit math will not overflow. */
664 return uaddr > ULONG_MAX || size > ULONG_MAX || uaddr > ULONG_MAX - size;
665}
666
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000667/* Caller should have vq mutex and device mutex. */
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800668static bool vq_memory_access_ok(void __user *log_base, struct vhost_umem *umem,
669 int log_all)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000670{
Jason Wanga9709d62016-06-23 02:04:31 -0400671 struct vhost_umem_node *node;
Jeff Dike179b2842010-04-07 09:59:10 -0400672
Jason Wanga9709d62016-06-23 02:04:31 -0400673 if (!umem)
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800674 return false;
Jeff Dike179b2842010-04-07 09:59:10 -0400675
Jason Wanga9709d62016-06-23 02:04:31 -0400676 list_for_each_entry(node, &umem->umem_list, link) {
677 unsigned long a = node->userspace_addr;
678
Michael S. Tsirkinec33d032016-08-01 23:20:53 +0300679 if (vhost_overflow(node->userspace_addr, node->size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800680 return false;
Michael S. Tsirkinec33d032016-08-01 23:20:53 +0300681
682
683 if (!access_ok(VERIFY_WRITE, (void __user *)a,
Jason Wanga9709d62016-06-23 02:04:31 -0400684 node->size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800685 return false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000686 else if (log_all && !log_access_ok(log_base,
Jason Wanga9709d62016-06-23 02:04:31 -0400687 node->start,
688 node->size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800689 return false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000690 }
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800691 return true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000692}
693
Jason Wangf8894912017-02-28 17:56:02 +0800694static inline void __user *vhost_vq_meta_fetch(struct vhost_virtqueue *vq,
695 u64 addr, unsigned int size,
696 int type)
697{
698 const struct vhost_umem_node *node = vq->meta_iotlb[type];
699
700 if (!node)
701 return NULL;
702
703 return (void *)(uintptr_t)(node->userspace_addr + addr - node->start);
704}
705
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000706/* Can we switch to this memory table? */
707/* Caller should have device mutex but not vq mutex */
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800708static bool memory_access_ok(struct vhost_dev *d, struct vhost_umem *umem,
709 int log_all)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000710{
711 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530712
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000713 for (i = 0; i < d->nvqs; ++i) {
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800714 bool ok;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300715 bool log;
716
Asias He3ab2e422013-04-27 11:16:48 +0800717 mutex_lock(&d->vqs[i]->mutex);
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300718 log = log_all || vhost_has_feature(d->vqs[i], VHOST_F_LOG_ALL);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000719 /* If ring is inactive, will check when it's enabled. */
Asias He3ab2e422013-04-27 11:16:48 +0800720 if (d->vqs[i]->private_data)
Jason Wanga9709d62016-06-23 02:04:31 -0400721 ok = vq_memory_access_ok(d->vqs[i]->log_base,
722 umem, log);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000723 else
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800724 ok = true;
Asias He3ab2e422013-04-27 11:16:48 +0800725 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000726 if (!ok)
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800727 return false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000728 }
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800729 return true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000730}
731
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400732static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
733 struct iovec iov[], int iov_size, int access);
Jason Wangbfe2bc52016-06-23 02:04:30 -0400734
Michael S. Tsirkin72952cc2016-12-06 06:01:41 +0200735static int vhost_copy_to_user(struct vhost_virtqueue *vq, void __user *to,
Jason Wangbfe2bc52016-06-23 02:04:30 -0400736 const void *from, unsigned size)
737{
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400738 int ret;
Jason Wangbfe2bc52016-06-23 02:04:30 -0400739
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400740 if (!vq->iotlb)
741 return __copy_to_user(to, from, size);
742 else {
743 /* This function should be called after iotlb
744 * prefetch, which means we're sure that all vq
745 * could be access through iotlb. So -EAGAIN should
746 * not happen in this case.
747 */
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400748 struct iov_iter t;
Jason Wangf8894912017-02-28 17:56:02 +0800749 void __user *uaddr = vhost_vq_meta_fetch(vq,
750 (u64)(uintptr_t)to, size,
Eric Auger7ced6c92018-04-11 15:30:38 +0200751 VHOST_ADDR_USED);
Jason Wangf8894912017-02-28 17:56:02 +0800752
753 if (uaddr)
754 return __copy_to_user(uaddr, from, size);
755
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400756 ret = translate_desc(vq, (u64)(uintptr_t)to, size, vq->iotlb_iov,
757 ARRAY_SIZE(vq->iotlb_iov),
758 VHOST_ACCESS_WO);
759 if (ret < 0)
760 goto out;
761 iov_iter_init(&t, WRITE, vq->iotlb_iov, ret, size);
762 ret = copy_to_iter(from, size, &t);
763 if (ret == size)
764 ret = 0;
765 }
766out:
767 return ret;
768}
Jason Wangbfe2bc52016-06-23 02:04:30 -0400769
770static int vhost_copy_from_user(struct vhost_virtqueue *vq, void *to,
Michael S. Tsirkin72952cc2016-12-06 06:01:41 +0200771 void __user *from, unsigned size)
Jason Wangbfe2bc52016-06-23 02:04:30 -0400772{
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400773 int ret;
774
775 if (!vq->iotlb)
776 return __copy_from_user(to, from, size);
777 else {
778 /* This function should be called after iotlb
779 * prefetch, which means we're sure that vq
780 * could be access through iotlb. So -EAGAIN should
781 * not happen in this case.
782 */
Jason Wangf8894912017-02-28 17:56:02 +0800783 void __user *uaddr = vhost_vq_meta_fetch(vq,
784 (u64)(uintptr_t)from, size,
785 VHOST_ADDR_DESC);
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400786 struct iov_iter f;
Jason Wangf8894912017-02-28 17:56:02 +0800787
788 if (uaddr)
789 return __copy_from_user(to, uaddr, size);
790
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400791 ret = translate_desc(vq, (u64)(uintptr_t)from, size, vq->iotlb_iov,
792 ARRAY_SIZE(vq->iotlb_iov),
793 VHOST_ACCESS_RO);
794 if (ret < 0) {
795 vq_err(vq, "IOTLB translation failure: uaddr "
796 "%p size 0x%llx\n", from,
797 (unsigned long long) size);
798 goto out;
799 }
800 iov_iter_init(&f, READ, vq->iotlb_iov, ret, size);
801 ret = copy_from_iter(to, size, &f);
802 if (ret == size)
803 ret = 0;
804 }
805
806out:
807 return ret;
808}
809
Jason Wangf8894912017-02-28 17:56:02 +0800810static void __user *__vhost_get_user_slow(struct vhost_virtqueue *vq,
811 void __user *addr, unsigned int size,
812 int type)
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400813{
814 int ret;
815
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400816 ret = translate_desc(vq, (u64)(uintptr_t)addr, size, vq->iotlb_iov,
817 ARRAY_SIZE(vq->iotlb_iov),
818 VHOST_ACCESS_RO);
819 if (ret < 0) {
820 vq_err(vq, "IOTLB translation failure: uaddr "
821 "%p size 0x%llx\n", addr,
822 (unsigned long long) size);
823 return NULL;
824 }
825
826 if (ret != 1 || vq->iotlb_iov[0].iov_len != size) {
827 vq_err(vq, "Non atomic userspace memory access: uaddr "
828 "%p size 0x%llx\n", addr,
829 (unsigned long long) size);
830 return NULL;
831 }
832
833 return vq->iotlb_iov[0].iov_base;
834}
835
Jason Wangf8894912017-02-28 17:56:02 +0800836/* This function should be called after iotlb
837 * prefetch, which means we're sure that vq
838 * could be access through iotlb. So -EAGAIN should
839 * not happen in this case.
840 */
841static inline void __user *__vhost_get_user(struct vhost_virtqueue *vq,
842 void *addr, unsigned int size,
843 int type)
844{
845 void __user *uaddr = vhost_vq_meta_fetch(vq,
846 (u64)(uintptr_t)addr, size, type);
847 if (uaddr)
848 return uaddr;
849
850 return __vhost_get_user_slow(vq, addr, size, type);
851}
852
853#define vhost_put_user(vq, x, ptr) \
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400854({ \
855 int ret = -EFAULT; \
856 if (!vq->iotlb) { \
857 ret = __put_user(x, ptr); \
858 } else { \
859 __typeof__(ptr) to = \
Jason Wangf8894912017-02-28 17:56:02 +0800860 (__typeof__(ptr)) __vhost_get_user(vq, ptr, \
861 sizeof(*ptr), VHOST_ADDR_USED); \
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400862 if (to != NULL) \
863 ret = __put_user(x, to); \
864 else \
865 ret = -EFAULT; \
866 } \
867 ret; \
868})
869
Jason Wangf8894912017-02-28 17:56:02 +0800870#define vhost_get_user(vq, x, ptr, type) \
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400871({ \
872 int ret; \
873 if (!vq->iotlb) { \
874 ret = __get_user(x, ptr); \
875 } else { \
876 __typeof__(ptr) from = \
Jason Wangf8894912017-02-28 17:56:02 +0800877 (__typeof__(ptr)) __vhost_get_user(vq, ptr, \
878 sizeof(*ptr), \
879 type); \
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400880 if (from != NULL) \
881 ret = __get_user(x, from); \
882 else \
883 ret = -EFAULT; \
884 } \
885 ret; \
886})
887
Jason Wangf8894912017-02-28 17:56:02 +0800888#define vhost_get_avail(vq, x, ptr) \
889 vhost_get_user(vq, x, ptr, VHOST_ADDR_AVAIL)
890
891#define vhost_get_used(vq, x, ptr) \
892 vhost_get_user(vq, x, ptr, VHOST_ADDR_USED)
893
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400894static void vhost_dev_lock_vqs(struct vhost_dev *d)
895{
896 int i = 0;
897 for (i = 0; i < d->nvqs; ++i)
Jason Wange9cb42392018-01-23 17:27:25 +0800898 mutex_lock_nested(&d->vqs[i]->mutex, i);
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400899}
900
901static void vhost_dev_unlock_vqs(struct vhost_dev *d)
902{
903 int i = 0;
904 for (i = 0; i < d->nvqs; ++i)
905 mutex_unlock(&d->vqs[i]->mutex);
906}
907
908static int vhost_new_umem_range(struct vhost_umem *umem,
909 u64 start, u64 size, u64 end,
910 u64 userspace_addr, int perm)
911{
912 struct vhost_umem_node *tmp, *node = kmalloc(sizeof(*node), GFP_ATOMIC);
913
914 if (!node)
915 return -ENOMEM;
916
917 if (umem->numem == max_iotlb_entries) {
918 tmp = list_first_entry(&umem->umem_list, typeof(*tmp), link);
919 vhost_umem_free(umem, tmp);
920 }
921
922 node->start = start;
923 node->size = size;
924 node->last = end;
925 node->userspace_addr = userspace_addr;
926 node->perm = perm;
927 INIT_LIST_HEAD(&node->link);
928 list_add_tail(&node->link, &umem->umem_list);
929 vhost_umem_interval_tree_insert(node, &umem->umem_tree);
930 umem->numem++;
931
932 return 0;
933}
934
935static void vhost_del_umem_range(struct vhost_umem *umem,
936 u64 start, u64 end)
937{
938 struct vhost_umem_node *node;
939
940 while ((node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
941 start, end)))
942 vhost_umem_free(umem, node);
943}
944
945static void vhost_iotlb_notify_vq(struct vhost_dev *d,
946 struct vhost_iotlb_msg *msg)
947{
948 struct vhost_msg_node *node, *n;
949
950 spin_lock(&d->iotlb_lock);
951
952 list_for_each_entry_safe(node, n, &d->pending_list, node) {
953 struct vhost_iotlb_msg *vq_msg = &node->msg.iotlb;
954 if (msg->iova <= vq_msg->iova &&
Jason Wang2d66f992018-08-24 16:53:13 +0800955 msg->iova + msg->size - 1 >= vq_msg->iova &&
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400956 vq_msg->type == VHOST_IOTLB_MISS) {
957 vhost_poll_queue(&node->vq->poll);
958 list_del(&node->node);
959 kfree(node);
960 }
961 }
962
963 spin_unlock(&d->iotlb_lock);
964}
965
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800966static bool umem_access_ok(u64 uaddr, u64 size, int access)
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400967{
968 unsigned long a = uaddr;
969
Michael S. Tsirkinec33d032016-08-01 23:20:53 +0300970 /* Make sure 64 bit math will not overflow. */
971 if (vhost_overflow(uaddr, size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800972 return false;
Michael S. Tsirkinec33d032016-08-01 23:20:53 +0300973
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400974 if ((access & VHOST_ACCESS_RO) &&
975 !access_ok(VERIFY_READ, (void __user *)a, size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800976 return false;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400977 if ((access & VHOST_ACCESS_WO) &&
978 !access_ok(VERIFY_WRITE, (void __user *)a, size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800979 return false;
980 return true;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400981}
982
Michael S. Tsirkin72952cc2016-12-06 06:01:41 +0200983static int vhost_process_iotlb_msg(struct vhost_dev *dev,
984 struct vhost_iotlb_msg *msg)
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400985{
986 int ret = 0;
987
Jason Wang1b15ad62018-05-22 19:58:57 +0800988 mutex_lock(&dev->mutex);
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400989 vhost_dev_lock_vqs(dev);
990 switch (msg->type) {
991 case VHOST_IOTLB_UPDATE:
992 if (!dev->iotlb) {
993 ret = -EFAULT;
994 break;
995 }
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800996 if (!umem_access_ok(msg->uaddr, msg->size, msg->perm)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400997 ret = -EFAULT;
998 break;
999 }
Jason Wangf8894912017-02-28 17:56:02 +08001000 vhost_vq_meta_reset(dev);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001001 if (vhost_new_umem_range(dev->iotlb, msg->iova, msg->size,
1002 msg->iova + msg->size - 1,
1003 msg->uaddr, msg->perm)) {
1004 ret = -ENOMEM;
1005 break;
1006 }
1007 vhost_iotlb_notify_vq(dev, msg);
1008 break;
1009 case VHOST_IOTLB_INVALIDATE:
Jason Wang6f3180a2018-01-23 17:27:26 +08001010 if (!dev->iotlb) {
1011 ret = -EFAULT;
1012 break;
1013 }
Jason Wangf8894912017-02-28 17:56:02 +08001014 vhost_vq_meta_reset(dev);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001015 vhost_del_umem_range(dev->iotlb, msg->iova,
1016 msg->iova + msg->size - 1);
1017 break;
1018 default:
1019 ret = -EINVAL;
1020 break;
1021 }
1022
1023 vhost_dev_unlock_vqs(dev);
Jason Wang1b15ad62018-05-22 19:58:57 +08001024 mutex_unlock(&dev->mutex);
1025
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001026 return ret;
1027}
1028ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
1029 struct iov_iter *from)
1030{
Jason Wang429711a2018-08-06 11:17:47 +08001031 struct vhost_iotlb_msg msg;
1032 size_t offset;
1033 int type, ret;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001034
Jason Wang429711a2018-08-06 11:17:47 +08001035 ret = copy_from_iter(&type, sizeof(type), from);
1036 if (ret != sizeof(type))
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001037 goto done;
1038
Jason Wang429711a2018-08-06 11:17:47 +08001039 switch (type) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001040 case VHOST_IOTLB_MSG:
Jason Wang429711a2018-08-06 11:17:47 +08001041 /* There maybe a hole after type for V1 message type,
1042 * so skip it here.
1043 */
1044 offset = offsetof(struct vhost_msg, iotlb) - sizeof(int);
1045 break;
1046 case VHOST_IOTLB_MSG_V2:
1047 offset = sizeof(__u32);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001048 break;
1049 default:
1050 ret = -EINVAL;
Jason Wang429711a2018-08-06 11:17:47 +08001051 goto done;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001052 }
1053
Jason Wang429711a2018-08-06 11:17:47 +08001054 iov_iter_advance(from, offset);
1055 ret = copy_from_iter(&msg, sizeof(msg), from);
1056 if (ret != sizeof(msg))
1057 goto done;
1058 if (vhost_process_iotlb_msg(dev, &msg)) {
1059 ret = -EFAULT;
1060 goto done;
1061 }
1062
1063 ret = (type == VHOST_IOTLB_MSG) ? sizeof(struct vhost_msg) :
1064 sizeof(struct vhost_msg_v2);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001065done:
1066 return ret;
1067}
1068EXPORT_SYMBOL(vhost_chr_write_iter);
1069
Al Viroafc9a422017-07-03 06:39:46 -04001070__poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001071 poll_table *wait)
1072{
Al Viroafc9a422017-07-03 06:39:46 -04001073 __poll_t mask = 0;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001074
1075 poll_wait(file, &dev->wait, wait);
1076
1077 if (!list_empty(&dev->read_list))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001078 mask |= EPOLLIN | EPOLLRDNORM;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001079
1080 return mask;
1081}
1082EXPORT_SYMBOL(vhost_chr_poll);
1083
1084ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to,
1085 int noblock)
1086{
1087 DEFINE_WAIT(wait);
1088 struct vhost_msg_node *node;
1089 ssize_t ret = 0;
1090 unsigned size = sizeof(struct vhost_msg);
1091
1092 if (iov_iter_count(to) < size)
1093 return 0;
1094
1095 while (1) {
1096 if (!noblock)
1097 prepare_to_wait(&dev->wait, &wait,
1098 TASK_INTERRUPTIBLE);
1099
1100 node = vhost_dequeue_msg(dev, &dev->read_list);
1101 if (node)
1102 break;
1103 if (noblock) {
1104 ret = -EAGAIN;
1105 break;
1106 }
1107 if (signal_pending(current)) {
1108 ret = -ERESTARTSYS;
1109 break;
1110 }
1111 if (!dev->iotlb) {
1112 ret = -EBADFD;
1113 break;
1114 }
1115
1116 schedule();
1117 }
1118
1119 if (!noblock)
1120 finish_wait(&dev->wait, &wait);
1121
1122 if (node) {
Jason Wang429711a2018-08-06 11:17:47 +08001123 struct vhost_iotlb_msg *msg;
1124 void *start = &node->msg;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001125
Jason Wang429711a2018-08-06 11:17:47 +08001126 switch (node->msg.type) {
1127 case VHOST_IOTLB_MSG:
1128 size = sizeof(node->msg);
1129 msg = &node->msg.iotlb;
1130 break;
1131 case VHOST_IOTLB_MSG_V2:
1132 size = sizeof(node->msg_v2);
1133 msg = &node->msg_v2.iotlb;
1134 break;
1135 default:
1136 BUG();
1137 break;
1138 }
1139
1140 ret = copy_to_iter(start, size, to);
1141 if (ret != size || msg->type != VHOST_IOTLB_MISS) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001142 kfree(node);
1143 return ret;
1144 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001145 vhost_enqueue_msg(dev, &dev->pending_list, node);
1146 }
1147
1148 return ret;
1149}
1150EXPORT_SYMBOL_GPL(vhost_chr_read_iter);
1151
1152static int vhost_iotlb_miss(struct vhost_virtqueue *vq, u64 iova, int access)
1153{
1154 struct vhost_dev *dev = vq->dev;
1155 struct vhost_msg_node *node;
1156 struct vhost_iotlb_msg *msg;
Jason Wang429711a2018-08-06 11:17:47 +08001157 bool v2 = vhost_backend_has_feature(vq, VHOST_BACKEND_F_IOTLB_MSG_V2);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001158
Jason Wang429711a2018-08-06 11:17:47 +08001159 node = vhost_new_msg(vq, v2 ? VHOST_IOTLB_MSG_V2 : VHOST_IOTLB_MSG);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001160 if (!node)
1161 return -ENOMEM;
1162
Jason Wang429711a2018-08-06 11:17:47 +08001163 if (v2) {
1164 node->msg_v2.type = VHOST_IOTLB_MSG_V2;
1165 msg = &node->msg_v2.iotlb;
1166 } else {
1167 msg = &node->msg.iotlb;
1168 }
1169
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001170 msg->type = VHOST_IOTLB_MISS;
1171 msg->iova = iova;
1172 msg->perm = access;
1173
1174 vhost_enqueue_msg(dev, &dev->read_list, node);
1175
1176 return 0;
Jason Wangbfe2bc52016-06-23 02:04:30 -04001177}
1178
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001179static bool vq_access_ok(struct vhost_virtqueue *vq, unsigned int num,
1180 struct vring_desc __user *desc,
1181 struct vring_avail __user *avail,
1182 struct vring_used __user *used)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001183
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001184{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001185 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001186
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001187 return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
1188 access_ok(VERIFY_READ, avail,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001189 sizeof *avail + num * sizeof *avail->ring + s) &&
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001190 access_ok(VERIFY_WRITE, used,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001191 sizeof *used + num * sizeof *used->ring + s);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001192}
1193
Jason Wangf8894912017-02-28 17:56:02 +08001194static void vhost_vq_meta_update(struct vhost_virtqueue *vq,
1195 const struct vhost_umem_node *node,
1196 int type)
1197{
1198 int access = (type == VHOST_ADDR_USED) ?
1199 VHOST_ACCESS_WO : VHOST_ACCESS_RO;
1200
1201 if (likely(node->perm & access))
1202 vq->meta_iotlb[type] = node;
1203}
1204
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001205static bool iotlb_access_ok(struct vhost_virtqueue *vq,
1206 int access, u64 addr, u64 len, int type)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001207{
1208 const struct vhost_umem_node *node;
1209 struct vhost_umem *umem = vq->iotlb;
Michael S. Tsirkinca2c5b32017-08-21 22:33:33 +03001210 u64 s = 0, size, orig_addr = addr, last = addr + len - 1;
Jason Wangf8894912017-02-28 17:56:02 +08001211
1212 if (vhost_vq_meta_fetch(vq, addr, len, type))
1213 return true;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001214
1215 while (len > s) {
1216 node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
1217 addr,
Michael S. Tsirkinca2c5b32017-08-21 22:33:33 +03001218 last);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001219 if (node == NULL || node->start > addr) {
1220 vhost_iotlb_miss(vq, addr, access);
1221 return false;
1222 } else if (!(node->perm & access)) {
1223 /* Report the possible access violation by
1224 * request another translation from userspace.
1225 */
1226 return false;
1227 }
1228
1229 size = node->size - addr + node->start;
Jason Wangf8894912017-02-28 17:56:02 +08001230
1231 if (orig_addr == addr && size >= len)
1232 vhost_vq_meta_update(vq, node, type);
1233
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001234 s += size;
1235 addr += size;
1236 }
1237
1238 return true;
1239}
1240
1241int vq_iotlb_prefetch(struct vhost_virtqueue *vq)
1242{
1243 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
1244 unsigned int num = vq->num;
1245
1246 if (!vq->iotlb)
1247 return 1;
1248
1249 return iotlb_access_ok(vq, VHOST_ACCESS_RO, (u64)(uintptr_t)vq->desc,
Jason Wangf8894912017-02-28 17:56:02 +08001250 num * sizeof(*vq->desc), VHOST_ADDR_DESC) &&
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001251 iotlb_access_ok(vq, VHOST_ACCESS_RO, (u64)(uintptr_t)vq->avail,
1252 sizeof *vq->avail +
Jason Wangf8894912017-02-28 17:56:02 +08001253 num * sizeof(*vq->avail->ring) + s,
1254 VHOST_ADDR_AVAIL) &&
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001255 iotlb_access_ok(vq, VHOST_ACCESS_WO, (u64)(uintptr_t)vq->used,
1256 sizeof *vq->used +
Jason Wangf8894912017-02-28 17:56:02 +08001257 num * sizeof(*vq->used->ring) + s,
1258 VHOST_ADDR_USED);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001259}
1260EXPORT_SYMBOL_GPL(vq_iotlb_prefetch);
1261
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001262/* Can we log writes? */
1263/* Caller should have device mutex but not vq mutex */
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001264bool vhost_log_access_ok(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001265{
Jason Wanga9709d62016-06-23 02:04:31 -04001266 return memory_access_ok(dev, dev->umem, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001267}
Asias He6ac1afb2013-05-06 16:38:21 +08001268EXPORT_SYMBOL_GPL(vhost_log_access_ok);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001269
1270/* Verify access for write logging. */
1271/* Caller should have vq mutex and device mutex */
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001272static bool vq_log_access_ok(struct vhost_virtqueue *vq,
1273 void __user *log_base)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001274{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001275 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
Arnd Bergmann28457ee2010-03-09 19:24:45 +01001276
Jason Wanga9709d62016-06-23 02:04:31 -04001277 return vq_memory_access_ok(log_base, vq->umem,
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001278 vhost_has_feature(vq, VHOST_F_LOG_ALL)) &&
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001279 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
1280 sizeof *vq->used +
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001281 vq->num * sizeof *vq->used->ring + s));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001282}
1283
1284/* Can we start vq? */
1285/* Caller should have vq mutex and device mutex */
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001286bool vhost_vq_access_ok(struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001287{
Stefan Hajnoczid14d2b72018-04-11 10:35:40 +08001288 if (!vq_log_access_ok(vq, vq->log_base))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001289 return false;
Jason Wangd65026c2018-03-29 16:00:04 +08001290
Stefan Hajnoczid14d2b72018-04-11 10:35:40 +08001291 /* Access validation occurs at prefetch time with IOTLB */
1292 if (vq->iotlb)
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001293 return true;
Jason Wangd65026c2018-03-29 16:00:04 +08001294
1295 return vq_access_ok(vq, vq->num, vq->desc, vq->avail, vq->used);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001296}
Asias He6ac1afb2013-05-06 16:38:21 +08001297EXPORT_SYMBOL_GPL(vhost_vq_access_ok);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001298
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001299static struct vhost_umem *vhost_umem_alloc(void)
1300{
Michal Hocko6c5ab652017-05-08 15:57:15 -07001301 struct vhost_umem *umem = kvzalloc(sizeof(*umem), GFP_KERNEL);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001302
1303 if (!umem)
1304 return NULL;
1305
Davidlohr Buesof808c132017-09-08 16:15:08 -07001306 umem->umem_tree = RB_ROOT_CACHED;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001307 umem->numem = 0;
1308 INIT_LIST_HEAD(&umem->umem_list);
1309
1310 return umem;
1311}
1312
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001313static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
1314{
Jason Wanga9709d62016-06-23 02:04:31 -04001315 struct vhost_memory mem, *newmem;
1316 struct vhost_memory_region *region;
Jason Wanga9709d62016-06-23 02:04:31 -04001317 struct vhost_umem *newumem, *oldumem;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001318 unsigned long size = offsetof(struct vhost_memory, regions);
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001319 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +05301320
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001321 if (copy_from_user(&mem, m, size))
1322 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001323 if (mem.padding)
1324 return -EOPNOTSUPP;
Igor Mammedovc9ce42f2015-07-02 15:08:11 +02001325 if (mem.nregions > max_mem_regions)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001326 return -E2BIG;
Matthew Wilcoxb2303d72018-06-07 07:57:18 -07001327 newmem = kvzalloc(struct_size(newmem, regions, mem.nregions),
1328 GFP_KERNEL);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001329 if (!newmem)
1330 return -ENOMEM;
1331
1332 memcpy(newmem, &mem, size);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001333 if (copy_from_user(newmem->regions, m->regions,
1334 mem.nregions * sizeof *m->regions)) {
Igor Mammedovbcfeaca2015-06-16 18:33:35 +02001335 kvfree(newmem);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001336 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001337 }
1338
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001339 newumem = vhost_umem_alloc();
Jason Wanga9709d62016-06-23 02:04:31 -04001340 if (!newumem) {
Igor Mammedov4de72552015-07-01 11:07:09 +02001341 kvfree(newmem);
Jason Wanga9709d62016-06-23 02:04:31 -04001342 return -ENOMEM;
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +09001343 }
Jason Wanga9709d62016-06-23 02:04:31 -04001344
Jason Wanga9709d62016-06-23 02:04:31 -04001345 for (region = newmem->regions;
1346 region < newmem->regions + mem.nregions;
1347 region++) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001348 if (vhost_new_umem_range(newumem,
1349 region->guest_phys_addr,
1350 region->memory_size,
1351 region->guest_phys_addr +
1352 region->memory_size - 1,
1353 region->userspace_addr,
1354 VHOST_ACCESS_RW))
Jason Wanga9709d62016-06-23 02:04:31 -04001355 goto err;
Jason Wanga9709d62016-06-23 02:04:31 -04001356 }
1357
1358 if (!memory_access_ok(d, newumem, 0))
1359 goto err;
1360
1361 oldumem = d->umem;
1362 d->umem = newumem;
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001363
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001364 /* All memory accesses are done under some VQ mutex. */
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001365 for (i = 0; i < d->nvqs; ++i) {
1366 mutex_lock(&d->vqs[i]->mutex);
Jason Wanga9709d62016-06-23 02:04:31 -04001367 d->vqs[i]->umem = newumem;
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001368 mutex_unlock(&d->vqs[i]->mutex);
1369 }
Jason Wanga9709d62016-06-23 02:04:31 -04001370
1371 kvfree(newmem);
1372 vhost_umem_clean(oldumem);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001373 return 0;
Jason Wanga9709d62016-06-23 02:04:31 -04001374
1375err:
1376 vhost_umem_clean(newumem);
1377 kvfree(newmem);
1378 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001379}
1380
Sonny Rao26b36602018-03-14 10:05:06 -07001381long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001382{
Al Virocecb46f2012-08-27 14:21:39 -04001383 struct file *eventfp, *filep = NULL;
1384 bool pollstart = false, pollstop = false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001385 struct eventfd_ctx *ctx = NULL;
1386 u32 __user *idxp = argp;
1387 struct vhost_virtqueue *vq;
1388 struct vhost_vring_state s;
1389 struct vhost_vring_file f;
1390 struct vhost_vring_addr a;
1391 u32 idx;
1392 long r;
1393
1394 r = get_user(idx, idxp);
1395 if (r < 0)
1396 return r;
Krishna Kumar0f3d9a12010-05-25 11:10:36 +05301397 if (idx >= d->nvqs)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001398 return -ENOBUFS;
1399
Asias He3ab2e422013-04-27 11:16:48 +08001400 vq = d->vqs[idx];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001401
1402 mutex_lock(&vq->mutex);
1403
1404 switch (ioctl) {
1405 case VHOST_SET_VRING_NUM:
1406 /* Resizing ring with an active backend?
1407 * You don't want to do that. */
1408 if (vq->private_data) {
1409 r = -EBUSY;
1410 break;
1411 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001412 if (copy_from_user(&s, argp, sizeof s)) {
1413 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001414 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001415 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001416 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
1417 r = -EINVAL;
1418 break;
1419 }
1420 vq->num = s.num;
1421 break;
1422 case VHOST_SET_VRING_BASE:
1423 /* Moving base with an active backend?
1424 * You don't want to do that. */
1425 if (vq->private_data) {
1426 r = -EBUSY;
1427 break;
1428 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001429 if (copy_from_user(&s, argp, sizeof s)) {
1430 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001431 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001432 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001433 if (s.num > 0xffff) {
1434 r = -EINVAL;
1435 break;
1436 }
Jason Wang8d658432017-07-27 11:22:05 +08001437 vq->last_avail_idx = s.num;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001438 /* Forget the cached index value. */
1439 vq->avail_idx = vq->last_avail_idx;
1440 break;
1441 case VHOST_GET_VRING_BASE:
1442 s.index = idx;
1443 s.num = vq->last_avail_idx;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001444 if (copy_to_user(argp, &s, sizeof s))
1445 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001446 break;
1447 case VHOST_SET_VRING_ADDR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001448 if (copy_from_user(&a, argp, sizeof a)) {
1449 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001450 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001451 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001452 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
1453 r = -EOPNOTSUPP;
1454 break;
1455 }
1456 /* For 32bit, verify that the top 32bits of the user
1457 data are set to zero. */
1458 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
1459 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
1460 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
1461 r = -EFAULT;
1462 break;
1463 }
Michael S. Tsirkin5d9a07b2014-12-21 01:00:23 +02001464
1465 /* Make sure it's safe to cast pointers to vring types. */
1466 BUILD_BUG_ON(__alignof__ *vq->avail > VRING_AVAIL_ALIGN_SIZE);
1467 BUILD_BUG_ON(__alignof__ *vq->used > VRING_USED_ALIGN_SIZE);
1468 if ((a.avail_user_addr & (VRING_AVAIL_ALIGN_SIZE - 1)) ||
1469 (a.used_user_addr & (VRING_USED_ALIGN_SIZE - 1)) ||
Michael S. Tsirkind5424832015-11-16 16:57:08 +02001470 (a.log_guest_addr & (VRING_USED_ALIGN_SIZE - 1))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001471 r = -EINVAL;
1472 break;
1473 }
1474
1475 /* We only verify access here if backend is configured.
1476 * If it is not, we don't as size might not have been setup.
1477 * We will verify when backend is configured. */
1478 if (vq->private_data) {
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001479 if (!vq_access_ok(vq, vq->num,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001480 (void __user *)(unsigned long)a.desc_user_addr,
1481 (void __user *)(unsigned long)a.avail_user_addr,
1482 (void __user *)(unsigned long)a.used_user_addr)) {
1483 r = -EINVAL;
1484 break;
1485 }
1486
1487 /* Also validate log access for used ring if enabled. */
1488 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
1489 !log_access_ok(vq->log_base, a.log_guest_addr,
1490 sizeof *vq->used +
1491 vq->num * sizeof *vq->used->ring)) {
1492 r = -EINVAL;
1493 break;
1494 }
1495 }
1496
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001497 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
1498 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
1499 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
1500 vq->log_addr = a.log_guest_addr;
1501 vq->used = (void __user *)(unsigned long)a.used_user_addr;
1502 break;
1503 case VHOST_SET_VRING_KICK:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001504 if (copy_from_user(&f, argp, sizeof f)) {
1505 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001506 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001507 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001508 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +02001509 if (IS_ERR(eventfp)) {
1510 r = PTR_ERR(eventfp);
1511 break;
1512 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001513 if (eventfp != vq->kick) {
Al Virocecb46f2012-08-27 14:21:39 -04001514 pollstop = (filep = vq->kick) != NULL;
1515 pollstart = (vq->kick = eventfp) != NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001516 } else
1517 filep = eventfp;
1518 break;
1519 case VHOST_SET_VRING_CALL:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001520 if (copy_from_user(&f, argp, sizeof f)) {
1521 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001522 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001523 }
Eric Biggerse050c7d2018-01-06 14:52:19 -08001524 ctx = f.fd == -1 ? NULL : eventfd_ctx_fdget(f.fd);
1525 if (IS_ERR(ctx)) {
1526 r = PTR_ERR(ctx);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +02001527 break;
1528 }
Eric Biggerse050c7d2018-01-06 14:52:19 -08001529 swap(ctx, vq->call_ctx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001530 break;
1531 case VHOST_SET_VRING_ERR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001532 if (copy_from_user(&f, argp, sizeof f)) {
1533 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001534 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001535 }
Eric Biggers09f332a2018-01-06 14:52:20 -08001536 ctx = f.fd == -1 ? NULL : eventfd_ctx_fdget(f.fd);
1537 if (IS_ERR(ctx)) {
1538 r = PTR_ERR(ctx);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +02001539 break;
1540 }
Eric Biggers09f332a2018-01-06 14:52:20 -08001541 swap(ctx, vq->error_ctx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001542 break;
Greg Kurz2751c982015-04-24 14:27:24 +02001543 case VHOST_SET_VRING_ENDIAN:
1544 r = vhost_set_vring_endian(vq, argp);
1545 break;
1546 case VHOST_GET_VRING_ENDIAN:
1547 r = vhost_get_vring_endian(vq, idx, argp);
1548 break;
Jason Wang03088132016-03-04 06:24:53 -05001549 case VHOST_SET_VRING_BUSYLOOP_TIMEOUT:
1550 if (copy_from_user(&s, argp, sizeof(s))) {
1551 r = -EFAULT;
1552 break;
1553 }
1554 vq->busyloop_timeout = s.num;
1555 break;
1556 case VHOST_GET_VRING_BUSYLOOP_TIMEOUT:
1557 s.index = idx;
1558 s.num = vq->busyloop_timeout;
1559 if (copy_to_user(argp, &s, sizeof(s)))
1560 r = -EFAULT;
1561 break;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001562 default:
1563 r = -ENOIOCTLCMD;
1564 }
1565
1566 if (pollstop && vq->handle_kick)
1567 vhost_poll_stop(&vq->poll);
1568
Eric Biggerse050c7d2018-01-06 14:52:19 -08001569 if (!IS_ERR_OR_NULL(ctx))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001570 eventfd_ctx_put(ctx);
1571 if (filep)
1572 fput(filep);
1573
1574 if (pollstart && vq->handle_kick)
Jason Wang2b8b3282013-01-28 01:05:18 +00001575 r = vhost_poll_start(&vq->poll, vq->kick);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001576
1577 mutex_unlock(&vq->mutex);
1578
1579 if (pollstop && vq->handle_kick)
1580 vhost_poll_flush(&vq->poll);
1581 return r;
1582}
Asias He6ac1afb2013-05-06 16:38:21 +08001583EXPORT_SYMBOL_GPL(vhost_vring_ioctl);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001584
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001585int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled)
1586{
1587 struct vhost_umem *niotlb, *oiotlb;
1588 int i;
1589
1590 niotlb = vhost_umem_alloc();
1591 if (!niotlb)
1592 return -ENOMEM;
1593
1594 oiotlb = d->iotlb;
1595 d->iotlb = niotlb;
1596
1597 for (i = 0; i < d->nvqs; ++i) {
Jason Wangb13f9c62018-08-08 11:43:04 +08001598 struct vhost_virtqueue *vq = d->vqs[i];
1599
1600 mutex_lock(&vq->mutex);
1601 vq->iotlb = niotlb;
1602 __vhost_vq_meta_reset(vq);
1603 mutex_unlock(&vq->mutex);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001604 }
1605
1606 vhost_umem_clean(oiotlb);
1607
1608 return 0;
1609}
1610EXPORT_SYMBOL_GPL(vhost_init_device_iotlb);
1611
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001612/* Caller must have device mutex */
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001613long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001614{
Eric Biggersd25cc432018-01-06 14:52:21 -08001615 struct eventfd_ctx *ctx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001616 u64 p;
1617 long r;
1618 int i, fd;
1619
1620 /* If you are not the owner, you can become one */
1621 if (ioctl == VHOST_SET_OWNER) {
1622 r = vhost_dev_set_owner(d);
1623 goto done;
1624 }
1625
1626 /* You must be the owner to do anything else */
1627 r = vhost_dev_check_owner(d);
1628 if (r)
1629 goto done;
1630
1631 switch (ioctl) {
1632 case VHOST_SET_MEM_TABLE:
1633 r = vhost_set_memory(d, argp);
1634 break;
1635 case VHOST_SET_LOG_BASE:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001636 if (copy_from_user(&p, argp, sizeof p)) {
1637 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001638 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001639 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001640 if ((u64)(unsigned long)p != p) {
1641 r = -EFAULT;
1642 break;
1643 }
1644 for (i = 0; i < d->nvqs; ++i) {
1645 struct vhost_virtqueue *vq;
1646 void __user *base = (void __user *)(unsigned long)p;
Asias He3ab2e422013-04-27 11:16:48 +08001647 vq = d->vqs[i];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001648 mutex_lock(&vq->mutex);
1649 /* If ring is inactive, will check when it's enabled. */
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001650 if (vq->private_data && !vq_log_access_ok(vq, base))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001651 r = -EFAULT;
1652 else
1653 vq->log_base = base;
1654 mutex_unlock(&vq->mutex);
1655 }
1656 break;
1657 case VHOST_SET_LOG_FD:
1658 r = get_user(fd, (int __user *)argp);
1659 if (r < 0)
1660 break;
Eric Biggersd25cc432018-01-06 14:52:21 -08001661 ctx = fd == -1 ? NULL : eventfd_ctx_fdget(fd);
1662 if (IS_ERR(ctx)) {
1663 r = PTR_ERR(ctx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001664 break;
1665 }
Eric Biggersd25cc432018-01-06 14:52:21 -08001666 swap(ctx, d->log_ctx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001667 for (i = 0; i < d->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +08001668 mutex_lock(&d->vqs[i]->mutex);
1669 d->vqs[i]->log_ctx = d->log_ctx;
1670 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001671 }
1672 if (ctx)
1673 eventfd_ctx_put(ctx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001674 break;
1675 default:
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001676 r = -ENOIOCTLCMD;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001677 break;
1678 }
1679done:
1680 return r;
1681}
Asias He6ac1afb2013-05-06 16:38:21 +08001682EXPORT_SYMBOL_GPL(vhost_dev_ioctl);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001683
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001684/* TODO: This is really inefficient. We need something like get_user()
1685 * (instruction directly accesses the data, with an exception table entry
1686 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
1687 */
1688static int set_bit_to_user(int nr, void __user *addr)
1689{
1690 unsigned long log = (unsigned long)addr;
1691 struct page *page;
1692 void *base;
1693 int bit = nr + (log % PAGE_SIZE) * 8;
1694 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301695
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001696 r = get_user_pages_fast(log, 1, 1, &page);
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +02001697 if (r < 0)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001698 return r;
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +02001699 BUG_ON(r != 1);
Cong Wangc6daa7f2011-11-25 23:14:26 +08001700 base = kmap_atomic(page);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001701 set_bit(bit, base);
Cong Wangc6daa7f2011-11-25 23:14:26 +08001702 kunmap_atomic(base);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001703 set_page_dirty_lock(page);
1704 put_page(page);
1705 return 0;
1706}
1707
1708static int log_write(void __user *log_base,
1709 u64 write_address, u64 write_length)
1710{
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001711 u64 write_page = write_address / VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001712 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301713
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001714 if (!write_length)
1715 return 0;
Michael S. Tsirkin3bf9be42010-11-29 10:19:07 +02001716 write_length += write_address % VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001717 for (;;) {
1718 u64 base = (u64)(unsigned long)log_base;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001719 u64 log = base + write_page / 8;
1720 int bit = write_page % 8;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001721 if ((u64)(unsigned long)log != log)
1722 return -EFAULT;
1723 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
1724 if (r < 0)
1725 return r;
1726 if (write_length <= VHOST_PAGE_SIZE)
1727 break;
1728 write_length -= VHOST_PAGE_SIZE;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001729 write_page += 1;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001730 }
1731 return r;
1732}
1733
1734int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
1735 unsigned int log_num, u64 len)
1736{
1737 int i, r;
1738
1739 /* Make sure data written is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001740 smp_wmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001741 for (i = 0; i < log_num; ++i) {
1742 u64 l = min(log[i].len, len);
1743 r = log_write(vq->log_base, log[i].addr, l);
1744 if (r < 0)
1745 return r;
1746 len -= l;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +02001747 if (!len) {
1748 if (vq->log_ctx)
1749 eventfd_signal(vq->log_ctx, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001750 return 0;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +02001751 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001752 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001753 /* Length written exceeds what we have stored. This is a bug. */
1754 BUG();
1755 return 0;
1756}
Asias He6ac1afb2013-05-06 16:38:21 +08001757EXPORT_SYMBOL_GPL(vhost_log_write);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001758
Jason Wang2723fea2011-06-21 18:04:38 +08001759static int vhost_update_used_flags(struct vhost_virtqueue *vq)
1760{
1761 void __user *used;
Jason Wangbfe2bc52016-06-23 02:04:30 -04001762 if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->used_flags),
1763 &vq->used->flags) < 0)
Jason Wang2723fea2011-06-21 18:04:38 +08001764 return -EFAULT;
1765 if (unlikely(vq->log_used)) {
1766 /* Make sure the flag is seen before log. */
1767 smp_wmb();
1768 /* Log used flag write. */
1769 used = &vq->used->flags;
1770 log_write(vq->log_base, vq->log_addr +
1771 (used - (void __user *)vq->used),
1772 sizeof vq->used->flags);
1773 if (vq->log_ctx)
1774 eventfd_signal(vq->log_ctx, 1);
1775 }
1776 return 0;
1777}
1778
1779static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
1780{
Jason Wangbfe2bc52016-06-23 02:04:30 -04001781 if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->avail_idx),
1782 vhost_avail_event(vq)))
Jason Wang2723fea2011-06-21 18:04:38 +08001783 return -EFAULT;
1784 if (unlikely(vq->log_used)) {
1785 void __user *used;
1786 /* Make sure the event is seen before log. */
1787 smp_wmb();
1788 /* Log avail event write */
1789 used = vhost_avail_event(vq);
1790 log_write(vq->log_base, vq->log_addr +
1791 (used - (void __user *)vq->used),
1792 sizeof *vhost_avail_event(vq));
1793 if (vq->log_ctx)
1794 eventfd_signal(vq->log_ctx, 1);
1795 }
1796 return 0;
1797}
1798
Greg Kurz80f7d032016-02-16 15:59:44 +01001799int vhost_vq_init_access(struct vhost_virtqueue *vq)
Jason Wang2723fea2011-06-21 18:04:38 +08001800{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001801 __virtio16 last_used_idx;
Jason Wang2723fea2011-06-21 18:04:38 +08001802 int r;
Greg Kurze1f33be2016-02-16 15:54:28 +01001803 bool is_le = vq->is_le;
1804
Halil Pasiccda8bba2017-01-30 11:09:36 +01001805 if (!vq->private_data)
Jason Wang2723fea2011-06-21 18:04:38 +08001806 return 0;
Greg Kurz2751c982015-04-24 14:27:24 +02001807
1808 vhost_init_is_le(vq);
Jason Wang2723fea2011-06-21 18:04:38 +08001809
1810 r = vhost_update_used_flags(vq);
1811 if (r)
Greg Kurze1f33be2016-02-16 15:54:28 +01001812 goto err;
Jason Wang2723fea2011-06-21 18:04:38 +08001813 vq->signalled_used_valid = false;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001814 if (!vq->iotlb &&
1815 !access_ok(VERIFY_READ, &vq->used->idx, sizeof vq->used->idx)) {
Greg Kurze1f33be2016-02-16 15:54:28 +01001816 r = -EFAULT;
1817 goto err;
1818 }
Jason Wangf8894912017-02-28 17:56:02 +08001819 r = vhost_get_used(vq, last_used_idx, &vq->used->idx);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001820 if (r) {
1821 vq_err(vq, "Can't access used idx at %p\n",
1822 &vq->used->idx);
Greg Kurze1f33be2016-02-16 15:54:28 +01001823 goto err;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001824 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001825 vq->last_used_idx = vhost16_to_cpu(vq, last_used_idx);
Michael S. Tsirkin64f7f052014-12-01 17:39:39 +02001826 return 0;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001827
Greg Kurze1f33be2016-02-16 15:54:28 +01001828err:
1829 vq->is_le = is_le;
1830 return r;
Jason Wang2723fea2011-06-21 18:04:38 +08001831}
Greg Kurz80f7d032016-02-16 15:59:44 +01001832EXPORT_SYMBOL_GPL(vhost_vq_init_access);
Jason Wang2723fea2011-06-21 18:04:38 +08001833
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001834static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001835 struct iovec iov[], int iov_size, int access)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001836{
Jason Wanga9709d62016-06-23 02:04:31 -04001837 const struct vhost_umem_node *node;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001838 struct vhost_dev *dev = vq->dev;
1839 struct vhost_umem *umem = dev->iotlb ? dev->iotlb : dev->umem;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001840 struct iovec *_iov;
1841 u64 s = 0;
1842 int ret = 0;
1843
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001844 while ((u64)len > s) {
1845 u64 size;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001846 if (unlikely(ret >= iov_size)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001847 ret = -ENOBUFS;
1848 break;
1849 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001850
Jason Wanga9709d62016-06-23 02:04:31 -04001851 node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
1852 addr, addr + len - 1);
1853 if (node == NULL || node->start > addr) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001854 if (umem != dev->iotlb) {
1855 ret = -EFAULT;
1856 break;
1857 }
1858 ret = -EAGAIN;
1859 break;
1860 } else if (!(node->perm & access)) {
1861 ret = -EPERM;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001862 break;
1863 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001864
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001865 _iov = iov + ret;
Jason Wanga9709d62016-06-23 02:04:31 -04001866 size = node->size - addr + node->start;
Michael S. Tsirkinbd971202012-11-26 05:57:27 +00001867 _iov->iov_len = min((u64)len - s, size);
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001868 _iov->iov_base = (void __user *)(unsigned long)
Jason Wanga9709d62016-06-23 02:04:31 -04001869 (node->userspace_addr + addr - node->start);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001870 s += size;
1871 addr += size;
1872 ++ret;
1873 }
1874
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001875 if (ret == -EAGAIN)
1876 vhost_iotlb_miss(vq, addr, access);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001877 return ret;
1878}
1879
1880/* Each buffer in the virtqueues is actually a chain of descriptors. This
1881 * function returns the next descriptor in the chain,
1882 * or -1U if we're at the end. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001883static unsigned next_desc(struct vhost_virtqueue *vq, struct vring_desc *desc)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001884{
1885 unsigned int next;
1886
1887 /* If this descriptor says it doesn't chain, we're done. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001888 if (!(desc->flags & cpu_to_vhost16(vq, VRING_DESC_F_NEXT)))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001889 return -1U;
1890
1891 /* Check they're not leading us off end of descriptors. */
Paul E. McKenney3a5db0b2017-11-27 09:45:10 -08001892 next = vhost16_to_cpu(vq, READ_ONCE(desc->next));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001893 return next;
1894}
1895
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001896static int get_indirect(struct vhost_virtqueue *vq,
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001897 struct iovec iov[], unsigned int iov_size,
1898 unsigned int *out_num, unsigned int *in_num,
1899 struct vhost_log *log, unsigned int *log_num,
1900 struct vring_desc *indirect)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001901{
1902 struct vring_desc desc;
1903 unsigned int i = 0, count, found = 0;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001904 u32 len = vhost32_to_cpu(vq, indirect->len);
Al Viroaad9a1c2014-12-10 14:49:01 -05001905 struct iov_iter from;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001906 int ret, access;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001907
1908 /* Sanity check */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001909 if (unlikely(len % sizeof desc)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001910 vq_err(vq, "Invalid length in indirect descriptor: "
1911 "len 0x%llx not multiple of 0x%zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001912 (unsigned long long)len,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001913 sizeof desc);
1914 return -EINVAL;
1915 }
1916
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001917 ret = translate_desc(vq, vhost64_to_cpu(vq, indirect->addr), len, vq->indirect,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001918 UIO_MAXIOV, VHOST_ACCESS_RO);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001919 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001920 if (ret != -EAGAIN)
1921 vq_err(vq, "Translation failure %d in indirect.\n", ret);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001922 return ret;
1923 }
Al Viroaad9a1c2014-12-10 14:49:01 -05001924 iov_iter_init(&from, READ, vq->indirect, ret, len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001925
1926 /* We will use the result as an address to read from, so most
1927 * architectures only need a compiler barrier here. */
1928 read_barrier_depends();
1929
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001930 count = len / sizeof desc;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001931 /* Buffers are chained via a 16 bit next field, so
1932 * we can have at most 2^16 of these. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001933 if (unlikely(count > USHRT_MAX + 1)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001934 vq_err(vq, "Indirect buffer length too big: %d\n",
1935 indirect->len);
1936 return -E2BIG;
1937 }
1938
1939 do {
1940 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001941 if (unlikely(++found > count)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001942 vq_err(vq, "Loop detected: last one at %u "
1943 "indirect size %u\n",
1944 i, count);
1945 return -EINVAL;
1946 }
Al Virocbbd26b2016-11-01 22:09:04 -04001947 if (unlikely(!copy_from_iter_full(&desc, sizeof(desc), &from))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001948 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001949 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001950 return -EINVAL;
1951 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001952 if (unlikely(desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001953 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001954 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001955 return -EINVAL;
1956 }
1957
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001958 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE))
1959 access = VHOST_ACCESS_WO;
1960 else
1961 access = VHOST_ACCESS_RO;
1962
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001963 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
1964 vhost32_to_cpu(vq, desc.len), iov + iov_count,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001965 iov_size - iov_count, access);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001966 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001967 if (ret != -EAGAIN)
1968 vq_err(vq, "Translation failure %d indirect idx %d\n",
1969 ret, i);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001970 return ret;
1971 }
1972 /* If this is an input descriptor, increment that count. */
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001973 if (access == VHOST_ACCESS_WO) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001974 *in_num += ret;
1975 if (unlikely(log)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001976 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
1977 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001978 ++*log_num;
1979 }
1980 } else {
1981 /* If it's an output descriptor, they're all supposed
1982 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001983 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001984 vq_err(vq, "Indirect descriptor "
1985 "has out after in: idx %d\n", i);
1986 return -EINVAL;
1987 }
1988 *out_num += ret;
1989 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001990 } while ((i = next_desc(vq, &desc)) != -1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001991 return 0;
1992}
1993
1994/* This looks in the virtqueue and for the first available buffer, and converts
1995 * it to an iovec for convenient access. Since descriptors consist of some
1996 * number of output then some number of input descriptors, it's actually two
1997 * iovecs, but we pack them into one and note how many of each there were.
1998 *
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001999 * This function returns the descriptor number found, or vq->num (which is
2000 * never a valid descriptor number) if none was found. A negative code is
2001 * returned on error. */
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03002002int vhost_get_vq_desc(struct vhost_virtqueue *vq,
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002003 struct iovec iov[], unsigned int iov_size,
2004 unsigned int *out_num, unsigned int *in_num,
2005 struct vhost_log *log, unsigned int *log_num)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002006{
2007 struct vring_desc desc;
2008 unsigned int i, head, found = 0;
2009 u16 last_avail_idx;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002010 __virtio16 avail_idx;
2011 __virtio16 ring_head;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002012 int ret, access;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002013
2014 /* Check it isn't doing very strange things with descriptor numbers. */
2015 last_avail_idx = vq->last_avail_idx;
Jason Wange3b56cd2017-02-07 15:49:50 +08002016
2017 if (vq->avail_idx == vq->last_avail_idx) {
Jason Wangf8894912017-02-28 17:56:02 +08002018 if (unlikely(vhost_get_avail(vq, avail_idx, &vq->avail->idx))) {
Jason Wange3b56cd2017-02-07 15:49:50 +08002019 vq_err(vq, "Failed to access avail idx at %p\n",
2020 &vq->avail->idx);
2021 return -EFAULT;
2022 }
2023 vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
2024
2025 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
2026 vq_err(vq, "Guest moved used index from %u to %u",
2027 last_avail_idx, vq->avail_idx);
2028 return -EFAULT;
2029 }
2030
2031 /* If there's nothing new since last we looked, return
2032 * invalid.
2033 */
2034 if (vq->avail_idx == last_avail_idx)
2035 return vq->num;
2036
2037 /* Only get avail ring entries after they have been
2038 * exposed by guest.
2039 */
2040 smp_rmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002041 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002042
2043 /* Grab the next descriptor number they're advertising, and increment
2044 * the index we've seen. */
Jason Wangf8894912017-02-28 17:56:02 +08002045 if (unlikely(vhost_get_avail(vq, ring_head,
Jason Wangbfe2bc52016-06-23 02:04:30 -04002046 &vq->avail->ring[last_avail_idx & (vq->num - 1)]))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002047 vq_err(vq, "Failed to read head: idx %d address %p\n",
2048 last_avail_idx,
2049 &vq->avail->ring[last_avail_idx % vq->num]);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002050 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002051 }
2052
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002053 head = vhost16_to_cpu(vq, ring_head);
2054
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002055 /* If their number is silly, that's an error. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002056 if (unlikely(head >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002057 vq_err(vq, "Guest says index %u > %u is available",
2058 head, vq->num);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002059 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002060 }
2061
2062 /* When we start there are none of either input nor output. */
2063 *out_num = *in_num = 0;
2064 if (unlikely(log))
2065 *log_num = 0;
2066
2067 i = head;
2068 do {
2069 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002070 if (unlikely(i >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002071 vq_err(vq, "Desc index is %u > %u, head = %u",
2072 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002073 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002074 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002075 if (unlikely(++found > vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002076 vq_err(vq, "Loop detected: last one at %u "
2077 "vq size %u head %u\n",
2078 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002079 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002080 }
Jason Wangbfe2bc52016-06-23 02:04:30 -04002081 ret = vhost_copy_from_user(vq, &desc, vq->desc + i,
2082 sizeof desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002083 if (unlikely(ret)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002084 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
2085 i, vq->desc + i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002086 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002087 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002088 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT)) {
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03002089 ret = get_indirect(vq, iov, iov_size,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002090 out_num, in_num,
2091 log, log_num, &desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002092 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002093 if (ret != -EAGAIN)
2094 vq_err(vq, "Failure detected "
2095 "in indirect descriptor at idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002096 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002097 }
2098 continue;
2099 }
2100
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002101 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE))
2102 access = VHOST_ACCESS_WO;
2103 else
2104 access = VHOST_ACCESS_RO;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002105 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
2106 vhost32_to_cpu(vq, desc.len), iov + iov_count,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002107 iov_size - iov_count, access);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002108 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002109 if (ret != -EAGAIN)
2110 vq_err(vq, "Translation failure %d descriptor idx %d\n",
2111 ret, i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002112 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002113 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002114 if (access == VHOST_ACCESS_WO) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002115 /* If this is an input descriptor,
2116 * increment that count. */
2117 *in_num += ret;
2118 if (unlikely(log)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002119 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
2120 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002121 ++*log_num;
2122 }
2123 } else {
2124 /* If it's an output descriptor, they're all supposed
2125 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002126 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002127 vq_err(vq, "Descriptor has out after in: "
2128 "idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002129 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002130 }
2131 *out_num += ret;
2132 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002133 } while ((i = next_desc(vq, &desc)) != -1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002134
2135 /* On success, increment avail index. */
2136 vq->last_avail_idx++;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002137
2138 /* Assume notifications from guest are disabled at this point,
2139 * if they aren't we would need to update avail_event index. */
2140 BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002141 return head;
2142}
Asias He6ac1afb2013-05-06 16:38:21 +08002143EXPORT_SYMBOL_GPL(vhost_get_vq_desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002144
2145/* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
David Stevens8dd014a2010-07-27 18:52:21 +03002146void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002147{
David Stevens8dd014a2010-07-27 18:52:21 +03002148 vq->last_avail_idx -= n;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002149}
Asias He6ac1afb2013-05-06 16:38:21 +08002150EXPORT_SYMBOL_GPL(vhost_discard_vq_desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002151
2152/* After we've used one of their buffers, we tell them about it. We'll then
2153 * want to notify the guest, using eventfd. */
2154int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
2155{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002156 struct vring_used_elem heads = {
2157 cpu_to_vhost32(vq, head),
2158 cpu_to_vhost32(vq, len)
2159 };
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002160
Jason Wangc49e4e52013-09-02 16:40:58 +08002161 return vhost_add_used_n(vq, &heads, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002162}
Asias He6ac1afb2013-05-06 16:38:21 +08002163EXPORT_SYMBOL_GPL(vhost_add_used);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002164
David Stevens8dd014a2010-07-27 18:52:21 +03002165static int __vhost_add_used_n(struct vhost_virtqueue *vq,
2166 struct vring_used_elem *heads,
2167 unsigned count)
2168{
2169 struct vring_used_elem __user *used;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002170 u16 old, new;
David Stevens8dd014a2010-07-27 18:52:21 +03002171 int start;
2172
Michael S. Tsirkin5fba13b2015-11-29 13:34:44 +02002173 start = vq->last_used_idx & (vq->num - 1);
David Stevens8dd014a2010-07-27 18:52:21 +03002174 used = vq->used->ring + start;
Jason Wangc49e4e52013-09-02 16:40:58 +08002175 if (count == 1) {
Jason Wangbfe2bc52016-06-23 02:04:30 -04002176 if (vhost_put_user(vq, heads[0].id, &used->id)) {
Jason Wangc49e4e52013-09-02 16:40:58 +08002177 vq_err(vq, "Failed to write used id");
2178 return -EFAULT;
2179 }
Jason Wangbfe2bc52016-06-23 02:04:30 -04002180 if (vhost_put_user(vq, heads[0].len, &used->len)) {
Jason Wangc49e4e52013-09-02 16:40:58 +08002181 vq_err(vq, "Failed to write used len");
2182 return -EFAULT;
2183 }
Jason Wangbfe2bc52016-06-23 02:04:30 -04002184 } else if (vhost_copy_to_user(vq, used, heads, count * sizeof *used)) {
David Stevens8dd014a2010-07-27 18:52:21 +03002185 vq_err(vq, "Failed to write used");
2186 return -EFAULT;
2187 }
2188 if (unlikely(vq->log_used)) {
2189 /* Make sure data is seen before log. */
2190 smp_wmb();
2191 /* Log used ring entry write. */
2192 log_write(vq->log_base,
2193 vq->log_addr +
2194 ((void __user *)used - (void __user *)vq->used),
2195 count * sizeof *used);
2196 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002197 old = vq->last_used_idx;
2198 new = (vq->last_used_idx += count);
2199 /* If the driver never bothers to signal in a very long while,
2200 * used index might wrap around. If that happens, invalidate
2201 * signalled_used index we stored. TODO: make sure driver
2202 * signals at least once in 2^16 and remove this. */
2203 if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old)))
2204 vq->signalled_used_valid = false;
David Stevens8dd014a2010-07-27 18:52:21 +03002205 return 0;
2206}
2207
2208/* After we've used one of their buffers, we tell them about it. We'll then
2209 * want to notify the guest, using eventfd. */
2210int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
2211 unsigned count)
2212{
2213 int start, n, r;
2214
Michael S. Tsirkin5fba13b2015-11-29 13:34:44 +02002215 start = vq->last_used_idx & (vq->num - 1);
David Stevens8dd014a2010-07-27 18:52:21 +03002216 n = vq->num - start;
2217 if (n < count) {
2218 r = __vhost_add_used_n(vq, heads, n);
2219 if (r < 0)
2220 return r;
2221 heads += n;
2222 count -= n;
2223 }
2224 r = __vhost_add_used_n(vq, heads, count);
2225
2226 /* Make sure buffer is written before we update index. */
2227 smp_wmb();
Jason Wangbfe2bc52016-06-23 02:04:30 -04002228 if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->last_used_idx),
2229 &vq->used->idx)) {
David Stevens8dd014a2010-07-27 18:52:21 +03002230 vq_err(vq, "Failed to increment used idx");
2231 return -EFAULT;
2232 }
2233 if (unlikely(vq->log_used)) {
2234 /* Log used index update. */
2235 log_write(vq->log_base,
2236 vq->log_addr + offsetof(struct vring_used, idx),
2237 sizeof vq->used->idx);
2238 if (vq->log_ctx)
2239 eventfd_signal(vq->log_ctx, 1);
2240 }
2241 return r;
2242}
Asias He6ac1afb2013-05-06 16:38:21 +08002243EXPORT_SYMBOL_GPL(vhost_add_used_n);
David Stevens8dd014a2010-07-27 18:52:21 +03002244
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002245static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002246{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002247 __u16 old, new;
2248 __virtio16 event;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002249 bool v;
Jason Wang8d658432017-07-27 11:22:05 +08002250 /* Flush out used index updates. This is paired
2251 * with the barrier that the Guest executes when enabling
2252 * interrupts. */
2253 smp_mb();
Michael S. Tsirkin0d499352010-05-11 19:44:17 +03002254
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002255 if (vhost_has_feature(vq, VIRTIO_F_NOTIFY_ON_EMPTY) &&
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002256 unlikely(vq->avail_idx == vq->last_avail_idx))
2257 return true;
2258
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002259 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002260 __virtio16 flags;
Jason Wangf8894912017-02-28 17:56:02 +08002261 if (vhost_get_avail(vq, flags, &vq->avail->flags)) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002262 vq_err(vq, "Failed to get flags");
2263 return true;
2264 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002265 return !(flags & cpu_to_vhost16(vq, VRING_AVAIL_F_NO_INTERRUPT));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002266 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002267 old = vq->signalled_used;
2268 v = vq->signalled_used_valid;
2269 new = vq->signalled_used = vq->last_used_idx;
2270 vq->signalled_used_valid = true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002271
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002272 if (unlikely(!v))
2273 return true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002274
Jason Wangf8894912017-02-28 17:56:02 +08002275 if (vhost_get_avail(vq, event, vhost_used_event(vq))) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002276 vq_err(vq, "Failed to get used event idx");
2277 return true;
2278 }
Jason Wang8d658432017-07-27 11:22:05 +08002279 return vring_need_event(vhost16_to_cpu(vq, event), new, old);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002280}
2281
2282/* This actually signals the guest, using eventfd. */
2283void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
2284{
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002285 /* Signal the Guest tell them we used something up. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002286 if (vq->call_ctx && vhost_notify(dev, vq))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002287 eventfd_signal(vq->call_ctx, 1);
2288}
Asias He6ac1afb2013-05-06 16:38:21 +08002289EXPORT_SYMBOL_GPL(vhost_signal);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002290
2291/* And here's the combo meal deal. Supersize me! */
2292void vhost_add_used_and_signal(struct vhost_dev *dev,
2293 struct vhost_virtqueue *vq,
2294 unsigned int head, int len)
2295{
2296 vhost_add_used(vq, head, len);
2297 vhost_signal(dev, vq);
2298}
Asias He6ac1afb2013-05-06 16:38:21 +08002299EXPORT_SYMBOL_GPL(vhost_add_used_and_signal);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002300
David Stevens8dd014a2010-07-27 18:52:21 +03002301/* multi-buffer version of vhost_add_used_and_signal */
2302void vhost_add_used_and_signal_n(struct vhost_dev *dev,
2303 struct vhost_virtqueue *vq,
2304 struct vring_used_elem *heads, unsigned count)
2305{
2306 vhost_add_used_n(vq, heads, count);
2307 vhost_signal(dev, vq);
2308}
Asias He6ac1afb2013-05-06 16:38:21 +08002309EXPORT_SYMBOL_GPL(vhost_add_used_and_signal_n);
David Stevens8dd014a2010-07-27 18:52:21 +03002310
Jason Wangd4a60602016-03-04 06:24:52 -05002311/* return true if we're sure that avaiable ring is empty */
2312bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq)
2313{
2314 __virtio16 avail_idx;
2315 int r;
2316
Jason Wang275bf962017-01-18 15:02:01 +08002317 if (vq->avail_idx != vq->last_avail_idx)
Jason Wangd4a60602016-03-04 06:24:52 -05002318 return false;
2319
Linus Torvalds54d79892017-03-02 13:53:13 -08002320 r = vhost_get_avail(vq, avail_idx, &vq->avail->idx);
Jason Wang275bf962017-01-18 15:02:01 +08002321 if (unlikely(r))
2322 return false;
2323 vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
2324
2325 return vq->avail_idx == vq->last_avail_idx;
Jason Wangd4a60602016-03-04 06:24:52 -05002326}
2327EXPORT_SYMBOL_GPL(vhost_vq_avail_empty);
2328
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002329/* OK, now we need to know about added descriptors. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002330bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002331{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002332 __virtio16 avail_idx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002333 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05302334
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002335 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
2336 return false;
2337 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002338 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08002339 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002340 if (r) {
2341 vq_err(vq, "Failed to enable notification at %p: %d\n",
2342 &vq->used->flags, r);
2343 return false;
2344 }
2345 } else {
Jason Wang2723fea2011-06-21 18:04:38 +08002346 r = vhost_update_avail_event(vq, vq->avail_idx);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002347 if (r) {
2348 vq_err(vq, "Failed to update avail event index at %p: %d\n",
2349 vhost_avail_event(vq), r);
2350 return false;
2351 }
2352 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002353 /* They could have slipped one in as we were doing that: make
2354 * sure it's written, then check again. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00002355 smp_mb();
Jason Wangf8894912017-02-28 17:56:02 +08002356 r = vhost_get_avail(vq, avail_idx, &vq->avail->idx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002357 if (r) {
2358 vq_err(vq, "Failed to check avail idx at %p: %d\n",
2359 &vq->avail->idx, r);
2360 return false;
2361 }
2362
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002363 return vhost16_to_cpu(vq, avail_idx) != vq->avail_idx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002364}
Asias He6ac1afb2013-05-06 16:38:21 +08002365EXPORT_SYMBOL_GPL(vhost_enable_notify);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002366
2367/* We don't need to be notified again. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002368void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002369{
2370 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05302371
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002372 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
2373 return;
2374 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002375 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08002376 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002377 if (r)
2378 vq_err(vq, "Failed to enable notification at %p: %d\n",
2379 &vq->used->flags, r);
2380 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002381}
Asias He6ac1afb2013-05-06 16:38:21 +08002382EXPORT_SYMBOL_GPL(vhost_disable_notify);
2383
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002384/* Create a new message. */
2385struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type)
2386{
2387 struct vhost_msg_node *node = kmalloc(sizeof *node, GFP_KERNEL);
2388 if (!node)
2389 return NULL;
Michael S. Tsirkin670ae9c2018-05-12 00:33:10 +03002390
2391 /* Make sure all padding within the structure is initialized. */
2392 memset(&node->msg, 0, sizeof node->msg);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002393 node->vq = vq;
2394 node->msg.type = type;
2395 return node;
2396}
2397EXPORT_SYMBOL_GPL(vhost_new_msg);
2398
2399void vhost_enqueue_msg(struct vhost_dev *dev, struct list_head *head,
2400 struct vhost_msg_node *node)
2401{
2402 spin_lock(&dev->iotlb_lock);
2403 list_add_tail(&node->node, head);
2404 spin_unlock(&dev->iotlb_lock);
2405
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002406 wake_up_interruptible_poll(&dev->wait, EPOLLIN | EPOLLRDNORM);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002407}
2408EXPORT_SYMBOL_GPL(vhost_enqueue_msg);
2409
2410struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev,
2411 struct list_head *head)
2412{
2413 struct vhost_msg_node *node = NULL;
2414
2415 spin_lock(&dev->iotlb_lock);
2416 if (!list_empty(head)) {
2417 node = list_first_entry(head, struct vhost_msg_node,
2418 node);
2419 list_del(&node->node);
2420 }
2421 spin_unlock(&dev->iotlb_lock);
2422
2423 return node;
2424}
2425EXPORT_SYMBOL_GPL(vhost_dequeue_msg);
2426
2427
Asias He6ac1afb2013-05-06 16:38:21 +08002428static int __init vhost_init(void)
2429{
2430 return 0;
2431}
2432
2433static void __exit vhost_exit(void)
2434{
2435}
2436
2437module_init(vhost_init);
2438module_exit(vhost_exit);
2439
2440MODULE_VERSION("0.0.1");
2441MODULE_LICENSE("GPL v2");
2442MODULE_AUTHOR("Michael S. Tsirkin");
2443MODULE_DESCRIPTION("Host kernel accelerator for virtio");