blob: 9e8e004bb1c38d809c2af43b9d42c053db3a41a2 [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>
Tejun Heoc23f34452010-06-02 20:40:00 +020025#include <linux/kthread.h>
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +030026#include <linux/cgroup.h>
Asias He6ac1afb2013-05-06 16:38:21 +080027#include <linux/module.h>
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000028
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000029#include "vhost.h"
30
31enum {
32 VHOST_MEMORY_MAX_NREGIONS = 64,
33 VHOST_MEMORY_F_LOG = 0x1,
34};
35
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +030036#define vhost_used_event(vq) ((__virtio16 __user *)&vq->avail->ring[vq->num])
37#define vhost_avail_event(vq) ((__virtio16 __user *)&vq->used->ring[vq->num])
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +030038
Greg Kurz2751c982015-04-24 14:27:24 +020039#ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
40static void vhost_vq_reset_user_be(struct vhost_virtqueue *vq)
41{
42 vq->user_be = !virtio_legacy_is_little_endian();
43}
44
45static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
46{
47 struct vhost_vring_state s;
48
49 if (vq->private_data)
50 return -EBUSY;
51
52 if (copy_from_user(&s, argp, sizeof(s)))
53 return -EFAULT;
54
55 if (s.num != VHOST_VRING_LITTLE_ENDIAN &&
56 s.num != VHOST_VRING_BIG_ENDIAN)
57 return -EINVAL;
58
59 vq->user_be = s.num;
60
61 return 0;
62}
63
64static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
65 int __user *argp)
66{
67 struct vhost_vring_state s = {
68 .index = idx,
69 .num = vq->user_be
70 };
71
72 if (copy_to_user(argp, &s, sizeof(s)))
73 return -EFAULT;
74
75 return 0;
76}
77
78static void vhost_init_is_le(struct vhost_virtqueue *vq)
79{
80 /* Note for legacy virtio: user_be is initialized at reset time
81 * according to the host endianness. If userspace does not set an
82 * explicit endianness, the default behavior is native endian, as
83 * expected by legacy virtio.
84 */
85 vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) || !vq->user_be;
86}
87#else
88static void vhost_vq_reset_user_be(struct vhost_virtqueue *vq)
89{
90}
91
92static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
93{
94 return -ENOIOCTLCMD;
95}
96
97static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
98 int __user *argp)
99{
100 return -ENOIOCTLCMD;
101}
102
103static void vhost_init_is_le(struct vhost_virtqueue *vq)
104{
105 if (vhost_has_feature(vq, VIRTIO_F_VERSION_1))
106 vq->is_le = true;
107}
108#endif /* CONFIG_VHOST_CROSS_ENDIAN_LEGACY */
109
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000110static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
111 poll_table *pt)
112{
113 struct vhost_poll *poll;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000114
Krishna Kumard47effe2011-03-01 17:06:37 +0530115 poll = container_of(pt, struct vhost_poll, table);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000116 poll->wqh = wqh;
117 add_wait_queue(wqh, &poll->wait);
118}
119
120static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync,
121 void *key)
122{
Tejun Heoc23f34452010-06-02 20:40:00 +0200123 struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
124
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000125 if (!((unsigned long)key & poll->mask))
126 return 0;
127
Tejun Heoc23f34452010-06-02 20:40:00 +0200128 vhost_poll_queue(poll);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000129 return 0;
130}
131
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000132void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000133{
Tejun Heoc23f34452010-06-02 20:40:00 +0200134 INIT_LIST_HEAD(&work->node);
135 work->fn = fn;
136 init_waitqueue_head(&work->done);
137 work->flushing = 0;
138 work->queue_seq = work->done_seq = 0;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000139}
Asias He6ac1afb2013-05-06 16:38:21 +0800140EXPORT_SYMBOL_GPL(vhost_work_init);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000141
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300142/* Init poll structure */
143void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
144 unsigned long mask, struct vhost_dev *dev)
145{
146 init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
147 init_poll_funcptr(&poll->table, vhost_poll_func);
148 poll->mask = mask;
149 poll->dev = dev;
Jason Wang2b8b3282013-01-28 01:05:18 +0000150 poll->wqh = NULL;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300151
152 vhost_work_init(&poll->work, fn);
153}
Asias He6ac1afb2013-05-06 16:38:21 +0800154EXPORT_SYMBOL_GPL(vhost_poll_init);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300155
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000156/* Start polling a file. We add ourselves to file's wait queue. The caller must
157 * keep a reference to a file until after vhost_poll_stop is called. */
Jason Wang2b8b3282013-01-28 01:05:18 +0000158int vhost_poll_start(struct vhost_poll *poll, struct file *file)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000159{
160 unsigned long mask;
Jason Wang2b8b3282013-01-28 01:05:18 +0000161 int ret = 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530162
Jason Wang70181d512013-04-10 20:50:48 +0000163 if (poll->wqh)
164 return 0;
165
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000166 mask = file->f_op->poll(file, &poll->table);
167 if (mask)
168 vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
Jason Wang2b8b3282013-01-28 01:05:18 +0000169 if (mask & POLLERR) {
170 if (poll->wqh)
171 remove_wait_queue(poll->wqh, &poll->wait);
172 ret = -EINVAL;
173 }
174
175 return ret;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000176}
Asias He6ac1afb2013-05-06 16:38:21 +0800177EXPORT_SYMBOL_GPL(vhost_poll_start);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000178
179/* Stop polling a file. After this function returns, it becomes safe to drop the
180 * file reference. You must also flush afterwards. */
181void vhost_poll_stop(struct vhost_poll *poll)
182{
Jason Wang2b8b3282013-01-28 01:05:18 +0000183 if (poll->wqh) {
184 remove_wait_queue(poll->wqh, &poll->wait);
185 poll->wqh = NULL;
186 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000187}
Asias He6ac1afb2013-05-06 16:38:21 +0800188EXPORT_SYMBOL_GPL(vhost_poll_stop);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000189
Michael S. Tsirkin0174b0c2011-01-10 10:03:20 +0200190static bool vhost_work_seq_done(struct vhost_dev *dev, struct vhost_work *work,
191 unsigned seq)
192{
193 int left;
Krishna Kumard47effe2011-03-01 17:06:37 +0530194
Michael S. Tsirkin0174b0c2011-01-10 10:03:20 +0200195 spin_lock_irq(&dev->work_lock);
196 left = seq - work->done_seq;
197 spin_unlock_irq(&dev->work_lock);
198 return left <= 0;
199}
200
Asias He6ac1afb2013-05-06 16:38:21 +0800201void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000202{
Tejun Heoc23f34452010-06-02 20:40:00 +0200203 unsigned seq;
Tejun Heoc23f34452010-06-02 20:40:00 +0200204 int flushing;
205
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300206 spin_lock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200207 seq = work->queue_seq;
208 work->flushing++;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300209 spin_unlock_irq(&dev->work_lock);
Michael S. Tsirkin0174b0c2011-01-10 10:03:20 +0200210 wait_event(work->done, vhost_work_seq_done(dev, work, seq));
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300211 spin_lock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200212 flushing = --work->flushing;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300213 spin_unlock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200214 BUG_ON(flushing < 0);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000215}
Asias He6ac1afb2013-05-06 16:38:21 +0800216EXPORT_SYMBOL_GPL(vhost_work_flush);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000217
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300218/* Flush any work that has been scheduled. When calling this, don't hold any
219 * locks that are also used by the callback. */
220void vhost_poll_flush(struct vhost_poll *poll)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000221{
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300222 vhost_work_flush(poll->dev, &poll->work);
223}
Asias He6ac1afb2013-05-06 16:38:21 +0800224EXPORT_SYMBOL_GPL(vhost_poll_flush);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300225
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000226void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300227{
Tejun Heoc23f34452010-06-02 20:40:00 +0200228 unsigned long flags;
229
230 spin_lock_irqsave(&dev->work_lock, flags);
231 if (list_empty(&work->node)) {
232 list_add_tail(&work->node, &dev->work_list);
233 work->queue_seq++;
Qin Chuanyuac9fde22013-06-07 21:50:16 +0800234 spin_unlock_irqrestore(&dev->work_lock, flags);
Tejun Heoc23f34452010-06-02 20:40:00 +0200235 wake_up_process(dev->worker);
Qin Chuanyuac9fde22013-06-07 21:50:16 +0800236 } else {
237 spin_unlock_irqrestore(&dev->work_lock, flags);
Tejun Heoc23f34452010-06-02 20:40:00 +0200238 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000239}
Asias He6ac1afb2013-05-06 16:38:21 +0800240EXPORT_SYMBOL_GPL(vhost_work_queue);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000241
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300242void vhost_poll_queue(struct vhost_poll *poll)
243{
244 vhost_work_queue(poll->dev, &poll->work);
245}
Asias He6ac1afb2013-05-06 16:38:21 +0800246EXPORT_SYMBOL_GPL(vhost_poll_queue);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300247
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000248static void vhost_vq_reset(struct vhost_dev *dev,
249 struct vhost_virtqueue *vq)
250{
251 vq->num = 1;
252 vq->desc = NULL;
253 vq->avail = NULL;
254 vq->used = NULL;
255 vq->last_avail_idx = 0;
256 vq->avail_idx = 0;
257 vq->last_used_idx = 0;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300258 vq->signalled_used = 0;
259 vq->signalled_used_valid = false;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000260 vq->used_flags = 0;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000261 vq->log_used = false;
262 vq->log_addr = -1ull;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000263 vq->private_data = NULL;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300264 vq->acked_features = 0;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000265 vq->log_base = NULL;
266 vq->error_ctx = NULL;
267 vq->error = NULL;
268 vq->kick = NULL;
269 vq->call_ctx = NULL;
270 vq->call = NULL;
Michael S. Tsirkin73a99f02010-02-23 11:23:45 +0200271 vq->log_ctx = NULL;
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300272 vq->memory = NULL;
Greg Kurz2751c982015-04-24 14:27:24 +0200273 vq->is_le = virtio_legacy_is_little_endian();
274 vhost_vq_reset_user_be(vq);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000275}
276
Tejun Heoc23f34452010-06-02 20:40:00 +0200277static int vhost_worker(void *data)
278{
279 struct vhost_dev *dev = data;
280 struct vhost_work *work = NULL;
281 unsigned uninitialized_var(seq);
Jens Freimannd7ffde32012-06-26 00:59:58 +0000282 mm_segment_t oldfs = get_fs();
Tejun Heoc23f34452010-06-02 20:40:00 +0200283
Jens Freimannd7ffde32012-06-26 00:59:58 +0000284 set_fs(USER_DS);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200285 use_mm(dev->mm);
286
Tejun Heoc23f34452010-06-02 20:40:00 +0200287 for (;;) {
288 /* mb paired w/ kthread_stop */
289 set_current_state(TASK_INTERRUPTIBLE);
290
291 spin_lock_irq(&dev->work_lock);
292 if (work) {
293 work->done_seq = seq;
294 if (work->flushing)
295 wake_up_all(&work->done);
296 }
297
298 if (kthread_should_stop()) {
299 spin_unlock_irq(&dev->work_lock);
300 __set_current_state(TASK_RUNNING);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200301 break;
Tejun Heoc23f34452010-06-02 20:40:00 +0200302 }
303 if (!list_empty(&dev->work_list)) {
304 work = list_first_entry(&dev->work_list,
305 struct vhost_work, node);
306 list_del_init(&work->node);
307 seq = work->queue_seq;
308 } else
309 work = NULL;
310 spin_unlock_irq(&dev->work_lock);
311
312 if (work) {
313 __set_current_state(TASK_RUNNING);
314 work->fn(work);
Nadav Har'Eld550dda2012-02-27 15:07:29 +0200315 if (need_resched())
316 schedule();
Tejun Heoc23f34452010-06-02 20:40:00 +0200317 } else
318 schedule();
319
320 }
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200321 unuse_mm(dev->mm);
Jens Freimannd7ffde32012-06-26 00:59:58 +0000322 set_fs(oldfs);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200323 return 0;
Tejun Heoc23f34452010-06-02 20:40:00 +0200324}
325
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000326static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
327{
328 kfree(vq->indirect);
329 vq->indirect = NULL;
330 kfree(vq->log);
331 vq->log = NULL;
332 kfree(vq->heads);
333 vq->heads = NULL;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000334}
335
Jason Wange0e9b402010-09-14 23:53:05 +0800336/* Helper to allocate iovec buffers for all vqs. */
337static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
338{
Asias He6d5e6aa2013-05-06 16:38:23 +0800339 struct vhost_virtqueue *vq;
Jason Wange0e9b402010-09-14 23:53:05 +0800340 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530341
Jason Wange0e9b402010-09-14 23:53:05 +0800342 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800343 vq = dev->vqs[i];
344 vq->indirect = kmalloc(sizeof *vq->indirect * UIO_MAXIOV,
345 GFP_KERNEL);
346 vq->log = kmalloc(sizeof *vq->log * UIO_MAXIOV, GFP_KERNEL);
347 vq->heads = kmalloc(sizeof *vq->heads * UIO_MAXIOV, GFP_KERNEL);
348 if (!vq->indirect || !vq->log || !vq->heads)
Jason Wange0e9b402010-09-14 23:53:05 +0800349 goto err_nomem;
350 }
351 return 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530352
Jason Wange0e9b402010-09-14 23:53:05 +0800353err_nomem:
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000354 for (; i >= 0; --i)
Asias He3ab2e422013-04-27 11:16:48 +0800355 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800356 return -ENOMEM;
357}
358
359static void vhost_dev_free_iovecs(struct vhost_dev *dev)
360{
361 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530362
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000363 for (i = 0; i < dev->nvqs; ++i)
Asias He3ab2e422013-04-27 11:16:48 +0800364 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800365}
366
Zhi Yong Wu59566b6e2013-12-07 04:13:03 +0800367void vhost_dev_init(struct vhost_dev *dev,
Asias He3ab2e422013-04-27 11:16:48 +0800368 struct vhost_virtqueue **vqs, int nvqs)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000369{
Asias He6d5e6aa2013-05-06 16:38:23 +0800370 struct vhost_virtqueue *vq;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000371 int i;
Tejun Heoc23f34452010-06-02 20:40:00 +0200372
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000373 dev->vqs = vqs;
374 dev->nvqs = nvqs;
375 mutex_init(&dev->mutex);
376 dev->log_ctx = NULL;
377 dev->log_file = NULL;
378 dev->memory = NULL;
379 dev->mm = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200380 spin_lock_init(&dev->work_lock);
381 INIT_LIST_HEAD(&dev->work_list);
382 dev->worker = NULL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000383
384 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800385 vq = dev->vqs[i];
386 vq->log = NULL;
387 vq->indirect = NULL;
388 vq->heads = NULL;
389 vq->dev = dev;
390 mutex_init(&vq->mutex);
391 vhost_vq_reset(dev, vq);
392 if (vq->handle_kick)
393 vhost_poll_init(&vq->poll, vq->handle_kick,
394 POLLIN, dev);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000395 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000396}
Asias He6ac1afb2013-05-06 16:38:21 +0800397EXPORT_SYMBOL_GPL(vhost_dev_init);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000398
399/* Caller should have device mutex */
400long vhost_dev_check_owner(struct vhost_dev *dev)
401{
402 /* Are you the owner? If not, I don't think you mean to do that */
403 return dev->mm == current->mm ? 0 : -EPERM;
404}
Asias He6ac1afb2013-05-06 16:38:21 +0800405EXPORT_SYMBOL_GPL(vhost_dev_check_owner);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000406
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300407struct vhost_attach_cgroups_struct {
Krishna Kumard47effe2011-03-01 17:06:37 +0530408 struct vhost_work work;
409 struct task_struct *owner;
410 int ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300411};
412
413static void vhost_attach_cgroups_work(struct vhost_work *work)
414{
Krishna Kumard47effe2011-03-01 17:06:37 +0530415 struct vhost_attach_cgroups_struct *s;
416
417 s = container_of(work, struct vhost_attach_cgroups_struct, work);
418 s->ret = cgroup_attach_task_all(s->owner, current);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300419}
420
421static int vhost_attach_cgroups(struct vhost_dev *dev)
422{
Krishna Kumard47effe2011-03-01 17:06:37 +0530423 struct vhost_attach_cgroups_struct attach;
424
425 attach.owner = current;
426 vhost_work_init(&attach.work, vhost_attach_cgroups_work);
427 vhost_work_queue(dev, &attach.work);
428 vhost_work_flush(dev, &attach.work);
429 return attach.ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300430}
431
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000432/* Caller should have device mutex */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300433bool vhost_dev_has_owner(struct vhost_dev *dev)
434{
435 return dev->mm;
436}
Asias He6ac1afb2013-05-06 16:38:21 +0800437EXPORT_SYMBOL_GPL(vhost_dev_has_owner);
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300438
439/* Caller should have device mutex */
Asias He54db63c2013-05-06 11:15:59 +0800440long vhost_dev_set_owner(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000441{
Tejun Heoc23f34452010-06-02 20:40:00 +0200442 struct task_struct *worker;
443 int err;
Krishna Kumard47effe2011-03-01 17:06:37 +0530444
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000445 /* Is there an owner already? */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300446 if (vhost_dev_has_owner(dev)) {
Tejun Heoc23f34452010-06-02 20:40:00 +0200447 err = -EBUSY;
448 goto err_mm;
449 }
Krishna Kumard47effe2011-03-01 17:06:37 +0530450
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000451 /* No owner, become one */
452 dev->mm = get_task_mm(current);
Tejun Heoc23f34452010-06-02 20:40:00 +0200453 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
454 if (IS_ERR(worker)) {
455 err = PTR_ERR(worker);
456 goto err_worker;
457 }
458
459 dev->worker = worker;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300460 wake_up_process(worker); /* avoid contributing to loadavg */
461
462 err = vhost_attach_cgroups(dev);
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300463 if (err)
464 goto err_cgroup;
Tejun Heoc23f34452010-06-02 20:40:00 +0200465
Jason Wange0e9b402010-09-14 23:53:05 +0800466 err = vhost_dev_alloc_iovecs(dev);
467 if (err)
468 goto err_cgroup;
469
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000470 return 0;
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300471err_cgroup:
472 kthread_stop(worker);
Michael S. Tsirkin615cc222010-09-02 14:16:36 +0300473 dev->worker = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200474err_worker:
475 if (dev->mm)
476 mmput(dev->mm);
477 dev->mm = NULL;
478err_mm:
479 return err;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000480}
Asias He6ac1afb2013-05-06 16:38:21 +0800481EXPORT_SYMBOL_GPL(vhost_dev_set_owner);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000482
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300483struct vhost_memory *vhost_dev_reset_owner_prepare(void)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000484{
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300485 return kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
486}
Asias He6ac1afb2013-05-06 16:38:21 +0800487EXPORT_SYMBOL_GPL(vhost_dev_reset_owner_prepare);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000488
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300489/* Caller should have device mutex */
490void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_memory *memory)
491{
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300492 int i;
493
Michael S. Tsirkinea5d4042011-11-27 19:05:58 +0200494 vhost_dev_cleanup(dev, true);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000495
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300496 /* Restore memory to default empty mapping. */
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000497 memory->nregions = 0;
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300498 dev->memory = memory;
499 /* We don't need VQ locks below since vhost_dev_cleanup makes sure
500 * VQs aren't running.
501 */
502 for (i = 0; i < dev->nvqs; ++i)
503 dev->vqs[i]->memory = memory;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000504}
Asias He6ac1afb2013-05-06 16:38:21 +0800505EXPORT_SYMBOL_GPL(vhost_dev_reset_owner);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000506
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000507void vhost_dev_stop(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000508{
509 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530510
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000511 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800512 if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) {
513 vhost_poll_stop(&dev->vqs[i]->poll);
514 vhost_poll_flush(&dev->vqs[i]->poll);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000515 }
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000516 }
517}
Asias He6ac1afb2013-05-06 16:38:21 +0800518EXPORT_SYMBOL_GPL(vhost_dev_stop);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000519
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000520/* Caller should have device mutex if and only if locked is set */
521void vhost_dev_cleanup(struct vhost_dev *dev, bool locked)
522{
523 int i;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000524
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000525 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800526 if (dev->vqs[i]->error_ctx)
527 eventfd_ctx_put(dev->vqs[i]->error_ctx);
528 if (dev->vqs[i]->error)
529 fput(dev->vqs[i]->error);
530 if (dev->vqs[i]->kick)
531 fput(dev->vqs[i]->kick);
532 if (dev->vqs[i]->call_ctx)
533 eventfd_ctx_put(dev->vqs[i]->call_ctx);
534 if (dev->vqs[i]->call)
535 fput(dev->vqs[i]->call);
536 vhost_vq_reset(dev, dev->vqs[i]);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000537 }
Jason Wange0e9b402010-09-14 23:53:05 +0800538 vhost_dev_free_iovecs(dev);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000539 if (dev->log_ctx)
540 eventfd_ctx_put(dev->log_ctx);
541 dev->log_ctx = NULL;
542 if (dev->log_file)
543 fput(dev->log_file);
544 dev->log_file = NULL;
545 /* No one will access memory at this point */
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300546 kfree(dev->memory);
547 dev->memory = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200548 WARN_ON(!list_empty(&dev->work_list));
Eric Dumazet78b620c2010-08-31 02:05:57 +0000549 if (dev->worker) {
550 kthread_stop(dev->worker);
551 dev->worker = NULL;
552 }
Michael S. Tsirkin533a19b2010-10-06 15:34:38 +0200553 if (dev->mm)
554 mmput(dev->mm);
555 dev->mm = NULL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000556}
Asias He6ac1afb2013-05-06 16:38:21 +0800557EXPORT_SYMBOL_GPL(vhost_dev_cleanup);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000558
559static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
560{
561 u64 a = addr / VHOST_PAGE_SIZE / 8;
Krishna Kumard47effe2011-03-01 17:06:37 +0530562
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000563 /* Make sure 64 bit math will not overflow. */
564 if (a > ULONG_MAX - (unsigned long)log_base ||
565 a + (unsigned long)log_base > ULONG_MAX)
Dan Carpenter6d97e552010-10-11 19:24:19 +0200566 return 0;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000567
568 return access_ok(VERIFY_WRITE, log_base + a,
569 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
570}
571
572/* Caller should have vq mutex and device mutex. */
573static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
574 int log_all)
575{
576 int i;
Jeff Dike179b2842010-04-07 09:59:10 -0400577
Michael S. Tsirkinf8322fb2010-05-27 12:28:03 +0300578 if (!mem)
579 return 0;
Jeff Dike179b2842010-04-07 09:59:10 -0400580
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000581 for (i = 0; i < mem->nregions; ++i) {
582 struct vhost_memory_region *m = mem->regions + i;
583 unsigned long a = m->userspace_addr;
584 if (m->memory_size > ULONG_MAX)
585 return 0;
586 else if (!access_ok(VERIFY_WRITE, (void __user *)a,
587 m->memory_size))
588 return 0;
589 else if (log_all && !log_access_ok(log_base,
590 m->guest_phys_addr,
591 m->memory_size))
592 return 0;
593 }
594 return 1;
595}
596
597/* Can we switch to this memory table? */
598/* Caller should have device mutex but not vq mutex */
599static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
600 int log_all)
601{
602 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530603
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000604 for (i = 0; i < d->nvqs; ++i) {
605 int ok;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300606 bool log;
607
Asias He3ab2e422013-04-27 11:16:48 +0800608 mutex_lock(&d->vqs[i]->mutex);
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300609 log = log_all || vhost_has_feature(d->vqs[i], VHOST_F_LOG_ALL);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000610 /* If ring is inactive, will check when it's enabled. */
Asias He3ab2e422013-04-27 11:16:48 +0800611 if (d->vqs[i]->private_data)
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300612 ok = vq_memory_access_ok(d->vqs[i]->log_base, mem, log);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000613 else
614 ok = 1;
Asias He3ab2e422013-04-27 11:16:48 +0800615 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000616 if (!ok)
617 return 0;
618 }
619 return 1;
620}
621
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300622static int vq_access_ok(struct vhost_virtqueue *vq, unsigned int num,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000623 struct vring_desc __user *desc,
624 struct vring_avail __user *avail,
625 struct vring_used __user *used)
626{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300627 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000628 return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
629 access_ok(VERIFY_READ, avail,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300630 sizeof *avail + num * sizeof *avail->ring + s) &&
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000631 access_ok(VERIFY_WRITE, used,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300632 sizeof *used + num * sizeof *used->ring + s);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000633}
634
635/* Can we log writes? */
636/* Caller should have device mutex but not vq mutex */
637int vhost_log_access_ok(struct vhost_dev *dev)
638{
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300639 return memory_access_ok(dev, dev->memory, 1);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000640}
Asias He6ac1afb2013-05-06 16:38:21 +0800641EXPORT_SYMBOL_GPL(vhost_log_access_ok);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000642
643/* Verify access for write logging. */
644/* Caller should have vq mutex and device mutex */
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300645static int vq_log_access_ok(struct vhost_virtqueue *vq,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300646 void __user *log_base)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000647{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300648 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100649
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300650 return vq_memory_access_ok(log_base, vq->memory,
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300651 vhost_has_feature(vq, VHOST_F_LOG_ALL)) &&
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000652 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
653 sizeof *vq->used +
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300654 vq->num * sizeof *vq->used->ring + s));
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000655}
656
657/* Can we start vq? */
658/* Caller should have vq mutex and device mutex */
659int vhost_vq_access_ok(struct vhost_virtqueue *vq)
660{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300661 return vq_access_ok(vq, vq->num, vq->desc, vq->avail, vq->used) &&
662 vq_log_access_ok(vq, vq->log_base);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000663}
Asias He6ac1afb2013-05-06 16:38:21 +0800664EXPORT_SYMBOL_GPL(vhost_vq_access_ok);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000665
666static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
667{
668 struct vhost_memory mem, *newmem, *oldmem;
669 unsigned long size = offsetof(struct vhost_memory, regions);
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +0300670 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530671
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900672 if (copy_from_user(&mem, m, size))
673 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000674 if (mem.padding)
675 return -EOPNOTSUPP;
676 if (mem.nregions > VHOST_MEMORY_MAX_NREGIONS)
677 return -E2BIG;
678 newmem = kmalloc(size + mem.nregions * sizeof *m->regions, GFP_KERNEL);
679 if (!newmem)
680 return -ENOMEM;
681
682 memcpy(newmem, &mem, size);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900683 if (copy_from_user(newmem->regions, m->regions,
684 mem.nregions * sizeof *m->regions)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000685 kfree(newmem);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900686 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000687 }
688
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300689 if (!memory_access_ok(d, newmem, 0)) {
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +0900690 kfree(newmem);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000691 return -EFAULT;
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +0900692 }
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300693 oldmem = d->memory;
694 d->memory = newmem;
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +0300695
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300696 /* All memory accesses are done under some VQ mutex. */
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +0300697 for (i = 0; i < d->nvqs; ++i) {
698 mutex_lock(&d->vqs[i]->mutex);
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300699 d->vqs[i]->memory = newmem;
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +0300700 mutex_unlock(&d->vqs[i]->mutex);
701 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000702 kfree(oldmem);
703 return 0;
704}
705
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200706long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000707{
Al Virocecb46f2012-08-27 14:21:39 -0400708 struct file *eventfp, *filep = NULL;
709 bool pollstart = false, pollstop = false;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000710 struct eventfd_ctx *ctx = NULL;
711 u32 __user *idxp = argp;
712 struct vhost_virtqueue *vq;
713 struct vhost_vring_state s;
714 struct vhost_vring_file f;
715 struct vhost_vring_addr a;
716 u32 idx;
717 long r;
718
719 r = get_user(idx, idxp);
720 if (r < 0)
721 return r;
Krishna Kumar0f3d9a12010-05-25 11:10:36 +0530722 if (idx >= d->nvqs)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000723 return -ENOBUFS;
724
Asias He3ab2e422013-04-27 11:16:48 +0800725 vq = d->vqs[idx];
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000726
727 mutex_lock(&vq->mutex);
728
729 switch (ioctl) {
730 case VHOST_SET_VRING_NUM:
731 /* Resizing ring with an active backend?
732 * You don't want to do that. */
733 if (vq->private_data) {
734 r = -EBUSY;
735 break;
736 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900737 if (copy_from_user(&s, argp, sizeof s)) {
738 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000739 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900740 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000741 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
742 r = -EINVAL;
743 break;
744 }
745 vq->num = s.num;
746 break;
747 case VHOST_SET_VRING_BASE:
748 /* Moving base with an active backend?
749 * You don't want to do that. */
750 if (vq->private_data) {
751 r = -EBUSY;
752 break;
753 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900754 if (copy_from_user(&s, argp, sizeof s)) {
755 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000756 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900757 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000758 if (s.num > 0xffff) {
759 r = -EINVAL;
760 break;
761 }
762 vq->last_avail_idx = s.num;
763 /* Forget the cached index value. */
764 vq->avail_idx = vq->last_avail_idx;
765 break;
766 case VHOST_GET_VRING_BASE:
767 s.index = idx;
768 s.num = vq->last_avail_idx;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900769 if (copy_to_user(argp, &s, sizeof s))
770 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000771 break;
772 case VHOST_SET_VRING_ADDR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900773 if (copy_from_user(&a, argp, sizeof a)) {
774 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000775 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900776 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000777 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
778 r = -EOPNOTSUPP;
779 break;
780 }
781 /* For 32bit, verify that the top 32bits of the user
782 data are set to zero. */
783 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
784 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
785 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
786 r = -EFAULT;
787 break;
788 }
Michael S. Tsirkin5d9a07b2014-12-21 01:00:23 +0200789
790 /* Make sure it's safe to cast pointers to vring types. */
791 BUILD_BUG_ON(__alignof__ *vq->avail > VRING_AVAIL_ALIGN_SIZE);
792 BUILD_BUG_ON(__alignof__ *vq->used > VRING_USED_ALIGN_SIZE);
793 if ((a.avail_user_addr & (VRING_AVAIL_ALIGN_SIZE - 1)) ||
794 (a.used_user_addr & (VRING_USED_ALIGN_SIZE - 1)) ||
795 (a.log_guest_addr & (sizeof(u64) - 1))) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000796 r = -EINVAL;
797 break;
798 }
799
800 /* We only verify access here if backend is configured.
801 * If it is not, we don't as size might not have been setup.
802 * We will verify when backend is configured. */
803 if (vq->private_data) {
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300804 if (!vq_access_ok(vq, vq->num,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000805 (void __user *)(unsigned long)a.desc_user_addr,
806 (void __user *)(unsigned long)a.avail_user_addr,
807 (void __user *)(unsigned long)a.used_user_addr)) {
808 r = -EINVAL;
809 break;
810 }
811
812 /* Also validate log access for used ring if enabled. */
813 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
814 !log_access_ok(vq->log_base, a.log_guest_addr,
815 sizeof *vq->used +
816 vq->num * sizeof *vq->used->ring)) {
817 r = -EINVAL;
818 break;
819 }
820 }
821
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000822 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
823 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
824 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
825 vq->log_addr = a.log_guest_addr;
826 vq->used = (void __user *)(unsigned long)a.used_user_addr;
827 break;
828 case VHOST_SET_VRING_KICK:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900829 if (copy_from_user(&f, argp, sizeof f)) {
830 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000831 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900832 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000833 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200834 if (IS_ERR(eventfp)) {
835 r = PTR_ERR(eventfp);
836 break;
837 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000838 if (eventfp != vq->kick) {
Al Virocecb46f2012-08-27 14:21:39 -0400839 pollstop = (filep = vq->kick) != NULL;
840 pollstart = (vq->kick = eventfp) != NULL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000841 } else
842 filep = eventfp;
843 break;
844 case VHOST_SET_VRING_CALL:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900845 if (copy_from_user(&f, argp, sizeof f)) {
846 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000847 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900848 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000849 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200850 if (IS_ERR(eventfp)) {
851 r = PTR_ERR(eventfp);
852 break;
853 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000854 if (eventfp != vq->call) {
855 filep = vq->call;
856 ctx = vq->call_ctx;
857 vq->call = eventfp;
858 vq->call_ctx = eventfp ?
859 eventfd_ctx_fileget(eventfp) : NULL;
860 } else
861 filep = eventfp;
862 break;
863 case VHOST_SET_VRING_ERR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900864 if (copy_from_user(&f, argp, sizeof f)) {
865 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000866 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900867 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000868 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200869 if (IS_ERR(eventfp)) {
870 r = PTR_ERR(eventfp);
871 break;
872 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000873 if (eventfp != vq->error) {
874 filep = vq->error;
875 vq->error = eventfp;
876 ctx = vq->error_ctx;
877 vq->error_ctx = eventfp ?
878 eventfd_ctx_fileget(eventfp) : NULL;
879 } else
880 filep = eventfp;
881 break;
Greg Kurz2751c982015-04-24 14:27:24 +0200882 case VHOST_SET_VRING_ENDIAN:
883 r = vhost_set_vring_endian(vq, argp);
884 break;
885 case VHOST_GET_VRING_ENDIAN:
886 r = vhost_get_vring_endian(vq, idx, argp);
887 break;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000888 default:
889 r = -ENOIOCTLCMD;
890 }
891
892 if (pollstop && vq->handle_kick)
893 vhost_poll_stop(&vq->poll);
894
895 if (ctx)
896 eventfd_ctx_put(ctx);
897 if (filep)
898 fput(filep);
899
900 if (pollstart && vq->handle_kick)
Jason Wang2b8b3282013-01-28 01:05:18 +0000901 r = vhost_poll_start(&vq->poll, vq->kick);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000902
903 mutex_unlock(&vq->mutex);
904
905 if (pollstop && vq->handle_kick)
906 vhost_poll_flush(&vq->poll);
907 return r;
908}
Asias He6ac1afb2013-05-06 16:38:21 +0800909EXPORT_SYMBOL_GPL(vhost_vring_ioctl);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000910
911/* Caller must have device mutex */
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200912long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000913{
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000914 struct file *eventfp, *filep = NULL;
915 struct eventfd_ctx *ctx = NULL;
916 u64 p;
917 long r;
918 int i, fd;
919
920 /* If you are not the owner, you can become one */
921 if (ioctl == VHOST_SET_OWNER) {
922 r = vhost_dev_set_owner(d);
923 goto done;
924 }
925
926 /* You must be the owner to do anything else */
927 r = vhost_dev_check_owner(d);
928 if (r)
929 goto done;
930
931 switch (ioctl) {
932 case VHOST_SET_MEM_TABLE:
933 r = vhost_set_memory(d, argp);
934 break;
935 case VHOST_SET_LOG_BASE:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900936 if (copy_from_user(&p, argp, sizeof p)) {
937 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000938 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900939 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000940 if ((u64)(unsigned long)p != p) {
941 r = -EFAULT;
942 break;
943 }
944 for (i = 0; i < d->nvqs; ++i) {
945 struct vhost_virtqueue *vq;
946 void __user *base = (void __user *)(unsigned long)p;
Asias He3ab2e422013-04-27 11:16:48 +0800947 vq = d->vqs[i];
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000948 mutex_lock(&vq->mutex);
949 /* If ring is inactive, will check when it's enabled. */
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300950 if (vq->private_data && !vq_log_access_ok(vq, base))
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000951 r = -EFAULT;
952 else
953 vq->log_base = base;
954 mutex_unlock(&vq->mutex);
955 }
956 break;
957 case VHOST_SET_LOG_FD:
958 r = get_user(fd, (int __user *)argp);
959 if (r < 0)
960 break;
961 eventfp = fd == -1 ? NULL : eventfd_fget(fd);
962 if (IS_ERR(eventfp)) {
963 r = PTR_ERR(eventfp);
964 break;
965 }
966 if (eventfp != d->log_file) {
967 filep = d->log_file;
968 ctx = d->log_ctx;
969 d->log_ctx = eventfp ?
970 eventfd_ctx_fileget(eventfp) : NULL;
971 } else
972 filep = eventfp;
973 for (i = 0; i < d->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800974 mutex_lock(&d->vqs[i]->mutex);
975 d->vqs[i]->log_ctx = d->log_ctx;
976 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000977 }
978 if (ctx)
979 eventfd_ctx_put(ctx);
980 if (filep)
981 fput(filep);
982 break;
983 default:
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200984 r = -ENOIOCTLCMD;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000985 break;
986 }
987done:
988 return r;
989}
Asias He6ac1afb2013-05-06 16:38:21 +0800990EXPORT_SYMBOL_GPL(vhost_dev_ioctl);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000991
992static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
993 __u64 addr, __u32 len)
994{
995 struct vhost_memory_region *reg;
996 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530997
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000998 /* linear search is not brilliant, but we really have on the order of 6
999 * regions in practice */
1000 for (i = 0; i < mem->nregions; ++i) {
1001 reg = mem->regions + i;
1002 if (reg->guest_phys_addr <= addr &&
1003 reg->guest_phys_addr + reg->memory_size - 1 >= addr)
1004 return reg;
1005 }
1006 return NULL;
1007}
1008
1009/* TODO: This is really inefficient. We need something like get_user()
1010 * (instruction directly accesses the data, with an exception table entry
1011 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
1012 */
1013static int set_bit_to_user(int nr, void __user *addr)
1014{
1015 unsigned long log = (unsigned long)addr;
1016 struct page *page;
1017 void *base;
1018 int bit = nr + (log % PAGE_SIZE) * 8;
1019 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301020
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001021 r = get_user_pages_fast(log, 1, 1, &page);
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +02001022 if (r < 0)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001023 return r;
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +02001024 BUG_ON(r != 1);
Cong Wangc6daa7f2011-11-25 23:14:26 +08001025 base = kmap_atomic(page);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001026 set_bit(bit, base);
Cong Wangc6daa7f2011-11-25 23:14:26 +08001027 kunmap_atomic(base);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001028 set_page_dirty_lock(page);
1029 put_page(page);
1030 return 0;
1031}
1032
1033static int log_write(void __user *log_base,
1034 u64 write_address, u64 write_length)
1035{
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001036 u64 write_page = write_address / VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001037 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301038
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001039 if (!write_length)
1040 return 0;
Michael S. Tsirkin3bf9be42010-11-29 10:19:07 +02001041 write_length += write_address % VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001042 for (;;) {
1043 u64 base = (u64)(unsigned long)log_base;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001044 u64 log = base + write_page / 8;
1045 int bit = write_page % 8;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001046 if ((u64)(unsigned long)log != log)
1047 return -EFAULT;
1048 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
1049 if (r < 0)
1050 return r;
1051 if (write_length <= VHOST_PAGE_SIZE)
1052 break;
1053 write_length -= VHOST_PAGE_SIZE;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001054 write_page += 1;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001055 }
1056 return r;
1057}
1058
1059int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
1060 unsigned int log_num, u64 len)
1061{
1062 int i, r;
1063
1064 /* Make sure data written is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001065 smp_wmb();
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001066 for (i = 0; i < log_num; ++i) {
1067 u64 l = min(log[i].len, len);
1068 r = log_write(vq->log_base, log[i].addr, l);
1069 if (r < 0)
1070 return r;
1071 len -= l;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +02001072 if (!len) {
1073 if (vq->log_ctx)
1074 eventfd_signal(vq->log_ctx, 1);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001075 return 0;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +02001076 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001077 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001078 /* Length written exceeds what we have stored. This is a bug. */
1079 BUG();
1080 return 0;
1081}
Asias He6ac1afb2013-05-06 16:38:21 +08001082EXPORT_SYMBOL_GPL(vhost_log_write);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001083
Jason Wang2723fea2011-06-21 18:04:38 +08001084static int vhost_update_used_flags(struct vhost_virtqueue *vq)
1085{
1086 void __user *used;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001087 if (__put_user(cpu_to_vhost16(vq, vq->used_flags), &vq->used->flags) < 0)
Jason Wang2723fea2011-06-21 18:04:38 +08001088 return -EFAULT;
1089 if (unlikely(vq->log_used)) {
1090 /* Make sure the flag is seen before log. */
1091 smp_wmb();
1092 /* Log used flag write. */
1093 used = &vq->used->flags;
1094 log_write(vq->log_base, vq->log_addr +
1095 (used - (void __user *)vq->used),
1096 sizeof vq->used->flags);
1097 if (vq->log_ctx)
1098 eventfd_signal(vq->log_ctx, 1);
1099 }
1100 return 0;
1101}
1102
1103static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
1104{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001105 if (__put_user(cpu_to_vhost16(vq, vq->avail_idx), vhost_avail_event(vq)))
Jason Wang2723fea2011-06-21 18:04:38 +08001106 return -EFAULT;
1107 if (unlikely(vq->log_used)) {
1108 void __user *used;
1109 /* Make sure the event is seen before log. */
1110 smp_wmb();
1111 /* Log avail event write */
1112 used = vhost_avail_event(vq);
1113 log_write(vq->log_base, vq->log_addr +
1114 (used - (void __user *)vq->used),
1115 sizeof *vhost_avail_event(vq));
1116 if (vq->log_ctx)
1117 eventfd_signal(vq->log_ctx, 1);
1118 }
1119 return 0;
1120}
1121
1122int vhost_init_used(struct vhost_virtqueue *vq)
1123{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001124 __virtio16 last_used_idx;
Jason Wang2723fea2011-06-21 18:04:38 +08001125 int r;
Greg Kurz2751c982015-04-24 14:27:24 +02001126 if (!vq->private_data) {
1127 vq->is_le = virtio_legacy_is_little_endian();
Jason Wang2723fea2011-06-21 18:04:38 +08001128 return 0;
Greg Kurz2751c982015-04-24 14:27:24 +02001129 }
1130
1131 vhost_init_is_le(vq);
Jason Wang2723fea2011-06-21 18:04:38 +08001132
1133 r = vhost_update_used_flags(vq);
1134 if (r)
1135 return r;
1136 vq->signalled_used_valid = false;
Michael S. Tsirkin64f7f052014-12-01 17:39:39 +02001137 if (!access_ok(VERIFY_READ, &vq->used->idx, sizeof vq->used->idx))
1138 return -EFAULT;
1139 r = __get_user(last_used_idx, &vq->used->idx);
1140 if (r)
1141 return r;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001142 vq->last_used_idx = vhost16_to_cpu(vq, last_used_idx);
Michael S. Tsirkin64f7f052014-12-01 17:39:39 +02001143 return 0;
Jason Wang2723fea2011-06-21 18:04:38 +08001144}
Asias He6ac1afb2013-05-06 16:38:21 +08001145EXPORT_SYMBOL_GPL(vhost_init_used);
Jason Wang2723fea2011-06-21 18:04:38 +08001146
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001147static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001148 struct iovec iov[], int iov_size)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001149{
1150 const struct vhost_memory_region *reg;
1151 struct vhost_memory *mem;
1152 struct iovec *_iov;
1153 u64 s = 0;
1154 int ret = 0;
1155
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001156 mem = vq->memory;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001157 while ((u64)len > s) {
1158 u64 size;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001159 if (unlikely(ret >= iov_size)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001160 ret = -ENOBUFS;
1161 break;
1162 }
1163 reg = find_region(mem, addr, len);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001164 if (unlikely(!reg)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001165 ret = -EFAULT;
1166 break;
1167 }
1168 _iov = iov + ret;
1169 size = reg->memory_size - addr + reg->guest_phys_addr;
Michael S. Tsirkinbd971202012-11-26 05:57:27 +00001170 _iov->iov_len = min((u64)len - s, size);
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001171 _iov->iov_base = (void __user *)(unsigned long)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001172 (reg->userspace_addr + addr - reg->guest_phys_addr);
1173 s += size;
1174 addr += size;
1175 ++ret;
1176 }
1177
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001178 return ret;
1179}
1180
1181/* Each buffer in the virtqueues is actually a chain of descriptors. This
1182 * function returns the next descriptor in the chain,
1183 * or -1U if we're at the end. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001184static unsigned next_desc(struct vhost_virtqueue *vq, struct vring_desc *desc)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001185{
1186 unsigned int next;
1187
1188 /* If this descriptor says it doesn't chain, we're done. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001189 if (!(desc->flags & cpu_to_vhost16(vq, VRING_DESC_F_NEXT)))
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001190 return -1U;
1191
1192 /* Check they're not leading us off end of descriptors. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001193 next = vhost16_to_cpu(vq, desc->next);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001194 /* Make sure compiler knows to grab that: we don't want it changing! */
1195 /* We will use the result as an index in an array, so most
1196 * architectures only need a compiler barrier here. */
1197 read_barrier_depends();
1198
1199 return next;
1200}
1201
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001202static int get_indirect(struct vhost_virtqueue *vq,
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001203 struct iovec iov[], unsigned int iov_size,
1204 unsigned int *out_num, unsigned int *in_num,
1205 struct vhost_log *log, unsigned int *log_num,
1206 struct vring_desc *indirect)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001207{
1208 struct vring_desc desc;
1209 unsigned int i = 0, count, found = 0;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001210 u32 len = vhost32_to_cpu(vq, indirect->len);
Al Viroaad9a1c2014-12-10 14:49:01 -05001211 struct iov_iter from;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001212 int ret;
1213
1214 /* Sanity check */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001215 if (unlikely(len % sizeof desc)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001216 vq_err(vq, "Invalid length in indirect descriptor: "
1217 "len 0x%llx not multiple of 0x%zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001218 (unsigned long long)len,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001219 sizeof desc);
1220 return -EINVAL;
1221 }
1222
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001223 ret = translate_desc(vq, vhost64_to_cpu(vq, indirect->addr), len, vq->indirect,
Jason Wange0e9b402010-09-14 23:53:05 +08001224 UIO_MAXIOV);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001225 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001226 vq_err(vq, "Translation failure %d in indirect.\n", ret);
1227 return ret;
1228 }
Al Viroaad9a1c2014-12-10 14:49:01 -05001229 iov_iter_init(&from, READ, vq->indirect, ret, len);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001230
1231 /* We will use the result as an address to read from, so most
1232 * architectures only need a compiler barrier here. */
1233 read_barrier_depends();
1234
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001235 count = len / sizeof desc;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001236 /* Buffers are chained via a 16 bit next field, so
1237 * we can have at most 2^16 of these. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001238 if (unlikely(count > USHRT_MAX + 1)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001239 vq_err(vq, "Indirect buffer length too big: %d\n",
1240 indirect->len);
1241 return -E2BIG;
1242 }
1243
1244 do {
1245 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001246 if (unlikely(++found > count)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001247 vq_err(vq, "Loop detected: last one at %u "
1248 "indirect size %u\n",
1249 i, count);
1250 return -EINVAL;
1251 }
Al Viroaad9a1c2014-12-10 14:49:01 -05001252 if (unlikely(copy_from_iter(&desc, sizeof(desc), &from) !=
1253 sizeof(desc))) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001254 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001255 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001256 return -EINVAL;
1257 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001258 if (unlikely(desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT))) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001259 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001260 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001261 return -EINVAL;
1262 }
1263
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001264 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
1265 vhost32_to_cpu(vq, desc.len), iov + iov_count,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001266 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001267 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001268 vq_err(vq, "Translation failure %d indirect idx %d\n",
1269 ret, i);
1270 return ret;
1271 }
1272 /* If this is an input descriptor, increment that count. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001273 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001274 *in_num += ret;
1275 if (unlikely(log)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001276 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
1277 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001278 ++*log_num;
1279 }
1280 } else {
1281 /* If it's an output descriptor, they're all supposed
1282 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001283 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001284 vq_err(vq, "Indirect descriptor "
1285 "has out after in: idx %d\n", i);
1286 return -EINVAL;
1287 }
1288 *out_num += ret;
1289 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001290 } while ((i = next_desc(vq, &desc)) != -1);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001291 return 0;
1292}
1293
1294/* This looks in the virtqueue and for the first available buffer, and converts
1295 * it to an iovec for convenient access. Since descriptors consist of some
1296 * number of output then some number of input descriptors, it's actually two
1297 * iovecs, but we pack them into one and note how many of each there were.
1298 *
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001299 * This function returns the descriptor number found, or vq->num (which is
1300 * never a valid descriptor number) if none was found. A negative code is
1301 * returned on error. */
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001302int vhost_get_vq_desc(struct vhost_virtqueue *vq,
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001303 struct iovec iov[], unsigned int iov_size,
1304 unsigned int *out_num, unsigned int *in_num,
1305 struct vhost_log *log, unsigned int *log_num)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001306{
1307 struct vring_desc desc;
1308 unsigned int i, head, found = 0;
1309 u16 last_avail_idx;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001310 __virtio16 avail_idx;
1311 __virtio16 ring_head;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001312 int ret;
1313
1314 /* Check it isn't doing very strange things with descriptor numbers. */
1315 last_avail_idx = vq->last_avail_idx;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001316 if (unlikely(__get_user(avail_idx, &vq->avail->idx))) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001317 vq_err(vq, "Failed to access avail idx at %p\n",
1318 &vq->avail->idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001319 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001320 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001321 vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001322
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001323 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001324 vq_err(vq, "Guest moved used index from %u to %u",
1325 last_avail_idx, vq->avail_idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001326 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001327 }
1328
1329 /* If there's nothing new since last we looked, return invalid. */
1330 if (vq->avail_idx == last_avail_idx)
1331 return vq->num;
1332
1333 /* Only get avail ring entries after they have been exposed by guest. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001334 smp_rmb();
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001335
1336 /* Grab the next descriptor number they're advertising, and increment
1337 * the index we've seen. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001338 if (unlikely(__get_user(ring_head,
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001339 &vq->avail->ring[last_avail_idx % vq->num]))) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001340 vq_err(vq, "Failed to read head: idx %d address %p\n",
1341 last_avail_idx,
1342 &vq->avail->ring[last_avail_idx % vq->num]);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001343 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001344 }
1345
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001346 head = vhost16_to_cpu(vq, ring_head);
1347
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001348 /* If their number is silly, that's an error. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001349 if (unlikely(head >= vq->num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001350 vq_err(vq, "Guest says index %u > %u is available",
1351 head, vq->num);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001352 return -EINVAL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001353 }
1354
1355 /* When we start there are none of either input nor output. */
1356 *out_num = *in_num = 0;
1357 if (unlikely(log))
1358 *log_num = 0;
1359
1360 i = head;
1361 do {
1362 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001363 if (unlikely(i >= vq->num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001364 vq_err(vq, "Desc index is %u > %u, head = %u",
1365 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001366 return -EINVAL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001367 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001368 if (unlikely(++found > vq->num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001369 vq_err(vq, "Loop detected: last one at %u "
1370 "vq size %u head %u\n",
1371 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001372 return -EINVAL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001373 }
Michael S. Tsirkinfcc042a2011-03-06 13:33:49 +02001374 ret = __copy_from_user(&desc, vq->desc + i, sizeof desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001375 if (unlikely(ret)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001376 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
1377 i, vq->desc + i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001378 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001379 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001380 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT)) {
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001381 ret = get_indirect(vq, iov, iov_size,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001382 out_num, in_num,
1383 log, log_num, &desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001384 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001385 vq_err(vq, "Failure detected "
1386 "in indirect descriptor at idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001387 return ret;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001388 }
1389 continue;
1390 }
1391
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001392 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
1393 vhost32_to_cpu(vq, desc.len), iov + iov_count,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001394 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001395 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001396 vq_err(vq, "Translation failure %d descriptor idx %d\n",
1397 ret, i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001398 return ret;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001399 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001400 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001401 /* If this is an input descriptor,
1402 * increment that count. */
1403 *in_num += ret;
1404 if (unlikely(log)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001405 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
1406 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001407 ++*log_num;
1408 }
1409 } else {
1410 /* If it's an output descriptor, they're all supposed
1411 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001412 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001413 vq_err(vq, "Descriptor has out after in: "
1414 "idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001415 return -EINVAL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001416 }
1417 *out_num += ret;
1418 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001419 } while ((i = next_desc(vq, &desc)) != -1);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001420
1421 /* On success, increment avail index. */
1422 vq->last_avail_idx++;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001423
1424 /* Assume notifications from guest are disabled at this point,
1425 * if they aren't we would need to update avail_event index. */
1426 BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY));
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001427 return head;
1428}
Asias He6ac1afb2013-05-06 16:38:21 +08001429EXPORT_SYMBOL_GPL(vhost_get_vq_desc);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001430
1431/* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
David Stevens8dd014a2010-07-27 18:52:21 +03001432void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001433{
David Stevens8dd014a2010-07-27 18:52:21 +03001434 vq->last_avail_idx -= n;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001435}
Asias He6ac1afb2013-05-06 16:38:21 +08001436EXPORT_SYMBOL_GPL(vhost_discard_vq_desc);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001437
1438/* After we've used one of their buffers, we tell them about it. We'll then
1439 * want to notify the guest, using eventfd. */
1440int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
1441{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001442 struct vring_used_elem heads = {
1443 cpu_to_vhost32(vq, head),
1444 cpu_to_vhost32(vq, len)
1445 };
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001446
Jason Wangc49e4e52013-09-02 16:40:58 +08001447 return vhost_add_used_n(vq, &heads, 1);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001448}
Asias He6ac1afb2013-05-06 16:38:21 +08001449EXPORT_SYMBOL_GPL(vhost_add_used);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001450
David Stevens8dd014a2010-07-27 18:52:21 +03001451static int __vhost_add_used_n(struct vhost_virtqueue *vq,
1452 struct vring_used_elem *heads,
1453 unsigned count)
1454{
1455 struct vring_used_elem __user *used;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001456 u16 old, new;
David Stevens8dd014a2010-07-27 18:52:21 +03001457 int start;
1458
1459 start = vq->last_used_idx % vq->num;
1460 used = vq->used->ring + start;
Jason Wangc49e4e52013-09-02 16:40:58 +08001461 if (count == 1) {
1462 if (__put_user(heads[0].id, &used->id)) {
1463 vq_err(vq, "Failed to write used id");
1464 return -EFAULT;
1465 }
1466 if (__put_user(heads[0].len, &used->len)) {
1467 vq_err(vq, "Failed to write used len");
1468 return -EFAULT;
1469 }
1470 } else if (__copy_to_user(used, heads, count * sizeof *used)) {
David Stevens8dd014a2010-07-27 18:52:21 +03001471 vq_err(vq, "Failed to write used");
1472 return -EFAULT;
1473 }
1474 if (unlikely(vq->log_used)) {
1475 /* Make sure data is seen before log. */
1476 smp_wmb();
1477 /* Log used ring entry write. */
1478 log_write(vq->log_base,
1479 vq->log_addr +
1480 ((void __user *)used - (void __user *)vq->used),
1481 count * sizeof *used);
1482 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001483 old = vq->last_used_idx;
1484 new = (vq->last_used_idx += count);
1485 /* If the driver never bothers to signal in a very long while,
1486 * used index might wrap around. If that happens, invalidate
1487 * signalled_used index we stored. TODO: make sure driver
1488 * signals at least once in 2^16 and remove this. */
1489 if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old)))
1490 vq->signalled_used_valid = false;
David Stevens8dd014a2010-07-27 18:52:21 +03001491 return 0;
1492}
1493
1494/* After we've used one of their buffers, we tell them about it. We'll then
1495 * want to notify the guest, using eventfd. */
1496int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
1497 unsigned count)
1498{
1499 int start, n, r;
1500
1501 start = vq->last_used_idx % vq->num;
1502 n = vq->num - start;
1503 if (n < count) {
1504 r = __vhost_add_used_n(vq, heads, n);
1505 if (r < 0)
1506 return r;
1507 heads += n;
1508 count -= n;
1509 }
1510 r = __vhost_add_used_n(vq, heads, count);
1511
1512 /* Make sure buffer is written before we update index. */
1513 smp_wmb();
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001514 if (__put_user(cpu_to_vhost16(vq, vq->last_used_idx), &vq->used->idx)) {
David Stevens8dd014a2010-07-27 18:52:21 +03001515 vq_err(vq, "Failed to increment used idx");
1516 return -EFAULT;
1517 }
1518 if (unlikely(vq->log_used)) {
1519 /* Log used index update. */
1520 log_write(vq->log_base,
1521 vq->log_addr + offsetof(struct vring_used, idx),
1522 sizeof vq->used->idx);
1523 if (vq->log_ctx)
1524 eventfd_signal(vq->log_ctx, 1);
1525 }
1526 return r;
1527}
Asias He6ac1afb2013-05-06 16:38:21 +08001528EXPORT_SYMBOL_GPL(vhost_add_used_n);
David Stevens8dd014a2010-07-27 18:52:21 +03001529
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001530static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001531{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001532 __u16 old, new;
1533 __virtio16 event;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001534 bool v;
Michael S. Tsirkin0d499352010-05-11 19:44:17 +03001535 /* Flush out used index updates. This is paired
1536 * with the barrier that the Guest executes when enabling
1537 * interrupts. */
1538 smp_mb();
1539
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001540 if (vhost_has_feature(vq, VIRTIO_F_NOTIFY_ON_EMPTY) &&
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001541 unlikely(vq->avail_idx == vq->last_avail_idx))
1542 return true;
1543
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001544 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001545 __virtio16 flags;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001546 if (__get_user(flags, &vq->avail->flags)) {
1547 vq_err(vq, "Failed to get flags");
1548 return true;
1549 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001550 return !(flags & cpu_to_vhost16(vq, VRING_AVAIL_F_NO_INTERRUPT));
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001551 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001552 old = vq->signalled_used;
1553 v = vq->signalled_used_valid;
1554 new = vq->signalled_used = vq->last_used_idx;
1555 vq->signalled_used_valid = true;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001556
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001557 if (unlikely(!v))
1558 return true;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001559
Michael S. Tsirkin64f7f052014-12-01 17:39:39 +02001560 if (__get_user(event, vhost_used_event(vq))) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001561 vq_err(vq, "Failed to get used event idx");
1562 return true;
1563 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001564 return vring_need_event(vhost16_to_cpu(vq, event), new, old);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001565}
1566
1567/* This actually signals the guest, using eventfd. */
1568void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
1569{
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001570 /* Signal the Guest tell them we used something up. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001571 if (vq->call_ctx && vhost_notify(dev, vq))
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001572 eventfd_signal(vq->call_ctx, 1);
1573}
Asias He6ac1afb2013-05-06 16:38:21 +08001574EXPORT_SYMBOL_GPL(vhost_signal);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001575
1576/* And here's the combo meal deal. Supersize me! */
1577void vhost_add_used_and_signal(struct vhost_dev *dev,
1578 struct vhost_virtqueue *vq,
1579 unsigned int head, int len)
1580{
1581 vhost_add_used(vq, head, len);
1582 vhost_signal(dev, vq);
1583}
Asias He6ac1afb2013-05-06 16:38:21 +08001584EXPORT_SYMBOL_GPL(vhost_add_used_and_signal);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001585
David Stevens8dd014a2010-07-27 18:52:21 +03001586/* multi-buffer version of vhost_add_used_and_signal */
1587void vhost_add_used_and_signal_n(struct vhost_dev *dev,
1588 struct vhost_virtqueue *vq,
1589 struct vring_used_elem *heads, unsigned count)
1590{
1591 vhost_add_used_n(vq, heads, count);
1592 vhost_signal(dev, vq);
1593}
Asias He6ac1afb2013-05-06 16:38:21 +08001594EXPORT_SYMBOL_GPL(vhost_add_used_and_signal_n);
David Stevens8dd014a2010-07-27 18:52:21 +03001595
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001596/* OK, now we need to know about added descriptors. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001597bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001598{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001599 __virtio16 avail_idx;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001600 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301601
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001602 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
1603 return false;
1604 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001605 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08001606 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001607 if (r) {
1608 vq_err(vq, "Failed to enable notification at %p: %d\n",
1609 &vq->used->flags, r);
1610 return false;
1611 }
1612 } else {
Jason Wang2723fea2011-06-21 18:04:38 +08001613 r = vhost_update_avail_event(vq, vq->avail_idx);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001614 if (r) {
1615 vq_err(vq, "Failed to update avail event index at %p: %d\n",
1616 vhost_avail_event(vq), r);
1617 return false;
1618 }
1619 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001620 /* They could have slipped one in as we were doing that: make
1621 * sure it's written, then check again. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001622 smp_mb();
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001623 r = __get_user(avail_idx, &vq->avail->idx);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001624 if (r) {
1625 vq_err(vq, "Failed to check avail idx at %p: %d\n",
1626 &vq->avail->idx, r);
1627 return false;
1628 }
1629
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001630 return vhost16_to_cpu(vq, avail_idx) != vq->avail_idx;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001631}
Asias He6ac1afb2013-05-06 16:38:21 +08001632EXPORT_SYMBOL_GPL(vhost_enable_notify);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001633
1634/* We don't need to be notified again. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001635void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001636{
1637 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301638
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001639 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
1640 return;
1641 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001642 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08001643 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001644 if (r)
1645 vq_err(vq, "Failed to enable notification at %p: %d\n",
1646 &vq->used->flags, r);
1647 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001648}
Asias He6ac1afb2013-05-06 16:38:21 +08001649EXPORT_SYMBOL_GPL(vhost_disable_notify);
1650
1651static int __init vhost_init(void)
1652{
1653 return 0;
1654}
1655
1656static void __exit vhost_exit(void)
1657{
1658}
1659
1660module_init(vhost_init);
1661module_exit(vhost_exit);
1662
1663MODULE_VERSION("0.0.1");
1664MODULE_LICENSE("GPL v2");
1665MODULE_AUTHOR("Michael S. Tsirkin");
1666MODULE_DESCRIPTION("Host kernel accelerator for virtio");