blob: ade0568c07a4e9ff3ab2fc56e9dc527ca58bdf72 [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;
44 poll = container_of(pt, struct vhost_poll, table);
45
46 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;
88 mask = file->f_op->poll(file, &poll->table);
89 if (mask)
90 vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
91}
92
93/* Stop polling a file. After this function returns, it becomes safe to drop the
94 * file reference. You must also flush afterwards. */
95void vhost_poll_stop(struct vhost_poll *poll)
96{
97 remove_wait_queue(poll->wqh, &poll->wait);
98}
99
Michael S. Tsirkin0174b0c2011-01-10 10:03:20 +0200100static bool vhost_work_seq_done(struct vhost_dev *dev, struct vhost_work *work,
101 unsigned seq)
102{
103 int left;
104 spin_lock_irq(&dev->work_lock);
105 left = seq - work->done_seq;
106 spin_unlock_irq(&dev->work_lock);
107 return left <= 0;
108}
109
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300110static void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000111{
Tejun Heoc23f3442010-06-02 20:40:00 +0200112 unsigned seq;
Tejun Heoc23f3442010-06-02 20:40:00 +0200113 int flushing;
114
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300115 spin_lock_irq(&dev->work_lock);
Tejun Heoc23f3442010-06-02 20:40:00 +0200116 seq = work->queue_seq;
117 work->flushing++;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300118 spin_unlock_irq(&dev->work_lock);
Michael S. Tsirkin0174b0c2011-01-10 10:03:20 +0200119 wait_event(work->done, vhost_work_seq_done(dev, work, seq));
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300120 spin_lock_irq(&dev->work_lock);
Tejun Heoc23f3442010-06-02 20:40:00 +0200121 flushing = --work->flushing;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300122 spin_unlock_irq(&dev->work_lock);
Tejun Heoc23f3442010-06-02 20:40:00 +0200123 BUG_ON(flushing < 0);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000124}
125
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300126/* Flush any work that has been scheduled. When calling this, don't hold any
127 * locks that are also used by the callback. */
128void vhost_poll_flush(struct vhost_poll *poll)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000129{
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300130 vhost_work_flush(poll->dev, &poll->work);
131}
132
133static inline void vhost_work_queue(struct vhost_dev *dev,
134 struct vhost_work *work)
135{
Tejun Heoc23f3442010-06-02 20:40:00 +0200136 unsigned long flags;
137
138 spin_lock_irqsave(&dev->work_lock, flags);
139 if (list_empty(&work->node)) {
140 list_add_tail(&work->node, &dev->work_list);
141 work->queue_seq++;
142 wake_up_process(dev->worker);
143 }
144 spin_unlock_irqrestore(&dev->work_lock, flags);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000145}
146
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300147void vhost_poll_queue(struct vhost_poll *poll)
148{
149 vhost_work_queue(poll->dev, &poll->work);
150}
151
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000152static void vhost_vq_reset(struct vhost_dev *dev,
153 struct vhost_virtqueue *vq)
154{
155 vq->num = 1;
156 vq->desc = NULL;
157 vq->avail = NULL;
158 vq->used = NULL;
159 vq->last_avail_idx = 0;
160 vq->avail_idx = 0;
161 vq->last_used_idx = 0;
162 vq->used_flags = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000163 vq->log_used = false;
164 vq->log_addr = -1ull;
David Stevens8dd014a2010-07-27 18:52:21 +0300165 vq->vhost_hlen = 0;
166 vq->sock_hlen = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000167 vq->private_data = NULL;
168 vq->log_base = NULL;
169 vq->error_ctx = NULL;
170 vq->error = NULL;
171 vq->kick = NULL;
172 vq->call_ctx = NULL;
173 vq->call = NULL;
Michael S. Tsirkin73a99f02010-02-23 11:23:45 +0200174 vq->log_ctx = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000175}
176
Tejun Heoc23f3442010-06-02 20:40:00 +0200177static int vhost_worker(void *data)
178{
179 struct vhost_dev *dev = data;
180 struct vhost_work *work = NULL;
181 unsigned uninitialized_var(seq);
182
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200183 use_mm(dev->mm);
184
Tejun Heoc23f3442010-06-02 20:40:00 +0200185 for (;;) {
186 /* mb paired w/ kthread_stop */
187 set_current_state(TASK_INTERRUPTIBLE);
188
189 spin_lock_irq(&dev->work_lock);
190 if (work) {
191 work->done_seq = seq;
192 if (work->flushing)
193 wake_up_all(&work->done);
194 }
195
196 if (kthread_should_stop()) {
197 spin_unlock_irq(&dev->work_lock);
198 __set_current_state(TASK_RUNNING);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200199 break;
Tejun Heoc23f3442010-06-02 20:40:00 +0200200 }
201 if (!list_empty(&dev->work_list)) {
202 work = list_first_entry(&dev->work_list,
203 struct vhost_work, node);
204 list_del_init(&work->node);
205 seq = work->queue_seq;
206 } else
207 work = NULL;
208 spin_unlock_irq(&dev->work_lock);
209
210 if (work) {
211 __set_current_state(TASK_RUNNING);
212 work->fn(work);
213 } else
214 schedule();
215
216 }
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200217 unuse_mm(dev->mm);
218 return 0;
Tejun Heoc23f3442010-06-02 20:40:00 +0200219}
220
Jason Wange0e9b402010-09-14 23:53:05 +0800221/* Helper to allocate iovec buffers for all vqs. */
222static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
223{
224 int i;
225 for (i = 0; i < dev->nvqs; ++i) {
226 dev->vqs[i].indirect = kmalloc(sizeof *dev->vqs[i].indirect *
227 UIO_MAXIOV, GFP_KERNEL);
228 dev->vqs[i].log = kmalloc(sizeof *dev->vqs[i].log * UIO_MAXIOV,
229 GFP_KERNEL);
230 dev->vqs[i].heads = kmalloc(sizeof *dev->vqs[i].heads *
231 UIO_MAXIOV, GFP_KERNEL);
232
233 if (!dev->vqs[i].indirect || !dev->vqs[i].log ||
234 !dev->vqs[i].heads)
235 goto err_nomem;
236 }
237 return 0;
238err_nomem:
239 for (; i >= 0; --i) {
240 kfree(dev->vqs[i].indirect);
241 kfree(dev->vqs[i].log);
242 kfree(dev->vqs[i].heads);
243 }
244 return -ENOMEM;
245}
246
247static void vhost_dev_free_iovecs(struct vhost_dev *dev)
248{
249 int i;
250 for (i = 0; i < dev->nvqs; ++i) {
251 kfree(dev->vqs[i].indirect);
252 dev->vqs[i].indirect = NULL;
253 kfree(dev->vqs[i].log);
254 dev->vqs[i].log = NULL;
255 kfree(dev->vqs[i].heads);
256 dev->vqs[i].heads = NULL;
257 }
258}
259
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000260long vhost_dev_init(struct vhost_dev *dev,
261 struct vhost_virtqueue *vqs, int nvqs)
262{
263 int i;
Tejun Heoc23f3442010-06-02 20:40:00 +0200264
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000265 dev->vqs = vqs;
266 dev->nvqs = nvqs;
267 mutex_init(&dev->mutex);
268 dev->log_ctx = NULL;
269 dev->log_file = NULL;
270 dev->memory = NULL;
271 dev->mm = NULL;
Tejun Heoc23f3442010-06-02 20:40:00 +0200272 spin_lock_init(&dev->work_lock);
273 INIT_LIST_HEAD(&dev->work_list);
274 dev->worker = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000275
276 for (i = 0; i < dev->nvqs; ++i) {
Jason Wange0e9b402010-09-14 23:53:05 +0800277 dev->vqs[i].log = NULL;
278 dev->vqs[i].indirect = NULL;
279 dev->vqs[i].heads = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000280 dev->vqs[i].dev = dev;
281 mutex_init(&dev->vqs[i].mutex);
282 vhost_vq_reset(dev, dev->vqs + i);
283 if (dev->vqs[i].handle_kick)
284 vhost_poll_init(&dev->vqs[i].poll,
Tejun Heoc23f3442010-06-02 20:40:00 +0200285 dev->vqs[i].handle_kick, POLLIN, dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000286 }
Tejun Heoc23f3442010-06-02 20:40:00 +0200287
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000288 return 0;
289}
290
291/* Caller should have device mutex */
292long vhost_dev_check_owner(struct vhost_dev *dev)
293{
294 /* Are you the owner? If not, I don't think you mean to do that */
295 return dev->mm == current->mm ? 0 : -EPERM;
296}
297
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300298struct vhost_attach_cgroups_struct {
299 struct vhost_work work;
300 struct task_struct *owner;
301 int ret;
302};
303
304static void vhost_attach_cgroups_work(struct vhost_work *work)
305{
306 struct vhost_attach_cgroups_struct *s;
307 s = container_of(work, struct vhost_attach_cgroups_struct, work);
308 s->ret = cgroup_attach_task_all(s->owner, current);
309}
310
311static int vhost_attach_cgroups(struct vhost_dev *dev)
312{
313 struct vhost_attach_cgroups_struct attach;
314 attach.owner = current;
315 vhost_work_init(&attach.work, vhost_attach_cgroups_work);
316 vhost_work_queue(dev, &attach.work);
317 vhost_work_flush(dev, &attach.work);
318 return attach.ret;
319}
320
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000321/* Caller should have device mutex */
322static long vhost_dev_set_owner(struct vhost_dev *dev)
323{
Tejun Heoc23f3442010-06-02 20:40:00 +0200324 struct task_struct *worker;
325 int err;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000326 /* Is there an owner already? */
Tejun Heoc23f3442010-06-02 20:40:00 +0200327 if (dev->mm) {
328 err = -EBUSY;
329 goto err_mm;
330 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000331 /* No owner, become one */
332 dev->mm = get_task_mm(current);
Tejun Heoc23f3442010-06-02 20:40:00 +0200333 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
334 if (IS_ERR(worker)) {
335 err = PTR_ERR(worker);
336 goto err_worker;
337 }
338
339 dev->worker = worker;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300340 wake_up_process(worker); /* avoid contributing to loadavg */
341
342 err = vhost_attach_cgroups(dev);
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300343 if (err)
344 goto err_cgroup;
Tejun Heoc23f3442010-06-02 20:40:00 +0200345
Jason Wange0e9b402010-09-14 23:53:05 +0800346 err = vhost_dev_alloc_iovecs(dev);
347 if (err)
348 goto err_cgroup;
349
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000350 return 0;
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300351err_cgroup:
352 kthread_stop(worker);
Michael S. Tsirkin615cc222010-09-02 14:16:36 +0300353 dev->worker = NULL;
Tejun Heoc23f3442010-06-02 20:40:00 +0200354err_worker:
355 if (dev->mm)
356 mmput(dev->mm);
357 dev->mm = NULL;
358err_mm:
359 return err;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000360}
361
362/* Caller should have device mutex */
363long vhost_dev_reset_owner(struct vhost_dev *dev)
364{
365 struct vhost_memory *memory;
366
367 /* Restore memory to default empty mapping. */
368 memory = kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
369 if (!memory)
370 return -ENOMEM;
371
372 vhost_dev_cleanup(dev);
373
374 memory->nregions = 0;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100375 RCU_INIT_POINTER(dev->memory, memory);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000376 return 0;
377}
378
379/* Caller should have device mutex */
380void vhost_dev_cleanup(struct vhost_dev *dev)
381{
382 int i;
383 for (i = 0; i < dev->nvqs; ++i) {
384 if (dev->vqs[i].kick && dev->vqs[i].handle_kick) {
385 vhost_poll_stop(&dev->vqs[i].poll);
386 vhost_poll_flush(&dev->vqs[i].poll);
387 }
388 if (dev->vqs[i].error_ctx)
389 eventfd_ctx_put(dev->vqs[i].error_ctx);
390 if (dev->vqs[i].error)
391 fput(dev->vqs[i].error);
392 if (dev->vqs[i].kick)
393 fput(dev->vqs[i].kick);
394 if (dev->vqs[i].call_ctx)
395 eventfd_ctx_put(dev->vqs[i].call_ctx);
396 if (dev->vqs[i].call)
397 fput(dev->vqs[i].call);
398 vhost_vq_reset(dev, dev->vqs + i);
399 }
Jason Wange0e9b402010-09-14 23:53:05 +0800400 vhost_dev_free_iovecs(dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000401 if (dev->log_ctx)
402 eventfd_ctx_put(dev->log_ctx);
403 dev->log_ctx = NULL;
404 if (dev->log_file)
405 fput(dev->log_file);
406 dev->log_file = NULL;
407 /* No one will access memory at this point */
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100408 kfree(rcu_dereference_protected(dev->memory,
409 lockdep_is_held(&dev->mutex)));
410 RCU_INIT_POINTER(dev->memory, NULL);
Tejun Heoc23f3442010-06-02 20:40:00 +0200411 WARN_ON(!list_empty(&dev->work_list));
Eric Dumazet78b620c2010-08-31 02:05:57 +0000412 if (dev->worker) {
413 kthread_stop(dev->worker);
414 dev->worker = NULL;
415 }
Michael S. Tsirkin533a19b2010-10-06 15:34:38 +0200416 if (dev->mm)
417 mmput(dev->mm);
418 dev->mm = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000419}
420
421static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
422{
423 u64 a = addr / VHOST_PAGE_SIZE / 8;
424 /* Make sure 64 bit math will not overflow. */
425 if (a > ULONG_MAX - (unsigned long)log_base ||
426 a + (unsigned long)log_base > ULONG_MAX)
Dan Carpenter6d97e552010-10-11 19:24:19 +0200427 return 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000428
429 return access_ok(VERIFY_WRITE, log_base + a,
430 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
431}
432
433/* Caller should have vq mutex and device mutex. */
434static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
435 int log_all)
436{
437 int i;
Jeff Dike179b2842010-04-07 09:59:10 -0400438
Michael S. Tsirkinf8322fb2010-05-27 12:28:03 +0300439 if (!mem)
440 return 0;
Jeff Dike179b2842010-04-07 09:59:10 -0400441
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000442 for (i = 0; i < mem->nregions; ++i) {
443 struct vhost_memory_region *m = mem->regions + i;
444 unsigned long a = m->userspace_addr;
445 if (m->memory_size > ULONG_MAX)
446 return 0;
447 else if (!access_ok(VERIFY_WRITE, (void __user *)a,
448 m->memory_size))
449 return 0;
450 else if (log_all && !log_access_ok(log_base,
451 m->guest_phys_addr,
452 m->memory_size))
453 return 0;
454 }
455 return 1;
456}
457
458/* Can we switch to this memory table? */
459/* Caller should have device mutex but not vq mutex */
460static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
461 int log_all)
462{
463 int i;
464 for (i = 0; i < d->nvqs; ++i) {
465 int ok;
466 mutex_lock(&d->vqs[i].mutex);
467 /* If ring is inactive, will check when it's enabled. */
468 if (d->vqs[i].private_data)
469 ok = vq_memory_access_ok(d->vqs[i].log_base, mem,
470 log_all);
471 else
472 ok = 1;
473 mutex_unlock(&d->vqs[i].mutex);
474 if (!ok)
475 return 0;
476 }
477 return 1;
478}
479
480static int vq_access_ok(unsigned int num,
481 struct vring_desc __user *desc,
482 struct vring_avail __user *avail,
483 struct vring_used __user *used)
484{
485 return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
486 access_ok(VERIFY_READ, avail,
487 sizeof *avail + num * sizeof *avail->ring) &&
488 access_ok(VERIFY_WRITE, used,
489 sizeof *used + num * sizeof *used->ring);
490}
491
492/* Can we log writes? */
493/* Caller should have device mutex but not vq mutex */
494int vhost_log_access_ok(struct vhost_dev *dev)
495{
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100496 struct vhost_memory *mp;
497
498 mp = rcu_dereference_protected(dev->memory,
499 lockdep_is_held(&dev->mutex));
500 return memory_access_ok(dev, mp, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000501}
502
503/* Verify access for write logging. */
504/* Caller should have vq mutex and device mutex */
505static int vq_log_access_ok(struct vhost_virtqueue *vq, void __user *log_base)
506{
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100507 struct vhost_memory *mp;
508
509 mp = rcu_dereference_protected(vq->dev->memory,
510 lockdep_is_held(&vq->mutex));
511 return vq_memory_access_ok(log_base, mp,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000512 vhost_has_feature(vq->dev, VHOST_F_LOG_ALL)) &&
513 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
514 sizeof *vq->used +
515 vq->num * sizeof *vq->used->ring));
516}
517
518/* Can we start vq? */
519/* Caller should have vq mutex and device mutex */
520int vhost_vq_access_ok(struct vhost_virtqueue *vq)
521{
522 return vq_access_ok(vq->num, vq->desc, vq->avail, vq->used) &&
523 vq_log_access_ok(vq, vq->log_base);
524}
525
526static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
527{
528 struct vhost_memory mem, *newmem, *oldmem;
529 unsigned long size = offsetof(struct vhost_memory, regions);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900530 if (copy_from_user(&mem, m, size))
531 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000532 if (mem.padding)
533 return -EOPNOTSUPP;
534 if (mem.nregions > VHOST_MEMORY_MAX_NREGIONS)
535 return -E2BIG;
536 newmem = kmalloc(size + mem.nregions * sizeof *m->regions, GFP_KERNEL);
537 if (!newmem)
538 return -ENOMEM;
539
540 memcpy(newmem, &mem, size);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900541 if (copy_from_user(newmem->regions, m->regions,
542 mem.nregions * sizeof *m->regions)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000543 kfree(newmem);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900544 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000545 }
546
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +0900547 if (!memory_access_ok(d, newmem, vhost_has_feature(d, VHOST_F_LOG_ALL))) {
548 kfree(newmem);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000549 return -EFAULT;
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +0900550 }
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100551 oldmem = rcu_dereference_protected(d->memory,
552 lockdep_is_held(&d->mutex));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000553 rcu_assign_pointer(d->memory, newmem);
554 synchronize_rcu();
555 kfree(oldmem);
556 return 0;
557}
558
559static int init_used(struct vhost_virtqueue *vq,
560 struct vring_used __user *used)
561{
562 int r = put_user(vq->used_flags, &used->flags);
563 if (r)
564 return r;
565 return get_user(vq->last_used_idx, &used->idx);
566}
567
568static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp)
569{
570 struct file *eventfp, *filep = NULL,
571 *pollstart = NULL, *pollstop = NULL;
572 struct eventfd_ctx *ctx = NULL;
573 u32 __user *idxp = argp;
574 struct vhost_virtqueue *vq;
575 struct vhost_vring_state s;
576 struct vhost_vring_file f;
577 struct vhost_vring_addr a;
578 u32 idx;
579 long r;
580
581 r = get_user(idx, idxp);
582 if (r < 0)
583 return r;
Krishna Kumar0f3d9a12010-05-25 11:10:36 +0530584 if (idx >= d->nvqs)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000585 return -ENOBUFS;
586
587 vq = d->vqs + idx;
588
589 mutex_lock(&vq->mutex);
590
591 switch (ioctl) {
592 case VHOST_SET_VRING_NUM:
593 /* Resizing ring with an active backend?
594 * You don't want to do that. */
595 if (vq->private_data) {
596 r = -EBUSY;
597 break;
598 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900599 if (copy_from_user(&s, argp, sizeof s)) {
600 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000601 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900602 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000603 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
604 r = -EINVAL;
605 break;
606 }
607 vq->num = s.num;
608 break;
609 case VHOST_SET_VRING_BASE:
610 /* Moving base with an active backend?
611 * You don't want to do that. */
612 if (vq->private_data) {
613 r = -EBUSY;
614 break;
615 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900616 if (copy_from_user(&s, argp, sizeof s)) {
617 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000618 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900619 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000620 if (s.num > 0xffff) {
621 r = -EINVAL;
622 break;
623 }
624 vq->last_avail_idx = s.num;
625 /* Forget the cached index value. */
626 vq->avail_idx = vq->last_avail_idx;
627 break;
628 case VHOST_GET_VRING_BASE:
629 s.index = idx;
630 s.num = vq->last_avail_idx;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900631 if (copy_to_user(argp, &s, sizeof s))
632 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000633 break;
634 case VHOST_SET_VRING_ADDR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900635 if (copy_from_user(&a, argp, sizeof a)) {
636 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000637 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900638 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000639 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
640 r = -EOPNOTSUPP;
641 break;
642 }
643 /* For 32bit, verify that the top 32bits of the user
644 data are set to zero. */
645 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
646 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
647 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
648 r = -EFAULT;
649 break;
650 }
651 if ((a.avail_user_addr & (sizeof *vq->avail->ring - 1)) ||
652 (a.used_user_addr & (sizeof *vq->used->ring - 1)) ||
653 (a.log_guest_addr & (sizeof *vq->used->ring - 1))) {
654 r = -EINVAL;
655 break;
656 }
657
658 /* We only verify access here if backend is configured.
659 * If it is not, we don't as size might not have been setup.
660 * We will verify when backend is configured. */
661 if (vq->private_data) {
662 if (!vq_access_ok(vq->num,
663 (void __user *)(unsigned long)a.desc_user_addr,
664 (void __user *)(unsigned long)a.avail_user_addr,
665 (void __user *)(unsigned long)a.used_user_addr)) {
666 r = -EINVAL;
667 break;
668 }
669
670 /* Also validate log access for used ring if enabled. */
671 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
672 !log_access_ok(vq->log_base, a.log_guest_addr,
673 sizeof *vq->used +
674 vq->num * sizeof *vq->used->ring)) {
675 r = -EINVAL;
676 break;
677 }
678 }
679
680 r = init_used(vq, (struct vring_used __user *)(unsigned long)
681 a.used_user_addr);
682 if (r)
683 break;
684 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
685 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
686 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
687 vq->log_addr = a.log_guest_addr;
688 vq->used = (void __user *)(unsigned long)a.used_user_addr;
689 break;
690 case VHOST_SET_VRING_KICK:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900691 if (copy_from_user(&f, argp, sizeof f)) {
692 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000693 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900694 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000695 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200696 if (IS_ERR(eventfp)) {
697 r = PTR_ERR(eventfp);
698 break;
699 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000700 if (eventfp != vq->kick) {
701 pollstop = filep = vq->kick;
702 pollstart = vq->kick = eventfp;
703 } else
704 filep = eventfp;
705 break;
706 case VHOST_SET_VRING_CALL:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900707 if (copy_from_user(&f, argp, sizeof f)) {
708 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000709 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900710 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000711 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200712 if (IS_ERR(eventfp)) {
713 r = PTR_ERR(eventfp);
714 break;
715 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000716 if (eventfp != vq->call) {
717 filep = vq->call;
718 ctx = vq->call_ctx;
719 vq->call = eventfp;
720 vq->call_ctx = eventfp ?
721 eventfd_ctx_fileget(eventfp) : NULL;
722 } else
723 filep = eventfp;
724 break;
725 case VHOST_SET_VRING_ERR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900726 if (copy_from_user(&f, argp, sizeof f)) {
727 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000728 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900729 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000730 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200731 if (IS_ERR(eventfp)) {
732 r = PTR_ERR(eventfp);
733 break;
734 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000735 if (eventfp != vq->error) {
736 filep = vq->error;
737 vq->error = eventfp;
738 ctx = vq->error_ctx;
739 vq->error_ctx = eventfp ?
740 eventfd_ctx_fileget(eventfp) : NULL;
741 } else
742 filep = eventfp;
743 break;
744 default:
745 r = -ENOIOCTLCMD;
746 }
747
748 if (pollstop && vq->handle_kick)
749 vhost_poll_stop(&vq->poll);
750
751 if (ctx)
752 eventfd_ctx_put(ctx);
753 if (filep)
754 fput(filep);
755
756 if (pollstart && vq->handle_kick)
757 vhost_poll_start(&vq->poll, vq->kick);
758
759 mutex_unlock(&vq->mutex);
760
761 if (pollstop && vq->handle_kick)
762 vhost_poll_flush(&vq->poll);
763 return r;
764}
765
766/* Caller must have device mutex */
767long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, unsigned long arg)
768{
769 void __user *argp = (void __user *)arg;
770 struct file *eventfp, *filep = NULL;
771 struct eventfd_ctx *ctx = NULL;
772 u64 p;
773 long r;
774 int i, fd;
775
776 /* If you are not the owner, you can become one */
777 if (ioctl == VHOST_SET_OWNER) {
778 r = vhost_dev_set_owner(d);
779 goto done;
780 }
781
782 /* You must be the owner to do anything else */
783 r = vhost_dev_check_owner(d);
784 if (r)
785 goto done;
786
787 switch (ioctl) {
788 case VHOST_SET_MEM_TABLE:
789 r = vhost_set_memory(d, argp);
790 break;
791 case VHOST_SET_LOG_BASE:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900792 if (copy_from_user(&p, argp, sizeof p)) {
793 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000794 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900795 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000796 if ((u64)(unsigned long)p != p) {
797 r = -EFAULT;
798 break;
799 }
800 for (i = 0; i < d->nvqs; ++i) {
801 struct vhost_virtqueue *vq;
802 void __user *base = (void __user *)(unsigned long)p;
803 vq = d->vqs + i;
804 mutex_lock(&vq->mutex);
805 /* If ring is inactive, will check when it's enabled. */
806 if (vq->private_data && !vq_log_access_ok(vq, base))
807 r = -EFAULT;
808 else
809 vq->log_base = base;
810 mutex_unlock(&vq->mutex);
811 }
812 break;
813 case VHOST_SET_LOG_FD:
814 r = get_user(fd, (int __user *)argp);
815 if (r < 0)
816 break;
817 eventfp = fd == -1 ? NULL : eventfd_fget(fd);
818 if (IS_ERR(eventfp)) {
819 r = PTR_ERR(eventfp);
820 break;
821 }
822 if (eventfp != d->log_file) {
823 filep = d->log_file;
824 ctx = d->log_ctx;
825 d->log_ctx = eventfp ?
826 eventfd_ctx_fileget(eventfp) : NULL;
827 } else
828 filep = eventfp;
829 for (i = 0; i < d->nvqs; ++i) {
830 mutex_lock(&d->vqs[i].mutex);
831 d->vqs[i].log_ctx = d->log_ctx;
832 mutex_unlock(&d->vqs[i].mutex);
833 }
834 if (ctx)
835 eventfd_ctx_put(ctx);
836 if (filep)
837 fput(filep);
838 break;
839 default:
840 r = vhost_set_vring(d, ioctl, argp);
841 break;
842 }
843done:
844 return r;
845}
846
847static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
848 __u64 addr, __u32 len)
849{
850 struct vhost_memory_region *reg;
851 int i;
852 /* linear search is not brilliant, but we really have on the order of 6
853 * regions in practice */
854 for (i = 0; i < mem->nregions; ++i) {
855 reg = mem->regions + i;
856 if (reg->guest_phys_addr <= addr &&
857 reg->guest_phys_addr + reg->memory_size - 1 >= addr)
858 return reg;
859 }
860 return NULL;
861}
862
863/* TODO: This is really inefficient. We need something like get_user()
864 * (instruction directly accesses the data, with an exception table entry
865 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
866 */
867static int set_bit_to_user(int nr, void __user *addr)
868{
869 unsigned long log = (unsigned long)addr;
870 struct page *page;
871 void *base;
872 int bit = nr + (log % PAGE_SIZE) * 8;
873 int r;
874 r = get_user_pages_fast(log, 1, 1, &page);
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +0200875 if (r < 0)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000876 return r;
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +0200877 BUG_ON(r != 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000878 base = kmap_atomic(page, KM_USER0);
879 set_bit(bit, base);
880 kunmap_atomic(base, KM_USER0);
881 set_page_dirty_lock(page);
882 put_page(page);
883 return 0;
884}
885
886static int log_write(void __user *log_base,
887 u64 write_address, u64 write_length)
888{
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +0200889 u64 write_page = write_address / VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000890 int r;
891 if (!write_length)
892 return 0;
Michael S. Tsirkin3bf9be42010-11-29 10:19:07 +0200893 write_length += write_address % VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000894 for (;;) {
895 u64 base = (u64)(unsigned long)log_base;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +0200896 u64 log = base + write_page / 8;
897 int bit = write_page % 8;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000898 if ((u64)(unsigned long)log != log)
899 return -EFAULT;
900 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
901 if (r < 0)
902 return r;
903 if (write_length <= VHOST_PAGE_SIZE)
904 break;
905 write_length -= VHOST_PAGE_SIZE;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +0200906 write_page += 1;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000907 }
908 return r;
909}
910
911int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
912 unsigned int log_num, u64 len)
913{
914 int i, r;
915
916 /* Make sure data written is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +0000917 smp_wmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000918 for (i = 0; i < log_num; ++i) {
919 u64 l = min(log[i].len, len);
920 r = log_write(vq->log_base, log[i].addr, l);
921 if (r < 0)
922 return r;
923 len -= l;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +0200924 if (!len) {
925 if (vq->log_ctx)
926 eventfd_signal(vq->log_ctx, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000927 return 0;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +0200928 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000929 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000930 /* Length written exceeds what we have stored. This is a bug. */
931 BUG();
932 return 0;
933}
934
Christoph Hellwiga8d37822010-04-13 14:11:25 -0400935static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
936 struct iovec iov[], int iov_size)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000937{
938 const struct vhost_memory_region *reg;
939 struct vhost_memory *mem;
940 struct iovec *_iov;
941 u64 s = 0;
942 int ret = 0;
943
944 rcu_read_lock();
945
946 mem = rcu_dereference(dev->memory);
947 while ((u64)len > s) {
948 u64 size;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300949 if (unlikely(ret >= iov_size)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000950 ret = -ENOBUFS;
951 break;
952 }
953 reg = find_region(mem, addr, len);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300954 if (unlikely(!reg)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000955 ret = -EFAULT;
956 break;
957 }
958 _iov = iov + ret;
959 size = reg->memory_size - addr + reg->guest_phys_addr;
960 _iov->iov_len = min((u64)len, size);
Christoph Hellwiga8d37822010-04-13 14:11:25 -0400961 _iov->iov_base = (void __user *)(unsigned long)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000962 (reg->userspace_addr + addr - reg->guest_phys_addr);
963 s += size;
964 addr += size;
965 ++ret;
966 }
967
968 rcu_read_unlock();
969 return ret;
970}
971
972/* Each buffer in the virtqueues is actually a chain of descriptors. This
973 * function returns the next descriptor in the chain,
974 * or -1U if we're at the end. */
975static unsigned next_desc(struct vring_desc *desc)
976{
977 unsigned int next;
978
979 /* If this descriptor says it doesn't chain, we're done. */
980 if (!(desc->flags & VRING_DESC_F_NEXT))
981 return -1U;
982
983 /* Check they're not leading us off end of descriptors. */
984 next = desc->next;
985 /* Make sure compiler knows to grab that: we don't want it changing! */
986 /* We will use the result as an index in an array, so most
987 * architectures only need a compiler barrier here. */
988 read_barrier_depends();
989
990 return next;
991}
992
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300993static int get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
994 struct iovec iov[], unsigned int iov_size,
995 unsigned int *out_num, unsigned int *in_num,
996 struct vhost_log *log, unsigned int *log_num,
997 struct vring_desc *indirect)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000998{
999 struct vring_desc desc;
1000 unsigned int i = 0, count, found = 0;
1001 int ret;
1002
1003 /* Sanity check */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001004 if (unlikely(indirect->len % sizeof desc)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001005 vq_err(vq, "Invalid length in indirect descriptor: "
1006 "len 0x%llx not multiple of 0x%zx\n",
1007 (unsigned long long)indirect->len,
1008 sizeof desc);
1009 return -EINVAL;
1010 }
1011
1012 ret = translate_desc(dev, indirect->addr, indirect->len, vq->indirect,
Jason Wange0e9b402010-09-14 23:53:05 +08001013 UIO_MAXIOV);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001014 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001015 vq_err(vq, "Translation failure %d in indirect.\n", ret);
1016 return ret;
1017 }
1018
1019 /* We will use the result as an address to read from, so most
1020 * architectures only need a compiler barrier here. */
1021 read_barrier_depends();
1022
1023 count = indirect->len / sizeof desc;
1024 /* Buffers are chained via a 16 bit next field, so
1025 * we can have at most 2^16 of these. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001026 if (unlikely(count > USHRT_MAX + 1)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001027 vq_err(vq, "Indirect buffer length too big: %d\n",
1028 indirect->len);
1029 return -E2BIG;
1030 }
1031
1032 do {
1033 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001034 if (unlikely(++found > count)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001035 vq_err(vq, "Loop detected: last one at %u "
1036 "indirect size %u\n",
1037 i, count);
1038 return -EINVAL;
1039 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001040 if (unlikely(memcpy_fromiovec((unsigned char *)&desc, vq->indirect,
1041 sizeof desc))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001042 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
1043 i, (size_t)indirect->addr + i * sizeof desc);
1044 return -EINVAL;
1045 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001046 if (unlikely(desc.flags & VRING_DESC_F_INDIRECT)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001047 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
1048 i, (size_t)indirect->addr + i * sizeof desc);
1049 return -EINVAL;
1050 }
1051
1052 ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
1053 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001054 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001055 vq_err(vq, "Translation failure %d indirect idx %d\n",
1056 ret, i);
1057 return ret;
1058 }
1059 /* If this is an input descriptor, increment that count. */
1060 if (desc.flags & VRING_DESC_F_WRITE) {
1061 *in_num += ret;
1062 if (unlikely(log)) {
1063 log[*log_num].addr = desc.addr;
1064 log[*log_num].len = desc.len;
1065 ++*log_num;
1066 }
1067 } else {
1068 /* If it's an output descriptor, they're all supposed
1069 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001070 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001071 vq_err(vq, "Indirect descriptor "
1072 "has out after in: idx %d\n", i);
1073 return -EINVAL;
1074 }
1075 *out_num += ret;
1076 }
1077 } while ((i = next_desc(&desc)) != -1);
1078 return 0;
1079}
1080
1081/* This looks in the virtqueue and for the first available buffer, and converts
1082 * it to an iovec for convenient access. Since descriptors consist of some
1083 * number of output then some number of input descriptors, it's actually two
1084 * iovecs, but we pack them into one and note how many of each there were.
1085 *
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001086 * This function returns the descriptor number found, or vq->num (which is
1087 * never a valid descriptor number) if none was found. A negative code is
1088 * returned on error. */
1089int vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
1090 struct iovec iov[], unsigned int iov_size,
1091 unsigned int *out_num, unsigned int *in_num,
1092 struct vhost_log *log, unsigned int *log_num)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001093{
1094 struct vring_desc desc;
1095 unsigned int i, head, found = 0;
1096 u16 last_avail_idx;
1097 int ret;
1098
1099 /* Check it isn't doing very strange things with descriptor numbers. */
1100 last_avail_idx = vq->last_avail_idx;
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001101 if (unlikely(__get_user(vq->avail_idx, &vq->avail->idx))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001102 vq_err(vq, "Failed to access avail idx at %p\n",
1103 &vq->avail->idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001104 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001105 }
1106
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001107 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001108 vq_err(vq, "Guest moved used index from %u to %u",
1109 last_avail_idx, vq->avail_idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001110 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001111 }
1112
1113 /* If there's nothing new since last we looked, return invalid. */
1114 if (vq->avail_idx == last_avail_idx)
1115 return vq->num;
1116
1117 /* Only get avail ring entries after they have been exposed by guest. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001118 smp_rmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001119
1120 /* Grab the next descriptor number they're advertising, and increment
1121 * the index we've seen. */
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001122 if (unlikely(__get_user(head,
1123 &vq->avail->ring[last_avail_idx % vq->num]))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001124 vq_err(vq, "Failed to read head: idx %d address %p\n",
1125 last_avail_idx,
1126 &vq->avail->ring[last_avail_idx % vq->num]);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001127 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001128 }
1129
1130 /* If their number is silly, that's an error. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001131 if (unlikely(head >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001132 vq_err(vq, "Guest says index %u > %u is available",
1133 head, vq->num);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001134 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001135 }
1136
1137 /* When we start there are none of either input nor output. */
1138 *out_num = *in_num = 0;
1139 if (unlikely(log))
1140 *log_num = 0;
1141
1142 i = head;
1143 do {
1144 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001145 if (unlikely(i >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001146 vq_err(vq, "Desc index is %u > %u, head = %u",
1147 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001148 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001149 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001150 if (unlikely(++found > vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001151 vq_err(vq, "Loop detected: last one at %u "
1152 "vq size %u head %u\n",
1153 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001154 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001155 }
1156 ret = copy_from_user(&desc, vq->desc + i, sizeof desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001157 if (unlikely(ret)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001158 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
1159 i, vq->desc + i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001160 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001161 }
1162 if (desc.flags & VRING_DESC_F_INDIRECT) {
1163 ret = get_indirect(dev, vq, iov, iov_size,
1164 out_num, in_num,
1165 log, log_num, &desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001166 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001167 vq_err(vq, "Failure detected "
1168 "in indirect descriptor at idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001169 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001170 }
1171 continue;
1172 }
1173
1174 ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
1175 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001176 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001177 vq_err(vq, "Translation failure %d descriptor idx %d\n",
1178 ret, i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001179 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001180 }
1181 if (desc.flags & VRING_DESC_F_WRITE) {
1182 /* If this is an input descriptor,
1183 * increment that count. */
1184 *in_num += ret;
1185 if (unlikely(log)) {
1186 log[*log_num].addr = desc.addr;
1187 log[*log_num].len = desc.len;
1188 ++*log_num;
1189 }
1190 } else {
1191 /* If it's an output descriptor, they're all supposed
1192 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001193 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001194 vq_err(vq, "Descriptor has out after in: "
1195 "idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001196 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001197 }
1198 *out_num += ret;
1199 }
1200 } while ((i = next_desc(&desc)) != -1);
1201
1202 /* On success, increment avail index. */
1203 vq->last_avail_idx++;
1204 return head;
1205}
1206
1207/* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
David Stevens8dd014a2010-07-27 18:52:21 +03001208void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001209{
David Stevens8dd014a2010-07-27 18:52:21 +03001210 vq->last_avail_idx -= n;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001211}
1212
1213/* After we've used one of their buffers, we tell them about it. We'll then
1214 * want to notify the guest, using eventfd. */
1215int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
1216{
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001217 struct vring_used_elem __user *used;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001218
1219 /* The virtqueue contains a ring of used buffers. Get a pointer to the
1220 * next entry in that used ring. */
1221 used = &vq->used->ring[vq->last_used_idx % vq->num];
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001222 if (__put_user(head, &used->id)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001223 vq_err(vq, "Failed to write used id");
1224 return -EFAULT;
1225 }
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001226 if (__put_user(len, &used->len)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001227 vq_err(vq, "Failed to write used len");
1228 return -EFAULT;
1229 }
1230 /* Make sure buffer is written before we update index. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001231 smp_wmb();
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001232 if (__put_user(vq->last_used_idx + 1, &vq->used->idx)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001233 vq_err(vq, "Failed to increment used idx");
1234 return -EFAULT;
1235 }
1236 if (unlikely(vq->log_used)) {
1237 /* Make sure data is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001238 smp_wmb();
Michael S. Tsirkin86e94242010-02-17 19:11:33 +02001239 /* Log used ring entry write. */
1240 log_write(vq->log_base,
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001241 vq->log_addr +
1242 ((void __user *)used - (void __user *)vq->used),
Michael S. Tsirkin86e94242010-02-17 19:11:33 +02001243 sizeof *used);
1244 /* Log used index update. */
1245 log_write(vq->log_base,
1246 vq->log_addr + offsetof(struct vring_used, idx),
1247 sizeof vq->used->idx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001248 if (vq->log_ctx)
1249 eventfd_signal(vq->log_ctx, 1);
1250 }
1251 vq->last_used_idx++;
1252 return 0;
1253}
1254
David Stevens8dd014a2010-07-27 18:52:21 +03001255static int __vhost_add_used_n(struct vhost_virtqueue *vq,
1256 struct vring_used_elem *heads,
1257 unsigned count)
1258{
1259 struct vring_used_elem __user *used;
1260 int start;
1261
1262 start = vq->last_used_idx % vq->num;
1263 used = vq->used->ring + start;
Michael S. Tsirkindfe5ac52010-09-21 14:18:01 +02001264 if (__copy_to_user(used, heads, count * sizeof *used)) {
David Stevens8dd014a2010-07-27 18:52:21 +03001265 vq_err(vq, "Failed to write used");
1266 return -EFAULT;
1267 }
1268 if (unlikely(vq->log_used)) {
1269 /* Make sure data is seen before log. */
1270 smp_wmb();
1271 /* Log used ring entry write. */
1272 log_write(vq->log_base,
1273 vq->log_addr +
1274 ((void __user *)used - (void __user *)vq->used),
1275 count * sizeof *used);
1276 }
1277 vq->last_used_idx += count;
1278 return 0;
1279}
1280
1281/* After we've used one of their buffers, we tell them about it. We'll then
1282 * want to notify the guest, using eventfd. */
1283int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
1284 unsigned count)
1285{
1286 int start, n, r;
1287
1288 start = vq->last_used_idx % vq->num;
1289 n = vq->num - start;
1290 if (n < count) {
1291 r = __vhost_add_used_n(vq, heads, n);
1292 if (r < 0)
1293 return r;
1294 heads += n;
1295 count -= n;
1296 }
1297 r = __vhost_add_used_n(vq, heads, count);
1298
1299 /* Make sure buffer is written before we update index. */
1300 smp_wmb();
1301 if (put_user(vq->last_used_idx, &vq->used->idx)) {
1302 vq_err(vq, "Failed to increment used idx");
1303 return -EFAULT;
1304 }
1305 if (unlikely(vq->log_used)) {
1306 /* Log used index update. */
1307 log_write(vq->log_base,
1308 vq->log_addr + offsetof(struct vring_used, idx),
1309 sizeof vq->used->idx);
1310 if (vq->log_ctx)
1311 eventfd_signal(vq->log_ctx, 1);
1312 }
1313 return r;
1314}
1315
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001316/* This actually signals the guest, using eventfd. */
1317void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
1318{
Michael S. Tsirkin0d499352010-05-11 19:44:17 +03001319 __u16 flags;
1320 /* Flush out used index updates. This is paired
1321 * with the barrier that the Guest executes when enabling
1322 * interrupts. */
1323 smp_mb();
1324
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001325 if (__get_user(flags, &vq->avail->flags)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001326 vq_err(vq, "Failed to get flags");
1327 return;
1328 }
1329
1330 /* If they don't want an interrupt, don't signal, unless empty. */
1331 if ((flags & VRING_AVAIL_F_NO_INTERRUPT) &&
1332 (vq->avail_idx != vq->last_avail_idx ||
1333 !vhost_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY)))
1334 return;
1335
1336 /* Signal the Guest tell them we used something up. */
1337 if (vq->call_ctx)
1338 eventfd_signal(vq->call_ctx, 1);
1339}
1340
1341/* And here's the combo meal deal. Supersize me! */
1342void vhost_add_used_and_signal(struct vhost_dev *dev,
1343 struct vhost_virtqueue *vq,
1344 unsigned int head, int len)
1345{
1346 vhost_add_used(vq, head, len);
1347 vhost_signal(dev, vq);
1348}
1349
David Stevens8dd014a2010-07-27 18:52:21 +03001350/* multi-buffer version of vhost_add_used_and_signal */
1351void vhost_add_used_and_signal_n(struct vhost_dev *dev,
1352 struct vhost_virtqueue *vq,
1353 struct vring_used_elem *heads, unsigned count)
1354{
1355 vhost_add_used_n(vq, heads, count);
1356 vhost_signal(dev, vq);
1357}
1358
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001359/* OK, now we need to know about added descriptors. */
1360bool vhost_enable_notify(struct vhost_virtqueue *vq)
1361{
1362 u16 avail_idx;
1363 int r;
1364 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
1365 return false;
1366 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
1367 r = put_user(vq->used_flags, &vq->used->flags);
1368 if (r) {
1369 vq_err(vq, "Failed to enable notification at %p: %d\n",
1370 &vq->used->flags, r);
1371 return false;
1372 }
1373 /* They could have slipped one in as we were doing that: make
1374 * sure it's written, then check again. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001375 smp_mb();
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001376 r = __get_user(avail_idx, &vq->avail->idx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001377 if (r) {
1378 vq_err(vq, "Failed to check avail idx at %p: %d\n",
1379 &vq->avail->idx, r);
1380 return false;
1381 }
1382
David Stevens8dd014a2010-07-27 18:52:21 +03001383 return avail_idx != vq->avail_idx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001384}
1385
1386/* We don't need to be notified again. */
1387void vhost_disable_notify(struct vhost_virtqueue *vq)
1388{
1389 int r;
1390 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
1391 return;
1392 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
1393 r = put_user(vq->used_flags, &vq->used->flags);
1394 if (r)
1395 vq_err(vq, "Failed to enable notification at %p: %d\n",
1396 &vq->used->flags, r);
1397}