blob: ef82a0d18489365e64a7ab0958bf01f654cfc8b1 [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
29#include <linux/net.h>
30#include <linux/if_packet.h>
31#include <linux/if_arp.h>
32
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000033#include "vhost.h"
34
35enum {
36 VHOST_MEMORY_MAX_NREGIONS = 64,
37 VHOST_MEMORY_F_LOG = 0x1,
38};
39
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +000040static unsigned vhost_zcopy_mask __read_mostly;
41
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +030042#define vhost_used_event(vq) ((u16 __user *)&vq->avail->ring[vq->num])
43#define vhost_avail_event(vq) ((u16 __user *)&vq->used->ring[vq->num])
44
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000045static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
46 poll_table *pt)
47{
48 struct vhost_poll *poll;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000049
Krishna Kumard47effe2011-03-01 17:06:37 +053050 poll = container_of(pt, struct vhost_poll, table);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000051 poll->wqh = wqh;
52 add_wait_queue(wqh, &poll->wait);
53}
54
55static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync,
56 void *key)
57{
Tejun Heoc23f34452010-06-02 20:40:00 +020058 struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
59
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000060 if (!((unsigned long)key & poll->mask))
61 return 0;
62
Tejun Heoc23f34452010-06-02 20:40:00 +020063 vhost_poll_queue(poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000064 return 0;
65}
66
Stefan Hajnoczi163049a2012-07-21 06:55:37 +000067void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000068{
Tejun Heoc23f34452010-06-02 20:40:00 +020069 INIT_LIST_HEAD(&work->node);
70 work->fn = fn;
71 init_waitqueue_head(&work->done);
72 work->flushing = 0;
73 work->queue_seq = work->done_seq = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000074}
75
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +030076/* Init poll structure */
77void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
78 unsigned long mask, struct vhost_dev *dev)
79{
80 init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
81 init_poll_funcptr(&poll->table, vhost_poll_func);
82 poll->mask = mask;
83 poll->dev = dev;
84
85 vhost_work_init(&poll->work, fn);
86}
87
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000088/* Start polling a file. We add ourselves to file's wait queue. The caller must
89 * keep a reference to a file until after vhost_poll_stop is called. */
90void vhost_poll_start(struct vhost_poll *poll, struct file *file)
91{
92 unsigned long mask;
Krishna Kumard47effe2011-03-01 17:06:37 +053093
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000094 mask = file->f_op->poll(file, &poll->table);
95 if (mask)
96 vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
97}
98
99/* Stop polling a file. After this function returns, it becomes safe to drop the
100 * file reference. You must also flush afterwards. */
101void vhost_poll_stop(struct vhost_poll *poll)
102{
103 remove_wait_queue(poll->wqh, &poll->wait);
104}
105
Michael S. Tsirkin0174b0c2011-01-10 10:03:20 +0200106static bool vhost_work_seq_done(struct vhost_dev *dev, struct vhost_work *work,
107 unsigned seq)
108{
109 int left;
Krishna Kumard47effe2011-03-01 17:06:37 +0530110
Michael S. Tsirkin0174b0c2011-01-10 10:03:20 +0200111 spin_lock_irq(&dev->work_lock);
112 left = seq - work->done_seq;
113 spin_unlock_irq(&dev->work_lock);
114 return left <= 0;
115}
116
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300117static void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000118{
Tejun Heoc23f34452010-06-02 20:40:00 +0200119 unsigned seq;
Tejun Heoc23f34452010-06-02 20:40:00 +0200120 int flushing;
121
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300122 spin_lock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200123 seq = work->queue_seq;
124 work->flushing++;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300125 spin_unlock_irq(&dev->work_lock);
Michael S. Tsirkin0174b0c2011-01-10 10:03:20 +0200126 wait_event(work->done, vhost_work_seq_done(dev, work, seq));
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300127 spin_lock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200128 flushing = --work->flushing;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300129 spin_unlock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200130 BUG_ON(flushing < 0);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000131}
132
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300133/* Flush any work that has been scheduled. When calling this, don't hold any
134 * locks that are also used by the callback. */
135void vhost_poll_flush(struct vhost_poll *poll)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000136{
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300137 vhost_work_flush(poll->dev, &poll->work);
138}
139
Stefan Hajnoczi163049a2012-07-21 06:55:37 +0000140void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300141{
Tejun Heoc23f34452010-06-02 20:40:00 +0200142 unsigned long flags;
143
144 spin_lock_irqsave(&dev->work_lock, flags);
145 if (list_empty(&work->node)) {
146 list_add_tail(&work->node, &dev->work_list);
147 work->queue_seq++;
148 wake_up_process(dev->worker);
149 }
150 spin_unlock_irqrestore(&dev->work_lock, flags);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000151}
152
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300153void vhost_poll_queue(struct vhost_poll *poll)
154{
155 vhost_work_queue(poll->dev, &poll->work);
156}
157
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000158static void vhost_vq_reset(struct vhost_dev *dev,
159 struct vhost_virtqueue *vq)
160{
161 vq->num = 1;
162 vq->desc = NULL;
163 vq->avail = NULL;
164 vq->used = NULL;
165 vq->last_avail_idx = 0;
166 vq->avail_idx = 0;
167 vq->last_used_idx = 0;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300168 vq->signalled_used = 0;
169 vq->signalled_used_valid = false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000170 vq->used_flags = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000171 vq->log_used = false;
172 vq->log_addr = -1ull;
David Stevens8dd014a2010-07-27 18:52:21 +0300173 vq->vhost_hlen = 0;
174 vq->sock_hlen = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000175 vq->private_data = NULL;
176 vq->log_base = NULL;
177 vq->error_ctx = NULL;
178 vq->error = NULL;
179 vq->kick = NULL;
180 vq->call_ctx = NULL;
181 vq->call = NULL;
Michael S. Tsirkin73a99f02010-02-23 11:23:45 +0200182 vq->log_ctx = NULL;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000183 vq->upend_idx = 0;
184 vq->done_idx = 0;
185 vq->ubufs = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000186}
187
Tejun Heoc23f34452010-06-02 20:40:00 +0200188static int vhost_worker(void *data)
189{
190 struct vhost_dev *dev = data;
191 struct vhost_work *work = NULL;
192 unsigned uninitialized_var(seq);
Jens Freimannd7ffde32012-06-26 00:59:58 +0000193 mm_segment_t oldfs = get_fs();
Tejun Heoc23f34452010-06-02 20:40:00 +0200194
Jens Freimannd7ffde32012-06-26 00:59:58 +0000195 set_fs(USER_DS);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200196 use_mm(dev->mm);
197
Tejun Heoc23f34452010-06-02 20:40:00 +0200198 for (;;) {
199 /* mb paired w/ kthread_stop */
200 set_current_state(TASK_INTERRUPTIBLE);
201
202 spin_lock_irq(&dev->work_lock);
203 if (work) {
204 work->done_seq = seq;
205 if (work->flushing)
206 wake_up_all(&work->done);
207 }
208
209 if (kthread_should_stop()) {
210 spin_unlock_irq(&dev->work_lock);
211 __set_current_state(TASK_RUNNING);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200212 break;
Tejun Heoc23f34452010-06-02 20:40:00 +0200213 }
214 if (!list_empty(&dev->work_list)) {
215 work = list_first_entry(&dev->work_list,
216 struct vhost_work, node);
217 list_del_init(&work->node);
218 seq = work->queue_seq;
219 } else
220 work = NULL;
221 spin_unlock_irq(&dev->work_lock);
222
223 if (work) {
224 __set_current_state(TASK_RUNNING);
225 work->fn(work);
Nadav Har'Eld550dda2012-02-27 15:07:29 +0200226 if (need_resched())
227 schedule();
Tejun Heoc23f34452010-06-02 20:40:00 +0200228 } else
229 schedule();
230
231 }
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200232 unuse_mm(dev->mm);
Jens Freimannd7ffde32012-06-26 00:59:58 +0000233 set_fs(oldfs);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200234 return 0;
Tejun Heoc23f34452010-06-02 20:40:00 +0200235}
236
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000237static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
238{
239 kfree(vq->indirect);
240 vq->indirect = NULL;
241 kfree(vq->log);
242 vq->log = NULL;
243 kfree(vq->heads);
244 vq->heads = NULL;
245 kfree(vq->ubuf_info);
246 vq->ubuf_info = NULL;
247}
248
249void vhost_enable_zcopy(int vq)
250{
251 vhost_zcopy_mask |= 0x1 << vq;
252}
253
Jason Wange0e9b402010-09-14 23:53:05 +0800254/* Helper to allocate iovec buffers for all vqs. */
255static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
256{
257 int i;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000258 bool zcopy;
Krishna Kumard47effe2011-03-01 17:06:37 +0530259
Jason Wange0e9b402010-09-14 23:53:05 +0800260 for (i = 0; i < dev->nvqs; ++i) {
261 dev->vqs[i].indirect = kmalloc(sizeof *dev->vqs[i].indirect *
262 UIO_MAXIOV, GFP_KERNEL);
263 dev->vqs[i].log = kmalloc(sizeof *dev->vqs[i].log * UIO_MAXIOV,
264 GFP_KERNEL);
265 dev->vqs[i].heads = kmalloc(sizeof *dev->vqs[i].heads *
266 UIO_MAXIOV, GFP_KERNEL);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000267 zcopy = vhost_zcopy_mask & (0x1 << i);
268 if (zcopy)
269 dev->vqs[i].ubuf_info =
270 kmalloc(sizeof *dev->vqs[i].ubuf_info *
271 UIO_MAXIOV, GFP_KERNEL);
Jason Wange0e9b402010-09-14 23:53:05 +0800272 if (!dev->vqs[i].indirect || !dev->vqs[i].log ||
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000273 !dev->vqs[i].heads ||
274 (zcopy && !dev->vqs[i].ubuf_info))
Jason Wange0e9b402010-09-14 23:53:05 +0800275 goto err_nomem;
276 }
277 return 0;
Krishna Kumard47effe2011-03-01 17:06:37 +0530278
Jason Wange0e9b402010-09-14 23:53:05 +0800279err_nomem:
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000280 for (; i >= 0; --i)
281 vhost_vq_free_iovecs(&dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800282 return -ENOMEM;
283}
284
285static void vhost_dev_free_iovecs(struct vhost_dev *dev)
286{
287 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530288
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000289 for (i = 0; i < dev->nvqs; ++i)
290 vhost_vq_free_iovecs(&dev->vqs[i]);
Jason Wange0e9b402010-09-14 23:53:05 +0800291}
292
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000293long vhost_dev_init(struct vhost_dev *dev,
294 struct vhost_virtqueue *vqs, int nvqs)
295{
296 int i;
Tejun Heoc23f34452010-06-02 20:40:00 +0200297
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000298 dev->vqs = vqs;
299 dev->nvqs = nvqs;
300 mutex_init(&dev->mutex);
301 dev->log_ctx = NULL;
302 dev->log_file = NULL;
303 dev->memory = NULL;
304 dev->mm = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200305 spin_lock_init(&dev->work_lock);
306 INIT_LIST_HEAD(&dev->work_list);
307 dev->worker = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000308
309 for (i = 0; i < dev->nvqs; ++i) {
Jason Wange0e9b402010-09-14 23:53:05 +0800310 dev->vqs[i].log = NULL;
311 dev->vqs[i].indirect = NULL;
312 dev->vqs[i].heads = NULL;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000313 dev->vqs[i].ubuf_info = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000314 dev->vqs[i].dev = dev;
315 mutex_init(&dev->vqs[i].mutex);
316 vhost_vq_reset(dev, dev->vqs + i);
317 if (dev->vqs[i].handle_kick)
318 vhost_poll_init(&dev->vqs[i].poll,
Tejun Heoc23f34452010-06-02 20:40:00 +0200319 dev->vqs[i].handle_kick, POLLIN, dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000320 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200321
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000322 return 0;
323}
324
325/* Caller should have device mutex */
326long vhost_dev_check_owner(struct vhost_dev *dev)
327{
328 /* Are you the owner? If not, I don't think you mean to do that */
329 return dev->mm == current->mm ? 0 : -EPERM;
330}
331
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300332struct vhost_attach_cgroups_struct {
Krishna Kumard47effe2011-03-01 17:06:37 +0530333 struct vhost_work work;
334 struct task_struct *owner;
335 int ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300336};
337
338static void vhost_attach_cgroups_work(struct vhost_work *work)
339{
Krishna Kumard47effe2011-03-01 17:06:37 +0530340 struct vhost_attach_cgroups_struct *s;
341
342 s = container_of(work, struct vhost_attach_cgroups_struct, work);
343 s->ret = cgroup_attach_task_all(s->owner, current);
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300344}
345
346static int vhost_attach_cgroups(struct vhost_dev *dev)
347{
Krishna Kumard47effe2011-03-01 17:06:37 +0530348 struct vhost_attach_cgroups_struct attach;
349
350 attach.owner = current;
351 vhost_work_init(&attach.work, vhost_attach_cgroups_work);
352 vhost_work_queue(dev, &attach.work);
353 vhost_work_flush(dev, &attach.work);
354 return attach.ret;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300355}
356
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000357/* Caller should have device mutex */
358static long vhost_dev_set_owner(struct vhost_dev *dev)
359{
Tejun Heoc23f34452010-06-02 20:40:00 +0200360 struct task_struct *worker;
361 int err;
Krishna Kumard47effe2011-03-01 17:06:37 +0530362
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000363 /* Is there an owner already? */
Tejun Heoc23f34452010-06-02 20:40:00 +0200364 if (dev->mm) {
365 err = -EBUSY;
366 goto err_mm;
367 }
Krishna Kumard47effe2011-03-01 17:06:37 +0530368
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000369 /* No owner, become one */
370 dev->mm = get_task_mm(current);
Tejun Heoc23f34452010-06-02 20:40:00 +0200371 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
372 if (IS_ERR(worker)) {
373 err = PTR_ERR(worker);
374 goto err_worker;
375 }
376
377 dev->worker = worker;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300378 wake_up_process(worker); /* avoid contributing to loadavg */
379
380 err = vhost_attach_cgroups(dev);
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300381 if (err)
382 goto err_cgroup;
Tejun Heoc23f34452010-06-02 20:40:00 +0200383
Jason Wange0e9b402010-09-14 23:53:05 +0800384 err = vhost_dev_alloc_iovecs(dev);
385 if (err)
386 goto err_cgroup;
387
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000388 return 0;
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300389err_cgroup:
390 kthread_stop(worker);
Michael S. Tsirkin615cc222010-09-02 14:16:36 +0300391 dev->worker = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200392err_worker:
393 if (dev->mm)
394 mmput(dev->mm);
395 dev->mm = NULL;
396err_mm:
397 return err;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000398}
399
400/* Caller should have device mutex */
401long vhost_dev_reset_owner(struct vhost_dev *dev)
402{
403 struct vhost_memory *memory;
404
405 /* Restore memory to default empty mapping. */
406 memory = kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
407 if (!memory)
408 return -ENOMEM;
409
Michael S. Tsirkinea5d4042011-11-27 19:05:58 +0200410 vhost_dev_cleanup(dev, true);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000411
412 memory->nregions = 0;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100413 RCU_INIT_POINTER(dev->memory, memory);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000414 return 0;
415}
416
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000417/* In case of DMA done not in order in lower device driver for some reason.
418 * upend_idx is used to track end of used idx, done_idx is used to track head
419 * of used idx. Once lower device DMA done contiguously, we will signal KVM
420 * guest used idx.
421 */
422int vhost_zerocopy_signal_used(struct vhost_virtqueue *vq)
423{
424 int i;
425 int j = 0;
426
427 for (i = vq->done_idx; i != vq->upend_idx; i = (i + 1) % UIO_MAXIOV) {
428 if ((vq->heads[i].len == VHOST_DMA_DONE_LEN)) {
429 vq->heads[i].len = VHOST_DMA_CLEAR_LEN;
430 vhost_add_used_and_signal(vq->dev, vq,
431 vq->heads[i].id, 0);
432 ++j;
433 } else
434 break;
435 }
436 if (j)
437 vq->done_idx = i;
438 return j;
439}
440
Michael S. Tsirkinea5d4042011-11-27 19:05:58 +0200441/* Caller should have device mutex if and only if locked is set */
442void vhost_dev_cleanup(struct vhost_dev *dev, bool locked)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000443{
444 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530445
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000446 for (i = 0; i < dev->nvqs; ++i) {
447 if (dev->vqs[i].kick && dev->vqs[i].handle_kick) {
448 vhost_poll_stop(&dev->vqs[i].poll);
449 vhost_poll_flush(&dev->vqs[i].poll);
450 }
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000451 /* Wait for all lower device DMAs done. */
452 if (dev->vqs[i].ubufs)
453 vhost_ubuf_put_and_wait(dev->vqs[i].ubufs);
454
455 /* Signal guest as appropriate. */
456 vhost_zerocopy_signal_used(&dev->vqs[i]);
457
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000458 if (dev->vqs[i].error_ctx)
459 eventfd_ctx_put(dev->vqs[i].error_ctx);
460 if (dev->vqs[i].error)
461 fput(dev->vqs[i].error);
462 if (dev->vqs[i].kick)
463 fput(dev->vqs[i].kick);
464 if (dev->vqs[i].call_ctx)
465 eventfd_ctx_put(dev->vqs[i].call_ctx);
466 if (dev->vqs[i].call)
467 fput(dev->vqs[i].call);
468 vhost_vq_reset(dev, dev->vqs + i);
469 }
Jason Wange0e9b402010-09-14 23:53:05 +0800470 vhost_dev_free_iovecs(dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000471 if (dev->log_ctx)
472 eventfd_ctx_put(dev->log_ctx);
473 dev->log_ctx = NULL;
474 if (dev->log_file)
475 fput(dev->log_file);
476 dev->log_file = NULL;
477 /* No one will access memory at this point */
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100478 kfree(rcu_dereference_protected(dev->memory,
Michael S. Tsirkinea5d4042011-11-27 19:05:58 +0200479 locked ==
480 lockdep_is_held(&dev->mutex)));
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100481 RCU_INIT_POINTER(dev->memory, NULL);
Tejun Heoc23f34452010-06-02 20:40:00 +0200482 WARN_ON(!list_empty(&dev->work_list));
Eric Dumazet78b620c2010-08-31 02:05:57 +0000483 if (dev->worker) {
484 kthread_stop(dev->worker);
485 dev->worker = NULL;
486 }
Michael S. Tsirkin533a19b2010-10-06 15:34:38 +0200487 if (dev->mm)
488 mmput(dev->mm);
489 dev->mm = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000490}
491
492static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
493{
494 u64 a = addr / VHOST_PAGE_SIZE / 8;
Krishna Kumard47effe2011-03-01 17:06:37 +0530495
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000496 /* Make sure 64 bit math will not overflow. */
497 if (a > ULONG_MAX - (unsigned long)log_base ||
498 a + (unsigned long)log_base > ULONG_MAX)
Dan Carpenter6d97e552010-10-11 19:24:19 +0200499 return 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000500
501 return access_ok(VERIFY_WRITE, log_base + a,
502 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
503}
504
505/* Caller should have vq mutex and device mutex. */
506static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
507 int log_all)
508{
509 int i;
Jeff Dike179b2842010-04-07 09:59:10 -0400510
Michael S. Tsirkinf8322fb2010-05-27 12:28:03 +0300511 if (!mem)
512 return 0;
Jeff Dike179b2842010-04-07 09:59:10 -0400513
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000514 for (i = 0; i < mem->nregions; ++i) {
515 struct vhost_memory_region *m = mem->regions + i;
516 unsigned long a = m->userspace_addr;
517 if (m->memory_size > ULONG_MAX)
518 return 0;
519 else if (!access_ok(VERIFY_WRITE, (void __user *)a,
520 m->memory_size))
521 return 0;
522 else if (log_all && !log_access_ok(log_base,
523 m->guest_phys_addr,
524 m->memory_size))
525 return 0;
526 }
527 return 1;
528}
529
530/* Can we switch to this memory table? */
531/* Caller should have device mutex but not vq mutex */
532static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
533 int log_all)
534{
535 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530536
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000537 for (i = 0; i < d->nvqs; ++i) {
538 int ok;
539 mutex_lock(&d->vqs[i].mutex);
540 /* If ring is inactive, will check when it's enabled. */
541 if (d->vqs[i].private_data)
542 ok = vq_memory_access_ok(d->vqs[i].log_base, mem,
543 log_all);
544 else
545 ok = 1;
546 mutex_unlock(&d->vqs[i].mutex);
547 if (!ok)
548 return 0;
549 }
550 return 1;
551}
552
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300553static int vq_access_ok(struct vhost_dev *d, unsigned int num,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000554 struct vring_desc __user *desc,
555 struct vring_avail __user *avail,
556 struct vring_used __user *used)
557{
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300558 size_t s = vhost_has_feature(d, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000559 return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
560 access_ok(VERIFY_READ, avail,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300561 sizeof *avail + num * sizeof *avail->ring + s) &&
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000562 access_ok(VERIFY_WRITE, used,
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300563 sizeof *used + num * sizeof *used->ring + s);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000564}
565
566/* Can we log writes? */
567/* Caller should have device mutex but not vq mutex */
568int vhost_log_access_ok(struct vhost_dev *dev)
569{
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100570 struct vhost_memory *mp;
571
572 mp = rcu_dereference_protected(dev->memory,
573 lockdep_is_held(&dev->mutex));
574 return memory_access_ok(dev, mp, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000575}
576
577/* Verify access for write logging. */
578/* Caller should have vq mutex and device mutex */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300579static int vq_log_access_ok(struct vhost_dev *d, struct vhost_virtqueue *vq,
580 void __user *log_base)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000581{
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100582 struct vhost_memory *mp;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300583 size_t s = vhost_has_feature(d, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100584
585 mp = rcu_dereference_protected(vq->dev->memory,
586 lockdep_is_held(&vq->mutex));
587 return vq_memory_access_ok(log_base, mp,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000588 vhost_has_feature(vq->dev, VHOST_F_LOG_ALL)) &&
589 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
590 sizeof *vq->used +
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300591 vq->num * sizeof *vq->used->ring + s));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000592}
593
594/* Can we start vq? */
595/* Caller should have vq mutex and device mutex */
596int vhost_vq_access_ok(struct vhost_virtqueue *vq)
597{
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300598 return vq_access_ok(vq->dev, vq->num, vq->desc, vq->avail, vq->used) &&
599 vq_log_access_ok(vq->dev, vq, vq->log_base);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000600}
601
602static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
603{
604 struct vhost_memory mem, *newmem, *oldmem;
605 unsigned long size = offsetof(struct vhost_memory, regions);
Krishna Kumard47effe2011-03-01 17:06:37 +0530606
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900607 if (copy_from_user(&mem, m, size))
608 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000609 if (mem.padding)
610 return -EOPNOTSUPP;
611 if (mem.nregions > VHOST_MEMORY_MAX_NREGIONS)
612 return -E2BIG;
613 newmem = kmalloc(size + mem.nregions * sizeof *m->regions, GFP_KERNEL);
614 if (!newmem)
615 return -ENOMEM;
616
617 memcpy(newmem, &mem, size);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900618 if (copy_from_user(newmem->regions, m->regions,
619 mem.nregions * sizeof *m->regions)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000620 kfree(newmem);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900621 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000622 }
623
Krishna Kumard47effe2011-03-01 17:06:37 +0530624 if (!memory_access_ok(d, newmem,
625 vhost_has_feature(d, VHOST_F_LOG_ALL))) {
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +0900626 kfree(newmem);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000627 return -EFAULT;
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +0900628 }
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100629 oldmem = rcu_dereference_protected(d->memory,
630 lockdep_is_held(&d->mutex));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000631 rcu_assign_pointer(d->memory, newmem);
632 synchronize_rcu();
633 kfree(oldmem);
634 return 0;
635}
636
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000637static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp)
638{
639 struct file *eventfp, *filep = NULL,
640 *pollstart = NULL, *pollstop = NULL;
641 struct eventfd_ctx *ctx = NULL;
642 u32 __user *idxp = argp;
643 struct vhost_virtqueue *vq;
644 struct vhost_vring_state s;
645 struct vhost_vring_file f;
646 struct vhost_vring_addr a;
647 u32 idx;
648 long r;
649
650 r = get_user(idx, idxp);
651 if (r < 0)
652 return r;
Krishna Kumar0f3d9a12010-05-25 11:10:36 +0530653 if (idx >= d->nvqs)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000654 return -ENOBUFS;
655
656 vq = d->vqs + idx;
657
658 mutex_lock(&vq->mutex);
659
660 switch (ioctl) {
661 case VHOST_SET_VRING_NUM:
662 /* Resizing ring with an active backend?
663 * You don't want to do that. */
664 if (vq->private_data) {
665 r = -EBUSY;
666 break;
667 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900668 if (copy_from_user(&s, argp, sizeof s)) {
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 (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
673 r = -EINVAL;
674 break;
675 }
676 vq->num = s.num;
677 break;
678 case VHOST_SET_VRING_BASE:
679 /* Moving base with an active backend?
680 * You don't want to do that. */
681 if (vq->private_data) {
682 r = -EBUSY;
683 break;
684 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900685 if (copy_from_user(&s, argp, sizeof s)) {
686 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000687 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900688 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000689 if (s.num > 0xffff) {
690 r = -EINVAL;
691 break;
692 }
693 vq->last_avail_idx = s.num;
694 /* Forget the cached index value. */
695 vq->avail_idx = vq->last_avail_idx;
696 break;
697 case VHOST_GET_VRING_BASE:
698 s.index = idx;
699 s.num = vq->last_avail_idx;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900700 if (copy_to_user(argp, &s, sizeof s))
701 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000702 break;
703 case VHOST_SET_VRING_ADDR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900704 if (copy_from_user(&a, argp, sizeof a)) {
705 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000706 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900707 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000708 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
709 r = -EOPNOTSUPP;
710 break;
711 }
712 /* For 32bit, verify that the top 32bits of the user
713 data are set to zero. */
714 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
715 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
716 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
717 r = -EFAULT;
718 break;
719 }
720 if ((a.avail_user_addr & (sizeof *vq->avail->ring - 1)) ||
721 (a.used_user_addr & (sizeof *vq->used->ring - 1)) ||
722 (a.log_guest_addr & (sizeof *vq->used->ring - 1))) {
723 r = -EINVAL;
724 break;
725 }
726
727 /* We only verify access here if backend is configured.
728 * If it is not, we don't as size might not have been setup.
729 * We will verify when backend is configured. */
730 if (vq->private_data) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300731 if (!vq_access_ok(d, vq->num,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000732 (void __user *)(unsigned long)a.desc_user_addr,
733 (void __user *)(unsigned long)a.avail_user_addr,
734 (void __user *)(unsigned long)a.used_user_addr)) {
735 r = -EINVAL;
736 break;
737 }
738
739 /* Also validate log access for used ring if enabled. */
740 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
741 !log_access_ok(vq->log_base, a.log_guest_addr,
742 sizeof *vq->used +
743 vq->num * sizeof *vq->used->ring)) {
744 r = -EINVAL;
745 break;
746 }
747 }
748
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000749 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
750 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
751 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
752 vq->log_addr = a.log_guest_addr;
753 vq->used = (void __user *)(unsigned long)a.used_user_addr;
754 break;
755 case VHOST_SET_VRING_KICK:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900756 if (copy_from_user(&f, argp, sizeof f)) {
757 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000758 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900759 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000760 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200761 if (IS_ERR(eventfp)) {
762 r = PTR_ERR(eventfp);
763 break;
764 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000765 if (eventfp != vq->kick) {
766 pollstop = filep = vq->kick;
767 pollstart = vq->kick = eventfp;
768 } else
769 filep = eventfp;
770 break;
771 case VHOST_SET_VRING_CALL:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900772 if (copy_from_user(&f, argp, sizeof f)) {
773 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000774 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900775 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000776 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200777 if (IS_ERR(eventfp)) {
778 r = PTR_ERR(eventfp);
779 break;
780 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000781 if (eventfp != vq->call) {
782 filep = vq->call;
783 ctx = vq->call_ctx;
784 vq->call = eventfp;
785 vq->call_ctx = eventfp ?
786 eventfd_ctx_fileget(eventfp) : NULL;
787 } else
788 filep = eventfp;
789 break;
790 case VHOST_SET_VRING_ERR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900791 if (copy_from_user(&f, argp, sizeof f)) {
792 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000793 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900794 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000795 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200796 if (IS_ERR(eventfp)) {
797 r = PTR_ERR(eventfp);
798 break;
799 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000800 if (eventfp != vq->error) {
801 filep = vq->error;
802 vq->error = eventfp;
803 ctx = vq->error_ctx;
804 vq->error_ctx = eventfp ?
805 eventfd_ctx_fileget(eventfp) : NULL;
806 } else
807 filep = eventfp;
808 break;
809 default:
810 r = -ENOIOCTLCMD;
811 }
812
813 if (pollstop && vq->handle_kick)
814 vhost_poll_stop(&vq->poll);
815
816 if (ctx)
817 eventfd_ctx_put(ctx);
818 if (filep)
819 fput(filep);
820
821 if (pollstart && vq->handle_kick)
822 vhost_poll_start(&vq->poll, vq->kick);
823
824 mutex_unlock(&vq->mutex);
825
826 if (pollstop && vq->handle_kick)
827 vhost_poll_flush(&vq->poll);
828 return r;
829}
830
831/* Caller must have device mutex */
832long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, unsigned long arg)
833{
834 void __user *argp = (void __user *)arg;
835 struct file *eventfp, *filep = NULL;
836 struct eventfd_ctx *ctx = NULL;
837 u64 p;
838 long r;
839 int i, fd;
840
841 /* If you are not the owner, you can become one */
842 if (ioctl == VHOST_SET_OWNER) {
843 r = vhost_dev_set_owner(d);
844 goto done;
845 }
846
847 /* You must be the owner to do anything else */
848 r = vhost_dev_check_owner(d);
849 if (r)
850 goto done;
851
852 switch (ioctl) {
853 case VHOST_SET_MEM_TABLE:
854 r = vhost_set_memory(d, argp);
855 break;
856 case VHOST_SET_LOG_BASE:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900857 if (copy_from_user(&p, argp, sizeof p)) {
858 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000859 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900860 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000861 if ((u64)(unsigned long)p != p) {
862 r = -EFAULT;
863 break;
864 }
865 for (i = 0; i < d->nvqs; ++i) {
866 struct vhost_virtqueue *vq;
867 void __user *base = (void __user *)(unsigned long)p;
868 vq = d->vqs + i;
869 mutex_lock(&vq->mutex);
870 /* If ring is inactive, will check when it's enabled. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300871 if (vq->private_data && !vq_log_access_ok(d, vq, base))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000872 r = -EFAULT;
873 else
874 vq->log_base = base;
875 mutex_unlock(&vq->mutex);
876 }
877 break;
878 case VHOST_SET_LOG_FD:
879 r = get_user(fd, (int __user *)argp);
880 if (r < 0)
881 break;
882 eventfp = fd == -1 ? NULL : eventfd_fget(fd);
883 if (IS_ERR(eventfp)) {
884 r = PTR_ERR(eventfp);
885 break;
886 }
887 if (eventfp != d->log_file) {
888 filep = d->log_file;
889 ctx = d->log_ctx;
890 d->log_ctx = eventfp ?
891 eventfd_ctx_fileget(eventfp) : NULL;
892 } else
893 filep = eventfp;
894 for (i = 0; i < d->nvqs; ++i) {
895 mutex_lock(&d->vqs[i].mutex);
896 d->vqs[i].log_ctx = d->log_ctx;
897 mutex_unlock(&d->vqs[i].mutex);
898 }
899 if (ctx)
900 eventfd_ctx_put(ctx);
901 if (filep)
902 fput(filep);
903 break;
904 default:
905 r = vhost_set_vring(d, ioctl, argp);
906 break;
907 }
908done:
909 return r;
910}
911
912static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
913 __u64 addr, __u32 len)
914{
915 struct vhost_memory_region *reg;
916 int i;
Krishna Kumard47effe2011-03-01 17:06:37 +0530917
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000918 /* linear search is not brilliant, but we really have on the order of 6
919 * regions in practice */
920 for (i = 0; i < mem->nregions; ++i) {
921 reg = mem->regions + i;
922 if (reg->guest_phys_addr <= addr &&
923 reg->guest_phys_addr + reg->memory_size - 1 >= addr)
924 return reg;
925 }
926 return NULL;
927}
928
929/* TODO: This is really inefficient. We need something like get_user()
930 * (instruction directly accesses the data, with an exception table entry
931 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
932 */
933static int set_bit_to_user(int nr, void __user *addr)
934{
935 unsigned long log = (unsigned long)addr;
936 struct page *page;
937 void *base;
938 int bit = nr + (log % PAGE_SIZE) * 8;
939 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +0530940
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000941 r = get_user_pages_fast(log, 1, 1, &page);
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +0200942 if (r < 0)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000943 return r;
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +0200944 BUG_ON(r != 1);
Cong Wangc6daa7f2011-11-25 23:14:26 +0800945 base = kmap_atomic(page);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000946 set_bit(bit, base);
Cong Wangc6daa7f2011-11-25 23:14:26 +0800947 kunmap_atomic(base);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000948 set_page_dirty_lock(page);
949 put_page(page);
950 return 0;
951}
952
953static int log_write(void __user *log_base,
954 u64 write_address, u64 write_length)
955{
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +0200956 u64 write_page = write_address / VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000957 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +0530958
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000959 if (!write_length)
960 return 0;
Michael S. Tsirkin3bf9be42010-11-29 10:19:07 +0200961 write_length += write_address % VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000962 for (;;) {
963 u64 base = (u64)(unsigned long)log_base;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +0200964 u64 log = base + write_page / 8;
965 int bit = write_page % 8;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000966 if ((u64)(unsigned long)log != log)
967 return -EFAULT;
968 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
969 if (r < 0)
970 return r;
971 if (write_length <= VHOST_PAGE_SIZE)
972 break;
973 write_length -= VHOST_PAGE_SIZE;
Michael S. Tsirkin28831ee2010-11-29 10:22:10 +0200974 write_page += 1;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000975 }
976 return r;
977}
978
979int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
980 unsigned int log_num, u64 len)
981{
982 int i, r;
983
984 /* Make sure data written is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +0000985 smp_wmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000986 for (i = 0; i < log_num; ++i) {
987 u64 l = min(log[i].len, len);
988 r = log_write(vq->log_base, log[i].addr, l);
989 if (r < 0)
990 return r;
991 len -= l;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +0200992 if (!len) {
993 if (vq->log_ctx)
994 eventfd_signal(vq->log_ctx, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000995 return 0;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +0200996 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000997 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000998 /* Length written exceeds what we have stored. This is a bug. */
999 BUG();
1000 return 0;
1001}
1002
Jason Wang2723fea2011-06-21 18:04:38 +08001003static int vhost_update_used_flags(struct vhost_virtqueue *vq)
1004{
1005 void __user *used;
Michael S. Tsirkinb8342262011-07-19 17:15:43 +03001006 if (__put_user(vq->used_flags, &vq->used->flags) < 0)
Jason Wang2723fea2011-06-21 18:04:38 +08001007 return -EFAULT;
1008 if (unlikely(vq->log_used)) {
1009 /* Make sure the flag is seen before log. */
1010 smp_wmb();
1011 /* Log used flag write. */
1012 used = &vq->used->flags;
1013 log_write(vq->log_base, vq->log_addr +
1014 (used - (void __user *)vq->used),
1015 sizeof vq->used->flags);
1016 if (vq->log_ctx)
1017 eventfd_signal(vq->log_ctx, 1);
1018 }
1019 return 0;
1020}
1021
1022static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
1023{
Michael S. Tsirkinb8342262011-07-19 17:15:43 +03001024 if (__put_user(vq->avail_idx, vhost_avail_event(vq)))
Jason Wang2723fea2011-06-21 18:04:38 +08001025 return -EFAULT;
1026 if (unlikely(vq->log_used)) {
1027 void __user *used;
1028 /* Make sure the event is seen before log. */
1029 smp_wmb();
1030 /* Log avail event write */
1031 used = vhost_avail_event(vq);
1032 log_write(vq->log_base, vq->log_addr +
1033 (used - (void __user *)vq->used),
1034 sizeof *vhost_avail_event(vq));
1035 if (vq->log_ctx)
1036 eventfd_signal(vq->log_ctx, 1);
1037 }
1038 return 0;
1039}
1040
1041int vhost_init_used(struct vhost_virtqueue *vq)
1042{
1043 int r;
1044 if (!vq->private_data)
1045 return 0;
1046
1047 r = vhost_update_used_flags(vq);
1048 if (r)
1049 return r;
1050 vq->signalled_used_valid = false;
1051 return get_user(vq->last_used_idx, &vq->used->idx);
1052}
1053
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001054static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
1055 struct iovec iov[], int iov_size)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001056{
1057 const struct vhost_memory_region *reg;
1058 struct vhost_memory *mem;
1059 struct iovec *_iov;
1060 u64 s = 0;
1061 int ret = 0;
1062
1063 rcu_read_lock();
1064
1065 mem = rcu_dereference(dev->memory);
1066 while ((u64)len > s) {
1067 u64 size;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001068 if (unlikely(ret >= iov_size)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001069 ret = -ENOBUFS;
1070 break;
1071 }
1072 reg = find_region(mem, addr, len);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001073 if (unlikely(!reg)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001074 ret = -EFAULT;
1075 break;
1076 }
1077 _iov = iov + ret;
1078 size = reg->memory_size - addr + reg->guest_phys_addr;
1079 _iov->iov_len = min((u64)len, size);
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001080 _iov->iov_base = (void __user *)(unsigned long)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001081 (reg->userspace_addr + addr - reg->guest_phys_addr);
1082 s += size;
1083 addr += size;
1084 ++ret;
1085 }
1086
1087 rcu_read_unlock();
1088 return ret;
1089}
1090
1091/* Each buffer in the virtqueues is actually a chain of descriptors. This
1092 * function returns the next descriptor in the chain,
1093 * or -1U if we're at the end. */
1094static unsigned next_desc(struct vring_desc *desc)
1095{
1096 unsigned int next;
1097
1098 /* If this descriptor says it doesn't chain, we're done. */
1099 if (!(desc->flags & VRING_DESC_F_NEXT))
1100 return -1U;
1101
1102 /* Check they're not leading us off end of descriptors. */
1103 next = desc->next;
1104 /* Make sure compiler knows to grab that: we don't want it changing! */
1105 /* We will use the result as an index in an array, so most
1106 * architectures only need a compiler barrier here. */
1107 read_barrier_depends();
1108
1109 return next;
1110}
1111
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001112static int get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
1113 struct iovec iov[], unsigned int iov_size,
1114 unsigned int *out_num, unsigned int *in_num,
1115 struct vhost_log *log, unsigned int *log_num,
1116 struct vring_desc *indirect)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001117{
1118 struct vring_desc desc;
1119 unsigned int i = 0, count, found = 0;
1120 int ret;
1121
1122 /* Sanity check */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001123 if (unlikely(indirect->len % sizeof desc)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001124 vq_err(vq, "Invalid length in indirect descriptor: "
1125 "len 0x%llx not multiple of 0x%zx\n",
1126 (unsigned long long)indirect->len,
1127 sizeof desc);
1128 return -EINVAL;
1129 }
1130
1131 ret = translate_desc(dev, indirect->addr, indirect->len, vq->indirect,
Jason Wange0e9b402010-09-14 23:53:05 +08001132 UIO_MAXIOV);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001133 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001134 vq_err(vq, "Translation failure %d in indirect.\n", ret);
1135 return ret;
1136 }
1137
1138 /* We will use the result as an address to read from, so most
1139 * architectures only need a compiler barrier here. */
1140 read_barrier_depends();
1141
1142 count = indirect->len / sizeof desc;
1143 /* Buffers are chained via a 16 bit next field, so
1144 * we can have at most 2^16 of these. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001145 if (unlikely(count > USHRT_MAX + 1)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001146 vq_err(vq, "Indirect buffer length too big: %d\n",
1147 indirect->len);
1148 return -E2BIG;
1149 }
1150
1151 do {
1152 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001153 if (unlikely(++found > count)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001154 vq_err(vq, "Loop detected: last one at %u "
1155 "indirect size %u\n",
1156 i, count);
1157 return -EINVAL;
1158 }
Krishna Kumard47effe2011-03-01 17:06:37 +05301159 if (unlikely(memcpy_fromiovec((unsigned char *)&desc,
1160 vq->indirect, sizeof desc))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001161 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
1162 i, (size_t)indirect->addr + i * sizeof desc);
1163 return -EINVAL;
1164 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001165 if (unlikely(desc.flags & VRING_DESC_F_INDIRECT)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001166 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
1167 i, (size_t)indirect->addr + i * sizeof desc);
1168 return -EINVAL;
1169 }
1170
1171 ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
1172 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001173 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001174 vq_err(vq, "Translation failure %d indirect idx %d\n",
1175 ret, i);
1176 return ret;
1177 }
1178 /* If this is an input descriptor, increment that count. */
1179 if (desc.flags & VRING_DESC_F_WRITE) {
1180 *in_num += ret;
1181 if (unlikely(log)) {
1182 log[*log_num].addr = desc.addr;
1183 log[*log_num].len = desc.len;
1184 ++*log_num;
1185 }
1186 } else {
1187 /* If it's an output descriptor, they're all supposed
1188 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001189 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001190 vq_err(vq, "Indirect descriptor "
1191 "has out after in: idx %d\n", i);
1192 return -EINVAL;
1193 }
1194 *out_num += ret;
1195 }
1196 } while ((i = next_desc(&desc)) != -1);
1197 return 0;
1198}
1199
1200/* This looks in the virtqueue and for the first available buffer, and converts
1201 * it to an iovec for convenient access. Since descriptors consist of some
1202 * number of output then some number of input descriptors, it's actually two
1203 * iovecs, but we pack them into one and note how many of each there were.
1204 *
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001205 * This function returns the descriptor number found, or vq->num (which is
1206 * never a valid descriptor number) if none was found. A negative code is
1207 * returned on error. */
1208int vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
1209 struct iovec iov[], unsigned int iov_size,
1210 unsigned int *out_num, unsigned int *in_num,
1211 struct vhost_log *log, unsigned int *log_num)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001212{
1213 struct vring_desc desc;
1214 unsigned int i, head, found = 0;
1215 u16 last_avail_idx;
1216 int ret;
1217
1218 /* Check it isn't doing very strange things with descriptor numbers. */
1219 last_avail_idx = vq->last_avail_idx;
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001220 if (unlikely(__get_user(vq->avail_idx, &vq->avail->idx))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001221 vq_err(vq, "Failed to access avail idx at %p\n",
1222 &vq->avail->idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001223 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001224 }
1225
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001226 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001227 vq_err(vq, "Guest moved used index from %u to %u",
1228 last_avail_idx, vq->avail_idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001229 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001230 }
1231
1232 /* If there's nothing new since last we looked, return invalid. */
1233 if (vq->avail_idx == last_avail_idx)
1234 return vq->num;
1235
1236 /* Only get avail ring entries after they have been exposed by guest. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001237 smp_rmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001238
1239 /* Grab the next descriptor number they're advertising, and increment
1240 * the index we've seen. */
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001241 if (unlikely(__get_user(head,
1242 &vq->avail->ring[last_avail_idx % vq->num]))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001243 vq_err(vq, "Failed to read head: idx %d address %p\n",
1244 last_avail_idx,
1245 &vq->avail->ring[last_avail_idx % vq->num]);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001246 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001247 }
1248
1249 /* If their number is silly, that's an error. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001250 if (unlikely(head >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001251 vq_err(vq, "Guest says index %u > %u is available",
1252 head, vq->num);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001253 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001254 }
1255
1256 /* When we start there are none of either input nor output. */
1257 *out_num = *in_num = 0;
1258 if (unlikely(log))
1259 *log_num = 0;
1260
1261 i = head;
1262 do {
1263 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001264 if (unlikely(i >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001265 vq_err(vq, "Desc index is %u > %u, head = %u",
1266 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001267 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001268 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001269 if (unlikely(++found > vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001270 vq_err(vq, "Loop detected: last one at %u "
1271 "vq size %u head %u\n",
1272 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001273 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001274 }
Michael S. Tsirkinfcc042a2011-03-06 13:33:49 +02001275 ret = __copy_from_user(&desc, vq->desc + i, sizeof desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001276 if (unlikely(ret)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001277 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
1278 i, vq->desc + i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001279 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001280 }
1281 if (desc.flags & VRING_DESC_F_INDIRECT) {
1282 ret = get_indirect(dev, vq, iov, iov_size,
1283 out_num, in_num,
1284 log, log_num, &desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001285 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001286 vq_err(vq, "Failure detected "
1287 "in indirect descriptor at idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001288 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001289 }
1290 continue;
1291 }
1292
1293 ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
1294 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001295 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001296 vq_err(vq, "Translation failure %d descriptor idx %d\n",
1297 ret, i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001298 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001299 }
1300 if (desc.flags & VRING_DESC_F_WRITE) {
1301 /* If this is an input descriptor,
1302 * increment that count. */
1303 *in_num += ret;
1304 if (unlikely(log)) {
1305 log[*log_num].addr = desc.addr;
1306 log[*log_num].len = desc.len;
1307 ++*log_num;
1308 }
1309 } else {
1310 /* If it's an output descriptor, they're all supposed
1311 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001312 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001313 vq_err(vq, "Descriptor has out after in: "
1314 "idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001315 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001316 }
1317 *out_num += ret;
1318 }
1319 } while ((i = next_desc(&desc)) != -1);
1320
1321 /* On success, increment avail index. */
1322 vq->last_avail_idx++;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001323
1324 /* Assume notifications from guest are disabled at this point,
1325 * if they aren't we would need to update avail_event index. */
1326 BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001327 return head;
1328}
1329
1330/* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
David Stevens8dd014a2010-07-27 18:52:21 +03001331void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001332{
David Stevens8dd014a2010-07-27 18:52:21 +03001333 vq->last_avail_idx -= n;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001334}
1335
1336/* After we've used one of their buffers, we tell them about it. We'll then
1337 * want to notify the guest, using eventfd. */
1338int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
1339{
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001340 struct vring_used_elem __user *used;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001341
1342 /* The virtqueue contains a ring of used buffers. Get a pointer to the
1343 * next entry in that used ring. */
1344 used = &vq->used->ring[vq->last_used_idx % vq->num];
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001345 if (__put_user(head, &used->id)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001346 vq_err(vq, "Failed to write used id");
1347 return -EFAULT;
1348 }
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001349 if (__put_user(len, &used->len)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001350 vq_err(vq, "Failed to write used len");
1351 return -EFAULT;
1352 }
1353 /* Make sure buffer is written before we update index. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001354 smp_wmb();
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001355 if (__put_user(vq->last_used_idx + 1, &vq->used->idx)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001356 vq_err(vq, "Failed to increment used idx");
1357 return -EFAULT;
1358 }
1359 if (unlikely(vq->log_used)) {
1360 /* Make sure data is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001361 smp_wmb();
Michael S. Tsirkin86e94242010-02-17 19:11:33 +02001362 /* Log used ring entry write. */
1363 log_write(vq->log_base,
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001364 vq->log_addr +
1365 ((void __user *)used - (void __user *)vq->used),
Michael S. Tsirkin86e94242010-02-17 19:11:33 +02001366 sizeof *used);
1367 /* Log used index update. */
1368 log_write(vq->log_base,
1369 vq->log_addr + offsetof(struct vring_used, idx),
1370 sizeof vq->used->idx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001371 if (vq->log_ctx)
1372 eventfd_signal(vq->log_ctx, 1);
1373 }
1374 vq->last_used_idx++;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001375 /* If the driver never bothers to signal in a very long while,
1376 * used index might wrap around. If that happens, invalidate
1377 * signalled_used index we stored. TODO: make sure driver
1378 * signals at least once in 2^16 and remove this. */
1379 if (unlikely(vq->last_used_idx == vq->signalled_used))
1380 vq->signalled_used_valid = false;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001381 return 0;
1382}
1383
David Stevens8dd014a2010-07-27 18:52:21 +03001384static int __vhost_add_used_n(struct vhost_virtqueue *vq,
1385 struct vring_used_elem *heads,
1386 unsigned count)
1387{
1388 struct vring_used_elem __user *used;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001389 u16 old, new;
David Stevens8dd014a2010-07-27 18:52:21 +03001390 int start;
1391
1392 start = vq->last_used_idx % vq->num;
1393 used = vq->used->ring + start;
Michael S. Tsirkindfe5ac52010-09-21 14:18:01 +02001394 if (__copy_to_user(used, heads, count * sizeof *used)) {
David Stevens8dd014a2010-07-27 18:52:21 +03001395 vq_err(vq, "Failed to write used");
1396 return -EFAULT;
1397 }
1398 if (unlikely(vq->log_used)) {
1399 /* Make sure data is seen before log. */
1400 smp_wmb();
1401 /* Log used ring entry write. */
1402 log_write(vq->log_base,
1403 vq->log_addr +
1404 ((void __user *)used - (void __user *)vq->used),
1405 count * sizeof *used);
1406 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001407 old = vq->last_used_idx;
1408 new = (vq->last_used_idx += count);
1409 /* If the driver never bothers to signal in a very long while,
1410 * used index might wrap around. If that happens, invalidate
1411 * signalled_used index we stored. TODO: make sure driver
1412 * signals at least once in 2^16 and remove this. */
1413 if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old)))
1414 vq->signalled_used_valid = false;
David Stevens8dd014a2010-07-27 18:52:21 +03001415 return 0;
1416}
1417
1418/* After we've used one of their buffers, we tell them about it. We'll then
1419 * want to notify the guest, using eventfd. */
1420int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
1421 unsigned count)
1422{
1423 int start, n, r;
1424
1425 start = vq->last_used_idx % vq->num;
1426 n = vq->num - start;
1427 if (n < count) {
1428 r = __vhost_add_used_n(vq, heads, n);
1429 if (r < 0)
1430 return r;
1431 heads += n;
1432 count -= n;
1433 }
1434 r = __vhost_add_used_n(vq, heads, count);
1435
1436 /* Make sure buffer is written before we update index. */
1437 smp_wmb();
1438 if (put_user(vq->last_used_idx, &vq->used->idx)) {
1439 vq_err(vq, "Failed to increment used idx");
1440 return -EFAULT;
1441 }
1442 if (unlikely(vq->log_used)) {
1443 /* Log used index update. */
1444 log_write(vq->log_base,
1445 vq->log_addr + offsetof(struct vring_used, idx),
1446 sizeof vq->used->idx);
1447 if (vq->log_ctx)
1448 eventfd_signal(vq->log_ctx, 1);
1449 }
1450 return r;
1451}
1452
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001453static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001454{
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001455 __u16 old, new, event;
1456 bool v;
Michael S. Tsirkin0d499352010-05-11 19:44:17 +03001457 /* Flush out used index updates. This is paired
1458 * with the barrier that the Guest executes when enabling
1459 * interrupts. */
1460 smp_mb();
1461
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001462 if (vhost_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY) &&
1463 unlikely(vq->avail_idx == vq->last_avail_idx))
1464 return true;
1465
1466 if (!vhost_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
1467 __u16 flags;
1468 if (__get_user(flags, &vq->avail->flags)) {
1469 vq_err(vq, "Failed to get flags");
1470 return true;
1471 }
1472 return !(flags & VRING_AVAIL_F_NO_INTERRUPT);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001473 }
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001474 old = vq->signalled_used;
1475 v = vq->signalled_used_valid;
1476 new = vq->signalled_used = vq->last_used_idx;
1477 vq->signalled_used_valid = true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001478
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001479 if (unlikely(!v))
1480 return true;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001481
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001482 if (get_user(event, vhost_used_event(vq))) {
1483 vq_err(vq, "Failed to get used event idx");
1484 return true;
1485 }
1486 return vring_need_event(event, new, old);
1487}
1488
1489/* This actually signals the guest, using eventfd. */
1490void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
1491{
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001492 /* Signal the Guest tell them we used something up. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001493 if (vq->call_ctx && vhost_notify(dev, vq))
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001494 eventfd_signal(vq->call_ctx, 1);
1495}
1496
1497/* And here's the combo meal deal. Supersize me! */
1498void vhost_add_used_and_signal(struct vhost_dev *dev,
1499 struct vhost_virtqueue *vq,
1500 unsigned int head, int len)
1501{
1502 vhost_add_used(vq, head, len);
1503 vhost_signal(dev, vq);
1504}
1505
David Stevens8dd014a2010-07-27 18:52:21 +03001506/* multi-buffer version of vhost_add_used_and_signal */
1507void vhost_add_used_and_signal_n(struct vhost_dev *dev,
1508 struct vhost_virtqueue *vq,
1509 struct vring_used_elem *heads, unsigned count)
1510{
1511 vhost_add_used_n(vq, heads, count);
1512 vhost_signal(dev, vq);
1513}
1514
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001515/* OK, now we need to know about added descriptors. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001516bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001517{
1518 u16 avail_idx;
1519 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301520
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001521 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
1522 return false;
1523 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001524 if (!vhost_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08001525 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001526 if (r) {
1527 vq_err(vq, "Failed to enable notification at %p: %d\n",
1528 &vq->used->flags, r);
1529 return false;
1530 }
1531 } else {
Jason Wang2723fea2011-06-21 18:04:38 +08001532 r = vhost_update_avail_event(vq, vq->avail_idx);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001533 if (r) {
1534 vq_err(vq, "Failed to update avail event index at %p: %d\n",
1535 vhost_avail_event(vq), r);
1536 return false;
1537 }
1538 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001539 /* They could have slipped one in as we were doing that: make
1540 * sure it's written, then check again. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001541 smp_mb();
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001542 r = __get_user(avail_idx, &vq->avail->idx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001543 if (r) {
1544 vq_err(vq, "Failed to check avail idx at %p: %d\n",
1545 &vq->avail->idx, r);
1546 return false;
1547 }
1548
David Stevens8dd014a2010-07-27 18:52:21 +03001549 return avail_idx != vq->avail_idx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001550}
1551
1552/* We don't need to be notified again. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001553void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001554{
1555 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301556
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001557 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
1558 return;
1559 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001560 if (!vhost_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
Jason Wang2723fea2011-06-21 18:04:38 +08001561 r = vhost_update_used_flags(vq);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +03001562 if (r)
1563 vq_err(vq, "Failed to enable notification at %p: %d\n",
1564 &vq->used->flags, r);
1565 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001566}
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +00001567
1568static void vhost_zerocopy_done_signal(struct kref *kref)
1569{
1570 struct vhost_ubuf_ref *ubufs = container_of(kref, struct vhost_ubuf_ref,
1571 kref);
1572 wake_up(&ubufs->wait);
1573}
1574
1575struct vhost_ubuf_ref *vhost_ubuf_alloc(struct vhost_virtqueue *vq,
1576 bool zcopy)
1577{
1578 struct vhost_ubuf_ref *ubufs;
1579 /* No zero copy backend? Nothing to count. */
1580 if (!zcopy)
1581 return NULL;
1582 ubufs = kmalloc(sizeof *ubufs, GFP_KERNEL);
1583 if (!ubufs)
1584 return ERR_PTR(-ENOMEM);
1585 kref_init(&ubufs->kref);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +00001586 init_waitqueue_head(&ubufs->wait);
1587 ubufs->vq = vq;
1588 return ubufs;
1589}
1590
1591void vhost_ubuf_put(struct vhost_ubuf_ref *ubufs)
1592{
1593 kref_put(&ubufs->kref, vhost_zerocopy_done_signal);
1594}
1595
1596void vhost_ubuf_put_and_wait(struct vhost_ubuf_ref *ubufs)
1597{
1598 kref_put(&ubufs->kref, vhost_zerocopy_done_signal);
1599 wait_event(ubufs->wait, !atomic_read(&ubufs->kref.refcount));
1600 kfree(ubufs);
1601}
1602
Michael S. Tsirkinca8f4fb2012-04-09 00:24:02 +00001603void vhost_zerocopy_callback(struct ubuf_info *ubuf)
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +00001604{
Michael S. Tsirkinca8f4fb2012-04-09 00:24:02 +00001605 struct vhost_ubuf_ref *ubufs = ubuf->ctx;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +00001606 struct vhost_virtqueue *vq = ubufs->vq;
1607
Jason Wangc70aa542012-05-02 11:42:54 +08001608 vhost_poll_queue(&vq->poll);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +00001609 /* set len = 1 to mark this desc buffers done DMA */
1610 vq->heads[ubuf->desc].len = VHOST_DMA_DONE_LEN;
1611 kref_put(&ubufs->kref, vhost_zerocopy_done_signal);
1612}