blob: f44340b41494e5a20ed4a7948d9c5e17addcce11 [file] [log] [blame]
Thomas Gleixner7a338472019-06-04 10:11:15 +02001// SPDX-License-Identifier: GPL-2.0-only
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002/* Copyright (C) 2009 Red Hat, Inc.
3 * Copyright (C) 2006 Rusty Russell IBM Corporation
4 *
5 * Author: Michael S. Tsirkin <mst@redhat.com>
6 *
7 * Inspiration, some code, and most witty comments come from
Rob Landley61516582011-05-06 09:27:36 -07008 * Documentation/virtual/lguest/lguest.c, by Rusty Russell
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00009 *
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000010 * Generic code for virtio server in host kernel.
11 */
12
13#include <linux/eventfd.h>
14#include <linux/vhost.h>
Asias He35596b22013-08-19 09:23:19 +080015#include <linux/uio.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000016#include <linux/mm.h>
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +020017#include <linux/mmu_context.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000018#include <linux/miscdevice.h>
19#include <linux/mutex.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000020#include <linux/poll.h>
21#include <linux/file.h>
22#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Igor Mammedov4de72552015-07-01 11:07:09 +020024#include <linux/vmalloc.h>
Tejun Heoc23f34452010-06-02 20:40:00 +020025#include <linux/kthread.h>
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +030026#include <linux/cgroup.h>
Asias He6ac1afb2013-05-06 16:38:21 +080027#include <linux/module.h>
Igor Mammedovbcfeaca2015-06-16 18:33:35 +020028#include <linux/sort.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010029#include <linux/sched/mm.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010030#include <linux/sched/signal.h>
Jason Wanga9709d62016-06-23 02:04:31 -040031#include <linux/interval_tree_generic.h>
Jason Wangff002262018-10-30 14:10:49 +080032#include <linux/nospec.h>
Andrey Konovalov8f6a7f92019-12-04 16:52:50 -080033#include <linux/kcov.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000034
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000035#include "vhost.h"
36
Igor Mammedovc9ce42f2015-07-02 15:08:11 +020037static ushort max_mem_regions = 64;
38module_param(max_mem_regions, ushort, 0444);
39MODULE_PARM_DESC(max_mem_regions,
40 "Maximum number of memory regions in memory map. (default: 64)");
Jason Wang6b1e6cc2016-06-23 02:04:32 -040041static int max_iotlb_entries = 2048;
42module_param(max_iotlb_entries, int, 0444);
43MODULE_PARM_DESC(max_iotlb_entries,
44 "Maximum number of iotlb entries. (default: 2048)");
Igor Mammedovc9ce42f2015-07-02 15:08:11 +020045
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000046enum {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000047 VHOST_MEMORY_F_LOG = 0x1,
48};
49
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +030050#define vhost_used_event(vq) ((__virtio16 __user *)&vq->avail->ring[vq->num])
51#define vhost_avail_event(vq) ((__virtio16 __user *)&vq->used->ring[vq->num])
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +030052
Jason Wanga9709d62016-06-23 02:04:31 -040053INTERVAL_TREE_DEFINE(struct vhost_umem_node,
54 rb, __u64, __subtree_last,
Michael S. Tsirkin2f952c02016-12-06 05:57:54 +020055 START, LAST, static inline, vhost_umem_interval_tree);
Jason Wanga9709d62016-06-23 02:04:31 -040056
Greg Kurz2751c982015-04-24 14:27:24 +020057#ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
Greg Kurzc5072032016-02-16 15:59:34 +010058static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
Greg Kurz2751c982015-04-24 14:27:24 +020059{
60 vq->user_be = !virtio_legacy_is_little_endian();
61}
62
Greg Kurzc5072032016-02-16 15:59:34 +010063static void vhost_enable_cross_endian_big(struct vhost_virtqueue *vq)
64{
65 vq->user_be = true;
66}
67
68static void vhost_enable_cross_endian_little(struct vhost_virtqueue *vq)
69{
70 vq->user_be = false;
71}
72
Greg Kurz2751c982015-04-24 14:27:24 +020073static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
74{
75 struct vhost_vring_state s;
76
77 if (vq->private_data)
78 return -EBUSY;
79
80 if (copy_from_user(&s, argp, sizeof(s)))
81 return -EFAULT;
82
83 if (s.num != VHOST_VRING_LITTLE_ENDIAN &&
84 s.num != VHOST_VRING_BIG_ENDIAN)
85 return -EINVAL;
86
Greg Kurzc5072032016-02-16 15:59:34 +010087 if (s.num == VHOST_VRING_BIG_ENDIAN)
88 vhost_enable_cross_endian_big(vq);
89 else
90 vhost_enable_cross_endian_little(vq);
Greg Kurz2751c982015-04-24 14:27:24 +020091
92 return 0;
93}
94
95static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
96 int __user *argp)
97{
98 struct vhost_vring_state s = {
99 .index = idx,
100 .num = vq->user_be
101 };
102
103 if (copy_to_user(argp, &s, sizeof(s)))
104 return -EFAULT;
105
106 return 0;
107}
108
109static void vhost_init_is_le(struct vhost_virtqueue *vq)
110{
111 /* Note for legacy virtio: user_be is initialized at reset time
112 * according to the host endianness. If userspace does not set an
113 * explicit endianness, the default behavior is native endian, as
114 * expected by legacy virtio.
115 */
116 vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) || !vq->user_be;
117}
118#else
Greg Kurzc5072032016-02-16 15:59:34 +0100119static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
Greg Kurz2751c982015-04-24 14:27:24 +0200120{
121}
122
123static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
124{
125 return -ENOIOCTLCMD;
126}
127
128static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
129 int __user *argp)
130{
131 return -ENOIOCTLCMD;
132}
133
134static void vhost_init_is_le(struct vhost_virtqueue *vq)
135{
Halil Pasiccda8bba2017-01-30 11:09:36 +0100136 vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1)
137 || virtio_legacy_is_little_endian();
Greg Kurz2751c982015-04-24 14:27:24 +0200138}
139#endif /* CONFIG_VHOST_CROSS_ENDIAN_LEGACY */
140
Greg Kurzc5072032016-02-16 15:59:34 +0100141static void vhost_reset_is_le(struct vhost_virtqueue *vq)
142{
Halil Pasiccda8bba2017-01-30 11:09:36 +0100143 vhost_init_is_le(vq);
Greg Kurzc5072032016-02-16 15:59:34 +0100144}
145
Jason Wang7235acd2016-04-25 22:14:32 -0400146struct vhost_flush_struct {
147 struct vhost_work work;
148 struct completion wait_event;
149};
150
151static void vhost_flush_work(struct vhost_work *work)
152{
153 struct vhost_flush_struct *s;
154
155 s = container_of(work, struct vhost_flush_struct, work);
156 complete(&s->wait_event);
157}
158
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000159static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
160 poll_table *pt)
161{
162 struct vhost_poll *poll;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000163
Krishna Kumard47effe2011-03-01 17:06:37 +0530164 poll = container_of(pt, struct vhost_poll, table);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000165 poll->wqh = wqh;
166 add_wait_queue(wqh, &poll->wait);
167}
168
Ingo Molnarac6424b2017-06-20 12:06:13 +0200169static int vhost_poll_wakeup(wait_queue_entry_t *wait, unsigned mode, int sync,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000170 void *key)
171{
Tejun Heoc23f34452010-06-02 20:40:00 +0200172 struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
173
Al Viro3ad6f932017-07-03 20:14:56 -0400174 if (!(key_to_poll(key) & poll->mask))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000175 return 0;
176
Tejun Heoc23f34452010-06-02 20:40:00 +0200177 vhost_poll_queue(poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000178 return 0;
179}
180
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000181void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000182{
Jason Wang04b96e52016-04-25 22:14:33 -0400183 clear_bit(VHOST_WORK_QUEUED, &work->flags);
Tejun Heoc23f34452010-06-02 20:40:00 +0200184 work->fn = fn;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000185}
Asias He6ac1afb2013-05-06 16:38:21 +0800186EXPORT_SYMBOL_GPL(vhost_work_init);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000187
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300188/* Init poll structure */
189void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
Al Viro58e3b602017-07-03 23:50:40 -0400190 __poll_t mask, struct vhost_dev *dev)
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300191{
192 init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
193 init_poll_funcptr(&poll->table, vhost_poll_func);
194 poll->mask = mask;
195 poll->dev = dev;
Jason Wang2b8b3282013-01-28 01:05:18 +0000196 poll->wqh = NULL;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300197
198 vhost_work_init(&poll->work, fn);
199}
Asias He6ac1afb2013-05-06 16:38:21 +0800200EXPORT_SYMBOL_GPL(vhost_poll_init);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300201
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000202/* Start polling a file. We add ourselves to file's wait queue. The caller must
203 * keep a reference to a file until after vhost_poll_stop is called. */
Jason Wang2b8b3282013-01-28 01:05:18 +0000204int vhost_poll_start(struct vhost_poll *poll, struct file *file)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000205{
Al Viroe6c8adc2017-07-03 22:25:56 -0400206 __poll_t mask;
Krishna Kumard47effe2011-03-01 17:06:37 +0530207
Jason Wang70181d512013-04-10 20:50:48 +0000208 if (poll->wqh)
209 return 0;
210
Christoph Hellwig9965ed172018-03-05 07:26:05 -0800211 mask = vfs_poll(file, &poll->table);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000212 if (mask)
Al Viro3ad6f932017-07-03 20:14:56 -0400213 vhost_poll_wakeup(&poll->wait, 0, 0, poll_to_key(mask));
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800214 if (mask & EPOLLERR) {
Jason Wangdc6455a2018-03-27 20:50:52 +0800215 vhost_poll_stop(poll);
Yunsheng Lin896fc242019-08-20 20:36:32 +0800216 return -EINVAL;
Jason Wang2b8b3282013-01-28 01:05:18 +0000217 }
218
Yunsheng Lin896fc242019-08-20 20:36:32 +0800219 return 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000220}
Asias He6ac1afb2013-05-06 16:38:21 +0800221EXPORT_SYMBOL_GPL(vhost_poll_start);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000222
223/* Stop polling a file. After this function returns, it becomes safe to drop the
224 * file reference. You must also flush afterwards. */
225void vhost_poll_stop(struct vhost_poll *poll)
226{
Jason Wang2b8b3282013-01-28 01:05:18 +0000227 if (poll->wqh) {
228 remove_wait_queue(poll->wqh, &poll->wait);
229 poll->wqh = NULL;
230 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000231}
Asias He6ac1afb2013-05-06 16:38:21 +0800232EXPORT_SYMBOL_GPL(vhost_poll_stop);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000233
Asias He6ac1afb2013-05-06 16:38:21 +0800234void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000235{
Jason Wang7235acd2016-04-25 22:14:32 -0400236 struct vhost_flush_struct flush;
Tejun Heoc23f34452010-06-02 20:40:00 +0200237
Jason Wang7235acd2016-04-25 22:14:32 -0400238 if (dev->worker) {
239 init_completion(&flush.wait_event);
240 vhost_work_init(&flush.work, vhost_flush_work);
241
242 vhost_work_queue(dev, &flush.work);
243 wait_for_completion(&flush.wait_event);
244 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000245}
Asias He6ac1afb2013-05-06 16:38:21 +0800246EXPORT_SYMBOL_GPL(vhost_work_flush);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000247
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300248/* Flush any work that has been scheduled. When calling this, don't hold any
249 * locks that are also used by the callback. */
250void vhost_poll_flush(struct vhost_poll *poll)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000251{
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300252 vhost_work_flush(poll->dev, &poll->work);
253}
Asias He6ac1afb2013-05-06 16:38:21 +0800254EXPORT_SYMBOL_GPL(vhost_poll_flush);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300255
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000256void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300257{
Jason Wang04b96e52016-04-25 22:14:33 -0400258 if (!dev->worker)
259 return;
Tejun Heoc23f34452010-06-02 20:40:00 +0200260
Jason Wang04b96e52016-04-25 22:14:33 -0400261 if (!test_and_set_bit(VHOST_WORK_QUEUED, &work->flags)) {
262 /* We can only add the work to the list after we're
263 * sure it was not in the list.
Peng Tao635abf02016-12-07 17:52:19 +0800264 * test_and_set_bit() implies a memory barrier.
Jason Wang04b96e52016-04-25 22:14:33 -0400265 */
Jason Wang04b96e52016-04-25 22:14:33 -0400266 llist_add(&work->node, &dev->work_list);
Tejun Heoc23f34452010-06-02 20:40:00 +0200267 wake_up_process(dev->worker);
268 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000269}
Asias He6ac1afb2013-05-06 16:38:21 +0800270EXPORT_SYMBOL_GPL(vhost_work_queue);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000271
Jason Wang526d3e72016-03-04 06:24:51 -0500272/* A lockless hint for busy polling code to exit the loop */
273bool vhost_has_work(struct vhost_dev *dev)
274{
Jason Wang04b96e52016-04-25 22:14:33 -0400275 return !llist_empty(&dev->work_list);
Jason Wang526d3e72016-03-04 06:24:51 -0500276}
277EXPORT_SYMBOL_GPL(vhost_has_work);
278
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300279void vhost_poll_queue(struct vhost_poll *poll)
280{
281 vhost_work_queue(poll->dev, &poll->work);
282}
Asias He6ac1afb2013-05-06 16:38:21 +0800283EXPORT_SYMBOL_GPL(vhost_poll_queue);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300284
Jason Wangf8894912017-02-28 17:56:02 +0800285static void __vhost_vq_meta_reset(struct vhost_virtqueue *vq)
286{
287 int j;
288
289 for (j = 0; j < VHOST_NUM_ADDRS; j++)
290 vq->meta_iotlb[j] = NULL;
291}
292
293static void vhost_vq_meta_reset(struct vhost_dev *d)
294{
295 int i;
296
Jason Wang86a07da2018-12-13 10:53:39 +0800297 for (i = 0; i < d->nvqs; ++i)
Jason Wangf8894912017-02-28 17:56:02 +0800298 __vhost_vq_meta_reset(d->vqs[i]);
299}
300
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000301static void vhost_vq_reset(struct vhost_dev *dev,
302 struct vhost_virtqueue *vq)
303{
304 vq->num = 1;
305 vq->desc = NULL;
306 vq->avail = NULL;
307 vq->used = NULL;
308 vq->last_avail_idx = 0;
309 vq->avail_idx = 0;
310 vq->last_used_idx = 0;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300311 vq->signalled_used = 0;
312 vq->signalled_used_valid = false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000313 vq->used_flags = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000314 vq->log_used = false;
315 vq->log_addr = -1ull;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000316 vq->private_data = NULL;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300317 vq->acked_features = 0;
Jason Wang429711a2018-08-06 11:17:47 +0800318 vq->acked_backend_features = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000319 vq->log_base = NULL;
320 vq->error_ctx = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000321 vq->kick = NULL;
322 vq->call_ctx = NULL;
Michael S. Tsirkin73a99f02010-02-23 11:23:45 +0200323 vq->log_ctx = NULL;
Greg Kurzc5072032016-02-16 15:59:34 +0100324 vhost_reset_is_le(vq);
325 vhost_disable_cross_endian(vq);
Jason Wang03088132016-03-04 06:24:53 -0500326 vq->busyloop_timeout = 0;
Jason Wanga9709d62016-06-23 02:04:31 -0400327 vq->umem = NULL;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400328 vq->iotlb = NULL;
Jason Wangf8894912017-02-28 17:56:02 +0800329 __vhost_vq_meta_reset(vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000330}
331
Tejun Heoc23f34452010-06-02 20:40:00 +0200332static int vhost_worker(void *data)
333{
334 struct vhost_dev *dev = data;
Jason Wang04b96e52016-04-25 22:14:33 -0400335 struct vhost_work *work, *work_next;
336 struct llist_node *node;
Jens Freimannd7ffde32012-06-26 00:59:58 +0000337 mm_segment_t oldfs = get_fs();
Tejun Heoc23f34452010-06-02 20:40:00 +0200338
Jens Freimannd7ffde32012-06-26 00:59:58 +0000339 set_fs(USER_DS);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200340 use_mm(dev->mm);
341
Tejun Heoc23f34452010-06-02 20:40:00 +0200342 for (;;) {
343 /* mb paired w/ kthread_stop */
344 set_current_state(TASK_INTERRUPTIBLE);
345
Tejun Heoc23f34452010-06-02 20:40:00 +0200346 if (kthread_should_stop()) {
Tejun Heoc23f34452010-06-02 20:40:00 +0200347 __set_current_state(TASK_RUNNING);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200348 break;
Tejun Heoc23f34452010-06-02 20:40:00 +0200349 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200350
Jason Wang04b96e52016-04-25 22:14:33 -0400351 node = llist_del_all(&dev->work_list);
352 if (!node)
353 schedule();
354
355 node = llist_reverse_order(node);
356 /* make sure flag is seen after deletion */
357 smp_wmb();
358 llist_for_each_entry_safe(work, work_next, node, node) {
359 clear_bit(VHOST_WORK_QUEUED, &work->flags);
Tejun Heoc23f34452010-06-02 20:40:00 +0200360 __set_current_state(TASK_RUNNING);
Andrey Konovalov8f6a7f92019-12-04 16:52:50 -0800361 kcov_remote_start_common(dev->kcov_handle);
Tejun Heoc23f34452010-06-02 20:40:00 +0200362 work->fn(work);
Andrey Konovalov8f6a7f92019-12-04 16:52:50 -0800363 kcov_remote_stop();
Nadav Har'Eld550dda2012-02-27 15:07:29 +0200364 if (need_resched())
365 schedule();
Jason Wang04b96e52016-04-25 22:14:33 -0400366 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200367 }
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200368 unuse_mm(dev->mm);
Jens Freimannd7ffde32012-06-26 00:59:58 +0000369 set_fs(oldfs);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200370 return 0;
Tejun Heoc23f34452010-06-02 20:40:00 +0200371}
372
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000373static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
374{
375 kfree(vq->indirect);
376 vq->indirect = NULL;
377 kfree(vq->log);
378 vq->log = NULL;
379 kfree(vq->heads);
380 vq->heads = NULL;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000381}
382
Jason Wange0e9b402010-09-14 23:53:05 +0800383/* Helper to allocate iovec buffers for all vqs. */
384static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
385{
Asias He6d5e6aa2013-05-06 16:38:23 +0800386 struct vhost_virtqueue *vq;
Jason Wange0e9b402010-09-14 23:53:05 +0800387 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530388
Jason Wange0e9b402010-09-14 23:53:05 +0800389 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800390 vq = dev->vqs[i];
Kees Cook6da2ec52018-06-12 13:55:00 -0700391 vq->indirect = kmalloc_array(UIO_MAXIOV,
392 sizeof(*vq->indirect),
393 GFP_KERNEL);
Jason Wangb46a0bf2019-01-28 15:05:05 +0800394 vq->log = kmalloc_array(dev->iov_limit, sizeof(*vq->log),
Kees Cook6da2ec52018-06-12 13:55:00 -0700395 GFP_KERNEL);
Jason Wangb46a0bf2019-01-28 15:05:05 +0800396 vq->heads = kmalloc_array(dev->iov_limit, sizeof(*vq->heads),
Kees Cook6da2ec52018-06-12 13:55:00 -0700397 GFP_KERNEL);
Asias He6d5e6aa2013-05-06 16:38:23 +0800398 if (!vq->indirect || !vq->log || !vq->heads)
Jason Wange0e9b402010-09-14 23:53:05 +0800399 goto err_nomem;
400 }
401 return 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530402
Jason Wange0e9b402010-09-14 23:53:05 +0800403err_nomem:
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000404 for (; i >= 0; --i)
Asias He3ab2e422013-04-27 11:16:48 +0800405 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800406 return -ENOMEM;
407}
408
409static void vhost_dev_free_iovecs(struct vhost_dev *dev)
410{
411 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530412
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000413 for (i = 0; i < dev->nvqs; ++i)
Asias He3ab2e422013-04-27 11:16:48 +0800414 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800415}
416
Jason Wange82b9b02019-05-17 00:29:49 -0400417bool vhost_exceeds_weight(struct vhost_virtqueue *vq,
418 int pkts, int total_len)
419{
420 struct vhost_dev *dev = vq->dev;
421
422 if ((dev->byte_weight && total_len >= dev->byte_weight) ||
423 pkts >= dev->weight) {
424 vhost_poll_queue(&vq->poll);
425 return true;
426 }
427
428 return false;
429}
430EXPORT_SYMBOL_GPL(vhost_exceeds_weight);
431
Jason Wang4942e822019-05-24 04:12:16 -0400432static size_t vhost_get_avail_size(struct vhost_virtqueue *vq,
433 unsigned int num)
434{
435 size_t event __maybe_unused =
436 vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
437
438 return sizeof(*vq->avail) +
439 sizeof(*vq->avail->ring) * num + event;
440}
441
442static size_t vhost_get_used_size(struct vhost_virtqueue *vq,
443 unsigned int num)
444{
445 size_t event __maybe_unused =
446 vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
447
448 return sizeof(*vq->used) +
449 sizeof(*vq->used->ring) * num + event;
450}
451
452static size_t vhost_get_desc_size(struct vhost_virtqueue *vq,
453 unsigned int num)
454{
455 return sizeof(*vq->desc) * num;
456}
457
Zhi Yong Wu59566b6e2013-12-07 04:13:03 +0800458void vhost_dev_init(struct vhost_dev *dev,
Jason Wange82b9b02019-05-17 00:29:49 -0400459 struct vhost_virtqueue **vqs, int nvqs,
460 int iov_limit, int weight, int byte_weight)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000461{
Asias He6d5e6aa2013-05-06 16:38:23 +0800462 struct vhost_virtqueue *vq;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000463 int i;
Tejun Heoc23f34452010-06-02 20:40:00 +0200464
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000465 dev->vqs = vqs;
466 dev->nvqs = nvqs;
467 mutex_init(&dev->mutex);
468 dev->log_ctx = NULL;
Jason Wanga9709d62016-06-23 02:04:31 -0400469 dev->umem = NULL;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400470 dev->iotlb = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000471 dev->mm = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200472 dev->worker = NULL;
Jason Wangb46a0bf2019-01-28 15:05:05 +0800473 dev->iov_limit = iov_limit;
Jason Wange82b9b02019-05-17 00:29:49 -0400474 dev->weight = weight;
475 dev->byte_weight = byte_weight;
Jason Wang04b96e52016-04-25 22:14:33 -0400476 init_llist_head(&dev->work_list);
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400477 init_waitqueue_head(&dev->wait);
478 INIT_LIST_HEAD(&dev->read_list);
479 INIT_LIST_HEAD(&dev->pending_list);
480 spin_lock_init(&dev->iotlb_lock);
Michael S. Tsirkin3d2c7d32019-08-10 13:53:21 -0400481
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000482
483 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800484 vq = dev->vqs[i];
485 vq->log = NULL;
486 vq->indirect = NULL;
487 vq->heads = NULL;
488 vq->dev = dev;
489 mutex_init(&vq->mutex);
490 vhost_vq_reset(dev, vq);
491 if (vq->handle_kick)
492 vhost_poll_init(&vq->poll, vq->handle_kick,
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800493 EPOLLIN, dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000494 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000495}
Asias He6ac1afb2013-05-06 16:38:21 +0800496EXPORT_SYMBOL_GPL(vhost_dev_init);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000497
498/* Caller should have device mutex */
499long vhost_dev_check_owner(struct vhost_dev *dev)
500{
501 /* Are you the owner? If not, I don't think you mean to do that */
502 return dev->mm == current->mm ? 0 : -EPERM;
503}
Asias He6ac1afb2013-05-06 16:38:21 +0800504EXPORT_SYMBOL_GPL(vhost_dev_check_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000505
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300506struct vhost_attach_cgroups_struct {
Krishna Kumard47effe2011-03-01 17:06:37 +0530507 struct vhost_work work;
508 struct task_struct *owner;
509 int ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300510};
511
512static void vhost_attach_cgroups_work(struct vhost_work *work)
513{
Krishna Kumard47effe2011-03-01 17:06:37 +0530514 struct vhost_attach_cgroups_struct *s;
515
516 s = container_of(work, struct vhost_attach_cgroups_struct, work);
517 s->ret = cgroup_attach_task_all(s->owner, current);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300518}
519
520static int vhost_attach_cgroups(struct vhost_dev *dev)
521{
Krishna Kumard47effe2011-03-01 17:06:37 +0530522 struct vhost_attach_cgroups_struct attach;
523
524 attach.owner = current;
525 vhost_work_init(&attach.work, vhost_attach_cgroups_work);
526 vhost_work_queue(dev, &attach.work);
527 vhost_work_flush(dev, &attach.work);
528 return attach.ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300529}
530
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000531/* Caller should have device mutex */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300532bool vhost_dev_has_owner(struct vhost_dev *dev)
533{
534 return dev->mm;
535}
Asias He6ac1afb2013-05-06 16:38:21 +0800536EXPORT_SYMBOL_GPL(vhost_dev_has_owner);
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300537
538/* Caller should have device mutex */
Asias He54db63c2013-05-06 11:15:59 +0800539long vhost_dev_set_owner(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000540{
Tejun Heoc23f34452010-06-02 20:40:00 +0200541 struct task_struct *worker;
542 int err;
Krishna Kumard47effe2011-03-01 17:06:37 +0530543
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000544 /* Is there an owner already? */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300545 if (vhost_dev_has_owner(dev)) {
Tejun Heoc23f34452010-06-02 20:40:00 +0200546 err = -EBUSY;
547 goto err_mm;
548 }
Krishna Kumard47effe2011-03-01 17:06:37 +0530549
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000550 /* No owner, become one */
551 dev->mm = get_task_mm(current);
Andrey Konovalov8f6a7f92019-12-04 16:52:50 -0800552 dev->kcov_handle = kcov_common_handle();
Tejun Heoc23f34452010-06-02 20:40:00 +0200553 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
554 if (IS_ERR(worker)) {
555 err = PTR_ERR(worker);
556 goto err_worker;
557 }
558
559 dev->worker = worker;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300560 wake_up_process(worker); /* avoid contributing to loadavg */
561
562 err = vhost_attach_cgroups(dev);
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300563 if (err)
564 goto err_cgroup;
Tejun Heoc23f34452010-06-02 20:40:00 +0200565
Jason Wange0e9b402010-09-14 23:53:05 +0800566 err = vhost_dev_alloc_iovecs(dev);
567 if (err)
568 goto err_cgroup;
569
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000570 return 0;
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300571err_cgroup:
572 kthread_stop(worker);
Michael S. Tsirkin615cc222010-09-02 14:16:36 +0300573 dev->worker = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200574err_worker:
575 if (dev->mm)
576 mmput(dev->mm);
577 dev->mm = NULL;
Andrey Konovalov8f6a7f92019-12-04 16:52:50 -0800578 dev->kcov_handle = 0;
Tejun Heoc23f34452010-06-02 20:40:00 +0200579err_mm:
580 return err;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000581}
Asias He6ac1afb2013-05-06 16:38:21 +0800582EXPORT_SYMBOL_GPL(vhost_dev_set_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000583
Jason Wanga9709d62016-06-23 02:04:31 -0400584struct vhost_umem *vhost_dev_reset_owner_prepare(void)
585{
Michal Hocko6c5ab652017-05-08 15:57:15 -0700586 return kvzalloc(sizeof(struct vhost_umem), GFP_KERNEL);
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300587}
Asias He6ac1afb2013-05-06 16:38:21 +0800588EXPORT_SYMBOL_GPL(vhost_dev_reset_owner_prepare);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000589
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300590/* Caller should have device mutex */
Jason Wanga9709d62016-06-23 02:04:31 -0400591void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_umem *umem)
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300592{
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300593 int i;
594
夷则(Caspar)f6f93f72017-12-25 00:08:58 +0800595 vhost_dev_cleanup(dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000596
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300597 /* Restore memory to default empty mapping. */
Jason Wanga9709d62016-06-23 02:04:31 -0400598 INIT_LIST_HEAD(&umem->umem_list);
599 dev->umem = umem;
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300600 /* We don't need VQ locks below since vhost_dev_cleanup makes sure
601 * VQs aren't running.
602 */
603 for (i = 0; i < dev->nvqs; ++i)
Jason Wanga9709d62016-06-23 02:04:31 -0400604 dev->vqs[i]->umem = umem;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000605}
Asias He6ac1afb2013-05-06 16:38:21 +0800606EXPORT_SYMBOL_GPL(vhost_dev_reset_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000607
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000608void vhost_dev_stop(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000609{
610 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530611
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000612 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800613 if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) {
614 vhost_poll_stop(&dev->vqs[i]->poll);
615 vhost_poll_flush(&dev->vqs[i]->poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000616 }
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000617 }
618}
Asias He6ac1afb2013-05-06 16:38:21 +0800619EXPORT_SYMBOL_GPL(vhost_dev_stop);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000620
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400621static void vhost_umem_free(struct vhost_umem *umem,
622 struct vhost_umem_node *node)
623{
624 vhost_umem_interval_tree_remove(node, &umem->umem_tree);
625 list_del(&node->link);
626 kfree(node);
627 umem->numem--;
628}
629
Jason Wanga9709d62016-06-23 02:04:31 -0400630static void vhost_umem_clean(struct vhost_umem *umem)
631{
632 struct vhost_umem_node *node, *tmp;
633
634 if (!umem)
635 return;
636
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400637 list_for_each_entry_safe(node, tmp, &umem->umem_list, link)
638 vhost_umem_free(umem, node);
639
Jason Wanga9709d62016-06-23 02:04:31 -0400640 kvfree(umem);
641}
642
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400643static void vhost_clear_msg(struct vhost_dev *dev)
644{
645 struct vhost_msg_node *node, *n;
646
647 spin_lock(&dev->iotlb_lock);
648
649 list_for_each_entry_safe(node, n, &dev->read_list, node) {
650 list_del(&node->node);
651 kfree(node);
652 }
653
654 list_for_each_entry_safe(node, n, &dev->pending_list, node) {
655 list_del(&node->node);
656 kfree(node);
657 }
658
659 spin_unlock(&dev->iotlb_lock);
660}
661
夷则(Caspar)f6f93f72017-12-25 00:08:58 +0800662void vhost_dev_cleanup(struct vhost_dev *dev)
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000663{
664 int i;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000665
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000666 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800667 if (dev->vqs[i]->error_ctx)
668 eventfd_ctx_put(dev->vqs[i]->error_ctx);
Asias He3ab2e422013-04-27 11:16:48 +0800669 if (dev->vqs[i]->kick)
670 fput(dev->vqs[i]->kick);
671 if (dev->vqs[i]->call_ctx)
672 eventfd_ctx_put(dev->vqs[i]->call_ctx);
Asias He3ab2e422013-04-27 11:16:48 +0800673 vhost_vq_reset(dev, dev->vqs[i]);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000674 }
Jason Wange0e9b402010-09-14 23:53:05 +0800675 vhost_dev_free_iovecs(dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000676 if (dev->log_ctx)
677 eventfd_ctx_put(dev->log_ctx);
678 dev->log_ctx = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000679 /* No one will access memory at this point */
Jason Wanga9709d62016-06-23 02:04:31 -0400680 vhost_umem_clean(dev->umem);
681 dev->umem = NULL;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400682 vhost_umem_clean(dev->iotlb);
683 dev->iotlb = NULL;
684 vhost_clear_msg(dev);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800685 wake_up_interruptible_poll(&dev->wait, EPOLLIN | EPOLLRDNORM);
Jason Wang04b96e52016-04-25 22:14:33 -0400686 WARN_ON(!llist_empty(&dev->work_list));
Eric Dumazet78b620c2010-08-31 02:05:57 +0000687 if (dev->worker) {
688 kthread_stop(dev->worker);
689 dev->worker = NULL;
Andrey Konovalov8f6a7f92019-12-04 16:52:50 -0800690 dev->kcov_handle = 0;
Eric Dumazet78b620c2010-08-31 02:05:57 +0000691 }
Michael S. Tsirkin3d2c7d32019-08-10 13:53:21 -0400692 if (dev->mm)
Michael S. Tsirkin533a19b2010-10-06 15:34:38 +0200693 mmput(dev->mm);
694 dev->mm = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000695}
Asias He6ac1afb2013-05-06 16:38:21 +0800696EXPORT_SYMBOL_GPL(vhost_dev_cleanup);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000697
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800698static bool log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000699{
700 u64 a = addr / VHOST_PAGE_SIZE / 8;
Krishna Kumard47effe2011-03-01 17:06:37 +0530701
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000702 /* Make sure 64 bit math will not overflow. */
703 if (a > ULONG_MAX - (unsigned long)log_base ||
704 a + (unsigned long)log_base > ULONG_MAX)
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800705 return false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000706
Linus Torvalds96d4f262019-01-03 18:57:57 -0800707 return access_ok(log_base + a,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000708 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
709}
710
Michael S. Tsirkinec33d032016-08-01 23:20:53 +0300711static bool vhost_overflow(u64 uaddr, u64 size)
712{
713 /* Make sure 64 bit math will not overflow. */
714 return uaddr > ULONG_MAX || size > ULONG_MAX || uaddr > ULONG_MAX - size;
715}
716
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000717/* Caller should have vq mutex and device mutex. */
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800718static bool vq_memory_access_ok(void __user *log_base, struct vhost_umem *umem,
719 int log_all)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000720{
Jason Wanga9709d62016-06-23 02:04:31 -0400721 struct vhost_umem_node *node;
Jeff Dike179b2842010-04-07 09:59:10 -0400722
Jason Wanga9709d62016-06-23 02:04:31 -0400723 if (!umem)
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800724 return false;
Jeff Dike179b2842010-04-07 09:59:10 -0400725
Jason Wanga9709d62016-06-23 02:04:31 -0400726 list_for_each_entry(node, &umem->umem_list, link) {
727 unsigned long a = node->userspace_addr;
728
Michael S. Tsirkinec33d032016-08-01 23:20:53 +0300729 if (vhost_overflow(node->userspace_addr, node->size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800730 return false;
Michael S. Tsirkinec33d032016-08-01 23:20:53 +0300731
732
Linus Torvalds96d4f262019-01-03 18:57:57 -0800733 if (!access_ok((void __user *)a,
Jason Wanga9709d62016-06-23 02:04:31 -0400734 node->size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800735 return false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000736 else if (log_all && !log_access_ok(log_base,
Jason Wanga9709d62016-06-23 02:04:31 -0400737 node->start,
738 node->size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800739 return false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000740 }
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800741 return true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000742}
743
Jason Wangf8894912017-02-28 17:56:02 +0800744static inline void __user *vhost_vq_meta_fetch(struct vhost_virtqueue *vq,
745 u64 addr, unsigned int size,
746 int type)
747{
748 const struct vhost_umem_node *node = vq->meta_iotlb[type];
749
750 if (!node)
751 return NULL;
752
753 return (void *)(uintptr_t)(node->userspace_addr + addr - node->start);
754}
755
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000756/* Can we switch to this memory table? */
757/* Caller should have device mutex but not vq mutex */
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800758static bool memory_access_ok(struct vhost_dev *d, struct vhost_umem *umem,
759 int log_all)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000760{
761 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530762
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000763 for (i = 0; i < d->nvqs; ++i) {
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800764 bool ok;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300765 bool log;
766
Asias He3ab2e422013-04-27 11:16:48 +0800767 mutex_lock(&d->vqs[i]->mutex);
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300768 log = log_all || vhost_has_feature(d->vqs[i], VHOST_F_LOG_ALL);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000769 /* If ring is inactive, will check when it's enabled. */
Asias He3ab2e422013-04-27 11:16:48 +0800770 if (d->vqs[i]->private_data)
Jason Wanga9709d62016-06-23 02:04:31 -0400771 ok = vq_memory_access_ok(d->vqs[i]->log_base,
772 umem, log);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000773 else
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800774 ok = true;
Asias He3ab2e422013-04-27 11:16:48 +0800775 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000776 if (!ok)
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800777 return false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000778 }
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800779 return true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000780}
781
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400782static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
783 struct iovec iov[], int iov_size, int access);
Jason Wangbfe2bc52016-06-23 02:04:30 -0400784
Michael S. Tsirkin72952cc2016-12-06 06:01:41 +0200785static int vhost_copy_to_user(struct vhost_virtqueue *vq, void __user *to,
Jason Wangbfe2bc52016-06-23 02:04:30 -0400786 const void *from, unsigned size)
787{
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400788 int ret;
Jason Wangbfe2bc52016-06-23 02:04:30 -0400789
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400790 if (!vq->iotlb)
791 return __copy_to_user(to, from, size);
792 else {
793 /* This function should be called after iotlb
794 * prefetch, which means we're sure that all vq
795 * could be access through iotlb. So -EAGAIN should
796 * not happen in this case.
797 */
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400798 struct iov_iter t;
Jason Wangf8894912017-02-28 17:56:02 +0800799 void __user *uaddr = vhost_vq_meta_fetch(vq,
800 (u64)(uintptr_t)to, size,
Eric Auger7ced6c92018-04-11 15:30:38 +0200801 VHOST_ADDR_USED);
Jason Wangf8894912017-02-28 17:56:02 +0800802
803 if (uaddr)
804 return __copy_to_user(uaddr, from, size);
805
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400806 ret = translate_desc(vq, (u64)(uintptr_t)to, size, vq->iotlb_iov,
807 ARRAY_SIZE(vq->iotlb_iov),
808 VHOST_ACCESS_WO);
809 if (ret < 0)
810 goto out;
811 iov_iter_init(&t, WRITE, vq->iotlb_iov, ret, size);
812 ret = copy_to_iter(from, size, &t);
813 if (ret == size)
814 ret = 0;
815 }
816out:
817 return ret;
818}
Jason Wangbfe2bc52016-06-23 02:04:30 -0400819
820static int vhost_copy_from_user(struct vhost_virtqueue *vq, void *to,
Michael S. Tsirkin72952cc2016-12-06 06:01:41 +0200821 void __user *from, unsigned size)
Jason Wangbfe2bc52016-06-23 02:04:30 -0400822{
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400823 int ret;
824
825 if (!vq->iotlb)
826 return __copy_from_user(to, from, size);
827 else {
828 /* This function should be called after iotlb
829 * prefetch, which means we're sure that vq
830 * could be access through iotlb. So -EAGAIN should
831 * not happen in this case.
832 */
Jason Wangf8894912017-02-28 17:56:02 +0800833 void __user *uaddr = vhost_vq_meta_fetch(vq,
834 (u64)(uintptr_t)from, size,
835 VHOST_ADDR_DESC);
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400836 struct iov_iter f;
Jason Wangf8894912017-02-28 17:56:02 +0800837
838 if (uaddr)
839 return __copy_from_user(to, uaddr, size);
840
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400841 ret = translate_desc(vq, (u64)(uintptr_t)from, size, vq->iotlb_iov,
842 ARRAY_SIZE(vq->iotlb_iov),
843 VHOST_ACCESS_RO);
844 if (ret < 0) {
845 vq_err(vq, "IOTLB translation failure: uaddr "
846 "%p size 0x%llx\n", from,
847 (unsigned long long) size);
848 goto out;
849 }
850 iov_iter_init(&f, READ, vq->iotlb_iov, ret, size);
851 ret = copy_from_iter(to, size, &f);
852 if (ret == size)
853 ret = 0;
854 }
855
856out:
857 return ret;
858}
859
Jason Wangf8894912017-02-28 17:56:02 +0800860static void __user *__vhost_get_user_slow(struct vhost_virtqueue *vq,
861 void __user *addr, unsigned int size,
862 int type)
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400863{
864 int ret;
865
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400866 ret = translate_desc(vq, (u64)(uintptr_t)addr, size, vq->iotlb_iov,
867 ARRAY_SIZE(vq->iotlb_iov),
868 VHOST_ACCESS_RO);
869 if (ret < 0) {
870 vq_err(vq, "IOTLB translation failure: uaddr "
871 "%p size 0x%llx\n", addr,
872 (unsigned long long) size);
873 return NULL;
874 }
875
876 if (ret != 1 || vq->iotlb_iov[0].iov_len != size) {
877 vq_err(vq, "Non atomic userspace memory access: uaddr "
878 "%p size 0x%llx\n", addr,
879 (unsigned long long) size);
880 return NULL;
881 }
882
883 return vq->iotlb_iov[0].iov_base;
884}
885
Jason Wangf8894912017-02-28 17:56:02 +0800886/* This function should be called after iotlb
887 * prefetch, which means we're sure that vq
888 * could be access through iotlb. So -EAGAIN should
889 * not happen in this case.
890 */
891static inline void __user *__vhost_get_user(struct vhost_virtqueue *vq,
892 void *addr, unsigned int size,
893 int type)
894{
895 void __user *uaddr = vhost_vq_meta_fetch(vq,
896 (u64)(uintptr_t)addr, size, type);
897 if (uaddr)
898 return uaddr;
899
900 return __vhost_get_user_slow(vq, addr, size, type);
901}
902
903#define vhost_put_user(vq, x, ptr) \
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400904({ \
905 int ret = -EFAULT; \
906 if (!vq->iotlb) { \
907 ret = __put_user(x, ptr); \
908 } else { \
909 __typeof__(ptr) to = \
Jason Wangf8894912017-02-28 17:56:02 +0800910 (__typeof__(ptr)) __vhost_get_user(vq, ptr, \
911 sizeof(*ptr), VHOST_ADDR_USED); \
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400912 if (to != NULL) \
913 ret = __put_user(x, to); \
914 else \
915 ret = -EFAULT; \
916 } \
917 ret; \
918})
919
Jason Wang7b5d7532019-05-24 04:12:14 -0400920static inline int vhost_put_avail_event(struct vhost_virtqueue *vq)
921{
922 return vhost_put_user(vq, cpu_to_vhost16(vq, vq->avail_idx),
923 vhost_avail_event(vq));
924}
925
926static inline int vhost_put_used(struct vhost_virtqueue *vq,
927 struct vring_used_elem *head, int idx,
928 int count)
929{
930 return vhost_copy_to_user(vq, vq->used->ring + idx, head,
931 count * sizeof(*head));
932}
933
934static inline int vhost_put_used_flags(struct vhost_virtqueue *vq)
935
936{
937 return vhost_put_user(vq, cpu_to_vhost16(vq, vq->used_flags),
938 &vq->used->flags);
939}
940
941static inline int vhost_put_used_idx(struct vhost_virtqueue *vq)
942
943{
944 return vhost_put_user(vq, cpu_to_vhost16(vq, vq->last_used_idx),
945 &vq->used->idx);
946}
947
Jason Wangf8894912017-02-28 17:56:02 +0800948#define vhost_get_user(vq, x, ptr, type) \
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400949({ \
950 int ret; \
951 if (!vq->iotlb) { \
952 ret = __get_user(x, ptr); \
953 } else { \
954 __typeof__(ptr) from = \
Jason Wangf8894912017-02-28 17:56:02 +0800955 (__typeof__(ptr)) __vhost_get_user(vq, ptr, \
956 sizeof(*ptr), \
957 type); \
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400958 if (from != NULL) \
959 ret = __get_user(x, from); \
960 else \
961 ret = -EFAULT; \
962 } \
963 ret; \
964})
965
Jason Wangf8894912017-02-28 17:56:02 +0800966#define vhost_get_avail(vq, x, ptr) \
967 vhost_get_user(vq, x, ptr, VHOST_ADDR_AVAIL)
968
969#define vhost_get_used(vq, x, ptr) \
970 vhost_get_user(vq, x, ptr, VHOST_ADDR_USED)
971
Jason Wang86a07da2018-12-13 10:53:39 +0800972static void vhost_dev_lock_vqs(struct vhost_dev *d)
973{
974 int i = 0;
975 for (i = 0; i < d->nvqs; ++i)
976 mutex_lock_nested(&d->vqs[i]->mutex, i);
977}
978
979static void vhost_dev_unlock_vqs(struct vhost_dev *d)
980{
981 int i = 0;
982 for (i = 0; i < d->nvqs; ++i)
983 mutex_unlock(&d->vqs[i]->mutex);
984}
985
Jason Wang7b5d7532019-05-24 04:12:14 -0400986static inline int vhost_get_avail_idx(struct vhost_virtqueue *vq,
987 __virtio16 *idx)
988{
989 return vhost_get_avail(vq, *idx, &vq->avail->idx);
990}
991
992static inline int vhost_get_avail_head(struct vhost_virtqueue *vq,
993 __virtio16 *head, int idx)
994{
995 return vhost_get_avail(vq, *head,
996 &vq->avail->ring[idx & (vq->num - 1)]);
997}
998
999static inline int vhost_get_avail_flags(struct vhost_virtqueue *vq,
1000 __virtio16 *flags)
1001{
1002 return vhost_get_avail(vq, *flags, &vq->avail->flags);
1003}
1004
1005static inline int vhost_get_used_event(struct vhost_virtqueue *vq,
1006 __virtio16 *event)
1007{
1008 return vhost_get_avail(vq, *event, vhost_used_event(vq));
1009}
1010
1011static inline int vhost_get_used_idx(struct vhost_virtqueue *vq,
1012 __virtio16 *idx)
1013{
1014 return vhost_get_used(vq, *idx, &vq->used->idx);
1015}
1016
1017static inline int vhost_get_desc(struct vhost_virtqueue *vq,
1018 struct vring_desc *desc, int idx)
1019{
1020 return vhost_copy_from_user(vq, desc, vq->desc + idx, sizeof(*desc));
1021}
1022
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001023static int vhost_new_umem_range(struct vhost_umem *umem,
1024 u64 start, u64 size, u64 end,
1025 u64 userspace_addr, int perm)
1026{
Jason Wang813dbeb2019-04-09 12:10:25 +08001027 struct vhost_umem_node *tmp, *node;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001028
Jason Wang813dbeb2019-04-09 12:10:25 +08001029 if (!size)
1030 return -EFAULT;
1031
1032 node = kmalloc(sizeof(*node), GFP_ATOMIC);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001033 if (!node)
1034 return -ENOMEM;
1035
1036 if (umem->numem == max_iotlb_entries) {
1037 tmp = list_first_entry(&umem->umem_list, typeof(*tmp), link);
1038 vhost_umem_free(umem, tmp);
1039 }
1040
1041 node->start = start;
1042 node->size = size;
1043 node->last = end;
1044 node->userspace_addr = userspace_addr;
1045 node->perm = perm;
1046 INIT_LIST_HEAD(&node->link);
1047 list_add_tail(&node->link, &umem->umem_list);
1048 vhost_umem_interval_tree_insert(node, &umem->umem_tree);
1049 umem->numem++;
1050
1051 return 0;
1052}
1053
1054static void vhost_del_umem_range(struct vhost_umem *umem,
1055 u64 start, u64 end)
1056{
1057 struct vhost_umem_node *node;
1058
1059 while ((node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
1060 start, end)))
1061 vhost_umem_free(umem, node);
1062}
1063
1064static void vhost_iotlb_notify_vq(struct vhost_dev *d,
1065 struct vhost_iotlb_msg *msg)
1066{
1067 struct vhost_msg_node *node, *n;
1068
1069 spin_lock(&d->iotlb_lock);
1070
1071 list_for_each_entry_safe(node, n, &d->pending_list, node) {
1072 struct vhost_iotlb_msg *vq_msg = &node->msg.iotlb;
1073 if (msg->iova <= vq_msg->iova &&
Jason Wang2d66f992018-08-24 16:53:13 +08001074 msg->iova + msg->size - 1 >= vq_msg->iova &&
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001075 vq_msg->type == VHOST_IOTLB_MISS) {
1076 vhost_poll_queue(&node->vq->poll);
1077 list_del(&node->node);
1078 kfree(node);
1079 }
1080 }
1081
1082 spin_unlock(&d->iotlb_lock);
1083}
1084
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001085static bool umem_access_ok(u64 uaddr, u64 size, int access)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001086{
1087 unsigned long a = uaddr;
1088
Michael S. Tsirkinec33d032016-08-01 23:20:53 +03001089 /* Make sure 64 bit math will not overflow. */
1090 if (vhost_overflow(uaddr, size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001091 return false;
Michael S. Tsirkinec33d032016-08-01 23:20:53 +03001092
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001093 if ((access & VHOST_ACCESS_RO) &&
Linus Torvalds96d4f262019-01-03 18:57:57 -08001094 !access_ok((void __user *)a, size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001095 return false;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001096 if ((access & VHOST_ACCESS_WO) &&
Linus Torvalds96d4f262019-01-03 18:57:57 -08001097 !access_ok((void __user *)a, size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001098 return false;
1099 return true;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001100}
1101
Michael S. Tsirkin72952cc2016-12-06 06:01:41 +02001102static int vhost_process_iotlb_msg(struct vhost_dev *dev,
1103 struct vhost_iotlb_msg *msg)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001104{
1105 int ret = 0;
1106
Jason Wang1b15ad62018-05-22 19:58:57 +08001107 mutex_lock(&dev->mutex);
Jason Wang86a07da2018-12-13 10:53:39 +08001108 vhost_dev_lock_vqs(dev);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001109 switch (msg->type) {
1110 case VHOST_IOTLB_UPDATE:
1111 if (!dev->iotlb) {
1112 ret = -EFAULT;
1113 break;
1114 }
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001115 if (!umem_access_ok(msg->uaddr, msg->size, msg->perm)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001116 ret = -EFAULT;
1117 break;
1118 }
Jason Wangf8894912017-02-28 17:56:02 +08001119 vhost_vq_meta_reset(dev);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001120 if (vhost_new_umem_range(dev->iotlb, msg->iova, msg->size,
1121 msg->iova + msg->size - 1,
1122 msg->uaddr, msg->perm)) {
1123 ret = -ENOMEM;
1124 break;
1125 }
1126 vhost_iotlb_notify_vq(dev, msg);
1127 break;
1128 case VHOST_IOTLB_INVALIDATE:
Jason Wang6f3180a2018-01-23 17:27:26 +08001129 if (!dev->iotlb) {
1130 ret = -EFAULT;
1131 break;
1132 }
Jason Wangf8894912017-02-28 17:56:02 +08001133 vhost_vq_meta_reset(dev);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001134 vhost_del_umem_range(dev->iotlb, msg->iova,
1135 msg->iova + msg->size - 1);
1136 break;
1137 default:
1138 ret = -EINVAL;
1139 break;
1140 }
1141
Jason Wang86a07da2018-12-13 10:53:39 +08001142 vhost_dev_unlock_vqs(dev);
Jason Wang1b15ad62018-05-22 19:58:57 +08001143 mutex_unlock(&dev->mutex);
1144
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001145 return ret;
1146}
1147ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
1148 struct iov_iter *from)
1149{
Jason Wang429711a2018-08-06 11:17:47 +08001150 struct vhost_iotlb_msg msg;
1151 size_t offset;
1152 int type, ret;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001153
Jason Wang429711a2018-08-06 11:17:47 +08001154 ret = copy_from_iter(&type, sizeof(type), from);
Pavel Tikhomirov74ad7412018-12-13 17:53:50 +03001155 if (ret != sizeof(type)) {
1156 ret = -EINVAL;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001157 goto done;
Pavel Tikhomirov74ad7412018-12-13 17:53:50 +03001158 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001159
Jason Wang429711a2018-08-06 11:17:47 +08001160 switch (type) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001161 case VHOST_IOTLB_MSG:
Jason Wang429711a2018-08-06 11:17:47 +08001162 /* There maybe a hole after type for V1 message type,
1163 * so skip it here.
1164 */
1165 offset = offsetof(struct vhost_msg, iotlb) - sizeof(int);
1166 break;
1167 case VHOST_IOTLB_MSG_V2:
1168 offset = sizeof(__u32);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001169 break;
1170 default:
1171 ret = -EINVAL;
Jason Wang429711a2018-08-06 11:17:47 +08001172 goto done;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001173 }
1174
Jason Wang429711a2018-08-06 11:17:47 +08001175 iov_iter_advance(from, offset);
1176 ret = copy_from_iter(&msg, sizeof(msg), from);
Pavel Tikhomirov74ad7412018-12-13 17:53:50 +03001177 if (ret != sizeof(msg)) {
1178 ret = -EINVAL;
Jason Wang429711a2018-08-06 11:17:47 +08001179 goto done;
Pavel Tikhomirov74ad7412018-12-13 17:53:50 +03001180 }
Jason Wang429711a2018-08-06 11:17:47 +08001181 if (vhost_process_iotlb_msg(dev, &msg)) {
1182 ret = -EFAULT;
1183 goto done;
1184 }
1185
1186 ret = (type == VHOST_IOTLB_MSG) ? sizeof(struct vhost_msg) :
1187 sizeof(struct vhost_msg_v2);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001188done:
1189 return ret;
1190}
1191EXPORT_SYMBOL(vhost_chr_write_iter);
1192
Al Viroafc9a422017-07-03 06:39:46 -04001193__poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001194 poll_table *wait)
1195{
Al Viroafc9a422017-07-03 06:39:46 -04001196 __poll_t mask = 0;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001197
1198 poll_wait(file, &dev->wait, wait);
1199
1200 if (!list_empty(&dev->read_list))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001201 mask |= EPOLLIN | EPOLLRDNORM;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001202
1203 return mask;
1204}
1205EXPORT_SYMBOL(vhost_chr_poll);
1206
1207ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to,
1208 int noblock)
1209{
1210 DEFINE_WAIT(wait);
1211 struct vhost_msg_node *node;
1212 ssize_t ret = 0;
1213 unsigned size = sizeof(struct vhost_msg);
1214
1215 if (iov_iter_count(to) < size)
1216 return 0;
1217
1218 while (1) {
1219 if (!noblock)
1220 prepare_to_wait(&dev->wait, &wait,
1221 TASK_INTERRUPTIBLE);
1222
1223 node = vhost_dequeue_msg(dev, &dev->read_list);
1224 if (node)
1225 break;
1226 if (noblock) {
1227 ret = -EAGAIN;
1228 break;
1229 }
1230 if (signal_pending(current)) {
1231 ret = -ERESTARTSYS;
1232 break;
1233 }
1234 if (!dev->iotlb) {
1235 ret = -EBADFD;
1236 break;
1237 }
1238
1239 schedule();
1240 }
1241
1242 if (!noblock)
1243 finish_wait(&dev->wait, &wait);
1244
1245 if (node) {
Jason Wang429711a2018-08-06 11:17:47 +08001246 struct vhost_iotlb_msg *msg;
1247 void *start = &node->msg;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001248
Jason Wang429711a2018-08-06 11:17:47 +08001249 switch (node->msg.type) {
1250 case VHOST_IOTLB_MSG:
1251 size = sizeof(node->msg);
1252 msg = &node->msg.iotlb;
1253 break;
1254 case VHOST_IOTLB_MSG_V2:
1255 size = sizeof(node->msg_v2);
1256 msg = &node->msg_v2.iotlb;
1257 break;
1258 default:
1259 BUG();
1260 break;
1261 }
1262
1263 ret = copy_to_iter(start, size, to);
1264 if (ret != size || msg->type != VHOST_IOTLB_MISS) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001265 kfree(node);
1266 return ret;
1267 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001268 vhost_enqueue_msg(dev, &dev->pending_list, node);
1269 }
1270
1271 return ret;
1272}
1273EXPORT_SYMBOL_GPL(vhost_chr_read_iter);
1274
1275static int vhost_iotlb_miss(struct vhost_virtqueue *vq, u64 iova, int access)
1276{
1277 struct vhost_dev *dev = vq->dev;
1278 struct vhost_msg_node *node;
1279 struct vhost_iotlb_msg *msg;
Jason Wang429711a2018-08-06 11:17:47 +08001280 bool v2 = vhost_backend_has_feature(vq, VHOST_BACKEND_F_IOTLB_MSG_V2);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001281
Jason Wang429711a2018-08-06 11:17:47 +08001282 node = vhost_new_msg(vq, v2 ? VHOST_IOTLB_MSG_V2 : VHOST_IOTLB_MSG);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001283 if (!node)
1284 return -ENOMEM;
1285
Jason Wang429711a2018-08-06 11:17:47 +08001286 if (v2) {
1287 node->msg_v2.type = VHOST_IOTLB_MSG_V2;
1288 msg = &node->msg_v2.iotlb;
1289 } else {
1290 msg = &node->msg.iotlb;
1291 }
1292
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001293 msg->type = VHOST_IOTLB_MISS;
1294 msg->iova = iova;
1295 msg->perm = access;
1296
1297 vhost_enqueue_msg(dev, &dev->read_list, node);
1298
1299 return 0;
Jason Wangbfe2bc52016-06-23 02:04:30 -04001300}
1301
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001302static bool vq_access_ok(struct vhost_virtqueue *vq, unsigned int num,
1303 struct vring_desc __user *desc,
1304 struct vring_avail __user *avail,
1305 struct vring_used __user *used)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001306
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001307{
Jason Wang4942e822019-05-24 04:12:16 -04001308 return access_ok(desc, vhost_get_desc_size(vq, num)) &&
1309 access_ok(avail, vhost_get_avail_size(vq, num)) &&
1310 access_ok(used, vhost_get_used_size(vq, num));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001311}
1312
Jason Wangf8894912017-02-28 17:56:02 +08001313static void vhost_vq_meta_update(struct vhost_virtqueue *vq,
1314 const struct vhost_umem_node *node,
1315 int type)
1316{
1317 int access = (type == VHOST_ADDR_USED) ?
1318 VHOST_ACCESS_WO : VHOST_ACCESS_RO;
1319
1320 if (likely(node->perm & access))
1321 vq->meta_iotlb[type] = node;
1322}
1323
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001324static bool iotlb_access_ok(struct vhost_virtqueue *vq,
1325 int access, u64 addr, u64 len, int type)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001326{
1327 const struct vhost_umem_node *node;
1328 struct vhost_umem *umem = vq->iotlb;
Michael S. Tsirkinca2c5b32017-08-21 22:33:33 +03001329 u64 s = 0, size, orig_addr = addr, last = addr + len - 1;
Jason Wangf8894912017-02-28 17:56:02 +08001330
1331 if (vhost_vq_meta_fetch(vq, addr, len, type))
1332 return true;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001333
1334 while (len > s) {
1335 node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
1336 addr,
Michael S. Tsirkinca2c5b32017-08-21 22:33:33 +03001337 last);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001338 if (node == NULL || node->start > addr) {
1339 vhost_iotlb_miss(vq, addr, access);
1340 return false;
1341 } else if (!(node->perm & access)) {
1342 /* Report the possible access violation by
1343 * request another translation from userspace.
1344 */
1345 return false;
1346 }
1347
1348 size = node->size - addr + node->start;
Jason Wangf8894912017-02-28 17:56:02 +08001349
1350 if (orig_addr == addr && size >= len)
1351 vhost_vq_meta_update(vq, node, type);
1352
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001353 s += size;
1354 addr += size;
1355 }
1356
1357 return true;
1358}
1359
Jason Wang9b5e8302019-05-24 04:12:15 -04001360int vq_meta_prefetch(struct vhost_virtqueue *vq)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001361{
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001362 unsigned int num = vq->num;
1363
Michael S. Tsirkin3d2c7d32019-08-10 13:53:21 -04001364 if (!vq->iotlb)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001365 return 1;
1366
1367 return iotlb_access_ok(vq, VHOST_ACCESS_RO, (u64)(uintptr_t)vq->desc,
Jason Wang4942e822019-05-24 04:12:16 -04001368 vhost_get_desc_size(vq, num), VHOST_ADDR_DESC) &&
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001369 iotlb_access_ok(vq, VHOST_ACCESS_RO, (u64)(uintptr_t)vq->avail,
Jason Wang4942e822019-05-24 04:12:16 -04001370 vhost_get_avail_size(vq, num),
Jason Wangf8894912017-02-28 17:56:02 +08001371 VHOST_ADDR_AVAIL) &&
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001372 iotlb_access_ok(vq, VHOST_ACCESS_WO, (u64)(uintptr_t)vq->used,
Jason Wang4942e822019-05-24 04:12:16 -04001373 vhost_get_used_size(vq, num), VHOST_ADDR_USED);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001374}
Jason Wang9b5e8302019-05-24 04:12:15 -04001375EXPORT_SYMBOL_GPL(vq_meta_prefetch);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001376
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001377/* Can we log writes? */
1378/* Caller should have device mutex but not vq mutex */
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001379bool vhost_log_access_ok(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001380{
Jason Wanga9709d62016-06-23 02:04:31 -04001381 return memory_access_ok(dev, dev->umem, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001382}
Asias He6ac1afb2013-05-06 16:38:21 +08001383EXPORT_SYMBOL_GPL(vhost_log_access_ok);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001384
1385/* Verify access for write logging. */
1386/* Caller should have vq mutex and device mutex */
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001387static bool vq_log_access_ok(struct vhost_virtqueue *vq,
1388 void __user *log_base)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001389{
Jason Wanga9709d62016-06-23 02:04:31 -04001390 return vq_memory_access_ok(log_base, vq->umem,
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001391 vhost_has_feature(vq, VHOST_F_LOG_ALL)) &&
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001392 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
Jason Wang4942e822019-05-24 04:12:16 -04001393 vhost_get_used_size(vq, vq->num)));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001394}
1395
1396/* Can we start vq? */
1397/* Caller should have vq mutex and device mutex */
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001398bool vhost_vq_access_ok(struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001399{
Stefan Hajnoczid14d2b72018-04-11 10:35:40 +08001400 if (!vq_log_access_ok(vq, vq->log_base))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001401 return false;
Jason Wangd65026c2018-03-29 16:00:04 +08001402
Stefan Hajnoczid14d2b72018-04-11 10:35:40 +08001403 /* Access validation occurs at prefetch time with IOTLB */
1404 if (vq->iotlb)
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001405 return true;
Jason Wangd65026c2018-03-29 16:00:04 +08001406
1407 return vq_access_ok(vq, vq->num, vq->desc, vq->avail, vq->used);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001408}
Asias He6ac1afb2013-05-06 16:38:21 +08001409EXPORT_SYMBOL_GPL(vhost_vq_access_ok);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001410
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001411static struct vhost_umem *vhost_umem_alloc(void)
1412{
Michal Hocko6c5ab652017-05-08 15:57:15 -07001413 struct vhost_umem *umem = kvzalloc(sizeof(*umem), GFP_KERNEL);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001414
1415 if (!umem)
1416 return NULL;
1417
Davidlohr Buesof808c132017-09-08 16:15:08 -07001418 umem->umem_tree = RB_ROOT_CACHED;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001419 umem->numem = 0;
1420 INIT_LIST_HEAD(&umem->umem_list);
1421
1422 return umem;
1423}
1424
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001425static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
1426{
Jason Wanga9709d62016-06-23 02:04:31 -04001427 struct vhost_memory mem, *newmem;
1428 struct vhost_memory_region *region;
Jason Wanga9709d62016-06-23 02:04:31 -04001429 struct vhost_umem *newumem, *oldumem;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001430 unsigned long size = offsetof(struct vhost_memory, regions);
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001431 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +05301432
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001433 if (copy_from_user(&mem, m, size))
1434 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001435 if (mem.padding)
1436 return -EOPNOTSUPP;
Igor Mammedovc9ce42f2015-07-02 15:08:11 +02001437 if (mem.nregions > max_mem_regions)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001438 return -E2BIG;
Matthew Wilcoxb2303d72018-06-07 07:57:18 -07001439 newmem = kvzalloc(struct_size(newmem, regions, mem.nregions),
1440 GFP_KERNEL);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001441 if (!newmem)
1442 return -ENOMEM;
1443
1444 memcpy(newmem, &mem, size);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001445 if (copy_from_user(newmem->regions, m->regions,
1446 mem.nregions * sizeof *m->regions)) {
Igor Mammedovbcfeaca2015-06-16 18:33:35 +02001447 kvfree(newmem);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001448 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001449 }
1450
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001451 newumem = vhost_umem_alloc();
Jason Wanga9709d62016-06-23 02:04:31 -04001452 if (!newumem) {
Igor Mammedov4de72552015-07-01 11:07:09 +02001453 kvfree(newmem);
Jason Wanga9709d62016-06-23 02:04:31 -04001454 return -ENOMEM;
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +09001455 }
Jason Wanga9709d62016-06-23 02:04:31 -04001456
Jason Wanga9709d62016-06-23 02:04:31 -04001457 for (region = newmem->regions;
1458 region < newmem->regions + mem.nregions;
1459 region++) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001460 if (vhost_new_umem_range(newumem,
1461 region->guest_phys_addr,
1462 region->memory_size,
1463 region->guest_phys_addr +
1464 region->memory_size - 1,
1465 region->userspace_addr,
1466 VHOST_ACCESS_RW))
Jason Wanga9709d62016-06-23 02:04:31 -04001467 goto err;
Jason Wanga9709d62016-06-23 02:04:31 -04001468 }
1469
1470 if (!memory_access_ok(d, newumem, 0))
1471 goto err;
1472
1473 oldumem = d->umem;
1474 d->umem = newumem;
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001475
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001476 /* All memory accesses are done under some VQ mutex. */
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001477 for (i = 0; i < d->nvqs; ++i) {
1478 mutex_lock(&d->vqs[i]->mutex);
Jason Wanga9709d62016-06-23 02:04:31 -04001479 d->vqs[i]->umem = newumem;
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001480 mutex_unlock(&d->vqs[i]->mutex);
1481 }
Jason Wanga9709d62016-06-23 02:04:31 -04001482
1483 kvfree(newmem);
1484 vhost_umem_clean(oldumem);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001485 return 0;
Jason Wanga9709d62016-06-23 02:04:31 -04001486
1487err:
1488 vhost_umem_clean(newumem);
1489 kvfree(newmem);
1490 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001491}
1492
Jason Wangfeebcae2019-05-24 04:12:17 -04001493static long vhost_vring_set_num(struct vhost_dev *d,
1494 struct vhost_virtqueue *vq,
1495 void __user *argp)
1496{
1497 struct vhost_vring_state s;
1498
1499 /* Resizing ring with an active backend?
1500 * You don't want to do that. */
1501 if (vq->private_data)
1502 return -EBUSY;
1503
1504 if (copy_from_user(&s, argp, sizeof s))
1505 return -EFAULT;
1506
1507 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1)))
1508 return -EINVAL;
1509 vq->num = s.num;
1510
1511 return 0;
1512}
1513
1514static long vhost_vring_set_addr(struct vhost_dev *d,
1515 struct vhost_virtqueue *vq,
1516 void __user *argp)
1517{
1518 struct vhost_vring_addr a;
1519
1520 if (copy_from_user(&a, argp, sizeof a))
1521 return -EFAULT;
1522 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG))
1523 return -EOPNOTSUPP;
1524
1525 /* For 32bit, verify that the top 32bits of the user
1526 data are set to zero. */
1527 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
1528 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
1529 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr)
1530 return -EFAULT;
1531
1532 /* Make sure it's safe to cast pointers to vring types. */
1533 BUILD_BUG_ON(__alignof__ *vq->avail > VRING_AVAIL_ALIGN_SIZE);
1534 BUILD_BUG_ON(__alignof__ *vq->used > VRING_USED_ALIGN_SIZE);
1535 if ((a.avail_user_addr & (VRING_AVAIL_ALIGN_SIZE - 1)) ||
1536 (a.used_user_addr & (VRING_USED_ALIGN_SIZE - 1)) ||
1537 (a.log_guest_addr & (VRING_USED_ALIGN_SIZE - 1)))
1538 return -EINVAL;
1539
1540 /* We only verify access here if backend is configured.
1541 * If it is not, we don't as size might not have been setup.
1542 * We will verify when backend is configured. */
1543 if (vq->private_data) {
1544 if (!vq_access_ok(vq, vq->num,
1545 (void __user *)(unsigned long)a.desc_user_addr,
1546 (void __user *)(unsigned long)a.avail_user_addr,
1547 (void __user *)(unsigned long)a.used_user_addr))
1548 return -EINVAL;
1549
1550 /* Also validate log access for used ring if enabled. */
1551 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
1552 !log_access_ok(vq->log_base, a.log_guest_addr,
1553 sizeof *vq->used +
1554 vq->num * sizeof *vq->used->ring))
1555 return -EINVAL;
1556 }
1557
1558 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
1559 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
1560 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
1561 vq->log_addr = a.log_guest_addr;
1562 vq->used = (void __user *)(unsigned long)a.used_user_addr;
1563
1564 return 0;
1565}
1566
1567static long vhost_vring_set_num_addr(struct vhost_dev *d,
1568 struct vhost_virtqueue *vq,
1569 unsigned int ioctl,
1570 void __user *argp)
1571{
1572 long r;
1573
1574 mutex_lock(&vq->mutex);
1575
1576 switch (ioctl) {
1577 case VHOST_SET_VRING_NUM:
1578 r = vhost_vring_set_num(d, vq, argp);
1579 break;
1580 case VHOST_SET_VRING_ADDR:
1581 r = vhost_vring_set_addr(d, vq, argp);
1582 break;
1583 default:
1584 BUG();
1585 }
1586
1587 mutex_unlock(&vq->mutex);
1588
1589 return r;
1590}
Sonny Rao26b36602018-03-14 10:05:06 -07001591long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001592{
Al Virocecb46f2012-08-27 14:21:39 -04001593 struct file *eventfp, *filep = NULL;
1594 bool pollstart = false, pollstop = false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001595 struct eventfd_ctx *ctx = NULL;
1596 u32 __user *idxp = argp;
1597 struct vhost_virtqueue *vq;
1598 struct vhost_vring_state s;
1599 struct vhost_vring_file f;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001600 u32 idx;
1601 long r;
1602
1603 r = get_user(idx, idxp);
1604 if (r < 0)
1605 return r;
Krishna Kumar0f3d9a12010-05-25 11:10:36 +05301606 if (idx >= d->nvqs)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001607 return -ENOBUFS;
1608
Jason Wangff002262018-10-30 14:10:49 +08001609 idx = array_index_nospec(idx, d->nvqs);
Asias He3ab2e422013-04-27 11:16:48 +08001610 vq = d->vqs[idx];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001611
Jason Wangfeebcae2019-05-24 04:12:17 -04001612 if (ioctl == VHOST_SET_VRING_NUM ||
1613 ioctl == VHOST_SET_VRING_ADDR) {
1614 return vhost_vring_set_num_addr(d, vq, ioctl, argp);
1615 }
1616
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001617 mutex_lock(&vq->mutex);
1618
1619 switch (ioctl) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001620 case VHOST_SET_VRING_BASE:
1621 /* Moving base with an active backend?
1622 * You don't want to do that. */
1623 if (vq->private_data) {
1624 r = -EBUSY;
1625 break;
1626 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001627 if (copy_from_user(&s, argp, sizeof s)) {
1628 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001629 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001630 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001631 if (s.num > 0xffff) {
1632 r = -EINVAL;
1633 break;
1634 }
Jason Wang8d658432017-07-27 11:22:05 +08001635 vq->last_avail_idx = s.num;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001636 /* Forget the cached index value. */
1637 vq->avail_idx = vq->last_avail_idx;
1638 break;
1639 case VHOST_GET_VRING_BASE:
1640 s.index = idx;
1641 s.num = vq->last_avail_idx;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001642 if (copy_to_user(argp, &s, sizeof s))
1643 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001644 break;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001645 case VHOST_SET_VRING_KICK:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001646 if (copy_from_user(&f, argp, sizeof f)) {
1647 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001648 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001649 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001650 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +02001651 if (IS_ERR(eventfp)) {
1652 r = PTR_ERR(eventfp);
1653 break;
1654 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001655 if (eventfp != vq->kick) {
Al Virocecb46f2012-08-27 14:21:39 -04001656 pollstop = (filep = vq->kick) != NULL;
1657 pollstart = (vq->kick = eventfp) != NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001658 } else
1659 filep = eventfp;
1660 break;
1661 case VHOST_SET_VRING_CALL:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001662 if (copy_from_user(&f, argp, sizeof f)) {
1663 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001664 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001665 }
Eric Biggerse050c7d2018-01-06 14:52:19 -08001666 ctx = f.fd == -1 ? NULL : eventfd_ctx_fdget(f.fd);
1667 if (IS_ERR(ctx)) {
1668 r = PTR_ERR(ctx);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +02001669 break;
1670 }
Eric Biggerse050c7d2018-01-06 14:52:19 -08001671 swap(ctx, vq->call_ctx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001672 break;
1673 case VHOST_SET_VRING_ERR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001674 if (copy_from_user(&f, argp, sizeof f)) {
1675 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001676 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001677 }
Eric Biggers09f332a2018-01-06 14:52:20 -08001678 ctx = f.fd == -1 ? NULL : eventfd_ctx_fdget(f.fd);
1679 if (IS_ERR(ctx)) {
1680 r = PTR_ERR(ctx);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +02001681 break;
1682 }
Eric Biggers09f332a2018-01-06 14:52:20 -08001683 swap(ctx, vq->error_ctx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001684 break;
Greg Kurz2751c982015-04-24 14:27:24 +02001685 case VHOST_SET_VRING_ENDIAN:
1686 r = vhost_set_vring_endian(vq, argp);
1687 break;
1688 case VHOST_GET_VRING_ENDIAN:
1689 r = vhost_get_vring_endian(vq, idx, argp);
1690 break;
Jason Wang03088132016-03-04 06:24:53 -05001691 case VHOST_SET_VRING_BUSYLOOP_TIMEOUT:
1692 if (copy_from_user(&s, argp, sizeof(s))) {
1693 r = -EFAULT;
1694 break;
1695 }
1696 vq->busyloop_timeout = s.num;
1697 break;
1698 case VHOST_GET_VRING_BUSYLOOP_TIMEOUT:
1699 s.index = idx;
1700 s.num = vq->busyloop_timeout;
1701 if (copy_to_user(argp, &s, sizeof(s)))
1702 r = -EFAULT;
1703 break;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001704 default:
1705 r = -ENOIOCTLCMD;
1706 }
1707
1708 if (pollstop && vq->handle_kick)
1709 vhost_poll_stop(&vq->poll);
1710
Eric Biggerse050c7d2018-01-06 14:52:19 -08001711 if (!IS_ERR_OR_NULL(ctx))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001712 eventfd_ctx_put(ctx);
1713 if (filep)
1714 fput(filep);
1715
1716 if (pollstart && vq->handle_kick)
Jason Wang2b8b3282013-01-28 01:05:18 +00001717 r = vhost_poll_start(&vq->poll, vq->kick);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001718
1719 mutex_unlock(&vq->mutex);
1720
1721 if (pollstop && vq->handle_kick)
1722 vhost_poll_flush(&vq->poll);
1723 return r;
1724}
Asias He6ac1afb2013-05-06 16:38:21 +08001725EXPORT_SYMBOL_GPL(vhost_vring_ioctl);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001726
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001727int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled)
1728{
1729 struct vhost_umem *niotlb, *oiotlb;
1730 int i;
1731
1732 niotlb = vhost_umem_alloc();
1733 if (!niotlb)
1734 return -ENOMEM;
1735
1736 oiotlb = d->iotlb;
1737 d->iotlb = niotlb;
1738
1739 for (i = 0; i < d->nvqs; ++i) {
Jason Wangb13f9c62018-08-08 11:43:04 +08001740 struct vhost_virtqueue *vq = d->vqs[i];
1741
1742 mutex_lock(&vq->mutex);
1743 vq->iotlb = niotlb;
1744 __vhost_vq_meta_reset(vq);
1745 mutex_unlock(&vq->mutex);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001746 }
1747
1748 vhost_umem_clean(oiotlb);
1749
1750 return 0;
1751}
1752EXPORT_SYMBOL_GPL(vhost_init_device_iotlb);
1753
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001754/* Caller must have device mutex */
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001755long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001756{
Eric Biggersd25cc432018-01-06 14:52:21 -08001757 struct eventfd_ctx *ctx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001758 u64 p;
1759 long r;
1760 int i, fd;
1761
1762 /* If you are not the owner, you can become one */
1763 if (ioctl == VHOST_SET_OWNER) {
1764 r = vhost_dev_set_owner(d);
1765 goto done;
1766 }
1767
1768 /* You must be the owner to do anything else */
1769 r = vhost_dev_check_owner(d);
1770 if (r)
1771 goto done;
1772
1773 switch (ioctl) {
1774 case VHOST_SET_MEM_TABLE:
1775 r = vhost_set_memory(d, argp);
1776 break;
1777 case VHOST_SET_LOG_BASE:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001778 if (copy_from_user(&p, argp, sizeof p)) {
1779 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001780 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001781 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001782 if ((u64)(unsigned long)p != p) {
1783 r = -EFAULT;
1784 break;
1785 }
1786 for (i = 0; i < d->nvqs; ++i) {
1787 struct vhost_virtqueue *vq;
1788 void __user *base = (void __user *)(unsigned long)p;
Asias He3ab2e422013-04-27 11:16:48 +08001789 vq = d->vqs[i];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001790 mutex_lock(&vq->mutex);
1791 /* If ring is inactive, will check when it's enabled. */
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001792 if (vq->private_data && !vq_log_access_ok(vq, base))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001793 r = -EFAULT;
1794 else
1795 vq->log_base = base;
1796 mutex_unlock(&vq->mutex);
1797 }
1798 break;
1799 case VHOST_SET_LOG_FD:
1800 r = get_user(fd, (int __user *)argp);
1801 if (r < 0)
1802 break;
Eric Biggersd25cc432018-01-06 14:52:21 -08001803 ctx = fd == -1 ? NULL : eventfd_ctx_fdget(fd);
1804 if (IS_ERR(ctx)) {
1805 r = PTR_ERR(ctx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001806 break;
1807 }
Eric Biggersd25cc432018-01-06 14:52:21 -08001808 swap(ctx, d->log_ctx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001809 for (i = 0; i < d->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +08001810 mutex_lock(&d->vqs[i]->mutex);
1811 d->vqs[i]->log_ctx = d->log_ctx;
1812 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001813 }
1814 if (ctx)
1815 eventfd_ctx_put(ctx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001816 break;
1817 default:
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001818 r = -ENOIOCTLCMD;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001819 break;
1820 }
1821done:
1822 return r;
1823}
Asias He6ac1afb2013-05-06 16:38:21 +08001824EXPORT_SYMBOL_GPL(vhost_dev_ioctl);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001825
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001826/* TODO: This is really inefficient. We need something like get_user()
1827 * (instruction directly accesses the data, with an exception table entry
Mauro Carvalho Chehabcb1aaeb2019-06-07 15:54:32 -03001828 * returning -EFAULT). See Documentation/x86/exception-tables.rst.
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001829 */
1830static int set_bit_to_user(int nr, void __user *addr)
1831{
1832 unsigned long log = (unsigned long)addr;
1833 struct page *page;
1834 void *base;
1835 int bit = nr + (log % PAGE_SIZE) * 8;
1836 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301837
Ira Weiny73b01402019-05-13 17:17:11 -07001838 r = get_user_pages_fast(log, 1, FOLL_WRITE, &page);
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +02001839 if (r < 0)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001840 return r;
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +02001841 BUG_ON(r != 1);
Cong Wangc6daa7f2011-11-25 23:14:26 +08001842 base = kmap_atomic(page);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001843 set_bit(bit, base);
Cong Wangc6daa7f2011-11-25 23:14:26 +08001844 kunmap_atomic(base);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001845 set_page_dirty_lock(page);
1846 put_page(page);
1847 return 0;
1848}
1849
1850static int log_write(void __user *log_base,
1851 u64 write_address, u64 write_length)
1852{
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001853 u64 write_page = write_address / VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001854 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301855
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001856 if (!write_length)
1857 return 0;
Michael S. Tsirkin3bf9be42010-11-29 10:19:07 +02001858 write_length += write_address % VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001859 for (;;) {
1860 u64 base = (u64)(unsigned long)log_base;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001861 u64 log = base + write_page / 8;
1862 int bit = write_page % 8;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001863 if ((u64)(unsigned long)log != log)
1864 return -EFAULT;
1865 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
1866 if (r < 0)
1867 return r;
1868 if (write_length <= VHOST_PAGE_SIZE)
1869 break;
1870 write_length -= VHOST_PAGE_SIZE;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001871 write_page += 1;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001872 }
1873 return r;
1874}
1875
Jason Wangcc5e7102019-01-16 16:54:42 +08001876static int log_write_hva(struct vhost_virtqueue *vq, u64 hva, u64 len)
1877{
1878 struct vhost_umem *umem = vq->umem;
1879 struct vhost_umem_node *u;
1880 u64 start, end, l, min;
1881 int r;
1882 bool hit = false;
1883
1884 while (len) {
1885 min = len;
1886 /* More than one GPAs can be mapped into a single HVA. So
1887 * iterate all possible umems here to be safe.
1888 */
1889 list_for_each_entry(u, &umem->umem_list, link) {
1890 if (u->userspace_addr > hva - 1 + len ||
1891 u->userspace_addr - 1 + u->size < hva)
1892 continue;
1893 start = max(u->userspace_addr, hva);
1894 end = min(u->userspace_addr - 1 + u->size,
1895 hva - 1 + len);
1896 l = end - start + 1;
1897 r = log_write(vq->log_base,
1898 u->start + start - u->userspace_addr,
1899 l);
1900 if (r < 0)
1901 return r;
1902 hit = true;
1903 min = min(l, min);
1904 }
1905
1906 if (!hit)
1907 return -EFAULT;
1908
1909 len -= min;
1910 hva += min;
1911 }
1912
1913 return 0;
1914}
1915
1916static int log_used(struct vhost_virtqueue *vq, u64 used_offset, u64 len)
1917{
1918 struct iovec iov[64];
1919 int i, ret;
1920
1921 if (!vq->iotlb)
1922 return log_write(vq->log_base, vq->log_addr + used_offset, len);
1923
1924 ret = translate_desc(vq, (uintptr_t)vq->used + used_offset,
1925 len, iov, 64, VHOST_ACCESS_WO);
Jason Wang816db762019-02-19 14:53:44 +08001926 if (ret < 0)
Jason Wangcc5e7102019-01-16 16:54:42 +08001927 return ret;
1928
1929 for (i = 0; i < ret; i++) {
1930 ret = log_write_hva(vq, (uintptr_t)iov[i].iov_base,
1931 iov[i].iov_len);
1932 if (ret)
1933 return ret;
1934 }
1935
1936 return 0;
1937}
1938
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001939int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
Jason Wangcc5e7102019-01-16 16:54:42 +08001940 unsigned int log_num, u64 len, struct iovec *iov, int count)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001941{
1942 int i, r;
1943
1944 /* Make sure data written is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001945 smp_wmb();
Jason Wangcc5e7102019-01-16 16:54:42 +08001946
1947 if (vq->iotlb) {
1948 for (i = 0; i < count; i++) {
1949 r = log_write_hva(vq, (uintptr_t)iov[i].iov_base,
1950 iov[i].iov_len);
1951 if (r < 0)
1952 return r;
1953 }
1954 return 0;
1955 }
1956
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001957 for (i = 0; i < log_num; ++i) {
1958 u64 l = min(log[i].len, len);
1959 r = log_write(vq->log_base, log[i].addr, l);
1960 if (r < 0)
1961 return r;
1962 len -= l;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +02001963 if (!len) {
1964 if (vq->log_ctx)
1965 eventfd_signal(vq->log_ctx, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001966 return 0;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +02001967 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001968 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001969 /* Length written exceeds what we have stored. This is a bug. */
1970 BUG();
1971 return 0;
1972}
Asias He6ac1afb2013-05-06 16:38:21 +08001973EXPORT_SYMBOL_GPL(vhost_log_write);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001974
Jason Wang2723fea2011-06-21 18:04:38 +08001975static int vhost_update_used_flags(struct vhost_virtqueue *vq)
1976{
1977 void __user *used;
Jason Wang7b5d7532019-05-24 04:12:14 -04001978 if (vhost_put_used_flags(vq))
Jason Wang2723fea2011-06-21 18:04:38 +08001979 return -EFAULT;
1980 if (unlikely(vq->log_used)) {
1981 /* Make sure the flag is seen before log. */
1982 smp_wmb();
1983 /* Log used flag write. */
1984 used = &vq->used->flags;
Jason Wangcc5e7102019-01-16 16:54:42 +08001985 log_used(vq, (used - (void __user *)vq->used),
1986 sizeof vq->used->flags);
Jason Wang2723fea2011-06-21 18:04:38 +08001987 if (vq->log_ctx)
1988 eventfd_signal(vq->log_ctx, 1);
1989 }
1990 return 0;
1991}
1992
1993static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
1994{
Jason Wang7b5d7532019-05-24 04:12:14 -04001995 if (vhost_put_avail_event(vq))
Jason Wang2723fea2011-06-21 18:04:38 +08001996 return -EFAULT;
1997 if (unlikely(vq->log_used)) {
1998 void __user *used;
1999 /* Make sure the event is seen before log. */
2000 smp_wmb();
2001 /* Log avail event write */
2002 used = vhost_avail_event(vq);
Jason Wangcc5e7102019-01-16 16:54:42 +08002003 log_used(vq, (used - (void __user *)vq->used),
2004 sizeof *vhost_avail_event(vq));
Jason Wang2723fea2011-06-21 18:04:38 +08002005 if (vq->log_ctx)
2006 eventfd_signal(vq->log_ctx, 1);
2007 }
2008 return 0;
2009}
2010
Greg Kurz80f7d032016-02-16 15:59:44 +01002011int vhost_vq_init_access(struct vhost_virtqueue *vq)
Jason Wang2723fea2011-06-21 18:04:38 +08002012{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002013 __virtio16 last_used_idx;
Jason Wang2723fea2011-06-21 18:04:38 +08002014 int r;
Greg Kurze1f33be2016-02-16 15:54:28 +01002015 bool is_le = vq->is_le;
2016
Halil Pasiccda8bba2017-01-30 11:09:36 +01002017 if (!vq->private_data)
Jason Wang2723fea2011-06-21 18:04:38 +08002018 return 0;
Greg Kurz2751c982015-04-24 14:27:24 +02002019
2020 vhost_init_is_le(vq);
Jason Wang2723fea2011-06-21 18:04:38 +08002021
2022 r = vhost_update_used_flags(vq);
2023 if (r)
Greg Kurze1f33be2016-02-16 15:54:28 +01002024 goto err;
Jason Wang2723fea2011-06-21 18:04:38 +08002025 vq->signalled_used_valid = false;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002026 if (!vq->iotlb &&
Linus Torvalds96d4f262019-01-03 18:57:57 -08002027 !access_ok(&vq->used->idx, sizeof vq->used->idx)) {
Greg Kurze1f33be2016-02-16 15:54:28 +01002028 r = -EFAULT;
2029 goto err;
2030 }
Jason Wang7b5d7532019-05-24 04:12:14 -04002031 r = vhost_get_used_idx(vq, &last_used_idx);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002032 if (r) {
2033 vq_err(vq, "Can't access used idx at %p\n",
2034 &vq->used->idx);
Greg Kurze1f33be2016-02-16 15:54:28 +01002035 goto err;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002036 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002037 vq->last_used_idx = vhost16_to_cpu(vq, last_used_idx);
Michael S. Tsirkin64f7f052014-12-01 17:39:39 +02002038 return 0;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002039
Greg Kurze1f33be2016-02-16 15:54:28 +01002040err:
2041 vq->is_le = is_le;
2042 return r;
Jason Wang2723fea2011-06-21 18:04:38 +08002043}
Greg Kurz80f7d032016-02-16 15:59:44 +01002044EXPORT_SYMBOL_GPL(vhost_vq_init_access);
Jason Wang2723fea2011-06-21 18:04:38 +08002045
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03002046static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002047 struct iovec iov[], int iov_size, int access)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002048{
Jason Wanga9709d62016-06-23 02:04:31 -04002049 const struct vhost_umem_node *node;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002050 struct vhost_dev *dev = vq->dev;
2051 struct vhost_umem *umem = dev->iotlb ? dev->iotlb : dev->umem;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002052 struct iovec *_iov;
2053 u64 s = 0;
2054 int ret = 0;
2055
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002056 while ((u64)len > s) {
2057 u64 size;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002058 if (unlikely(ret >= iov_size)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002059 ret = -ENOBUFS;
2060 break;
2061 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002062
Jason Wanga9709d62016-06-23 02:04:31 -04002063 node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
2064 addr, addr + len - 1);
2065 if (node == NULL || node->start > addr) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002066 if (umem != dev->iotlb) {
2067 ret = -EFAULT;
2068 break;
2069 }
2070 ret = -EAGAIN;
2071 break;
2072 } else if (!(node->perm & access)) {
2073 ret = -EPERM;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002074 break;
2075 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002076
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002077 _iov = iov + ret;
Jason Wanga9709d62016-06-23 02:04:31 -04002078 size = node->size - addr + node->start;
Michael S. Tsirkinbd971202012-11-26 05:57:27 +00002079 _iov->iov_len = min((u64)len - s, size);
Michael S. Tsirkin0d4a3f22019-09-14 15:21:51 -04002080 _iov->iov_base = (void __user *)(unsigned long)
2081 (node->userspace_addr + addr - node->start);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002082 s += size;
2083 addr += size;
2084 ++ret;
2085 }
2086
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002087 if (ret == -EAGAIN)
2088 vhost_iotlb_miss(vq, addr, access);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002089 return ret;
2090}
2091
2092/* Each buffer in the virtqueues is actually a chain of descriptors. This
2093 * function returns the next descriptor in the chain,
2094 * or -1U if we're at the end. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002095static unsigned next_desc(struct vhost_virtqueue *vq, struct vring_desc *desc)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002096{
2097 unsigned int next;
2098
2099 /* If this descriptor says it doesn't chain, we're done. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002100 if (!(desc->flags & cpu_to_vhost16(vq, VRING_DESC_F_NEXT)))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002101 return -1U;
2102
2103 /* Check they're not leading us off end of descriptors. */
Paul E. McKenney3a5db0b2017-11-27 09:45:10 -08002104 next = vhost16_to_cpu(vq, READ_ONCE(desc->next));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002105 return next;
2106}
2107
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03002108static int get_indirect(struct vhost_virtqueue *vq,
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002109 struct iovec iov[], unsigned int iov_size,
2110 unsigned int *out_num, unsigned int *in_num,
2111 struct vhost_log *log, unsigned int *log_num,
2112 struct vring_desc *indirect)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002113{
2114 struct vring_desc desc;
2115 unsigned int i = 0, count, found = 0;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002116 u32 len = vhost32_to_cpu(vq, indirect->len);
Al Viroaad9a1c2014-12-10 14:49:01 -05002117 struct iov_iter from;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002118 int ret, access;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002119
2120 /* Sanity check */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002121 if (unlikely(len % sizeof desc)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002122 vq_err(vq, "Invalid length in indirect descriptor: "
2123 "len 0x%llx not multiple of 0x%zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002124 (unsigned long long)len,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002125 sizeof desc);
2126 return -EINVAL;
2127 }
2128
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002129 ret = translate_desc(vq, vhost64_to_cpu(vq, indirect->addr), len, vq->indirect,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002130 UIO_MAXIOV, VHOST_ACCESS_RO);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002131 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002132 if (ret != -EAGAIN)
2133 vq_err(vq, "Translation failure %d in indirect.\n", ret);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002134 return ret;
2135 }
Al Viroaad9a1c2014-12-10 14:49:01 -05002136 iov_iter_init(&from, READ, vq->indirect, ret, len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002137
2138 /* We will use the result as an address to read from, so most
2139 * architectures only need a compiler barrier here. */
2140 read_barrier_depends();
2141
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002142 count = len / sizeof desc;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002143 /* Buffers are chained via a 16 bit next field, so
2144 * we can have at most 2^16 of these. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002145 if (unlikely(count > USHRT_MAX + 1)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002146 vq_err(vq, "Indirect buffer length too big: %d\n",
2147 indirect->len);
2148 return -E2BIG;
2149 }
2150
2151 do {
2152 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002153 if (unlikely(++found > count)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002154 vq_err(vq, "Loop detected: last one at %u "
2155 "indirect size %u\n",
2156 i, count);
2157 return -EINVAL;
2158 }
Al Virocbbd26b2016-11-01 22:09:04 -04002159 if (unlikely(!copy_from_iter_full(&desc, sizeof(desc), &from))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002160 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002161 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002162 return -EINVAL;
2163 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002164 if (unlikely(desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002165 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002166 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002167 return -EINVAL;
2168 }
2169
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002170 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE))
2171 access = VHOST_ACCESS_WO;
2172 else
2173 access = VHOST_ACCESS_RO;
2174
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002175 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
2176 vhost32_to_cpu(vq, desc.len), iov + iov_count,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002177 iov_size - iov_count, access);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002178 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002179 if (ret != -EAGAIN)
2180 vq_err(vq, "Translation failure %d indirect idx %d\n",
2181 ret, i);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002182 return ret;
2183 }
2184 /* If this is an input descriptor, increment that count. */
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002185 if (access == VHOST_ACCESS_WO) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002186 *in_num += ret;
yongduan060423b2019-09-11 17:44:24 +08002187 if (unlikely(log && ret)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002188 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
2189 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002190 ++*log_num;
2191 }
2192 } else {
2193 /* If it's an output descriptor, they're all supposed
2194 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002195 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002196 vq_err(vq, "Indirect descriptor "
2197 "has out after in: idx %d\n", i);
2198 return -EINVAL;
2199 }
2200 *out_num += ret;
2201 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002202 } while ((i = next_desc(vq, &desc)) != -1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002203 return 0;
2204}
2205
2206/* This looks in the virtqueue and for the first available buffer, and converts
2207 * it to an iovec for convenient access. Since descriptors consist of some
2208 * number of output then some number of input descriptors, it's actually two
2209 * iovecs, but we pack them into one and note how many of each there were.
2210 *
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002211 * This function returns the descriptor number found, or vq->num (which is
2212 * never a valid descriptor number) if none was found. A negative code is
2213 * returned on error. */
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03002214int vhost_get_vq_desc(struct vhost_virtqueue *vq,
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002215 struct iovec iov[], unsigned int iov_size,
2216 unsigned int *out_num, unsigned int *in_num,
2217 struct vhost_log *log, unsigned int *log_num)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002218{
2219 struct vring_desc desc;
2220 unsigned int i, head, found = 0;
2221 u16 last_avail_idx;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002222 __virtio16 avail_idx;
2223 __virtio16 ring_head;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002224 int ret, access;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002225
2226 /* Check it isn't doing very strange things with descriptor numbers. */
2227 last_avail_idx = vq->last_avail_idx;
Jason Wange3b56cd2017-02-07 15:49:50 +08002228
2229 if (vq->avail_idx == vq->last_avail_idx) {
Jason Wang7b5d7532019-05-24 04:12:14 -04002230 if (unlikely(vhost_get_avail_idx(vq, &avail_idx))) {
Jason Wange3b56cd2017-02-07 15:49:50 +08002231 vq_err(vq, "Failed to access avail idx at %p\n",
2232 &vq->avail->idx);
2233 return -EFAULT;
2234 }
2235 vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
2236
2237 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
2238 vq_err(vq, "Guest moved used index from %u to %u",
2239 last_avail_idx, vq->avail_idx);
2240 return -EFAULT;
2241 }
2242
2243 /* If there's nothing new since last we looked, return
2244 * invalid.
2245 */
2246 if (vq->avail_idx == last_avail_idx)
2247 return vq->num;
2248
2249 /* Only get avail ring entries after they have been
2250 * exposed by guest.
2251 */
2252 smp_rmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002253 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002254
2255 /* Grab the next descriptor number they're advertising, and increment
2256 * the index we've seen. */
Jason Wang7b5d7532019-05-24 04:12:14 -04002257 if (unlikely(vhost_get_avail_head(vq, &ring_head, last_avail_idx))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002258 vq_err(vq, "Failed to read head: idx %d address %p\n",
2259 last_avail_idx,
2260 &vq->avail->ring[last_avail_idx % vq->num]);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002261 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002262 }
2263
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002264 head = vhost16_to_cpu(vq, ring_head);
2265
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002266 /* If their number is silly, that's an error. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002267 if (unlikely(head >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002268 vq_err(vq, "Guest says index %u > %u is available",
2269 head, vq->num);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002270 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002271 }
2272
2273 /* When we start there are none of either input nor output. */
2274 *out_num = *in_num = 0;
2275 if (unlikely(log))
2276 *log_num = 0;
2277
2278 i = head;
2279 do {
2280 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002281 if (unlikely(i >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002282 vq_err(vq, "Desc index is %u > %u, head = %u",
2283 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002284 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002285 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002286 if (unlikely(++found > vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002287 vq_err(vq, "Loop detected: last one at %u "
2288 "vq size %u head %u\n",
2289 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002290 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002291 }
Jason Wang7b5d7532019-05-24 04:12:14 -04002292 ret = vhost_get_desc(vq, &desc, i);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002293 if (unlikely(ret)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002294 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
2295 i, vq->desc + i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002296 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002297 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002298 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT)) {
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03002299 ret = get_indirect(vq, iov, iov_size,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002300 out_num, in_num,
2301 log, log_num, &desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002302 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002303 if (ret != -EAGAIN)
2304 vq_err(vq, "Failure detected "
2305 "in indirect descriptor at idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002306 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002307 }
2308 continue;
2309 }
2310
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002311 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE))
2312 access = VHOST_ACCESS_WO;
2313 else
2314 access = VHOST_ACCESS_RO;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002315 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
2316 vhost32_to_cpu(vq, desc.len), iov + iov_count,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002317 iov_size - iov_count, access);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002318 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002319 if (ret != -EAGAIN)
2320 vq_err(vq, "Translation failure %d descriptor idx %d\n",
2321 ret, i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002322 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002323 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002324 if (access == VHOST_ACCESS_WO) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002325 /* If this is an input descriptor,
2326 * increment that count. */
2327 *in_num += ret;
yongduan060423b2019-09-11 17:44:24 +08002328 if (unlikely(log && ret)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002329 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
2330 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002331 ++*log_num;
2332 }
2333 } else {
2334 /* If it's an output descriptor, they're all supposed
2335 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002336 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002337 vq_err(vq, "Descriptor has out after in: "
2338 "idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002339 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002340 }
2341 *out_num += ret;
2342 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002343 } while ((i = next_desc(vq, &desc)) != -1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002344
2345 /* On success, increment avail index. */
2346 vq->last_avail_idx++;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002347
2348 /* Assume notifications from guest are disabled at this point,
2349 * if they aren't we would need to update avail_event index. */
2350 BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002351 return head;
2352}
Asias He6ac1afb2013-05-06 16:38:21 +08002353EXPORT_SYMBOL_GPL(vhost_get_vq_desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002354
2355/* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
David Stevens8dd014a2010-07-27 18:52:21 +03002356void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002357{
David Stevens8dd014a2010-07-27 18:52:21 +03002358 vq->last_avail_idx -= n;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002359}
Asias He6ac1afb2013-05-06 16:38:21 +08002360EXPORT_SYMBOL_GPL(vhost_discard_vq_desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002361
2362/* After we've used one of their buffers, we tell them about it. We'll then
2363 * want to notify the guest, using eventfd. */
2364int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
2365{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002366 struct vring_used_elem heads = {
2367 cpu_to_vhost32(vq, head),
2368 cpu_to_vhost32(vq, len)
2369 };
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002370
Jason Wangc49e4e52013-09-02 16:40:58 +08002371 return vhost_add_used_n(vq, &heads, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002372}
Asias He6ac1afb2013-05-06 16:38:21 +08002373EXPORT_SYMBOL_GPL(vhost_add_used);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002374
David Stevens8dd014a2010-07-27 18:52:21 +03002375static int __vhost_add_used_n(struct vhost_virtqueue *vq,
2376 struct vring_used_elem *heads,
2377 unsigned count)
2378{
2379 struct vring_used_elem __user *used;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002380 u16 old, new;
David Stevens8dd014a2010-07-27 18:52:21 +03002381 int start;
2382
Michael S. Tsirkin5fba13b2015-11-29 13:34:44 +02002383 start = vq->last_used_idx & (vq->num - 1);
David Stevens8dd014a2010-07-27 18:52:21 +03002384 used = vq->used->ring + start;
Jason Wang7b5d7532019-05-24 04:12:14 -04002385 if (vhost_put_used(vq, heads, start, count)) {
David Stevens8dd014a2010-07-27 18:52:21 +03002386 vq_err(vq, "Failed to write used");
2387 return -EFAULT;
2388 }
2389 if (unlikely(vq->log_used)) {
2390 /* Make sure data is seen before log. */
2391 smp_wmb();
2392 /* Log used ring entry write. */
Jason Wangcc5e7102019-01-16 16:54:42 +08002393 log_used(vq, ((void __user *)used - (void __user *)vq->used),
2394 count * sizeof *used);
David Stevens8dd014a2010-07-27 18:52:21 +03002395 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002396 old = vq->last_used_idx;
2397 new = (vq->last_used_idx += count);
2398 /* If the driver never bothers to signal in a very long while,
2399 * used index might wrap around. If that happens, invalidate
2400 * signalled_used index we stored. TODO: make sure driver
2401 * signals at least once in 2^16 and remove this. */
2402 if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old)))
2403 vq->signalled_used_valid = false;
David Stevens8dd014a2010-07-27 18:52:21 +03002404 return 0;
2405}
2406
2407/* After we've used one of their buffers, we tell them about it. We'll then
2408 * want to notify the guest, using eventfd. */
2409int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
2410 unsigned count)
2411{
2412 int start, n, r;
2413
Michael S. Tsirkin5fba13b2015-11-29 13:34:44 +02002414 start = vq->last_used_idx & (vq->num - 1);
David Stevens8dd014a2010-07-27 18:52:21 +03002415 n = vq->num - start;
2416 if (n < count) {
2417 r = __vhost_add_used_n(vq, heads, n);
2418 if (r < 0)
2419 return r;
2420 heads += n;
2421 count -= n;
2422 }
2423 r = __vhost_add_used_n(vq, heads, count);
2424
2425 /* Make sure buffer is written before we update index. */
2426 smp_wmb();
Jason Wang7b5d7532019-05-24 04:12:14 -04002427 if (vhost_put_used_idx(vq)) {
David Stevens8dd014a2010-07-27 18:52:21 +03002428 vq_err(vq, "Failed to increment used idx");
2429 return -EFAULT;
2430 }
2431 if (unlikely(vq->log_used)) {
Jason Wang841df922018-12-13 10:53:37 +08002432 /* Make sure used idx is seen before log. */
2433 smp_wmb();
David Stevens8dd014a2010-07-27 18:52:21 +03002434 /* Log used index update. */
Jason Wangcc5e7102019-01-16 16:54:42 +08002435 log_used(vq, offsetof(struct vring_used, idx),
2436 sizeof vq->used->idx);
David Stevens8dd014a2010-07-27 18:52:21 +03002437 if (vq->log_ctx)
2438 eventfd_signal(vq->log_ctx, 1);
2439 }
2440 return r;
2441}
Asias He6ac1afb2013-05-06 16:38:21 +08002442EXPORT_SYMBOL_GPL(vhost_add_used_n);
David Stevens8dd014a2010-07-27 18:52:21 +03002443
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002444static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002445{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002446 __u16 old, new;
2447 __virtio16 event;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002448 bool v;
Jason Wang8d658432017-07-27 11:22:05 +08002449 /* Flush out used index updates. This is paired
2450 * with the barrier that the Guest executes when enabling
2451 * interrupts. */
2452 smp_mb();
Michael S. Tsirkin0d499352010-05-11 19:44:17 +03002453
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002454 if (vhost_has_feature(vq, VIRTIO_F_NOTIFY_ON_EMPTY) &&
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002455 unlikely(vq->avail_idx == vq->last_avail_idx))
2456 return true;
2457
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002458 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002459 __virtio16 flags;
Jason Wang7b5d7532019-05-24 04:12:14 -04002460 if (vhost_get_avail_flags(vq, &flags)) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002461 vq_err(vq, "Failed to get flags");
2462 return true;
2463 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002464 return !(flags & cpu_to_vhost16(vq, VRING_AVAIL_F_NO_INTERRUPT));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002465 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002466 old = vq->signalled_used;
2467 v = vq->signalled_used_valid;
2468 new = vq->signalled_used = vq->last_used_idx;
2469 vq->signalled_used_valid = true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002470
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002471 if (unlikely(!v))
2472 return true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002473
Jason Wang7b5d7532019-05-24 04:12:14 -04002474 if (vhost_get_used_event(vq, &event)) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002475 vq_err(vq, "Failed to get used event idx");
2476 return true;
2477 }
Jason Wang8d658432017-07-27 11:22:05 +08002478 return vring_need_event(vhost16_to_cpu(vq, event), new, old);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002479}
2480
2481/* This actually signals the guest, using eventfd. */
2482void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
2483{
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002484 /* Signal the Guest tell them we used something up. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002485 if (vq->call_ctx && vhost_notify(dev, vq))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002486 eventfd_signal(vq->call_ctx, 1);
2487}
Asias He6ac1afb2013-05-06 16:38:21 +08002488EXPORT_SYMBOL_GPL(vhost_signal);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002489
2490/* And here's the combo meal deal. Supersize me! */
2491void vhost_add_used_and_signal(struct vhost_dev *dev,
2492 struct vhost_virtqueue *vq,
2493 unsigned int head, int len)
2494{
2495 vhost_add_used(vq, head, len);
2496 vhost_signal(dev, vq);
2497}
Asias He6ac1afb2013-05-06 16:38:21 +08002498EXPORT_SYMBOL_GPL(vhost_add_used_and_signal);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002499
David Stevens8dd014a2010-07-27 18:52:21 +03002500/* multi-buffer version of vhost_add_used_and_signal */
2501void vhost_add_used_and_signal_n(struct vhost_dev *dev,
2502 struct vhost_virtqueue *vq,
2503 struct vring_used_elem *heads, unsigned count)
2504{
2505 vhost_add_used_n(vq, heads, count);
2506 vhost_signal(dev, vq);
2507}
Asias He6ac1afb2013-05-06 16:38:21 +08002508EXPORT_SYMBOL_GPL(vhost_add_used_and_signal_n);
David Stevens8dd014a2010-07-27 18:52:21 +03002509
Jason Wangd4a60602016-03-04 06:24:52 -05002510/* return true if we're sure that avaiable ring is empty */
2511bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq)
2512{
2513 __virtio16 avail_idx;
2514 int r;
2515
Jason Wang275bf962017-01-18 15:02:01 +08002516 if (vq->avail_idx != vq->last_avail_idx)
Jason Wangd4a60602016-03-04 06:24:52 -05002517 return false;
2518
Jason Wang7b5d7532019-05-24 04:12:14 -04002519 r = vhost_get_avail_idx(vq, &avail_idx);
Jason Wang275bf962017-01-18 15:02:01 +08002520 if (unlikely(r))
2521 return false;
2522 vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
2523
2524 return vq->avail_idx == vq->last_avail_idx;
Jason Wangd4a60602016-03-04 06:24:52 -05002525}
2526EXPORT_SYMBOL_GPL(vhost_vq_avail_empty);
2527
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002528/* OK, now we need to know about added descriptors. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002529bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002530{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002531 __virtio16 avail_idx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002532 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05302533
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002534 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
2535 return false;
2536 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002537 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08002538 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002539 if (r) {
2540 vq_err(vq, "Failed to enable notification at %p: %d\n",
2541 &vq->used->flags, r);
2542 return false;
2543 }
2544 } else {
Jason Wang2723fea2011-06-21 18:04:38 +08002545 r = vhost_update_avail_event(vq, vq->avail_idx);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002546 if (r) {
2547 vq_err(vq, "Failed to update avail event index at %p: %d\n",
2548 vhost_avail_event(vq), r);
2549 return false;
2550 }
2551 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002552 /* They could have slipped one in as we were doing that: make
2553 * sure it's written, then check again. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00002554 smp_mb();
Jason Wang7b5d7532019-05-24 04:12:14 -04002555 r = vhost_get_avail_idx(vq, &avail_idx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002556 if (r) {
2557 vq_err(vq, "Failed to check avail idx at %p: %d\n",
2558 &vq->avail->idx, r);
2559 return false;
2560 }
2561
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002562 return vhost16_to_cpu(vq, avail_idx) != vq->avail_idx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002563}
Asias He6ac1afb2013-05-06 16:38:21 +08002564EXPORT_SYMBOL_GPL(vhost_enable_notify);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002565
2566/* We don't need to be notified again. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002567void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002568{
2569 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05302570
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002571 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
2572 return;
2573 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002574 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08002575 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002576 if (r)
2577 vq_err(vq, "Failed to enable notification at %p: %d\n",
2578 &vq->used->flags, r);
2579 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002580}
Asias He6ac1afb2013-05-06 16:38:21 +08002581EXPORT_SYMBOL_GPL(vhost_disable_notify);
2582
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002583/* Create a new message. */
2584struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type)
2585{
2586 struct vhost_msg_node *node = kmalloc(sizeof *node, GFP_KERNEL);
2587 if (!node)
2588 return NULL;
Michael S. Tsirkin670ae9c2018-05-12 00:33:10 +03002589
2590 /* Make sure all padding within the structure is initialized. */
2591 memset(&node->msg, 0, sizeof node->msg);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002592 node->vq = vq;
2593 node->msg.type = type;
2594 return node;
2595}
2596EXPORT_SYMBOL_GPL(vhost_new_msg);
2597
2598void vhost_enqueue_msg(struct vhost_dev *dev, struct list_head *head,
2599 struct vhost_msg_node *node)
2600{
2601 spin_lock(&dev->iotlb_lock);
2602 list_add_tail(&node->node, head);
2603 spin_unlock(&dev->iotlb_lock);
2604
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002605 wake_up_interruptible_poll(&dev->wait, EPOLLIN | EPOLLRDNORM);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002606}
2607EXPORT_SYMBOL_GPL(vhost_enqueue_msg);
2608
2609struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev,
2610 struct list_head *head)
2611{
2612 struct vhost_msg_node *node = NULL;
2613
2614 spin_lock(&dev->iotlb_lock);
2615 if (!list_empty(head)) {
2616 node = list_first_entry(head, struct vhost_msg_node,
2617 node);
2618 list_del(&node->node);
2619 }
2620 spin_unlock(&dev->iotlb_lock);
2621
2622 return node;
2623}
2624EXPORT_SYMBOL_GPL(vhost_dequeue_msg);
2625
2626
Asias He6ac1afb2013-05-06 16:38:21 +08002627static int __init vhost_init(void)
2628{
2629 return 0;
2630}
2631
2632static void __exit vhost_exit(void)
2633{
2634}
2635
2636module_init(vhost_init);
2637module_exit(vhost_exit);
2638
2639MODULE_VERSION("0.0.1");
2640MODULE_LICENSE("GPL v2");
2641MODULE_AUTHOR("Michael S. Tsirkin");
2642MODULE_DESCRIPTION("Host kernel accelerator for virtio");