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