blob: 71bb468130310bbf5937e30a0f60d8e838a3b3c9 [file] [log] [blame]
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001/* Copyright (C) 2009 Red Hat, Inc.
2 * Copyright (C) 2006 Rusty Russell IBM Corporation
3 *
4 * Author: Michael S. Tsirkin <mst@redhat.com>
5 *
6 * Inspiration, some code, and most witty comments come from
Rob Landley61516582011-05-06 09:27:36 -07007 * Documentation/virtual/lguest/lguest.c, by Rusty Russell
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00008 *
9 * This work is licensed under the terms of the GNU GPL, version 2.
10 *
11 * Generic code for virtio server in host kernel.
12 */
13
14#include <linux/eventfd.h>
15#include <linux/vhost.h>
Asias He35596b22013-08-19 09:23:19 +080016#include <linux/uio.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000017#include <linux/mm.h>
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +020018#include <linux/mmu_context.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000019#include <linux/miscdevice.h>
20#include <linux/mutex.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000021#include <linux/poll.h>
22#include <linux/file.h>
23#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
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>
Igor Mammedovbcfeaca2015-06-16 18:33:35 +020028#include <linux/sort.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000029
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000030#include "vhost.h"
31
32enum {
33 VHOST_MEMORY_MAX_NREGIONS = 64,
34 VHOST_MEMORY_F_LOG = 0x1,
35};
36
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +030037#define vhost_used_event(vq) ((__virtio16 __user *)&vq->avail->ring[vq->num])
38#define vhost_avail_event(vq) ((__virtio16 __user *)&vq->used->ring[vq->num])
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +030039
Greg Kurz2751c982015-04-24 14:27:24 +020040#ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
41static void vhost_vq_reset_user_be(struct vhost_virtqueue *vq)
42{
43 vq->user_be = !virtio_legacy_is_little_endian();
44}
45
46static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
47{
48 struct vhost_vring_state s;
49
50 if (vq->private_data)
51 return -EBUSY;
52
53 if (copy_from_user(&s, argp, sizeof(s)))
54 return -EFAULT;
55
56 if (s.num != VHOST_VRING_LITTLE_ENDIAN &&
57 s.num != VHOST_VRING_BIG_ENDIAN)
58 return -EINVAL;
59
60 vq->user_be = s.num;
61
62 return 0;
63}
64
65static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
66 int __user *argp)
67{
68 struct vhost_vring_state s = {
69 .index = idx,
70 .num = vq->user_be
71 };
72
73 if (copy_to_user(argp, &s, sizeof(s)))
74 return -EFAULT;
75
76 return 0;
77}
78
79static void vhost_init_is_le(struct vhost_virtqueue *vq)
80{
81 /* Note for legacy virtio: user_be is initialized at reset time
82 * according to the host endianness. If userspace does not set an
83 * explicit endianness, the default behavior is native endian, as
84 * expected by legacy virtio.
85 */
86 vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) || !vq->user_be;
87}
88#else
89static void vhost_vq_reset_user_be(struct vhost_virtqueue *vq)
90{
91}
92
93static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
94{
95 return -ENOIOCTLCMD;
96}
97
98static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
99 int __user *argp)
100{
101 return -ENOIOCTLCMD;
102}
103
104static void vhost_init_is_le(struct vhost_virtqueue *vq)
105{
106 if (vhost_has_feature(vq, VIRTIO_F_VERSION_1))
107 vq->is_le = true;
108}
109#endif /* CONFIG_VHOST_CROSS_ENDIAN_LEGACY */
110
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000111static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
112 poll_table *pt)
113{
114 struct vhost_poll *poll;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000115
Krishna Kumard47effe2011-03-01 17:06:37 +0530116 poll = container_of(pt, struct vhost_poll, table);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000117 poll->wqh = wqh;
118 add_wait_queue(wqh, &poll->wait);
119}
120
121static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync,
122 void *key)
123{
Tejun Heoc23f34452010-06-02 20:40:00 +0200124 struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
125
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000126 if (!((unsigned long)key & poll->mask))
127 return 0;
128
Tejun Heoc23f34452010-06-02 20:40:00 +0200129 vhost_poll_queue(poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000130 return 0;
131}
132
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000133void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000134{
Tejun Heoc23f34452010-06-02 20:40:00 +0200135 INIT_LIST_HEAD(&work->node);
136 work->fn = fn;
137 init_waitqueue_head(&work->done);
138 work->flushing = 0;
139 work->queue_seq = work->done_seq = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000140}
Asias He6ac1afb2013-05-06 16:38:21 +0800141EXPORT_SYMBOL_GPL(vhost_work_init);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000142
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300143/* Init poll structure */
144void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
145 unsigned long mask, struct vhost_dev *dev)
146{
147 init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
148 init_poll_funcptr(&poll->table, vhost_poll_func);
149 poll->mask = mask;
150 poll->dev = dev;
Jason Wang2b8b3282013-01-28 01:05:18 +0000151 poll->wqh = NULL;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300152
153 vhost_work_init(&poll->work, fn);
154}
Asias He6ac1afb2013-05-06 16:38:21 +0800155EXPORT_SYMBOL_GPL(vhost_poll_init);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300156
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000157/* Start polling a file. We add ourselves to file's wait queue. The caller must
158 * keep a reference to a file until after vhost_poll_stop is called. */
Jason Wang2b8b3282013-01-28 01:05:18 +0000159int vhost_poll_start(struct vhost_poll *poll, struct file *file)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000160{
161 unsigned long mask;
Jason Wang2b8b3282013-01-28 01:05:18 +0000162 int ret = 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530163
Jason Wang70181d512013-04-10 20:50:48 +0000164 if (poll->wqh)
165 return 0;
166
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000167 mask = file->f_op->poll(file, &poll->table);
168 if (mask)
169 vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
Jason Wang2b8b3282013-01-28 01:05:18 +0000170 if (mask & POLLERR) {
171 if (poll->wqh)
172 remove_wait_queue(poll->wqh, &poll->wait);
173 ret = -EINVAL;
174 }
175
176 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000177}
Asias He6ac1afb2013-05-06 16:38:21 +0800178EXPORT_SYMBOL_GPL(vhost_poll_start);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000179
180/* Stop polling a file. After this function returns, it becomes safe to drop the
181 * file reference. You must also flush afterwards. */
182void vhost_poll_stop(struct vhost_poll *poll)
183{
Jason Wang2b8b3282013-01-28 01:05:18 +0000184 if (poll->wqh) {
185 remove_wait_queue(poll->wqh, &poll->wait);
186 poll->wqh = NULL;
187 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000188}
Asias He6ac1afb2013-05-06 16:38:21 +0800189EXPORT_SYMBOL_GPL(vhost_poll_stop);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000190
Michael S. Tsirkin0174b0c2011-01-10 10:03:20 +0200191static bool vhost_work_seq_done(struct vhost_dev *dev, struct vhost_work *work,
192 unsigned seq)
193{
194 int left;
Krishna Kumard47effe2011-03-01 17:06:37 +0530195
Michael S. Tsirkin0174b0c2011-01-10 10:03:20 +0200196 spin_lock_irq(&dev->work_lock);
197 left = seq - work->done_seq;
198 spin_unlock_irq(&dev->work_lock);
199 return left <= 0;
200}
201
Asias He6ac1afb2013-05-06 16:38:21 +0800202void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000203{
Tejun Heoc23f34452010-06-02 20:40:00 +0200204 unsigned seq;
Tejun Heoc23f34452010-06-02 20:40:00 +0200205 int flushing;
206
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300207 spin_lock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200208 seq = work->queue_seq;
209 work->flushing++;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300210 spin_unlock_irq(&dev->work_lock);
Michael S. Tsirkin0174b0c2011-01-10 10:03:20 +0200211 wait_event(work->done, vhost_work_seq_done(dev, work, seq));
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300212 spin_lock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200213 flushing = --work->flushing;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300214 spin_unlock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200215 BUG_ON(flushing < 0);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000216}
Asias He6ac1afb2013-05-06 16:38:21 +0800217EXPORT_SYMBOL_GPL(vhost_work_flush);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000218
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300219/* Flush any work that has been scheduled. When calling this, don't hold any
220 * locks that are also used by the callback. */
221void vhost_poll_flush(struct vhost_poll *poll)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000222{
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300223 vhost_work_flush(poll->dev, &poll->work);
224}
Asias He6ac1afb2013-05-06 16:38:21 +0800225EXPORT_SYMBOL_GPL(vhost_poll_flush);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300226
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000227void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300228{
Tejun Heoc23f34452010-06-02 20:40:00 +0200229 unsigned long flags;
230
231 spin_lock_irqsave(&dev->work_lock, flags);
232 if (list_empty(&work->node)) {
233 list_add_tail(&work->node, &dev->work_list);
234 work->queue_seq++;
Qin Chuanyuac9fde22013-06-07 21:50:16 +0800235 spin_unlock_irqrestore(&dev->work_lock, flags);
Tejun Heoc23f34452010-06-02 20:40:00 +0200236 wake_up_process(dev->worker);
Qin Chuanyuac9fde22013-06-07 21:50:16 +0800237 } else {
238 spin_unlock_irqrestore(&dev->work_lock, flags);
Tejun Heoc23f34452010-06-02 20:40:00 +0200239 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000240}
Asias He6ac1afb2013-05-06 16:38:21 +0800241EXPORT_SYMBOL_GPL(vhost_work_queue);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000242
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300243void vhost_poll_queue(struct vhost_poll *poll)
244{
245 vhost_work_queue(poll->dev, &poll->work);
246}
Asias He6ac1afb2013-05-06 16:38:21 +0800247EXPORT_SYMBOL_GPL(vhost_poll_queue);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300248
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000249static void vhost_vq_reset(struct vhost_dev *dev,
250 struct vhost_virtqueue *vq)
251{
252 vq->num = 1;
253 vq->desc = NULL;
254 vq->avail = NULL;
255 vq->used = NULL;
256 vq->last_avail_idx = 0;
257 vq->avail_idx = 0;
258 vq->last_used_idx = 0;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300259 vq->signalled_used = 0;
260 vq->signalled_used_valid = false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000261 vq->used_flags = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000262 vq->log_used = false;
263 vq->log_addr = -1ull;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000264 vq->private_data = NULL;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300265 vq->acked_features = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000266 vq->log_base = NULL;
267 vq->error_ctx = NULL;
268 vq->error = NULL;
269 vq->kick = NULL;
270 vq->call_ctx = NULL;
271 vq->call = NULL;
Michael S. Tsirkin73a99f02010-02-23 11:23:45 +0200272 vq->log_ctx = NULL;
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300273 vq->memory = NULL;
Greg Kurz2751c982015-04-24 14:27:24 +0200274 vq->is_le = virtio_legacy_is_little_endian();
275 vhost_vq_reset_user_be(vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000276}
277
Tejun Heoc23f34452010-06-02 20:40:00 +0200278static int vhost_worker(void *data)
279{
280 struct vhost_dev *dev = data;
281 struct vhost_work *work = NULL;
282 unsigned uninitialized_var(seq);
Jens Freimannd7ffde32012-06-26 00:59:58 +0000283 mm_segment_t oldfs = get_fs();
Tejun Heoc23f34452010-06-02 20:40:00 +0200284
Jens Freimannd7ffde32012-06-26 00:59:58 +0000285 set_fs(USER_DS);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200286 use_mm(dev->mm);
287
Tejun Heoc23f34452010-06-02 20:40:00 +0200288 for (;;) {
289 /* mb paired w/ kthread_stop */
290 set_current_state(TASK_INTERRUPTIBLE);
291
292 spin_lock_irq(&dev->work_lock);
293 if (work) {
294 work->done_seq = seq;
295 if (work->flushing)
296 wake_up_all(&work->done);
297 }
298
299 if (kthread_should_stop()) {
300 spin_unlock_irq(&dev->work_lock);
301 __set_current_state(TASK_RUNNING);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200302 break;
Tejun Heoc23f34452010-06-02 20:40:00 +0200303 }
304 if (!list_empty(&dev->work_list)) {
305 work = list_first_entry(&dev->work_list,
306 struct vhost_work, node);
307 list_del_init(&work->node);
308 seq = work->queue_seq;
309 } else
310 work = NULL;
311 spin_unlock_irq(&dev->work_lock);
312
313 if (work) {
314 __set_current_state(TASK_RUNNING);
315 work->fn(work);
Nadav Har'Eld550dda2012-02-27 15:07:29 +0200316 if (need_resched())
317 schedule();
Tejun Heoc23f34452010-06-02 20:40:00 +0200318 } else
319 schedule();
320
321 }
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200322 unuse_mm(dev->mm);
Jens Freimannd7ffde32012-06-26 00:59:58 +0000323 set_fs(oldfs);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200324 return 0;
Tejun Heoc23f34452010-06-02 20:40:00 +0200325}
326
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000327static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
328{
329 kfree(vq->indirect);
330 vq->indirect = NULL;
331 kfree(vq->log);
332 vq->log = NULL;
333 kfree(vq->heads);
334 vq->heads = NULL;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000335}
336
Jason Wange0e9b402010-09-14 23:53:05 +0800337/* Helper to allocate iovec buffers for all vqs. */
338static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
339{
Asias He6d5e6aa2013-05-06 16:38:23 +0800340 struct vhost_virtqueue *vq;
Jason Wange0e9b402010-09-14 23:53:05 +0800341 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530342
Jason Wange0e9b402010-09-14 23:53:05 +0800343 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800344 vq = dev->vqs[i];
345 vq->indirect = kmalloc(sizeof *vq->indirect * UIO_MAXIOV,
346 GFP_KERNEL);
347 vq->log = kmalloc(sizeof *vq->log * UIO_MAXIOV, GFP_KERNEL);
348 vq->heads = kmalloc(sizeof *vq->heads * UIO_MAXIOV, GFP_KERNEL);
349 if (!vq->indirect || !vq->log || !vq->heads)
Jason Wange0e9b402010-09-14 23:53:05 +0800350 goto err_nomem;
351 }
352 return 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530353
Jason Wange0e9b402010-09-14 23:53:05 +0800354err_nomem:
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000355 for (; i >= 0; --i)
Asias He3ab2e422013-04-27 11:16:48 +0800356 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800357 return -ENOMEM;
358}
359
360static void vhost_dev_free_iovecs(struct vhost_dev *dev)
361{
362 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530363
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000364 for (i = 0; i < dev->nvqs; ++i)
Asias He3ab2e422013-04-27 11:16:48 +0800365 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800366}
367
Zhi Yong Wu59566b6e2013-12-07 04:13:03 +0800368void vhost_dev_init(struct vhost_dev *dev,
Asias He3ab2e422013-04-27 11:16:48 +0800369 struct vhost_virtqueue **vqs, int nvqs)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000370{
Asias He6d5e6aa2013-05-06 16:38:23 +0800371 struct vhost_virtqueue *vq;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000372 int i;
Tejun Heoc23f34452010-06-02 20:40:00 +0200373
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000374 dev->vqs = vqs;
375 dev->nvqs = nvqs;
376 mutex_init(&dev->mutex);
377 dev->log_ctx = NULL;
378 dev->log_file = NULL;
379 dev->memory = NULL;
380 dev->mm = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200381 spin_lock_init(&dev->work_lock);
382 INIT_LIST_HEAD(&dev->work_list);
383 dev->worker = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000384
385 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800386 vq = dev->vqs[i];
387 vq->log = NULL;
388 vq->indirect = NULL;
389 vq->heads = NULL;
390 vq->dev = dev;
391 mutex_init(&vq->mutex);
392 vhost_vq_reset(dev, vq);
393 if (vq->handle_kick)
394 vhost_poll_init(&vq->poll, vq->handle_kick,
395 POLLIN, dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000396 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000397}
Asias He6ac1afb2013-05-06 16:38:21 +0800398EXPORT_SYMBOL_GPL(vhost_dev_init);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000399
400/* Caller should have device mutex */
401long vhost_dev_check_owner(struct vhost_dev *dev)
402{
403 /* Are you the owner? If not, I don't think you mean to do that */
404 return dev->mm == current->mm ? 0 : -EPERM;
405}
Asias He6ac1afb2013-05-06 16:38:21 +0800406EXPORT_SYMBOL_GPL(vhost_dev_check_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000407
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300408struct vhost_attach_cgroups_struct {
Krishna Kumard47effe2011-03-01 17:06:37 +0530409 struct vhost_work work;
410 struct task_struct *owner;
411 int ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300412};
413
414static void vhost_attach_cgroups_work(struct vhost_work *work)
415{
Krishna Kumard47effe2011-03-01 17:06:37 +0530416 struct vhost_attach_cgroups_struct *s;
417
418 s = container_of(work, struct vhost_attach_cgroups_struct, work);
419 s->ret = cgroup_attach_task_all(s->owner, current);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300420}
421
422static int vhost_attach_cgroups(struct vhost_dev *dev)
423{
Krishna Kumard47effe2011-03-01 17:06:37 +0530424 struct vhost_attach_cgroups_struct attach;
425
426 attach.owner = current;
427 vhost_work_init(&attach.work, vhost_attach_cgroups_work);
428 vhost_work_queue(dev, &attach.work);
429 vhost_work_flush(dev, &attach.work);
430 return attach.ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300431}
432
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000433/* Caller should have device mutex */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300434bool vhost_dev_has_owner(struct vhost_dev *dev)
435{
436 return dev->mm;
437}
Asias He6ac1afb2013-05-06 16:38:21 +0800438EXPORT_SYMBOL_GPL(vhost_dev_has_owner);
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300439
440/* Caller should have device mutex */
Asias He54db63c2013-05-06 11:15:59 +0800441long vhost_dev_set_owner(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000442{
Tejun Heoc23f34452010-06-02 20:40:00 +0200443 struct task_struct *worker;
444 int err;
Krishna Kumard47effe2011-03-01 17:06:37 +0530445
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000446 /* Is there an owner already? */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300447 if (vhost_dev_has_owner(dev)) {
Tejun Heoc23f34452010-06-02 20:40:00 +0200448 err = -EBUSY;
449 goto err_mm;
450 }
Krishna Kumard47effe2011-03-01 17:06:37 +0530451
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000452 /* No owner, become one */
453 dev->mm = get_task_mm(current);
Tejun Heoc23f34452010-06-02 20:40:00 +0200454 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
455 if (IS_ERR(worker)) {
456 err = PTR_ERR(worker);
457 goto err_worker;
458 }
459
460 dev->worker = worker;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300461 wake_up_process(worker); /* avoid contributing to loadavg */
462
463 err = vhost_attach_cgroups(dev);
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300464 if (err)
465 goto err_cgroup;
Tejun Heoc23f34452010-06-02 20:40:00 +0200466
Jason Wange0e9b402010-09-14 23:53:05 +0800467 err = vhost_dev_alloc_iovecs(dev);
468 if (err)
469 goto err_cgroup;
470
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000471 return 0;
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300472err_cgroup:
473 kthread_stop(worker);
Michael S. Tsirkin615cc222010-09-02 14:16:36 +0300474 dev->worker = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200475err_worker:
476 if (dev->mm)
477 mmput(dev->mm);
478 dev->mm = NULL;
479err_mm:
480 return err;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000481}
Asias He6ac1afb2013-05-06 16:38:21 +0800482EXPORT_SYMBOL_GPL(vhost_dev_set_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000483
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300484struct vhost_memory *vhost_dev_reset_owner_prepare(void)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000485{
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300486 return kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
487}
Asias He6ac1afb2013-05-06 16:38:21 +0800488EXPORT_SYMBOL_GPL(vhost_dev_reset_owner_prepare);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000489
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300490/* Caller should have device mutex */
491void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_memory *memory)
492{
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300493 int i;
494
Michael S. Tsirkinea5d4042011-11-27 19:05:58 +0200495 vhost_dev_cleanup(dev, true);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000496
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300497 /* Restore memory to default empty mapping. */
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000498 memory->nregions = 0;
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300499 dev->memory = memory;
500 /* We don't need VQ locks below since vhost_dev_cleanup makes sure
501 * VQs aren't running.
502 */
503 for (i = 0; i < dev->nvqs; ++i)
504 dev->vqs[i]->memory = memory;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000505}
Asias He6ac1afb2013-05-06 16:38:21 +0800506EXPORT_SYMBOL_GPL(vhost_dev_reset_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000507
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000508void vhost_dev_stop(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000509{
510 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530511
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000512 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800513 if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) {
514 vhost_poll_stop(&dev->vqs[i]->poll);
515 vhost_poll_flush(&dev->vqs[i]->poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000516 }
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000517 }
518}
Asias He6ac1afb2013-05-06 16:38:21 +0800519EXPORT_SYMBOL_GPL(vhost_dev_stop);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000520
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000521/* Caller should have device mutex if and only if locked is set */
522void vhost_dev_cleanup(struct vhost_dev *dev, bool locked)
523{
524 int i;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000525
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000526 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800527 if (dev->vqs[i]->error_ctx)
528 eventfd_ctx_put(dev->vqs[i]->error_ctx);
529 if (dev->vqs[i]->error)
530 fput(dev->vqs[i]->error);
531 if (dev->vqs[i]->kick)
532 fput(dev->vqs[i]->kick);
533 if (dev->vqs[i]->call_ctx)
534 eventfd_ctx_put(dev->vqs[i]->call_ctx);
535 if (dev->vqs[i]->call)
536 fput(dev->vqs[i]->call);
537 vhost_vq_reset(dev, dev->vqs[i]);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000538 }
Jason Wange0e9b402010-09-14 23:53:05 +0800539 vhost_dev_free_iovecs(dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000540 if (dev->log_ctx)
541 eventfd_ctx_put(dev->log_ctx);
542 dev->log_ctx = NULL;
543 if (dev->log_file)
544 fput(dev->log_file);
545 dev->log_file = NULL;
546 /* No one will access memory at this point */
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300547 kfree(dev->memory);
548 dev->memory = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200549 WARN_ON(!list_empty(&dev->work_list));
Eric Dumazet78b620c2010-08-31 02:05:57 +0000550 if (dev->worker) {
551 kthread_stop(dev->worker);
552 dev->worker = NULL;
553 }
Michael S. Tsirkin533a19b2010-10-06 15:34:38 +0200554 if (dev->mm)
555 mmput(dev->mm);
556 dev->mm = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000557}
Asias He6ac1afb2013-05-06 16:38:21 +0800558EXPORT_SYMBOL_GPL(vhost_dev_cleanup);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000559
560static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
561{
562 u64 a = addr / VHOST_PAGE_SIZE / 8;
Krishna Kumard47effe2011-03-01 17:06:37 +0530563
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000564 /* Make sure 64 bit math will not overflow. */
565 if (a > ULONG_MAX - (unsigned long)log_base ||
566 a + (unsigned long)log_base > ULONG_MAX)
Dan Carpenter6d97e552010-10-11 19:24:19 +0200567 return 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000568
569 return access_ok(VERIFY_WRITE, log_base + a,
570 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
571}
572
573/* Caller should have vq mutex and device mutex. */
574static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
575 int log_all)
576{
577 int i;
Jeff Dike179b2842010-04-07 09:59:10 -0400578
Michael S. Tsirkinf8322fb2010-05-27 12:28:03 +0300579 if (!mem)
580 return 0;
Jeff Dike179b2842010-04-07 09:59:10 -0400581
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000582 for (i = 0; i < mem->nregions; ++i) {
583 struct vhost_memory_region *m = mem->regions + i;
584 unsigned long a = m->userspace_addr;
585 if (m->memory_size > ULONG_MAX)
586 return 0;
587 else if (!access_ok(VERIFY_WRITE, (void __user *)a,
588 m->memory_size))
589 return 0;
590 else if (log_all && !log_access_ok(log_base,
591 m->guest_phys_addr,
592 m->memory_size))
593 return 0;
594 }
595 return 1;
596}
597
598/* Can we switch to this memory table? */
599/* Caller should have device mutex but not vq mutex */
600static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
601 int log_all)
602{
603 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530604
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000605 for (i = 0; i < d->nvqs; ++i) {
606 int ok;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300607 bool log;
608
Asias He3ab2e422013-04-27 11:16:48 +0800609 mutex_lock(&d->vqs[i]->mutex);
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300610 log = log_all || vhost_has_feature(d->vqs[i], VHOST_F_LOG_ALL);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000611 /* If ring is inactive, will check when it's enabled. */
Asias He3ab2e422013-04-27 11:16:48 +0800612 if (d->vqs[i]->private_data)
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300613 ok = vq_memory_access_ok(d->vqs[i]->log_base, mem, log);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000614 else
615 ok = 1;
Asias He3ab2e422013-04-27 11:16:48 +0800616 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000617 if (!ok)
618 return 0;
619 }
620 return 1;
621}
622
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300623static int vq_access_ok(struct vhost_virtqueue *vq, unsigned int num,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000624 struct vring_desc __user *desc,
625 struct vring_avail __user *avail,
626 struct vring_used __user *used)
627{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300628 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000629 return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
630 access_ok(VERIFY_READ, avail,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300631 sizeof *avail + num * sizeof *avail->ring + s) &&
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000632 access_ok(VERIFY_WRITE, used,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300633 sizeof *used + num * sizeof *used->ring + s);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000634}
635
636/* Can we log writes? */
637/* Caller should have device mutex but not vq mutex */
638int vhost_log_access_ok(struct vhost_dev *dev)
639{
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300640 return memory_access_ok(dev, dev->memory, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000641}
Asias He6ac1afb2013-05-06 16:38:21 +0800642EXPORT_SYMBOL_GPL(vhost_log_access_ok);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000643
644/* Verify access for write logging. */
645/* Caller should have vq mutex and device mutex */
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300646static int vq_log_access_ok(struct vhost_virtqueue *vq,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300647 void __user *log_base)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000648{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300649 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100650
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300651 return vq_memory_access_ok(log_base, vq->memory,
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300652 vhost_has_feature(vq, VHOST_F_LOG_ALL)) &&
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000653 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
654 sizeof *vq->used +
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300655 vq->num * sizeof *vq->used->ring + s));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000656}
657
658/* Can we start vq? */
659/* Caller should have vq mutex and device mutex */
660int vhost_vq_access_ok(struct vhost_virtqueue *vq)
661{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300662 return vq_access_ok(vq, vq->num, vq->desc, vq->avail, vq->used) &&
663 vq_log_access_ok(vq, vq->log_base);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000664}
Asias He6ac1afb2013-05-06 16:38:21 +0800665EXPORT_SYMBOL_GPL(vhost_vq_access_ok);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000666
Igor Mammedovbcfeaca2015-06-16 18:33:35 +0200667static int vhost_memory_reg_sort_cmp(const void *p1, const void *p2)
668{
669 const struct vhost_memory_region *r1 = p1, *r2 = p2;
670 if (r1->guest_phys_addr < r2->guest_phys_addr)
671 return 1;
672 if (r1->guest_phys_addr > r2->guest_phys_addr)
673 return -1;
674 return 0;
675}
676
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000677static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
678{
679 struct vhost_memory mem, *newmem, *oldmem;
680 unsigned long size = offsetof(struct vhost_memory, regions);
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +0300681 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530682
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900683 if (copy_from_user(&mem, m, size))
684 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000685 if (mem.padding)
686 return -EOPNOTSUPP;
687 if (mem.nregions > VHOST_MEMORY_MAX_NREGIONS)
688 return -E2BIG;
689 newmem = kmalloc(size + mem.nregions * sizeof *m->regions, GFP_KERNEL);
690 if (!newmem)
691 return -ENOMEM;
692
693 memcpy(newmem, &mem, size);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900694 if (copy_from_user(newmem->regions, m->regions,
695 mem.nregions * sizeof *m->regions)) {
Igor Mammedovbcfeaca2015-06-16 18:33:35 +0200696 kvfree(newmem);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900697 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000698 }
Igor Mammedovbcfeaca2015-06-16 18:33:35 +0200699 sort(newmem->regions, newmem->nregions, sizeof(*newmem->regions),
700 vhost_memory_reg_sort_cmp, NULL);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000701
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300702 if (!memory_access_ok(d, newmem, 0)) {
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +0900703 kfree(newmem);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000704 return -EFAULT;
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +0900705 }
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300706 oldmem = d->memory;
707 d->memory = newmem;
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +0300708
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300709 /* All memory accesses are done under some VQ mutex. */
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +0300710 for (i = 0; i < d->nvqs; ++i) {
711 mutex_lock(&d->vqs[i]->mutex);
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300712 d->vqs[i]->memory = newmem;
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +0300713 mutex_unlock(&d->vqs[i]->mutex);
714 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000715 kfree(oldmem);
716 return 0;
717}
718
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200719long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000720{
Al Virocecb46f2012-08-27 14:21:39 -0400721 struct file *eventfp, *filep = NULL;
722 bool pollstart = false, pollstop = false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000723 struct eventfd_ctx *ctx = NULL;
724 u32 __user *idxp = argp;
725 struct vhost_virtqueue *vq;
726 struct vhost_vring_state s;
727 struct vhost_vring_file f;
728 struct vhost_vring_addr a;
729 u32 idx;
730 long r;
731
732 r = get_user(idx, idxp);
733 if (r < 0)
734 return r;
Krishna Kumar0f3d9a12010-05-25 11:10:36 +0530735 if (idx >= d->nvqs)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000736 return -ENOBUFS;
737
Asias He3ab2e422013-04-27 11:16:48 +0800738 vq = d->vqs[idx];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000739
740 mutex_lock(&vq->mutex);
741
742 switch (ioctl) {
743 case VHOST_SET_VRING_NUM:
744 /* Resizing ring with an active backend?
745 * You don't want to do that. */
746 if (vq->private_data) {
747 r = -EBUSY;
748 break;
749 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900750 if (copy_from_user(&s, argp, sizeof s)) {
751 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000752 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900753 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000754 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
755 r = -EINVAL;
756 break;
757 }
758 vq->num = s.num;
759 break;
760 case VHOST_SET_VRING_BASE:
761 /* Moving base with an active backend?
762 * You don't want to do that. */
763 if (vq->private_data) {
764 r = -EBUSY;
765 break;
766 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900767 if (copy_from_user(&s, argp, sizeof s)) {
768 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000769 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900770 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000771 if (s.num > 0xffff) {
772 r = -EINVAL;
773 break;
774 }
775 vq->last_avail_idx = s.num;
776 /* Forget the cached index value. */
777 vq->avail_idx = vq->last_avail_idx;
778 break;
779 case VHOST_GET_VRING_BASE:
780 s.index = idx;
781 s.num = vq->last_avail_idx;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900782 if (copy_to_user(argp, &s, sizeof s))
783 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000784 break;
785 case VHOST_SET_VRING_ADDR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900786 if (copy_from_user(&a, argp, sizeof a)) {
787 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000788 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900789 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000790 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
791 r = -EOPNOTSUPP;
792 break;
793 }
794 /* For 32bit, verify that the top 32bits of the user
795 data are set to zero. */
796 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
797 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
798 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
799 r = -EFAULT;
800 break;
801 }
Michael S. Tsirkin5d9a07b2014-12-21 01:00:23 +0200802
803 /* Make sure it's safe to cast pointers to vring types. */
804 BUILD_BUG_ON(__alignof__ *vq->avail > VRING_AVAIL_ALIGN_SIZE);
805 BUILD_BUG_ON(__alignof__ *vq->used > VRING_USED_ALIGN_SIZE);
806 if ((a.avail_user_addr & (VRING_AVAIL_ALIGN_SIZE - 1)) ||
807 (a.used_user_addr & (VRING_USED_ALIGN_SIZE - 1)) ||
808 (a.log_guest_addr & (sizeof(u64) - 1))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000809 r = -EINVAL;
810 break;
811 }
812
813 /* We only verify access here if backend is configured.
814 * If it is not, we don't as size might not have been setup.
815 * We will verify when backend is configured. */
816 if (vq->private_data) {
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300817 if (!vq_access_ok(vq, vq->num,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000818 (void __user *)(unsigned long)a.desc_user_addr,
819 (void __user *)(unsigned long)a.avail_user_addr,
820 (void __user *)(unsigned long)a.used_user_addr)) {
821 r = -EINVAL;
822 break;
823 }
824
825 /* Also validate log access for used ring if enabled. */
826 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
827 !log_access_ok(vq->log_base, a.log_guest_addr,
828 sizeof *vq->used +
829 vq->num * sizeof *vq->used->ring)) {
830 r = -EINVAL;
831 break;
832 }
833 }
834
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000835 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
836 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
837 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
838 vq->log_addr = a.log_guest_addr;
839 vq->used = (void __user *)(unsigned long)a.used_user_addr;
840 break;
841 case VHOST_SET_VRING_KICK:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900842 if (copy_from_user(&f, argp, sizeof f)) {
843 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000844 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900845 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000846 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200847 if (IS_ERR(eventfp)) {
848 r = PTR_ERR(eventfp);
849 break;
850 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000851 if (eventfp != vq->kick) {
Al Virocecb46f2012-08-27 14:21:39 -0400852 pollstop = (filep = vq->kick) != NULL;
853 pollstart = (vq->kick = eventfp) != NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000854 } else
855 filep = eventfp;
856 break;
857 case VHOST_SET_VRING_CALL:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900858 if (copy_from_user(&f, argp, sizeof f)) {
859 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000860 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900861 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000862 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200863 if (IS_ERR(eventfp)) {
864 r = PTR_ERR(eventfp);
865 break;
866 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000867 if (eventfp != vq->call) {
868 filep = vq->call;
869 ctx = vq->call_ctx;
870 vq->call = eventfp;
871 vq->call_ctx = eventfp ?
872 eventfd_ctx_fileget(eventfp) : NULL;
873 } else
874 filep = eventfp;
875 break;
876 case VHOST_SET_VRING_ERR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900877 if (copy_from_user(&f, argp, sizeof f)) {
878 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000879 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900880 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000881 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200882 if (IS_ERR(eventfp)) {
883 r = PTR_ERR(eventfp);
884 break;
885 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000886 if (eventfp != vq->error) {
887 filep = vq->error;
888 vq->error = eventfp;
889 ctx = vq->error_ctx;
890 vq->error_ctx = eventfp ?
891 eventfd_ctx_fileget(eventfp) : NULL;
892 } else
893 filep = eventfp;
894 break;
Greg Kurz2751c982015-04-24 14:27:24 +0200895 case VHOST_SET_VRING_ENDIAN:
896 r = vhost_set_vring_endian(vq, argp);
897 break;
898 case VHOST_GET_VRING_ENDIAN:
899 r = vhost_get_vring_endian(vq, idx, argp);
900 break;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000901 default:
902 r = -ENOIOCTLCMD;
903 }
904
905 if (pollstop && vq->handle_kick)
906 vhost_poll_stop(&vq->poll);
907
908 if (ctx)
909 eventfd_ctx_put(ctx);
910 if (filep)
911 fput(filep);
912
913 if (pollstart && vq->handle_kick)
Jason Wang2b8b3282013-01-28 01:05:18 +0000914 r = vhost_poll_start(&vq->poll, vq->kick);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000915
916 mutex_unlock(&vq->mutex);
917
918 if (pollstop && vq->handle_kick)
919 vhost_poll_flush(&vq->poll);
920 return r;
921}
Asias He6ac1afb2013-05-06 16:38:21 +0800922EXPORT_SYMBOL_GPL(vhost_vring_ioctl);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000923
924/* Caller must have device mutex */
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200925long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000926{
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000927 struct file *eventfp, *filep = NULL;
928 struct eventfd_ctx *ctx = NULL;
929 u64 p;
930 long r;
931 int i, fd;
932
933 /* If you are not the owner, you can become one */
934 if (ioctl == VHOST_SET_OWNER) {
935 r = vhost_dev_set_owner(d);
936 goto done;
937 }
938
939 /* You must be the owner to do anything else */
940 r = vhost_dev_check_owner(d);
941 if (r)
942 goto done;
943
944 switch (ioctl) {
945 case VHOST_SET_MEM_TABLE:
946 r = vhost_set_memory(d, argp);
947 break;
948 case VHOST_SET_LOG_BASE:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900949 if (copy_from_user(&p, argp, sizeof p)) {
950 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000951 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900952 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000953 if ((u64)(unsigned long)p != p) {
954 r = -EFAULT;
955 break;
956 }
957 for (i = 0; i < d->nvqs; ++i) {
958 struct vhost_virtqueue *vq;
959 void __user *base = (void __user *)(unsigned long)p;
Asias He3ab2e422013-04-27 11:16:48 +0800960 vq = d->vqs[i];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000961 mutex_lock(&vq->mutex);
962 /* If ring is inactive, will check when it's enabled. */
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300963 if (vq->private_data && !vq_log_access_ok(vq, base))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000964 r = -EFAULT;
965 else
966 vq->log_base = base;
967 mutex_unlock(&vq->mutex);
968 }
969 break;
970 case VHOST_SET_LOG_FD:
971 r = get_user(fd, (int __user *)argp);
972 if (r < 0)
973 break;
974 eventfp = fd == -1 ? NULL : eventfd_fget(fd);
975 if (IS_ERR(eventfp)) {
976 r = PTR_ERR(eventfp);
977 break;
978 }
979 if (eventfp != d->log_file) {
980 filep = d->log_file;
981 ctx = d->log_ctx;
982 d->log_ctx = eventfp ?
983 eventfd_ctx_fileget(eventfp) : NULL;
984 } else
985 filep = eventfp;
986 for (i = 0; i < d->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800987 mutex_lock(&d->vqs[i]->mutex);
988 d->vqs[i]->log_ctx = d->log_ctx;
989 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000990 }
991 if (ctx)
992 eventfd_ctx_put(ctx);
993 if (filep)
994 fput(filep);
995 break;
996 default:
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200997 r = -ENOIOCTLCMD;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000998 break;
999 }
1000done:
1001 return r;
1002}
Asias He6ac1afb2013-05-06 16:38:21 +08001003EXPORT_SYMBOL_GPL(vhost_dev_ioctl);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001004
1005static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
1006 __u64 addr, __u32 len)
1007{
Igor Mammedovbcfeaca2015-06-16 18:33:35 +02001008 const struct vhost_memory_region *reg;
1009 int start = 0, end = mem->nregions;
Krishna Kumard47effe2011-03-01 17:06:37 +05301010
Igor Mammedovbcfeaca2015-06-16 18:33:35 +02001011 while (start < end) {
1012 int slot = start + (end - start) / 2;
1013 reg = mem->regions + slot;
1014 if (addr >= reg->guest_phys_addr)
1015 end = slot;
1016 else
1017 start = slot + 1;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001018 }
Igor Mammedovbcfeaca2015-06-16 18:33:35 +02001019
1020 reg = mem->regions + start;
1021 if (addr >= reg->guest_phys_addr &&
1022 reg->guest_phys_addr + reg->memory_size > addr)
1023 return reg;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001024 return NULL;
1025}
1026
1027/* TODO: This is really inefficient. We need something like get_user()
1028 * (instruction directly accesses the data, with an exception table entry
1029 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
1030 */
1031static int set_bit_to_user(int nr, void __user *addr)
1032{
1033 unsigned long log = (unsigned long)addr;
1034 struct page *page;
1035 void *base;
1036 int bit = nr + (log % PAGE_SIZE) * 8;
1037 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301038
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001039 r = get_user_pages_fast(log, 1, 1, &page);
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +02001040 if (r < 0)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001041 return r;
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +02001042 BUG_ON(r != 1);
Cong Wangc6daa7f2011-11-25 23:14:26 +08001043 base = kmap_atomic(page);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001044 set_bit(bit, base);
Cong Wangc6daa7f2011-11-25 23:14:26 +08001045 kunmap_atomic(base);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001046 set_page_dirty_lock(page);
1047 put_page(page);
1048 return 0;
1049}
1050
1051static int log_write(void __user *log_base,
1052 u64 write_address, u64 write_length)
1053{
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001054 u64 write_page = write_address / VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001055 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301056
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001057 if (!write_length)
1058 return 0;
Michael S. Tsirkin3bf9be42010-11-29 10:19:07 +02001059 write_length += write_address % VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001060 for (;;) {
1061 u64 base = (u64)(unsigned long)log_base;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001062 u64 log = base + write_page / 8;
1063 int bit = write_page % 8;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001064 if ((u64)(unsigned long)log != log)
1065 return -EFAULT;
1066 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
1067 if (r < 0)
1068 return r;
1069 if (write_length <= VHOST_PAGE_SIZE)
1070 break;
1071 write_length -= VHOST_PAGE_SIZE;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001072 write_page += 1;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001073 }
1074 return r;
1075}
1076
1077int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
1078 unsigned int log_num, u64 len)
1079{
1080 int i, r;
1081
1082 /* Make sure data written is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001083 smp_wmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001084 for (i = 0; i < log_num; ++i) {
1085 u64 l = min(log[i].len, len);
1086 r = log_write(vq->log_base, log[i].addr, l);
1087 if (r < 0)
1088 return r;
1089 len -= l;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +02001090 if (!len) {
1091 if (vq->log_ctx)
1092 eventfd_signal(vq->log_ctx, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001093 return 0;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +02001094 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001095 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001096 /* Length written exceeds what we have stored. This is a bug. */
1097 BUG();
1098 return 0;
1099}
Asias He6ac1afb2013-05-06 16:38:21 +08001100EXPORT_SYMBOL_GPL(vhost_log_write);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001101
Jason Wang2723fea2011-06-21 18:04:38 +08001102static int vhost_update_used_flags(struct vhost_virtqueue *vq)
1103{
1104 void __user *used;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001105 if (__put_user(cpu_to_vhost16(vq, vq->used_flags), &vq->used->flags) < 0)
Jason Wang2723fea2011-06-21 18:04:38 +08001106 return -EFAULT;
1107 if (unlikely(vq->log_used)) {
1108 /* Make sure the flag is seen before log. */
1109 smp_wmb();
1110 /* Log used flag write. */
1111 used = &vq->used->flags;
1112 log_write(vq->log_base, vq->log_addr +
1113 (used - (void __user *)vq->used),
1114 sizeof vq->used->flags);
1115 if (vq->log_ctx)
1116 eventfd_signal(vq->log_ctx, 1);
1117 }
1118 return 0;
1119}
1120
1121static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
1122{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001123 if (__put_user(cpu_to_vhost16(vq, vq->avail_idx), vhost_avail_event(vq)))
Jason Wang2723fea2011-06-21 18:04:38 +08001124 return -EFAULT;
1125 if (unlikely(vq->log_used)) {
1126 void __user *used;
1127 /* Make sure the event is seen before log. */
1128 smp_wmb();
1129 /* Log avail event write */
1130 used = vhost_avail_event(vq);
1131 log_write(vq->log_base, vq->log_addr +
1132 (used - (void __user *)vq->used),
1133 sizeof *vhost_avail_event(vq));
1134 if (vq->log_ctx)
1135 eventfd_signal(vq->log_ctx, 1);
1136 }
1137 return 0;
1138}
1139
1140int vhost_init_used(struct vhost_virtqueue *vq)
1141{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001142 __virtio16 last_used_idx;
Jason Wang2723fea2011-06-21 18:04:38 +08001143 int r;
Greg Kurz2751c982015-04-24 14:27:24 +02001144 if (!vq->private_data) {
1145 vq->is_le = virtio_legacy_is_little_endian();
Jason Wang2723fea2011-06-21 18:04:38 +08001146 return 0;
Greg Kurz2751c982015-04-24 14:27:24 +02001147 }
1148
1149 vhost_init_is_le(vq);
Jason Wang2723fea2011-06-21 18:04:38 +08001150
1151 r = vhost_update_used_flags(vq);
1152 if (r)
1153 return r;
1154 vq->signalled_used_valid = false;
Michael S. Tsirkin64f7f052014-12-01 17:39:39 +02001155 if (!access_ok(VERIFY_READ, &vq->used->idx, sizeof vq->used->idx))
1156 return -EFAULT;
1157 r = __get_user(last_used_idx, &vq->used->idx);
1158 if (r)
1159 return r;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001160 vq->last_used_idx = vhost16_to_cpu(vq, last_used_idx);
Michael S. Tsirkin64f7f052014-12-01 17:39:39 +02001161 return 0;
Jason Wang2723fea2011-06-21 18:04:38 +08001162}
Asias He6ac1afb2013-05-06 16:38:21 +08001163EXPORT_SYMBOL_GPL(vhost_init_used);
Jason Wang2723fea2011-06-21 18:04:38 +08001164
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001165static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001166 struct iovec iov[], int iov_size)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001167{
1168 const struct vhost_memory_region *reg;
1169 struct vhost_memory *mem;
1170 struct iovec *_iov;
1171 u64 s = 0;
1172 int ret = 0;
1173
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001174 mem = vq->memory;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001175 while ((u64)len > s) {
1176 u64 size;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001177 if (unlikely(ret >= iov_size)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001178 ret = -ENOBUFS;
1179 break;
1180 }
1181 reg = find_region(mem, addr, len);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001182 if (unlikely(!reg)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001183 ret = -EFAULT;
1184 break;
1185 }
1186 _iov = iov + ret;
1187 size = reg->memory_size - addr + reg->guest_phys_addr;
Michael S. Tsirkinbd971202012-11-26 05:57:27 +00001188 _iov->iov_len = min((u64)len - s, size);
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001189 _iov->iov_base = (void __user *)(unsigned long)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001190 (reg->userspace_addr + addr - reg->guest_phys_addr);
1191 s += size;
1192 addr += size;
1193 ++ret;
1194 }
1195
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001196 return ret;
1197}
1198
1199/* Each buffer in the virtqueues is actually a chain of descriptors. This
1200 * function returns the next descriptor in the chain,
1201 * or -1U if we're at the end. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001202static unsigned next_desc(struct vhost_virtqueue *vq, struct vring_desc *desc)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001203{
1204 unsigned int next;
1205
1206 /* If this descriptor says it doesn't chain, we're done. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001207 if (!(desc->flags & cpu_to_vhost16(vq, VRING_DESC_F_NEXT)))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001208 return -1U;
1209
1210 /* Check they're not leading us off end of descriptors. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001211 next = vhost16_to_cpu(vq, desc->next);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001212 /* Make sure compiler knows to grab that: we don't want it changing! */
1213 /* We will use the result as an index in an array, so most
1214 * architectures only need a compiler barrier here. */
1215 read_barrier_depends();
1216
1217 return next;
1218}
1219
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001220static int get_indirect(struct vhost_virtqueue *vq,
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001221 struct iovec iov[], unsigned int iov_size,
1222 unsigned int *out_num, unsigned int *in_num,
1223 struct vhost_log *log, unsigned int *log_num,
1224 struct vring_desc *indirect)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001225{
1226 struct vring_desc desc;
1227 unsigned int i = 0, count, found = 0;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001228 u32 len = vhost32_to_cpu(vq, indirect->len);
Al Viroaad9a1c2014-12-10 14:49:01 -05001229 struct iov_iter from;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001230 int ret;
1231
1232 /* Sanity check */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001233 if (unlikely(len % sizeof desc)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001234 vq_err(vq, "Invalid length in indirect descriptor: "
1235 "len 0x%llx not multiple of 0x%zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001236 (unsigned long long)len,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001237 sizeof desc);
1238 return -EINVAL;
1239 }
1240
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001241 ret = translate_desc(vq, vhost64_to_cpu(vq, indirect->addr), len, vq->indirect,
Jason Wange0e9b402010-09-14 23:53:05 +08001242 UIO_MAXIOV);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001243 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001244 vq_err(vq, "Translation failure %d in indirect.\n", ret);
1245 return ret;
1246 }
Al Viroaad9a1c2014-12-10 14:49:01 -05001247 iov_iter_init(&from, READ, vq->indirect, ret, len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001248
1249 /* We will use the result as an address to read from, so most
1250 * architectures only need a compiler barrier here. */
1251 read_barrier_depends();
1252
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001253 count = len / sizeof desc;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001254 /* Buffers are chained via a 16 bit next field, so
1255 * we can have at most 2^16 of these. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001256 if (unlikely(count > USHRT_MAX + 1)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001257 vq_err(vq, "Indirect buffer length too big: %d\n",
1258 indirect->len);
1259 return -E2BIG;
1260 }
1261
1262 do {
1263 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001264 if (unlikely(++found > count)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001265 vq_err(vq, "Loop detected: last one at %u "
1266 "indirect size %u\n",
1267 i, count);
1268 return -EINVAL;
1269 }
Al Viroaad9a1c2014-12-10 14:49:01 -05001270 if (unlikely(copy_from_iter(&desc, sizeof(desc), &from) !=
1271 sizeof(desc))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001272 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001273 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001274 return -EINVAL;
1275 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001276 if (unlikely(desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001277 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001278 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001279 return -EINVAL;
1280 }
1281
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001282 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
1283 vhost32_to_cpu(vq, desc.len), iov + iov_count,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001284 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001285 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001286 vq_err(vq, "Translation failure %d indirect idx %d\n",
1287 ret, i);
1288 return ret;
1289 }
1290 /* If this is an input descriptor, increment that count. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001291 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001292 *in_num += ret;
1293 if (unlikely(log)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001294 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
1295 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001296 ++*log_num;
1297 }
1298 } else {
1299 /* If it's an output descriptor, they're all supposed
1300 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001301 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001302 vq_err(vq, "Indirect descriptor "
1303 "has out after in: idx %d\n", i);
1304 return -EINVAL;
1305 }
1306 *out_num += ret;
1307 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001308 } while ((i = next_desc(vq, &desc)) != -1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001309 return 0;
1310}
1311
1312/* This looks in the virtqueue and for the first available buffer, and converts
1313 * it to an iovec for convenient access. Since descriptors consist of some
1314 * number of output then some number of input descriptors, it's actually two
1315 * iovecs, but we pack them into one and note how many of each there were.
1316 *
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001317 * This function returns the descriptor number found, or vq->num (which is
1318 * never a valid descriptor number) if none was found. A negative code is
1319 * returned on error. */
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001320int vhost_get_vq_desc(struct vhost_virtqueue *vq,
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001321 struct iovec iov[], unsigned int iov_size,
1322 unsigned int *out_num, unsigned int *in_num,
1323 struct vhost_log *log, unsigned int *log_num)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001324{
1325 struct vring_desc desc;
1326 unsigned int i, head, found = 0;
1327 u16 last_avail_idx;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001328 __virtio16 avail_idx;
1329 __virtio16 ring_head;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001330 int ret;
1331
1332 /* Check it isn't doing very strange things with descriptor numbers. */
1333 last_avail_idx = vq->last_avail_idx;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001334 if (unlikely(__get_user(avail_idx, &vq->avail->idx))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001335 vq_err(vq, "Failed to access avail idx at %p\n",
1336 &vq->avail->idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001337 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001338 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001339 vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001340
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001341 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001342 vq_err(vq, "Guest moved used index from %u to %u",
1343 last_avail_idx, vq->avail_idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001344 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001345 }
1346
1347 /* If there's nothing new since last we looked, return invalid. */
1348 if (vq->avail_idx == last_avail_idx)
1349 return vq->num;
1350
1351 /* Only get avail ring entries after they have been exposed by guest. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001352 smp_rmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001353
1354 /* Grab the next descriptor number they're advertising, and increment
1355 * the index we've seen. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001356 if (unlikely(__get_user(ring_head,
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001357 &vq->avail->ring[last_avail_idx % vq->num]))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001358 vq_err(vq, "Failed to read head: idx %d address %p\n",
1359 last_avail_idx,
1360 &vq->avail->ring[last_avail_idx % vq->num]);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001361 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001362 }
1363
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001364 head = vhost16_to_cpu(vq, ring_head);
1365
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001366 /* If their number is silly, that's an error. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001367 if (unlikely(head >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001368 vq_err(vq, "Guest says index %u > %u is available",
1369 head, vq->num);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001370 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001371 }
1372
1373 /* When we start there are none of either input nor output. */
1374 *out_num = *in_num = 0;
1375 if (unlikely(log))
1376 *log_num = 0;
1377
1378 i = head;
1379 do {
1380 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001381 if (unlikely(i >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001382 vq_err(vq, "Desc index is %u > %u, head = %u",
1383 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001384 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001385 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001386 if (unlikely(++found > vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001387 vq_err(vq, "Loop detected: last one at %u "
1388 "vq size %u head %u\n",
1389 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001390 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001391 }
Michael S. Tsirkinfcc042a2011-03-06 13:33:49 +02001392 ret = __copy_from_user(&desc, vq->desc + i, sizeof desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001393 if (unlikely(ret)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001394 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
1395 i, vq->desc + i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001396 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001397 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001398 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT)) {
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001399 ret = get_indirect(vq, iov, iov_size,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001400 out_num, in_num,
1401 log, log_num, &desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001402 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001403 vq_err(vq, "Failure detected "
1404 "in indirect descriptor at idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001405 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001406 }
1407 continue;
1408 }
1409
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001410 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
1411 vhost32_to_cpu(vq, desc.len), iov + iov_count,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001412 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001413 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001414 vq_err(vq, "Translation failure %d descriptor idx %d\n",
1415 ret, i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001416 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001417 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001418 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001419 /* If this is an input descriptor,
1420 * increment that count. */
1421 *in_num += ret;
1422 if (unlikely(log)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001423 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
1424 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001425 ++*log_num;
1426 }
1427 } else {
1428 /* If it's an output descriptor, they're all supposed
1429 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001430 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001431 vq_err(vq, "Descriptor has out after in: "
1432 "idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001433 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001434 }
1435 *out_num += ret;
1436 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001437 } while ((i = next_desc(vq, &desc)) != -1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001438
1439 /* On success, increment avail index. */
1440 vq->last_avail_idx++;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001441
1442 /* Assume notifications from guest are disabled at this point,
1443 * if they aren't we would need to update avail_event index. */
1444 BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001445 return head;
1446}
Asias He6ac1afb2013-05-06 16:38:21 +08001447EXPORT_SYMBOL_GPL(vhost_get_vq_desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001448
1449/* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
David Stevens8dd014a2010-07-27 18:52:21 +03001450void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001451{
David Stevens8dd014a2010-07-27 18:52:21 +03001452 vq->last_avail_idx -= n;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001453}
Asias He6ac1afb2013-05-06 16:38:21 +08001454EXPORT_SYMBOL_GPL(vhost_discard_vq_desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001455
1456/* After we've used one of their buffers, we tell them about it. We'll then
1457 * want to notify the guest, using eventfd. */
1458int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
1459{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001460 struct vring_used_elem heads = {
1461 cpu_to_vhost32(vq, head),
1462 cpu_to_vhost32(vq, len)
1463 };
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001464
Jason Wangc49e4e52013-09-02 16:40:58 +08001465 return vhost_add_used_n(vq, &heads, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001466}
Asias He6ac1afb2013-05-06 16:38:21 +08001467EXPORT_SYMBOL_GPL(vhost_add_used);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001468
David Stevens8dd014a2010-07-27 18:52:21 +03001469static int __vhost_add_used_n(struct vhost_virtqueue *vq,
1470 struct vring_used_elem *heads,
1471 unsigned count)
1472{
1473 struct vring_used_elem __user *used;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001474 u16 old, new;
David Stevens8dd014a2010-07-27 18:52:21 +03001475 int start;
1476
1477 start = vq->last_used_idx % vq->num;
1478 used = vq->used->ring + start;
Jason Wangc49e4e52013-09-02 16:40:58 +08001479 if (count == 1) {
1480 if (__put_user(heads[0].id, &used->id)) {
1481 vq_err(vq, "Failed to write used id");
1482 return -EFAULT;
1483 }
1484 if (__put_user(heads[0].len, &used->len)) {
1485 vq_err(vq, "Failed to write used len");
1486 return -EFAULT;
1487 }
1488 } else if (__copy_to_user(used, heads, count * sizeof *used)) {
David Stevens8dd014a2010-07-27 18:52:21 +03001489 vq_err(vq, "Failed to write used");
1490 return -EFAULT;
1491 }
1492 if (unlikely(vq->log_used)) {
1493 /* Make sure data is seen before log. */
1494 smp_wmb();
1495 /* Log used ring entry write. */
1496 log_write(vq->log_base,
1497 vq->log_addr +
1498 ((void __user *)used - (void __user *)vq->used),
1499 count * sizeof *used);
1500 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001501 old = vq->last_used_idx;
1502 new = (vq->last_used_idx += count);
1503 /* If the driver never bothers to signal in a very long while,
1504 * used index might wrap around. If that happens, invalidate
1505 * signalled_used index we stored. TODO: make sure driver
1506 * signals at least once in 2^16 and remove this. */
1507 if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old)))
1508 vq->signalled_used_valid = false;
David Stevens8dd014a2010-07-27 18:52:21 +03001509 return 0;
1510}
1511
1512/* After we've used one of their buffers, we tell them about it. We'll then
1513 * want to notify the guest, using eventfd. */
1514int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
1515 unsigned count)
1516{
1517 int start, n, r;
1518
1519 start = vq->last_used_idx % vq->num;
1520 n = vq->num - start;
1521 if (n < count) {
1522 r = __vhost_add_used_n(vq, heads, n);
1523 if (r < 0)
1524 return r;
1525 heads += n;
1526 count -= n;
1527 }
1528 r = __vhost_add_used_n(vq, heads, count);
1529
1530 /* Make sure buffer is written before we update index. */
1531 smp_wmb();
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001532 if (__put_user(cpu_to_vhost16(vq, vq->last_used_idx), &vq->used->idx)) {
David Stevens8dd014a2010-07-27 18:52:21 +03001533 vq_err(vq, "Failed to increment used idx");
1534 return -EFAULT;
1535 }
1536 if (unlikely(vq->log_used)) {
1537 /* Log used index update. */
1538 log_write(vq->log_base,
1539 vq->log_addr + offsetof(struct vring_used, idx),
1540 sizeof vq->used->idx);
1541 if (vq->log_ctx)
1542 eventfd_signal(vq->log_ctx, 1);
1543 }
1544 return r;
1545}
Asias He6ac1afb2013-05-06 16:38:21 +08001546EXPORT_SYMBOL_GPL(vhost_add_used_n);
David Stevens8dd014a2010-07-27 18:52:21 +03001547
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001548static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001549{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001550 __u16 old, new;
1551 __virtio16 event;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001552 bool v;
Michael S. Tsirkin0d499352010-05-11 19:44:17 +03001553 /* Flush out used index updates. This is paired
1554 * with the barrier that the Guest executes when enabling
1555 * interrupts. */
1556 smp_mb();
1557
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001558 if (vhost_has_feature(vq, VIRTIO_F_NOTIFY_ON_EMPTY) &&
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001559 unlikely(vq->avail_idx == vq->last_avail_idx))
1560 return true;
1561
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001562 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001563 __virtio16 flags;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001564 if (__get_user(flags, &vq->avail->flags)) {
1565 vq_err(vq, "Failed to get flags");
1566 return true;
1567 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001568 return !(flags & cpu_to_vhost16(vq, VRING_AVAIL_F_NO_INTERRUPT));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001569 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001570 old = vq->signalled_used;
1571 v = vq->signalled_used_valid;
1572 new = vq->signalled_used = vq->last_used_idx;
1573 vq->signalled_used_valid = true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001574
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001575 if (unlikely(!v))
1576 return true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001577
Michael S. Tsirkin64f7f052014-12-01 17:39:39 +02001578 if (__get_user(event, vhost_used_event(vq))) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001579 vq_err(vq, "Failed to get used event idx");
1580 return true;
1581 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001582 return vring_need_event(vhost16_to_cpu(vq, event), new, old);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001583}
1584
1585/* This actually signals the guest, using eventfd. */
1586void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
1587{
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001588 /* Signal the Guest tell them we used something up. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001589 if (vq->call_ctx && vhost_notify(dev, vq))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001590 eventfd_signal(vq->call_ctx, 1);
1591}
Asias He6ac1afb2013-05-06 16:38:21 +08001592EXPORT_SYMBOL_GPL(vhost_signal);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001593
1594/* And here's the combo meal deal. Supersize me! */
1595void vhost_add_used_and_signal(struct vhost_dev *dev,
1596 struct vhost_virtqueue *vq,
1597 unsigned int head, int len)
1598{
1599 vhost_add_used(vq, head, len);
1600 vhost_signal(dev, vq);
1601}
Asias He6ac1afb2013-05-06 16:38:21 +08001602EXPORT_SYMBOL_GPL(vhost_add_used_and_signal);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001603
David Stevens8dd014a2010-07-27 18:52:21 +03001604/* multi-buffer version of vhost_add_used_and_signal */
1605void vhost_add_used_and_signal_n(struct vhost_dev *dev,
1606 struct vhost_virtqueue *vq,
1607 struct vring_used_elem *heads, unsigned count)
1608{
1609 vhost_add_used_n(vq, heads, count);
1610 vhost_signal(dev, vq);
1611}
Asias He6ac1afb2013-05-06 16:38:21 +08001612EXPORT_SYMBOL_GPL(vhost_add_used_and_signal_n);
David Stevens8dd014a2010-07-27 18:52:21 +03001613
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001614/* OK, now we need to know about added descriptors. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001615bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001616{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001617 __virtio16 avail_idx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001618 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301619
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001620 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
1621 return false;
1622 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001623 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08001624 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001625 if (r) {
1626 vq_err(vq, "Failed to enable notification at %p: %d\n",
1627 &vq->used->flags, r);
1628 return false;
1629 }
1630 } else {
Jason Wang2723fea2011-06-21 18:04:38 +08001631 r = vhost_update_avail_event(vq, vq->avail_idx);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001632 if (r) {
1633 vq_err(vq, "Failed to update avail event index at %p: %d\n",
1634 vhost_avail_event(vq), r);
1635 return false;
1636 }
1637 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001638 /* They could have slipped one in as we were doing that: make
1639 * sure it's written, then check again. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001640 smp_mb();
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001641 r = __get_user(avail_idx, &vq->avail->idx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001642 if (r) {
1643 vq_err(vq, "Failed to check avail idx at %p: %d\n",
1644 &vq->avail->idx, r);
1645 return false;
1646 }
1647
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001648 return vhost16_to_cpu(vq, avail_idx) != vq->avail_idx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001649}
Asias He6ac1afb2013-05-06 16:38:21 +08001650EXPORT_SYMBOL_GPL(vhost_enable_notify);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001651
1652/* We don't need to be notified again. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001653void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001654{
1655 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301656
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001657 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
1658 return;
1659 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001660 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08001661 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001662 if (r)
1663 vq_err(vq, "Failed to enable notification at %p: %d\n",
1664 &vq->used->flags, r);
1665 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001666}
Asias He6ac1afb2013-05-06 16:38:21 +08001667EXPORT_SYMBOL_GPL(vhost_disable_notify);
1668
1669static int __init vhost_init(void)
1670{
1671 return 0;
1672}
1673
1674static void __exit vhost_exit(void)
1675{
1676}
1677
1678module_init(vhost_init);
1679module_exit(vhost_exit);
1680
1681MODULE_VERSION("0.0.1");
1682MODULE_LICENSE("GPL v2");
1683MODULE_AUTHOR("Michael S. Tsirkin");
1684MODULE_DESCRIPTION("Host kernel accelerator for virtio");