Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2009 Red Hat, Inc. |
| 2 | * Copyright (C) 2006 Rusty Russell IBM Corporation |
| 3 | * |
| 4 | * Author: Michael S. Tsirkin <mst@redhat.com> |
| 5 | * |
| 6 | * Inspiration, some code, and most witty comments come from |
Rob Landley | 6151658 | 2011-05-06 09:27:36 -0700 | [diff] [blame] | 7 | * Documentation/virtual/lguest/lguest.c, by Rusty Russell |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2. |
| 10 | * |
| 11 | * Generic code for virtio server in host kernel. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/eventfd.h> |
| 15 | #include <linux/vhost.h> |
Asias He | 35596b2 | 2013-08-19 09:23:19 +0800 | [diff] [blame] | 16 | #include <linux/uio.h> |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 17 | #include <linux/mm.h> |
Michael S. Tsirkin | 64e1c80 | 2010-10-06 15:34:45 +0200 | [diff] [blame] | 18 | #include <linux/mmu_context.h> |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 19 | #include <linux/miscdevice.h> |
| 20 | #include <linux/mutex.h> |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 21 | #include <linux/poll.h> |
| 22 | #include <linux/file.h> |
| 23 | #include <linux/highmem.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Igor Mammedov | 4de7255 | 2015-07-01 11:07:09 +0200 | [diff] [blame] | 25 | #include <linux/vmalloc.h> |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 26 | #include <linux/kthread.h> |
Michael S. Tsirkin | 9e3d195 | 2010-07-27 22:56:50 +0300 | [diff] [blame] | 27 | #include <linux/cgroup.h> |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 28 | #include <linux/module.h> |
Igor Mammedov | bcfeaca | 2015-06-16 18:33:35 +0200 | [diff] [blame] | 29 | #include <linux/sort.h> |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 30 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 31 | #include "vhost.h" |
| 32 | |
Igor Mammedov | c9ce42f | 2015-07-02 15:08:11 +0200 | [diff] [blame] | 33 | static ushort max_mem_regions = 64; |
| 34 | module_param(max_mem_regions, ushort, 0444); |
| 35 | MODULE_PARM_DESC(max_mem_regions, |
| 36 | "Maximum number of memory regions in memory map. (default: 64)"); |
| 37 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 38 | enum { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 39 | VHOST_MEMORY_F_LOG = 0x1, |
| 40 | }; |
| 41 | |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 42 | #define vhost_used_event(vq) ((__virtio16 __user *)&vq->avail->ring[vq->num]) |
| 43 | #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] | 44 | |
Greg Kurz | 2751c98 | 2015-04-24 14:27:24 +0200 | [diff] [blame] | 45 | #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY |
Greg Kurz | c507203 | 2016-02-16 15:59:34 +0100 | [diff] [blame] | 46 | static void vhost_disable_cross_endian(struct vhost_virtqueue *vq) |
Greg Kurz | 2751c98 | 2015-04-24 14:27:24 +0200 | [diff] [blame] | 47 | { |
| 48 | vq->user_be = !virtio_legacy_is_little_endian(); |
| 49 | } |
| 50 | |
Greg Kurz | c507203 | 2016-02-16 15:59:34 +0100 | [diff] [blame] | 51 | static void vhost_enable_cross_endian_big(struct vhost_virtqueue *vq) |
| 52 | { |
| 53 | vq->user_be = true; |
| 54 | } |
| 55 | |
| 56 | static void vhost_enable_cross_endian_little(struct vhost_virtqueue *vq) |
| 57 | { |
| 58 | vq->user_be = false; |
| 59 | } |
| 60 | |
Greg Kurz | 2751c98 | 2015-04-24 14:27:24 +0200 | [diff] [blame] | 61 | static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp) |
| 62 | { |
| 63 | struct vhost_vring_state s; |
| 64 | |
| 65 | if (vq->private_data) |
| 66 | return -EBUSY; |
| 67 | |
| 68 | if (copy_from_user(&s, argp, sizeof(s))) |
| 69 | return -EFAULT; |
| 70 | |
| 71 | if (s.num != VHOST_VRING_LITTLE_ENDIAN && |
| 72 | s.num != VHOST_VRING_BIG_ENDIAN) |
| 73 | return -EINVAL; |
| 74 | |
Greg Kurz | c507203 | 2016-02-16 15:59:34 +0100 | [diff] [blame] | 75 | if (s.num == VHOST_VRING_BIG_ENDIAN) |
| 76 | vhost_enable_cross_endian_big(vq); |
| 77 | else |
| 78 | vhost_enable_cross_endian_little(vq); |
Greg Kurz | 2751c98 | 2015-04-24 14:27:24 +0200 | [diff] [blame] | 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx, |
| 84 | int __user *argp) |
| 85 | { |
| 86 | struct vhost_vring_state s = { |
| 87 | .index = idx, |
| 88 | .num = vq->user_be |
| 89 | }; |
| 90 | |
| 91 | if (copy_to_user(argp, &s, sizeof(s))) |
| 92 | return -EFAULT; |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static void vhost_init_is_le(struct vhost_virtqueue *vq) |
| 98 | { |
| 99 | /* Note for legacy virtio: user_be is initialized at reset time |
| 100 | * according to the host endianness. If userspace does not set an |
| 101 | * explicit endianness, the default behavior is native endian, as |
| 102 | * expected by legacy virtio. |
| 103 | */ |
| 104 | vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) || !vq->user_be; |
| 105 | } |
| 106 | #else |
Greg Kurz | c507203 | 2016-02-16 15:59:34 +0100 | [diff] [blame] | 107 | static void vhost_disable_cross_endian(struct vhost_virtqueue *vq) |
Greg Kurz | 2751c98 | 2015-04-24 14:27:24 +0200 | [diff] [blame] | 108 | { |
| 109 | } |
| 110 | |
| 111 | static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp) |
| 112 | { |
| 113 | return -ENOIOCTLCMD; |
| 114 | } |
| 115 | |
| 116 | static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx, |
| 117 | int __user *argp) |
| 118 | { |
| 119 | return -ENOIOCTLCMD; |
| 120 | } |
| 121 | |
| 122 | static void vhost_init_is_le(struct vhost_virtqueue *vq) |
| 123 | { |
| 124 | if (vhost_has_feature(vq, VIRTIO_F_VERSION_1)) |
| 125 | vq->is_le = true; |
| 126 | } |
| 127 | #endif /* CONFIG_VHOST_CROSS_ENDIAN_LEGACY */ |
| 128 | |
Greg Kurz | c507203 | 2016-02-16 15:59:34 +0100 | [diff] [blame] | 129 | static void vhost_reset_is_le(struct vhost_virtqueue *vq) |
| 130 | { |
| 131 | vq->is_le = virtio_legacy_is_little_endian(); |
| 132 | } |
| 133 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 134 | static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh, |
| 135 | poll_table *pt) |
| 136 | { |
| 137 | struct vhost_poll *poll; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 138 | |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 139 | poll = container_of(pt, struct vhost_poll, table); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 140 | poll->wqh = wqh; |
| 141 | add_wait_queue(wqh, &poll->wait); |
| 142 | } |
| 143 | |
| 144 | static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync, |
| 145 | void *key) |
| 146 | { |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 147 | struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait); |
| 148 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 149 | if (!((unsigned long)key & poll->mask)) |
| 150 | return 0; |
| 151 | |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 152 | vhost_poll_queue(poll); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 153 | return 0; |
| 154 | } |
| 155 | |
Stefan Hajnoczi | 163049a | 2012-07-21 06:55:37 +0000 | [diff] [blame] | 156 | 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] | 157 | { |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 158 | INIT_LIST_HEAD(&work->node); |
| 159 | work->fn = fn; |
| 160 | init_waitqueue_head(&work->done); |
| 161 | work->flushing = 0; |
| 162 | work->queue_seq = work->done_seq = 0; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 163 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 164 | EXPORT_SYMBOL_GPL(vhost_work_init); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 165 | |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 166 | /* Init poll structure */ |
| 167 | void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn, |
| 168 | unsigned long mask, struct vhost_dev *dev) |
| 169 | { |
| 170 | init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup); |
| 171 | init_poll_funcptr(&poll->table, vhost_poll_func); |
| 172 | poll->mask = mask; |
| 173 | poll->dev = dev; |
Jason Wang | 2b8b328 | 2013-01-28 01:05:18 +0000 | [diff] [blame] | 174 | poll->wqh = NULL; |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 175 | |
| 176 | vhost_work_init(&poll->work, fn); |
| 177 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 178 | EXPORT_SYMBOL_GPL(vhost_poll_init); |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 179 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 180 | /* Start polling a file. We add ourselves to file's wait queue. The caller must |
| 181 | * 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] | 182 | int vhost_poll_start(struct vhost_poll *poll, struct file *file) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 183 | { |
| 184 | unsigned long mask; |
Jason Wang | 2b8b328 | 2013-01-28 01:05:18 +0000 | [diff] [blame] | 185 | int ret = 0; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 186 | |
Jason Wang | 70181d51 | 2013-04-10 20:50:48 +0000 | [diff] [blame] | 187 | if (poll->wqh) |
| 188 | return 0; |
| 189 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 190 | mask = file->f_op->poll(file, &poll->table); |
| 191 | if (mask) |
| 192 | vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask); |
Jason Wang | 2b8b328 | 2013-01-28 01:05:18 +0000 | [diff] [blame] | 193 | if (mask & POLLERR) { |
| 194 | if (poll->wqh) |
| 195 | remove_wait_queue(poll->wqh, &poll->wait); |
| 196 | ret = -EINVAL; |
| 197 | } |
| 198 | |
| 199 | return ret; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 200 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 201 | EXPORT_SYMBOL_GPL(vhost_poll_start); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 202 | |
| 203 | /* Stop polling a file. After this function returns, it becomes safe to drop the |
| 204 | * file reference. You must also flush afterwards. */ |
| 205 | void vhost_poll_stop(struct vhost_poll *poll) |
| 206 | { |
Jason Wang | 2b8b328 | 2013-01-28 01:05:18 +0000 | [diff] [blame] | 207 | if (poll->wqh) { |
| 208 | remove_wait_queue(poll->wqh, &poll->wait); |
| 209 | poll->wqh = NULL; |
| 210 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 211 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 212 | EXPORT_SYMBOL_GPL(vhost_poll_stop); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 213 | |
Michael S. Tsirkin | 0174b0c | 2011-01-10 10:03:20 +0200 | [diff] [blame] | 214 | static bool vhost_work_seq_done(struct vhost_dev *dev, struct vhost_work *work, |
| 215 | unsigned seq) |
| 216 | { |
| 217 | int left; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 218 | |
Michael S. Tsirkin | 0174b0c | 2011-01-10 10:03:20 +0200 | [diff] [blame] | 219 | spin_lock_irq(&dev->work_lock); |
| 220 | left = seq - work->done_seq; |
| 221 | spin_unlock_irq(&dev->work_lock); |
| 222 | return left <= 0; |
| 223 | } |
| 224 | |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 225 | 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] | 226 | { |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 227 | unsigned seq; |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 228 | int flushing; |
| 229 | |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 230 | spin_lock_irq(&dev->work_lock); |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 231 | seq = work->queue_seq; |
| 232 | work->flushing++; |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 233 | spin_unlock_irq(&dev->work_lock); |
Michael S. Tsirkin | 0174b0c | 2011-01-10 10:03:20 +0200 | [diff] [blame] | 234 | wait_event(work->done, vhost_work_seq_done(dev, work, seq)); |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 235 | spin_lock_irq(&dev->work_lock); |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 236 | flushing = --work->flushing; |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 237 | spin_unlock_irq(&dev->work_lock); |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 238 | BUG_ON(flushing < 0); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 239 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 240 | EXPORT_SYMBOL_GPL(vhost_work_flush); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 241 | |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 242 | /* Flush any work that has been scheduled. When calling this, don't hold any |
| 243 | * locks that are also used by the callback. */ |
| 244 | void vhost_poll_flush(struct vhost_poll *poll) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 245 | { |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 246 | vhost_work_flush(poll->dev, &poll->work); |
| 247 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 248 | EXPORT_SYMBOL_GPL(vhost_poll_flush); |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 249 | |
Stefan Hajnoczi | 163049a | 2012-07-21 06:55:37 +0000 | [diff] [blame] | 250 | 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] | 251 | { |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 252 | unsigned long flags; |
| 253 | |
| 254 | spin_lock_irqsave(&dev->work_lock, flags); |
| 255 | if (list_empty(&work->node)) { |
| 256 | list_add_tail(&work->node, &dev->work_list); |
| 257 | work->queue_seq++; |
Qin Chuanyu | ac9fde2 | 2013-06-07 21:50:16 +0800 | [diff] [blame] | 258 | spin_unlock_irqrestore(&dev->work_lock, flags); |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 259 | wake_up_process(dev->worker); |
Qin Chuanyu | ac9fde2 | 2013-06-07 21:50:16 +0800 | [diff] [blame] | 260 | } else { |
| 261 | spin_unlock_irqrestore(&dev->work_lock, flags); |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 262 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 263 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 264 | EXPORT_SYMBOL_GPL(vhost_work_queue); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 265 | |
Jason Wang | 526d3e7 | 2016-03-04 06:24:51 -0500 | [diff] [blame^] | 266 | /* A lockless hint for busy polling code to exit the loop */ |
| 267 | bool vhost_has_work(struct vhost_dev *dev) |
| 268 | { |
| 269 | return !list_empty(&dev->work_list); |
| 270 | } |
| 271 | EXPORT_SYMBOL_GPL(vhost_has_work); |
| 272 | |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 273 | void vhost_poll_queue(struct vhost_poll *poll) |
| 274 | { |
| 275 | vhost_work_queue(poll->dev, &poll->work); |
| 276 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 277 | EXPORT_SYMBOL_GPL(vhost_poll_queue); |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 278 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 279 | static void vhost_vq_reset(struct vhost_dev *dev, |
| 280 | struct vhost_virtqueue *vq) |
| 281 | { |
| 282 | vq->num = 1; |
| 283 | vq->desc = NULL; |
| 284 | vq->avail = NULL; |
| 285 | vq->used = NULL; |
| 286 | vq->last_avail_idx = 0; |
| 287 | vq->avail_idx = 0; |
| 288 | vq->last_used_idx = 0; |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 289 | vq->signalled_used = 0; |
| 290 | vq->signalled_used_valid = false; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 291 | vq->used_flags = 0; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 292 | vq->log_used = false; |
| 293 | vq->log_addr = -1ull; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 294 | vq->private_data = NULL; |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 295 | vq->acked_features = 0; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 296 | vq->log_base = NULL; |
| 297 | vq->error_ctx = NULL; |
| 298 | vq->error = NULL; |
| 299 | vq->kick = NULL; |
| 300 | vq->call_ctx = NULL; |
| 301 | vq->call = NULL; |
Michael S. Tsirkin | 73a99f0 | 2010-02-23 11:23:45 +0200 | [diff] [blame] | 302 | vq->log_ctx = NULL; |
Michael S. Tsirkin | 47283be | 2014-06-05 15:20:27 +0300 | [diff] [blame] | 303 | vq->memory = NULL; |
Greg Kurz | c507203 | 2016-02-16 15:59:34 +0100 | [diff] [blame] | 304 | vhost_reset_is_le(vq); |
| 305 | vhost_disable_cross_endian(vq); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 308 | static int vhost_worker(void *data) |
| 309 | { |
| 310 | struct vhost_dev *dev = data; |
| 311 | struct vhost_work *work = NULL; |
| 312 | unsigned uninitialized_var(seq); |
Jens Freimann | d7ffde3 | 2012-06-26 00:59:58 +0000 | [diff] [blame] | 313 | mm_segment_t oldfs = get_fs(); |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 314 | |
Jens Freimann | d7ffde3 | 2012-06-26 00:59:58 +0000 | [diff] [blame] | 315 | set_fs(USER_DS); |
Michael S. Tsirkin | 64e1c80 | 2010-10-06 15:34:45 +0200 | [diff] [blame] | 316 | use_mm(dev->mm); |
| 317 | |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 318 | for (;;) { |
| 319 | /* mb paired w/ kthread_stop */ |
| 320 | set_current_state(TASK_INTERRUPTIBLE); |
| 321 | |
| 322 | spin_lock_irq(&dev->work_lock); |
| 323 | if (work) { |
| 324 | work->done_seq = seq; |
| 325 | if (work->flushing) |
| 326 | wake_up_all(&work->done); |
| 327 | } |
| 328 | |
| 329 | if (kthread_should_stop()) { |
| 330 | spin_unlock_irq(&dev->work_lock); |
| 331 | __set_current_state(TASK_RUNNING); |
Michael S. Tsirkin | 64e1c80 | 2010-10-06 15:34:45 +0200 | [diff] [blame] | 332 | break; |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 333 | } |
| 334 | if (!list_empty(&dev->work_list)) { |
| 335 | work = list_first_entry(&dev->work_list, |
| 336 | struct vhost_work, node); |
| 337 | list_del_init(&work->node); |
| 338 | seq = work->queue_seq; |
| 339 | } else |
| 340 | work = NULL; |
| 341 | spin_unlock_irq(&dev->work_lock); |
| 342 | |
| 343 | if (work) { |
| 344 | __set_current_state(TASK_RUNNING); |
| 345 | work->fn(work); |
Nadav Har'El | d550dda | 2012-02-27 15:07:29 +0200 | [diff] [blame] | 346 | if (need_resched()) |
| 347 | schedule(); |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 348 | } else |
| 349 | schedule(); |
| 350 | |
| 351 | } |
Michael S. Tsirkin | 64e1c80 | 2010-10-06 15:34:45 +0200 | [diff] [blame] | 352 | unuse_mm(dev->mm); |
Jens Freimann | d7ffde3 | 2012-06-26 00:59:58 +0000 | [diff] [blame] | 353 | set_fs(oldfs); |
Michael S. Tsirkin | 64e1c80 | 2010-10-06 15:34:45 +0200 | [diff] [blame] | 354 | return 0; |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 355 | } |
| 356 | |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 357 | static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq) |
| 358 | { |
| 359 | kfree(vq->indirect); |
| 360 | vq->indirect = NULL; |
| 361 | kfree(vq->log); |
| 362 | vq->log = NULL; |
| 363 | kfree(vq->heads); |
| 364 | vq->heads = NULL; |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Jason Wang | e0e9b40 | 2010-09-14 23:53:05 +0800 | [diff] [blame] | 367 | /* Helper to allocate iovec buffers for all vqs. */ |
| 368 | static long vhost_dev_alloc_iovecs(struct vhost_dev *dev) |
| 369 | { |
Asias He | 6d5e6aa | 2013-05-06 16:38:23 +0800 | [diff] [blame] | 370 | struct vhost_virtqueue *vq; |
Jason Wang | e0e9b40 | 2010-09-14 23:53:05 +0800 | [diff] [blame] | 371 | int i; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 372 | |
Jason Wang | e0e9b40 | 2010-09-14 23:53:05 +0800 | [diff] [blame] | 373 | for (i = 0; i < dev->nvqs; ++i) { |
Asias He | 6d5e6aa | 2013-05-06 16:38:23 +0800 | [diff] [blame] | 374 | vq = dev->vqs[i]; |
| 375 | vq->indirect = kmalloc(sizeof *vq->indirect * UIO_MAXIOV, |
| 376 | GFP_KERNEL); |
| 377 | vq->log = kmalloc(sizeof *vq->log * UIO_MAXIOV, GFP_KERNEL); |
| 378 | vq->heads = kmalloc(sizeof *vq->heads * UIO_MAXIOV, GFP_KERNEL); |
| 379 | if (!vq->indirect || !vq->log || !vq->heads) |
Jason Wang | e0e9b40 | 2010-09-14 23:53:05 +0800 | [diff] [blame] | 380 | goto err_nomem; |
| 381 | } |
| 382 | return 0; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 383 | |
Jason Wang | e0e9b40 | 2010-09-14 23:53:05 +0800 | [diff] [blame] | 384 | err_nomem: |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 385 | for (; i >= 0; --i) |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 386 | vhost_vq_free_iovecs(dev->vqs[i]); |
Jason Wang | e0e9b40 | 2010-09-14 23:53:05 +0800 | [diff] [blame] | 387 | return -ENOMEM; |
| 388 | } |
| 389 | |
| 390 | static void vhost_dev_free_iovecs(struct vhost_dev *dev) |
| 391 | { |
| 392 | int i; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 393 | |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 394 | for (i = 0; i < dev->nvqs; ++i) |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 395 | vhost_vq_free_iovecs(dev->vqs[i]); |
Jason Wang | e0e9b40 | 2010-09-14 23:53:05 +0800 | [diff] [blame] | 396 | } |
| 397 | |
Zhi Yong Wu | 59566b6e | 2013-12-07 04:13:03 +0800 | [diff] [blame] | 398 | void vhost_dev_init(struct vhost_dev *dev, |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 399 | struct vhost_virtqueue **vqs, int nvqs) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 400 | { |
Asias He | 6d5e6aa | 2013-05-06 16:38:23 +0800 | [diff] [blame] | 401 | struct vhost_virtqueue *vq; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 402 | int i; |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 403 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 404 | dev->vqs = vqs; |
| 405 | dev->nvqs = nvqs; |
| 406 | mutex_init(&dev->mutex); |
| 407 | dev->log_ctx = NULL; |
| 408 | dev->log_file = NULL; |
| 409 | dev->memory = NULL; |
| 410 | dev->mm = NULL; |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 411 | spin_lock_init(&dev->work_lock); |
| 412 | INIT_LIST_HEAD(&dev->work_list); |
| 413 | dev->worker = NULL; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 414 | |
| 415 | for (i = 0; i < dev->nvqs; ++i) { |
Asias He | 6d5e6aa | 2013-05-06 16:38:23 +0800 | [diff] [blame] | 416 | vq = dev->vqs[i]; |
| 417 | vq->log = NULL; |
| 418 | vq->indirect = NULL; |
| 419 | vq->heads = NULL; |
| 420 | vq->dev = dev; |
| 421 | mutex_init(&vq->mutex); |
| 422 | vhost_vq_reset(dev, vq); |
| 423 | if (vq->handle_kick) |
| 424 | vhost_poll_init(&vq->poll, vq->handle_kick, |
| 425 | POLLIN, dev); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 426 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 427 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 428 | EXPORT_SYMBOL_GPL(vhost_dev_init); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 429 | |
| 430 | /* Caller should have device mutex */ |
| 431 | long vhost_dev_check_owner(struct vhost_dev *dev) |
| 432 | { |
| 433 | /* Are you the owner? If not, I don't think you mean to do that */ |
| 434 | return dev->mm == current->mm ? 0 : -EPERM; |
| 435 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 436 | EXPORT_SYMBOL_GPL(vhost_dev_check_owner); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 437 | |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 438 | struct vhost_attach_cgroups_struct { |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 439 | struct vhost_work work; |
| 440 | struct task_struct *owner; |
| 441 | int ret; |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 442 | }; |
| 443 | |
| 444 | static void vhost_attach_cgroups_work(struct vhost_work *work) |
| 445 | { |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 446 | struct vhost_attach_cgroups_struct *s; |
| 447 | |
| 448 | s = container_of(work, struct vhost_attach_cgroups_struct, work); |
| 449 | s->ret = cgroup_attach_task_all(s->owner, current); |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | static int vhost_attach_cgroups(struct vhost_dev *dev) |
| 453 | { |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 454 | struct vhost_attach_cgroups_struct attach; |
| 455 | |
| 456 | attach.owner = current; |
| 457 | vhost_work_init(&attach.work, vhost_attach_cgroups_work); |
| 458 | vhost_work_queue(dev, &attach.work); |
| 459 | vhost_work_flush(dev, &attach.work); |
| 460 | return attach.ret; |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 461 | } |
| 462 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 463 | /* Caller should have device mutex */ |
Michael S. Tsirkin | 05c0535 | 2013-06-06 15:20:39 +0300 | [diff] [blame] | 464 | bool vhost_dev_has_owner(struct vhost_dev *dev) |
| 465 | { |
| 466 | return dev->mm; |
| 467 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 468 | EXPORT_SYMBOL_GPL(vhost_dev_has_owner); |
Michael S. Tsirkin | 05c0535 | 2013-06-06 15:20:39 +0300 | [diff] [blame] | 469 | |
| 470 | /* Caller should have device mutex */ |
Asias He | 54db63c | 2013-05-06 11:15:59 +0800 | [diff] [blame] | 471 | long vhost_dev_set_owner(struct vhost_dev *dev) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 472 | { |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 473 | struct task_struct *worker; |
| 474 | int err; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 475 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 476 | /* Is there an owner already? */ |
Michael S. Tsirkin | 05c0535 | 2013-06-06 15:20:39 +0300 | [diff] [blame] | 477 | if (vhost_dev_has_owner(dev)) { |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 478 | err = -EBUSY; |
| 479 | goto err_mm; |
| 480 | } |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 481 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 482 | /* No owner, become one */ |
| 483 | dev->mm = get_task_mm(current); |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 484 | worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid); |
| 485 | if (IS_ERR(worker)) { |
| 486 | err = PTR_ERR(worker); |
| 487 | goto err_worker; |
| 488 | } |
| 489 | |
| 490 | dev->worker = worker; |
Michael S. Tsirkin | 87d6a41 | 2010-09-02 14:05:30 +0300 | [diff] [blame] | 491 | wake_up_process(worker); /* avoid contributing to loadavg */ |
| 492 | |
| 493 | err = vhost_attach_cgroups(dev); |
Michael S. Tsirkin | 9e3d195 | 2010-07-27 22:56:50 +0300 | [diff] [blame] | 494 | if (err) |
| 495 | goto err_cgroup; |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 496 | |
Jason Wang | e0e9b40 | 2010-09-14 23:53:05 +0800 | [diff] [blame] | 497 | err = vhost_dev_alloc_iovecs(dev); |
| 498 | if (err) |
| 499 | goto err_cgroup; |
| 500 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 501 | return 0; |
Michael S. Tsirkin | 9e3d195 | 2010-07-27 22:56:50 +0300 | [diff] [blame] | 502 | err_cgroup: |
| 503 | kthread_stop(worker); |
Michael S. Tsirkin | 615cc22 | 2010-09-02 14:16:36 +0300 | [diff] [blame] | 504 | dev->worker = NULL; |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 505 | err_worker: |
| 506 | if (dev->mm) |
| 507 | mmput(dev->mm); |
| 508 | dev->mm = NULL; |
| 509 | err_mm: |
| 510 | return err; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 511 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 512 | EXPORT_SYMBOL_GPL(vhost_dev_set_owner); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 513 | |
Michael S. Tsirkin | 150b9e5 | 2013-04-28 17:12:08 +0300 | [diff] [blame] | 514 | struct vhost_memory *vhost_dev_reset_owner_prepare(void) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 515 | { |
Michael S. Tsirkin | 150b9e5 | 2013-04-28 17:12:08 +0300 | [diff] [blame] | 516 | return kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL); |
| 517 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 518 | EXPORT_SYMBOL_GPL(vhost_dev_reset_owner_prepare); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 519 | |
Michael S. Tsirkin | 150b9e5 | 2013-04-28 17:12:08 +0300 | [diff] [blame] | 520 | /* Caller should have device mutex */ |
| 521 | void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_memory *memory) |
| 522 | { |
Michael S. Tsirkin | 47283be | 2014-06-05 15:20:27 +0300 | [diff] [blame] | 523 | int i; |
| 524 | |
Michael S. Tsirkin | ea5d404 | 2011-11-27 19:05:58 +0200 | [diff] [blame] | 525 | vhost_dev_cleanup(dev, true); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 526 | |
Michael S. Tsirkin | 150b9e5 | 2013-04-28 17:12:08 +0300 | [diff] [blame] | 527 | /* Restore memory to default empty mapping. */ |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 528 | memory->nregions = 0; |
Michael S. Tsirkin | 47283be | 2014-06-05 15:20:27 +0300 | [diff] [blame] | 529 | dev->memory = memory; |
| 530 | /* We don't need VQ locks below since vhost_dev_cleanup makes sure |
| 531 | * VQs aren't running. |
| 532 | */ |
| 533 | for (i = 0; i < dev->nvqs; ++i) |
| 534 | dev->vqs[i]->memory = memory; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 535 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 536 | EXPORT_SYMBOL_GPL(vhost_dev_reset_owner); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 537 | |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 538 | void vhost_dev_stop(struct vhost_dev *dev) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 539 | { |
| 540 | int i; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 541 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 542 | for (i = 0; i < dev->nvqs; ++i) { |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 543 | if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) { |
| 544 | vhost_poll_stop(&dev->vqs[i]->poll); |
| 545 | vhost_poll_flush(&dev->vqs[i]->poll); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 546 | } |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 547 | } |
| 548 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 549 | EXPORT_SYMBOL_GPL(vhost_dev_stop); |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 550 | |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 551 | /* Caller should have device mutex if and only if locked is set */ |
| 552 | void vhost_dev_cleanup(struct vhost_dev *dev, bool locked) |
| 553 | { |
| 554 | int i; |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 555 | |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 556 | for (i = 0; i < dev->nvqs; ++i) { |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 557 | if (dev->vqs[i]->error_ctx) |
| 558 | eventfd_ctx_put(dev->vqs[i]->error_ctx); |
| 559 | if (dev->vqs[i]->error) |
| 560 | fput(dev->vqs[i]->error); |
| 561 | if (dev->vqs[i]->kick) |
| 562 | fput(dev->vqs[i]->kick); |
| 563 | if (dev->vqs[i]->call_ctx) |
| 564 | eventfd_ctx_put(dev->vqs[i]->call_ctx); |
| 565 | if (dev->vqs[i]->call) |
| 566 | fput(dev->vqs[i]->call); |
| 567 | vhost_vq_reset(dev, dev->vqs[i]); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 568 | } |
Jason Wang | e0e9b40 | 2010-09-14 23:53:05 +0800 | [diff] [blame] | 569 | vhost_dev_free_iovecs(dev); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 570 | if (dev->log_ctx) |
| 571 | eventfd_ctx_put(dev->log_ctx); |
| 572 | dev->log_ctx = NULL; |
| 573 | if (dev->log_file) |
| 574 | fput(dev->log_file); |
| 575 | dev->log_file = NULL; |
| 576 | /* No one will access memory at this point */ |
Igor Mammedov | 4de7255 | 2015-07-01 11:07:09 +0200 | [diff] [blame] | 577 | kvfree(dev->memory); |
Michael S. Tsirkin | 47283be | 2014-06-05 15:20:27 +0300 | [diff] [blame] | 578 | dev->memory = NULL; |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 579 | WARN_ON(!list_empty(&dev->work_list)); |
Eric Dumazet | 78b620c | 2010-08-31 02:05:57 +0000 | [diff] [blame] | 580 | if (dev->worker) { |
| 581 | kthread_stop(dev->worker); |
| 582 | dev->worker = NULL; |
| 583 | } |
Michael S. Tsirkin | 533a19b | 2010-10-06 15:34:38 +0200 | [diff] [blame] | 584 | if (dev->mm) |
| 585 | mmput(dev->mm); |
| 586 | dev->mm = NULL; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 587 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 588 | EXPORT_SYMBOL_GPL(vhost_dev_cleanup); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 589 | |
| 590 | static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz) |
| 591 | { |
| 592 | u64 a = addr / VHOST_PAGE_SIZE / 8; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 593 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 594 | /* Make sure 64 bit math will not overflow. */ |
| 595 | if (a > ULONG_MAX - (unsigned long)log_base || |
| 596 | a + (unsigned long)log_base > ULONG_MAX) |
Dan Carpenter | 6d97e55 | 2010-10-11 19:24:19 +0200 | [diff] [blame] | 597 | return 0; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 598 | |
| 599 | return access_ok(VERIFY_WRITE, log_base + a, |
| 600 | (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8); |
| 601 | } |
| 602 | |
| 603 | /* Caller should have vq mutex and device mutex. */ |
| 604 | static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem, |
| 605 | int log_all) |
| 606 | { |
| 607 | int i; |
Jeff Dike | 179b284 | 2010-04-07 09:59:10 -0400 | [diff] [blame] | 608 | |
Michael S. Tsirkin | f8322fb | 2010-05-27 12:28:03 +0300 | [diff] [blame] | 609 | if (!mem) |
| 610 | return 0; |
Jeff Dike | 179b284 | 2010-04-07 09:59:10 -0400 | [diff] [blame] | 611 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 612 | for (i = 0; i < mem->nregions; ++i) { |
| 613 | struct vhost_memory_region *m = mem->regions + i; |
| 614 | unsigned long a = m->userspace_addr; |
| 615 | if (m->memory_size > ULONG_MAX) |
| 616 | return 0; |
| 617 | else if (!access_ok(VERIFY_WRITE, (void __user *)a, |
| 618 | m->memory_size)) |
| 619 | return 0; |
| 620 | else if (log_all && !log_access_ok(log_base, |
| 621 | m->guest_phys_addr, |
| 622 | m->memory_size)) |
| 623 | return 0; |
| 624 | } |
| 625 | return 1; |
| 626 | } |
| 627 | |
| 628 | /* Can we switch to this memory table? */ |
| 629 | /* Caller should have device mutex but not vq mutex */ |
| 630 | static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem, |
| 631 | int log_all) |
| 632 | { |
| 633 | int i; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 634 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 635 | for (i = 0; i < d->nvqs; ++i) { |
| 636 | int ok; |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 637 | bool log; |
| 638 | |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 639 | mutex_lock(&d->vqs[i]->mutex); |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 640 | 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] | 641 | /* If ring is inactive, will check when it's enabled. */ |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 642 | if (d->vqs[i]->private_data) |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 643 | ok = vq_memory_access_ok(d->vqs[i]->log_base, mem, log); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 644 | else |
| 645 | ok = 1; |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 646 | mutex_unlock(&d->vqs[i]->mutex); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 647 | if (!ok) |
| 648 | return 0; |
| 649 | } |
| 650 | return 1; |
| 651 | } |
| 652 | |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 653 | static int vq_access_ok(struct vhost_virtqueue *vq, unsigned int num, |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 654 | struct vring_desc __user *desc, |
| 655 | struct vring_avail __user *avail, |
| 656 | struct vring_used __user *used) |
| 657 | { |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 658 | size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 659 | return access_ok(VERIFY_READ, desc, num * sizeof *desc) && |
| 660 | access_ok(VERIFY_READ, avail, |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 661 | sizeof *avail + num * sizeof *avail->ring + s) && |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 662 | access_ok(VERIFY_WRITE, used, |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 663 | sizeof *used + num * sizeof *used->ring + s); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | /* Can we log writes? */ |
| 667 | /* Caller should have device mutex but not vq mutex */ |
| 668 | int vhost_log_access_ok(struct vhost_dev *dev) |
| 669 | { |
Michael S. Tsirkin | 47283be | 2014-06-05 15:20:27 +0300 | [diff] [blame] | 670 | return memory_access_ok(dev, dev->memory, 1); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 671 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 672 | EXPORT_SYMBOL_GPL(vhost_log_access_ok); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 673 | |
| 674 | /* Verify access for write logging. */ |
| 675 | /* Caller should have vq mutex and device mutex */ |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 676 | static int vq_log_access_ok(struct vhost_virtqueue *vq, |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 677 | void __user *log_base) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 678 | { |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 679 | size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0; |
Arnd Bergmann | 28457ee | 2010-03-09 19:24:45 +0100 | [diff] [blame] | 680 | |
Michael S. Tsirkin | 47283be | 2014-06-05 15:20:27 +0300 | [diff] [blame] | 681 | return vq_memory_access_ok(log_base, vq->memory, |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 682 | vhost_has_feature(vq, VHOST_F_LOG_ALL)) && |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 683 | (!vq->log_used || log_access_ok(log_base, vq->log_addr, |
| 684 | sizeof *vq->used + |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 685 | vq->num * sizeof *vq->used->ring + s)); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | /* Can we start vq? */ |
| 689 | /* Caller should have vq mutex and device mutex */ |
| 690 | int vhost_vq_access_ok(struct vhost_virtqueue *vq) |
| 691 | { |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 692 | return vq_access_ok(vq, vq->num, vq->desc, vq->avail, vq->used) && |
| 693 | vq_log_access_ok(vq, vq->log_base); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 694 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 695 | EXPORT_SYMBOL_GPL(vhost_vq_access_ok); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 696 | |
Igor Mammedov | bcfeaca | 2015-06-16 18:33:35 +0200 | [diff] [blame] | 697 | static int vhost_memory_reg_sort_cmp(const void *p1, const void *p2) |
| 698 | { |
| 699 | const struct vhost_memory_region *r1 = p1, *r2 = p2; |
| 700 | if (r1->guest_phys_addr < r2->guest_phys_addr) |
| 701 | return 1; |
| 702 | if (r1->guest_phys_addr > r2->guest_phys_addr) |
| 703 | return -1; |
| 704 | return 0; |
| 705 | } |
| 706 | |
Igor Mammedov | 4de7255 | 2015-07-01 11:07:09 +0200 | [diff] [blame] | 707 | static void *vhost_kvzalloc(unsigned long size) |
| 708 | { |
| 709 | void *n = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT); |
| 710 | |
Igor Mammedov | 1e09947 | 2015-07-15 16:48:50 +0200 | [diff] [blame] | 711 | if (!n) |
Igor Mammedov | 4de7255 | 2015-07-01 11:07:09 +0200 | [diff] [blame] | 712 | n = vzalloc(size); |
Igor Mammedov | 4de7255 | 2015-07-01 11:07:09 +0200 | [diff] [blame] | 713 | return n; |
| 714 | } |
| 715 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 716 | static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m) |
| 717 | { |
| 718 | struct vhost_memory mem, *newmem, *oldmem; |
| 719 | unsigned long size = offsetof(struct vhost_memory, regions); |
Michael S. Tsirkin | 98f9ca0 | 2014-05-28 17:07:02 +0300 | [diff] [blame] | 720 | int i; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 721 | |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 722 | if (copy_from_user(&mem, m, size)) |
| 723 | return -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 724 | if (mem.padding) |
| 725 | return -EOPNOTSUPP; |
Igor Mammedov | c9ce42f | 2015-07-02 15:08:11 +0200 | [diff] [blame] | 726 | if (mem.nregions > max_mem_regions) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 727 | return -E2BIG; |
Igor Mammedov | 4de7255 | 2015-07-01 11:07:09 +0200 | [diff] [blame] | 728 | newmem = vhost_kvzalloc(size + mem.nregions * sizeof(*m->regions)); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 729 | if (!newmem) |
| 730 | return -ENOMEM; |
| 731 | |
| 732 | memcpy(newmem, &mem, size); |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 733 | if (copy_from_user(newmem->regions, m->regions, |
| 734 | mem.nregions * sizeof *m->regions)) { |
Igor Mammedov | bcfeaca | 2015-06-16 18:33:35 +0200 | [diff] [blame] | 735 | kvfree(newmem); |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 736 | return -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 737 | } |
Igor Mammedov | bcfeaca | 2015-06-16 18:33:35 +0200 | [diff] [blame] | 738 | sort(newmem->regions, newmem->nregions, sizeof(*newmem->regions), |
| 739 | vhost_memory_reg_sort_cmp, NULL); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 740 | |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 741 | if (!memory_access_ok(d, newmem, 0)) { |
Igor Mammedov | 4de7255 | 2015-07-01 11:07:09 +0200 | [diff] [blame] | 742 | kvfree(newmem); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 743 | return -EFAULT; |
Takuya Yoshikawa | a02c378 | 2010-05-27 19:03:56 +0900 | [diff] [blame] | 744 | } |
Michael S. Tsirkin | 47283be | 2014-06-05 15:20:27 +0300 | [diff] [blame] | 745 | oldmem = d->memory; |
| 746 | d->memory = newmem; |
Michael S. Tsirkin | 98f9ca0 | 2014-05-28 17:07:02 +0300 | [diff] [blame] | 747 | |
Michael S. Tsirkin | 47283be | 2014-06-05 15:20:27 +0300 | [diff] [blame] | 748 | /* All memory accesses are done under some VQ mutex. */ |
Michael S. Tsirkin | 98f9ca0 | 2014-05-28 17:07:02 +0300 | [diff] [blame] | 749 | for (i = 0; i < d->nvqs; ++i) { |
| 750 | mutex_lock(&d->vqs[i]->mutex); |
Michael S. Tsirkin | 47283be | 2014-06-05 15:20:27 +0300 | [diff] [blame] | 751 | d->vqs[i]->memory = newmem; |
Michael S. Tsirkin | 98f9ca0 | 2014-05-28 17:07:02 +0300 | [diff] [blame] | 752 | mutex_unlock(&d->vqs[i]->mutex); |
| 753 | } |
Igor Mammedov | 4de7255 | 2015-07-01 11:07:09 +0200 | [diff] [blame] | 754 | kvfree(oldmem); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 755 | return 0; |
| 756 | } |
| 757 | |
Michael S. Tsirkin | 935cdee | 2012-12-06 14:03:34 +0200 | [diff] [blame] | 758 | long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 759 | { |
Al Viro | cecb46f | 2012-08-27 14:21:39 -0400 | [diff] [blame] | 760 | struct file *eventfp, *filep = NULL; |
| 761 | bool pollstart = false, pollstop = false; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 762 | struct eventfd_ctx *ctx = NULL; |
| 763 | u32 __user *idxp = argp; |
| 764 | struct vhost_virtqueue *vq; |
| 765 | struct vhost_vring_state s; |
| 766 | struct vhost_vring_file f; |
| 767 | struct vhost_vring_addr a; |
| 768 | u32 idx; |
| 769 | long r; |
| 770 | |
| 771 | r = get_user(idx, idxp); |
| 772 | if (r < 0) |
| 773 | return r; |
Krishna Kumar | 0f3d9a1 | 2010-05-25 11:10:36 +0530 | [diff] [blame] | 774 | if (idx >= d->nvqs) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 775 | return -ENOBUFS; |
| 776 | |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 777 | vq = d->vqs[idx]; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 778 | |
| 779 | mutex_lock(&vq->mutex); |
| 780 | |
| 781 | switch (ioctl) { |
| 782 | case VHOST_SET_VRING_NUM: |
| 783 | /* Resizing ring with an active backend? |
| 784 | * You don't want to do that. */ |
| 785 | if (vq->private_data) { |
| 786 | r = -EBUSY; |
| 787 | break; |
| 788 | } |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 789 | if (copy_from_user(&s, argp, sizeof s)) { |
| 790 | r = -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 791 | break; |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 792 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 793 | if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) { |
| 794 | r = -EINVAL; |
| 795 | break; |
| 796 | } |
| 797 | vq->num = s.num; |
| 798 | break; |
| 799 | case VHOST_SET_VRING_BASE: |
| 800 | /* Moving base with an active backend? |
| 801 | * You don't want to do that. */ |
| 802 | if (vq->private_data) { |
| 803 | r = -EBUSY; |
| 804 | break; |
| 805 | } |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 806 | if (copy_from_user(&s, argp, sizeof s)) { |
| 807 | r = -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 808 | break; |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 809 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 810 | if (s.num > 0xffff) { |
| 811 | r = -EINVAL; |
| 812 | break; |
| 813 | } |
| 814 | vq->last_avail_idx = s.num; |
| 815 | /* Forget the cached index value. */ |
| 816 | vq->avail_idx = vq->last_avail_idx; |
| 817 | break; |
| 818 | case VHOST_GET_VRING_BASE: |
| 819 | s.index = idx; |
| 820 | s.num = vq->last_avail_idx; |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 821 | if (copy_to_user(argp, &s, sizeof s)) |
| 822 | r = -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 823 | break; |
| 824 | case VHOST_SET_VRING_ADDR: |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 825 | if (copy_from_user(&a, argp, sizeof a)) { |
| 826 | r = -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 827 | break; |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 828 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 829 | if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) { |
| 830 | r = -EOPNOTSUPP; |
| 831 | break; |
| 832 | } |
| 833 | /* For 32bit, verify that the top 32bits of the user |
| 834 | data are set to zero. */ |
| 835 | if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr || |
| 836 | (u64)(unsigned long)a.used_user_addr != a.used_user_addr || |
| 837 | (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) { |
| 838 | r = -EFAULT; |
| 839 | break; |
| 840 | } |
Michael S. Tsirkin | 5d9a07b | 2014-12-21 01:00:23 +0200 | [diff] [blame] | 841 | |
| 842 | /* Make sure it's safe to cast pointers to vring types. */ |
| 843 | BUILD_BUG_ON(__alignof__ *vq->avail > VRING_AVAIL_ALIGN_SIZE); |
| 844 | BUILD_BUG_ON(__alignof__ *vq->used > VRING_USED_ALIGN_SIZE); |
| 845 | if ((a.avail_user_addr & (VRING_AVAIL_ALIGN_SIZE - 1)) || |
| 846 | (a.used_user_addr & (VRING_USED_ALIGN_SIZE - 1)) || |
Michael S. Tsirkin | d542483 | 2015-11-16 16:57:08 +0200 | [diff] [blame] | 847 | (a.log_guest_addr & (VRING_USED_ALIGN_SIZE - 1))) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 848 | r = -EINVAL; |
| 849 | break; |
| 850 | } |
| 851 | |
| 852 | /* We only verify access here if backend is configured. |
| 853 | * If it is not, we don't as size might not have been setup. |
| 854 | * We will verify when backend is configured. */ |
| 855 | if (vq->private_data) { |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 856 | if (!vq_access_ok(vq, vq->num, |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 857 | (void __user *)(unsigned long)a.desc_user_addr, |
| 858 | (void __user *)(unsigned long)a.avail_user_addr, |
| 859 | (void __user *)(unsigned long)a.used_user_addr)) { |
| 860 | r = -EINVAL; |
| 861 | break; |
| 862 | } |
| 863 | |
| 864 | /* Also validate log access for used ring if enabled. */ |
| 865 | if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) && |
| 866 | !log_access_ok(vq->log_base, a.log_guest_addr, |
| 867 | sizeof *vq->used + |
| 868 | vq->num * sizeof *vq->used->ring)) { |
| 869 | r = -EINVAL; |
| 870 | break; |
| 871 | } |
| 872 | } |
| 873 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 874 | vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG)); |
| 875 | vq->desc = (void __user *)(unsigned long)a.desc_user_addr; |
| 876 | vq->avail = (void __user *)(unsigned long)a.avail_user_addr; |
| 877 | vq->log_addr = a.log_guest_addr; |
| 878 | vq->used = (void __user *)(unsigned long)a.used_user_addr; |
| 879 | break; |
| 880 | case VHOST_SET_VRING_KICK: |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 881 | if (copy_from_user(&f, argp, sizeof f)) { |
| 882 | r = -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 883 | break; |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 884 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 885 | eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd); |
Michael S. Tsirkin | 535297a | 2010-03-17 16:06:11 +0200 | [diff] [blame] | 886 | if (IS_ERR(eventfp)) { |
| 887 | r = PTR_ERR(eventfp); |
| 888 | break; |
| 889 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 890 | if (eventfp != vq->kick) { |
Al Viro | cecb46f | 2012-08-27 14:21:39 -0400 | [diff] [blame] | 891 | pollstop = (filep = vq->kick) != NULL; |
| 892 | pollstart = (vq->kick = eventfp) != NULL; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 893 | } else |
| 894 | filep = eventfp; |
| 895 | break; |
| 896 | case VHOST_SET_VRING_CALL: |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 897 | if (copy_from_user(&f, argp, sizeof f)) { |
| 898 | r = -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 899 | break; |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 900 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 901 | eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd); |
Michael S. Tsirkin | 535297a | 2010-03-17 16:06:11 +0200 | [diff] [blame] | 902 | if (IS_ERR(eventfp)) { |
| 903 | r = PTR_ERR(eventfp); |
| 904 | break; |
| 905 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 906 | if (eventfp != vq->call) { |
| 907 | filep = vq->call; |
| 908 | ctx = vq->call_ctx; |
| 909 | vq->call = eventfp; |
| 910 | vq->call_ctx = eventfp ? |
| 911 | eventfd_ctx_fileget(eventfp) : NULL; |
| 912 | } else |
| 913 | filep = eventfp; |
| 914 | break; |
| 915 | case VHOST_SET_VRING_ERR: |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 916 | if (copy_from_user(&f, argp, sizeof f)) { |
| 917 | r = -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 918 | break; |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 919 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 920 | eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd); |
Michael S. Tsirkin | 535297a | 2010-03-17 16:06:11 +0200 | [diff] [blame] | 921 | if (IS_ERR(eventfp)) { |
| 922 | r = PTR_ERR(eventfp); |
| 923 | break; |
| 924 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 925 | if (eventfp != vq->error) { |
| 926 | filep = vq->error; |
| 927 | vq->error = eventfp; |
| 928 | ctx = vq->error_ctx; |
| 929 | vq->error_ctx = eventfp ? |
| 930 | eventfd_ctx_fileget(eventfp) : NULL; |
| 931 | } else |
| 932 | filep = eventfp; |
| 933 | break; |
Greg Kurz | 2751c98 | 2015-04-24 14:27:24 +0200 | [diff] [blame] | 934 | case VHOST_SET_VRING_ENDIAN: |
| 935 | r = vhost_set_vring_endian(vq, argp); |
| 936 | break; |
| 937 | case VHOST_GET_VRING_ENDIAN: |
| 938 | r = vhost_get_vring_endian(vq, idx, argp); |
| 939 | break; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 940 | default: |
| 941 | r = -ENOIOCTLCMD; |
| 942 | } |
| 943 | |
| 944 | if (pollstop && vq->handle_kick) |
| 945 | vhost_poll_stop(&vq->poll); |
| 946 | |
| 947 | if (ctx) |
| 948 | eventfd_ctx_put(ctx); |
| 949 | if (filep) |
| 950 | fput(filep); |
| 951 | |
| 952 | if (pollstart && vq->handle_kick) |
Jason Wang | 2b8b328 | 2013-01-28 01:05:18 +0000 | [diff] [blame] | 953 | r = vhost_poll_start(&vq->poll, vq->kick); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 954 | |
| 955 | mutex_unlock(&vq->mutex); |
| 956 | |
| 957 | if (pollstop && vq->handle_kick) |
| 958 | vhost_poll_flush(&vq->poll); |
| 959 | return r; |
| 960 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 961 | EXPORT_SYMBOL_GPL(vhost_vring_ioctl); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 962 | |
| 963 | /* Caller must have device mutex */ |
Michael S. Tsirkin | 935cdee | 2012-12-06 14:03:34 +0200 | [diff] [blame] | 964 | 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] | 965 | { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 966 | struct file *eventfp, *filep = NULL; |
| 967 | struct eventfd_ctx *ctx = NULL; |
| 968 | u64 p; |
| 969 | long r; |
| 970 | int i, fd; |
| 971 | |
| 972 | /* If you are not the owner, you can become one */ |
| 973 | if (ioctl == VHOST_SET_OWNER) { |
| 974 | r = vhost_dev_set_owner(d); |
| 975 | goto done; |
| 976 | } |
| 977 | |
| 978 | /* You must be the owner to do anything else */ |
| 979 | r = vhost_dev_check_owner(d); |
| 980 | if (r) |
| 981 | goto done; |
| 982 | |
| 983 | switch (ioctl) { |
| 984 | case VHOST_SET_MEM_TABLE: |
| 985 | r = vhost_set_memory(d, argp); |
| 986 | break; |
| 987 | case VHOST_SET_LOG_BASE: |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 988 | if (copy_from_user(&p, argp, sizeof p)) { |
| 989 | r = -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 990 | break; |
Takuya Yoshikawa | 7ad9c9d | 2010-05-27 18:58:03 +0900 | [diff] [blame] | 991 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 992 | if ((u64)(unsigned long)p != p) { |
| 993 | r = -EFAULT; |
| 994 | break; |
| 995 | } |
| 996 | for (i = 0; i < d->nvqs; ++i) { |
| 997 | struct vhost_virtqueue *vq; |
| 998 | void __user *base = (void __user *)(unsigned long)p; |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 999 | vq = d->vqs[i]; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1000 | mutex_lock(&vq->mutex); |
| 1001 | /* If ring is inactive, will check when it's enabled. */ |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 1002 | if (vq->private_data && !vq_log_access_ok(vq, base)) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1003 | r = -EFAULT; |
| 1004 | else |
| 1005 | vq->log_base = base; |
| 1006 | mutex_unlock(&vq->mutex); |
| 1007 | } |
| 1008 | break; |
| 1009 | case VHOST_SET_LOG_FD: |
| 1010 | r = get_user(fd, (int __user *)argp); |
| 1011 | if (r < 0) |
| 1012 | break; |
| 1013 | eventfp = fd == -1 ? NULL : eventfd_fget(fd); |
| 1014 | if (IS_ERR(eventfp)) { |
| 1015 | r = PTR_ERR(eventfp); |
| 1016 | break; |
| 1017 | } |
| 1018 | if (eventfp != d->log_file) { |
| 1019 | filep = d->log_file; |
Marc-André Lureau | 7932c0b | 2015-07-17 15:32:03 +0200 | [diff] [blame] | 1020 | d->log_file = eventfp; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1021 | ctx = d->log_ctx; |
| 1022 | d->log_ctx = eventfp ? |
| 1023 | eventfd_ctx_fileget(eventfp) : NULL; |
| 1024 | } else |
| 1025 | filep = eventfp; |
| 1026 | for (i = 0; i < d->nvqs; ++i) { |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 1027 | mutex_lock(&d->vqs[i]->mutex); |
| 1028 | d->vqs[i]->log_ctx = d->log_ctx; |
| 1029 | mutex_unlock(&d->vqs[i]->mutex); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1030 | } |
| 1031 | if (ctx) |
| 1032 | eventfd_ctx_put(ctx); |
| 1033 | if (filep) |
| 1034 | fput(filep); |
| 1035 | break; |
| 1036 | default: |
Michael S. Tsirkin | 935cdee | 2012-12-06 14:03:34 +0200 | [diff] [blame] | 1037 | r = -ENOIOCTLCMD; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1038 | break; |
| 1039 | } |
| 1040 | done: |
| 1041 | return r; |
| 1042 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 1043 | EXPORT_SYMBOL_GPL(vhost_dev_ioctl); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1044 | |
| 1045 | static const struct vhost_memory_region *find_region(struct vhost_memory *mem, |
| 1046 | __u64 addr, __u32 len) |
| 1047 | { |
Igor Mammedov | bcfeaca | 2015-06-16 18:33:35 +0200 | [diff] [blame] | 1048 | const struct vhost_memory_region *reg; |
| 1049 | int start = 0, end = mem->nregions; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 1050 | |
Igor Mammedov | bcfeaca | 2015-06-16 18:33:35 +0200 | [diff] [blame] | 1051 | while (start < end) { |
| 1052 | int slot = start + (end - start) / 2; |
| 1053 | reg = mem->regions + slot; |
| 1054 | if (addr >= reg->guest_phys_addr) |
| 1055 | end = slot; |
| 1056 | else |
| 1057 | start = slot + 1; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1058 | } |
Igor Mammedov | bcfeaca | 2015-06-16 18:33:35 +0200 | [diff] [blame] | 1059 | |
| 1060 | reg = mem->regions + start; |
| 1061 | if (addr >= reg->guest_phys_addr && |
| 1062 | reg->guest_phys_addr + reg->memory_size > addr) |
| 1063 | return reg; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1064 | return NULL; |
| 1065 | } |
| 1066 | |
| 1067 | /* TODO: This is really inefficient. We need something like get_user() |
| 1068 | * (instruction directly accesses the data, with an exception table entry |
| 1069 | * returning -EFAULT). See Documentation/x86/exception-tables.txt. |
| 1070 | */ |
| 1071 | static int set_bit_to_user(int nr, void __user *addr) |
| 1072 | { |
| 1073 | unsigned long log = (unsigned long)addr; |
| 1074 | struct page *page; |
| 1075 | void *base; |
| 1076 | int bit = nr + (log % PAGE_SIZE) * 8; |
| 1077 | int r; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 1078 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1079 | r = get_user_pages_fast(log, 1, 1, &page); |
Michael S. Tsirkin | d6db3f5 | 2010-02-23 11:25:23 +0200 | [diff] [blame] | 1080 | if (r < 0) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1081 | return r; |
Michael S. Tsirkin | d6db3f5 | 2010-02-23 11:25:23 +0200 | [diff] [blame] | 1082 | BUG_ON(r != 1); |
Cong Wang | c6daa7f | 2011-11-25 23:14:26 +0800 | [diff] [blame] | 1083 | base = kmap_atomic(page); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1084 | set_bit(bit, base); |
Cong Wang | c6daa7f | 2011-11-25 23:14:26 +0800 | [diff] [blame] | 1085 | kunmap_atomic(base); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1086 | set_page_dirty_lock(page); |
| 1087 | put_page(page); |
| 1088 | return 0; |
| 1089 | } |
| 1090 | |
| 1091 | static int log_write(void __user *log_base, |
| 1092 | u64 write_address, u64 write_length) |
| 1093 | { |
Michael S. Tsirkin | 28831ee | 2010-11-29 10:22:10 +0200 | [diff] [blame] | 1094 | u64 write_page = write_address / VHOST_PAGE_SIZE; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1095 | int r; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 1096 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1097 | if (!write_length) |
| 1098 | return 0; |
Michael S. Tsirkin | 3bf9be4 | 2010-11-29 10:19:07 +0200 | [diff] [blame] | 1099 | write_length += write_address % VHOST_PAGE_SIZE; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1100 | for (;;) { |
| 1101 | u64 base = (u64)(unsigned long)log_base; |
Michael S. Tsirkin | 28831ee | 2010-11-29 10:22:10 +0200 | [diff] [blame] | 1102 | u64 log = base + write_page / 8; |
| 1103 | int bit = write_page % 8; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1104 | if ((u64)(unsigned long)log != log) |
| 1105 | return -EFAULT; |
| 1106 | r = set_bit_to_user(bit, (void __user *)(unsigned long)log); |
| 1107 | if (r < 0) |
| 1108 | return r; |
| 1109 | if (write_length <= VHOST_PAGE_SIZE) |
| 1110 | break; |
| 1111 | write_length -= VHOST_PAGE_SIZE; |
Michael S. Tsirkin | 28831ee | 2010-11-29 10:22:10 +0200 | [diff] [blame] | 1112 | write_page += 1; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1113 | } |
| 1114 | return r; |
| 1115 | } |
| 1116 | |
| 1117 | int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log, |
| 1118 | unsigned int log_num, u64 len) |
| 1119 | { |
| 1120 | int i, r; |
| 1121 | |
| 1122 | /* Make sure data written is seen before log. */ |
Michael S. Tsirkin | 5659338 | 2010-02-01 07:21:02 +0000 | [diff] [blame] | 1123 | smp_wmb(); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1124 | for (i = 0; i < log_num; ++i) { |
| 1125 | u64 l = min(log[i].len, len); |
| 1126 | r = log_write(vq->log_base, log[i].addr, l); |
| 1127 | if (r < 0) |
| 1128 | return r; |
| 1129 | len -= l; |
Michael S. Tsirkin | 5786aee | 2010-09-22 12:31:53 +0200 | [diff] [blame] | 1130 | if (!len) { |
| 1131 | if (vq->log_ctx) |
| 1132 | eventfd_signal(vq->log_ctx, 1); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1133 | return 0; |
Michael S. Tsirkin | 5786aee | 2010-09-22 12:31:53 +0200 | [diff] [blame] | 1134 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1135 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1136 | /* Length written exceeds what we have stored. This is a bug. */ |
| 1137 | BUG(); |
| 1138 | return 0; |
| 1139 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 1140 | EXPORT_SYMBOL_GPL(vhost_log_write); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1141 | |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 1142 | static int vhost_update_used_flags(struct vhost_virtqueue *vq) |
| 1143 | { |
| 1144 | void __user *used; |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1145 | if (__put_user(cpu_to_vhost16(vq, vq->used_flags), &vq->used->flags) < 0) |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 1146 | return -EFAULT; |
| 1147 | if (unlikely(vq->log_used)) { |
| 1148 | /* Make sure the flag is seen before log. */ |
| 1149 | smp_wmb(); |
| 1150 | /* Log used flag write. */ |
| 1151 | used = &vq->used->flags; |
| 1152 | log_write(vq->log_base, vq->log_addr + |
| 1153 | (used - (void __user *)vq->used), |
| 1154 | sizeof vq->used->flags); |
| 1155 | if (vq->log_ctx) |
| 1156 | eventfd_signal(vq->log_ctx, 1); |
| 1157 | } |
| 1158 | return 0; |
| 1159 | } |
| 1160 | |
| 1161 | static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event) |
| 1162 | { |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1163 | if (__put_user(cpu_to_vhost16(vq, vq->avail_idx), vhost_avail_event(vq))) |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 1164 | return -EFAULT; |
| 1165 | if (unlikely(vq->log_used)) { |
| 1166 | void __user *used; |
| 1167 | /* Make sure the event is seen before log. */ |
| 1168 | smp_wmb(); |
| 1169 | /* Log avail event write */ |
| 1170 | used = vhost_avail_event(vq); |
| 1171 | log_write(vq->log_base, vq->log_addr + |
| 1172 | (used - (void __user *)vq->used), |
| 1173 | sizeof *vhost_avail_event(vq)); |
| 1174 | if (vq->log_ctx) |
| 1175 | eventfd_signal(vq->log_ctx, 1); |
| 1176 | } |
| 1177 | return 0; |
| 1178 | } |
| 1179 | |
Greg Kurz | 80f7d03 | 2016-02-16 15:59:44 +0100 | [diff] [blame] | 1180 | int vhost_vq_init_access(struct vhost_virtqueue *vq) |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 1181 | { |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1182 | __virtio16 last_used_idx; |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 1183 | int r; |
Greg Kurz | e1f33be | 2016-02-16 15:54:28 +0100 | [diff] [blame] | 1184 | bool is_le = vq->is_le; |
| 1185 | |
Greg Kurz | 2751c98 | 2015-04-24 14:27:24 +0200 | [diff] [blame] | 1186 | if (!vq->private_data) { |
Greg Kurz | c507203 | 2016-02-16 15:59:34 +0100 | [diff] [blame] | 1187 | vhost_reset_is_le(vq); |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 1188 | return 0; |
Greg Kurz | 2751c98 | 2015-04-24 14:27:24 +0200 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | vhost_init_is_le(vq); |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 1192 | |
| 1193 | r = vhost_update_used_flags(vq); |
| 1194 | if (r) |
Greg Kurz | e1f33be | 2016-02-16 15:54:28 +0100 | [diff] [blame] | 1195 | goto err; |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 1196 | vq->signalled_used_valid = false; |
Greg Kurz | e1f33be | 2016-02-16 15:54:28 +0100 | [diff] [blame] | 1197 | if (!access_ok(VERIFY_READ, &vq->used->idx, sizeof vq->used->idx)) { |
| 1198 | r = -EFAULT; |
| 1199 | goto err; |
| 1200 | } |
Michael S. Tsirkin | 64f7f05 | 2014-12-01 17:39:39 +0200 | [diff] [blame] | 1201 | r = __get_user(last_used_idx, &vq->used->idx); |
| 1202 | if (r) |
Greg Kurz | e1f33be | 2016-02-16 15:54:28 +0100 | [diff] [blame] | 1203 | goto err; |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1204 | vq->last_used_idx = vhost16_to_cpu(vq, last_used_idx); |
Michael S. Tsirkin | 64f7f05 | 2014-12-01 17:39:39 +0200 | [diff] [blame] | 1205 | return 0; |
Greg Kurz | e1f33be | 2016-02-16 15:54:28 +0100 | [diff] [blame] | 1206 | err: |
| 1207 | vq->is_le = is_le; |
| 1208 | return r; |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 1209 | } |
Greg Kurz | 80f7d03 | 2016-02-16 15:59:44 +0100 | [diff] [blame] | 1210 | EXPORT_SYMBOL_GPL(vhost_vq_init_access); |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 1211 | |
Michael S. Tsirkin | 47283be | 2014-06-05 15:20:27 +0300 | [diff] [blame] | 1212 | static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len, |
Christoph Hellwig | a8d3782 | 2010-04-13 14:11:25 -0400 | [diff] [blame] | 1213 | struct iovec iov[], int iov_size) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1214 | { |
| 1215 | const struct vhost_memory_region *reg; |
| 1216 | struct vhost_memory *mem; |
| 1217 | struct iovec *_iov; |
| 1218 | u64 s = 0; |
| 1219 | int ret = 0; |
| 1220 | |
Michael S. Tsirkin | 47283be | 2014-06-05 15:20:27 +0300 | [diff] [blame] | 1221 | mem = vq->memory; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1222 | while ((u64)len > s) { |
| 1223 | u64 size; |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 1224 | if (unlikely(ret >= iov_size)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1225 | ret = -ENOBUFS; |
| 1226 | break; |
| 1227 | } |
| 1228 | reg = find_region(mem, addr, len); |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 1229 | if (unlikely(!reg)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1230 | ret = -EFAULT; |
| 1231 | break; |
| 1232 | } |
| 1233 | _iov = iov + ret; |
| 1234 | size = reg->memory_size - addr + reg->guest_phys_addr; |
Michael S. Tsirkin | bd97120 | 2012-11-26 05:57:27 +0000 | [diff] [blame] | 1235 | _iov->iov_len = min((u64)len - s, size); |
Christoph Hellwig | a8d3782 | 2010-04-13 14:11:25 -0400 | [diff] [blame] | 1236 | _iov->iov_base = (void __user *)(unsigned long) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1237 | (reg->userspace_addr + addr - reg->guest_phys_addr); |
| 1238 | s += size; |
| 1239 | addr += size; |
| 1240 | ++ret; |
| 1241 | } |
| 1242 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1243 | return ret; |
| 1244 | } |
| 1245 | |
| 1246 | /* Each buffer in the virtqueues is actually a chain of descriptors. This |
| 1247 | * function returns the next descriptor in the chain, |
| 1248 | * or -1U if we're at the end. */ |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1249 | 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] | 1250 | { |
| 1251 | unsigned int next; |
| 1252 | |
| 1253 | /* If this descriptor says it doesn't chain, we're done. */ |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1254 | if (!(desc->flags & cpu_to_vhost16(vq, VRING_DESC_F_NEXT))) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1255 | return -1U; |
| 1256 | |
| 1257 | /* Check they're not leading us off end of descriptors. */ |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1258 | next = vhost16_to_cpu(vq, desc->next); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1259 | /* Make sure compiler knows to grab that: we don't want it changing! */ |
| 1260 | /* We will use the result as an index in an array, so most |
| 1261 | * architectures only need a compiler barrier here. */ |
| 1262 | read_barrier_depends(); |
| 1263 | |
| 1264 | return next; |
| 1265 | } |
| 1266 | |
Michael S. Tsirkin | 47283be | 2014-06-05 15:20:27 +0300 | [diff] [blame] | 1267 | static int get_indirect(struct vhost_virtqueue *vq, |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 1268 | struct iovec iov[], unsigned int iov_size, |
| 1269 | unsigned int *out_num, unsigned int *in_num, |
| 1270 | struct vhost_log *log, unsigned int *log_num, |
| 1271 | struct vring_desc *indirect) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1272 | { |
| 1273 | struct vring_desc desc; |
| 1274 | unsigned int i = 0, count, found = 0; |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1275 | u32 len = vhost32_to_cpu(vq, indirect->len); |
Al Viro | aad9a1c | 2014-12-10 14:49:01 -0500 | [diff] [blame] | 1276 | struct iov_iter from; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1277 | int ret; |
| 1278 | |
| 1279 | /* Sanity check */ |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1280 | if (unlikely(len % sizeof desc)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1281 | vq_err(vq, "Invalid length in indirect descriptor: " |
| 1282 | "len 0x%llx not multiple of 0x%zx\n", |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1283 | (unsigned long long)len, |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1284 | sizeof desc); |
| 1285 | return -EINVAL; |
| 1286 | } |
| 1287 | |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1288 | ret = translate_desc(vq, vhost64_to_cpu(vq, indirect->addr), len, vq->indirect, |
Jason Wang | e0e9b40 | 2010-09-14 23:53:05 +0800 | [diff] [blame] | 1289 | UIO_MAXIOV); |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 1290 | if (unlikely(ret < 0)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1291 | vq_err(vq, "Translation failure %d in indirect.\n", ret); |
| 1292 | return ret; |
| 1293 | } |
Al Viro | aad9a1c | 2014-12-10 14:49:01 -0500 | [diff] [blame] | 1294 | iov_iter_init(&from, READ, vq->indirect, ret, len); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1295 | |
| 1296 | /* We will use the result as an address to read from, so most |
| 1297 | * architectures only need a compiler barrier here. */ |
| 1298 | read_barrier_depends(); |
| 1299 | |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1300 | count = len / sizeof desc; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1301 | /* Buffers are chained via a 16 bit next field, so |
| 1302 | * we can have at most 2^16 of these. */ |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 1303 | if (unlikely(count > USHRT_MAX + 1)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1304 | vq_err(vq, "Indirect buffer length too big: %d\n", |
| 1305 | indirect->len); |
| 1306 | return -E2BIG; |
| 1307 | } |
| 1308 | |
| 1309 | do { |
| 1310 | unsigned iov_count = *in_num + *out_num; |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 1311 | if (unlikely(++found > count)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1312 | vq_err(vq, "Loop detected: last one at %u " |
| 1313 | "indirect size %u\n", |
| 1314 | i, count); |
| 1315 | return -EINVAL; |
| 1316 | } |
Al Viro | aad9a1c | 2014-12-10 14:49:01 -0500 | [diff] [blame] | 1317 | if (unlikely(copy_from_iter(&desc, sizeof(desc), &from) != |
| 1318 | sizeof(desc))) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1319 | vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n", |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1320 | 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] | 1321 | return -EINVAL; |
| 1322 | } |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1323 | 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] | 1324 | vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n", |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1325 | 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] | 1326 | return -EINVAL; |
| 1327 | } |
| 1328 | |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1329 | ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr), |
| 1330 | vhost32_to_cpu(vq, desc.len), iov + iov_count, |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1331 | iov_size - iov_count); |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 1332 | if (unlikely(ret < 0)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1333 | vq_err(vq, "Translation failure %d indirect idx %d\n", |
| 1334 | ret, i); |
| 1335 | return ret; |
| 1336 | } |
| 1337 | /* If this is an input descriptor, increment that count. */ |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1338 | if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1339 | *in_num += ret; |
| 1340 | if (unlikely(log)) { |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1341 | log[*log_num].addr = vhost64_to_cpu(vq, desc.addr); |
| 1342 | log[*log_num].len = vhost32_to_cpu(vq, desc.len); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1343 | ++*log_num; |
| 1344 | } |
| 1345 | } else { |
| 1346 | /* If it's an output descriptor, they're all supposed |
| 1347 | * to come before any input descriptors. */ |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 1348 | if (unlikely(*in_num)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1349 | vq_err(vq, "Indirect descriptor " |
| 1350 | "has out after in: idx %d\n", i); |
| 1351 | return -EINVAL; |
| 1352 | } |
| 1353 | *out_num += ret; |
| 1354 | } |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1355 | } while ((i = next_desc(vq, &desc)) != -1); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1356 | return 0; |
| 1357 | } |
| 1358 | |
| 1359 | /* This looks in the virtqueue and for the first available buffer, and converts |
| 1360 | * it to an iovec for convenient access. Since descriptors consist of some |
| 1361 | * number of output then some number of input descriptors, it's actually two |
| 1362 | * iovecs, but we pack them into one and note how many of each there were. |
| 1363 | * |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 1364 | * This function returns the descriptor number found, or vq->num (which is |
| 1365 | * never a valid descriptor number) if none was found. A negative code is |
| 1366 | * returned on error. */ |
Michael S. Tsirkin | 47283be | 2014-06-05 15:20:27 +0300 | [diff] [blame] | 1367 | int vhost_get_vq_desc(struct vhost_virtqueue *vq, |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 1368 | struct iovec iov[], unsigned int iov_size, |
| 1369 | unsigned int *out_num, unsigned int *in_num, |
| 1370 | struct vhost_log *log, unsigned int *log_num) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1371 | { |
| 1372 | struct vring_desc desc; |
| 1373 | unsigned int i, head, found = 0; |
| 1374 | u16 last_avail_idx; |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1375 | __virtio16 avail_idx; |
| 1376 | __virtio16 ring_head; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1377 | int ret; |
| 1378 | |
| 1379 | /* Check it isn't doing very strange things with descriptor numbers. */ |
| 1380 | last_avail_idx = vq->last_avail_idx; |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1381 | if (unlikely(__get_user(avail_idx, &vq->avail->idx))) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1382 | vq_err(vq, "Failed to access avail idx at %p\n", |
| 1383 | &vq->avail->idx); |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 1384 | return -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1385 | } |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1386 | vq->avail_idx = vhost16_to_cpu(vq, avail_idx); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1387 | |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 1388 | if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1389 | vq_err(vq, "Guest moved used index from %u to %u", |
| 1390 | last_avail_idx, vq->avail_idx); |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 1391 | return -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | /* If there's nothing new since last we looked, return invalid. */ |
| 1395 | if (vq->avail_idx == last_avail_idx) |
| 1396 | return vq->num; |
| 1397 | |
| 1398 | /* Only get avail ring entries after they have been exposed by guest. */ |
Michael S. Tsirkin | 5659338 | 2010-02-01 07:21:02 +0000 | [diff] [blame] | 1399 | smp_rmb(); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1400 | |
| 1401 | /* Grab the next descriptor number they're advertising, and increment |
| 1402 | * the index we've seen. */ |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1403 | if (unlikely(__get_user(ring_head, |
Michael S. Tsirkin | 5fba13b | 2015-11-29 13:34:44 +0200 | [diff] [blame] | 1404 | &vq->avail->ring[last_avail_idx & (vq->num - 1)]))) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1405 | vq_err(vq, "Failed to read head: idx %d address %p\n", |
| 1406 | last_avail_idx, |
| 1407 | &vq->avail->ring[last_avail_idx % vq->num]); |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 1408 | return -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1411 | head = vhost16_to_cpu(vq, ring_head); |
| 1412 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1413 | /* If their number is silly, that's an error. */ |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 1414 | if (unlikely(head >= vq->num)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1415 | vq_err(vq, "Guest says index %u > %u is available", |
| 1416 | head, vq->num); |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 1417 | return -EINVAL; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | /* When we start there are none of either input nor output. */ |
| 1421 | *out_num = *in_num = 0; |
| 1422 | if (unlikely(log)) |
| 1423 | *log_num = 0; |
| 1424 | |
| 1425 | i = head; |
| 1426 | do { |
| 1427 | unsigned iov_count = *in_num + *out_num; |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 1428 | if (unlikely(i >= vq->num)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1429 | vq_err(vq, "Desc index is %u > %u, head = %u", |
| 1430 | i, vq->num, head); |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 1431 | return -EINVAL; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1432 | } |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 1433 | if (unlikely(++found > vq->num)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1434 | vq_err(vq, "Loop detected: last one at %u " |
| 1435 | "vq size %u head %u\n", |
| 1436 | i, vq->num, head); |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 1437 | return -EINVAL; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1438 | } |
Michael S. Tsirkin | fcc042a | 2011-03-06 13:33:49 +0200 | [diff] [blame] | 1439 | ret = __copy_from_user(&desc, vq->desc + i, sizeof desc); |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 1440 | if (unlikely(ret)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1441 | vq_err(vq, "Failed to get descriptor: idx %d addr %p\n", |
| 1442 | i, vq->desc + i); |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 1443 | return -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1444 | } |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1445 | if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT)) { |
Michael S. Tsirkin | 47283be | 2014-06-05 15:20:27 +0300 | [diff] [blame] | 1446 | ret = get_indirect(vq, iov, iov_size, |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1447 | out_num, in_num, |
| 1448 | log, log_num, &desc); |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 1449 | if (unlikely(ret < 0)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1450 | vq_err(vq, "Failure detected " |
| 1451 | "in indirect descriptor at idx %d\n", i); |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 1452 | return ret; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1453 | } |
| 1454 | continue; |
| 1455 | } |
| 1456 | |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1457 | ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr), |
| 1458 | vhost32_to_cpu(vq, desc.len), iov + iov_count, |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1459 | iov_size - iov_count); |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 1460 | if (unlikely(ret < 0)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1461 | vq_err(vq, "Translation failure %d descriptor idx %d\n", |
| 1462 | ret, i); |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 1463 | return ret; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1464 | } |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1465 | if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1466 | /* If this is an input descriptor, |
| 1467 | * increment that count. */ |
| 1468 | *in_num += ret; |
| 1469 | if (unlikely(log)) { |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1470 | log[*log_num].addr = vhost64_to_cpu(vq, desc.addr); |
| 1471 | log[*log_num].len = vhost32_to_cpu(vq, desc.len); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1472 | ++*log_num; |
| 1473 | } |
| 1474 | } else { |
| 1475 | /* If it's an output descriptor, they're all supposed |
| 1476 | * to come before any input descriptors. */ |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 1477 | if (unlikely(*in_num)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1478 | vq_err(vq, "Descriptor has out after in: " |
| 1479 | "idx %d\n", i); |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 1480 | return -EINVAL; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1481 | } |
| 1482 | *out_num += ret; |
| 1483 | } |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1484 | } while ((i = next_desc(vq, &desc)) != -1); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1485 | |
| 1486 | /* On success, increment avail index. */ |
| 1487 | vq->last_avail_idx++; |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 1488 | |
| 1489 | /* Assume notifications from guest are disabled at this point, |
| 1490 | * if they aren't we would need to update avail_event index. */ |
| 1491 | BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY)); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1492 | return head; |
| 1493 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 1494 | EXPORT_SYMBOL_GPL(vhost_get_vq_desc); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1495 | |
| 1496 | /* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */ |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 1497 | void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1498 | { |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 1499 | vq->last_avail_idx -= n; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1500 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 1501 | EXPORT_SYMBOL_GPL(vhost_discard_vq_desc); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1502 | |
| 1503 | /* After we've used one of their buffers, we tell them about it. We'll then |
| 1504 | * want to notify the guest, using eventfd. */ |
| 1505 | int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len) |
| 1506 | { |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1507 | struct vring_used_elem heads = { |
| 1508 | cpu_to_vhost32(vq, head), |
| 1509 | cpu_to_vhost32(vq, len) |
| 1510 | }; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1511 | |
Jason Wang | c49e4e5 | 2013-09-02 16:40:58 +0800 | [diff] [blame] | 1512 | return vhost_add_used_n(vq, &heads, 1); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1513 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 1514 | EXPORT_SYMBOL_GPL(vhost_add_used); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1515 | |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 1516 | static int __vhost_add_used_n(struct vhost_virtqueue *vq, |
| 1517 | struct vring_used_elem *heads, |
| 1518 | unsigned count) |
| 1519 | { |
| 1520 | struct vring_used_elem __user *used; |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 1521 | u16 old, new; |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 1522 | int start; |
| 1523 | |
Michael S. Tsirkin | 5fba13b | 2015-11-29 13:34:44 +0200 | [diff] [blame] | 1524 | start = vq->last_used_idx & (vq->num - 1); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 1525 | used = vq->used->ring + start; |
Jason Wang | c49e4e5 | 2013-09-02 16:40:58 +0800 | [diff] [blame] | 1526 | if (count == 1) { |
| 1527 | if (__put_user(heads[0].id, &used->id)) { |
| 1528 | vq_err(vq, "Failed to write used id"); |
| 1529 | return -EFAULT; |
| 1530 | } |
| 1531 | if (__put_user(heads[0].len, &used->len)) { |
| 1532 | vq_err(vq, "Failed to write used len"); |
| 1533 | return -EFAULT; |
| 1534 | } |
| 1535 | } else if (__copy_to_user(used, heads, count * sizeof *used)) { |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 1536 | vq_err(vq, "Failed to write used"); |
| 1537 | return -EFAULT; |
| 1538 | } |
| 1539 | if (unlikely(vq->log_used)) { |
| 1540 | /* Make sure data is seen before log. */ |
| 1541 | smp_wmb(); |
| 1542 | /* Log used ring entry write. */ |
| 1543 | log_write(vq->log_base, |
| 1544 | vq->log_addr + |
| 1545 | ((void __user *)used - (void __user *)vq->used), |
| 1546 | count * sizeof *used); |
| 1547 | } |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 1548 | old = vq->last_used_idx; |
| 1549 | new = (vq->last_used_idx += count); |
| 1550 | /* If the driver never bothers to signal in a very long while, |
| 1551 | * used index might wrap around. If that happens, invalidate |
| 1552 | * signalled_used index we stored. TODO: make sure driver |
| 1553 | * signals at least once in 2^16 and remove this. */ |
| 1554 | if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old))) |
| 1555 | vq->signalled_used_valid = false; |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 1556 | return 0; |
| 1557 | } |
| 1558 | |
| 1559 | /* After we've used one of their buffers, we tell them about it. We'll then |
| 1560 | * want to notify the guest, using eventfd. */ |
| 1561 | int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads, |
| 1562 | unsigned count) |
| 1563 | { |
| 1564 | int start, n, r; |
| 1565 | |
Michael S. Tsirkin | 5fba13b | 2015-11-29 13:34:44 +0200 | [diff] [blame] | 1566 | start = vq->last_used_idx & (vq->num - 1); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 1567 | n = vq->num - start; |
| 1568 | if (n < count) { |
| 1569 | r = __vhost_add_used_n(vq, heads, n); |
| 1570 | if (r < 0) |
| 1571 | return r; |
| 1572 | heads += n; |
| 1573 | count -= n; |
| 1574 | } |
| 1575 | r = __vhost_add_used_n(vq, heads, count); |
| 1576 | |
| 1577 | /* Make sure buffer is written before we update index. */ |
| 1578 | smp_wmb(); |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1579 | if (__put_user(cpu_to_vhost16(vq, vq->last_used_idx), &vq->used->idx)) { |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 1580 | vq_err(vq, "Failed to increment used idx"); |
| 1581 | return -EFAULT; |
| 1582 | } |
| 1583 | if (unlikely(vq->log_used)) { |
| 1584 | /* Log used index update. */ |
| 1585 | log_write(vq->log_base, |
| 1586 | vq->log_addr + offsetof(struct vring_used, idx), |
| 1587 | sizeof vq->used->idx); |
| 1588 | if (vq->log_ctx) |
| 1589 | eventfd_signal(vq->log_ctx, 1); |
| 1590 | } |
| 1591 | return r; |
| 1592 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 1593 | EXPORT_SYMBOL_GPL(vhost_add_used_n); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 1594 | |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 1595 | 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] | 1596 | { |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1597 | __u16 old, new; |
| 1598 | __virtio16 event; |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 1599 | bool v; |
Michael S. Tsirkin | 0d49935 | 2010-05-11 19:44:17 +0300 | [diff] [blame] | 1600 | /* Flush out used index updates. This is paired |
| 1601 | * with the barrier that the Guest executes when enabling |
| 1602 | * interrupts. */ |
| 1603 | smp_mb(); |
| 1604 | |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 1605 | if (vhost_has_feature(vq, VIRTIO_F_NOTIFY_ON_EMPTY) && |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 1606 | unlikely(vq->avail_idx == vq->last_avail_idx)) |
| 1607 | return true; |
| 1608 | |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 1609 | if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) { |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1610 | __virtio16 flags; |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 1611 | if (__get_user(flags, &vq->avail->flags)) { |
| 1612 | vq_err(vq, "Failed to get flags"); |
| 1613 | return true; |
| 1614 | } |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1615 | return !(flags & cpu_to_vhost16(vq, VRING_AVAIL_F_NO_INTERRUPT)); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1616 | } |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 1617 | old = vq->signalled_used; |
| 1618 | v = vq->signalled_used_valid; |
| 1619 | new = vq->signalled_used = vq->last_used_idx; |
| 1620 | vq->signalled_used_valid = true; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1621 | |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 1622 | if (unlikely(!v)) |
| 1623 | return true; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1624 | |
Michael S. Tsirkin | 64f7f05 | 2014-12-01 17:39:39 +0200 | [diff] [blame] | 1625 | if (__get_user(event, vhost_used_event(vq))) { |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 1626 | vq_err(vq, "Failed to get used event idx"); |
| 1627 | return true; |
| 1628 | } |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1629 | return vring_need_event(vhost16_to_cpu(vq, event), new, old); |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | /* This actually signals the guest, using eventfd. */ |
| 1633 | void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq) |
| 1634 | { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1635 | /* Signal the Guest tell them we used something up. */ |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 1636 | if (vq->call_ctx && vhost_notify(dev, vq)) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1637 | eventfd_signal(vq->call_ctx, 1); |
| 1638 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 1639 | EXPORT_SYMBOL_GPL(vhost_signal); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1640 | |
| 1641 | /* And here's the combo meal deal. Supersize me! */ |
| 1642 | void vhost_add_used_and_signal(struct vhost_dev *dev, |
| 1643 | struct vhost_virtqueue *vq, |
| 1644 | unsigned int head, int len) |
| 1645 | { |
| 1646 | vhost_add_used(vq, head, len); |
| 1647 | vhost_signal(dev, vq); |
| 1648 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 1649 | EXPORT_SYMBOL_GPL(vhost_add_used_and_signal); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1650 | |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 1651 | /* multi-buffer version of vhost_add_used_and_signal */ |
| 1652 | void vhost_add_used_and_signal_n(struct vhost_dev *dev, |
| 1653 | struct vhost_virtqueue *vq, |
| 1654 | struct vring_used_elem *heads, unsigned count) |
| 1655 | { |
| 1656 | vhost_add_used_n(vq, heads, count); |
| 1657 | vhost_signal(dev, vq); |
| 1658 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 1659 | EXPORT_SYMBOL_GPL(vhost_add_used_and_signal_n); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 1660 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1661 | /* OK, now we need to know about added descriptors. */ |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 1662 | 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] | 1663 | { |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1664 | __virtio16 avail_idx; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1665 | int r; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 1666 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1667 | if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY)) |
| 1668 | return false; |
| 1669 | vq->used_flags &= ~VRING_USED_F_NO_NOTIFY; |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 1670 | if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) { |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 1671 | r = vhost_update_used_flags(vq); |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 1672 | if (r) { |
| 1673 | vq_err(vq, "Failed to enable notification at %p: %d\n", |
| 1674 | &vq->used->flags, r); |
| 1675 | return false; |
| 1676 | } |
| 1677 | } else { |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 1678 | r = vhost_update_avail_event(vq, vq->avail_idx); |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 1679 | if (r) { |
| 1680 | vq_err(vq, "Failed to update avail event index at %p: %d\n", |
| 1681 | vhost_avail_event(vq), r); |
| 1682 | return false; |
| 1683 | } |
| 1684 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1685 | /* They could have slipped one in as we were doing that: make |
| 1686 | * sure it's written, then check again. */ |
Michael S. Tsirkin | 5659338 | 2010-02-01 07:21:02 +0000 | [diff] [blame] | 1687 | smp_mb(); |
Michael S. Tsirkin | 8b7347a | 2010-09-19 15:56:30 +0200 | [diff] [blame] | 1688 | r = __get_user(avail_idx, &vq->avail->idx); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1689 | if (r) { |
| 1690 | vq_err(vq, "Failed to check avail idx at %p: %d\n", |
| 1691 | &vq->avail->idx, r); |
| 1692 | return false; |
| 1693 | } |
| 1694 | |
Michael S. Tsirkin | 3b1bbe8 | 2014-10-24 14:04:47 +0300 | [diff] [blame] | 1695 | return vhost16_to_cpu(vq, avail_idx) != vq->avail_idx; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1696 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 1697 | EXPORT_SYMBOL_GPL(vhost_enable_notify); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1698 | |
| 1699 | /* We don't need to be notified again. */ |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 1700 | 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] | 1701 | { |
| 1702 | int r; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 1703 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1704 | if (vq->used_flags & VRING_USED_F_NO_NOTIFY) |
| 1705 | return; |
| 1706 | vq->used_flags |= VRING_USED_F_NO_NOTIFY; |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 1707 | if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) { |
Jason Wang | 2723fea | 2011-06-21 18:04:38 +0800 | [diff] [blame] | 1708 | r = vhost_update_used_flags(vq); |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 1709 | if (r) |
| 1710 | vq_err(vq, "Failed to enable notification at %p: %d\n", |
| 1711 | &vq->used->flags, r); |
| 1712 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1713 | } |
Asias He | 6ac1afb | 2013-05-06 16:38:21 +0800 | [diff] [blame] | 1714 | EXPORT_SYMBOL_GPL(vhost_disable_notify); |
| 1715 | |
| 1716 | static int __init vhost_init(void) |
| 1717 | { |
| 1718 | return 0; |
| 1719 | } |
| 1720 | |
| 1721 | static void __exit vhost_exit(void) |
| 1722 | { |
| 1723 | } |
| 1724 | |
| 1725 | module_init(vhost_init); |
| 1726 | module_exit(vhost_exit); |
| 1727 | |
| 1728 | MODULE_VERSION("0.0.1"); |
| 1729 | MODULE_LICENSE("GPL v2"); |
| 1730 | MODULE_AUTHOR("Michael S. Tsirkin"); |
| 1731 | MODULE_DESCRIPTION("Host kernel accelerator for virtio"); |