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