blob: de9441a1b158cf68682604518f3a748c88f110e2 [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
Rob Landley61516582011-05-06 09:27:36 -07007 * Documentation/virtual/lguest/lguest.c, by Rusty Russell
Michael S. Tsirkin3a4d5c92010-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>
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 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. Tsirkin3a4d5c92010-01-14 06:17:27 +000028
Michael S. Tsirkin3a4d5c92010-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. Tsirkin3a4d5c92010-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. Tsirkin3a4d5c92010-01-14 06:17:27 +000043
Krishna Kumard47effe2011-03-01 17:06:37 +053044 poll = container_of(pt, struct vhost_poll, table);
Michael S. Tsirkin3a4d5c92010-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. Tsirkin3a4d5c92010-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. Tsirkin3a4d5c92010-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. Tsirkin3a4d5c92010-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. Tsirkin3a4d5c92010-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. 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. */
Jason Wang2b8b3282013-01-28 01:05:18 +000085int vhost_poll_start(struct vhost_poll *poll, struct file *file)
Michael S. Tsirkin3a4d5c92010-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. Tsirkin3a4d5c92010-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. Tsirkin3a4d5c92010-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. Tsirkin3a4d5c92010-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. Tsirkin3a4d5c92010-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. Tsirkin3a4d5c92010-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. Tsirkin3a4d5c92010-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. Tsirkin3a4d5c92010-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. Tsirkin3a4d5c92010-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. Tsirkin3a4d5c92010-01-14 06:17:27 +0000179 vq->used_flags = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000180 vq->log_used = false;
181 vq->log_addr = -1ull;
Michael S. Tsirkin3a4d5c92010-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. Tsirkin3a4d5c92010-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{
254 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530255
Jason Wange0e9b402010-09-14 23:53:05 +0800256 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800257 dev->vqs[i]->indirect = kmalloc(sizeof *dev->vqs[i]->indirect *
Jason Wange0e9b402010-09-14 23:53:05 +0800258 UIO_MAXIOV, GFP_KERNEL);
Asias He3ab2e422013-04-27 11:16:48 +0800259 dev->vqs[i]->log = kmalloc(sizeof *dev->vqs[i]->log * UIO_MAXIOV,
Jason Wange0e9b402010-09-14 23:53:05 +0800260 GFP_KERNEL);
Asias He3ab2e422013-04-27 11:16:48 +0800261 dev->vqs[i]->heads = kmalloc(sizeof *dev->vqs[i]->heads *
Jason Wange0e9b402010-09-14 23:53:05 +0800262 UIO_MAXIOV, GFP_KERNEL);
Asias He3ab2e422013-04-27 11:16:48 +0800263 if (!dev->vqs[i]->indirect || !dev->vqs[i]->log ||
Asias He28394002013-04-27 15:07:46 +0800264 !dev->vqs[i]->heads)
Jason Wange0e9b402010-09-14 23:53:05 +0800265 goto err_nomem;
266 }
267 return 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530268
Jason Wange0e9b402010-09-14 23:53:05 +0800269err_nomem:
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000270 for (; i >= 0; --i)
Asias He3ab2e422013-04-27 11:16:48 +0800271 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800272 return -ENOMEM;
273}
274
275static void vhost_dev_free_iovecs(struct vhost_dev *dev)
276{
277 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530278
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000279 for (i = 0; i < dev->nvqs; ++i)
Asias He3ab2e422013-04-27 11:16:48 +0800280 vhost_vq_free_iovecs(dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800281}
282
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000283long vhost_dev_init(struct vhost_dev *dev,
Asias He3ab2e422013-04-27 11:16:48 +0800284 struct vhost_virtqueue **vqs, int nvqs)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000285{
286 int i;
Tejun Heoc23f34452010-06-02 20:40:00 +0200287
Michael S. Tsirkin3a4d5c92010-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. Tsirkin3a4d5c92010-01-14 06:17:27 +0000298
299 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800300 dev->vqs[i]->log = NULL;
301 dev->vqs[i]->indirect = NULL;
302 dev->vqs[i]->heads = NULL;
Asias He3ab2e422013-04-27 11:16:48 +0800303 dev->vqs[i]->dev = dev;
304 mutex_init(&dev->vqs[i]->mutex);
305 vhost_vq_reset(dev, dev->vqs[i]);
306 if (dev->vqs[i]->handle_kick)
307 vhost_poll_init(&dev->vqs[i]->poll,
308 dev->vqs[i]->handle_kick, POLLIN, dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000309 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200310
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000311 return 0;
312}
313
314/* Caller should have device mutex */
315long vhost_dev_check_owner(struct vhost_dev *dev)
316{
317 /* Are you the owner? If not, I don't think you mean to do that */
318 return dev->mm == current->mm ? 0 : -EPERM;
319}
320
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300321struct vhost_attach_cgroups_struct {
Krishna Kumard47effe2011-03-01 17:06:37 +0530322 struct vhost_work work;
323 struct task_struct *owner;
324 int ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300325};
326
327static void vhost_attach_cgroups_work(struct vhost_work *work)
328{
Krishna Kumard47effe2011-03-01 17:06:37 +0530329 struct vhost_attach_cgroups_struct *s;
330
331 s = container_of(work, struct vhost_attach_cgroups_struct, work);
332 s->ret = cgroup_attach_task_all(s->owner, current);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300333}
334
335static int vhost_attach_cgroups(struct vhost_dev *dev)
336{
Krishna Kumard47effe2011-03-01 17:06:37 +0530337 struct vhost_attach_cgroups_struct attach;
338
339 attach.owner = current;
340 vhost_work_init(&attach.work, vhost_attach_cgroups_work);
341 vhost_work_queue(dev, &attach.work);
342 vhost_work_flush(dev, &attach.work);
343 return attach.ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300344}
345
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000346/* Caller should have device mutex */
Asias He54db63c2013-05-06 11:15:59 +0800347long vhost_dev_set_owner(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000348{
Tejun Heoc23f34452010-06-02 20:40:00 +0200349 struct task_struct *worker;
350 int err;
Krishna Kumard47effe2011-03-01 17:06:37 +0530351
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000352 /* Is there an owner already? */
Tejun Heoc23f34452010-06-02 20:40:00 +0200353 if (dev->mm) {
354 err = -EBUSY;
355 goto err_mm;
356 }
Krishna Kumard47effe2011-03-01 17:06:37 +0530357
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000358 /* No owner, become one */
359 dev->mm = get_task_mm(current);
Tejun Heoc23f34452010-06-02 20:40:00 +0200360 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
361 if (IS_ERR(worker)) {
362 err = PTR_ERR(worker);
363 goto err_worker;
364 }
365
366 dev->worker = worker;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300367 wake_up_process(worker); /* avoid contributing to loadavg */
368
369 err = vhost_attach_cgroups(dev);
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300370 if (err)
371 goto err_cgroup;
Tejun Heoc23f34452010-06-02 20:40:00 +0200372
Jason Wange0e9b402010-09-14 23:53:05 +0800373 err = vhost_dev_alloc_iovecs(dev);
374 if (err)
375 goto err_cgroup;
376
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000377 return 0;
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300378err_cgroup:
379 kthread_stop(worker);
Michael S. Tsirkin615cc222010-09-02 14:16:36 +0300380 dev->worker = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200381err_worker:
382 if (dev->mm)
383 mmput(dev->mm);
384 dev->mm = NULL;
385err_mm:
386 return err;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000387}
388
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300389struct vhost_memory *vhost_dev_reset_owner_prepare(void)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000390{
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300391 return kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
392}
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000393
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300394/* Caller should have device mutex */
395void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_memory *memory)
396{
Michael S. Tsirkinea5d4042011-11-27 19:05:58 +0200397 vhost_dev_cleanup(dev, true);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000398
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +0300399 /* Restore memory to default empty mapping. */
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000400 memory->nregions = 0;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100401 RCU_INIT_POINTER(dev->memory, memory);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000402}
403
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000404void vhost_dev_stop(struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000405{
406 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530407
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000408 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800409 if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) {
410 vhost_poll_stop(&dev->vqs[i]->poll);
411 vhost_poll_flush(&dev->vqs[i]->poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000412 }
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000413 }
414}
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000415
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000416/* Caller should have device mutex if and only if locked is set */
417void vhost_dev_cleanup(struct vhost_dev *dev, bool locked)
418{
419 int i;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000420
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000421 for (i = 0; i < dev->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800422 if (dev->vqs[i]->error_ctx)
423 eventfd_ctx_put(dev->vqs[i]->error_ctx);
424 if (dev->vqs[i]->error)
425 fput(dev->vqs[i]->error);
426 if (dev->vqs[i]->kick)
427 fput(dev->vqs[i]->kick);
428 if (dev->vqs[i]->call_ctx)
429 eventfd_ctx_put(dev->vqs[i]->call_ctx);
430 if (dev->vqs[i]->call)
431 fput(dev->vqs[i]->call);
432 vhost_vq_reset(dev, dev->vqs[i]);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000433 }
Jason Wange0e9b402010-09-14 23:53:05 +0800434 vhost_dev_free_iovecs(dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000435 if (dev->log_ctx)
436 eventfd_ctx_put(dev->log_ctx);
437 dev->log_ctx = NULL;
438 if (dev->log_file)
439 fput(dev->log_file);
440 dev->log_file = NULL;
441 /* No one will access memory at this point */
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100442 kfree(rcu_dereference_protected(dev->memory,
Michael S. Tsirkinea5d4042011-11-27 19:05:58 +0200443 locked ==
444 lockdep_is_held(&dev->mutex)));
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100445 RCU_INIT_POINTER(dev->memory, NULL);
Tejun Heoc23f34452010-06-02 20:40:00 +0200446 WARN_ON(!list_empty(&dev->work_list));
Eric Dumazet78b620c2010-08-31 02:05:57 +0000447 if (dev->worker) {
448 kthread_stop(dev->worker);
449 dev->worker = NULL;
450 }
Michael S. Tsirkin533a19b2010-10-06 15:34:38 +0200451 if (dev->mm)
452 mmput(dev->mm);
453 dev->mm = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000454}
455
456static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
457{
458 u64 a = addr / VHOST_PAGE_SIZE / 8;
Krishna Kumard47effe2011-03-01 17:06:37 +0530459
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000460 /* Make sure 64 bit math will not overflow. */
461 if (a > ULONG_MAX - (unsigned long)log_base ||
462 a + (unsigned long)log_base > ULONG_MAX)
Dan Carpenter6d97e552010-10-11 19:24:19 +0200463 return 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000464
465 return access_ok(VERIFY_WRITE, log_base + a,
466 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
467}
468
469/* Caller should have vq mutex and device mutex. */
470static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
471 int log_all)
472{
473 int i;
Jeff Dike179b2842010-04-07 09:59:10 -0400474
Michael S. Tsirkinf8322fb2010-05-27 12:28:03 +0300475 if (!mem)
476 return 0;
Jeff Dike179b2842010-04-07 09:59:10 -0400477
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000478 for (i = 0; i < mem->nregions; ++i) {
479 struct vhost_memory_region *m = mem->regions + i;
480 unsigned long a = m->userspace_addr;
481 if (m->memory_size > ULONG_MAX)
482 return 0;
483 else if (!access_ok(VERIFY_WRITE, (void __user *)a,
484 m->memory_size))
485 return 0;
486 else if (log_all && !log_access_ok(log_base,
487 m->guest_phys_addr,
488 m->memory_size))
489 return 0;
490 }
491 return 1;
492}
493
494/* Can we switch to this memory table? */
495/* Caller should have device mutex but not vq mutex */
496static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
497 int log_all)
498{
499 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530500
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000501 for (i = 0; i < d->nvqs; ++i) {
502 int ok;
Asias He3ab2e422013-04-27 11:16:48 +0800503 mutex_lock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000504 /* If ring is inactive, will check when it's enabled. */
Asias He3ab2e422013-04-27 11:16:48 +0800505 if (d->vqs[i]->private_data)
506 ok = vq_memory_access_ok(d->vqs[i]->log_base, mem,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000507 log_all);
508 else
509 ok = 1;
Asias He3ab2e422013-04-27 11:16:48 +0800510 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000511 if (!ok)
512 return 0;
513 }
514 return 1;
515}
516
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300517static int vq_access_ok(struct vhost_dev *d, unsigned int num,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000518 struct vring_desc __user *desc,
519 struct vring_avail __user *avail,
520 struct vring_used __user *used)
521{
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300522 size_t s = vhost_has_feature(d, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000523 return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
524 access_ok(VERIFY_READ, avail,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300525 sizeof *avail + num * sizeof *avail->ring + s) &&
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000526 access_ok(VERIFY_WRITE, used,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300527 sizeof *used + num * sizeof *used->ring + s);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000528}
529
530/* Can we log writes? */
531/* Caller should have device mutex but not vq mutex */
532int vhost_log_access_ok(struct vhost_dev *dev)
533{
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100534 struct vhost_memory *mp;
535
536 mp = rcu_dereference_protected(dev->memory,
537 lockdep_is_held(&dev->mutex));
538 return memory_access_ok(dev, mp, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000539}
540
541/* Verify access for write logging. */
542/* Caller should have vq mutex and device mutex */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300543static int vq_log_access_ok(struct vhost_dev *d, struct vhost_virtqueue *vq,
544 void __user *log_base)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000545{
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100546 struct vhost_memory *mp;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300547 size_t s = vhost_has_feature(d, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100548
549 mp = rcu_dereference_protected(vq->dev->memory,
550 lockdep_is_held(&vq->mutex));
551 return vq_memory_access_ok(log_base, mp,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000552 vhost_has_feature(vq->dev, VHOST_F_LOG_ALL)) &&
553 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
554 sizeof *vq->used +
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300555 vq->num * sizeof *vq->used->ring + s));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000556}
557
558/* Can we start vq? */
559/* Caller should have vq mutex and device mutex */
560int vhost_vq_access_ok(struct vhost_virtqueue *vq)
561{
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300562 return vq_access_ok(vq->dev, vq->num, vq->desc, vq->avail, vq->used) &&
563 vq_log_access_ok(vq->dev, vq, vq->log_base);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000564}
565
566static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
567{
568 struct vhost_memory mem, *newmem, *oldmem;
569 unsigned long size = offsetof(struct vhost_memory, regions);
Krishna Kumard47effe2011-03-01 17:06:37 +0530570
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900571 if (copy_from_user(&mem, m, size))
572 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000573 if (mem.padding)
574 return -EOPNOTSUPP;
575 if (mem.nregions > VHOST_MEMORY_MAX_NREGIONS)
576 return -E2BIG;
577 newmem = kmalloc(size + mem.nregions * sizeof *m->regions, GFP_KERNEL);
578 if (!newmem)
579 return -ENOMEM;
580
581 memcpy(newmem, &mem, size);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900582 if (copy_from_user(newmem->regions, m->regions,
583 mem.nregions * sizeof *m->regions)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000584 kfree(newmem);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900585 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000586 }
587
Krishna Kumard47effe2011-03-01 17:06:37 +0530588 if (!memory_access_ok(d, newmem,
589 vhost_has_feature(d, VHOST_F_LOG_ALL))) {
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +0900590 kfree(newmem);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000591 return -EFAULT;
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +0900592 }
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100593 oldmem = rcu_dereference_protected(d->memory,
594 lockdep_is_held(&d->mutex));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000595 rcu_assign_pointer(d->memory, newmem);
596 synchronize_rcu();
597 kfree(oldmem);
598 return 0;
599}
600
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200601long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000602{
Al Virocecb46f2012-08-27 14:21:39 -0400603 struct file *eventfp, *filep = NULL;
604 bool pollstart = false, pollstop = false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000605 struct eventfd_ctx *ctx = NULL;
606 u32 __user *idxp = argp;
607 struct vhost_virtqueue *vq;
608 struct vhost_vring_state s;
609 struct vhost_vring_file f;
610 struct vhost_vring_addr a;
611 u32 idx;
612 long r;
613
614 r = get_user(idx, idxp);
615 if (r < 0)
616 return r;
Krishna Kumar0f3d9a12010-05-25 11:10:36 +0530617 if (idx >= d->nvqs)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000618 return -ENOBUFS;
619
Asias He3ab2e422013-04-27 11:16:48 +0800620 vq = d->vqs[idx];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000621
622 mutex_lock(&vq->mutex);
623
624 switch (ioctl) {
625 case VHOST_SET_VRING_NUM:
626 /* Resizing ring with an active backend?
627 * You don't want to do that. */
628 if (vq->private_data) {
629 r = -EBUSY;
630 break;
631 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900632 if (copy_from_user(&s, argp, sizeof s)) {
633 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000634 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900635 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000636 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
637 r = -EINVAL;
638 break;
639 }
640 vq->num = s.num;
641 break;
642 case VHOST_SET_VRING_BASE:
643 /* Moving base with an active backend?
644 * You don't want to do that. */
645 if (vq->private_data) {
646 r = -EBUSY;
647 break;
648 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900649 if (copy_from_user(&s, argp, sizeof s)) {
650 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000651 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900652 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000653 if (s.num > 0xffff) {
654 r = -EINVAL;
655 break;
656 }
657 vq->last_avail_idx = s.num;
658 /* Forget the cached index value. */
659 vq->avail_idx = vq->last_avail_idx;
660 break;
661 case VHOST_GET_VRING_BASE:
662 s.index = idx;
663 s.num = vq->last_avail_idx;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900664 if (copy_to_user(argp, &s, sizeof s))
665 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000666 break;
667 case VHOST_SET_VRING_ADDR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900668 if (copy_from_user(&a, argp, sizeof a)) {
669 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000670 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900671 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000672 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
673 r = -EOPNOTSUPP;
674 break;
675 }
676 /* For 32bit, verify that the top 32bits of the user
677 data are set to zero. */
678 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
679 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
680 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
681 r = -EFAULT;
682 break;
683 }
684 if ((a.avail_user_addr & (sizeof *vq->avail->ring - 1)) ||
685 (a.used_user_addr & (sizeof *vq->used->ring - 1)) ||
686 (a.log_guest_addr & (sizeof *vq->used->ring - 1))) {
687 r = -EINVAL;
688 break;
689 }
690
691 /* We only verify access here if backend is configured.
692 * If it is not, we don't as size might not have been setup.
693 * We will verify when backend is configured. */
694 if (vq->private_data) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300695 if (!vq_access_ok(d, vq->num,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000696 (void __user *)(unsigned long)a.desc_user_addr,
697 (void __user *)(unsigned long)a.avail_user_addr,
698 (void __user *)(unsigned long)a.used_user_addr)) {
699 r = -EINVAL;
700 break;
701 }
702
703 /* Also validate log access for used ring if enabled. */
704 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
705 !log_access_ok(vq->log_base, a.log_guest_addr,
706 sizeof *vq->used +
707 vq->num * sizeof *vq->used->ring)) {
708 r = -EINVAL;
709 break;
710 }
711 }
712
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000713 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
714 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
715 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
716 vq->log_addr = a.log_guest_addr;
717 vq->used = (void __user *)(unsigned long)a.used_user_addr;
718 break;
719 case VHOST_SET_VRING_KICK:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900720 if (copy_from_user(&f, argp, sizeof f)) {
721 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000722 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900723 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000724 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200725 if (IS_ERR(eventfp)) {
726 r = PTR_ERR(eventfp);
727 break;
728 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000729 if (eventfp != vq->kick) {
Al Virocecb46f2012-08-27 14:21:39 -0400730 pollstop = (filep = vq->kick) != NULL;
731 pollstart = (vq->kick = eventfp) != NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000732 } else
733 filep = eventfp;
734 break;
735 case VHOST_SET_VRING_CALL:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900736 if (copy_from_user(&f, argp, sizeof f)) {
737 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000738 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900739 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000740 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200741 if (IS_ERR(eventfp)) {
742 r = PTR_ERR(eventfp);
743 break;
744 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000745 if (eventfp != vq->call) {
746 filep = vq->call;
747 ctx = vq->call_ctx;
748 vq->call = eventfp;
749 vq->call_ctx = eventfp ?
750 eventfd_ctx_fileget(eventfp) : NULL;
751 } else
752 filep = eventfp;
753 break;
754 case VHOST_SET_VRING_ERR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900755 if (copy_from_user(&f, argp, sizeof f)) {
756 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000757 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900758 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000759 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200760 if (IS_ERR(eventfp)) {
761 r = PTR_ERR(eventfp);
762 break;
763 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000764 if (eventfp != vq->error) {
765 filep = vq->error;
766 vq->error = eventfp;
767 ctx = vq->error_ctx;
768 vq->error_ctx = eventfp ?
769 eventfd_ctx_fileget(eventfp) : NULL;
770 } else
771 filep = eventfp;
772 break;
773 default:
774 r = -ENOIOCTLCMD;
775 }
776
777 if (pollstop && vq->handle_kick)
778 vhost_poll_stop(&vq->poll);
779
780 if (ctx)
781 eventfd_ctx_put(ctx);
782 if (filep)
783 fput(filep);
784
785 if (pollstart && vq->handle_kick)
Jason Wang2b8b3282013-01-28 01:05:18 +0000786 r = vhost_poll_start(&vq->poll, vq->kick);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000787
788 mutex_unlock(&vq->mutex);
789
790 if (pollstop && vq->handle_kick)
791 vhost_poll_flush(&vq->poll);
792 return r;
793}
794
795/* Caller must have device mutex */
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200796long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000797{
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000798 struct file *eventfp, *filep = NULL;
799 struct eventfd_ctx *ctx = NULL;
800 u64 p;
801 long r;
802 int i, fd;
803
804 /* If you are not the owner, you can become one */
805 if (ioctl == VHOST_SET_OWNER) {
806 r = vhost_dev_set_owner(d);
807 goto done;
808 }
809
810 /* You must be the owner to do anything else */
811 r = vhost_dev_check_owner(d);
812 if (r)
813 goto done;
814
815 switch (ioctl) {
816 case VHOST_SET_MEM_TABLE:
817 r = vhost_set_memory(d, argp);
818 break;
819 case VHOST_SET_LOG_BASE:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900820 if (copy_from_user(&p, argp, sizeof p)) {
821 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000822 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900823 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000824 if ((u64)(unsigned long)p != p) {
825 r = -EFAULT;
826 break;
827 }
828 for (i = 0; i < d->nvqs; ++i) {
829 struct vhost_virtqueue *vq;
830 void __user *base = (void __user *)(unsigned long)p;
Asias He3ab2e422013-04-27 11:16:48 +0800831 vq = d->vqs[i];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000832 mutex_lock(&vq->mutex);
833 /* If ring is inactive, will check when it's enabled. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300834 if (vq->private_data && !vq_log_access_ok(d, vq, base))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000835 r = -EFAULT;
836 else
837 vq->log_base = base;
838 mutex_unlock(&vq->mutex);
839 }
840 break;
841 case VHOST_SET_LOG_FD:
842 r = get_user(fd, (int __user *)argp);
843 if (r < 0)
844 break;
845 eventfp = fd == -1 ? NULL : eventfd_fget(fd);
846 if (IS_ERR(eventfp)) {
847 r = PTR_ERR(eventfp);
848 break;
849 }
850 if (eventfp != d->log_file) {
851 filep = d->log_file;
852 ctx = d->log_ctx;
853 d->log_ctx = eventfp ?
854 eventfd_ctx_fileget(eventfp) : NULL;
855 } else
856 filep = eventfp;
857 for (i = 0; i < d->nvqs; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800858 mutex_lock(&d->vqs[i]->mutex);
859 d->vqs[i]->log_ctx = d->log_ctx;
860 mutex_unlock(&d->vqs[i]->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000861 }
862 if (ctx)
863 eventfd_ctx_put(ctx);
864 if (filep)
865 fput(filep);
866 break;
867 default:
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200868 r = -ENOIOCTLCMD;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000869 break;
870 }
871done:
872 return r;
873}
874
875static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
876 __u64 addr, __u32 len)
877{
878 struct vhost_memory_region *reg;
879 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530880
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000881 /* linear search is not brilliant, but we really have on the order of 6
882 * regions in practice */
883 for (i = 0; i < mem->nregions; ++i) {
884 reg = mem->regions + i;
885 if (reg->guest_phys_addr <= addr &&
886 reg->guest_phys_addr + reg->memory_size - 1 >= addr)
887 return reg;
888 }
889 return NULL;
890}
891
892/* TODO: This is really inefficient. We need something like get_user()
893 * (instruction directly accesses the data, with an exception table entry
894 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
895 */
896static int set_bit_to_user(int nr, void __user *addr)
897{
898 unsigned long log = (unsigned long)addr;
899 struct page *page;
900 void *base;
901 int bit = nr + (log % PAGE_SIZE) * 8;
902 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +0530903
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000904 r = get_user_pages_fast(log, 1, 1, &page);
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +0200905 if (r < 0)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000906 return r;
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +0200907 BUG_ON(r != 1);
Cong Wangc6daa7f2011-11-25 23:14:26 +0800908 base = kmap_atomic(page);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000909 set_bit(bit, base);
Cong Wangc6daa7f2011-11-25 23:14:26 +0800910 kunmap_atomic(base);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000911 set_page_dirty_lock(page);
912 put_page(page);
913 return 0;
914}
915
916static int log_write(void __user *log_base,
917 u64 write_address, u64 write_length)
918{
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +0200919 u64 write_page = write_address / VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000920 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +0530921
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000922 if (!write_length)
923 return 0;
Michael S. Tsirkin3bf9be42010-11-29 10:19:07 +0200924 write_length += write_address % VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000925 for (;;) {
926 u64 base = (u64)(unsigned long)log_base;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +0200927 u64 log = base + write_page / 8;
928 int bit = write_page % 8;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000929 if ((u64)(unsigned long)log != log)
930 return -EFAULT;
931 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
932 if (r < 0)
933 return r;
934 if (write_length <= VHOST_PAGE_SIZE)
935 break;
936 write_length -= VHOST_PAGE_SIZE;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +0200937 write_page += 1;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000938 }
939 return r;
940}
941
942int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
943 unsigned int log_num, u64 len)
944{
945 int i, r;
946
947 /* Make sure data written is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +0000948 smp_wmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000949 for (i = 0; i < log_num; ++i) {
950 u64 l = min(log[i].len, len);
951 r = log_write(vq->log_base, log[i].addr, l);
952 if (r < 0)
953 return r;
954 len -= l;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +0200955 if (!len) {
956 if (vq->log_ctx)
957 eventfd_signal(vq->log_ctx, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000958 return 0;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +0200959 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000960 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000961 /* Length written exceeds what we have stored. This is a bug. */
962 BUG();
963 return 0;
964}
965
Jason Wang2723fea2011-06-21 18:04:38 +0800966static int vhost_update_used_flags(struct vhost_virtqueue *vq)
967{
968 void __user *used;
Michael S. Tsirkinb8342262011-07-19 17:15:43 +0300969 if (__put_user(vq->used_flags, &vq->used->flags) < 0)
Jason Wang2723fea2011-06-21 18:04:38 +0800970 return -EFAULT;
971 if (unlikely(vq->log_used)) {
972 /* Make sure the flag is seen before log. */
973 smp_wmb();
974 /* Log used flag write. */
975 used = &vq->used->flags;
976 log_write(vq->log_base, vq->log_addr +
977 (used - (void __user *)vq->used),
978 sizeof vq->used->flags);
979 if (vq->log_ctx)
980 eventfd_signal(vq->log_ctx, 1);
981 }
982 return 0;
983}
984
985static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
986{
Michael S. Tsirkinb8342262011-07-19 17:15:43 +0300987 if (__put_user(vq->avail_idx, vhost_avail_event(vq)))
Jason Wang2723fea2011-06-21 18:04:38 +0800988 return -EFAULT;
989 if (unlikely(vq->log_used)) {
990 void __user *used;
991 /* Make sure the event is seen before log. */
992 smp_wmb();
993 /* Log avail event write */
994 used = vhost_avail_event(vq);
995 log_write(vq->log_base, vq->log_addr +
996 (used - (void __user *)vq->used),
997 sizeof *vhost_avail_event(vq));
998 if (vq->log_ctx)
999 eventfd_signal(vq->log_ctx, 1);
1000 }
1001 return 0;
1002}
1003
1004int vhost_init_used(struct vhost_virtqueue *vq)
1005{
1006 int r;
1007 if (!vq->private_data)
1008 return 0;
1009
1010 r = vhost_update_used_flags(vq);
1011 if (r)
1012 return r;
1013 vq->signalled_used_valid = false;
1014 return get_user(vq->last_used_idx, &vq->used->idx);
1015}
1016
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001017static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
1018 struct iovec iov[], int iov_size)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001019{
1020 const struct vhost_memory_region *reg;
1021 struct vhost_memory *mem;
1022 struct iovec *_iov;
1023 u64 s = 0;
1024 int ret = 0;
1025
1026 rcu_read_lock();
1027
1028 mem = rcu_dereference(dev->memory);
1029 while ((u64)len > s) {
1030 u64 size;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001031 if (unlikely(ret >= iov_size)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001032 ret = -ENOBUFS;
1033 break;
1034 }
1035 reg = find_region(mem, addr, len);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001036 if (unlikely(!reg)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001037 ret = -EFAULT;
1038 break;
1039 }
1040 _iov = iov + ret;
1041 size = reg->memory_size - addr + reg->guest_phys_addr;
Michael S. Tsirkinbd971202012-11-26 05:57:27 +00001042 _iov->iov_len = min((u64)len - s, size);
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001043 _iov->iov_base = (void __user *)(unsigned long)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001044 (reg->userspace_addr + addr - reg->guest_phys_addr);
1045 s += size;
1046 addr += size;
1047 ++ret;
1048 }
1049
1050 rcu_read_unlock();
1051 return ret;
1052}
1053
1054/* Each buffer in the virtqueues is actually a chain of descriptors. This
1055 * function returns the next descriptor in the chain,
1056 * or -1U if we're at the end. */
1057static unsigned next_desc(struct vring_desc *desc)
1058{
1059 unsigned int next;
1060
1061 /* If this descriptor says it doesn't chain, we're done. */
1062 if (!(desc->flags & VRING_DESC_F_NEXT))
1063 return -1U;
1064
1065 /* Check they're not leading us off end of descriptors. */
1066 next = desc->next;
1067 /* Make sure compiler knows to grab that: we don't want it changing! */
1068 /* We will use the result as an index in an array, so most
1069 * architectures only need a compiler barrier here. */
1070 read_barrier_depends();
1071
1072 return next;
1073}
1074
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001075static int get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
1076 struct iovec iov[], unsigned int iov_size,
1077 unsigned int *out_num, unsigned int *in_num,
1078 struct vhost_log *log, unsigned int *log_num,
1079 struct vring_desc *indirect)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001080{
1081 struct vring_desc desc;
1082 unsigned int i = 0, count, found = 0;
1083 int ret;
1084
1085 /* Sanity check */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001086 if (unlikely(indirect->len % sizeof desc)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001087 vq_err(vq, "Invalid length in indirect descriptor: "
1088 "len 0x%llx not multiple of 0x%zx\n",
1089 (unsigned long long)indirect->len,
1090 sizeof desc);
1091 return -EINVAL;
1092 }
1093
1094 ret = translate_desc(dev, indirect->addr, indirect->len, vq->indirect,
Jason Wange0e9b402010-09-14 23:53:05 +08001095 UIO_MAXIOV);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001096 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001097 vq_err(vq, "Translation failure %d in indirect.\n", ret);
1098 return ret;
1099 }
1100
1101 /* We will use the result as an address to read from, so most
1102 * architectures only need a compiler barrier here. */
1103 read_barrier_depends();
1104
1105 count = indirect->len / sizeof desc;
1106 /* Buffers are chained via a 16 bit next field, so
1107 * we can have at most 2^16 of these. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001108 if (unlikely(count > USHRT_MAX + 1)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001109 vq_err(vq, "Indirect buffer length too big: %d\n",
1110 indirect->len);
1111 return -E2BIG;
1112 }
1113
1114 do {
1115 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001116 if (unlikely(++found > count)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001117 vq_err(vq, "Loop detected: last one at %u "
1118 "indirect size %u\n",
1119 i, count);
1120 return -EINVAL;
1121 }
Krishna Kumard47effe2011-03-01 17:06:37 +05301122 if (unlikely(memcpy_fromiovec((unsigned char *)&desc,
1123 vq->indirect, sizeof desc))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001124 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
1125 i, (size_t)indirect->addr + i * sizeof desc);
1126 return -EINVAL;
1127 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001128 if (unlikely(desc.flags & VRING_DESC_F_INDIRECT)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001129 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
1130 i, (size_t)indirect->addr + i * sizeof desc);
1131 return -EINVAL;
1132 }
1133
1134 ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
1135 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001136 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001137 vq_err(vq, "Translation failure %d indirect idx %d\n",
1138 ret, i);
1139 return ret;
1140 }
1141 /* If this is an input descriptor, increment that count. */
1142 if (desc.flags & VRING_DESC_F_WRITE) {
1143 *in_num += ret;
1144 if (unlikely(log)) {
1145 log[*log_num].addr = desc.addr;
1146 log[*log_num].len = desc.len;
1147 ++*log_num;
1148 }
1149 } else {
1150 /* If it's an output descriptor, they're all supposed
1151 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001152 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001153 vq_err(vq, "Indirect descriptor "
1154 "has out after in: idx %d\n", i);
1155 return -EINVAL;
1156 }
1157 *out_num += ret;
1158 }
1159 } while ((i = next_desc(&desc)) != -1);
1160 return 0;
1161}
1162
1163/* This looks in the virtqueue and for the first available buffer, and converts
1164 * it to an iovec for convenient access. Since descriptors consist of some
1165 * number of output then some number of input descriptors, it's actually two
1166 * iovecs, but we pack them into one and note how many of each there were.
1167 *
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001168 * This function returns the descriptor number found, or vq->num (which is
1169 * never a valid descriptor number) if none was found. A negative code is
1170 * returned on error. */
1171int vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
1172 struct iovec iov[], unsigned int iov_size,
1173 unsigned int *out_num, unsigned int *in_num,
1174 struct vhost_log *log, unsigned int *log_num)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001175{
1176 struct vring_desc desc;
1177 unsigned int i, head, found = 0;
1178 u16 last_avail_idx;
1179 int ret;
1180
1181 /* Check it isn't doing very strange things with descriptor numbers. */
1182 last_avail_idx = vq->last_avail_idx;
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001183 if (unlikely(__get_user(vq->avail_idx, &vq->avail->idx))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001184 vq_err(vq, "Failed to access avail idx at %p\n",
1185 &vq->avail->idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001186 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001187 }
1188
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001189 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001190 vq_err(vq, "Guest moved used index from %u to %u",
1191 last_avail_idx, vq->avail_idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001192 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001193 }
1194
1195 /* If there's nothing new since last we looked, return invalid. */
1196 if (vq->avail_idx == last_avail_idx)
1197 return vq->num;
1198
1199 /* Only get avail ring entries after they have been exposed by guest. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001200 smp_rmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001201
1202 /* Grab the next descriptor number they're advertising, and increment
1203 * the index we've seen. */
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001204 if (unlikely(__get_user(head,
1205 &vq->avail->ring[last_avail_idx % vq->num]))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001206 vq_err(vq, "Failed to read head: idx %d address %p\n",
1207 last_avail_idx,
1208 &vq->avail->ring[last_avail_idx % vq->num]);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001209 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001210 }
1211
1212 /* If their number is silly, that's an error. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001213 if (unlikely(head >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001214 vq_err(vq, "Guest says index %u > %u is available",
1215 head, vq->num);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001216 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001217 }
1218
1219 /* When we start there are none of either input nor output. */
1220 *out_num = *in_num = 0;
1221 if (unlikely(log))
1222 *log_num = 0;
1223
1224 i = head;
1225 do {
1226 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001227 if (unlikely(i >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001228 vq_err(vq, "Desc index is %u > %u, head = %u",
1229 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001230 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001231 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001232 if (unlikely(++found > vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001233 vq_err(vq, "Loop detected: last one at %u "
1234 "vq size %u head %u\n",
1235 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001236 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001237 }
Michael S. Tsirkinfcc042a2011-03-06 13:33:49 +02001238 ret = __copy_from_user(&desc, vq->desc + i, sizeof desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001239 if (unlikely(ret)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001240 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
1241 i, vq->desc + i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001242 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001243 }
1244 if (desc.flags & VRING_DESC_F_INDIRECT) {
1245 ret = get_indirect(dev, vq, iov, iov_size,
1246 out_num, in_num,
1247 log, log_num, &desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001248 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001249 vq_err(vq, "Failure detected "
1250 "in indirect descriptor at idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001251 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001252 }
1253 continue;
1254 }
1255
1256 ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
1257 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001258 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001259 vq_err(vq, "Translation failure %d descriptor idx %d\n",
1260 ret, i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001261 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001262 }
1263 if (desc.flags & VRING_DESC_F_WRITE) {
1264 /* If this is an input descriptor,
1265 * increment that count. */
1266 *in_num += ret;
1267 if (unlikely(log)) {
1268 log[*log_num].addr = desc.addr;
1269 log[*log_num].len = desc.len;
1270 ++*log_num;
1271 }
1272 } else {
1273 /* If it's an output descriptor, they're all supposed
1274 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001275 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001276 vq_err(vq, "Descriptor has out after in: "
1277 "idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001278 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001279 }
1280 *out_num += ret;
1281 }
1282 } while ((i = next_desc(&desc)) != -1);
1283
1284 /* On success, increment avail index. */
1285 vq->last_avail_idx++;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001286
1287 /* Assume notifications from guest are disabled at this point,
1288 * if they aren't we would need to update avail_event index. */
1289 BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001290 return head;
1291}
1292
1293/* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
David Stevens8dd014a2010-07-27 18:52:21 +03001294void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001295{
David Stevens8dd014a2010-07-27 18:52:21 +03001296 vq->last_avail_idx -= n;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001297}
1298
1299/* After we've used one of their buffers, we tell them about it. We'll then
1300 * want to notify the guest, using eventfd. */
1301int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
1302{
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001303 struct vring_used_elem __user *used;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001304
1305 /* The virtqueue contains a ring of used buffers. Get a pointer to the
1306 * next entry in that used ring. */
1307 used = &vq->used->ring[vq->last_used_idx % vq->num];
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001308 if (__put_user(head, &used->id)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001309 vq_err(vq, "Failed to write used id");
1310 return -EFAULT;
1311 }
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001312 if (__put_user(len, &used->len)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001313 vq_err(vq, "Failed to write used len");
1314 return -EFAULT;
1315 }
1316 /* Make sure buffer is written before we update index. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001317 smp_wmb();
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001318 if (__put_user(vq->last_used_idx + 1, &vq->used->idx)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001319 vq_err(vq, "Failed to increment used idx");
1320 return -EFAULT;
1321 }
1322 if (unlikely(vq->log_used)) {
1323 /* Make sure data is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001324 smp_wmb();
Michael S. Tsirkin86e94242010-02-17 19:11:33 +02001325 /* Log used ring entry write. */
1326 log_write(vq->log_base,
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001327 vq->log_addr +
1328 ((void __user *)used - (void __user *)vq->used),
Michael S. Tsirkin86e94242010-02-17 19:11:33 +02001329 sizeof *used);
1330 /* Log used index update. */
1331 log_write(vq->log_base,
1332 vq->log_addr + offsetof(struct vring_used, idx),
1333 sizeof vq->used->idx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001334 if (vq->log_ctx)
1335 eventfd_signal(vq->log_ctx, 1);
1336 }
1337 vq->last_used_idx++;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001338 /* If the driver never bothers to signal in a very long while,
1339 * used index might wrap around. If that happens, invalidate
1340 * signalled_used index we stored. TODO: make sure driver
1341 * signals at least once in 2^16 and remove this. */
1342 if (unlikely(vq->last_used_idx == vq->signalled_used))
1343 vq->signalled_used_valid = false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001344 return 0;
1345}
1346
David Stevens8dd014a2010-07-27 18:52:21 +03001347static int __vhost_add_used_n(struct vhost_virtqueue *vq,
1348 struct vring_used_elem *heads,
1349 unsigned count)
1350{
1351 struct vring_used_elem __user *used;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001352 u16 old, new;
David Stevens8dd014a2010-07-27 18:52:21 +03001353 int start;
1354
1355 start = vq->last_used_idx % vq->num;
1356 used = vq->used->ring + start;
Michael S. Tsirkindfe5ac52010-09-21 14:18:01 +02001357 if (__copy_to_user(used, heads, count * sizeof *used)) {
David Stevens8dd014a2010-07-27 18:52:21 +03001358 vq_err(vq, "Failed to write used");
1359 return -EFAULT;
1360 }
1361 if (unlikely(vq->log_used)) {
1362 /* Make sure data is seen before log. */
1363 smp_wmb();
1364 /* Log used ring entry write. */
1365 log_write(vq->log_base,
1366 vq->log_addr +
1367 ((void __user *)used - (void __user *)vq->used),
1368 count * sizeof *used);
1369 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001370 old = vq->last_used_idx;
1371 new = (vq->last_used_idx += count);
1372 /* If the driver never bothers to signal in a very long while,
1373 * used index might wrap around. If that happens, invalidate
1374 * signalled_used index we stored. TODO: make sure driver
1375 * signals at least once in 2^16 and remove this. */
1376 if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old)))
1377 vq->signalled_used_valid = false;
David Stevens8dd014a2010-07-27 18:52:21 +03001378 return 0;
1379}
1380
1381/* After we've used one of their buffers, we tell them about it. We'll then
1382 * want to notify the guest, using eventfd. */
1383int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
1384 unsigned count)
1385{
1386 int start, n, r;
1387
1388 start = vq->last_used_idx % vq->num;
1389 n = vq->num - start;
1390 if (n < count) {
1391 r = __vhost_add_used_n(vq, heads, n);
1392 if (r < 0)
1393 return r;
1394 heads += n;
1395 count -= n;
1396 }
1397 r = __vhost_add_used_n(vq, heads, count);
1398
1399 /* Make sure buffer is written before we update index. */
1400 smp_wmb();
1401 if (put_user(vq->last_used_idx, &vq->used->idx)) {
1402 vq_err(vq, "Failed to increment used idx");
1403 return -EFAULT;
1404 }
1405 if (unlikely(vq->log_used)) {
1406 /* Log used index update. */
1407 log_write(vq->log_base,
1408 vq->log_addr + offsetof(struct vring_used, idx),
1409 sizeof vq->used->idx);
1410 if (vq->log_ctx)
1411 eventfd_signal(vq->log_ctx, 1);
1412 }
1413 return r;
1414}
1415
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001416static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001417{
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001418 __u16 old, new, event;
1419 bool v;
Michael S. Tsirkin0d499352010-05-11 19:44:17 +03001420 /* Flush out used index updates. This is paired
1421 * with the barrier that the Guest executes when enabling
1422 * interrupts. */
1423 smp_mb();
1424
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001425 if (vhost_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY) &&
1426 unlikely(vq->avail_idx == vq->last_avail_idx))
1427 return true;
1428
1429 if (!vhost_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
1430 __u16 flags;
1431 if (__get_user(flags, &vq->avail->flags)) {
1432 vq_err(vq, "Failed to get flags");
1433 return true;
1434 }
1435 return !(flags & VRING_AVAIL_F_NO_INTERRUPT);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001436 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001437 old = vq->signalled_used;
1438 v = vq->signalled_used_valid;
1439 new = vq->signalled_used = vq->last_used_idx;
1440 vq->signalled_used_valid = true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001441
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001442 if (unlikely(!v))
1443 return true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001444
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001445 if (get_user(event, vhost_used_event(vq))) {
1446 vq_err(vq, "Failed to get used event idx");
1447 return true;
1448 }
1449 return vring_need_event(event, new, old);
1450}
1451
1452/* This actually signals the guest, using eventfd. */
1453void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
1454{
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001455 /* Signal the Guest tell them we used something up. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001456 if (vq->call_ctx && vhost_notify(dev, vq))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001457 eventfd_signal(vq->call_ctx, 1);
1458}
1459
1460/* And here's the combo meal deal. Supersize me! */
1461void vhost_add_used_and_signal(struct vhost_dev *dev,
1462 struct vhost_virtqueue *vq,
1463 unsigned int head, int len)
1464{
1465 vhost_add_used(vq, head, len);
1466 vhost_signal(dev, vq);
1467}
1468
David Stevens8dd014a2010-07-27 18:52:21 +03001469/* multi-buffer version of vhost_add_used_and_signal */
1470void vhost_add_used_and_signal_n(struct vhost_dev *dev,
1471 struct vhost_virtqueue *vq,
1472 struct vring_used_elem *heads, unsigned count)
1473{
1474 vhost_add_used_n(vq, heads, count);
1475 vhost_signal(dev, vq);
1476}
1477
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001478/* OK, now we need to know about added descriptors. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001479bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001480{
1481 u16 avail_idx;
1482 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301483
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001484 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
1485 return false;
1486 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001487 if (!vhost_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08001488 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001489 if (r) {
1490 vq_err(vq, "Failed to enable notification at %p: %d\n",
1491 &vq->used->flags, r);
1492 return false;
1493 }
1494 } else {
Jason Wang2723fea2011-06-21 18:04:38 +08001495 r = vhost_update_avail_event(vq, vq->avail_idx);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001496 if (r) {
1497 vq_err(vq, "Failed to update avail event index at %p: %d\n",
1498 vhost_avail_event(vq), r);
1499 return false;
1500 }
1501 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001502 /* They could have slipped one in as we were doing that: make
1503 * sure it's written, then check again. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001504 smp_mb();
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001505 r = __get_user(avail_idx, &vq->avail->idx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001506 if (r) {
1507 vq_err(vq, "Failed to check avail idx at %p: %d\n",
1508 &vq->avail->idx, r);
1509 return false;
1510 }
1511
David Stevens8dd014a2010-07-27 18:52:21 +03001512 return avail_idx != vq->avail_idx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001513}
1514
1515/* We don't need to be notified again. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001516void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001517{
1518 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301519
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001520 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
1521 return;
1522 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001523 if (!vhost_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08001524 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001525 if (r)
1526 vq_err(vq, "Failed to enable notification at %p: %d\n",
1527 &vq->used->flags, r);
1528 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001529}