blob: 1ac9de250319682f60574ed4e0544e138d44e39e [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>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000033
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000034#include "vhost.h"
35
Igor Mammedovc9ce42f2015-07-02 15:08:11 +020036static ushort max_mem_regions = 64;
37module_param(max_mem_regions, ushort, 0444);
38MODULE_PARM_DESC(max_mem_regions,
39 "Maximum number of memory regions in memory map. (default: 64)");
Jason Wang6b1e6cc2016-06-23 02:04:32 -040040static int max_iotlb_entries = 2048;
41module_param(max_iotlb_entries, int, 0444);
42MODULE_PARM_DESC(max_iotlb_entries,
43 "Maximum number of iotlb entries. (default: 2048)");
Igor Mammedovc9ce42f2015-07-02 15:08:11 +020044
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000045enum {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000046 VHOST_MEMORY_F_LOG = 0x1,
47};
48
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +030049#define vhost_used_event(vq) ((__virtio16 __user *)&vq->avail->ring[vq->num])
50#define vhost_avail_event(vq) ((__virtio16 __user *)&vq->used->ring[vq->num])
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +030051
Jason Wanga9709d62016-06-23 02:04:31 -040052INTERVAL_TREE_DEFINE(struct vhost_umem_node,
53 rb, __u64, __subtree_last,
Michael S. Tsirkin2f952c02016-12-06 05:57:54 +020054 START, LAST, static inline, vhost_umem_interval_tree);
Jason Wanga9709d62016-06-23 02:04:31 -040055
Greg Kurz2751c982015-04-24 14:27:24 +020056#ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
Greg Kurzc5072032016-02-16 15:59:34 +010057static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
Greg Kurz2751c982015-04-24 14:27:24 +020058{
59 vq->user_be = !virtio_legacy_is_little_endian();
60}
61
Greg Kurzc5072032016-02-16 15:59:34 +010062static void vhost_enable_cross_endian_big(struct vhost_virtqueue *vq)
63{
64 vq->user_be = true;
65}
66
67static void vhost_enable_cross_endian_little(struct vhost_virtqueue *vq)
68{
69 vq->user_be = false;
70}
71
Greg Kurz2751c982015-04-24 14:27:24 +020072static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
73{
74 struct vhost_vring_state s;
75
76 if (vq->private_data)
77 return -EBUSY;
78
79 if (copy_from_user(&s, argp, sizeof(s)))
80 return -EFAULT;
81
82 if (s.num != VHOST_VRING_LITTLE_ENDIAN &&
83 s.num != VHOST_VRING_BIG_ENDIAN)
84 return -EINVAL;
85
Greg Kurzc5072032016-02-16 15:59:34 +010086 if (s.num == VHOST_VRING_BIG_ENDIAN)
87 vhost_enable_cross_endian_big(vq);
88 else
89 vhost_enable_cross_endian_little(vq);
Greg Kurz2751c982015-04-24 14:27:24 +020090
91 return 0;
92}
93
94static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
95 int __user *argp)
96{
97 struct vhost_vring_state s = {
98 .index = idx,
99 .num = vq->user_be
100 };
101
102 if (copy_to_user(argp, &s, sizeof(s)))
103 return -EFAULT;
104
105 return 0;
106}
107
108static void vhost_init_is_le(struct vhost_virtqueue *vq)
109{
110 /* Note for legacy virtio: user_be is initialized at reset time
111 * according to the host endianness. If userspace does not set an
112 * explicit endianness, the default behavior is native endian, as
113 * expected by legacy virtio.
114 */
115 vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) || !vq->user_be;
116}
117#else
Greg Kurzc5072032016-02-16 15:59:34 +0100118static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
Greg Kurz2751c982015-04-24 14:27:24 +0200119{
120}
121
122static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
123{
124 return -ENOIOCTLCMD;
125}
126
127static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
128 int __user *argp)
129{
130 return -ENOIOCTLCMD;
131}
132
133static void vhost_init_is_le(struct vhost_virtqueue *vq)
134{
Halil Pasiccda8bba2017-01-30 11:09:36 +0100135 vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1)
136 || virtio_legacy_is_little_endian();
Greg Kurz2751c982015-04-24 14:27:24 +0200137}
138#endif /* CONFIG_VHOST_CROSS_ENDIAN_LEGACY */
139
Greg Kurzc5072032016-02-16 15:59:34 +0100140static void vhost_reset_is_le(struct vhost_virtqueue *vq)
141{
Halil Pasiccda8bba2017-01-30 11:09:36 +0100142 vhost_init_is_le(vq);
Greg Kurzc5072032016-02-16 15:59:34 +0100143}
144
Jason Wang7235acd2016-04-25 22:14:32 -0400145struct vhost_flush_struct {
146 struct vhost_work work;
147 struct completion wait_event;
148};
149
150static void vhost_flush_work(struct vhost_work *work)
151{
152 struct vhost_flush_struct *s;
153
154 s = container_of(work, struct vhost_flush_struct, work);
155 complete(&s->wait_event);
156}
157
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000158static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
159 poll_table *pt)
160{
161 struct vhost_poll *poll;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000162
Krishna Kumard47effe2011-03-01 17:06:37 +0530163 poll = container_of(pt, struct vhost_poll, table);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000164 poll->wqh = wqh;
165 add_wait_queue(wqh, &poll->wait);
166}
167
Ingo Molnarac6424b2017-06-20 12:06:13 +0200168static int vhost_poll_wakeup(wait_queue_entry_t *wait, unsigned mode, int sync,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000169 void *key)
170{
Tejun Heoc23f34452010-06-02 20:40:00 +0200171 struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
172
Al Viro3ad6f932017-07-03 20:14:56 -0400173 if (!(key_to_poll(key) & poll->mask))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000174 return 0;
175
Tejun Heoc23f34452010-06-02 20:40:00 +0200176 vhost_poll_queue(poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000177 return 0;
178}
179
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000180void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000181{
Jason Wang04b96e52016-04-25 22:14:33 -0400182 clear_bit(VHOST_WORK_QUEUED, &work->flags);
Tejun Heoc23f34452010-06-02 20:40:00 +0200183 work->fn = fn;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000184}
Asias He6ac1afb2013-05-06 16:38:21 +0800185EXPORT_SYMBOL_GPL(vhost_work_init);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000186
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300187/* Init poll structure */
188void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
Al Viro58e3b602017-07-03 23:50:40 -0400189 __poll_t mask, struct vhost_dev *dev)
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300190{
191 init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
192 init_poll_funcptr(&poll->table, vhost_poll_func);
193 poll->mask = mask;
194 poll->dev = dev;
Jason Wang2b8b3282013-01-28 01:05:18 +0000195 poll->wqh = NULL;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300196
197 vhost_work_init(&poll->work, fn);
198}
Asias He6ac1afb2013-05-06 16:38:21 +0800199EXPORT_SYMBOL_GPL(vhost_poll_init);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300200
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000201/* Start polling a file. We add ourselves to file's wait queue. The caller must
202 * keep a reference to a file until after vhost_poll_stop is called. */
Jason Wang2b8b3282013-01-28 01:05:18 +0000203int vhost_poll_start(struct vhost_poll *poll, struct file *file)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000204{
Al Viroe6c8adc2017-07-03 22:25:56 -0400205 __poll_t mask;
Krishna Kumard47effe2011-03-01 17:06:37 +0530206
Jason Wang70181d512013-04-10 20:50:48 +0000207 if (poll->wqh)
208 return 0;
209
Christoph Hellwig9965ed172018-03-05 07:26:05 -0800210 mask = vfs_poll(file, &poll->table);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000211 if (mask)
Al Viro3ad6f932017-07-03 20:14:56 -0400212 vhost_poll_wakeup(&poll->wait, 0, 0, poll_to_key(mask));
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800213 if (mask & EPOLLERR) {
Jason Wangdc6455a2018-03-27 20:50:52 +0800214 vhost_poll_stop(poll);
Yunsheng Lin896fc242019-08-20 20:36:32 +0800215 return -EINVAL;
Jason Wang2b8b3282013-01-28 01:05:18 +0000216 }
217
Yunsheng Lin896fc242019-08-20 20:36:32 +0800218 return 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000219}
Asias He6ac1afb2013-05-06 16:38:21 +0800220EXPORT_SYMBOL_GPL(vhost_poll_start);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000221
222/* Stop polling a file. After this function returns, it becomes safe to drop the
223 * file reference. You must also flush afterwards. */
224void vhost_poll_stop(struct vhost_poll *poll)
225{
Jason Wang2b8b3282013-01-28 01:05:18 +0000226 if (poll->wqh) {
227 remove_wait_queue(poll->wqh, &poll->wait);
228 poll->wqh = NULL;
229 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000230}
Asias He6ac1afb2013-05-06 16:38:21 +0800231EXPORT_SYMBOL_GPL(vhost_poll_stop);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000232
Asias He6ac1afb2013-05-06 16:38:21 +0800233void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000234{
Jason Wang7235acd2016-04-25 22:14:32 -0400235 struct vhost_flush_struct flush;
Tejun Heoc23f34452010-06-02 20:40:00 +0200236
Jason Wang7235acd2016-04-25 22:14:32 -0400237 if (dev->worker) {
238 init_completion(&flush.wait_event);
239 vhost_work_init(&flush.work, vhost_flush_work);
240
241 vhost_work_queue(dev, &flush.work);
242 wait_for_completion(&flush.wait_event);
243 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000244}
Asias He6ac1afb2013-05-06 16:38:21 +0800245EXPORT_SYMBOL_GPL(vhost_work_flush);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000246
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300247/* Flush any work that has been scheduled. When calling this, don't hold any
248 * locks that are also used by the callback. */
249void vhost_poll_flush(struct vhost_poll *poll)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000250{
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300251 vhost_work_flush(poll->dev, &poll->work);
252}
Asias He6ac1afb2013-05-06 16:38:21 +0800253EXPORT_SYMBOL_GPL(vhost_poll_flush);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300254
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000255void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300256{
Jason Wang04b96e52016-04-25 22:14:33 -0400257 if (!dev->worker)
258 return;
Tejun Heoc23f34452010-06-02 20:40:00 +0200259
Jason Wang04b96e52016-04-25 22:14:33 -0400260 if (!test_and_set_bit(VHOST_WORK_QUEUED, &work->flags)) {
261 /* We can only add the work to the list after we're
262 * sure it was not in the list.
Peng Tao635abf02016-12-07 17:52:19 +0800263 * test_and_set_bit() implies a memory barrier.
Jason Wang04b96e52016-04-25 22:14:33 -0400264 */
Jason Wang04b96e52016-04-25 22:14:33 -0400265 llist_add(&work->node, &dev->work_list);
Tejun Heoc23f34452010-06-02 20:40:00 +0200266 wake_up_process(dev->worker);
267 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000268}
Asias He6ac1afb2013-05-06 16:38:21 +0800269EXPORT_SYMBOL_GPL(vhost_work_queue);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000270
Jason Wang526d3e72016-03-04 06:24:51 -0500271/* A lockless hint for busy polling code to exit the loop */
272bool vhost_has_work(struct vhost_dev *dev)
273{
Jason Wang04b96e52016-04-25 22:14:33 -0400274 return !llist_empty(&dev->work_list);
Jason Wang526d3e72016-03-04 06:24:51 -0500275}
276EXPORT_SYMBOL_GPL(vhost_has_work);
277
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300278void vhost_poll_queue(struct vhost_poll *poll)
279{
280 vhost_work_queue(poll->dev, &poll->work);
281}
Asias He6ac1afb2013-05-06 16:38:21 +0800282EXPORT_SYMBOL_GPL(vhost_poll_queue);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300283
Jason Wangf8894912017-02-28 17:56:02 +0800284static void __vhost_vq_meta_reset(struct vhost_virtqueue *vq)
285{
286 int j;
287
288 for (j = 0; j < VHOST_NUM_ADDRS; j++)
289 vq->meta_iotlb[j] = NULL;
290}
291
292static void vhost_vq_meta_reset(struct vhost_dev *d)
293{
294 int i;
295
Jason Wang86a07da2018-12-13 10:53:39 +0800296 for (i = 0; i < d->nvqs; ++i)
Jason Wangf8894912017-02-28 17:56:02 +0800297 __vhost_vq_meta_reset(d->vqs[i]);
298}
299
Jason Wang7f466032019-05-24 04:12:18 -0400300#if VHOST_ARCH_CAN_ACCEL_UACCESS
301static void vhost_map_unprefetch(struct vhost_map *map)
302{
303 kfree(map->pages);
304 map->pages = NULL;
305 map->npages = 0;
306 map->addr = NULL;
307}
308
309static void vhost_uninit_vq_maps(struct vhost_virtqueue *vq)
310{
311 struct vhost_map *map[VHOST_NUM_ADDRS];
312 int i;
313
314 spin_lock(&vq->mmu_lock);
315 for (i = 0; i < VHOST_NUM_ADDRS; i++) {
316 map[i] = rcu_dereference_protected(vq->maps[i],
317 lockdep_is_held(&vq->mmu_lock));
318 if (map[i])
319 rcu_assign_pointer(vq->maps[i], NULL);
320 }
321 spin_unlock(&vq->mmu_lock);
322
323 synchronize_rcu();
324
325 for (i = 0; i < VHOST_NUM_ADDRS; i++)
326 if (map[i])
327 vhost_map_unprefetch(map[i]);
328
329}
330
331static void vhost_reset_vq_maps(struct vhost_virtqueue *vq)
332{
333 int i;
334
335 vhost_uninit_vq_maps(vq);
336 for (i = 0; i < VHOST_NUM_ADDRS; i++)
337 vq->uaddrs[i].size = 0;
338}
339
340static bool vhost_map_range_overlap(struct vhost_uaddr *uaddr,
341 unsigned long start,
342 unsigned long end)
343{
344 if (unlikely(!uaddr->size))
345 return false;
346
347 return !(end < uaddr->uaddr || start > uaddr->uaddr - 1 + uaddr->size);
348}
349
350static void vhost_invalidate_vq_start(struct vhost_virtqueue *vq,
351 int index,
352 unsigned long start,
353 unsigned long end)
354{
355 struct vhost_uaddr *uaddr = &vq->uaddrs[index];
356 struct vhost_map *map;
357 int i;
358
359 if (!vhost_map_range_overlap(uaddr, start, end))
360 return;
361
362 spin_lock(&vq->mmu_lock);
363 ++vq->invalidate_count;
364
365 map = rcu_dereference_protected(vq->maps[index],
366 lockdep_is_held(&vq->mmu_lock));
367 if (map) {
368 if (uaddr->write) {
369 for (i = 0; i < map->npages; i++)
370 set_page_dirty(map->pages[i]);
371 }
372 rcu_assign_pointer(vq->maps[index], NULL);
373 }
374 spin_unlock(&vq->mmu_lock);
375
376 if (map) {
377 synchronize_rcu();
378 vhost_map_unprefetch(map);
379 }
380}
381
382static void vhost_invalidate_vq_end(struct vhost_virtqueue *vq,
383 int index,
384 unsigned long start,
385 unsigned long end)
386{
387 if (!vhost_map_range_overlap(&vq->uaddrs[index], start, end))
388 return;
389
390 spin_lock(&vq->mmu_lock);
391 --vq->invalidate_count;
392 spin_unlock(&vq->mmu_lock);
393}
394
395static int vhost_invalidate_range_start(struct mmu_notifier *mn,
396 const struct mmu_notifier_range *range)
397{
398 struct vhost_dev *dev = container_of(mn, struct vhost_dev,
399 mmu_notifier);
400 int i, j;
401
402 if (!mmu_notifier_range_blockable(range))
403 return -EAGAIN;
404
405 for (i = 0; i < dev->nvqs; i++) {
406 struct vhost_virtqueue *vq = dev->vqs[i];
407
408 for (j = 0; j < VHOST_NUM_ADDRS; j++)
409 vhost_invalidate_vq_start(vq, j,
410 range->start,
411 range->end);
412 }
413
414 return 0;
415}
416
417static void vhost_invalidate_range_end(struct mmu_notifier *mn,
418 const struct mmu_notifier_range *range)
419{
420 struct vhost_dev *dev = container_of(mn, struct vhost_dev,
421 mmu_notifier);
422 int i, j;
423
424 for (i = 0; i < dev->nvqs; i++) {
425 struct vhost_virtqueue *vq = dev->vqs[i];
426
427 for (j = 0; j < VHOST_NUM_ADDRS; j++)
428 vhost_invalidate_vq_end(vq, j,
429 range->start,
430 range->end);
431 }
432}
433
434static const struct mmu_notifier_ops vhost_mmu_notifier_ops = {
435 .invalidate_range_start = vhost_invalidate_range_start,
436 .invalidate_range_end = vhost_invalidate_range_end,
437};
438
439static void vhost_init_maps(struct vhost_dev *dev)
440{
441 struct vhost_virtqueue *vq;
442 int i, j;
443
444 dev->mmu_notifier.ops = &vhost_mmu_notifier_ops;
445
446 for (i = 0; i < dev->nvqs; ++i) {
447 vq = dev->vqs[i];
448 for (j = 0; j < VHOST_NUM_ADDRS; j++)
449 RCU_INIT_POINTER(vq->maps[j], NULL);
450 }
451}
452#endif
453
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000454static void vhost_vq_reset(struct vhost_dev *dev,
455 struct vhost_virtqueue *vq)
456{
457 vq->num = 1;
458 vq->desc = NULL;
459 vq->avail = NULL;
460 vq->used = NULL;
461 vq->last_avail_idx = 0;
462 vq->avail_idx = 0;
463 vq->last_used_idx = 0;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300464 vq->signalled_used = 0;
465 vq->signalled_used_valid = false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000466 vq->used_flags = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000467 vq->log_used = false;
468 vq->log_addr = -1ull;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000469 vq->private_data = NULL;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300470 vq->acked_features = 0;
Jason Wang429711a2018-08-06 11:17:47 +0800471 vq->acked_backend_features = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000472 vq->log_base = NULL;
473 vq->error_ctx = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000474 vq->kick = NULL;
475 vq->call_ctx = NULL;
Michael S. Tsirkin73a99f02010-02-23 11:23:45 +0200476 vq->log_ctx = NULL;
Greg Kurzc5072032016-02-16 15:59:34 +0100477 vhost_reset_is_le(vq);
478 vhost_disable_cross_endian(vq);
Jason Wang03088132016-03-04 06:24:53 -0500479 vq->busyloop_timeout = 0;
Jason Wanga9709d62016-06-23 02:04:31 -0400480 vq->umem = NULL;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400481 vq->iotlb = NULL;
Jason Wang7f466032019-05-24 04:12:18 -0400482 vq->invalidate_count = 0;
Jason Wangf8894912017-02-28 17:56:02 +0800483 __vhost_vq_meta_reset(vq);
Jason Wang7f466032019-05-24 04:12:18 -0400484#if VHOST_ARCH_CAN_ACCEL_UACCESS
485 vhost_reset_vq_maps(vq);
486#endif
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000487}
488
Tejun Heoc23f34452010-06-02 20:40:00 +0200489static int vhost_worker(void *data)
490{
491 struct vhost_dev *dev = data;
Jason Wang04b96e52016-04-25 22:14:33 -0400492 struct vhost_work *work, *work_next;
493 struct llist_node *node;
Jens Freimannd7ffde32012-06-26 00:59:58 +0000494 mm_segment_t oldfs = get_fs();
Tejun Heoc23f34452010-06-02 20:40:00 +0200495
Jens Freimannd7ffde32012-06-26 00:59:58 +0000496 set_fs(USER_DS);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200497 use_mm(dev->mm);
498
Tejun Heoc23f34452010-06-02 20:40:00 +0200499 for (;;) {
500 /* mb paired w/ kthread_stop */
501 set_current_state(TASK_INTERRUPTIBLE);
502
Tejun Heoc23f34452010-06-02 20:40:00 +0200503 if (kthread_should_stop()) {
Tejun Heoc23f34452010-06-02 20:40:00 +0200504 __set_current_state(TASK_RUNNING);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200505 break;
Tejun Heoc23f34452010-06-02 20:40:00 +0200506 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200507
Jason Wang04b96e52016-04-25 22:14:33 -0400508 node = llist_del_all(&dev->work_list);
509 if (!node)
510 schedule();
511
512 node = llist_reverse_order(node);
513 /* make sure flag is seen after deletion */
514 smp_wmb();
515 llist_for_each_entry_safe(work, work_next, node, node) {
516 clear_bit(VHOST_WORK_QUEUED, &work->flags);
Tejun Heoc23f34452010-06-02 20:40:00 +0200517 __set_current_state(TASK_RUNNING);
518 work->fn(work);
Nadav Har'Eld550dda2012-02-27 15:07:29 +0200519 if (need_resched())
520 schedule();
Jason Wang04b96e52016-04-25 22:14:33 -0400521 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200522 }
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200523 unuse_mm(dev->mm);
Jens Freimannd7ffde32012-06-26 00:59:58 +0000524 set_fs(oldfs);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200525 return 0;
Tejun Heoc23f34452010-06-02 20:40:00 +0200526}
527
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000528static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
529{
530 kfree(vq->indirect);
531 vq->indirect = NULL;
532 kfree(vq->log);
533 vq->log = NULL;
534 kfree(vq->heads);
535 vq->heads = NULL;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000536}
537
Jason Wange0e9b402010-09-14 23:53:05 +0800538/* Helper to allocate iovec buffers for all vqs. */
539static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
540{
Asias He6d5e6aa2013-05-06 16:38:23 +0800541 struct vhost_virtqueue *vq;
Jason Wange0e9b402010-09-14 23:53:05 +0800542 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530543
Jason Wange0e9b402010-09-14 23:53:05 +0800544 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800545 vq = dev->vqs[i];
Kees Cook6da2ec52018-06-12 13:55:00 -0700546 vq->indirect = kmalloc_array(UIO_MAXIOV,
547 sizeof(*vq->indirect),
548 GFP_KERNEL);
Jason Wangb46a0bf2019-01-28 15:05:05 +0800549 vq->log = kmalloc_array(dev->iov_limit, sizeof(*vq->log),
Kees Cook6da2ec52018-06-12 13:55:00 -0700550 GFP_KERNEL);
Jason Wangb46a0bf2019-01-28 15:05:05 +0800551 vq->heads = kmalloc_array(dev->iov_limit, sizeof(*vq->heads),
Kees Cook6da2ec52018-06-12 13:55:00 -0700552 GFP_KERNEL);
Asias He6d5e6aa2013-05-06 16:38:23 +0800553 if (!vq->indirect || !vq->log || !vq->heads)
Jason Wange0e9b402010-09-14 23:53:05 +0800554 goto err_nomem;
555 }
556 return 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530557
Jason Wange0e9b402010-09-14 23:53:05 +0800558err_nomem:
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000559 for (; i >= 0; --i)
Asias He3ab2e422013-04-27 11:16:48 +0800560 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800561 return -ENOMEM;
562}
563
564static void vhost_dev_free_iovecs(struct vhost_dev *dev)
565{
566 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530567
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000568 for (i = 0; i < dev->nvqs; ++i)
Asias He3ab2e422013-04-27 11:16:48 +0800569 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800570}
571
Jason Wange82b9b02019-05-17 00:29:49 -0400572bool vhost_exceeds_weight(struct vhost_virtqueue *vq,
573 int pkts, int total_len)
574{
575 struct vhost_dev *dev = vq->dev;
576
577 if ((dev->byte_weight && total_len >= dev->byte_weight) ||
578 pkts >= dev->weight) {
579 vhost_poll_queue(&vq->poll);
580 return true;
581 }
582
583 return false;
584}
585EXPORT_SYMBOL_GPL(vhost_exceeds_weight);
586
Jason Wang4942e822019-05-24 04:12:16 -0400587static size_t vhost_get_avail_size(struct vhost_virtqueue *vq,
588 unsigned int num)
589{
590 size_t event __maybe_unused =
591 vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
592
593 return sizeof(*vq->avail) +
594 sizeof(*vq->avail->ring) * num + event;
595}
596
597static size_t vhost_get_used_size(struct vhost_virtqueue *vq,
598 unsigned int num)
599{
600 size_t event __maybe_unused =
601 vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
602
603 return sizeof(*vq->used) +
604 sizeof(*vq->used->ring) * num + event;
605}
606
607static size_t vhost_get_desc_size(struct vhost_virtqueue *vq,
608 unsigned int num)
609{
610 return sizeof(*vq->desc) * num;
611}
612
Zhi Yong Wu59566b6e2013-12-07 04:13:03 +0800613void vhost_dev_init(struct vhost_dev *dev,
Jason Wange82b9b02019-05-17 00:29:49 -0400614 struct vhost_virtqueue **vqs, int nvqs,
615 int iov_limit, int weight, int byte_weight)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000616{
Asias He6d5e6aa2013-05-06 16:38:23 +0800617 struct vhost_virtqueue *vq;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000618 int i;
Tejun Heoc23f34452010-06-02 20:40:00 +0200619
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000620 dev->vqs = vqs;
621 dev->nvqs = nvqs;
622 mutex_init(&dev->mutex);
623 dev->log_ctx = NULL;
Jason Wanga9709d62016-06-23 02:04:31 -0400624 dev->umem = NULL;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400625 dev->iotlb = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000626 dev->mm = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200627 dev->worker = NULL;
Jason Wangb46a0bf2019-01-28 15:05:05 +0800628 dev->iov_limit = iov_limit;
Jason Wange82b9b02019-05-17 00:29:49 -0400629 dev->weight = weight;
630 dev->byte_weight = byte_weight;
Jason Wang04b96e52016-04-25 22:14:33 -0400631 init_llist_head(&dev->work_list);
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400632 init_waitqueue_head(&dev->wait);
633 INIT_LIST_HEAD(&dev->read_list);
634 INIT_LIST_HEAD(&dev->pending_list);
635 spin_lock_init(&dev->iotlb_lock);
Jason Wang7f466032019-05-24 04:12:18 -0400636#if VHOST_ARCH_CAN_ACCEL_UACCESS
637 vhost_init_maps(dev);
638#endif
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000639
640 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800641 vq = dev->vqs[i];
642 vq->log = NULL;
643 vq->indirect = NULL;
644 vq->heads = NULL;
645 vq->dev = dev;
646 mutex_init(&vq->mutex);
Jason Wang7f466032019-05-24 04:12:18 -0400647 spin_lock_init(&vq->mmu_lock);
Asias He6d5e6aa2013-05-06 16:38:23 +0800648 vhost_vq_reset(dev, vq);
649 if (vq->handle_kick)
650 vhost_poll_init(&vq->poll, vq->handle_kick,
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800651 EPOLLIN, dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000652 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000653}
Asias He6ac1afb2013-05-06 16:38:21 +0800654EXPORT_SYMBOL_GPL(vhost_dev_init);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000655
656/* Caller should have device mutex */
657long vhost_dev_check_owner(struct vhost_dev *dev)
658{
659 /* Are you the owner? If not, I don't think you mean to do that */
660 return dev->mm == current->mm ? 0 : -EPERM;
661}
Asias He6ac1afb2013-05-06 16:38:21 +0800662EXPORT_SYMBOL_GPL(vhost_dev_check_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000663
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300664struct vhost_attach_cgroups_struct {
Krishna Kumard47effe2011-03-01 17:06:37 +0530665 struct vhost_work work;
666 struct task_struct *owner;
667 int ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300668};
669
670static void vhost_attach_cgroups_work(struct vhost_work *work)
671{
Krishna Kumard47effe2011-03-01 17:06:37 +0530672 struct vhost_attach_cgroups_struct *s;
673
674 s = container_of(work, struct vhost_attach_cgroups_struct, work);
675 s->ret = cgroup_attach_task_all(s->owner, current);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300676}
677
678static int vhost_attach_cgroups(struct vhost_dev *dev)
679{
Krishna Kumard47effe2011-03-01 17:06:37 +0530680 struct vhost_attach_cgroups_struct attach;
681
682 attach.owner = current;
683 vhost_work_init(&attach.work, vhost_attach_cgroups_work);
684 vhost_work_queue(dev, &attach.work);
685 vhost_work_flush(dev, &attach.work);
686 return attach.ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300687}
688
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000689/* Caller should have device mutex */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300690bool vhost_dev_has_owner(struct vhost_dev *dev)
691{
692 return dev->mm;
693}
Asias He6ac1afb2013-05-06 16:38:21 +0800694EXPORT_SYMBOL_GPL(vhost_dev_has_owner);
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300695
696/* Caller should have device mutex */
Asias He54db63c2013-05-06 11:15:59 +0800697long vhost_dev_set_owner(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000698{
Tejun Heoc23f34452010-06-02 20:40:00 +0200699 struct task_struct *worker;
700 int err;
Krishna Kumard47effe2011-03-01 17:06:37 +0530701
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000702 /* Is there an owner already? */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300703 if (vhost_dev_has_owner(dev)) {
Tejun Heoc23f34452010-06-02 20:40:00 +0200704 err = -EBUSY;
705 goto err_mm;
706 }
Krishna Kumard47effe2011-03-01 17:06:37 +0530707
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000708 /* No owner, become one */
709 dev->mm = get_task_mm(current);
Tejun Heoc23f34452010-06-02 20:40:00 +0200710 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
711 if (IS_ERR(worker)) {
712 err = PTR_ERR(worker);
713 goto err_worker;
714 }
715
716 dev->worker = worker;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300717 wake_up_process(worker); /* avoid contributing to loadavg */
718
719 err = vhost_attach_cgroups(dev);
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300720 if (err)
721 goto err_cgroup;
Tejun Heoc23f34452010-06-02 20:40:00 +0200722
Jason Wange0e9b402010-09-14 23:53:05 +0800723 err = vhost_dev_alloc_iovecs(dev);
724 if (err)
725 goto err_cgroup;
726
Jason Wang7f466032019-05-24 04:12:18 -0400727#if VHOST_ARCH_CAN_ACCEL_UACCESS
728 err = mmu_notifier_register(&dev->mmu_notifier, dev->mm);
729 if (err)
730 goto err_mmu_notifier;
731#endif
732
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000733 return 0;
Jason Wang7f466032019-05-24 04:12:18 -0400734
735#if VHOST_ARCH_CAN_ACCEL_UACCESS
736err_mmu_notifier:
737 vhost_dev_free_iovecs(dev);
738#endif
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300739err_cgroup:
740 kthread_stop(worker);
Michael S. Tsirkin615cc222010-09-02 14:16:36 +0300741 dev->worker = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200742err_worker:
743 if (dev->mm)
744 mmput(dev->mm);
745 dev->mm = NULL;
746err_mm:
747 return err;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000748}
Asias He6ac1afb2013-05-06 16:38:21 +0800749EXPORT_SYMBOL_GPL(vhost_dev_set_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000750
Jason Wanga9709d62016-06-23 02:04:31 -0400751struct vhost_umem *vhost_dev_reset_owner_prepare(void)
752{
Michal Hocko6c5ab652017-05-08 15:57:15 -0700753 return kvzalloc(sizeof(struct vhost_umem), GFP_KERNEL);
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300754}
Asias He6ac1afb2013-05-06 16:38:21 +0800755EXPORT_SYMBOL_GPL(vhost_dev_reset_owner_prepare);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000756
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300757/* Caller should have device mutex */
Jason Wanga9709d62016-06-23 02:04:31 -0400758void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_umem *umem)
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300759{
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300760 int i;
761
夷则(Caspar)f6f93f72017-12-25 00:08:58 +0800762 vhost_dev_cleanup(dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000763
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300764 /* Restore memory to default empty mapping. */
Jason Wanga9709d62016-06-23 02:04:31 -0400765 INIT_LIST_HEAD(&umem->umem_list);
766 dev->umem = umem;
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300767 /* We don't need VQ locks below since vhost_dev_cleanup makes sure
768 * VQs aren't running.
769 */
770 for (i = 0; i < dev->nvqs; ++i)
Jason Wanga9709d62016-06-23 02:04:31 -0400771 dev->vqs[i]->umem = umem;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000772}
Asias He6ac1afb2013-05-06 16:38:21 +0800773EXPORT_SYMBOL_GPL(vhost_dev_reset_owner);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000774
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000775void vhost_dev_stop(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000776{
777 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530778
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000779 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800780 if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) {
781 vhost_poll_stop(&dev->vqs[i]->poll);
782 vhost_poll_flush(&dev->vqs[i]->poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000783 }
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000784 }
785}
Asias He6ac1afb2013-05-06 16:38:21 +0800786EXPORT_SYMBOL_GPL(vhost_dev_stop);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000787
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400788static void vhost_umem_free(struct vhost_umem *umem,
789 struct vhost_umem_node *node)
790{
791 vhost_umem_interval_tree_remove(node, &umem->umem_tree);
792 list_del(&node->link);
793 kfree(node);
794 umem->numem--;
795}
796
Jason Wanga9709d62016-06-23 02:04:31 -0400797static void vhost_umem_clean(struct vhost_umem *umem)
798{
799 struct vhost_umem_node *node, *tmp;
800
801 if (!umem)
802 return;
803
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400804 list_for_each_entry_safe(node, tmp, &umem->umem_list, link)
805 vhost_umem_free(umem, node);
806
Jason Wanga9709d62016-06-23 02:04:31 -0400807 kvfree(umem);
808}
809
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400810static void vhost_clear_msg(struct vhost_dev *dev)
811{
812 struct vhost_msg_node *node, *n;
813
814 spin_lock(&dev->iotlb_lock);
815
816 list_for_each_entry_safe(node, n, &dev->read_list, node) {
817 list_del(&node->node);
818 kfree(node);
819 }
820
821 list_for_each_entry_safe(node, n, &dev->pending_list, node) {
822 list_del(&node->node);
823 kfree(node);
824 }
825
826 spin_unlock(&dev->iotlb_lock);
827}
828
Jason Wang7f466032019-05-24 04:12:18 -0400829#if VHOST_ARCH_CAN_ACCEL_UACCESS
830static void vhost_setup_uaddr(struct vhost_virtqueue *vq,
831 int index, unsigned long uaddr,
832 size_t size, bool write)
833{
834 struct vhost_uaddr *addr = &vq->uaddrs[index];
835
836 addr->uaddr = uaddr;
837 addr->size = size;
838 addr->write = write;
839}
840
841static void vhost_setup_vq_uaddr(struct vhost_virtqueue *vq)
842{
843 vhost_setup_uaddr(vq, VHOST_ADDR_DESC,
844 (unsigned long)vq->desc,
845 vhost_get_desc_size(vq, vq->num),
846 false);
847 vhost_setup_uaddr(vq, VHOST_ADDR_AVAIL,
848 (unsigned long)vq->avail,
849 vhost_get_avail_size(vq, vq->num),
850 false);
851 vhost_setup_uaddr(vq, VHOST_ADDR_USED,
852 (unsigned long)vq->used,
853 vhost_get_used_size(vq, vq->num),
854 true);
855}
856
857static int vhost_map_prefetch(struct vhost_virtqueue *vq,
858 int index)
859{
860 struct vhost_map *map;
861 struct vhost_uaddr *uaddr = &vq->uaddrs[index];
862 struct page **pages;
863 int npages = DIV_ROUND_UP(uaddr->size, PAGE_SIZE);
864 int npinned;
865 void *vaddr, *v;
866 int err;
867 int i;
868
869 spin_lock(&vq->mmu_lock);
870
871 err = -EFAULT;
872 if (vq->invalidate_count)
873 goto err;
874
875 err = -ENOMEM;
876 map = kmalloc(sizeof(*map), GFP_ATOMIC);
877 if (!map)
878 goto err;
879
880 pages = kmalloc_array(npages, sizeof(struct page *), GFP_ATOMIC);
881 if (!pages)
882 goto err_pages;
883
884 err = EFAULT;
885 npinned = __get_user_pages_fast(uaddr->uaddr, npages,
886 uaddr->write, pages);
887 if (npinned > 0)
888 release_pages(pages, npinned);
889 if (npinned != npages)
890 goto err_gup;
891
892 for (i = 0; i < npinned; i++)
893 if (PageHighMem(pages[i]))
894 goto err_gup;
895
896 vaddr = v = page_address(pages[0]);
897
898 /* For simplicity, fallback to userspace address if VA is not
899 * contigious.
900 */
901 for (i = 1; i < npinned; i++) {
902 v += PAGE_SIZE;
903 if (v != page_address(pages[i]))
904 goto err_gup;
905 }
906
907 map->addr = vaddr + (uaddr->uaddr & (PAGE_SIZE - 1));
908 map->npages = npages;
909 map->pages = pages;
910
911 rcu_assign_pointer(vq->maps[index], map);
912 /* No need for a synchronize_rcu(). This function should be
913 * called by dev->worker so we are serialized with all
914 * readers.
915 */
916 spin_unlock(&vq->mmu_lock);
917
918 return 0;
919
920err_gup:
921 kfree(pages);
922err_pages:
923 kfree(map);
924err:
925 spin_unlock(&vq->mmu_lock);
926 return err;
927}
928#endif
929
夷则(Caspar)f6f93f72017-12-25 00:08:58 +0800930void vhost_dev_cleanup(struct vhost_dev *dev)
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000931{
932 int i;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000933
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000934 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800935 if (dev->vqs[i]->error_ctx)
936 eventfd_ctx_put(dev->vqs[i]->error_ctx);
Asias He3ab2e422013-04-27 11:16:48 +0800937 if (dev->vqs[i]->kick)
938 fput(dev->vqs[i]->kick);
939 if (dev->vqs[i]->call_ctx)
940 eventfd_ctx_put(dev->vqs[i]->call_ctx);
Asias He3ab2e422013-04-27 11:16:48 +0800941 vhost_vq_reset(dev, dev->vqs[i]);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000942 }
Jason Wange0e9b402010-09-14 23:53:05 +0800943 vhost_dev_free_iovecs(dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000944 if (dev->log_ctx)
945 eventfd_ctx_put(dev->log_ctx);
946 dev->log_ctx = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000947 /* No one will access memory at this point */
Jason Wanga9709d62016-06-23 02:04:31 -0400948 vhost_umem_clean(dev->umem);
949 dev->umem = NULL;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400950 vhost_umem_clean(dev->iotlb);
951 dev->iotlb = NULL;
952 vhost_clear_msg(dev);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800953 wake_up_interruptible_poll(&dev->wait, EPOLLIN | EPOLLRDNORM);
Jason Wang04b96e52016-04-25 22:14:33 -0400954 WARN_ON(!llist_empty(&dev->work_list));
Eric Dumazet78b620c2010-08-31 02:05:57 +0000955 if (dev->worker) {
956 kthread_stop(dev->worker);
957 dev->worker = NULL;
958 }
Jason Wang7f466032019-05-24 04:12:18 -0400959 if (dev->mm) {
960#if VHOST_ARCH_CAN_ACCEL_UACCESS
961 mmu_notifier_unregister(&dev->mmu_notifier, dev->mm);
962#endif
Michael S. Tsirkin533a19b2010-10-06 15:34:38 +0200963 mmput(dev->mm);
Jason Wang7f466032019-05-24 04:12:18 -0400964 }
965#if VHOST_ARCH_CAN_ACCEL_UACCESS
966 for (i = 0; i < dev->nvqs; i++)
967 vhost_uninit_vq_maps(dev->vqs[i]);
968#endif
Michael S. Tsirkin533a19b2010-10-06 15:34:38 +0200969 dev->mm = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000970}
Asias He6ac1afb2013-05-06 16:38:21 +0800971EXPORT_SYMBOL_GPL(vhost_dev_cleanup);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000972
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800973static bool log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000974{
975 u64 a = addr / VHOST_PAGE_SIZE / 8;
Krishna Kumard47effe2011-03-01 17:06:37 +0530976
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000977 /* Make sure 64 bit math will not overflow. */
978 if (a > ULONG_MAX - (unsigned long)log_base ||
979 a + (unsigned long)log_base > ULONG_MAX)
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800980 return false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000981
Linus Torvalds96d4f262019-01-03 18:57:57 -0800982 return access_ok(log_base + a,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000983 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
984}
985
Michael S. Tsirkinec33d032016-08-01 23:20:53 +0300986static bool vhost_overflow(u64 uaddr, u64 size)
987{
988 /* Make sure 64 bit math will not overflow. */
989 return uaddr > ULONG_MAX || size > ULONG_MAX || uaddr > ULONG_MAX - size;
990}
991
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000992/* Caller should have vq mutex and device mutex. */
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800993static bool vq_memory_access_ok(void __user *log_base, struct vhost_umem *umem,
994 int log_all)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000995{
Jason Wanga9709d62016-06-23 02:04:31 -0400996 struct vhost_umem_node *node;
Jeff Dike179b2842010-04-07 09:59:10 -0400997
Jason Wanga9709d62016-06-23 02:04:31 -0400998 if (!umem)
Stefan Hajnocziddd3d402018-04-11 10:35:41 +0800999 return false;
Jeff Dike179b2842010-04-07 09:59:10 -04001000
Jason Wanga9709d62016-06-23 02:04:31 -04001001 list_for_each_entry(node, &umem->umem_list, link) {
1002 unsigned long a = node->userspace_addr;
1003
Michael S. Tsirkinec33d032016-08-01 23:20:53 +03001004 if (vhost_overflow(node->userspace_addr, node->size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001005 return false;
Michael S. Tsirkinec33d032016-08-01 23:20:53 +03001006
1007
Linus Torvalds96d4f262019-01-03 18:57:57 -08001008 if (!access_ok((void __user *)a,
Jason Wanga9709d62016-06-23 02:04:31 -04001009 node->size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001010 return false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001011 else if (log_all && !log_access_ok(log_base,
Jason Wanga9709d62016-06-23 02:04:31 -04001012 node->start,
1013 node->size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001014 return false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001015 }
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001016 return true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001017}
1018
Jason Wangf8894912017-02-28 17:56:02 +08001019static inline void __user *vhost_vq_meta_fetch(struct vhost_virtqueue *vq,
1020 u64 addr, unsigned int size,
1021 int type)
1022{
1023 const struct vhost_umem_node *node = vq->meta_iotlb[type];
1024
1025 if (!node)
1026 return NULL;
1027
1028 return (void *)(uintptr_t)(node->userspace_addr + addr - node->start);
1029}
1030
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001031/* Can we switch to this memory table? */
1032/* Caller should have device mutex but not vq mutex */
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001033static bool memory_access_ok(struct vhost_dev *d, struct vhost_umem *umem,
1034 int log_all)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001035{
1036 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +05301037
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001038 for (i = 0; i < d->nvqs; ++i) {
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001039 bool ok;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001040 bool log;
1041
Asias He3ab2e422013-04-27 11:16:48 +08001042 mutex_lock(&d->vqs[i]->mutex);
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001043 log = log_all || vhost_has_feature(d->vqs[i], VHOST_F_LOG_ALL);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001044 /* If ring is inactive, will check when it's enabled. */
Asias He3ab2e422013-04-27 11:16:48 +08001045 if (d->vqs[i]->private_data)
Jason Wanga9709d62016-06-23 02:04:31 -04001046 ok = vq_memory_access_ok(d->vqs[i]->log_base,
1047 umem, log);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001048 else
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001049 ok = true;
Asias He3ab2e422013-04-27 11:16:48 +08001050 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001051 if (!ok)
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001052 return false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001053 }
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001054 return true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001055}
1056
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001057static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
1058 struct iovec iov[], int iov_size, int access);
Jason Wangbfe2bc52016-06-23 02:04:30 -04001059
Michael S. Tsirkin72952cc2016-12-06 06:01:41 +02001060static int vhost_copy_to_user(struct vhost_virtqueue *vq, void __user *to,
Jason Wangbfe2bc52016-06-23 02:04:30 -04001061 const void *from, unsigned size)
1062{
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001063 int ret;
Jason Wangbfe2bc52016-06-23 02:04:30 -04001064
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001065 if (!vq->iotlb)
1066 return __copy_to_user(to, from, size);
1067 else {
1068 /* This function should be called after iotlb
1069 * prefetch, which means we're sure that all vq
1070 * could be access through iotlb. So -EAGAIN should
1071 * not happen in this case.
1072 */
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001073 struct iov_iter t;
Jason Wangf8894912017-02-28 17:56:02 +08001074 void __user *uaddr = vhost_vq_meta_fetch(vq,
1075 (u64)(uintptr_t)to, size,
Eric Auger7ced6c92018-04-11 15:30:38 +02001076 VHOST_ADDR_USED);
Jason Wangf8894912017-02-28 17:56:02 +08001077
1078 if (uaddr)
1079 return __copy_to_user(uaddr, from, size);
1080
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001081 ret = translate_desc(vq, (u64)(uintptr_t)to, size, vq->iotlb_iov,
1082 ARRAY_SIZE(vq->iotlb_iov),
1083 VHOST_ACCESS_WO);
1084 if (ret < 0)
1085 goto out;
1086 iov_iter_init(&t, WRITE, vq->iotlb_iov, ret, size);
1087 ret = copy_to_iter(from, size, &t);
1088 if (ret == size)
1089 ret = 0;
1090 }
1091out:
1092 return ret;
1093}
Jason Wangbfe2bc52016-06-23 02:04:30 -04001094
1095static int vhost_copy_from_user(struct vhost_virtqueue *vq, void *to,
Michael S. Tsirkin72952cc2016-12-06 06:01:41 +02001096 void __user *from, unsigned size)
Jason Wangbfe2bc52016-06-23 02:04:30 -04001097{
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001098 int ret;
1099
1100 if (!vq->iotlb)
1101 return __copy_from_user(to, from, size);
1102 else {
1103 /* This function should be called after iotlb
1104 * prefetch, which means we're sure that vq
1105 * could be access through iotlb. So -EAGAIN should
1106 * not happen in this case.
1107 */
Jason Wangf8894912017-02-28 17:56:02 +08001108 void __user *uaddr = vhost_vq_meta_fetch(vq,
1109 (u64)(uintptr_t)from, size,
1110 VHOST_ADDR_DESC);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001111 struct iov_iter f;
Jason Wangf8894912017-02-28 17:56:02 +08001112
1113 if (uaddr)
1114 return __copy_from_user(to, uaddr, size);
1115
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001116 ret = translate_desc(vq, (u64)(uintptr_t)from, size, vq->iotlb_iov,
1117 ARRAY_SIZE(vq->iotlb_iov),
1118 VHOST_ACCESS_RO);
1119 if (ret < 0) {
1120 vq_err(vq, "IOTLB translation failure: uaddr "
1121 "%p size 0x%llx\n", from,
1122 (unsigned long long) size);
1123 goto out;
1124 }
1125 iov_iter_init(&f, READ, vq->iotlb_iov, ret, size);
1126 ret = copy_from_iter(to, size, &f);
1127 if (ret == size)
1128 ret = 0;
1129 }
1130
1131out:
1132 return ret;
1133}
1134
Jason Wangf8894912017-02-28 17:56:02 +08001135static void __user *__vhost_get_user_slow(struct vhost_virtqueue *vq,
1136 void __user *addr, unsigned int size,
1137 int type)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001138{
1139 int ret;
1140
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001141 ret = translate_desc(vq, (u64)(uintptr_t)addr, size, vq->iotlb_iov,
1142 ARRAY_SIZE(vq->iotlb_iov),
1143 VHOST_ACCESS_RO);
1144 if (ret < 0) {
1145 vq_err(vq, "IOTLB translation failure: uaddr "
1146 "%p size 0x%llx\n", addr,
1147 (unsigned long long) size);
1148 return NULL;
1149 }
1150
1151 if (ret != 1 || vq->iotlb_iov[0].iov_len != size) {
1152 vq_err(vq, "Non atomic userspace memory access: uaddr "
1153 "%p size 0x%llx\n", addr,
1154 (unsigned long long) size);
1155 return NULL;
1156 }
1157
1158 return vq->iotlb_iov[0].iov_base;
1159}
1160
Jason Wangf8894912017-02-28 17:56:02 +08001161/* This function should be called after iotlb
1162 * prefetch, which means we're sure that vq
1163 * could be access through iotlb. So -EAGAIN should
1164 * not happen in this case.
1165 */
1166static inline void __user *__vhost_get_user(struct vhost_virtqueue *vq,
1167 void *addr, unsigned int size,
1168 int type)
1169{
1170 void __user *uaddr = vhost_vq_meta_fetch(vq,
1171 (u64)(uintptr_t)addr, size, type);
1172 if (uaddr)
1173 return uaddr;
1174
1175 return __vhost_get_user_slow(vq, addr, size, type);
1176}
1177
1178#define vhost_put_user(vq, x, ptr) \
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001179({ \
1180 int ret = -EFAULT; \
1181 if (!vq->iotlb) { \
1182 ret = __put_user(x, ptr); \
1183 } else { \
1184 __typeof__(ptr) to = \
Jason Wangf8894912017-02-28 17:56:02 +08001185 (__typeof__(ptr)) __vhost_get_user(vq, ptr, \
1186 sizeof(*ptr), VHOST_ADDR_USED); \
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001187 if (to != NULL) \
1188 ret = __put_user(x, to); \
1189 else \
1190 ret = -EFAULT; \
1191 } \
1192 ret; \
1193})
1194
Jason Wang7b5d7532019-05-24 04:12:14 -04001195static inline int vhost_put_avail_event(struct vhost_virtqueue *vq)
1196{
Jason Wang7f466032019-05-24 04:12:18 -04001197#if VHOST_ARCH_CAN_ACCEL_UACCESS
1198 struct vhost_map *map;
1199 struct vring_used *used;
1200
1201 if (!vq->iotlb) {
1202 rcu_read_lock();
1203
1204 map = rcu_dereference(vq->maps[VHOST_ADDR_USED]);
1205 if (likely(map)) {
1206 used = map->addr;
1207 *((__virtio16 *)&used->ring[vq->num]) =
1208 cpu_to_vhost16(vq, vq->avail_idx);
1209 rcu_read_unlock();
1210 return 0;
1211 }
1212
1213 rcu_read_unlock();
1214 }
1215#endif
1216
Jason Wang7b5d7532019-05-24 04:12:14 -04001217 return vhost_put_user(vq, cpu_to_vhost16(vq, vq->avail_idx),
1218 vhost_avail_event(vq));
1219}
1220
1221static inline int vhost_put_used(struct vhost_virtqueue *vq,
1222 struct vring_used_elem *head, int idx,
1223 int count)
1224{
Jason Wang7f466032019-05-24 04:12:18 -04001225#if VHOST_ARCH_CAN_ACCEL_UACCESS
1226 struct vhost_map *map;
1227 struct vring_used *used;
1228 size_t size;
1229
1230 if (!vq->iotlb) {
1231 rcu_read_lock();
1232
1233 map = rcu_dereference(vq->maps[VHOST_ADDR_USED]);
1234 if (likely(map)) {
1235 used = map->addr;
1236 size = count * sizeof(*head);
1237 memcpy(used->ring + idx, head, size);
1238 rcu_read_unlock();
1239 return 0;
1240 }
1241
1242 rcu_read_unlock();
1243 }
1244#endif
1245
Jason Wang7b5d7532019-05-24 04:12:14 -04001246 return vhost_copy_to_user(vq, vq->used->ring + idx, head,
1247 count * sizeof(*head));
1248}
1249
1250static inline int vhost_put_used_flags(struct vhost_virtqueue *vq)
1251
1252{
Jason Wang7f466032019-05-24 04:12:18 -04001253#if VHOST_ARCH_CAN_ACCEL_UACCESS
1254 struct vhost_map *map;
1255 struct vring_used *used;
1256
1257 if (!vq->iotlb) {
1258 rcu_read_lock();
1259
1260 map = rcu_dereference(vq->maps[VHOST_ADDR_USED]);
1261 if (likely(map)) {
1262 used = map->addr;
1263 used->flags = cpu_to_vhost16(vq, vq->used_flags);
1264 rcu_read_unlock();
1265 return 0;
1266 }
1267
1268 rcu_read_unlock();
1269 }
1270#endif
1271
Jason Wang7b5d7532019-05-24 04:12:14 -04001272 return vhost_put_user(vq, cpu_to_vhost16(vq, vq->used_flags),
1273 &vq->used->flags);
1274}
1275
1276static inline int vhost_put_used_idx(struct vhost_virtqueue *vq)
1277
1278{
Jason Wang7f466032019-05-24 04:12:18 -04001279#if VHOST_ARCH_CAN_ACCEL_UACCESS
1280 struct vhost_map *map;
1281 struct vring_used *used;
1282
1283 if (!vq->iotlb) {
1284 rcu_read_lock();
1285
1286 map = rcu_dereference(vq->maps[VHOST_ADDR_USED]);
1287 if (likely(map)) {
1288 used = map->addr;
1289 used->idx = cpu_to_vhost16(vq, vq->last_used_idx);
1290 rcu_read_unlock();
1291 return 0;
1292 }
1293
1294 rcu_read_unlock();
1295 }
1296#endif
1297
Jason Wang7b5d7532019-05-24 04:12:14 -04001298 return vhost_put_user(vq, cpu_to_vhost16(vq, vq->last_used_idx),
1299 &vq->used->idx);
1300}
1301
Jason Wangf8894912017-02-28 17:56:02 +08001302#define vhost_get_user(vq, x, ptr, type) \
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001303({ \
1304 int ret; \
1305 if (!vq->iotlb) { \
1306 ret = __get_user(x, ptr); \
1307 } else { \
1308 __typeof__(ptr) from = \
Jason Wangf8894912017-02-28 17:56:02 +08001309 (__typeof__(ptr)) __vhost_get_user(vq, ptr, \
1310 sizeof(*ptr), \
1311 type); \
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001312 if (from != NULL) \
1313 ret = __get_user(x, from); \
1314 else \
1315 ret = -EFAULT; \
1316 } \
1317 ret; \
1318})
1319
Jason Wangf8894912017-02-28 17:56:02 +08001320#define vhost_get_avail(vq, x, ptr) \
1321 vhost_get_user(vq, x, ptr, VHOST_ADDR_AVAIL)
1322
1323#define vhost_get_used(vq, x, ptr) \
1324 vhost_get_user(vq, x, ptr, VHOST_ADDR_USED)
1325
Jason Wang86a07da2018-12-13 10:53:39 +08001326static void vhost_dev_lock_vqs(struct vhost_dev *d)
1327{
1328 int i = 0;
1329 for (i = 0; i < d->nvqs; ++i)
1330 mutex_lock_nested(&d->vqs[i]->mutex, i);
1331}
1332
1333static void vhost_dev_unlock_vqs(struct vhost_dev *d)
1334{
1335 int i = 0;
1336 for (i = 0; i < d->nvqs; ++i)
1337 mutex_unlock(&d->vqs[i]->mutex);
1338}
1339
Jason Wang7b5d7532019-05-24 04:12:14 -04001340static inline int vhost_get_avail_idx(struct vhost_virtqueue *vq,
1341 __virtio16 *idx)
1342{
Jason Wang7f466032019-05-24 04:12:18 -04001343#if VHOST_ARCH_CAN_ACCEL_UACCESS
1344 struct vhost_map *map;
1345 struct vring_avail *avail;
1346
1347 if (!vq->iotlb) {
1348 rcu_read_lock();
1349
1350 map = rcu_dereference(vq->maps[VHOST_ADDR_AVAIL]);
1351 if (likely(map)) {
1352 avail = map->addr;
1353 *idx = avail->idx;
1354 rcu_read_unlock();
1355 return 0;
1356 }
1357
1358 rcu_read_unlock();
1359 }
1360#endif
1361
Jason Wang7b5d7532019-05-24 04:12:14 -04001362 return vhost_get_avail(vq, *idx, &vq->avail->idx);
1363}
1364
1365static inline int vhost_get_avail_head(struct vhost_virtqueue *vq,
1366 __virtio16 *head, int idx)
1367{
Jason Wang7f466032019-05-24 04:12:18 -04001368#if VHOST_ARCH_CAN_ACCEL_UACCESS
1369 struct vhost_map *map;
1370 struct vring_avail *avail;
1371
1372 if (!vq->iotlb) {
1373 rcu_read_lock();
1374
1375 map = rcu_dereference(vq->maps[VHOST_ADDR_AVAIL]);
1376 if (likely(map)) {
1377 avail = map->addr;
1378 *head = avail->ring[idx & (vq->num - 1)];
1379 rcu_read_unlock();
1380 return 0;
1381 }
1382
1383 rcu_read_unlock();
1384 }
1385#endif
1386
Jason Wang7b5d7532019-05-24 04:12:14 -04001387 return vhost_get_avail(vq, *head,
1388 &vq->avail->ring[idx & (vq->num - 1)]);
1389}
1390
1391static inline int vhost_get_avail_flags(struct vhost_virtqueue *vq,
1392 __virtio16 *flags)
1393{
Jason Wang7f466032019-05-24 04:12:18 -04001394#if VHOST_ARCH_CAN_ACCEL_UACCESS
1395 struct vhost_map *map;
1396 struct vring_avail *avail;
1397
1398 if (!vq->iotlb) {
1399 rcu_read_lock();
1400
1401 map = rcu_dereference(vq->maps[VHOST_ADDR_AVAIL]);
1402 if (likely(map)) {
1403 avail = map->addr;
1404 *flags = avail->flags;
1405 rcu_read_unlock();
1406 return 0;
1407 }
1408
1409 rcu_read_unlock();
1410 }
1411#endif
1412
Jason Wang7b5d7532019-05-24 04:12:14 -04001413 return vhost_get_avail(vq, *flags, &vq->avail->flags);
1414}
1415
1416static inline int vhost_get_used_event(struct vhost_virtqueue *vq,
1417 __virtio16 *event)
1418{
Jason Wang7f466032019-05-24 04:12:18 -04001419#if VHOST_ARCH_CAN_ACCEL_UACCESS
1420 struct vhost_map *map;
1421 struct vring_avail *avail;
1422
1423 if (!vq->iotlb) {
1424 rcu_read_lock();
1425 map = rcu_dereference(vq->maps[VHOST_ADDR_AVAIL]);
1426 if (likely(map)) {
1427 avail = map->addr;
1428 *event = (__virtio16)avail->ring[vq->num];
1429 rcu_read_unlock();
1430 return 0;
1431 }
1432 rcu_read_unlock();
1433 }
1434#endif
1435
Jason Wang7b5d7532019-05-24 04:12:14 -04001436 return vhost_get_avail(vq, *event, vhost_used_event(vq));
1437}
1438
1439static inline int vhost_get_used_idx(struct vhost_virtqueue *vq,
1440 __virtio16 *idx)
1441{
Jason Wang7f466032019-05-24 04:12:18 -04001442#if VHOST_ARCH_CAN_ACCEL_UACCESS
1443 struct vhost_map *map;
1444 struct vring_used *used;
1445
1446 if (!vq->iotlb) {
1447 rcu_read_lock();
1448
1449 map = rcu_dereference(vq->maps[VHOST_ADDR_USED]);
1450 if (likely(map)) {
1451 used = map->addr;
1452 *idx = used->idx;
1453 rcu_read_unlock();
1454 return 0;
1455 }
1456
1457 rcu_read_unlock();
1458 }
1459#endif
1460
Jason Wang7b5d7532019-05-24 04:12:14 -04001461 return vhost_get_used(vq, *idx, &vq->used->idx);
1462}
1463
1464static inline int vhost_get_desc(struct vhost_virtqueue *vq,
1465 struct vring_desc *desc, int idx)
1466{
Jason Wang7f466032019-05-24 04:12:18 -04001467#if VHOST_ARCH_CAN_ACCEL_UACCESS
1468 struct vhost_map *map;
1469 struct vring_desc *d;
1470
1471 if (!vq->iotlb) {
1472 rcu_read_lock();
1473
1474 map = rcu_dereference(vq->maps[VHOST_ADDR_DESC]);
1475 if (likely(map)) {
1476 d = map->addr;
1477 *desc = *(d + idx);
1478 rcu_read_unlock();
1479 return 0;
1480 }
1481
1482 rcu_read_unlock();
1483 }
1484#endif
1485
Jason Wang7b5d7532019-05-24 04:12:14 -04001486 return vhost_copy_from_user(vq, desc, vq->desc + idx, sizeof(*desc));
1487}
1488
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001489static int vhost_new_umem_range(struct vhost_umem *umem,
1490 u64 start, u64 size, u64 end,
1491 u64 userspace_addr, int perm)
1492{
Jason Wang813dbeb2019-04-09 12:10:25 +08001493 struct vhost_umem_node *tmp, *node;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001494
Jason Wang813dbeb2019-04-09 12:10:25 +08001495 if (!size)
1496 return -EFAULT;
1497
1498 node = kmalloc(sizeof(*node), GFP_ATOMIC);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001499 if (!node)
1500 return -ENOMEM;
1501
1502 if (umem->numem == max_iotlb_entries) {
1503 tmp = list_first_entry(&umem->umem_list, typeof(*tmp), link);
1504 vhost_umem_free(umem, tmp);
1505 }
1506
1507 node->start = start;
1508 node->size = size;
1509 node->last = end;
1510 node->userspace_addr = userspace_addr;
1511 node->perm = perm;
1512 INIT_LIST_HEAD(&node->link);
1513 list_add_tail(&node->link, &umem->umem_list);
1514 vhost_umem_interval_tree_insert(node, &umem->umem_tree);
1515 umem->numem++;
1516
1517 return 0;
1518}
1519
1520static void vhost_del_umem_range(struct vhost_umem *umem,
1521 u64 start, u64 end)
1522{
1523 struct vhost_umem_node *node;
1524
1525 while ((node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
1526 start, end)))
1527 vhost_umem_free(umem, node);
1528}
1529
1530static void vhost_iotlb_notify_vq(struct vhost_dev *d,
1531 struct vhost_iotlb_msg *msg)
1532{
1533 struct vhost_msg_node *node, *n;
1534
1535 spin_lock(&d->iotlb_lock);
1536
1537 list_for_each_entry_safe(node, n, &d->pending_list, node) {
1538 struct vhost_iotlb_msg *vq_msg = &node->msg.iotlb;
1539 if (msg->iova <= vq_msg->iova &&
Jason Wang2d66f992018-08-24 16:53:13 +08001540 msg->iova + msg->size - 1 >= vq_msg->iova &&
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001541 vq_msg->type == VHOST_IOTLB_MISS) {
1542 vhost_poll_queue(&node->vq->poll);
1543 list_del(&node->node);
1544 kfree(node);
1545 }
1546 }
1547
1548 spin_unlock(&d->iotlb_lock);
1549}
1550
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001551static bool umem_access_ok(u64 uaddr, u64 size, int access)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001552{
1553 unsigned long a = uaddr;
1554
Michael S. Tsirkinec33d032016-08-01 23:20:53 +03001555 /* Make sure 64 bit math will not overflow. */
1556 if (vhost_overflow(uaddr, size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001557 return false;
Michael S. Tsirkinec33d032016-08-01 23:20:53 +03001558
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001559 if ((access & VHOST_ACCESS_RO) &&
Linus Torvalds96d4f262019-01-03 18:57:57 -08001560 !access_ok((void __user *)a, size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001561 return false;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001562 if ((access & VHOST_ACCESS_WO) &&
Linus Torvalds96d4f262019-01-03 18:57:57 -08001563 !access_ok((void __user *)a, size))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001564 return false;
1565 return true;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001566}
1567
Michael S. Tsirkin72952cc2016-12-06 06:01:41 +02001568static int vhost_process_iotlb_msg(struct vhost_dev *dev,
1569 struct vhost_iotlb_msg *msg)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001570{
1571 int ret = 0;
1572
Jason Wang1b15ad62018-05-22 19:58:57 +08001573 mutex_lock(&dev->mutex);
Jason Wang86a07da2018-12-13 10:53:39 +08001574 vhost_dev_lock_vqs(dev);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001575 switch (msg->type) {
1576 case VHOST_IOTLB_UPDATE:
1577 if (!dev->iotlb) {
1578 ret = -EFAULT;
1579 break;
1580 }
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001581 if (!umem_access_ok(msg->uaddr, msg->size, msg->perm)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001582 ret = -EFAULT;
1583 break;
1584 }
Jason Wangf8894912017-02-28 17:56:02 +08001585 vhost_vq_meta_reset(dev);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001586 if (vhost_new_umem_range(dev->iotlb, msg->iova, msg->size,
1587 msg->iova + msg->size - 1,
1588 msg->uaddr, msg->perm)) {
1589 ret = -ENOMEM;
1590 break;
1591 }
1592 vhost_iotlb_notify_vq(dev, msg);
1593 break;
1594 case VHOST_IOTLB_INVALIDATE:
Jason Wang6f3180a2018-01-23 17:27:26 +08001595 if (!dev->iotlb) {
1596 ret = -EFAULT;
1597 break;
1598 }
Jason Wangf8894912017-02-28 17:56:02 +08001599 vhost_vq_meta_reset(dev);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001600 vhost_del_umem_range(dev->iotlb, msg->iova,
1601 msg->iova + msg->size - 1);
1602 break;
1603 default:
1604 ret = -EINVAL;
1605 break;
1606 }
1607
Jason Wang86a07da2018-12-13 10:53:39 +08001608 vhost_dev_unlock_vqs(dev);
Jason Wang1b15ad62018-05-22 19:58:57 +08001609 mutex_unlock(&dev->mutex);
1610
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001611 return ret;
1612}
1613ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
1614 struct iov_iter *from)
1615{
Jason Wang429711a2018-08-06 11:17:47 +08001616 struct vhost_iotlb_msg msg;
1617 size_t offset;
1618 int type, ret;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001619
Jason Wang429711a2018-08-06 11:17:47 +08001620 ret = copy_from_iter(&type, sizeof(type), from);
Pavel Tikhomirov74ad7412018-12-13 17:53:50 +03001621 if (ret != sizeof(type)) {
1622 ret = -EINVAL;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001623 goto done;
Pavel Tikhomirov74ad7412018-12-13 17:53:50 +03001624 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001625
Jason Wang429711a2018-08-06 11:17:47 +08001626 switch (type) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001627 case VHOST_IOTLB_MSG:
Jason Wang429711a2018-08-06 11:17:47 +08001628 /* There maybe a hole after type for V1 message type,
1629 * so skip it here.
1630 */
1631 offset = offsetof(struct vhost_msg, iotlb) - sizeof(int);
1632 break;
1633 case VHOST_IOTLB_MSG_V2:
1634 offset = sizeof(__u32);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001635 break;
1636 default:
1637 ret = -EINVAL;
Jason Wang429711a2018-08-06 11:17:47 +08001638 goto done;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001639 }
1640
Jason Wang429711a2018-08-06 11:17:47 +08001641 iov_iter_advance(from, offset);
1642 ret = copy_from_iter(&msg, sizeof(msg), from);
Pavel Tikhomirov74ad7412018-12-13 17:53:50 +03001643 if (ret != sizeof(msg)) {
1644 ret = -EINVAL;
Jason Wang429711a2018-08-06 11:17:47 +08001645 goto done;
Pavel Tikhomirov74ad7412018-12-13 17:53:50 +03001646 }
Jason Wang429711a2018-08-06 11:17:47 +08001647 if (vhost_process_iotlb_msg(dev, &msg)) {
1648 ret = -EFAULT;
1649 goto done;
1650 }
1651
1652 ret = (type == VHOST_IOTLB_MSG) ? sizeof(struct vhost_msg) :
1653 sizeof(struct vhost_msg_v2);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001654done:
1655 return ret;
1656}
1657EXPORT_SYMBOL(vhost_chr_write_iter);
1658
Al Viroafc9a422017-07-03 06:39:46 -04001659__poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001660 poll_table *wait)
1661{
Al Viroafc9a422017-07-03 06:39:46 -04001662 __poll_t mask = 0;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001663
1664 poll_wait(file, &dev->wait, wait);
1665
1666 if (!list_empty(&dev->read_list))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001667 mask |= EPOLLIN | EPOLLRDNORM;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001668
1669 return mask;
1670}
1671EXPORT_SYMBOL(vhost_chr_poll);
1672
1673ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to,
1674 int noblock)
1675{
1676 DEFINE_WAIT(wait);
1677 struct vhost_msg_node *node;
1678 ssize_t ret = 0;
1679 unsigned size = sizeof(struct vhost_msg);
1680
1681 if (iov_iter_count(to) < size)
1682 return 0;
1683
1684 while (1) {
1685 if (!noblock)
1686 prepare_to_wait(&dev->wait, &wait,
1687 TASK_INTERRUPTIBLE);
1688
1689 node = vhost_dequeue_msg(dev, &dev->read_list);
1690 if (node)
1691 break;
1692 if (noblock) {
1693 ret = -EAGAIN;
1694 break;
1695 }
1696 if (signal_pending(current)) {
1697 ret = -ERESTARTSYS;
1698 break;
1699 }
1700 if (!dev->iotlb) {
1701 ret = -EBADFD;
1702 break;
1703 }
1704
1705 schedule();
1706 }
1707
1708 if (!noblock)
1709 finish_wait(&dev->wait, &wait);
1710
1711 if (node) {
Jason Wang429711a2018-08-06 11:17:47 +08001712 struct vhost_iotlb_msg *msg;
1713 void *start = &node->msg;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001714
Jason Wang429711a2018-08-06 11:17:47 +08001715 switch (node->msg.type) {
1716 case VHOST_IOTLB_MSG:
1717 size = sizeof(node->msg);
1718 msg = &node->msg.iotlb;
1719 break;
1720 case VHOST_IOTLB_MSG_V2:
1721 size = sizeof(node->msg_v2);
1722 msg = &node->msg_v2.iotlb;
1723 break;
1724 default:
1725 BUG();
1726 break;
1727 }
1728
1729 ret = copy_to_iter(start, size, to);
1730 if (ret != size || msg->type != VHOST_IOTLB_MISS) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001731 kfree(node);
1732 return ret;
1733 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001734 vhost_enqueue_msg(dev, &dev->pending_list, node);
1735 }
1736
1737 return ret;
1738}
1739EXPORT_SYMBOL_GPL(vhost_chr_read_iter);
1740
1741static int vhost_iotlb_miss(struct vhost_virtqueue *vq, u64 iova, int access)
1742{
1743 struct vhost_dev *dev = vq->dev;
1744 struct vhost_msg_node *node;
1745 struct vhost_iotlb_msg *msg;
Jason Wang429711a2018-08-06 11:17:47 +08001746 bool v2 = vhost_backend_has_feature(vq, VHOST_BACKEND_F_IOTLB_MSG_V2);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001747
Jason Wang429711a2018-08-06 11:17:47 +08001748 node = vhost_new_msg(vq, v2 ? VHOST_IOTLB_MSG_V2 : VHOST_IOTLB_MSG);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001749 if (!node)
1750 return -ENOMEM;
1751
Jason Wang429711a2018-08-06 11:17:47 +08001752 if (v2) {
1753 node->msg_v2.type = VHOST_IOTLB_MSG_V2;
1754 msg = &node->msg_v2.iotlb;
1755 } else {
1756 msg = &node->msg.iotlb;
1757 }
1758
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001759 msg->type = VHOST_IOTLB_MISS;
1760 msg->iova = iova;
1761 msg->perm = access;
1762
1763 vhost_enqueue_msg(dev, &dev->read_list, node);
1764
1765 return 0;
Jason Wangbfe2bc52016-06-23 02:04:30 -04001766}
1767
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001768static bool vq_access_ok(struct vhost_virtqueue *vq, unsigned int num,
1769 struct vring_desc __user *desc,
1770 struct vring_avail __user *avail,
1771 struct vring_used __user *used)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001772
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001773{
Jason Wang4942e822019-05-24 04:12:16 -04001774 return access_ok(desc, vhost_get_desc_size(vq, num)) &&
1775 access_ok(avail, vhost_get_avail_size(vq, num)) &&
1776 access_ok(used, vhost_get_used_size(vq, num));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001777}
1778
Jason Wangf8894912017-02-28 17:56:02 +08001779static void vhost_vq_meta_update(struct vhost_virtqueue *vq,
1780 const struct vhost_umem_node *node,
1781 int type)
1782{
1783 int access = (type == VHOST_ADDR_USED) ?
1784 VHOST_ACCESS_WO : VHOST_ACCESS_RO;
1785
1786 if (likely(node->perm & access))
1787 vq->meta_iotlb[type] = node;
1788}
1789
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001790static bool iotlb_access_ok(struct vhost_virtqueue *vq,
1791 int access, u64 addr, u64 len, int type)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001792{
1793 const struct vhost_umem_node *node;
1794 struct vhost_umem *umem = vq->iotlb;
Michael S. Tsirkinca2c5b32017-08-21 22:33:33 +03001795 u64 s = 0, size, orig_addr = addr, last = addr + len - 1;
Jason Wangf8894912017-02-28 17:56:02 +08001796
1797 if (vhost_vq_meta_fetch(vq, addr, len, type))
1798 return true;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001799
1800 while (len > s) {
1801 node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
1802 addr,
Michael S. Tsirkinca2c5b32017-08-21 22:33:33 +03001803 last);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001804 if (node == NULL || node->start > addr) {
1805 vhost_iotlb_miss(vq, addr, access);
1806 return false;
1807 } else if (!(node->perm & access)) {
1808 /* Report the possible access violation by
1809 * request another translation from userspace.
1810 */
1811 return false;
1812 }
1813
1814 size = node->size - addr + node->start;
Jason Wangf8894912017-02-28 17:56:02 +08001815
1816 if (orig_addr == addr && size >= len)
1817 vhost_vq_meta_update(vq, node, type);
1818
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001819 s += size;
1820 addr += size;
1821 }
1822
1823 return true;
1824}
1825
Jason Wang7f466032019-05-24 04:12:18 -04001826#if VHOST_ARCH_CAN_ACCEL_UACCESS
1827static void vhost_vq_map_prefetch(struct vhost_virtqueue *vq)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001828{
Jason Wang7f466032019-05-24 04:12:18 -04001829 struct vhost_map __rcu *map;
1830 int i;
1831
1832 for (i = 0; i < VHOST_NUM_ADDRS; i++) {
1833 rcu_read_lock();
1834 map = rcu_dereference(vq->maps[i]);
1835 rcu_read_unlock();
1836 if (unlikely(!map))
1837 vhost_map_prefetch(vq, i);
1838 }
1839}
1840#endif
1841
Jason Wang9b5e8302019-05-24 04:12:15 -04001842int vq_meta_prefetch(struct vhost_virtqueue *vq)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001843{
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001844 unsigned int num = vq->num;
1845
Jason Wang7f466032019-05-24 04:12:18 -04001846 if (!vq->iotlb) {
1847#if VHOST_ARCH_CAN_ACCEL_UACCESS
1848 vhost_vq_map_prefetch(vq);
1849#endif
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001850 return 1;
Jason Wang7f466032019-05-24 04:12:18 -04001851 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001852
1853 return iotlb_access_ok(vq, VHOST_ACCESS_RO, (u64)(uintptr_t)vq->desc,
Jason Wang4942e822019-05-24 04:12:16 -04001854 vhost_get_desc_size(vq, num), VHOST_ADDR_DESC) &&
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001855 iotlb_access_ok(vq, VHOST_ACCESS_RO, (u64)(uintptr_t)vq->avail,
Jason Wang4942e822019-05-24 04:12:16 -04001856 vhost_get_avail_size(vq, num),
Jason Wangf8894912017-02-28 17:56:02 +08001857 VHOST_ADDR_AVAIL) &&
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001858 iotlb_access_ok(vq, VHOST_ACCESS_WO, (u64)(uintptr_t)vq->used,
Jason Wang4942e822019-05-24 04:12:16 -04001859 vhost_get_used_size(vq, num), VHOST_ADDR_USED);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001860}
Jason Wang9b5e8302019-05-24 04:12:15 -04001861EXPORT_SYMBOL_GPL(vq_meta_prefetch);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001862
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001863/* Can we log writes? */
1864/* Caller should have device mutex but not vq mutex */
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001865bool vhost_log_access_ok(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001866{
Jason Wanga9709d62016-06-23 02:04:31 -04001867 return memory_access_ok(dev, dev->umem, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001868}
Asias He6ac1afb2013-05-06 16:38:21 +08001869EXPORT_SYMBOL_GPL(vhost_log_access_ok);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001870
1871/* Verify access for write logging. */
1872/* Caller should have vq mutex and device mutex */
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001873static bool vq_log_access_ok(struct vhost_virtqueue *vq,
1874 void __user *log_base)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001875{
Jason Wanga9709d62016-06-23 02:04:31 -04001876 return vq_memory_access_ok(log_base, vq->umem,
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001877 vhost_has_feature(vq, VHOST_F_LOG_ALL)) &&
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001878 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
Jason Wang4942e822019-05-24 04:12:16 -04001879 vhost_get_used_size(vq, vq->num)));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001880}
1881
1882/* Can we start vq? */
1883/* Caller should have vq mutex and device mutex */
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001884bool vhost_vq_access_ok(struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001885{
Stefan Hajnoczid14d2b72018-04-11 10:35:40 +08001886 if (!vq_log_access_ok(vq, vq->log_base))
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001887 return false;
Jason Wangd65026c2018-03-29 16:00:04 +08001888
Stefan Hajnoczid14d2b72018-04-11 10:35:40 +08001889 /* Access validation occurs at prefetch time with IOTLB */
1890 if (vq->iotlb)
Stefan Hajnocziddd3d402018-04-11 10:35:41 +08001891 return true;
Jason Wangd65026c2018-03-29 16:00:04 +08001892
1893 return vq_access_ok(vq, vq->num, vq->desc, vq->avail, vq->used);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001894}
Asias He6ac1afb2013-05-06 16:38:21 +08001895EXPORT_SYMBOL_GPL(vhost_vq_access_ok);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001896
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001897static struct vhost_umem *vhost_umem_alloc(void)
1898{
Michal Hocko6c5ab652017-05-08 15:57:15 -07001899 struct vhost_umem *umem = kvzalloc(sizeof(*umem), GFP_KERNEL);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001900
1901 if (!umem)
1902 return NULL;
1903
Davidlohr Buesof808c132017-09-08 16:15:08 -07001904 umem->umem_tree = RB_ROOT_CACHED;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001905 umem->numem = 0;
1906 INIT_LIST_HEAD(&umem->umem_list);
1907
1908 return umem;
1909}
1910
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001911static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
1912{
Jason Wanga9709d62016-06-23 02:04:31 -04001913 struct vhost_memory mem, *newmem;
1914 struct vhost_memory_region *region;
Jason Wanga9709d62016-06-23 02:04:31 -04001915 struct vhost_umem *newumem, *oldumem;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001916 unsigned long size = offsetof(struct vhost_memory, regions);
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001917 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +05301918
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001919 if (copy_from_user(&mem, m, size))
1920 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001921 if (mem.padding)
1922 return -EOPNOTSUPP;
Igor Mammedovc9ce42f2015-07-02 15:08:11 +02001923 if (mem.nregions > max_mem_regions)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001924 return -E2BIG;
Matthew Wilcoxb2303d72018-06-07 07:57:18 -07001925 newmem = kvzalloc(struct_size(newmem, regions, mem.nregions),
1926 GFP_KERNEL);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001927 if (!newmem)
1928 return -ENOMEM;
1929
1930 memcpy(newmem, &mem, size);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001931 if (copy_from_user(newmem->regions, m->regions,
1932 mem.nregions * sizeof *m->regions)) {
Igor Mammedovbcfeaca2015-06-16 18:33:35 +02001933 kvfree(newmem);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09001934 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001935 }
1936
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001937 newumem = vhost_umem_alloc();
Jason Wanga9709d62016-06-23 02:04:31 -04001938 if (!newumem) {
Igor Mammedov4de72552015-07-01 11:07:09 +02001939 kvfree(newmem);
Jason Wanga9709d62016-06-23 02:04:31 -04001940 return -ENOMEM;
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +09001941 }
Jason Wanga9709d62016-06-23 02:04:31 -04001942
Jason Wanga9709d62016-06-23 02:04:31 -04001943 for (region = newmem->regions;
1944 region < newmem->regions + mem.nregions;
1945 region++) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001946 if (vhost_new_umem_range(newumem,
1947 region->guest_phys_addr,
1948 region->memory_size,
1949 region->guest_phys_addr +
1950 region->memory_size - 1,
1951 region->userspace_addr,
1952 VHOST_ACCESS_RW))
Jason Wanga9709d62016-06-23 02:04:31 -04001953 goto err;
Jason Wanga9709d62016-06-23 02:04:31 -04001954 }
1955
1956 if (!memory_access_ok(d, newumem, 0))
1957 goto err;
1958
1959 oldumem = d->umem;
1960 d->umem = newumem;
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001961
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001962 /* All memory accesses are done under some VQ mutex. */
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001963 for (i = 0; i < d->nvqs; ++i) {
1964 mutex_lock(&d->vqs[i]->mutex);
Jason Wanga9709d62016-06-23 02:04:31 -04001965 d->vqs[i]->umem = newumem;
Michael S. Tsirkin98f9ca02014-05-28 17:07:02 +03001966 mutex_unlock(&d->vqs[i]->mutex);
1967 }
Jason Wanga9709d62016-06-23 02:04:31 -04001968
1969 kvfree(newmem);
1970 vhost_umem_clean(oldumem);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001971 return 0;
Jason Wanga9709d62016-06-23 02:04:31 -04001972
1973err:
1974 vhost_umem_clean(newumem);
1975 kvfree(newmem);
1976 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001977}
1978
Jason Wangfeebcae2019-05-24 04:12:17 -04001979static long vhost_vring_set_num(struct vhost_dev *d,
1980 struct vhost_virtqueue *vq,
1981 void __user *argp)
1982{
1983 struct vhost_vring_state s;
1984
1985 /* Resizing ring with an active backend?
1986 * You don't want to do that. */
1987 if (vq->private_data)
1988 return -EBUSY;
1989
1990 if (copy_from_user(&s, argp, sizeof s))
1991 return -EFAULT;
1992
1993 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1)))
1994 return -EINVAL;
1995 vq->num = s.num;
1996
1997 return 0;
1998}
1999
2000static long vhost_vring_set_addr(struct vhost_dev *d,
2001 struct vhost_virtqueue *vq,
2002 void __user *argp)
2003{
2004 struct vhost_vring_addr a;
2005
2006 if (copy_from_user(&a, argp, sizeof a))
2007 return -EFAULT;
2008 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG))
2009 return -EOPNOTSUPP;
2010
2011 /* For 32bit, verify that the top 32bits of the user
2012 data are set to zero. */
2013 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
2014 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
2015 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr)
2016 return -EFAULT;
2017
2018 /* Make sure it's safe to cast pointers to vring types. */
2019 BUILD_BUG_ON(__alignof__ *vq->avail > VRING_AVAIL_ALIGN_SIZE);
2020 BUILD_BUG_ON(__alignof__ *vq->used > VRING_USED_ALIGN_SIZE);
2021 if ((a.avail_user_addr & (VRING_AVAIL_ALIGN_SIZE - 1)) ||
2022 (a.used_user_addr & (VRING_USED_ALIGN_SIZE - 1)) ||
2023 (a.log_guest_addr & (VRING_USED_ALIGN_SIZE - 1)))
2024 return -EINVAL;
2025
2026 /* We only verify access here if backend is configured.
2027 * If it is not, we don't as size might not have been setup.
2028 * We will verify when backend is configured. */
2029 if (vq->private_data) {
2030 if (!vq_access_ok(vq, vq->num,
2031 (void __user *)(unsigned long)a.desc_user_addr,
2032 (void __user *)(unsigned long)a.avail_user_addr,
2033 (void __user *)(unsigned long)a.used_user_addr))
2034 return -EINVAL;
2035
2036 /* Also validate log access for used ring if enabled. */
2037 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
2038 !log_access_ok(vq->log_base, a.log_guest_addr,
2039 sizeof *vq->used +
2040 vq->num * sizeof *vq->used->ring))
2041 return -EINVAL;
2042 }
2043
2044 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
2045 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
2046 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
2047 vq->log_addr = a.log_guest_addr;
2048 vq->used = (void __user *)(unsigned long)a.used_user_addr;
2049
2050 return 0;
2051}
2052
2053static long vhost_vring_set_num_addr(struct vhost_dev *d,
2054 struct vhost_virtqueue *vq,
2055 unsigned int ioctl,
2056 void __user *argp)
2057{
2058 long r;
2059
2060 mutex_lock(&vq->mutex);
2061
Jason Wang7f466032019-05-24 04:12:18 -04002062#if VHOST_ARCH_CAN_ACCEL_UACCESS
2063 /* Unregister MMU notifer to allow invalidation callback
2064 * can access vq->uaddrs[] without holding a lock.
2065 */
2066 if (d->mm)
2067 mmu_notifier_unregister(&d->mmu_notifier, d->mm);
2068
2069 vhost_uninit_vq_maps(vq);
2070#endif
2071
Jason Wangfeebcae2019-05-24 04:12:17 -04002072 switch (ioctl) {
2073 case VHOST_SET_VRING_NUM:
2074 r = vhost_vring_set_num(d, vq, argp);
2075 break;
2076 case VHOST_SET_VRING_ADDR:
2077 r = vhost_vring_set_addr(d, vq, argp);
2078 break;
2079 default:
2080 BUG();
2081 }
2082
Jason Wang7f466032019-05-24 04:12:18 -04002083#if VHOST_ARCH_CAN_ACCEL_UACCESS
2084 vhost_setup_vq_uaddr(vq);
2085
2086 if (d->mm)
2087 mmu_notifier_register(&d->mmu_notifier, d->mm);
2088#endif
2089
Jason Wangfeebcae2019-05-24 04:12:17 -04002090 mutex_unlock(&vq->mutex);
2091
2092 return r;
2093}
Sonny Rao26b36602018-03-14 10:05:06 -07002094long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002095{
Al Virocecb46f2012-08-27 14:21:39 -04002096 struct file *eventfp, *filep = NULL;
2097 bool pollstart = false, pollstop = false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002098 struct eventfd_ctx *ctx = NULL;
2099 u32 __user *idxp = argp;
2100 struct vhost_virtqueue *vq;
2101 struct vhost_vring_state s;
2102 struct vhost_vring_file f;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002103 u32 idx;
2104 long r;
2105
2106 r = get_user(idx, idxp);
2107 if (r < 0)
2108 return r;
Krishna Kumar0f3d9a12010-05-25 11:10:36 +05302109 if (idx >= d->nvqs)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002110 return -ENOBUFS;
2111
Jason Wangff002262018-10-30 14:10:49 +08002112 idx = array_index_nospec(idx, d->nvqs);
Asias He3ab2e422013-04-27 11:16:48 +08002113 vq = d->vqs[idx];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002114
Jason Wangfeebcae2019-05-24 04:12:17 -04002115 if (ioctl == VHOST_SET_VRING_NUM ||
2116 ioctl == VHOST_SET_VRING_ADDR) {
2117 return vhost_vring_set_num_addr(d, vq, ioctl, argp);
2118 }
2119
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002120 mutex_lock(&vq->mutex);
2121
2122 switch (ioctl) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002123 case VHOST_SET_VRING_BASE:
2124 /* Moving base with an active backend?
2125 * You don't want to do that. */
2126 if (vq->private_data) {
2127 r = -EBUSY;
2128 break;
2129 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09002130 if (copy_from_user(&s, argp, sizeof s)) {
2131 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002132 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09002133 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002134 if (s.num > 0xffff) {
2135 r = -EINVAL;
2136 break;
2137 }
Jason Wang8d658432017-07-27 11:22:05 +08002138 vq->last_avail_idx = s.num;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002139 /* Forget the cached index value. */
2140 vq->avail_idx = vq->last_avail_idx;
2141 break;
2142 case VHOST_GET_VRING_BASE:
2143 s.index = idx;
2144 s.num = vq->last_avail_idx;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09002145 if (copy_to_user(argp, &s, sizeof s))
2146 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002147 break;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002148 case VHOST_SET_VRING_KICK:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09002149 if (copy_from_user(&f, argp, sizeof f)) {
2150 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002151 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09002152 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002153 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +02002154 if (IS_ERR(eventfp)) {
2155 r = PTR_ERR(eventfp);
2156 break;
2157 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002158 if (eventfp != vq->kick) {
Al Virocecb46f2012-08-27 14:21:39 -04002159 pollstop = (filep = vq->kick) != NULL;
2160 pollstart = (vq->kick = eventfp) != NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002161 } else
2162 filep = eventfp;
2163 break;
2164 case VHOST_SET_VRING_CALL:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09002165 if (copy_from_user(&f, argp, sizeof f)) {
2166 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002167 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09002168 }
Eric Biggerse050c7d2018-01-06 14:52:19 -08002169 ctx = f.fd == -1 ? NULL : eventfd_ctx_fdget(f.fd);
2170 if (IS_ERR(ctx)) {
2171 r = PTR_ERR(ctx);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +02002172 break;
2173 }
Eric Biggerse050c7d2018-01-06 14:52:19 -08002174 swap(ctx, vq->call_ctx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002175 break;
2176 case VHOST_SET_VRING_ERR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09002177 if (copy_from_user(&f, argp, sizeof f)) {
2178 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002179 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09002180 }
Eric Biggers09f332a2018-01-06 14:52:20 -08002181 ctx = f.fd == -1 ? NULL : eventfd_ctx_fdget(f.fd);
2182 if (IS_ERR(ctx)) {
2183 r = PTR_ERR(ctx);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +02002184 break;
2185 }
Eric Biggers09f332a2018-01-06 14:52:20 -08002186 swap(ctx, vq->error_ctx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002187 break;
Greg Kurz2751c982015-04-24 14:27:24 +02002188 case VHOST_SET_VRING_ENDIAN:
2189 r = vhost_set_vring_endian(vq, argp);
2190 break;
2191 case VHOST_GET_VRING_ENDIAN:
2192 r = vhost_get_vring_endian(vq, idx, argp);
2193 break;
Jason Wang03088132016-03-04 06:24:53 -05002194 case VHOST_SET_VRING_BUSYLOOP_TIMEOUT:
2195 if (copy_from_user(&s, argp, sizeof(s))) {
2196 r = -EFAULT;
2197 break;
2198 }
2199 vq->busyloop_timeout = s.num;
2200 break;
2201 case VHOST_GET_VRING_BUSYLOOP_TIMEOUT:
2202 s.index = idx;
2203 s.num = vq->busyloop_timeout;
2204 if (copy_to_user(argp, &s, sizeof(s)))
2205 r = -EFAULT;
2206 break;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002207 default:
2208 r = -ENOIOCTLCMD;
2209 }
2210
2211 if (pollstop && vq->handle_kick)
2212 vhost_poll_stop(&vq->poll);
2213
Eric Biggerse050c7d2018-01-06 14:52:19 -08002214 if (!IS_ERR_OR_NULL(ctx))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002215 eventfd_ctx_put(ctx);
2216 if (filep)
2217 fput(filep);
2218
2219 if (pollstart && vq->handle_kick)
Jason Wang2b8b3282013-01-28 01:05:18 +00002220 r = vhost_poll_start(&vq->poll, vq->kick);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002221
2222 mutex_unlock(&vq->mutex);
2223
2224 if (pollstop && vq->handle_kick)
2225 vhost_poll_flush(&vq->poll);
2226 return r;
2227}
Asias He6ac1afb2013-05-06 16:38:21 +08002228EXPORT_SYMBOL_GPL(vhost_vring_ioctl);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002229
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002230int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled)
2231{
2232 struct vhost_umem *niotlb, *oiotlb;
2233 int i;
2234
2235 niotlb = vhost_umem_alloc();
2236 if (!niotlb)
2237 return -ENOMEM;
2238
2239 oiotlb = d->iotlb;
2240 d->iotlb = niotlb;
2241
2242 for (i = 0; i < d->nvqs; ++i) {
Jason Wangb13f9c62018-08-08 11:43:04 +08002243 struct vhost_virtqueue *vq = d->vqs[i];
2244
2245 mutex_lock(&vq->mutex);
2246 vq->iotlb = niotlb;
2247 __vhost_vq_meta_reset(vq);
2248 mutex_unlock(&vq->mutex);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002249 }
2250
2251 vhost_umem_clean(oiotlb);
2252
2253 return 0;
2254}
2255EXPORT_SYMBOL_GPL(vhost_init_device_iotlb);
2256
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002257/* Caller must have device mutex */
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02002258long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002259{
Eric Biggersd25cc432018-01-06 14:52:21 -08002260 struct eventfd_ctx *ctx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002261 u64 p;
2262 long r;
2263 int i, fd;
2264
2265 /* If you are not the owner, you can become one */
2266 if (ioctl == VHOST_SET_OWNER) {
2267 r = vhost_dev_set_owner(d);
2268 goto done;
2269 }
2270
2271 /* You must be the owner to do anything else */
2272 r = vhost_dev_check_owner(d);
2273 if (r)
2274 goto done;
2275
2276 switch (ioctl) {
2277 case VHOST_SET_MEM_TABLE:
2278 r = vhost_set_memory(d, argp);
2279 break;
2280 case VHOST_SET_LOG_BASE:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09002281 if (copy_from_user(&p, argp, sizeof p)) {
2282 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002283 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +09002284 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002285 if ((u64)(unsigned long)p != p) {
2286 r = -EFAULT;
2287 break;
2288 }
2289 for (i = 0; i < d->nvqs; ++i) {
2290 struct vhost_virtqueue *vq;
2291 void __user *base = (void __user *)(unsigned long)p;
Asias He3ab2e422013-04-27 11:16:48 +08002292 vq = d->vqs[i];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002293 mutex_lock(&vq->mutex);
2294 /* If ring is inactive, will check when it's enabled. */
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002295 if (vq->private_data && !vq_log_access_ok(vq, base))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002296 r = -EFAULT;
2297 else
2298 vq->log_base = base;
2299 mutex_unlock(&vq->mutex);
2300 }
2301 break;
2302 case VHOST_SET_LOG_FD:
2303 r = get_user(fd, (int __user *)argp);
2304 if (r < 0)
2305 break;
Eric Biggersd25cc432018-01-06 14:52:21 -08002306 ctx = fd == -1 ? NULL : eventfd_ctx_fdget(fd);
2307 if (IS_ERR(ctx)) {
2308 r = PTR_ERR(ctx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002309 break;
2310 }
Eric Biggersd25cc432018-01-06 14:52:21 -08002311 swap(ctx, d->log_ctx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002312 for (i = 0; i < d->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +08002313 mutex_lock(&d->vqs[i]->mutex);
2314 d->vqs[i]->log_ctx = d->log_ctx;
2315 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002316 }
2317 if (ctx)
2318 eventfd_ctx_put(ctx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002319 break;
2320 default:
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02002321 r = -ENOIOCTLCMD;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002322 break;
2323 }
2324done:
2325 return r;
2326}
Asias He6ac1afb2013-05-06 16:38:21 +08002327EXPORT_SYMBOL_GPL(vhost_dev_ioctl);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002328
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002329/* TODO: This is really inefficient. We need something like get_user()
2330 * (instruction directly accesses the data, with an exception table entry
Mauro Carvalho Chehabcb1aaeb2019-06-07 15:54:32 -03002331 * returning -EFAULT). See Documentation/x86/exception-tables.rst.
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002332 */
2333static int set_bit_to_user(int nr, void __user *addr)
2334{
2335 unsigned long log = (unsigned long)addr;
2336 struct page *page;
2337 void *base;
2338 int bit = nr + (log % PAGE_SIZE) * 8;
2339 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05302340
Ira Weiny73b01402019-05-13 17:17:11 -07002341 r = get_user_pages_fast(log, 1, FOLL_WRITE, &page);
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +02002342 if (r < 0)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002343 return r;
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +02002344 BUG_ON(r != 1);
Cong Wangc6daa7f2011-11-25 23:14:26 +08002345 base = kmap_atomic(page);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002346 set_bit(bit, base);
Cong Wangc6daa7f2011-11-25 23:14:26 +08002347 kunmap_atomic(base);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002348 set_page_dirty_lock(page);
2349 put_page(page);
2350 return 0;
2351}
2352
2353static int log_write(void __user *log_base,
2354 u64 write_address, u64 write_length)
2355{
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02002356 u64 write_page = write_address / VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002357 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05302358
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002359 if (!write_length)
2360 return 0;
Michael S. Tsirkin3bf9be42010-11-29 10:19:07 +02002361 write_length += write_address % VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002362 for (;;) {
2363 u64 base = (u64)(unsigned long)log_base;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02002364 u64 log = base + write_page / 8;
2365 int bit = write_page % 8;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002366 if ((u64)(unsigned long)log != log)
2367 return -EFAULT;
2368 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
2369 if (r < 0)
2370 return r;
2371 if (write_length <= VHOST_PAGE_SIZE)
2372 break;
2373 write_length -= VHOST_PAGE_SIZE;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +02002374 write_page += 1;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002375 }
2376 return r;
2377}
2378
Jason Wangcc5e7102019-01-16 16:54:42 +08002379static int log_write_hva(struct vhost_virtqueue *vq, u64 hva, u64 len)
2380{
2381 struct vhost_umem *umem = vq->umem;
2382 struct vhost_umem_node *u;
2383 u64 start, end, l, min;
2384 int r;
2385 bool hit = false;
2386
2387 while (len) {
2388 min = len;
2389 /* More than one GPAs can be mapped into a single HVA. So
2390 * iterate all possible umems here to be safe.
2391 */
2392 list_for_each_entry(u, &umem->umem_list, link) {
2393 if (u->userspace_addr > hva - 1 + len ||
2394 u->userspace_addr - 1 + u->size < hva)
2395 continue;
2396 start = max(u->userspace_addr, hva);
2397 end = min(u->userspace_addr - 1 + u->size,
2398 hva - 1 + len);
2399 l = end - start + 1;
2400 r = log_write(vq->log_base,
2401 u->start + start - u->userspace_addr,
2402 l);
2403 if (r < 0)
2404 return r;
2405 hit = true;
2406 min = min(l, min);
2407 }
2408
2409 if (!hit)
2410 return -EFAULT;
2411
2412 len -= min;
2413 hva += min;
2414 }
2415
2416 return 0;
2417}
2418
2419static int log_used(struct vhost_virtqueue *vq, u64 used_offset, u64 len)
2420{
2421 struct iovec iov[64];
2422 int i, ret;
2423
2424 if (!vq->iotlb)
2425 return log_write(vq->log_base, vq->log_addr + used_offset, len);
2426
2427 ret = translate_desc(vq, (uintptr_t)vq->used + used_offset,
2428 len, iov, 64, VHOST_ACCESS_WO);
Jason Wang816db762019-02-19 14:53:44 +08002429 if (ret < 0)
Jason Wangcc5e7102019-01-16 16:54:42 +08002430 return ret;
2431
2432 for (i = 0; i < ret; i++) {
2433 ret = log_write_hva(vq, (uintptr_t)iov[i].iov_base,
2434 iov[i].iov_len);
2435 if (ret)
2436 return ret;
2437 }
2438
2439 return 0;
2440}
2441
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002442int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
Jason Wangcc5e7102019-01-16 16:54:42 +08002443 unsigned int log_num, u64 len, struct iovec *iov, int count)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002444{
2445 int i, r;
2446
2447 /* Make sure data written is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00002448 smp_wmb();
Jason Wangcc5e7102019-01-16 16:54:42 +08002449
2450 if (vq->iotlb) {
2451 for (i = 0; i < count; i++) {
2452 r = log_write_hva(vq, (uintptr_t)iov[i].iov_base,
2453 iov[i].iov_len);
2454 if (r < 0)
2455 return r;
2456 }
2457 return 0;
2458 }
2459
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002460 for (i = 0; i < log_num; ++i) {
2461 u64 l = min(log[i].len, len);
2462 r = log_write(vq->log_base, log[i].addr, l);
2463 if (r < 0)
2464 return r;
2465 len -= l;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +02002466 if (!len) {
2467 if (vq->log_ctx)
2468 eventfd_signal(vq->log_ctx, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002469 return 0;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +02002470 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002471 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002472 /* Length written exceeds what we have stored. This is a bug. */
2473 BUG();
2474 return 0;
2475}
Asias He6ac1afb2013-05-06 16:38:21 +08002476EXPORT_SYMBOL_GPL(vhost_log_write);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002477
Jason Wang2723fea2011-06-21 18:04:38 +08002478static int vhost_update_used_flags(struct vhost_virtqueue *vq)
2479{
2480 void __user *used;
Jason Wang7b5d7532019-05-24 04:12:14 -04002481 if (vhost_put_used_flags(vq))
Jason Wang2723fea2011-06-21 18:04:38 +08002482 return -EFAULT;
2483 if (unlikely(vq->log_used)) {
2484 /* Make sure the flag is seen before log. */
2485 smp_wmb();
2486 /* Log used flag write. */
2487 used = &vq->used->flags;
Jason Wangcc5e7102019-01-16 16:54:42 +08002488 log_used(vq, (used - (void __user *)vq->used),
2489 sizeof vq->used->flags);
Jason Wang2723fea2011-06-21 18:04:38 +08002490 if (vq->log_ctx)
2491 eventfd_signal(vq->log_ctx, 1);
2492 }
2493 return 0;
2494}
2495
2496static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
2497{
Jason Wang7b5d7532019-05-24 04:12:14 -04002498 if (vhost_put_avail_event(vq))
Jason Wang2723fea2011-06-21 18:04:38 +08002499 return -EFAULT;
2500 if (unlikely(vq->log_used)) {
2501 void __user *used;
2502 /* Make sure the event is seen before log. */
2503 smp_wmb();
2504 /* Log avail event write */
2505 used = vhost_avail_event(vq);
Jason Wangcc5e7102019-01-16 16:54:42 +08002506 log_used(vq, (used - (void __user *)vq->used),
2507 sizeof *vhost_avail_event(vq));
Jason Wang2723fea2011-06-21 18:04:38 +08002508 if (vq->log_ctx)
2509 eventfd_signal(vq->log_ctx, 1);
2510 }
2511 return 0;
2512}
2513
Greg Kurz80f7d032016-02-16 15:59:44 +01002514int vhost_vq_init_access(struct vhost_virtqueue *vq)
Jason Wang2723fea2011-06-21 18:04:38 +08002515{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002516 __virtio16 last_used_idx;
Jason Wang2723fea2011-06-21 18:04:38 +08002517 int r;
Greg Kurze1f33be2016-02-16 15:54:28 +01002518 bool is_le = vq->is_le;
2519
Halil Pasiccda8bba2017-01-30 11:09:36 +01002520 if (!vq->private_data)
Jason Wang2723fea2011-06-21 18:04:38 +08002521 return 0;
Greg Kurz2751c982015-04-24 14:27:24 +02002522
2523 vhost_init_is_le(vq);
Jason Wang2723fea2011-06-21 18:04:38 +08002524
2525 r = vhost_update_used_flags(vq);
2526 if (r)
Greg Kurze1f33be2016-02-16 15:54:28 +01002527 goto err;
Jason Wang2723fea2011-06-21 18:04:38 +08002528 vq->signalled_used_valid = false;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002529 if (!vq->iotlb &&
Linus Torvalds96d4f262019-01-03 18:57:57 -08002530 !access_ok(&vq->used->idx, sizeof vq->used->idx)) {
Greg Kurze1f33be2016-02-16 15:54:28 +01002531 r = -EFAULT;
2532 goto err;
2533 }
Jason Wang7b5d7532019-05-24 04:12:14 -04002534 r = vhost_get_used_idx(vq, &last_used_idx);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002535 if (r) {
2536 vq_err(vq, "Can't access used idx at %p\n",
2537 &vq->used->idx);
Greg Kurze1f33be2016-02-16 15:54:28 +01002538 goto err;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002539 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002540 vq->last_used_idx = vhost16_to_cpu(vq, last_used_idx);
Michael S. Tsirkin64f7f052014-12-01 17:39:39 +02002541 return 0;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002542
Greg Kurze1f33be2016-02-16 15:54:28 +01002543err:
2544 vq->is_le = is_le;
2545 return r;
Jason Wang2723fea2011-06-21 18:04:38 +08002546}
Greg Kurz80f7d032016-02-16 15:59:44 +01002547EXPORT_SYMBOL_GPL(vhost_vq_init_access);
Jason Wang2723fea2011-06-21 18:04:38 +08002548
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03002549static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002550 struct iovec iov[], int iov_size, int access)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002551{
Jason Wanga9709d62016-06-23 02:04:31 -04002552 const struct vhost_umem_node *node;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002553 struct vhost_dev *dev = vq->dev;
2554 struct vhost_umem *umem = dev->iotlb ? dev->iotlb : dev->umem;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002555 struct iovec *_iov;
2556 u64 s = 0;
2557 int ret = 0;
2558
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002559 while ((u64)len > s) {
2560 u64 size;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002561 if (unlikely(ret >= iov_size)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002562 ret = -ENOBUFS;
2563 break;
2564 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002565
Jason Wanga9709d62016-06-23 02:04:31 -04002566 node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
2567 addr, addr + len - 1);
2568 if (node == NULL || node->start > addr) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002569 if (umem != dev->iotlb) {
2570 ret = -EFAULT;
2571 break;
2572 }
2573 ret = -EAGAIN;
2574 break;
2575 } else if (!(node->perm & access)) {
2576 ret = -EPERM;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002577 break;
2578 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002579
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002580 _iov = iov + ret;
Jason Wanga9709d62016-06-23 02:04:31 -04002581 size = node->size - addr + node->start;
Michael S. Tsirkinbd971202012-11-26 05:57:27 +00002582 _iov->iov_len = min((u64)len - s, size);
Christoph Hellwiga8d37822010-04-13 14:11:25 -04002583 _iov->iov_base = (void __user *)(unsigned long)
Jason Wanga9709d62016-06-23 02:04:31 -04002584 (node->userspace_addr + addr - node->start);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002585 s += size;
2586 addr += size;
2587 ++ret;
2588 }
2589
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002590 if (ret == -EAGAIN)
2591 vhost_iotlb_miss(vq, addr, access);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002592 return ret;
2593}
2594
2595/* Each buffer in the virtqueues is actually a chain of descriptors. This
2596 * function returns the next descriptor in the chain,
2597 * or -1U if we're at the end. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002598static unsigned next_desc(struct vhost_virtqueue *vq, struct vring_desc *desc)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002599{
2600 unsigned int next;
2601
2602 /* If this descriptor says it doesn't chain, we're done. */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002603 if (!(desc->flags & cpu_to_vhost16(vq, VRING_DESC_F_NEXT)))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002604 return -1U;
2605
2606 /* Check they're not leading us off end of descriptors. */
Paul E. McKenney3a5db0b2017-11-27 09:45:10 -08002607 next = vhost16_to_cpu(vq, READ_ONCE(desc->next));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002608 return next;
2609}
2610
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03002611static int get_indirect(struct vhost_virtqueue *vq,
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002612 struct iovec iov[], unsigned int iov_size,
2613 unsigned int *out_num, unsigned int *in_num,
2614 struct vhost_log *log, unsigned int *log_num,
2615 struct vring_desc *indirect)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002616{
2617 struct vring_desc desc;
2618 unsigned int i = 0, count, found = 0;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002619 u32 len = vhost32_to_cpu(vq, indirect->len);
Al Viroaad9a1c2014-12-10 14:49:01 -05002620 struct iov_iter from;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002621 int ret, access;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002622
2623 /* Sanity check */
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002624 if (unlikely(len % sizeof desc)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002625 vq_err(vq, "Invalid length in indirect descriptor: "
2626 "len 0x%llx not multiple of 0x%zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002627 (unsigned long long)len,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002628 sizeof desc);
2629 return -EINVAL;
2630 }
2631
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002632 ret = translate_desc(vq, vhost64_to_cpu(vq, indirect->addr), len, vq->indirect,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002633 UIO_MAXIOV, VHOST_ACCESS_RO);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002634 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002635 if (ret != -EAGAIN)
2636 vq_err(vq, "Translation failure %d in indirect.\n", ret);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002637 return ret;
2638 }
Al Viroaad9a1c2014-12-10 14:49:01 -05002639 iov_iter_init(&from, READ, vq->indirect, ret, len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002640
2641 /* We will use the result as an address to read from, so most
2642 * architectures only need a compiler barrier here. */
2643 read_barrier_depends();
2644
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002645 count = len / sizeof desc;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002646 /* Buffers are chained via a 16 bit next field, so
2647 * we can have at most 2^16 of these. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002648 if (unlikely(count > USHRT_MAX + 1)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002649 vq_err(vq, "Indirect buffer length too big: %d\n",
2650 indirect->len);
2651 return -E2BIG;
2652 }
2653
2654 do {
2655 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002656 if (unlikely(++found > count)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002657 vq_err(vq, "Loop detected: last one at %u "
2658 "indirect size %u\n",
2659 i, count);
2660 return -EINVAL;
2661 }
Al Virocbbd26b2016-11-01 22:09:04 -04002662 if (unlikely(!copy_from_iter_full(&desc, sizeof(desc), &from))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002663 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002664 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002665 return -EINVAL;
2666 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002667 if (unlikely(desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002668 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002669 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002670 return -EINVAL;
2671 }
2672
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002673 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE))
2674 access = VHOST_ACCESS_WO;
2675 else
2676 access = VHOST_ACCESS_RO;
2677
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002678 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
2679 vhost32_to_cpu(vq, desc.len), iov + iov_count,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002680 iov_size - iov_count, access);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002681 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002682 if (ret != -EAGAIN)
2683 vq_err(vq, "Translation failure %d indirect idx %d\n",
2684 ret, i);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002685 return ret;
2686 }
2687 /* If this is an input descriptor, increment that count. */
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002688 if (access == VHOST_ACCESS_WO) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002689 *in_num += ret;
2690 if (unlikely(log)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002691 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
2692 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002693 ++*log_num;
2694 }
2695 } else {
2696 /* If it's an output descriptor, they're all supposed
2697 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002698 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002699 vq_err(vq, "Indirect descriptor "
2700 "has out after in: idx %d\n", i);
2701 return -EINVAL;
2702 }
2703 *out_num += ret;
2704 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002705 } while ((i = next_desc(vq, &desc)) != -1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002706 return 0;
2707}
2708
2709/* This looks in the virtqueue and for the first available buffer, and converts
2710 * it to an iovec for convenient access. Since descriptors consist of some
2711 * number of output then some number of input descriptors, it's actually two
2712 * iovecs, but we pack them into one and note how many of each there were.
2713 *
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002714 * This function returns the descriptor number found, or vq->num (which is
2715 * never a valid descriptor number) if none was found. A negative code is
2716 * returned on error. */
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03002717int vhost_get_vq_desc(struct vhost_virtqueue *vq,
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002718 struct iovec iov[], unsigned int iov_size,
2719 unsigned int *out_num, unsigned int *in_num,
2720 struct vhost_log *log, unsigned int *log_num)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002721{
2722 struct vring_desc desc;
2723 unsigned int i, head, found = 0;
2724 u16 last_avail_idx;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002725 __virtio16 avail_idx;
2726 __virtio16 ring_head;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002727 int ret, access;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002728
2729 /* Check it isn't doing very strange things with descriptor numbers. */
2730 last_avail_idx = vq->last_avail_idx;
Jason Wange3b56cd2017-02-07 15:49:50 +08002731
2732 if (vq->avail_idx == vq->last_avail_idx) {
Jason Wang7b5d7532019-05-24 04:12:14 -04002733 if (unlikely(vhost_get_avail_idx(vq, &avail_idx))) {
Jason Wange3b56cd2017-02-07 15:49:50 +08002734 vq_err(vq, "Failed to access avail idx at %p\n",
2735 &vq->avail->idx);
2736 return -EFAULT;
2737 }
2738 vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
2739
2740 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
2741 vq_err(vq, "Guest moved used index from %u to %u",
2742 last_avail_idx, vq->avail_idx);
2743 return -EFAULT;
2744 }
2745
2746 /* If there's nothing new since last we looked, return
2747 * invalid.
2748 */
2749 if (vq->avail_idx == last_avail_idx)
2750 return vq->num;
2751
2752 /* Only get avail ring entries after they have been
2753 * exposed by guest.
2754 */
2755 smp_rmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002756 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002757
2758 /* Grab the next descriptor number they're advertising, and increment
2759 * the index we've seen. */
Jason Wang7b5d7532019-05-24 04:12:14 -04002760 if (unlikely(vhost_get_avail_head(vq, &ring_head, last_avail_idx))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002761 vq_err(vq, "Failed to read head: idx %d address %p\n",
2762 last_avail_idx,
2763 &vq->avail->ring[last_avail_idx % vq->num]);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002764 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002765 }
2766
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002767 head = vhost16_to_cpu(vq, ring_head);
2768
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002769 /* If their number is silly, that's an error. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002770 if (unlikely(head >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002771 vq_err(vq, "Guest says index %u > %u is available",
2772 head, vq->num);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002773 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002774 }
2775
2776 /* When we start there are none of either input nor output. */
2777 *out_num = *in_num = 0;
2778 if (unlikely(log))
2779 *log_num = 0;
2780
2781 i = head;
2782 do {
2783 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002784 if (unlikely(i >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002785 vq_err(vq, "Desc index is %u > %u, head = %u",
2786 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002787 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002788 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002789 if (unlikely(++found > vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002790 vq_err(vq, "Loop detected: last one at %u "
2791 "vq size %u head %u\n",
2792 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002793 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002794 }
Jason Wang7b5d7532019-05-24 04:12:14 -04002795 ret = vhost_get_desc(vq, &desc, i);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002796 if (unlikely(ret)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002797 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
2798 i, vq->desc + i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002799 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002800 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002801 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT)) {
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03002802 ret = get_indirect(vq, iov, iov_size,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002803 out_num, in_num,
2804 log, log_num, &desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002805 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002806 if (ret != -EAGAIN)
2807 vq_err(vq, "Failure detected "
2808 "in indirect descriptor at idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002809 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002810 }
2811 continue;
2812 }
2813
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002814 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE))
2815 access = VHOST_ACCESS_WO;
2816 else
2817 access = VHOST_ACCESS_RO;
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002818 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
2819 vhost32_to_cpu(vq, desc.len), iov + iov_count,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002820 iov_size - iov_count, access);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002821 if (unlikely(ret < 0)) {
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002822 if (ret != -EAGAIN)
2823 vq_err(vq, "Translation failure %d descriptor idx %d\n",
2824 ret, i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002825 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002826 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04002827 if (access == VHOST_ACCESS_WO) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002828 /* If this is an input descriptor,
2829 * increment that count. */
2830 *in_num += ret;
2831 if (unlikely(log)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002832 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
2833 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002834 ++*log_num;
2835 }
2836 } else {
2837 /* If it's an output descriptor, they're all supposed
2838 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03002839 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002840 vq_err(vq, "Descriptor has out after in: "
2841 "idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03002842 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002843 }
2844 *out_num += ret;
2845 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002846 } while ((i = next_desc(vq, &desc)) != -1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002847
2848 /* On success, increment avail index. */
2849 vq->last_avail_idx++;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002850
2851 /* Assume notifications from guest are disabled at this point,
2852 * if they aren't we would need to update avail_event index. */
2853 BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002854 return head;
2855}
Asias He6ac1afb2013-05-06 16:38:21 +08002856EXPORT_SYMBOL_GPL(vhost_get_vq_desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002857
2858/* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
David Stevens8dd014a2010-07-27 18:52:21 +03002859void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002860{
David Stevens8dd014a2010-07-27 18:52:21 +03002861 vq->last_avail_idx -= n;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002862}
Asias He6ac1afb2013-05-06 16:38:21 +08002863EXPORT_SYMBOL_GPL(vhost_discard_vq_desc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002864
2865/* After we've used one of their buffers, we tell them about it. We'll then
2866 * want to notify the guest, using eventfd. */
2867int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
2868{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002869 struct vring_used_elem heads = {
2870 cpu_to_vhost32(vq, head),
2871 cpu_to_vhost32(vq, len)
2872 };
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002873
Jason Wangc49e4e52013-09-02 16:40:58 +08002874 return vhost_add_used_n(vq, &heads, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002875}
Asias He6ac1afb2013-05-06 16:38:21 +08002876EXPORT_SYMBOL_GPL(vhost_add_used);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002877
David Stevens8dd014a2010-07-27 18:52:21 +03002878static int __vhost_add_used_n(struct vhost_virtqueue *vq,
2879 struct vring_used_elem *heads,
2880 unsigned count)
2881{
2882 struct vring_used_elem __user *used;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002883 u16 old, new;
David Stevens8dd014a2010-07-27 18:52:21 +03002884 int start;
2885
Michael S. Tsirkin5fba13b2015-11-29 13:34:44 +02002886 start = vq->last_used_idx & (vq->num - 1);
David Stevens8dd014a2010-07-27 18:52:21 +03002887 used = vq->used->ring + start;
Jason Wang7b5d7532019-05-24 04:12:14 -04002888 if (vhost_put_used(vq, heads, start, count)) {
David Stevens8dd014a2010-07-27 18:52:21 +03002889 vq_err(vq, "Failed to write used");
2890 return -EFAULT;
2891 }
2892 if (unlikely(vq->log_used)) {
2893 /* Make sure data is seen before log. */
2894 smp_wmb();
2895 /* Log used ring entry write. */
Jason Wangcc5e7102019-01-16 16:54:42 +08002896 log_used(vq, ((void __user *)used - (void __user *)vq->used),
2897 count * sizeof *used);
David Stevens8dd014a2010-07-27 18:52:21 +03002898 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002899 old = vq->last_used_idx;
2900 new = (vq->last_used_idx += count);
2901 /* If the driver never bothers to signal in a very long while,
2902 * used index might wrap around. If that happens, invalidate
2903 * signalled_used index we stored. TODO: make sure driver
2904 * signals at least once in 2^16 and remove this. */
2905 if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old)))
2906 vq->signalled_used_valid = false;
David Stevens8dd014a2010-07-27 18:52:21 +03002907 return 0;
2908}
2909
2910/* After we've used one of their buffers, we tell them about it. We'll then
2911 * want to notify the guest, using eventfd. */
2912int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
2913 unsigned count)
2914{
2915 int start, n, r;
2916
Michael S. Tsirkin5fba13b2015-11-29 13:34:44 +02002917 start = vq->last_used_idx & (vq->num - 1);
David Stevens8dd014a2010-07-27 18:52:21 +03002918 n = vq->num - start;
2919 if (n < count) {
2920 r = __vhost_add_used_n(vq, heads, n);
2921 if (r < 0)
2922 return r;
2923 heads += n;
2924 count -= n;
2925 }
2926 r = __vhost_add_used_n(vq, heads, count);
2927
2928 /* Make sure buffer is written before we update index. */
2929 smp_wmb();
Jason Wang7b5d7532019-05-24 04:12:14 -04002930 if (vhost_put_used_idx(vq)) {
David Stevens8dd014a2010-07-27 18:52:21 +03002931 vq_err(vq, "Failed to increment used idx");
2932 return -EFAULT;
2933 }
2934 if (unlikely(vq->log_used)) {
Jason Wang841df922018-12-13 10:53:37 +08002935 /* Make sure used idx is seen before log. */
2936 smp_wmb();
David Stevens8dd014a2010-07-27 18:52:21 +03002937 /* Log used index update. */
Jason Wangcc5e7102019-01-16 16:54:42 +08002938 log_used(vq, offsetof(struct vring_used, idx),
2939 sizeof vq->used->idx);
David Stevens8dd014a2010-07-27 18:52:21 +03002940 if (vq->log_ctx)
2941 eventfd_signal(vq->log_ctx, 1);
2942 }
2943 return r;
2944}
Asias He6ac1afb2013-05-06 16:38:21 +08002945EXPORT_SYMBOL_GPL(vhost_add_used_n);
David Stevens8dd014a2010-07-27 18:52:21 +03002946
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002947static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002948{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002949 __u16 old, new;
2950 __virtio16 event;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002951 bool v;
Jason Wang8d658432017-07-27 11:22:05 +08002952 /* Flush out used index updates. This is paired
2953 * with the barrier that the Guest executes when enabling
2954 * interrupts. */
2955 smp_mb();
Michael S. Tsirkin0d499352010-05-11 19:44:17 +03002956
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002957 if (vhost_has_feature(vq, VIRTIO_F_NOTIFY_ON_EMPTY) &&
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002958 unlikely(vq->avail_idx == vq->last_avail_idx))
2959 return true;
2960
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03002961 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002962 __virtio16 flags;
Jason Wang7b5d7532019-05-24 04:12:14 -04002963 if (vhost_get_avail_flags(vq, &flags)) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002964 vq_err(vq, "Failed to get flags");
2965 return true;
2966 }
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03002967 return !(flags & cpu_to_vhost16(vq, VRING_AVAIL_F_NO_INTERRUPT));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002968 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002969 old = vq->signalled_used;
2970 v = vq->signalled_used_valid;
2971 new = vq->signalled_used = vq->last_used_idx;
2972 vq->signalled_used_valid = true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002973
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002974 if (unlikely(!v))
2975 return true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002976
Jason Wang7b5d7532019-05-24 04:12:14 -04002977 if (vhost_get_used_event(vq, &event)) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002978 vq_err(vq, "Failed to get used event idx");
2979 return true;
2980 }
Jason Wang8d658432017-07-27 11:22:05 +08002981 return vring_need_event(vhost16_to_cpu(vq, event), new, old);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002982}
2983
2984/* This actually signals the guest, using eventfd. */
2985void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
2986{
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002987 /* Signal the Guest tell them we used something up. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03002988 if (vq->call_ctx && vhost_notify(dev, vq))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002989 eventfd_signal(vq->call_ctx, 1);
2990}
Asias He6ac1afb2013-05-06 16:38:21 +08002991EXPORT_SYMBOL_GPL(vhost_signal);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00002992
2993/* And here's the combo meal deal. Supersize me! */
2994void vhost_add_used_and_signal(struct vhost_dev *dev,
2995 struct vhost_virtqueue *vq,
2996 unsigned int head, int len)
2997{
2998 vhost_add_used(vq, head, len);
2999 vhost_signal(dev, vq);
3000}
Asias He6ac1afb2013-05-06 16:38:21 +08003001EXPORT_SYMBOL_GPL(vhost_add_used_and_signal);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00003002
David Stevens8dd014a2010-07-27 18:52:21 +03003003/* multi-buffer version of vhost_add_used_and_signal */
3004void vhost_add_used_and_signal_n(struct vhost_dev *dev,
3005 struct vhost_virtqueue *vq,
3006 struct vring_used_elem *heads, unsigned count)
3007{
3008 vhost_add_used_n(vq, heads, count);
3009 vhost_signal(dev, vq);
3010}
Asias He6ac1afb2013-05-06 16:38:21 +08003011EXPORT_SYMBOL_GPL(vhost_add_used_and_signal_n);
David Stevens8dd014a2010-07-27 18:52:21 +03003012
Jason Wangd4a60602016-03-04 06:24:52 -05003013/* return true if we're sure that avaiable ring is empty */
3014bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq)
3015{
3016 __virtio16 avail_idx;
3017 int r;
3018
Jason Wang275bf962017-01-18 15:02:01 +08003019 if (vq->avail_idx != vq->last_avail_idx)
Jason Wangd4a60602016-03-04 06:24:52 -05003020 return false;
3021
Jason Wang7b5d7532019-05-24 04:12:14 -04003022 r = vhost_get_avail_idx(vq, &avail_idx);
Jason Wang275bf962017-01-18 15:02:01 +08003023 if (unlikely(r))
3024 return false;
3025 vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
3026
3027 return vq->avail_idx == vq->last_avail_idx;
Jason Wangd4a60602016-03-04 06:24:52 -05003028}
3029EXPORT_SYMBOL_GPL(vhost_vq_avail_empty);
3030
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00003031/* OK, now we need to know about added descriptors. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03003032bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00003033{
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03003034 __virtio16 avail_idx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00003035 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05303036
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00003037 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
3038 return false;
3039 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03003040 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08003041 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03003042 if (r) {
3043 vq_err(vq, "Failed to enable notification at %p: %d\n",
3044 &vq->used->flags, r);
3045 return false;
3046 }
3047 } else {
Jason Wang2723fea2011-06-21 18:04:38 +08003048 r = vhost_update_avail_event(vq, vq->avail_idx);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03003049 if (r) {
3050 vq_err(vq, "Failed to update avail event index at %p: %d\n",
3051 vhost_avail_event(vq), r);
3052 return false;
3053 }
3054 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00003055 /* They could have slipped one in as we were doing that: make
3056 * sure it's written, then check again. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00003057 smp_mb();
Jason Wang7b5d7532019-05-24 04:12:14 -04003058 r = vhost_get_avail_idx(vq, &avail_idx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00003059 if (r) {
3060 vq_err(vq, "Failed to check avail idx at %p: %d\n",
3061 &vq->avail->idx, r);
3062 return false;
3063 }
3064
Michael S. Tsirkin3b1bbe82014-10-24 14:04:47 +03003065 return vhost16_to_cpu(vq, avail_idx) != vq->avail_idx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00003066}
Asias He6ac1afb2013-05-06 16:38:21 +08003067EXPORT_SYMBOL_GPL(vhost_enable_notify);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00003068
3069/* We don't need to be notified again. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03003070void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00003071{
3072 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05303073
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00003074 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
3075 return;
3076 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03003077 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08003078 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03003079 if (r)
3080 vq_err(vq, "Failed to enable notification at %p: %d\n",
3081 &vq->used->flags, r);
3082 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00003083}
Asias He6ac1afb2013-05-06 16:38:21 +08003084EXPORT_SYMBOL_GPL(vhost_disable_notify);
3085
Jason Wang6b1e6cc2016-06-23 02:04:32 -04003086/* Create a new message. */
3087struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type)
3088{
3089 struct vhost_msg_node *node = kmalloc(sizeof *node, GFP_KERNEL);
3090 if (!node)
3091 return NULL;
Michael S. Tsirkin670ae9c2018-05-12 00:33:10 +03003092
3093 /* Make sure all padding within the structure is initialized. */
3094 memset(&node->msg, 0, sizeof node->msg);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04003095 node->vq = vq;
3096 node->msg.type = type;
3097 return node;
3098}
3099EXPORT_SYMBOL_GPL(vhost_new_msg);
3100
3101void vhost_enqueue_msg(struct vhost_dev *dev, struct list_head *head,
3102 struct vhost_msg_node *node)
3103{
3104 spin_lock(&dev->iotlb_lock);
3105 list_add_tail(&node->node, head);
3106 spin_unlock(&dev->iotlb_lock);
3107
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003108 wake_up_interruptible_poll(&dev->wait, EPOLLIN | EPOLLRDNORM);
Jason Wang6b1e6cc2016-06-23 02:04:32 -04003109}
3110EXPORT_SYMBOL_GPL(vhost_enqueue_msg);
3111
3112struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev,
3113 struct list_head *head)
3114{
3115 struct vhost_msg_node *node = NULL;
3116
3117 spin_lock(&dev->iotlb_lock);
3118 if (!list_empty(head)) {
3119 node = list_first_entry(head, struct vhost_msg_node,
3120 node);
3121 list_del(&node->node);
3122 }
3123 spin_unlock(&dev->iotlb_lock);
3124
3125 return node;
3126}
3127EXPORT_SYMBOL_GPL(vhost_dequeue_msg);
3128
3129
Asias He6ac1afb2013-05-06 16:38:21 +08003130static int __init vhost_init(void)
3131{
3132 return 0;
3133}
3134
3135static void __exit vhost_exit(void)
3136{
3137}
3138
3139module_init(vhost_init);
3140module_exit(vhost_exit);
3141
3142MODULE_VERSION("0.0.1");
3143MODULE_LICENSE("GPL v2");
3144MODULE_AUTHOR("Michael S. Tsirkin");
3145MODULE_DESCRIPTION("Host kernel accelerator for virtio");