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