blob: 2ab291241635d94278ff9f417b19223c265a044c [file] [log] [blame]
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001/* 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
7 * Documentation/lguest/lguest.c, by Rusty Russell
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>
16#include <linux/virtio_net.h>
17#include <linux/mm.h>
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +020018#include <linux/mmu_context.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000019#include <linux/miscdevice.h>
20#include <linux/mutex.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000021#include <linux/rcupdate.h>
22#include <linux/poll.h>
23#include <linux/file.h>
24#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Tejun Heoc23f3442010-06-02 20:40:00 +020026#include <linux/kthread.h>
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +030027#include <linux/cgroup.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000028
29#include <linux/net.h>
30#include <linux/if_packet.h>
31#include <linux/if_arp.h>
32
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000033#include "vhost.h"
34
35enum {
36 VHOST_MEMORY_MAX_NREGIONS = 64,
37 VHOST_MEMORY_F_LOG = 0x1,
38};
39
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000040static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
41 poll_table *pt)
42{
43 struct vhost_poll *poll;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000044
Krishna Kumard47effe2011-03-01 17:06:37 +053045 poll = container_of(pt, struct vhost_poll, table);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000046 poll->wqh = wqh;
47 add_wait_queue(wqh, &poll->wait);
48}
49
50static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync,
51 void *key)
52{
Tejun Heoc23f3442010-06-02 20:40:00 +020053 struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
54
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000055 if (!((unsigned long)key & poll->mask))
56 return 0;
57
Tejun Heoc23f3442010-06-02 20:40:00 +020058 vhost_poll_queue(poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000059 return 0;
60}
61
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +030062static void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000063{
Tejun Heoc23f3442010-06-02 20:40:00 +020064 INIT_LIST_HEAD(&work->node);
65 work->fn = fn;
66 init_waitqueue_head(&work->done);
67 work->flushing = 0;
68 work->queue_seq = work->done_seq = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000069}
70
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +030071/* Init poll structure */
72void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
73 unsigned long mask, struct vhost_dev *dev)
74{
75 init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
76 init_poll_funcptr(&poll->table, vhost_poll_func);
77 poll->mask = mask;
78 poll->dev = dev;
79
80 vhost_work_init(&poll->work, fn);
81}
82
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000083/* 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. */
85void vhost_poll_start(struct vhost_poll *poll, struct file *file)
86{
87 unsigned long mask;
Krishna Kumard47effe2011-03-01 17:06:37 +053088
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000089 mask = file->f_op->poll(file, &poll->table);
90 if (mask)
91 vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
92}
93
94/* Stop polling a file. After this function returns, it becomes safe to drop the
95 * file reference. You must also flush afterwards. */
96void vhost_poll_stop(struct vhost_poll *poll)
97{
98 remove_wait_queue(poll->wqh, &poll->wait);
99}
100
Michael S. Tsirkin0174b0c2011-01-10 10:03:20 +0200101static bool vhost_work_seq_done(struct vhost_dev *dev, struct vhost_work *work,
102 unsigned seq)
103{
104 int left;
Krishna Kumard47effe2011-03-01 17:06:37 +0530105
Michael S. Tsirkin0174b0c2011-01-10 10:03:20 +0200106 spin_lock_irq(&dev->work_lock);
107 left = seq - work->done_seq;
108 spin_unlock_irq(&dev->work_lock);
109 return left <= 0;
110}
111
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300112static void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000113{
Tejun Heoc23f3442010-06-02 20:40:00 +0200114 unsigned seq;
Tejun Heoc23f3442010-06-02 20:40:00 +0200115 int flushing;
116
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300117 spin_lock_irq(&dev->work_lock);
Tejun Heoc23f3442010-06-02 20:40:00 +0200118 seq = work->queue_seq;
119 work->flushing++;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300120 spin_unlock_irq(&dev->work_lock);
Michael S. Tsirkin0174b0c2011-01-10 10:03:20 +0200121 wait_event(work->done, vhost_work_seq_done(dev, work, seq));
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300122 spin_lock_irq(&dev->work_lock);
Tejun Heoc23f3442010-06-02 20:40:00 +0200123 flushing = --work->flushing;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300124 spin_unlock_irq(&dev->work_lock);
Tejun Heoc23f3442010-06-02 20:40:00 +0200125 BUG_ON(flushing < 0);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000126}
127
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300128/* Flush any work that has been scheduled. When calling this, don't hold any
129 * locks that are also used by the callback. */
130void vhost_poll_flush(struct vhost_poll *poll)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000131{
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300132 vhost_work_flush(poll->dev, &poll->work);
133}
134
135static inline void vhost_work_queue(struct vhost_dev *dev,
136 struct vhost_work *work)
137{
Tejun Heoc23f3442010-06-02 20:40:00 +0200138 unsigned long flags;
139
140 spin_lock_irqsave(&dev->work_lock, flags);
141 if (list_empty(&work->node)) {
142 list_add_tail(&work->node, &dev->work_list);
143 work->queue_seq++;
144 wake_up_process(dev->worker);
145 }
146 spin_unlock_irqrestore(&dev->work_lock, flags);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000147}
148
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300149void vhost_poll_queue(struct vhost_poll *poll)
150{
151 vhost_work_queue(poll->dev, &poll->work);
152}
153
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000154static void vhost_vq_reset(struct vhost_dev *dev,
155 struct vhost_virtqueue *vq)
156{
157 vq->num = 1;
158 vq->desc = NULL;
159 vq->avail = NULL;
160 vq->used = NULL;
161 vq->last_avail_idx = 0;
162 vq->avail_idx = 0;
163 vq->last_used_idx = 0;
164 vq->used_flags = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000165 vq->log_used = false;
166 vq->log_addr = -1ull;
David Stevens8dd014a2010-07-27 18:52:21 +0300167 vq->vhost_hlen = 0;
168 vq->sock_hlen = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000169 vq->private_data = NULL;
170 vq->log_base = NULL;
171 vq->error_ctx = NULL;
172 vq->error = NULL;
173 vq->kick = NULL;
174 vq->call_ctx = NULL;
175 vq->call = NULL;
Michael S. Tsirkin73a99f02010-02-23 11:23:45 +0200176 vq->log_ctx = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000177}
178
Tejun Heoc23f3442010-06-02 20:40:00 +0200179static int vhost_worker(void *data)
180{
181 struct vhost_dev *dev = data;
182 struct vhost_work *work = NULL;
183 unsigned uninitialized_var(seq);
184
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200185 use_mm(dev->mm);
186
Tejun Heoc23f3442010-06-02 20:40:00 +0200187 for (;;) {
188 /* mb paired w/ kthread_stop */
189 set_current_state(TASK_INTERRUPTIBLE);
190
191 spin_lock_irq(&dev->work_lock);
192 if (work) {
193 work->done_seq = seq;
194 if (work->flushing)
195 wake_up_all(&work->done);
196 }
197
198 if (kthread_should_stop()) {
199 spin_unlock_irq(&dev->work_lock);
200 __set_current_state(TASK_RUNNING);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200201 break;
Tejun Heoc23f3442010-06-02 20:40:00 +0200202 }
203 if (!list_empty(&dev->work_list)) {
204 work = list_first_entry(&dev->work_list,
205 struct vhost_work, node);
206 list_del_init(&work->node);
207 seq = work->queue_seq;
208 } else
209 work = NULL;
210 spin_unlock_irq(&dev->work_lock);
211
212 if (work) {
213 __set_current_state(TASK_RUNNING);
214 work->fn(work);
215 } else
216 schedule();
217
218 }
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200219 unuse_mm(dev->mm);
220 return 0;
Tejun Heoc23f3442010-06-02 20:40:00 +0200221}
222
Jason Wange0e9b402010-09-14 23:53:05 +0800223/* Helper to allocate iovec buffers for all vqs. */
224static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
225{
226 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530227
Jason Wange0e9b402010-09-14 23:53:05 +0800228 for (i = 0; i < dev->nvqs; ++i) {
229 dev->vqs[i].indirect = kmalloc(sizeof *dev->vqs[i].indirect *
230 UIO_MAXIOV, GFP_KERNEL);
231 dev->vqs[i].log = kmalloc(sizeof *dev->vqs[i].log * UIO_MAXIOV,
232 GFP_KERNEL);
233 dev->vqs[i].heads = kmalloc(sizeof *dev->vqs[i].heads *
234 UIO_MAXIOV, GFP_KERNEL);
235
236 if (!dev->vqs[i].indirect || !dev->vqs[i].log ||
237 !dev->vqs[i].heads)
238 goto err_nomem;
239 }
240 return 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530241
Jason Wange0e9b402010-09-14 23:53:05 +0800242err_nomem:
243 for (; i >= 0; --i) {
244 kfree(dev->vqs[i].indirect);
245 kfree(dev->vqs[i].log);
246 kfree(dev->vqs[i].heads);
247 }
248 return -ENOMEM;
249}
250
251static void vhost_dev_free_iovecs(struct vhost_dev *dev)
252{
253 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530254
Jason Wange0e9b402010-09-14 23:53:05 +0800255 for (i = 0; i < dev->nvqs; ++i) {
256 kfree(dev->vqs[i].indirect);
257 dev->vqs[i].indirect = NULL;
258 kfree(dev->vqs[i].log);
259 dev->vqs[i].log = NULL;
260 kfree(dev->vqs[i].heads);
261 dev->vqs[i].heads = NULL;
262 }
263}
264
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000265long vhost_dev_init(struct vhost_dev *dev,
266 struct vhost_virtqueue *vqs, int nvqs)
267{
268 int i;
Tejun Heoc23f3442010-06-02 20:40:00 +0200269
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000270 dev->vqs = vqs;
271 dev->nvqs = nvqs;
272 mutex_init(&dev->mutex);
273 dev->log_ctx = NULL;
274 dev->log_file = NULL;
275 dev->memory = NULL;
276 dev->mm = NULL;
Tejun Heoc23f3442010-06-02 20:40:00 +0200277 spin_lock_init(&dev->work_lock);
278 INIT_LIST_HEAD(&dev->work_list);
279 dev->worker = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000280
281 for (i = 0; i < dev->nvqs; ++i) {
Jason Wange0e9b402010-09-14 23:53:05 +0800282 dev->vqs[i].log = NULL;
283 dev->vqs[i].indirect = NULL;
284 dev->vqs[i].heads = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000285 dev->vqs[i].dev = dev;
286 mutex_init(&dev->vqs[i].mutex);
287 vhost_vq_reset(dev, dev->vqs + i);
288 if (dev->vqs[i].handle_kick)
289 vhost_poll_init(&dev->vqs[i].poll,
Tejun Heoc23f3442010-06-02 20:40:00 +0200290 dev->vqs[i].handle_kick, POLLIN, dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000291 }
Tejun Heoc23f3442010-06-02 20:40:00 +0200292
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000293 return 0;
294}
295
296/* Caller should have device mutex */
297long vhost_dev_check_owner(struct vhost_dev *dev)
298{
299 /* Are you the owner? If not, I don't think you mean to do that */
300 return dev->mm == current->mm ? 0 : -EPERM;
301}
302
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300303struct vhost_attach_cgroups_struct {
Krishna Kumard47effe2011-03-01 17:06:37 +0530304 struct vhost_work work;
305 struct task_struct *owner;
306 int ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300307};
308
309static void vhost_attach_cgroups_work(struct vhost_work *work)
310{
Krishna Kumard47effe2011-03-01 17:06:37 +0530311 struct vhost_attach_cgroups_struct *s;
312
313 s = container_of(work, struct vhost_attach_cgroups_struct, work);
314 s->ret = cgroup_attach_task_all(s->owner, current);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300315}
316
317static int vhost_attach_cgroups(struct vhost_dev *dev)
318{
Krishna Kumard47effe2011-03-01 17:06:37 +0530319 struct vhost_attach_cgroups_struct attach;
320
321 attach.owner = current;
322 vhost_work_init(&attach.work, vhost_attach_cgroups_work);
323 vhost_work_queue(dev, &attach.work);
324 vhost_work_flush(dev, &attach.work);
325 return attach.ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300326}
327
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000328/* Caller should have device mutex */
329static long vhost_dev_set_owner(struct vhost_dev *dev)
330{
Tejun Heoc23f3442010-06-02 20:40:00 +0200331 struct task_struct *worker;
332 int err;
Krishna Kumard47effe2011-03-01 17:06:37 +0530333
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000334 /* Is there an owner already? */
Tejun Heoc23f3442010-06-02 20:40:00 +0200335 if (dev->mm) {
336 err = -EBUSY;
337 goto err_mm;
338 }
Krishna Kumard47effe2011-03-01 17:06:37 +0530339
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000340 /* No owner, become one */
341 dev->mm = get_task_mm(current);
Tejun Heoc23f3442010-06-02 20:40:00 +0200342 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
343 if (IS_ERR(worker)) {
344 err = PTR_ERR(worker);
345 goto err_worker;
346 }
347
348 dev->worker = worker;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300349 wake_up_process(worker); /* avoid contributing to loadavg */
350
351 err = vhost_attach_cgroups(dev);
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300352 if (err)
353 goto err_cgroup;
Tejun Heoc23f3442010-06-02 20:40:00 +0200354
Jason Wange0e9b402010-09-14 23:53:05 +0800355 err = vhost_dev_alloc_iovecs(dev);
356 if (err)
357 goto err_cgroup;
358
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000359 return 0;
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300360err_cgroup:
361 kthread_stop(worker);
Michael S. Tsirkin615cc222010-09-02 14:16:36 +0300362 dev->worker = NULL;
Tejun Heoc23f3442010-06-02 20:40:00 +0200363err_worker:
364 if (dev->mm)
365 mmput(dev->mm);
366 dev->mm = NULL;
367err_mm:
368 return err;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000369}
370
371/* Caller should have device mutex */
372long vhost_dev_reset_owner(struct vhost_dev *dev)
373{
374 struct vhost_memory *memory;
375
376 /* Restore memory to default empty mapping. */
377 memory = kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
378 if (!memory)
379 return -ENOMEM;
380
381 vhost_dev_cleanup(dev);
382
383 memory->nregions = 0;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100384 RCU_INIT_POINTER(dev->memory, memory);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000385 return 0;
386}
387
388/* Caller should have device mutex */
389void vhost_dev_cleanup(struct vhost_dev *dev)
390{
391 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530392
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000393 for (i = 0; i < dev->nvqs; ++i) {
394 if (dev->vqs[i].kick && dev->vqs[i].handle_kick) {
395 vhost_poll_stop(&dev->vqs[i].poll);
396 vhost_poll_flush(&dev->vqs[i].poll);
397 }
398 if (dev->vqs[i].error_ctx)
399 eventfd_ctx_put(dev->vqs[i].error_ctx);
400 if (dev->vqs[i].error)
401 fput(dev->vqs[i].error);
402 if (dev->vqs[i].kick)
403 fput(dev->vqs[i].kick);
404 if (dev->vqs[i].call_ctx)
405 eventfd_ctx_put(dev->vqs[i].call_ctx);
406 if (dev->vqs[i].call)
407 fput(dev->vqs[i].call);
408 vhost_vq_reset(dev, dev->vqs + i);
409 }
Jason Wange0e9b402010-09-14 23:53:05 +0800410 vhost_dev_free_iovecs(dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000411 if (dev->log_ctx)
412 eventfd_ctx_put(dev->log_ctx);
413 dev->log_ctx = NULL;
414 if (dev->log_file)
415 fput(dev->log_file);
416 dev->log_file = NULL;
417 /* No one will access memory at this point */
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100418 kfree(rcu_dereference_protected(dev->memory,
419 lockdep_is_held(&dev->mutex)));
420 RCU_INIT_POINTER(dev->memory, NULL);
Tejun Heoc23f3442010-06-02 20:40:00 +0200421 WARN_ON(!list_empty(&dev->work_list));
Eric Dumazet78b620c2010-08-31 02:05:57 +0000422 if (dev->worker) {
423 kthread_stop(dev->worker);
424 dev->worker = NULL;
425 }
Michael S. Tsirkin533a19b2010-10-06 15:34:38 +0200426 if (dev->mm)
427 mmput(dev->mm);
428 dev->mm = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000429}
430
431static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
432{
433 u64 a = addr / VHOST_PAGE_SIZE / 8;
Krishna Kumard47effe2011-03-01 17:06:37 +0530434
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000435 /* Make sure 64 bit math will not overflow. */
436 if (a > ULONG_MAX - (unsigned long)log_base ||
437 a + (unsigned long)log_base > ULONG_MAX)
Dan Carpenter6d97e552010-10-11 19:24:19 +0200438 return 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000439
440 return access_ok(VERIFY_WRITE, log_base + a,
441 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
442}
443
444/* Caller should have vq mutex and device mutex. */
445static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
446 int log_all)
447{
448 int i;
Jeff Dike179b2842010-04-07 09:59:10 -0400449
Michael S. Tsirkinf8322fb2010-05-27 12:28:03 +0300450 if (!mem)
451 return 0;
Jeff Dike179b2842010-04-07 09:59:10 -0400452
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000453 for (i = 0; i < mem->nregions; ++i) {
454 struct vhost_memory_region *m = mem->regions + i;
455 unsigned long a = m->userspace_addr;
456 if (m->memory_size > ULONG_MAX)
457 return 0;
458 else if (!access_ok(VERIFY_WRITE, (void __user *)a,
459 m->memory_size))
460 return 0;
461 else if (log_all && !log_access_ok(log_base,
462 m->guest_phys_addr,
463 m->memory_size))
464 return 0;
465 }
466 return 1;
467}
468
469/* Can we switch to this memory table? */
470/* Caller should have device mutex but not vq mutex */
471static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
472 int log_all)
473{
474 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530475
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000476 for (i = 0; i < d->nvqs; ++i) {
477 int ok;
478 mutex_lock(&d->vqs[i].mutex);
479 /* If ring is inactive, will check when it's enabled. */
480 if (d->vqs[i].private_data)
481 ok = vq_memory_access_ok(d->vqs[i].log_base, mem,
482 log_all);
483 else
484 ok = 1;
485 mutex_unlock(&d->vqs[i].mutex);
486 if (!ok)
487 return 0;
488 }
489 return 1;
490}
491
492static int vq_access_ok(unsigned int num,
493 struct vring_desc __user *desc,
494 struct vring_avail __user *avail,
495 struct vring_used __user *used)
496{
497 return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
498 access_ok(VERIFY_READ, avail,
499 sizeof *avail + num * sizeof *avail->ring) &&
500 access_ok(VERIFY_WRITE, used,
501 sizeof *used + num * sizeof *used->ring);
502}
503
504/* Can we log writes? */
505/* Caller should have device mutex but not vq mutex */
506int vhost_log_access_ok(struct vhost_dev *dev)
507{
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100508 struct vhost_memory *mp;
509
510 mp = rcu_dereference_protected(dev->memory,
511 lockdep_is_held(&dev->mutex));
512 return memory_access_ok(dev, mp, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000513}
514
515/* Verify access for write logging. */
516/* Caller should have vq mutex and device mutex */
517static int vq_log_access_ok(struct vhost_virtqueue *vq, void __user *log_base)
518{
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100519 struct vhost_memory *mp;
520
521 mp = rcu_dereference_protected(vq->dev->memory,
522 lockdep_is_held(&vq->mutex));
523 return vq_memory_access_ok(log_base, mp,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000524 vhost_has_feature(vq->dev, VHOST_F_LOG_ALL)) &&
525 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
526 sizeof *vq->used +
527 vq->num * sizeof *vq->used->ring));
528}
529
530/* Can we start vq? */
531/* Caller should have vq mutex and device mutex */
532int vhost_vq_access_ok(struct vhost_virtqueue *vq)
533{
534 return vq_access_ok(vq->num, vq->desc, vq->avail, vq->used) &&
535 vq_log_access_ok(vq, vq->log_base);
536}
537
538static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
539{
540 struct vhost_memory mem, *newmem, *oldmem;
541 unsigned long size = offsetof(struct vhost_memory, regions);
Krishna Kumard47effe2011-03-01 17:06:37 +0530542
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900543 if (copy_from_user(&mem, m, size))
544 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000545 if (mem.padding)
546 return -EOPNOTSUPP;
547 if (mem.nregions > VHOST_MEMORY_MAX_NREGIONS)
548 return -E2BIG;
549 newmem = kmalloc(size + mem.nregions * sizeof *m->regions, GFP_KERNEL);
550 if (!newmem)
551 return -ENOMEM;
552
553 memcpy(newmem, &mem, size);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900554 if (copy_from_user(newmem->regions, m->regions,
555 mem.nregions * sizeof *m->regions)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000556 kfree(newmem);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900557 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000558 }
559
Krishna Kumard47effe2011-03-01 17:06:37 +0530560 if (!memory_access_ok(d, newmem,
561 vhost_has_feature(d, VHOST_F_LOG_ALL))) {
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +0900562 kfree(newmem);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000563 return -EFAULT;
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +0900564 }
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100565 oldmem = rcu_dereference_protected(d->memory,
566 lockdep_is_held(&d->mutex));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000567 rcu_assign_pointer(d->memory, newmem);
568 synchronize_rcu();
569 kfree(oldmem);
570 return 0;
571}
572
573static int init_used(struct vhost_virtqueue *vq,
574 struct vring_used __user *used)
575{
576 int r = put_user(vq->used_flags, &used->flags);
Krishna Kumard47effe2011-03-01 17:06:37 +0530577
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000578 if (r)
579 return r;
580 return get_user(vq->last_used_idx, &used->idx);
581}
582
583static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp)
584{
585 struct file *eventfp, *filep = NULL,
586 *pollstart = NULL, *pollstop = NULL;
587 struct eventfd_ctx *ctx = NULL;
588 u32 __user *idxp = argp;
589 struct vhost_virtqueue *vq;
590 struct vhost_vring_state s;
591 struct vhost_vring_file f;
592 struct vhost_vring_addr a;
593 u32 idx;
594 long r;
595
596 r = get_user(idx, idxp);
597 if (r < 0)
598 return r;
Krishna Kumar0f3d9a12010-05-25 11:10:36 +0530599 if (idx >= d->nvqs)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000600 return -ENOBUFS;
601
602 vq = d->vqs + idx;
603
604 mutex_lock(&vq->mutex);
605
606 switch (ioctl) {
607 case VHOST_SET_VRING_NUM:
608 /* Resizing ring with an active backend?
609 * You don't want to do that. */
610 if (vq->private_data) {
611 r = -EBUSY;
612 break;
613 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900614 if (copy_from_user(&s, argp, sizeof s)) {
615 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000616 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900617 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000618 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
619 r = -EINVAL;
620 break;
621 }
622 vq->num = s.num;
623 break;
624 case VHOST_SET_VRING_BASE:
625 /* Moving base with an active backend?
626 * You don't want to do that. */
627 if (vq->private_data) {
628 r = -EBUSY;
629 break;
630 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900631 if (copy_from_user(&s, argp, sizeof s)) {
632 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000633 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900634 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000635 if (s.num > 0xffff) {
636 r = -EINVAL;
637 break;
638 }
639 vq->last_avail_idx = s.num;
640 /* Forget the cached index value. */
641 vq->avail_idx = vq->last_avail_idx;
642 break;
643 case VHOST_GET_VRING_BASE:
644 s.index = idx;
645 s.num = vq->last_avail_idx;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900646 if (copy_to_user(argp, &s, sizeof s))
647 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000648 break;
649 case VHOST_SET_VRING_ADDR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900650 if (copy_from_user(&a, argp, sizeof a)) {
651 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000652 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900653 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000654 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
655 r = -EOPNOTSUPP;
656 break;
657 }
658 /* For 32bit, verify that the top 32bits of the user
659 data are set to zero. */
660 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
661 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
662 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
663 r = -EFAULT;
664 break;
665 }
666 if ((a.avail_user_addr & (sizeof *vq->avail->ring - 1)) ||
667 (a.used_user_addr & (sizeof *vq->used->ring - 1)) ||
668 (a.log_guest_addr & (sizeof *vq->used->ring - 1))) {
669 r = -EINVAL;
670 break;
671 }
672
673 /* We only verify access here if backend is configured.
674 * If it is not, we don't as size might not have been setup.
675 * We will verify when backend is configured. */
676 if (vq->private_data) {
677 if (!vq_access_ok(vq->num,
678 (void __user *)(unsigned long)a.desc_user_addr,
679 (void __user *)(unsigned long)a.avail_user_addr,
680 (void __user *)(unsigned long)a.used_user_addr)) {
681 r = -EINVAL;
682 break;
683 }
684
685 /* Also validate log access for used ring if enabled. */
686 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
687 !log_access_ok(vq->log_base, a.log_guest_addr,
688 sizeof *vq->used +
689 vq->num * sizeof *vq->used->ring)) {
690 r = -EINVAL;
691 break;
692 }
693 }
694
695 r = init_used(vq, (struct vring_used __user *)(unsigned long)
696 a.used_user_addr);
697 if (r)
698 break;
699 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
700 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
701 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
702 vq->log_addr = a.log_guest_addr;
703 vq->used = (void __user *)(unsigned long)a.used_user_addr;
704 break;
705 case VHOST_SET_VRING_KICK:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900706 if (copy_from_user(&f, argp, sizeof f)) {
707 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000708 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900709 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000710 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200711 if (IS_ERR(eventfp)) {
712 r = PTR_ERR(eventfp);
713 break;
714 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000715 if (eventfp != vq->kick) {
716 pollstop = filep = vq->kick;
717 pollstart = vq->kick = eventfp;
718 } else
719 filep = eventfp;
720 break;
721 case VHOST_SET_VRING_CALL:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900722 if (copy_from_user(&f, argp, sizeof f)) {
723 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000724 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900725 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000726 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200727 if (IS_ERR(eventfp)) {
728 r = PTR_ERR(eventfp);
729 break;
730 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000731 if (eventfp != vq->call) {
732 filep = vq->call;
733 ctx = vq->call_ctx;
734 vq->call = eventfp;
735 vq->call_ctx = eventfp ?
736 eventfd_ctx_fileget(eventfp) : NULL;
737 } else
738 filep = eventfp;
739 break;
740 case VHOST_SET_VRING_ERR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900741 if (copy_from_user(&f, argp, sizeof f)) {
742 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000743 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900744 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000745 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200746 if (IS_ERR(eventfp)) {
747 r = PTR_ERR(eventfp);
748 break;
749 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000750 if (eventfp != vq->error) {
751 filep = vq->error;
752 vq->error = eventfp;
753 ctx = vq->error_ctx;
754 vq->error_ctx = eventfp ?
755 eventfd_ctx_fileget(eventfp) : NULL;
756 } else
757 filep = eventfp;
758 break;
759 default:
760 r = -ENOIOCTLCMD;
761 }
762
763 if (pollstop && vq->handle_kick)
764 vhost_poll_stop(&vq->poll);
765
766 if (ctx)
767 eventfd_ctx_put(ctx);
768 if (filep)
769 fput(filep);
770
771 if (pollstart && vq->handle_kick)
772 vhost_poll_start(&vq->poll, vq->kick);
773
774 mutex_unlock(&vq->mutex);
775
776 if (pollstop && vq->handle_kick)
777 vhost_poll_flush(&vq->poll);
778 return r;
779}
780
781/* Caller must have device mutex */
782long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, unsigned long arg)
783{
784 void __user *argp = (void __user *)arg;
785 struct file *eventfp, *filep = NULL;
786 struct eventfd_ctx *ctx = NULL;
787 u64 p;
788 long r;
789 int i, fd;
790
791 /* If you are not the owner, you can become one */
792 if (ioctl == VHOST_SET_OWNER) {
793 r = vhost_dev_set_owner(d);
794 goto done;
795 }
796
797 /* You must be the owner to do anything else */
798 r = vhost_dev_check_owner(d);
799 if (r)
800 goto done;
801
802 switch (ioctl) {
803 case VHOST_SET_MEM_TABLE:
804 r = vhost_set_memory(d, argp);
805 break;
806 case VHOST_SET_LOG_BASE:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900807 if (copy_from_user(&p, argp, sizeof p)) {
808 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000809 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900810 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000811 if ((u64)(unsigned long)p != p) {
812 r = -EFAULT;
813 break;
814 }
815 for (i = 0; i < d->nvqs; ++i) {
816 struct vhost_virtqueue *vq;
817 void __user *base = (void __user *)(unsigned long)p;
818 vq = d->vqs + i;
819 mutex_lock(&vq->mutex);
820 /* If ring is inactive, will check when it's enabled. */
821 if (vq->private_data && !vq_log_access_ok(vq, base))
822 r = -EFAULT;
823 else
824 vq->log_base = base;
825 mutex_unlock(&vq->mutex);
826 }
827 break;
828 case VHOST_SET_LOG_FD:
829 r = get_user(fd, (int __user *)argp);
830 if (r < 0)
831 break;
832 eventfp = fd == -1 ? NULL : eventfd_fget(fd);
833 if (IS_ERR(eventfp)) {
834 r = PTR_ERR(eventfp);
835 break;
836 }
837 if (eventfp != d->log_file) {
838 filep = d->log_file;
839 ctx = d->log_ctx;
840 d->log_ctx = eventfp ?
841 eventfd_ctx_fileget(eventfp) : NULL;
842 } else
843 filep = eventfp;
844 for (i = 0; i < d->nvqs; ++i) {
845 mutex_lock(&d->vqs[i].mutex);
846 d->vqs[i].log_ctx = d->log_ctx;
847 mutex_unlock(&d->vqs[i].mutex);
848 }
849 if (ctx)
850 eventfd_ctx_put(ctx);
851 if (filep)
852 fput(filep);
853 break;
854 default:
855 r = vhost_set_vring(d, ioctl, argp);
856 break;
857 }
858done:
859 return r;
860}
861
862static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
863 __u64 addr, __u32 len)
864{
865 struct vhost_memory_region *reg;
866 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530867
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000868 /* linear search is not brilliant, but we really have on the order of 6
869 * regions in practice */
870 for (i = 0; i < mem->nregions; ++i) {
871 reg = mem->regions + i;
872 if (reg->guest_phys_addr <= addr &&
873 reg->guest_phys_addr + reg->memory_size - 1 >= addr)
874 return reg;
875 }
876 return NULL;
877}
878
879/* TODO: This is really inefficient. We need something like get_user()
880 * (instruction directly accesses the data, with an exception table entry
881 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
882 */
883static int set_bit_to_user(int nr, void __user *addr)
884{
885 unsigned long log = (unsigned long)addr;
886 struct page *page;
887 void *base;
888 int bit = nr + (log % PAGE_SIZE) * 8;
889 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +0530890
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000891 r = get_user_pages_fast(log, 1, 1, &page);
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +0200892 if (r < 0)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000893 return r;
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +0200894 BUG_ON(r != 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000895 base = kmap_atomic(page, KM_USER0);
896 set_bit(bit, base);
897 kunmap_atomic(base, KM_USER0);
898 set_page_dirty_lock(page);
899 put_page(page);
900 return 0;
901}
902
903static int log_write(void __user *log_base,
904 u64 write_address, u64 write_length)
905{
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +0200906 u64 write_page = write_address / VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000907 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +0530908
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000909 if (!write_length)
910 return 0;
Michael S. Tsirkin3bf9be42010-11-29 10:19:07 +0200911 write_length += write_address % VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000912 for (;;) {
913 u64 base = (u64)(unsigned long)log_base;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +0200914 u64 log = base + write_page / 8;
915 int bit = write_page % 8;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000916 if ((u64)(unsigned long)log != log)
917 return -EFAULT;
918 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
919 if (r < 0)
920 return r;
921 if (write_length <= VHOST_PAGE_SIZE)
922 break;
923 write_length -= VHOST_PAGE_SIZE;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +0200924 write_page += 1;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000925 }
926 return r;
927}
928
929int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
930 unsigned int log_num, u64 len)
931{
932 int i, r;
933
934 /* Make sure data written is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +0000935 smp_wmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000936 for (i = 0; i < log_num; ++i) {
937 u64 l = min(log[i].len, len);
938 r = log_write(vq->log_base, log[i].addr, l);
939 if (r < 0)
940 return r;
941 len -= l;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +0200942 if (!len) {
943 if (vq->log_ctx)
944 eventfd_signal(vq->log_ctx, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000945 return 0;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +0200946 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000947 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000948 /* Length written exceeds what we have stored. This is a bug. */
949 BUG();
950 return 0;
951}
952
Christoph Hellwiga8d37822010-04-13 14:11:25 -0400953static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
954 struct iovec iov[], int iov_size)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000955{
956 const struct vhost_memory_region *reg;
957 struct vhost_memory *mem;
958 struct iovec *_iov;
959 u64 s = 0;
960 int ret = 0;
961
962 rcu_read_lock();
963
964 mem = rcu_dereference(dev->memory);
965 while ((u64)len > s) {
966 u64 size;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300967 if (unlikely(ret >= iov_size)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000968 ret = -ENOBUFS;
969 break;
970 }
971 reg = find_region(mem, addr, len);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300972 if (unlikely(!reg)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000973 ret = -EFAULT;
974 break;
975 }
976 _iov = iov + ret;
977 size = reg->memory_size - addr + reg->guest_phys_addr;
978 _iov->iov_len = min((u64)len, size);
Christoph Hellwiga8d37822010-04-13 14:11:25 -0400979 _iov->iov_base = (void __user *)(unsigned long)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000980 (reg->userspace_addr + addr - reg->guest_phys_addr);
981 s += size;
982 addr += size;
983 ++ret;
984 }
985
986 rcu_read_unlock();
987 return ret;
988}
989
990/* Each buffer in the virtqueues is actually a chain of descriptors. This
991 * function returns the next descriptor in the chain,
992 * or -1U if we're at the end. */
993static unsigned next_desc(struct vring_desc *desc)
994{
995 unsigned int next;
996
997 /* If this descriptor says it doesn't chain, we're done. */
998 if (!(desc->flags & VRING_DESC_F_NEXT))
999 return -1U;
1000
1001 /* Check they're not leading us off end of descriptors. */
1002 next = desc->next;
1003 /* Make sure compiler knows to grab that: we don't want it changing! */
1004 /* We will use the result as an index in an array, so most
1005 * architectures only need a compiler barrier here. */
1006 read_barrier_depends();
1007
1008 return next;
1009}
1010
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001011static int get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
1012 struct iovec iov[], unsigned int iov_size,
1013 unsigned int *out_num, unsigned int *in_num,
1014 struct vhost_log *log, unsigned int *log_num,
1015 struct vring_desc *indirect)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001016{
1017 struct vring_desc desc;
1018 unsigned int i = 0, count, found = 0;
1019 int ret;
1020
1021 /* Sanity check */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001022 if (unlikely(indirect->len % sizeof desc)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001023 vq_err(vq, "Invalid length in indirect descriptor: "
1024 "len 0x%llx not multiple of 0x%zx\n",
1025 (unsigned long long)indirect->len,
1026 sizeof desc);
1027 return -EINVAL;
1028 }
1029
1030 ret = translate_desc(dev, indirect->addr, indirect->len, vq->indirect,
Jason Wange0e9b402010-09-14 23:53:05 +08001031 UIO_MAXIOV);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001032 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001033 vq_err(vq, "Translation failure %d in indirect.\n", ret);
1034 return ret;
1035 }
1036
1037 /* We will use the result as an address to read from, so most
1038 * architectures only need a compiler barrier here. */
1039 read_barrier_depends();
1040
1041 count = indirect->len / sizeof desc;
1042 /* Buffers are chained via a 16 bit next field, so
1043 * we can have at most 2^16 of these. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001044 if (unlikely(count > USHRT_MAX + 1)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001045 vq_err(vq, "Indirect buffer length too big: %d\n",
1046 indirect->len);
1047 return -E2BIG;
1048 }
1049
1050 do {
1051 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001052 if (unlikely(++found > count)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001053 vq_err(vq, "Loop detected: last one at %u "
1054 "indirect size %u\n",
1055 i, count);
1056 return -EINVAL;
1057 }
Krishna Kumard47effe2011-03-01 17:06:37 +05301058 if (unlikely(memcpy_fromiovec((unsigned char *)&desc,
1059 vq->indirect, sizeof desc))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001060 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
1061 i, (size_t)indirect->addr + i * sizeof desc);
1062 return -EINVAL;
1063 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001064 if (unlikely(desc.flags & VRING_DESC_F_INDIRECT)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001065 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
1066 i, (size_t)indirect->addr + i * sizeof desc);
1067 return -EINVAL;
1068 }
1069
1070 ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
1071 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001072 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001073 vq_err(vq, "Translation failure %d indirect idx %d\n",
1074 ret, i);
1075 return ret;
1076 }
1077 /* If this is an input descriptor, increment that count. */
1078 if (desc.flags & VRING_DESC_F_WRITE) {
1079 *in_num += ret;
1080 if (unlikely(log)) {
1081 log[*log_num].addr = desc.addr;
1082 log[*log_num].len = desc.len;
1083 ++*log_num;
1084 }
1085 } else {
1086 /* If it's an output descriptor, they're all supposed
1087 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001088 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001089 vq_err(vq, "Indirect descriptor "
1090 "has out after in: idx %d\n", i);
1091 return -EINVAL;
1092 }
1093 *out_num += ret;
1094 }
1095 } while ((i = next_desc(&desc)) != -1);
1096 return 0;
1097}
1098
1099/* This looks in the virtqueue and for the first available buffer, and converts
1100 * it to an iovec for convenient access. Since descriptors consist of some
1101 * number of output then some number of input descriptors, it's actually two
1102 * iovecs, but we pack them into one and note how many of each there were.
1103 *
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001104 * This function returns the descriptor number found, or vq->num (which is
1105 * never a valid descriptor number) if none was found. A negative code is
1106 * returned on error. */
1107int vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
1108 struct iovec iov[], unsigned int iov_size,
1109 unsigned int *out_num, unsigned int *in_num,
1110 struct vhost_log *log, unsigned int *log_num)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001111{
1112 struct vring_desc desc;
1113 unsigned int i, head, found = 0;
1114 u16 last_avail_idx;
1115 int ret;
1116
1117 /* Check it isn't doing very strange things with descriptor numbers. */
1118 last_avail_idx = vq->last_avail_idx;
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001119 if (unlikely(__get_user(vq->avail_idx, &vq->avail->idx))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001120 vq_err(vq, "Failed to access avail idx at %p\n",
1121 &vq->avail->idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001122 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001123 }
1124
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001125 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001126 vq_err(vq, "Guest moved used index from %u to %u",
1127 last_avail_idx, vq->avail_idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001128 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001129 }
1130
1131 /* If there's nothing new since last we looked, return invalid. */
1132 if (vq->avail_idx == last_avail_idx)
1133 return vq->num;
1134
1135 /* Only get avail ring entries after they have been exposed by guest. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001136 smp_rmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001137
1138 /* Grab the next descriptor number they're advertising, and increment
1139 * the index we've seen. */
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001140 if (unlikely(__get_user(head,
1141 &vq->avail->ring[last_avail_idx % vq->num]))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001142 vq_err(vq, "Failed to read head: idx %d address %p\n",
1143 last_avail_idx,
1144 &vq->avail->ring[last_avail_idx % vq->num]);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001145 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001146 }
1147
1148 /* If their number is silly, that's an error. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001149 if (unlikely(head >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001150 vq_err(vq, "Guest says index %u > %u is available",
1151 head, vq->num);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001152 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001153 }
1154
1155 /* When we start there are none of either input nor output. */
1156 *out_num = *in_num = 0;
1157 if (unlikely(log))
1158 *log_num = 0;
1159
1160 i = head;
1161 do {
1162 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001163 if (unlikely(i >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001164 vq_err(vq, "Desc index is %u > %u, head = %u",
1165 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001166 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001167 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001168 if (unlikely(++found > vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001169 vq_err(vq, "Loop detected: last one at %u "
1170 "vq size %u head %u\n",
1171 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001172 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001173 }
Michael S. Tsirkinfcc042a2011-03-06 13:33:49 +02001174 ret = __copy_from_user(&desc, vq->desc + i, sizeof desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001175 if (unlikely(ret)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001176 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
1177 i, vq->desc + i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001178 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001179 }
1180 if (desc.flags & VRING_DESC_F_INDIRECT) {
1181 ret = get_indirect(dev, vq, iov, iov_size,
1182 out_num, in_num,
1183 log, log_num, &desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001184 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001185 vq_err(vq, "Failure detected "
1186 "in indirect descriptor at idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001187 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001188 }
1189 continue;
1190 }
1191
1192 ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
1193 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001194 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001195 vq_err(vq, "Translation failure %d descriptor idx %d\n",
1196 ret, i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001197 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001198 }
1199 if (desc.flags & VRING_DESC_F_WRITE) {
1200 /* If this is an input descriptor,
1201 * increment that count. */
1202 *in_num += ret;
1203 if (unlikely(log)) {
1204 log[*log_num].addr = desc.addr;
1205 log[*log_num].len = desc.len;
1206 ++*log_num;
1207 }
1208 } else {
1209 /* If it's an output descriptor, they're all supposed
1210 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001211 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001212 vq_err(vq, "Descriptor has out after in: "
1213 "idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001214 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001215 }
1216 *out_num += ret;
1217 }
1218 } while ((i = next_desc(&desc)) != -1);
1219
1220 /* On success, increment avail index. */
1221 vq->last_avail_idx++;
1222 return head;
1223}
1224
1225/* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
David Stevens8dd014a2010-07-27 18:52:21 +03001226void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001227{
David Stevens8dd014a2010-07-27 18:52:21 +03001228 vq->last_avail_idx -= n;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001229}
1230
1231/* After we've used one of their buffers, we tell them about it. We'll then
1232 * want to notify the guest, using eventfd. */
1233int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
1234{
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001235 struct vring_used_elem __user *used;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001236
1237 /* The virtqueue contains a ring of used buffers. Get a pointer to the
1238 * next entry in that used ring. */
1239 used = &vq->used->ring[vq->last_used_idx % vq->num];
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001240 if (__put_user(head, &used->id)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001241 vq_err(vq, "Failed to write used id");
1242 return -EFAULT;
1243 }
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001244 if (__put_user(len, &used->len)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001245 vq_err(vq, "Failed to write used len");
1246 return -EFAULT;
1247 }
1248 /* Make sure buffer is written before we update index. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001249 smp_wmb();
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001250 if (__put_user(vq->last_used_idx + 1, &vq->used->idx)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001251 vq_err(vq, "Failed to increment used idx");
1252 return -EFAULT;
1253 }
1254 if (unlikely(vq->log_used)) {
1255 /* Make sure data is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001256 smp_wmb();
Michael S. Tsirkin86e94242010-02-17 19:11:33 +02001257 /* Log used ring entry write. */
1258 log_write(vq->log_base,
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001259 vq->log_addr +
1260 ((void __user *)used - (void __user *)vq->used),
Michael S. Tsirkin86e94242010-02-17 19:11:33 +02001261 sizeof *used);
1262 /* Log used index update. */
1263 log_write(vq->log_base,
1264 vq->log_addr + offsetof(struct vring_used, idx),
1265 sizeof vq->used->idx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001266 if (vq->log_ctx)
1267 eventfd_signal(vq->log_ctx, 1);
1268 }
1269 vq->last_used_idx++;
1270 return 0;
1271}
1272
David Stevens8dd014a2010-07-27 18:52:21 +03001273static int __vhost_add_used_n(struct vhost_virtqueue *vq,
1274 struct vring_used_elem *heads,
1275 unsigned count)
1276{
1277 struct vring_used_elem __user *used;
1278 int start;
1279
1280 start = vq->last_used_idx % vq->num;
1281 used = vq->used->ring + start;
Michael S. Tsirkindfe5ac52010-09-21 14:18:01 +02001282 if (__copy_to_user(used, heads, count * sizeof *used)) {
David Stevens8dd014a2010-07-27 18:52:21 +03001283 vq_err(vq, "Failed to write used");
1284 return -EFAULT;
1285 }
1286 if (unlikely(vq->log_used)) {
1287 /* Make sure data is seen before log. */
1288 smp_wmb();
1289 /* Log used ring entry write. */
1290 log_write(vq->log_base,
1291 vq->log_addr +
1292 ((void __user *)used - (void __user *)vq->used),
1293 count * sizeof *used);
1294 }
1295 vq->last_used_idx += count;
1296 return 0;
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. */
1301int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
1302 unsigned count)
1303{
1304 int start, n, r;
1305
1306 start = vq->last_used_idx % vq->num;
1307 n = vq->num - start;
1308 if (n < count) {
1309 r = __vhost_add_used_n(vq, heads, n);
1310 if (r < 0)
1311 return r;
1312 heads += n;
1313 count -= n;
1314 }
1315 r = __vhost_add_used_n(vq, heads, count);
1316
1317 /* Make sure buffer is written before we update index. */
1318 smp_wmb();
1319 if (put_user(vq->last_used_idx, &vq->used->idx)) {
1320 vq_err(vq, "Failed to increment used idx");
1321 return -EFAULT;
1322 }
1323 if (unlikely(vq->log_used)) {
1324 /* Log used index update. */
1325 log_write(vq->log_base,
1326 vq->log_addr + offsetof(struct vring_used, idx),
1327 sizeof vq->used->idx);
1328 if (vq->log_ctx)
1329 eventfd_signal(vq->log_ctx, 1);
1330 }
1331 return r;
1332}
1333
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001334/* This actually signals the guest, using eventfd. */
1335void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
1336{
Michael S. Tsirkin0d499352010-05-11 19:44:17 +03001337 __u16 flags;
Krishna Kumard47effe2011-03-01 17:06:37 +05301338
Michael S. Tsirkin0d499352010-05-11 19:44:17 +03001339 /* Flush out used index updates. This is paired
1340 * with the barrier that the Guest executes when enabling
1341 * interrupts. */
1342 smp_mb();
1343
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001344 if (__get_user(flags, &vq->avail->flags)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001345 vq_err(vq, "Failed to get flags");
1346 return;
1347 }
1348
1349 /* If they don't want an interrupt, don't signal, unless empty. */
1350 if ((flags & VRING_AVAIL_F_NO_INTERRUPT) &&
1351 (vq->avail_idx != vq->last_avail_idx ||
1352 !vhost_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY)))
1353 return;
1354
1355 /* Signal the Guest tell them we used something up. */
1356 if (vq->call_ctx)
1357 eventfd_signal(vq->call_ctx, 1);
1358}
1359
1360/* And here's the combo meal deal. Supersize me! */
1361void vhost_add_used_and_signal(struct vhost_dev *dev,
1362 struct vhost_virtqueue *vq,
1363 unsigned int head, int len)
1364{
1365 vhost_add_used(vq, head, len);
1366 vhost_signal(dev, vq);
1367}
1368
David Stevens8dd014a2010-07-27 18:52:21 +03001369/* multi-buffer version of vhost_add_used_and_signal */
1370void vhost_add_used_and_signal_n(struct vhost_dev *dev,
1371 struct vhost_virtqueue *vq,
1372 struct vring_used_elem *heads, unsigned count)
1373{
1374 vhost_add_used_n(vq, heads, count);
1375 vhost_signal(dev, vq);
1376}
1377
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001378/* OK, now we need to know about added descriptors. */
1379bool vhost_enable_notify(struct vhost_virtqueue *vq)
1380{
1381 u16 avail_idx;
1382 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301383
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001384 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
1385 return false;
1386 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
1387 r = put_user(vq->used_flags, &vq->used->flags);
1388 if (r) {
1389 vq_err(vq, "Failed to enable notification at %p: %d\n",
1390 &vq->used->flags, r);
1391 return false;
1392 }
1393 /* They could have slipped one in as we were doing that: make
1394 * sure it's written, then check again. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001395 smp_mb();
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001396 r = __get_user(avail_idx, &vq->avail->idx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001397 if (r) {
1398 vq_err(vq, "Failed to check avail idx at %p: %d\n",
1399 &vq->avail->idx, r);
1400 return false;
1401 }
1402
David Stevens8dd014a2010-07-27 18:52:21 +03001403 return avail_idx != vq->avail_idx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001404}
1405
1406/* We don't need to be notified again. */
1407void vhost_disable_notify(struct vhost_virtqueue *vq)
1408{
1409 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301410
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001411 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
1412 return;
1413 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
1414 r = put_user(vq->used_flags, &vq->used->flags);
1415 if (r)
1416 vq_err(vq, "Failed to enable notification at %p: %d\n",
1417 &vq->used->flags, r);
1418}