blob: 328c54ab01544929f2d0ba476f2bbc1f1c688ea8 [file] [log] [blame]
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001/* Copyright (C) 2009 Red Hat, Inc.
2 * Copyright (C) 2006 Rusty Russell IBM Corporation
3 *
4 * Author: Michael S. Tsirkin <mst@redhat.com>
5 *
6 * Inspiration, some code, and most witty comments come from
Rob Landley61516582011-05-06 09:27:36 -07007 * Documentation/virtual/lguest/lguest.c, by Rusty Russell
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00008 *
9 * This work is licensed under the terms of the GNU GPL, version 2.
10 *
11 * Generic code for virtio server in host kernel.
12 */
13
14#include <linux/eventfd.h>
15#include <linux/vhost.h>
Asias He35596b22013-08-19 09:23:19 +080016#include <linux/uio.h>
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000017#include <linux/mm.h>
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +020018#include <linux/mmu_context.h>
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000019#include <linux/miscdevice.h>
20#include <linux/mutex.h>
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000021#include <linux/poll.h>
22#include <linux/file.h>
23#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Igor Mammedov4de72552015-07-01 11:07:09 +020025#include <linux/vmalloc.h>
Tejun Heoc23f34452010-06-02 20:40:00 +020026#include <linux/kthread.h>
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +030027#include <linux/cgroup.h>
Asias He6ac1afb2013-05-06 16:38:21 +080028#include <linux/module.h>
Igor Mammedovbcfeaca2015-06-16 18:33:35 +020029#include <linux/sort.h>
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000030
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000031#include "vhost.h"
32
Igor Mammedovc9ce42f2015-07-02 15:08:11 +020033static ushort max_mem_regions = 64;
34module_param(max_mem_regions, ushort, 0444);
35MODULE_PARM_DESC(max_mem_regions,
36 "Maximum number of memory regions in memory map. (default: 64)");
37
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000038enum {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000039 VHOST_MEMORY_F_LOG = 0x1,
40};
41
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +030042#define vhost_used_event(vq) ((__virtio16 __user *)&vq->avail->ring[vq->num])
43#define vhost_avail_event(vq) ((__virtio16 __user *)&vq->used->ring[vq->num])
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +030044
Greg Kurz2751c982015-04-24 14:27:24 +020045#ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
Greg Kurzc5072032016-02-16 15:59:34 +010046static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
Greg Kurz2751c982015-04-24 14:27:24 +020047{
48 vq->user_be = !virtio_legacy_is_little_endian();
49}
50
Greg Kurzc5072032016-02-16 15:59:34 +010051static void vhost_enable_cross_endian_big(struct vhost_virtqueue *vq)
52{
53 vq->user_be = true;
54}
55
56static void vhost_enable_cross_endian_little(struct vhost_virtqueue *vq)
57{
58 vq->user_be = false;
59}
60
Greg Kurz2751c982015-04-24 14:27:24 +020061static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
62{
63 struct vhost_vring_state s;
64
65 if (vq->private_data)
66 return -EBUSY;
67
68 if (copy_from_user(&s, argp, sizeof(s)))
69 return -EFAULT;
70
71 if (s.num != VHOST_VRING_LITTLE_ENDIAN &&
72 s.num != VHOST_VRING_BIG_ENDIAN)
73 return -EINVAL;
74
Greg Kurzc5072032016-02-16 15:59:34 +010075 if (s.num == VHOST_VRING_BIG_ENDIAN)
76 vhost_enable_cross_endian_big(vq);
77 else
78 vhost_enable_cross_endian_little(vq);
Greg Kurz2751c982015-04-24 14:27:24 +020079
80 return 0;
81}
82
83static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
84 int __user *argp)
85{
86 struct vhost_vring_state s = {
87 .index = idx,
88 .num = vq->user_be
89 };
90
91 if (copy_to_user(argp, &s, sizeof(s)))
92 return -EFAULT;
93
94 return 0;
95}
96
97static void vhost_init_is_le(struct vhost_virtqueue *vq)
98{
99 /* Note for legacy virtio: user_be is initialized at reset time
100 * according to the host endianness. If userspace does not set an
101 * explicit endianness, the default behavior is native endian, as
102 * expected by legacy virtio.
103 */
104 vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) || !vq->user_be;
105}
106#else
Greg Kurzc5072032016-02-16 15:59:34 +0100107static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
Greg Kurz2751c982015-04-24 14:27:24 +0200108{
109}
110
111static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
112{
113 return -ENOIOCTLCMD;
114}
115
116static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
117 int __user *argp)
118{
119 return -ENOIOCTLCMD;
120}
121
122static void vhost_init_is_le(struct vhost_virtqueue *vq)
123{
124 if (vhost_has_feature(vq, VIRTIO_F_VERSION_1))
125 vq->is_le = true;
126}
127#endif /* CONFIG_VHOST_CROSS_ENDIAN_LEGACY */
128
Greg Kurzc5072032016-02-16 15:59:34 +0100129static void vhost_reset_is_le(struct vhost_virtqueue *vq)
130{
131 vq->is_le = virtio_legacy_is_little_endian();
132}
133
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000134static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
135 poll_table *pt)
136{
137 struct vhost_poll *poll;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000138
Krishna Kumard47effe2011-03-01 17:06:37 +0530139 poll = container_of(pt, struct vhost_poll, table);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000140 poll->wqh = wqh;
141 add_wait_queue(wqh, &poll->wait);
142}
143
144static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync,
145 void *key)
146{
Tejun Heoc23f34452010-06-02 20:40:00 +0200147 struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
148
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000149 if (!((unsigned long)key & poll->mask))
150 return 0;
151
Tejun Heoc23f34452010-06-02 20:40:00 +0200152 vhost_poll_queue(poll);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000153 return 0;
154}
155
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000156void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000157{
Tejun Heoc23f34452010-06-02 20:40:00 +0200158 INIT_LIST_HEAD(&work->node);
159 work->fn = fn;
160 init_waitqueue_head(&work->done);
161 work->flushing = 0;
162 work->queue_seq = work->done_seq = 0;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000163}
Asias He6ac1afb2013-05-06 16:38:21 +0800164EXPORT_SYMBOL_GPL(vhost_work_init);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000165
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300166/* Init poll structure */
167void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
168 unsigned long mask, struct vhost_dev *dev)
169{
170 init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
171 init_poll_funcptr(&poll->table, vhost_poll_func);
172 poll->mask = mask;
173 poll->dev = dev;
Jason Wang2b8b3282013-01-28 01:05:18 +0000174 poll->wqh = NULL;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300175
176 vhost_work_init(&poll->work, fn);
177}
Asias He6ac1afb2013-05-06 16:38:21 +0800178EXPORT_SYMBOL_GPL(vhost_poll_init);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300179
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000180/* Start polling a file. We add ourselves to file's wait queue. The caller must
181 * keep a reference to a file until after vhost_poll_stop is called. */
Jason Wang2b8b3282013-01-28 01:05:18 +0000182int vhost_poll_start(struct vhost_poll *poll, struct file *file)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000183{
184 unsigned long mask;
Jason Wang2b8b3282013-01-28 01:05:18 +0000185 int ret = 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530186
Jason Wang70181d512013-04-10 20:50:48 +0000187 if (poll->wqh)
188 return 0;
189
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000190 mask = file->f_op->poll(file, &poll->table);
191 if (mask)
192 vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
Jason Wang2b8b3282013-01-28 01:05:18 +0000193 if (mask & POLLERR) {
194 if (poll->wqh)
195 remove_wait_queue(poll->wqh, &poll->wait);
196 ret = -EINVAL;
197 }
198
199 return ret;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000200}
Asias He6ac1afb2013-05-06 16:38:21 +0800201EXPORT_SYMBOL_GPL(vhost_poll_start);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000202
203/* Stop polling a file. After this function returns, it becomes safe to drop the
204 * file reference. You must also flush afterwards. */
205void vhost_poll_stop(struct vhost_poll *poll)
206{
Jason Wang2b8b3282013-01-28 01:05:18 +0000207 if (poll->wqh) {
208 remove_wait_queue(poll->wqh, &poll->wait);
209 poll->wqh = NULL;
210 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000211}
Asias He6ac1afb2013-05-06 16:38:21 +0800212EXPORT_SYMBOL_GPL(vhost_poll_stop);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000213
Michael S. Tsirkin0174b0c2011-01-10 10:03:20 +0200214static bool vhost_work_seq_done(struct vhost_dev *dev, struct vhost_work *work,
215 unsigned seq)
216{
217 int left;
Krishna Kumard47effe2011-03-01 17:06:37 +0530218
Michael S. Tsirkin0174b0c2011-01-10 10:03:20 +0200219 spin_lock_irq(&dev->work_lock);
220 left = seq - work->done_seq;
221 spin_unlock_irq(&dev->work_lock);
222 return left <= 0;
223}
224
Asias He6ac1afb2013-05-06 16:38:21 +0800225void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000226{
Tejun Heoc23f34452010-06-02 20:40:00 +0200227 unsigned seq;
Tejun Heoc23f34452010-06-02 20:40:00 +0200228 int flushing;
229
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300230 spin_lock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200231 seq = work->queue_seq;
232 work->flushing++;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300233 spin_unlock_irq(&dev->work_lock);
Michael S. Tsirkin0174b0c2011-01-10 10:03:20 +0200234 wait_event(work->done, vhost_work_seq_done(dev, work, seq));
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300235 spin_lock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200236 flushing = --work->flushing;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300237 spin_unlock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200238 BUG_ON(flushing < 0);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000239}
Asias He6ac1afb2013-05-06 16:38:21 +0800240EXPORT_SYMBOL_GPL(vhost_work_flush);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000241
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300242/* Flush any work that has been scheduled. When calling this, don't hold any
243 * locks that are also used by the callback. */
244void vhost_poll_flush(struct vhost_poll *poll)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000245{
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300246 vhost_work_flush(poll->dev, &poll->work);
247}
Asias He6ac1afb2013-05-06 16:38:21 +0800248EXPORT_SYMBOL_GPL(vhost_poll_flush);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300249
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000250void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300251{
Tejun Heoc23f34452010-06-02 20:40:00 +0200252 unsigned long flags;
253
254 spin_lock_irqsave(&dev->work_lock, flags);
255 if (list_empty(&work->node)) {
256 list_add_tail(&work->node, &dev->work_list);
257 work->queue_seq++;
Qin Chuanyuac9fde22013-06-07 21:50:16 +0800258 spin_unlock_irqrestore(&dev->work_lock, flags);
Tejun Heoc23f34452010-06-02 20:40:00 +0200259 wake_up_process(dev->worker);
Qin Chuanyuac9fde22013-06-07 21:50:16 +0800260 } else {
261 spin_unlock_irqrestore(&dev->work_lock, flags);
Tejun Heoc23f34452010-06-02 20:40:00 +0200262 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000263}
Asias He6ac1afb2013-05-06 16:38:21 +0800264EXPORT_SYMBOL_GPL(vhost_work_queue);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000265
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300266void vhost_poll_queue(struct vhost_poll *poll)
267{
268 vhost_work_queue(poll->dev, &poll->work);
269}
Asias He6ac1afb2013-05-06 16:38:21 +0800270EXPORT_SYMBOL_GPL(vhost_poll_queue);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300271
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000272static void vhost_vq_reset(struct vhost_dev *dev,
273 struct vhost_virtqueue *vq)
274{
275 vq->num = 1;
276 vq->desc = NULL;
277 vq->avail = NULL;
278 vq->used = NULL;
279 vq->last_avail_idx = 0;
280 vq->avail_idx = 0;
281 vq->last_used_idx = 0;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300282 vq->signalled_used = 0;
283 vq->signalled_used_valid = false;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000284 vq->used_flags = 0;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000285 vq->log_used = false;
286 vq->log_addr = -1ull;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000287 vq->private_data = NULL;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300288 vq->acked_features = 0;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000289 vq->log_base = NULL;
290 vq->error_ctx = NULL;
291 vq->error = NULL;
292 vq->kick = NULL;
293 vq->call_ctx = NULL;
294 vq->call = NULL;
Michael S. Tsirkin73a99f02010-02-23 11:23:45 +0200295 vq->log_ctx = NULL;
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300296 vq->memory = NULL;
Greg Kurzc5072032016-02-16 15:59:34 +0100297 vhost_reset_is_le(vq);
298 vhost_disable_cross_endian(vq);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000299}
300
Tejun Heoc23f34452010-06-02 20:40:00 +0200301static int vhost_worker(void *data)
302{
303 struct vhost_dev *dev = data;
304 struct vhost_work *work = NULL;
305 unsigned uninitialized_var(seq);
Jens Freimannd7ffde32012-06-26 00:59:58 +0000306 mm_segment_t oldfs = get_fs();
Tejun Heoc23f34452010-06-02 20:40:00 +0200307
Jens Freimannd7ffde32012-06-26 00:59:58 +0000308 set_fs(USER_DS);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200309 use_mm(dev->mm);
310
Tejun Heoc23f34452010-06-02 20:40:00 +0200311 for (;;) {
312 /* mb paired w/ kthread_stop */
313 set_current_state(TASK_INTERRUPTIBLE);
314
315 spin_lock_irq(&dev->work_lock);
316 if (work) {
317 work->done_seq = seq;
318 if (work->flushing)
319 wake_up_all(&work->done);
320 }
321
322 if (kthread_should_stop()) {
323 spin_unlock_irq(&dev->work_lock);
324 __set_current_state(TASK_RUNNING);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200325 break;
Tejun Heoc23f34452010-06-02 20:40:00 +0200326 }
327 if (!list_empty(&dev->work_list)) {
328 work = list_first_entry(&dev->work_list,
329 struct vhost_work, node);
330 list_del_init(&work->node);
331 seq = work->queue_seq;
332 } else
333 work = NULL;
334 spin_unlock_irq(&dev->work_lock);
335
336 if (work) {
337 __set_current_state(TASK_RUNNING);
338 work->fn(work);
Nadav Har'Eld550dda2012-02-27 15:07:29 +0200339 if (need_resched())
340 schedule();
Tejun Heoc23f34452010-06-02 20:40:00 +0200341 } else
342 schedule();
343
344 }
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200345 unuse_mm(dev->mm);
Jens Freimannd7ffde32012-06-26 00:59:58 +0000346 set_fs(oldfs);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200347 return 0;
Tejun Heoc23f34452010-06-02 20:40:00 +0200348}
349
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000350static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
351{
352 kfree(vq->indirect);
353 vq->indirect = NULL;
354 kfree(vq->log);
355 vq->log = NULL;
356 kfree(vq->heads);
357 vq->heads = NULL;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000358}
359
Jason Wange0e9b402010-09-14 23:53:05 +0800360/* Helper to allocate iovec buffers for all vqs. */
361static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
362{
Asias He6d5e6aa2013-05-06 16:38:23 +0800363 struct vhost_virtqueue *vq;
Jason Wange0e9b402010-09-14 23:53:05 +0800364 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530365
Jason Wange0e9b402010-09-14 23:53:05 +0800366 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800367 vq = dev->vqs[i];
368 vq->indirect = kmalloc(sizeof *vq->indirect * UIO_MAXIOV,
369 GFP_KERNEL);
370 vq->log = kmalloc(sizeof *vq->log * UIO_MAXIOV, GFP_KERNEL);
371 vq->heads = kmalloc(sizeof *vq->heads * UIO_MAXIOV, GFP_KERNEL);
372 if (!vq->indirect || !vq->log || !vq->heads)
Jason Wange0e9b402010-09-14 23:53:05 +0800373 goto err_nomem;
374 }
375 return 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530376
Jason Wange0e9b402010-09-14 23:53:05 +0800377err_nomem:
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000378 for (; i >= 0; --i)
Asias He3ab2e422013-04-27 11:16:48 +0800379 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800380 return -ENOMEM;
381}
382
383static void vhost_dev_free_iovecs(struct vhost_dev *dev)
384{
385 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530386
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000387 for (i = 0; i < dev->nvqs; ++i)
Asias He3ab2e422013-04-27 11:16:48 +0800388 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800389}
390
Zhi Yong Wu59566b6e2013-12-07 04:13:03 +0800391void vhost_dev_init(struct vhost_dev *dev,
Asias He3ab2e422013-04-27 11:16:48 +0800392 struct vhost_virtqueue **vqs, int nvqs)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000393{
Asias He6d5e6aa2013-05-06 16:38:23 +0800394 struct vhost_virtqueue *vq;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000395 int i;
Tejun Heoc23f34452010-06-02 20:40:00 +0200396
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000397 dev->vqs = vqs;
398 dev->nvqs = nvqs;
399 mutex_init(&dev->mutex);
400 dev->log_ctx = NULL;
401 dev->log_file = NULL;
402 dev->memory = NULL;
403 dev->mm = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200404 spin_lock_init(&dev->work_lock);
405 INIT_LIST_HEAD(&dev->work_list);
406 dev->worker = NULL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000407
408 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800409 vq = dev->vqs[i];
410 vq->log = NULL;
411 vq->indirect = NULL;
412 vq->heads = NULL;
413 vq->dev = dev;
414 mutex_init(&vq->mutex);
415 vhost_vq_reset(dev, vq);
416 if (vq->handle_kick)
417 vhost_poll_init(&vq->poll, vq->handle_kick,
418 POLLIN, dev);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000419 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000420}
Asias He6ac1afb2013-05-06 16:38:21 +0800421EXPORT_SYMBOL_GPL(vhost_dev_init);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000422
423/* Caller should have device mutex */
424long vhost_dev_check_owner(struct vhost_dev *dev)
425{
426 /* Are you the owner? If not, I don't think you mean to do that */
427 return dev->mm == current->mm ? 0 : -EPERM;
428}
Asias He6ac1afb2013-05-06 16:38:21 +0800429EXPORT_SYMBOL_GPL(vhost_dev_check_owner);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000430
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300431struct vhost_attach_cgroups_struct {
Krishna Kumard47effe2011-03-01 17:06:37 +0530432 struct vhost_work work;
433 struct task_struct *owner;
434 int ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300435};
436
437static void vhost_attach_cgroups_work(struct vhost_work *work)
438{
Krishna Kumard47effe2011-03-01 17:06:37 +0530439 struct vhost_attach_cgroups_struct *s;
440
441 s = container_of(work, struct vhost_attach_cgroups_struct, work);
442 s->ret = cgroup_attach_task_all(s->owner, current);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300443}
444
445static int vhost_attach_cgroups(struct vhost_dev *dev)
446{
Krishna Kumard47effe2011-03-01 17:06:37 +0530447 struct vhost_attach_cgroups_struct attach;
448
449 attach.owner = current;
450 vhost_work_init(&attach.work, vhost_attach_cgroups_work);
451 vhost_work_queue(dev, &attach.work);
452 vhost_work_flush(dev, &attach.work);
453 return attach.ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300454}
455
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000456/* Caller should have device mutex */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300457bool vhost_dev_has_owner(struct vhost_dev *dev)
458{
459 return dev->mm;
460}
Asias He6ac1afb2013-05-06 16:38:21 +0800461EXPORT_SYMBOL_GPL(vhost_dev_has_owner);
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300462
463/* Caller should have device mutex */
Asias He54db63c2013-05-06 11:15:59 +0800464long vhost_dev_set_owner(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000465{
Tejun Heoc23f34452010-06-02 20:40:00 +0200466 struct task_struct *worker;
467 int err;
Krishna Kumard47effe2011-03-01 17:06:37 +0530468
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000469 /* Is there an owner already? */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300470 if (vhost_dev_has_owner(dev)) {
Tejun Heoc23f34452010-06-02 20:40:00 +0200471 err = -EBUSY;
472 goto err_mm;
473 }
Krishna Kumard47effe2011-03-01 17:06:37 +0530474
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000475 /* No owner, become one */
476 dev->mm = get_task_mm(current);
Tejun Heoc23f34452010-06-02 20:40:00 +0200477 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
478 if (IS_ERR(worker)) {
479 err = PTR_ERR(worker);
480 goto err_worker;
481 }
482
483 dev->worker = worker;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300484 wake_up_process(worker); /* avoid contributing to loadavg */
485
486 err = vhost_attach_cgroups(dev);
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300487 if (err)
488 goto err_cgroup;
Tejun Heoc23f34452010-06-02 20:40:00 +0200489
Jason Wange0e9b402010-09-14 23:53:05 +0800490 err = vhost_dev_alloc_iovecs(dev);
491 if (err)
492 goto err_cgroup;
493
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000494 return 0;
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300495err_cgroup:
496 kthread_stop(worker);
Michael S. Tsirkin615cc222010-09-02 14:16:36 +0300497 dev->worker = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200498err_worker:
499 if (dev->mm)
500 mmput(dev->mm);
501 dev->mm = NULL;
502err_mm:
503 return err;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000504}
Asias He6ac1afb2013-05-06 16:38:21 +0800505EXPORT_SYMBOL_GPL(vhost_dev_set_owner);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000506
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300507struct vhost_memory *vhost_dev_reset_owner_prepare(void)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000508{
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300509 return kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
510}
Asias He6ac1afb2013-05-06 16:38:21 +0800511EXPORT_SYMBOL_GPL(vhost_dev_reset_owner_prepare);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000512
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300513/* Caller should have device mutex */
514void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_memory *memory)
515{
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300516 int i;
517
Michael S. Tsirkinea5d4042011-11-27 19:05:58 +0200518 vhost_dev_cleanup(dev, true);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000519
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300520 /* Restore memory to default empty mapping. */
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000521 memory->nregions = 0;
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300522 dev->memory = memory;
523 /* We don't need VQ locks below since vhost_dev_cleanup makes sure
524 * VQs aren't running.
525 */
526 for (i = 0; i < dev->nvqs; ++i)
527 dev->vqs[i]->memory = memory;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000528}
Asias He6ac1afb2013-05-06 16:38:21 +0800529EXPORT_SYMBOL_GPL(vhost_dev_reset_owner);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000530
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000531void vhost_dev_stop(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000532{
533 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530534
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000535 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800536 if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) {
537 vhost_poll_stop(&dev->vqs[i]->poll);
538 vhost_poll_flush(&dev->vqs[i]->poll);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000539 }
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000540 }
541}
Asias He6ac1afb2013-05-06 16:38:21 +0800542EXPORT_SYMBOL_GPL(vhost_dev_stop);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000543
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000544/* Caller should have device mutex if and only if locked is set */
545void vhost_dev_cleanup(struct vhost_dev *dev, bool locked)
546{
547 int i;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000548
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000549 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800550 if (dev->vqs[i]->error_ctx)
551 eventfd_ctx_put(dev->vqs[i]->error_ctx);
552 if (dev->vqs[i]->error)
553 fput(dev->vqs[i]->error);
554 if (dev->vqs[i]->kick)
555 fput(dev->vqs[i]->kick);
556 if (dev->vqs[i]->call_ctx)
557 eventfd_ctx_put(dev->vqs[i]->call_ctx);
558 if (dev->vqs[i]->call)
559 fput(dev->vqs[i]->call);
560 vhost_vq_reset(dev, dev->vqs[i]);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000561 }
Jason Wange0e9b402010-09-14 23:53:05 +0800562 vhost_dev_free_iovecs(dev);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000563 if (dev->log_ctx)
564 eventfd_ctx_put(dev->log_ctx);
565 dev->log_ctx = NULL;
566 if (dev->log_file)
567 fput(dev->log_file);
568 dev->log_file = NULL;
569 /* No one will access memory at this point */
Igor Mammedov4de72552015-07-01 11:07:09 +0200570 kvfree(dev->memory);
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300571 dev->memory = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200572 WARN_ON(!list_empty(&dev->work_list));
Eric Dumazet78b620c2010-08-31 02:05:57 +0000573 if (dev->worker) {
574 kthread_stop(dev->worker);
575 dev->worker = NULL;
576 }
Michael S. Tsirkin533a19b2010-10-06 15:34:38 +0200577 if (dev->mm)
578 mmput(dev->mm);
579 dev->mm = NULL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000580}
Asias He6ac1afb2013-05-06 16:38:21 +0800581EXPORT_SYMBOL_GPL(vhost_dev_cleanup);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000582
583static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
584{
585 u64 a = addr / VHOST_PAGE_SIZE / 8;
Krishna Kumard47effe2011-03-01 17:06:37 +0530586
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000587 /* Make sure 64 bit math will not overflow. */
588 if (a > ULONG_MAX - (unsigned long)log_base ||
589 a + (unsigned long)log_base > ULONG_MAX)
Dan Carpenter6d97e552010-10-11 19:24:19 +0200590 return 0;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000591
592 return access_ok(VERIFY_WRITE, log_base + a,
593 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
594}
595
596/* Caller should have vq mutex and device mutex. */
597static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
598 int log_all)
599{
600 int i;
Jeff Dike179b2842010-04-07 09:59:10 -0400601
Michael S. Tsirkinf8322fb2010-05-27 12:28:03 +0300602 if (!mem)
603 return 0;
Jeff Dike179b2842010-04-07 09:59:10 -0400604
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000605 for (i = 0; i < mem->nregions; ++i) {
606 struct vhost_memory_region *m = mem->regions + i;
607 unsigned long a = m->userspace_addr;
608 if (m->memory_size > ULONG_MAX)
609 return 0;
610 else if (!access_ok(VERIFY_WRITE, (void __user *)a,
611 m->memory_size))
612 return 0;
613 else if (log_all && !log_access_ok(log_base,
614 m->guest_phys_addr,
615 m->memory_size))
616 return 0;
617 }
618 return 1;
619}
620
621/* Can we switch to this memory table? */
622/* Caller should have device mutex but not vq mutex */
623static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
624 int log_all)
625{
626 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530627
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000628 for (i = 0; i < d->nvqs; ++i) {
629 int ok;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300630 bool log;
631
Asias He3ab2e422013-04-27 11:16:48 +0800632 mutex_lock(&d->vqs[i]->mutex);
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300633 log = log_all || vhost_has_feature(d->vqs[i], VHOST_F_LOG_ALL);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000634 /* If ring is inactive, will check when it's enabled. */
Asias He3ab2e422013-04-27 11:16:48 +0800635 if (d->vqs[i]->private_data)
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300636 ok = vq_memory_access_ok(d->vqs[i]->log_base, mem, log);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000637 else
638 ok = 1;
Asias He3ab2e422013-04-27 11:16:48 +0800639 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000640 if (!ok)
641 return 0;
642 }
643 return 1;
644}
645
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300646static int vq_access_ok(struct vhost_virtqueue *vq, unsigned int num,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000647 struct vring_desc __user *desc,
648 struct vring_avail __user *avail,
649 struct vring_used __user *used)
650{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300651 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000652 return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
653 access_ok(VERIFY_READ, avail,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300654 sizeof *avail + num * sizeof *avail->ring + s) &&
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000655 access_ok(VERIFY_WRITE, used,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300656 sizeof *used + num * sizeof *used->ring + s);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000657}
658
659/* Can we log writes? */
660/* Caller should have device mutex but not vq mutex */
661int vhost_log_access_ok(struct vhost_dev *dev)
662{
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300663 return memory_access_ok(dev, dev->memory, 1);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000664}
Asias He6ac1afb2013-05-06 16:38:21 +0800665EXPORT_SYMBOL_GPL(vhost_log_access_ok);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000666
667/* Verify access for write logging. */
668/* Caller should have vq mutex and device mutex */
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300669static int vq_log_access_ok(struct vhost_virtqueue *vq,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300670 void __user *log_base)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000671{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300672 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100673
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300674 return vq_memory_access_ok(log_base, vq->memory,
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300675 vhost_has_feature(vq, VHOST_F_LOG_ALL)) &&
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000676 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
677 sizeof *vq->used +
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300678 vq->num * sizeof *vq->used->ring + s));
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000679}
680
681/* Can we start vq? */
682/* Caller should have vq mutex and device mutex */
683int vhost_vq_access_ok(struct vhost_virtqueue *vq)
684{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300685 return vq_access_ok(vq, vq->num, vq->desc, vq->avail, vq->used) &&
686 vq_log_access_ok(vq, vq->log_base);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000687}
Asias He6ac1afb2013-05-06 16:38:21 +0800688EXPORT_SYMBOL_GPL(vhost_vq_access_ok);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000689
Igor Mammedovbcfeaca2015-06-16 18:33:35 +0200690static int vhost_memory_reg_sort_cmp(const void *p1, const void *p2)
691{
692 const struct vhost_memory_region *r1 = p1, *r2 = p2;
693 if (r1->guest_phys_addr < r2->guest_phys_addr)
694 return 1;
695 if (r1->guest_phys_addr > r2->guest_phys_addr)
696 return -1;
697 return 0;
698}
699
Igor Mammedov4de72552015-07-01 11:07:09 +0200700static void *vhost_kvzalloc(unsigned long size)
701{
702 void *n = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
703
Igor Mammedov1e099472015-07-15 16:48:50 +0200704 if (!n)
Igor Mammedov4de72552015-07-01 11:07:09 +0200705 n = vzalloc(size);
Igor Mammedov4de72552015-07-01 11:07:09 +0200706 return n;
707}
708
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000709static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
710{
711 struct vhost_memory mem, *newmem, *oldmem;
712 unsigned long size = offsetof(struct vhost_memory, regions);
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +0300713 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530714
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900715 if (copy_from_user(&mem, m, size))
716 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000717 if (mem.padding)
718 return -EOPNOTSUPP;
Igor Mammedovc9ce42f2015-07-02 15:08:11 +0200719 if (mem.nregions > max_mem_regions)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000720 return -E2BIG;
Igor Mammedov4de72552015-07-01 11:07:09 +0200721 newmem = vhost_kvzalloc(size + mem.nregions * sizeof(*m->regions));
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000722 if (!newmem)
723 return -ENOMEM;
724
725 memcpy(newmem, &mem, size);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900726 if (copy_from_user(newmem->regions, m->regions,
727 mem.nregions * sizeof *m->regions)) {
Igor Mammedovbcfeaca2015-06-16 18:33:35 +0200728 kvfree(newmem);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900729 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000730 }
Igor Mammedovbcfeaca2015-06-16 18:33:35 +0200731 sort(newmem->regions, newmem->nregions, sizeof(*newmem->regions),
732 vhost_memory_reg_sort_cmp, NULL);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000733
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300734 if (!memory_access_ok(d, newmem, 0)) {
Igor Mammedov4de72552015-07-01 11:07:09 +0200735 kvfree(newmem);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000736 return -EFAULT;
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +0900737 }
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300738 oldmem = d->memory;
739 d->memory = newmem;
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +0300740
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300741 /* All memory accesses are done under some VQ mutex. */
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +0300742 for (i = 0; i < d->nvqs; ++i) {
743 mutex_lock(&d->vqs[i]->mutex);
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300744 d->vqs[i]->memory = newmem;
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +0300745 mutex_unlock(&d->vqs[i]->mutex);
746 }
Igor Mammedov4de72552015-07-01 11:07:09 +0200747 kvfree(oldmem);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000748 return 0;
749}
750
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200751long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000752{
Al Virocecb46f2012-08-27 14:21:39 -0400753 struct file *eventfp, *filep = NULL;
754 bool pollstart = false, pollstop = false;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000755 struct eventfd_ctx *ctx = NULL;
756 u32 __user *idxp = argp;
757 struct vhost_virtqueue *vq;
758 struct vhost_vring_state s;
759 struct vhost_vring_file f;
760 struct vhost_vring_addr a;
761 u32 idx;
762 long r;
763
764 r = get_user(idx, idxp);
765 if (r < 0)
766 return r;
Krishna Kumar0f3d9a12010-05-25 11:10:36 +0530767 if (idx >= d->nvqs)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000768 return -ENOBUFS;
769
Asias He3ab2e422013-04-27 11:16:48 +0800770 vq = d->vqs[idx];
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000771
772 mutex_lock(&vq->mutex);
773
774 switch (ioctl) {
775 case VHOST_SET_VRING_NUM:
776 /* Resizing ring with an active backend?
777 * You don't want to do that. */
778 if (vq->private_data) {
779 r = -EBUSY;
780 break;
781 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900782 if (copy_from_user(&s, argp, sizeof s)) {
783 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000784 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900785 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000786 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
787 r = -EINVAL;
788 break;
789 }
790 vq->num = s.num;
791 break;
792 case VHOST_SET_VRING_BASE:
793 /* Moving base with an active backend?
794 * You don't want to do that. */
795 if (vq->private_data) {
796 r = -EBUSY;
797 break;
798 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900799 if (copy_from_user(&s, argp, sizeof s)) {
800 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000801 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900802 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000803 if (s.num > 0xffff) {
804 r = -EINVAL;
805 break;
806 }
807 vq->last_avail_idx = s.num;
808 /* Forget the cached index value. */
809 vq->avail_idx = vq->last_avail_idx;
810 break;
811 case VHOST_GET_VRING_BASE:
812 s.index = idx;
813 s.num = vq->last_avail_idx;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900814 if (copy_to_user(argp, &s, sizeof s))
815 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000816 break;
817 case VHOST_SET_VRING_ADDR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900818 if (copy_from_user(&a, argp, sizeof a)) {
819 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000820 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900821 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000822 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
823 r = -EOPNOTSUPP;
824 break;
825 }
826 /* For 32bit, verify that the top 32bits of the user
827 data are set to zero. */
828 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
829 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
830 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
831 r = -EFAULT;
832 break;
833 }
Michael S. Tsirkin5d9a07b2014-12-21 01:00:23 +0200834
835 /* Make sure it's safe to cast pointers to vring types. */
836 BUILD_BUG_ON(__alignof__ *vq->avail > VRING_AVAIL_ALIGN_SIZE);
837 BUILD_BUG_ON(__alignof__ *vq->used > VRING_USED_ALIGN_SIZE);
838 if ((a.avail_user_addr & (VRING_AVAIL_ALIGN_SIZE - 1)) ||
839 (a.used_user_addr & (VRING_USED_ALIGN_SIZE - 1)) ||
Michael S. Tsirkind5424832015-11-16 16:57:08 +0200840 (a.log_guest_addr & (VRING_USED_ALIGN_SIZE - 1))) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000841 r = -EINVAL;
842 break;
843 }
844
845 /* We only verify access here if backend is configured.
846 * If it is not, we don't as size might not have been setup.
847 * We will verify when backend is configured. */
848 if (vq->private_data) {
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300849 if (!vq_access_ok(vq, vq->num,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000850 (void __user *)(unsigned long)a.desc_user_addr,
851 (void __user *)(unsigned long)a.avail_user_addr,
852 (void __user *)(unsigned long)a.used_user_addr)) {
853 r = -EINVAL;
854 break;
855 }
856
857 /* Also validate log access for used ring if enabled. */
858 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
859 !log_access_ok(vq->log_base, a.log_guest_addr,
860 sizeof *vq->used +
861 vq->num * sizeof *vq->used->ring)) {
862 r = -EINVAL;
863 break;
864 }
865 }
866
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000867 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
868 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
869 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
870 vq->log_addr = a.log_guest_addr;
871 vq->used = (void __user *)(unsigned long)a.used_user_addr;
872 break;
873 case VHOST_SET_VRING_KICK:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900874 if (copy_from_user(&f, argp, sizeof f)) {
875 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000876 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900877 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000878 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200879 if (IS_ERR(eventfp)) {
880 r = PTR_ERR(eventfp);
881 break;
882 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000883 if (eventfp != vq->kick) {
Al Virocecb46f2012-08-27 14:21:39 -0400884 pollstop = (filep = vq->kick) != NULL;
885 pollstart = (vq->kick = eventfp) != NULL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000886 } else
887 filep = eventfp;
888 break;
889 case VHOST_SET_VRING_CALL:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900890 if (copy_from_user(&f, argp, sizeof f)) {
891 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000892 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900893 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000894 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200895 if (IS_ERR(eventfp)) {
896 r = PTR_ERR(eventfp);
897 break;
898 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000899 if (eventfp != vq->call) {
900 filep = vq->call;
901 ctx = vq->call_ctx;
902 vq->call = eventfp;
903 vq->call_ctx = eventfp ?
904 eventfd_ctx_fileget(eventfp) : NULL;
905 } else
906 filep = eventfp;
907 break;
908 case VHOST_SET_VRING_ERR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900909 if (copy_from_user(&f, argp, sizeof f)) {
910 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000911 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900912 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000913 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200914 if (IS_ERR(eventfp)) {
915 r = PTR_ERR(eventfp);
916 break;
917 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000918 if (eventfp != vq->error) {
919 filep = vq->error;
920 vq->error = eventfp;
921 ctx = vq->error_ctx;
922 vq->error_ctx = eventfp ?
923 eventfd_ctx_fileget(eventfp) : NULL;
924 } else
925 filep = eventfp;
926 break;
Greg Kurz2751c982015-04-24 14:27:24 +0200927 case VHOST_SET_VRING_ENDIAN:
928 r = vhost_set_vring_endian(vq, argp);
929 break;
930 case VHOST_GET_VRING_ENDIAN:
931 r = vhost_get_vring_endian(vq, idx, argp);
932 break;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000933 default:
934 r = -ENOIOCTLCMD;
935 }
936
937 if (pollstop && vq->handle_kick)
938 vhost_poll_stop(&vq->poll);
939
940 if (ctx)
941 eventfd_ctx_put(ctx);
942 if (filep)
943 fput(filep);
944
945 if (pollstart && vq->handle_kick)
Jason Wang2b8b3282013-01-28 01:05:18 +0000946 r = vhost_poll_start(&vq->poll, vq->kick);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000947
948 mutex_unlock(&vq->mutex);
949
950 if (pollstop && vq->handle_kick)
951 vhost_poll_flush(&vq->poll);
952 return r;
953}
Asias He6ac1afb2013-05-06 16:38:21 +0800954EXPORT_SYMBOL_GPL(vhost_vring_ioctl);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000955
956/* Caller must have device mutex */
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200957long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000958{
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000959 struct file *eventfp, *filep = NULL;
960 struct eventfd_ctx *ctx = NULL;
961 u64 p;
962 long r;
963 int i, fd;
964
965 /* If you are not the owner, you can become one */
966 if (ioctl == VHOST_SET_OWNER) {
967 r = vhost_dev_set_owner(d);
968 goto done;
969 }
970
971 /* You must be the owner to do anything else */
972 r = vhost_dev_check_owner(d);
973 if (r)
974 goto done;
975
976 switch (ioctl) {
977 case VHOST_SET_MEM_TABLE:
978 r = vhost_set_memory(d, argp);
979 break;
980 case VHOST_SET_LOG_BASE:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900981 if (copy_from_user(&p, argp, sizeof p)) {
982 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000983 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900984 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000985 if ((u64)(unsigned long)p != p) {
986 r = -EFAULT;
987 break;
988 }
989 for (i = 0; i < d->nvqs; ++i) {
990 struct vhost_virtqueue *vq;
991 void __user *base = (void __user *)(unsigned long)p;
Asias He3ab2e422013-04-27 11:16:48 +0800992 vq = d->vqs[i];
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000993 mutex_lock(&vq->mutex);
994 /* If ring is inactive, will check when it's enabled. */
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300995 if (vq->private_data && !vq_log_access_ok(vq, base))
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000996 r = -EFAULT;
997 else
998 vq->log_base = base;
999 mutex_unlock(&vq->mutex);
1000 }
1001 break;
1002 case VHOST_SET_LOG_FD:
1003 r = get_user(fd, (int __user *)argp);
1004 if (r < 0)
1005 break;
1006 eventfp = fd == -1 ? NULL : eventfd_fget(fd);
1007 if (IS_ERR(eventfp)) {
1008 r = PTR_ERR(eventfp);
1009 break;
1010 }
1011 if (eventfp != d->log_file) {
1012 filep = d->log_file;
Marc-André Lureau7932c0b2015-07-17 15:32:03 +02001013 d->log_file = eventfp;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001014 ctx = d->log_ctx;
1015 d->log_ctx = eventfp ?
1016 eventfd_ctx_fileget(eventfp) : NULL;
1017 } else
1018 filep = eventfp;
1019 for (i = 0; i < d->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +08001020 mutex_lock(&d->vqs[i]->mutex);
1021 d->vqs[i]->log_ctx = d->log_ctx;
1022 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001023 }
1024 if (ctx)
1025 eventfd_ctx_put(ctx);
1026 if (filep)
1027 fput(filep);
1028 break;
1029 default:
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001030 r = -ENOIOCTLCMD;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001031 break;
1032 }
1033done:
1034 return r;
1035}
Asias He6ac1afb2013-05-06 16:38:21 +08001036EXPORT_SYMBOL_GPL(vhost_dev_ioctl);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001037
1038static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
1039 __u64 addr, __u32 len)
1040{
Igor Mammedovbcfeaca2015-06-16 18:33:35 +02001041 const struct vhost_memory_region *reg;
1042 int start = 0, end = mem->nregions;
Krishna Kumard47effe2011-03-01 17:06:37 +05301043
Igor Mammedovbcfeaca2015-06-16 18:33:35 +02001044 while (start < end) {
1045 int slot = start + (end - start) / 2;
1046 reg = mem->regions + slot;
1047 if (addr >= reg->guest_phys_addr)
1048 end = slot;
1049 else
1050 start = slot + 1;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001051 }
Igor Mammedovbcfeaca2015-06-16 18:33:35 +02001052
1053 reg = mem->regions + start;
1054 if (addr >= reg->guest_phys_addr &&
1055 reg->guest_phys_addr + reg->memory_size > addr)
1056 return reg;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001057 return NULL;
1058}
1059
1060/* TODO: This is really inefficient. We need something like get_user()
1061 * (instruction directly accesses the data, with an exception table entry
1062 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
1063 */
1064static int set_bit_to_user(int nr, void __user *addr)
1065{
1066 unsigned long log = (unsigned long)addr;
1067 struct page *page;
1068 void *base;
1069 int bit = nr + (log % PAGE_SIZE) * 8;
1070 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301071
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001072 r = get_user_pages_fast(log, 1, 1, &page);
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +02001073 if (r < 0)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001074 return r;
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +02001075 BUG_ON(r != 1);
Cong Wangc6daa7f2011-11-25 23:14:26 +08001076 base = kmap_atomic(page);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001077 set_bit(bit, base);
Cong Wangc6daa7f2011-11-25 23:14:26 +08001078 kunmap_atomic(base);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001079 set_page_dirty_lock(page);
1080 put_page(page);
1081 return 0;
1082}
1083
1084static int log_write(void __user *log_base,
1085 u64 write_address, u64 write_length)
1086{
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001087 u64 write_page = write_address / VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001088 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301089
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001090 if (!write_length)
1091 return 0;
Michael S. Tsirkin3bf9be42010-11-29 10:19:07 +02001092 write_length += write_address % VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001093 for (;;) {
1094 u64 base = (u64)(unsigned long)log_base;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001095 u64 log = base + write_page / 8;
1096 int bit = write_page % 8;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001097 if ((u64)(unsigned long)log != log)
1098 return -EFAULT;
1099 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
1100 if (r < 0)
1101 return r;
1102 if (write_length <= VHOST_PAGE_SIZE)
1103 break;
1104 write_length -= VHOST_PAGE_SIZE;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001105 write_page += 1;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001106 }
1107 return r;
1108}
1109
1110int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
1111 unsigned int log_num, u64 len)
1112{
1113 int i, r;
1114
1115 /* Make sure data written is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001116 smp_wmb();
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001117 for (i = 0; i < log_num; ++i) {
1118 u64 l = min(log[i].len, len);
1119 r = log_write(vq->log_base, log[i].addr, l);
1120 if (r < 0)
1121 return r;
1122 len -= l;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +02001123 if (!len) {
1124 if (vq->log_ctx)
1125 eventfd_signal(vq->log_ctx, 1);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001126 return 0;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +02001127 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001128 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001129 /* Length written exceeds what we have stored. This is a bug. */
1130 BUG();
1131 return 0;
1132}
Asias He6ac1afb2013-05-06 16:38:21 +08001133EXPORT_SYMBOL_GPL(vhost_log_write);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001134
Jason Wang2723fea2011-06-21 18:04:38 +08001135static int vhost_update_used_flags(struct vhost_virtqueue *vq)
1136{
1137 void __user *used;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001138 if (__put_user(cpu_to_vhost16(vq, vq->used_flags), &vq->used->flags) < 0)
Jason Wang2723fea2011-06-21 18:04:38 +08001139 return -EFAULT;
1140 if (unlikely(vq->log_used)) {
1141 /* Make sure the flag is seen before log. */
1142 smp_wmb();
1143 /* Log used flag write. */
1144 used = &vq->used->flags;
1145 log_write(vq->log_base, vq->log_addr +
1146 (used - (void __user *)vq->used),
1147 sizeof vq->used->flags);
1148 if (vq->log_ctx)
1149 eventfd_signal(vq->log_ctx, 1);
1150 }
1151 return 0;
1152}
1153
1154static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
1155{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001156 if (__put_user(cpu_to_vhost16(vq, vq->avail_idx), vhost_avail_event(vq)))
Jason Wang2723fea2011-06-21 18:04:38 +08001157 return -EFAULT;
1158 if (unlikely(vq->log_used)) {
1159 void __user *used;
1160 /* Make sure the event is seen before log. */
1161 smp_wmb();
1162 /* Log avail event write */
1163 used = vhost_avail_event(vq);
1164 log_write(vq->log_base, vq->log_addr +
1165 (used - (void __user *)vq->used),
1166 sizeof *vhost_avail_event(vq));
1167 if (vq->log_ctx)
1168 eventfd_signal(vq->log_ctx, 1);
1169 }
1170 return 0;
1171}
1172
Greg Kurz80f7d032016-02-16 15:59:44 +01001173int vhost_vq_init_access(struct vhost_virtqueue *vq)
Jason Wang2723fea2011-06-21 18:04:38 +08001174{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001175 __virtio16 last_used_idx;
Jason Wang2723fea2011-06-21 18:04:38 +08001176 int r;
Greg Kurze1f33be2016-02-16 15:54:28 +01001177 bool is_le = vq->is_le;
1178
Greg Kurz2751c982015-04-24 14:27:24 +02001179 if (!vq->private_data) {
Greg Kurzc5072032016-02-16 15:59:34 +01001180 vhost_reset_is_le(vq);
Jason Wang2723fea2011-06-21 18:04:38 +08001181 return 0;
Greg Kurz2751c982015-04-24 14:27:24 +02001182 }
1183
1184 vhost_init_is_le(vq);
Jason Wang2723fea2011-06-21 18:04:38 +08001185
1186 r = vhost_update_used_flags(vq);
1187 if (r)
Greg Kurze1f33be2016-02-16 15:54:28 +01001188 goto err;
Jason Wang2723fea2011-06-21 18:04:38 +08001189 vq->signalled_used_valid = false;
Greg Kurze1f33be2016-02-16 15:54:28 +01001190 if (!access_ok(VERIFY_READ, &vq->used->idx, sizeof vq->used->idx)) {
1191 r = -EFAULT;
1192 goto err;
1193 }
Michael S. Tsirkin64f7f052014-12-01 17:39:39 +02001194 r = __get_user(last_used_idx, &vq->used->idx);
1195 if (r)
Greg Kurze1f33be2016-02-16 15:54:28 +01001196 goto err;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001197 vq->last_used_idx = vhost16_to_cpu(vq, last_used_idx);
Michael S. Tsirkin64f7f052014-12-01 17:39:39 +02001198 return 0;
Greg Kurze1f33be2016-02-16 15:54:28 +01001199err:
1200 vq->is_le = is_le;
1201 return r;
Jason Wang2723fea2011-06-21 18:04:38 +08001202}
Greg Kurz80f7d032016-02-16 15:59:44 +01001203EXPORT_SYMBOL_GPL(vhost_vq_init_access);
Jason Wang2723fea2011-06-21 18:04:38 +08001204
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001205static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001206 struct iovec iov[], int iov_size)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001207{
1208 const struct vhost_memory_region *reg;
1209 struct vhost_memory *mem;
1210 struct iovec *_iov;
1211 u64 s = 0;
1212 int ret = 0;
1213
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001214 mem = vq->memory;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001215 while ((u64)len > s) {
1216 u64 size;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001217 if (unlikely(ret >= iov_size)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001218 ret = -ENOBUFS;
1219 break;
1220 }
1221 reg = find_region(mem, addr, len);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001222 if (unlikely(!reg)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001223 ret = -EFAULT;
1224 break;
1225 }
1226 _iov = iov + ret;
1227 size = reg->memory_size - addr + reg->guest_phys_addr;
Michael S. Tsirkinbd971202012-11-26 05:57:27 +00001228 _iov->iov_len = min((u64)len - s, size);
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001229 _iov->iov_base = (void __user *)(unsigned long)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001230 (reg->userspace_addr + addr - reg->guest_phys_addr);
1231 s += size;
1232 addr += size;
1233 ++ret;
1234 }
1235
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001236 return ret;
1237}
1238
1239/* Each buffer in the virtqueues is actually a chain of descriptors. This
1240 * function returns the next descriptor in the chain,
1241 * or -1U if we're at the end. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001242static unsigned next_desc(struct vhost_virtqueue *vq, struct vring_desc *desc)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001243{
1244 unsigned int next;
1245
1246 /* If this descriptor says it doesn't chain, we're done. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001247 if (!(desc->flags & cpu_to_vhost16(vq, VRING_DESC_F_NEXT)))
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001248 return -1U;
1249
1250 /* Check they're not leading us off end of descriptors. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001251 next = vhost16_to_cpu(vq, desc->next);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001252 /* Make sure compiler knows to grab that: we don't want it changing! */
1253 /* We will use the result as an index in an array, so most
1254 * architectures only need a compiler barrier here. */
1255 read_barrier_depends();
1256
1257 return next;
1258}
1259
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001260static int get_indirect(struct vhost_virtqueue *vq,
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001261 struct iovec iov[], unsigned int iov_size,
1262 unsigned int *out_num, unsigned int *in_num,
1263 struct vhost_log *log, unsigned int *log_num,
1264 struct vring_desc *indirect)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001265{
1266 struct vring_desc desc;
1267 unsigned int i = 0, count, found = 0;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001268 u32 len = vhost32_to_cpu(vq, indirect->len);
Al Viroaad9a1c2014-12-10 14:49:01 -05001269 struct iov_iter from;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001270 int ret;
1271
1272 /* Sanity check */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001273 if (unlikely(len % sizeof desc)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001274 vq_err(vq, "Invalid length in indirect descriptor: "
1275 "len 0x%llx not multiple of 0x%zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001276 (unsigned long long)len,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001277 sizeof desc);
1278 return -EINVAL;
1279 }
1280
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001281 ret = translate_desc(vq, vhost64_to_cpu(vq, indirect->addr), len, vq->indirect,
Jason Wange0e9b402010-09-14 23:53:05 +08001282 UIO_MAXIOV);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001283 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001284 vq_err(vq, "Translation failure %d in indirect.\n", ret);
1285 return ret;
1286 }
Al Viroaad9a1c2014-12-10 14:49:01 -05001287 iov_iter_init(&from, READ, vq->indirect, ret, len);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001288
1289 /* We will use the result as an address to read from, so most
1290 * architectures only need a compiler barrier here. */
1291 read_barrier_depends();
1292
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001293 count = len / sizeof desc;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001294 /* Buffers are chained via a 16 bit next field, so
1295 * we can have at most 2^16 of these. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001296 if (unlikely(count > USHRT_MAX + 1)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001297 vq_err(vq, "Indirect buffer length too big: %d\n",
1298 indirect->len);
1299 return -E2BIG;
1300 }
1301
1302 do {
1303 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001304 if (unlikely(++found > count)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001305 vq_err(vq, "Loop detected: last one at %u "
1306 "indirect size %u\n",
1307 i, count);
1308 return -EINVAL;
1309 }
Al Viroaad9a1c2014-12-10 14:49:01 -05001310 if (unlikely(copy_from_iter(&desc, sizeof(desc), &from) !=
1311 sizeof(desc))) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001312 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001313 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001314 return -EINVAL;
1315 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001316 if (unlikely(desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT))) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001317 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001318 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001319 return -EINVAL;
1320 }
1321
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001322 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
1323 vhost32_to_cpu(vq, desc.len), iov + iov_count,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001324 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001325 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001326 vq_err(vq, "Translation failure %d indirect idx %d\n",
1327 ret, i);
1328 return ret;
1329 }
1330 /* If this is an input descriptor, increment that count. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001331 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001332 *in_num += ret;
1333 if (unlikely(log)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001334 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
1335 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001336 ++*log_num;
1337 }
1338 } else {
1339 /* If it's an output descriptor, they're all supposed
1340 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001341 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001342 vq_err(vq, "Indirect descriptor "
1343 "has out after in: idx %d\n", i);
1344 return -EINVAL;
1345 }
1346 *out_num += ret;
1347 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001348 } while ((i = next_desc(vq, &desc)) != -1);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001349 return 0;
1350}
1351
1352/* This looks in the virtqueue and for the first available buffer, and converts
1353 * it to an iovec for convenient access. Since descriptors consist of some
1354 * number of output then some number of input descriptors, it's actually two
1355 * iovecs, but we pack them into one and note how many of each there were.
1356 *
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001357 * This function returns the descriptor number found, or vq->num (which is
1358 * never a valid descriptor number) if none was found. A negative code is
1359 * returned on error. */
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001360int vhost_get_vq_desc(struct vhost_virtqueue *vq,
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001361 struct iovec iov[], unsigned int iov_size,
1362 unsigned int *out_num, unsigned int *in_num,
1363 struct vhost_log *log, unsigned int *log_num)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001364{
1365 struct vring_desc desc;
1366 unsigned int i, head, found = 0;
1367 u16 last_avail_idx;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001368 __virtio16 avail_idx;
1369 __virtio16 ring_head;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001370 int ret;
1371
1372 /* Check it isn't doing very strange things with descriptor numbers. */
1373 last_avail_idx = vq->last_avail_idx;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001374 if (unlikely(__get_user(avail_idx, &vq->avail->idx))) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001375 vq_err(vq, "Failed to access avail idx at %p\n",
1376 &vq->avail->idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001377 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001378 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001379 vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001380
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001381 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001382 vq_err(vq, "Guest moved used index from %u to %u",
1383 last_avail_idx, vq->avail_idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001384 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001385 }
1386
1387 /* If there's nothing new since last we looked, return invalid. */
1388 if (vq->avail_idx == last_avail_idx)
1389 return vq->num;
1390
1391 /* Only get avail ring entries after they have been exposed by guest. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001392 smp_rmb();
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001393
1394 /* Grab the next descriptor number they're advertising, and increment
1395 * the index we've seen. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001396 if (unlikely(__get_user(ring_head,
Michael S. Tsirkin5fba13b2015-11-29 13:34:44 +02001397 &vq->avail->ring[last_avail_idx & (vq->num - 1)]))) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001398 vq_err(vq, "Failed to read head: idx %d address %p\n",
1399 last_avail_idx,
1400 &vq->avail->ring[last_avail_idx % vq->num]);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001401 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001402 }
1403
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001404 head = vhost16_to_cpu(vq, ring_head);
1405
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001406 /* If their number is silly, that's an error. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001407 if (unlikely(head >= vq->num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001408 vq_err(vq, "Guest says index %u > %u is available",
1409 head, vq->num);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001410 return -EINVAL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001411 }
1412
1413 /* When we start there are none of either input nor output. */
1414 *out_num = *in_num = 0;
1415 if (unlikely(log))
1416 *log_num = 0;
1417
1418 i = head;
1419 do {
1420 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001421 if (unlikely(i >= vq->num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001422 vq_err(vq, "Desc index is %u > %u, head = %u",
1423 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001424 return -EINVAL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001425 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001426 if (unlikely(++found > vq->num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001427 vq_err(vq, "Loop detected: last one at %u "
1428 "vq size %u head %u\n",
1429 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001430 return -EINVAL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001431 }
Michael S. Tsirkinfcc042a2011-03-06 13:33:49 +02001432 ret = __copy_from_user(&desc, vq->desc + i, sizeof desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001433 if (unlikely(ret)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001434 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
1435 i, vq->desc + i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001436 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001437 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001438 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT)) {
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001439 ret = get_indirect(vq, iov, iov_size,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001440 out_num, in_num,
1441 log, log_num, &desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001442 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001443 vq_err(vq, "Failure detected "
1444 "in indirect descriptor at idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001445 return ret;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001446 }
1447 continue;
1448 }
1449
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001450 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
1451 vhost32_to_cpu(vq, desc.len), iov + iov_count,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001452 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001453 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001454 vq_err(vq, "Translation failure %d descriptor idx %d\n",
1455 ret, i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001456 return ret;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001457 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001458 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001459 /* If this is an input descriptor,
1460 * increment that count. */
1461 *in_num += ret;
1462 if (unlikely(log)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001463 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
1464 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001465 ++*log_num;
1466 }
1467 } else {
1468 /* If it's an output descriptor, they're all supposed
1469 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001470 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001471 vq_err(vq, "Descriptor has out after in: "
1472 "idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001473 return -EINVAL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001474 }
1475 *out_num += ret;
1476 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001477 } while ((i = next_desc(vq, &desc)) != -1);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001478
1479 /* On success, increment avail index. */
1480 vq->last_avail_idx++;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001481
1482 /* Assume notifications from guest are disabled at this point,
1483 * if they aren't we would need to update avail_event index. */
1484 BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY));
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001485 return head;
1486}
Asias He6ac1afb2013-05-06 16:38:21 +08001487EXPORT_SYMBOL_GPL(vhost_get_vq_desc);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001488
1489/* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
David Stevens8dd014a2010-07-27 18:52:21 +03001490void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001491{
David Stevens8dd014a2010-07-27 18:52:21 +03001492 vq->last_avail_idx -= n;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001493}
Asias He6ac1afb2013-05-06 16:38:21 +08001494EXPORT_SYMBOL_GPL(vhost_discard_vq_desc);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001495
1496/* After we've used one of their buffers, we tell them about it. We'll then
1497 * want to notify the guest, using eventfd. */
1498int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
1499{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001500 struct vring_used_elem heads = {
1501 cpu_to_vhost32(vq, head),
1502 cpu_to_vhost32(vq, len)
1503 };
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001504
Jason Wangc49e4e52013-09-02 16:40:58 +08001505 return vhost_add_used_n(vq, &heads, 1);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001506}
Asias He6ac1afb2013-05-06 16:38:21 +08001507EXPORT_SYMBOL_GPL(vhost_add_used);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001508
David Stevens8dd014a2010-07-27 18:52:21 +03001509static int __vhost_add_used_n(struct vhost_virtqueue *vq,
1510 struct vring_used_elem *heads,
1511 unsigned count)
1512{
1513 struct vring_used_elem __user *used;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001514 u16 old, new;
David Stevens8dd014a2010-07-27 18:52:21 +03001515 int start;
1516
Michael S. Tsirkin5fba13b2015-11-29 13:34:44 +02001517 start = vq->last_used_idx & (vq->num - 1);
David Stevens8dd014a2010-07-27 18:52:21 +03001518 used = vq->used->ring + start;
Jason Wangc49e4e52013-09-02 16:40:58 +08001519 if (count == 1) {
1520 if (__put_user(heads[0].id, &used->id)) {
1521 vq_err(vq, "Failed to write used id");
1522 return -EFAULT;
1523 }
1524 if (__put_user(heads[0].len, &used->len)) {
1525 vq_err(vq, "Failed to write used len");
1526 return -EFAULT;
1527 }
1528 } else if (__copy_to_user(used, heads, count * sizeof *used)) {
David Stevens8dd014a2010-07-27 18:52:21 +03001529 vq_err(vq, "Failed to write used");
1530 return -EFAULT;
1531 }
1532 if (unlikely(vq->log_used)) {
1533 /* Make sure data is seen before log. */
1534 smp_wmb();
1535 /* Log used ring entry write. */
1536 log_write(vq->log_base,
1537 vq->log_addr +
1538 ((void __user *)used - (void __user *)vq->used),
1539 count * sizeof *used);
1540 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001541 old = vq->last_used_idx;
1542 new = (vq->last_used_idx += count);
1543 /* If the driver never bothers to signal in a very long while,
1544 * used index might wrap around. If that happens, invalidate
1545 * signalled_used index we stored. TODO: make sure driver
1546 * signals at least once in 2^16 and remove this. */
1547 if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old)))
1548 vq->signalled_used_valid = false;
David Stevens8dd014a2010-07-27 18:52:21 +03001549 return 0;
1550}
1551
1552/* After we've used one of their buffers, we tell them about it. We'll then
1553 * want to notify the guest, using eventfd. */
1554int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
1555 unsigned count)
1556{
1557 int start, n, r;
1558
Michael S. Tsirkin5fba13b2015-11-29 13:34:44 +02001559 start = vq->last_used_idx & (vq->num - 1);
David Stevens8dd014a2010-07-27 18:52:21 +03001560 n = vq->num - start;
1561 if (n < count) {
1562 r = __vhost_add_used_n(vq, heads, n);
1563 if (r < 0)
1564 return r;
1565 heads += n;
1566 count -= n;
1567 }
1568 r = __vhost_add_used_n(vq, heads, count);
1569
1570 /* Make sure buffer is written before we update index. */
1571 smp_wmb();
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001572 if (__put_user(cpu_to_vhost16(vq, vq->last_used_idx), &vq->used->idx)) {
David Stevens8dd014a2010-07-27 18:52:21 +03001573 vq_err(vq, "Failed to increment used idx");
1574 return -EFAULT;
1575 }
1576 if (unlikely(vq->log_used)) {
1577 /* Log used index update. */
1578 log_write(vq->log_base,
1579 vq->log_addr + offsetof(struct vring_used, idx),
1580 sizeof vq->used->idx);
1581 if (vq->log_ctx)
1582 eventfd_signal(vq->log_ctx, 1);
1583 }
1584 return r;
1585}
Asias He6ac1afb2013-05-06 16:38:21 +08001586EXPORT_SYMBOL_GPL(vhost_add_used_n);
David Stevens8dd014a2010-07-27 18:52:21 +03001587
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001588static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001589{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001590 __u16 old, new;
1591 __virtio16 event;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001592 bool v;
Michael S. Tsirkin0d499352010-05-11 19:44:17 +03001593 /* Flush out used index updates. This is paired
1594 * with the barrier that the Guest executes when enabling
1595 * interrupts. */
1596 smp_mb();
1597
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001598 if (vhost_has_feature(vq, VIRTIO_F_NOTIFY_ON_EMPTY) &&
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001599 unlikely(vq->avail_idx == vq->last_avail_idx))
1600 return true;
1601
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001602 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001603 __virtio16 flags;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001604 if (__get_user(flags, &vq->avail->flags)) {
1605 vq_err(vq, "Failed to get flags");
1606 return true;
1607 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001608 return !(flags & cpu_to_vhost16(vq, VRING_AVAIL_F_NO_INTERRUPT));
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001609 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001610 old = vq->signalled_used;
1611 v = vq->signalled_used_valid;
1612 new = vq->signalled_used = vq->last_used_idx;
1613 vq->signalled_used_valid = true;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001614
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001615 if (unlikely(!v))
1616 return true;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001617
Michael S. Tsirkin64f7f052014-12-01 17:39:39 +02001618 if (__get_user(event, vhost_used_event(vq))) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001619 vq_err(vq, "Failed to get used event idx");
1620 return true;
1621 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001622 return vring_need_event(vhost16_to_cpu(vq, event), new, old);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001623}
1624
1625/* This actually signals the guest, using eventfd. */
1626void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
1627{
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001628 /* Signal the Guest tell them we used something up. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001629 if (vq->call_ctx && vhost_notify(dev, vq))
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001630 eventfd_signal(vq->call_ctx, 1);
1631}
Asias He6ac1afb2013-05-06 16:38:21 +08001632EXPORT_SYMBOL_GPL(vhost_signal);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001633
1634/* And here's the combo meal deal. Supersize me! */
1635void vhost_add_used_and_signal(struct vhost_dev *dev,
1636 struct vhost_virtqueue *vq,
1637 unsigned int head, int len)
1638{
1639 vhost_add_used(vq, head, len);
1640 vhost_signal(dev, vq);
1641}
Asias He6ac1afb2013-05-06 16:38:21 +08001642EXPORT_SYMBOL_GPL(vhost_add_used_and_signal);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001643
David Stevens8dd014a2010-07-27 18:52:21 +03001644/* multi-buffer version of vhost_add_used_and_signal */
1645void vhost_add_used_and_signal_n(struct vhost_dev *dev,
1646 struct vhost_virtqueue *vq,
1647 struct vring_used_elem *heads, unsigned count)
1648{
1649 vhost_add_used_n(vq, heads, count);
1650 vhost_signal(dev, vq);
1651}
Asias He6ac1afb2013-05-06 16:38:21 +08001652EXPORT_SYMBOL_GPL(vhost_add_used_and_signal_n);
David Stevens8dd014a2010-07-27 18:52:21 +03001653
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001654/* OK, now we need to know about added descriptors. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001655bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001656{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001657 __virtio16 avail_idx;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001658 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301659
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001660 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
1661 return false;
1662 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001663 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08001664 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001665 if (r) {
1666 vq_err(vq, "Failed to enable notification at %p: %d\n",
1667 &vq->used->flags, r);
1668 return false;
1669 }
1670 } else {
Jason Wang2723fea2011-06-21 18:04:38 +08001671 r = vhost_update_avail_event(vq, vq->avail_idx);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001672 if (r) {
1673 vq_err(vq, "Failed to update avail event index at %p: %d\n",
1674 vhost_avail_event(vq), r);
1675 return false;
1676 }
1677 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001678 /* They could have slipped one in as we were doing that: make
1679 * sure it's written, then check again. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001680 smp_mb();
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001681 r = __get_user(avail_idx, &vq->avail->idx);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001682 if (r) {
1683 vq_err(vq, "Failed to check avail idx at %p: %d\n",
1684 &vq->avail->idx, r);
1685 return false;
1686 }
1687
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001688 return vhost16_to_cpu(vq, avail_idx) != vq->avail_idx;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001689}
Asias He6ac1afb2013-05-06 16:38:21 +08001690EXPORT_SYMBOL_GPL(vhost_enable_notify);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001691
1692/* We don't need to be notified again. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001693void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001694{
1695 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301696
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001697 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
1698 return;
1699 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001700 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08001701 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001702 if (r)
1703 vq_err(vq, "Failed to enable notification at %p: %d\n",
1704 &vq->used->flags, r);
1705 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001706}
Asias He6ac1afb2013-05-06 16:38:21 +08001707EXPORT_SYMBOL_GPL(vhost_disable_notify);
1708
1709static int __init vhost_init(void)
1710{
1711 return 0;
1712}
1713
1714static void __exit vhost_exit(void)
1715{
1716}
1717
1718module_init(vhost_init);
1719module_exit(vhost_exit);
1720
1721MODULE_VERSION("0.0.1");
1722MODULE_LICENSE("GPL v2");
1723MODULE_AUTHOR("Michael S. Tsirkin");
1724MODULE_DESCRIPTION("Host kernel accelerator for virtio");