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