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