blob: 7bf0b734ae7032900c051eb7ac7d99fe0e7ee6c5 [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
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000173 if (!((unsigned long)key & poll->mask))
174 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,
189 unsigned long mask, struct vhost_dev *dev)
190{
191 init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
192 init_poll_funcptr(&poll->table, vhost_poll_func);
193 poll->mask = mask;
194 poll->dev = dev;
Jason Wang2b8b3282013-01-28 01:05:18 +0000195 poll->wqh = NULL;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300196
197 vhost_work_init(&poll->work, fn);
198}
Asias He6ac1afb2013-05-06 16:38:21 +0800199EXPORT_SYMBOL_GPL(vhost_poll_init);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300200
Michael S. 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{
205 unsigned long mask;
Jason Wang2b8b3282013-01-28 01:05:18 +0000206 int ret = 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530207
Jason Wang70181d512013-04-10 20:50:48 +0000208 if (poll->wqh)
209 return 0;
210
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000211 mask = file->f_op->poll(file, &poll->table);
212 if (mask)
213 vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
Jason Wang2b8b3282013-01-28 01:05:18 +0000214 if (mask & POLLERR) {
215 if (poll->wqh)
216 remove_wait_queue(poll->wqh, &poll->wait);
217 ret = -EINVAL;
218 }
219
220 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000221}
Asias He6ac1afb2013-05-06 16:38:21 +0800222EXPORT_SYMBOL_GPL(vhost_poll_start);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000223
224/* Stop polling a file. After this function returns, it becomes safe to drop the
225 * file reference. You must also flush afterwards. */
226void vhost_poll_stop(struct vhost_poll *poll)
227{
Jason Wang2b8b3282013-01-28 01:05:18 +0000228 if (poll->wqh) {
229 remove_wait_queue(poll->wqh, &poll->wait);
230 poll->wqh = NULL;
231 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000232}
Asias He6ac1afb2013-05-06 16:38:21 +0800233EXPORT_SYMBOL_GPL(vhost_poll_stop);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000234
Asias He6ac1afb2013-05-06 16:38:21 +0800235void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000236{
Jason Wang7235acd2016-04-25 22:14:32 -0400237 struct vhost_flush_struct flush;
Tejun Heoc23f34452010-06-02 20:40:00 +0200238
Jason Wang7235acd2016-04-25 22:14:32 -0400239 if (dev->worker) {
240 init_completion(&flush.wait_event);
241 vhost_work_init(&flush.work, vhost_flush_work);
242
243 vhost_work_queue(dev, &flush.work);
244 wait_for_completion(&flush.wait_event);
245 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000246}
Asias He6ac1afb2013-05-06 16:38:21 +0800247EXPORT_SYMBOL_GPL(vhost_work_flush);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000248
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300249/* Flush any work that has been scheduled. When calling this, don't hold any
250 * locks that are also used by the callback. */
251void vhost_poll_flush(struct vhost_poll *poll)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000252{
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300253 vhost_work_flush(poll->dev, &poll->work);
254}
Asias He6ac1afb2013-05-06 16:38:21 +0800255EXPORT_SYMBOL_GPL(vhost_poll_flush);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300256
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000257void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300258{
Jason Wang04b96e52016-04-25 22:14:33 -0400259 if (!dev->worker)
260 return;
Tejun Heoc23f34452010-06-02 20:40:00 +0200261
Jason Wang04b96e52016-04-25 22:14:33 -0400262 if (!test_and_set_bit(VHOST_WORK_QUEUED, &work->flags)) {
263 /* We can only add the work to the list after we're
264 * sure it was not in the list.
Peng Tao635abf02016-12-07 17:52:19 +0800265 * test_and_set_bit() implies a memory barrier.
Jason Wang04b96e52016-04-25 22:14:33 -0400266 */
Jason Wang04b96e52016-04-25 22:14:33 -0400267 llist_add(&work->node, &dev->work_list);
Tejun Heoc23f34452010-06-02 20:40:00 +0200268 wake_up_process(dev->worker);
269 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000270}
Asias He6ac1afb2013-05-06 16:38:21 +0800271EXPORT_SYMBOL_GPL(vhost_work_queue);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000272
Jason Wang526d3e72016-03-04 06:24:51 -0500273/* A lockless hint for busy polling code to exit the loop */
274bool vhost_has_work(struct vhost_dev *dev)
275{
Jason Wang04b96e52016-04-25 22:14:33 -0400276 return !llist_empty(&dev->work_list);
Jason Wang526d3e72016-03-04 06:24:51 -0500277}
278EXPORT_SYMBOL_GPL(vhost_has_work);
279
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300280void vhost_poll_queue(struct vhost_poll *poll)
281{
282 vhost_work_queue(poll->dev, &poll->work);
283}
Asias He6ac1afb2013-05-06 16:38:21 +0800284EXPORT_SYMBOL_GPL(vhost_poll_queue);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300285
Jason Wangf8894912017-02-28 17:56:02 +0800286static void __vhost_vq_meta_reset(struct vhost_virtqueue *vq)
287{
288 int j;
289
290 for (j = 0; j < VHOST_NUM_ADDRS; j++)
291 vq->meta_iotlb[j] = NULL;
292}
293
294static void vhost_vq_meta_reset(struct vhost_dev *d)
295{
296 int i;
297
298 for (i = 0; i < d->nvqs; ++i)
299 __vhost_vq_meta_reset(d->vqs[i]);
300}
301
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000302static void vhost_vq_reset(struct vhost_dev *dev,
303 struct vhost_virtqueue *vq)
304{
305 vq->num = 1;
306 vq->desc = NULL;
307 vq->avail = NULL;
308 vq->used = NULL;
309 vq->last_avail_idx = 0;
310 vq->avail_idx = 0;
311 vq->last_used_idx = 0;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300312 vq->signalled_used = 0;
313 vq->signalled_used_valid = false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000314 vq->used_flags = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000315 vq->log_used = false;
316 vq->log_addr = -1ull;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000317 vq->private_data = NULL;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300318 vq->acked_features = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000319 vq->log_base = NULL;
320 vq->error_ctx = NULL;
321 vq->error = NULL;
322 vq->kick = NULL;
323 vq->call_ctx = NULL;
Michael S. Tsirkin73a99f02010-02-23 11:23:45 +0200324 vq->log_ctx = NULL;
Greg Kurzc5072032016-02-16 15:59:34 +0100325 vhost_reset_is_le(vq);
326 vhost_disable_cross_endian(vq);
Jason Wang03088132016-03-04 06:24:53 -0500327 vq->busyloop_timeout = 0;
Jason Wanga9709d62016-06-23 02:04:31 -0400328 vq->umem = NULL;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400329 vq->iotlb = NULL;
Jason Wangf8894912017-02-28 17:56:02 +0800330 __vhost_vq_meta_reset(vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000331}
332
Tejun Heoc23f34452010-06-02 20:40:00 +0200333static int vhost_worker(void *data)
334{
335 struct vhost_dev *dev = data;
Jason Wang04b96e52016-04-25 22:14:33 -0400336 struct vhost_work *work, *work_next;
337 struct llist_node *node;
Jens Freimannd7ffde32012-06-26 00:59:58 +0000338 mm_segment_t oldfs = get_fs();
Tejun Heoc23f34452010-06-02 20:40:00 +0200339
Jens Freimannd7ffde32012-06-26 00:59:58 +0000340 set_fs(USER_DS);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200341 use_mm(dev->mm);
342
Tejun Heoc23f34452010-06-02 20:40:00 +0200343 for (;;) {
344 /* mb paired w/ kthread_stop */
345 set_current_state(TASK_INTERRUPTIBLE);
346
Tejun Heoc23f34452010-06-02 20:40:00 +0200347 if (kthread_should_stop()) {
Tejun Heoc23f34452010-06-02 20:40:00 +0200348 __set_current_state(TASK_RUNNING);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200349 break;
Tejun Heoc23f34452010-06-02 20:40:00 +0200350 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200351
Jason Wang04b96e52016-04-25 22:14:33 -0400352 node = llist_del_all(&dev->work_list);
353 if (!node)
354 schedule();
355
356 node = llist_reverse_order(node);
357 /* make sure flag is seen after deletion */
358 smp_wmb();
359 llist_for_each_entry_safe(work, work_next, node, node) {
360 clear_bit(VHOST_WORK_QUEUED, &work->flags);
Tejun Heoc23f34452010-06-02 20:40:00 +0200361 __set_current_state(TASK_RUNNING);
362 work->fn(work);
Nadav Har'Eld550dda2012-02-27 15:07:29 +0200363 if (need_resched())
364 schedule();
Jason Wang04b96e52016-04-25 22:14:33 -0400365 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200366 }
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200367 unuse_mm(dev->mm);
Jens Freimannd7ffde32012-06-26 00:59:58 +0000368 set_fs(oldfs);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200369 return 0;
Tejun Heoc23f34452010-06-02 20:40:00 +0200370}
371
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000372static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
373{
374 kfree(vq->indirect);
375 vq->indirect = NULL;
376 kfree(vq->log);
377 vq->log = NULL;
378 kfree(vq->heads);
379 vq->heads = NULL;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000380}
381
Jason Wange0e9b402010-09-14 23:53:05 +0800382/* Helper to allocate iovec buffers for all vqs. */
383static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
384{
Asias He6d5e6aa2013-05-06 16:38:23 +0800385 struct vhost_virtqueue *vq;
Jason Wange0e9b402010-09-14 23:53:05 +0800386 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530387
Jason Wange0e9b402010-09-14 23:53:05 +0800388 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800389 vq = dev->vqs[i];
390 vq->indirect = kmalloc(sizeof *vq->indirect * UIO_MAXIOV,
391 GFP_KERNEL);
392 vq->log = kmalloc(sizeof *vq->log * UIO_MAXIOV, GFP_KERNEL);
393 vq->heads = kmalloc(sizeof *vq->heads * UIO_MAXIOV, GFP_KERNEL);
394 if (!vq->indirect || !vq->log || !vq->heads)
Jason Wange0e9b402010-09-14 23:53:05 +0800395 goto err_nomem;
396 }
397 return 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530398
Jason Wange0e9b402010-09-14 23:53:05 +0800399err_nomem:
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000400 for (; i >= 0; --i)
Asias He3ab2e422013-04-27 11:16:48 +0800401 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800402 return -ENOMEM;
403}
404
405static void vhost_dev_free_iovecs(struct vhost_dev *dev)
406{
407 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530408
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000409 for (i = 0; i < dev->nvqs; ++i)
Asias He3ab2e422013-04-27 11:16:48 +0800410 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800411}
412
Zhi Yong Wu59566b6e2013-12-07 04:13:03 +0800413void vhost_dev_init(struct vhost_dev *dev,
Asias He3ab2e422013-04-27 11:16:48 +0800414 struct vhost_virtqueue **vqs, int nvqs)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000415{
Asias He6d5e6aa2013-05-06 16:38:23 +0800416 struct vhost_virtqueue *vq;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000417 int i;
Tejun Heoc23f34452010-06-02 20:40:00 +0200418
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000419 dev->vqs = vqs;
420 dev->nvqs = nvqs;
421 mutex_init(&dev->mutex);
422 dev->log_ctx = NULL;
423 dev->log_file = NULL;
Jason Wanga9709d62016-06-23 02:04:31 -0400424 dev->umem = NULL;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400425 dev->iotlb = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000426 dev->mm = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200427 dev->worker = NULL;
Jason Wang04b96e52016-04-25 22:14:33 -0400428 init_llist_head(&dev->work_list);
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400429 init_waitqueue_head(&dev->wait);
430 INIT_LIST_HEAD(&dev->read_list);
431 INIT_LIST_HEAD(&dev->pending_list);
432 spin_lock_init(&dev->iotlb_lock);
Jason Wang04b96e52016-04-25 22:14:33 -0400433
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000434
435 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800436 vq = dev->vqs[i];
437 vq->log = NULL;
438 vq->indirect = NULL;
439 vq->heads = NULL;
440 vq->dev = dev;
441 mutex_init(&vq->mutex);
442 vhost_vq_reset(dev, vq);
443 if (vq->handle_kick)
444 vhost_poll_init(&vq->poll, vq->handle_kick,
445 POLLIN, dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000446 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000447}
Asias He6ac1afb2013-05-06 16:38:21 +0800448EXPORT_SYMBOL_GPL(vhost_dev_init);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000449
450/* Caller should have device mutex */
451long vhost_dev_check_owner(struct vhost_dev *dev)
452{
453 /* Are you the owner? If not, I don't think you mean to do that */
454 return dev->mm == current->mm ? 0 : -EPERM;
455}
Asias He6ac1afb2013-05-06 16:38:21 +0800456EXPORT_SYMBOL_GPL(vhost_dev_check_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000457
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300458struct vhost_attach_cgroups_struct {
Krishna Kumard47effe2011-03-01 17:06:37 +0530459 struct vhost_work work;
460 struct task_struct *owner;
461 int ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300462};
463
464static void vhost_attach_cgroups_work(struct vhost_work *work)
465{
Krishna Kumard47effe2011-03-01 17:06:37 +0530466 struct vhost_attach_cgroups_struct *s;
467
468 s = container_of(work, struct vhost_attach_cgroups_struct, work);
469 s->ret = cgroup_attach_task_all(s->owner, current);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300470}
471
472static int vhost_attach_cgroups(struct vhost_dev *dev)
473{
Krishna Kumard47effe2011-03-01 17:06:37 +0530474 struct vhost_attach_cgroups_struct attach;
475
476 attach.owner = current;
477 vhost_work_init(&attach.work, vhost_attach_cgroups_work);
478 vhost_work_queue(dev, &attach.work);
479 vhost_work_flush(dev, &attach.work);
480 return attach.ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300481}
482
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000483/* Caller should have device mutex */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300484bool vhost_dev_has_owner(struct vhost_dev *dev)
485{
486 return dev->mm;
487}
Asias He6ac1afb2013-05-06 16:38:21 +0800488EXPORT_SYMBOL_GPL(vhost_dev_has_owner);
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300489
490/* Caller should have device mutex */
Asias He54db63c2013-05-06 11:15:59 +0800491long vhost_dev_set_owner(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000492{
Tejun Heoc23f34452010-06-02 20:40:00 +0200493 struct task_struct *worker;
494 int err;
Krishna Kumard47effe2011-03-01 17:06:37 +0530495
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000496 /* Is there an owner already? */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300497 if (vhost_dev_has_owner(dev)) {
Tejun Heoc23f34452010-06-02 20:40:00 +0200498 err = -EBUSY;
499 goto err_mm;
500 }
Krishna Kumard47effe2011-03-01 17:06:37 +0530501
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000502 /* No owner, become one */
503 dev->mm = get_task_mm(current);
Tejun Heoc23f34452010-06-02 20:40:00 +0200504 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
505 if (IS_ERR(worker)) {
506 err = PTR_ERR(worker);
507 goto err_worker;
508 }
509
510 dev->worker = worker;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300511 wake_up_process(worker); /* avoid contributing to loadavg */
512
513 err = vhost_attach_cgroups(dev);
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300514 if (err)
515 goto err_cgroup;
Tejun Heoc23f34452010-06-02 20:40:00 +0200516
Jason Wange0e9b402010-09-14 23:53:05 +0800517 err = vhost_dev_alloc_iovecs(dev);
518 if (err)
519 goto err_cgroup;
520
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000521 return 0;
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300522err_cgroup:
523 kthread_stop(worker);
Michael S. Tsirkin615cc222010-09-02 14:16:36 +0300524 dev->worker = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200525err_worker:
526 if (dev->mm)
527 mmput(dev->mm);
528 dev->mm = NULL;
529err_mm:
530 return err;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000531}
Asias He6ac1afb2013-05-06 16:38:21 +0800532EXPORT_SYMBOL_GPL(vhost_dev_set_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000533
Jason Wanga9709d62016-06-23 02:04:31 -0400534struct vhost_umem *vhost_dev_reset_owner_prepare(void)
535{
Michal Hocko6c5ab652017-05-08 15:57:15 -0700536 return kvzalloc(sizeof(struct vhost_umem), GFP_KERNEL);
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300537}
Asias He6ac1afb2013-05-06 16:38:21 +0800538EXPORT_SYMBOL_GPL(vhost_dev_reset_owner_prepare);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000539
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300540/* Caller should have device mutex */
Jason Wanga9709d62016-06-23 02:04:31 -0400541void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_umem *umem)
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300542{
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300543 int i;
544
夷则(Caspar)f6f93f72017-12-25 00:08:58 +0800545 vhost_dev_cleanup(dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000546
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300547 /* Restore memory to default empty mapping. */
Jason Wanga9709d62016-06-23 02:04:31 -0400548 INIT_LIST_HEAD(&umem->umem_list);
549 dev->umem = umem;
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300550 /* We don't need VQ locks below since vhost_dev_cleanup makes sure
551 * VQs aren't running.
552 */
553 for (i = 0; i < dev->nvqs; ++i)
Jason Wanga9709d62016-06-23 02:04:31 -0400554 dev->vqs[i]->umem = umem;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000555}
Asias He6ac1afb2013-05-06 16:38:21 +0800556EXPORT_SYMBOL_GPL(vhost_dev_reset_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000557
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000558void vhost_dev_stop(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000559{
560 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530561
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000562 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800563 if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) {
564 vhost_poll_stop(&dev->vqs[i]->poll);
565 vhost_poll_flush(&dev->vqs[i]->poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000566 }
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000567 }
568}
Asias He6ac1afb2013-05-06 16:38:21 +0800569EXPORT_SYMBOL_GPL(vhost_dev_stop);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000570
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400571static void vhost_umem_free(struct vhost_umem *umem,
572 struct vhost_umem_node *node)
573{
574 vhost_umem_interval_tree_remove(node, &umem->umem_tree);
575 list_del(&node->link);
576 kfree(node);
577 umem->numem--;
578}
579
Jason Wanga9709d62016-06-23 02:04:31 -0400580static void vhost_umem_clean(struct vhost_umem *umem)
581{
582 struct vhost_umem_node *node, *tmp;
583
584 if (!umem)
585 return;
586
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400587 list_for_each_entry_safe(node, tmp, &umem->umem_list, link)
588 vhost_umem_free(umem, node);
589
Jason Wanga9709d62016-06-23 02:04:31 -0400590 kvfree(umem);
591}
592
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400593static void vhost_clear_msg(struct vhost_dev *dev)
594{
595 struct vhost_msg_node *node, *n;
596
597 spin_lock(&dev->iotlb_lock);
598
599 list_for_each_entry_safe(node, n, &dev->read_list, node) {
600 list_del(&node->node);
601 kfree(node);
602 }
603
604 list_for_each_entry_safe(node, n, &dev->pending_list, node) {
605 list_del(&node->node);
606 kfree(node);
607 }
608
609 spin_unlock(&dev->iotlb_lock);
610}
611
夷则(Caspar)f6f93f72017-12-25 00:08:58 +0800612void vhost_dev_cleanup(struct vhost_dev *dev)
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000613{
614 int i;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000615
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000616 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800617 if (dev->vqs[i]->error_ctx)
618 eventfd_ctx_put(dev->vqs[i]->error_ctx);
619 if (dev->vqs[i]->error)
620 fput(dev->vqs[i]->error);
621 if (dev->vqs[i]->kick)
622 fput(dev->vqs[i]->kick);
623 if (dev->vqs[i]->call_ctx)
624 eventfd_ctx_put(dev->vqs[i]->call_ctx);
Asias He3ab2e422013-04-27 11:16:48 +0800625 vhost_vq_reset(dev, dev->vqs[i]);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000626 }
Jason Wange0e9b402010-09-14 23:53:05 +0800627 vhost_dev_free_iovecs(dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000628 if (dev->log_ctx)
629 eventfd_ctx_put(dev->log_ctx);
630 dev->log_ctx = NULL;
631 if (dev->log_file)
632 fput(dev->log_file);
633 dev->log_file = NULL;
634 /* No one will access memory at this point */
Jason Wanga9709d62016-06-23 02:04:31 -0400635 vhost_umem_clean(dev->umem);
636 dev->umem = NULL;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400637 vhost_umem_clean(dev->iotlb);
638 dev->iotlb = NULL;
639 vhost_clear_msg(dev);
640 wake_up_interruptible_poll(&dev->wait, POLLIN | POLLRDNORM);
Jason Wang04b96e52016-04-25 22:14:33 -0400641 WARN_ON(!llist_empty(&dev->work_list));
Eric Dumazet78b620c2010-08-31 02:05:57 +0000642 if (dev->worker) {
643 kthread_stop(dev->worker);
644 dev->worker = NULL;
645 }
Michael S. Tsirkin533a19b2010-10-06 15:34:38 +0200646 if (dev->mm)
647 mmput(dev->mm);
648 dev->mm = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000649}
Asias He6ac1afb2013-05-06 16:38:21 +0800650EXPORT_SYMBOL_GPL(vhost_dev_cleanup);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000651
652static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
653{
654 u64 a = addr / VHOST_PAGE_SIZE / 8;
Krishna Kumard47effe2011-03-01 17:06:37 +0530655
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000656 /* Make sure 64 bit math will not overflow. */
657 if (a > ULONG_MAX - (unsigned long)log_base ||
658 a + (unsigned long)log_base > ULONG_MAX)
Dan Carpenter6d97e552010-10-11 19:24:19 +0200659 return 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000660
661 return access_ok(VERIFY_WRITE, log_base + a,
662 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
663}
664
Michael S. Tsirkinec33d032016-08-01 23:20:53 +0300665static bool vhost_overflow(u64 uaddr, u64 size)
666{
667 /* Make sure 64 bit math will not overflow. */
668 return uaddr > ULONG_MAX || size > ULONG_MAX || uaddr > ULONG_MAX - size;
669}
670
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000671/* Caller should have vq mutex and device mutex. */
Jason Wanga9709d62016-06-23 02:04:31 -0400672static int vq_memory_access_ok(void __user *log_base, struct vhost_umem *umem,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000673 int log_all)
674{
Jason Wanga9709d62016-06-23 02:04:31 -0400675 struct vhost_umem_node *node;
Jeff Dike179b2842010-04-07 09:59:10 -0400676
Jason Wanga9709d62016-06-23 02:04:31 -0400677 if (!umem)
Michael S. Tsirkinf8322fb2010-05-27 12:28:03 +0300678 return 0;
Jeff Dike179b2842010-04-07 09:59:10 -0400679
Jason Wanga9709d62016-06-23 02:04:31 -0400680 list_for_each_entry(node, &umem->umem_list, link) {
681 unsigned long a = node->userspace_addr;
682
Michael S. Tsirkinec33d032016-08-01 23:20:53 +0300683 if (vhost_overflow(node->userspace_addr, node->size))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000684 return 0;
Michael S. Tsirkinec33d032016-08-01 23:20:53 +0300685
686
687 if (!access_ok(VERIFY_WRITE, (void __user *)a,
Jason Wanga9709d62016-06-23 02:04:31 -0400688 node->size))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000689 return 0;
690 else if (log_all && !log_access_ok(log_base,
Jason Wanga9709d62016-06-23 02:04:31 -0400691 node->start,
692 node->size))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000693 return 0;
694 }
695 return 1;
696}
697
Jason Wangf8894912017-02-28 17:56:02 +0800698static inline void __user *vhost_vq_meta_fetch(struct vhost_virtqueue *vq,
699 u64 addr, unsigned int size,
700 int type)
701{
702 const struct vhost_umem_node *node = vq->meta_iotlb[type];
703
704 if (!node)
705 return NULL;
706
707 return (void *)(uintptr_t)(node->userspace_addr + addr - node->start);
708}
709
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000710/* Can we switch to this memory table? */
711/* Caller should have device mutex but not vq mutex */
Jason Wanga9709d62016-06-23 02:04:31 -0400712static int memory_access_ok(struct vhost_dev *d, struct vhost_umem *umem,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000713 int log_all)
714{
715 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530716
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000717 for (i = 0; i < d->nvqs; ++i) {
718 int ok;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300719 bool log;
720
Asias He3ab2e422013-04-27 11:16:48 +0800721 mutex_lock(&d->vqs[i]->mutex);
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300722 log = log_all || vhost_has_feature(d->vqs[i], VHOST_F_LOG_ALL);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000723 /* If ring is inactive, will check when it's enabled. */
Asias He3ab2e422013-04-27 11:16:48 +0800724 if (d->vqs[i]->private_data)
Jason Wanga9709d62016-06-23 02:04:31 -0400725 ok = vq_memory_access_ok(d->vqs[i]->log_base,
726 umem, log);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000727 else
728 ok = 1;
Asias He3ab2e422013-04-27 11:16:48 +0800729 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000730 if (!ok)
731 return 0;
732 }
733 return 1;
734}
735
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400736static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
737 struct iovec iov[], int iov_size, int access);
Jason Wangbfe2bc52016-06-23 02:04:30 -0400738
Michael S. Tsirkin72952cc2016-12-06 06:01:41 +0200739static int vhost_copy_to_user(struct vhost_virtqueue *vq, void __user *to,
Jason Wangbfe2bc52016-06-23 02:04:30 -0400740 const void *from, unsigned size)
741{
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400742 int ret;
Jason Wangbfe2bc52016-06-23 02:04:30 -0400743
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400744 if (!vq->iotlb)
745 return __copy_to_user(to, from, size);
746 else {
747 /* This function should be called after iotlb
748 * prefetch, which means we're sure that all vq
749 * could be access through iotlb. So -EAGAIN should
750 * not happen in this case.
751 */
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400752 struct iov_iter t;
Jason Wangf8894912017-02-28 17:56:02 +0800753 void __user *uaddr = vhost_vq_meta_fetch(vq,
754 (u64)(uintptr_t)to, size,
755 VHOST_ADDR_DESC);
756
757 if (uaddr)
758 return __copy_to_user(uaddr, from, size);
759
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400760 ret = translate_desc(vq, (u64)(uintptr_t)to, size, vq->iotlb_iov,
761 ARRAY_SIZE(vq->iotlb_iov),
762 VHOST_ACCESS_WO);
763 if (ret < 0)
764 goto out;
765 iov_iter_init(&t, WRITE, vq->iotlb_iov, ret, size);
766 ret = copy_to_iter(from, size, &t);
767 if (ret == size)
768 ret = 0;
769 }
770out:
771 return ret;
772}
Jason Wangbfe2bc52016-06-23 02:04:30 -0400773
774static int vhost_copy_from_user(struct vhost_virtqueue *vq, void *to,
Michael S. Tsirkin72952cc2016-12-06 06:01:41 +0200775 void __user *from, unsigned size)
Jason Wangbfe2bc52016-06-23 02:04:30 -0400776{
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400777 int ret;
778
779 if (!vq->iotlb)
780 return __copy_from_user(to, from, size);
781 else {
782 /* This function should be called after iotlb
783 * prefetch, which means we're sure that vq
784 * could be access through iotlb. So -EAGAIN should
785 * not happen in this case.
786 */
Jason Wangf8894912017-02-28 17:56:02 +0800787 void __user *uaddr = vhost_vq_meta_fetch(vq,
788 (u64)(uintptr_t)from, size,
789 VHOST_ADDR_DESC);
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400790 struct iov_iter f;
Jason Wangf8894912017-02-28 17:56:02 +0800791
792 if (uaddr)
793 return __copy_from_user(to, uaddr, size);
794
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400795 ret = translate_desc(vq, (u64)(uintptr_t)from, size, vq->iotlb_iov,
796 ARRAY_SIZE(vq->iotlb_iov),
797 VHOST_ACCESS_RO);
798 if (ret < 0) {
799 vq_err(vq, "IOTLB translation failure: uaddr "
800 "%p size 0x%llx\n", from,
801 (unsigned long long) size);
802 goto out;
803 }
804 iov_iter_init(&f, READ, vq->iotlb_iov, ret, size);
805 ret = copy_from_iter(to, size, &f);
806 if (ret == size)
807 ret = 0;
808 }
809
810out:
811 return ret;
812}
813
Jason Wangf8894912017-02-28 17:56:02 +0800814static void __user *__vhost_get_user_slow(struct vhost_virtqueue *vq,
815 void __user *addr, unsigned int size,
816 int type)
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400817{
818 int ret;
819
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400820 ret = translate_desc(vq, (u64)(uintptr_t)addr, size, vq->iotlb_iov,
821 ARRAY_SIZE(vq->iotlb_iov),
822 VHOST_ACCESS_RO);
823 if (ret < 0) {
824 vq_err(vq, "IOTLB translation failure: uaddr "
825 "%p size 0x%llx\n", addr,
826 (unsigned long long) size);
827 return NULL;
828 }
829
830 if (ret != 1 || vq->iotlb_iov[0].iov_len != size) {
831 vq_err(vq, "Non atomic userspace memory access: uaddr "
832 "%p size 0x%llx\n", addr,
833 (unsigned long long) size);
834 return NULL;
835 }
836
837 return vq->iotlb_iov[0].iov_base;
838}
839
Jason Wangf8894912017-02-28 17:56:02 +0800840/* This function should be called after iotlb
841 * prefetch, which means we're sure that vq
842 * could be access through iotlb. So -EAGAIN should
843 * not happen in this case.
844 */
845static inline void __user *__vhost_get_user(struct vhost_virtqueue *vq,
846 void *addr, unsigned int size,
847 int type)
848{
849 void __user *uaddr = vhost_vq_meta_fetch(vq,
850 (u64)(uintptr_t)addr, size, type);
851 if (uaddr)
852 return uaddr;
853
854 return __vhost_get_user_slow(vq, addr, size, type);
855}
856
857#define vhost_put_user(vq, x, ptr) \
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400858({ \
859 int ret = -EFAULT; \
860 if (!vq->iotlb) { \
861 ret = __put_user(x, ptr); \
862 } else { \
863 __typeof__(ptr) to = \
Jason Wangf8894912017-02-28 17:56:02 +0800864 (__typeof__(ptr)) __vhost_get_user(vq, ptr, \
865 sizeof(*ptr), VHOST_ADDR_USED); \
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400866 if (to != NULL) \
867 ret = __put_user(x, to); \
868 else \
869 ret = -EFAULT; \
870 } \
871 ret; \
872})
873
Jason Wangf8894912017-02-28 17:56:02 +0800874#define vhost_get_user(vq, x, ptr, type) \
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400875({ \
876 int ret; \
877 if (!vq->iotlb) { \
878 ret = __get_user(x, ptr); \
879 } else { \
880 __typeof__(ptr) from = \
Jason Wangf8894912017-02-28 17:56:02 +0800881 (__typeof__(ptr)) __vhost_get_user(vq, ptr, \
882 sizeof(*ptr), \
883 type); \
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400884 if (from != NULL) \
885 ret = __get_user(x, from); \
886 else \
887 ret = -EFAULT; \
888 } \
889 ret; \
890})
891
Jason Wangf8894912017-02-28 17:56:02 +0800892#define vhost_get_avail(vq, x, ptr) \
893 vhost_get_user(vq, x, ptr, VHOST_ADDR_AVAIL)
894
895#define vhost_get_used(vq, x, ptr) \
896 vhost_get_user(vq, x, ptr, VHOST_ADDR_USED)
897
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400898static void vhost_dev_lock_vqs(struct vhost_dev *d)
899{
900 int i = 0;
901 for (i = 0; i < d->nvqs; ++i)
Jason Wange9cb42392018-01-23 17:27:25 +0800902 mutex_lock_nested(&d->vqs[i]->mutex, i);
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400903}
904
905static void vhost_dev_unlock_vqs(struct vhost_dev *d)
906{
907 int i = 0;
908 for (i = 0; i < d->nvqs; ++i)
909 mutex_unlock(&d->vqs[i]->mutex);
910}
911
912static int vhost_new_umem_range(struct vhost_umem *umem,
913 u64 start, u64 size, u64 end,
914 u64 userspace_addr, int perm)
915{
916 struct vhost_umem_node *tmp, *node = kmalloc(sizeof(*node), GFP_ATOMIC);
917
918 if (!node)
919 return -ENOMEM;
920
921 if (umem->numem == max_iotlb_entries) {
922 tmp = list_first_entry(&umem->umem_list, typeof(*tmp), link);
923 vhost_umem_free(umem, tmp);
924 }
925
926 node->start = start;
927 node->size = size;
928 node->last = end;
929 node->userspace_addr = userspace_addr;
930 node->perm = perm;
931 INIT_LIST_HEAD(&node->link);
932 list_add_tail(&node->link, &umem->umem_list);
933 vhost_umem_interval_tree_insert(node, &umem->umem_tree);
934 umem->numem++;
935
936 return 0;
937}
938
939static void vhost_del_umem_range(struct vhost_umem *umem,
940 u64 start, u64 end)
941{
942 struct vhost_umem_node *node;
943
944 while ((node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
945 start, end)))
946 vhost_umem_free(umem, node);
947}
948
949static void vhost_iotlb_notify_vq(struct vhost_dev *d,
950 struct vhost_iotlb_msg *msg)
951{
952 struct vhost_msg_node *node, *n;
953
954 spin_lock(&d->iotlb_lock);
955
956 list_for_each_entry_safe(node, n, &d->pending_list, node) {
957 struct vhost_iotlb_msg *vq_msg = &node->msg.iotlb;
958 if (msg->iova <= vq_msg->iova &&
959 msg->iova + msg->size - 1 > vq_msg->iova &&
960 vq_msg->type == VHOST_IOTLB_MISS) {
961 vhost_poll_queue(&node->vq->poll);
962 list_del(&node->node);
963 kfree(node);
964 }
965 }
966
967 spin_unlock(&d->iotlb_lock);
968}
969
970static int umem_access_ok(u64 uaddr, u64 size, int access)
971{
972 unsigned long a = uaddr;
973
Michael S. Tsirkinec33d032016-08-01 23:20:53 +0300974 /* Make sure 64 bit math will not overflow. */
975 if (vhost_overflow(uaddr, size))
976 return -EFAULT;
977
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400978 if ((access & VHOST_ACCESS_RO) &&
979 !access_ok(VERIFY_READ, (void __user *)a, size))
980 return -EFAULT;
981 if ((access & VHOST_ACCESS_WO) &&
982 !access_ok(VERIFY_WRITE, (void __user *)a, size))
983 return -EFAULT;
984 return 0;
985}
986
Michael S. Tsirkin72952cc2016-12-06 06:01:41 +0200987static int vhost_process_iotlb_msg(struct vhost_dev *dev,
988 struct vhost_iotlb_msg *msg)
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400989{
990 int ret = 0;
991
992 vhost_dev_lock_vqs(dev);
993 switch (msg->type) {
994 case VHOST_IOTLB_UPDATE:
995 if (!dev->iotlb) {
996 ret = -EFAULT;
997 break;
998 }
999 if (umem_access_ok(msg->uaddr, msg->size, msg->perm)) {
1000 ret = -EFAULT;
1001 break;
1002 }
Jason Wangf8894912017-02-28 17:56:02 +08001003 vhost_vq_meta_reset(dev);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001004 if (vhost_new_umem_range(dev->iotlb, msg->iova, msg->size,
1005 msg->iova + msg->size - 1,
1006 msg->uaddr, msg->perm)) {
1007 ret = -ENOMEM;
1008 break;
1009 }
1010 vhost_iotlb_notify_vq(dev, msg);
1011 break;
1012 case VHOST_IOTLB_INVALIDATE:
Jason Wang6f3180a2018-01-23 17:27:26 +08001013 if (!dev->iotlb) {
1014 ret = -EFAULT;
1015 break;
1016 }
Jason Wangf8894912017-02-28 17:56:02 +08001017 vhost_vq_meta_reset(dev);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001018 vhost_del_umem_range(dev->iotlb, msg->iova,
1019 msg->iova + msg->size - 1);
1020 break;
1021 default:
1022 ret = -EINVAL;
1023 break;
1024 }
1025
1026 vhost_dev_unlock_vqs(dev);
1027 return ret;
1028}
1029ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
1030 struct iov_iter *from)
1031{
1032 struct vhost_msg_node node;
1033 unsigned size = sizeof(struct vhost_msg);
1034 size_t ret;
1035 int err;
1036
1037 if (iov_iter_count(from) < size)
1038 return 0;
1039 ret = copy_from_iter(&node.msg, size, from);
1040 if (ret != size)
1041 goto done;
1042
1043 switch (node.msg.type) {
1044 case VHOST_IOTLB_MSG:
1045 err = vhost_process_iotlb_msg(dev, &node.msg.iotlb);
1046 if (err)
1047 ret = err;
1048 break;
1049 default:
1050 ret = -EINVAL;
1051 break;
1052 }
1053
1054done:
1055 return ret;
1056}
1057EXPORT_SYMBOL(vhost_chr_write_iter);
1058
1059unsigned int vhost_chr_poll(struct file *file, struct vhost_dev *dev,
1060 poll_table *wait)
1061{
1062 unsigned int mask = 0;
1063
1064 poll_wait(file, &dev->wait, wait);
1065
1066 if (!list_empty(&dev->read_list))
1067 mask |= POLLIN | POLLRDNORM;
1068
1069 return mask;
1070}
1071EXPORT_SYMBOL(vhost_chr_poll);
1072
1073ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to,
1074 int noblock)
1075{
1076 DEFINE_WAIT(wait);
1077 struct vhost_msg_node *node;
1078 ssize_t ret = 0;
1079 unsigned size = sizeof(struct vhost_msg);
1080
1081 if (iov_iter_count(to) < size)
1082 return 0;
1083
1084 while (1) {
1085 if (!noblock)
1086 prepare_to_wait(&dev->wait, &wait,
1087 TASK_INTERRUPTIBLE);
1088
1089 node = vhost_dequeue_msg(dev, &dev->read_list);
1090 if (node)
1091 break;
1092 if (noblock) {
1093 ret = -EAGAIN;
1094 break;
1095 }
1096 if (signal_pending(current)) {
1097 ret = -ERESTARTSYS;
1098 break;
1099 }
1100 if (!dev->iotlb) {
1101 ret = -EBADFD;
1102 break;
1103 }
1104
1105 schedule();
1106 }
1107
1108 if (!noblock)
1109 finish_wait(&dev->wait, &wait);
1110
1111 if (node) {
1112 ret = copy_to_iter(&node->msg, size, to);
1113
1114 if (ret != size || node->msg.type != VHOST_IOTLB_MISS) {
1115 kfree(node);
1116 return ret;
1117 }
1118
1119 vhost_enqueue_msg(dev, &dev->pending_list, node);
1120 }
1121
1122 return ret;
1123}
1124EXPORT_SYMBOL_GPL(vhost_chr_read_iter);
1125
1126static int vhost_iotlb_miss(struct vhost_virtqueue *vq, u64 iova, int access)
1127{
1128 struct vhost_dev *dev = vq->dev;
1129 struct vhost_msg_node *node;
1130 struct vhost_iotlb_msg *msg;
1131
1132 node = vhost_new_msg(vq, VHOST_IOTLB_MISS);
1133 if (!node)
1134 return -ENOMEM;
1135
1136 msg = &node->msg.iotlb;
1137 msg->type = VHOST_IOTLB_MISS;
1138 msg->iova = iova;
1139 msg->perm = access;
1140
1141 vhost_enqueue_msg(dev, &dev->read_list, node);
1142
1143 return 0;
Jason Wangbfe2bc52016-06-23 02:04:30 -04001144}
1145
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001146static int vq_access_ok(struct vhost_virtqueue *vq, unsigned int num,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001147 struct vring_desc __user *desc,
1148 struct vring_avail __user *avail,
1149 struct vring_used __user *used)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001150
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001151{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001152 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001153
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001154 return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
1155 access_ok(VERIFY_READ, avail,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001156 sizeof *avail + num * sizeof *avail->ring + s) &&
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001157 access_ok(VERIFY_WRITE, used,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001158 sizeof *used + num * sizeof *used->ring + s);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001159}
1160
Jason Wangf8894912017-02-28 17:56:02 +08001161static void vhost_vq_meta_update(struct vhost_virtqueue *vq,
1162 const struct vhost_umem_node *node,
1163 int type)
1164{
1165 int access = (type == VHOST_ADDR_USED) ?
1166 VHOST_ACCESS_WO : VHOST_ACCESS_RO;
1167
1168 if (likely(node->perm & access))
1169 vq->meta_iotlb[type] = node;
1170}
1171
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001172static int iotlb_access_ok(struct vhost_virtqueue *vq,
Jason Wangf8894912017-02-28 17:56:02 +08001173 int access, u64 addr, u64 len, int type)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001174{
1175 const struct vhost_umem_node *node;
1176 struct vhost_umem *umem = vq->iotlb;
Michael S. Tsirkinca2c5b32017-08-21 22:33:33 +03001177 u64 s = 0, size, orig_addr = addr, last = addr + len - 1;
Jason Wangf8894912017-02-28 17:56:02 +08001178
1179 if (vhost_vq_meta_fetch(vq, addr, len, type))
1180 return true;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001181
1182 while (len > s) {
1183 node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
1184 addr,
Michael S. Tsirkinca2c5b32017-08-21 22:33:33 +03001185 last);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001186 if (node == NULL || node->start > addr) {
1187 vhost_iotlb_miss(vq, addr, access);
1188 return false;
1189 } else if (!(node->perm & access)) {
1190 /* Report the possible access violation by
1191 * request another translation from userspace.
1192 */
1193 return false;
1194 }
1195
1196 size = node->size - addr + node->start;
Jason Wangf8894912017-02-28 17:56:02 +08001197
1198 if (orig_addr == addr && size >= len)
1199 vhost_vq_meta_update(vq, node, type);
1200
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001201 s += size;
1202 addr += size;
1203 }
1204
1205 return true;
1206}
1207
1208int vq_iotlb_prefetch(struct vhost_virtqueue *vq)
1209{
1210 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
1211 unsigned int num = vq->num;
1212
1213 if (!vq->iotlb)
1214 return 1;
1215
1216 return iotlb_access_ok(vq, VHOST_ACCESS_RO, (u64)(uintptr_t)vq->desc,
Jason Wangf8894912017-02-28 17:56:02 +08001217 num * sizeof(*vq->desc), VHOST_ADDR_DESC) &&
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001218 iotlb_access_ok(vq, VHOST_ACCESS_RO, (u64)(uintptr_t)vq->avail,
1219 sizeof *vq->avail +
Jason Wangf8894912017-02-28 17:56:02 +08001220 num * sizeof(*vq->avail->ring) + s,
1221 VHOST_ADDR_AVAIL) &&
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001222 iotlb_access_ok(vq, VHOST_ACCESS_WO, (u64)(uintptr_t)vq->used,
1223 sizeof *vq->used +
Jason Wangf8894912017-02-28 17:56:02 +08001224 num * sizeof(*vq->used->ring) + s,
1225 VHOST_ADDR_USED);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001226}
1227EXPORT_SYMBOL_GPL(vq_iotlb_prefetch);
1228
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001229/* Can we log writes? */
1230/* Caller should have device mutex but not vq mutex */
1231int vhost_log_access_ok(struct vhost_dev *dev)
1232{
Jason Wanga9709d62016-06-23 02:04:31 -04001233 return memory_access_ok(dev, dev->umem, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001234}
Asias He6ac1afb2013-05-06 16:38:21 +08001235EXPORT_SYMBOL_GPL(vhost_log_access_ok);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001236
1237/* Verify access for write logging. */
1238/* Caller should have vq mutex and device mutex */
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001239static int vq_log_access_ok(struct vhost_virtqueue *vq,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001240 void __user *log_base)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001241{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001242 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
Arnd Bergmann28457ee2010-03-09 19:24:45 +01001243
Jason Wanga9709d62016-06-23 02:04:31 -04001244 return vq_memory_access_ok(log_base, vq->umem,
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001245 vhost_has_feature(vq, VHOST_F_LOG_ALL)) &&
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001246 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
1247 sizeof *vq->used +
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001248 vq->num * sizeof *vq->used->ring + s));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001249}
1250
1251/* Can we start vq? */
1252/* Caller should have vq mutex and device mutex */
1253int vhost_vq_access_ok(struct vhost_virtqueue *vq)
1254{
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001255 if (vq->iotlb) {
1256 /* When device IOTLB was used, the access validation
1257 * will be validated during prefetching.
1258 */
1259 return 1;
1260 }
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001261 return vq_access_ok(vq, vq->num, vq->desc, vq->avail, vq->used) &&
1262 vq_log_access_ok(vq, vq->log_base);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001263}
Asias He6ac1afb2013-05-06 16:38:21 +08001264EXPORT_SYMBOL_GPL(vhost_vq_access_ok);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001265
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001266static struct vhost_umem *vhost_umem_alloc(void)
1267{
Michal Hocko6c5ab652017-05-08 15:57:15 -07001268 struct vhost_umem *umem = kvzalloc(sizeof(*umem), GFP_KERNEL);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001269
1270 if (!umem)
1271 return NULL;
1272
Davidlohr Buesof808c132017-09-08 16:15:08 -07001273 umem->umem_tree = RB_ROOT_CACHED;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001274 umem->numem = 0;
1275 INIT_LIST_HEAD(&umem->umem_list);
1276
1277 return umem;
1278}
1279
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001280static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
1281{
Jason Wanga9709d62016-06-23 02:04:31 -04001282 struct vhost_memory mem, *newmem;
1283 struct vhost_memory_region *region;
Jason Wanga9709d62016-06-23 02:04:31 -04001284 struct vhost_umem *newumem, *oldumem;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001285 unsigned long size = offsetof(struct vhost_memory, regions);
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001286 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +05301287
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001288 if (copy_from_user(&mem, m, size))
1289 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001290 if (mem.padding)
1291 return -EOPNOTSUPP;
Igor Mammedovc9ce42f2015-07-02 15:08:11 +02001292 if (mem.nregions > max_mem_regions)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001293 return -E2BIG;
Michal Hocko6c5ab652017-05-08 15:57:15 -07001294 newmem = kvzalloc(size + mem.nregions * sizeof(*m->regions), GFP_KERNEL);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001295 if (!newmem)
1296 return -ENOMEM;
1297
1298 memcpy(newmem, &mem, size);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001299 if (copy_from_user(newmem->regions, m->regions,
1300 mem.nregions * sizeof *m->regions)) {
Igor Mammedovbcfeaca2015-06-16 18:33:35 +02001301 kvfree(newmem);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001302 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001303 }
1304
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001305 newumem = vhost_umem_alloc();
Jason Wanga9709d62016-06-23 02:04:31 -04001306 if (!newumem) {
Igor Mammedov4de72552015-07-01 11:07:09 +02001307 kvfree(newmem);
Jason Wanga9709d62016-06-23 02:04:31 -04001308 return -ENOMEM;
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +09001309 }
Jason Wanga9709d62016-06-23 02:04:31 -04001310
Jason Wanga9709d62016-06-23 02:04:31 -04001311 for (region = newmem->regions;
1312 region < newmem->regions + mem.nregions;
1313 region++) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001314 if (vhost_new_umem_range(newumem,
1315 region->guest_phys_addr,
1316 region->memory_size,
1317 region->guest_phys_addr +
1318 region->memory_size - 1,
1319 region->userspace_addr,
1320 VHOST_ACCESS_RW))
Jason Wanga9709d62016-06-23 02:04:31 -04001321 goto err;
Jason Wanga9709d62016-06-23 02:04:31 -04001322 }
1323
1324 if (!memory_access_ok(d, newumem, 0))
1325 goto err;
1326
1327 oldumem = d->umem;
1328 d->umem = newumem;
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001329
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001330 /* All memory accesses are done under some VQ mutex. */
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001331 for (i = 0; i < d->nvqs; ++i) {
1332 mutex_lock(&d->vqs[i]->mutex);
Jason Wanga9709d62016-06-23 02:04:31 -04001333 d->vqs[i]->umem = newumem;
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001334 mutex_unlock(&d->vqs[i]->mutex);
1335 }
Jason Wanga9709d62016-06-23 02:04:31 -04001336
1337 kvfree(newmem);
1338 vhost_umem_clean(oldumem);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001339 return 0;
Jason Wanga9709d62016-06-23 02:04:31 -04001340
1341err:
1342 vhost_umem_clean(newumem);
1343 kvfree(newmem);
1344 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001345}
1346
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001347long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001348{
Al Virocecb46f2012-08-27 14:21:39 -04001349 struct file *eventfp, *filep = NULL;
1350 bool pollstart = false, pollstop = false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001351 struct eventfd_ctx *ctx = NULL;
1352 u32 __user *idxp = argp;
1353 struct vhost_virtqueue *vq;
1354 struct vhost_vring_state s;
1355 struct vhost_vring_file f;
1356 struct vhost_vring_addr a;
1357 u32 idx;
1358 long r;
1359
1360 r = get_user(idx, idxp);
1361 if (r < 0)
1362 return r;
Krishna Kumar0f3d9a12010-05-25 11:10:36 +05301363 if (idx >= d->nvqs)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001364 return -ENOBUFS;
1365
Asias He3ab2e422013-04-27 11:16:48 +08001366 vq = d->vqs[idx];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001367
1368 mutex_lock(&vq->mutex);
1369
1370 switch (ioctl) {
1371 case VHOST_SET_VRING_NUM:
1372 /* Resizing ring with an active backend?
1373 * You don't want to do that. */
1374 if (vq->private_data) {
1375 r = -EBUSY;
1376 break;
1377 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001378 if (copy_from_user(&s, argp, sizeof s)) {
1379 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001380 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001381 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001382 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
1383 r = -EINVAL;
1384 break;
1385 }
1386 vq->num = s.num;
1387 break;
1388 case VHOST_SET_VRING_BASE:
1389 /* Moving base with an active backend?
1390 * You don't want to do that. */
1391 if (vq->private_data) {
1392 r = -EBUSY;
1393 break;
1394 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001395 if (copy_from_user(&s, argp, sizeof s)) {
1396 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001397 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001398 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001399 if (s.num > 0xffff) {
1400 r = -EINVAL;
1401 break;
1402 }
Jason Wang8d658432017-07-27 11:22:05 +08001403 vq->last_avail_idx = s.num;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001404 /* Forget the cached index value. */
1405 vq->avail_idx = vq->last_avail_idx;
1406 break;
1407 case VHOST_GET_VRING_BASE:
1408 s.index = idx;
1409 s.num = vq->last_avail_idx;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001410 if (copy_to_user(argp, &s, sizeof s))
1411 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001412 break;
1413 case VHOST_SET_VRING_ADDR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001414 if (copy_from_user(&a, argp, sizeof a)) {
1415 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001416 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001417 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001418 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
1419 r = -EOPNOTSUPP;
1420 break;
1421 }
1422 /* For 32bit, verify that the top 32bits of the user
1423 data are set to zero. */
1424 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
1425 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
1426 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
1427 r = -EFAULT;
1428 break;
1429 }
Michael S. Tsirkin5d9a07b2014-12-21 01:00:23 +02001430
1431 /* Make sure it's safe to cast pointers to vring types. */
1432 BUILD_BUG_ON(__alignof__ *vq->avail > VRING_AVAIL_ALIGN_SIZE);
1433 BUILD_BUG_ON(__alignof__ *vq->used > VRING_USED_ALIGN_SIZE);
1434 if ((a.avail_user_addr & (VRING_AVAIL_ALIGN_SIZE - 1)) ||
1435 (a.used_user_addr & (VRING_USED_ALIGN_SIZE - 1)) ||
Michael S. Tsirkind5424832015-11-16 16:57:08 +02001436 (a.log_guest_addr & (VRING_USED_ALIGN_SIZE - 1))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001437 r = -EINVAL;
1438 break;
1439 }
1440
1441 /* We only verify access here if backend is configured.
1442 * If it is not, we don't as size might not have been setup.
1443 * We will verify when backend is configured. */
1444 if (vq->private_data) {
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001445 if (!vq_access_ok(vq, vq->num,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001446 (void __user *)(unsigned long)a.desc_user_addr,
1447 (void __user *)(unsigned long)a.avail_user_addr,
1448 (void __user *)(unsigned long)a.used_user_addr)) {
1449 r = -EINVAL;
1450 break;
1451 }
1452
1453 /* Also validate log access for used ring if enabled. */
1454 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
1455 !log_access_ok(vq->log_base, a.log_guest_addr,
1456 sizeof *vq->used +
1457 vq->num * sizeof *vq->used->ring)) {
1458 r = -EINVAL;
1459 break;
1460 }
1461 }
1462
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001463 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
1464 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
1465 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
1466 vq->log_addr = a.log_guest_addr;
1467 vq->used = (void __user *)(unsigned long)a.used_user_addr;
1468 break;
1469 case VHOST_SET_VRING_KICK:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001470 if (copy_from_user(&f, argp, sizeof f)) {
1471 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001472 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001473 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001474 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +02001475 if (IS_ERR(eventfp)) {
1476 r = PTR_ERR(eventfp);
1477 break;
1478 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001479 if (eventfp != vq->kick) {
Al Virocecb46f2012-08-27 14:21:39 -04001480 pollstop = (filep = vq->kick) != NULL;
1481 pollstart = (vq->kick = eventfp) != NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001482 } else
1483 filep = eventfp;
1484 break;
1485 case VHOST_SET_VRING_CALL:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001486 if (copy_from_user(&f, argp, sizeof f)) {
1487 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001488 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001489 }
Eric Biggerse050c7d2018-01-06 14:52:19 -08001490 ctx = f.fd == -1 ? NULL : eventfd_ctx_fdget(f.fd);
1491 if (IS_ERR(ctx)) {
1492 r = PTR_ERR(ctx);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +02001493 break;
1494 }
Eric Biggerse050c7d2018-01-06 14:52:19 -08001495 swap(ctx, vq->call_ctx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001496 break;
1497 case VHOST_SET_VRING_ERR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001498 if (copy_from_user(&f, argp, sizeof f)) {
1499 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001500 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001501 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001502 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +02001503 if (IS_ERR(eventfp)) {
1504 r = PTR_ERR(eventfp);
1505 break;
1506 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001507 if (eventfp != vq->error) {
1508 filep = vq->error;
1509 vq->error = eventfp;
1510 ctx = vq->error_ctx;
1511 vq->error_ctx = eventfp ?
1512 eventfd_ctx_fileget(eventfp) : NULL;
1513 } else
1514 filep = eventfp;
1515 break;
Greg Kurz2751c982015-04-24 14:27:24 +02001516 case VHOST_SET_VRING_ENDIAN:
1517 r = vhost_set_vring_endian(vq, argp);
1518 break;
1519 case VHOST_GET_VRING_ENDIAN:
1520 r = vhost_get_vring_endian(vq, idx, argp);
1521 break;
Jason Wang03088132016-03-04 06:24:53 -05001522 case VHOST_SET_VRING_BUSYLOOP_TIMEOUT:
1523 if (copy_from_user(&s, argp, sizeof(s))) {
1524 r = -EFAULT;
1525 break;
1526 }
1527 vq->busyloop_timeout = s.num;
1528 break;
1529 case VHOST_GET_VRING_BUSYLOOP_TIMEOUT:
1530 s.index = idx;
1531 s.num = vq->busyloop_timeout;
1532 if (copy_to_user(argp, &s, sizeof(s)))
1533 r = -EFAULT;
1534 break;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001535 default:
1536 r = -ENOIOCTLCMD;
1537 }
1538
1539 if (pollstop && vq->handle_kick)
1540 vhost_poll_stop(&vq->poll);
1541
Eric Biggerse050c7d2018-01-06 14:52:19 -08001542 if (!IS_ERR_OR_NULL(ctx))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001543 eventfd_ctx_put(ctx);
1544 if (filep)
1545 fput(filep);
1546
1547 if (pollstart && vq->handle_kick)
Jason Wang2b8b3282013-01-28 01:05:18 +00001548 r = vhost_poll_start(&vq->poll, vq->kick);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001549
1550 mutex_unlock(&vq->mutex);
1551
1552 if (pollstop && vq->handle_kick)
1553 vhost_poll_flush(&vq->poll);
1554 return r;
1555}
Asias He6ac1afb2013-05-06 16:38:21 +08001556EXPORT_SYMBOL_GPL(vhost_vring_ioctl);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001557
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001558int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled)
1559{
1560 struct vhost_umem *niotlb, *oiotlb;
1561 int i;
1562
1563 niotlb = vhost_umem_alloc();
1564 if (!niotlb)
1565 return -ENOMEM;
1566
1567 oiotlb = d->iotlb;
1568 d->iotlb = niotlb;
1569
1570 for (i = 0; i < d->nvqs; ++i) {
1571 mutex_lock(&d->vqs[i]->mutex);
1572 d->vqs[i]->iotlb = niotlb;
1573 mutex_unlock(&d->vqs[i]->mutex);
1574 }
1575
1576 vhost_umem_clean(oiotlb);
1577
1578 return 0;
1579}
1580EXPORT_SYMBOL_GPL(vhost_init_device_iotlb);
1581
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001582/* Caller must have device mutex */
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001583long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001584{
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001585 struct file *eventfp, *filep = NULL;
1586 struct eventfd_ctx *ctx = NULL;
1587 u64 p;
1588 long r;
1589 int i, fd;
1590
1591 /* If you are not the owner, you can become one */
1592 if (ioctl == VHOST_SET_OWNER) {
1593 r = vhost_dev_set_owner(d);
1594 goto done;
1595 }
1596
1597 /* You must be the owner to do anything else */
1598 r = vhost_dev_check_owner(d);
1599 if (r)
1600 goto done;
1601
1602 switch (ioctl) {
1603 case VHOST_SET_MEM_TABLE:
1604 r = vhost_set_memory(d, argp);
1605 break;
1606 case VHOST_SET_LOG_BASE:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001607 if (copy_from_user(&p, argp, sizeof p)) {
1608 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001609 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001610 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001611 if ((u64)(unsigned long)p != p) {
1612 r = -EFAULT;
1613 break;
1614 }
1615 for (i = 0; i < d->nvqs; ++i) {
1616 struct vhost_virtqueue *vq;
1617 void __user *base = (void __user *)(unsigned long)p;
Asias He3ab2e422013-04-27 11:16:48 +08001618 vq = d->vqs[i];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001619 mutex_lock(&vq->mutex);
1620 /* If ring is inactive, will check when it's enabled. */
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001621 if (vq->private_data && !vq_log_access_ok(vq, base))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001622 r = -EFAULT;
1623 else
1624 vq->log_base = base;
1625 mutex_unlock(&vq->mutex);
1626 }
1627 break;
1628 case VHOST_SET_LOG_FD:
1629 r = get_user(fd, (int __user *)argp);
1630 if (r < 0)
1631 break;
1632 eventfp = fd == -1 ? NULL : eventfd_fget(fd);
1633 if (IS_ERR(eventfp)) {
1634 r = PTR_ERR(eventfp);
1635 break;
1636 }
1637 if (eventfp != d->log_file) {
1638 filep = d->log_file;
Marc-André Lureau7932c0b2015-07-17 15:32:03 +02001639 d->log_file = eventfp;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001640 ctx = d->log_ctx;
1641 d->log_ctx = eventfp ?
1642 eventfd_ctx_fileget(eventfp) : NULL;
1643 } else
1644 filep = eventfp;
1645 for (i = 0; i < d->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +08001646 mutex_lock(&d->vqs[i]->mutex);
1647 d->vqs[i]->log_ctx = d->log_ctx;
1648 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001649 }
1650 if (ctx)
1651 eventfd_ctx_put(ctx);
1652 if (filep)
1653 fput(filep);
1654 break;
1655 default:
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001656 r = -ENOIOCTLCMD;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001657 break;
1658 }
1659done:
1660 return r;
1661}
Asias He6ac1afb2013-05-06 16:38:21 +08001662EXPORT_SYMBOL_GPL(vhost_dev_ioctl);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001663
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001664/* TODO: This is really inefficient. We need something like get_user()
1665 * (instruction directly accesses the data, with an exception table entry
1666 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
1667 */
1668static int set_bit_to_user(int nr, void __user *addr)
1669{
1670 unsigned long log = (unsigned long)addr;
1671 struct page *page;
1672 void *base;
1673 int bit = nr + (log % PAGE_SIZE) * 8;
1674 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301675
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001676 r = get_user_pages_fast(log, 1, 1, &page);
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +02001677 if (r < 0)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001678 return r;
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +02001679 BUG_ON(r != 1);
Cong Wangc6daa7f2011-11-25 23:14:26 +08001680 base = kmap_atomic(page);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001681 set_bit(bit, base);
Cong Wangc6daa7f2011-11-25 23:14:26 +08001682 kunmap_atomic(base);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001683 set_page_dirty_lock(page);
1684 put_page(page);
1685 return 0;
1686}
1687
1688static int log_write(void __user *log_base,
1689 u64 write_address, u64 write_length)
1690{
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001691 u64 write_page = write_address / VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001692 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301693
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001694 if (!write_length)
1695 return 0;
Michael S. Tsirkin3bf9be42010-11-29 10:19:07 +02001696 write_length += write_address % VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001697 for (;;) {
1698 u64 base = (u64)(unsigned long)log_base;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001699 u64 log = base + write_page / 8;
1700 int bit = write_page % 8;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001701 if ((u64)(unsigned long)log != log)
1702 return -EFAULT;
1703 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
1704 if (r < 0)
1705 return r;
1706 if (write_length <= VHOST_PAGE_SIZE)
1707 break;
1708 write_length -= VHOST_PAGE_SIZE;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001709 write_page += 1;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001710 }
1711 return r;
1712}
1713
1714int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
1715 unsigned int log_num, u64 len)
1716{
1717 int i, r;
1718
1719 /* Make sure data written is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001720 smp_wmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001721 for (i = 0; i < log_num; ++i) {
1722 u64 l = min(log[i].len, len);
1723 r = log_write(vq->log_base, log[i].addr, l);
1724 if (r < 0)
1725 return r;
1726 len -= l;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +02001727 if (!len) {
1728 if (vq->log_ctx)
1729 eventfd_signal(vq->log_ctx, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001730 return 0;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +02001731 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001732 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001733 /* Length written exceeds what we have stored. This is a bug. */
1734 BUG();
1735 return 0;
1736}
Asias He6ac1afb2013-05-06 16:38:21 +08001737EXPORT_SYMBOL_GPL(vhost_log_write);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001738
Jason Wang2723fea2011-06-21 18:04:38 +08001739static int vhost_update_used_flags(struct vhost_virtqueue *vq)
1740{
1741 void __user *used;
Jason Wangbfe2bc52016-06-23 02:04:30 -04001742 if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->used_flags),
1743 &vq->used->flags) < 0)
Jason Wang2723fea2011-06-21 18:04:38 +08001744 return -EFAULT;
1745 if (unlikely(vq->log_used)) {
1746 /* Make sure the flag is seen before log. */
1747 smp_wmb();
1748 /* Log used flag write. */
1749 used = &vq->used->flags;
1750 log_write(vq->log_base, vq->log_addr +
1751 (used - (void __user *)vq->used),
1752 sizeof vq->used->flags);
1753 if (vq->log_ctx)
1754 eventfd_signal(vq->log_ctx, 1);
1755 }
1756 return 0;
1757}
1758
1759static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
1760{
Jason Wangbfe2bc52016-06-23 02:04:30 -04001761 if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->avail_idx),
1762 vhost_avail_event(vq)))
Jason Wang2723fea2011-06-21 18:04:38 +08001763 return -EFAULT;
1764 if (unlikely(vq->log_used)) {
1765 void __user *used;
1766 /* Make sure the event is seen before log. */
1767 smp_wmb();
1768 /* Log avail event write */
1769 used = vhost_avail_event(vq);
1770 log_write(vq->log_base, vq->log_addr +
1771 (used - (void __user *)vq->used),
1772 sizeof *vhost_avail_event(vq));
1773 if (vq->log_ctx)
1774 eventfd_signal(vq->log_ctx, 1);
1775 }
1776 return 0;
1777}
1778
Greg Kurz80f7d032016-02-16 15:59:44 +01001779int vhost_vq_init_access(struct vhost_virtqueue *vq)
Jason Wang2723fea2011-06-21 18:04:38 +08001780{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001781 __virtio16 last_used_idx;
Jason Wang2723fea2011-06-21 18:04:38 +08001782 int r;
Greg Kurze1f33be2016-02-16 15:54:28 +01001783 bool is_le = vq->is_le;
1784
Halil Pasiccda8bba2017-01-30 11:09:36 +01001785 if (!vq->private_data)
Jason Wang2723fea2011-06-21 18:04:38 +08001786 return 0;
Greg Kurz2751c982015-04-24 14:27:24 +02001787
1788 vhost_init_is_le(vq);
Jason Wang2723fea2011-06-21 18:04:38 +08001789
1790 r = vhost_update_used_flags(vq);
1791 if (r)
Greg Kurze1f33be2016-02-16 15:54:28 +01001792 goto err;
Jason Wang2723fea2011-06-21 18:04:38 +08001793 vq->signalled_used_valid = false;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001794 if (!vq->iotlb &&
1795 !access_ok(VERIFY_READ, &vq->used->idx, sizeof vq->used->idx)) {
Greg Kurze1f33be2016-02-16 15:54:28 +01001796 r = -EFAULT;
1797 goto err;
1798 }
Jason Wangf8894912017-02-28 17:56:02 +08001799 r = vhost_get_used(vq, last_used_idx, &vq->used->idx);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001800 if (r) {
1801 vq_err(vq, "Can't access used idx at %p\n",
1802 &vq->used->idx);
Greg Kurze1f33be2016-02-16 15:54:28 +01001803 goto err;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001804 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001805 vq->last_used_idx = vhost16_to_cpu(vq, last_used_idx);
Michael S. Tsirkin64f7f052014-12-01 17:39:39 +02001806 return 0;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001807
Greg Kurze1f33be2016-02-16 15:54:28 +01001808err:
1809 vq->is_le = is_le;
1810 return r;
Jason Wang2723fea2011-06-21 18:04:38 +08001811}
Greg Kurz80f7d032016-02-16 15:59:44 +01001812EXPORT_SYMBOL_GPL(vhost_vq_init_access);
Jason Wang2723fea2011-06-21 18:04:38 +08001813
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001814static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001815 struct iovec iov[], int iov_size, int access)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001816{
Jason Wanga9709d62016-06-23 02:04:31 -04001817 const struct vhost_umem_node *node;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001818 struct vhost_dev *dev = vq->dev;
1819 struct vhost_umem *umem = dev->iotlb ? dev->iotlb : dev->umem;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001820 struct iovec *_iov;
1821 u64 s = 0;
1822 int ret = 0;
1823
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001824 while ((u64)len > s) {
1825 u64 size;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001826 if (unlikely(ret >= iov_size)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001827 ret = -ENOBUFS;
1828 break;
1829 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001830
Jason Wanga9709d62016-06-23 02:04:31 -04001831 node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
1832 addr, addr + len - 1);
1833 if (node == NULL || node->start > addr) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001834 if (umem != dev->iotlb) {
1835 ret = -EFAULT;
1836 break;
1837 }
1838 ret = -EAGAIN;
1839 break;
1840 } else if (!(node->perm & access)) {
1841 ret = -EPERM;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001842 break;
1843 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001844
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001845 _iov = iov + ret;
Jason Wanga9709d62016-06-23 02:04:31 -04001846 size = node->size - addr + node->start;
Michael S. Tsirkinbd971202012-11-26 05:57:27 +00001847 _iov->iov_len = min((u64)len - s, size);
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001848 _iov->iov_base = (void __user *)(unsigned long)
Jason Wanga9709d62016-06-23 02:04:31 -04001849 (node->userspace_addr + addr - node->start);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001850 s += size;
1851 addr += size;
1852 ++ret;
1853 }
1854
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001855 if (ret == -EAGAIN)
1856 vhost_iotlb_miss(vq, addr, access);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001857 return ret;
1858}
1859
1860/* Each buffer in the virtqueues is actually a chain of descriptors. This
1861 * function returns the next descriptor in the chain,
1862 * or -1U if we're at the end. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001863static unsigned next_desc(struct vhost_virtqueue *vq, struct vring_desc *desc)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001864{
1865 unsigned int next;
1866
1867 /* If this descriptor says it doesn't chain, we're done. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001868 if (!(desc->flags & cpu_to_vhost16(vq, VRING_DESC_F_NEXT)))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001869 return -1U;
1870
1871 /* Check they're not leading us off end of descriptors. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001872 next = vhost16_to_cpu(vq, desc->next);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001873 /* Make sure compiler knows to grab that: we don't want it changing! */
1874 /* We will use the result as an index in an array, so most
1875 * architectures only need a compiler barrier here. */
1876 read_barrier_depends();
1877
1878 return next;
1879}
1880
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001881static int get_indirect(struct vhost_virtqueue *vq,
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001882 struct iovec iov[], unsigned int iov_size,
1883 unsigned int *out_num, unsigned int *in_num,
1884 struct vhost_log *log, unsigned int *log_num,
1885 struct vring_desc *indirect)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001886{
1887 struct vring_desc desc;
1888 unsigned int i = 0, count, found = 0;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001889 u32 len = vhost32_to_cpu(vq, indirect->len);
Al Viroaad9a1c2014-12-10 14:49:01 -05001890 struct iov_iter from;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001891 int ret, access;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001892
1893 /* Sanity check */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001894 if (unlikely(len % sizeof desc)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001895 vq_err(vq, "Invalid length in indirect descriptor: "
1896 "len 0x%llx not multiple of 0x%zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001897 (unsigned long long)len,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001898 sizeof desc);
1899 return -EINVAL;
1900 }
1901
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001902 ret = translate_desc(vq, vhost64_to_cpu(vq, indirect->addr), len, vq->indirect,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001903 UIO_MAXIOV, VHOST_ACCESS_RO);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001904 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001905 if (ret != -EAGAIN)
1906 vq_err(vq, "Translation failure %d in indirect.\n", ret);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001907 return ret;
1908 }
Al Viroaad9a1c2014-12-10 14:49:01 -05001909 iov_iter_init(&from, READ, vq->indirect, ret, len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001910
1911 /* We will use the result as an address to read from, so most
1912 * architectures only need a compiler barrier here. */
1913 read_barrier_depends();
1914
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001915 count = len / sizeof desc;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001916 /* Buffers are chained via a 16 bit next field, so
1917 * we can have at most 2^16 of these. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001918 if (unlikely(count > USHRT_MAX + 1)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001919 vq_err(vq, "Indirect buffer length too big: %d\n",
1920 indirect->len);
1921 return -E2BIG;
1922 }
1923
1924 do {
1925 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001926 if (unlikely(++found > count)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001927 vq_err(vq, "Loop detected: last one at %u "
1928 "indirect size %u\n",
1929 i, count);
1930 return -EINVAL;
1931 }
Al Virocbbd26b2016-11-01 22:09:04 -04001932 if (unlikely(!copy_from_iter_full(&desc, sizeof(desc), &from))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001933 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001934 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001935 return -EINVAL;
1936 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001937 if (unlikely(desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001938 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001939 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001940 return -EINVAL;
1941 }
1942
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001943 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE))
1944 access = VHOST_ACCESS_WO;
1945 else
1946 access = VHOST_ACCESS_RO;
1947
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001948 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
1949 vhost32_to_cpu(vq, desc.len), iov + iov_count,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001950 iov_size - iov_count, access);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001951 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001952 if (ret != -EAGAIN)
1953 vq_err(vq, "Translation failure %d indirect idx %d\n",
1954 ret, i);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001955 return ret;
1956 }
1957 /* If this is an input descriptor, increment that count. */
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001958 if (access == VHOST_ACCESS_WO) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001959 *in_num += ret;
1960 if (unlikely(log)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001961 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
1962 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001963 ++*log_num;
1964 }
1965 } else {
1966 /* If it's an output descriptor, they're all supposed
1967 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001968 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001969 vq_err(vq, "Indirect descriptor "
1970 "has out after in: idx %d\n", i);
1971 return -EINVAL;
1972 }
1973 *out_num += ret;
1974 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001975 } while ((i = next_desc(vq, &desc)) != -1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001976 return 0;
1977}
1978
1979/* This looks in the virtqueue and for the first available buffer, and converts
1980 * it to an iovec for convenient access. Since descriptors consist of some
1981 * number of output then some number of input descriptors, it's actually two
1982 * iovecs, but we pack them into one and note how many of each there were.
1983 *
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001984 * This function returns the descriptor number found, or vq->num (which is
1985 * never a valid descriptor number) if none was found. A negative code is
1986 * returned on error. */
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001987int vhost_get_vq_desc(struct vhost_virtqueue *vq,
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001988 struct iovec iov[], unsigned int iov_size,
1989 unsigned int *out_num, unsigned int *in_num,
1990 struct vhost_log *log, unsigned int *log_num)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001991{
1992 struct vring_desc desc;
1993 unsigned int i, head, found = 0;
1994 u16 last_avail_idx;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001995 __virtio16 avail_idx;
1996 __virtio16 ring_head;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001997 int ret, access;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001998
1999 /* Check it isn't doing very strange things with descriptor numbers. */
2000 last_avail_idx = vq->last_avail_idx;
Jason Wange3b56cd2017-02-07 15:49:50 +08002001
2002 if (vq->avail_idx == vq->last_avail_idx) {
Jason Wangf8894912017-02-28 17:56:02 +08002003 if (unlikely(vhost_get_avail(vq, avail_idx, &vq->avail->idx))) {
Jason Wange3b56cd2017-02-07 15:49:50 +08002004 vq_err(vq, "Failed to access avail idx at %p\n",
2005 &vq->avail->idx);
2006 return -EFAULT;
2007 }
2008 vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
2009
2010 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
2011 vq_err(vq, "Guest moved used index from %u to %u",
2012 last_avail_idx, vq->avail_idx);
2013 return -EFAULT;
2014 }
2015
2016 /* If there's nothing new since last we looked, return
2017 * invalid.
2018 */
2019 if (vq->avail_idx == last_avail_idx)
2020 return vq->num;
2021
2022 /* Only get avail ring entries after they have been
2023 * exposed by guest.
2024 */
2025 smp_rmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002026 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002027
2028 /* Grab the next descriptor number they're advertising, and increment
2029 * the index we've seen. */
Jason Wangf8894912017-02-28 17:56:02 +08002030 if (unlikely(vhost_get_avail(vq, ring_head,
Jason Wangbfe2bc52016-06-23 02:04:30 -04002031 &vq->avail->ring[last_avail_idx & (vq->num - 1)]))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002032 vq_err(vq, "Failed to read head: idx %d address %p\n",
2033 last_avail_idx,
2034 &vq->avail->ring[last_avail_idx % vq->num]);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002035 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002036 }
2037
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002038 head = vhost16_to_cpu(vq, ring_head);
2039
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002040 /* If their number is silly, that's an error. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002041 if (unlikely(head >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002042 vq_err(vq, "Guest says index %u > %u is available",
2043 head, vq->num);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002044 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002045 }
2046
2047 /* When we start there are none of either input nor output. */
2048 *out_num = *in_num = 0;
2049 if (unlikely(log))
2050 *log_num = 0;
2051
2052 i = head;
2053 do {
2054 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002055 if (unlikely(i >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002056 vq_err(vq, "Desc index is %u > %u, head = %u",
2057 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002058 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002059 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002060 if (unlikely(++found > vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002061 vq_err(vq, "Loop detected: last one at %u "
2062 "vq size %u head %u\n",
2063 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002064 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002065 }
Jason Wangbfe2bc52016-06-23 02:04:30 -04002066 ret = vhost_copy_from_user(vq, &desc, vq->desc + i,
2067 sizeof desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002068 if (unlikely(ret)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002069 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
2070 i, vq->desc + i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002071 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002072 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002073 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT)) {
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03002074 ret = get_indirect(vq, iov, iov_size,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002075 out_num, in_num,
2076 log, log_num, &desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002077 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002078 if (ret != -EAGAIN)
2079 vq_err(vq, "Failure detected "
2080 "in indirect descriptor at idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002081 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002082 }
2083 continue;
2084 }
2085
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002086 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE))
2087 access = VHOST_ACCESS_WO;
2088 else
2089 access = VHOST_ACCESS_RO;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002090 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
2091 vhost32_to_cpu(vq, desc.len), iov + iov_count,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002092 iov_size - iov_count, access);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002093 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002094 if (ret != -EAGAIN)
2095 vq_err(vq, "Translation failure %d descriptor idx %d\n",
2096 ret, i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002097 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002098 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002099 if (access == VHOST_ACCESS_WO) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002100 /* If this is an input descriptor,
2101 * increment that count. */
2102 *in_num += ret;
2103 if (unlikely(log)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002104 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
2105 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002106 ++*log_num;
2107 }
2108 } else {
2109 /* If it's an output descriptor, they're all supposed
2110 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002111 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002112 vq_err(vq, "Descriptor has out after in: "
2113 "idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002114 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002115 }
2116 *out_num += ret;
2117 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002118 } while ((i = next_desc(vq, &desc)) != -1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002119
2120 /* On success, increment avail index. */
2121 vq->last_avail_idx++;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002122
2123 /* Assume notifications from guest are disabled at this point,
2124 * if they aren't we would need to update avail_event index. */
2125 BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002126 return head;
2127}
Asias He6ac1afb2013-05-06 16:38:21 +08002128EXPORT_SYMBOL_GPL(vhost_get_vq_desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002129
2130/* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
David Stevens8dd014a2010-07-27 18:52:21 +03002131void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002132{
David Stevens8dd014a2010-07-27 18:52:21 +03002133 vq->last_avail_idx -= n;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002134}
Asias He6ac1afb2013-05-06 16:38:21 +08002135EXPORT_SYMBOL_GPL(vhost_discard_vq_desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002136
2137/* After we've used one of their buffers, we tell them about it. We'll then
2138 * want to notify the guest, using eventfd. */
2139int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
2140{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002141 struct vring_used_elem heads = {
2142 cpu_to_vhost32(vq, head),
2143 cpu_to_vhost32(vq, len)
2144 };
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002145
Jason Wangc49e4e52013-09-02 16:40:58 +08002146 return vhost_add_used_n(vq, &heads, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002147}
Asias He6ac1afb2013-05-06 16:38:21 +08002148EXPORT_SYMBOL_GPL(vhost_add_used);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002149
David Stevens8dd014a2010-07-27 18:52:21 +03002150static int __vhost_add_used_n(struct vhost_virtqueue *vq,
2151 struct vring_used_elem *heads,
2152 unsigned count)
2153{
2154 struct vring_used_elem __user *used;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002155 u16 old, new;
David Stevens8dd014a2010-07-27 18:52:21 +03002156 int start;
2157
Michael S. Tsirkin5fba13b2015-11-29 13:34:44 +02002158 start = vq->last_used_idx & (vq->num - 1);
David Stevens8dd014a2010-07-27 18:52:21 +03002159 used = vq->used->ring + start;
Jason Wangc49e4e52013-09-02 16:40:58 +08002160 if (count == 1) {
Jason Wangbfe2bc52016-06-23 02:04:30 -04002161 if (vhost_put_user(vq, heads[0].id, &used->id)) {
Jason Wangc49e4e52013-09-02 16:40:58 +08002162 vq_err(vq, "Failed to write used id");
2163 return -EFAULT;
2164 }
Jason Wangbfe2bc52016-06-23 02:04:30 -04002165 if (vhost_put_user(vq, heads[0].len, &used->len)) {
Jason Wangc49e4e52013-09-02 16:40:58 +08002166 vq_err(vq, "Failed to write used len");
2167 return -EFAULT;
2168 }
Jason Wangbfe2bc52016-06-23 02:04:30 -04002169 } else if (vhost_copy_to_user(vq, used, heads, count * sizeof *used)) {
David Stevens8dd014a2010-07-27 18:52:21 +03002170 vq_err(vq, "Failed to write used");
2171 return -EFAULT;
2172 }
2173 if (unlikely(vq->log_used)) {
2174 /* Make sure data is seen before log. */
2175 smp_wmb();
2176 /* Log used ring entry write. */
2177 log_write(vq->log_base,
2178 vq->log_addr +
2179 ((void __user *)used - (void __user *)vq->used),
2180 count * sizeof *used);
2181 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002182 old = vq->last_used_idx;
2183 new = (vq->last_used_idx += count);
2184 /* If the driver never bothers to signal in a very long while,
2185 * used index might wrap around. If that happens, invalidate
2186 * signalled_used index we stored. TODO: make sure driver
2187 * signals at least once in 2^16 and remove this. */
2188 if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old)))
2189 vq->signalled_used_valid = false;
David Stevens8dd014a2010-07-27 18:52:21 +03002190 return 0;
2191}
2192
2193/* After we've used one of their buffers, we tell them about it. We'll then
2194 * want to notify the guest, using eventfd. */
2195int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
2196 unsigned count)
2197{
2198 int start, n, r;
2199
Michael S. Tsirkin5fba13b2015-11-29 13:34:44 +02002200 start = vq->last_used_idx & (vq->num - 1);
David Stevens8dd014a2010-07-27 18:52:21 +03002201 n = vq->num - start;
2202 if (n < count) {
2203 r = __vhost_add_used_n(vq, heads, n);
2204 if (r < 0)
2205 return r;
2206 heads += n;
2207 count -= n;
2208 }
2209 r = __vhost_add_used_n(vq, heads, count);
2210
2211 /* Make sure buffer is written before we update index. */
2212 smp_wmb();
Jason Wangbfe2bc52016-06-23 02:04:30 -04002213 if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->last_used_idx),
2214 &vq->used->idx)) {
David Stevens8dd014a2010-07-27 18:52:21 +03002215 vq_err(vq, "Failed to increment used idx");
2216 return -EFAULT;
2217 }
2218 if (unlikely(vq->log_used)) {
2219 /* Log used index update. */
2220 log_write(vq->log_base,
2221 vq->log_addr + offsetof(struct vring_used, idx),
2222 sizeof vq->used->idx);
2223 if (vq->log_ctx)
2224 eventfd_signal(vq->log_ctx, 1);
2225 }
2226 return r;
2227}
Asias He6ac1afb2013-05-06 16:38:21 +08002228EXPORT_SYMBOL_GPL(vhost_add_used_n);
David Stevens8dd014a2010-07-27 18:52:21 +03002229
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002230static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002231{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002232 __u16 old, new;
2233 __virtio16 event;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002234 bool v;
Jason Wang8d658432017-07-27 11:22:05 +08002235 /* Flush out used index updates. This is paired
2236 * with the barrier that the Guest executes when enabling
2237 * interrupts. */
2238 smp_mb();
Michael S. Tsirkin0d499352010-05-11 19:44:17 +03002239
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002240 if (vhost_has_feature(vq, VIRTIO_F_NOTIFY_ON_EMPTY) &&
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002241 unlikely(vq->avail_idx == vq->last_avail_idx))
2242 return true;
2243
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002244 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002245 __virtio16 flags;
Jason Wangf8894912017-02-28 17:56:02 +08002246 if (vhost_get_avail(vq, flags, &vq->avail->flags)) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002247 vq_err(vq, "Failed to get flags");
2248 return true;
2249 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002250 return !(flags & cpu_to_vhost16(vq, VRING_AVAIL_F_NO_INTERRUPT));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002251 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002252 old = vq->signalled_used;
2253 v = vq->signalled_used_valid;
2254 new = vq->signalled_used = vq->last_used_idx;
2255 vq->signalled_used_valid = true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002256
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002257 if (unlikely(!v))
2258 return true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002259
Jason Wangf8894912017-02-28 17:56:02 +08002260 if (vhost_get_avail(vq, event, vhost_used_event(vq))) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002261 vq_err(vq, "Failed to get used event idx");
2262 return true;
2263 }
Jason Wang8d658432017-07-27 11:22:05 +08002264 return vring_need_event(vhost16_to_cpu(vq, event), new, old);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002265}
2266
2267/* This actually signals the guest, using eventfd. */
2268void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
2269{
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002270 /* Signal the Guest tell them we used something up. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002271 if (vq->call_ctx && vhost_notify(dev, vq))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002272 eventfd_signal(vq->call_ctx, 1);
2273}
Asias He6ac1afb2013-05-06 16:38:21 +08002274EXPORT_SYMBOL_GPL(vhost_signal);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002275
2276/* And here's the combo meal deal. Supersize me! */
2277void vhost_add_used_and_signal(struct vhost_dev *dev,
2278 struct vhost_virtqueue *vq,
2279 unsigned int head, int len)
2280{
2281 vhost_add_used(vq, head, len);
2282 vhost_signal(dev, vq);
2283}
Asias He6ac1afb2013-05-06 16:38:21 +08002284EXPORT_SYMBOL_GPL(vhost_add_used_and_signal);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002285
David Stevens8dd014a2010-07-27 18:52:21 +03002286/* multi-buffer version of vhost_add_used_and_signal */
2287void vhost_add_used_and_signal_n(struct vhost_dev *dev,
2288 struct vhost_virtqueue *vq,
2289 struct vring_used_elem *heads, unsigned count)
2290{
2291 vhost_add_used_n(vq, heads, count);
2292 vhost_signal(dev, vq);
2293}
Asias He6ac1afb2013-05-06 16:38:21 +08002294EXPORT_SYMBOL_GPL(vhost_add_used_and_signal_n);
David Stevens8dd014a2010-07-27 18:52:21 +03002295
Jason Wangd4a60602016-03-04 06:24:52 -05002296/* return true if we're sure that avaiable ring is empty */
2297bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq)
2298{
2299 __virtio16 avail_idx;
2300 int r;
2301
Jason Wang275bf962017-01-18 15:02:01 +08002302 if (vq->avail_idx != vq->last_avail_idx)
Jason Wangd4a60602016-03-04 06:24:52 -05002303 return false;
2304
Linus Torvalds54d79892017-03-02 13:53:13 -08002305 r = vhost_get_avail(vq, avail_idx, &vq->avail->idx);
Jason Wang275bf962017-01-18 15:02:01 +08002306 if (unlikely(r))
2307 return false;
2308 vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
2309
2310 return vq->avail_idx == vq->last_avail_idx;
Jason Wangd4a60602016-03-04 06:24:52 -05002311}
2312EXPORT_SYMBOL_GPL(vhost_vq_avail_empty);
2313
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002314/* OK, now we need to know about added descriptors. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002315bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002316{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002317 __virtio16 avail_idx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002318 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05302319
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002320 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
2321 return false;
2322 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002323 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08002324 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002325 if (r) {
2326 vq_err(vq, "Failed to enable notification at %p: %d\n",
2327 &vq->used->flags, r);
2328 return false;
2329 }
2330 } else {
Jason Wang2723fea2011-06-21 18:04:38 +08002331 r = vhost_update_avail_event(vq, vq->avail_idx);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002332 if (r) {
2333 vq_err(vq, "Failed to update avail event index at %p: %d\n",
2334 vhost_avail_event(vq), r);
2335 return false;
2336 }
2337 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002338 /* They could have slipped one in as we were doing that: make
2339 * sure it's written, then check again. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00002340 smp_mb();
Jason Wangf8894912017-02-28 17:56:02 +08002341 r = vhost_get_avail(vq, avail_idx, &vq->avail->idx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002342 if (r) {
2343 vq_err(vq, "Failed to check avail idx at %p: %d\n",
2344 &vq->avail->idx, r);
2345 return false;
2346 }
2347
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002348 return vhost16_to_cpu(vq, avail_idx) != vq->avail_idx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002349}
Asias He6ac1afb2013-05-06 16:38:21 +08002350EXPORT_SYMBOL_GPL(vhost_enable_notify);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002351
2352/* We don't need to be notified again. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002353void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002354{
2355 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05302356
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002357 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
2358 return;
2359 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002360 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08002361 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002362 if (r)
2363 vq_err(vq, "Failed to enable notification at %p: %d\n",
2364 &vq->used->flags, r);
2365 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002366}
Asias He6ac1afb2013-05-06 16:38:21 +08002367EXPORT_SYMBOL_GPL(vhost_disable_notify);
2368
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002369/* Create a new message. */
2370struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type)
2371{
2372 struct vhost_msg_node *node = kmalloc(sizeof *node, GFP_KERNEL);
2373 if (!node)
2374 return NULL;
2375 node->vq = vq;
2376 node->msg.type = type;
2377 return node;
2378}
2379EXPORT_SYMBOL_GPL(vhost_new_msg);
2380
2381void vhost_enqueue_msg(struct vhost_dev *dev, struct list_head *head,
2382 struct vhost_msg_node *node)
2383{
2384 spin_lock(&dev->iotlb_lock);
2385 list_add_tail(&node->node, head);
2386 spin_unlock(&dev->iotlb_lock);
2387
2388 wake_up_interruptible_poll(&dev->wait, POLLIN | POLLRDNORM);
2389}
2390EXPORT_SYMBOL_GPL(vhost_enqueue_msg);
2391
2392struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev,
2393 struct list_head *head)
2394{
2395 struct vhost_msg_node *node = NULL;
2396
2397 spin_lock(&dev->iotlb_lock);
2398 if (!list_empty(head)) {
2399 node = list_first_entry(head, struct vhost_msg_node,
2400 node);
2401 list_del(&node->node);
2402 }
2403 spin_unlock(&dev->iotlb_lock);
2404
2405 return node;
2406}
2407EXPORT_SYMBOL_GPL(vhost_dequeue_msg);
2408
2409
Asias He6ac1afb2013-05-06 16:38:21 +08002410static int __init vhost_init(void)
2411{
2412 return 0;
2413}
2414
2415static void __exit vhost_exit(void)
2416{
2417}
2418
2419module_init(vhost_init);
2420module_exit(vhost_exit);
2421
2422MODULE_VERSION("0.0.1");
2423MODULE_LICENSE("GPL v2");
2424MODULE_AUTHOR("Michael S. Tsirkin");
2425MODULE_DESCRIPTION("Host kernel accelerator for virtio");