blob: 596de212fe7e15b401af5ad6d257eb6bc5878799 [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
Greg Kurzc5072032016-02-16 15:59:34 +010046static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
Greg Kurz2751c982015-04-24 14:27:24 +020047{
48 vq->user_be = !virtio_legacy_is_little_endian();
49}
50
Greg Kurzc5072032016-02-16 15:59:34 +010051static void vhost_enable_cross_endian_big(struct vhost_virtqueue *vq)
52{
53 vq->user_be = true;
54}
55
56static void vhost_enable_cross_endian_little(struct vhost_virtqueue *vq)
57{
58 vq->user_be = false;
59}
60
Greg Kurz2751c982015-04-24 14:27:24 +020061static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
62{
63 struct vhost_vring_state s;
64
65 if (vq->private_data)
66 return -EBUSY;
67
68 if (copy_from_user(&s, argp, sizeof(s)))
69 return -EFAULT;
70
71 if (s.num != VHOST_VRING_LITTLE_ENDIAN &&
72 s.num != VHOST_VRING_BIG_ENDIAN)
73 return -EINVAL;
74
Greg Kurzc5072032016-02-16 15:59:34 +010075 if (s.num == VHOST_VRING_BIG_ENDIAN)
76 vhost_enable_cross_endian_big(vq);
77 else
78 vhost_enable_cross_endian_little(vq);
Greg Kurz2751c982015-04-24 14:27:24 +020079
80 return 0;
81}
82
83static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
84 int __user *argp)
85{
86 struct vhost_vring_state s = {
87 .index = idx,
88 .num = vq->user_be
89 };
90
91 if (copy_to_user(argp, &s, sizeof(s)))
92 return -EFAULT;
93
94 return 0;
95}
96
97static void vhost_init_is_le(struct vhost_virtqueue *vq)
98{
99 /* Note for legacy virtio: user_be is initialized at reset time
100 * according to the host endianness. If userspace does not set an
101 * explicit endianness, the default behavior is native endian, as
102 * expected by legacy virtio.
103 */
104 vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) || !vq->user_be;
105}
106#else
Greg Kurzc5072032016-02-16 15:59:34 +0100107static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
Greg Kurz2751c982015-04-24 14:27:24 +0200108{
109}
110
111static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
112{
113 return -ENOIOCTLCMD;
114}
115
116static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
117 int __user *argp)
118{
119 return -ENOIOCTLCMD;
120}
121
122static void vhost_init_is_le(struct vhost_virtqueue *vq)
123{
124 if (vhost_has_feature(vq, VIRTIO_F_VERSION_1))
125 vq->is_le = true;
126}
127#endif /* CONFIG_VHOST_CROSS_ENDIAN_LEGACY */
128
Greg Kurzc5072032016-02-16 15:59:34 +0100129static void vhost_reset_is_le(struct vhost_virtqueue *vq)
130{
131 vq->is_le = virtio_legacy_is_little_endian();
132}
133
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000134static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
135 poll_table *pt)
136{
137 struct vhost_poll *poll;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000138
Krishna Kumard47effe2011-03-01 17:06:37 +0530139 poll = container_of(pt, struct vhost_poll, table);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000140 poll->wqh = wqh;
141 add_wait_queue(wqh, &poll->wait);
142}
143
144static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync,
145 void *key)
146{
Tejun Heoc23f34452010-06-02 20:40:00 +0200147 struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
148
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000149 if (!((unsigned long)key & poll->mask))
150 return 0;
151
Tejun Heoc23f34452010-06-02 20:40:00 +0200152 vhost_poll_queue(poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000153 return 0;
154}
155
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000156void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000157{
Tejun Heoc23f34452010-06-02 20:40:00 +0200158 INIT_LIST_HEAD(&work->node);
159 work->fn = fn;
160 init_waitqueue_head(&work->done);
161 work->flushing = 0;
162 work->queue_seq = work->done_seq = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000163}
Asias He6ac1afb2013-05-06 16:38:21 +0800164EXPORT_SYMBOL_GPL(vhost_work_init);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000165
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300166/* Init poll structure */
167void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
168 unsigned long mask, struct vhost_dev *dev)
169{
170 init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
171 init_poll_funcptr(&poll->table, vhost_poll_func);
172 poll->mask = mask;
173 poll->dev = dev;
Jason Wang2b8b3282013-01-28 01:05:18 +0000174 poll->wqh = NULL;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300175
176 vhost_work_init(&poll->work, fn);
177}
Asias He6ac1afb2013-05-06 16:38:21 +0800178EXPORT_SYMBOL_GPL(vhost_poll_init);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300179
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000180/* Start polling a file. We add ourselves to file's wait queue. The caller must
181 * keep a reference to a file until after vhost_poll_stop is called. */
Jason Wang2b8b3282013-01-28 01:05:18 +0000182int vhost_poll_start(struct vhost_poll *poll, struct file *file)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000183{
184 unsigned long mask;
Jason Wang2b8b3282013-01-28 01:05:18 +0000185 int ret = 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530186
Jason Wang70181d512013-04-10 20:50:48 +0000187 if (poll->wqh)
188 return 0;
189
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000190 mask = file->f_op->poll(file, &poll->table);
191 if (mask)
192 vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
Jason Wang2b8b3282013-01-28 01:05:18 +0000193 if (mask & POLLERR) {
194 if (poll->wqh)
195 remove_wait_queue(poll->wqh, &poll->wait);
196 ret = -EINVAL;
197 }
198
199 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000200}
Asias He6ac1afb2013-05-06 16:38:21 +0800201EXPORT_SYMBOL_GPL(vhost_poll_start);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000202
203/* Stop polling a file. After this function returns, it becomes safe to drop the
204 * file reference. You must also flush afterwards. */
205void vhost_poll_stop(struct vhost_poll *poll)
206{
Jason Wang2b8b3282013-01-28 01:05:18 +0000207 if (poll->wqh) {
208 remove_wait_queue(poll->wqh, &poll->wait);
209 poll->wqh = NULL;
210 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000211}
Asias He6ac1afb2013-05-06 16:38:21 +0800212EXPORT_SYMBOL_GPL(vhost_poll_stop);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000213
Michael S. Tsirkin0174b0c2011-01-10 10:03:20 +0200214static bool vhost_work_seq_done(struct vhost_dev *dev, struct vhost_work *work,
215 unsigned seq)
216{
217 int left;
Krishna Kumard47effe2011-03-01 17:06:37 +0530218
Michael S. Tsirkin0174b0c2011-01-10 10:03:20 +0200219 spin_lock_irq(&dev->work_lock);
220 left = seq - work->done_seq;
221 spin_unlock_irq(&dev->work_lock);
222 return left <= 0;
223}
224
Asias He6ac1afb2013-05-06 16:38:21 +0800225void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000226{
Tejun Heoc23f34452010-06-02 20:40:00 +0200227 unsigned seq;
Tejun Heoc23f34452010-06-02 20:40:00 +0200228 int flushing;
229
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300230 spin_lock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200231 seq = work->queue_seq;
232 work->flushing++;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300233 spin_unlock_irq(&dev->work_lock);
Michael S. Tsirkin0174b0c2011-01-10 10:03:20 +0200234 wait_event(work->done, vhost_work_seq_done(dev, work, seq));
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300235 spin_lock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200236 flushing = --work->flushing;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300237 spin_unlock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200238 BUG_ON(flushing < 0);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000239}
Asias He6ac1afb2013-05-06 16:38:21 +0800240EXPORT_SYMBOL_GPL(vhost_work_flush);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000241
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300242/* Flush any work that has been scheduled. When calling this, don't hold any
243 * locks that are also used by the callback. */
244void vhost_poll_flush(struct vhost_poll *poll)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000245{
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300246 vhost_work_flush(poll->dev, &poll->work);
247}
Asias He6ac1afb2013-05-06 16:38:21 +0800248EXPORT_SYMBOL_GPL(vhost_poll_flush);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300249
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000250void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300251{
Tejun Heoc23f34452010-06-02 20:40:00 +0200252 unsigned long flags;
253
254 spin_lock_irqsave(&dev->work_lock, flags);
255 if (list_empty(&work->node)) {
256 list_add_tail(&work->node, &dev->work_list);
257 work->queue_seq++;
Qin Chuanyuac9fde22013-06-07 21:50:16 +0800258 spin_unlock_irqrestore(&dev->work_lock, flags);
Tejun Heoc23f34452010-06-02 20:40:00 +0200259 wake_up_process(dev->worker);
Qin Chuanyuac9fde22013-06-07 21:50:16 +0800260 } else {
261 spin_unlock_irqrestore(&dev->work_lock, flags);
Tejun Heoc23f34452010-06-02 20:40:00 +0200262 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000263}
Asias He6ac1afb2013-05-06 16:38:21 +0800264EXPORT_SYMBOL_GPL(vhost_work_queue);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000265
Jason Wang526d3e72016-03-04 06:24:51 -0500266/* A lockless hint for busy polling code to exit the loop */
267bool vhost_has_work(struct vhost_dev *dev)
268{
269 return !list_empty(&dev->work_list);
270}
271EXPORT_SYMBOL_GPL(vhost_has_work);
272
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300273void vhost_poll_queue(struct vhost_poll *poll)
274{
275 vhost_work_queue(poll->dev, &poll->work);
276}
Asias He6ac1afb2013-05-06 16:38:21 +0800277EXPORT_SYMBOL_GPL(vhost_poll_queue);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300278
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000279static void vhost_vq_reset(struct vhost_dev *dev,
280 struct vhost_virtqueue *vq)
281{
282 vq->num = 1;
283 vq->desc = NULL;
284 vq->avail = NULL;
285 vq->used = NULL;
286 vq->last_avail_idx = 0;
287 vq->avail_idx = 0;
288 vq->last_used_idx = 0;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300289 vq->signalled_used = 0;
290 vq->signalled_used_valid = false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000291 vq->used_flags = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000292 vq->log_used = false;
293 vq->log_addr = -1ull;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000294 vq->private_data = NULL;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300295 vq->acked_features = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000296 vq->log_base = NULL;
297 vq->error_ctx = NULL;
298 vq->error = NULL;
299 vq->kick = NULL;
300 vq->call_ctx = NULL;
301 vq->call = NULL;
Michael S. Tsirkin73a99f02010-02-23 11:23:45 +0200302 vq->log_ctx = NULL;
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300303 vq->memory = NULL;
Greg Kurzc5072032016-02-16 15:59:34 +0100304 vhost_reset_is_le(vq);
305 vhost_disable_cross_endian(vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000306}
307
Tejun Heoc23f34452010-06-02 20:40:00 +0200308static int vhost_worker(void *data)
309{
310 struct vhost_dev *dev = data;
311 struct vhost_work *work = NULL;
312 unsigned uninitialized_var(seq);
Jens Freimannd7ffde32012-06-26 00:59:58 +0000313 mm_segment_t oldfs = get_fs();
Tejun Heoc23f34452010-06-02 20:40:00 +0200314
Jens Freimannd7ffde32012-06-26 00:59:58 +0000315 set_fs(USER_DS);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200316 use_mm(dev->mm);
317
Tejun Heoc23f34452010-06-02 20:40:00 +0200318 for (;;) {
319 /* mb paired w/ kthread_stop */
320 set_current_state(TASK_INTERRUPTIBLE);
321
322 spin_lock_irq(&dev->work_lock);
323 if (work) {
324 work->done_seq = seq;
325 if (work->flushing)
326 wake_up_all(&work->done);
327 }
328
329 if (kthread_should_stop()) {
330 spin_unlock_irq(&dev->work_lock);
331 __set_current_state(TASK_RUNNING);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200332 break;
Tejun Heoc23f34452010-06-02 20:40:00 +0200333 }
334 if (!list_empty(&dev->work_list)) {
335 work = list_first_entry(&dev->work_list,
336 struct vhost_work, node);
337 list_del_init(&work->node);
338 seq = work->queue_seq;
339 } else
340 work = NULL;
341 spin_unlock_irq(&dev->work_lock);
342
343 if (work) {
344 __set_current_state(TASK_RUNNING);
345 work->fn(work);
Nadav Har'Eld550dda2012-02-27 15:07:29 +0200346 if (need_resched())
347 schedule();
Tejun Heoc23f34452010-06-02 20:40:00 +0200348 } else
349 schedule();
350
351 }
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200352 unuse_mm(dev->mm);
Jens Freimannd7ffde32012-06-26 00:59:58 +0000353 set_fs(oldfs);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200354 return 0;
Tejun Heoc23f34452010-06-02 20:40:00 +0200355}
356
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000357static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
358{
359 kfree(vq->indirect);
360 vq->indirect = NULL;
361 kfree(vq->log);
362 vq->log = NULL;
363 kfree(vq->heads);
364 vq->heads = NULL;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000365}
366
Jason Wange0e9b402010-09-14 23:53:05 +0800367/* Helper to allocate iovec buffers for all vqs. */
368static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
369{
Asias He6d5e6aa2013-05-06 16:38:23 +0800370 struct vhost_virtqueue *vq;
Jason Wange0e9b402010-09-14 23:53:05 +0800371 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530372
Jason Wange0e9b402010-09-14 23:53:05 +0800373 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800374 vq = dev->vqs[i];
375 vq->indirect = kmalloc(sizeof *vq->indirect * UIO_MAXIOV,
376 GFP_KERNEL);
377 vq->log = kmalloc(sizeof *vq->log * UIO_MAXIOV, GFP_KERNEL);
378 vq->heads = kmalloc(sizeof *vq->heads * UIO_MAXIOV, GFP_KERNEL);
379 if (!vq->indirect || !vq->log || !vq->heads)
Jason Wange0e9b402010-09-14 23:53:05 +0800380 goto err_nomem;
381 }
382 return 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530383
Jason Wange0e9b402010-09-14 23:53:05 +0800384err_nomem:
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000385 for (; i >= 0; --i)
Asias He3ab2e422013-04-27 11:16:48 +0800386 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800387 return -ENOMEM;
388}
389
390static void vhost_dev_free_iovecs(struct vhost_dev *dev)
391{
392 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530393
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000394 for (i = 0; i < dev->nvqs; ++i)
Asias He3ab2e422013-04-27 11:16:48 +0800395 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800396}
397
Zhi Yong Wu59566b6e2013-12-07 04:13:03 +0800398void vhost_dev_init(struct vhost_dev *dev,
Asias He3ab2e422013-04-27 11:16:48 +0800399 struct vhost_virtqueue **vqs, int nvqs)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000400{
Asias He6d5e6aa2013-05-06 16:38:23 +0800401 struct vhost_virtqueue *vq;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000402 int i;
Tejun Heoc23f34452010-06-02 20:40:00 +0200403
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000404 dev->vqs = vqs;
405 dev->nvqs = nvqs;
406 mutex_init(&dev->mutex);
407 dev->log_ctx = NULL;
408 dev->log_file = NULL;
409 dev->memory = NULL;
410 dev->mm = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200411 spin_lock_init(&dev->work_lock);
412 INIT_LIST_HEAD(&dev->work_list);
413 dev->worker = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000414
415 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800416 vq = dev->vqs[i];
417 vq->log = NULL;
418 vq->indirect = NULL;
419 vq->heads = NULL;
420 vq->dev = dev;
421 mutex_init(&vq->mutex);
422 vhost_vq_reset(dev, vq);
423 if (vq->handle_kick)
424 vhost_poll_init(&vq->poll, vq->handle_kick,
425 POLLIN, dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000426 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000427}
Asias He6ac1afb2013-05-06 16:38:21 +0800428EXPORT_SYMBOL_GPL(vhost_dev_init);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000429
430/* Caller should have device mutex */
431long vhost_dev_check_owner(struct vhost_dev *dev)
432{
433 /* Are you the owner? If not, I don't think you mean to do that */
434 return dev->mm == current->mm ? 0 : -EPERM;
435}
Asias He6ac1afb2013-05-06 16:38:21 +0800436EXPORT_SYMBOL_GPL(vhost_dev_check_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000437
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300438struct vhost_attach_cgroups_struct {
Krishna Kumard47effe2011-03-01 17:06:37 +0530439 struct vhost_work work;
440 struct task_struct *owner;
441 int ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300442};
443
444static void vhost_attach_cgroups_work(struct vhost_work *work)
445{
Krishna Kumard47effe2011-03-01 17:06:37 +0530446 struct vhost_attach_cgroups_struct *s;
447
448 s = container_of(work, struct vhost_attach_cgroups_struct, work);
449 s->ret = cgroup_attach_task_all(s->owner, current);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300450}
451
452static int vhost_attach_cgroups(struct vhost_dev *dev)
453{
Krishna Kumard47effe2011-03-01 17:06:37 +0530454 struct vhost_attach_cgroups_struct attach;
455
456 attach.owner = current;
457 vhost_work_init(&attach.work, vhost_attach_cgroups_work);
458 vhost_work_queue(dev, &attach.work);
459 vhost_work_flush(dev, &attach.work);
460 return attach.ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300461}
462
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000463/* Caller should have device mutex */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300464bool vhost_dev_has_owner(struct vhost_dev *dev)
465{
466 return dev->mm;
467}
Asias He6ac1afb2013-05-06 16:38:21 +0800468EXPORT_SYMBOL_GPL(vhost_dev_has_owner);
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300469
470/* Caller should have device mutex */
Asias He54db63c2013-05-06 11:15:59 +0800471long vhost_dev_set_owner(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000472{
Tejun Heoc23f34452010-06-02 20:40:00 +0200473 struct task_struct *worker;
474 int err;
Krishna Kumard47effe2011-03-01 17:06:37 +0530475
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000476 /* Is there an owner already? */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300477 if (vhost_dev_has_owner(dev)) {
Tejun Heoc23f34452010-06-02 20:40:00 +0200478 err = -EBUSY;
479 goto err_mm;
480 }
Krishna Kumard47effe2011-03-01 17:06:37 +0530481
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000482 /* No owner, become one */
483 dev->mm = get_task_mm(current);
Tejun Heoc23f34452010-06-02 20:40:00 +0200484 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
485 if (IS_ERR(worker)) {
486 err = PTR_ERR(worker);
487 goto err_worker;
488 }
489
490 dev->worker = worker;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300491 wake_up_process(worker); /* avoid contributing to loadavg */
492
493 err = vhost_attach_cgroups(dev);
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300494 if (err)
495 goto err_cgroup;
Tejun Heoc23f34452010-06-02 20:40:00 +0200496
Jason Wange0e9b402010-09-14 23:53:05 +0800497 err = vhost_dev_alloc_iovecs(dev);
498 if (err)
499 goto err_cgroup;
500
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000501 return 0;
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300502err_cgroup:
503 kthread_stop(worker);
Michael S. Tsirkin615cc222010-09-02 14:16:36 +0300504 dev->worker = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200505err_worker:
506 if (dev->mm)
507 mmput(dev->mm);
508 dev->mm = NULL;
509err_mm:
510 return err;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000511}
Asias He6ac1afb2013-05-06 16:38:21 +0800512EXPORT_SYMBOL_GPL(vhost_dev_set_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000513
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300514struct vhost_memory *vhost_dev_reset_owner_prepare(void)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000515{
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300516 return kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
517}
Asias He6ac1afb2013-05-06 16:38:21 +0800518EXPORT_SYMBOL_GPL(vhost_dev_reset_owner_prepare);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000519
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300520/* Caller should have device mutex */
521void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_memory *memory)
522{
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300523 int i;
524
Michael S. Tsirkinea5d4042011-11-27 19:05:58 +0200525 vhost_dev_cleanup(dev, true);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000526
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300527 /* Restore memory to default empty mapping. */
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000528 memory->nregions = 0;
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300529 dev->memory = memory;
530 /* We don't need VQ locks below since vhost_dev_cleanup makes sure
531 * VQs aren't running.
532 */
533 for (i = 0; i < dev->nvqs; ++i)
534 dev->vqs[i]->memory = memory;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000535}
Asias He6ac1afb2013-05-06 16:38:21 +0800536EXPORT_SYMBOL_GPL(vhost_dev_reset_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000537
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000538void vhost_dev_stop(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000539{
540 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530541
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000542 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800543 if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) {
544 vhost_poll_stop(&dev->vqs[i]->poll);
545 vhost_poll_flush(&dev->vqs[i]->poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000546 }
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000547 }
548}
Asias He6ac1afb2013-05-06 16:38:21 +0800549EXPORT_SYMBOL_GPL(vhost_dev_stop);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000550
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000551/* Caller should have device mutex if and only if locked is set */
552void vhost_dev_cleanup(struct vhost_dev *dev, bool locked)
553{
554 int i;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000555
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000556 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800557 if (dev->vqs[i]->error_ctx)
558 eventfd_ctx_put(dev->vqs[i]->error_ctx);
559 if (dev->vqs[i]->error)
560 fput(dev->vqs[i]->error);
561 if (dev->vqs[i]->kick)
562 fput(dev->vqs[i]->kick);
563 if (dev->vqs[i]->call_ctx)
564 eventfd_ctx_put(dev->vqs[i]->call_ctx);
565 if (dev->vqs[i]->call)
566 fput(dev->vqs[i]->call);
567 vhost_vq_reset(dev, dev->vqs[i]);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000568 }
Jason Wange0e9b402010-09-14 23:53:05 +0800569 vhost_dev_free_iovecs(dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000570 if (dev->log_ctx)
571 eventfd_ctx_put(dev->log_ctx);
572 dev->log_ctx = NULL;
573 if (dev->log_file)
574 fput(dev->log_file);
575 dev->log_file = NULL;
576 /* No one will access memory at this point */
Igor Mammedov4de72552015-07-01 11:07:09 +0200577 kvfree(dev->memory);
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300578 dev->memory = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200579 WARN_ON(!list_empty(&dev->work_list));
Eric Dumazet78b620c2010-08-31 02:05:57 +0000580 if (dev->worker) {
581 kthread_stop(dev->worker);
582 dev->worker = NULL;
583 }
Michael S. Tsirkin533a19b2010-10-06 15:34:38 +0200584 if (dev->mm)
585 mmput(dev->mm);
586 dev->mm = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000587}
Asias He6ac1afb2013-05-06 16:38:21 +0800588EXPORT_SYMBOL_GPL(vhost_dev_cleanup);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000589
590static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
591{
592 u64 a = addr / VHOST_PAGE_SIZE / 8;
Krishna Kumard47effe2011-03-01 17:06:37 +0530593
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000594 /* Make sure 64 bit math will not overflow. */
595 if (a > ULONG_MAX - (unsigned long)log_base ||
596 a + (unsigned long)log_base > ULONG_MAX)
Dan Carpenter6d97e552010-10-11 19:24:19 +0200597 return 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000598
599 return access_ok(VERIFY_WRITE, log_base + a,
600 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
601}
602
603/* Caller should have vq mutex and device mutex. */
604static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
605 int log_all)
606{
607 int i;
Jeff Dike179b2842010-04-07 09:59:10 -0400608
Michael S. Tsirkinf8322fb2010-05-27 12:28:03 +0300609 if (!mem)
610 return 0;
Jeff Dike179b2842010-04-07 09:59:10 -0400611
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000612 for (i = 0; i < mem->nregions; ++i) {
613 struct vhost_memory_region *m = mem->regions + i;
614 unsigned long a = m->userspace_addr;
615 if (m->memory_size > ULONG_MAX)
616 return 0;
617 else if (!access_ok(VERIFY_WRITE, (void __user *)a,
618 m->memory_size))
619 return 0;
620 else if (log_all && !log_access_ok(log_base,
621 m->guest_phys_addr,
622 m->memory_size))
623 return 0;
624 }
625 return 1;
626}
627
628/* Can we switch to this memory table? */
629/* Caller should have device mutex but not vq mutex */
630static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
631 int log_all)
632{
633 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530634
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000635 for (i = 0; i < d->nvqs; ++i) {
636 int ok;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300637 bool log;
638
Asias He3ab2e422013-04-27 11:16:48 +0800639 mutex_lock(&d->vqs[i]->mutex);
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300640 log = log_all || vhost_has_feature(d->vqs[i], VHOST_F_LOG_ALL);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000641 /* If ring is inactive, will check when it's enabled. */
Asias He3ab2e422013-04-27 11:16:48 +0800642 if (d->vqs[i]->private_data)
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300643 ok = vq_memory_access_ok(d->vqs[i]->log_base, mem, log);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000644 else
645 ok = 1;
Asias He3ab2e422013-04-27 11:16:48 +0800646 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000647 if (!ok)
648 return 0;
649 }
650 return 1;
651}
652
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300653static int vq_access_ok(struct vhost_virtqueue *vq, unsigned int num,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000654 struct vring_desc __user *desc,
655 struct vring_avail __user *avail,
656 struct vring_used __user *used)
657{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300658 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000659 return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
660 access_ok(VERIFY_READ, avail,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300661 sizeof *avail + num * sizeof *avail->ring + s) &&
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000662 access_ok(VERIFY_WRITE, used,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300663 sizeof *used + num * sizeof *used->ring + s);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000664}
665
666/* Can we log writes? */
667/* Caller should have device mutex but not vq mutex */
668int vhost_log_access_ok(struct vhost_dev *dev)
669{
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300670 return memory_access_ok(dev, dev->memory, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000671}
Asias He6ac1afb2013-05-06 16:38:21 +0800672EXPORT_SYMBOL_GPL(vhost_log_access_ok);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000673
674/* Verify access for write logging. */
675/* Caller should have vq mutex and device mutex */
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300676static int vq_log_access_ok(struct vhost_virtqueue *vq,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300677 void __user *log_base)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000678{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300679 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100680
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300681 return vq_memory_access_ok(log_base, vq->memory,
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300682 vhost_has_feature(vq, VHOST_F_LOG_ALL)) &&
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000683 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
684 sizeof *vq->used +
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300685 vq->num * sizeof *vq->used->ring + s));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000686}
687
688/* Can we start vq? */
689/* Caller should have vq mutex and device mutex */
690int vhost_vq_access_ok(struct vhost_virtqueue *vq)
691{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300692 return vq_access_ok(vq, vq->num, vq->desc, vq->avail, vq->used) &&
693 vq_log_access_ok(vq, vq->log_base);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000694}
Asias He6ac1afb2013-05-06 16:38:21 +0800695EXPORT_SYMBOL_GPL(vhost_vq_access_ok);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000696
Igor Mammedovbcfeaca2015-06-16 18:33:35 +0200697static int vhost_memory_reg_sort_cmp(const void *p1, const void *p2)
698{
699 const struct vhost_memory_region *r1 = p1, *r2 = p2;
700 if (r1->guest_phys_addr < r2->guest_phys_addr)
701 return 1;
702 if (r1->guest_phys_addr > r2->guest_phys_addr)
703 return -1;
704 return 0;
705}
706
Igor Mammedov4de72552015-07-01 11:07:09 +0200707static void *vhost_kvzalloc(unsigned long size)
708{
709 void *n = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
710
Igor Mammedov1e099472015-07-15 16:48:50 +0200711 if (!n)
Igor Mammedov4de72552015-07-01 11:07:09 +0200712 n = vzalloc(size);
Igor Mammedov4de72552015-07-01 11:07:09 +0200713 return n;
714}
715
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000716static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
717{
718 struct vhost_memory mem, *newmem, *oldmem;
719 unsigned long size = offsetof(struct vhost_memory, regions);
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +0300720 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530721
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900722 if (copy_from_user(&mem, m, size))
723 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000724 if (mem.padding)
725 return -EOPNOTSUPP;
Igor Mammedovc9ce42f2015-07-02 15:08:11 +0200726 if (mem.nregions > max_mem_regions)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000727 return -E2BIG;
Igor Mammedov4de72552015-07-01 11:07:09 +0200728 newmem = vhost_kvzalloc(size + mem.nregions * sizeof(*m->regions));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000729 if (!newmem)
730 return -ENOMEM;
731
732 memcpy(newmem, &mem, size);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900733 if (copy_from_user(newmem->regions, m->regions,
734 mem.nregions * sizeof *m->regions)) {
Igor Mammedovbcfeaca2015-06-16 18:33:35 +0200735 kvfree(newmem);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900736 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000737 }
Igor Mammedovbcfeaca2015-06-16 18:33:35 +0200738 sort(newmem->regions, newmem->nregions, sizeof(*newmem->regions),
739 vhost_memory_reg_sort_cmp, NULL);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000740
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300741 if (!memory_access_ok(d, newmem, 0)) {
Igor Mammedov4de72552015-07-01 11:07:09 +0200742 kvfree(newmem);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000743 return -EFAULT;
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +0900744 }
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300745 oldmem = d->memory;
746 d->memory = newmem;
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +0300747
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300748 /* All memory accesses are done under some VQ mutex. */
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +0300749 for (i = 0; i < d->nvqs; ++i) {
750 mutex_lock(&d->vqs[i]->mutex);
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300751 d->vqs[i]->memory = newmem;
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +0300752 mutex_unlock(&d->vqs[i]->mutex);
753 }
Igor Mammedov4de72552015-07-01 11:07:09 +0200754 kvfree(oldmem);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000755 return 0;
756}
757
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200758long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000759{
Al Virocecb46f2012-08-27 14:21:39 -0400760 struct file *eventfp, *filep = NULL;
761 bool pollstart = false, pollstop = false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000762 struct eventfd_ctx *ctx = NULL;
763 u32 __user *idxp = argp;
764 struct vhost_virtqueue *vq;
765 struct vhost_vring_state s;
766 struct vhost_vring_file f;
767 struct vhost_vring_addr a;
768 u32 idx;
769 long r;
770
771 r = get_user(idx, idxp);
772 if (r < 0)
773 return r;
Krishna Kumar0f3d9a12010-05-25 11:10:36 +0530774 if (idx >= d->nvqs)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000775 return -ENOBUFS;
776
Asias He3ab2e422013-04-27 11:16:48 +0800777 vq = d->vqs[idx];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000778
779 mutex_lock(&vq->mutex);
780
781 switch (ioctl) {
782 case VHOST_SET_VRING_NUM:
783 /* Resizing ring with an active backend?
784 * You don't want to do that. */
785 if (vq->private_data) {
786 r = -EBUSY;
787 break;
788 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900789 if (copy_from_user(&s, argp, sizeof s)) {
790 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000791 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900792 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000793 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
794 r = -EINVAL;
795 break;
796 }
797 vq->num = s.num;
798 break;
799 case VHOST_SET_VRING_BASE:
800 /* Moving base with an active backend?
801 * You don't want to do that. */
802 if (vq->private_data) {
803 r = -EBUSY;
804 break;
805 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900806 if (copy_from_user(&s, argp, sizeof s)) {
807 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000808 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900809 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000810 if (s.num > 0xffff) {
811 r = -EINVAL;
812 break;
813 }
814 vq->last_avail_idx = s.num;
815 /* Forget the cached index value. */
816 vq->avail_idx = vq->last_avail_idx;
817 break;
818 case VHOST_GET_VRING_BASE:
819 s.index = idx;
820 s.num = vq->last_avail_idx;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900821 if (copy_to_user(argp, &s, sizeof s))
822 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000823 break;
824 case VHOST_SET_VRING_ADDR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900825 if (copy_from_user(&a, argp, sizeof a)) {
826 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000827 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900828 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000829 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
830 r = -EOPNOTSUPP;
831 break;
832 }
833 /* For 32bit, verify that the top 32bits of the user
834 data are set to zero. */
835 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
836 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
837 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
838 r = -EFAULT;
839 break;
840 }
Michael S. Tsirkin5d9a07b2014-12-21 01:00:23 +0200841
842 /* Make sure it's safe to cast pointers to vring types. */
843 BUILD_BUG_ON(__alignof__ *vq->avail > VRING_AVAIL_ALIGN_SIZE);
844 BUILD_BUG_ON(__alignof__ *vq->used > VRING_USED_ALIGN_SIZE);
845 if ((a.avail_user_addr & (VRING_AVAIL_ALIGN_SIZE - 1)) ||
846 (a.used_user_addr & (VRING_USED_ALIGN_SIZE - 1)) ||
Michael S. Tsirkind5424832015-11-16 16:57:08 +0200847 (a.log_guest_addr & (VRING_USED_ALIGN_SIZE - 1))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000848 r = -EINVAL;
849 break;
850 }
851
852 /* We only verify access here if backend is configured.
853 * If it is not, we don't as size might not have been setup.
854 * We will verify when backend is configured. */
855 if (vq->private_data) {
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300856 if (!vq_access_ok(vq, vq->num,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000857 (void __user *)(unsigned long)a.desc_user_addr,
858 (void __user *)(unsigned long)a.avail_user_addr,
859 (void __user *)(unsigned long)a.used_user_addr)) {
860 r = -EINVAL;
861 break;
862 }
863
864 /* Also validate log access for used ring if enabled. */
865 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
866 !log_access_ok(vq->log_base, a.log_guest_addr,
867 sizeof *vq->used +
868 vq->num * sizeof *vq->used->ring)) {
869 r = -EINVAL;
870 break;
871 }
872 }
873
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000874 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
875 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
876 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
877 vq->log_addr = a.log_guest_addr;
878 vq->used = (void __user *)(unsigned long)a.used_user_addr;
879 break;
880 case VHOST_SET_VRING_KICK:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900881 if (copy_from_user(&f, argp, sizeof f)) {
882 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000883 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900884 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000885 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200886 if (IS_ERR(eventfp)) {
887 r = PTR_ERR(eventfp);
888 break;
889 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000890 if (eventfp != vq->kick) {
Al Virocecb46f2012-08-27 14:21:39 -0400891 pollstop = (filep = vq->kick) != NULL;
892 pollstart = (vq->kick = eventfp) != NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000893 } else
894 filep = eventfp;
895 break;
896 case VHOST_SET_VRING_CALL:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900897 if (copy_from_user(&f, argp, sizeof f)) {
898 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000899 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900900 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000901 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200902 if (IS_ERR(eventfp)) {
903 r = PTR_ERR(eventfp);
904 break;
905 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000906 if (eventfp != vq->call) {
907 filep = vq->call;
908 ctx = vq->call_ctx;
909 vq->call = eventfp;
910 vq->call_ctx = eventfp ?
911 eventfd_ctx_fileget(eventfp) : NULL;
912 } else
913 filep = eventfp;
914 break;
915 case VHOST_SET_VRING_ERR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900916 if (copy_from_user(&f, argp, sizeof f)) {
917 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000918 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900919 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000920 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200921 if (IS_ERR(eventfp)) {
922 r = PTR_ERR(eventfp);
923 break;
924 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000925 if (eventfp != vq->error) {
926 filep = vq->error;
927 vq->error = eventfp;
928 ctx = vq->error_ctx;
929 vq->error_ctx = eventfp ?
930 eventfd_ctx_fileget(eventfp) : NULL;
931 } else
932 filep = eventfp;
933 break;
Greg Kurz2751c982015-04-24 14:27:24 +0200934 case VHOST_SET_VRING_ENDIAN:
935 r = vhost_set_vring_endian(vq, argp);
936 break;
937 case VHOST_GET_VRING_ENDIAN:
938 r = vhost_get_vring_endian(vq, idx, argp);
939 break;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000940 default:
941 r = -ENOIOCTLCMD;
942 }
943
944 if (pollstop && vq->handle_kick)
945 vhost_poll_stop(&vq->poll);
946
947 if (ctx)
948 eventfd_ctx_put(ctx);
949 if (filep)
950 fput(filep);
951
952 if (pollstart && vq->handle_kick)
Jason Wang2b8b3282013-01-28 01:05:18 +0000953 r = vhost_poll_start(&vq->poll, vq->kick);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000954
955 mutex_unlock(&vq->mutex);
956
957 if (pollstop && vq->handle_kick)
958 vhost_poll_flush(&vq->poll);
959 return r;
960}
Asias He6ac1afb2013-05-06 16:38:21 +0800961EXPORT_SYMBOL_GPL(vhost_vring_ioctl);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000962
963/* Caller must have device mutex */
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200964long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000965{
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000966 struct file *eventfp, *filep = NULL;
967 struct eventfd_ctx *ctx = NULL;
968 u64 p;
969 long r;
970 int i, fd;
971
972 /* If you are not the owner, you can become one */
973 if (ioctl == VHOST_SET_OWNER) {
974 r = vhost_dev_set_owner(d);
975 goto done;
976 }
977
978 /* You must be the owner to do anything else */
979 r = vhost_dev_check_owner(d);
980 if (r)
981 goto done;
982
983 switch (ioctl) {
984 case VHOST_SET_MEM_TABLE:
985 r = vhost_set_memory(d, argp);
986 break;
987 case VHOST_SET_LOG_BASE:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900988 if (copy_from_user(&p, argp, sizeof p)) {
989 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000990 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900991 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000992 if ((u64)(unsigned long)p != p) {
993 r = -EFAULT;
994 break;
995 }
996 for (i = 0; i < d->nvqs; ++i) {
997 struct vhost_virtqueue *vq;
998 void __user *base = (void __user *)(unsigned long)p;
Asias He3ab2e422013-04-27 11:16:48 +0800999 vq = d->vqs[i];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001000 mutex_lock(&vq->mutex);
1001 /* If ring is inactive, will check when it's enabled. */
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001002 if (vq->private_data && !vq_log_access_ok(vq, base))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001003 r = -EFAULT;
1004 else
1005 vq->log_base = base;
1006 mutex_unlock(&vq->mutex);
1007 }
1008 break;
1009 case VHOST_SET_LOG_FD:
1010 r = get_user(fd, (int __user *)argp);
1011 if (r < 0)
1012 break;
1013 eventfp = fd == -1 ? NULL : eventfd_fget(fd);
1014 if (IS_ERR(eventfp)) {
1015 r = PTR_ERR(eventfp);
1016 break;
1017 }
1018 if (eventfp != d->log_file) {
1019 filep = d->log_file;
Marc-André Lureau7932c0b2015-07-17 15:32:03 +02001020 d->log_file = eventfp;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001021 ctx = d->log_ctx;
1022 d->log_ctx = eventfp ?
1023 eventfd_ctx_fileget(eventfp) : NULL;
1024 } else
1025 filep = eventfp;
1026 for (i = 0; i < d->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +08001027 mutex_lock(&d->vqs[i]->mutex);
1028 d->vqs[i]->log_ctx = d->log_ctx;
1029 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001030 }
1031 if (ctx)
1032 eventfd_ctx_put(ctx);
1033 if (filep)
1034 fput(filep);
1035 break;
1036 default:
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001037 r = -ENOIOCTLCMD;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001038 break;
1039 }
1040done:
1041 return r;
1042}
Asias He6ac1afb2013-05-06 16:38:21 +08001043EXPORT_SYMBOL_GPL(vhost_dev_ioctl);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001044
1045static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
1046 __u64 addr, __u32 len)
1047{
Igor Mammedovbcfeaca2015-06-16 18:33:35 +02001048 const struct vhost_memory_region *reg;
1049 int start = 0, end = mem->nregions;
Krishna Kumard47effe2011-03-01 17:06:37 +05301050
Igor Mammedovbcfeaca2015-06-16 18:33:35 +02001051 while (start < end) {
1052 int slot = start + (end - start) / 2;
1053 reg = mem->regions + slot;
1054 if (addr >= reg->guest_phys_addr)
1055 end = slot;
1056 else
1057 start = slot + 1;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001058 }
Igor Mammedovbcfeaca2015-06-16 18:33:35 +02001059
1060 reg = mem->regions + start;
1061 if (addr >= reg->guest_phys_addr &&
1062 reg->guest_phys_addr + reg->memory_size > addr)
1063 return reg;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001064 return NULL;
1065}
1066
1067/* TODO: This is really inefficient. We need something like get_user()
1068 * (instruction directly accesses the data, with an exception table entry
1069 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
1070 */
1071static int set_bit_to_user(int nr, void __user *addr)
1072{
1073 unsigned long log = (unsigned long)addr;
1074 struct page *page;
1075 void *base;
1076 int bit = nr + (log % PAGE_SIZE) * 8;
1077 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301078
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001079 r = get_user_pages_fast(log, 1, 1, &page);
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +02001080 if (r < 0)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001081 return r;
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +02001082 BUG_ON(r != 1);
Cong Wangc6daa7f2011-11-25 23:14:26 +08001083 base = kmap_atomic(page);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001084 set_bit(bit, base);
Cong Wangc6daa7f2011-11-25 23:14:26 +08001085 kunmap_atomic(base);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001086 set_page_dirty_lock(page);
1087 put_page(page);
1088 return 0;
1089}
1090
1091static int log_write(void __user *log_base,
1092 u64 write_address, u64 write_length)
1093{
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001094 u64 write_page = write_address / VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001095 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301096
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001097 if (!write_length)
1098 return 0;
Michael S. Tsirkin3bf9be42010-11-29 10:19:07 +02001099 write_length += write_address % VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001100 for (;;) {
1101 u64 base = (u64)(unsigned long)log_base;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001102 u64 log = base + write_page / 8;
1103 int bit = write_page % 8;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001104 if ((u64)(unsigned long)log != log)
1105 return -EFAULT;
1106 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
1107 if (r < 0)
1108 return r;
1109 if (write_length <= VHOST_PAGE_SIZE)
1110 break;
1111 write_length -= VHOST_PAGE_SIZE;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001112 write_page += 1;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001113 }
1114 return r;
1115}
1116
1117int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
1118 unsigned int log_num, u64 len)
1119{
1120 int i, r;
1121
1122 /* Make sure data written is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001123 smp_wmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001124 for (i = 0; i < log_num; ++i) {
1125 u64 l = min(log[i].len, len);
1126 r = log_write(vq->log_base, log[i].addr, l);
1127 if (r < 0)
1128 return r;
1129 len -= l;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +02001130 if (!len) {
1131 if (vq->log_ctx)
1132 eventfd_signal(vq->log_ctx, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001133 return 0;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +02001134 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001135 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001136 /* Length written exceeds what we have stored. This is a bug. */
1137 BUG();
1138 return 0;
1139}
Asias He6ac1afb2013-05-06 16:38:21 +08001140EXPORT_SYMBOL_GPL(vhost_log_write);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001141
Jason Wang2723fea2011-06-21 18:04:38 +08001142static int vhost_update_used_flags(struct vhost_virtqueue *vq)
1143{
1144 void __user *used;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001145 if (__put_user(cpu_to_vhost16(vq, vq->used_flags), &vq->used->flags) < 0)
Jason Wang2723fea2011-06-21 18:04:38 +08001146 return -EFAULT;
1147 if (unlikely(vq->log_used)) {
1148 /* Make sure the flag is seen before log. */
1149 smp_wmb();
1150 /* Log used flag write. */
1151 used = &vq->used->flags;
1152 log_write(vq->log_base, vq->log_addr +
1153 (used - (void __user *)vq->used),
1154 sizeof vq->used->flags);
1155 if (vq->log_ctx)
1156 eventfd_signal(vq->log_ctx, 1);
1157 }
1158 return 0;
1159}
1160
1161static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
1162{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001163 if (__put_user(cpu_to_vhost16(vq, vq->avail_idx), vhost_avail_event(vq)))
Jason Wang2723fea2011-06-21 18:04:38 +08001164 return -EFAULT;
1165 if (unlikely(vq->log_used)) {
1166 void __user *used;
1167 /* Make sure the event is seen before log. */
1168 smp_wmb();
1169 /* Log avail event write */
1170 used = vhost_avail_event(vq);
1171 log_write(vq->log_base, vq->log_addr +
1172 (used - (void __user *)vq->used),
1173 sizeof *vhost_avail_event(vq));
1174 if (vq->log_ctx)
1175 eventfd_signal(vq->log_ctx, 1);
1176 }
1177 return 0;
1178}
1179
Greg Kurz80f7d032016-02-16 15:59:44 +01001180int vhost_vq_init_access(struct vhost_virtqueue *vq)
Jason Wang2723fea2011-06-21 18:04:38 +08001181{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001182 __virtio16 last_used_idx;
Jason Wang2723fea2011-06-21 18:04:38 +08001183 int r;
Greg Kurze1f33be2016-02-16 15:54:28 +01001184 bool is_le = vq->is_le;
1185
Greg Kurz2751c982015-04-24 14:27:24 +02001186 if (!vq->private_data) {
Greg Kurzc5072032016-02-16 15:59:34 +01001187 vhost_reset_is_le(vq);
Jason Wang2723fea2011-06-21 18:04:38 +08001188 return 0;
Greg Kurz2751c982015-04-24 14:27:24 +02001189 }
1190
1191 vhost_init_is_le(vq);
Jason Wang2723fea2011-06-21 18:04:38 +08001192
1193 r = vhost_update_used_flags(vq);
1194 if (r)
Greg Kurze1f33be2016-02-16 15:54:28 +01001195 goto err;
Jason Wang2723fea2011-06-21 18:04:38 +08001196 vq->signalled_used_valid = false;
Greg Kurze1f33be2016-02-16 15:54:28 +01001197 if (!access_ok(VERIFY_READ, &vq->used->idx, sizeof vq->used->idx)) {
1198 r = -EFAULT;
1199 goto err;
1200 }
Michael S. Tsirkin64f7f052014-12-01 17:39:39 +02001201 r = __get_user(last_used_idx, &vq->used->idx);
1202 if (r)
Greg Kurze1f33be2016-02-16 15:54:28 +01001203 goto err;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001204 vq->last_used_idx = vhost16_to_cpu(vq, last_used_idx);
Michael S. Tsirkin64f7f052014-12-01 17:39:39 +02001205 return 0;
Greg Kurze1f33be2016-02-16 15:54:28 +01001206err:
1207 vq->is_le = is_le;
1208 return r;
Jason Wang2723fea2011-06-21 18:04:38 +08001209}
Greg Kurz80f7d032016-02-16 15:59:44 +01001210EXPORT_SYMBOL_GPL(vhost_vq_init_access);
Jason Wang2723fea2011-06-21 18:04:38 +08001211
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001212static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001213 struct iovec iov[], int iov_size)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001214{
1215 const struct vhost_memory_region *reg;
1216 struct vhost_memory *mem;
1217 struct iovec *_iov;
1218 u64 s = 0;
1219 int ret = 0;
1220
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001221 mem = vq->memory;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001222 while ((u64)len > s) {
1223 u64 size;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001224 if (unlikely(ret >= iov_size)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001225 ret = -ENOBUFS;
1226 break;
1227 }
1228 reg = find_region(mem, addr, len);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001229 if (unlikely(!reg)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001230 ret = -EFAULT;
1231 break;
1232 }
1233 _iov = iov + ret;
1234 size = reg->memory_size - addr + reg->guest_phys_addr;
Michael S. Tsirkinbd971202012-11-26 05:57:27 +00001235 _iov->iov_len = min((u64)len - s, size);
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001236 _iov->iov_base = (void __user *)(unsigned long)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001237 (reg->userspace_addr + addr - reg->guest_phys_addr);
1238 s += size;
1239 addr += size;
1240 ++ret;
1241 }
1242
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001243 return ret;
1244}
1245
1246/* Each buffer in the virtqueues is actually a chain of descriptors. This
1247 * function returns the next descriptor in the chain,
1248 * or -1U if we're at the end. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001249static unsigned next_desc(struct vhost_virtqueue *vq, struct vring_desc *desc)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001250{
1251 unsigned int next;
1252
1253 /* If this descriptor says it doesn't chain, we're done. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001254 if (!(desc->flags & cpu_to_vhost16(vq, VRING_DESC_F_NEXT)))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001255 return -1U;
1256
1257 /* Check they're not leading us off end of descriptors. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001258 next = vhost16_to_cpu(vq, desc->next);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001259 /* Make sure compiler knows to grab that: we don't want it changing! */
1260 /* We will use the result as an index in an array, so most
1261 * architectures only need a compiler barrier here. */
1262 read_barrier_depends();
1263
1264 return next;
1265}
1266
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001267static int get_indirect(struct vhost_virtqueue *vq,
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001268 struct iovec iov[], unsigned int iov_size,
1269 unsigned int *out_num, unsigned int *in_num,
1270 struct vhost_log *log, unsigned int *log_num,
1271 struct vring_desc *indirect)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001272{
1273 struct vring_desc desc;
1274 unsigned int i = 0, count, found = 0;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001275 u32 len = vhost32_to_cpu(vq, indirect->len);
Al Viroaad9a1c2014-12-10 14:49:01 -05001276 struct iov_iter from;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001277 int ret;
1278
1279 /* Sanity check */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001280 if (unlikely(len % sizeof desc)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001281 vq_err(vq, "Invalid length in indirect descriptor: "
1282 "len 0x%llx not multiple of 0x%zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001283 (unsigned long long)len,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001284 sizeof desc);
1285 return -EINVAL;
1286 }
1287
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001288 ret = translate_desc(vq, vhost64_to_cpu(vq, indirect->addr), len, vq->indirect,
Jason Wange0e9b402010-09-14 23:53:05 +08001289 UIO_MAXIOV);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001290 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001291 vq_err(vq, "Translation failure %d in indirect.\n", ret);
1292 return ret;
1293 }
Al Viroaad9a1c2014-12-10 14:49:01 -05001294 iov_iter_init(&from, READ, vq->indirect, ret, len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001295
1296 /* We will use the result as an address to read from, so most
1297 * architectures only need a compiler barrier here. */
1298 read_barrier_depends();
1299
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001300 count = len / sizeof desc;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001301 /* Buffers are chained via a 16 bit next field, so
1302 * we can have at most 2^16 of these. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001303 if (unlikely(count > USHRT_MAX + 1)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001304 vq_err(vq, "Indirect buffer length too big: %d\n",
1305 indirect->len);
1306 return -E2BIG;
1307 }
1308
1309 do {
1310 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001311 if (unlikely(++found > count)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001312 vq_err(vq, "Loop detected: last one at %u "
1313 "indirect size %u\n",
1314 i, count);
1315 return -EINVAL;
1316 }
Al Viroaad9a1c2014-12-10 14:49:01 -05001317 if (unlikely(copy_from_iter(&desc, sizeof(desc), &from) !=
1318 sizeof(desc))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001319 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001320 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001321 return -EINVAL;
1322 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001323 if (unlikely(desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001324 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001325 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001326 return -EINVAL;
1327 }
1328
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001329 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
1330 vhost32_to_cpu(vq, desc.len), iov + iov_count,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001331 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001332 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001333 vq_err(vq, "Translation failure %d indirect idx %d\n",
1334 ret, i);
1335 return ret;
1336 }
1337 /* If this is an input descriptor, increment that count. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001338 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001339 *in_num += ret;
1340 if (unlikely(log)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001341 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
1342 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001343 ++*log_num;
1344 }
1345 } else {
1346 /* If it's an output descriptor, they're all supposed
1347 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001348 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001349 vq_err(vq, "Indirect descriptor "
1350 "has out after in: idx %d\n", i);
1351 return -EINVAL;
1352 }
1353 *out_num += ret;
1354 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001355 } while ((i = next_desc(vq, &desc)) != -1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001356 return 0;
1357}
1358
1359/* This looks in the virtqueue and for the first available buffer, and converts
1360 * it to an iovec for convenient access. Since descriptors consist of some
1361 * number of output then some number of input descriptors, it's actually two
1362 * iovecs, but we pack them into one and note how many of each there were.
1363 *
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001364 * This function returns the descriptor number found, or vq->num (which is
1365 * never a valid descriptor number) if none was found. A negative code is
1366 * returned on error. */
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001367int vhost_get_vq_desc(struct vhost_virtqueue *vq,
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001368 struct iovec iov[], unsigned int iov_size,
1369 unsigned int *out_num, unsigned int *in_num,
1370 struct vhost_log *log, unsigned int *log_num)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001371{
1372 struct vring_desc desc;
1373 unsigned int i, head, found = 0;
1374 u16 last_avail_idx;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001375 __virtio16 avail_idx;
1376 __virtio16 ring_head;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001377 int ret;
1378
1379 /* Check it isn't doing very strange things with descriptor numbers. */
1380 last_avail_idx = vq->last_avail_idx;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001381 if (unlikely(__get_user(avail_idx, &vq->avail->idx))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001382 vq_err(vq, "Failed to access avail idx at %p\n",
1383 &vq->avail->idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001384 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001385 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001386 vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001387
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001388 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001389 vq_err(vq, "Guest moved used index from %u to %u",
1390 last_avail_idx, vq->avail_idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001391 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001392 }
1393
1394 /* If there's nothing new since last we looked, return invalid. */
1395 if (vq->avail_idx == last_avail_idx)
1396 return vq->num;
1397
1398 /* Only get avail ring entries after they have been exposed by guest. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001399 smp_rmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001400
1401 /* Grab the next descriptor number they're advertising, and increment
1402 * the index we've seen. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001403 if (unlikely(__get_user(ring_head,
Michael S. Tsirkin5fba13b2015-11-29 13:34:44 +02001404 &vq->avail->ring[last_avail_idx & (vq->num - 1)]))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001405 vq_err(vq, "Failed to read head: idx %d address %p\n",
1406 last_avail_idx,
1407 &vq->avail->ring[last_avail_idx % vq->num]);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001408 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001409 }
1410
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001411 head = vhost16_to_cpu(vq, ring_head);
1412
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001413 /* If their number is silly, that's an error. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001414 if (unlikely(head >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001415 vq_err(vq, "Guest says index %u > %u is available",
1416 head, vq->num);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001417 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001418 }
1419
1420 /* When we start there are none of either input nor output. */
1421 *out_num = *in_num = 0;
1422 if (unlikely(log))
1423 *log_num = 0;
1424
1425 i = head;
1426 do {
1427 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001428 if (unlikely(i >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001429 vq_err(vq, "Desc index is %u > %u, head = %u",
1430 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001431 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001432 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001433 if (unlikely(++found > vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001434 vq_err(vq, "Loop detected: last one at %u "
1435 "vq size %u head %u\n",
1436 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001437 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001438 }
Michael S. Tsirkinfcc042a2011-03-06 13:33:49 +02001439 ret = __copy_from_user(&desc, vq->desc + i, sizeof desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001440 if (unlikely(ret)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001441 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
1442 i, vq->desc + i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001443 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001444 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001445 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT)) {
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001446 ret = get_indirect(vq, iov, iov_size,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001447 out_num, in_num,
1448 log, log_num, &desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001449 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001450 vq_err(vq, "Failure detected "
1451 "in indirect descriptor at idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001452 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001453 }
1454 continue;
1455 }
1456
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001457 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
1458 vhost32_to_cpu(vq, desc.len), iov + iov_count,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001459 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001460 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001461 vq_err(vq, "Translation failure %d descriptor idx %d\n",
1462 ret, i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001463 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001464 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001465 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001466 /* If this is an input descriptor,
1467 * increment that count. */
1468 *in_num += ret;
1469 if (unlikely(log)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001470 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
1471 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001472 ++*log_num;
1473 }
1474 } else {
1475 /* If it's an output descriptor, they're all supposed
1476 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001477 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001478 vq_err(vq, "Descriptor has out after in: "
1479 "idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001480 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001481 }
1482 *out_num += ret;
1483 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001484 } while ((i = next_desc(vq, &desc)) != -1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001485
1486 /* On success, increment avail index. */
1487 vq->last_avail_idx++;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001488
1489 /* Assume notifications from guest are disabled at this point,
1490 * if they aren't we would need to update avail_event index. */
1491 BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001492 return head;
1493}
Asias He6ac1afb2013-05-06 16:38:21 +08001494EXPORT_SYMBOL_GPL(vhost_get_vq_desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001495
1496/* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
David Stevens8dd014a2010-07-27 18:52:21 +03001497void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001498{
David Stevens8dd014a2010-07-27 18:52:21 +03001499 vq->last_avail_idx -= n;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001500}
Asias He6ac1afb2013-05-06 16:38:21 +08001501EXPORT_SYMBOL_GPL(vhost_discard_vq_desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001502
1503/* After we've used one of their buffers, we tell them about it. We'll then
1504 * want to notify the guest, using eventfd. */
1505int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
1506{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001507 struct vring_used_elem heads = {
1508 cpu_to_vhost32(vq, head),
1509 cpu_to_vhost32(vq, len)
1510 };
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001511
Jason Wangc49e4e52013-09-02 16:40:58 +08001512 return vhost_add_used_n(vq, &heads, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001513}
Asias He6ac1afb2013-05-06 16:38:21 +08001514EXPORT_SYMBOL_GPL(vhost_add_used);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001515
David Stevens8dd014a2010-07-27 18:52:21 +03001516static int __vhost_add_used_n(struct vhost_virtqueue *vq,
1517 struct vring_used_elem *heads,
1518 unsigned count)
1519{
1520 struct vring_used_elem __user *used;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001521 u16 old, new;
David Stevens8dd014a2010-07-27 18:52:21 +03001522 int start;
1523
Michael S. Tsirkin5fba13b2015-11-29 13:34:44 +02001524 start = vq->last_used_idx & (vq->num - 1);
David Stevens8dd014a2010-07-27 18:52:21 +03001525 used = vq->used->ring + start;
Jason Wangc49e4e52013-09-02 16:40:58 +08001526 if (count == 1) {
1527 if (__put_user(heads[0].id, &used->id)) {
1528 vq_err(vq, "Failed to write used id");
1529 return -EFAULT;
1530 }
1531 if (__put_user(heads[0].len, &used->len)) {
1532 vq_err(vq, "Failed to write used len");
1533 return -EFAULT;
1534 }
1535 } else if (__copy_to_user(used, heads, count * sizeof *used)) {
David Stevens8dd014a2010-07-27 18:52:21 +03001536 vq_err(vq, "Failed to write used");
1537 return -EFAULT;
1538 }
1539 if (unlikely(vq->log_used)) {
1540 /* Make sure data is seen before log. */
1541 smp_wmb();
1542 /* Log used ring entry write. */
1543 log_write(vq->log_base,
1544 vq->log_addr +
1545 ((void __user *)used - (void __user *)vq->used),
1546 count * sizeof *used);
1547 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001548 old = vq->last_used_idx;
1549 new = (vq->last_used_idx += count);
1550 /* If the driver never bothers to signal in a very long while,
1551 * used index might wrap around. If that happens, invalidate
1552 * signalled_used index we stored. TODO: make sure driver
1553 * signals at least once in 2^16 and remove this. */
1554 if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old)))
1555 vq->signalled_used_valid = false;
David Stevens8dd014a2010-07-27 18:52:21 +03001556 return 0;
1557}
1558
1559/* After we've used one of their buffers, we tell them about it. We'll then
1560 * want to notify the guest, using eventfd. */
1561int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
1562 unsigned count)
1563{
1564 int start, n, r;
1565
Michael S. Tsirkin5fba13b2015-11-29 13:34:44 +02001566 start = vq->last_used_idx & (vq->num - 1);
David Stevens8dd014a2010-07-27 18:52:21 +03001567 n = vq->num - start;
1568 if (n < count) {
1569 r = __vhost_add_used_n(vq, heads, n);
1570 if (r < 0)
1571 return r;
1572 heads += n;
1573 count -= n;
1574 }
1575 r = __vhost_add_used_n(vq, heads, count);
1576
1577 /* Make sure buffer is written before we update index. */
1578 smp_wmb();
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001579 if (__put_user(cpu_to_vhost16(vq, vq->last_used_idx), &vq->used->idx)) {
David Stevens8dd014a2010-07-27 18:52:21 +03001580 vq_err(vq, "Failed to increment used idx");
1581 return -EFAULT;
1582 }
1583 if (unlikely(vq->log_used)) {
1584 /* Log used index update. */
1585 log_write(vq->log_base,
1586 vq->log_addr + offsetof(struct vring_used, idx),
1587 sizeof vq->used->idx);
1588 if (vq->log_ctx)
1589 eventfd_signal(vq->log_ctx, 1);
1590 }
1591 return r;
1592}
Asias He6ac1afb2013-05-06 16:38:21 +08001593EXPORT_SYMBOL_GPL(vhost_add_used_n);
David Stevens8dd014a2010-07-27 18:52:21 +03001594
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001595static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001596{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001597 __u16 old, new;
1598 __virtio16 event;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001599 bool v;
Michael S. Tsirkin0d499352010-05-11 19:44:17 +03001600 /* Flush out used index updates. This is paired
1601 * with the barrier that the Guest executes when enabling
1602 * interrupts. */
1603 smp_mb();
1604
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001605 if (vhost_has_feature(vq, VIRTIO_F_NOTIFY_ON_EMPTY) &&
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001606 unlikely(vq->avail_idx == vq->last_avail_idx))
1607 return true;
1608
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001609 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001610 __virtio16 flags;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001611 if (__get_user(flags, &vq->avail->flags)) {
1612 vq_err(vq, "Failed to get flags");
1613 return true;
1614 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001615 return !(flags & cpu_to_vhost16(vq, VRING_AVAIL_F_NO_INTERRUPT));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001616 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001617 old = vq->signalled_used;
1618 v = vq->signalled_used_valid;
1619 new = vq->signalled_used = vq->last_used_idx;
1620 vq->signalled_used_valid = true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001621
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001622 if (unlikely(!v))
1623 return true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001624
Michael S. Tsirkin64f7f052014-12-01 17:39:39 +02001625 if (__get_user(event, vhost_used_event(vq))) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001626 vq_err(vq, "Failed to get used event idx");
1627 return true;
1628 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001629 return vring_need_event(vhost16_to_cpu(vq, event), new, old);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001630}
1631
1632/* This actually signals the guest, using eventfd. */
1633void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
1634{
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001635 /* Signal the Guest tell them we used something up. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001636 if (vq->call_ctx && vhost_notify(dev, vq))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001637 eventfd_signal(vq->call_ctx, 1);
1638}
Asias He6ac1afb2013-05-06 16:38:21 +08001639EXPORT_SYMBOL_GPL(vhost_signal);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001640
1641/* And here's the combo meal deal. Supersize me! */
1642void vhost_add_used_and_signal(struct vhost_dev *dev,
1643 struct vhost_virtqueue *vq,
1644 unsigned int head, int len)
1645{
1646 vhost_add_used(vq, head, len);
1647 vhost_signal(dev, vq);
1648}
Asias He6ac1afb2013-05-06 16:38:21 +08001649EXPORT_SYMBOL_GPL(vhost_add_used_and_signal);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001650
David Stevens8dd014a2010-07-27 18:52:21 +03001651/* multi-buffer version of vhost_add_used_and_signal */
1652void vhost_add_used_and_signal_n(struct vhost_dev *dev,
1653 struct vhost_virtqueue *vq,
1654 struct vring_used_elem *heads, unsigned count)
1655{
1656 vhost_add_used_n(vq, heads, count);
1657 vhost_signal(dev, vq);
1658}
Asias He6ac1afb2013-05-06 16:38:21 +08001659EXPORT_SYMBOL_GPL(vhost_add_used_and_signal_n);
David Stevens8dd014a2010-07-27 18:52:21 +03001660
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001661/* OK, now we need to know about added descriptors. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001662bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001663{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001664 __virtio16 avail_idx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001665 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301666
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001667 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
1668 return false;
1669 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001670 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08001671 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001672 if (r) {
1673 vq_err(vq, "Failed to enable notification at %p: %d\n",
1674 &vq->used->flags, r);
1675 return false;
1676 }
1677 } else {
Jason Wang2723fea2011-06-21 18:04:38 +08001678 r = vhost_update_avail_event(vq, vq->avail_idx);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001679 if (r) {
1680 vq_err(vq, "Failed to update avail event index at %p: %d\n",
1681 vhost_avail_event(vq), r);
1682 return false;
1683 }
1684 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001685 /* They could have slipped one in as we were doing that: make
1686 * sure it's written, then check again. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001687 smp_mb();
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001688 r = __get_user(avail_idx, &vq->avail->idx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001689 if (r) {
1690 vq_err(vq, "Failed to check avail idx at %p: %d\n",
1691 &vq->avail->idx, r);
1692 return false;
1693 }
1694
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001695 return vhost16_to_cpu(vq, avail_idx) != vq->avail_idx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001696}
Asias He6ac1afb2013-05-06 16:38:21 +08001697EXPORT_SYMBOL_GPL(vhost_enable_notify);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001698
1699/* We don't need to be notified again. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001700void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001701{
1702 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301703
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001704 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
1705 return;
1706 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001707 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08001708 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001709 if (r)
1710 vq_err(vq, "Failed to enable notification at %p: %d\n",
1711 &vq->used->flags, r);
1712 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001713}
Asias He6ac1afb2013-05-06 16:38:21 +08001714EXPORT_SYMBOL_GPL(vhost_disable_notify);
1715
1716static int __init vhost_init(void)
1717{
1718 return 0;
1719}
1720
1721static void __exit vhost_exit(void)
1722{
1723}
1724
1725module_init(vhost_init);
1726module_exit(vhost_exit);
1727
1728MODULE_VERSION("0.0.1");
1729MODULE_LICENSE("GPL v2");
1730MODULE_AUTHOR("Michael S. Tsirkin");
1731MODULE_DESCRIPTION("Host kernel accelerator for virtio");