Thomas Gleixner | 7a33847 | 2019-06-04 10:11:15 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2 | /* 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 Landley | 6151658 | 2011-05-06 09:27:36 -0700 | [diff] [blame] | 8 | * Documentation/virtual/lguest/lguest.c, by Rusty Russell |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 9 | * |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 10 | * Generic code for virtio server in host kernel. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/eventfd.h> |
| 14 | #include <linux/vhost.h> |
Asias He | 35596b2 | 2013-08-19 09:23:19 +0800 | [diff] [blame] | 15 | #include <linux/uio.h> |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 16 | #include <linux/mm.h> |
Michael S. Tsirkin | 64e1c80 | 2010-10-06 15:34:45 +0200 | [diff] [blame] | 17 | #include <linux/mmu_context.h> |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 18 | #include <linux/miscdevice.h> |
| 19 | #include <linux/mutex.h> |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 20 | #include <linux/poll.h> |
| 21 | #include <linux/file.h> |
| 22 | #include <linux/highmem.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Igor Mammedov | 4de7255 | 2015-07-01 11:07:09 +0200 | [diff] [blame] | 24 | #include <linux/vmalloc.h> |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 25 | #include <linux/kthread.h> |
Michael S. Tsirkin | 9e3d195 | 2010-07-27 22:56:50 +0300 | [diff] [blame] | 26 | #include <linux/cgroup.h> |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 27 | #include <linux/module.h> |
Igor Mammedov | bcfeaca | 2015-06-16 18:33:35 +0200 | [diff] [blame] | 28 | #include <linux/sort.h> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 29 | #include <linux/sched/mm.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 30 | #include <linux/sched/signal.h> |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 31 | #include <linux/interval_tree_generic.h> |
Jason Wang | ff00226 | 2018-10-30 14:10:49 +0800 | [diff] [blame] | 32 | #include <linux/nospec.h> |
Andrey Konovalov | 8f6a7f9 | 2019-12-04 16:52:50 -0800 | [diff] [blame] | 33 | #include <linux/kcov.h> |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 34 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 35 | #include "vhost.h" |
| 36 | |
Igor Mammedov | c9ce42f | 2015-07-02 15:08:11 +0200 | [diff] [blame] | 37 | static ushort max_mem_regions = 64; |
| 38 | module_param(max_mem_regions, ushort, 0444); |
| 39 | MODULE_PARM_DESC(max_mem_regions, |
| 40 | "Maximum number of memory regions in memory map. (default: 64)"); |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 41 | static int max_iotlb_entries = 2048; |
| 42 | module_param(max_iotlb_entries, int, 0444); |
| 43 | MODULE_PARM_DESC(max_iotlb_entries, |
| 44 | "Maximum number of iotlb entries. (default: 2048)"); |
Igor Mammedov | c9ce42f | 2015-07-02 15:08:11 +0200 | [diff] [blame] | 45 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 46 | enum { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 47 | VHOST_MEMORY_F_LOG = 0x1, |
| 48 | }; |
| 49 | |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 50 | #define vhost_used_event(vq) ((__virtio16 __user *)&vq->avail->ring[vq->num]) |
| 51 | #define vhost_avail_event(vq) ((__virtio16 __user *)&vq->used->ring[vq->num]) |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 52 | |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 53 | INTERVAL_TREE_DEFINE(struct vhost_umem_node, |
| 54 | rb, __u64, __subtree_last, |
Michael S. Tsirkin | 2f952c0 | 2016-12-06 05:57:54 +0200 | [diff] [blame] | 55 | START, LAST, static inline, vhost_umem_interval_tree); |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 56 | |
Greg Kurz | 2751c98 | 2015-04-24 14:27:24 +0200 | [diff] [blame] | 57 | #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY |
Greg Kurz | c507203 | 2016-02-16 15:59:34 +0100 | [diff] [blame] | 58 | static void vhost_disable_cross_endian(struct vhost_virtqueue *vq) |
Greg Kurz | 2751c98 | 2015-04-24 14:27:24 +0200 | [diff] [blame] | 59 | { |
| 60 | vq->user_be = !virtio_legacy_is_little_endian(); |
| 61 | } |
| 62 | |
Greg Kurz | c507203 | 2016-02-16 15:59:34 +0100 | [diff] [blame] | 63 | static void vhost_enable_cross_endian_big(struct vhost_virtqueue *vq) |
| 64 | { |
| 65 | vq->user_be = true; |
| 66 | } |
| 67 | |
| 68 | static void vhost_enable_cross_endian_little(struct vhost_virtqueue *vq) |
| 69 | { |
| 70 | vq->user_be = false; |
| 71 | } |
| 72 | |
Greg Kurz | 2751c98 | 2015-04-24 14:27:24 +0200 | [diff] [blame] | 73 | static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp) |
| 74 | { |
| 75 | struct vhost_vring_state s; |
| 76 | |
| 77 | if (vq->private_data) |
| 78 | return -EBUSY; |
| 79 | |
| 80 | if (copy_from_user(&s, argp, sizeof(s))) |
| 81 | return -EFAULT; |
| 82 | |
| 83 | if (s.num != VHOST_VRING_LITTLE_ENDIAN && |
| 84 | s.num != VHOST_VRING_BIG_ENDIAN) |
| 85 | return -EINVAL; |
| 86 | |
Greg Kurz | c507203 | 2016-02-16 15:59:34 +0100 | [diff] [blame] | 87 | if (s.num == VHOST_VRING_BIG_ENDIAN) |
| 88 | vhost_enable_cross_endian_big(vq); |
| 89 | else |
| 90 | vhost_enable_cross_endian_little(vq); |
Greg Kurz | 2751c98 | 2015-04-24 14:27:24 +0200 | [diff] [blame] | 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx, |
| 96 | int __user *argp) |
| 97 | { |
| 98 | struct vhost_vring_state s = { |
| 99 | .index = idx, |
| 100 | .num = vq->user_be |
| 101 | }; |
| 102 | |
| 103 | if (copy_to_user(argp, &s, sizeof(s))) |
| 104 | return -EFAULT; |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static void vhost_init_is_le(struct vhost_virtqueue *vq) |
| 110 | { |
| 111 | /* Note for legacy virtio: user_be is initialized at reset time |
| 112 | * according to the host endianness. If userspace does not set an |
| 113 | * explicit endianness, the default behavior is native endian, as |
| 114 | * expected by legacy virtio. |
| 115 | */ |
| 116 | vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) || !vq->user_be; |
| 117 | } |
| 118 | #else |
Greg Kurz | c507203 | 2016-02-16 15:59:34 +0100 | [diff] [blame] | 119 | static void vhost_disable_cross_endian(struct vhost_virtqueue *vq) |
Greg Kurz | 2751c98 | 2015-04-24 14:27:24 +0200 | [diff] [blame] | 120 | { |
| 121 | } |
| 122 | |
| 123 | static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp) |
| 124 | { |
| 125 | return -ENOIOCTLCMD; |
| 126 | } |
| 127 | |
| 128 | static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx, |
| 129 | int __user *argp) |
| 130 | { |
| 131 | return -ENOIOCTLCMD; |
| 132 | } |
| 133 | |
| 134 | static void vhost_init_is_le(struct vhost_virtqueue *vq) |
| 135 | { |
Halil Pasic | cda8bba | 2017-01-30 11:09:36 +0100 | [diff] [blame] | 136 | vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) |
| 137 | || virtio_legacy_is_little_endian(); |
Greg Kurz | 2751c98 | 2015-04-24 14:27:24 +0200 | [diff] [blame] | 138 | } |
| 139 | #endif /* CONFIG_VHOST_CROSS_ENDIAN_LEGACY */ |
| 140 | |
Greg Kurz | c507203 | 2016-02-16 15:59:34 +0100 | [diff] [blame] | 141 | static void vhost_reset_is_le(struct vhost_virtqueue *vq) |
| 142 | { |
Halil Pasic | cda8bba | 2017-01-30 11:09:36 +0100 | [diff] [blame] | 143 | vhost_init_is_le(vq); |
Greg Kurz | c507203 | 2016-02-16 15:59:34 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Jason Wang | 7235acd | 2016-04-25 22:14:32 -0400 | [diff] [blame] | 146 | struct vhost_flush_struct { |
| 147 | struct vhost_work work; |
| 148 | struct completion wait_event; |
| 149 | }; |
| 150 | |
| 151 | static void vhost_flush_work(struct vhost_work *work) |
| 152 | { |
| 153 | struct vhost_flush_struct *s; |
| 154 | |
| 155 | s = container_of(work, struct vhost_flush_struct, work); |
| 156 | complete(&s->wait_event); |
| 157 | } |
| 158 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 159 | static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh, |
| 160 | poll_table *pt) |
| 161 | { |
| 162 | struct vhost_poll *poll; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 163 | |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 164 | poll = container_of(pt, struct vhost_poll, table); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 165 | poll->wqh = wqh; |
| 166 | add_wait_queue(wqh, &poll->wait); |
| 167 | } |
| 168 | |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 169 | static int vhost_poll_wakeup(wait_queue_entry_t *wait, unsigned mode, int sync, |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 170 | void *key) |
| 171 | { |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 172 | struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait); |
| 173 | |
Al Viro | 3ad6f93 | 2017-07-03 20:14:56 -0400 | [diff] [blame] | 174 | if (!(key_to_poll(key) & poll->mask)) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 175 | return 0; |
| 176 | |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 177 | vhost_poll_queue(poll); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 178 | return 0; |
| 179 | } |
| 180 | |
Stefan Hajnoczi | 163049a | 2012-07-21 06:55:37 +0000 | [diff] [blame] | 181 | void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 182 | { |
Jason Wang | 04b96e5 | 2016-04-25 22:14:33 -0400 | [diff] [blame] | 183 | clear_bit(VHOST_WORK_QUEUED, &work->flags); |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 184 | work->fn = fn; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 185 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 186 | EXPORT_SYMBOL_GPL(vhost_work_init); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 187 | |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 188 | /* Init poll structure */ |
| 189 | void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn, |
Al Viro | 58e3b60 | 2017-07-03 23:50:40 -0400 | [diff] [blame] | 190 | __poll_t mask, struct vhost_dev *dev) |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 191 | { |
| 192 | init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup); |
| 193 | init_poll_funcptr(&poll->table, vhost_poll_func); |
| 194 | poll->mask = mask; |
| 195 | poll->dev = dev; |
Jason Wang | 2b8b328 | 2013-01-28 01:05:18 +0000 | [diff] [blame] | 196 | poll->wqh = NULL; |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 197 | |
| 198 | vhost_work_init(&poll->work, fn); |
| 199 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 200 | EXPORT_SYMBOL_GPL(vhost_poll_init); |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 201 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 202 | /* Start polling a file. We add ourselves to file's wait queue. The caller must |
| 203 | * keep a reference to a file until after vhost_poll_stop is called. */ |
Jason Wang | 2b8b328 | 2013-01-28 01:05:18 +0000 | [diff] [blame] | 204 | int vhost_poll_start(struct vhost_poll *poll, struct file *file) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 205 | { |
Al Viro | e6c8adc | 2017-07-03 22:25:56 -0400 | [diff] [blame] | 206 | __poll_t mask; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 207 | |
Jason Wang | 70181d51 | 2013-04-10 20:50:48 +0000 | [diff] [blame] | 208 | if (poll->wqh) |
| 209 | return 0; |
| 210 | |
Christoph Hellwig | 9965ed17 | 2018-03-05 07:26:05 -0800 | [diff] [blame] | 211 | mask = vfs_poll(file, &poll->table); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 212 | if (mask) |
Al Viro | 3ad6f93 | 2017-07-03 20:14:56 -0400 | [diff] [blame] | 213 | vhost_poll_wakeup(&poll->wait, 0, 0, poll_to_key(mask)); |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 214 | if (mask & EPOLLERR) { |
Jason Wang | dc6455a | 2018-03-27 20:50:52 +0800 | [diff] [blame] | 215 | vhost_poll_stop(poll); |
Yunsheng Lin | 896fc24 | 2019-08-20 20:36:32 +0800 | [diff] [blame] | 216 | return -EINVAL; |
Jason Wang | 2b8b328 | 2013-01-28 01:05:18 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Yunsheng Lin | 896fc24 | 2019-08-20 20:36:32 +0800 | [diff] [blame] | 219 | return 0; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 220 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 221 | EXPORT_SYMBOL_GPL(vhost_poll_start); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 222 | |
| 223 | /* Stop polling a file. After this function returns, it becomes safe to drop the |
| 224 | * file reference. You must also flush afterwards. */ |
| 225 | void vhost_poll_stop(struct vhost_poll *poll) |
| 226 | { |
Jason Wang | 2b8b328 | 2013-01-28 01:05:18 +0000 | [diff] [blame] | 227 | if (poll->wqh) { |
| 228 | remove_wait_queue(poll->wqh, &poll->wait); |
| 229 | poll->wqh = NULL; |
| 230 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 231 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 232 | EXPORT_SYMBOL_GPL(vhost_poll_stop); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 233 | |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 234 | void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 235 | { |
Jason Wang | 7235acd | 2016-04-25 22:14:32 -0400 | [diff] [blame] | 236 | struct vhost_flush_struct flush; |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 237 | |
Jason Wang | 7235acd | 2016-04-25 22:14:32 -0400 | [diff] [blame] | 238 | if (dev->worker) { |
| 239 | init_completion(&flush.wait_event); |
| 240 | vhost_work_init(&flush.work, vhost_flush_work); |
| 241 | |
| 242 | vhost_work_queue(dev, &flush.work); |
| 243 | wait_for_completion(&flush.wait_event); |
| 244 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 245 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 246 | EXPORT_SYMBOL_GPL(vhost_work_flush); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 247 | |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 248 | /* Flush any work that has been scheduled. When calling this, don't hold any |
| 249 | * locks that are also used by the callback. */ |
| 250 | void vhost_poll_flush(struct vhost_poll *poll) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 251 | { |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 252 | vhost_work_flush(poll->dev, &poll->work); |
| 253 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 254 | EXPORT_SYMBOL_GPL(vhost_poll_flush); |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 255 | |
Stefan Hajnoczi | 163049a | 2012-07-21 06:55:37 +0000 | [diff] [blame] | 256 | void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work) |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 257 | { |
Jason Wang | 04b96e5 | 2016-04-25 22:14:33 -0400 | [diff] [blame] | 258 | if (!dev->worker) |
| 259 | return; |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 260 | |
Jason Wang | 04b96e5 | 2016-04-25 22:14:33 -0400 | [diff] [blame] | 261 | if (!test_and_set_bit(VHOST_WORK_QUEUED, &work->flags)) { |
| 262 | /* We can only add the work to the list after we're |
| 263 | * sure it was not in the list. |
Peng Tao | 635abf0 | 2016-12-07 17:52:19 +0800 | [diff] [blame] | 264 | * test_and_set_bit() implies a memory barrier. |
Jason Wang | 04b96e5 | 2016-04-25 22:14:33 -0400 | [diff] [blame] | 265 | */ |
Jason Wang | 04b96e5 | 2016-04-25 22:14:33 -0400 | [diff] [blame] | 266 | llist_add(&work->node, &dev->work_list); |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 267 | wake_up_process(dev->worker); |
| 268 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 269 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 270 | EXPORT_SYMBOL_GPL(vhost_work_queue); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 271 | |
Jason Wang | 526d3e7 | 2016-03-04 06:24:51 -0500 | [diff] [blame] | 272 | /* A lockless hint for busy polling code to exit the loop */ |
| 273 | bool vhost_has_work(struct vhost_dev *dev) |
| 274 | { |
Jason Wang | 04b96e5 | 2016-04-25 22:14:33 -0400 | [diff] [blame] | 275 | return !llist_empty(&dev->work_list); |
Jason Wang | 526d3e7 | 2016-03-04 06:24:51 -0500 | [diff] [blame] | 276 | } |
| 277 | EXPORT_SYMBOL_GPL(vhost_has_work); |
| 278 | |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 279 | void vhost_poll_queue(struct vhost_poll *poll) |
| 280 | { |
| 281 | vhost_work_queue(poll->dev, &poll->work); |
| 282 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 283 | EXPORT_SYMBOL_GPL(vhost_poll_queue); |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 284 | |
Jason Wang | f889491 | 2017-02-28 17:56:02 +0800 | [diff] [blame] | 285 | static void __vhost_vq_meta_reset(struct vhost_virtqueue *vq) |
| 286 | { |
| 287 | int j; |
| 288 | |
| 289 | for (j = 0; j < VHOST_NUM_ADDRS; j++) |
| 290 | vq->meta_iotlb[j] = NULL; |
| 291 | } |
| 292 | |
| 293 | static void vhost_vq_meta_reset(struct vhost_dev *d) |
| 294 | { |
| 295 | int i; |
| 296 | |
Jason Wang | 86a07da | 2018-12-13 10:53:39 +0800 | [diff] [blame] | 297 | for (i = 0; i < d->nvqs; ++i) |
Jason Wang | f889491 | 2017-02-28 17:56:02 +0800 | [diff] [blame] | 298 | __vhost_vq_meta_reset(d->vqs[i]); |
| 299 | } |
| 300 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 301 | static void vhost_vq_reset(struct vhost_dev *dev, |
| 302 | struct vhost_virtqueue *vq) |
| 303 | { |
| 304 | vq->num = 1; |
| 305 | vq->desc = NULL; |
| 306 | vq->avail = NULL; |
| 307 | vq->used = NULL; |
| 308 | vq->last_avail_idx = 0; |
| 309 | vq->avail_idx = 0; |
| 310 | vq->last_used_idx = 0; |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 311 | vq->signalled_used = 0; |
| 312 | vq->signalled_used_valid = false; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 313 | vq->used_flags = 0; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 314 | vq->log_used = false; |
| 315 | vq->log_addr = -1ull; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 316 | vq->private_data = NULL; |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 317 | vq->acked_features = 0; |
Jason Wang | 429711a | 2018-08-06 11:17:47 +0800 | [diff] [blame] | 318 | vq->acked_backend_features = 0; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 319 | vq->log_base = NULL; |
| 320 | vq->error_ctx = NULL; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 321 | vq->kick = NULL; |
| 322 | vq->call_ctx = NULL; |
Michael S. Tsirkin | 73a99f0 | 2010-02-23 11:23:45 +0200 | [diff] [blame] | 323 | vq->log_ctx = NULL; |
Greg Kurz | c507203 | 2016-02-16 15:59:34 +0100 | [diff] [blame] | 324 | vhost_reset_is_le(vq); |
| 325 | vhost_disable_cross_endian(vq); |
Jason Wang | 0308813 | 2016-03-04 06:24:53 -0500 | [diff] [blame] | 326 | vq->busyloop_timeout = 0; |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 327 | vq->umem = NULL; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 328 | vq->iotlb = NULL; |
Jason Wang | f889491 | 2017-02-28 17:56:02 +0800 | [diff] [blame] | 329 | __vhost_vq_meta_reset(vq); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 332 | static int vhost_worker(void *data) |
| 333 | { |
| 334 | struct vhost_dev *dev = data; |
Jason Wang | 04b96e5 | 2016-04-25 22:14:33 -0400 | [diff] [blame] | 335 | struct vhost_work *work, *work_next; |
| 336 | struct llist_node *node; |
Jens Freimann | d7ffde3 | 2012-06-26 00:59:58 +0000 | [diff] [blame] | 337 | mm_segment_t oldfs = get_fs(); |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 338 | |
Jens Freimann | d7ffde3 | 2012-06-26 00:59:58 +0000 | [diff] [blame] | 339 | set_fs(USER_DS); |
Michael S. Tsirkin | 64e1c80 | 2010-10-06 15:34:45 +0200 | [diff] [blame] | 340 | use_mm(dev->mm); |
| 341 | |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 342 | for (;;) { |
| 343 | /* mb paired w/ kthread_stop */ |
| 344 | set_current_state(TASK_INTERRUPTIBLE); |
| 345 | |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 346 | if (kthread_should_stop()) { |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 347 | __set_current_state(TASK_RUNNING); |
Michael S. Tsirkin | 64e1c80 | 2010-10-06 15:34:45 +0200 | [diff] [blame] | 348 | break; |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 349 | } |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 350 | |
Jason Wang | 04b96e5 | 2016-04-25 22:14:33 -0400 | [diff] [blame] | 351 | node = llist_del_all(&dev->work_list); |
| 352 | if (!node) |
| 353 | schedule(); |
| 354 | |
| 355 | node = llist_reverse_order(node); |
| 356 | /* make sure flag is seen after deletion */ |
| 357 | smp_wmb(); |
| 358 | llist_for_each_entry_safe(work, work_next, node, node) { |
| 359 | clear_bit(VHOST_WORK_QUEUED, &work->flags); |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 360 | __set_current_state(TASK_RUNNING); |
Andrey Konovalov | 8f6a7f9 | 2019-12-04 16:52:50 -0800 | [diff] [blame] | 361 | kcov_remote_start_common(dev->kcov_handle); |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 362 | work->fn(work); |
Andrey Konovalov | 8f6a7f9 | 2019-12-04 16:52:50 -0800 | [diff] [blame] | 363 | kcov_remote_stop(); |
Nadav Har'El | d550dda | 2012-02-27 15:07:29 +0200 | [diff] [blame] | 364 | if (need_resched()) |
| 365 | schedule(); |
Jason Wang | 04b96e5 | 2016-04-25 22:14:33 -0400 | [diff] [blame] | 366 | } |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 367 | } |
Michael S. Tsirkin | 64e1c80 | 2010-10-06 15:34:45 +0200 | [diff] [blame] | 368 | unuse_mm(dev->mm); |
Jens Freimann | d7ffde3 | 2012-06-26 00:59:58 +0000 | [diff] [blame] | 369 | set_fs(oldfs); |
Michael S. Tsirkin | 64e1c80 | 2010-10-06 15:34:45 +0200 | [diff] [blame] | 370 | return 0; |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 371 | } |
| 372 | |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 373 | static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq) |
| 374 | { |
| 375 | kfree(vq->indirect); |
| 376 | vq->indirect = NULL; |
| 377 | kfree(vq->log); |
| 378 | vq->log = NULL; |
| 379 | kfree(vq->heads); |
| 380 | vq->heads = NULL; |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Jason Wang | e0e9b40 | 2010-09-14 23:53:05 +0800 | [diff] [blame] | 383 | /* Helper to allocate iovec buffers for all vqs. */ |
| 384 | static long vhost_dev_alloc_iovecs(struct vhost_dev *dev) |
| 385 | { |
Asias He | 6d5e6aa | 2013-05-06 16:38:23 +0800 | [diff] [blame] | 386 | struct vhost_virtqueue *vq; |
Jason Wang | e0e9b40 | 2010-09-14 23:53:05 +0800 | [diff] [blame] | 387 | int i; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 388 | |
Jason Wang | e0e9b40 | 2010-09-14 23:53:05 +0800 | [diff] [blame] | 389 | for (i = 0; i < dev->nvqs; ++i) { |
Asias He | 6d5e6aa | 2013-05-06 16:38:23 +0800 | [diff] [blame] | 390 | vq = dev->vqs[i]; |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 391 | vq->indirect = kmalloc_array(UIO_MAXIOV, |
| 392 | sizeof(*vq->indirect), |
| 393 | GFP_KERNEL); |
Jason Wang | b46a0bf | 2019-01-28 15:05:05 +0800 | [diff] [blame] | 394 | vq->log = kmalloc_array(dev->iov_limit, sizeof(*vq->log), |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 395 | GFP_KERNEL); |
Jason Wang | b46a0bf | 2019-01-28 15:05:05 +0800 | [diff] [blame] | 396 | vq->heads = kmalloc_array(dev->iov_limit, sizeof(*vq->heads), |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 397 | GFP_KERNEL); |
Asias He | 6d5e6aa | 2013-05-06 16:38:23 +0800 | [diff] [blame] | 398 | if (!vq->indirect || !vq->log || !vq->heads) |
Jason Wang | e0e9b40 | 2010-09-14 23:53:05 +0800 | [diff] [blame] | 399 | goto err_nomem; |
| 400 | } |
| 401 | return 0; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 402 | |
Jason Wang | e0e9b40 | 2010-09-14 23:53:05 +0800 | [diff] [blame] | 403 | err_nomem: |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 404 | for (; i >= 0; --i) |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 405 | vhost_vq_free_iovecs(dev->vqs[i]); |
Jason Wang | e0e9b40 | 2010-09-14 23:53:05 +0800 | [diff] [blame] | 406 | return -ENOMEM; |
| 407 | } |
| 408 | |
| 409 | static void vhost_dev_free_iovecs(struct vhost_dev *dev) |
| 410 | { |
| 411 | int i; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 412 | |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 413 | for (i = 0; i < dev->nvqs; ++i) |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 414 | vhost_vq_free_iovecs(dev->vqs[i]); |
Jason Wang | e0e9b40 | 2010-09-14 23:53:05 +0800 | [diff] [blame] | 415 | } |
| 416 | |
Jason Wang | e82b9b0 | 2019-05-17 00:29:49 -0400 | [diff] [blame] | 417 | bool vhost_exceeds_weight(struct vhost_virtqueue *vq, |
| 418 | int pkts, int total_len) |
| 419 | { |
| 420 | struct vhost_dev *dev = vq->dev; |
| 421 | |
| 422 | if ((dev->byte_weight && total_len >= dev->byte_weight) || |
| 423 | pkts >= dev->weight) { |
| 424 | vhost_poll_queue(&vq->poll); |
| 425 | return true; |
| 426 | } |
| 427 | |
| 428 | return false; |
| 429 | } |
| 430 | EXPORT_SYMBOL_GPL(vhost_exceeds_weight); |
| 431 | |
Jason Wang | 4942e82 | 2019-05-24 04:12:16 -0400 | [diff] [blame] | 432 | static size_t vhost_get_avail_size(struct vhost_virtqueue *vq, |
| 433 | unsigned int num) |
| 434 | { |
| 435 | size_t event __maybe_unused = |
| 436 | vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0; |
| 437 | |
| 438 | return sizeof(*vq->avail) + |
| 439 | sizeof(*vq->avail->ring) * num + event; |
| 440 | } |
| 441 | |
| 442 | static size_t vhost_get_used_size(struct vhost_virtqueue *vq, |
| 443 | unsigned int num) |
| 444 | { |
| 445 | size_t event __maybe_unused = |
| 446 | vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0; |
| 447 | |
| 448 | return sizeof(*vq->used) + |
| 449 | sizeof(*vq->used->ring) * num + event; |
| 450 | } |
| 451 | |
| 452 | static size_t vhost_get_desc_size(struct vhost_virtqueue *vq, |
| 453 | unsigned int num) |
| 454 | { |
| 455 | return sizeof(*vq->desc) * num; |
| 456 | } |
| 457 | |
Zhi Yong Wu | 59566b6e | 2013-12-07 04:13:03 +0800 | [diff] [blame] | 458 | void vhost_dev_init(struct vhost_dev *dev, |
Jason Wang | e82b9b0 | 2019-05-17 00:29:49 -0400 | [diff] [blame] | 459 | struct vhost_virtqueue **vqs, int nvqs, |
| 460 | int iov_limit, int weight, int byte_weight) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 461 | { |
Asias He | 6d5e6aa | 2013-05-06 16:38:23 +0800 | [diff] [blame] | 462 | struct vhost_virtqueue *vq; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 463 | int i; |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 464 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 465 | dev->vqs = vqs; |
| 466 | dev->nvqs = nvqs; |
| 467 | mutex_init(&dev->mutex); |
| 468 | dev->log_ctx = NULL; |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 469 | dev->umem = NULL; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 470 | dev->iotlb = NULL; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 471 | dev->mm = NULL; |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 472 | dev->worker = NULL; |
Jason Wang | b46a0bf | 2019-01-28 15:05:05 +0800 | [diff] [blame] | 473 | dev->iov_limit = iov_limit; |
Jason Wang | e82b9b0 | 2019-05-17 00:29:49 -0400 | [diff] [blame] | 474 | dev->weight = weight; |
| 475 | dev->byte_weight = byte_weight; |
Jason Wang | 04b96e5 | 2016-04-25 22:14:33 -0400 | [diff] [blame] | 476 | init_llist_head(&dev->work_list); |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 477 | init_waitqueue_head(&dev->wait); |
| 478 | INIT_LIST_HEAD(&dev->read_list); |
| 479 | INIT_LIST_HEAD(&dev->pending_list); |
| 480 | spin_lock_init(&dev->iotlb_lock); |
Michael S. Tsirkin | 3d2c7d3 | 2019-08-10 13:53:21 -0400 | [diff] [blame] | 481 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 482 | |
| 483 | for (i = 0; i < dev->nvqs; ++i) { |
Asias He | 6d5e6aa | 2013-05-06 16:38:23 +0800 | [diff] [blame] | 484 | vq = dev->vqs[i]; |
| 485 | vq->log = NULL; |
| 486 | vq->indirect = NULL; |
| 487 | vq->heads = NULL; |
| 488 | vq->dev = dev; |
| 489 | mutex_init(&vq->mutex); |
| 490 | vhost_vq_reset(dev, vq); |
| 491 | if (vq->handle_kick) |
| 492 | vhost_poll_init(&vq->poll, vq->handle_kick, |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 493 | EPOLLIN, dev); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 494 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 495 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 496 | EXPORT_SYMBOL_GPL(vhost_dev_init); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 497 | |
| 498 | /* Caller should have device mutex */ |
| 499 | long vhost_dev_check_owner(struct vhost_dev *dev) |
| 500 | { |
| 501 | /* Are you the owner? If not, I don't think you mean to do that */ |
| 502 | return dev->mm == current->mm ? 0 : -EPERM; |
| 503 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 504 | EXPORT_SYMBOL_GPL(vhost_dev_check_owner); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 505 | |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 506 | struct vhost_attach_cgroups_struct { |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 507 | struct vhost_work work; |
| 508 | struct task_struct *owner; |
| 509 | int ret; |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 510 | }; |
| 511 | |
| 512 | static void vhost_attach_cgroups_work(struct vhost_work *work) |
| 513 | { |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 514 | struct vhost_attach_cgroups_struct *s; |
| 515 | |
| 516 | s = container_of(work, struct vhost_attach_cgroups_struct, work); |
| 517 | s->ret = cgroup_attach_task_all(s->owner, current); |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | static int vhost_attach_cgroups(struct vhost_dev *dev) |
| 521 | { |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 522 | struct vhost_attach_cgroups_struct attach; |
| 523 | |
| 524 | attach.owner = current; |
| 525 | vhost_work_init(&attach.work, vhost_attach_cgroups_work); |
| 526 | vhost_work_queue(dev, &attach.work); |
| 527 | vhost_work_flush(dev, &attach.work); |
| 528 | return attach.ret; |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 529 | } |
| 530 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 531 | /* Caller should have device mutex */ |
Michael S. Tsirkin | 05c0535 | 2013-06-06 15:20:39 +0300 | [diff] [blame] | 532 | bool vhost_dev_has_owner(struct vhost_dev *dev) |
| 533 | { |
| 534 | return dev->mm; |
| 535 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 536 | EXPORT_SYMBOL_GPL(vhost_dev_has_owner); |
Michael S. Tsirkin | 05c0535 | 2013-06-06 15:20:39 +0300 | [diff] [blame] | 537 | |
| 538 | /* Caller should have device mutex */ |
Asias He | 54db63c | 2013-05-06 11:15:59 +0800 | [diff] [blame] | 539 | long vhost_dev_set_owner(struct vhost_dev *dev) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 540 | { |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 541 | struct task_struct *worker; |
| 542 | int err; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 543 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 544 | /* Is there an owner already? */ |
Michael S. Tsirkin | 05c0535 | 2013-06-06 15:20:39 +0300 | [diff] [blame] | 545 | if (vhost_dev_has_owner(dev)) { |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 546 | err = -EBUSY; |
| 547 | goto err_mm; |
| 548 | } |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 549 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 550 | /* No owner, become one */ |
| 551 | dev->mm = get_task_mm(current); |
Andrey Konovalov | 8f6a7f9 | 2019-12-04 16:52:50 -0800 | [diff] [blame] | 552 | dev->kcov_handle = kcov_common_handle(); |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 553 | worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid); |
| 554 | if (IS_ERR(worker)) { |
| 555 | err = PTR_ERR(worker); |
| 556 | goto err_worker; |
| 557 | } |
| 558 | |
| 559 | dev->worker = worker; |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 560 | wake_up_process(worker); /* avoid contributing to loadavg */ |
| 561 | |
| 562 | err = vhost_attach_cgroups(dev); |
Michael S. Tsirkin | 9e3d195 | 2010-07-27 22:56:50 +0300 | [diff] [blame] | 563 | if (err) |
| 564 | goto err_cgroup; |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 565 | |
Jason Wang | e0e9b40 | 2010-09-14 23:53:05 +0800 | [diff] [blame] | 566 | err = vhost_dev_alloc_iovecs(dev); |
| 567 | if (err) |
| 568 | goto err_cgroup; |
| 569 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 570 | return 0; |
Michael S. Tsirkin | 9e3d195 | 2010-07-27 22:56:50 +0300 | [diff] [blame] | 571 | err_cgroup: |
| 572 | kthread_stop(worker); |
Michael S. Tsirkin | 615cc22 | 2010-09-02 14:16:36 +0300 | [diff] [blame] | 573 | dev->worker = NULL; |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 574 | err_worker: |
| 575 | if (dev->mm) |
| 576 | mmput(dev->mm); |
| 577 | dev->mm = NULL; |
Andrey Konovalov | 8f6a7f9 | 2019-12-04 16:52:50 -0800 | [diff] [blame] | 578 | dev->kcov_handle = 0; |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 579 | err_mm: |
| 580 | return err; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 581 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 582 | EXPORT_SYMBOL_GPL(vhost_dev_set_owner); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 583 | |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 584 | struct vhost_umem *vhost_dev_reset_owner_prepare(void) |
| 585 | { |
Michal Hocko | 6c5ab65 | 2017-05-08 15:57:15 -0700 | [diff] [blame] | 586 | return kvzalloc(sizeof(struct vhost_umem), GFP_KERNEL); |
Michael S. Tsirkin | 150b9e5 | 2013-04-28 17:12:08 +0300 | [diff] [blame] | 587 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 588 | EXPORT_SYMBOL_GPL(vhost_dev_reset_owner_prepare); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 589 | |
Michael S. Tsirkin | 150b9e5 | 2013-04-28 17:12:08 +0300 | [diff] [blame] | 590 | /* Caller should have device mutex */ |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 591 | void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_umem *umem) |
Michael S. Tsirkin | 150b9e5 | 2013-04-28 17:12:08 +0300 | [diff] [blame] | 592 | { |
Michael S. Tsirkin | 47283be | 2014-06-05 15:20:27 +0300 | [diff] [blame] | 593 | int i; |
| 594 | |
夷则(Caspar) | f6f93f7 | 2017-12-25 00:08:58 +0800 | [diff] [blame] | 595 | vhost_dev_cleanup(dev); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 596 | |
Michael S. Tsirkin | 150b9e5 | 2013-04-28 17:12:08 +0300 | [diff] [blame] | 597 | /* Restore memory to default empty mapping. */ |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 598 | INIT_LIST_HEAD(&umem->umem_list); |
| 599 | dev->umem = umem; |
Michael S. Tsirkin | 47283be | 2014-06-05 15:20:27 +0300 | [diff] [blame] | 600 | /* We don't need VQ locks below since vhost_dev_cleanup makes sure |
| 601 | * VQs aren't running. |
| 602 | */ |
| 603 | for (i = 0; i < dev->nvqs; ++i) |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 604 | dev->vqs[i]->umem = umem; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 605 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 606 | EXPORT_SYMBOL_GPL(vhost_dev_reset_owner); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 607 | |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 608 | void vhost_dev_stop(struct vhost_dev *dev) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 609 | { |
| 610 | int i; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 611 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 612 | for (i = 0; i < dev->nvqs; ++i) { |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 613 | if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) { |
| 614 | vhost_poll_stop(&dev->vqs[i]->poll); |
| 615 | vhost_poll_flush(&dev->vqs[i]->poll); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 616 | } |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 617 | } |
| 618 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 619 | EXPORT_SYMBOL_GPL(vhost_dev_stop); |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 620 | |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 621 | static void vhost_umem_free(struct vhost_umem *umem, |
| 622 | struct vhost_umem_node *node) |
| 623 | { |
| 624 | vhost_umem_interval_tree_remove(node, &umem->umem_tree); |
| 625 | list_del(&node->link); |
| 626 | kfree(node); |
| 627 | umem->numem--; |
| 628 | } |
| 629 | |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 630 | static void vhost_umem_clean(struct vhost_umem *umem) |
| 631 | { |
| 632 | struct vhost_umem_node *node, *tmp; |
| 633 | |
| 634 | if (!umem) |
| 635 | return; |
| 636 | |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 637 | list_for_each_entry_safe(node, tmp, &umem->umem_list, link) |
| 638 | vhost_umem_free(umem, node); |
| 639 | |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 640 | kvfree(umem); |
| 641 | } |
| 642 | |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 643 | static void vhost_clear_msg(struct vhost_dev *dev) |
| 644 | { |
| 645 | struct vhost_msg_node *node, *n; |
| 646 | |
| 647 | spin_lock(&dev->iotlb_lock); |
| 648 | |
| 649 | list_for_each_entry_safe(node, n, &dev->read_list, node) { |
| 650 | list_del(&node->node); |
| 651 | kfree(node); |
| 652 | } |
| 653 | |
| 654 | list_for_each_entry_safe(node, n, &dev->pending_list, node) { |
| 655 | list_del(&node->node); |
| 656 | kfree(node); |
| 657 | } |
| 658 | |
| 659 | spin_unlock(&dev->iotlb_lock); |
| 660 | } |
| 661 | |
夷则(Caspar) | f6f93f7 | 2017-12-25 00:08:58 +0800 | [diff] [blame] | 662 | void vhost_dev_cleanup(struct vhost_dev *dev) |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 663 | { |
| 664 | int i; |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 665 | |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 666 | for (i = 0; i < dev->nvqs; ++i) { |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 667 | if (dev->vqs[i]->error_ctx) |
| 668 | eventfd_ctx_put(dev->vqs[i]->error_ctx); |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 669 | if (dev->vqs[i]->kick) |
| 670 | fput(dev->vqs[i]->kick); |
| 671 | if (dev->vqs[i]->call_ctx) |
| 672 | eventfd_ctx_put(dev->vqs[i]->call_ctx); |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 673 | vhost_vq_reset(dev, dev->vqs[i]); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 674 | } |
Jason Wang | e0e9b40 | 2010-09-14 23:53:05 +0800 | [diff] [blame] | 675 | vhost_dev_free_iovecs(dev); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 676 | if (dev->log_ctx) |
| 677 | eventfd_ctx_put(dev->log_ctx); |
| 678 | dev->log_ctx = NULL; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 679 | /* No one will access memory at this point */ |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 680 | vhost_umem_clean(dev->umem); |
| 681 | dev->umem = NULL; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 682 | vhost_umem_clean(dev->iotlb); |
| 683 | dev->iotlb = NULL; |
| 684 | vhost_clear_msg(dev); |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 685 | wake_up_interruptible_poll(&dev->wait, EPOLLIN | EPOLLRDNORM); |
Jason Wang | 04b96e5 | 2016-04-25 22:14:33 -0400 | [diff] [blame] | 686 | WARN_ON(!llist_empty(&dev->work_list)); |
Eric Dumazet | 78b620c | 2010-08-31 02:05:57 +0000 | [diff] [blame] | 687 | if (dev->worker) { |
| 688 | kthread_stop(dev->worker); |
| 689 | dev->worker = NULL; |
Andrey Konovalov | 8f6a7f9 | 2019-12-04 16:52:50 -0800 | [diff] [blame] | 690 | dev->kcov_handle = 0; |
Eric Dumazet | 78b620c | 2010-08-31 02:05:57 +0000 | [diff] [blame] | 691 | } |
Michael S. Tsirkin | 3d2c7d3 | 2019-08-10 13:53:21 -0400 | [diff] [blame] | 692 | if (dev->mm) |
Michael S. Tsirkin | 533a19b | 2010-10-06 15:34:38 +0200 | [diff] [blame] | 693 | mmput(dev->mm); |
| 694 | dev->mm = NULL; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 695 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 696 | EXPORT_SYMBOL_GPL(vhost_dev_cleanup); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 697 | |
Stefan Hajnoczi | ddd3d40 | 2018-04-11 10:35:41 +0800 | [diff] [blame] | 698 | static bool log_access_ok(void __user *log_base, u64 addr, unsigned long sz) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 699 | { |
| 700 | u64 a = addr / VHOST_PAGE_SIZE / 8; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 701 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 702 | /* Make sure 64 bit math will not overflow. */ |
| 703 | if (a > ULONG_MAX - (unsigned long)log_base || |
| 704 | a + (unsigned long)log_base > ULONG_MAX) |
Stefan Hajnoczi | ddd3d40 | 2018-04-11 10:35:41 +0800 | [diff] [blame] | 705 | return false; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 706 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 707 | return access_ok(log_base + a, |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 708 | (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8); |
| 709 | } |
| 710 | |
Michael S. Tsirkin | ec33d03 | 2016-08-01 23:20:53 +0300 | [diff] [blame] | 711 | static bool vhost_overflow(u64 uaddr, u64 size) |
| 712 | { |
| 713 | /* Make sure 64 bit math will not overflow. */ |
| 714 | return uaddr > ULONG_MAX || size > ULONG_MAX || uaddr > ULONG_MAX - size; |
| 715 | } |
| 716 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 717 | /* Caller should have vq mutex and device mutex. */ |
Stefan Hajnoczi | ddd3d40 | 2018-04-11 10:35:41 +0800 | [diff] [blame] | 718 | static bool vq_memory_access_ok(void __user *log_base, struct vhost_umem *umem, |
| 719 | int log_all) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 720 | { |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 721 | struct vhost_umem_node *node; |
Jeff Dike | 179b284 | 2010-04-07 09:59:10 -0400 | [diff] [blame] | 722 | |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 723 | if (!umem) |
Stefan Hajnoczi | ddd3d40 | 2018-04-11 10:35:41 +0800 | [diff] [blame] | 724 | return false; |
Jeff Dike | 179b284 | 2010-04-07 09:59:10 -0400 | [diff] [blame] | 725 | |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 726 | list_for_each_entry(node, &umem->umem_list, link) { |
| 727 | unsigned long a = node->userspace_addr; |
| 728 | |
Michael S. Tsirkin | ec33d03 | 2016-08-01 23:20:53 +0300 | [diff] [blame] | 729 | if (vhost_overflow(node->userspace_addr, node->size)) |
Stefan Hajnoczi | ddd3d40 | 2018-04-11 10:35:41 +0800 | [diff] [blame] | 730 | return false; |
Michael S. Tsirkin | ec33d03 | 2016-08-01 23:20:53 +0300 | [diff] [blame] | 731 | |
| 732 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 733 | if (!access_ok((void __user *)a, |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 734 | node->size)) |
Stefan Hajnoczi | ddd3d40 | 2018-04-11 10:35:41 +0800 | [diff] [blame] | 735 | return false; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 736 | else if (log_all && !log_access_ok(log_base, |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 737 | node->start, |
| 738 | node->size)) |
Stefan Hajnoczi | ddd3d40 | 2018-04-11 10:35:41 +0800 | [diff] [blame] | 739 | return false; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 740 | } |
Stefan Hajnoczi | ddd3d40 | 2018-04-11 10:35:41 +0800 | [diff] [blame] | 741 | return true; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Jason Wang | f889491 | 2017-02-28 17:56:02 +0800 | [diff] [blame] | 744 | static inline void __user *vhost_vq_meta_fetch(struct vhost_virtqueue *vq, |
| 745 | u64 addr, unsigned int size, |
| 746 | int type) |
| 747 | { |
| 748 | const struct vhost_umem_node *node = vq->meta_iotlb[type]; |
| 749 | |
| 750 | if (!node) |
| 751 | return NULL; |
| 752 | |
| 753 | return (void *)(uintptr_t)(node->userspace_addr + addr - node->start); |
| 754 | } |
| 755 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 756 | /* Can we switch to this memory table? */ |
| 757 | /* Caller should have device mutex but not vq mutex */ |
Stefan Hajnoczi | ddd3d40 | 2018-04-11 10:35:41 +0800 | [diff] [blame] | 758 | static bool memory_access_ok(struct vhost_dev *d, struct vhost_umem *umem, |
| 759 | int log_all) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 760 | { |
| 761 | int i; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 762 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 763 | for (i = 0; i < d->nvqs; ++i) { |
Stefan Hajnoczi | ddd3d40 | 2018-04-11 10:35:41 +0800 | [diff] [blame] | 764 | bool ok; |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 765 | bool log; |
| 766 | |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 767 | mutex_lock(&d->vqs[i]->mutex); |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 768 | log = log_all || vhost_has_feature(d->vqs[i], VHOST_F_LOG_ALL); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 769 | /* If ring is inactive, will check when it's enabled. */ |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 770 | if (d->vqs[i]->private_data) |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 771 | ok = vq_memory_access_ok(d->vqs[i]->log_base, |
| 772 | umem, log); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 773 | else |
Stefan Hajnoczi | ddd3d40 | 2018-04-11 10:35:41 +0800 | [diff] [blame] | 774 | ok = true; |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 775 | mutex_unlock(&d->vqs[i]->mutex); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 776 | if (!ok) |
Stefan Hajnoczi | ddd3d40 | 2018-04-11 10:35:41 +0800 | [diff] [blame] | 777 | return false; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 778 | } |
Stefan Hajnoczi | ddd3d40 | 2018-04-11 10:35:41 +0800 | [diff] [blame] | 779 | return true; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 782 | static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len, |
| 783 | struct iovec iov[], int iov_size, int access); |
Jason Wang | bfe2bc5 | 2016-06-23 02:04:30 -0400 | [diff] [blame] | 784 | |
Michael S. Tsirkin | 72952cc | 2016-12-06 06:01:41 +0200 | [diff] [blame] | 785 | static int vhost_copy_to_user(struct vhost_virtqueue *vq, void __user *to, |
Jason Wang | bfe2bc5 | 2016-06-23 02:04:30 -0400 | [diff] [blame] | 786 | const void *from, unsigned size) |
| 787 | { |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 788 | int ret; |
Jason Wang | bfe2bc5 | 2016-06-23 02:04:30 -0400 | [diff] [blame] | 789 | |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 790 | if (!vq->iotlb) |
| 791 | return __copy_to_user(to, from, size); |
| 792 | else { |
| 793 | /* This function should be called after iotlb |
| 794 | * prefetch, which means we're sure that all vq |
| 795 | * could be access through iotlb. So -EAGAIN should |
| 796 | * not happen in this case. |
| 797 | */ |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 798 | struct iov_iter t; |
Jason Wang | f889491 | 2017-02-28 17:56:02 +0800 | [diff] [blame] | 799 | void __user *uaddr = vhost_vq_meta_fetch(vq, |
| 800 | (u64)(uintptr_t)to, size, |
Eric Auger | 7ced6c9 | 2018-04-11 15:30:38 +0200 | [diff] [blame] | 801 | VHOST_ADDR_USED); |
Jason Wang | f889491 | 2017-02-28 17:56:02 +0800 | [diff] [blame] | 802 | |
| 803 | if (uaddr) |
| 804 | return __copy_to_user(uaddr, from, size); |
| 805 | |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 806 | ret = translate_desc(vq, (u64)(uintptr_t)to, size, vq->iotlb_iov, |
| 807 | ARRAY_SIZE(vq->iotlb_iov), |
| 808 | VHOST_ACCESS_WO); |
| 809 | if (ret < 0) |
| 810 | goto out; |
| 811 | iov_iter_init(&t, WRITE, vq->iotlb_iov, ret, size); |
| 812 | ret = copy_to_iter(from, size, &t); |
| 813 | if (ret == size) |
| 814 | ret = 0; |
| 815 | } |
| 816 | out: |
| 817 | return ret; |
| 818 | } |
Jason Wang | bfe2bc5 | 2016-06-23 02:04:30 -0400 | [diff] [blame] | 819 | |
| 820 | static int vhost_copy_from_user(struct vhost_virtqueue *vq, void *to, |
Michael S. Tsirkin | 72952cc | 2016-12-06 06:01:41 +0200 | [diff] [blame] | 821 | void __user *from, unsigned size) |
Jason Wang | bfe2bc5 | 2016-06-23 02:04:30 -0400 | [diff] [blame] | 822 | { |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 823 | int ret; |
| 824 | |
| 825 | if (!vq->iotlb) |
| 826 | return __copy_from_user(to, from, size); |
| 827 | else { |
| 828 | /* This function should be called after iotlb |
| 829 | * prefetch, which means we're sure that vq |
| 830 | * could be access through iotlb. So -EAGAIN should |
| 831 | * not happen in this case. |
| 832 | */ |
Jason Wang | f889491 | 2017-02-28 17:56:02 +0800 | [diff] [blame] | 833 | void __user *uaddr = vhost_vq_meta_fetch(vq, |
| 834 | (u64)(uintptr_t)from, size, |
| 835 | VHOST_ADDR_DESC); |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 836 | struct iov_iter f; |
Jason Wang | f889491 | 2017-02-28 17:56:02 +0800 | [diff] [blame] | 837 | |
| 838 | if (uaddr) |
| 839 | return __copy_from_user(to, uaddr, size); |
| 840 | |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 841 | ret = translate_desc(vq, (u64)(uintptr_t)from, size, vq->iotlb_iov, |
| 842 | ARRAY_SIZE(vq->iotlb_iov), |
| 843 | VHOST_ACCESS_RO); |
| 844 | if (ret < 0) { |
| 845 | vq_err(vq, "IOTLB translation failure: uaddr " |
| 846 | "%p size 0x%llx\n", from, |
| 847 | (unsigned long long) size); |
| 848 | goto out; |
| 849 | } |
| 850 | iov_iter_init(&f, READ, vq->iotlb_iov, ret, size); |
| 851 | ret = copy_from_iter(to, size, &f); |
| 852 | if (ret == size) |
| 853 | ret = 0; |
| 854 | } |
| 855 | |
| 856 | out: |
| 857 | return ret; |
| 858 | } |
| 859 | |
Jason Wang | f889491 | 2017-02-28 17:56:02 +0800 | [diff] [blame] | 860 | static void __user *__vhost_get_user_slow(struct vhost_virtqueue *vq, |
| 861 | void __user *addr, unsigned int size, |
| 862 | int type) |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 863 | { |
| 864 | int ret; |
| 865 | |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 866 | ret = translate_desc(vq, (u64)(uintptr_t)addr, size, vq->iotlb_iov, |
| 867 | ARRAY_SIZE(vq->iotlb_iov), |
| 868 | VHOST_ACCESS_RO); |
| 869 | if (ret < 0) { |
| 870 | vq_err(vq, "IOTLB translation failure: uaddr " |
| 871 | "%p size 0x%llx\n", addr, |
| 872 | (unsigned long long) size); |
| 873 | return NULL; |
| 874 | } |
| 875 | |
| 876 | if (ret != 1 || vq->iotlb_iov[0].iov_len != size) { |
| 877 | vq_err(vq, "Non atomic userspace memory access: uaddr " |
| 878 | "%p size 0x%llx\n", addr, |
| 879 | (unsigned long long) size); |
| 880 | return NULL; |
| 881 | } |
| 882 | |
| 883 | return vq->iotlb_iov[0].iov_base; |
| 884 | } |
| 885 | |
Jason Wang | f889491 | 2017-02-28 17:56:02 +0800 | [diff] [blame] | 886 | /* This function should be called after iotlb |
| 887 | * prefetch, which means we're sure that vq |
| 888 | * could be access through iotlb. So -EAGAIN should |
| 889 | * not happen in this case. |
| 890 | */ |
| 891 | static inline void __user *__vhost_get_user(struct vhost_virtqueue *vq, |
| 892 | void *addr, unsigned int size, |
| 893 | int type) |
| 894 | { |
| 895 | void __user *uaddr = vhost_vq_meta_fetch(vq, |
| 896 | (u64)(uintptr_t)addr, size, type); |
| 897 | if (uaddr) |
| 898 | return uaddr; |
| 899 | |
| 900 | return __vhost_get_user_slow(vq, addr, size, type); |
| 901 | } |
| 902 | |
| 903 | #define vhost_put_user(vq, x, ptr) \ |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 904 | ({ \ |
| 905 | int ret = -EFAULT; \ |
| 906 | if (!vq->iotlb) { \ |
| 907 | ret = __put_user(x, ptr); \ |
| 908 | } else { \ |
| 909 | __typeof__(ptr) to = \ |
Jason Wang | f889491 | 2017-02-28 17:56:02 +0800 | [diff] [blame] | 910 | (__typeof__(ptr)) __vhost_get_user(vq, ptr, \ |
| 911 | sizeof(*ptr), VHOST_ADDR_USED); \ |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 912 | if (to != NULL) \ |
| 913 | ret = __put_user(x, to); \ |
| 914 | else \ |
| 915 | ret = -EFAULT; \ |
| 916 | } \ |
| 917 | ret; \ |
| 918 | }) |
| 919 | |
Jason Wang | 7b5d753 | 2019-05-24 04:12:14 -0400 | [diff] [blame] | 920 | static inline int vhost_put_avail_event(struct vhost_virtqueue *vq) |
| 921 | { |
| 922 | return vhost_put_user(vq, cpu_to_vhost16(vq, vq->avail_idx), |
| 923 | vhost_avail_event(vq)); |
| 924 | } |
| 925 | |
| 926 | static inline int vhost_put_used(struct vhost_virtqueue *vq, |
| 927 | struct vring_used_elem *head, int idx, |
| 928 | int count) |
| 929 | { |
| 930 | return vhost_copy_to_user(vq, vq->used->ring + idx, head, |
| 931 | count * sizeof(*head)); |
| 932 | } |
| 933 | |
| 934 | static inline int vhost_put_used_flags(struct vhost_virtqueue *vq) |
| 935 | |
| 936 | { |
| 937 | return vhost_put_user(vq, cpu_to_vhost16(vq, vq->used_flags), |
| 938 | &vq->used->flags); |
| 939 | } |
| 940 | |
| 941 | static inline int vhost_put_used_idx(struct vhost_virtqueue *vq) |
| 942 | |
| 943 | { |
| 944 | return vhost_put_user(vq, cpu_to_vhost16(vq, vq->last_used_idx), |
| 945 | &vq->used->idx); |
| 946 | } |
| 947 | |
Jason Wang | f889491 | 2017-02-28 17:56:02 +0800 | [diff] [blame] | 948 | #define vhost_get_user(vq, x, ptr, type) \ |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 949 | ({ \ |
| 950 | int ret; \ |
| 951 | if (!vq->iotlb) { \ |
| 952 | ret = __get_user(x, ptr); \ |
| 953 | } else { \ |
| 954 | __typeof__(ptr) from = \ |
Jason Wang | f889491 | 2017-02-28 17:56:02 +0800 | [diff] [blame] | 955 | (__typeof__(ptr)) __vhost_get_user(vq, ptr, \ |
| 956 | sizeof(*ptr), \ |
| 957 | type); \ |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 958 | if (from != NULL) \ |
| 959 | ret = __get_user(x, from); \ |
| 960 | else \ |
| 961 | ret = -EFAULT; \ |
| 962 | } \ |
| 963 | ret; \ |
| 964 | }) |
| 965 | |
Jason Wang | f889491 | 2017-02-28 17:56:02 +0800 | [diff] [blame] | 966 | #define vhost_get_avail(vq, x, ptr) \ |
| 967 | vhost_get_user(vq, x, ptr, VHOST_ADDR_AVAIL) |
| 968 | |
| 969 | #define vhost_get_used(vq, x, ptr) \ |
| 970 | vhost_get_user(vq, x, ptr, VHOST_ADDR_USED) |
| 971 | |
Jason Wang | 86a07da | 2018-12-13 10:53:39 +0800 | [diff] [blame] | 972 | static void vhost_dev_lock_vqs(struct vhost_dev *d) |
| 973 | { |
| 974 | int i = 0; |
| 975 | for (i = 0; i < d->nvqs; ++i) |
| 976 | mutex_lock_nested(&d->vqs[i]->mutex, i); |
| 977 | } |
| 978 | |
| 979 | static void vhost_dev_unlock_vqs(struct vhost_dev *d) |
| 980 | { |
| 981 | int i = 0; |
| 982 | for (i = 0; i < d->nvqs; ++i) |
| 983 | mutex_unlock(&d->vqs[i]->mutex); |
| 984 | } |
| 985 | |
Jason Wang | 7b5d753 | 2019-05-24 04:12:14 -0400 | [diff] [blame] | 986 | static inline int vhost_get_avail_idx(struct vhost_virtqueue *vq, |
| 987 | __virtio16 *idx) |
| 988 | { |
| 989 | return vhost_get_avail(vq, *idx, &vq->avail->idx); |
| 990 | } |
| 991 | |
| 992 | static inline int vhost_get_avail_head(struct vhost_virtqueue *vq, |
| 993 | __virtio16 *head, int idx) |
| 994 | { |
| 995 | return vhost_get_avail(vq, *head, |
| 996 | &vq->avail->ring[idx & (vq->num - 1)]); |
| 997 | } |
| 998 | |
| 999 | static inline int vhost_get_avail_flags(struct vhost_virtqueue *vq, |
| 1000 | __virtio16 *flags) |
| 1001 | { |
| 1002 | return vhost_get_avail(vq, *flags, &vq->avail->flags); |
| 1003 | } |
| 1004 | |
| 1005 | static inline int vhost_get_used_event(struct vhost_virtqueue *vq, |
| 1006 | __virtio16 *event) |
| 1007 | { |
| 1008 | return vhost_get_avail(vq, *event, vhost_used_event(vq)); |
| 1009 | } |
| 1010 | |
| 1011 | static inline int vhost_get_used_idx(struct vhost_virtqueue *vq, |
| 1012 | __virtio16 *idx) |
| 1013 | { |
| 1014 | return vhost_get_used(vq, *idx, &vq->used->idx); |
| 1015 | } |
| 1016 | |
| 1017 | static inline int vhost_get_desc(struct vhost_virtqueue *vq, |
| 1018 | struct vring_desc *desc, int idx) |
| 1019 | { |
| 1020 | return vhost_copy_from_user(vq, desc, vq->desc + idx, sizeof(*desc)); |
| 1021 | } |
| 1022 | |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1023 | static int vhost_new_umem_range(struct vhost_umem *umem, |
| 1024 | u64 start, u64 size, u64 end, |
| 1025 | u64 userspace_addr, int perm) |
| 1026 | { |
Jason Wang | 813dbeb | 2019-04-09 12:10:25 +0800 | [diff] [blame] | 1027 | struct vhost_umem_node *tmp, *node; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1028 | |
Jason Wang | 813dbeb | 2019-04-09 12:10:25 +0800 | [diff] [blame] | 1029 | if (!size) |
| 1030 | return -EFAULT; |
| 1031 | |
| 1032 | node = kmalloc(sizeof(*node), GFP_ATOMIC); |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1033 | if (!node) |
| 1034 | return -ENOMEM; |
| 1035 | |
| 1036 | if (umem->numem == max_iotlb_entries) { |
| 1037 | tmp = list_first_entry(&umem->umem_list, typeof(*tmp), link); |
| 1038 | vhost_umem_free(umem, tmp); |
| 1039 | } |
| 1040 | |
| 1041 | node->start = start; |
| 1042 | node->size = size; |
| 1043 | node->last = end; |
| 1044 | node->userspace_addr = userspace_addr; |
| 1045 | node->perm = perm; |
| 1046 | INIT_LIST_HEAD(&node->link); |
| 1047 | list_add_tail(&node->link, &umem->umem_list); |
| 1048 | vhost_umem_interval_tree_insert(node, &umem->umem_tree); |
| 1049 | umem->numem++; |
| 1050 | |
| 1051 | return 0; |
| 1052 | } |
| 1053 | |
| 1054 | static void vhost_del_umem_range(struct vhost_umem *umem, |
| 1055 | u64 start, u64 end) |
| 1056 | { |
| 1057 | struct vhost_umem_node *node; |
| 1058 | |
| 1059 | while ((node = vhost_umem_interval_tree_iter_first(&umem->umem_tree, |
| 1060 | start, end))) |
| 1061 | vhost_umem_free(umem, node); |
| 1062 | } |
| 1063 | |
| 1064 | static void vhost_iotlb_notify_vq(struct vhost_dev *d, |
| 1065 | struct vhost_iotlb_msg *msg) |
| 1066 | { |
| 1067 | struct vhost_msg_node *node, *n; |
| 1068 | |
| 1069 | spin_lock(&d->iotlb_lock); |
| 1070 | |
| 1071 | list_for_each_entry_safe(node, n, &d->pending_list, node) { |
| 1072 | struct vhost_iotlb_msg *vq_msg = &node->msg.iotlb; |
| 1073 | if (msg->iova <= vq_msg->iova && |
Jason Wang | 2d66f99 | 2018-08-24 16:53:13 +0800 | [diff] [blame] | 1074 | msg->iova + msg->size - 1 >= vq_msg->iova && |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1075 | vq_msg->type == VHOST_IOTLB_MISS) { |
| 1076 | vhost_poll_queue(&node->vq->poll); |
| 1077 | list_del(&node->node); |
| 1078 | kfree(node); |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | spin_unlock(&d->iotlb_lock); |
| 1083 | } |
| 1084 | |
Stefan Hajnoczi | ddd3d40 | 2018-04-11 10:35:41 +0800 | [diff] [blame] | 1085 | static bool umem_access_ok(u64 uaddr, u64 size, int access) |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1086 | { |
| 1087 | unsigned long a = uaddr; |
| 1088 | |
Michael S. Tsirkin | ec33d03 | 2016-08-01 23:20:53 +0300 | [diff] [blame] | 1089 | /* Make sure 64 bit math will not overflow. */ |
| 1090 | if (vhost_overflow(uaddr, size)) |
Stefan Hajnoczi | ddd3d40 | 2018-04-11 10:35:41 +0800 | [diff] [blame] | 1091 | return false; |
Michael S. Tsirkin | ec33d03 | 2016-08-01 23:20:53 +0300 | [diff] [blame] | 1092 | |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1093 | if ((access & VHOST_ACCESS_RO) && |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 1094 | !access_ok((void __user *)a, size)) |
Stefan Hajnoczi | ddd3d40 | 2018-04-11 10:35:41 +0800 | [diff] [blame] | 1095 | return false; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1096 | if ((access & VHOST_ACCESS_WO) && |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 1097 | !access_ok((void __user *)a, size)) |
Stefan Hajnoczi | ddd3d40 | 2018-04-11 10:35:41 +0800 | [diff] [blame] | 1098 | return false; |
| 1099 | return true; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1100 | } |
| 1101 | |
Michael S. Tsirkin | 72952cc | 2016-12-06 06:01:41 +0200 | [diff] [blame] | 1102 | static int vhost_process_iotlb_msg(struct vhost_dev *dev, |
| 1103 | struct vhost_iotlb_msg *msg) |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1104 | { |
| 1105 | int ret = 0; |
| 1106 | |
Jason Wang | 1b15ad6 | 2018-05-22 19:58:57 +0800 | [diff] [blame] | 1107 | mutex_lock(&dev->mutex); |
Jason Wang | 86a07da | 2018-12-13 10:53:39 +0800 | [diff] [blame] | 1108 | vhost_dev_lock_vqs(dev); |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1109 | switch (msg->type) { |
| 1110 | case VHOST_IOTLB_UPDATE: |
| 1111 | if (!dev->iotlb) { |
| 1112 | ret = -EFAULT; |
| 1113 | break; |
| 1114 | } |
Stefan Hajnoczi | ddd3d40 | 2018-04-11 10:35:41 +0800 | [diff] [blame] | 1115 | if (!umem_access_ok(msg->uaddr, msg->size, msg->perm)) { |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1116 | ret = -EFAULT; |
| 1117 | break; |
| 1118 | } |
Jason Wang | f889491 | 2017-02-28 17:56:02 +0800 | [diff] [blame] | 1119 | vhost_vq_meta_reset(dev); |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1120 | if (vhost_new_umem_range(dev->iotlb, msg->iova, msg->size, |
| 1121 | msg->iova + msg->size - 1, |
| 1122 | msg->uaddr, msg->perm)) { |
| 1123 | ret = -ENOMEM; |
| 1124 | break; |
| 1125 | } |
| 1126 | vhost_iotlb_notify_vq(dev, msg); |
| 1127 | break; |
| 1128 | case VHOST_IOTLB_INVALIDATE: |
Jason Wang | 6f3180a | 2018-01-23 17:27:26 +0800 | [diff] [blame] | 1129 | if (!dev->iotlb) { |
| 1130 | ret = -EFAULT; |
| 1131 | break; |
| 1132 | } |
Jason Wang | f889491 | 2017-02-28 17:56:02 +0800 | [diff] [blame] | 1133 | vhost_vq_meta_reset(dev); |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1134 | vhost_del_umem_range(dev->iotlb, msg->iova, |
| 1135 | msg->iova + msg->size - 1); |
| 1136 | break; |
| 1137 | default: |
| 1138 | ret = -EINVAL; |
| 1139 | break; |
| 1140 | } |
| 1141 | |
Jason Wang | 86a07da | 2018-12-13 10:53:39 +0800 | [diff] [blame] | 1142 | vhost_dev_unlock_vqs(dev); |
Jason Wang | 1b15ad6 | 2018-05-22 19:58:57 +0800 | [diff] [blame] | 1143 | mutex_unlock(&dev->mutex); |
| 1144 | |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1145 | return ret; |
| 1146 | } |
| 1147 | ssize_t vhost_chr_write_iter(struct vhost_dev *dev, |
| 1148 | struct iov_iter *from) |
| 1149 | { |
Jason Wang | 429711a | 2018-08-06 11:17:47 +0800 | [diff] [blame] | 1150 | struct vhost_iotlb_msg msg; |
| 1151 | size_t offset; |
| 1152 | int type, ret; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1153 | |
Jason Wang | 429711a | 2018-08-06 11:17:47 +0800 | [diff] [blame] | 1154 | ret = copy_from_iter(&type, sizeof(type), from); |
Pavel Tikhomirov | 74ad741 | 2018-12-13 17:53:50 +0300 | [diff] [blame] | 1155 | if (ret != sizeof(type)) { |
| 1156 | ret = -EINVAL; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1157 | goto done; |
Pavel Tikhomirov | 74ad741 | 2018-12-13 17:53:50 +0300 | [diff] [blame] | 1158 | } |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1159 | |
Jason Wang | 429711a | 2018-08-06 11:17:47 +0800 | [diff] [blame] | 1160 | switch (type) { |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1161 | case VHOST_IOTLB_MSG: |
Jason Wang | 429711a | 2018-08-06 11:17:47 +0800 | [diff] [blame] | 1162 | /* There maybe a hole after type for V1 message type, |
| 1163 | * so skip it here. |
| 1164 | */ |
| 1165 | offset = offsetof(struct vhost_msg, iotlb) - sizeof(int); |
| 1166 | break; |
| 1167 | case VHOST_IOTLB_MSG_V2: |
| 1168 | offset = sizeof(__u32); |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1169 | break; |
| 1170 | default: |
| 1171 | ret = -EINVAL; |
Jason Wang | 429711a | 2018-08-06 11:17:47 +0800 | [diff] [blame] | 1172 | goto done; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1173 | } |
| 1174 | |
Jason Wang | 429711a | 2018-08-06 11:17:47 +0800 | [diff] [blame] | 1175 | iov_iter_advance(from, offset); |
| 1176 | ret = copy_from_iter(&msg, sizeof(msg), from); |
Pavel Tikhomirov | 74ad741 | 2018-12-13 17:53:50 +0300 | [diff] [blame] | 1177 | if (ret != sizeof(msg)) { |
| 1178 | ret = -EINVAL; |
Jason Wang | 429711a | 2018-08-06 11:17:47 +0800 | [diff] [blame] | 1179 | goto done; |
Pavel Tikhomirov | 74ad741 | 2018-12-13 17:53:50 +0300 | [diff] [blame] | 1180 | } |
Jason Wang | 429711a | 2018-08-06 11:17:47 +0800 | [diff] [blame] | 1181 | if (vhost_process_iotlb_msg(dev, &msg)) { |
| 1182 | ret = -EFAULT; |
| 1183 | goto done; |
| 1184 | } |
| 1185 | |
| 1186 | ret = (type == VHOST_IOTLB_MSG) ? sizeof(struct vhost_msg) : |
| 1187 | sizeof(struct vhost_msg_v2); |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1188 | done: |
| 1189 | return ret; |
| 1190 | } |
| 1191 | EXPORT_SYMBOL(vhost_chr_write_iter); |
| 1192 | |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 1193 | __poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev, |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1194 | poll_table *wait) |
| 1195 | { |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 1196 | __poll_t mask = 0; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1197 | |
| 1198 | poll_wait(file, &dev->wait, wait); |
| 1199 | |
| 1200 | if (!list_empty(&dev->read_list)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1201 | mask |= EPOLLIN | EPOLLRDNORM; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1202 | |
| 1203 | return mask; |
| 1204 | } |
| 1205 | EXPORT_SYMBOL(vhost_chr_poll); |
| 1206 | |
| 1207 | ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to, |
| 1208 | int noblock) |
| 1209 | { |
| 1210 | DEFINE_WAIT(wait); |
| 1211 | struct vhost_msg_node *node; |
| 1212 | ssize_t ret = 0; |
| 1213 | unsigned size = sizeof(struct vhost_msg); |
| 1214 | |
| 1215 | if (iov_iter_count(to) < size) |
| 1216 | return 0; |
| 1217 | |
| 1218 | while (1) { |
| 1219 | if (!noblock) |
| 1220 | prepare_to_wait(&dev->wait, &wait, |
| 1221 | TASK_INTERRUPTIBLE); |
| 1222 | |
| 1223 | node = vhost_dequeue_msg(dev, &dev->read_list); |
| 1224 | if (node) |
| 1225 | break; |
| 1226 | if (noblock) { |
| 1227 | ret = -EAGAIN; |
| 1228 | break; |
| 1229 | } |
| 1230 | if (signal_pending(current)) { |
| 1231 | ret = -ERESTARTSYS; |
| 1232 | break; |
| 1233 | } |
| 1234 | if (!dev->iotlb) { |
| 1235 | ret = -EBADFD; |
| 1236 | break; |
| 1237 | } |
| 1238 | |
| 1239 | schedule(); |
| 1240 | } |
| 1241 | |
| 1242 | if (!noblock) |
| 1243 | finish_wait(&dev->wait, &wait); |
| 1244 | |
| 1245 | if (node) { |
Jason Wang | 429711a | 2018-08-06 11:17:47 +0800 | [diff] [blame] | 1246 | struct vhost_iotlb_msg *msg; |
| 1247 | void *start = &node->msg; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1248 | |
Jason Wang | 429711a | 2018-08-06 11:17:47 +0800 | [diff] [blame] | 1249 | switch (node->msg.type) { |
| 1250 | case VHOST_IOTLB_MSG: |
| 1251 | size = sizeof(node->msg); |
| 1252 | msg = &node->msg.iotlb; |
| 1253 | break; |
| 1254 | case VHOST_IOTLB_MSG_V2: |
| 1255 | size = sizeof(node->msg_v2); |
| 1256 | msg = &node->msg_v2.iotlb; |
| 1257 | break; |
| 1258 | default: |
| 1259 | BUG(); |
| 1260 | break; |
| 1261 | } |
| 1262 | |
| 1263 | ret = copy_to_iter(start, size, to); |
| 1264 | if (ret != size || msg->type != VHOST_IOTLB_MISS) { |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1265 | kfree(node); |
| 1266 | return ret; |
| 1267 | } |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1268 | vhost_enqueue_msg(dev, &dev->pending_list, node); |
| 1269 | } |
| 1270 | |
| 1271 | return ret; |
| 1272 | } |
| 1273 | EXPORT_SYMBOL_GPL(vhost_chr_read_iter); |
| 1274 | |
| 1275 | static int vhost_iotlb_miss(struct vhost_virtqueue *vq, u64 iova, int access) |
| 1276 | { |
| 1277 | struct vhost_dev *dev = vq->dev; |
| 1278 | struct vhost_msg_node *node; |
| 1279 | struct vhost_iotlb_msg *msg; |
Jason Wang | 429711a | 2018-08-06 11:17:47 +0800 | [diff] [blame] | 1280 | bool v2 = vhost_backend_has_feature(vq, VHOST_BACKEND_F_IOTLB_MSG_V2); |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1281 | |
Jason Wang | 429711a | 2018-08-06 11:17:47 +0800 | [diff] [blame] | 1282 | node = vhost_new_msg(vq, v2 ? VHOST_IOTLB_MSG_V2 : VHOST_IOTLB_MSG); |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1283 | if (!node) |
| 1284 | return -ENOMEM; |
| 1285 | |
Jason Wang | 429711a | 2018-08-06 11:17:47 +0800 | [diff] [blame] | 1286 | if (v2) { |
| 1287 | node->msg_v2.type = VHOST_IOTLB_MSG_V2; |
| 1288 | msg = &node->msg_v2.iotlb; |
| 1289 | } else { |
| 1290 | msg = &node->msg.iotlb; |
| 1291 | } |
| 1292 | |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1293 | msg->type = VHOST_IOTLB_MISS; |
| 1294 | msg->iova = iova; |
| 1295 | msg->perm = access; |
| 1296 | |
| 1297 | vhost_enqueue_msg(dev, &dev->read_list, node); |
| 1298 | |
| 1299 | return 0; |
Jason Wang | bfe2bc5 | 2016-06-23 02:04:30 -0400 | [diff] [blame] | 1300 | } |
| 1301 | |
Stefan Hajnoczi | ddd3d40 | 2018-04-11 10:35:41 +0800 | [diff] [blame] | 1302 | static bool vq_access_ok(struct vhost_virtqueue *vq, unsigned int num, |
| 1303 | struct vring_desc __user *desc, |
| 1304 | struct vring_avail __user *avail, |
| 1305 | struct vring_used __user *used) |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1306 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1307 | { |
Jason Wang | 4942e82 | 2019-05-24 04:12:16 -0400 | [diff] [blame] | 1308 | return access_ok(desc, vhost_get_desc_size(vq, num)) && |
| 1309 | access_ok(avail, vhost_get_avail_size(vq, num)) && |
| 1310 | access_ok(used, vhost_get_used_size(vq, num)); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
Jason Wang | f889491 | 2017-02-28 17:56:02 +0800 | [diff] [blame] | 1313 | static void vhost_vq_meta_update(struct vhost_virtqueue *vq, |
| 1314 | const struct vhost_umem_node *node, |
| 1315 | int type) |
| 1316 | { |
| 1317 | int access = (type == VHOST_ADDR_USED) ? |
| 1318 | VHOST_ACCESS_WO : VHOST_ACCESS_RO; |
| 1319 | |
| 1320 | if (likely(node->perm & access)) |
| 1321 | vq->meta_iotlb[type] = node; |
| 1322 | } |
| 1323 | |
Stefan Hajnoczi | ddd3d40 | 2018-04-11 10:35:41 +0800 | [diff] [blame] | 1324 | static bool iotlb_access_ok(struct vhost_virtqueue *vq, |
| 1325 | int access, u64 addr, u64 len, int type) |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1326 | { |
| 1327 | const struct vhost_umem_node *node; |
| 1328 | struct vhost_umem *umem = vq->iotlb; |
Michael S. Tsirkin | ca2c5b3 | 2017-08-21 22:33:33 +0300 | [diff] [blame] | 1329 | u64 s = 0, size, orig_addr = addr, last = addr + len - 1; |
Jason Wang | f889491 | 2017-02-28 17:56:02 +0800 | [diff] [blame] | 1330 | |
| 1331 | if (vhost_vq_meta_fetch(vq, addr, len, type)) |
| 1332 | return true; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1333 | |
| 1334 | while (len > s) { |
| 1335 | node = vhost_umem_interval_tree_iter_first(&umem->umem_tree, |
| 1336 | addr, |
Michael S. Tsirkin | ca2c5b3 | 2017-08-21 22:33:33 +0300 | [diff] [blame] | 1337 | last); |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1338 | if (node == NULL || node->start > addr) { |
| 1339 | vhost_iotlb_miss(vq, addr, access); |
| 1340 | return false; |
| 1341 | } else if (!(node->perm & access)) { |
| 1342 | /* Report the possible access violation by |
| 1343 | * request another translation from userspace. |
| 1344 | */ |
| 1345 | return false; |
| 1346 | } |
| 1347 | |
| 1348 | size = node->size - addr + node->start; |
Jason Wang | f889491 | 2017-02-28 17:56:02 +0800 | [diff] [blame] | 1349 | |
| 1350 | if (orig_addr == addr && size >= len) |
| 1351 | vhost_vq_meta_update(vq, node, type); |
| 1352 | |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1353 | s += size; |
| 1354 | addr += size; |
| 1355 | } |
| 1356 | |
| 1357 | return true; |
| 1358 | } |
| 1359 | |
Jason Wang | 9b5e830 | 2019-05-24 04:12:15 -0400 | [diff] [blame] | 1360 | int vq_meta_prefetch(struct vhost_virtqueue *vq) |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1361 | { |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1362 | unsigned int num = vq->num; |
| 1363 | |
Michael S. Tsirkin | 3d2c7d3 | 2019-08-10 13:53:21 -0400 | [diff] [blame] | 1364 | if (!vq->iotlb) |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1365 | return 1; |
| 1366 | |
| 1367 | return iotlb_access_ok(vq, VHOST_ACCESS_RO, (u64)(uintptr_t)vq->desc, |
Jason Wang | 4942e82 | 2019-05-24 04:12:16 -0400 | [diff] [blame] | 1368 | vhost_get_desc_size(vq, num), VHOST_ADDR_DESC) && |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1369 | iotlb_access_ok(vq, VHOST_ACCESS_RO, (u64)(uintptr_t)vq->avail, |
Jason Wang | 4942e82 | 2019-05-24 04:12:16 -0400 | [diff] [blame] | 1370 | vhost_get_avail_size(vq, num), |
Jason Wang | f889491 | 2017-02-28 17:56:02 +0800 | [diff] [blame] | 1371 | VHOST_ADDR_AVAIL) && |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1372 | iotlb_access_ok(vq, VHOST_ACCESS_WO, (u64)(uintptr_t)vq->used, |
Jason Wang | 4942e82 | 2019-05-24 04:12:16 -0400 | [diff] [blame] | 1373 | vhost_get_used_size(vq, num), VHOST_ADDR_USED); |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1374 | } |
Jason Wang | 9b5e830 | 2019-05-24 04:12:15 -0400 | [diff] [blame] | 1375 | EXPORT_SYMBOL_GPL(vq_meta_prefetch); |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1376 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1377 | /* Can we log writes? */ |
| 1378 | /* Caller should have device mutex but not vq mutex */ |
Stefan Hajnoczi | ddd3d40 | 2018-04-11 10:35:41 +0800 | [diff] [blame] | 1379 | bool vhost_log_access_ok(struct vhost_dev *dev) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1380 | { |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 1381 | return memory_access_ok(dev, dev->umem, 1); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1382 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 1383 | EXPORT_SYMBOL_GPL(vhost_log_access_ok); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1384 | |
| 1385 | /* Verify access for write logging. */ |
| 1386 | /* Caller should have vq mutex and device mutex */ |
Stefan Hajnoczi | ddd3d40 | 2018-04-11 10:35:41 +0800 | [diff] [blame] | 1387 | static bool vq_log_access_ok(struct vhost_virtqueue *vq, |
| 1388 | void __user *log_base) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1389 | { |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 1390 | return vq_memory_access_ok(log_base, vq->umem, |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 1391 | vhost_has_feature(vq, VHOST_F_LOG_ALL)) && |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1392 | (!vq->log_used || log_access_ok(log_base, vq->log_addr, |
Jason Wang | 4942e82 | 2019-05-24 04:12:16 -0400 | [diff] [blame] | 1393 | vhost_get_used_size(vq, vq->num))); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | /* Can we start vq? */ |
| 1397 | /* Caller should have vq mutex and device mutex */ |
Stefan Hajnoczi | ddd3d40 | 2018-04-11 10:35:41 +0800 | [diff] [blame] | 1398 | bool vhost_vq_access_ok(struct vhost_virtqueue *vq) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1399 | { |
Stefan Hajnoczi | d14d2b7 | 2018-04-11 10:35:40 +0800 | [diff] [blame] | 1400 | if (!vq_log_access_ok(vq, vq->log_base)) |
Stefan Hajnoczi | ddd3d40 | 2018-04-11 10:35:41 +0800 | [diff] [blame] | 1401 | return false; |
Jason Wang | d65026c | 2018-03-29 16:00:04 +0800 | [diff] [blame] | 1402 | |
Stefan Hajnoczi | d14d2b7 | 2018-04-11 10:35:40 +0800 | [diff] [blame] | 1403 | /* Access validation occurs at prefetch time with IOTLB */ |
| 1404 | if (vq->iotlb) |
Stefan Hajnoczi | ddd3d40 | 2018-04-11 10:35:41 +0800 | [diff] [blame] | 1405 | return true; |
Jason Wang | d65026c | 2018-03-29 16:00:04 +0800 | [diff] [blame] | 1406 | |
| 1407 | return vq_access_ok(vq, vq->num, vq->desc, vq->avail, vq->used); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1408 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 1409 | EXPORT_SYMBOL_GPL(vhost_vq_access_ok); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1410 | |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1411 | static struct vhost_umem *vhost_umem_alloc(void) |
| 1412 | { |
Michal Hocko | 6c5ab65 | 2017-05-08 15:57:15 -0700 | [diff] [blame] | 1413 | struct vhost_umem *umem = kvzalloc(sizeof(*umem), GFP_KERNEL); |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1414 | |
| 1415 | if (!umem) |
| 1416 | return NULL; |
| 1417 | |
Davidlohr Bueso | f808c13 | 2017-09-08 16:15:08 -0700 | [diff] [blame] | 1418 | umem->umem_tree = RB_ROOT_CACHED; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1419 | umem->numem = 0; |
| 1420 | INIT_LIST_HEAD(&umem->umem_list); |
| 1421 | |
| 1422 | return umem; |
| 1423 | } |
| 1424 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1425 | static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m) |
| 1426 | { |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 1427 | struct vhost_memory mem, *newmem; |
| 1428 | struct vhost_memory_region *region; |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 1429 | struct vhost_umem *newumem, *oldumem; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1430 | unsigned long size = offsetof(struct vhost_memory, regions); |
Michael S. Tsirkin | 98f9ca0 | 2014-05-28 17:07:02 +0300 | [diff] [blame] | 1431 | int i; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 1432 | |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 1433 | if (copy_from_user(&mem, m, size)) |
| 1434 | return -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1435 | if (mem.padding) |
| 1436 | return -EOPNOTSUPP; |
Igor Mammedov | c9ce42f | 2015-07-02 15:08:11 +0200 | [diff] [blame] | 1437 | if (mem.nregions > max_mem_regions) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1438 | return -E2BIG; |
Matthew Wilcox | b2303d7 | 2018-06-07 07:57:18 -0700 | [diff] [blame] | 1439 | newmem = kvzalloc(struct_size(newmem, regions, mem.nregions), |
| 1440 | GFP_KERNEL); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1441 | if (!newmem) |
| 1442 | return -ENOMEM; |
| 1443 | |
| 1444 | memcpy(newmem, &mem, size); |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 1445 | if (copy_from_user(newmem->regions, m->regions, |
| 1446 | mem.nregions * sizeof *m->regions)) { |
Igor Mammedov | bcfeaca | 2015-06-16 18:33:35 +0200 | [diff] [blame] | 1447 | kvfree(newmem); |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 1448 | return -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1451 | newumem = vhost_umem_alloc(); |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 1452 | if (!newumem) { |
Igor Mammedov | 4de7255 | 2015-07-01 11:07:09 +0200 | [diff] [blame] | 1453 | kvfree(newmem); |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 1454 | return -ENOMEM; |
Takuya Yoshikawa | a02c378 | 2010-05-27 19:03:56 +0900 | [diff] [blame] | 1455 | } |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 1456 | |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 1457 | for (region = newmem->regions; |
| 1458 | region < newmem->regions + mem.nregions; |
| 1459 | region++) { |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1460 | if (vhost_new_umem_range(newumem, |
| 1461 | region->guest_phys_addr, |
| 1462 | region->memory_size, |
| 1463 | region->guest_phys_addr + |
| 1464 | region->memory_size - 1, |
| 1465 | region->userspace_addr, |
| 1466 | VHOST_ACCESS_RW)) |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 1467 | goto err; |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | if (!memory_access_ok(d, newumem, 0)) |
| 1471 | goto err; |
| 1472 | |
| 1473 | oldumem = d->umem; |
| 1474 | d->umem = newumem; |
Michael S. Tsirkin | 98f9ca0 | 2014-05-28 17:07:02 +0300 | [diff] [blame] | 1475 | |
Michael S. Tsirkin | 47283be | 2014-06-05 15:20:27 +0300 | [diff] [blame] | 1476 | /* All memory accesses are done under some VQ mutex. */ |
Michael S. Tsirkin | 98f9ca0 | 2014-05-28 17:07:02 +0300 | [diff] [blame] | 1477 | for (i = 0; i < d->nvqs; ++i) { |
| 1478 | mutex_lock(&d->vqs[i]->mutex); |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 1479 | d->vqs[i]->umem = newumem; |
Michael S. Tsirkin | 98f9ca0 | 2014-05-28 17:07:02 +0300 | [diff] [blame] | 1480 | mutex_unlock(&d->vqs[i]->mutex); |
| 1481 | } |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 1482 | |
| 1483 | kvfree(newmem); |
| 1484 | vhost_umem_clean(oldumem); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1485 | return 0; |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 1486 | |
| 1487 | err: |
| 1488 | vhost_umem_clean(newumem); |
| 1489 | kvfree(newmem); |
| 1490 | return -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
Jason Wang | feebcae | 2019-05-24 04:12:17 -0400 | [diff] [blame] | 1493 | static long vhost_vring_set_num(struct vhost_dev *d, |
| 1494 | struct vhost_virtqueue *vq, |
| 1495 | void __user *argp) |
| 1496 | { |
| 1497 | struct vhost_vring_state s; |
| 1498 | |
| 1499 | /* Resizing ring with an active backend? |
| 1500 | * You don't want to do that. */ |
| 1501 | if (vq->private_data) |
| 1502 | return -EBUSY; |
| 1503 | |
| 1504 | if (copy_from_user(&s, argp, sizeof s)) |
| 1505 | return -EFAULT; |
| 1506 | |
| 1507 | if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) |
| 1508 | return -EINVAL; |
| 1509 | vq->num = s.num; |
| 1510 | |
| 1511 | return 0; |
| 1512 | } |
| 1513 | |
| 1514 | static long vhost_vring_set_addr(struct vhost_dev *d, |
| 1515 | struct vhost_virtqueue *vq, |
| 1516 | void __user *argp) |
| 1517 | { |
| 1518 | struct vhost_vring_addr a; |
| 1519 | |
| 1520 | if (copy_from_user(&a, argp, sizeof a)) |
| 1521 | return -EFAULT; |
| 1522 | if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) |
| 1523 | return -EOPNOTSUPP; |
| 1524 | |
| 1525 | /* For 32bit, verify that the top 32bits of the user |
| 1526 | data are set to zero. */ |
| 1527 | if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr || |
| 1528 | (u64)(unsigned long)a.used_user_addr != a.used_user_addr || |
| 1529 | (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) |
| 1530 | return -EFAULT; |
| 1531 | |
| 1532 | /* Make sure it's safe to cast pointers to vring types. */ |
| 1533 | BUILD_BUG_ON(__alignof__ *vq->avail > VRING_AVAIL_ALIGN_SIZE); |
| 1534 | BUILD_BUG_ON(__alignof__ *vq->used > VRING_USED_ALIGN_SIZE); |
| 1535 | if ((a.avail_user_addr & (VRING_AVAIL_ALIGN_SIZE - 1)) || |
| 1536 | (a.used_user_addr & (VRING_USED_ALIGN_SIZE - 1)) || |
| 1537 | (a.log_guest_addr & (VRING_USED_ALIGN_SIZE - 1))) |
| 1538 | return -EINVAL; |
| 1539 | |
| 1540 | /* We only verify access here if backend is configured. |
| 1541 | * If it is not, we don't as size might not have been setup. |
| 1542 | * We will verify when backend is configured. */ |
| 1543 | if (vq->private_data) { |
| 1544 | if (!vq_access_ok(vq, vq->num, |
| 1545 | (void __user *)(unsigned long)a.desc_user_addr, |
| 1546 | (void __user *)(unsigned long)a.avail_user_addr, |
| 1547 | (void __user *)(unsigned long)a.used_user_addr)) |
| 1548 | return -EINVAL; |
| 1549 | |
| 1550 | /* Also validate log access for used ring if enabled. */ |
| 1551 | if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) && |
| 1552 | !log_access_ok(vq->log_base, a.log_guest_addr, |
| 1553 | sizeof *vq->used + |
| 1554 | vq->num * sizeof *vq->used->ring)) |
| 1555 | return -EINVAL; |
| 1556 | } |
| 1557 | |
| 1558 | vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG)); |
| 1559 | vq->desc = (void __user *)(unsigned long)a.desc_user_addr; |
| 1560 | vq->avail = (void __user *)(unsigned long)a.avail_user_addr; |
| 1561 | vq->log_addr = a.log_guest_addr; |
| 1562 | vq->used = (void __user *)(unsigned long)a.used_user_addr; |
| 1563 | |
| 1564 | return 0; |
| 1565 | } |
| 1566 | |
| 1567 | static long vhost_vring_set_num_addr(struct vhost_dev *d, |
| 1568 | struct vhost_virtqueue *vq, |
| 1569 | unsigned int ioctl, |
| 1570 | void __user *argp) |
| 1571 | { |
| 1572 | long r; |
| 1573 | |
| 1574 | mutex_lock(&vq->mutex); |
| 1575 | |
| 1576 | switch (ioctl) { |
| 1577 | case VHOST_SET_VRING_NUM: |
| 1578 | r = vhost_vring_set_num(d, vq, argp); |
| 1579 | break; |
| 1580 | case VHOST_SET_VRING_ADDR: |
| 1581 | r = vhost_vring_set_addr(d, vq, argp); |
| 1582 | break; |
| 1583 | default: |
| 1584 | BUG(); |
| 1585 | } |
| 1586 | |
| 1587 | mutex_unlock(&vq->mutex); |
| 1588 | |
| 1589 | return r; |
| 1590 | } |
Sonny Rao | 26b3660 | 2018-03-14 10:05:06 -0700 | [diff] [blame] | 1591 | long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1592 | { |
Al Viro | cecb46f | 2012-08-27 14:21:39 -0400 | [diff] [blame] | 1593 | struct file *eventfp, *filep = NULL; |
| 1594 | bool pollstart = false, pollstop = false; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1595 | struct eventfd_ctx *ctx = NULL; |
| 1596 | u32 __user *idxp = argp; |
| 1597 | struct vhost_virtqueue *vq; |
| 1598 | struct vhost_vring_state s; |
| 1599 | struct vhost_vring_file f; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1600 | u32 idx; |
| 1601 | long r; |
| 1602 | |
| 1603 | r = get_user(idx, idxp); |
| 1604 | if (r < 0) |
| 1605 | return r; |
Krishna Kumar | 0f3d9a1 | 2010-05-25 11:10:36 +0530 | [diff] [blame] | 1606 | if (idx >= d->nvqs) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1607 | return -ENOBUFS; |
| 1608 | |
Jason Wang | ff00226 | 2018-10-30 14:10:49 +0800 | [diff] [blame] | 1609 | idx = array_index_nospec(idx, d->nvqs); |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 1610 | vq = d->vqs[idx]; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1611 | |
Jason Wang | feebcae | 2019-05-24 04:12:17 -0400 | [diff] [blame] | 1612 | if (ioctl == VHOST_SET_VRING_NUM || |
| 1613 | ioctl == VHOST_SET_VRING_ADDR) { |
| 1614 | return vhost_vring_set_num_addr(d, vq, ioctl, argp); |
| 1615 | } |
| 1616 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1617 | mutex_lock(&vq->mutex); |
| 1618 | |
| 1619 | switch (ioctl) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1620 | case VHOST_SET_VRING_BASE: |
| 1621 | /* Moving base with an active backend? |
| 1622 | * You don't want to do that. */ |
| 1623 | if (vq->private_data) { |
| 1624 | r = -EBUSY; |
| 1625 | break; |
| 1626 | } |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 1627 | if (copy_from_user(&s, argp, sizeof s)) { |
| 1628 | r = -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1629 | break; |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 1630 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1631 | if (s.num > 0xffff) { |
| 1632 | r = -EINVAL; |
| 1633 | break; |
| 1634 | } |
Jason Wang | 8d65843 | 2017-07-27 11:22:05 +0800 | [diff] [blame] | 1635 | vq->last_avail_idx = s.num; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1636 | /* Forget the cached index value. */ |
| 1637 | vq->avail_idx = vq->last_avail_idx; |
| 1638 | break; |
| 1639 | case VHOST_GET_VRING_BASE: |
| 1640 | s.index = idx; |
| 1641 | s.num = vq->last_avail_idx; |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 1642 | if (copy_to_user(argp, &s, sizeof s)) |
| 1643 | r = -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1644 | break; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1645 | case VHOST_SET_VRING_KICK: |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 1646 | if (copy_from_user(&f, argp, sizeof f)) { |
| 1647 | r = -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1648 | break; |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 1649 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1650 | eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd); |
Michael S. Tsirkin | 535297a | 2010-03-17 16:06:11 +0200 | [diff] [blame] | 1651 | if (IS_ERR(eventfp)) { |
| 1652 | r = PTR_ERR(eventfp); |
| 1653 | break; |
| 1654 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1655 | if (eventfp != vq->kick) { |
Al Viro | cecb46f | 2012-08-27 14:21:39 -0400 | [diff] [blame] | 1656 | pollstop = (filep = vq->kick) != NULL; |
| 1657 | pollstart = (vq->kick = eventfp) != NULL; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1658 | } else |
| 1659 | filep = eventfp; |
| 1660 | break; |
| 1661 | case VHOST_SET_VRING_CALL: |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 1662 | if (copy_from_user(&f, argp, sizeof f)) { |
| 1663 | r = -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1664 | break; |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 1665 | } |
Eric Biggers | e050c7d | 2018-01-06 14:52:19 -0800 | [diff] [blame] | 1666 | ctx = f.fd == -1 ? NULL : eventfd_ctx_fdget(f.fd); |
| 1667 | if (IS_ERR(ctx)) { |
| 1668 | r = PTR_ERR(ctx); |
Michael S. Tsirkin | 535297a | 2010-03-17 16:06:11 +0200 | [diff] [blame] | 1669 | break; |
| 1670 | } |
Eric Biggers | e050c7d | 2018-01-06 14:52:19 -0800 | [diff] [blame] | 1671 | swap(ctx, vq->call_ctx); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1672 | break; |
| 1673 | case VHOST_SET_VRING_ERR: |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 1674 | if (copy_from_user(&f, argp, sizeof f)) { |
| 1675 | r = -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1676 | break; |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 1677 | } |
Eric Biggers | 09f332a | 2018-01-06 14:52:20 -0800 | [diff] [blame] | 1678 | ctx = f.fd == -1 ? NULL : eventfd_ctx_fdget(f.fd); |
| 1679 | if (IS_ERR(ctx)) { |
| 1680 | r = PTR_ERR(ctx); |
Michael S. Tsirkin | 535297a | 2010-03-17 16:06:11 +0200 | [diff] [blame] | 1681 | break; |
| 1682 | } |
Eric Biggers | 09f332a | 2018-01-06 14:52:20 -0800 | [diff] [blame] | 1683 | swap(ctx, vq->error_ctx); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1684 | break; |
Greg Kurz | 2751c98 | 2015-04-24 14:27:24 +0200 | [diff] [blame] | 1685 | case VHOST_SET_VRING_ENDIAN: |
| 1686 | r = vhost_set_vring_endian(vq, argp); |
| 1687 | break; |
| 1688 | case VHOST_GET_VRING_ENDIAN: |
| 1689 | r = vhost_get_vring_endian(vq, idx, argp); |
| 1690 | break; |
Jason Wang | 0308813 | 2016-03-04 06:24:53 -0500 | [diff] [blame] | 1691 | case VHOST_SET_VRING_BUSYLOOP_TIMEOUT: |
| 1692 | if (copy_from_user(&s, argp, sizeof(s))) { |
| 1693 | r = -EFAULT; |
| 1694 | break; |
| 1695 | } |
| 1696 | vq->busyloop_timeout = s.num; |
| 1697 | break; |
| 1698 | case VHOST_GET_VRING_BUSYLOOP_TIMEOUT: |
| 1699 | s.index = idx; |
| 1700 | s.num = vq->busyloop_timeout; |
| 1701 | if (copy_to_user(argp, &s, sizeof(s))) |
| 1702 | r = -EFAULT; |
| 1703 | break; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1704 | default: |
| 1705 | r = -ENOIOCTLCMD; |
| 1706 | } |
| 1707 | |
| 1708 | if (pollstop && vq->handle_kick) |
| 1709 | vhost_poll_stop(&vq->poll); |
| 1710 | |
Eric Biggers | e050c7d | 2018-01-06 14:52:19 -0800 | [diff] [blame] | 1711 | if (!IS_ERR_OR_NULL(ctx)) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1712 | eventfd_ctx_put(ctx); |
| 1713 | if (filep) |
| 1714 | fput(filep); |
| 1715 | |
| 1716 | if (pollstart && vq->handle_kick) |
Jason Wang | 2b8b328 | 2013-01-28 01:05:18 +0000 | [diff] [blame] | 1717 | r = vhost_poll_start(&vq->poll, vq->kick); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1718 | |
| 1719 | mutex_unlock(&vq->mutex); |
| 1720 | |
| 1721 | if (pollstop && vq->handle_kick) |
| 1722 | vhost_poll_flush(&vq->poll); |
| 1723 | return r; |
| 1724 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 1725 | EXPORT_SYMBOL_GPL(vhost_vring_ioctl); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1726 | |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1727 | int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled) |
| 1728 | { |
| 1729 | struct vhost_umem *niotlb, *oiotlb; |
| 1730 | int i; |
| 1731 | |
| 1732 | niotlb = vhost_umem_alloc(); |
| 1733 | if (!niotlb) |
| 1734 | return -ENOMEM; |
| 1735 | |
| 1736 | oiotlb = d->iotlb; |
| 1737 | d->iotlb = niotlb; |
| 1738 | |
| 1739 | for (i = 0; i < d->nvqs; ++i) { |
Jason Wang | b13f9c6 | 2018-08-08 11:43:04 +0800 | [diff] [blame] | 1740 | struct vhost_virtqueue *vq = d->vqs[i]; |
| 1741 | |
| 1742 | mutex_lock(&vq->mutex); |
| 1743 | vq->iotlb = niotlb; |
| 1744 | __vhost_vq_meta_reset(vq); |
| 1745 | mutex_unlock(&vq->mutex); |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | vhost_umem_clean(oiotlb); |
| 1749 | |
| 1750 | return 0; |
| 1751 | } |
| 1752 | EXPORT_SYMBOL_GPL(vhost_init_device_iotlb); |
| 1753 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1754 | /* Caller must have device mutex */ |
Michael S. Tsirkin | 935cdee | 2012-12-06 14:03:34 +0200 | [diff] [blame] | 1755 | long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1756 | { |
Eric Biggers | d25cc43 | 2018-01-06 14:52:21 -0800 | [diff] [blame] | 1757 | struct eventfd_ctx *ctx; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1758 | u64 p; |
| 1759 | long r; |
| 1760 | int i, fd; |
| 1761 | |
| 1762 | /* If you are not the owner, you can become one */ |
| 1763 | if (ioctl == VHOST_SET_OWNER) { |
| 1764 | r = vhost_dev_set_owner(d); |
| 1765 | goto done; |
| 1766 | } |
| 1767 | |
| 1768 | /* You must be the owner to do anything else */ |
| 1769 | r = vhost_dev_check_owner(d); |
| 1770 | if (r) |
| 1771 | goto done; |
| 1772 | |
| 1773 | switch (ioctl) { |
| 1774 | case VHOST_SET_MEM_TABLE: |
| 1775 | r = vhost_set_memory(d, argp); |
| 1776 | break; |
| 1777 | case VHOST_SET_LOG_BASE: |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 1778 | if (copy_from_user(&p, argp, sizeof p)) { |
| 1779 | r = -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1780 | break; |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 1781 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1782 | if ((u64)(unsigned long)p != p) { |
| 1783 | r = -EFAULT; |
| 1784 | break; |
| 1785 | } |
| 1786 | for (i = 0; i < d->nvqs; ++i) { |
| 1787 | struct vhost_virtqueue *vq; |
| 1788 | void __user *base = (void __user *)(unsigned long)p; |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 1789 | vq = d->vqs[i]; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1790 | mutex_lock(&vq->mutex); |
| 1791 | /* If ring is inactive, will check when it's enabled. */ |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 1792 | if (vq->private_data && !vq_log_access_ok(vq, base)) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1793 | r = -EFAULT; |
| 1794 | else |
| 1795 | vq->log_base = base; |
| 1796 | mutex_unlock(&vq->mutex); |
| 1797 | } |
| 1798 | break; |
| 1799 | case VHOST_SET_LOG_FD: |
| 1800 | r = get_user(fd, (int __user *)argp); |
| 1801 | if (r < 0) |
| 1802 | break; |
Eric Biggers | d25cc43 | 2018-01-06 14:52:21 -0800 | [diff] [blame] | 1803 | ctx = fd == -1 ? NULL : eventfd_ctx_fdget(fd); |
| 1804 | if (IS_ERR(ctx)) { |
| 1805 | r = PTR_ERR(ctx); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1806 | break; |
| 1807 | } |
Eric Biggers | d25cc43 | 2018-01-06 14:52:21 -0800 | [diff] [blame] | 1808 | swap(ctx, d->log_ctx); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1809 | for (i = 0; i < d->nvqs; ++i) { |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 1810 | mutex_lock(&d->vqs[i]->mutex); |
| 1811 | d->vqs[i]->log_ctx = d->log_ctx; |
| 1812 | mutex_unlock(&d->vqs[i]->mutex); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1813 | } |
| 1814 | if (ctx) |
| 1815 | eventfd_ctx_put(ctx); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1816 | break; |
| 1817 | default: |
Michael S. Tsirkin | 935cdee | 2012-12-06 14:03:34 +0200 | [diff] [blame] | 1818 | r = -ENOIOCTLCMD; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1819 | break; |
| 1820 | } |
| 1821 | done: |
| 1822 | return r; |
| 1823 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 1824 | EXPORT_SYMBOL_GPL(vhost_dev_ioctl); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1825 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1826 | /* TODO: This is really inefficient. We need something like get_user() |
| 1827 | * (instruction directly accesses the data, with an exception table entry |
Mauro Carvalho Chehab | cb1aaeb | 2019-06-07 15:54:32 -0300 | [diff] [blame] | 1828 | * returning -EFAULT). See Documentation/x86/exception-tables.rst. |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1829 | */ |
| 1830 | static int set_bit_to_user(int nr, void __user *addr) |
| 1831 | { |
| 1832 | unsigned long log = (unsigned long)addr; |
| 1833 | struct page *page; |
| 1834 | void *base; |
| 1835 | int bit = nr + (log % PAGE_SIZE) * 8; |
| 1836 | int r; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 1837 | |
Ira Weiny | 73b0140 | 2019-05-13 17:17:11 -0700 | [diff] [blame] | 1838 | r = get_user_pages_fast(log, 1, FOLL_WRITE, &page); |
Michael S. Tsirkin | d6db3f5 | 2010-02-23 11:25:23 +0200 | [diff] [blame] | 1839 | if (r < 0) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1840 | return r; |
Michael S. Tsirkin | d6db3f5 | 2010-02-23 11:25:23 +0200 | [diff] [blame] | 1841 | BUG_ON(r != 1); |
Cong Wang | c6daa7f | 2011-11-25 23:14:26 +0800 | [diff] [blame] | 1842 | base = kmap_atomic(page); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1843 | set_bit(bit, base); |
Cong Wang | c6daa7f | 2011-11-25 23:14:26 +0800 | [diff] [blame] | 1844 | kunmap_atomic(base); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1845 | set_page_dirty_lock(page); |
| 1846 | put_page(page); |
| 1847 | return 0; |
| 1848 | } |
| 1849 | |
| 1850 | static int log_write(void __user *log_base, |
| 1851 | u64 write_address, u64 write_length) |
| 1852 | { |
Michael S. Tsirkin | 28831ee | 2010-11-29 10:22:10 +0200 | [diff] [blame] | 1853 | u64 write_page = write_address / VHOST_PAGE_SIZE; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1854 | int r; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 1855 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1856 | if (!write_length) |
| 1857 | return 0; |
Michael S. Tsirkin | 3bf9be4 | 2010-11-29 10:19:07 +0200 | [diff] [blame] | 1858 | write_length += write_address % VHOST_PAGE_SIZE; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1859 | for (;;) { |
| 1860 | u64 base = (u64)(unsigned long)log_base; |
Michael S. Tsirkin | 28831ee | 2010-11-29 10:22:10 +0200 | [diff] [blame] | 1861 | u64 log = base + write_page / 8; |
| 1862 | int bit = write_page % 8; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1863 | if ((u64)(unsigned long)log != log) |
| 1864 | return -EFAULT; |
| 1865 | r = set_bit_to_user(bit, (void __user *)(unsigned long)log); |
| 1866 | if (r < 0) |
| 1867 | return r; |
| 1868 | if (write_length <= VHOST_PAGE_SIZE) |
| 1869 | break; |
| 1870 | write_length -= VHOST_PAGE_SIZE; |
Michael S. Tsirkin | 28831ee | 2010-11-29 10:22:10 +0200 | [diff] [blame] | 1871 | write_page += 1; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1872 | } |
| 1873 | return r; |
| 1874 | } |
| 1875 | |
Jason Wang | cc5e710 | 2019-01-16 16:54:42 +0800 | [diff] [blame] | 1876 | static int log_write_hva(struct vhost_virtqueue *vq, u64 hva, u64 len) |
| 1877 | { |
| 1878 | struct vhost_umem *umem = vq->umem; |
| 1879 | struct vhost_umem_node *u; |
| 1880 | u64 start, end, l, min; |
| 1881 | int r; |
| 1882 | bool hit = false; |
| 1883 | |
| 1884 | while (len) { |
| 1885 | min = len; |
| 1886 | /* More than one GPAs can be mapped into a single HVA. So |
| 1887 | * iterate all possible umems here to be safe. |
| 1888 | */ |
| 1889 | list_for_each_entry(u, &umem->umem_list, link) { |
| 1890 | if (u->userspace_addr > hva - 1 + len || |
| 1891 | u->userspace_addr - 1 + u->size < hva) |
| 1892 | continue; |
| 1893 | start = max(u->userspace_addr, hva); |
| 1894 | end = min(u->userspace_addr - 1 + u->size, |
| 1895 | hva - 1 + len); |
| 1896 | l = end - start + 1; |
| 1897 | r = log_write(vq->log_base, |
| 1898 | u->start + start - u->userspace_addr, |
| 1899 | l); |
| 1900 | if (r < 0) |
| 1901 | return r; |
| 1902 | hit = true; |
| 1903 | min = min(l, min); |
| 1904 | } |
| 1905 | |
| 1906 | if (!hit) |
| 1907 | return -EFAULT; |
| 1908 | |
| 1909 | len -= min; |
| 1910 | hva += min; |
| 1911 | } |
| 1912 | |
| 1913 | return 0; |
| 1914 | } |
| 1915 | |
| 1916 | static int log_used(struct vhost_virtqueue *vq, u64 used_offset, u64 len) |
| 1917 | { |
| 1918 | struct iovec iov[64]; |
| 1919 | int i, ret; |
| 1920 | |
| 1921 | if (!vq->iotlb) |
| 1922 | return log_write(vq->log_base, vq->log_addr + used_offset, len); |
| 1923 | |
| 1924 | ret = translate_desc(vq, (uintptr_t)vq->used + used_offset, |
| 1925 | len, iov, 64, VHOST_ACCESS_WO); |
Jason Wang | 816db76 | 2019-02-19 14:53:44 +0800 | [diff] [blame] | 1926 | if (ret < 0) |
Jason Wang | cc5e710 | 2019-01-16 16:54:42 +0800 | [diff] [blame] | 1927 | return ret; |
| 1928 | |
| 1929 | for (i = 0; i < ret; i++) { |
| 1930 | ret = log_write_hva(vq, (uintptr_t)iov[i].iov_base, |
| 1931 | iov[i].iov_len); |
| 1932 | if (ret) |
| 1933 | return ret; |
| 1934 | } |
| 1935 | |
| 1936 | return 0; |
| 1937 | } |
| 1938 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1939 | int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log, |
Jason Wang | cc5e710 | 2019-01-16 16:54:42 +0800 | [diff] [blame] | 1940 | unsigned int log_num, u64 len, struct iovec *iov, int count) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1941 | { |
| 1942 | int i, r; |
| 1943 | |
| 1944 | /* Make sure data written is seen before log. */ |
Michael S. Tsirkin | 5659338 | 2010-02-01 07:21:02 +0000 | [diff] [blame] | 1945 | smp_wmb(); |
Jason Wang | cc5e710 | 2019-01-16 16:54:42 +0800 | [diff] [blame] | 1946 | |
| 1947 | if (vq->iotlb) { |
| 1948 | for (i = 0; i < count; i++) { |
| 1949 | r = log_write_hva(vq, (uintptr_t)iov[i].iov_base, |
| 1950 | iov[i].iov_len); |
| 1951 | if (r < 0) |
| 1952 | return r; |
| 1953 | } |
| 1954 | return 0; |
| 1955 | } |
| 1956 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1957 | for (i = 0; i < log_num; ++i) { |
| 1958 | u64 l = min(log[i].len, len); |
| 1959 | r = log_write(vq->log_base, log[i].addr, l); |
| 1960 | if (r < 0) |
| 1961 | return r; |
| 1962 | len -= l; |
Michael S. Tsirkin | 5786aee | 2010-09-22 12:31:53 +0200 | [diff] [blame] | 1963 | if (!len) { |
| 1964 | if (vq->log_ctx) |
| 1965 | eventfd_signal(vq->log_ctx, 1); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1966 | return 0; |
Michael S. Tsirkin | 5786aee | 2010-09-22 12:31:53 +0200 | [diff] [blame] | 1967 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1968 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1969 | /* Length written exceeds what we have stored. This is a bug. */ |
| 1970 | BUG(); |
| 1971 | return 0; |
| 1972 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 1973 | EXPORT_SYMBOL_GPL(vhost_log_write); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1974 | |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 1975 | static int vhost_update_used_flags(struct vhost_virtqueue *vq) |
| 1976 | { |
| 1977 | void __user *used; |
Jason Wang | 7b5d753 | 2019-05-24 04:12:14 -0400 | [diff] [blame] | 1978 | if (vhost_put_used_flags(vq)) |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 1979 | return -EFAULT; |
| 1980 | if (unlikely(vq->log_used)) { |
| 1981 | /* Make sure the flag is seen before log. */ |
| 1982 | smp_wmb(); |
| 1983 | /* Log used flag write. */ |
| 1984 | used = &vq->used->flags; |
Jason Wang | cc5e710 | 2019-01-16 16:54:42 +0800 | [diff] [blame] | 1985 | log_used(vq, (used - (void __user *)vq->used), |
| 1986 | sizeof vq->used->flags); |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 1987 | if (vq->log_ctx) |
| 1988 | eventfd_signal(vq->log_ctx, 1); |
| 1989 | } |
| 1990 | return 0; |
| 1991 | } |
| 1992 | |
| 1993 | static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event) |
| 1994 | { |
Jason Wang | 7b5d753 | 2019-05-24 04:12:14 -0400 | [diff] [blame] | 1995 | if (vhost_put_avail_event(vq)) |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 1996 | return -EFAULT; |
| 1997 | if (unlikely(vq->log_used)) { |
| 1998 | void __user *used; |
| 1999 | /* Make sure the event is seen before log. */ |
| 2000 | smp_wmb(); |
| 2001 | /* Log avail event write */ |
| 2002 | used = vhost_avail_event(vq); |
Jason Wang | cc5e710 | 2019-01-16 16:54:42 +0800 | [diff] [blame] | 2003 | log_used(vq, (used - (void __user *)vq->used), |
| 2004 | sizeof *vhost_avail_event(vq)); |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 2005 | if (vq->log_ctx) |
| 2006 | eventfd_signal(vq->log_ctx, 1); |
| 2007 | } |
| 2008 | return 0; |
| 2009 | } |
| 2010 | |
Greg Kurz | 80f7d03 | 2016-02-16 15:59:44 +0100 | [diff] [blame] | 2011 | int vhost_vq_init_access(struct vhost_virtqueue *vq) |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 2012 | { |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2013 | __virtio16 last_used_idx; |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 2014 | int r; |
Greg Kurz | e1f33be | 2016-02-16 15:54:28 +0100 | [diff] [blame] | 2015 | bool is_le = vq->is_le; |
| 2016 | |
Halil Pasic | cda8bba | 2017-01-30 11:09:36 +0100 | [diff] [blame] | 2017 | if (!vq->private_data) |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 2018 | return 0; |
Greg Kurz | 2751c98 | 2015-04-24 14:27:24 +0200 | [diff] [blame] | 2019 | |
| 2020 | vhost_init_is_le(vq); |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 2021 | |
| 2022 | r = vhost_update_used_flags(vq); |
| 2023 | if (r) |
Greg Kurz | e1f33be | 2016-02-16 15:54:28 +0100 | [diff] [blame] | 2024 | goto err; |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 2025 | vq->signalled_used_valid = false; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 2026 | if (!vq->iotlb && |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 2027 | !access_ok(&vq->used->idx, sizeof vq->used->idx)) { |
Greg Kurz | e1f33be | 2016-02-16 15:54:28 +0100 | [diff] [blame] | 2028 | r = -EFAULT; |
| 2029 | goto err; |
| 2030 | } |
Jason Wang | 7b5d753 | 2019-05-24 04:12:14 -0400 | [diff] [blame] | 2031 | r = vhost_get_used_idx(vq, &last_used_idx); |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 2032 | if (r) { |
| 2033 | vq_err(vq, "Can't access used idx at %p\n", |
| 2034 | &vq->used->idx); |
Greg Kurz | e1f33be | 2016-02-16 15:54:28 +0100 | [diff] [blame] | 2035 | goto err; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 2036 | } |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2037 | vq->last_used_idx = vhost16_to_cpu(vq, last_used_idx); |
Michael S. Tsirkin | 64f7f05 | 2014-12-01 17:39:39 +0200 | [diff] [blame] | 2038 | return 0; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 2039 | |
Greg Kurz | e1f33be | 2016-02-16 15:54:28 +0100 | [diff] [blame] | 2040 | err: |
| 2041 | vq->is_le = is_le; |
| 2042 | return r; |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 2043 | } |
Greg Kurz | 80f7d03 | 2016-02-16 15:59:44 +0100 | [diff] [blame] | 2044 | EXPORT_SYMBOL_GPL(vhost_vq_init_access); |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 2045 | |
Michael S. Tsirkin | 47283be | 2014-06-05 15:20:27 +0300 | [diff] [blame] | 2046 | static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len, |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 2047 | struct iovec iov[], int iov_size, int access) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2048 | { |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 2049 | const struct vhost_umem_node *node; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 2050 | struct vhost_dev *dev = vq->dev; |
| 2051 | struct vhost_umem *umem = dev->iotlb ? dev->iotlb : dev->umem; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2052 | struct iovec *_iov; |
| 2053 | u64 s = 0; |
| 2054 | int ret = 0; |
| 2055 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2056 | while ((u64)len > s) { |
| 2057 | u64 size; |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 2058 | if (unlikely(ret >= iov_size)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2059 | ret = -ENOBUFS; |
| 2060 | break; |
| 2061 | } |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 2062 | |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 2063 | node = vhost_umem_interval_tree_iter_first(&umem->umem_tree, |
| 2064 | addr, addr + len - 1); |
| 2065 | if (node == NULL || node->start > addr) { |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 2066 | if (umem != dev->iotlb) { |
| 2067 | ret = -EFAULT; |
| 2068 | break; |
| 2069 | } |
| 2070 | ret = -EAGAIN; |
| 2071 | break; |
| 2072 | } else if (!(node->perm & access)) { |
| 2073 | ret = -EPERM; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2074 | break; |
| 2075 | } |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 2076 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2077 | _iov = iov + ret; |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 2078 | size = node->size - addr + node->start; |
Michael S. Tsirkin | bd97120 | 2012-11-26 05:57:27 +0000 | [diff] [blame] | 2079 | _iov->iov_len = min((u64)len - s, size); |
Michael S. Tsirkin | 0d4a3f2 | 2019-09-14 15:21:51 -0400 | [diff] [blame] | 2080 | _iov->iov_base = (void __user *)(unsigned long) |
| 2081 | (node->userspace_addr + addr - node->start); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2082 | s += size; |
| 2083 | addr += size; |
| 2084 | ++ret; |
| 2085 | } |
| 2086 | |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 2087 | if (ret == -EAGAIN) |
| 2088 | vhost_iotlb_miss(vq, addr, access); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2089 | return ret; |
| 2090 | } |
| 2091 | |
| 2092 | /* Each buffer in the virtqueues is actually a chain of descriptors. This |
| 2093 | * function returns the next descriptor in the chain, |
| 2094 | * or -1U if we're at the end. */ |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2095 | static unsigned next_desc(struct vhost_virtqueue *vq, struct vring_desc *desc) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2096 | { |
| 2097 | unsigned int next; |
| 2098 | |
| 2099 | /* If this descriptor says it doesn't chain, we're done. */ |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2100 | if (!(desc->flags & cpu_to_vhost16(vq, VRING_DESC_F_NEXT))) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2101 | return -1U; |
| 2102 | |
| 2103 | /* Check they're not leading us off end of descriptors. */ |
Paul E. McKenney | 3a5db0b | 2017-11-27 09:45:10 -0800 | [diff] [blame] | 2104 | next = vhost16_to_cpu(vq, READ_ONCE(desc->next)); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2105 | return next; |
| 2106 | } |
| 2107 | |
Michael S. Tsirkin | 47283be | 2014-06-05 15:20:27 +0300 | [diff] [blame] | 2108 | static int get_indirect(struct vhost_virtqueue *vq, |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 2109 | struct iovec iov[], unsigned int iov_size, |
| 2110 | unsigned int *out_num, unsigned int *in_num, |
| 2111 | struct vhost_log *log, unsigned int *log_num, |
| 2112 | struct vring_desc *indirect) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2113 | { |
| 2114 | struct vring_desc desc; |
| 2115 | unsigned int i = 0, count, found = 0; |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2116 | u32 len = vhost32_to_cpu(vq, indirect->len); |
Al Viro | aad9a1c | 2014-12-10 14:49:01 -0500 | [diff] [blame] | 2117 | struct iov_iter from; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 2118 | int ret, access; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2119 | |
| 2120 | /* Sanity check */ |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2121 | if (unlikely(len % sizeof desc)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2122 | vq_err(vq, "Invalid length in indirect descriptor: " |
| 2123 | "len 0x%llx not multiple of 0x%zx\n", |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2124 | (unsigned long long)len, |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2125 | sizeof desc); |
| 2126 | return -EINVAL; |
| 2127 | } |
| 2128 | |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2129 | ret = translate_desc(vq, vhost64_to_cpu(vq, indirect->addr), len, vq->indirect, |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 2130 | UIO_MAXIOV, VHOST_ACCESS_RO); |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 2131 | if (unlikely(ret < 0)) { |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 2132 | if (ret != -EAGAIN) |
| 2133 | vq_err(vq, "Translation failure %d in indirect.\n", ret); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2134 | return ret; |
| 2135 | } |
Al Viro | aad9a1c | 2014-12-10 14:49:01 -0500 | [diff] [blame] | 2136 | iov_iter_init(&from, READ, vq->indirect, ret, len); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2137 | |
| 2138 | /* We will use the result as an address to read from, so most |
| 2139 | * architectures only need a compiler barrier here. */ |
| 2140 | read_barrier_depends(); |
| 2141 | |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2142 | count = len / sizeof desc; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2143 | /* Buffers are chained via a 16 bit next field, so |
| 2144 | * we can have at most 2^16 of these. */ |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 2145 | if (unlikely(count > USHRT_MAX + 1)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2146 | vq_err(vq, "Indirect buffer length too big: %d\n", |
| 2147 | indirect->len); |
| 2148 | return -E2BIG; |
| 2149 | } |
| 2150 | |
| 2151 | do { |
| 2152 | unsigned iov_count = *in_num + *out_num; |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 2153 | if (unlikely(++found > count)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2154 | vq_err(vq, "Loop detected: last one at %u " |
| 2155 | "indirect size %u\n", |
| 2156 | i, count); |
| 2157 | return -EINVAL; |
| 2158 | } |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 2159 | if (unlikely(!copy_from_iter_full(&desc, sizeof(desc), &from))) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2160 | vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n", |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2161 | i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2162 | return -EINVAL; |
| 2163 | } |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2164 | if (unlikely(desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT))) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2165 | vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n", |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2166 | i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2167 | return -EINVAL; |
| 2168 | } |
| 2169 | |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 2170 | if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) |
| 2171 | access = VHOST_ACCESS_WO; |
| 2172 | else |
| 2173 | access = VHOST_ACCESS_RO; |
| 2174 | |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2175 | ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr), |
| 2176 | vhost32_to_cpu(vq, desc.len), iov + iov_count, |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 2177 | iov_size - iov_count, access); |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 2178 | if (unlikely(ret < 0)) { |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 2179 | if (ret != -EAGAIN) |
| 2180 | vq_err(vq, "Translation failure %d indirect idx %d\n", |
| 2181 | ret, i); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2182 | return ret; |
| 2183 | } |
| 2184 | /* If this is an input descriptor, increment that count. */ |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 2185 | if (access == VHOST_ACCESS_WO) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2186 | *in_num += ret; |
yongduan | 060423b | 2019-09-11 17:44:24 +0800 | [diff] [blame] | 2187 | if (unlikely(log && ret)) { |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2188 | log[*log_num].addr = vhost64_to_cpu(vq, desc.addr); |
| 2189 | log[*log_num].len = vhost32_to_cpu(vq, desc.len); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2190 | ++*log_num; |
| 2191 | } |
| 2192 | } else { |
| 2193 | /* If it's an output descriptor, they're all supposed |
| 2194 | * to come before any input descriptors. */ |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 2195 | if (unlikely(*in_num)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2196 | vq_err(vq, "Indirect descriptor " |
| 2197 | "has out after in: idx %d\n", i); |
| 2198 | return -EINVAL; |
| 2199 | } |
| 2200 | *out_num += ret; |
| 2201 | } |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2202 | } while ((i = next_desc(vq, &desc)) != -1); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2203 | return 0; |
| 2204 | } |
| 2205 | |
| 2206 | /* This looks in the virtqueue and for the first available buffer, and converts |
| 2207 | * it to an iovec for convenient access. Since descriptors consist of some |
| 2208 | * number of output then some number of input descriptors, it's actually two |
| 2209 | * iovecs, but we pack them into one and note how many of each there were. |
| 2210 | * |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 2211 | * This function returns the descriptor number found, or vq->num (which is |
| 2212 | * never a valid descriptor number) if none was found. A negative code is |
| 2213 | * returned on error. */ |
Michael S. Tsirkin | 47283be | 2014-06-05 15:20:27 +0300 | [diff] [blame] | 2214 | int vhost_get_vq_desc(struct vhost_virtqueue *vq, |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 2215 | struct iovec iov[], unsigned int iov_size, |
| 2216 | unsigned int *out_num, unsigned int *in_num, |
| 2217 | struct vhost_log *log, unsigned int *log_num) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2218 | { |
| 2219 | struct vring_desc desc; |
| 2220 | unsigned int i, head, found = 0; |
| 2221 | u16 last_avail_idx; |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2222 | __virtio16 avail_idx; |
| 2223 | __virtio16 ring_head; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 2224 | int ret, access; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2225 | |
| 2226 | /* Check it isn't doing very strange things with descriptor numbers. */ |
| 2227 | last_avail_idx = vq->last_avail_idx; |
Jason Wang | e3b56cd | 2017-02-07 15:49:50 +0800 | [diff] [blame] | 2228 | |
| 2229 | if (vq->avail_idx == vq->last_avail_idx) { |
Jason Wang | 7b5d753 | 2019-05-24 04:12:14 -0400 | [diff] [blame] | 2230 | if (unlikely(vhost_get_avail_idx(vq, &avail_idx))) { |
Jason Wang | e3b56cd | 2017-02-07 15:49:50 +0800 | [diff] [blame] | 2231 | vq_err(vq, "Failed to access avail idx at %p\n", |
| 2232 | &vq->avail->idx); |
| 2233 | return -EFAULT; |
| 2234 | } |
| 2235 | vq->avail_idx = vhost16_to_cpu(vq, avail_idx); |
| 2236 | |
| 2237 | if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) { |
| 2238 | vq_err(vq, "Guest moved used index from %u to %u", |
| 2239 | last_avail_idx, vq->avail_idx); |
| 2240 | return -EFAULT; |
| 2241 | } |
| 2242 | |
| 2243 | /* If there's nothing new since last we looked, return |
| 2244 | * invalid. |
| 2245 | */ |
| 2246 | if (vq->avail_idx == last_avail_idx) |
| 2247 | return vq->num; |
| 2248 | |
| 2249 | /* Only get avail ring entries after they have been |
| 2250 | * exposed by guest. |
| 2251 | */ |
| 2252 | smp_rmb(); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2253 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2254 | |
| 2255 | /* Grab the next descriptor number they're advertising, and increment |
| 2256 | * the index we've seen. */ |
Jason Wang | 7b5d753 | 2019-05-24 04:12:14 -0400 | [diff] [blame] | 2257 | if (unlikely(vhost_get_avail_head(vq, &ring_head, last_avail_idx))) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2258 | vq_err(vq, "Failed to read head: idx %d address %p\n", |
| 2259 | last_avail_idx, |
| 2260 | &vq->avail->ring[last_avail_idx % vq->num]); |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 2261 | return -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2264 | head = vhost16_to_cpu(vq, ring_head); |
| 2265 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2266 | /* If their number is silly, that's an error. */ |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 2267 | if (unlikely(head >= vq->num)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2268 | vq_err(vq, "Guest says index %u > %u is available", |
| 2269 | head, vq->num); |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 2270 | return -EINVAL; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
| 2273 | /* When we start there are none of either input nor output. */ |
| 2274 | *out_num = *in_num = 0; |
| 2275 | if (unlikely(log)) |
| 2276 | *log_num = 0; |
| 2277 | |
| 2278 | i = head; |
| 2279 | do { |
| 2280 | unsigned iov_count = *in_num + *out_num; |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 2281 | if (unlikely(i >= vq->num)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2282 | vq_err(vq, "Desc index is %u > %u, head = %u", |
| 2283 | i, vq->num, head); |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 2284 | return -EINVAL; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2285 | } |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 2286 | if (unlikely(++found > vq->num)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2287 | vq_err(vq, "Loop detected: last one at %u " |
| 2288 | "vq size %u head %u\n", |
| 2289 | i, vq->num, head); |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 2290 | return -EINVAL; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2291 | } |
Jason Wang | 7b5d753 | 2019-05-24 04:12:14 -0400 | [diff] [blame] | 2292 | ret = vhost_get_desc(vq, &desc, i); |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 2293 | if (unlikely(ret)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2294 | vq_err(vq, "Failed to get descriptor: idx %d addr %p\n", |
| 2295 | i, vq->desc + i); |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 2296 | return -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2297 | } |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2298 | if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT)) { |
Michael S. Tsirkin | 47283be | 2014-06-05 15:20:27 +0300 | [diff] [blame] | 2299 | ret = get_indirect(vq, iov, iov_size, |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2300 | out_num, in_num, |
| 2301 | log, log_num, &desc); |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 2302 | if (unlikely(ret < 0)) { |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 2303 | if (ret != -EAGAIN) |
| 2304 | vq_err(vq, "Failure detected " |
| 2305 | "in indirect descriptor at idx %d\n", i); |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 2306 | return ret; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2307 | } |
| 2308 | continue; |
| 2309 | } |
| 2310 | |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 2311 | if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) |
| 2312 | access = VHOST_ACCESS_WO; |
| 2313 | else |
| 2314 | access = VHOST_ACCESS_RO; |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2315 | ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr), |
| 2316 | vhost32_to_cpu(vq, desc.len), iov + iov_count, |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 2317 | iov_size - iov_count, access); |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 2318 | if (unlikely(ret < 0)) { |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 2319 | if (ret != -EAGAIN) |
| 2320 | vq_err(vq, "Translation failure %d descriptor idx %d\n", |
| 2321 | ret, i); |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 2322 | return ret; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2323 | } |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 2324 | if (access == VHOST_ACCESS_WO) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2325 | /* If this is an input descriptor, |
| 2326 | * increment that count. */ |
| 2327 | *in_num += ret; |
yongduan | 060423b | 2019-09-11 17:44:24 +0800 | [diff] [blame] | 2328 | if (unlikely(log && ret)) { |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2329 | log[*log_num].addr = vhost64_to_cpu(vq, desc.addr); |
| 2330 | log[*log_num].len = vhost32_to_cpu(vq, desc.len); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2331 | ++*log_num; |
| 2332 | } |
| 2333 | } else { |
| 2334 | /* If it's an output descriptor, they're all supposed |
| 2335 | * to come before any input descriptors. */ |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 2336 | if (unlikely(*in_num)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2337 | vq_err(vq, "Descriptor has out after in: " |
| 2338 | "idx %d\n", i); |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 2339 | return -EINVAL; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2340 | } |
| 2341 | *out_num += ret; |
| 2342 | } |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2343 | } while ((i = next_desc(vq, &desc)) != -1); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2344 | |
| 2345 | /* On success, increment avail index. */ |
| 2346 | vq->last_avail_idx++; |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 2347 | |
| 2348 | /* Assume notifications from guest are disabled at this point, |
| 2349 | * if they aren't we would need to update avail_event index. */ |
| 2350 | BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY)); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2351 | return head; |
| 2352 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 2353 | EXPORT_SYMBOL_GPL(vhost_get_vq_desc); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2354 | |
| 2355 | /* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */ |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 2356 | void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2357 | { |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 2358 | vq->last_avail_idx -= n; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2359 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 2360 | EXPORT_SYMBOL_GPL(vhost_discard_vq_desc); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2361 | |
| 2362 | /* After we've used one of their buffers, we tell them about it. We'll then |
| 2363 | * want to notify the guest, using eventfd. */ |
| 2364 | int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len) |
| 2365 | { |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2366 | struct vring_used_elem heads = { |
| 2367 | cpu_to_vhost32(vq, head), |
| 2368 | cpu_to_vhost32(vq, len) |
| 2369 | }; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2370 | |
Jason Wang | c49e4e5 | 2013-09-02 16:40:58 +0800 | [diff] [blame] | 2371 | return vhost_add_used_n(vq, &heads, 1); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2372 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 2373 | EXPORT_SYMBOL_GPL(vhost_add_used); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2374 | |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 2375 | static int __vhost_add_used_n(struct vhost_virtqueue *vq, |
| 2376 | struct vring_used_elem *heads, |
| 2377 | unsigned count) |
| 2378 | { |
| 2379 | struct vring_used_elem __user *used; |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 2380 | u16 old, new; |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 2381 | int start; |
| 2382 | |
Michael S. Tsirkin | 5fba13b | 2015-11-29 13:34:44 +0200 | [diff] [blame] | 2383 | start = vq->last_used_idx & (vq->num - 1); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 2384 | used = vq->used->ring + start; |
Jason Wang | 7b5d753 | 2019-05-24 04:12:14 -0400 | [diff] [blame] | 2385 | if (vhost_put_used(vq, heads, start, count)) { |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 2386 | vq_err(vq, "Failed to write used"); |
| 2387 | return -EFAULT; |
| 2388 | } |
| 2389 | if (unlikely(vq->log_used)) { |
| 2390 | /* Make sure data is seen before log. */ |
| 2391 | smp_wmb(); |
| 2392 | /* Log used ring entry write. */ |
Jason Wang | cc5e710 | 2019-01-16 16:54:42 +0800 | [diff] [blame] | 2393 | log_used(vq, ((void __user *)used - (void __user *)vq->used), |
| 2394 | count * sizeof *used); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 2395 | } |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 2396 | old = vq->last_used_idx; |
| 2397 | new = (vq->last_used_idx += count); |
| 2398 | /* If the driver never bothers to signal in a very long while, |
| 2399 | * used index might wrap around. If that happens, invalidate |
| 2400 | * signalled_used index we stored. TODO: make sure driver |
| 2401 | * signals at least once in 2^16 and remove this. */ |
| 2402 | if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old))) |
| 2403 | vq->signalled_used_valid = false; |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 2404 | return 0; |
| 2405 | } |
| 2406 | |
| 2407 | /* After we've used one of their buffers, we tell them about it. We'll then |
| 2408 | * want to notify the guest, using eventfd. */ |
| 2409 | int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads, |
| 2410 | unsigned count) |
| 2411 | { |
| 2412 | int start, n, r; |
| 2413 | |
Michael S. Tsirkin | 5fba13b | 2015-11-29 13:34:44 +0200 | [diff] [blame] | 2414 | start = vq->last_used_idx & (vq->num - 1); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 2415 | n = vq->num - start; |
| 2416 | if (n < count) { |
| 2417 | r = __vhost_add_used_n(vq, heads, n); |
| 2418 | if (r < 0) |
| 2419 | return r; |
| 2420 | heads += n; |
| 2421 | count -= n; |
| 2422 | } |
| 2423 | r = __vhost_add_used_n(vq, heads, count); |
| 2424 | |
| 2425 | /* Make sure buffer is written before we update index. */ |
| 2426 | smp_wmb(); |
Jason Wang | 7b5d753 | 2019-05-24 04:12:14 -0400 | [diff] [blame] | 2427 | if (vhost_put_used_idx(vq)) { |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 2428 | vq_err(vq, "Failed to increment used idx"); |
| 2429 | return -EFAULT; |
| 2430 | } |
| 2431 | if (unlikely(vq->log_used)) { |
Jason Wang | 841df92 | 2018-12-13 10:53:37 +0800 | [diff] [blame] | 2432 | /* Make sure used idx is seen before log. */ |
| 2433 | smp_wmb(); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 2434 | /* Log used index update. */ |
Jason Wang | cc5e710 | 2019-01-16 16:54:42 +0800 | [diff] [blame] | 2435 | log_used(vq, offsetof(struct vring_used, idx), |
| 2436 | sizeof vq->used->idx); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 2437 | if (vq->log_ctx) |
| 2438 | eventfd_signal(vq->log_ctx, 1); |
| 2439 | } |
| 2440 | return r; |
| 2441 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 2442 | EXPORT_SYMBOL_GPL(vhost_add_used_n); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 2443 | |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 2444 | static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2445 | { |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2446 | __u16 old, new; |
| 2447 | __virtio16 event; |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 2448 | bool v; |
Jason Wang | 8d65843 | 2017-07-27 11:22:05 +0800 | [diff] [blame] | 2449 | /* Flush out used index updates. This is paired |
| 2450 | * with the barrier that the Guest executes when enabling |
| 2451 | * interrupts. */ |
| 2452 | smp_mb(); |
Michael S. Tsirkin | 0d49935 | 2010-05-11 19:44:17 +0300 | [diff] [blame] | 2453 | |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 2454 | if (vhost_has_feature(vq, VIRTIO_F_NOTIFY_ON_EMPTY) && |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 2455 | unlikely(vq->avail_idx == vq->last_avail_idx)) |
| 2456 | return true; |
| 2457 | |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 2458 | if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) { |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2459 | __virtio16 flags; |
Jason Wang | 7b5d753 | 2019-05-24 04:12:14 -0400 | [diff] [blame] | 2460 | if (vhost_get_avail_flags(vq, &flags)) { |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 2461 | vq_err(vq, "Failed to get flags"); |
| 2462 | return true; |
| 2463 | } |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2464 | return !(flags & cpu_to_vhost16(vq, VRING_AVAIL_F_NO_INTERRUPT)); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2465 | } |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 2466 | old = vq->signalled_used; |
| 2467 | v = vq->signalled_used_valid; |
| 2468 | new = vq->signalled_used = vq->last_used_idx; |
| 2469 | vq->signalled_used_valid = true; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2470 | |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 2471 | if (unlikely(!v)) |
| 2472 | return true; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2473 | |
Jason Wang | 7b5d753 | 2019-05-24 04:12:14 -0400 | [diff] [blame] | 2474 | if (vhost_get_used_event(vq, &event)) { |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 2475 | vq_err(vq, "Failed to get used event idx"); |
| 2476 | return true; |
| 2477 | } |
Jason Wang | 8d65843 | 2017-07-27 11:22:05 +0800 | [diff] [blame] | 2478 | return vring_need_event(vhost16_to_cpu(vq, event), new, old); |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 2479 | } |
| 2480 | |
| 2481 | /* This actually signals the guest, using eventfd. */ |
| 2482 | void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq) |
| 2483 | { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2484 | /* Signal the Guest tell them we used something up. */ |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 2485 | if (vq->call_ctx && vhost_notify(dev, vq)) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2486 | eventfd_signal(vq->call_ctx, 1); |
| 2487 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 2488 | EXPORT_SYMBOL_GPL(vhost_signal); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2489 | |
| 2490 | /* And here's the combo meal deal. Supersize me! */ |
| 2491 | void vhost_add_used_and_signal(struct vhost_dev *dev, |
| 2492 | struct vhost_virtqueue *vq, |
| 2493 | unsigned int head, int len) |
| 2494 | { |
| 2495 | vhost_add_used(vq, head, len); |
| 2496 | vhost_signal(dev, vq); |
| 2497 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 2498 | EXPORT_SYMBOL_GPL(vhost_add_used_and_signal); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2499 | |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 2500 | /* multi-buffer version of vhost_add_used_and_signal */ |
| 2501 | void vhost_add_used_and_signal_n(struct vhost_dev *dev, |
| 2502 | struct vhost_virtqueue *vq, |
| 2503 | struct vring_used_elem *heads, unsigned count) |
| 2504 | { |
| 2505 | vhost_add_used_n(vq, heads, count); |
| 2506 | vhost_signal(dev, vq); |
| 2507 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 2508 | EXPORT_SYMBOL_GPL(vhost_add_used_and_signal_n); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 2509 | |
Jason Wang | d4a6060 | 2016-03-04 06:24:52 -0500 | [diff] [blame] | 2510 | /* return true if we're sure that avaiable ring is empty */ |
| 2511 | bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq) |
| 2512 | { |
| 2513 | __virtio16 avail_idx; |
| 2514 | int r; |
| 2515 | |
Jason Wang | 275bf96 | 2017-01-18 15:02:01 +0800 | [diff] [blame] | 2516 | if (vq->avail_idx != vq->last_avail_idx) |
Jason Wang | d4a6060 | 2016-03-04 06:24:52 -0500 | [diff] [blame] | 2517 | return false; |
| 2518 | |
Jason Wang | 7b5d753 | 2019-05-24 04:12:14 -0400 | [diff] [blame] | 2519 | r = vhost_get_avail_idx(vq, &avail_idx); |
Jason Wang | 275bf96 | 2017-01-18 15:02:01 +0800 | [diff] [blame] | 2520 | if (unlikely(r)) |
| 2521 | return false; |
| 2522 | vq->avail_idx = vhost16_to_cpu(vq, avail_idx); |
| 2523 | |
| 2524 | return vq->avail_idx == vq->last_avail_idx; |
Jason Wang | d4a6060 | 2016-03-04 06:24:52 -0500 | [diff] [blame] | 2525 | } |
| 2526 | EXPORT_SYMBOL_GPL(vhost_vq_avail_empty); |
| 2527 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2528 | /* OK, now we need to know about added descriptors. */ |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 2529 | bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2530 | { |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2531 | __virtio16 avail_idx; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2532 | int r; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 2533 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2534 | if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY)) |
| 2535 | return false; |
| 2536 | vq->used_flags &= ~VRING_USED_F_NO_NOTIFY; |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 2537 | if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) { |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 2538 | r = vhost_update_used_flags(vq); |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 2539 | if (r) { |
| 2540 | vq_err(vq, "Failed to enable notification at %p: %d\n", |
| 2541 | &vq->used->flags, r); |
| 2542 | return false; |
| 2543 | } |
| 2544 | } else { |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 2545 | r = vhost_update_avail_event(vq, vq->avail_idx); |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 2546 | if (r) { |
| 2547 | vq_err(vq, "Failed to update avail event index at %p: %d\n", |
| 2548 | vhost_avail_event(vq), r); |
| 2549 | return false; |
| 2550 | } |
| 2551 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2552 | /* They could have slipped one in as we were doing that: make |
| 2553 | * sure it's written, then check again. */ |
Michael S. Tsirkin | 5659338 | 2010-02-01 07:21:02 +0000 | [diff] [blame] | 2554 | smp_mb(); |
Jason Wang | 7b5d753 | 2019-05-24 04:12:14 -0400 | [diff] [blame] | 2555 | r = vhost_get_avail_idx(vq, &avail_idx); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2556 | if (r) { |
| 2557 | vq_err(vq, "Failed to check avail idx at %p: %d\n", |
| 2558 | &vq->avail->idx, r); |
| 2559 | return false; |
| 2560 | } |
| 2561 | |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 2562 | return vhost16_to_cpu(vq, avail_idx) != vq->avail_idx; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2563 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 2564 | EXPORT_SYMBOL_GPL(vhost_enable_notify); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2565 | |
| 2566 | /* We don't need to be notified again. */ |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 2567 | void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2568 | { |
| 2569 | int r; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 2570 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2571 | if (vq->used_flags & VRING_USED_F_NO_NOTIFY) |
| 2572 | return; |
| 2573 | vq->used_flags |= VRING_USED_F_NO_NOTIFY; |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 2574 | if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) { |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 2575 | r = vhost_update_used_flags(vq); |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 2576 | if (r) |
| 2577 | vq_err(vq, "Failed to enable notification at %p: %d\n", |
| 2578 | &vq->used->flags, r); |
| 2579 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 2580 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 2581 | EXPORT_SYMBOL_GPL(vhost_disable_notify); |
| 2582 | |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 2583 | /* Create a new message. */ |
| 2584 | struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type) |
| 2585 | { |
| 2586 | struct vhost_msg_node *node = kmalloc(sizeof *node, GFP_KERNEL); |
| 2587 | if (!node) |
| 2588 | return NULL; |
Michael S. Tsirkin | 670ae9c | 2018-05-12 00:33:10 +0300 | [diff] [blame] | 2589 | |
| 2590 | /* Make sure all padding within the structure is initialized. */ |
| 2591 | memset(&node->msg, 0, sizeof node->msg); |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 2592 | node->vq = vq; |
| 2593 | node->msg.type = type; |
| 2594 | return node; |
| 2595 | } |
| 2596 | EXPORT_SYMBOL_GPL(vhost_new_msg); |
| 2597 | |
| 2598 | void vhost_enqueue_msg(struct vhost_dev *dev, struct list_head *head, |
| 2599 | struct vhost_msg_node *node) |
| 2600 | { |
| 2601 | spin_lock(&dev->iotlb_lock); |
| 2602 | list_add_tail(&node->node, head); |
| 2603 | spin_unlock(&dev->iotlb_lock); |
| 2604 | |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 2605 | wake_up_interruptible_poll(&dev->wait, EPOLLIN | EPOLLRDNORM); |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 2606 | } |
| 2607 | EXPORT_SYMBOL_GPL(vhost_enqueue_msg); |
| 2608 | |
| 2609 | struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev, |
| 2610 | struct list_head *head) |
| 2611 | { |
| 2612 | struct vhost_msg_node *node = NULL; |
| 2613 | |
| 2614 | spin_lock(&dev->iotlb_lock); |
| 2615 | if (!list_empty(head)) { |
| 2616 | node = list_first_entry(head, struct vhost_msg_node, |
| 2617 | node); |
| 2618 | list_del(&node->node); |
| 2619 | } |
| 2620 | spin_unlock(&dev->iotlb_lock); |
| 2621 | |
| 2622 | return node; |
| 2623 | } |
| 2624 | EXPORT_SYMBOL_GPL(vhost_dequeue_msg); |
| 2625 | |
| 2626 | |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 2627 | static int __init vhost_init(void) |
| 2628 | { |
| 2629 | return 0; |
| 2630 | } |
| 2631 | |
| 2632 | static void __exit vhost_exit(void) |
| 2633 | { |
| 2634 | } |
| 2635 | |
| 2636 | module_init(vhost_init); |
| 2637 | module_exit(vhost_exit); |
| 2638 | |
| 2639 | MODULE_VERSION("0.0.1"); |
| 2640 | MODULE_LICENSE("GPL v2"); |
| 2641 | MODULE_AUTHOR("Michael S. Tsirkin"); |
| 2642 | MODULE_DESCRIPTION("Host kernel accelerator for virtio"); |