blob: 0061a7bd85f662bc7ea37a53ceab8437a0910e78 [file] [log] [blame]
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001/* Copyright (C) 2009 Red Hat, Inc.
2 * Copyright (C) 2006 Rusty Russell IBM Corporation
3 *
4 * Author: Michael S. Tsirkin <mst@redhat.com>
5 *
6 * Inspiration, some code, and most witty comments come from
Rob Landley61516582011-05-06 09:27:36 -07007 * Documentation/virtual/lguest/lguest.c, by Rusty Russell
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00008 *
9 * This work is licensed under the terms of the GNU GPL, version 2.
10 *
11 * Generic code for virtio server in host kernel.
12 */
13
14#include <linux/eventfd.h>
15#include <linux/vhost.h>
Asias He35596b22013-08-19 09:23:19 +080016#include <linux/uio.h>
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000017#include <linux/mm.h>
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +020018#include <linux/mmu_context.h>
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000019#include <linux/miscdevice.h>
20#include <linux/mutex.h>
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000021#include <linux/poll.h>
22#include <linux/file.h>
23#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Igor Mammedov4de72552015-07-01 11:07:09 +020025#include <linux/vmalloc.h>
Tejun Heoc23f34452010-06-02 20:40:00 +020026#include <linux/kthread.h>
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +030027#include <linux/cgroup.h>
Asias He6ac1afb2013-05-06 16:38:21 +080028#include <linux/module.h>
Igor Mammedovbcfeaca2015-06-16 18:33:35 +020029#include <linux/sort.h>
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000030
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000031#include "vhost.h"
32
Igor Mammedovc9ce42f2015-07-02 15:08:11 +020033static ushort max_mem_regions = 64;
34module_param(max_mem_regions, ushort, 0444);
35MODULE_PARM_DESC(max_mem_regions,
36 "Maximum number of memory regions in memory map. (default: 64)");
37
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000038enum {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000039 VHOST_MEMORY_F_LOG = 0x1,
40};
41
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +030042#define vhost_used_event(vq) ((__virtio16 __user *)&vq->avail->ring[vq->num])
43#define vhost_avail_event(vq) ((__virtio16 __user *)&vq->used->ring[vq->num])
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +030044
Greg Kurz2751c982015-04-24 14:27:24 +020045#ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
Greg Kurzc5072032016-02-16 15:59:34 +010046static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
Greg Kurz2751c982015-04-24 14:27:24 +020047{
48 vq->user_be = !virtio_legacy_is_little_endian();
49}
50
Greg Kurzc5072032016-02-16 15:59:34 +010051static void vhost_enable_cross_endian_big(struct vhost_virtqueue *vq)
52{
53 vq->user_be = true;
54}
55
56static void vhost_enable_cross_endian_little(struct vhost_virtqueue *vq)
57{
58 vq->user_be = false;
59}
60
Greg Kurz2751c982015-04-24 14:27:24 +020061static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
62{
63 struct vhost_vring_state s;
64
65 if (vq->private_data)
66 return -EBUSY;
67
68 if (copy_from_user(&s, argp, sizeof(s)))
69 return -EFAULT;
70
71 if (s.num != VHOST_VRING_LITTLE_ENDIAN &&
72 s.num != VHOST_VRING_BIG_ENDIAN)
73 return -EINVAL;
74
Greg Kurzc5072032016-02-16 15:59:34 +010075 if (s.num == VHOST_VRING_BIG_ENDIAN)
76 vhost_enable_cross_endian_big(vq);
77 else
78 vhost_enable_cross_endian_little(vq);
Greg Kurz2751c982015-04-24 14:27:24 +020079
80 return 0;
81}
82
83static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
84 int __user *argp)
85{
86 struct vhost_vring_state s = {
87 .index = idx,
88 .num = vq->user_be
89 };
90
91 if (copy_to_user(argp, &s, sizeof(s)))
92 return -EFAULT;
93
94 return 0;
95}
96
97static void vhost_init_is_le(struct vhost_virtqueue *vq)
98{
99 /* Note for legacy virtio: user_be is initialized at reset time
100 * according to the host endianness. If userspace does not set an
101 * explicit endianness, the default behavior is native endian, as
102 * expected by legacy virtio.
103 */
104 vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) || !vq->user_be;
105}
106#else
Greg Kurzc5072032016-02-16 15:59:34 +0100107static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
Greg Kurz2751c982015-04-24 14:27:24 +0200108{
109}
110
111static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
112{
113 return -ENOIOCTLCMD;
114}
115
116static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
117 int __user *argp)
118{
119 return -ENOIOCTLCMD;
120}
121
122static void vhost_init_is_le(struct vhost_virtqueue *vq)
123{
124 if (vhost_has_feature(vq, VIRTIO_F_VERSION_1))
125 vq->is_le = true;
126}
127#endif /* CONFIG_VHOST_CROSS_ENDIAN_LEGACY */
128
Greg Kurzc5072032016-02-16 15:59:34 +0100129static void vhost_reset_is_le(struct vhost_virtqueue *vq)
130{
131 vq->is_le = virtio_legacy_is_little_endian();
132}
133
Jason Wang7235acd2016-04-25 22:14:32 -0400134struct vhost_flush_struct {
135 struct vhost_work work;
136 struct completion wait_event;
137};
138
139static void vhost_flush_work(struct vhost_work *work)
140{
141 struct vhost_flush_struct *s;
142
143 s = container_of(work, struct vhost_flush_struct, work);
144 complete(&s->wait_event);
145}
146
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000147static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
148 poll_table *pt)
149{
150 struct vhost_poll *poll;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000151
Krishna Kumard47effe2011-03-01 17:06:37 +0530152 poll = container_of(pt, struct vhost_poll, table);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000153 poll->wqh = wqh;
154 add_wait_queue(wqh, &poll->wait);
155}
156
157static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync,
158 void *key)
159{
Tejun Heoc23f34452010-06-02 20:40:00 +0200160 struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
161
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000162 if (!((unsigned long)key & poll->mask))
163 return 0;
164
Tejun Heoc23f34452010-06-02 20:40:00 +0200165 vhost_poll_queue(poll);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000166 return 0;
167}
168
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000169void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000170{
Jason Wang04b96e52016-04-25 22:14:33 -0400171 clear_bit(VHOST_WORK_QUEUED, &work->flags);
Tejun Heoc23f34452010-06-02 20:40:00 +0200172 work->fn = fn;
173 init_waitqueue_head(&work->done);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000174}
Asias He6ac1afb2013-05-06 16:38:21 +0800175EXPORT_SYMBOL_GPL(vhost_work_init);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000176
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300177/* Init poll structure */
178void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
179 unsigned long mask, struct vhost_dev *dev)
180{
181 init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
182 init_poll_funcptr(&poll->table, vhost_poll_func);
183 poll->mask = mask;
184 poll->dev = dev;
Jason Wang2b8b3282013-01-28 01:05:18 +0000185 poll->wqh = NULL;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300186
187 vhost_work_init(&poll->work, fn);
188}
Asias He6ac1afb2013-05-06 16:38:21 +0800189EXPORT_SYMBOL_GPL(vhost_poll_init);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300190
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000191/* Start polling a file. We add ourselves to file's wait queue. The caller must
192 * keep a reference to a file until after vhost_poll_stop is called. */
Jason Wang2b8b3282013-01-28 01:05:18 +0000193int vhost_poll_start(struct vhost_poll *poll, struct file *file)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000194{
195 unsigned long mask;
Jason Wang2b8b3282013-01-28 01:05:18 +0000196 int ret = 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530197
Jason Wang70181d512013-04-10 20:50:48 +0000198 if (poll->wqh)
199 return 0;
200
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000201 mask = file->f_op->poll(file, &poll->table);
202 if (mask)
203 vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
Jason Wang2b8b3282013-01-28 01:05:18 +0000204 if (mask & POLLERR) {
205 if (poll->wqh)
206 remove_wait_queue(poll->wqh, &poll->wait);
207 ret = -EINVAL;
208 }
209
210 return ret;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000211}
Asias He6ac1afb2013-05-06 16:38:21 +0800212EXPORT_SYMBOL_GPL(vhost_poll_start);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000213
214/* Stop polling a file. After this function returns, it becomes safe to drop the
215 * file reference. You must also flush afterwards. */
216void vhost_poll_stop(struct vhost_poll *poll)
217{
Jason Wang2b8b3282013-01-28 01:05:18 +0000218 if (poll->wqh) {
219 remove_wait_queue(poll->wqh, &poll->wait);
220 poll->wqh = NULL;
221 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000222}
Asias He6ac1afb2013-05-06 16:38:21 +0800223EXPORT_SYMBOL_GPL(vhost_poll_stop);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000224
Asias He6ac1afb2013-05-06 16:38:21 +0800225void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000226{
Jason Wang7235acd2016-04-25 22:14:32 -0400227 struct vhost_flush_struct flush;
Tejun Heoc23f34452010-06-02 20:40:00 +0200228
Jason Wang7235acd2016-04-25 22:14:32 -0400229 if (dev->worker) {
230 init_completion(&flush.wait_event);
231 vhost_work_init(&flush.work, vhost_flush_work);
232
233 vhost_work_queue(dev, &flush.work);
234 wait_for_completion(&flush.wait_event);
235 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000236}
Asias He6ac1afb2013-05-06 16:38:21 +0800237EXPORT_SYMBOL_GPL(vhost_work_flush);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000238
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300239/* Flush any work that has been scheduled. When calling this, don't hold any
240 * locks that are also used by the callback. */
241void vhost_poll_flush(struct vhost_poll *poll)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000242{
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300243 vhost_work_flush(poll->dev, &poll->work);
244}
Asias He6ac1afb2013-05-06 16:38:21 +0800245EXPORT_SYMBOL_GPL(vhost_poll_flush);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300246
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000247void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300248{
Jason Wang04b96e52016-04-25 22:14:33 -0400249 if (!dev->worker)
250 return;
Tejun Heoc23f34452010-06-02 20:40:00 +0200251
Jason Wang04b96e52016-04-25 22:14:33 -0400252 if (!test_and_set_bit(VHOST_WORK_QUEUED, &work->flags)) {
253 /* We can only add the work to the list after we're
254 * sure it was not in the list.
255 */
256 smp_mb();
257 llist_add(&work->node, &dev->work_list);
Tejun Heoc23f34452010-06-02 20:40:00 +0200258 wake_up_process(dev->worker);
259 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000260}
Asias He6ac1afb2013-05-06 16:38:21 +0800261EXPORT_SYMBOL_GPL(vhost_work_queue);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000262
Jason Wang526d3e72016-03-04 06:24:51 -0500263/* A lockless hint for busy polling code to exit the loop */
264bool vhost_has_work(struct vhost_dev *dev)
265{
Jason Wang04b96e52016-04-25 22:14:33 -0400266 return !llist_empty(&dev->work_list);
Jason Wang526d3e72016-03-04 06:24:51 -0500267}
268EXPORT_SYMBOL_GPL(vhost_has_work);
269
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300270void vhost_poll_queue(struct vhost_poll *poll)
271{
272 vhost_work_queue(poll->dev, &poll->work);
273}
Asias He6ac1afb2013-05-06 16:38:21 +0800274EXPORT_SYMBOL_GPL(vhost_poll_queue);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300275
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000276static void vhost_vq_reset(struct vhost_dev *dev,
277 struct vhost_virtqueue *vq)
278{
279 vq->num = 1;
280 vq->desc = NULL;
281 vq->avail = NULL;
282 vq->used = NULL;
283 vq->last_avail_idx = 0;
284 vq->avail_idx = 0;
285 vq->last_used_idx = 0;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300286 vq->signalled_used = 0;
287 vq->signalled_used_valid = false;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000288 vq->used_flags = 0;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000289 vq->log_used = false;
290 vq->log_addr = -1ull;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000291 vq->private_data = NULL;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300292 vq->acked_features = 0;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000293 vq->log_base = NULL;
294 vq->error_ctx = NULL;
295 vq->error = NULL;
296 vq->kick = NULL;
297 vq->call_ctx = NULL;
298 vq->call = NULL;
Michael S. Tsirkin73a99f02010-02-23 11:23:45 +0200299 vq->log_ctx = NULL;
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300300 vq->memory = NULL;
Greg Kurzc5072032016-02-16 15:59:34 +0100301 vhost_reset_is_le(vq);
302 vhost_disable_cross_endian(vq);
Jason Wang03088132016-03-04 06:24:53 -0500303 vq->busyloop_timeout = 0;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000304}
305
Tejun Heoc23f34452010-06-02 20:40:00 +0200306static int vhost_worker(void *data)
307{
308 struct vhost_dev *dev = data;
Jason Wang04b96e52016-04-25 22:14:33 -0400309 struct vhost_work *work, *work_next;
310 struct llist_node *node;
Jens Freimannd7ffde32012-06-26 00:59:58 +0000311 mm_segment_t oldfs = get_fs();
Tejun Heoc23f34452010-06-02 20:40:00 +0200312
Jens Freimannd7ffde32012-06-26 00:59:58 +0000313 set_fs(USER_DS);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200314 use_mm(dev->mm);
315
Tejun Heoc23f34452010-06-02 20:40:00 +0200316 for (;;) {
317 /* mb paired w/ kthread_stop */
318 set_current_state(TASK_INTERRUPTIBLE);
319
Tejun Heoc23f34452010-06-02 20:40:00 +0200320 if (kthread_should_stop()) {
Tejun Heoc23f34452010-06-02 20:40:00 +0200321 __set_current_state(TASK_RUNNING);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200322 break;
Tejun Heoc23f34452010-06-02 20:40:00 +0200323 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200324
Jason Wang04b96e52016-04-25 22:14:33 -0400325 node = llist_del_all(&dev->work_list);
326 if (!node)
327 schedule();
328
329 node = llist_reverse_order(node);
330 /* make sure flag is seen after deletion */
331 smp_wmb();
332 llist_for_each_entry_safe(work, work_next, node, node) {
333 clear_bit(VHOST_WORK_QUEUED, &work->flags);
Tejun Heoc23f34452010-06-02 20:40:00 +0200334 __set_current_state(TASK_RUNNING);
335 work->fn(work);
Nadav Har'Eld550dda2012-02-27 15:07:29 +0200336 if (need_resched())
337 schedule();
Jason Wang04b96e52016-04-25 22:14:33 -0400338 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200339 }
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200340 unuse_mm(dev->mm);
Jens Freimannd7ffde32012-06-26 00:59:58 +0000341 set_fs(oldfs);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200342 return 0;
Tejun Heoc23f34452010-06-02 20:40:00 +0200343}
344
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000345static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
346{
347 kfree(vq->indirect);
348 vq->indirect = NULL;
349 kfree(vq->log);
350 vq->log = NULL;
351 kfree(vq->heads);
352 vq->heads = NULL;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000353}
354
Jason Wange0e9b402010-09-14 23:53:05 +0800355/* Helper to allocate iovec buffers for all vqs. */
356static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
357{
Asias He6d5e6aa2013-05-06 16:38:23 +0800358 struct vhost_virtqueue *vq;
Jason Wange0e9b402010-09-14 23:53:05 +0800359 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530360
Jason Wange0e9b402010-09-14 23:53:05 +0800361 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800362 vq = dev->vqs[i];
363 vq->indirect = kmalloc(sizeof *vq->indirect * UIO_MAXIOV,
364 GFP_KERNEL);
365 vq->log = kmalloc(sizeof *vq->log * UIO_MAXIOV, GFP_KERNEL);
366 vq->heads = kmalloc(sizeof *vq->heads * UIO_MAXIOV, GFP_KERNEL);
367 if (!vq->indirect || !vq->log || !vq->heads)
Jason Wange0e9b402010-09-14 23:53:05 +0800368 goto err_nomem;
369 }
370 return 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530371
Jason Wange0e9b402010-09-14 23:53:05 +0800372err_nomem:
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000373 for (; i >= 0; --i)
Asias He3ab2e422013-04-27 11:16:48 +0800374 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800375 return -ENOMEM;
376}
377
378static void vhost_dev_free_iovecs(struct vhost_dev *dev)
379{
380 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530381
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000382 for (i = 0; i < dev->nvqs; ++i)
Asias He3ab2e422013-04-27 11:16:48 +0800383 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800384}
385
Zhi Yong Wu59566b6e2013-12-07 04:13:03 +0800386void vhost_dev_init(struct vhost_dev *dev,
Asias He3ab2e422013-04-27 11:16:48 +0800387 struct vhost_virtqueue **vqs, int nvqs)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000388{
Asias He6d5e6aa2013-05-06 16:38:23 +0800389 struct vhost_virtqueue *vq;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000390 int i;
Tejun Heoc23f34452010-06-02 20:40:00 +0200391
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000392 dev->vqs = vqs;
393 dev->nvqs = nvqs;
394 mutex_init(&dev->mutex);
395 dev->log_ctx = NULL;
396 dev->log_file = NULL;
397 dev->memory = NULL;
398 dev->mm = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200399 dev->worker = NULL;
Jason Wang04b96e52016-04-25 22:14:33 -0400400 init_llist_head(&dev->work_list);
401
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000402
403 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800404 vq = dev->vqs[i];
405 vq->log = NULL;
406 vq->indirect = NULL;
407 vq->heads = NULL;
408 vq->dev = dev;
409 mutex_init(&vq->mutex);
410 vhost_vq_reset(dev, vq);
411 if (vq->handle_kick)
412 vhost_poll_init(&vq->poll, vq->handle_kick,
413 POLLIN, dev);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000414 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000415}
Asias He6ac1afb2013-05-06 16:38:21 +0800416EXPORT_SYMBOL_GPL(vhost_dev_init);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000417
418/* Caller should have device mutex */
419long vhost_dev_check_owner(struct vhost_dev *dev)
420{
421 /* Are you the owner? If not, I don't think you mean to do that */
422 return dev->mm == current->mm ? 0 : -EPERM;
423}
Asias He6ac1afb2013-05-06 16:38:21 +0800424EXPORT_SYMBOL_GPL(vhost_dev_check_owner);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000425
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300426struct vhost_attach_cgroups_struct {
Krishna Kumard47effe2011-03-01 17:06:37 +0530427 struct vhost_work work;
428 struct task_struct *owner;
429 int ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300430};
431
432static void vhost_attach_cgroups_work(struct vhost_work *work)
433{
Krishna Kumard47effe2011-03-01 17:06:37 +0530434 struct vhost_attach_cgroups_struct *s;
435
436 s = container_of(work, struct vhost_attach_cgroups_struct, work);
437 s->ret = cgroup_attach_task_all(s->owner, current);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300438}
439
440static int vhost_attach_cgroups(struct vhost_dev *dev)
441{
Krishna Kumard47effe2011-03-01 17:06:37 +0530442 struct vhost_attach_cgroups_struct attach;
443
444 attach.owner = current;
445 vhost_work_init(&attach.work, vhost_attach_cgroups_work);
446 vhost_work_queue(dev, &attach.work);
447 vhost_work_flush(dev, &attach.work);
448 return attach.ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300449}
450
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000451/* Caller should have device mutex */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300452bool vhost_dev_has_owner(struct vhost_dev *dev)
453{
454 return dev->mm;
455}
Asias He6ac1afb2013-05-06 16:38:21 +0800456EXPORT_SYMBOL_GPL(vhost_dev_has_owner);
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300457
458/* Caller should have device mutex */
Asias He54db63c2013-05-06 11:15:59 +0800459long vhost_dev_set_owner(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000460{
Tejun Heoc23f34452010-06-02 20:40:00 +0200461 struct task_struct *worker;
462 int err;
Krishna Kumard47effe2011-03-01 17:06:37 +0530463
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000464 /* Is there an owner already? */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300465 if (vhost_dev_has_owner(dev)) {
Tejun Heoc23f34452010-06-02 20:40:00 +0200466 err = -EBUSY;
467 goto err_mm;
468 }
Krishna Kumard47effe2011-03-01 17:06:37 +0530469
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000470 /* No owner, become one */
471 dev->mm = get_task_mm(current);
Tejun Heoc23f34452010-06-02 20:40:00 +0200472 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
473 if (IS_ERR(worker)) {
474 err = PTR_ERR(worker);
475 goto err_worker;
476 }
477
478 dev->worker = worker;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300479 wake_up_process(worker); /* avoid contributing to loadavg */
480
481 err = vhost_attach_cgroups(dev);
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300482 if (err)
483 goto err_cgroup;
Tejun Heoc23f34452010-06-02 20:40:00 +0200484
Jason Wange0e9b402010-09-14 23:53:05 +0800485 err = vhost_dev_alloc_iovecs(dev);
486 if (err)
487 goto err_cgroup;
488
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000489 return 0;
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300490err_cgroup:
491 kthread_stop(worker);
Michael S. Tsirkin615cc222010-09-02 14:16:36 +0300492 dev->worker = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200493err_worker:
494 if (dev->mm)
495 mmput(dev->mm);
496 dev->mm = NULL;
497err_mm:
498 return err;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000499}
Asias He6ac1afb2013-05-06 16:38:21 +0800500EXPORT_SYMBOL_GPL(vhost_dev_set_owner);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000501
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300502struct vhost_memory *vhost_dev_reset_owner_prepare(void)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000503{
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300504 return kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
505}
Asias He6ac1afb2013-05-06 16:38:21 +0800506EXPORT_SYMBOL_GPL(vhost_dev_reset_owner_prepare);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000507
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300508/* Caller should have device mutex */
509void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_memory *memory)
510{
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300511 int i;
512
Michael S. Tsirkinea5d4042011-11-27 19:05:58 +0200513 vhost_dev_cleanup(dev, true);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000514
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300515 /* Restore memory to default empty mapping. */
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000516 memory->nregions = 0;
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300517 dev->memory = memory;
518 /* We don't need VQ locks below since vhost_dev_cleanup makes sure
519 * VQs aren't running.
520 */
521 for (i = 0; i < dev->nvqs; ++i)
522 dev->vqs[i]->memory = memory;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000523}
Asias He6ac1afb2013-05-06 16:38:21 +0800524EXPORT_SYMBOL_GPL(vhost_dev_reset_owner);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000525
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000526void vhost_dev_stop(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000527{
528 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530529
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000530 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800531 if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) {
532 vhost_poll_stop(&dev->vqs[i]->poll);
533 vhost_poll_flush(&dev->vqs[i]->poll);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000534 }
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000535 }
536}
Asias He6ac1afb2013-05-06 16:38:21 +0800537EXPORT_SYMBOL_GPL(vhost_dev_stop);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000538
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000539/* Caller should have device mutex if and only if locked is set */
540void vhost_dev_cleanup(struct vhost_dev *dev, bool locked)
541{
542 int i;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000543
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000544 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800545 if (dev->vqs[i]->error_ctx)
546 eventfd_ctx_put(dev->vqs[i]->error_ctx);
547 if (dev->vqs[i]->error)
548 fput(dev->vqs[i]->error);
549 if (dev->vqs[i]->kick)
550 fput(dev->vqs[i]->kick);
551 if (dev->vqs[i]->call_ctx)
552 eventfd_ctx_put(dev->vqs[i]->call_ctx);
553 if (dev->vqs[i]->call)
554 fput(dev->vqs[i]->call);
555 vhost_vq_reset(dev, dev->vqs[i]);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000556 }
Jason Wange0e9b402010-09-14 23:53:05 +0800557 vhost_dev_free_iovecs(dev);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000558 if (dev->log_ctx)
559 eventfd_ctx_put(dev->log_ctx);
560 dev->log_ctx = NULL;
561 if (dev->log_file)
562 fput(dev->log_file);
563 dev->log_file = NULL;
564 /* No one will access memory at this point */
Igor Mammedov4de72552015-07-01 11:07:09 +0200565 kvfree(dev->memory);
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300566 dev->memory = NULL;
Jason Wang04b96e52016-04-25 22:14:33 -0400567 WARN_ON(!llist_empty(&dev->work_list));
Eric Dumazet78b620c2010-08-31 02:05:57 +0000568 if (dev->worker) {
569 kthread_stop(dev->worker);
570 dev->worker = NULL;
571 }
Michael S. Tsirkin533a19b2010-10-06 15:34:38 +0200572 if (dev->mm)
573 mmput(dev->mm);
574 dev->mm = NULL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000575}
Asias He6ac1afb2013-05-06 16:38:21 +0800576EXPORT_SYMBOL_GPL(vhost_dev_cleanup);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000577
578static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
579{
580 u64 a = addr / VHOST_PAGE_SIZE / 8;
Krishna Kumard47effe2011-03-01 17:06:37 +0530581
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000582 /* Make sure 64 bit math will not overflow. */
583 if (a > ULONG_MAX - (unsigned long)log_base ||
584 a + (unsigned long)log_base > ULONG_MAX)
Dan Carpenter6d97e552010-10-11 19:24:19 +0200585 return 0;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000586
587 return access_ok(VERIFY_WRITE, log_base + a,
588 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
589}
590
591/* Caller should have vq mutex and device mutex. */
592static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
593 int log_all)
594{
595 int i;
Jeff Dike179b2842010-04-07 09:59:10 -0400596
Michael S. Tsirkinf8322fb2010-05-27 12:28:03 +0300597 if (!mem)
598 return 0;
Jeff Dike179b2842010-04-07 09:59:10 -0400599
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000600 for (i = 0; i < mem->nregions; ++i) {
601 struct vhost_memory_region *m = mem->regions + i;
602 unsigned long a = m->userspace_addr;
603 if (m->memory_size > ULONG_MAX)
604 return 0;
605 else if (!access_ok(VERIFY_WRITE, (void __user *)a,
606 m->memory_size))
607 return 0;
608 else if (log_all && !log_access_ok(log_base,
609 m->guest_phys_addr,
610 m->memory_size))
611 return 0;
612 }
613 return 1;
614}
615
616/* Can we switch to this memory table? */
617/* Caller should have device mutex but not vq mutex */
618static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
619 int log_all)
620{
621 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530622
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000623 for (i = 0; i < d->nvqs; ++i) {
624 int ok;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300625 bool log;
626
Asias He3ab2e422013-04-27 11:16:48 +0800627 mutex_lock(&d->vqs[i]->mutex);
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300628 log = log_all || vhost_has_feature(d->vqs[i], VHOST_F_LOG_ALL);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000629 /* If ring is inactive, will check when it's enabled. */
Asias He3ab2e422013-04-27 11:16:48 +0800630 if (d->vqs[i]->private_data)
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300631 ok = vq_memory_access_ok(d->vqs[i]->log_base, mem, log);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000632 else
633 ok = 1;
Asias He3ab2e422013-04-27 11:16:48 +0800634 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000635 if (!ok)
636 return 0;
637 }
638 return 1;
639}
640
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300641static int vq_access_ok(struct vhost_virtqueue *vq, unsigned int num,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000642 struct vring_desc __user *desc,
643 struct vring_avail __user *avail,
644 struct vring_used __user *used)
645{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300646 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000647 return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
648 access_ok(VERIFY_READ, avail,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300649 sizeof *avail + num * sizeof *avail->ring + s) &&
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000650 access_ok(VERIFY_WRITE, used,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300651 sizeof *used + num * sizeof *used->ring + s);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000652}
653
654/* Can we log writes? */
655/* Caller should have device mutex but not vq mutex */
656int vhost_log_access_ok(struct vhost_dev *dev)
657{
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300658 return memory_access_ok(dev, dev->memory, 1);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000659}
Asias He6ac1afb2013-05-06 16:38:21 +0800660EXPORT_SYMBOL_GPL(vhost_log_access_ok);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000661
662/* Verify access for write logging. */
663/* Caller should have vq mutex and device mutex */
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300664static int vq_log_access_ok(struct vhost_virtqueue *vq,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300665 void __user *log_base)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000666{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300667 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100668
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300669 return vq_memory_access_ok(log_base, vq->memory,
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300670 vhost_has_feature(vq, VHOST_F_LOG_ALL)) &&
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000671 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
672 sizeof *vq->used +
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300673 vq->num * sizeof *vq->used->ring + s));
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000674}
675
676/* Can we start vq? */
677/* Caller should have vq mutex and device mutex */
678int vhost_vq_access_ok(struct vhost_virtqueue *vq)
679{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300680 return vq_access_ok(vq, vq->num, vq->desc, vq->avail, vq->used) &&
681 vq_log_access_ok(vq, vq->log_base);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000682}
Asias He6ac1afb2013-05-06 16:38:21 +0800683EXPORT_SYMBOL_GPL(vhost_vq_access_ok);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000684
Igor Mammedovbcfeaca2015-06-16 18:33:35 +0200685static int vhost_memory_reg_sort_cmp(const void *p1, const void *p2)
686{
687 const struct vhost_memory_region *r1 = p1, *r2 = p2;
688 if (r1->guest_phys_addr < r2->guest_phys_addr)
689 return 1;
690 if (r1->guest_phys_addr > r2->guest_phys_addr)
691 return -1;
692 return 0;
693}
694
Igor Mammedov4de72552015-07-01 11:07:09 +0200695static void *vhost_kvzalloc(unsigned long size)
696{
697 void *n = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
698
Igor Mammedov1e099472015-07-15 16:48:50 +0200699 if (!n)
Igor Mammedov4de72552015-07-01 11:07:09 +0200700 n = vzalloc(size);
Igor Mammedov4de72552015-07-01 11:07:09 +0200701 return n;
702}
703
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000704static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
705{
706 struct vhost_memory mem, *newmem, *oldmem;
707 unsigned long size = offsetof(struct vhost_memory, regions);
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +0300708 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530709
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900710 if (copy_from_user(&mem, m, size))
711 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000712 if (mem.padding)
713 return -EOPNOTSUPP;
Igor Mammedovc9ce42f2015-07-02 15:08:11 +0200714 if (mem.nregions > max_mem_regions)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000715 return -E2BIG;
Igor Mammedov4de72552015-07-01 11:07:09 +0200716 newmem = vhost_kvzalloc(size + mem.nregions * sizeof(*m->regions));
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000717 if (!newmem)
718 return -ENOMEM;
719
720 memcpy(newmem, &mem, size);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900721 if (copy_from_user(newmem->regions, m->regions,
722 mem.nregions * sizeof *m->regions)) {
Igor Mammedovbcfeaca2015-06-16 18:33:35 +0200723 kvfree(newmem);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900724 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000725 }
Igor Mammedovbcfeaca2015-06-16 18:33:35 +0200726 sort(newmem->regions, newmem->nregions, sizeof(*newmem->regions),
727 vhost_memory_reg_sort_cmp, NULL);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000728
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300729 if (!memory_access_ok(d, newmem, 0)) {
Igor Mammedov4de72552015-07-01 11:07:09 +0200730 kvfree(newmem);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000731 return -EFAULT;
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +0900732 }
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300733 oldmem = d->memory;
734 d->memory = newmem;
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +0300735
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300736 /* All memory accesses are done under some VQ mutex. */
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +0300737 for (i = 0; i < d->nvqs; ++i) {
738 mutex_lock(&d->vqs[i]->mutex);
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300739 d->vqs[i]->memory = newmem;
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +0300740 mutex_unlock(&d->vqs[i]->mutex);
741 }
Igor Mammedov4de72552015-07-01 11:07:09 +0200742 kvfree(oldmem);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000743 return 0;
744}
745
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200746long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000747{
Al Virocecb46f2012-08-27 14:21:39 -0400748 struct file *eventfp, *filep = NULL;
749 bool pollstart = false, pollstop = false;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000750 struct eventfd_ctx *ctx = NULL;
751 u32 __user *idxp = argp;
752 struct vhost_virtqueue *vq;
753 struct vhost_vring_state s;
754 struct vhost_vring_file f;
755 struct vhost_vring_addr a;
756 u32 idx;
757 long r;
758
759 r = get_user(idx, idxp);
760 if (r < 0)
761 return r;
Krishna Kumar0f3d9a12010-05-25 11:10:36 +0530762 if (idx >= d->nvqs)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000763 return -ENOBUFS;
764
Asias He3ab2e422013-04-27 11:16:48 +0800765 vq = d->vqs[idx];
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000766
767 mutex_lock(&vq->mutex);
768
769 switch (ioctl) {
770 case VHOST_SET_VRING_NUM:
771 /* Resizing ring with an active backend?
772 * You don't want to do that. */
773 if (vq->private_data) {
774 r = -EBUSY;
775 break;
776 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900777 if (copy_from_user(&s, argp, sizeof s)) {
778 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000779 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900780 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000781 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
782 r = -EINVAL;
783 break;
784 }
785 vq->num = s.num;
786 break;
787 case VHOST_SET_VRING_BASE:
788 /* Moving base with an active backend?
789 * You don't want to do that. */
790 if (vq->private_data) {
791 r = -EBUSY;
792 break;
793 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900794 if (copy_from_user(&s, argp, sizeof s)) {
795 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000796 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900797 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000798 if (s.num > 0xffff) {
799 r = -EINVAL;
800 break;
801 }
802 vq->last_avail_idx = s.num;
803 /* Forget the cached index value. */
804 vq->avail_idx = vq->last_avail_idx;
805 break;
806 case VHOST_GET_VRING_BASE:
807 s.index = idx;
808 s.num = vq->last_avail_idx;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900809 if (copy_to_user(argp, &s, sizeof s))
810 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000811 break;
812 case VHOST_SET_VRING_ADDR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900813 if (copy_from_user(&a, argp, sizeof a)) {
814 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000815 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900816 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000817 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
818 r = -EOPNOTSUPP;
819 break;
820 }
821 /* For 32bit, verify that the top 32bits of the user
822 data are set to zero. */
823 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
824 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
825 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
826 r = -EFAULT;
827 break;
828 }
Michael S. Tsirkin5d9a07b2014-12-21 01:00:23 +0200829
830 /* Make sure it's safe to cast pointers to vring types. */
831 BUILD_BUG_ON(__alignof__ *vq->avail > VRING_AVAIL_ALIGN_SIZE);
832 BUILD_BUG_ON(__alignof__ *vq->used > VRING_USED_ALIGN_SIZE);
833 if ((a.avail_user_addr & (VRING_AVAIL_ALIGN_SIZE - 1)) ||
834 (a.used_user_addr & (VRING_USED_ALIGN_SIZE - 1)) ||
Michael S. Tsirkind5424832015-11-16 16:57:08 +0200835 (a.log_guest_addr & (VRING_USED_ALIGN_SIZE - 1))) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000836 r = -EINVAL;
837 break;
838 }
839
840 /* We only verify access here if backend is configured.
841 * If it is not, we don't as size might not have been setup.
842 * We will verify when backend is configured. */
843 if (vq->private_data) {
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300844 if (!vq_access_ok(vq, vq->num,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000845 (void __user *)(unsigned long)a.desc_user_addr,
846 (void __user *)(unsigned long)a.avail_user_addr,
847 (void __user *)(unsigned long)a.used_user_addr)) {
848 r = -EINVAL;
849 break;
850 }
851
852 /* Also validate log access for used ring if enabled. */
853 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
854 !log_access_ok(vq->log_base, a.log_guest_addr,
855 sizeof *vq->used +
856 vq->num * sizeof *vq->used->ring)) {
857 r = -EINVAL;
858 break;
859 }
860 }
861
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000862 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
863 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
864 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
865 vq->log_addr = a.log_guest_addr;
866 vq->used = (void __user *)(unsigned long)a.used_user_addr;
867 break;
868 case VHOST_SET_VRING_KICK:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900869 if (copy_from_user(&f, argp, sizeof f)) {
870 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000871 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900872 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000873 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200874 if (IS_ERR(eventfp)) {
875 r = PTR_ERR(eventfp);
876 break;
877 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000878 if (eventfp != vq->kick) {
Al Virocecb46f2012-08-27 14:21:39 -0400879 pollstop = (filep = vq->kick) != NULL;
880 pollstart = (vq->kick = eventfp) != NULL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000881 } else
882 filep = eventfp;
883 break;
884 case VHOST_SET_VRING_CALL:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900885 if (copy_from_user(&f, argp, sizeof f)) {
886 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000887 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900888 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000889 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200890 if (IS_ERR(eventfp)) {
891 r = PTR_ERR(eventfp);
892 break;
893 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000894 if (eventfp != vq->call) {
895 filep = vq->call;
896 ctx = vq->call_ctx;
897 vq->call = eventfp;
898 vq->call_ctx = eventfp ?
899 eventfd_ctx_fileget(eventfp) : NULL;
900 } else
901 filep = eventfp;
902 break;
903 case VHOST_SET_VRING_ERR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900904 if (copy_from_user(&f, argp, sizeof f)) {
905 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000906 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900907 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000908 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200909 if (IS_ERR(eventfp)) {
910 r = PTR_ERR(eventfp);
911 break;
912 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000913 if (eventfp != vq->error) {
914 filep = vq->error;
915 vq->error = eventfp;
916 ctx = vq->error_ctx;
917 vq->error_ctx = eventfp ?
918 eventfd_ctx_fileget(eventfp) : NULL;
919 } else
920 filep = eventfp;
921 break;
Greg Kurz2751c982015-04-24 14:27:24 +0200922 case VHOST_SET_VRING_ENDIAN:
923 r = vhost_set_vring_endian(vq, argp);
924 break;
925 case VHOST_GET_VRING_ENDIAN:
926 r = vhost_get_vring_endian(vq, idx, argp);
927 break;
Jason Wang03088132016-03-04 06:24:53 -0500928 case VHOST_SET_VRING_BUSYLOOP_TIMEOUT:
929 if (copy_from_user(&s, argp, sizeof(s))) {
930 r = -EFAULT;
931 break;
932 }
933 vq->busyloop_timeout = s.num;
934 break;
935 case VHOST_GET_VRING_BUSYLOOP_TIMEOUT:
936 s.index = idx;
937 s.num = vq->busyloop_timeout;
938 if (copy_to_user(argp, &s, sizeof(s)))
939 r = -EFAULT;
940 break;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000941 default:
942 r = -ENOIOCTLCMD;
943 }
944
945 if (pollstop && vq->handle_kick)
946 vhost_poll_stop(&vq->poll);
947
948 if (ctx)
949 eventfd_ctx_put(ctx);
950 if (filep)
951 fput(filep);
952
953 if (pollstart && vq->handle_kick)
Jason Wang2b8b3282013-01-28 01:05:18 +0000954 r = vhost_poll_start(&vq->poll, vq->kick);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000955
956 mutex_unlock(&vq->mutex);
957
958 if (pollstop && vq->handle_kick)
959 vhost_poll_flush(&vq->poll);
960 return r;
961}
Asias He6ac1afb2013-05-06 16:38:21 +0800962EXPORT_SYMBOL_GPL(vhost_vring_ioctl);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000963
964/* Caller must have device mutex */
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200965long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000966{
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000967 struct file *eventfp, *filep = NULL;
968 struct eventfd_ctx *ctx = NULL;
969 u64 p;
970 long r;
971 int i, fd;
972
973 /* If you are not the owner, you can become one */
974 if (ioctl == VHOST_SET_OWNER) {
975 r = vhost_dev_set_owner(d);
976 goto done;
977 }
978
979 /* You must be the owner to do anything else */
980 r = vhost_dev_check_owner(d);
981 if (r)
982 goto done;
983
984 switch (ioctl) {
985 case VHOST_SET_MEM_TABLE:
986 r = vhost_set_memory(d, argp);
987 break;
988 case VHOST_SET_LOG_BASE:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900989 if (copy_from_user(&p, argp, sizeof p)) {
990 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000991 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900992 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000993 if ((u64)(unsigned long)p != p) {
994 r = -EFAULT;
995 break;
996 }
997 for (i = 0; i < d->nvqs; ++i) {
998 struct vhost_virtqueue *vq;
999 void __user *base = (void __user *)(unsigned long)p;
Asias He3ab2e422013-04-27 11:16:48 +08001000 vq = d->vqs[i];
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001001 mutex_lock(&vq->mutex);
1002 /* If ring is inactive, will check when it's enabled. */
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001003 if (vq->private_data && !vq_log_access_ok(vq, base))
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001004 r = -EFAULT;
1005 else
1006 vq->log_base = base;
1007 mutex_unlock(&vq->mutex);
1008 }
1009 break;
1010 case VHOST_SET_LOG_FD:
1011 r = get_user(fd, (int __user *)argp);
1012 if (r < 0)
1013 break;
1014 eventfp = fd == -1 ? NULL : eventfd_fget(fd);
1015 if (IS_ERR(eventfp)) {
1016 r = PTR_ERR(eventfp);
1017 break;
1018 }
1019 if (eventfp != d->log_file) {
1020 filep = d->log_file;
Marc-André Lureau7932c0b2015-07-17 15:32:03 +02001021 d->log_file = eventfp;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001022 ctx = d->log_ctx;
1023 d->log_ctx = eventfp ?
1024 eventfd_ctx_fileget(eventfp) : NULL;
1025 } else
1026 filep = eventfp;
1027 for (i = 0; i < d->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +08001028 mutex_lock(&d->vqs[i]->mutex);
1029 d->vqs[i]->log_ctx = d->log_ctx;
1030 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001031 }
1032 if (ctx)
1033 eventfd_ctx_put(ctx);
1034 if (filep)
1035 fput(filep);
1036 break;
1037 default:
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001038 r = -ENOIOCTLCMD;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001039 break;
1040 }
1041done:
1042 return r;
1043}
Asias He6ac1afb2013-05-06 16:38:21 +08001044EXPORT_SYMBOL_GPL(vhost_dev_ioctl);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001045
1046static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
1047 __u64 addr, __u32 len)
1048{
Igor Mammedovbcfeaca2015-06-16 18:33:35 +02001049 const struct vhost_memory_region *reg;
1050 int start = 0, end = mem->nregions;
Krishna Kumard47effe2011-03-01 17:06:37 +05301051
Igor Mammedovbcfeaca2015-06-16 18:33:35 +02001052 while (start < end) {
1053 int slot = start + (end - start) / 2;
1054 reg = mem->regions + slot;
1055 if (addr >= reg->guest_phys_addr)
1056 end = slot;
1057 else
1058 start = slot + 1;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001059 }
Igor Mammedovbcfeaca2015-06-16 18:33:35 +02001060
1061 reg = mem->regions + start;
1062 if (addr >= reg->guest_phys_addr &&
1063 reg->guest_phys_addr + reg->memory_size > addr)
1064 return reg;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001065 return NULL;
1066}
1067
1068/* TODO: This is really inefficient. We need something like get_user()
1069 * (instruction directly accesses the data, with an exception table entry
1070 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
1071 */
1072static int set_bit_to_user(int nr, void __user *addr)
1073{
1074 unsigned long log = (unsigned long)addr;
1075 struct page *page;
1076 void *base;
1077 int bit = nr + (log % PAGE_SIZE) * 8;
1078 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301079
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001080 r = get_user_pages_fast(log, 1, 1, &page);
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +02001081 if (r < 0)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001082 return r;
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +02001083 BUG_ON(r != 1);
Cong Wangc6daa7f2011-11-25 23:14:26 +08001084 base = kmap_atomic(page);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001085 set_bit(bit, base);
Cong Wangc6daa7f2011-11-25 23:14:26 +08001086 kunmap_atomic(base);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001087 set_page_dirty_lock(page);
1088 put_page(page);
1089 return 0;
1090}
1091
1092static int log_write(void __user *log_base,
1093 u64 write_address, u64 write_length)
1094{
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001095 u64 write_page = write_address / VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001096 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301097
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001098 if (!write_length)
1099 return 0;
Michael S. Tsirkin3bf9be42010-11-29 10:19:07 +02001100 write_length += write_address % VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001101 for (;;) {
1102 u64 base = (u64)(unsigned long)log_base;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001103 u64 log = base + write_page / 8;
1104 int bit = write_page % 8;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001105 if ((u64)(unsigned long)log != log)
1106 return -EFAULT;
1107 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
1108 if (r < 0)
1109 return r;
1110 if (write_length <= VHOST_PAGE_SIZE)
1111 break;
1112 write_length -= VHOST_PAGE_SIZE;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001113 write_page += 1;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001114 }
1115 return r;
1116}
1117
1118int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
1119 unsigned int log_num, u64 len)
1120{
1121 int i, r;
1122
1123 /* Make sure data written is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001124 smp_wmb();
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001125 for (i = 0; i < log_num; ++i) {
1126 u64 l = min(log[i].len, len);
1127 r = log_write(vq->log_base, log[i].addr, l);
1128 if (r < 0)
1129 return r;
1130 len -= l;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +02001131 if (!len) {
1132 if (vq->log_ctx)
1133 eventfd_signal(vq->log_ctx, 1);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001134 return 0;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +02001135 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001136 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001137 /* Length written exceeds what we have stored. This is a bug. */
1138 BUG();
1139 return 0;
1140}
Asias He6ac1afb2013-05-06 16:38:21 +08001141EXPORT_SYMBOL_GPL(vhost_log_write);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001142
Jason Wang2723fea2011-06-21 18:04:38 +08001143static int vhost_update_used_flags(struct vhost_virtqueue *vq)
1144{
1145 void __user *used;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001146 if (__put_user(cpu_to_vhost16(vq, vq->used_flags), &vq->used->flags) < 0)
Jason Wang2723fea2011-06-21 18:04:38 +08001147 return -EFAULT;
1148 if (unlikely(vq->log_used)) {
1149 /* Make sure the flag is seen before log. */
1150 smp_wmb();
1151 /* Log used flag write. */
1152 used = &vq->used->flags;
1153 log_write(vq->log_base, vq->log_addr +
1154 (used - (void __user *)vq->used),
1155 sizeof vq->used->flags);
1156 if (vq->log_ctx)
1157 eventfd_signal(vq->log_ctx, 1);
1158 }
1159 return 0;
1160}
1161
1162static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
1163{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001164 if (__put_user(cpu_to_vhost16(vq, vq->avail_idx), vhost_avail_event(vq)))
Jason Wang2723fea2011-06-21 18:04:38 +08001165 return -EFAULT;
1166 if (unlikely(vq->log_used)) {
1167 void __user *used;
1168 /* Make sure the event is seen before log. */
1169 smp_wmb();
1170 /* Log avail event write */
1171 used = vhost_avail_event(vq);
1172 log_write(vq->log_base, vq->log_addr +
1173 (used - (void __user *)vq->used),
1174 sizeof *vhost_avail_event(vq));
1175 if (vq->log_ctx)
1176 eventfd_signal(vq->log_ctx, 1);
1177 }
1178 return 0;
1179}
1180
Greg Kurz80f7d032016-02-16 15:59:44 +01001181int vhost_vq_init_access(struct vhost_virtqueue *vq)
Jason Wang2723fea2011-06-21 18:04:38 +08001182{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001183 __virtio16 last_used_idx;
Jason Wang2723fea2011-06-21 18:04:38 +08001184 int r;
Greg Kurze1f33be2016-02-16 15:54:28 +01001185 bool is_le = vq->is_le;
1186
Greg Kurz2751c982015-04-24 14:27:24 +02001187 if (!vq->private_data) {
Greg Kurzc5072032016-02-16 15:59:34 +01001188 vhost_reset_is_le(vq);
Jason Wang2723fea2011-06-21 18:04:38 +08001189 return 0;
Greg Kurz2751c982015-04-24 14:27:24 +02001190 }
1191
1192 vhost_init_is_le(vq);
Jason Wang2723fea2011-06-21 18:04:38 +08001193
1194 r = vhost_update_used_flags(vq);
1195 if (r)
Greg Kurze1f33be2016-02-16 15:54:28 +01001196 goto err;
Jason Wang2723fea2011-06-21 18:04:38 +08001197 vq->signalled_used_valid = false;
Greg Kurze1f33be2016-02-16 15:54:28 +01001198 if (!access_ok(VERIFY_READ, &vq->used->idx, sizeof vq->used->idx)) {
1199 r = -EFAULT;
1200 goto err;
1201 }
Michael S. Tsirkin64f7f052014-12-01 17:39:39 +02001202 r = __get_user(last_used_idx, &vq->used->idx);
1203 if (r)
Greg Kurze1f33be2016-02-16 15:54:28 +01001204 goto err;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001205 vq->last_used_idx = vhost16_to_cpu(vq, last_used_idx);
Michael S. Tsirkin64f7f052014-12-01 17:39:39 +02001206 return 0;
Greg Kurze1f33be2016-02-16 15:54:28 +01001207err:
1208 vq->is_le = is_le;
1209 return r;
Jason Wang2723fea2011-06-21 18:04:38 +08001210}
Greg Kurz80f7d032016-02-16 15:59:44 +01001211EXPORT_SYMBOL_GPL(vhost_vq_init_access);
Jason Wang2723fea2011-06-21 18:04:38 +08001212
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001213static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001214 struct iovec iov[], int iov_size)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001215{
1216 const struct vhost_memory_region *reg;
1217 struct vhost_memory *mem;
1218 struct iovec *_iov;
1219 u64 s = 0;
1220 int ret = 0;
1221
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001222 mem = vq->memory;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001223 while ((u64)len > s) {
1224 u64 size;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001225 if (unlikely(ret >= iov_size)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001226 ret = -ENOBUFS;
1227 break;
1228 }
1229 reg = find_region(mem, addr, len);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001230 if (unlikely(!reg)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001231 ret = -EFAULT;
1232 break;
1233 }
1234 _iov = iov + ret;
1235 size = reg->memory_size - addr + reg->guest_phys_addr;
Michael S. Tsirkinbd971202012-11-26 05:57:27 +00001236 _iov->iov_len = min((u64)len - s, size);
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001237 _iov->iov_base = (void __user *)(unsigned long)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001238 (reg->userspace_addr + addr - reg->guest_phys_addr);
1239 s += size;
1240 addr += size;
1241 ++ret;
1242 }
1243
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001244 return ret;
1245}
1246
1247/* Each buffer in the virtqueues is actually a chain of descriptors. This
1248 * function returns the next descriptor in the chain,
1249 * or -1U if we're at the end. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001250static unsigned next_desc(struct vhost_virtqueue *vq, struct vring_desc *desc)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001251{
1252 unsigned int next;
1253
1254 /* If this descriptor says it doesn't chain, we're done. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001255 if (!(desc->flags & cpu_to_vhost16(vq, VRING_DESC_F_NEXT)))
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001256 return -1U;
1257
1258 /* Check they're not leading us off end of descriptors. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001259 next = vhost16_to_cpu(vq, desc->next);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001260 /* Make sure compiler knows to grab that: we don't want it changing! */
1261 /* We will use the result as an index in an array, so most
1262 * architectures only need a compiler barrier here. */
1263 read_barrier_depends();
1264
1265 return next;
1266}
1267
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001268static int get_indirect(struct vhost_virtqueue *vq,
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001269 struct iovec iov[], unsigned int iov_size,
1270 unsigned int *out_num, unsigned int *in_num,
1271 struct vhost_log *log, unsigned int *log_num,
1272 struct vring_desc *indirect)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001273{
1274 struct vring_desc desc;
1275 unsigned int i = 0, count, found = 0;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001276 u32 len = vhost32_to_cpu(vq, indirect->len);
Al Viroaad9a1c2014-12-10 14:49:01 -05001277 struct iov_iter from;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001278 int ret;
1279
1280 /* Sanity check */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001281 if (unlikely(len % sizeof desc)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001282 vq_err(vq, "Invalid length in indirect descriptor: "
1283 "len 0x%llx not multiple of 0x%zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001284 (unsigned long long)len,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001285 sizeof desc);
1286 return -EINVAL;
1287 }
1288
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001289 ret = translate_desc(vq, vhost64_to_cpu(vq, indirect->addr), len, vq->indirect,
Jason Wange0e9b402010-09-14 23:53:05 +08001290 UIO_MAXIOV);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001291 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001292 vq_err(vq, "Translation failure %d in indirect.\n", ret);
1293 return ret;
1294 }
Al Viroaad9a1c2014-12-10 14:49:01 -05001295 iov_iter_init(&from, READ, vq->indirect, ret, len);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001296
1297 /* We will use the result as an address to read from, so most
1298 * architectures only need a compiler barrier here. */
1299 read_barrier_depends();
1300
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001301 count = len / sizeof desc;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001302 /* Buffers are chained via a 16 bit next field, so
1303 * we can have at most 2^16 of these. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001304 if (unlikely(count > USHRT_MAX + 1)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001305 vq_err(vq, "Indirect buffer length too big: %d\n",
1306 indirect->len);
1307 return -E2BIG;
1308 }
1309
1310 do {
1311 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001312 if (unlikely(++found > count)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001313 vq_err(vq, "Loop detected: last one at %u "
1314 "indirect size %u\n",
1315 i, count);
1316 return -EINVAL;
1317 }
Al Viroaad9a1c2014-12-10 14:49:01 -05001318 if (unlikely(copy_from_iter(&desc, sizeof(desc), &from) !=
1319 sizeof(desc))) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001320 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001321 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001322 return -EINVAL;
1323 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001324 if (unlikely(desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT))) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001325 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001326 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001327 return -EINVAL;
1328 }
1329
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001330 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
1331 vhost32_to_cpu(vq, desc.len), iov + iov_count,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001332 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001333 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001334 vq_err(vq, "Translation failure %d indirect idx %d\n",
1335 ret, i);
1336 return ret;
1337 }
1338 /* If this is an input descriptor, increment that count. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001339 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001340 *in_num += ret;
1341 if (unlikely(log)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001342 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
1343 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001344 ++*log_num;
1345 }
1346 } else {
1347 /* If it's an output descriptor, they're all supposed
1348 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001349 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001350 vq_err(vq, "Indirect descriptor "
1351 "has out after in: idx %d\n", i);
1352 return -EINVAL;
1353 }
1354 *out_num += ret;
1355 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001356 } while ((i = next_desc(vq, &desc)) != -1);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001357 return 0;
1358}
1359
1360/* This looks in the virtqueue and for the first available buffer, and converts
1361 * it to an iovec for convenient access. Since descriptors consist of some
1362 * number of output then some number of input descriptors, it's actually two
1363 * iovecs, but we pack them into one and note how many of each there were.
1364 *
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001365 * This function returns the descriptor number found, or vq->num (which is
1366 * never a valid descriptor number) if none was found. A negative code is
1367 * returned on error. */
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001368int vhost_get_vq_desc(struct vhost_virtqueue *vq,
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001369 struct iovec iov[], unsigned int iov_size,
1370 unsigned int *out_num, unsigned int *in_num,
1371 struct vhost_log *log, unsigned int *log_num)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001372{
1373 struct vring_desc desc;
1374 unsigned int i, head, found = 0;
1375 u16 last_avail_idx;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001376 __virtio16 avail_idx;
1377 __virtio16 ring_head;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001378 int ret;
1379
1380 /* Check it isn't doing very strange things with descriptor numbers. */
1381 last_avail_idx = vq->last_avail_idx;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001382 if (unlikely(__get_user(avail_idx, &vq->avail->idx))) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001383 vq_err(vq, "Failed to access avail idx at %p\n",
1384 &vq->avail->idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001385 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001386 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001387 vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001388
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001389 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001390 vq_err(vq, "Guest moved used index from %u to %u",
1391 last_avail_idx, vq->avail_idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001392 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001393 }
1394
1395 /* If there's nothing new since last we looked, return invalid. */
1396 if (vq->avail_idx == last_avail_idx)
1397 return vq->num;
1398
1399 /* Only get avail ring entries after they have been exposed by guest. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001400 smp_rmb();
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001401
1402 /* Grab the next descriptor number they're advertising, and increment
1403 * the index we've seen. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001404 if (unlikely(__get_user(ring_head,
Michael S. Tsirkin5fba13b2015-11-29 13:34:44 +02001405 &vq->avail->ring[last_avail_idx & (vq->num - 1)]))) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001406 vq_err(vq, "Failed to read head: idx %d address %p\n",
1407 last_avail_idx,
1408 &vq->avail->ring[last_avail_idx % vq->num]);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001409 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001410 }
1411
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001412 head = vhost16_to_cpu(vq, ring_head);
1413
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001414 /* If their number is silly, that's an error. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001415 if (unlikely(head >= vq->num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001416 vq_err(vq, "Guest says index %u > %u is available",
1417 head, vq->num);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001418 return -EINVAL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001419 }
1420
1421 /* When we start there are none of either input nor output. */
1422 *out_num = *in_num = 0;
1423 if (unlikely(log))
1424 *log_num = 0;
1425
1426 i = head;
1427 do {
1428 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001429 if (unlikely(i >= vq->num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001430 vq_err(vq, "Desc index is %u > %u, head = %u",
1431 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001432 return -EINVAL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001433 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001434 if (unlikely(++found > vq->num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001435 vq_err(vq, "Loop detected: last one at %u "
1436 "vq size %u head %u\n",
1437 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001438 return -EINVAL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001439 }
Michael S. Tsirkinfcc042a2011-03-06 13:33:49 +02001440 ret = __copy_from_user(&desc, vq->desc + i, sizeof desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001441 if (unlikely(ret)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001442 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
1443 i, vq->desc + i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001444 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001445 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001446 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT)) {
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001447 ret = get_indirect(vq, iov, iov_size,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001448 out_num, in_num,
1449 log, log_num, &desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001450 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001451 vq_err(vq, "Failure detected "
1452 "in indirect descriptor at idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001453 return ret;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001454 }
1455 continue;
1456 }
1457
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001458 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
1459 vhost32_to_cpu(vq, desc.len), iov + iov_count,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001460 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001461 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001462 vq_err(vq, "Translation failure %d descriptor idx %d\n",
1463 ret, i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001464 return ret;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001465 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001466 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001467 /* If this is an input descriptor,
1468 * increment that count. */
1469 *in_num += ret;
1470 if (unlikely(log)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001471 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
1472 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001473 ++*log_num;
1474 }
1475 } else {
1476 /* If it's an output descriptor, they're all supposed
1477 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001478 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001479 vq_err(vq, "Descriptor has out after in: "
1480 "idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001481 return -EINVAL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001482 }
1483 *out_num += ret;
1484 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001485 } while ((i = next_desc(vq, &desc)) != -1);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001486
1487 /* On success, increment avail index. */
1488 vq->last_avail_idx++;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001489
1490 /* Assume notifications from guest are disabled at this point,
1491 * if they aren't we would need to update avail_event index. */
1492 BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY));
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001493 return head;
1494}
Asias He6ac1afb2013-05-06 16:38:21 +08001495EXPORT_SYMBOL_GPL(vhost_get_vq_desc);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001496
1497/* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
David Stevens8dd014a2010-07-27 18:52:21 +03001498void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001499{
David Stevens8dd014a2010-07-27 18:52:21 +03001500 vq->last_avail_idx -= n;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001501}
Asias He6ac1afb2013-05-06 16:38:21 +08001502EXPORT_SYMBOL_GPL(vhost_discard_vq_desc);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001503
1504/* After we've used one of their buffers, we tell them about it. We'll then
1505 * want to notify the guest, using eventfd. */
1506int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
1507{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001508 struct vring_used_elem heads = {
1509 cpu_to_vhost32(vq, head),
1510 cpu_to_vhost32(vq, len)
1511 };
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001512
Jason Wangc49e4e52013-09-02 16:40:58 +08001513 return vhost_add_used_n(vq, &heads, 1);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001514}
Asias He6ac1afb2013-05-06 16:38:21 +08001515EXPORT_SYMBOL_GPL(vhost_add_used);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001516
David Stevens8dd014a2010-07-27 18:52:21 +03001517static int __vhost_add_used_n(struct vhost_virtqueue *vq,
1518 struct vring_used_elem *heads,
1519 unsigned count)
1520{
1521 struct vring_used_elem __user *used;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001522 u16 old, new;
David Stevens8dd014a2010-07-27 18:52:21 +03001523 int start;
1524
Michael S. Tsirkin5fba13b2015-11-29 13:34:44 +02001525 start = vq->last_used_idx & (vq->num - 1);
David Stevens8dd014a2010-07-27 18:52:21 +03001526 used = vq->used->ring + start;
Jason Wangc49e4e52013-09-02 16:40:58 +08001527 if (count == 1) {
1528 if (__put_user(heads[0].id, &used->id)) {
1529 vq_err(vq, "Failed to write used id");
1530 return -EFAULT;
1531 }
1532 if (__put_user(heads[0].len, &used->len)) {
1533 vq_err(vq, "Failed to write used len");
1534 return -EFAULT;
1535 }
1536 } else if (__copy_to_user(used, heads, count * sizeof *used)) {
David Stevens8dd014a2010-07-27 18:52:21 +03001537 vq_err(vq, "Failed to write used");
1538 return -EFAULT;
1539 }
1540 if (unlikely(vq->log_used)) {
1541 /* Make sure data is seen before log. */
1542 smp_wmb();
1543 /* Log used ring entry write. */
1544 log_write(vq->log_base,
1545 vq->log_addr +
1546 ((void __user *)used - (void __user *)vq->used),
1547 count * sizeof *used);
1548 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001549 old = vq->last_used_idx;
1550 new = (vq->last_used_idx += count);
1551 /* If the driver never bothers to signal in a very long while,
1552 * used index might wrap around. If that happens, invalidate
1553 * signalled_used index we stored. TODO: make sure driver
1554 * signals at least once in 2^16 and remove this. */
1555 if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old)))
1556 vq->signalled_used_valid = false;
David Stevens8dd014a2010-07-27 18:52:21 +03001557 return 0;
1558}
1559
1560/* After we've used one of their buffers, we tell them about it. We'll then
1561 * want to notify the guest, using eventfd. */
1562int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
1563 unsigned count)
1564{
1565 int start, n, r;
1566
Michael S. Tsirkin5fba13b2015-11-29 13:34:44 +02001567 start = vq->last_used_idx & (vq->num - 1);
David Stevens8dd014a2010-07-27 18:52:21 +03001568 n = vq->num - start;
1569 if (n < count) {
1570 r = __vhost_add_used_n(vq, heads, n);
1571 if (r < 0)
1572 return r;
1573 heads += n;
1574 count -= n;
1575 }
1576 r = __vhost_add_used_n(vq, heads, count);
1577
1578 /* Make sure buffer is written before we update index. */
1579 smp_wmb();
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001580 if (__put_user(cpu_to_vhost16(vq, vq->last_used_idx), &vq->used->idx)) {
David Stevens8dd014a2010-07-27 18:52:21 +03001581 vq_err(vq, "Failed to increment used idx");
1582 return -EFAULT;
1583 }
1584 if (unlikely(vq->log_used)) {
1585 /* Log used index update. */
1586 log_write(vq->log_base,
1587 vq->log_addr + offsetof(struct vring_used, idx),
1588 sizeof vq->used->idx);
1589 if (vq->log_ctx)
1590 eventfd_signal(vq->log_ctx, 1);
1591 }
1592 return r;
1593}
Asias He6ac1afb2013-05-06 16:38:21 +08001594EXPORT_SYMBOL_GPL(vhost_add_used_n);
David Stevens8dd014a2010-07-27 18:52:21 +03001595
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001596static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001597{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001598 __u16 old, new;
1599 __virtio16 event;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001600 bool v;
Michael S. Tsirkin0d499352010-05-11 19:44:17 +03001601 /* Flush out used index updates. This is paired
1602 * with the barrier that the Guest executes when enabling
1603 * interrupts. */
1604 smp_mb();
1605
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001606 if (vhost_has_feature(vq, VIRTIO_F_NOTIFY_ON_EMPTY) &&
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001607 unlikely(vq->avail_idx == vq->last_avail_idx))
1608 return true;
1609
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001610 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001611 __virtio16 flags;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001612 if (__get_user(flags, &vq->avail->flags)) {
1613 vq_err(vq, "Failed to get flags");
1614 return true;
1615 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001616 return !(flags & cpu_to_vhost16(vq, VRING_AVAIL_F_NO_INTERRUPT));
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001617 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001618 old = vq->signalled_used;
1619 v = vq->signalled_used_valid;
1620 new = vq->signalled_used = vq->last_used_idx;
1621 vq->signalled_used_valid = true;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001622
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001623 if (unlikely(!v))
1624 return true;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001625
Michael S. Tsirkin64f7f052014-12-01 17:39:39 +02001626 if (__get_user(event, vhost_used_event(vq))) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001627 vq_err(vq, "Failed to get used event idx");
1628 return true;
1629 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001630 return vring_need_event(vhost16_to_cpu(vq, event), new, old);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001631}
1632
1633/* This actually signals the guest, using eventfd. */
1634void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
1635{
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001636 /* Signal the Guest tell them we used something up. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001637 if (vq->call_ctx && vhost_notify(dev, vq))
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001638 eventfd_signal(vq->call_ctx, 1);
1639}
Asias He6ac1afb2013-05-06 16:38:21 +08001640EXPORT_SYMBOL_GPL(vhost_signal);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001641
1642/* And here's the combo meal deal. Supersize me! */
1643void vhost_add_used_and_signal(struct vhost_dev *dev,
1644 struct vhost_virtqueue *vq,
1645 unsigned int head, int len)
1646{
1647 vhost_add_used(vq, head, len);
1648 vhost_signal(dev, vq);
1649}
Asias He6ac1afb2013-05-06 16:38:21 +08001650EXPORT_SYMBOL_GPL(vhost_add_used_and_signal);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001651
David Stevens8dd014a2010-07-27 18:52:21 +03001652/* multi-buffer version of vhost_add_used_and_signal */
1653void vhost_add_used_and_signal_n(struct vhost_dev *dev,
1654 struct vhost_virtqueue *vq,
1655 struct vring_used_elem *heads, unsigned count)
1656{
1657 vhost_add_used_n(vq, heads, count);
1658 vhost_signal(dev, vq);
1659}
Asias He6ac1afb2013-05-06 16:38:21 +08001660EXPORT_SYMBOL_GPL(vhost_add_used_and_signal_n);
David Stevens8dd014a2010-07-27 18:52:21 +03001661
Jason Wangd4a60602016-03-04 06:24:52 -05001662/* return true if we're sure that avaiable ring is empty */
1663bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq)
1664{
1665 __virtio16 avail_idx;
1666 int r;
1667
1668 r = __get_user(avail_idx, &vq->avail->idx);
1669 if (r)
1670 return false;
1671
1672 return vhost16_to_cpu(vq, avail_idx) == vq->avail_idx;
1673}
1674EXPORT_SYMBOL_GPL(vhost_vq_avail_empty);
1675
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001676/* OK, now we need to know about added descriptors. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001677bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001678{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001679 __virtio16 avail_idx;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001680 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301681
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001682 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
1683 return false;
1684 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001685 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08001686 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001687 if (r) {
1688 vq_err(vq, "Failed to enable notification at %p: %d\n",
1689 &vq->used->flags, r);
1690 return false;
1691 }
1692 } else {
Jason Wang2723fea2011-06-21 18:04:38 +08001693 r = vhost_update_avail_event(vq, vq->avail_idx);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001694 if (r) {
1695 vq_err(vq, "Failed to update avail event index at %p: %d\n",
1696 vhost_avail_event(vq), r);
1697 return false;
1698 }
1699 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001700 /* They could have slipped one in as we were doing that: make
1701 * sure it's written, then check again. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001702 smp_mb();
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001703 r = __get_user(avail_idx, &vq->avail->idx);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001704 if (r) {
1705 vq_err(vq, "Failed to check avail idx at %p: %d\n",
1706 &vq->avail->idx, r);
1707 return false;
1708 }
1709
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001710 return vhost16_to_cpu(vq, avail_idx) != vq->avail_idx;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001711}
Asias He6ac1afb2013-05-06 16:38:21 +08001712EXPORT_SYMBOL_GPL(vhost_enable_notify);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001713
1714/* We don't need to be notified again. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001715void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001716{
1717 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301718
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001719 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
1720 return;
1721 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001722 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08001723 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001724 if (r)
1725 vq_err(vq, "Failed to enable notification at %p: %d\n",
1726 &vq->used->flags, r);
1727 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001728}
Asias He6ac1afb2013-05-06 16:38:21 +08001729EXPORT_SYMBOL_GPL(vhost_disable_notify);
1730
1731static int __init vhost_init(void)
1732{
1733 return 0;
1734}
1735
1736static void __exit vhost_exit(void)
1737{
1738}
1739
1740module_init(vhost_init);
1741module_exit(vhost_exit);
1742
1743MODULE_VERSION("0.0.1");
1744MODULE_LICENSE("GPL v2");
1745MODULE_AUTHOR("Michael S. Tsirkin");
1746MODULE_DESCRIPTION("Host kernel accelerator for virtio");