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