blob: 95bdb90fd7f06a13a99e2d37a2c97aabc4545c05 [file] [log] [blame]
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001/* Copyright (C) 2009 Red Hat, Inc.
2 * Copyright (C) 2006 Rusty Russell IBM Corporation
3 *
4 * Author: Michael S. Tsirkin <mst@redhat.com>
5 *
6 * Inspiration, some code, and most witty comments come from
Rob Landley61516582011-05-06 09:27:36 -07007 * Documentation/virtual/lguest/lguest.c, by Rusty Russell
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00008 *
9 * This work is licensed under the terms of the GNU GPL, version 2.
10 *
11 * Generic code for virtio server in host kernel.
12 */
13
14#include <linux/eventfd.h>
15#include <linux/vhost.h>
Asias He35596b22013-08-19 09:23:19 +080016#include <linux/uio.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000017#include <linux/mm.h>
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +020018#include <linux/mmu_context.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000019#include <linux/miscdevice.h>
20#include <linux/mutex.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000021#include <linux/poll.h>
22#include <linux/file.h>
23#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Igor Mammedov4de72552015-07-01 11:07:09 +020025#include <linux/vmalloc.h>
Tejun Heoc23f34452010-06-02 20:40:00 +020026#include <linux/kthread.h>
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +030027#include <linux/cgroup.h>
Asias He6ac1afb2013-05-06 16:38:21 +080028#include <linux/module.h>
Igor Mammedovbcfeaca2015-06-16 18:33:35 +020029#include <linux/sort.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000030
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000031#include "vhost.h"
32
Igor Mammedovc9ce42f2015-07-02 15:08:11 +020033static ushort max_mem_regions = 64;
34module_param(max_mem_regions, ushort, 0444);
35MODULE_PARM_DESC(max_mem_regions,
36 "Maximum number of memory regions in memory map. (default: 64)");
37
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000038enum {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000039 VHOST_MEMORY_F_LOG = 0x1,
40};
41
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +030042#define vhost_used_event(vq) ((__virtio16 __user *)&vq->avail->ring[vq->num])
43#define vhost_avail_event(vq) ((__virtio16 __user *)&vq->used->ring[vq->num])
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +030044
Greg Kurz2751c982015-04-24 14:27:24 +020045#ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
46static void vhost_vq_reset_user_be(struct vhost_virtqueue *vq)
47{
48 vq->user_be = !virtio_legacy_is_little_endian();
49}
50
51static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
52{
53 struct vhost_vring_state s;
54
55 if (vq->private_data)
56 return -EBUSY;
57
58 if (copy_from_user(&s, argp, sizeof(s)))
59 return -EFAULT;
60
61 if (s.num != VHOST_VRING_LITTLE_ENDIAN &&
62 s.num != VHOST_VRING_BIG_ENDIAN)
63 return -EINVAL;
64
65 vq->user_be = s.num;
66
67 return 0;
68}
69
70static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
71 int __user *argp)
72{
73 struct vhost_vring_state s = {
74 .index = idx,
75 .num = vq->user_be
76 };
77
78 if (copy_to_user(argp, &s, sizeof(s)))
79 return -EFAULT;
80
81 return 0;
82}
83
84static void vhost_init_is_le(struct vhost_virtqueue *vq)
85{
86 /* Note for legacy virtio: user_be is initialized at reset time
87 * according to the host endianness. If userspace does not set an
88 * explicit endianness, the default behavior is native endian, as
89 * expected by legacy virtio.
90 */
91 vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) || !vq->user_be;
92}
93#else
94static void vhost_vq_reset_user_be(struct vhost_virtqueue *vq)
95{
96}
97
98static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
99{
100 return -ENOIOCTLCMD;
101}
102
103static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
104 int __user *argp)
105{
106 return -ENOIOCTLCMD;
107}
108
109static void vhost_init_is_le(struct vhost_virtqueue *vq)
110{
111 if (vhost_has_feature(vq, VIRTIO_F_VERSION_1))
112 vq->is_le = true;
113}
114#endif /* CONFIG_VHOST_CROSS_ENDIAN_LEGACY */
115
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000116static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
117 poll_table *pt)
118{
119 struct vhost_poll *poll;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000120
Krishna Kumard47effe2011-03-01 17:06:37 +0530121 poll = container_of(pt, struct vhost_poll, table);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000122 poll->wqh = wqh;
123 add_wait_queue(wqh, &poll->wait);
124}
125
126static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync,
127 void *key)
128{
Tejun Heoc23f34452010-06-02 20:40:00 +0200129 struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
130
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000131 if (!((unsigned long)key & poll->mask))
132 return 0;
133
Tejun Heoc23f34452010-06-02 20:40:00 +0200134 vhost_poll_queue(poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000135 return 0;
136}
137
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000138void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000139{
Tejun Heoc23f34452010-06-02 20:40:00 +0200140 INIT_LIST_HEAD(&work->node);
141 work->fn = fn;
142 init_waitqueue_head(&work->done);
143 work->flushing = 0;
144 work->queue_seq = work->done_seq = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000145}
Asias He6ac1afb2013-05-06 16:38:21 +0800146EXPORT_SYMBOL_GPL(vhost_work_init);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000147
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300148/* Init poll structure */
149void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
150 unsigned long mask, struct vhost_dev *dev)
151{
152 init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
153 init_poll_funcptr(&poll->table, vhost_poll_func);
154 poll->mask = mask;
155 poll->dev = dev;
Jason Wang2b8b3282013-01-28 01:05:18 +0000156 poll->wqh = NULL;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300157
158 vhost_work_init(&poll->work, fn);
159}
Asias He6ac1afb2013-05-06 16:38:21 +0800160EXPORT_SYMBOL_GPL(vhost_poll_init);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300161
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000162/* Start polling a file. We add ourselves to file's wait queue. The caller must
163 * keep a reference to a file until after vhost_poll_stop is called. */
Jason Wang2b8b3282013-01-28 01:05:18 +0000164int vhost_poll_start(struct vhost_poll *poll, struct file *file)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000165{
166 unsigned long mask;
Jason Wang2b8b3282013-01-28 01:05:18 +0000167 int ret = 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530168
Jason Wang70181d512013-04-10 20:50:48 +0000169 if (poll->wqh)
170 return 0;
171
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000172 mask = file->f_op->poll(file, &poll->table);
173 if (mask)
174 vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
Jason Wang2b8b3282013-01-28 01:05:18 +0000175 if (mask & POLLERR) {
176 if (poll->wqh)
177 remove_wait_queue(poll->wqh, &poll->wait);
178 ret = -EINVAL;
179 }
180
181 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000182}
Asias He6ac1afb2013-05-06 16:38:21 +0800183EXPORT_SYMBOL_GPL(vhost_poll_start);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000184
185/* Stop polling a file. After this function returns, it becomes safe to drop the
186 * file reference. You must also flush afterwards. */
187void vhost_poll_stop(struct vhost_poll *poll)
188{
Jason Wang2b8b3282013-01-28 01:05:18 +0000189 if (poll->wqh) {
190 remove_wait_queue(poll->wqh, &poll->wait);
191 poll->wqh = NULL;
192 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000193}
Asias He6ac1afb2013-05-06 16:38:21 +0800194EXPORT_SYMBOL_GPL(vhost_poll_stop);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000195
Michael S. Tsirkin0174b0c2011-01-10 10:03:20 +0200196static bool vhost_work_seq_done(struct vhost_dev *dev, struct vhost_work *work,
197 unsigned seq)
198{
199 int left;
Krishna Kumard47effe2011-03-01 17:06:37 +0530200
Michael S. Tsirkin0174b0c2011-01-10 10:03:20 +0200201 spin_lock_irq(&dev->work_lock);
202 left = seq - work->done_seq;
203 spin_unlock_irq(&dev->work_lock);
204 return left <= 0;
205}
206
Asias He6ac1afb2013-05-06 16:38:21 +0800207void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000208{
Tejun Heoc23f34452010-06-02 20:40:00 +0200209 unsigned seq;
Tejun Heoc23f34452010-06-02 20:40:00 +0200210 int flushing;
211
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300212 spin_lock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200213 seq = work->queue_seq;
214 work->flushing++;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300215 spin_unlock_irq(&dev->work_lock);
Michael S. Tsirkin0174b0c2011-01-10 10:03:20 +0200216 wait_event(work->done, vhost_work_seq_done(dev, work, seq));
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300217 spin_lock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200218 flushing = --work->flushing;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300219 spin_unlock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200220 BUG_ON(flushing < 0);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000221}
Asias He6ac1afb2013-05-06 16:38:21 +0800222EXPORT_SYMBOL_GPL(vhost_work_flush);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000223
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300224/* Flush any work that has been scheduled. When calling this, don't hold any
225 * locks that are also used by the callback. */
226void vhost_poll_flush(struct vhost_poll *poll)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000227{
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300228 vhost_work_flush(poll->dev, &poll->work);
229}
Asias He6ac1afb2013-05-06 16:38:21 +0800230EXPORT_SYMBOL_GPL(vhost_poll_flush);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300231
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000232void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300233{
Tejun Heoc23f34452010-06-02 20:40:00 +0200234 unsigned long flags;
235
236 spin_lock_irqsave(&dev->work_lock, flags);
237 if (list_empty(&work->node)) {
238 list_add_tail(&work->node, &dev->work_list);
239 work->queue_seq++;
Qin Chuanyuac9fde22013-06-07 21:50:16 +0800240 spin_unlock_irqrestore(&dev->work_lock, flags);
Tejun Heoc23f34452010-06-02 20:40:00 +0200241 wake_up_process(dev->worker);
Qin Chuanyuac9fde22013-06-07 21:50:16 +0800242 } else {
243 spin_unlock_irqrestore(&dev->work_lock, flags);
Tejun Heoc23f34452010-06-02 20:40:00 +0200244 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000245}
Asias He6ac1afb2013-05-06 16:38:21 +0800246EXPORT_SYMBOL_GPL(vhost_work_queue);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000247
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300248void vhost_poll_queue(struct vhost_poll *poll)
249{
250 vhost_work_queue(poll->dev, &poll->work);
251}
Asias He6ac1afb2013-05-06 16:38:21 +0800252EXPORT_SYMBOL_GPL(vhost_poll_queue);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300253
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000254static void vhost_vq_reset(struct vhost_dev *dev,
255 struct vhost_virtqueue *vq)
256{
257 vq->num = 1;
258 vq->desc = NULL;
259 vq->avail = NULL;
260 vq->used = NULL;
261 vq->last_avail_idx = 0;
262 vq->avail_idx = 0;
263 vq->last_used_idx = 0;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300264 vq->signalled_used = 0;
265 vq->signalled_used_valid = false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000266 vq->used_flags = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000267 vq->log_used = false;
268 vq->log_addr = -1ull;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000269 vq->private_data = NULL;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300270 vq->acked_features = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000271 vq->log_base = NULL;
272 vq->error_ctx = NULL;
273 vq->error = NULL;
274 vq->kick = NULL;
275 vq->call_ctx = NULL;
276 vq->call = NULL;
Michael S. Tsirkin73a99f02010-02-23 11:23:45 +0200277 vq->log_ctx = NULL;
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300278 vq->memory = NULL;
Greg Kurz2751c982015-04-24 14:27:24 +0200279 vq->is_le = virtio_legacy_is_little_endian();
280 vhost_vq_reset_user_be(vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000281}
282
Tejun Heoc23f34452010-06-02 20:40:00 +0200283static int vhost_worker(void *data)
284{
285 struct vhost_dev *dev = data;
286 struct vhost_work *work = NULL;
287 unsigned uninitialized_var(seq);
Jens Freimannd7ffde32012-06-26 00:59:58 +0000288 mm_segment_t oldfs = get_fs();
Tejun Heoc23f34452010-06-02 20:40:00 +0200289
Jens Freimannd7ffde32012-06-26 00:59:58 +0000290 set_fs(USER_DS);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200291 use_mm(dev->mm);
292
Tejun Heoc23f34452010-06-02 20:40:00 +0200293 for (;;) {
294 /* mb paired w/ kthread_stop */
295 set_current_state(TASK_INTERRUPTIBLE);
296
297 spin_lock_irq(&dev->work_lock);
298 if (work) {
299 work->done_seq = seq;
300 if (work->flushing)
301 wake_up_all(&work->done);
302 }
303
304 if (kthread_should_stop()) {
305 spin_unlock_irq(&dev->work_lock);
306 __set_current_state(TASK_RUNNING);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200307 break;
Tejun Heoc23f34452010-06-02 20:40:00 +0200308 }
309 if (!list_empty(&dev->work_list)) {
310 work = list_first_entry(&dev->work_list,
311 struct vhost_work, node);
312 list_del_init(&work->node);
313 seq = work->queue_seq;
314 } else
315 work = NULL;
316 spin_unlock_irq(&dev->work_lock);
317
318 if (work) {
319 __set_current_state(TASK_RUNNING);
320 work->fn(work);
Nadav Har'Eld550dda2012-02-27 15:07:29 +0200321 if (need_resched())
322 schedule();
Tejun Heoc23f34452010-06-02 20:40:00 +0200323 } else
324 schedule();
325
326 }
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200327 unuse_mm(dev->mm);
Jens Freimannd7ffde32012-06-26 00:59:58 +0000328 set_fs(oldfs);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200329 return 0;
Tejun Heoc23f34452010-06-02 20:40:00 +0200330}
331
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000332static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
333{
334 kfree(vq->indirect);
335 vq->indirect = NULL;
336 kfree(vq->log);
337 vq->log = NULL;
338 kfree(vq->heads);
339 vq->heads = NULL;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000340}
341
Jason Wange0e9b402010-09-14 23:53:05 +0800342/* Helper to allocate iovec buffers for all vqs. */
343static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
344{
Asias He6d5e6aa2013-05-06 16:38:23 +0800345 struct vhost_virtqueue *vq;
Jason Wange0e9b402010-09-14 23:53:05 +0800346 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530347
Jason Wange0e9b402010-09-14 23:53:05 +0800348 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800349 vq = dev->vqs[i];
350 vq->indirect = kmalloc(sizeof *vq->indirect * UIO_MAXIOV,
351 GFP_KERNEL);
352 vq->log = kmalloc(sizeof *vq->log * UIO_MAXIOV, GFP_KERNEL);
353 vq->heads = kmalloc(sizeof *vq->heads * UIO_MAXIOV, GFP_KERNEL);
354 if (!vq->indirect || !vq->log || !vq->heads)
Jason Wange0e9b402010-09-14 23:53:05 +0800355 goto err_nomem;
356 }
357 return 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530358
Jason Wange0e9b402010-09-14 23:53:05 +0800359err_nomem:
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000360 for (; i >= 0; --i)
Asias He3ab2e422013-04-27 11:16:48 +0800361 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800362 return -ENOMEM;
363}
364
365static void vhost_dev_free_iovecs(struct vhost_dev *dev)
366{
367 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530368
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000369 for (i = 0; i < dev->nvqs; ++i)
Asias He3ab2e422013-04-27 11:16:48 +0800370 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800371}
372
Zhi Yong Wu59566b6e2013-12-07 04:13:03 +0800373void vhost_dev_init(struct vhost_dev *dev,
Asias He3ab2e422013-04-27 11:16:48 +0800374 struct vhost_virtqueue **vqs, int nvqs)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000375{
Asias He6d5e6aa2013-05-06 16:38:23 +0800376 struct vhost_virtqueue *vq;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000377 int i;
Tejun Heoc23f34452010-06-02 20:40:00 +0200378
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000379 dev->vqs = vqs;
380 dev->nvqs = nvqs;
381 mutex_init(&dev->mutex);
382 dev->log_ctx = NULL;
383 dev->log_file = NULL;
384 dev->memory = NULL;
385 dev->mm = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200386 spin_lock_init(&dev->work_lock);
387 INIT_LIST_HEAD(&dev->work_list);
388 dev->worker = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000389
390 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800391 vq = dev->vqs[i];
392 vq->log = NULL;
393 vq->indirect = NULL;
394 vq->heads = NULL;
395 vq->dev = dev;
396 mutex_init(&vq->mutex);
397 vhost_vq_reset(dev, vq);
398 if (vq->handle_kick)
399 vhost_poll_init(&vq->poll, vq->handle_kick,
400 POLLIN, dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000401 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000402}
Asias He6ac1afb2013-05-06 16:38:21 +0800403EXPORT_SYMBOL_GPL(vhost_dev_init);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000404
405/* Caller should have device mutex */
406long vhost_dev_check_owner(struct vhost_dev *dev)
407{
408 /* Are you the owner? If not, I don't think you mean to do that */
409 return dev->mm == current->mm ? 0 : -EPERM;
410}
Asias He6ac1afb2013-05-06 16:38:21 +0800411EXPORT_SYMBOL_GPL(vhost_dev_check_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000412
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300413struct vhost_attach_cgroups_struct {
Krishna Kumard47effe2011-03-01 17:06:37 +0530414 struct vhost_work work;
415 struct task_struct *owner;
416 int ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300417};
418
419static void vhost_attach_cgroups_work(struct vhost_work *work)
420{
Krishna Kumard47effe2011-03-01 17:06:37 +0530421 struct vhost_attach_cgroups_struct *s;
422
423 s = container_of(work, struct vhost_attach_cgroups_struct, work);
424 s->ret = cgroup_attach_task_all(s->owner, current);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300425}
426
427static int vhost_attach_cgroups(struct vhost_dev *dev)
428{
Krishna Kumard47effe2011-03-01 17:06:37 +0530429 struct vhost_attach_cgroups_struct attach;
430
431 attach.owner = current;
432 vhost_work_init(&attach.work, vhost_attach_cgroups_work);
433 vhost_work_queue(dev, &attach.work);
434 vhost_work_flush(dev, &attach.work);
435 return attach.ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300436}
437
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000438/* Caller should have device mutex */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300439bool vhost_dev_has_owner(struct vhost_dev *dev)
440{
441 return dev->mm;
442}
Asias He6ac1afb2013-05-06 16:38:21 +0800443EXPORT_SYMBOL_GPL(vhost_dev_has_owner);
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300444
445/* Caller should have device mutex */
Asias He54db63c2013-05-06 11:15:59 +0800446long vhost_dev_set_owner(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000447{
Tejun Heoc23f34452010-06-02 20:40:00 +0200448 struct task_struct *worker;
449 int err;
Krishna Kumard47effe2011-03-01 17:06:37 +0530450
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000451 /* Is there an owner already? */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300452 if (vhost_dev_has_owner(dev)) {
Tejun Heoc23f34452010-06-02 20:40:00 +0200453 err = -EBUSY;
454 goto err_mm;
455 }
Krishna Kumard47effe2011-03-01 17:06:37 +0530456
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000457 /* No owner, become one */
458 dev->mm = get_task_mm(current);
Tejun Heoc23f34452010-06-02 20:40:00 +0200459 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
460 if (IS_ERR(worker)) {
461 err = PTR_ERR(worker);
462 goto err_worker;
463 }
464
465 dev->worker = worker;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300466 wake_up_process(worker); /* avoid contributing to loadavg */
467
468 err = vhost_attach_cgroups(dev);
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300469 if (err)
470 goto err_cgroup;
Tejun Heoc23f34452010-06-02 20:40:00 +0200471
Jason Wange0e9b402010-09-14 23:53:05 +0800472 err = vhost_dev_alloc_iovecs(dev);
473 if (err)
474 goto err_cgroup;
475
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000476 return 0;
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300477err_cgroup:
478 kthread_stop(worker);
Michael S. Tsirkin615cc222010-09-02 14:16:36 +0300479 dev->worker = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200480err_worker:
481 if (dev->mm)
482 mmput(dev->mm);
483 dev->mm = NULL;
484err_mm:
485 return err;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000486}
Asias He6ac1afb2013-05-06 16:38:21 +0800487EXPORT_SYMBOL_GPL(vhost_dev_set_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000488
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300489struct vhost_memory *vhost_dev_reset_owner_prepare(void)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000490{
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300491 return kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
492}
Asias He6ac1afb2013-05-06 16:38:21 +0800493EXPORT_SYMBOL_GPL(vhost_dev_reset_owner_prepare);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000494
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300495/* Caller should have device mutex */
496void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_memory *memory)
497{
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300498 int i;
499
Michael S. Tsirkinea5d4042011-11-27 19:05:58 +0200500 vhost_dev_cleanup(dev, true);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000501
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300502 /* Restore memory to default empty mapping. */
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000503 memory->nregions = 0;
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300504 dev->memory = memory;
505 /* We don't need VQ locks below since vhost_dev_cleanup makes sure
506 * VQs aren't running.
507 */
508 for (i = 0; i < dev->nvqs; ++i)
509 dev->vqs[i]->memory = memory;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000510}
Asias He6ac1afb2013-05-06 16:38:21 +0800511EXPORT_SYMBOL_GPL(vhost_dev_reset_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000512
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000513void vhost_dev_stop(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000514{
515 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530516
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000517 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800518 if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) {
519 vhost_poll_stop(&dev->vqs[i]->poll);
520 vhost_poll_flush(&dev->vqs[i]->poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000521 }
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000522 }
523}
Asias He6ac1afb2013-05-06 16:38:21 +0800524EXPORT_SYMBOL_GPL(vhost_dev_stop);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000525
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000526/* Caller should have device mutex if and only if locked is set */
527void vhost_dev_cleanup(struct vhost_dev *dev, bool locked)
528{
529 int i;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000530
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000531 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800532 if (dev->vqs[i]->error_ctx)
533 eventfd_ctx_put(dev->vqs[i]->error_ctx);
534 if (dev->vqs[i]->error)
535 fput(dev->vqs[i]->error);
536 if (dev->vqs[i]->kick)
537 fput(dev->vqs[i]->kick);
538 if (dev->vqs[i]->call_ctx)
539 eventfd_ctx_put(dev->vqs[i]->call_ctx);
540 if (dev->vqs[i]->call)
541 fput(dev->vqs[i]->call);
542 vhost_vq_reset(dev, dev->vqs[i]);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000543 }
Jason Wange0e9b402010-09-14 23:53:05 +0800544 vhost_dev_free_iovecs(dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000545 if (dev->log_ctx)
546 eventfd_ctx_put(dev->log_ctx);
547 dev->log_ctx = NULL;
548 if (dev->log_file)
549 fput(dev->log_file);
550 dev->log_file = NULL;
551 /* No one will access memory at this point */
Igor Mammedov4de72552015-07-01 11:07:09 +0200552 kvfree(dev->memory);
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300553 dev->memory = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200554 WARN_ON(!list_empty(&dev->work_list));
Eric Dumazet78b620c2010-08-31 02:05:57 +0000555 if (dev->worker) {
556 kthread_stop(dev->worker);
557 dev->worker = NULL;
558 }
Michael S. Tsirkin533a19b2010-10-06 15:34:38 +0200559 if (dev->mm)
560 mmput(dev->mm);
561 dev->mm = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000562}
Asias He6ac1afb2013-05-06 16:38:21 +0800563EXPORT_SYMBOL_GPL(vhost_dev_cleanup);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000564
565static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
566{
567 u64 a = addr / VHOST_PAGE_SIZE / 8;
Krishna Kumard47effe2011-03-01 17:06:37 +0530568
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000569 /* Make sure 64 bit math will not overflow. */
570 if (a > ULONG_MAX - (unsigned long)log_base ||
571 a + (unsigned long)log_base > ULONG_MAX)
Dan Carpenter6d97e552010-10-11 19:24:19 +0200572 return 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000573
574 return access_ok(VERIFY_WRITE, log_base + a,
575 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
576}
577
578/* Caller should have vq mutex and device mutex. */
579static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
580 int log_all)
581{
582 int i;
Jeff Dike179b2842010-04-07 09:59:10 -0400583
Michael S. Tsirkinf8322fb2010-05-27 12:28:03 +0300584 if (!mem)
585 return 0;
Jeff Dike179b2842010-04-07 09:59:10 -0400586
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000587 for (i = 0; i < mem->nregions; ++i) {
588 struct vhost_memory_region *m = mem->regions + i;
589 unsigned long a = m->userspace_addr;
590 if (m->memory_size > ULONG_MAX)
591 return 0;
592 else if (!access_ok(VERIFY_WRITE, (void __user *)a,
593 m->memory_size))
594 return 0;
595 else if (log_all && !log_access_ok(log_base,
596 m->guest_phys_addr,
597 m->memory_size))
598 return 0;
599 }
600 return 1;
601}
602
603/* Can we switch to this memory table? */
604/* Caller should have device mutex but not vq mutex */
605static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
606 int log_all)
607{
608 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530609
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000610 for (i = 0; i < d->nvqs; ++i) {
611 int ok;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300612 bool log;
613
Asias He3ab2e422013-04-27 11:16:48 +0800614 mutex_lock(&d->vqs[i]->mutex);
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300615 log = log_all || vhost_has_feature(d->vqs[i], VHOST_F_LOG_ALL);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000616 /* If ring is inactive, will check when it's enabled. */
Asias He3ab2e422013-04-27 11:16:48 +0800617 if (d->vqs[i]->private_data)
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300618 ok = vq_memory_access_ok(d->vqs[i]->log_base, mem, log);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000619 else
620 ok = 1;
Asias He3ab2e422013-04-27 11:16:48 +0800621 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000622 if (!ok)
623 return 0;
624 }
625 return 1;
626}
627
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300628static int vq_access_ok(struct vhost_virtqueue *vq, unsigned int num,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000629 struct vring_desc __user *desc,
630 struct vring_avail __user *avail,
631 struct vring_used __user *used)
632{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300633 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000634 return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
635 access_ok(VERIFY_READ, avail,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300636 sizeof *avail + num * sizeof *avail->ring + s) &&
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000637 access_ok(VERIFY_WRITE, used,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300638 sizeof *used + num * sizeof *used->ring + s);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000639}
640
641/* Can we log writes? */
642/* Caller should have device mutex but not vq mutex */
643int vhost_log_access_ok(struct vhost_dev *dev)
644{
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300645 return memory_access_ok(dev, dev->memory, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000646}
Asias He6ac1afb2013-05-06 16:38:21 +0800647EXPORT_SYMBOL_GPL(vhost_log_access_ok);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000648
649/* Verify access for write logging. */
650/* Caller should have vq mutex and device mutex */
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300651static int vq_log_access_ok(struct vhost_virtqueue *vq,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300652 void __user *log_base)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000653{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300654 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100655
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300656 return vq_memory_access_ok(log_base, vq->memory,
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300657 vhost_has_feature(vq, VHOST_F_LOG_ALL)) &&
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000658 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
659 sizeof *vq->used +
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300660 vq->num * sizeof *vq->used->ring + s));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000661}
662
663/* Can we start vq? */
664/* Caller should have vq mutex and device mutex */
665int vhost_vq_access_ok(struct vhost_virtqueue *vq)
666{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300667 return vq_access_ok(vq, vq->num, vq->desc, vq->avail, vq->used) &&
668 vq_log_access_ok(vq, vq->log_base);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000669}
Asias He6ac1afb2013-05-06 16:38:21 +0800670EXPORT_SYMBOL_GPL(vhost_vq_access_ok);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000671
Igor Mammedovbcfeaca2015-06-16 18:33:35 +0200672static int vhost_memory_reg_sort_cmp(const void *p1, const void *p2)
673{
674 const struct vhost_memory_region *r1 = p1, *r2 = p2;
675 if (r1->guest_phys_addr < r2->guest_phys_addr)
676 return 1;
677 if (r1->guest_phys_addr > r2->guest_phys_addr)
678 return -1;
679 return 0;
680}
681
Igor Mammedov4de72552015-07-01 11:07:09 +0200682static void *vhost_kvzalloc(unsigned long size)
683{
684 void *n = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
685
686 if (!n) {
687 n = vzalloc(size);
688 if (!n)
689 return ERR_PTR(-ENOMEM);
690 }
691 return n;
692}
693
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000694static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
695{
696 struct vhost_memory mem, *newmem, *oldmem;
697 unsigned long size = offsetof(struct vhost_memory, regions);
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +0300698 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530699
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900700 if (copy_from_user(&mem, m, size))
701 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000702 if (mem.padding)
703 return -EOPNOTSUPP;
Igor Mammedovc9ce42f2015-07-02 15:08:11 +0200704 if (mem.nregions > max_mem_regions)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000705 return -E2BIG;
Igor Mammedov4de72552015-07-01 11:07:09 +0200706 newmem = vhost_kvzalloc(size + mem.nregions * sizeof(*m->regions));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000707 if (!newmem)
708 return -ENOMEM;
709
710 memcpy(newmem, &mem, size);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900711 if (copy_from_user(newmem->regions, m->regions,
712 mem.nregions * sizeof *m->regions)) {
Igor Mammedovbcfeaca2015-06-16 18:33:35 +0200713 kvfree(newmem);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900714 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000715 }
Igor Mammedovbcfeaca2015-06-16 18:33:35 +0200716 sort(newmem->regions, newmem->nregions, sizeof(*newmem->regions),
717 vhost_memory_reg_sort_cmp, NULL);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000718
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300719 if (!memory_access_ok(d, newmem, 0)) {
Igor Mammedov4de72552015-07-01 11:07:09 +0200720 kvfree(newmem);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000721 return -EFAULT;
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +0900722 }
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300723 oldmem = d->memory;
724 d->memory = newmem;
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +0300725
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300726 /* All memory accesses are done under some VQ mutex. */
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +0300727 for (i = 0; i < d->nvqs; ++i) {
728 mutex_lock(&d->vqs[i]->mutex);
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300729 d->vqs[i]->memory = newmem;
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +0300730 mutex_unlock(&d->vqs[i]->mutex);
731 }
Igor Mammedov4de72552015-07-01 11:07:09 +0200732 kvfree(oldmem);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000733 return 0;
734}
735
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200736long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000737{
Al Virocecb46f2012-08-27 14:21:39 -0400738 struct file *eventfp, *filep = NULL;
739 bool pollstart = false, pollstop = false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000740 struct eventfd_ctx *ctx = NULL;
741 u32 __user *idxp = argp;
742 struct vhost_virtqueue *vq;
743 struct vhost_vring_state s;
744 struct vhost_vring_file f;
745 struct vhost_vring_addr a;
746 u32 idx;
747 long r;
748
749 r = get_user(idx, idxp);
750 if (r < 0)
751 return r;
Krishna Kumar0f3d9a12010-05-25 11:10:36 +0530752 if (idx >= d->nvqs)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000753 return -ENOBUFS;
754
Asias He3ab2e422013-04-27 11:16:48 +0800755 vq = d->vqs[idx];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000756
757 mutex_lock(&vq->mutex);
758
759 switch (ioctl) {
760 case VHOST_SET_VRING_NUM:
761 /* Resizing ring 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 || s.num > 0xffff || (s.num & (s.num - 1))) {
772 r = -EINVAL;
773 break;
774 }
775 vq->num = s.num;
776 break;
777 case VHOST_SET_VRING_BASE:
778 /* Moving base with an active backend?
779 * You don't want to do that. */
780 if (vq->private_data) {
781 r = -EBUSY;
782 break;
783 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900784 if (copy_from_user(&s, argp, sizeof s)) {
785 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000786 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900787 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000788 if (s.num > 0xffff) {
789 r = -EINVAL;
790 break;
791 }
792 vq->last_avail_idx = s.num;
793 /* Forget the cached index value. */
794 vq->avail_idx = vq->last_avail_idx;
795 break;
796 case VHOST_GET_VRING_BASE:
797 s.index = idx;
798 s.num = vq->last_avail_idx;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900799 if (copy_to_user(argp, &s, sizeof s))
800 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000801 break;
802 case VHOST_SET_VRING_ADDR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900803 if (copy_from_user(&a, argp, sizeof a)) {
804 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000805 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900806 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000807 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
808 r = -EOPNOTSUPP;
809 break;
810 }
811 /* For 32bit, verify that the top 32bits of the user
812 data are set to zero. */
813 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
814 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
815 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
816 r = -EFAULT;
817 break;
818 }
Michael S. Tsirkin5d9a07b2014-12-21 01:00:23 +0200819
820 /* Make sure it's safe to cast pointers to vring types. */
821 BUILD_BUG_ON(__alignof__ *vq->avail > VRING_AVAIL_ALIGN_SIZE);
822 BUILD_BUG_ON(__alignof__ *vq->used > VRING_USED_ALIGN_SIZE);
823 if ((a.avail_user_addr & (VRING_AVAIL_ALIGN_SIZE - 1)) ||
824 (a.used_user_addr & (VRING_USED_ALIGN_SIZE - 1)) ||
825 (a.log_guest_addr & (sizeof(u64) - 1))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000826 r = -EINVAL;
827 break;
828 }
829
830 /* We only verify access here if backend is configured.
831 * If it is not, we don't as size might not have been setup.
832 * We will verify when backend is configured. */
833 if (vq->private_data) {
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300834 if (!vq_access_ok(vq, vq->num,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000835 (void __user *)(unsigned long)a.desc_user_addr,
836 (void __user *)(unsigned long)a.avail_user_addr,
837 (void __user *)(unsigned long)a.used_user_addr)) {
838 r = -EINVAL;
839 break;
840 }
841
842 /* Also validate log access for used ring if enabled. */
843 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
844 !log_access_ok(vq->log_base, a.log_guest_addr,
845 sizeof *vq->used +
846 vq->num * sizeof *vq->used->ring)) {
847 r = -EINVAL;
848 break;
849 }
850 }
851
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000852 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
853 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
854 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
855 vq->log_addr = a.log_guest_addr;
856 vq->used = (void __user *)(unsigned long)a.used_user_addr;
857 break;
858 case VHOST_SET_VRING_KICK:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900859 if (copy_from_user(&f, argp, sizeof f)) {
860 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000861 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900862 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000863 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200864 if (IS_ERR(eventfp)) {
865 r = PTR_ERR(eventfp);
866 break;
867 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000868 if (eventfp != vq->kick) {
Al Virocecb46f2012-08-27 14:21:39 -0400869 pollstop = (filep = vq->kick) != NULL;
870 pollstart = (vq->kick = eventfp) != NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000871 } else
872 filep = eventfp;
873 break;
874 case VHOST_SET_VRING_CALL:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900875 if (copy_from_user(&f, argp, sizeof f)) {
876 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000877 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900878 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000879 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200880 if (IS_ERR(eventfp)) {
881 r = PTR_ERR(eventfp);
882 break;
883 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000884 if (eventfp != vq->call) {
885 filep = vq->call;
886 ctx = vq->call_ctx;
887 vq->call = eventfp;
888 vq->call_ctx = eventfp ?
889 eventfd_ctx_fileget(eventfp) : NULL;
890 } else
891 filep = eventfp;
892 break;
893 case VHOST_SET_VRING_ERR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900894 if (copy_from_user(&f, argp, sizeof f)) {
895 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000896 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900897 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000898 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200899 if (IS_ERR(eventfp)) {
900 r = PTR_ERR(eventfp);
901 break;
902 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000903 if (eventfp != vq->error) {
904 filep = vq->error;
905 vq->error = eventfp;
906 ctx = vq->error_ctx;
907 vq->error_ctx = eventfp ?
908 eventfd_ctx_fileget(eventfp) : NULL;
909 } else
910 filep = eventfp;
911 break;
Greg Kurz2751c982015-04-24 14:27:24 +0200912 case VHOST_SET_VRING_ENDIAN:
913 r = vhost_set_vring_endian(vq, argp);
914 break;
915 case VHOST_GET_VRING_ENDIAN:
916 r = vhost_get_vring_endian(vq, idx, argp);
917 break;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000918 default:
919 r = -ENOIOCTLCMD;
920 }
921
922 if (pollstop && vq->handle_kick)
923 vhost_poll_stop(&vq->poll);
924
925 if (ctx)
926 eventfd_ctx_put(ctx);
927 if (filep)
928 fput(filep);
929
930 if (pollstart && vq->handle_kick)
Jason Wang2b8b3282013-01-28 01:05:18 +0000931 r = vhost_poll_start(&vq->poll, vq->kick);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000932
933 mutex_unlock(&vq->mutex);
934
935 if (pollstop && vq->handle_kick)
936 vhost_poll_flush(&vq->poll);
937 return r;
938}
Asias He6ac1afb2013-05-06 16:38:21 +0800939EXPORT_SYMBOL_GPL(vhost_vring_ioctl);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000940
941/* Caller must have device mutex */
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200942long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000943{
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000944 struct file *eventfp, *filep = NULL;
945 struct eventfd_ctx *ctx = NULL;
946 u64 p;
947 long r;
948 int i, fd;
949
950 /* If you are not the owner, you can become one */
951 if (ioctl == VHOST_SET_OWNER) {
952 r = vhost_dev_set_owner(d);
953 goto done;
954 }
955
956 /* You must be the owner to do anything else */
957 r = vhost_dev_check_owner(d);
958 if (r)
959 goto done;
960
961 switch (ioctl) {
962 case VHOST_SET_MEM_TABLE:
963 r = vhost_set_memory(d, argp);
964 break;
965 case VHOST_SET_LOG_BASE:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900966 if (copy_from_user(&p, argp, sizeof p)) {
967 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000968 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900969 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000970 if ((u64)(unsigned long)p != p) {
971 r = -EFAULT;
972 break;
973 }
974 for (i = 0; i < d->nvqs; ++i) {
975 struct vhost_virtqueue *vq;
976 void __user *base = (void __user *)(unsigned long)p;
Asias He3ab2e422013-04-27 11:16:48 +0800977 vq = d->vqs[i];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000978 mutex_lock(&vq->mutex);
979 /* If ring is inactive, will check when it's enabled. */
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300980 if (vq->private_data && !vq_log_access_ok(vq, base))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000981 r = -EFAULT;
982 else
983 vq->log_base = base;
984 mutex_unlock(&vq->mutex);
985 }
986 break;
987 case VHOST_SET_LOG_FD:
988 r = get_user(fd, (int __user *)argp);
989 if (r < 0)
990 break;
991 eventfp = fd == -1 ? NULL : eventfd_fget(fd);
992 if (IS_ERR(eventfp)) {
993 r = PTR_ERR(eventfp);
994 break;
995 }
996 if (eventfp != d->log_file) {
997 filep = d->log_file;
Marc-André Lureau7932c0b2015-07-17 15:32:03 +0200998 d->log_file = eventfp;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000999 ctx = d->log_ctx;
1000 d->log_ctx = eventfp ?
1001 eventfd_ctx_fileget(eventfp) : NULL;
1002 } else
1003 filep = eventfp;
1004 for (i = 0; i < d->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +08001005 mutex_lock(&d->vqs[i]->mutex);
1006 d->vqs[i]->log_ctx = d->log_ctx;
1007 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001008 }
1009 if (ctx)
1010 eventfd_ctx_put(ctx);
1011 if (filep)
1012 fput(filep);
1013 break;
1014 default:
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001015 r = -ENOIOCTLCMD;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001016 break;
1017 }
1018done:
1019 return r;
1020}
Asias He6ac1afb2013-05-06 16:38:21 +08001021EXPORT_SYMBOL_GPL(vhost_dev_ioctl);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001022
1023static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
1024 __u64 addr, __u32 len)
1025{
Igor Mammedovbcfeaca2015-06-16 18:33:35 +02001026 const struct vhost_memory_region *reg;
1027 int start = 0, end = mem->nregions;
Krishna Kumard47effe2011-03-01 17:06:37 +05301028
Igor Mammedovbcfeaca2015-06-16 18:33:35 +02001029 while (start < end) {
1030 int slot = start + (end - start) / 2;
1031 reg = mem->regions + slot;
1032 if (addr >= reg->guest_phys_addr)
1033 end = slot;
1034 else
1035 start = slot + 1;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001036 }
Igor Mammedovbcfeaca2015-06-16 18:33:35 +02001037
1038 reg = mem->regions + start;
1039 if (addr >= reg->guest_phys_addr &&
1040 reg->guest_phys_addr + reg->memory_size > addr)
1041 return reg;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001042 return NULL;
1043}
1044
1045/* TODO: This is really inefficient. We need something like get_user()
1046 * (instruction directly accesses the data, with an exception table entry
1047 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
1048 */
1049static int set_bit_to_user(int nr, void __user *addr)
1050{
1051 unsigned long log = (unsigned long)addr;
1052 struct page *page;
1053 void *base;
1054 int bit = nr + (log % PAGE_SIZE) * 8;
1055 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301056
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001057 r = get_user_pages_fast(log, 1, 1, &page);
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +02001058 if (r < 0)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001059 return r;
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +02001060 BUG_ON(r != 1);
Cong Wangc6daa7f2011-11-25 23:14:26 +08001061 base = kmap_atomic(page);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001062 set_bit(bit, base);
Cong Wangc6daa7f2011-11-25 23:14:26 +08001063 kunmap_atomic(base);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001064 set_page_dirty_lock(page);
1065 put_page(page);
1066 return 0;
1067}
1068
1069static int log_write(void __user *log_base,
1070 u64 write_address, u64 write_length)
1071{
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001072 u64 write_page = write_address / VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001073 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301074
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001075 if (!write_length)
1076 return 0;
Michael S. Tsirkin3bf9be42010-11-29 10:19:07 +02001077 write_length += write_address % VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001078 for (;;) {
1079 u64 base = (u64)(unsigned long)log_base;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001080 u64 log = base + write_page / 8;
1081 int bit = write_page % 8;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001082 if ((u64)(unsigned long)log != log)
1083 return -EFAULT;
1084 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
1085 if (r < 0)
1086 return r;
1087 if (write_length <= VHOST_PAGE_SIZE)
1088 break;
1089 write_length -= VHOST_PAGE_SIZE;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001090 write_page += 1;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001091 }
1092 return r;
1093}
1094
1095int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
1096 unsigned int log_num, u64 len)
1097{
1098 int i, r;
1099
1100 /* Make sure data written is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001101 smp_wmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001102 for (i = 0; i < log_num; ++i) {
1103 u64 l = min(log[i].len, len);
1104 r = log_write(vq->log_base, log[i].addr, l);
1105 if (r < 0)
1106 return r;
1107 len -= l;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +02001108 if (!len) {
1109 if (vq->log_ctx)
1110 eventfd_signal(vq->log_ctx, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001111 return 0;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +02001112 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001113 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001114 /* Length written exceeds what we have stored. This is a bug. */
1115 BUG();
1116 return 0;
1117}
Asias He6ac1afb2013-05-06 16:38:21 +08001118EXPORT_SYMBOL_GPL(vhost_log_write);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001119
Jason Wang2723fea2011-06-21 18:04:38 +08001120static int vhost_update_used_flags(struct vhost_virtqueue *vq)
1121{
1122 void __user *used;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001123 if (__put_user(cpu_to_vhost16(vq, vq->used_flags), &vq->used->flags) < 0)
Jason Wang2723fea2011-06-21 18:04:38 +08001124 return -EFAULT;
1125 if (unlikely(vq->log_used)) {
1126 /* Make sure the flag is seen before log. */
1127 smp_wmb();
1128 /* Log used flag write. */
1129 used = &vq->used->flags;
1130 log_write(vq->log_base, vq->log_addr +
1131 (used - (void __user *)vq->used),
1132 sizeof vq->used->flags);
1133 if (vq->log_ctx)
1134 eventfd_signal(vq->log_ctx, 1);
1135 }
1136 return 0;
1137}
1138
1139static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
1140{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001141 if (__put_user(cpu_to_vhost16(vq, vq->avail_idx), vhost_avail_event(vq)))
Jason Wang2723fea2011-06-21 18:04:38 +08001142 return -EFAULT;
1143 if (unlikely(vq->log_used)) {
1144 void __user *used;
1145 /* Make sure the event is seen before log. */
1146 smp_wmb();
1147 /* Log avail event write */
1148 used = vhost_avail_event(vq);
1149 log_write(vq->log_base, vq->log_addr +
1150 (used - (void __user *)vq->used),
1151 sizeof *vhost_avail_event(vq));
1152 if (vq->log_ctx)
1153 eventfd_signal(vq->log_ctx, 1);
1154 }
1155 return 0;
1156}
1157
1158int vhost_init_used(struct vhost_virtqueue *vq)
1159{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001160 __virtio16 last_used_idx;
Jason Wang2723fea2011-06-21 18:04:38 +08001161 int r;
Greg Kurz2751c982015-04-24 14:27:24 +02001162 if (!vq->private_data) {
1163 vq->is_le = virtio_legacy_is_little_endian();
Jason Wang2723fea2011-06-21 18:04:38 +08001164 return 0;
Greg Kurz2751c982015-04-24 14:27:24 +02001165 }
1166
1167 vhost_init_is_le(vq);
Jason Wang2723fea2011-06-21 18:04:38 +08001168
1169 r = vhost_update_used_flags(vq);
1170 if (r)
1171 return r;
1172 vq->signalled_used_valid = false;
Michael S. Tsirkin64f7f052014-12-01 17:39:39 +02001173 if (!access_ok(VERIFY_READ, &vq->used->idx, sizeof vq->used->idx))
1174 return -EFAULT;
1175 r = __get_user(last_used_idx, &vq->used->idx);
1176 if (r)
1177 return r;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001178 vq->last_used_idx = vhost16_to_cpu(vq, last_used_idx);
Michael S. Tsirkin64f7f052014-12-01 17:39:39 +02001179 return 0;
Jason Wang2723fea2011-06-21 18:04:38 +08001180}
Asias He6ac1afb2013-05-06 16:38:21 +08001181EXPORT_SYMBOL_GPL(vhost_init_used);
Jason Wang2723fea2011-06-21 18:04:38 +08001182
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001183static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001184 struct iovec iov[], int iov_size)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001185{
1186 const struct vhost_memory_region *reg;
1187 struct vhost_memory *mem;
1188 struct iovec *_iov;
1189 u64 s = 0;
1190 int ret = 0;
1191
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001192 mem = vq->memory;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001193 while ((u64)len > s) {
1194 u64 size;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001195 if (unlikely(ret >= iov_size)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001196 ret = -ENOBUFS;
1197 break;
1198 }
1199 reg = find_region(mem, addr, len);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001200 if (unlikely(!reg)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001201 ret = -EFAULT;
1202 break;
1203 }
1204 _iov = iov + ret;
1205 size = reg->memory_size - addr + reg->guest_phys_addr;
Michael S. Tsirkinbd971202012-11-26 05:57:27 +00001206 _iov->iov_len = min((u64)len - s, size);
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001207 _iov->iov_base = (void __user *)(unsigned long)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001208 (reg->userspace_addr + addr - reg->guest_phys_addr);
1209 s += size;
1210 addr += size;
1211 ++ret;
1212 }
1213
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001214 return ret;
1215}
1216
1217/* Each buffer in the virtqueues is actually a chain of descriptors. This
1218 * function returns the next descriptor in the chain,
1219 * or -1U if we're at the end. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001220static unsigned next_desc(struct vhost_virtqueue *vq, struct vring_desc *desc)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001221{
1222 unsigned int next;
1223
1224 /* If this descriptor says it doesn't chain, we're done. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001225 if (!(desc->flags & cpu_to_vhost16(vq, VRING_DESC_F_NEXT)))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001226 return -1U;
1227
1228 /* Check they're not leading us off end of descriptors. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001229 next = vhost16_to_cpu(vq, desc->next);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001230 /* Make sure compiler knows to grab that: we don't want it changing! */
1231 /* We will use the result as an index in an array, so most
1232 * architectures only need a compiler barrier here. */
1233 read_barrier_depends();
1234
1235 return next;
1236}
1237
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001238static int get_indirect(struct vhost_virtqueue *vq,
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001239 struct iovec iov[], unsigned int iov_size,
1240 unsigned int *out_num, unsigned int *in_num,
1241 struct vhost_log *log, unsigned int *log_num,
1242 struct vring_desc *indirect)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001243{
1244 struct vring_desc desc;
1245 unsigned int i = 0, count, found = 0;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001246 u32 len = vhost32_to_cpu(vq, indirect->len);
Al Viroaad9a1c2014-12-10 14:49:01 -05001247 struct iov_iter from;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001248 int ret;
1249
1250 /* Sanity check */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001251 if (unlikely(len % sizeof desc)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001252 vq_err(vq, "Invalid length in indirect descriptor: "
1253 "len 0x%llx not multiple of 0x%zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001254 (unsigned long long)len,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001255 sizeof desc);
1256 return -EINVAL;
1257 }
1258
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001259 ret = translate_desc(vq, vhost64_to_cpu(vq, indirect->addr), len, vq->indirect,
Jason Wange0e9b402010-09-14 23:53:05 +08001260 UIO_MAXIOV);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001261 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001262 vq_err(vq, "Translation failure %d in indirect.\n", ret);
1263 return ret;
1264 }
Al Viroaad9a1c2014-12-10 14:49:01 -05001265 iov_iter_init(&from, READ, vq->indirect, ret, len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001266
1267 /* We will use the result as an address to read from, so most
1268 * architectures only need a compiler barrier here. */
1269 read_barrier_depends();
1270
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001271 count = len / sizeof desc;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001272 /* Buffers are chained via a 16 bit next field, so
1273 * we can have at most 2^16 of these. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001274 if (unlikely(count > USHRT_MAX + 1)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001275 vq_err(vq, "Indirect buffer length too big: %d\n",
1276 indirect->len);
1277 return -E2BIG;
1278 }
1279
1280 do {
1281 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001282 if (unlikely(++found > count)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001283 vq_err(vq, "Loop detected: last one at %u "
1284 "indirect size %u\n",
1285 i, count);
1286 return -EINVAL;
1287 }
Al Viroaad9a1c2014-12-10 14:49:01 -05001288 if (unlikely(copy_from_iter(&desc, sizeof(desc), &from) !=
1289 sizeof(desc))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001290 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001291 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001292 return -EINVAL;
1293 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001294 if (unlikely(desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001295 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001296 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001297 return -EINVAL;
1298 }
1299
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001300 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
1301 vhost32_to_cpu(vq, desc.len), iov + iov_count,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001302 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001303 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001304 vq_err(vq, "Translation failure %d indirect idx %d\n",
1305 ret, i);
1306 return ret;
1307 }
1308 /* If this is an input descriptor, increment that count. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001309 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001310 *in_num += ret;
1311 if (unlikely(log)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001312 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
1313 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001314 ++*log_num;
1315 }
1316 } else {
1317 /* If it's an output descriptor, they're all supposed
1318 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001319 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001320 vq_err(vq, "Indirect descriptor "
1321 "has out after in: idx %d\n", i);
1322 return -EINVAL;
1323 }
1324 *out_num += ret;
1325 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001326 } while ((i = next_desc(vq, &desc)) != -1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001327 return 0;
1328}
1329
1330/* This looks in the virtqueue and for the first available buffer, and converts
1331 * it to an iovec for convenient access. Since descriptors consist of some
1332 * number of output then some number of input descriptors, it's actually two
1333 * iovecs, but we pack them into one and note how many of each there were.
1334 *
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001335 * This function returns the descriptor number found, or vq->num (which is
1336 * never a valid descriptor number) if none was found. A negative code is
1337 * returned on error. */
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001338int vhost_get_vq_desc(struct vhost_virtqueue *vq,
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001339 struct iovec iov[], unsigned int iov_size,
1340 unsigned int *out_num, unsigned int *in_num,
1341 struct vhost_log *log, unsigned int *log_num)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001342{
1343 struct vring_desc desc;
1344 unsigned int i, head, found = 0;
1345 u16 last_avail_idx;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001346 __virtio16 avail_idx;
1347 __virtio16 ring_head;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001348 int ret;
1349
1350 /* Check it isn't doing very strange things with descriptor numbers. */
1351 last_avail_idx = vq->last_avail_idx;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001352 if (unlikely(__get_user(avail_idx, &vq->avail->idx))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001353 vq_err(vq, "Failed to access avail idx at %p\n",
1354 &vq->avail->idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001355 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001356 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001357 vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001358
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001359 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001360 vq_err(vq, "Guest moved used index from %u to %u",
1361 last_avail_idx, vq->avail_idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001362 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001363 }
1364
1365 /* If there's nothing new since last we looked, return invalid. */
1366 if (vq->avail_idx == last_avail_idx)
1367 return vq->num;
1368
1369 /* Only get avail ring entries after they have been exposed by guest. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001370 smp_rmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001371
1372 /* Grab the next descriptor number they're advertising, and increment
1373 * the index we've seen. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001374 if (unlikely(__get_user(ring_head,
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001375 &vq->avail->ring[last_avail_idx % vq->num]))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001376 vq_err(vq, "Failed to read head: idx %d address %p\n",
1377 last_avail_idx,
1378 &vq->avail->ring[last_avail_idx % vq->num]);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001379 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001380 }
1381
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001382 head = vhost16_to_cpu(vq, ring_head);
1383
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001384 /* If their number is silly, that's an error. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001385 if (unlikely(head >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001386 vq_err(vq, "Guest says index %u > %u is available",
1387 head, vq->num);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001388 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001389 }
1390
1391 /* When we start there are none of either input nor output. */
1392 *out_num = *in_num = 0;
1393 if (unlikely(log))
1394 *log_num = 0;
1395
1396 i = head;
1397 do {
1398 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001399 if (unlikely(i >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001400 vq_err(vq, "Desc index is %u > %u, head = %u",
1401 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001402 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001403 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001404 if (unlikely(++found > vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001405 vq_err(vq, "Loop detected: last one at %u "
1406 "vq size %u head %u\n",
1407 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001408 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001409 }
Michael S. Tsirkinfcc042a2011-03-06 13:33:49 +02001410 ret = __copy_from_user(&desc, vq->desc + i, sizeof desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001411 if (unlikely(ret)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001412 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
1413 i, vq->desc + i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001414 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001415 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001416 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT)) {
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001417 ret = get_indirect(vq, iov, iov_size,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001418 out_num, in_num,
1419 log, log_num, &desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001420 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001421 vq_err(vq, "Failure detected "
1422 "in indirect descriptor at idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001423 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001424 }
1425 continue;
1426 }
1427
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001428 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
1429 vhost32_to_cpu(vq, desc.len), iov + iov_count,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001430 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001431 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001432 vq_err(vq, "Translation failure %d descriptor idx %d\n",
1433 ret, i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001434 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001435 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001436 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001437 /* If this is an input descriptor,
1438 * increment that count. */
1439 *in_num += ret;
1440 if (unlikely(log)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001441 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
1442 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001443 ++*log_num;
1444 }
1445 } else {
1446 /* If it's an output descriptor, they're all supposed
1447 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001448 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001449 vq_err(vq, "Descriptor has out after in: "
1450 "idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001451 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001452 }
1453 *out_num += ret;
1454 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001455 } while ((i = next_desc(vq, &desc)) != -1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001456
1457 /* On success, increment avail index. */
1458 vq->last_avail_idx++;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001459
1460 /* Assume notifications from guest are disabled at this point,
1461 * if they aren't we would need to update avail_event index. */
1462 BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001463 return head;
1464}
Asias He6ac1afb2013-05-06 16:38:21 +08001465EXPORT_SYMBOL_GPL(vhost_get_vq_desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001466
1467/* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
David Stevens8dd014a2010-07-27 18:52:21 +03001468void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001469{
David Stevens8dd014a2010-07-27 18:52:21 +03001470 vq->last_avail_idx -= n;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001471}
Asias He6ac1afb2013-05-06 16:38:21 +08001472EXPORT_SYMBOL_GPL(vhost_discard_vq_desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001473
1474/* After we've used one of their buffers, we tell them about it. We'll then
1475 * want to notify the guest, using eventfd. */
1476int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
1477{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001478 struct vring_used_elem heads = {
1479 cpu_to_vhost32(vq, head),
1480 cpu_to_vhost32(vq, len)
1481 };
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001482
Jason Wangc49e4e52013-09-02 16:40:58 +08001483 return vhost_add_used_n(vq, &heads, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001484}
Asias He6ac1afb2013-05-06 16:38:21 +08001485EXPORT_SYMBOL_GPL(vhost_add_used);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001486
David Stevens8dd014a2010-07-27 18:52:21 +03001487static int __vhost_add_used_n(struct vhost_virtqueue *vq,
1488 struct vring_used_elem *heads,
1489 unsigned count)
1490{
1491 struct vring_used_elem __user *used;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001492 u16 old, new;
David Stevens8dd014a2010-07-27 18:52:21 +03001493 int start;
1494
1495 start = vq->last_used_idx % vq->num;
1496 used = vq->used->ring + start;
Jason Wangc49e4e52013-09-02 16:40:58 +08001497 if (count == 1) {
1498 if (__put_user(heads[0].id, &used->id)) {
1499 vq_err(vq, "Failed to write used id");
1500 return -EFAULT;
1501 }
1502 if (__put_user(heads[0].len, &used->len)) {
1503 vq_err(vq, "Failed to write used len");
1504 return -EFAULT;
1505 }
1506 } else if (__copy_to_user(used, heads, count * sizeof *used)) {
David Stevens8dd014a2010-07-27 18:52:21 +03001507 vq_err(vq, "Failed to write used");
1508 return -EFAULT;
1509 }
1510 if (unlikely(vq->log_used)) {
1511 /* Make sure data is seen before log. */
1512 smp_wmb();
1513 /* Log used ring entry write. */
1514 log_write(vq->log_base,
1515 vq->log_addr +
1516 ((void __user *)used - (void __user *)vq->used),
1517 count * sizeof *used);
1518 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001519 old = vq->last_used_idx;
1520 new = (vq->last_used_idx += count);
1521 /* If the driver never bothers to signal in a very long while,
1522 * used index might wrap around. If that happens, invalidate
1523 * signalled_used index we stored. TODO: make sure driver
1524 * signals at least once in 2^16 and remove this. */
1525 if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old)))
1526 vq->signalled_used_valid = false;
David Stevens8dd014a2010-07-27 18:52:21 +03001527 return 0;
1528}
1529
1530/* After we've used one of their buffers, we tell them about it. We'll then
1531 * want to notify the guest, using eventfd. */
1532int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
1533 unsigned count)
1534{
1535 int start, n, r;
1536
1537 start = vq->last_used_idx % vq->num;
1538 n = vq->num - start;
1539 if (n < count) {
1540 r = __vhost_add_used_n(vq, heads, n);
1541 if (r < 0)
1542 return r;
1543 heads += n;
1544 count -= n;
1545 }
1546 r = __vhost_add_used_n(vq, heads, count);
1547
1548 /* Make sure buffer is written before we update index. */
1549 smp_wmb();
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001550 if (__put_user(cpu_to_vhost16(vq, vq->last_used_idx), &vq->used->idx)) {
David Stevens8dd014a2010-07-27 18:52:21 +03001551 vq_err(vq, "Failed to increment used idx");
1552 return -EFAULT;
1553 }
1554 if (unlikely(vq->log_used)) {
1555 /* Log used index update. */
1556 log_write(vq->log_base,
1557 vq->log_addr + offsetof(struct vring_used, idx),
1558 sizeof vq->used->idx);
1559 if (vq->log_ctx)
1560 eventfd_signal(vq->log_ctx, 1);
1561 }
1562 return r;
1563}
Asias He6ac1afb2013-05-06 16:38:21 +08001564EXPORT_SYMBOL_GPL(vhost_add_used_n);
David Stevens8dd014a2010-07-27 18:52:21 +03001565
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001566static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001567{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001568 __u16 old, new;
1569 __virtio16 event;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001570 bool v;
Michael S. Tsirkin0d499352010-05-11 19:44:17 +03001571 /* Flush out used index updates. This is paired
1572 * with the barrier that the Guest executes when enabling
1573 * interrupts. */
1574 smp_mb();
1575
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001576 if (vhost_has_feature(vq, VIRTIO_F_NOTIFY_ON_EMPTY) &&
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001577 unlikely(vq->avail_idx == vq->last_avail_idx))
1578 return true;
1579
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001580 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001581 __virtio16 flags;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001582 if (__get_user(flags, &vq->avail->flags)) {
1583 vq_err(vq, "Failed to get flags");
1584 return true;
1585 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001586 return !(flags & cpu_to_vhost16(vq, VRING_AVAIL_F_NO_INTERRUPT));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001587 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001588 old = vq->signalled_used;
1589 v = vq->signalled_used_valid;
1590 new = vq->signalled_used = vq->last_used_idx;
1591 vq->signalled_used_valid = true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001592
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001593 if (unlikely(!v))
1594 return true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001595
Michael S. Tsirkin64f7f052014-12-01 17:39:39 +02001596 if (__get_user(event, vhost_used_event(vq))) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001597 vq_err(vq, "Failed to get used event idx");
1598 return true;
1599 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001600 return vring_need_event(vhost16_to_cpu(vq, event), new, old);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001601}
1602
1603/* This actually signals the guest, using eventfd. */
1604void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
1605{
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001606 /* Signal the Guest tell them we used something up. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001607 if (vq->call_ctx && vhost_notify(dev, vq))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001608 eventfd_signal(vq->call_ctx, 1);
1609}
Asias He6ac1afb2013-05-06 16:38:21 +08001610EXPORT_SYMBOL_GPL(vhost_signal);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001611
1612/* And here's the combo meal deal. Supersize me! */
1613void vhost_add_used_and_signal(struct vhost_dev *dev,
1614 struct vhost_virtqueue *vq,
1615 unsigned int head, int len)
1616{
1617 vhost_add_used(vq, head, len);
1618 vhost_signal(dev, vq);
1619}
Asias He6ac1afb2013-05-06 16:38:21 +08001620EXPORT_SYMBOL_GPL(vhost_add_used_and_signal);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001621
David Stevens8dd014a2010-07-27 18:52:21 +03001622/* multi-buffer version of vhost_add_used_and_signal */
1623void vhost_add_used_and_signal_n(struct vhost_dev *dev,
1624 struct vhost_virtqueue *vq,
1625 struct vring_used_elem *heads, unsigned count)
1626{
1627 vhost_add_used_n(vq, heads, count);
1628 vhost_signal(dev, vq);
1629}
Asias He6ac1afb2013-05-06 16:38:21 +08001630EXPORT_SYMBOL_GPL(vhost_add_used_and_signal_n);
David Stevens8dd014a2010-07-27 18:52:21 +03001631
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001632/* OK, now we need to know about added descriptors. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001633bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001634{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001635 __virtio16 avail_idx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001636 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301637
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001638 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
1639 return false;
1640 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001641 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08001642 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001643 if (r) {
1644 vq_err(vq, "Failed to enable notification at %p: %d\n",
1645 &vq->used->flags, r);
1646 return false;
1647 }
1648 } else {
Jason Wang2723fea2011-06-21 18:04:38 +08001649 r = vhost_update_avail_event(vq, vq->avail_idx);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001650 if (r) {
1651 vq_err(vq, "Failed to update avail event index at %p: %d\n",
1652 vhost_avail_event(vq), r);
1653 return false;
1654 }
1655 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001656 /* They could have slipped one in as we were doing that: make
1657 * sure it's written, then check again. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001658 smp_mb();
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001659 r = __get_user(avail_idx, &vq->avail->idx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001660 if (r) {
1661 vq_err(vq, "Failed to check avail idx at %p: %d\n",
1662 &vq->avail->idx, r);
1663 return false;
1664 }
1665
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001666 return vhost16_to_cpu(vq, avail_idx) != vq->avail_idx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001667}
Asias He6ac1afb2013-05-06 16:38:21 +08001668EXPORT_SYMBOL_GPL(vhost_enable_notify);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001669
1670/* We don't need to be notified again. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001671void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001672{
1673 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301674
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001675 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
1676 return;
1677 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001678 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08001679 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001680 if (r)
1681 vq_err(vq, "Failed to enable notification at %p: %d\n",
1682 &vq->used->flags, r);
1683 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001684}
Asias He6ac1afb2013-05-06 16:38:21 +08001685EXPORT_SYMBOL_GPL(vhost_disable_notify);
1686
1687static int __init vhost_init(void)
1688{
1689 return 0;
1690}
1691
1692static void __exit vhost_exit(void)
1693{
1694}
1695
1696module_init(vhost_init);
1697module_exit(vhost_exit);
1698
1699MODULE_VERSION("0.0.1");
1700MODULE_LICENSE("GPL v2");
1701MODULE_AUTHOR("Michael S. Tsirkin");
1702MODULE_DESCRIPTION("Host kernel accelerator for virtio");