blob: 4d135b1a861eeed6fda0d210f77ce1fa849391c6 [file] [log] [blame]
Michael S. Tsirkin3a4d5c94e2010-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
Rob Landley61516582011-05-06 09:27:36 -07007 * Documentation/virtual/lguest/lguest.c, by Rusty Russell
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00008 *
9 * This work is licensed under the terms of the GNU GPL, version 2.
10 *
11 * Generic code for virtio server in host kernel.
12 */
13
14#include <linux/eventfd.h>
15#include <linux/vhost.h>
Michael S. Tsirkin7542a6b2013-05-06 13:29:36 +030016#include <linux/socket.h> /* memcpy_fromiovec */
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000017#include <linux/mm.h>
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +020018#include <linux/mmu_context.h>
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000019#include <linux/miscdevice.h>
20#include <linux/mutex.h>
Michael S. Tsirkin3a4d5c94e2010-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 Heoc23f34452010-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. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000028
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000029#include "vhost.h"
30
31enum {
32 VHOST_MEMORY_MAX_NREGIONS = 64,
33 VHOST_MEMORY_F_LOG = 0x1,
34};
35
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +030036#define vhost_used_event(vq) ((u16 __user *)&vq->avail->ring[vq->num])
37#define vhost_avail_event(vq) ((u16 __user *)&vq->used->ring[vq->num])
38
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000039static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
40 poll_table *pt)
41{
42 struct vhost_poll *poll;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000043
Krishna Kumard47effe2011-03-01 17:06:37 +053044 poll = container_of(pt, struct vhost_poll, table);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000045 poll->wqh = wqh;
46 add_wait_queue(wqh, &poll->wait);
47}
48
49static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync,
50 void *key)
51{
Tejun Heoc23f34452010-06-02 20:40:00 +020052 struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
53
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000054 if (!((unsigned long)key & poll->mask))
55 return 0;
56
Tejun Heoc23f34452010-06-02 20:40:00 +020057 vhost_poll_queue(poll);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000058 return 0;
59}
60
Stefan Hajnoczi163049a2012-07-21 06:55:37 +000061void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000062{
Tejun Heoc23f34452010-06-02 20:40:00 +020063 INIT_LIST_HEAD(&work->node);
64 work->fn = fn;
65 init_waitqueue_head(&work->done);
66 work->flushing = 0;
67 work->queue_seq = work->done_seq = 0;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000068}
69
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +030070/* Init poll structure */
71void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
72 unsigned long mask, struct vhost_dev *dev)
73{
74 init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
75 init_poll_funcptr(&poll->table, vhost_poll_func);
76 poll->mask = mask;
77 poll->dev = dev;
Jason Wang2b8b3282013-01-28 01:05:18 +000078 poll->wqh = NULL;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +030079
80 vhost_work_init(&poll->work, fn);
81}
82
Michael S. Tsirkin3a4d5c94e2010-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. */
Jason Wang2b8b3282013-01-28 01:05:18 +000085int vhost_poll_start(struct vhost_poll *poll, struct file *file)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000086{
87 unsigned long mask;
Jason Wang2b8b3282013-01-28 01:05:18 +000088 int ret = 0;
Krishna Kumard47effe2011-03-01 17:06:37 +053089
Jason Wang70181d512013-04-10 20:50:48 +000090 if (poll->wqh)
91 return 0;
92
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000093 mask = file->f_op->poll(file, &poll->table);
94 if (mask)
95 vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
Jason Wang2b8b3282013-01-28 01:05:18 +000096 if (mask & POLLERR) {
97 if (poll->wqh)
98 remove_wait_queue(poll->wqh, &poll->wait);
99 ret = -EINVAL;
100 }
101
102 return ret;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000103}
104
105/* Stop polling a file. After this function returns, it becomes safe to drop the
106 * file reference. You must also flush afterwards. */
107void vhost_poll_stop(struct vhost_poll *poll)
108{
Jason Wang2b8b3282013-01-28 01:05:18 +0000109 if (poll->wqh) {
110 remove_wait_queue(poll->wqh, &poll->wait);
111 poll->wqh = NULL;
112 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000113}
114
Michael S. Tsirkin0174b0c2011-01-10 10:03:20 +0200115static bool vhost_work_seq_done(struct vhost_dev *dev, struct vhost_work *work,
116 unsigned seq)
117{
118 int left;
Krishna Kumard47effe2011-03-01 17:06:37 +0530119
Michael S. Tsirkin0174b0c2011-01-10 10:03:20 +0200120 spin_lock_irq(&dev->work_lock);
121 left = seq - work->done_seq;
122 spin_unlock_irq(&dev->work_lock);
123 return left <= 0;
124}
125
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300126static void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000127{
Tejun Heoc23f34452010-06-02 20:40:00 +0200128 unsigned seq;
Tejun Heoc23f34452010-06-02 20:40:00 +0200129 int flushing;
130
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300131 spin_lock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200132 seq = work->queue_seq;
133 work->flushing++;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300134 spin_unlock_irq(&dev->work_lock);
Michael S. Tsirkin0174b0c2011-01-10 10:03:20 +0200135 wait_event(work->done, vhost_work_seq_done(dev, work, seq));
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300136 spin_lock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200137 flushing = --work->flushing;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300138 spin_unlock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200139 BUG_ON(flushing < 0);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000140}
141
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300142/* Flush any work that has been scheduled. When calling this, don't hold any
143 * locks that are also used by the callback. */
144void vhost_poll_flush(struct vhost_poll *poll)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000145{
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300146 vhost_work_flush(poll->dev, &poll->work);
147}
148
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000149void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300150{
Tejun Heoc23f34452010-06-02 20:40:00 +0200151 unsigned long flags;
152
153 spin_lock_irqsave(&dev->work_lock, flags);
154 if (list_empty(&work->node)) {
155 list_add_tail(&work->node, &dev->work_list);
156 work->queue_seq++;
157 wake_up_process(dev->worker);
158 }
159 spin_unlock_irqrestore(&dev->work_lock, flags);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000160}
161
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300162void vhost_poll_queue(struct vhost_poll *poll)
163{
164 vhost_work_queue(poll->dev, &poll->work);
165}
166
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000167static void vhost_vq_reset(struct vhost_dev *dev,
168 struct vhost_virtqueue *vq)
169{
170 vq->num = 1;
171 vq->desc = NULL;
172 vq->avail = NULL;
173 vq->used = NULL;
174 vq->last_avail_idx = 0;
175 vq->avail_idx = 0;
176 vq->last_used_idx = 0;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300177 vq->signalled_used = 0;
178 vq->signalled_used_valid = false;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000179 vq->used_flags = 0;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000180 vq->log_used = false;
181 vq->log_addr = -1ull;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000182 vq->private_data = NULL;
183 vq->log_base = NULL;
184 vq->error_ctx = NULL;
185 vq->error = NULL;
186 vq->kick = NULL;
187 vq->call_ctx = NULL;
188 vq->call = NULL;
Michael S. Tsirkin73a99f02010-02-23 11:23:45 +0200189 vq->log_ctx = NULL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000190}
191
Tejun Heoc23f34452010-06-02 20:40:00 +0200192static int vhost_worker(void *data)
193{
194 struct vhost_dev *dev = data;
195 struct vhost_work *work = NULL;
196 unsigned uninitialized_var(seq);
Jens Freimannd7ffde32012-06-26 00:59:58 +0000197 mm_segment_t oldfs = get_fs();
Tejun Heoc23f34452010-06-02 20:40:00 +0200198
Jens Freimannd7ffde32012-06-26 00:59:58 +0000199 set_fs(USER_DS);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200200 use_mm(dev->mm);
201
Tejun Heoc23f34452010-06-02 20:40:00 +0200202 for (;;) {
203 /* mb paired w/ kthread_stop */
204 set_current_state(TASK_INTERRUPTIBLE);
205
206 spin_lock_irq(&dev->work_lock);
207 if (work) {
208 work->done_seq = seq;
209 if (work->flushing)
210 wake_up_all(&work->done);
211 }
212
213 if (kthread_should_stop()) {
214 spin_unlock_irq(&dev->work_lock);
215 __set_current_state(TASK_RUNNING);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200216 break;
Tejun Heoc23f34452010-06-02 20:40:00 +0200217 }
218 if (!list_empty(&dev->work_list)) {
219 work = list_first_entry(&dev->work_list,
220 struct vhost_work, node);
221 list_del_init(&work->node);
222 seq = work->queue_seq;
223 } else
224 work = NULL;
225 spin_unlock_irq(&dev->work_lock);
226
227 if (work) {
228 __set_current_state(TASK_RUNNING);
229 work->fn(work);
Nadav Har'Eld550dda2012-02-27 15:07:29 +0200230 if (need_resched())
231 schedule();
Tejun Heoc23f34452010-06-02 20:40:00 +0200232 } else
233 schedule();
234
235 }
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200236 unuse_mm(dev->mm);
Jens Freimannd7ffde32012-06-26 00:59:58 +0000237 set_fs(oldfs);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200238 return 0;
Tejun Heoc23f34452010-06-02 20:40:00 +0200239}
240
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000241static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
242{
243 kfree(vq->indirect);
244 vq->indirect = NULL;
245 kfree(vq->log);
246 vq->log = NULL;
247 kfree(vq->heads);
248 vq->heads = NULL;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000249}
250
Jason Wange0e9b402010-09-14 23:53:05 +0800251/* Helper to allocate iovec buffers for all vqs. */
252static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
253{
Asias He6d5e6aa2013-05-06 16:38:23 +0800254 struct vhost_virtqueue *vq;
Jason Wange0e9b402010-09-14 23:53:05 +0800255 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530256
Jason Wange0e9b402010-09-14 23:53:05 +0800257 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800258 vq = dev->vqs[i];
259 vq->indirect = kmalloc(sizeof *vq->indirect * UIO_MAXIOV,
260 GFP_KERNEL);
261 vq->log = kmalloc(sizeof *vq->log * UIO_MAXIOV, GFP_KERNEL);
262 vq->heads = kmalloc(sizeof *vq->heads * UIO_MAXIOV, GFP_KERNEL);
263 if (!vq->indirect || !vq->log || !vq->heads)
Jason Wange0e9b402010-09-14 23:53:05 +0800264 goto err_nomem;
265 }
266 return 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530267
Jason Wange0e9b402010-09-14 23:53:05 +0800268err_nomem:
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000269 for (; i >= 0; --i)
Asias He3ab2e422013-04-27 11:16:48 +0800270 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800271 return -ENOMEM;
272}
273
274static void vhost_dev_free_iovecs(struct vhost_dev *dev)
275{
276 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530277
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000278 for (i = 0; i < dev->nvqs; ++i)
Asias He3ab2e422013-04-27 11:16:48 +0800279 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800280}
281
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000282long vhost_dev_init(struct vhost_dev *dev,
Asias He3ab2e422013-04-27 11:16:48 +0800283 struct vhost_virtqueue **vqs, int nvqs)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000284{
Asias He6d5e6aa2013-05-06 16:38:23 +0800285 struct vhost_virtqueue *vq;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000286 int i;
Tejun Heoc23f34452010-06-02 20:40:00 +0200287
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000288 dev->vqs = vqs;
289 dev->nvqs = nvqs;
290 mutex_init(&dev->mutex);
291 dev->log_ctx = NULL;
292 dev->log_file = NULL;
293 dev->memory = NULL;
294 dev->mm = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200295 spin_lock_init(&dev->work_lock);
296 INIT_LIST_HEAD(&dev->work_list);
297 dev->worker = NULL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000298
299 for (i = 0; i < dev->nvqs; ++i) {
Asias He6d5e6aa2013-05-06 16:38:23 +0800300 vq = dev->vqs[i];
301 vq->log = NULL;
302 vq->indirect = NULL;
303 vq->heads = NULL;
304 vq->dev = dev;
305 mutex_init(&vq->mutex);
306 vhost_vq_reset(dev, vq);
307 if (vq->handle_kick)
308 vhost_poll_init(&vq->poll, vq->handle_kick,
309 POLLIN, dev);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000310 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200311
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000312 return 0;
313}
314
315/* Caller should have device mutex */
316long vhost_dev_check_owner(struct vhost_dev *dev)
317{
318 /* Are you the owner? If not, I don't think you mean to do that */
319 return dev->mm == current->mm ? 0 : -EPERM;
320}
321
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300322struct vhost_attach_cgroups_struct {
Krishna Kumard47effe2011-03-01 17:06:37 +0530323 struct vhost_work work;
324 struct task_struct *owner;
325 int ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300326};
327
328static void vhost_attach_cgroups_work(struct vhost_work *work)
329{
Krishna Kumard47effe2011-03-01 17:06:37 +0530330 struct vhost_attach_cgroups_struct *s;
331
332 s = container_of(work, struct vhost_attach_cgroups_struct, work);
333 s->ret = cgroup_attach_task_all(s->owner, current);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300334}
335
336static int vhost_attach_cgroups(struct vhost_dev *dev)
337{
Krishna Kumard47effe2011-03-01 17:06:37 +0530338 struct vhost_attach_cgroups_struct attach;
339
340 attach.owner = current;
341 vhost_work_init(&attach.work, vhost_attach_cgroups_work);
342 vhost_work_queue(dev, &attach.work);
343 vhost_work_flush(dev, &attach.work);
344 return attach.ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300345}
346
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000347/* Caller should have device mutex */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300348bool vhost_dev_has_owner(struct vhost_dev *dev)
349{
350 return dev->mm;
351}
352
353/* Caller should have device mutex */
Asias He54db63c2013-05-06 11:15:59 +0800354long vhost_dev_set_owner(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000355{
Tejun Heoc23f34452010-06-02 20:40:00 +0200356 struct task_struct *worker;
357 int err;
Krishna Kumard47effe2011-03-01 17:06:37 +0530358
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000359 /* Is there an owner already? */
Michael S. Tsirkin05c05352013-06-06 15:20:39 +0300360 if (vhost_dev_has_owner(dev)) {
Tejun Heoc23f34452010-06-02 20:40:00 +0200361 err = -EBUSY;
362 goto err_mm;
363 }
Krishna Kumard47effe2011-03-01 17:06:37 +0530364
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000365 /* No owner, become one */
366 dev->mm = get_task_mm(current);
Tejun Heoc23f34452010-06-02 20:40:00 +0200367 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
368 if (IS_ERR(worker)) {
369 err = PTR_ERR(worker);
370 goto err_worker;
371 }
372
373 dev->worker = worker;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300374 wake_up_process(worker); /* avoid contributing to loadavg */
375
376 err = vhost_attach_cgroups(dev);
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300377 if (err)
378 goto err_cgroup;
Tejun Heoc23f34452010-06-02 20:40:00 +0200379
Jason Wange0e9b402010-09-14 23:53:05 +0800380 err = vhost_dev_alloc_iovecs(dev);
381 if (err)
382 goto err_cgroup;
383
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000384 return 0;
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300385err_cgroup:
386 kthread_stop(worker);
Michael S. Tsirkin615cc222010-09-02 14:16:36 +0300387 dev->worker = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200388err_worker:
389 if (dev->mm)
390 mmput(dev->mm);
391 dev->mm = NULL;
392err_mm:
393 return err;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000394}
395
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300396struct vhost_memory *vhost_dev_reset_owner_prepare(void)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000397{
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300398 return kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
399}
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000400
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300401/* Caller should have device mutex */
402void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_memory *memory)
403{
Michael S. Tsirkinea5d4042011-11-27 19:05:58 +0200404 vhost_dev_cleanup(dev, true);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000405
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300406 /* Restore memory to default empty mapping. */
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000407 memory->nregions = 0;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100408 RCU_INIT_POINTER(dev->memory, memory);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000409}
410
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000411void vhost_dev_stop(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000412{
413 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530414
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000415 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800416 if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) {
417 vhost_poll_stop(&dev->vqs[i]->poll);
418 vhost_poll_flush(&dev->vqs[i]->poll);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000419 }
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000420 }
421}
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000422
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000423/* Caller should have device mutex if and only if locked is set */
424void vhost_dev_cleanup(struct vhost_dev *dev, bool locked)
425{
426 int i;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000427
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000428 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800429 if (dev->vqs[i]->error_ctx)
430 eventfd_ctx_put(dev->vqs[i]->error_ctx);
431 if (dev->vqs[i]->error)
432 fput(dev->vqs[i]->error);
433 if (dev->vqs[i]->kick)
434 fput(dev->vqs[i]->kick);
435 if (dev->vqs[i]->call_ctx)
436 eventfd_ctx_put(dev->vqs[i]->call_ctx);
437 if (dev->vqs[i]->call)
438 fput(dev->vqs[i]->call);
439 vhost_vq_reset(dev, dev->vqs[i]);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000440 }
Jason Wange0e9b402010-09-14 23:53:05 +0800441 vhost_dev_free_iovecs(dev);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000442 if (dev->log_ctx)
443 eventfd_ctx_put(dev->log_ctx);
444 dev->log_ctx = NULL;
445 if (dev->log_file)
446 fput(dev->log_file);
447 dev->log_file = NULL;
448 /* No one will access memory at this point */
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100449 kfree(rcu_dereference_protected(dev->memory,
Michael S. Tsirkinea5d4042011-11-27 19:05:58 +0200450 locked ==
451 lockdep_is_held(&dev->mutex)));
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100452 RCU_INIT_POINTER(dev->memory, NULL);
Tejun Heoc23f34452010-06-02 20:40:00 +0200453 WARN_ON(!list_empty(&dev->work_list));
Eric Dumazet78b620c2010-08-31 02:05:57 +0000454 if (dev->worker) {
455 kthread_stop(dev->worker);
456 dev->worker = NULL;
457 }
Michael S. Tsirkin533a19b2010-10-06 15:34:38 +0200458 if (dev->mm)
459 mmput(dev->mm);
460 dev->mm = NULL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000461}
462
463static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
464{
465 u64 a = addr / VHOST_PAGE_SIZE / 8;
Krishna Kumard47effe2011-03-01 17:06:37 +0530466
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000467 /* Make sure 64 bit math will not overflow. */
468 if (a > ULONG_MAX - (unsigned long)log_base ||
469 a + (unsigned long)log_base > ULONG_MAX)
Dan Carpenter6d97e552010-10-11 19:24:19 +0200470 return 0;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000471
472 return access_ok(VERIFY_WRITE, log_base + a,
473 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
474}
475
476/* Caller should have vq mutex and device mutex. */
477static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
478 int log_all)
479{
480 int i;
Jeff Dike179b2842010-04-07 09:59:10 -0400481
Michael S. Tsirkinf8322fb2010-05-27 12:28:03 +0300482 if (!mem)
483 return 0;
Jeff Dike179b2842010-04-07 09:59:10 -0400484
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000485 for (i = 0; i < mem->nregions; ++i) {
486 struct vhost_memory_region *m = mem->regions + i;
487 unsigned long a = m->userspace_addr;
488 if (m->memory_size > ULONG_MAX)
489 return 0;
490 else if (!access_ok(VERIFY_WRITE, (void __user *)a,
491 m->memory_size))
492 return 0;
493 else if (log_all && !log_access_ok(log_base,
494 m->guest_phys_addr,
495 m->memory_size))
496 return 0;
497 }
498 return 1;
499}
500
501/* Can we switch to this memory table? */
502/* Caller should have device mutex but not vq mutex */
503static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
504 int log_all)
505{
506 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530507
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000508 for (i = 0; i < d->nvqs; ++i) {
509 int ok;
Asias He3ab2e422013-04-27 11:16:48 +0800510 mutex_lock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000511 /* If ring is inactive, will check when it's enabled. */
Asias He3ab2e422013-04-27 11:16:48 +0800512 if (d->vqs[i]->private_data)
513 ok = vq_memory_access_ok(d->vqs[i]->log_base, mem,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000514 log_all);
515 else
516 ok = 1;
Asias He3ab2e422013-04-27 11:16:48 +0800517 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000518 if (!ok)
519 return 0;
520 }
521 return 1;
522}
523
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300524static int vq_access_ok(struct vhost_dev *d, unsigned int num,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000525 struct vring_desc __user *desc,
526 struct vring_avail __user *avail,
527 struct vring_used __user *used)
528{
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300529 size_t s = vhost_has_feature(d, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000530 return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
531 access_ok(VERIFY_READ, avail,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300532 sizeof *avail + num * sizeof *avail->ring + s) &&
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000533 access_ok(VERIFY_WRITE, used,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300534 sizeof *used + num * sizeof *used->ring + s);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000535}
536
537/* Can we log writes? */
538/* Caller should have device mutex but not vq mutex */
539int vhost_log_access_ok(struct vhost_dev *dev)
540{
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100541 struct vhost_memory *mp;
542
543 mp = rcu_dereference_protected(dev->memory,
544 lockdep_is_held(&dev->mutex));
545 return memory_access_ok(dev, mp, 1);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000546}
547
548/* Verify access for write logging. */
549/* Caller should have vq mutex and device mutex */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300550static int vq_log_access_ok(struct vhost_dev *d, struct vhost_virtqueue *vq,
551 void __user *log_base)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000552{
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100553 struct vhost_memory *mp;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300554 size_t s = vhost_has_feature(d, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100555
556 mp = rcu_dereference_protected(vq->dev->memory,
557 lockdep_is_held(&vq->mutex));
558 return vq_memory_access_ok(log_base, mp,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000559 vhost_has_feature(vq->dev, VHOST_F_LOG_ALL)) &&
560 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
561 sizeof *vq->used +
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300562 vq->num * sizeof *vq->used->ring + s));
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000563}
564
565/* Can we start vq? */
566/* Caller should have vq mutex and device mutex */
567int vhost_vq_access_ok(struct vhost_virtqueue *vq)
568{
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300569 return vq_access_ok(vq->dev, vq->num, vq->desc, vq->avail, vq->used) &&
570 vq_log_access_ok(vq->dev, vq, vq->log_base);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000571}
572
573static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
574{
575 struct vhost_memory mem, *newmem, *oldmem;
576 unsigned long size = offsetof(struct vhost_memory, regions);
Krishna Kumard47effe2011-03-01 17:06:37 +0530577
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900578 if (copy_from_user(&mem, m, size))
579 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000580 if (mem.padding)
581 return -EOPNOTSUPP;
582 if (mem.nregions > VHOST_MEMORY_MAX_NREGIONS)
583 return -E2BIG;
584 newmem = kmalloc(size + mem.nregions * sizeof *m->regions, GFP_KERNEL);
585 if (!newmem)
586 return -ENOMEM;
587
588 memcpy(newmem, &mem, size);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900589 if (copy_from_user(newmem->regions, m->regions,
590 mem.nregions * sizeof *m->regions)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000591 kfree(newmem);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900592 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000593 }
594
Krishna Kumard47effe2011-03-01 17:06:37 +0530595 if (!memory_access_ok(d, newmem,
596 vhost_has_feature(d, VHOST_F_LOG_ALL))) {
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +0900597 kfree(newmem);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000598 return -EFAULT;
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +0900599 }
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100600 oldmem = rcu_dereference_protected(d->memory,
601 lockdep_is_held(&d->mutex));
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000602 rcu_assign_pointer(d->memory, newmem);
603 synchronize_rcu();
604 kfree(oldmem);
605 return 0;
606}
607
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200608long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000609{
Al Virocecb46f2012-08-27 14:21:39 -0400610 struct file *eventfp, *filep = NULL;
611 bool pollstart = false, pollstop = false;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000612 struct eventfd_ctx *ctx = NULL;
613 u32 __user *idxp = argp;
614 struct vhost_virtqueue *vq;
615 struct vhost_vring_state s;
616 struct vhost_vring_file f;
617 struct vhost_vring_addr a;
618 u32 idx;
619 long r;
620
621 r = get_user(idx, idxp);
622 if (r < 0)
623 return r;
Krishna Kumar0f3d9a12010-05-25 11:10:36 +0530624 if (idx >= d->nvqs)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000625 return -ENOBUFS;
626
Asias He3ab2e422013-04-27 11:16:48 +0800627 vq = d->vqs[idx];
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000628
629 mutex_lock(&vq->mutex);
630
631 switch (ioctl) {
632 case VHOST_SET_VRING_NUM:
633 /* Resizing ring with an active backend?
634 * You don't want to do that. */
635 if (vq->private_data) {
636 r = -EBUSY;
637 break;
638 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900639 if (copy_from_user(&s, argp, sizeof s)) {
640 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000641 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900642 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000643 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
644 r = -EINVAL;
645 break;
646 }
647 vq->num = s.num;
648 break;
649 case VHOST_SET_VRING_BASE:
650 /* Moving base with an active backend?
651 * You don't want to do that. */
652 if (vq->private_data) {
653 r = -EBUSY;
654 break;
655 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900656 if (copy_from_user(&s, argp, sizeof s)) {
657 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000658 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900659 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000660 if (s.num > 0xffff) {
661 r = -EINVAL;
662 break;
663 }
664 vq->last_avail_idx = s.num;
665 /* Forget the cached index value. */
666 vq->avail_idx = vq->last_avail_idx;
667 break;
668 case VHOST_GET_VRING_BASE:
669 s.index = idx;
670 s.num = vq->last_avail_idx;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900671 if (copy_to_user(argp, &s, sizeof s))
672 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000673 break;
674 case VHOST_SET_VRING_ADDR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900675 if (copy_from_user(&a, argp, sizeof a)) {
676 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000677 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900678 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000679 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
680 r = -EOPNOTSUPP;
681 break;
682 }
683 /* For 32bit, verify that the top 32bits of the user
684 data are set to zero. */
685 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
686 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
687 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
688 r = -EFAULT;
689 break;
690 }
691 if ((a.avail_user_addr & (sizeof *vq->avail->ring - 1)) ||
692 (a.used_user_addr & (sizeof *vq->used->ring - 1)) ||
693 (a.log_guest_addr & (sizeof *vq->used->ring - 1))) {
694 r = -EINVAL;
695 break;
696 }
697
698 /* We only verify access here if backend is configured.
699 * If it is not, we don't as size might not have been setup.
700 * We will verify when backend is configured. */
701 if (vq->private_data) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300702 if (!vq_access_ok(d, vq->num,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000703 (void __user *)(unsigned long)a.desc_user_addr,
704 (void __user *)(unsigned long)a.avail_user_addr,
705 (void __user *)(unsigned long)a.used_user_addr)) {
706 r = -EINVAL;
707 break;
708 }
709
710 /* Also validate log access for used ring if enabled. */
711 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
712 !log_access_ok(vq->log_base, a.log_guest_addr,
713 sizeof *vq->used +
714 vq->num * sizeof *vq->used->ring)) {
715 r = -EINVAL;
716 break;
717 }
718 }
719
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000720 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
721 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
722 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
723 vq->log_addr = a.log_guest_addr;
724 vq->used = (void __user *)(unsigned long)a.used_user_addr;
725 break;
726 case VHOST_SET_VRING_KICK:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900727 if (copy_from_user(&f, argp, sizeof f)) {
728 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000729 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900730 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000731 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200732 if (IS_ERR(eventfp)) {
733 r = PTR_ERR(eventfp);
734 break;
735 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000736 if (eventfp != vq->kick) {
Al Virocecb46f2012-08-27 14:21:39 -0400737 pollstop = (filep = vq->kick) != NULL;
738 pollstart = (vq->kick = eventfp) != NULL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000739 } else
740 filep = eventfp;
741 break;
742 case VHOST_SET_VRING_CALL:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900743 if (copy_from_user(&f, argp, sizeof f)) {
744 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000745 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900746 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000747 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200748 if (IS_ERR(eventfp)) {
749 r = PTR_ERR(eventfp);
750 break;
751 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000752 if (eventfp != vq->call) {
753 filep = vq->call;
754 ctx = vq->call_ctx;
755 vq->call = eventfp;
756 vq->call_ctx = eventfp ?
757 eventfd_ctx_fileget(eventfp) : NULL;
758 } else
759 filep = eventfp;
760 break;
761 case VHOST_SET_VRING_ERR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900762 if (copy_from_user(&f, argp, sizeof f)) {
763 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000764 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900765 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000766 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200767 if (IS_ERR(eventfp)) {
768 r = PTR_ERR(eventfp);
769 break;
770 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000771 if (eventfp != vq->error) {
772 filep = vq->error;
773 vq->error = eventfp;
774 ctx = vq->error_ctx;
775 vq->error_ctx = eventfp ?
776 eventfd_ctx_fileget(eventfp) : NULL;
777 } else
778 filep = eventfp;
779 break;
780 default:
781 r = -ENOIOCTLCMD;
782 }
783
784 if (pollstop && vq->handle_kick)
785 vhost_poll_stop(&vq->poll);
786
787 if (ctx)
788 eventfd_ctx_put(ctx);
789 if (filep)
790 fput(filep);
791
792 if (pollstart && vq->handle_kick)
Jason Wang2b8b3282013-01-28 01:05:18 +0000793 r = vhost_poll_start(&vq->poll, vq->kick);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000794
795 mutex_unlock(&vq->mutex);
796
797 if (pollstop && vq->handle_kick)
798 vhost_poll_flush(&vq->poll);
799 return r;
800}
801
802/* Caller must have device mutex */
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200803long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000804{
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000805 struct file *eventfp, *filep = NULL;
806 struct eventfd_ctx *ctx = NULL;
807 u64 p;
808 long r;
809 int i, fd;
810
811 /* If you are not the owner, you can become one */
812 if (ioctl == VHOST_SET_OWNER) {
813 r = vhost_dev_set_owner(d);
814 goto done;
815 }
816
817 /* You must be the owner to do anything else */
818 r = vhost_dev_check_owner(d);
819 if (r)
820 goto done;
821
822 switch (ioctl) {
823 case VHOST_SET_MEM_TABLE:
824 r = vhost_set_memory(d, argp);
825 break;
826 case VHOST_SET_LOG_BASE:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900827 if (copy_from_user(&p, argp, sizeof p)) {
828 r = -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000829 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900830 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000831 if ((u64)(unsigned long)p != p) {
832 r = -EFAULT;
833 break;
834 }
835 for (i = 0; i < d->nvqs; ++i) {
836 struct vhost_virtqueue *vq;
837 void __user *base = (void __user *)(unsigned long)p;
Asias He3ab2e422013-04-27 11:16:48 +0800838 vq = d->vqs[i];
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000839 mutex_lock(&vq->mutex);
840 /* If ring is inactive, will check when it's enabled. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300841 if (vq->private_data && !vq_log_access_ok(d, vq, base))
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000842 r = -EFAULT;
843 else
844 vq->log_base = base;
845 mutex_unlock(&vq->mutex);
846 }
847 break;
848 case VHOST_SET_LOG_FD:
849 r = get_user(fd, (int __user *)argp);
850 if (r < 0)
851 break;
852 eventfp = fd == -1 ? NULL : eventfd_fget(fd);
853 if (IS_ERR(eventfp)) {
854 r = PTR_ERR(eventfp);
855 break;
856 }
857 if (eventfp != d->log_file) {
858 filep = d->log_file;
859 ctx = d->log_ctx;
860 d->log_ctx = eventfp ?
861 eventfd_ctx_fileget(eventfp) : NULL;
862 } else
863 filep = eventfp;
864 for (i = 0; i < d->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800865 mutex_lock(&d->vqs[i]->mutex);
866 d->vqs[i]->log_ctx = d->log_ctx;
867 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000868 }
869 if (ctx)
870 eventfd_ctx_put(ctx);
871 if (filep)
872 fput(filep);
873 break;
874 default:
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200875 r = -ENOIOCTLCMD;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000876 break;
877 }
878done:
879 return r;
880}
881
882static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
883 __u64 addr, __u32 len)
884{
885 struct vhost_memory_region *reg;
886 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530887
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000888 /* linear search is not brilliant, but we really have on the order of 6
889 * regions in practice */
890 for (i = 0; i < mem->nregions; ++i) {
891 reg = mem->regions + i;
892 if (reg->guest_phys_addr <= addr &&
893 reg->guest_phys_addr + reg->memory_size - 1 >= addr)
894 return reg;
895 }
896 return NULL;
897}
898
899/* TODO: This is really inefficient. We need something like get_user()
900 * (instruction directly accesses the data, with an exception table entry
901 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
902 */
903static int set_bit_to_user(int nr, void __user *addr)
904{
905 unsigned long log = (unsigned long)addr;
906 struct page *page;
907 void *base;
908 int bit = nr + (log % PAGE_SIZE) * 8;
909 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +0530910
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000911 r = get_user_pages_fast(log, 1, 1, &page);
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +0200912 if (r < 0)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000913 return r;
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +0200914 BUG_ON(r != 1);
Cong Wangc6daa7f2011-11-25 23:14:26 +0800915 base = kmap_atomic(page);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000916 set_bit(bit, base);
Cong Wangc6daa7f2011-11-25 23:14:26 +0800917 kunmap_atomic(base);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000918 set_page_dirty_lock(page);
919 put_page(page);
920 return 0;
921}
922
923static int log_write(void __user *log_base,
924 u64 write_address, u64 write_length)
925{
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +0200926 u64 write_page = write_address / VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000927 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +0530928
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000929 if (!write_length)
930 return 0;
Michael S. Tsirkin3bf9be42010-11-29 10:19:07 +0200931 write_length += write_address % VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000932 for (;;) {
933 u64 base = (u64)(unsigned long)log_base;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +0200934 u64 log = base + write_page / 8;
935 int bit = write_page % 8;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000936 if ((u64)(unsigned long)log != log)
937 return -EFAULT;
938 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
939 if (r < 0)
940 return r;
941 if (write_length <= VHOST_PAGE_SIZE)
942 break;
943 write_length -= VHOST_PAGE_SIZE;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +0200944 write_page += 1;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000945 }
946 return r;
947}
948
949int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
950 unsigned int log_num, u64 len)
951{
952 int i, r;
953
954 /* Make sure data written is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +0000955 smp_wmb();
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000956 for (i = 0; i < log_num; ++i) {
957 u64 l = min(log[i].len, len);
958 r = log_write(vq->log_base, log[i].addr, l);
959 if (r < 0)
960 return r;
961 len -= l;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +0200962 if (!len) {
963 if (vq->log_ctx)
964 eventfd_signal(vq->log_ctx, 1);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000965 return 0;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +0200966 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000967 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000968 /* Length written exceeds what we have stored. This is a bug. */
969 BUG();
970 return 0;
971}
972
Jason Wang2723fea2011-06-21 18:04:38 +0800973static int vhost_update_used_flags(struct vhost_virtqueue *vq)
974{
975 void __user *used;
Michael S. Tsirkinb8342262011-07-19 17:15:43 +0300976 if (__put_user(vq->used_flags, &vq->used->flags) < 0)
Jason Wang2723fea2011-06-21 18:04:38 +0800977 return -EFAULT;
978 if (unlikely(vq->log_used)) {
979 /* Make sure the flag is seen before log. */
980 smp_wmb();
981 /* Log used flag write. */
982 used = &vq->used->flags;
983 log_write(vq->log_base, vq->log_addr +
984 (used - (void __user *)vq->used),
985 sizeof vq->used->flags);
986 if (vq->log_ctx)
987 eventfd_signal(vq->log_ctx, 1);
988 }
989 return 0;
990}
991
992static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
993{
Michael S. Tsirkinb8342262011-07-19 17:15:43 +0300994 if (__put_user(vq->avail_idx, vhost_avail_event(vq)))
Jason Wang2723fea2011-06-21 18:04:38 +0800995 return -EFAULT;
996 if (unlikely(vq->log_used)) {
997 void __user *used;
998 /* Make sure the event is seen before log. */
999 smp_wmb();
1000 /* Log avail event write */
1001 used = vhost_avail_event(vq);
1002 log_write(vq->log_base, vq->log_addr +
1003 (used - (void __user *)vq->used),
1004 sizeof *vhost_avail_event(vq));
1005 if (vq->log_ctx)
1006 eventfd_signal(vq->log_ctx, 1);
1007 }
1008 return 0;
1009}
1010
1011int vhost_init_used(struct vhost_virtqueue *vq)
1012{
1013 int r;
1014 if (!vq->private_data)
1015 return 0;
1016
1017 r = vhost_update_used_flags(vq);
1018 if (r)
1019 return r;
1020 vq->signalled_used_valid = false;
1021 return get_user(vq->last_used_idx, &vq->used->idx);
1022}
1023
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001024static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
1025 struct iovec iov[], int iov_size)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001026{
1027 const struct vhost_memory_region *reg;
1028 struct vhost_memory *mem;
1029 struct iovec *_iov;
1030 u64 s = 0;
1031 int ret = 0;
1032
1033 rcu_read_lock();
1034
1035 mem = rcu_dereference(dev->memory);
1036 while ((u64)len > s) {
1037 u64 size;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001038 if (unlikely(ret >= iov_size)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001039 ret = -ENOBUFS;
1040 break;
1041 }
1042 reg = find_region(mem, addr, len);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001043 if (unlikely(!reg)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001044 ret = -EFAULT;
1045 break;
1046 }
1047 _iov = iov + ret;
1048 size = reg->memory_size - addr + reg->guest_phys_addr;
Michael S. Tsirkinbd971202012-11-26 05:57:27 +00001049 _iov->iov_len = min((u64)len - s, size);
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001050 _iov->iov_base = (void __user *)(unsigned long)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001051 (reg->userspace_addr + addr - reg->guest_phys_addr);
1052 s += size;
1053 addr += size;
1054 ++ret;
1055 }
1056
1057 rcu_read_unlock();
1058 return ret;
1059}
1060
1061/* Each buffer in the virtqueues is actually a chain of descriptors. This
1062 * function returns the next descriptor in the chain,
1063 * or -1U if we're at the end. */
1064static unsigned next_desc(struct vring_desc *desc)
1065{
1066 unsigned int next;
1067
1068 /* If this descriptor says it doesn't chain, we're done. */
1069 if (!(desc->flags & VRING_DESC_F_NEXT))
1070 return -1U;
1071
1072 /* Check they're not leading us off end of descriptors. */
1073 next = desc->next;
1074 /* Make sure compiler knows to grab that: we don't want it changing! */
1075 /* We will use the result as an index in an array, so most
1076 * architectures only need a compiler barrier here. */
1077 read_barrier_depends();
1078
1079 return next;
1080}
1081
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001082static int get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
1083 struct iovec iov[], unsigned int iov_size,
1084 unsigned int *out_num, unsigned int *in_num,
1085 struct vhost_log *log, unsigned int *log_num,
1086 struct vring_desc *indirect)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001087{
1088 struct vring_desc desc;
1089 unsigned int i = 0, count, found = 0;
1090 int ret;
1091
1092 /* Sanity check */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001093 if (unlikely(indirect->len % sizeof desc)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001094 vq_err(vq, "Invalid length in indirect descriptor: "
1095 "len 0x%llx not multiple of 0x%zx\n",
1096 (unsigned long long)indirect->len,
1097 sizeof desc);
1098 return -EINVAL;
1099 }
1100
1101 ret = translate_desc(dev, indirect->addr, indirect->len, vq->indirect,
Jason Wange0e9b402010-09-14 23:53:05 +08001102 UIO_MAXIOV);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001103 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001104 vq_err(vq, "Translation failure %d in indirect.\n", ret);
1105 return ret;
1106 }
1107
1108 /* We will use the result as an address to read from, so most
1109 * architectures only need a compiler barrier here. */
1110 read_barrier_depends();
1111
1112 count = indirect->len / sizeof desc;
1113 /* Buffers are chained via a 16 bit next field, so
1114 * we can have at most 2^16 of these. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001115 if (unlikely(count > USHRT_MAX + 1)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001116 vq_err(vq, "Indirect buffer length too big: %d\n",
1117 indirect->len);
1118 return -E2BIG;
1119 }
1120
1121 do {
1122 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001123 if (unlikely(++found > count)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001124 vq_err(vq, "Loop detected: last one at %u "
1125 "indirect size %u\n",
1126 i, count);
1127 return -EINVAL;
1128 }
Krishna Kumard47effe2011-03-01 17:06:37 +05301129 if (unlikely(memcpy_fromiovec((unsigned char *)&desc,
1130 vq->indirect, sizeof desc))) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001131 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
1132 i, (size_t)indirect->addr + i * sizeof desc);
1133 return -EINVAL;
1134 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001135 if (unlikely(desc.flags & VRING_DESC_F_INDIRECT)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001136 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
1137 i, (size_t)indirect->addr + i * sizeof desc);
1138 return -EINVAL;
1139 }
1140
1141 ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
1142 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001143 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001144 vq_err(vq, "Translation failure %d indirect idx %d\n",
1145 ret, i);
1146 return ret;
1147 }
1148 /* If this is an input descriptor, increment that count. */
1149 if (desc.flags & VRING_DESC_F_WRITE) {
1150 *in_num += ret;
1151 if (unlikely(log)) {
1152 log[*log_num].addr = desc.addr;
1153 log[*log_num].len = desc.len;
1154 ++*log_num;
1155 }
1156 } else {
1157 /* If it's an output descriptor, they're all supposed
1158 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001159 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001160 vq_err(vq, "Indirect descriptor "
1161 "has out after in: idx %d\n", i);
1162 return -EINVAL;
1163 }
1164 *out_num += ret;
1165 }
1166 } while ((i = next_desc(&desc)) != -1);
1167 return 0;
1168}
1169
1170/* This looks in the virtqueue and for the first available buffer, and converts
1171 * it to an iovec for convenient access. Since descriptors consist of some
1172 * number of output then some number of input descriptors, it's actually two
1173 * iovecs, but we pack them into one and note how many of each there were.
1174 *
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001175 * This function returns the descriptor number found, or vq->num (which is
1176 * never a valid descriptor number) if none was found. A negative code is
1177 * returned on error. */
1178int vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
1179 struct iovec iov[], unsigned int iov_size,
1180 unsigned int *out_num, unsigned int *in_num,
1181 struct vhost_log *log, unsigned int *log_num)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001182{
1183 struct vring_desc desc;
1184 unsigned int i, head, found = 0;
1185 u16 last_avail_idx;
1186 int ret;
1187
1188 /* Check it isn't doing very strange things with descriptor numbers. */
1189 last_avail_idx = vq->last_avail_idx;
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001190 if (unlikely(__get_user(vq->avail_idx, &vq->avail->idx))) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001191 vq_err(vq, "Failed to access avail idx at %p\n",
1192 &vq->avail->idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001193 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001194 }
1195
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001196 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001197 vq_err(vq, "Guest moved used index from %u to %u",
1198 last_avail_idx, vq->avail_idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001199 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001200 }
1201
1202 /* If there's nothing new since last we looked, return invalid. */
1203 if (vq->avail_idx == last_avail_idx)
1204 return vq->num;
1205
1206 /* Only get avail ring entries after they have been exposed by guest. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001207 smp_rmb();
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001208
1209 /* Grab the next descriptor number they're advertising, and increment
1210 * the index we've seen. */
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001211 if (unlikely(__get_user(head,
1212 &vq->avail->ring[last_avail_idx % vq->num]))) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001213 vq_err(vq, "Failed to read head: idx %d address %p\n",
1214 last_avail_idx,
1215 &vq->avail->ring[last_avail_idx % vq->num]);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001216 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001217 }
1218
1219 /* If their number is silly, that's an error. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001220 if (unlikely(head >= vq->num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001221 vq_err(vq, "Guest says index %u > %u is available",
1222 head, vq->num);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001223 return -EINVAL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001224 }
1225
1226 /* When we start there are none of either input nor output. */
1227 *out_num = *in_num = 0;
1228 if (unlikely(log))
1229 *log_num = 0;
1230
1231 i = head;
1232 do {
1233 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001234 if (unlikely(i >= vq->num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001235 vq_err(vq, "Desc index is %u > %u, head = %u",
1236 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001237 return -EINVAL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001238 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001239 if (unlikely(++found > vq->num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001240 vq_err(vq, "Loop detected: last one at %u "
1241 "vq size %u head %u\n",
1242 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001243 return -EINVAL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001244 }
Michael S. Tsirkinfcc042a2011-03-06 13:33:49 +02001245 ret = __copy_from_user(&desc, vq->desc + i, sizeof desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001246 if (unlikely(ret)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001247 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
1248 i, vq->desc + i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001249 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001250 }
1251 if (desc.flags & VRING_DESC_F_INDIRECT) {
1252 ret = get_indirect(dev, vq, iov, iov_size,
1253 out_num, in_num,
1254 log, log_num, &desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001255 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001256 vq_err(vq, "Failure detected "
1257 "in indirect descriptor at idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001258 return ret;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001259 }
1260 continue;
1261 }
1262
1263 ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
1264 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001265 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001266 vq_err(vq, "Translation failure %d descriptor idx %d\n",
1267 ret, i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001268 return ret;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001269 }
1270 if (desc.flags & VRING_DESC_F_WRITE) {
1271 /* If this is an input descriptor,
1272 * increment that count. */
1273 *in_num += ret;
1274 if (unlikely(log)) {
1275 log[*log_num].addr = desc.addr;
1276 log[*log_num].len = desc.len;
1277 ++*log_num;
1278 }
1279 } else {
1280 /* If it's an output descriptor, they're all supposed
1281 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001282 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001283 vq_err(vq, "Descriptor has out after in: "
1284 "idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001285 return -EINVAL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001286 }
1287 *out_num += ret;
1288 }
1289 } while ((i = next_desc(&desc)) != -1);
1290
1291 /* On success, increment avail index. */
1292 vq->last_avail_idx++;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001293
1294 /* Assume notifications from guest are disabled at this point,
1295 * if they aren't we would need to update avail_event index. */
1296 BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY));
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001297 return head;
1298}
1299
1300/* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
David Stevens8dd014a2010-07-27 18:52:21 +03001301void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001302{
David Stevens8dd014a2010-07-27 18:52:21 +03001303 vq->last_avail_idx -= n;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001304}
1305
1306/* After we've used one of their buffers, we tell them about it. We'll then
1307 * want to notify the guest, using eventfd. */
1308int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
1309{
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001310 struct vring_used_elem __user *used;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001311
1312 /* The virtqueue contains a ring of used buffers. Get a pointer to the
1313 * next entry in that used ring. */
1314 used = &vq->used->ring[vq->last_used_idx % vq->num];
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001315 if (__put_user(head, &used->id)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001316 vq_err(vq, "Failed to write used id");
1317 return -EFAULT;
1318 }
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001319 if (__put_user(len, &used->len)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001320 vq_err(vq, "Failed to write used len");
1321 return -EFAULT;
1322 }
1323 /* Make sure buffer is written before we update index. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001324 smp_wmb();
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001325 if (__put_user(vq->last_used_idx + 1, &vq->used->idx)) {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001326 vq_err(vq, "Failed to increment used idx");
1327 return -EFAULT;
1328 }
1329 if (unlikely(vq->log_used)) {
1330 /* Make sure data is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001331 smp_wmb();
Michael S. Tsirkin86e94242010-02-17 19:11:33 +02001332 /* Log used ring entry write. */
1333 log_write(vq->log_base,
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001334 vq->log_addr +
1335 ((void __user *)used - (void __user *)vq->used),
Michael S. Tsirkin86e94242010-02-17 19:11:33 +02001336 sizeof *used);
1337 /* Log used index update. */
1338 log_write(vq->log_base,
1339 vq->log_addr + offsetof(struct vring_used, idx),
1340 sizeof vq->used->idx);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001341 if (vq->log_ctx)
1342 eventfd_signal(vq->log_ctx, 1);
1343 }
1344 vq->last_used_idx++;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001345 /* If the driver never bothers to signal in a very long while,
1346 * used index might wrap around. If that happens, invalidate
1347 * signalled_used index we stored. TODO: make sure driver
1348 * signals at least once in 2^16 and remove this. */
1349 if (unlikely(vq->last_used_idx == vq->signalled_used))
1350 vq->signalled_used_valid = false;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001351 return 0;
1352}
1353
David Stevens8dd014a2010-07-27 18:52:21 +03001354static int __vhost_add_used_n(struct vhost_virtqueue *vq,
1355 struct vring_used_elem *heads,
1356 unsigned count)
1357{
1358 struct vring_used_elem __user *used;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001359 u16 old, new;
David Stevens8dd014a2010-07-27 18:52:21 +03001360 int start;
1361
1362 start = vq->last_used_idx % vq->num;
1363 used = vq->used->ring + start;
Michael S. Tsirkindfe5ac52010-09-21 14:18:01 +02001364 if (__copy_to_user(used, heads, count * sizeof *used)) {
David Stevens8dd014a2010-07-27 18:52:21 +03001365 vq_err(vq, "Failed to write used");
1366 return -EFAULT;
1367 }
1368 if (unlikely(vq->log_used)) {
1369 /* Make sure data is seen before log. */
1370 smp_wmb();
1371 /* Log used ring entry write. */
1372 log_write(vq->log_base,
1373 vq->log_addr +
1374 ((void __user *)used - (void __user *)vq->used),
1375 count * sizeof *used);
1376 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001377 old = vq->last_used_idx;
1378 new = (vq->last_used_idx += count);
1379 /* If the driver never bothers to signal in a very long while,
1380 * used index might wrap around. If that happens, invalidate
1381 * signalled_used index we stored. TODO: make sure driver
1382 * signals at least once in 2^16 and remove this. */
1383 if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old)))
1384 vq->signalled_used_valid = false;
David Stevens8dd014a2010-07-27 18:52:21 +03001385 return 0;
1386}
1387
1388/* After we've used one of their buffers, we tell them about it. We'll then
1389 * want to notify the guest, using eventfd. */
1390int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
1391 unsigned count)
1392{
1393 int start, n, r;
1394
1395 start = vq->last_used_idx % vq->num;
1396 n = vq->num - start;
1397 if (n < count) {
1398 r = __vhost_add_used_n(vq, heads, n);
1399 if (r < 0)
1400 return r;
1401 heads += n;
1402 count -= n;
1403 }
1404 r = __vhost_add_used_n(vq, heads, count);
1405
1406 /* Make sure buffer is written before we update index. */
1407 smp_wmb();
1408 if (put_user(vq->last_used_idx, &vq->used->idx)) {
1409 vq_err(vq, "Failed to increment used idx");
1410 return -EFAULT;
1411 }
1412 if (unlikely(vq->log_used)) {
1413 /* Log used index update. */
1414 log_write(vq->log_base,
1415 vq->log_addr + offsetof(struct vring_used, idx),
1416 sizeof vq->used->idx);
1417 if (vq->log_ctx)
1418 eventfd_signal(vq->log_ctx, 1);
1419 }
1420 return r;
1421}
1422
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001423static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001424{
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001425 __u16 old, new, event;
1426 bool v;
Michael S. Tsirkin0d499352010-05-11 19:44:17 +03001427 /* Flush out used index updates. This is paired
1428 * with the barrier that the Guest executes when enabling
1429 * interrupts. */
1430 smp_mb();
1431
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001432 if (vhost_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY) &&
1433 unlikely(vq->avail_idx == vq->last_avail_idx))
1434 return true;
1435
1436 if (!vhost_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
1437 __u16 flags;
1438 if (__get_user(flags, &vq->avail->flags)) {
1439 vq_err(vq, "Failed to get flags");
1440 return true;
1441 }
1442 return !(flags & VRING_AVAIL_F_NO_INTERRUPT);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001443 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001444 old = vq->signalled_used;
1445 v = vq->signalled_used_valid;
1446 new = vq->signalled_used = vq->last_used_idx;
1447 vq->signalled_used_valid = true;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001448
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001449 if (unlikely(!v))
1450 return true;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001451
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001452 if (get_user(event, vhost_used_event(vq))) {
1453 vq_err(vq, "Failed to get used event idx");
1454 return true;
1455 }
1456 return vring_need_event(event, new, old);
1457}
1458
1459/* This actually signals the guest, using eventfd. */
1460void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
1461{
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001462 /* Signal the Guest tell them we used something up. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001463 if (vq->call_ctx && vhost_notify(dev, vq))
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001464 eventfd_signal(vq->call_ctx, 1);
1465}
1466
1467/* And here's the combo meal deal. Supersize me! */
1468void vhost_add_used_and_signal(struct vhost_dev *dev,
1469 struct vhost_virtqueue *vq,
1470 unsigned int head, int len)
1471{
1472 vhost_add_used(vq, head, len);
1473 vhost_signal(dev, vq);
1474}
1475
David Stevens8dd014a2010-07-27 18:52:21 +03001476/* multi-buffer version of vhost_add_used_and_signal */
1477void vhost_add_used_and_signal_n(struct vhost_dev *dev,
1478 struct vhost_virtqueue *vq,
1479 struct vring_used_elem *heads, unsigned count)
1480{
1481 vhost_add_used_n(vq, heads, count);
1482 vhost_signal(dev, vq);
1483}
1484
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001485/* OK, now we need to know about added descriptors. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001486bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001487{
1488 u16 avail_idx;
1489 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301490
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001491 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
1492 return false;
1493 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001494 if (!vhost_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08001495 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001496 if (r) {
1497 vq_err(vq, "Failed to enable notification at %p: %d\n",
1498 &vq->used->flags, r);
1499 return false;
1500 }
1501 } else {
Jason Wang2723fea2011-06-21 18:04:38 +08001502 r = vhost_update_avail_event(vq, vq->avail_idx);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001503 if (r) {
1504 vq_err(vq, "Failed to update avail event index at %p: %d\n",
1505 vhost_avail_event(vq), r);
1506 return false;
1507 }
1508 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001509 /* They could have slipped one in as we were doing that: make
1510 * sure it's written, then check again. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001511 smp_mb();
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001512 r = __get_user(avail_idx, &vq->avail->idx);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001513 if (r) {
1514 vq_err(vq, "Failed to check avail idx at %p: %d\n",
1515 &vq->avail->idx, r);
1516 return false;
1517 }
1518
David Stevens8dd014a2010-07-27 18:52:21 +03001519 return avail_idx != vq->avail_idx;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001520}
1521
1522/* We don't need to be notified again. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001523void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001524{
1525 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301526
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001527 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
1528 return;
1529 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001530 if (!vhost_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08001531 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001532 if (r)
1533 vq_err(vq, "Failed to enable notification at %p: %d\n",
1534 &vq->used->flags, r);
1535 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001536}