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