blob: 80da9d9662f2611225b35bada6baed00da80b269 [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
Greg Kurz2751c982015-04-24 14:27:24 +020053#ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
Greg Kurzc5072032016-02-16 15:59:34 +010054static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
Greg Kurz2751c982015-04-24 14:27:24 +020055{
56 vq->user_be = !virtio_legacy_is_little_endian();
57}
58
Greg Kurzc5072032016-02-16 15:59:34 +010059static void vhost_enable_cross_endian_big(struct vhost_virtqueue *vq)
60{
61 vq->user_be = true;
62}
63
64static void vhost_enable_cross_endian_little(struct vhost_virtqueue *vq)
65{
66 vq->user_be = false;
67}
68
Greg Kurz2751c982015-04-24 14:27:24 +020069static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
70{
71 struct vhost_vring_state s;
72
73 if (vq->private_data)
74 return -EBUSY;
75
76 if (copy_from_user(&s, argp, sizeof(s)))
77 return -EFAULT;
78
79 if (s.num != VHOST_VRING_LITTLE_ENDIAN &&
80 s.num != VHOST_VRING_BIG_ENDIAN)
81 return -EINVAL;
82
Greg Kurzc5072032016-02-16 15:59:34 +010083 if (s.num == VHOST_VRING_BIG_ENDIAN)
84 vhost_enable_cross_endian_big(vq);
85 else
86 vhost_enable_cross_endian_little(vq);
Greg Kurz2751c982015-04-24 14:27:24 +020087
88 return 0;
89}
90
91static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
92 int __user *argp)
93{
94 struct vhost_vring_state s = {
95 .index = idx,
96 .num = vq->user_be
97 };
98
99 if (copy_to_user(argp, &s, sizeof(s)))
100 return -EFAULT;
101
102 return 0;
103}
104
105static void vhost_init_is_le(struct vhost_virtqueue *vq)
106{
107 /* Note for legacy virtio: user_be is initialized at reset time
108 * according to the host endianness. If userspace does not set an
109 * explicit endianness, the default behavior is native endian, as
110 * expected by legacy virtio.
111 */
112 vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) || !vq->user_be;
113}
114#else
Greg Kurzc5072032016-02-16 15:59:34 +0100115static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
Greg Kurz2751c982015-04-24 14:27:24 +0200116{
117}
118
119static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
120{
121 return -ENOIOCTLCMD;
122}
123
124static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
125 int __user *argp)
126{
127 return -ENOIOCTLCMD;
128}
129
130static void vhost_init_is_le(struct vhost_virtqueue *vq)
131{
Halil Pasiccda8bba2017-01-30 11:09:36 +0100132 vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1)
133 || virtio_legacy_is_little_endian();
Greg Kurz2751c982015-04-24 14:27:24 +0200134}
135#endif /* CONFIG_VHOST_CROSS_ENDIAN_LEGACY */
136
Greg Kurzc5072032016-02-16 15:59:34 +0100137static void vhost_reset_is_le(struct vhost_virtqueue *vq)
138{
Halil Pasiccda8bba2017-01-30 11:09:36 +0100139 vhost_init_is_le(vq);
Greg Kurzc5072032016-02-16 15:59:34 +0100140}
141
Jason Wang7235acd2016-04-25 22:14:32 -0400142struct vhost_flush_struct {
143 struct vhost_work work;
144 struct completion wait_event;
145};
146
147static void vhost_flush_work(struct vhost_work *work)
148{
149 struct vhost_flush_struct *s;
150
151 s = container_of(work, struct vhost_flush_struct, work);
152 complete(&s->wait_event);
153}
154
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000155static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
156 poll_table *pt)
157{
158 struct vhost_poll *poll;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000159
Krishna Kumard47effe2011-03-01 17:06:37 +0530160 poll = container_of(pt, struct vhost_poll, table);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000161 poll->wqh = wqh;
162 add_wait_queue(wqh, &poll->wait);
163}
164
Ingo Molnarac6424b2017-06-20 12:06:13 +0200165static int vhost_poll_wakeup(wait_queue_entry_t *wait, unsigned mode, int sync,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000166 void *key)
167{
Tejun Heoc23f34452010-06-02 20:40:00 +0200168 struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
Jason Wang01fcb1c2020-05-29 16:02:58 +0800169 struct vhost_work *work = &poll->work;
Tejun Heoc23f34452010-06-02 20:40:00 +0200170
Al Viro3ad6f932017-07-03 20:14:56 -0400171 if (!(key_to_poll(key) & poll->mask))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000172 return 0;
173
Jason Wang01fcb1c2020-05-29 16:02:58 +0800174 if (!poll->dev->use_worker)
175 work->fn(work);
176 else
177 vhost_poll_queue(poll);
178
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000179 return 0;
180}
181
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000182void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000183{
Jason Wang04b96e52016-04-25 22:14:33 -0400184 clear_bit(VHOST_WORK_QUEUED, &work->flags);
Tejun Heoc23f34452010-06-02 20:40:00 +0200185 work->fn = fn;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000186}
Asias He6ac1afb2013-05-06 16:38:21 +0800187EXPORT_SYMBOL_GPL(vhost_work_init);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000188
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300189/* Init poll structure */
190void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
Al Viro58e3b602017-07-03 23:50:40 -0400191 __poll_t mask, struct vhost_dev *dev)
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300192{
193 init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
194 init_poll_funcptr(&poll->table, vhost_poll_func);
195 poll->mask = mask;
196 poll->dev = dev;
Jason Wang2b8b3282013-01-28 01:05:18 +0000197 poll->wqh = NULL;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300198
199 vhost_work_init(&poll->work, fn);
200}
Asias He6ac1afb2013-05-06 16:38:21 +0800201EXPORT_SYMBOL_GPL(vhost_poll_init);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300202
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000203/* Start polling a file. We add ourselves to file's wait queue. The caller must
204 * keep a reference to a file until after vhost_poll_stop is called. */
Jason Wang2b8b3282013-01-28 01:05:18 +0000205int vhost_poll_start(struct vhost_poll *poll, struct file *file)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000206{
Al Viroe6c8adc2017-07-03 22:25:56 -0400207 __poll_t mask;
Krishna Kumard47effe2011-03-01 17:06:37 +0530208
Jason Wang70181d512013-04-10 20:50:48 +0000209 if (poll->wqh)
210 return 0;
211
Christoph Hellwig9965ed172018-03-05 07:26:05 -0800212 mask = vfs_poll(file, &poll->table);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000213 if (mask)
Al Viro3ad6f932017-07-03 20:14:56 -0400214 vhost_poll_wakeup(&poll->wait, 0, 0, poll_to_key(mask));
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800215 if (mask & EPOLLERR) {
Jason Wangdc6455a2018-03-27 20:50:52 +0800216 vhost_poll_stop(poll);
Yunsheng Lin896fc242019-08-20 20:36:32 +0800217 return -EINVAL;
Jason Wang2b8b3282013-01-28 01:05:18 +0000218 }
219
Yunsheng Lin896fc242019-08-20 20:36:32 +0800220 return 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000221}
Asias He6ac1afb2013-05-06 16:38:21 +0800222EXPORT_SYMBOL_GPL(vhost_poll_start);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000223
224/* Stop polling a file. After this function returns, it becomes safe to drop the
225 * file reference. You must also flush afterwards. */
226void vhost_poll_stop(struct vhost_poll *poll)
227{
Jason Wang2b8b3282013-01-28 01:05:18 +0000228 if (poll->wqh) {
229 remove_wait_queue(poll->wqh, &poll->wait);
230 poll->wqh = NULL;
231 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000232}
Asias He6ac1afb2013-05-06 16:38:21 +0800233EXPORT_SYMBOL_GPL(vhost_poll_stop);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000234
Asias He6ac1afb2013-05-06 16:38:21 +0800235void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000236{
Jason Wang7235acd2016-04-25 22:14:32 -0400237 struct vhost_flush_struct flush;
Tejun Heoc23f34452010-06-02 20:40:00 +0200238
Jason Wang7235acd2016-04-25 22:14:32 -0400239 if (dev->worker) {
240 init_completion(&flush.wait_event);
241 vhost_work_init(&flush.work, vhost_flush_work);
242
243 vhost_work_queue(dev, &flush.work);
244 wait_for_completion(&flush.wait_event);
245 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000246}
Asias He6ac1afb2013-05-06 16:38:21 +0800247EXPORT_SYMBOL_GPL(vhost_work_flush);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000248
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300249/* Flush any work that has been scheduled. When calling this, don't hold any
250 * locks that are also used by the callback. */
251void vhost_poll_flush(struct vhost_poll *poll)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000252{
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300253 vhost_work_flush(poll->dev, &poll->work);
254}
Asias He6ac1afb2013-05-06 16:38:21 +0800255EXPORT_SYMBOL_GPL(vhost_poll_flush);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300256
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000257void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300258{
Jason Wang04b96e52016-04-25 22:14:33 -0400259 if (!dev->worker)
260 return;
Tejun Heoc23f34452010-06-02 20:40:00 +0200261
Jason Wang04b96e52016-04-25 22:14:33 -0400262 if (!test_and_set_bit(VHOST_WORK_QUEUED, &work->flags)) {
263 /* We can only add the work to the list after we're
264 * sure it was not in the list.
Peng Tao635abf02016-12-07 17:52:19 +0800265 * test_and_set_bit() implies a memory barrier.
Jason Wang04b96e52016-04-25 22:14:33 -0400266 */
Jason Wang04b96e52016-04-25 22:14:33 -0400267 llist_add(&work->node, &dev->work_list);
Tejun Heoc23f34452010-06-02 20:40:00 +0200268 wake_up_process(dev->worker);
269 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000270}
Asias He6ac1afb2013-05-06 16:38:21 +0800271EXPORT_SYMBOL_GPL(vhost_work_queue);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000272
Jason Wang526d3e72016-03-04 06:24:51 -0500273/* A lockless hint for busy polling code to exit the loop */
274bool vhost_has_work(struct vhost_dev *dev)
275{
Jason Wang04b96e52016-04-25 22:14:33 -0400276 return !llist_empty(&dev->work_list);
Jason Wang526d3e72016-03-04 06:24:51 -0500277}
278EXPORT_SYMBOL_GPL(vhost_has_work);
279
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300280void vhost_poll_queue(struct vhost_poll *poll)
281{
282 vhost_work_queue(poll->dev, &poll->work);
283}
Asias He6ac1afb2013-05-06 16:38:21 +0800284EXPORT_SYMBOL_GPL(vhost_poll_queue);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300285
Jason Wangf8894912017-02-28 17:56:02 +0800286static void __vhost_vq_meta_reset(struct vhost_virtqueue *vq)
287{
288 int j;
289
290 for (j = 0; j < VHOST_NUM_ADDRS; j++)
291 vq->meta_iotlb[j] = NULL;
292}
293
294static void vhost_vq_meta_reset(struct vhost_dev *d)
295{
296 int i;
297
Jason Wang86a07da2018-12-13 10:53:39 +0800298 for (i = 0; i < d->nvqs; ++i)
Jason Wangf8894912017-02-28 17:56:02 +0800299 __vhost_vq_meta_reset(d->vqs[i]);
300}
301
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000302static void vhost_vq_reset(struct vhost_dev *dev,
303 struct vhost_virtqueue *vq)
304{
305 vq->num = 1;
306 vq->desc = NULL;
307 vq->avail = NULL;
308 vq->used = NULL;
309 vq->last_avail_idx = 0;
310 vq->avail_idx = 0;
311 vq->last_used_idx = 0;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300312 vq->signalled_used = 0;
313 vq->signalled_used_valid = false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000314 vq->used_flags = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000315 vq->log_used = false;
316 vq->log_addr = -1ull;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000317 vq->private_data = NULL;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300318 vq->acked_features = 0;
Jason Wang429711a2018-08-06 11:17:47 +0800319 vq->acked_backend_features = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000320 vq->log_base = NULL;
321 vq->error_ctx = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000322 vq->kick = NULL;
323 vq->call_ctx = NULL;
Michael S. Tsirkin73a99f02010-02-23 11:23:45 +0200324 vq->log_ctx = NULL;
Greg Kurzc5072032016-02-16 15:59:34 +0100325 vhost_reset_is_le(vq);
326 vhost_disable_cross_endian(vq);
Jason Wang03088132016-03-04 06:24:53 -0500327 vq->busyloop_timeout = 0;
Jason Wanga9709d62016-06-23 02:04:31 -0400328 vq->umem = NULL;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400329 vq->iotlb = NULL;
Jason Wangf8894912017-02-28 17:56:02 +0800330 __vhost_vq_meta_reset(vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000331}
332
Tejun Heoc23f34452010-06-02 20:40:00 +0200333static int vhost_worker(void *data)
334{
335 struct vhost_dev *dev = data;
Jason Wang04b96e52016-04-25 22:14:33 -0400336 struct vhost_work *work, *work_next;
337 struct llist_node *node;
Jens Freimannd7ffde32012-06-26 00:59:58 +0000338 mm_segment_t oldfs = get_fs();
Tejun Heoc23f34452010-06-02 20:40:00 +0200339
Jens Freimannd7ffde32012-06-26 00:59:58 +0000340 set_fs(USER_DS);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200341 use_mm(dev->mm);
342
Tejun Heoc23f34452010-06-02 20:40:00 +0200343 for (;;) {
344 /* mb paired w/ kthread_stop */
345 set_current_state(TASK_INTERRUPTIBLE);
346
Tejun Heoc23f34452010-06-02 20:40:00 +0200347 if (kthread_should_stop()) {
Tejun Heoc23f34452010-06-02 20:40:00 +0200348 __set_current_state(TASK_RUNNING);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200349 break;
Tejun Heoc23f34452010-06-02 20:40:00 +0200350 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200351
Jason Wang04b96e52016-04-25 22:14:33 -0400352 node = llist_del_all(&dev->work_list);
353 if (!node)
354 schedule();
355
356 node = llist_reverse_order(node);
357 /* make sure flag is seen after deletion */
358 smp_wmb();
359 llist_for_each_entry_safe(work, work_next, node, node) {
360 clear_bit(VHOST_WORK_QUEUED, &work->flags);
Tejun Heoc23f34452010-06-02 20:40:00 +0200361 __set_current_state(TASK_RUNNING);
Andrey Konovalov8f6a7f92019-12-04 16:52:50 -0800362 kcov_remote_start_common(dev->kcov_handle);
Tejun Heoc23f34452010-06-02 20:40:00 +0200363 work->fn(work);
Andrey Konovalov8f6a7f92019-12-04 16:52:50 -0800364 kcov_remote_stop();
Nadav Har'Eld550dda2012-02-27 15:07:29 +0200365 if (need_resched())
366 schedule();
Jason Wang04b96e52016-04-25 22:14:33 -0400367 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200368 }
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200369 unuse_mm(dev->mm);
Jens Freimannd7ffde32012-06-26 00:59:58 +0000370 set_fs(oldfs);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200371 return 0;
Tejun Heoc23f34452010-06-02 20:40:00 +0200372}
373
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000374static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
375{
376 kfree(vq->indirect);
377 vq->indirect = NULL;
378 kfree(vq->log);
379 vq->log = NULL;
380 kfree(vq->heads);
381 vq->heads = NULL;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000382}
383
Jason Wange0e9b402010-09-14 23:53:05 +0800384/* Helper to allocate iovec buffers for all vqs. */
385static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
386{
Asias He6d5e6aa2013-05-06 16:38:23 +0800387 struct vhost_virtqueue *vq;
Jason Wange0e9b402010-09-14 23:53:05 +0800388 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530389
Jason Wange0e9b402010-09-14 23:53:05 +0800390 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800391 vq = dev->vqs[i];
Kees Cook6da2ec52018-06-12 13:55:00 -0700392 vq->indirect = kmalloc_array(UIO_MAXIOV,
393 sizeof(*vq->indirect),
394 GFP_KERNEL);
Jason Wangb46a0bf2019-01-28 15:05:05 +0800395 vq->log = kmalloc_array(dev->iov_limit, sizeof(*vq->log),
Kees Cook6da2ec52018-06-12 13:55:00 -0700396 GFP_KERNEL);
Jason Wangb46a0bf2019-01-28 15:05:05 +0800397 vq->heads = kmalloc_array(dev->iov_limit, sizeof(*vq->heads),
Kees Cook6da2ec52018-06-12 13:55:00 -0700398 GFP_KERNEL);
Asias He6d5e6aa2013-05-06 16:38:23 +0800399 if (!vq->indirect || !vq->log || !vq->heads)
Jason Wange0e9b402010-09-14 23:53:05 +0800400 goto err_nomem;
401 }
402 return 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530403
Jason Wange0e9b402010-09-14 23:53:05 +0800404err_nomem:
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000405 for (; i >= 0; --i)
Asias He3ab2e422013-04-27 11:16:48 +0800406 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800407 return -ENOMEM;
408}
409
410static void vhost_dev_free_iovecs(struct vhost_dev *dev)
411{
412 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530413
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000414 for (i = 0; i < dev->nvqs; ++i)
Asias He3ab2e422013-04-27 11:16:48 +0800415 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800416}
417
Jason Wange82b9b02019-05-17 00:29:49 -0400418bool vhost_exceeds_weight(struct vhost_virtqueue *vq,
419 int pkts, int total_len)
420{
421 struct vhost_dev *dev = vq->dev;
422
423 if ((dev->byte_weight && total_len >= dev->byte_weight) ||
424 pkts >= dev->weight) {
425 vhost_poll_queue(&vq->poll);
426 return true;
427 }
428
429 return false;
430}
431EXPORT_SYMBOL_GPL(vhost_exceeds_weight);
432
Jason Wang4942e822019-05-24 04:12:16 -0400433static size_t vhost_get_avail_size(struct vhost_virtqueue *vq,
434 unsigned int num)
435{
436 size_t event __maybe_unused =
437 vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
438
439 return sizeof(*vq->avail) +
440 sizeof(*vq->avail->ring) * num + event;
441}
442
443static size_t vhost_get_used_size(struct vhost_virtqueue *vq,
444 unsigned int num)
445{
446 size_t event __maybe_unused =
447 vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
448
449 return sizeof(*vq->used) +
450 sizeof(*vq->used->ring) * num + event;
451}
452
453static size_t vhost_get_desc_size(struct vhost_virtqueue *vq,
454 unsigned int num)
455{
456 return sizeof(*vq->desc) * num;
457}
458
Zhi Yong Wu59566b6e2013-12-07 04:13:03 +0800459void vhost_dev_init(struct vhost_dev *dev,
Jason Wange82b9b02019-05-17 00:29:49 -0400460 struct vhost_virtqueue **vqs, int nvqs,
Jason Wang792a4f22020-03-26 22:01:18 +0800461 int iov_limit, int weight, int byte_weight,
Jason Wang01fcb1c2020-05-29 16:02:58 +0800462 bool use_worker,
Jason Wang792a4f22020-03-26 22:01:18 +0800463 int (*msg_handler)(struct vhost_dev *dev,
464 struct vhost_iotlb_msg *msg))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000465{
Asias He6d5e6aa2013-05-06 16:38:23 +0800466 struct vhost_virtqueue *vq;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000467 int i;
Tejun Heoc23f34452010-06-02 20:40:00 +0200468
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000469 dev->vqs = vqs;
470 dev->nvqs = nvqs;
471 mutex_init(&dev->mutex);
472 dev->log_ctx = NULL;
Jason Wanga9709d62016-06-23 02:04:31 -0400473 dev->umem = NULL;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400474 dev->iotlb = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000475 dev->mm = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200476 dev->worker = NULL;
Jason Wangb46a0bf2019-01-28 15:05:05 +0800477 dev->iov_limit = iov_limit;
Jason Wange82b9b02019-05-17 00:29:49 -0400478 dev->weight = weight;
479 dev->byte_weight = byte_weight;
Jason Wang01fcb1c2020-05-29 16:02:58 +0800480 dev->use_worker = use_worker;
Jason Wang792a4f22020-03-26 22:01:18 +0800481 dev->msg_handler = msg_handler;
Jason Wang04b96e52016-04-25 22:14:33 -0400482 init_llist_head(&dev->work_list);
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400483 init_waitqueue_head(&dev->wait);
484 INIT_LIST_HEAD(&dev->read_list);
485 INIT_LIST_HEAD(&dev->pending_list);
486 spin_lock_init(&dev->iotlb_lock);
Michael S. Tsirkin3d2c7d32019-08-10 13:53:21 -0400487
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000488
489 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800490 vq = dev->vqs[i];
491 vq->log = NULL;
492 vq->indirect = NULL;
493 vq->heads = NULL;
494 vq->dev = dev;
495 mutex_init(&vq->mutex);
496 vhost_vq_reset(dev, vq);
497 if (vq->handle_kick)
498 vhost_poll_init(&vq->poll, vq->handle_kick,
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800499 EPOLLIN, dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000500 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000501}
Asias He6ac1afb2013-05-06 16:38:21 +0800502EXPORT_SYMBOL_GPL(vhost_dev_init);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000503
504/* Caller should have device mutex */
505long vhost_dev_check_owner(struct vhost_dev *dev)
506{
507 /* Are you the owner? If not, I don't think you mean to do that */
508 return dev->mm == current->mm ? 0 : -EPERM;
509}
Asias He6ac1afb2013-05-06 16:38:21 +0800510EXPORT_SYMBOL_GPL(vhost_dev_check_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000511
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300512struct vhost_attach_cgroups_struct {
Krishna Kumard47effe2011-03-01 17:06:37 +0530513 struct vhost_work work;
514 struct task_struct *owner;
515 int ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300516};
517
518static void vhost_attach_cgroups_work(struct vhost_work *work)
519{
Krishna Kumard47effe2011-03-01 17:06:37 +0530520 struct vhost_attach_cgroups_struct *s;
521
522 s = container_of(work, struct vhost_attach_cgroups_struct, work);
523 s->ret = cgroup_attach_task_all(s->owner, current);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300524}
525
526static int vhost_attach_cgroups(struct vhost_dev *dev)
527{
Krishna Kumard47effe2011-03-01 17:06:37 +0530528 struct vhost_attach_cgroups_struct attach;
529
530 attach.owner = current;
531 vhost_work_init(&attach.work, vhost_attach_cgroups_work);
532 vhost_work_queue(dev, &attach.work);
533 vhost_work_flush(dev, &attach.work);
534 return attach.ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300535}
536
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000537/* Caller should have device mutex */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300538bool vhost_dev_has_owner(struct vhost_dev *dev)
539{
540 return dev->mm;
541}
Asias He6ac1afb2013-05-06 16:38:21 +0800542EXPORT_SYMBOL_GPL(vhost_dev_has_owner);
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300543
544/* Caller should have device mutex */
Asias He54db63c2013-05-06 11:15:59 +0800545long vhost_dev_set_owner(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000546{
Tejun Heoc23f34452010-06-02 20:40:00 +0200547 struct task_struct *worker;
548 int err;
Krishna Kumard47effe2011-03-01 17:06:37 +0530549
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000550 /* Is there an owner already? */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300551 if (vhost_dev_has_owner(dev)) {
Tejun Heoc23f34452010-06-02 20:40:00 +0200552 err = -EBUSY;
553 goto err_mm;
554 }
Krishna Kumard47effe2011-03-01 17:06:37 +0530555
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000556 /* No owner, become one */
557 dev->mm = get_task_mm(current);
Andrey Konovalov8f6a7f92019-12-04 16:52:50 -0800558 dev->kcov_handle = kcov_common_handle();
Jason Wang01fcb1c2020-05-29 16:02:58 +0800559 if (dev->use_worker) {
560 worker = kthread_create(vhost_worker, dev,
561 "vhost-%d", current->pid);
562 if (IS_ERR(worker)) {
563 err = PTR_ERR(worker);
564 goto err_worker;
565 }
566
567 dev->worker = worker;
568 wake_up_process(worker); /* avoid contributing to loadavg */
569
570 err = vhost_attach_cgroups(dev);
571 if (err)
572 goto err_cgroup;
Tejun Heoc23f34452010-06-02 20:40:00 +0200573 }
574
Jason Wange0e9b402010-09-14 23:53:05 +0800575 err = vhost_dev_alloc_iovecs(dev);
576 if (err)
577 goto err_cgroup;
578
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000579 return 0;
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300580err_cgroup:
Jason Wang01fcb1c2020-05-29 16:02:58 +0800581 if (dev->worker) {
582 kthread_stop(dev->worker);
583 dev->worker = NULL;
584 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200585err_worker:
586 if (dev->mm)
587 mmput(dev->mm);
588 dev->mm = NULL;
Andrey Konovalov8f6a7f92019-12-04 16:52:50 -0800589 dev->kcov_handle = 0;
Tejun Heoc23f34452010-06-02 20:40:00 +0200590err_mm:
591 return err;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000592}
Asias He6ac1afb2013-05-06 16:38:21 +0800593EXPORT_SYMBOL_GPL(vhost_dev_set_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000594
Jason Wang0bbe3062020-03-26 22:01:19 +0800595static struct vhost_iotlb *iotlb_alloc(void)
Jason Wanga9709d62016-06-23 02:04:31 -0400596{
Jason Wang0bbe3062020-03-26 22:01:19 +0800597 return vhost_iotlb_alloc(max_iotlb_entries,
598 VHOST_IOTLB_FLAG_RETIRE);
599}
600
601struct vhost_iotlb *vhost_dev_reset_owner_prepare(void)
602{
603 return iotlb_alloc();
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300604}
Asias He6ac1afb2013-05-06 16:38:21 +0800605EXPORT_SYMBOL_GPL(vhost_dev_reset_owner_prepare);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000606
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300607/* Caller should have device mutex */
Jason Wang0bbe3062020-03-26 22:01:19 +0800608void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_iotlb *umem)
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300609{
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300610 int i;
611
夷则(Caspar)f6f93f72017-12-25 00:08:58 +0800612 vhost_dev_cleanup(dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000613
Jason Wanga9709d62016-06-23 02:04:31 -0400614 dev->umem = umem;
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300615 /* We don't need VQ locks below since vhost_dev_cleanup makes sure
616 * VQs aren't running.
617 */
618 for (i = 0; i < dev->nvqs; ++i)
Jason Wanga9709d62016-06-23 02:04:31 -0400619 dev->vqs[i]->umem = umem;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000620}
Asias He6ac1afb2013-05-06 16:38:21 +0800621EXPORT_SYMBOL_GPL(vhost_dev_reset_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000622
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000623void vhost_dev_stop(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000624{
625 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530626
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000627 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800628 if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) {
629 vhost_poll_stop(&dev->vqs[i]->poll);
630 vhost_poll_flush(&dev->vqs[i]->poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000631 }
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000632 }
633}
Asias He6ac1afb2013-05-06 16:38:21 +0800634EXPORT_SYMBOL_GPL(vhost_dev_stop);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000635
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400636static void vhost_clear_msg(struct vhost_dev *dev)
637{
638 struct vhost_msg_node *node, *n;
639
640 spin_lock(&dev->iotlb_lock);
641
642 list_for_each_entry_safe(node, n, &dev->read_list, node) {
643 list_del(&node->node);
644 kfree(node);
645 }
646
647 list_for_each_entry_safe(node, n, &dev->pending_list, node) {
648 list_del(&node->node);
649 kfree(node);
650 }
651
652 spin_unlock(&dev->iotlb_lock);
653}
654
夷则(Caspar)f6f93f72017-12-25 00:08:58 +0800655void vhost_dev_cleanup(struct vhost_dev *dev)
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000656{
657 int i;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000658
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000659 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800660 if (dev->vqs[i]->error_ctx)
661 eventfd_ctx_put(dev->vqs[i]->error_ctx);
Asias He3ab2e422013-04-27 11:16:48 +0800662 if (dev->vqs[i]->kick)
663 fput(dev->vqs[i]->kick);
664 if (dev->vqs[i]->call_ctx)
665 eventfd_ctx_put(dev->vqs[i]->call_ctx);
Asias He3ab2e422013-04-27 11:16:48 +0800666 vhost_vq_reset(dev, dev->vqs[i]);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000667 }
Jason Wange0e9b402010-09-14 23:53:05 +0800668 vhost_dev_free_iovecs(dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000669 if (dev->log_ctx)
670 eventfd_ctx_put(dev->log_ctx);
671 dev->log_ctx = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000672 /* No one will access memory at this point */
Jason Wang0bbe3062020-03-26 22:01:19 +0800673 vhost_iotlb_free(dev->umem);
Jason Wanga9709d62016-06-23 02:04:31 -0400674 dev->umem = NULL;
Jason Wang0bbe3062020-03-26 22:01:19 +0800675 vhost_iotlb_free(dev->iotlb);
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400676 dev->iotlb = NULL;
677 vhost_clear_msg(dev);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800678 wake_up_interruptible_poll(&dev->wait, EPOLLIN | EPOLLRDNORM);
Jason Wang04b96e52016-04-25 22:14:33 -0400679 WARN_ON(!llist_empty(&dev->work_list));
Eric Dumazet78b620c2010-08-31 02:05:57 +0000680 if (dev->worker) {
681 kthread_stop(dev->worker);
682 dev->worker = NULL;
Andrey Konovalov8f6a7f92019-12-04 16:52:50 -0800683 dev->kcov_handle = 0;
Eric Dumazet78b620c2010-08-31 02:05:57 +0000684 }
Michael S. Tsirkin3d2c7d32019-08-10 13:53:21 -0400685 if (dev->mm)
Michael S. Tsirkin533a19b2010-10-06 15:34:38 +0200686 mmput(dev->mm);
687 dev->mm = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000688}
Asias He6ac1afb2013-05-06 16:38:21 +0800689EXPORT_SYMBOL_GPL(vhost_dev_cleanup);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000690
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800691static bool log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000692{
693 u64 a = addr / VHOST_PAGE_SIZE / 8;
Krishna Kumard47effe2011-03-01 17:06:37 +0530694
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000695 /* Make sure 64 bit math will not overflow. */
696 if (a > ULONG_MAX - (unsigned long)log_base ||
697 a + (unsigned long)log_base > ULONG_MAX)
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800698 return false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000699
Linus Torvalds96d4f262019-01-03 18:57:57 -0800700 return access_ok(log_base + a,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000701 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
702}
703
Michael S. Tsirkinec33d032016-08-01 23:20:53 +0300704static bool vhost_overflow(u64 uaddr, u64 size)
705{
706 /* Make sure 64 bit math will not overflow. */
707 return uaddr > ULONG_MAX || size > ULONG_MAX || uaddr > ULONG_MAX - size;
708}
709
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000710/* Caller should have vq mutex and device mutex. */
Jason Wang0bbe3062020-03-26 22:01:19 +0800711static bool vq_memory_access_ok(void __user *log_base, struct vhost_iotlb *umem,
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800712 int log_all)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000713{
Jason Wang0bbe3062020-03-26 22:01:19 +0800714 struct vhost_iotlb_map *map;
Jeff Dike179b2842010-04-07 09:59:10 -0400715
Jason Wanga9709d62016-06-23 02:04:31 -0400716 if (!umem)
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800717 return false;
Jeff Dike179b2842010-04-07 09:59:10 -0400718
Jason Wang0bbe3062020-03-26 22:01:19 +0800719 list_for_each_entry(map, &umem->list, link) {
720 unsigned long a = map->addr;
Jason Wanga9709d62016-06-23 02:04:31 -0400721
Jason Wang0bbe3062020-03-26 22:01:19 +0800722 if (vhost_overflow(map->addr, map->size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800723 return false;
Michael S. Tsirkinec33d032016-08-01 23:20:53 +0300724
725
Jason Wang0bbe3062020-03-26 22:01:19 +0800726 if (!access_ok((void __user *)a, map->size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800727 return false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000728 else if (log_all && !log_access_ok(log_base,
Jason Wang0bbe3062020-03-26 22:01:19 +0800729 map->start,
730 map->size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800731 return false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000732 }
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800733 return true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000734}
735
Jason Wangf8894912017-02-28 17:56:02 +0800736static inline void __user *vhost_vq_meta_fetch(struct vhost_virtqueue *vq,
737 u64 addr, unsigned int size,
738 int type)
739{
Jason Wang0bbe3062020-03-26 22:01:19 +0800740 const struct vhost_iotlb_map *map = vq->meta_iotlb[type];
Jason Wangf8894912017-02-28 17:56:02 +0800741
Jason Wang0bbe3062020-03-26 22:01:19 +0800742 if (!map)
Jason Wangf8894912017-02-28 17:56:02 +0800743 return NULL;
744
Michael S. Tsirkin1b0be992020-05-15 11:29:53 -0400745 return (void __user *)(uintptr_t)(map->addr + addr - map->start);
Jason Wangf8894912017-02-28 17:56:02 +0800746}
747
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000748/* Can we switch to this memory table? */
749/* Caller should have device mutex but not vq mutex */
Jason Wang0bbe3062020-03-26 22:01:19 +0800750static bool memory_access_ok(struct vhost_dev *d, struct vhost_iotlb *umem,
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800751 int log_all)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000752{
753 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530754
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000755 for (i = 0; i < d->nvqs; ++i) {
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800756 bool ok;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300757 bool log;
758
Asias He3ab2e422013-04-27 11:16:48 +0800759 mutex_lock(&d->vqs[i]->mutex);
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300760 log = log_all || vhost_has_feature(d->vqs[i], VHOST_F_LOG_ALL);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000761 /* If ring is inactive, will check when it's enabled. */
Asias He3ab2e422013-04-27 11:16:48 +0800762 if (d->vqs[i]->private_data)
Jason Wanga9709d62016-06-23 02:04:31 -0400763 ok = vq_memory_access_ok(d->vqs[i]->log_base,
764 umem, log);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000765 else
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800766 ok = true;
Asias He3ab2e422013-04-27 11:16:48 +0800767 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000768 if (!ok)
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800769 return false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000770 }
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800771 return true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000772}
773
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400774static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
775 struct iovec iov[], int iov_size, int access);
Jason Wangbfe2bc52016-06-23 02:04:30 -0400776
Michael S. Tsirkin72952cc2016-12-06 06:01:41 +0200777static int vhost_copy_to_user(struct vhost_virtqueue *vq, void __user *to,
Jason Wangbfe2bc52016-06-23 02:04:30 -0400778 const void *from, unsigned size)
779{
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400780 int ret;
Jason Wangbfe2bc52016-06-23 02:04:30 -0400781
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400782 if (!vq->iotlb)
783 return __copy_to_user(to, from, size);
784 else {
785 /* This function should be called after iotlb
786 * prefetch, which means we're sure that all vq
787 * could be access through iotlb. So -EAGAIN should
788 * not happen in this case.
789 */
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400790 struct iov_iter t;
Jason Wangf8894912017-02-28 17:56:02 +0800791 void __user *uaddr = vhost_vq_meta_fetch(vq,
792 (u64)(uintptr_t)to, size,
Eric Auger7ced6c92018-04-11 15:30:38 +0200793 VHOST_ADDR_USED);
Jason Wangf8894912017-02-28 17:56:02 +0800794
795 if (uaddr)
796 return __copy_to_user(uaddr, from, size);
797
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400798 ret = translate_desc(vq, (u64)(uintptr_t)to, size, vq->iotlb_iov,
799 ARRAY_SIZE(vq->iotlb_iov),
800 VHOST_ACCESS_WO);
801 if (ret < 0)
802 goto out;
803 iov_iter_init(&t, WRITE, vq->iotlb_iov, ret, size);
804 ret = copy_to_iter(from, size, &t);
805 if (ret == size)
806 ret = 0;
807 }
808out:
809 return ret;
810}
Jason Wangbfe2bc52016-06-23 02:04:30 -0400811
812static int vhost_copy_from_user(struct vhost_virtqueue *vq, void *to,
Michael S. Tsirkin72952cc2016-12-06 06:01:41 +0200813 void __user *from, unsigned size)
Jason Wangbfe2bc52016-06-23 02:04:30 -0400814{
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400815 int ret;
816
817 if (!vq->iotlb)
818 return __copy_from_user(to, from, size);
819 else {
820 /* This function should be called after iotlb
821 * prefetch, which means we're sure that vq
822 * could be access through iotlb. So -EAGAIN should
823 * not happen in this case.
824 */
Jason Wangf8894912017-02-28 17:56:02 +0800825 void __user *uaddr = vhost_vq_meta_fetch(vq,
826 (u64)(uintptr_t)from, size,
827 VHOST_ADDR_DESC);
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400828 struct iov_iter f;
Jason Wangf8894912017-02-28 17:56:02 +0800829
830 if (uaddr)
831 return __copy_from_user(to, uaddr, size);
832
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400833 ret = translate_desc(vq, (u64)(uintptr_t)from, size, vq->iotlb_iov,
834 ARRAY_SIZE(vq->iotlb_iov),
835 VHOST_ACCESS_RO);
836 if (ret < 0) {
837 vq_err(vq, "IOTLB translation failure: uaddr "
838 "%p size 0x%llx\n", from,
839 (unsigned long long) size);
840 goto out;
841 }
842 iov_iter_init(&f, READ, vq->iotlb_iov, ret, size);
843 ret = copy_from_iter(to, size, &f);
844 if (ret == size)
845 ret = 0;
846 }
847
848out:
849 return ret;
850}
851
Jason Wangf8894912017-02-28 17:56:02 +0800852static void __user *__vhost_get_user_slow(struct vhost_virtqueue *vq,
853 void __user *addr, unsigned int size,
854 int type)
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400855{
856 int ret;
857
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400858 ret = translate_desc(vq, (u64)(uintptr_t)addr, size, vq->iotlb_iov,
859 ARRAY_SIZE(vq->iotlb_iov),
860 VHOST_ACCESS_RO);
861 if (ret < 0) {
862 vq_err(vq, "IOTLB translation failure: uaddr "
863 "%p size 0x%llx\n", addr,
864 (unsigned long long) size);
865 return NULL;
866 }
867
868 if (ret != 1 || vq->iotlb_iov[0].iov_len != size) {
869 vq_err(vq, "Non atomic userspace memory access: uaddr "
870 "%p size 0x%llx\n", addr,
871 (unsigned long long) size);
872 return NULL;
873 }
874
875 return vq->iotlb_iov[0].iov_base;
876}
877
Jason Wangf8894912017-02-28 17:56:02 +0800878/* This function should be called after iotlb
879 * prefetch, which means we're sure that vq
880 * could be access through iotlb. So -EAGAIN should
881 * not happen in this case.
882 */
883static inline void __user *__vhost_get_user(struct vhost_virtqueue *vq,
Michael S. Tsirkin1b0be992020-05-15 11:29:53 -0400884 void __user *addr, unsigned int size,
Jason Wangf8894912017-02-28 17:56:02 +0800885 int type)
886{
887 void __user *uaddr = vhost_vq_meta_fetch(vq,
888 (u64)(uintptr_t)addr, size, type);
889 if (uaddr)
890 return uaddr;
891
892 return __vhost_get_user_slow(vq, addr, size, type);
893}
894
895#define vhost_put_user(vq, x, ptr) \
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400896({ \
897 int ret = -EFAULT; \
898 if (!vq->iotlb) { \
899 ret = __put_user(x, ptr); \
900 } else { \
901 __typeof__(ptr) to = \
Jason Wangf8894912017-02-28 17:56:02 +0800902 (__typeof__(ptr)) __vhost_get_user(vq, ptr, \
903 sizeof(*ptr), VHOST_ADDR_USED); \
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400904 if (to != NULL) \
905 ret = __put_user(x, to); \
906 else \
907 ret = -EFAULT; \
908 } \
909 ret; \
910})
911
Jason Wang7b5d7532019-05-24 04:12:14 -0400912static inline int vhost_put_avail_event(struct vhost_virtqueue *vq)
913{
914 return vhost_put_user(vq, cpu_to_vhost16(vq, vq->avail_idx),
915 vhost_avail_event(vq));
916}
917
918static inline int vhost_put_used(struct vhost_virtqueue *vq,
919 struct vring_used_elem *head, int idx,
920 int count)
921{
922 return vhost_copy_to_user(vq, vq->used->ring + idx, head,
923 count * sizeof(*head));
924}
925
926static inline int vhost_put_used_flags(struct vhost_virtqueue *vq)
927
928{
929 return vhost_put_user(vq, cpu_to_vhost16(vq, vq->used_flags),
930 &vq->used->flags);
931}
932
933static inline int vhost_put_used_idx(struct vhost_virtqueue *vq)
934
935{
936 return vhost_put_user(vq, cpu_to_vhost16(vq, vq->last_used_idx),
937 &vq->used->idx);
938}
939
Jason Wangf8894912017-02-28 17:56:02 +0800940#define vhost_get_user(vq, x, ptr, type) \
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400941({ \
942 int ret; \
943 if (!vq->iotlb) { \
944 ret = __get_user(x, ptr); \
945 } else { \
946 __typeof__(ptr) from = \
Jason Wangf8894912017-02-28 17:56:02 +0800947 (__typeof__(ptr)) __vhost_get_user(vq, ptr, \
948 sizeof(*ptr), \
949 type); \
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400950 if (from != NULL) \
951 ret = __get_user(x, from); \
952 else \
953 ret = -EFAULT; \
954 } \
955 ret; \
956})
957
Jason Wangf8894912017-02-28 17:56:02 +0800958#define vhost_get_avail(vq, x, ptr) \
959 vhost_get_user(vq, x, ptr, VHOST_ADDR_AVAIL)
960
961#define vhost_get_used(vq, x, ptr) \
962 vhost_get_user(vq, x, ptr, VHOST_ADDR_USED)
963
Jason Wang86a07da2018-12-13 10:53:39 +0800964static void vhost_dev_lock_vqs(struct vhost_dev *d)
965{
966 int i = 0;
967 for (i = 0; i < d->nvqs; ++i)
968 mutex_lock_nested(&d->vqs[i]->mutex, i);
969}
970
971static void vhost_dev_unlock_vqs(struct vhost_dev *d)
972{
973 int i = 0;
974 for (i = 0; i < d->nvqs; ++i)
975 mutex_unlock(&d->vqs[i]->mutex);
976}
977
Jason Wang7b5d7532019-05-24 04:12:14 -0400978static inline int vhost_get_avail_idx(struct vhost_virtqueue *vq,
979 __virtio16 *idx)
980{
981 return vhost_get_avail(vq, *idx, &vq->avail->idx);
982}
983
984static inline int vhost_get_avail_head(struct vhost_virtqueue *vq,
985 __virtio16 *head, int idx)
986{
987 return vhost_get_avail(vq, *head,
988 &vq->avail->ring[idx & (vq->num - 1)]);
989}
990
991static inline int vhost_get_avail_flags(struct vhost_virtqueue *vq,
992 __virtio16 *flags)
993{
994 return vhost_get_avail(vq, *flags, &vq->avail->flags);
995}
996
997static inline int vhost_get_used_event(struct vhost_virtqueue *vq,
998 __virtio16 *event)
999{
1000 return vhost_get_avail(vq, *event, vhost_used_event(vq));
1001}
1002
1003static inline int vhost_get_used_idx(struct vhost_virtqueue *vq,
1004 __virtio16 *idx)
1005{
1006 return vhost_get_used(vq, *idx, &vq->used->idx);
1007}
1008
1009static inline int vhost_get_desc(struct vhost_virtqueue *vq,
1010 struct vring_desc *desc, int idx)
1011{
1012 return vhost_copy_from_user(vq, desc, vq->desc + idx, sizeof(*desc));
1013}
1014
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001015static void vhost_iotlb_notify_vq(struct vhost_dev *d,
1016 struct vhost_iotlb_msg *msg)
1017{
1018 struct vhost_msg_node *node, *n;
1019
1020 spin_lock(&d->iotlb_lock);
1021
1022 list_for_each_entry_safe(node, n, &d->pending_list, node) {
1023 struct vhost_iotlb_msg *vq_msg = &node->msg.iotlb;
1024 if (msg->iova <= vq_msg->iova &&
Jason Wang2d66f992018-08-24 16:53:13 +08001025 msg->iova + msg->size - 1 >= vq_msg->iova &&
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001026 vq_msg->type == VHOST_IOTLB_MISS) {
1027 vhost_poll_queue(&node->vq->poll);
1028 list_del(&node->node);
1029 kfree(node);
1030 }
1031 }
1032
1033 spin_unlock(&d->iotlb_lock);
1034}
1035
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001036static bool umem_access_ok(u64 uaddr, u64 size, int access)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001037{
1038 unsigned long a = uaddr;
1039
Michael S. Tsirkinec33d032016-08-01 23:20:53 +03001040 /* Make sure 64 bit math will not overflow. */
1041 if (vhost_overflow(uaddr, size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001042 return false;
Michael S. Tsirkinec33d032016-08-01 23:20:53 +03001043
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001044 if ((access & VHOST_ACCESS_RO) &&
Linus Torvalds96d4f262019-01-03 18:57:57 -08001045 !access_ok((void __user *)a, size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001046 return false;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001047 if ((access & VHOST_ACCESS_WO) &&
Linus Torvalds96d4f262019-01-03 18:57:57 -08001048 !access_ok((void __user *)a, size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001049 return false;
1050 return true;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001051}
1052
Michael S. Tsirkin72952cc2016-12-06 06:01:41 +02001053static int vhost_process_iotlb_msg(struct vhost_dev *dev,
1054 struct vhost_iotlb_msg *msg)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001055{
1056 int ret = 0;
1057
Jason Wang1b15ad62018-05-22 19:58:57 +08001058 mutex_lock(&dev->mutex);
Jason Wang86a07da2018-12-13 10:53:39 +08001059 vhost_dev_lock_vqs(dev);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001060 switch (msg->type) {
1061 case VHOST_IOTLB_UPDATE:
1062 if (!dev->iotlb) {
1063 ret = -EFAULT;
1064 break;
1065 }
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001066 if (!umem_access_ok(msg->uaddr, msg->size, msg->perm)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001067 ret = -EFAULT;
1068 break;
1069 }
Jason Wangf8894912017-02-28 17:56:02 +08001070 vhost_vq_meta_reset(dev);
Jason Wang0bbe3062020-03-26 22:01:19 +08001071 if (vhost_iotlb_add_range(dev->iotlb, msg->iova,
1072 msg->iova + msg->size - 1,
1073 msg->uaddr, msg->perm)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001074 ret = -ENOMEM;
1075 break;
1076 }
1077 vhost_iotlb_notify_vq(dev, msg);
1078 break;
1079 case VHOST_IOTLB_INVALIDATE:
Jason Wang6f3180a2018-01-23 17:27:26 +08001080 if (!dev->iotlb) {
1081 ret = -EFAULT;
1082 break;
1083 }
Jason Wangf8894912017-02-28 17:56:02 +08001084 vhost_vq_meta_reset(dev);
Jason Wang0bbe3062020-03-26 22:01:19 +08001085 vhost_iotlb_del_range(dev->iotlb, msg->iova,
1086 msg->iova + msg->size - 1);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001087 break;
1088 default:
1089 ret = -EINVAL;
1090 break;
1091 }
1092
Jason Wang86a07da2018-12-13 10:53:39 +08001093 vhost_dev_unlock_vqs(dev);
Jason Wang1b15ad62018-05-22 19:58:57 +08001094 mutex_unlock(&dev->mutex);
1095
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001096 return ret;
1097}
1098ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
1099 struct iov_iter *from)
1100{
Jason Wang429711a2018-08-06 11:17:47 +08001101 struct vhost_iotlb_msg msg;
1102 size_t offset;
1103 int type, ret;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001104
Jason Wang429711a2018-08-06 11:17:47 +08001105 ret = copy_from_iter(&type, sizeof(type), from);
Pavel Tikhomirov74ad7412018-12-13 17:53:50 +03001106 if (ret != sizeof(type)) {
1107 ret = -EINVAL;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001108 goto done;
Pavel Tikhomirov74ad7412018-12-13 17:53:50 +03001109 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001110
Jason Wang429711a2018-08-06 11:17:47 +08001111 switch (type) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001112 case VHOST_IOTLB_MSG:
Jason Wang429711a2018-08-06 11:17:47 +08001113 /* There maybe a hole after type for V1 message type,
1114 * so skip it here.
1115 */
1116 offset = offsetof(struct vhost_msg, iotlb) - sizeof(int);
1117 break;
1118 case VHOST_IOTLB_MSG_V2:
1119 offset = sizeof(__u32);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001120 break;
1121 default:
1122 ret = -EINVAL;
Jason Wang429711a2018-08-06 11:17:47 +08001123 goto done;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001124 }
1125
Jason Wang429711a2018-08-06 11:17:47 +08001126 iov_iter_advance(from, offset);
1127 ret = copy_from_iter(&msg, sizeof(msg), from);
Pavel Tikhomirov74ad7412018-12-13 17:53:50 +03001128 if (ret != sizeof(msg)) {
1129 ret = -EINVAL;
Jason Wang429711a2018-08-06 11:17:47 +08001130 goto done;
Pavel Tikhomirov74ad7412018-12-13 17:53:50 +03001131 }
Jason Wang792a4f22020-03-26 22:01:18 +08001132
1133 if (dev->msg_handler)
1134 ret = dev->msg_handler(dev, &msg);
1135 else
1136 ret = vhost_process_iotlb_msg(dev, &msg);
1137 if (ret) {
Jason Wang429711a2018-08-06 11:17:47 +08001138 ret = -EFAULT;
1139 goto done;
1140 }
1141
1142 ret = (type == VHOST_IOTLB_MSG) ? sizeof(struct vhost_msg) :
1143 sizeof(struct vhost_msg_v2);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001144done:
1145 return ret;
1146}
1147EXPORT_SYMBOL(vhost_chr_write_iter);
1148
Al Viroafc9a422017-07-03 06:39:46 -04001149__poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001150 poll_table *wait)
1151{
Al Viroafc9a422017-07-03 06:39:46 -04001152 __poll_t mask = 0;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001153
1154 poll_wait(file, &dev->wait, wait);
1155
1156 if (!list_empty(&dev->read_list))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001157 mask |= EPOLLIN | EPOLLRDNORM;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001158
1159 return mask;
1160}
1161EXPORT_SYMBOL(vhost_chr_poll);
1162
1163ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to,
1164 int noblock)
1165{
1166 DEFINE_WAIT(wait);
1167 struct vhost_msg_node *node;
1168 ssize_t ret = 0;
1169 unsigned size = sizeof(struct vhost_msg);
1170
1171 if (iov_iter_count(to) < size)
1172 return 0;
1173
1174 while (1) {
1175 if (!noblock)
1176 prepare_to_wait(&dev->wait, &wait,
1177 TASK_INTERRUPTIBLE);
1178
1179 node = vhost_dequeue_msg(dev, &dev->read_list);
1180 if (node)
1181 break;
1182 if (noblock) {
1183 ret = -EAGAIN;
1184 break;
1185 }
1186 if (signal_pending(current)) {
1187 ret = -ERESTARTSYS;
1188 break;
1189 }
1190 if (!dev->iotlb) {
1191 ret = -EBADFD;
1192 break;
1193 }
1194
1195 schedule();
1196 }
1197
1198 if (!noblock)
1199 finish_wait(&dev->wait, &wait);
1200
1201 if (node) {
Jason Wang429711a2018-08-06 11:17:47 +08001202 struct vhost_iotlb_msg *msg;
1203 void *start = &node->msg;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001204
Jason Wang429711a2018-08-06 11:17:47 +08001205 switch (node->msg.type) {
1206 case VHOST_IOTLB_MSG:
1207 size = sizeof(node->msg);
1208 msg = &node->msg.iotlb;
1209 break;
1210 case VHOST_IOTLB_MSG_V2:
1211 size = sizeof(node->msg_v2);
1212 msg = &node->msg_v2.iotlb;
1213 break;
1214 default:
1215 BUG();
1216 break;
1217 }
1218
1219 ret = copy_to_iter(start, size, to);
1220 if (ret != size || msg->type != VHOST_IOTLB_MISS) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001221 kfree(node);
1222 return ret;
1223 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001224 vhost_enqueue_msg(dev, &dev->pending_list, node);
1225 }
1226
1227 return ret;
1228}
1229EXPORT_SYMBOL_GPL(vhost_chr_read_iter);
1230
1231static int vhost_iotlb_miss(struct vhost_virtqueue *vq, u64 iova, int access)
1232{
1233 struct vhost_dev *dev = vq->dev;
1234 struct vhost_msg_node *node;
1235 struct vhost_iotlb_msg *msg;
Jason Wang429711a2018-08-06 11:17:47 +08001236 bool v2 = vhost_backend_has_feature(vq, VHOST_BACKEND_F_IOTLB_MSG_V2);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001237
Jason Wang429711a2018-08-06 11:17:47 +08001238 node = vhost_new_msg(vq, v2 ? VHOST_IOTLB_MSG_V2 : VHOST_IOTLB_MSG);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001239 if (!node)
1240 return -ENOMEM;
1241
Jason Wang429711a2018-08-06 11:17:47 +08001242 if (v2) {
1243 node->msg_v2.type = VHOST_IOTLB_MSG_V2;
1244 msg = &node->msg_v2.iotlb;
1245 } else {
1246 msg = &node->msg.iotlb;
1247 }
1248
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001249 msg->type = VHOST_IOTLB_MISS;
1250 msg->iova = iova;
1251 msg->perm = access;
1252
1253 vhost_enqueue_msg(dev, &dev->read_list, node);
1254
1255 return 0;
Jason Wangbfe2bc52016-06-23 02:04:30 -04001256}
1257
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001258static bool vq_access_ok(struct vhost_virtqueue *vq, unsigned int num,
Michael S. Tsirkina865e422020-04-06 08:42:55 -04001259 vring_desc_t __user *desc,
1260 vring_avail_t __user *avail,
1261 vring_used_t __user *used)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001262
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001263{
Jason Wang4942e822019-05-24 04:12:16 -04001264 return access_ok(desc, vhost_get_desc_size(vq, num)) &&
1265 access_ok(avail, vhost_get_avail_size(vq, num)) &&
1266 access_ok(used, vhost_get_used_size(vq, num));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001267}
1268
Jason Wangf8894912017-02-28 17:56:02 +08001269static void vhost_vq_meta_update(struct vhost_virtqueue *vq,
Jason Wang0bbe3062020-03-26 22:01:19 +08001270 const struct vhost_iotlb_map *map,
Jason Wangf8894912017-02-28 17:56:02 +08001271 int type)
1272{
1273 int access = (type == VHOST_ADDR_USED) ?
1274 VHOST_ACCESS_WO : VHOST_ACCESS_RO;
1275
Jason Wang0bbe3062020-03-26 22:01:19 +08001276 if (likely(map->perm & access))
1277 vq->meta_iotlb[type] = map;
Jason Wangf8894912017-02-28 17:56:02 +08001278}
1279
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001280static bool iotlb_access_ok(struct vhost_virtqueue *vq,
1281 int access, u64 addr, u64 len, int type)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001282{
Jason Wang0bbe3062020-03-26 22:01:19 +08001283 const struct vhost_iotlb_map *map;
1284 struct vhost_iotlb *umem = vq->iotlb;
Michael S. Tsirkinca2c5b32017-08-21 22:33:33 +03001285 u64 s = 0, size, orig_addr = addr, last = addr + len - 1;
Jason Wangf8894912017-02-28 17:56:02 +08001286
1287 if (vhost_vq_meta_fetch(vq, addr, len, type))
1288 return true;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001289
1290 while (len > s) {
Jason Wang0bbe3062020-03-26 22:01:19 +08001291 map = vhost_iotlb_itree_first(umem, addr, last);
1292 if (map == NULL || map->start > addr) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001293 vhost_iotlb_miss(vq, addr, access);
1294 return false;
Jason Wang0bbe3062020-03-26 22:01:19 +08001295 } else if (!(map->perm & access)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001296 /* Report the possible access violation by
1297 * request another translation from userspace.
1298 */
1299 return false;
1300 }
1301
Jason Wang0bbe3062020-03-26 22:01:19 +08001302 size = map->size - addr + map->start;
Jason Wangf8894912017-02-28 17:56:02 +08001303
1304 if (orig_addr == addr && size >= len)
Jason Wang0bbe3062020-03-26 22:01:19 +08001305 vhost_vq_meta_update(vq, map, type);
Jason Wangf8894912017-02-28 17:56:02 +08001306
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001307 s += size;
1308 addr += size;
1309 }
1310
1311 return true;
1312}
1313
Jason Wang9b5e8302019-05-24 04:12:15 -04001314int vq_meta_prefetch(struct vhost_virtqueue *vq)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001315{
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001316 unsigned int num = vq->num;
1317
Michael S. Tsirkin3d2c7d32019-08-10 13:53:21 -04001318 if (!vq->iotlb)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001319 return 1;
1320
Jason Wang0bbe3062020-03-26 22:01:19 +08001321 return iotlb_access_ok(vq, VHOST_MAP_RO, (u64)(uintptr_t)vq->desc,
Jason Wang4942e822019-05-24 04:12:16 -04001322 vhost_get_desc_size(vq, num), VHOST_ADDR_DESC) &&
Jason Wang0bbe3062020-03-26 22:01:19 +08001323 iotlb_access_ok(vq, VHOST_MAP_RO, (u64)(uintptr_t)vq->avail,
Jason Wang4942e822019-05-24 04:12:16 -04001324 vhost_get_avail_size(vq, num),
Jason Wangf8894912017-02-28 17:56:02 +08001325 VHOST_ADDR_AVAIL) &&
Jason Wang0bbe3062020-03-26 22:01:19 +08001326 iotlb_access_ok(vq, VHOST_MAP_WO, (u64)(uintptr_t)vq->used,
Jason Wang4942e822019-05-24 04:12:16 -04001327 vhost_get_used_size(vq, num), VHOST_ADDR_USED);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001328}
Jason Wang9b5e8302019-05-24 04:12:15 -04001329EXPORT_SYMBOL_GPL(vq_meta_prefetch);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001330
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001331/* Can we log writes? */
1332/* Caller should have device mutex but not vq mutex */
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001333bool vhost_log_access_ok(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001334{
Jason Wanga9709d62016-06-23 02:04:31 -04001335 return memory_access_ok(dev, dev->umem, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001336}
Asias He6ac1afb2013-05-06 16:38:21 +08001337EXPORT_SYMBOL_GPL(vhost_log_access_ok);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001338
1339/* Verify access for write logging. */
1340/* Caller should have vq mutex and device mutex */
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001341static bool vq_log_access_ok(struct vhost_virtqueue *vq,
1342 void __user *log_base)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001343{
Jason Wanga9709d62016-06-23 02:04:31 -04001344 return vq_memory_access_ok(log_base, vq->umem,
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001345 vhost_has_feature(vq, VHOST_F_LOG_ALL)) &&
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001346 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
Jason Wang4942e822019-05-24 04:12:16 -04001347 vhost_get_used_size(vq, vq->num)));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001348}
1349
1350/* Can we start vq? */
1351/* Caller should have vq mutex and device mutex */
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001352bool vhost_vq_access_ok(struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001353{
Stefan Hajnoczid14d2b72018-04-11 10:35:40 +08001354 if (!vq_log_access_ok(vq, vq->log_base))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001355 return false;
Jason Wangd65026c2018-03-29 16:00:04 +08001356
Stefan Hajnoczid14d2b72018-04-11 10:35:40 +08001357 /* Access validation occurs at prefetch time with IOTLB */
1358 if (vq->iotlb)
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001359 return true;
Jason Wangd65026c2018-03-29 16:00:04 +08001360
1361 return vq_access_ok(vq, vq->num, vq->desc, vq->avail, vq->used);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001362}
Asias He6ac1afb2013-05-06 16:38:21 +08001363EXPORT_SYMBOL_GPL(vhost_vq_access_ok);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001364
1365static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
1366{
Jason Wanga9709d62016-06-23 02:04:31 -04001367 struct vhost_memory mem, *newmem;
1368 struct vhost_memory_region *region;
Jason Wang0bbe3062020-03-26 22:01:19 +08001369 struct vhost_iotlb *newumem, *oldumem;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001370 unsigned long size = offsetof(struct vhost_memory, regions);
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001371 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +05301372
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001373 if (copy_from_user(&mem, m, size))
1374 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001375 if (mem.padding)
1376 return -EOPNOTSUPP;
Igor Mammedovc9ce42f2015-07-02 15:08:11 +02001377 if (mem.nregions > max_mem_regions)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001378 return -E2BIG;
Matthew Wilcoxb2303d72018-06-07 07:57:18 -07001379 newmem = kvzalloc(struct_size(newmem, regions, mem.nregions),
1380 GFP_KERNEL);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001381 if (!newmem)
1382 return -ENOMEM;
1383
1384 memcpy(newmem, &mem, size);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001385 if (copy_from_user(newmem->regions, m->regions,
1386 mem.nregions * sizeof *m->regions)) {
Igor Mammedovbcfeaca2015-06-16 18:33:35 +02001387 kvfree(newmem);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001388 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001389 }
1390
Jason Wang0bbe3062020-03-26 22:01:19 +08001391 newumem = iotlb_alloc();
Jason Wanga9709d62016-06-23 02:04:31 -04001392 if (!newumem) {
Igor Mammedov4de72552015-07-01 11:07:09 +02001393 kvfree(newmem);
Jason Wanga9709d62016-06-23 02:04:31 -04001394 return -ENOMEM;
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +09001395 }
Jason Wanga9709d62016-06-23 02:04:31 -04001396
Jason Wanga9709d62016-06-23 02:04:31 -04001397 for (region = newmem->regions;
1398 region < newmem->regions + mem.nregions;
1399 region++) {
Jason Wang0bbe3062020-03-26 22:01:19 +08001400 if (vhost_iotlb_add_range(newumem,
1401 region->guest_phys_addr,
1402 region->guest_phys_addr +
1403 region->memory_size - 1,
1404 region->userspace_addr,
1405 VHOST_MAP_RW))
Jason Wanga9709d62016-06-23 02:04:31 -04001406 goto err;
Jason Wanga9709d62016-06-23 02:04:31 -04001407 }
1408
1409 if (!memory_access_ok(d, newumem, 0))
1410 goto err;
1411
1412 oldumem = d->umem;
1413 d->umem = newumem;
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001414
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001415 /* All memory accesses are done under some VQ mutex. */
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001416 for (i = 0; i < d->nvqs; ++i) {
1417 mutex_lock(&d->vqs[i]->mutex);
Jason Wanga9709d62016-06-23 02:04:31 -04001418 d->vqs[i]->umem = newumem;
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001419 mutex_unlock(&d->vqs[i]->mutex);
1420 }
Jason Wanga9709d62016-06-23 02:04:31 -04001421
1422 kvfree(newmem);
Jason Wang0bbe3062020-03-26 22:01:19 +08001423 vhost_iotlb_free(oldumem);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001424 return 0;
Jason Wanga9709d62016-06-23 02:04:31 -04001425
1426err:
Jason Wang0bbe3062020-03-26 22:01:19 +08001427 vhost_iotlb_free(newumem);
Jason Wanga9709d62016-06-23 02:04:31 -04001428 kvfree(newmem);
1429 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001430}
1431
Jason Wangfeebcae2019-05-24 04:12:17 -04001432static long vhost_vring_set_num(struct vhost_dev *d,
1433 struct vhost_virtqueue *vq,
1434 void __user *argp)
1435{
1436 struct vhost_vring_state s;
1437
1438 /* Resizing ring with an active backend?
1439 * You don't want to do that. */
1440 if (vq->private_data)
1441 return -EBUSY;
1442
1443 if (copy_from_user(&s, argp, sizeof s))
1444 return -EFAULT;
1445
1446 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1)))
1447 return -EINVAL;
1448 vq->num = s.num;
1449
1450 return 0;
1451}
1452
1453static long vhost_vring_set_addr(struct vhost_dev *d,
1454 struct vhost_virtqueue *vq,
1455 void __user *argp)
1456{
1457 struct vhost_vring_addr a;
1458
1459 if (copy_from_user(&a, argp, sizeof a))
1460 return -EFAULT;
1461 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG))
1462 return -EOPNOTSUPP;
1463
1464 /* For 32bit, verify that the top 32bits of the user
1465 data are set to zero. */
1466 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
1467 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
1468 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr)
1469 return -EFAULT;
1470
1471 /* Make sure it's safe to cast pointers to vring types. */
1472 BUILD_BUG_ON(__alignof__ *vq->avail > VRING_AVAIL_ALIGN_SIZE);
1473 BUILD_BUG_ON(__alignof__ *vq->used > VRING_USED_ALIGN_SIZE);
1474 if ((a.avail_user_addr & (VRING_AVAIL_ALIGN_SIZE - 1)) ||
1475 (a.used_user_addr & (VRING_USED_ALIGN_SIZE - 1)) ||
1476 (a.log_guest_addr & (VRING_USED_ALIGN_SIZE - 1)))
1477 return -EINVAL;
1478
1479 /* We only verify access here if backend is configured.
1480 * If it is not, we don't as size might not have been setup.
1481 * We will verify when backend is configured. */
1482 if (vq->private_data) {
1483 if (!vq_access_ok(vq, vq->num,
1484 (void __user *)(unsigned long)a.desc_user_addr,
1485 (void __user *)(unsigned long)a.avail_user_addr,
1486 (void __user *)(unsigned long)a.used_user_addr))
1487 return -EINVAL;
1488
1489 /* Also validate log access for used ring if enabled. */
1490 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
1491 !log_access_ok(vq->log_base, a.log_guest_addr,
1492 sizeof *vq->used +
1493 vq->num * sizeof *vq->used->ring))
1494 return -EINVAL;
1495 }
1496
1497 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
1498 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
1499 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
1500 vq->log_addr = a.log_guest_addr;
1501 vq->used = (void __user *)(unsigned long)a.used_user_addr;
1502
1503 return 0;
1504}
1505
1506static long vhost_vring_set_num_addr(struct vhost_dev *d,
1507 struct vhost_virtqueue *vq,
1508 unsigned int ioctl,
1509 void __user *argp)
1510{
1511 long r;
1512
1513 mutex_lock(&vq->mutex);
1514
1515 switch (ioctl) {
1516 case VHOST_SET_VRING_NUM:
1517 r = vhost_vring_set_num(d, vq, argp);
1518 break;
1519 case VHOST_SET_VRING_ADDR:
1520 r = vhost_vring_set_addr(d, vq, argp);
1521 break;
1522 default:
1523 BUG();
1524 }
1525
1526 mutex_unlock(&vq->mutex);
1527
1528 return r;
1529}
Sonny Rao26b36602018-03-14 10:05:06 -07001530long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001531{
Al Virocecb46f2012-08-27 14:21:39 -04001532 struct file *eventfp, *filep = NULL;
1533 bool pollstart = false, pollstop = false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001534 struct eventfd_ctx *ctx = NULL;
1535 u32 __user *idxp = argp;
1536 struct vhost_virtqueue *vq;
1537 struct vhost_vring_state s;
1538 struct vhost_vring_file f;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001539 u32 idx;
1540 long r;
1541
1542 r = get_user(idx, idxp);
1543 if (r < 0)
1544 return r;
Krishna Kumar0f3d9a12010-05-25 11:10:36 +05301545 if (idx >= d->nvqs)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001546 return -ENOBUFS;
1547
Jason Wangff002262018-10-30 14:10:49 +08001548 idx = array_index_nospec(idx, d->nvqs);
Asias He3ab2e422013-04-27 11:16:48 +08001549 vq = d->vqs[idx];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001550
Jason Wangfeebcae2019-05-24 04:12:17 -04001551 if (ioctl == VHOST_SET_VRING_NUM ||
1552 ioctl == VHOST_SET_VRING_ADDR) {
1553 return vhost_vring_set_num_addr(d, vq, ioctl, argp);
1554 }
1555
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001556 mutex_lock(&vq->mutex);
1557
1558 switch (ioctl) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001559 case VHOST_SET_VRING_BASE:
1560 /* Moving base with an active backend?
1561 * You don't want to do that. */
1562 if (vq->private_data) {
1563 r = -EBUSY;
1564 break;
1565 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001566 if (copy_from_user(&s, argp, sizeof s)) {
1567 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001568 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001569 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001570 if (s.num > 0xffff) {
1571 r = -EINVAL;
1572 break;
1573 }
Jason Wang8d658432017-07-27 11:22:05 +08001574 vq->last_avail_idx = s.num;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001575 /* Forget the cached index value. */
1576 vq->avail_idx = vq->last_avail_idx;
1577 break;
1578 case VHOST_GET_VRING_BASE:
1579 s.index = idx;
1580 s.num = vq->last_avail_idx;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001581 if (copy_to_user(argp, &s, sizeof s))
1582 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001583 break;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001584 case VHOST_SET_VRING_KICK:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001585 if (copy_from_user(&f, argp, sizeof f)) {
1586 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001587 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001588 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001589 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +02001590 if (IS_ERR(eventfp)) {
1591 r = PTR_ERR(eventfp);
1592 break;
1593 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001594 if (eventfp != vq->kick) {
Al Virocecb46f2012-08-27 14:21:39 -04001595 pollstop = (filep = vq->kick) != NULL;
1596 pollstart = (vq->kick = eventfp) != NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001597 } else
1598 filep = eventfp;
1599 break;
1600 case VHOST_SET_VRING_CALL:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001601 if (copy_from_user(&f, argp, sizeof f)) {
1602 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001603 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001604 }
Eric Biggerse050c7d2018-01-06 14:52:19 -08001605 ctx = f.fd == -1 ? NULL : eventfd_ctx_fdget(f.fd);
1606 if (IS_ERR(ctx)) {
1607 r = PTR_ERR(ctx);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +02001608 break;
1609 }
Eric Biggerse050c7d2018-01-06 14:52:19 -08001610 swap(ctx, vq->call_ctx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001611 break;
1612 case VHOST_SET_VRING_ERR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001613 if (copy_from_user(&f, argp, sizeof f)) {
1614 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001615 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001616 }
Eric Biggers09f332a2018-01-06 14:52:20 -08001617 ctx = f.fd == -1 ? NULL : eventfd_ctx_fdget(f.fd);
1618 if (IS_ERR(ctx)) {
1619 r = PTR_ERR(ctx);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +02001620 break;
1621 }
Eric Biggers09f332a2018-01-06 14:52:20 -08001622 swap(ctx, vq->error_ctx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001623 break;
Greg Kurz2751c982015-04-24 14:27:24 +02001624 case VHOST_SET_VRING_ENDIAN:
1625 r = vhost_set_vring_endian(vq, argp);
1626 break;
1627 case VHOST_GET_VRING_ENDIAN:
1628 r = vhost_get_vring_endian(vq, idx, argp);
1629 break;
Jason Wang03088132016-03-04 06:24:53 -05001630 case VHOST_SET_VRING_BUSYLOOP_TIMEOUT:
1631 if (copy_from_user(&s, argp, sizeof(s))) {
1632 r = -EFAULT;
1633 break;
1634 }
1635 vq->busyloop_timeout = s.num;
1636 break;
1637 case VHOST_GET_VRING_BUSYLOOP_TIMEOUT:
1638 s.index = idx;
1639 s.num = vq->busyloop_timeout;
1640 if (copy_to_user(argp, &s, sizeof(s)))
1641 r = -EFAULT;
1642 break;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001643 default:
1644 r = -ENOIOCTLCMD;
1645 }
1646
1647 if (pollstop && vq->handle_kick)
1648 vhost_poll_stop(&vq->poll);
1649
Eric Biggerse050c7d2018-01-06 14:52:19 -08001650 if (!IS_ERR_OR_NULL(ctx))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001651 eventfd_ctx_put(ctx);
1652 if (filep)
1653 fput(filep);
1654
1655 if (pollstart && vq->handle_kick)
Jason Wang2b8b3282013-01-28 01:05:18 +00001656 r = vhost_poll_start(&vq->poll, vq->kick);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001657
1658 mutex_unlock(&vq->mutex);
1659
1660 if (pollstop && vq->handle_kick)
1661 vhost_poll_flush(&vq->poll);
1662 return r;
1663}
Asias He6ac1afb2013-05-06 16:38:21 +08001664EXPORT_SYMBOL_GPL(vhost_vring_ioctl);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001665
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001666int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled)
1667{
Jason Wang0bbe3062020-03-26 22:01:19 +08001668 struct vhost_iotlb *niotlb, *oiotlb;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001669 int i;
1670
Jason Wang0bbe3062020-03-26 22:01:19 +08001671 niotlb = iotlb_alloc();
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001672 if (!niotlb)
1673 return -ENOMEM;
1674
1675 oiotlb = d->iotlb;
1676 d->iotlb = niotlb;
1677
1678 for (i = 0; i < d->nvqs; ++i) {
Jason Wangb13f9c62018-08-08 11:43:04 +08001679 struct vhost_virtqueue *vq = d->vqs[i];
1680
1681 mutex_lock(&vq->mutex);
1682 vq->iotlb = niotlb;
1683 __vhost_vq_meta_reset(vq);
1684 mutex_unlock(&vq->mutex);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001685 }
1686
Jason Wang0bbe3062020-03-26 22:01:19 +08001687 vhost_iotlb_free(oiotlb);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001688
1689 return 0;
1690}
1691EXPORT_SYMBOL_GPL(vhost_init_device_iotlb);
1692
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001693/* Caller must have device mutex */
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001694long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001695{
Eric Biggersd25cc432018-01-06 14:52:21 -08001696 struct eventfd_ctx *ctx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001697 u64 p;
1698 long r;
1699 int i, fd;
1700
1701 /* If you are not the owner, you can become one */
1702 if (ioctl == VHOST_SET_OWNER) {
1703 r = vhost_dev_set_owner(d);
1704 goto done;
1705 }
1706
1707 /* You must be the owner to do anything else */
1708 r = vhost_dev_check_owner(d);
1709 if (r)
1710 goto done;
1711
1712 switch (ioctl) {
1713 case VHOST_SET_MEM_TABLE:
1714 r = vhost_set_memory(d, argp);
1715 break;
1716 case VHOST_SET_LOG_BASE:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001717 if (copy_from_user(&p, argp, sizeof p)) {
1718 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001719 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001720 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001721 if ((u64)(unsigned long)p != p) {
1722 r = -EFAULT;
1723 break;
1724 }
1725 for (i = 0; i < d->nvqs; ++i) {
1726 struct vhost_virtqueue *vq;
1727 void __user *base = (void __user *)(unsigned long)p;
Asias He3ab2e422013-04-27 11:16:48 +08001728 vq = d->vqs[i];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001729 mutex_lock(&vq->mutex);
1730 /* If ring is inactive, will check when it's enabled. */
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001731 if (vq->private_data && !vq_log_access_ok(vq, base))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001732 r = -EFAULT;
1733 else
1734 vq->log_base = base;
1735 mutex_unlock(&vq->mutex);
1736 }
1737 break;
1738 case VHOST_SET_LOG_FD:
1739 r = get_user(fd, (int __user *)argp);
1740 if (r < 0)
1741 break;
Eric Biggersd25cc432018-01-06 14:52:21 -08001742 ctx = fd == -1 ? NULL : eventfd_ctx_fdget(fd);
1743 if (IS_ERR(ctx)) {
1744 r = PTR_ERR(ctx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001745 break;
1746 }
Eric Biggersd25cc432018-01-06 14:52:21 -08001747 swap(ctx, d->log_ctx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001748 for (i = 0; i < d->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +08001749 mutex_lock(&d->vqs[i]->mutex);
1750 d->vqs[i]->log_ctx = d->log_ctx;
1751 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001752 }
1753 if (ctx)
1754 eventfd_ctx_put(ctx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001755 break;
1756 default:
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001757 r = -ENOIOCTLCMD;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001758 break;
1759 }
1760done:
1761 return r;
1762}
Asias He6ac1afb2013-05-06 16:38:21 +08001763EXPORT_SYMBOL_GPL(vhost_dev_ioctl);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001764
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001765/* TODO: This is really inefficient. We need something like get_user()
1766 * (instruction directly accesses the data, with an exception table entry
Mauro Carvalho Chehabcb1aaeb2019-06-07 15:54:32 -03001767 * returning -EFAULT). See Documentation/x86/exception-tables.rst.
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001768 */
1769static int set_bit_to_user(int nr, void __user *addr)
1770{
1771 unsigned long log = (unsigned long)addr;
1772 struct page *page;
1773 void *base;
1774 int bit = nr + (log % PAGE_SIZE) * 8;
1775 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301776
Ira Weiny73b01402019-05-13 17:17:11 -07001777 r = get_user_pages_fast(log, 1, FOLL_WRITE, &page);
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +02001778 if (r < 0)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001779 return r;
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +02001780 BUG_ON(r != 1);
Cong Wangc6daa7f2011-11-25 23:14:26 +08001781 base = kmap_atomic(page);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001782 set_bit(bit, base);
Cong Wangc6daa7f2011-11-25 23:14:26 +08001783 kunmap_atomic(base);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001784 set_page_dirty_lock(page);
1785 put_page(page);
1786 return 0;
1787}
1788
1789static int log_write(void __user *log_base,
1790 u64 write_address, u64 write_length)
1791{
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001792 u64 write_page = write_address / VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001793 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301794
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001795 if (!write_length)
1796 return 0;
Michael S. Tsirkin3bf9be42010-11-29 10:19:07 +02001797 write_length += write_address % VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001798 for (;;) {
1799 u64 base = (u64)(unsigned long)log_base;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001800 u64 log = base + write_page / 8;
1801 int bit = write_page % 8;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001802 if ((u64)(unsigned long)log != log)
1803 return -EFAULT;
1804 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
1805 if (r < 0)
1806 return r;
1807 if (write_length <= VHOST_PAGE_SIZE)
1808 break;
1809 write_length -= VHOST_PAGE_SIZE;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02001810 write_page += 1;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001811 }
1812 return r;
1813}
1814
Jason Wangcc5e7102019-01-16 16:54:42 +08001815static int log_write_hva(struct vhost_virtqueue *vq, u64 hva, u64 len)
1816{
Jason Wang0bbe3062020-03-26 22:01:19 +08001817 struct vhost_iotlb *umem = vq->umem;
1818 struct vhost_iotlb_map *u;
Jason Wangcc5e7102019-01-16 16:54:42 +08001819 u64 start, end, l, min;
1820 int r;
1821 bool hit = false;
1822
1823 while (len) {
1824 min = len;
1825 /* More than one GPAs can be mapped into a single HVA. So
1826 * iterate all possible umems here to be safe.
1827 */
Jason Wang0bbe3062020-03-26 22:01:19 +08001828 list_for_each_entry(u, &umem->list, link) {
1829 if (u->addr > hva - 1 + len ||
1830 u->addr - 1 + u->size < hva)
Jason Wangcc5e7102019-01-16 16:54:42 +08001831 continue;
Jason Wang0bbe3062020-03-26 22:01:19 +08001832 start = max(u->addr, hva);
1833 end = min(u->addr - 1 + u->size, hva - 1 + len);
Jason Wangcc5e7102019-01-16 16:54:42 +08001834 l = end - start + 1;
1835 r = log_write(vq->log_base,
Jason Wang0bbe3062020-03-26 22:01:19 +08001836 u->start + start - u->addr,
Jason Wangcc5e7102019-01-16 16:54:42 +08001837 l);
1838 if (r < 0)
1839 return r;
1840 hit = true;
1841 min = min(l, min);
1842 }
1843
1844 if (!hit)
1845 return -EFAULT;
1846
1847 len -= min;
1848 hva += min;
1849 }
1850
1851 return 0;
1852}
1853
1854static int log_used(struct vhost_virtqueue *vq, u64 used_offset, u64 len)
1855{
1856 struct iovec iov[64];
1857 int i, ret;
1858
1859 if (!vq->iotlb)
1860 return log_write(vq->log_base, vq->log_addr + used_offset, len);
1861
1862 ret = translate_desc(vq, (uintptr_t)vq->used + used_offset,
1863 len, iov, 64, VHOST_ACCESS_WO);
Jason Wang816db762019-02-19 14:53:44 +08001864 if (ret < 0)
Jason Wangcc5e7102019-01-16 16:54:42 +08001865 return ret;
1866
1867 for (i = 0; i < ret; i++) {
1868 ret = log_write_hva(vq, (uintptr_t)iov[i].iov_base,
1869 iov[i].iov_len);
1870 if (ret)
1871 return ret;
1872 }
1873
1874 return 0;
1875}
1876
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001877int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
Jason Wangcc5e7102019-01-16 16:54:42 +08001878 unsigned int log_num, u64 len, struct iovec *iov, int count)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001879{
1880 int i, r;
1881
1882 /* Make sure data written is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001883 smp_wmb();
Jason Wangcc5e7102019-01-16 16:54:42 +08001884
1885 if (vq->iotlb) {
1886 for (i = 0; i < count; i++) {
1887 r = log_write_hva(vq, (uintptr_t)iov[i].iov_base,
1888 iov[i].iov_len);
1889 if (r < 0)
1890 return r;
1891 }
1892 return 0;
1893 }
1894
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001895 for (i = 0; i < log_num; ++i) {
1896 u64 l = min(log[i].len, len);
1897 r = log_write(vq->log_base, log[i].addr, l);
1898 if (r < 0)
1899 return r;
1900 len -= l;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +02001901 if (!len) {
1902 if (vq->log_ctx)
1903 eventfd_signal(vq->log_ctx, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001904 return 0;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +02001905 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001906 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001907 /* Length written exceeds what we have stored. This is a bug. */
1908 BUG();
1909 return 0;
1910}
Asias He6ac1afb2013-05-06 16:38:21 +08001911EXPORT_SYMBOL_GPL(vhost_log_write);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001912
Jason Wang2723fea2011-06-21 18:04:38 +08001913static int vhost_update_used_flags(struct vhost_virtqueue *vq)
1914{
1915 void __user *used;
Jason Wang7b5d7532019-05-24 04:12:14 -04001916 if (vhost_put_used_flags(vq))
Jason Wang2723fea2011-06-21 18:04:38 +08001917 return -EFAULT;
1918 if (unlikely(vq->log_used)) {
1919 /* Make sure the flag is seen before log. */
1920 smp_wmb();
1921 /* Log used flag write. */
1922 used = &vq->used->flags;
Jason Wangcc5e7102019-01-16 16:54:42 +08001923 log_used(vq, (used - (void __user *)vq->used),
1924 sizeof vq->used->flags);
Jason Wang2723fea2011-06-21 18:04:38 +08001925 if (vq->log_ctx)
1926 eventfd_signal(vq->log_ctx, 1);
1927 }
1928 return 0;
1929}
1930
1931static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
1932{
Jason Wang7b5d7532019-05-24 04:12:14 -04001933 if (vhost_put_avail_event(vq))
Jason Wang2723fea2011-06-21 18:04:38 +08001934 return -EFAULT;
1935 if (unlikely(vq->log_used)) {
1936 void __user *used;
1937 /* Make sure the event is seen before log. */
1938 smp_wmb();
1939 /* Log avail event write */
1940 used = vhost_avail_event(vq);
Jason Wangcc5e7102019-01-16 16:54:42 +08001941 log_used(vq, (used - (void __user *)vq->used),
1942 sizeof *vhost_avail_event(vq));
Jason Wang2723fea2011-06-21 18:04:38 +08001943 if (vq->log_ctx)
1944 eventfd_signal(vq->log_ctx, 1);
1945 }
1946 return 0;
1947}
1948
Greg Kurz80f7d032016-02-16 15:59:44 +01001949int vhost_vq_init_access(struct vhost_virtqueue *vq)
Jason Wang2723fea2011-06-21 18:04:38 +08001950{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001951 __virtio16 last_used_idx;
Jason Wang2723fea2011-06-21 18:04:38 +08001952 int r;
Greg Kurze1f33be2016-02-16 15:54:28 +01001953 bool is_le = vq->is_le;
1954
Halil Pasiccda8bba2017-01-30 11:09:36 +01001955 if (!vq->private_data)
Jason Wang2723fea2011-06-21 18:04:38 +08001956 return 0;
Greg Kurz2751c982015-04-24 14:27:24 +02001957
1958 vhost_init_is_le(vq);
Jason Wang2723fea2011-06-21 18:04:38 +08001959
1960 r = vhost_update_used_flags(vq);
1961 if (r)
Greg Kurze1f33be2016-02-16 15:54:28 +01001962 goto err;
Jason Wang2723fea2011-06-21 18:04:38 +08001963 vq->signalled_used_valid = false;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001964 if (!vq->iotlb &&
Linus Torvalds96d4f262019-01-03 18:57:57 -08001965 !access_ok(&vq->used->idx, sizeof vq->used->idx)) {
Greg Kurze1f33be2016-02-16 15:54:28 +01001966 r = -EFAULT;
1967 goto err;
1968 }
Jason Wang7b5d7532019-05-24 04:12:14 -04001969 r = vhost_get_used_idx(vq, &last_used_idx);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001970 if (r) {
1971 vq_err(vq, "Can't access used idx at %p\n",
1972 &vq->used->idx);
Greg Kurze1f33be2016-02-16 15:54:28 +01001973 goto err;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001974 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03001975 vq->last_used_idx = vhost16_to_cpu(vq, last_used_idx);
Michael S. Tsirkin64f7f052014-12-01 17:39:39 +02001976 return 0;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001977
Greg Kurze1f33be2016-02-16 15:54:28 +01001978err:
1979 vq->is_le = is_le;
1980 return r;
Jason Wang2723fea2011-06-21 18:04:38 +08001981}
Greg Kurz80f7d032016-02-16 15:59:44 +01001982EXPORT_SYMBOL_GPL(vhost_vq_init_access);
Jason Wang2723fea2011-06-21 18:04:38 +08001983
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001984static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001985 struct iovec iov[], int iov_size, int access)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001986{
Jason Wang0bbe3062020-03-26 22:01:19 +08001987 const struct vhost_iotlb_map *map;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001988 struct vhost_dev *dev = vq->dev;
Jason Wang0bbe3062020-03-26 22:01:19 +08001989 struct vhost_iotlb *umem = dev->iotlb ? dev->iotlb : dev->umem;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001990 struct iovec *_iov;
1991 u64 s = 0;
1992 int ret = 0;
1993
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001994 while ((u64)len > s) {
1995 u64 size;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001996 if (unlikely(ret >= iov_size)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001997 ret = -ENOBUFS;
1998 break;
1999 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002000
Jason Wang0bbe3062020-03-26 22:01:19 +08002001 map = vhost_iotlb_itree_first(umem, addr, addr + len - 1);
2002 if (map == NULL || map->start > addr) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002003 if (umem != dev->iotlb) {
2004 ret = -EFAULT;
2005 break;
2006 }
2007 ret = -EAGAIN;
2008 break;
Jason Wang0bbe3062020-03-26 22:01:19 +08002009 } else if (!(map->perm & access)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002010 ret = -EPERM;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002011 break;
2012 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002013
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002014 _iov = iov + ret;
Jason Wang0bbe3062020-03-26 22:01:19 +08002015 size = map->size - addr + map->start;
Michael S. Tsirkinbd971202012-11-26 05:57:27 +00002016 _iov->iov_len = min((u64)len - s, size);
Michael S. Tsirkin0d4a3f22019-09-14 15:21:51 -04002017 _iov->iov_base = (void __user *)(unsigned long)
Jason Wang0bbe3062020-03-26 22:01:19 +08002018 (map->addr + addr - map->start);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002019 s += size;
2020 addr += size;
2021 ++ret;
2022 }
2023
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002024 if (ret == -EAGAIN)
2025 vhost_iotlb_miss(vq, addr, access);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002026 return ret;
2027}
2028
2029/* Each buffer in the virtqueues is actually a chain of descriptors. This
2030 * function returns the next descriptor in the chain,
2031 * or -1U if we're at the end. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002032static unsigned next_desc(struct vhost_virtqueue *vq, struct vring_desc *desc)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002033{
2034 unsigned int next;
2035
2036 /* If this descriptor says it doesn't chain, we're done. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002037 if (!(desc->flags & cpu_to_vhost16(vq, VRING_DESC_F_NEXT)))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002038 return -1U;
2039
2040 /* Check they're not leading us off end of descriptors. */
Paul E. McKenney3a5db0b2017-11-27 09:45:10 -08002041 next = vhost16_to_cpu(vq, READ_ONCE(desc->next));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002042 return next;
2043}
2044
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03002045static int get_indirect(struct vhost_virtqueue *vq,
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002046 struct iovec iov[], unsigned int iov_size,
2047 unsigned int *out_num, unsigned int *in_num,
2048 struct vhost_log *log, unsigned int *log_num,
2049 struct vring_desc *indirect)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002050{
2051 struct vring_desc desc;
2052 unsigned int i = 0, count, found = 0;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002053 u32 len = vhost32_to_cpu(vq, indirect->len);
Al Viroaad9a1c2014-12-10 14:49:01 -05002054 struct iov_iter from;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002055 int ret, access;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002056
2057 /* Sanity check */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002058 if (unlikely(len % sizeof desc)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002059 vq_err(vq, "Invalid length in indirect descriptor: "
2060 "len 0x%llx not multiple of 0x%zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002061 (unsigned long long)len,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002062 sizeof desc);
2063 return -EINVAL;
2064 }
2065
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002066 ret = translate_desc(vq, vhost64_to_cpu(vq, indirect->addr), len, vq->indirect,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002067 UIO_MAXIOV, VHOST_ACCESS_RO);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002068 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002069 if (ret != -EAGAIN)
2070 vq_err(vq, "Translation failure %d in indirect.\n", ret);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002071 return ret;
2072 }
Al Viroaad9a1c2014-12-10 14:49:01 -05002073 iov_iter_init(&from, READ, vq->indirect, ret, len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002074
2075 /* We will use the result as an address to read from, so most
2076 * architectures only need a compiler barrier here. */
2077 read_barrier_depends();
2078
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002079 count = len / sizeof desc;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002080 /* Buffers are chained via a 16 bit next field, so
2081 * we can have at most 2^16 of these. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002082 if (unlikely(count > USHRT_MAX + 1)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002083 vq_err(vq, "Indirect buffer length too big: %d\n",
2084 indirect->len);
2085 return -E2BIG;
2086 }
2087
2088 do {
2089 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002090 if (unlikely(++found > count)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002091 vq_err(vq, "Loop detected: last one at %u "
2092 "indirect size %u\n",
2093 i, count);
2094 return -EINVAL;
2095 }
Al Virocbbd26b2016-11-01 22:09:04 -04002096 if (unlikely(!copy_from_iter_full(&desc, sizeof(desc), &from))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002097 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002098 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002099 return -EINVAL;
2100 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002101 if (unlikely(desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002102 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002103 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002104 return -EINVAL;
2105 }
2106
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002107 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE))
2108 access = VHOST_ACCESS_WO;
2109 else
2110 access = VHOST_ACCESS_RO;
2111
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002112 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
2113 vhost32_to_cpu(vq, desc.len), iov + iov_count,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002114 iov_size - iov_count, access);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002115 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002116 if (ret != -EAGAIN)
2117 vq_err(vq, "Translation failure %d indirect idx %d\n",
2118 ret, i);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002119 return ret;
2120 }
2121 /* If this is an input descriptor, increment that count. */
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002122 if (access == VHOST_ACCESS_WO) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002123 *in_num += ret;
yongduan060423b2019-09-11 17:44:24 +08002124 if (unlikely(log && ret)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002125 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
2126 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002127 ++*log_num;
2128 }
2129 } else {
2130 /* If it's an output descriptor, they're all supposed
2131 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002132 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002133 vq_err(vq, "Indirect descriptor "
2134 "has out after in: idx %d\n", i);
2135 return -EINVAL;
2136 }
2137 *out_num += ret;
2138 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002139 } while ((i = next_desc(vq, &desc)) != -1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002140 return 0;
2141}
2142
2143/* This looks in the virtqueue and for the first available buffer, and converts
2144 * it to an iovec for convenient access. Since descriptors consist of some
2145 * number of output then some number of input descriptors, it's actually two
2146 * iovecs, but we pack them into one and note how many of each there were.
2147 *
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002148 * This function returns the descriptor number found, or vq->num (which is
2149 * never a valid descriptor number) if none was found. A negative code is
2150 * returned on error. */
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03002151int vhost_get_vq_desc(struct vhost_virtqueue *vq,
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002152 struct iovec iov[], unsigned int iov_size,
2153 unsigned int *out_num, unsigned int *in_num,
2154 struct vhost_log *log, unsigned int *log_num)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002155{
2156 struct vring_desc desc;
2157 unsigned int i, head, found = 0;
2158 u16 last_avail_idx;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002159 __virtio16 avail_idx;
2160 __virtio16 ring_head;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002161 int ret, access;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002162
2163 /* Check it isn't doing very strange things with descriptor numbers. */
2164 last_avail_idx = vq->last_avail_idx;
Jason Wange3b56cd2017-02-07 15:49:50 +08002165
2166 if (vq->avail_idx == vq->last_avail_idx) {
Jason Wang7b5d7532019-05-24 04:12:14 -04002167 if (unlikely(vhost_get_avail_idx(vq, &avail_idx))) {
Jason Wange3b56cd2017-02-07 15:49:50 +08002168 vq_err(vq, "Failed to access avail idx at %p\n",
2169 &vq->avail->idx);
2170 return -EFAULT;
2171 }
2172 vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
2173
2174 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
2175 vq_err(vq, "Guest moved used index from %u to %u",
2176 last_avail_idx, vq->avail_idx);
2177 return -EFAULT;
2178 }
2179
2180 /* If there's nothing new since last we looked, return
2181 * invalid.
2182 */
2183 if (vq->avail_idx == last_avail_idx)
2184 return vq->num;
2185
2186 /* Only get avail ring entries after they have been
2187 * exposed by guest.
2188 */
2189 smp_rmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002190 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002191
2192 /* Grab the next descriptor number they're advertising, and increment
2193 * the index we've seen. */
Jason Wang7b5d7532019-05-24 04:12:14 -04002194 if (unlikely(vhost_get_avail_head(vq, &ring_head, last_avail_idx))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002195 vq_err(vq, "Failed to read head: idx %d address %p\n",
2196 last_avail_idx,
2197 &vq->avail->ring[last_avail_idx % vq->num]);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002198 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002199 }
2200
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002201 head = vhost16_to_cpu(vq, ring_head);
2202
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002203 /* If their number is silly, that's an error. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002204 if (unlikely(head >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002205 vq_err(vq, "Guest says index %u > %u is available",
2206 head, vq->num);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002207 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002208 }
2209
2210 /* When we start there are none of either input nor output. */
2211 *out_num = *in_num = 0;
2212 if (unlikely(log))
2213 *log_num = 0;
2214
2215 i = head;
2216 do {
2217 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002218 if (unlikely(i >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002219 vq_err(vq, "Desc index is %u > %u, head = %u",
2220 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002221 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002222 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002223 if (unlikely(++found > vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002224 vq_err(vq, "Loop detected: last one at %u "
2225 "vq size %u head %u\n",
2226 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002227 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002228 }
Jason Wang7b5d7532019-05-24 04:12:14 -04002229 ret = vhost_get_desc(vq, &desc, i);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002230 if (unlikely(ret)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002231 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
2232 i, vq->desc + i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002233 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002234 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002235 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT)) {
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03002236 ret = get_indirect(vq, iov, iov_size,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002237 out_num, in_num,
2238 log, log_num, &desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002239 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002240 if (ret != -EAGAIN)
2241 vq_err(vq, "Failure detected "
2242 "in indirect descriptor at idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002243 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002244 }
2245 continue;
2246 }
2247
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002248 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE))
2249 access = VHOST_ACCESS_WO;
2250 else
2251 access = VHOST_ACCESS_RO;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002252 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
2253 vhost32_to_cpu(vq, desc.len), iov + iov_count,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002254 iov_size - iov_count, access);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002255 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002256 if (ret != -EAGAIN)
2257 vq_err(vq, "Translation failure %d descriptor idx %d\n",
2258 ret, i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002259 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002260 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002261 if (access == VHOST_ACCESS_WO) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002262 /* If this is an input descriptor,
2263 * increment that count. */
2264 *in_num += ret;
yongduan060423b2019-09-11 17:44:24 +08002265 if (unlikely(log && ret)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002266 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
2267 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002268 ++*log_num;
2269 }
2270 } else {
2271 /* If it's an output descriptor, they're all supposed
2272 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002273 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002274 vq_err(vq, "Descriptor has out after in: "
2275 "idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002276 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002277 }
2278 *out_num += ret;
2279 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002280 } while ((i = next_desc(vq, &desc)) != -1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002281
2282 /* On success, increment avail index. */
2283 vq->last_avail_idx++;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002284
2285 /* Assume notifications from guest are disabled at this point,
2286 * if they aren't we would need to update avail_event index. */
2287 BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002288 return head;
2289}
Asias He6ac1afb2013-05-06 16:38:21 +08002290EXPORT_SYMBOL_GPL(vhost_get_vq_desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002291
2292/* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
David Stevens8dd014a2010-07-27 18:52:21 +03002293void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002294{
David Stevens8dd014a2010-07-27 18:52:21 +03002295 vq->last_avail_idx -= n;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002296}
Asias He6ac1afb2013-05-06 16:38:21 +08002297EXPORT_SYMBOL_GPL(vhost_discard_vq_desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002298
2299/* After we've used one of their buffers, we tell them about it. We'll then
2300 * want to notify the guest, using eventfd. */
2301int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
2302{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002303 struct vring_used_elem heads = {
2304 cpu_to_vhost32(vq, head),
2305 cpu_to_vhost32(vq, len)
2306 };
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002307
Jason Wangc49e4e52013-09-02 16:40:58 +08002308 return vhost_add_used_n(vq, &heads, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002309}
Asias He6ac1afb2013-05-06 16:38:21 +08002310EXPORT_SYMBOL_GPL(vhost_add_used);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002311
David Stevens8dd014a2010-07-27 18:52:21 +03002312static int __vhost_add_used_n(struct vhost_virtqueue *vq,
2313 struct vring_used_elem *heads,
2314 unsigned count)
2315{
Michael S. Tsirkina865e422020-04-06 08:42:55 -04002316 vring_used_elem_t __user *used;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002317 u16 old, new;
David Stevens8dd014a2010-07-27 18:52:21 +03002318 int start;
2319
Michael S. Tsirkin5fba13b2015-11-29 13:34:44 +02002320 start = vq->last_used_idx & (vq->num - 1);
David Stevens8dd014a2010-07-27 18:52:21 +03002321 used = vq->used->ring + start;
Jason Wang7b5d7532019-05-24 04:12:14 -04002322 if (vhost_put_used(vq, heads, start, count)) {
David Stevens8dd014a2010-07-27 18:52:21 +03002323 vq_err(vq, "Failed to write used");
2324 return -EFAULT;
2325 }
2326 if (unlikely(vq->log_used)) {
2327 /* Make sure data is seen before log. */
2328 smp_wmb();
2329 /* Log used ring entry write. */
Jason Wangcc5e7102019-01-16 16:54:42 +08002330 log_used(vq, ((void __user *)used - (void __user *)vq->used),
2331 count * sizeof *used);
David Stevens8dd014a2010-07-27 18:52:21 +03002332 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002333 old = vq->last_used_idx;
2334 new = (vq->last_used_idx += count);
2335 /* If the driver never bothers to signal in a very long while,
2336 * used index might wrap around. If that happens, invalidate
2337 * signalled_used index we stored. TODO: make sure driver
2338 * signals at least once in 2^16 and remove this. */
2339 if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old)))
2340 vq->signalled_used_valid = false;
David Stevens8dd014a2010-07-27 18:52:21 +03002341 return 0;
2342}
2343
2344/* After we've used one of their buffers, we tell them about it. We'll then
2345 * want to notify the guest, using eventfd. */
2346int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
2347 unsigned count)
2348{
2349 int start, n, r;
2350
Michael S. Tsirkin5fba13b2015-11-29 13:34:44 +02002351 start = vq->last_used_idx & (vq->num - 1);
David Stevens8dd014a2010-07-27 18:52:21 +03002352 n = vq->num - start;
2353 if (n < count) {
2354 r = __vhost_add_used_n(vq, heads, n);
2355 if (r < 0)
2356 return r;
2357 heads += n;
2358 count -= n;
2359 }
2360 r = __vhost_add_used_n(vq, heads, count);
2361
2362 /* Make sure buffer is written before we update index. */
2363 smp_wmb();
Jason Wang7b5d7532019-05-24 04:12:14 -04002364 if (vhost_put_used_idx(vq)) {
David Stevens8dd014a2010-07-27 18:52:21 +03002365 vq_err(vq, "Failed to increment used idx");
2366 return -EFAULT;
2367 }
2368 if (unlikely(vq->log_used)) {
Jason Wang841df922018-12-13 10:53:37 +08002369 /* Make sure used idx is seen before log. */
2370 smp_wmb();
David Stevens8dd014a2010-07-27 18:52:21 +03002371 /* Log used index update. */
Jason Wangcc5e7102019-01-16 16:54:42 +08002372 log_used(vq, offsetof(struct vring_used, idx),
2373 sizeof vq->used->idx);
David Stevens8dd014a2010-07-27 18:52:21 +03002374 if (vq->log_ctx)
2375 eventfd_signal(vq->log_ctx, 1);
2376 }
2377 return r;
2378}
Asias He6ac1afb2013-05-06 16:38:21 +08002379EXPORT_SYMBOL_GPL(vhost_add_used_n);
David Stevens8dd014a2010-07-27 18:52:21 +03002380
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002381static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002382{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002383 __u16 old, new;
2384 __virtio16 event;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002385 bool v;
Jason Wang8d658432017-07-27 11:22:05 +08002386 /* Flush out used index updates. This is paired
2387 * with the barrier that the Guest executes when enabling
2388 * interrupts. */
2389 smp_mb();
Michael S. Tsirkin0d499352010-05-11 19:44:17 +03002390
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002391 if (vhost_has_feature(vq, VIRTIO_F_NOTIFY_ON_EMPTY) &&
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002392 unlikely(vq->avail_idx == vq->last_avail_idx))
2393 return true;
2394
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002395 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002396 __virtio16 flags;
Jason Wang7b5d7532019-05-24 04:12:14 -04002397 if (vhost_get_avail_flags(vq, &flags)) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002398 vq_err(vq, "Failed to get flags");
2399 return true;
2400 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002401 return !(flags & cpu_to_vhost16(vq, VRING_AVAIL_F_NO_INTERRUPT));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002402 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002403 old = vq->signalled_used;
2404 v = vq->signalled_used_valid;
2405 new = vq->signalled_used = vq->last_used_idx;
2406 vq->signalled_used_valid = true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002407
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002408 if (unlikely(!v))
2409 return true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002410
Jason Wang7b5d7532019-05-24 04:12:14 -04002411 if (vhost_get_used_event(vq, &event)) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002412 vq_err(vq, "Failed to get used event idx");
2413 return true;
2414 }
Jason Wang8d658432017-07-27 11:22:05 +08002415 return vring_need_event(vhost16_to_cpu(vq, event), new, old);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002416}
2417
2418/* This actually signals the guest, using eventfd. */
2419void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
2420{
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002421 /* Signal the Guest tell them we used something up. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002422 if (vq->call_ctx && vhost_notify(dev, vq))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002423 eventfd_signal(vq->call_ctx, 1);
2424}
Asias He6ac1afb2013-05-06 16:38:21 +08002425EXPORT_SYMBOL_GPL(vhost_signal);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002426
2427/* And here's the combo meal deal. Supersize me! */
2428void vhost_add_used_and_signal(struct vhost_dev *dev,
2429 struct vhost_virtqueue *vq,
2430 unsigned int head, int len)
2431{
2432 vhost_add_used(vq, head, len);
2433 vhost_signal(dev, vq);
2434}
Asias He6ac1afb2013-05-06 16:38:21 +08002435EXPORT_SYMBOL_GPL(vhost_add_used_and_signal);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002436
David Stevens8dd014a2010-07-27 18:52:21 +03002437/* multi-buffer version of vhost_add_used_and_signal */
2438void vhost_add_used_and_signal_n(struct vhost_dev *dev,
2439 struct vhost_virtqueue *vq,
2440 struct vring_used_elem *heads, unsigned count)
2441{
2442 vhost_add_used_n(vq, heads, count);
2443 vhost_signal(dev, vq);
2444}
Asias He6ac1afb2013-05-06 16:38:21 +08002445EXPORT_SYMBOL_GPL(vhost_add_used_and_signal_n);
David Stevens8dd014a2010-07-27 18:52:21 +03002446
Jason Wangd4a60602016-03-04 06:24:52 -05002447/* return true if we're sure that avaiable ring is empty */
2448bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq)
2449{
2450 __virtio16 avail_idx;
2451 int r;
2452
Jason Wang275bf962017-01-18 15:02:01 +08002453 if (vq->avail_idx != vq->last_avail_idx)
Jason Wangd4a60602016-03-04 06:24:52 -05002454 return false;
2455
Jason Wang7b5d7532019-05-24 04:12:14 -04002456 r = vhost_get_avail_idx(vq, &avail_idx);
Jason Wang275bf962017-01-18 15:02:01 +08002457 if (unlikely(r))
2458 return false;
2459 vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
2460
2461 return vq->avail_idx == vq->last_avail_idx;
Jason Wangd4a60602016-03-04 06:24:52 -05002462}
2463EXPORT_SYMBOL_GPL(vhost_vq_avail_empty);
2464
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002465/* OK, now we need to know about added descriptors. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002466bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002467{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002468 __virtio16 avail_idx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002469 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05302470
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002471 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
2472 return false;
2473 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002474 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08002475 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002476 if (r) {
2477 vq_err(vq, "Failed to enable notification at %p: %d\n",
2478 &vq->used->flags, r);
2479 return false;
2480 }
2481 } else {
Jason Wang2723fea2011-06-21 18:04:38 +08002482 r = vhost_update_avail_event(vq, vq->avail_idx);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002483 if (r) {
2484 vq_err(vq, "Failed to update avail event index at %p: %d\n",
2485 vhost_avail_event(vq), r);
2486 return false;
2487 }
2488 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002489 /* They could have slipped one in as we were doing that: make
2490 * sure it's written, then check again. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00002491 smp_mb();
Jason Wang7b5d7532019-05-24 04:12:14 -04002492 r = vhost_get_avail_idx(vq, &avail_idx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002493 if (r) {
2494 vq_err(vq, "Failed to check avail idx at %p: %d\n",
2495 &vq->avail->idx, r);
2496 return false;
2497 }
2498
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002499 return vhost16_to_cpu(vq, avail_idx) != vq->avail_idx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002500}
Asias He6ac1afb2013-05-06 16:38:21 +08002501EXPORT_SYMBOL_GPL(vhost_enable_notify);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002502
2503/* We don't need to be notified again. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002504void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002505{
2506 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05302507
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002508 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
2509 return;
2510 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002511 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08002512 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002513 if (r)
2514 vq_err(vq, "Failed to enable notification at %p: %d\n",
2515 &vq->used->flags, r);
2516 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002517}
Asias He6ac1afb2013-05-06 16:38:21 +08002518EXPORT_SYMBOL_GPL(vhost_disable_notify);
2519
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002520/* Create a new message. */
2521struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type)
2522{
2523 struct vhost_msg_node *node = kmalloc(sizeof *node, GFP_KERNEL);
2524 if (!node)
2525 return NULL;
Michael S. Tsirkin670ae9c2018-05-12 00:33:10 +03002526
2527 /* Make sure all padding within the structure is initialized. */
2528 memset(&node->msg, 0, sizeof node->msg);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002529 node->vq = vq;
2530 node->msg.type = type;
2531 return node;
2532}
2533EXPORT_SYMBOL_GPL(vhost_new_msg);
2534
2535void vhost_enqueue_msg(struct vhost_dev *dev, struct list_head *head,
2536 struct vhost_msg_node *node)
2537{
2538 spin_lock(&dev->iotlb_lock);
2539 list_add_tail(&node->node, head);
2540 spin_unlock(&dev->iotlb_lock);
2541
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002542 wake_up_interruptible_poll(&dev->wait, EPOLLIN | EPOLLRDNORM);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002543}
2544EXPORT_SYMBOL_GPL(vhost_enqueue_msg);
2545
2546struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev,
2547 struct list_head *head)
2548{
2549 struct vhost_msg_node *node = NULL;
2550
2551 spin_lock(&dev->iotlb_lock);
2552 if (!list_empty(head)) {
2553 node = list_first_entry(head, struct vhost_msg_node,
2554 node);
2555 list_del(&node->node);
2556 }
2557 spin_unlock(&dev->iotlb_lock);
2558
2559 return node;
2560}
2561EXPORT_SYMBOL_GPL(vhost_dequeue_msg);
2562
2563
Asias He6ac1afb2013-05-06 16:38:21 +08002564static int __init vhost_init(void)
2565{
2566 return 0;
2567}
2568
2569static void __exit vhost_exit(void)
2570{
2571}
2572
2573module_init(vhost_init);
2574module_exit(vhost_exit);
2575
2576MODULE_VERSION("0.0.1");
2577MODULE_LICENSE("GPL v2");
2578MODULE_AUTHOR("Michael S. Tsirkin");
2579MODULE_DESCRIPTION("Host kernel accelerator for virtio");