xref: /openbmc/linux/drivers/vhost/vhost.c (revision df2634f43f5106947f3735a0b61a6527a4b278cd)
1 /* Copyright (C) 2009 Red Hat, Inc.
2  * Copyright (C) 2006 Rusty Russell IBM Corporation
3  *
4  * Author: Michael S. Tsirkin <mst@redhat.com>
5  *
6  * Inspiration, some code, and most witty comments come from
7  * Documentation/lguest/lguest.c, by Rusty Russell
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.
10  *
11  * Generic code for virtio server in host kernel.
12  */
13 
14 #include <linux/eventfd.h>
15 #include <linux/vhost.h>
16 #include <linux/virtio_net.h>
17 #include <linux/mm.h>
18 #include <linux/mmu_context.h>
19 #include <linux/miscdevice.h>
20 #include <linux/mutex.h>
21 #include <linux/rcupdate.h>
22 #include <linux/poll.h>
23 #include <linux/file.h>
24 #include <linux/highmem.h>
25 #include <linux/slab.h>
26 #include <linux/kthread.h>
27 #include <linux/cgroup.h>
28 
29 #include <linux/net.h>
30 #include <linux/if_packet.h>
31 #include <linux/if_arp.h>
32 
33 #include "vhost.h"
34 
35 enum {
36 	VHOST_MEMORY_MAX_NREGIONS = 64,
37 	VHOST_MEMORY_F_LOG = 0x1,
38 };
39 
40 static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
41 			    poll_table *pt)
42 {
43 	struct vhost_poll *poll;
44 	poll = container_of(pt, struct vhost_poll, table);
45 
46 	poll->wqh = wqh;
47 	add_wait_queue(wqh, &poll->wait);
48 }
49 
50 static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync,
51 			     void *key)
52 {
53 	struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
54 
55 	if (!((unsigned long)key & poll->mask))
56 		return 0;
57 
58 	vhost_poll_queue(poll);
59 	return 0;
60 }
61 
62 static void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
63 {
64 	INIT_LIST_HEAD(&work->node);
65 	work->fn = fn;
66 	init_waitqueue_head(&work->done);
67 	work->flushing = 0;
68 	work->queue_seq = work->done_seq = 0;
69 }
70 
71 /* Init poll structure */
72 void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
73 		     unsigned long mask, struct vhost_dev *dev)
74 {
75 	init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
76 	init_poll_funcptr(&poll->table, vhost_poll_func);
77 	poll->mask = mask;
78 	poll->dev = dev;
79 
80 	vhost_work_init(&poll->work, fn);
81 }
82 
83 /* 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. */
85 void vhost_poll_start(struct vhost_poll *poll, struct file *file)
86 {
87 	unsigned long mask;
88 	mask = file->f_op->poll(file, &poll->table);
89 	if (mask)
90 		vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
91 }
92 
93 /* Stop polling a file. After this function returns, it becomes safe to drop the
94  * file reference. You must also flush afterwards. */
95 void vhost_poll_stop(struct vhost_poll *poll)
96 {
97 	remove_wait_queue(poll->wqh, &poll->wait);
98 }
99 
100 static bool vhost_work_seq_done(struct vhost_dev *dev, struct vhost_work *work,
101 				unsigned seq)
102 {
103 	int left;
104 	spin_lock_irq(&dev->work_lock);
105 	left = seq - work->done_seq;
106 	spin_unlock_irq(&dev->work_lock);
107 	return left <= 0;
108 }
109 
110 static void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
111 {
112 	unsigned seq;
113 	int flushing;
114 
115 	spin_lock_irq(&dev->work_lock);
116 	seq = work->queue_seq;
117 	work->flushing++;
118 	spin_unlock_irq(&dev->work_lock);
119 	wait_event(work->done, vhost_work_seq_done(dev, work, seq));
120 	spin_lock_irq(&dev->work_lock);
121 	flushing = --work->flushing;
122 	spin_unlock_irq(&dev->work_lock);
123 	BUG_ON(flushing < 0);
124 }
125 
126 /* Flush any work that has been scheduled. When calling this, don't hold any
127  * locks that are also used by the callback. */
128 void vhost_poll_flush(struct vhost_poll *poll)
129 {
130 	vhost_work_flush(poll->dev, &poll->work);
131 }
132 
133 static inline void vhost_work_queue(struct vhost_dev *dev,
134 				    struct vhost_work *work)
135 {
136 	unsigned long flags;
137 
138 	spin_lock_irqsave(&dev->work_lock, flags);
139 	if (list_empty(&work->node)) {
140 		list_add_tail(&work->node, &dev->work_list);
141 		work->queue_seq++;
142 		wake_up_process(dev->worker);
143 	}
144 	spin_unlock_irqrestore(&dev->work_lock, flags);
145 }
146 
147 void vhost_poll_queue(struct vhost_poll *poll)
148 {
149 	vhost_work_queue(poll->dev, &poll->work);
150 }
151 
152 static void vhost_vq_reset(struct vhost_dev *dev,
153 			   struct vhost_virtqueue *vq)
154 {
155 	vq->num = 1;
156 	vq->desc = NULL;
157 	vq->avail = NULL;
158 	vq->used = NULL;
159 	vq->last_avail_idx = 0;
160 	vq->avail_idx = 0;
161 	vq->last_used_idx = 0;
162 	vq->used_flags = 0;
163 	vq->log_used = false;
164 	vq->log_addr = -1ull;
165 	vq->vhost_hlen = 0;
166 	vq->sock_hlen = 0;
167 	vq->private_data = NULL;
168 	vq->log_base = NULL;
169 	vq->error_ctx = NULL;
170 	vq->error = NULL;
171 	vq->kick = NULL;
172 	vq->call_ctx = NULL;
173 	vq->call = NULL;
174 	vq->log_ctx = NULL;
175 }
176 
177 static int vhost_worker(void *data)
178 {
179 	struct vhost_dev *dev = data;
180 	struct vhost_work *work = NULL;
181 	unsigned uninitialized_var(seq);
182 
183 	use_mm(dev->mm);
184 
185 	for (;;) {
186 		/* mb paired w/ kthread_stop */
187 		set_current_state(TASK_INTERRUPTIBLE);
188 
189 		spin_lock_irq(&dev->work_lock);
190 		if (work) {
191 			work->done_seq = seq;
192 			if (work->flushing)
193 				wake_up_all(&work->done);
194 		}
195 
196 		if (kthread_should_stop()) {
197 			spin_unlock_irq(&dev->work_lock);
198 			__set_current_state(TASK_RUNNING);
199 			break;
200 		}
201 		if (!list_empty(&dev->work_list)) {
202 			work = list_first_entry(&dev->work_list,
203 						struct vhost_work, node);
204 			list_del_init(&work->node);
205 			seq = work->queue_seq;
206 		} else
207 			work = NULL;
208 		spin_unlock_irq(&dev->work_lock);
209 
210 		if (work) {
211 			__set_current_state(TASK_RUNNING);
212 			work->fn(work);
213 		} else
214 			schedule();
215 
216 	}
217 	unuse_mm(dev->mm);
218 	return 0;
219 }
220 
221 /* Helper to allocate iovec buffers for all vqs. */
222 static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
223 {
224 	int i;
225 	for (i = 0; i < dev->nvqs; ++i) {
226 		dev->vqs[i].indirect = kmalloc(sizeof *dev->vqs[i].indirect *
227 					       UIO_MAXIOV, GFP_KERNEL);
228 		dev->vqs[i].log = kmalloc(sizeof *dev->vqs[i].log * UIO_MAXIOV,
229 					  GFP_KERNEL);
230 		dev->vqs[i].heads = kmalloc(sizeof *dev->vqs[i].heads *
231 					    UIO_MAXIOV, GFP_KERNEL);
232 
233 		if (!dev->vqs[i].indirect || !dev->vqs[i].log ||
234 			!dev->vqs[i].heads)
235 			goto err_nomem;
236 	}
237 	return 0;
238 err_nomem:
239 	for (; i >= 0; --i) {
240 		kfree(dev->vqs[i].indirect);
241 		kfree(dev->vqs[i].log);
242 		kfree(dev->vqs[i].heads);
243 	}
244 	return -ENOMEM;
245 }
246 
247 static void vhost_dev_free_iovecs(struct vhost_dev *dev)
248 {
249 	int i;
250 	for (i = 0; i < dev->nvqs; ++i) {
251 		kfree(dev->vqs[i].indirect);
252 		dev->vqs[i].indirect = NULL;
253 		kfree(dev->vqs[i].log);
254 		dev->vqs[i].log = NULL;
255 		kfree(dev->vqs[i].heads);
256 		dev->vqs[i].heads = NULL;
257 	}
258 }
259 
260 long vhost_dev_init(struct vhost_dev *dev,
261 		    struct vhost_virtqueue *vqs, int nvqs)
262 {
263 	int i;
264 
265 	dev->vqs = vqs;
266 	dev->nvqs = nvqs;
267 	mutex_init(&dev->mutex);
268 	dev->log_ctx = NULL;
269 	dev->log_file = NULL;
270 	dev->memory = NULL;
271 	dev->mm = NULL;
272 	spin_lock_init(&dev->work_lock);
273 	INIT_LIST_HEAD(&dev->work_list);
274 	dev->worker = NULL;
275 
276 	for (i = 0; i < dev->nvqs; ++i) {
277 		dev->vqs[i].log = NULL;
278 		dev->vqs[i].indirect = NULL;
279 		dev->vqs[i].heads = NULL;
280 		dev->vqs[i].dev = dev;
281 		mutex_init(&dev->vqs[i].mutex);
282 		vhost_vq_reset(dev, dev->vqs + i);
283 		if (dev->vqs[i].handle_kick)
284 			vhost_poll_init(&dev->vqs[i].poll,
285 					dev->vqs[i].handle_kick, POLLIN, dev);
286 	}
287 
288 	return 0;
289 }
290 
291 /* Caller should have device mutex */
292 long vhost_dev_check_owner(struct vhost_dev *dev)
293 {
294 	/* Are you the owner? If not, I don't think you mean to do that */
295 	return dev->mm == current->mm ? 0 : -EPERM;
296 }
297 
298 struct vhost_attach_cgroups_struct {
299         struct vhost_work work;
300         struct task_struct *owner;
301         int ret;
302 };
303 
304 static void vhost_attach_cgroups_work(struct vhost_work *work)
305 {
306         struct vhost_attach_cgroups_struct *s;
307         s = container_of(work, struct vhost_attach_cgroups_struct, work);
308         s->ret = cgroup_attach_task_all(s->owner, current);
309 }
310 
311 static int vhost_attach_cgroups(struct vhost_dev *dev)
312 {
313         struct vhost_attach_cgroups_struct attach;
314         attach.owner = current;
315         vhost_work_init(&attach.work, vhost_attach_cgroups_work);
316         vhost_work_queue(dev, &attach.work);
317         vhost_work_flush(dev, &attach.work);
318         return attach.ret;
319 }
320 
321 /* Caller should have device mutex */
322 static long vhost_dev_set_owner(struct vhost_dev *dev)
323 {
324 	struct task_struct *worker;
325 	int err;
326 	/* Is there an owner already? */
327 	if (dev->mm) {
328 		err = -EBUSY;
329 		goto err_mm;
330 	}
331 	/* No owner, become one */
332 	dev->mm = get_task_mm(current);
333 	worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
334 	if (IS_ERR(worker)) {
335 		err = PTR_ERR(worker);
336 		goto err_worker;
337 	}
338 
339 	dev->worker = worker;
340 	wake_up_process(worker);	/* avoid contributing to loadavg */
341 
342 	err = vhost_attach_cgroups(dev);
343 	if (err)
344 		goto err_cgroup;
345 
346 	err = vhost_dev_alloc_iovecs(dev);
347 	if (err)
348 		goto err_cgroup;
349 
350 	return 0;
351 err_cgroup:
352 	kthread_stop(worker);
353 	dev->worker = NULL;
354 err_worker:
355 	if (dev->mm)
356 		mmput(dev->mm);
357 	dev->mm = NULL;
358 err_mm:
359 	return err;
360 }
361 
362 /* Caller should have device mutex */
363 long vhost_dev_reset_owner(struct vhost_dev *dev)
364 {
365 	struct vhost_memory *memory;
366 
367 	/* Restore memory to default empty mapping. */
368 	memory = kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
369 	if (!memory)
370 		return -ENOMEM;
371 
372 	vhost_dev_cleanup(dev);
373 
374 	memory->nregions = 0;
375 	RCU_INIT_POINTER(dev->memory, memory);
376 	return 0;
377 }
378 
379 /* Caller should have device mutex */
380 void vhost_dev_cleanup(struct vhost_dev *dev)
381 {
382 	int i;
383 	for (i = 0; i < dev->nvqs; ++i) {
384 		if (dev->vqs[i].kick && dev->vqs[i].handle_kick) {
385 			vhost_poll_stop(&dev->vqs[i].poll);
386 			vhost_poll_flush(&dev->vqs[i].poll);
387 		}
388 		if (dev->vqs[i].error_ctx)
389 			eventfd_ctx_put(dev->vqs[i].error_ctx);
390 		if (dev->vqs[i].error)
391 			fput(dev->vqs[i].error);
392 		if (dev->vqs[i].kick)
393 			fput(dev->vqs[i].kick);
394 		if (dev->vqs[i].call_ctx)
395 			eventfd_ctx_put(dev->vqs[i].call_ctx);
396 		if (dev->vqs[i].call)
397 			fput(dev->vqs[i].call);
398 		vhost_vq_reset(dev, dev->vqs + i);
399 	}
400 	vhost_dev_free_iovecs(dev);
401 	if (dev->log_ctx)
402 		eventfd_ctx_put(dev->log_ctx);
403 	dev->log_ctx = NULL;
404 	if (dev->log_file)
405 		fput(dev->log_file);
406 	dev->log_file = NULL;
407 	/* No one will access memory at this point */
408 	kfree(rcu_dereference_protected(dev->memory,
409 					lockdep_is_held(&dev->mutex)));
410 	RCU_INIT_POINTER(dev->memory, NULL);
411 	WARN_ON(!list_empty(&dev->work_list));
412 	if (dev->worker) {
413 		kthread_stop(dev->worker);
414 		dev->worker = NULL;
415 	}
416 	if (dev->mm)
417 		mmput(dev->mm);
418 	dev->mm = NULL;
419 }
420 
421 static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
422 {
423 	u64 a = addr / VHOST_PAGE_SIZE / 8;
424 	/* Make sure 64 bit math will not overflow. */
425 	if (a > ULONG_MAX - (unsigned long)log_base ||
426 	    a + (unsigned long)log_base > ULONG_MAX)
427 		return 0;
428 
429 	return access_ok(VERIFY_WRITE, log_base + a,
430 			 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
431 }
432 
433 /* Caller should have vq mutex and device mutex. */
434 static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
435 			       int log_all)
436 {
437 	int i;
438 
439 	if (!mem)
440 		return 0;
441 
442 	for (i = 0; i < mem->nregions; ++i) {
443 		struct vhost_memory_region *m = mem->regions + i;
444 		unsigned long a = m->userspace_addr;
445 		if (m->memory_size > ULONG_MAX)
446 			return 0;
447 		else if (!access_ok(VERIFY_WRITE, (void __user *)a,
448 				    m->memory_size))
449 			return 0;
450 		else if (log_all && !log_access_ok(log_base,
451 						   m->guest_phys_addr,
452 						   m->memory_size))
453 			return 0;
454 	}
455 	return 1;
456 }
457 
458 /* Can we switch to this memory table? */
459 /* Caller should have device mutex but not vq mutex */
460 static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
461 			    int log_all)
462 {
463 	int i;
464 	for (i = 0; i < d->nvqs; ++i) {
465 		int ok;
466 		mutex_lock(&d->vqs[i].mutex);
467 		/* If ring is inactive, will check when it's enabled. */
468 		if (d->vqs[i].private_data)
469 			ok = vq_memory_access_ok(d->vqs[i].log_base, mem,
470 						 log_all);
471 		else
472 			ok = 1;
473 		mutex_unlock(&d->vqs[i].mutex);
474 		if (!ok)
475 			return 0;
476 	}
477 	return 1;
478 }
479 
480 static int vq_access_ok(unsigned int num,
481 			struct vring_desc __user *desc,
482 			struct vring_avail __user *avail,
483 			struct vring_used __user *used)
484 {
485 	return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
486 	       access_ok(VERIFY_READ, avail,
487 			 sizeof *avail + num * sizeof *avail->ring) &&
488 	       access_ok(VERIFY_WRITE, used,
489 			sizeof *used + num * sizeof *used->ring);
490 }
491 
492 /* Can we log writes? */
493 /* Caller should have device mutex but not vq mutex */
494 int vhost_log_access_ok(struct vhost_dev *dev)
495 {
496 	struct vhost_memory *mp;
497 
498 	mp = rcu_dereference_protected(dev->memory,
499 				       lockdep_is_held(&dev->mutex));
500 	return memory_access_ok(dev, mp, 1);
501 }
502 
503 /* Verify access for write logging. */
504 /* Caller should have vq mutex and device mutex */
505 static int vq_log_access_ok(struct vhost_virtqueue *vq, void __user *log_base)
506 {
507 	struct vhost_memory *mp;
508 
509 	mp = rcu_dereference_protected(vq->dev->memory,
510 				       lockdep_is_held(&vq->mutex));
511 	return vq_memory_access_ok(log_base, mp,
512 			    vhost_has_feature(vq->dev, VHOST_F_LOG_ALL)) &&
513 		(!vq->log_used || log_access_ok(log_base, vq->log_addr,
514 					sizeof *vq->used +
515 					vq->num * sizeof *vq->used->ring));
516 }
517 
518 /* Can we start vq? */
519 /* Caller should have vq mutex and device mutex */
520 int vhost_vq_access_ok(struct vhost_virtqueue *vq)
521 {
522 	return vq_access_ok(vq->num, vq->desc, vq->avail, vq->used) &&
523 		vq_log_access_ok(vq, vq->log_base);
524 }
525 
526 static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
527 {
528 	struct vhost_memory mem, *newmem, *oldmem;
529 	unsigned long size = offsetof(struct vhost_memory, regions);
530 	if (copy_from_user(&mem, m, size))
531 		return -EFAULT;
532 	if (mem.padding)
533 		return -EOPNOTSUPP;
534 	if (mem.nregions > VHOST_MEMORY_MAX_NREGIONS)
535 		return -E2BIG;
536 	newmem = kmalloc(size + mem.nregions * sizeof *m->regions, GFP_KERNEL);
537 	if (!newmem)
538 		return -ENOMEM;
539 
540 	memcpy(newmem, &mem, size);
541 	if (copy_from_user(newmem->regions, m->regions,
542 			   mem.nregions * sizeof *m->regions)) {
543 		kfree(newmem);
544 		return -EFAULT;
545 	}
546 
547 	if (!memory_access_ok(d, newmem, vhost_has_feature(d, VHOST_F_LOG_ALL))) {
548 		kfree(newmem);
549 		return -EFAULT;
550 	}
551 	oldmem = rcu_dereference_protected(d->memory,
552 					   lockdep_is_held(&d->mutex));
553 	rcu_assign_pointer(d->memory, newmem);
554 	synchronize_rcu();
555 	kfree(oldmem);
556 	return 0;
557 }
558 
559 static int init_used(struct vhost_virtqueue *vq,
560 		     struct vring_used __user *used)
561 {
562 	int r = put_user(vq->used_flags, &used->flags);
563 	if (r)
564 		return r;
565 	return get_user(vq->last_used_idx, &used->idx);
566 }
567 
568 static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp)
569 {
570 	struct file *eventfp, *filep = NULL,
571 		    *pollstart = NULL, *pollstop = NULL;
572 	struct eventfd_ctx *ctx = NULL;
573 	u32 __user *idxp = argp;
574 	struct vhost_virtqueue *vq;
575 	struct vhost_vring_state s;
576 	struct vhost_vring_file f;
577 	struct vhost_vring_addr a;
578 	u32 idx;
579 	long r;
580 
581 	r = get_user(idx, idxp);
582 	if (r < 0)
583 		return r;
584 	if (idx >= d->nvqs)
585 		return -ENOBUFS;
586 
587 	vq = d->vqs + idx;
588 
589 	mutex_lock(&vq->mutex);
590 
591 	switch (ioctl) {
592 	case VHOST_SET_VRING_NUM:
593 		/* Resizing ring with an active backend?
594 		 * You don't want to do that. */
595 		if (vq->private_data) {
596 			r = -EBUSY;
597 			break;
598 		}
599 		if (copy_from_user(&s, argp, sizeof s)) {
600 			r = -EFAULT;
601 			break;
602 		}
603 		if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
604 			r = -EINVAL;
605 			break;
606 		}
607 		vq->num = s.num;
608 		break;
609 	case VHOST_SET_VRING_BASE:
610 		/* Moving base with an active backend?
611 		 * You don't want to do that. */
612 		if (vq->private_data) {
613 			r = -EBUSY;
614 			break;
615 		}
616 		if (copy_from_user(&s, argp, sizeof s)) {
617 			r = -EFAULT;
618 			break;
619 		}
620 		if (s.num > 0xffff) {
621 			r = -EINVAL;
622 			break;
623 		}
624 		vq->last_avail_idx = s.num;
625 		/* Forget the cached index value. */
626 		vq->avail_idx = vq->last_avail_idx;
627 		break;
628 	case VHOST_GET_VRING_BASE:
629 		s.index = idx;
630 		s.num = vq->last_avail_idx;
631 		if (copy_to_user(argp, &s, sizeof s))
632 			r = -EFAULT;
633 		break;
634 	case VHOST_SET_VRING_ADDR:
635 		if (copy_from_user(&a, argp, sizeof a)) {
636 			r = -EFAULT;
637 			break;
638 		}
639 		if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
640 			r = -EOPNOTSUPP;
641 			break;
642 		}
643 		/* For 32bit, verify that the top 32bits of the user
644 		   data are set to zero. */
645 		if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
646 		    (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
647 		    (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
648 			r = -EFAULT;
649 			break;
650 		}
651 		if ((a.avail_user_addr & (sizeof *vq->avail->ring - 1)) ||
652 		    (a.used_user_addr & (sizeof *vq->used->ring - 1)) ||
653 		    (a.log_guest_addr & (sizeof *vq->used->ring - 1))) {
654 			r = -EINVAL;
655 			break;
656 		}
657 
658 		/* We only verify access here if backend is configured.
659 		 * If it is not, we don't as size might not have been setup.
660 		 * We will verify when backend is configured. */
661 		if (vq->private_data) {
662 			if (!vq_access_ok(vq->num,
663 				(void __user *)(unsigned long)a.desc_user_addr,
664 				(void __user *)(unsigned long)a.avail_user_addr,
665 				(void __user *)(unsigned long)a.used_user_addr)) {
666 				r = -EINVAL;
667 				break;
668 			}
669 
670 			/* Also validate log access for used ring if enabled. */
671 			if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
672 			    !log_access_ok(vq->log_base, a.log_guest_addr,
673 					   sizeof *vq->used +
674 					   vq->num * sizeof *vq->used->ring)) {
675 				r = -EINVAL;
676 				break;
677 			}
678 		}
679 
680 		r = init_used(vq, (struct vring_used __user *)(unsigned long)
681 			      a.used_user_addr);
682 		if (r)
683 			break;
684 		vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
685 		vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
686 		vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
687 		vq->log_addr = a.log_guest_addr;
688 		vq->used = (void __user *)(unsigned long)a.used_user_addr;
689 		break;
690 	case VHOST_SET_VRING_KICK:
691 		if (copy_from_user(&f, argp, sizeof f)) {
692 			r = -EFAULT;
693 			break;
694 		}
695 		eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
696 		if (IS_ERR(eventfp)) {
697 			r = PTR_ERR(eventfp);
698 			break;
699 		}
700 		if (eventfp != vq->kick) {
701 			pollstop = filep = vq->kick;
702 			pollstart = vq->kick = eventfp;
703 		} else
704 			filep = eventfp;
705 		break;
706 	case VHOST_SET_VRING_CALL:
707 		if (copy_from_user(&f, argp, sizeof f)) {
708 			r = -EFAULT;
709 			break;
710 		}
711 		eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
712 		if (IS_ERR(eventfp)) {
713 			r = PTR_ERR(eventfp);
714 			break;
715 		}
716 		if (eventfp != vq->call) {
717 			filep = vq->call;
718 			ctx = vq->call_ctx;
719 			vq->call = eventfp;
720 			vq->call_ctx = eventfp ?
721 				eventfd_ctx_fileget(eventfp) : NULL;
722 		} else
723 			filep = eventfp;
724 		break;
725 	case VHOST_SET_VRING_ERR:
726 		if (copy_from_user(&f, argp, sizeof f)) {
727 			r = -EFAULT;
728 			break;
729 		}
730 		eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
731 		if (IS_ERR(eventfp)) {
732 			r = PTR_ERR(eventfp);
733 			break;
734 		}
735 		if (eventfp != vq->error) {
736 			filep = vq->error;
737 			vq->error = eventfp;
738 			ctx = vq->error_ctx;
739 			vq->error_ctx = eventfp ?
740 				eventfd_ctx_fileget(eventfp) : NULL;
741 		} else
742 			filep = eventfp;
743 		break;
744 	default:
745 		r = -ENOIOCTLCMD;
746 	}
747 
748 	if (pollstop && vq->handle_kick)
749 		vhost_poll_stop(&vq->poll);
750 
751 	if (ctx)
752 		eventfd_ctx_put(ctx);
753 	if (filep)
754 		fput(filep);
755 
756 	if (pollstart && vq->handle_kick)
757 		vhost_poll_start(&vq->poll, vq->kick);
758 
759 	mutex_unlock(&vq->mutex);
760 
761 	if (pollstop && vq->handle_kick)
762 		vhost_poll_flush(&vq->poll);
763 	return r;
764 }
765 
766 /* Caller must have device mutex */
767 long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, unsigned long arg)
768 {
769 	void __user *argp = (void __user *)arg;
770 	struct file *eventfp, *filep = NULL;
771 	struct eventfd_ctx *ctx = NULL;
772 	u64 p;
773 	long r;
774 	int i, fd;
775 
776 	/* If you are not the owner, you can become one */
777 	if (ioctl == VHOST_SET_OWNER) {
778 		r = vhost_dev_set_owner(d);
779 		goto done;
780 	}
781 
782 	/* You must be the owner to do anything else */
783 	r = vhost_dev_check_owner(d);
784 	if (r)
785 		goto done;
786 
787 	switch (ioctl) {
788 	case VHOST_SET_MEM_TABLE:
789 		r = vhost_set_memory(d, argp);
790 		break;
791 	case VHOST_SET_LOG_BASE:
792 		if (copy_from_user(&p, argp, sizeof p)) {
793 			r = -EFAULT;
794 			break;
795 		}
796 		if ((u64)(unsigned long)p != p) {
797 			r = -EFAULT;
798 			break;
799 		}
800 		for (i = 0; i < d->nvqs; ++i) {
801 			struct vhost_virtqueue *vq;
802 			void __user *base = (void __user *)(unsigned long)p;
803 			vq = d->vqs + i;
804 			mutex_lock(&vq->mutex);
805 			/* If ring is inactive, will check when it's enabled. */
806 			if (vq->private_data && !vq_log_access_ok(vq, base))
807 				r = -EFAULT;
808 			else
809 				vq->log_base = base;
810 			mutex_unlock(&vq->mutex);
811 		}
812 		break;
813 	case VHOST_SET_LOG_FD:
814 		r = get_user(fd, (int __user *)argp);
815 		if (r < 0)
816 			break;
817 		eventfp = fd == -1 ? NULL : eventfd_fget(fd);
818 		if (IS_ERR(eventfp)) {
819 			r = PTR_ERR(eventfp);
820 			break;
821 		}
822 		if (eventfp != d->log_file) {
823 			filep = d->log_file;
824 			ctx = d->log_ctx;
825 			d->log_ctx = eventfp ?
826 				eventfd_ctx_fileget(eventfp) : NULL;
827 		} else
828 			filep = eventfp;
829 		for (i = 0; i < d->nvqs; ++i) {
830 			mutex_lock(&d->vqs[i].mutex);
831 			d->vqs[i].log_ctx = d->log_ctx;
832 			mutex_unlock(&d->vqs[i].mutex);
833 		}
834 		if (ctx)
835 			eventfd_ctx_put(ctx);
836 		if (filep)
837 			fput(filep);
838 		break;
839 	default:
840 		r = vhost_set_vring(d, ioctl, argp);
841 		break;
842 	}
843 done:
844 	return r;
845 }
846 
847 static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
848 						     __u64 addr, __u32 len)
849 {
850 	struct vhost_memory_region *reg;
851 	int i;
852 	/* linear search is not brilliant, but we really have on the order of 6
853 	 * regions in practice */
854 	for (i = 0; i < mem->nregions; ++i) {
855 		reg = mem->regions + i;
856 		if (reg->guest_phys_addr <= addr &&
857 		    reg->guest_phys_addr + reg->memory_size - 1 >= addr)
858 			return reg;
859 	}
860 	return NULL;
861 }
862 
863 /* TODO: This is really inefficient.  We need something like get_user()
864  * (instruction directly accesses the data, with an exception table entry
865  * returning -EFAULT). See Documentation/x86/exception-tables.txt.
866  */
867 static int set_bit_to_user(int nr, void __user *addr)
868 {
869 	unsigned long log = (unsigned long)addr;
870 	struct page *page;
871 	void *base;
872 	int bit = nr + (log % PAGE_SIZE) * 8;
873 	int r;
874 	r = get_user_pages_fast(log, 1, 1, &page);
875 	if (r < 0)
876 		return r;
877 	BUG_ON(r != 1);
878 	base = kmap_atomic(page, KM_USER0);
879 	set_bit(bit, base);
880 	kunmap_atomic(base, KM_USER0);
881 	set_page_dirty_lock(page);
882 	put_page(page);
883 	return 0;
884 }
885 
886 static int log_write(void __user *log_base,
887 		     u64 write_address, u64 write_length)
888 {
889 	u64 write_page = write_address / VHOST_PAGE_SIZE;
890 	int r;
891 	if (!write_length)
892 		return 0;
893 	write_length += write_address % VHOST_PAGE_SIZE;
894 	for (;;) {
895 		u64 base = (u64)(unsigned long)log_base;
896 		u64 log = base + write_page / 8;
897 		int bit = write_page % 8;
898 		if ((u64)(unsigned long)log != log)
899 			return -EFAULT;
900 		r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
901 		if (r < 0)
902 			return r;
903 		if (write_length <= VHOST_PAGE_SIZE)
904 			break;
905 		write_length -= VHOST_PAGE_SIZE;
906 		write_page += 1;
907 	}
908 	return r;
909 }
910 
911 int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
912 		    unsigned int log_num, u64 len)
913 {
914 	int i, r;
915 
916 	/* Make sure data written is seen before log. */
917 	smp_wmb();
918 	for (i = 0; i < log_num; ++i) {
919 		u64 l = min(log[i].len, len);
920 		r = log_write(vq->log_base, log[i].addr, l);
921 		if (r < 0)
922 			return r;
923 		len -= l;
924 		if (!len) {
925 			if (vq->log_ctx)
926 				eventfd_signal(vq->log_ctx, 1);
927 			return 0;
928 		}
929 	}
930 	/* Length written exceeds what we have stored. This is a bug. */
931 	BUG();
932 	return 0;
933 }
934 
935 static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
936 			  struct iovec iov[], int iov_size)
937 {
938 	const struct vhost_memory_region *reg;
939 	struct vhost_memory *mem;
940 	struct iovec *_iov;
941 	u64 s = 0;
942 	int ret = 0;
943 
944 	rcu_read_lock();
945 
946 	mem = rcu_dereference(dev->memory);
947 	while ((u64)len > s) {
948 		u64 size;
949 		if (unlikely(ret >= iov_size)) {
950 			ret = -ENOBUFS;
951 			break;
952 		}
953 		reg = find_region(mem, addr, len);
954 		if (unlikely(!reg)) {
955 			ret = -EFAULT;
956 			break;
957 		}
958 		_iov = iov + ret;
959 		size = reg->memory_size - addr + reg->guest_phys_addr;
960 		_iov->iov_len = min((u64)len, size);
961 		_iov->iov_base = (void __user *)(unsigned long)
962 			(reg->userspace_addr + addr - reg->guest_phys_addr);
963 		s += size;
964 		addr += size;
965 		++ret;
966 	}
967 
968 	rcu_read_unlock();
969 	return ret;
970 }
971 
972 /* Each buffer in the virtqueues is actually a chain of descriptors.  This
973  * function returns the next descriptor in the chain,
974  * or -1U if we're at the end. */
975 static unsigned next_desc(struct vring_desc *desc)
976 {
977 	unsigned int next;
978 
979 	/* If this descriptor says it doesn't chain, we're done. */
980 	if (!(desc->flags & VRING_DESC_F_NEXT))
981 		return -1U;
982 
983 	/* Check they're not leading us off end of descriptors. */
984 	next = desc->next;
985 	/* Make sure compiler knows to grab that: we don't want it changing! */
986 	/* We will use the result as an index in an array, so most
987 	 * architectures only need a compiler barrier here. */
988 	read_barrier_depends();
989 
990 	return next;
991 }
992 
993 static int get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
994 			struct iovec iov[], unsigned int iov_size,
995 			unsigned int *out_num, unsigned int *in_num,
996 			struct vhost_log *log, unsigned int *log_num,
997 			struct vring_desc *indirect)
998 {
999 	struct vring_desc desc;
1000 	unsigned int i = 0, count, found = 0;
1001 	int ret;
1002 
1003 	/* Sanity check */
1004 	if (unlikely(indirect->len % sizeof desc)) {
1005 		vq_err(vq, "Invalid length in indirect descriptor: "
1006 		       "len 0x%llx not multiple of 0x%zx\n",
1007 		       (unsigned long long)indirect->len,
1008 		       sizeof desc);
1009 		return -EINVAL;
1010 	}
1011 
1012 	ret = translate_desc(dev, indirect->addr, indirect->len, vq->indirect,
1013 			     UIO_MAXIOV);
1014 	if (unlikely(ret < 0)) {
1015 		vq_err(vq, "Translation failure %d in indirect.\n", ret);
1016 		return ret;
1017 	}
1018 
1019 	/* We will use the result as an address to read from, so most
1020 	 * architectures only need a compiler barrier here. */
1021 	read_barrier_depends();
1022 
1023 	count = indirect->len / sizeof desc;
1024 	/* Buffers are chained via a 16 bit next field, so
1025 	 * we can have at most 2^16 of these. */
1026 	if (unlikely(count > USHRT_MAX + 1)) {
1027 		vq_err(vq, "Indirect buffer length too big: %d\n",
1028 		       indirect->len);
1029 		return -E2BIG;
1030 	}
1031 
1032 	do {
1033 		unsigned iov_count = *in_num + *out_num;
1034 		if (unlikely(++found > count)) {
1035 			vq_err(vq, "Loop detected: last one at %u "
1036 			       "indirect size %u\n",
1037 			       i, count);
1038 			return -EINVAL;
1039 		}
1040 		if (unlikely(memcpy_fromiovec((unsigned char *)&desc, vq->indirect,
1041 					      sizeof desc))) {
1042 			vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
1043 			       i, (size_t)indirect->addr + i * sizeof desc);
1044 			return -EINVAL;
1045 		}
1046 		if (unlikely(desc.flags & VRING_DESC_F_INDIRECT)) {
1047 			vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
1048 			       i, (size_t)indirect->addr + i * sizeof desc);
1049 			return -EINVAL;
1050 		}
1051 
1052 		ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
1053 				     iov_size - iov_count);
1054 		if (unlikely(ret < 0)) {
1055 			vq_err(vq, "Translation failure %d indirect idx %d\n",
1056 			       ret, i);
1057 			return ret;
1058 		}
1059 		/* If this is an input descriptor, increment that count. */
1060 		if (desc.flags & VRING_DESC_F_WRITE) {
1061 			*in_num += ret;
1062 			if (unlikely(log)) {
1063 				log[*log_num].addr = desc.addr;
1064 				log[*log_num].len = desc.len;
1065 				++*log_num;
1066 			}
1067 		} else {
1068 			/* If it's an output descriptor, they're all supposed
1069 			 * to come before any input descriptors. */
1070 			if (unlikely(*in_num)) {
1071 				vq_err(vq, "Indirect descriptor "
1072 				       "has out after in: idx %d\n", i);
1073 				return -EINVAL;
1074 			}
1075 			*out_num += ret;
1076 		}
1077 	} while ((i = next_desc(&desc)) != -1);
1078 	return 0;
1079 }
1080 
1081 /* This looks in the virtqueue and for the first available buffer, and converts
1082  * it to an iovec for convenient access.  Since descriptors consist of some
1083  * number of output then some number of input descriptors, it's actually two
1084  * iovecs, but we pack them into one and note how many of each there were.
1085  *
1086  * This function returns the descriptor number found, or vq->num (which is
1087  * never a valid descriptor number) if none was found.  A negative code is
1088  * returned on error. */
1089 int vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
1090 		      struct iovec iov[], unsigned int iov_size,
1091 		      unsigned int *out_num, unsigned int *in_num,
1092 		      struct vhost_log *log, unsigned int *log_num)
1093 {
1094 	struct vring_desc desc;
1095 	unsigned int i, head, found = 0;
1096 	u16 last_avail_idx;
1097 	int ret;
1098 
1099 	/* Check it isn't doing very strange things with descriptor numbers. */
1100 	last_avail_idx = vq->last_avail_idx;
1101 	if (unlikely(__get_user(vq->avail_idx, &vq->avail->idx))) {
1102 		vq_err(vq, "Failed to access avail idx at %p\n",
1103 		       &vq->avail->idx);
1104 		return -EFAULT;
1105 	}
1106 
1107 	if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
1108 		vq_err(vq, "Guest moved used index from %u to %u",
1109 		       last_avail_idx, vq->avail_idx);
1110 		return -EFAULT;
1111 	}
1112 
1113 	/* If there's nothing new since last we looked, return invalid. */
1114 	if (vq->avail_idx == last_avail_idx)
1115 		return vq->num;
1116 
1117 	/* Only get avail ring entries after they have been exposed by guest. */
1118 	smp_rmb();
1119 
1120 	/* Grab the next descriptor number they're advertising, and increment
1121 	 * the index we've seen. */
1122 	if (unlikely(__get_user(head,
1123 				&vq->avail->ring[last_avail_idx % vq->num]))) {
1124 		vq_err(vq, "Failed to read head: idx %d address %p\n",
1125 		       last_avail_idx,
1126 		       &vq->avail->ring[last_avail_idx % vq->num]);
1127 		return -EFAULT;
1128 	}
1129 
1130 	/* If their number is silly, that's an error. */
1131 	if (unlikely(head >= vq->num)) {
1132 		vq_err(vq, "Guest says index %u > %u is available",
1133 		       head, vq->num);
1134 		return -EINVAL;
1135 	}
1136 
1137 	/* When we start there are none of either input nor output. */
1138 	*out_num = *in_num = 0;
1139 	if (unlikely(log))
1140 		*log_num = 0;
1141 
1142 	i = head;
1143 	do {
1144 		unsigned iov_count = *in_num + *out_num;
1145 		if (unlikely(i >= vq->num)) {
1146 			vq_err(vq, "Desc index is %u > %u, head = %u",
1147 			       i, vq->num, head);
1148 			return -EINVAL;
1149 		}
1150 		if (unlikely(++found > vq->num)) {
1151 			vq_err(vq, "Loop detected: last one at %u "
1152 			       "vq size %u head %u\n",
1153 			       i, vq->num, head);
1154 			return -EINVAL;
1155 		}
1156 		ret = copy_from_user(&desc, vq->desc + i, sizeof desc);
1157 		if (unlikely(ret)) {
1158 			vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
1159 			       i, vq->desc + i);
1160 			return -EFAULT;
1161 		}
1162 		if (desc.flags & VRING_DESC_F_INDIRECT) {
1163 			ret = get_indirect(dev, vq, iov, iov_size,
1164 					   out_num, in_num,
1165 					   log, log_num, &desc);
1166 			if (unlikely(ret < 0)) {
1167 				vq_err(vq, "Failure detected "
1168 				       "in indirect descriptor at idx %d\n", i);
1169 				return ret;
1170 			}
1171 			continue;
1172 		}
1173 
1174 		ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
1175 				     iov_size - iov_count);
1176 		if (unlikely(ret < 0)) {
1177 			vq_err(vq, "Translation failure %d descriptor idx %d\n",
1178 			       ret, i);
1179 			return ret;
1180 		}
1181 		if (desc.flags & VRING_DESC_F_WRITE) {
1182 			/* If this is an input descriptor,
1183 			 * increment that count. */
1184 			*in_num += ret;
1185 			if (unlikely(log)) {
1186 				log[*log_num].addr = desc.addr;
1187 				log[*log_num].len = desc.len;
1188 				++*log_num;
1189 			}
1190 		} else {
1191 			/* If it's an output descriptor, they're all supposed
1192 			 * to come before any input descriptors. */
1193 			if (unlikely(*in_num)) {
1194 				vq_err(vq, "Descriptor has out after in: "
1195 				       "idx %d\n", i);
1196 				return -EINVAL;
1197 			}
1198 			*out_num += ret;
1199 		}
1200 	} while ((i = next_desc(&desc)) != -1);
1201 
1202 	/* On success, increment avail index. */
1203 	vq->last_avail_idx++;
1204 	return head;
1205 }
1206 
1207 /* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
1208 void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
1209 {
1210 	vq->last_avail_idx -= n;
1211 }
1212 
1213 /* After we've used one of their buffers, we tell them about it.  We'll then
1214  * want to notify the guest, using eventfd. */
1215 int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
1216 {
1217 	struct vring_used_elem __user *used;
1218 
1219 	/* The virtqueue contains a ring of used buffers.  Get a pointer to the
1220 	 * next entry in that used ring. */
1221 	used = &vq->used->ring[vq->last_used_idx % vq->num];
1222 	if (__put_user(head, &used->id)) {
1223 		vq_err(vq, "Failed to write used id");
1224 		return -EFAULT;
1225 	}
1226 	if (__put_user(len, &used->len)) {
1227 		vq_err(vq, "Failed to write used len");
1228 		return -EFAULT;
1229 	}
1230 	/* Make sure buffer is written before we update index. */
1231 	smp_wmb();
1232 	if (__put_user(vq->last_used_idx + 1, &vq->used->idx)) {
1233 		vq_err(vq, "Failed to increment used idx");
1234 		return -EFAULT;
1235 	}
1236 	if (unlikely(vq->log_used)) {
1237 		/* Make sure data is seen before log. */
1238 		smp_wmb();
1239 		/* Log used ring entry write. */
1240 		log_write(vq->log_base,
1241 			  vq->log_addr +
1242 			   ((void __user *)used - (void __user *)vq->used),
1243 			  sizeof *used);
1244 		/* Log used index update. */
1245 		log_write(vq->log_base,
1246 			  vq->log_addr + offsetof(struct vring_used, idx),
1247 			  sizeof vq->used->idx);
1248 		if (vq->log_ctx)
1249 			eventfd_signal(vq->log_ctx, 1);
1250 	}
1251 	vq->last_used_idx++;
1252 	return 0;
1253 }
1254 
1255 static int __vhost_add_used_n(struct vhost_virtqueue *vq,
1256 			    struct vring_used_elem *heads,
1257 			    unsigned count)
1258 {
1259 	struct vring_used_elem __user *used;
1260 	int start;
1261 
1262 	start = vq->last_used_idx % vq->num;
1263 	used = vq->used->ring + start;
1264 	if (__copy_to_user(used, heads, count * sizeof *used)) {
1265 		vq_err(vq, "Failed to write used");
1266 		return -EFAULT;
1267 	}
1268 	if (unlikely(vq->log_used)) {
1269 		/* Make sure data is seen before log. */
1270 		smp_wmb();
1271 		/* Log used ring entry write. */
1272 		log_write(vq->log_base,
1273 			  vq->log_addr +
1274 			   ((void __user *)used - (void __user *)vq->used),
1275 			  count * sizeof *used);
1276 	}
1277 	vq->last_used_idx += count;
1278 	return 0;
1279 }
1280 
1281 /* After we've used one of their buffers, we tell them about it.  We'll then
1282  * want to notify the guest, using eventfd. */
1283 int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
1284 		     unsigned count)
1285 {
1286 	int start, n, r;
1287 
1288 	start = vq->last_used_idx % vq->num;
1289 	n = vq->num - start;
1290 	if (n < count) {
1291 		r = __vhost_add_used_n(vq, heads, n);
1292 		if (r < 0)
1293 			return r;
1294 		heads += n;
1295 		count -= n;
1296 	}
1297 	r = __vhost_add_used_n(vq, heads, count);
1298 
1299 	/* Make sure buffer is written before we update index. */
1300 	smp_wmb();
1301 	if (put_user(vq->last_used_idx, &vq->used->idx)) {
1302 		vq_err(vq, "Failed to increment used idx");
1303 		return -EFAULT;
1304 	}
1305 	if (unlikely(vq->log_used)) {
1306 		/* Log used index update. */
1307 		log_write(vq->log_base,
1308 			  vq->log_addr + offsetof(struct vring_used, idx),
1309 			  sizeof vq->used->idx);
1310 		if (vq->log_ctx)
1311 			eventfd_signal(vq->log_ctx, 1);
1312 	}
1313 	return r;
1314 }
1315 
1316 /* This actually signals the guest, using eventfd. */
1317 void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
1318 {
1319 	__u16 flags;
1320 	/* Flush out used index updates. This is paired
1321 	 * with the barrier that the Guest executes when enabling
1322 	 * interrupts. */
1323 	smp_mb();
1324 
1325 	if (__get_user(flags, &vq->avail->flags)) {
1326 		vq_err(vq, "Failed to get flags");
1327 		return;
1328 	}
1329 
1330 	/* If they don't want an interrupt, don't signal, unless empty. */
1331 	if ((flags & VRING_AVAIL_F_NO_INTERRUPT) &&
1332 	    (vq->avail_idx != vq->last_avail_idx ||
1333 	     !vhost_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY)))
1334 		return;
1335 
1336 	/* Signal the Guest tell them we used something up. */
1337 	if (vq->call_ctx)
1338 		eventfd_signal(vq->call_ctx, 1);
1339 }
1340 
1341 /* And here's the combo meal deal.  Supersize me! */
1342 void vhost_add_used_and_signal(struct vhost_dev *dev,
1343 			       struct vhost_virtqueue *vq,
1344 			       unsigned int head, int len)
1345 {
1346 	vhost_add_used(vq, head, len);
1347 	vhost_signal(dev, vq);
1348 }
1349 
1350 /* multi-buffer version of vhost_add_used_and_signal */
1351 void vhost_add_used_and_signal_n(struct vhost_dev *dev,
1352 				 struct vhost_virtqueue *vq,
1353 				 struct vring_used_elem *heads, unsigned count)
1354 {
1355 	vhost_add_used_n(vq, heads, count);
1356 	vhost_signal(dev, vq);
1357 }
1358 
1359 /* OK, now we need to know about added descriptors. */
1360 bool vhost_enable_notify(struct vhost_virtqueue *vq)
1361 {
1362 	u16 avail_idx;
1363 	int r;
1364 	if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
1365 		return false;
1366 	vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
1367 	r = put_user(vq->used_flags, &vq->used->flags);
1368 	if (r) {
1369 		vq_err(vq, "Failed to enable notification at %p: %d\n",
1370 		       &vq->used->flags, r);
1371 		return false;
1372 	}
1373 	/* They could have slipped one in as we were doing that: make
1374 	 * sure it's written, then check again. */
1375 	smp_mb();
1376 	r = __get_user(avail_idx, &vq->avail->idx);
1377 	if (r) {
1378 		vq_err(vq, "Failed to check avail idx at %p: %d\n",
1379 		       &vq->avail->idx, r);
1380 		return false;
1381 	}
1382 
1383 	return avail_idx != vq->avail_idx;
1384 }
1385 
1386 /* We don't need to be notified again. */
1387 void vhost_disable_notify(struct vhost_virtqueue *vq)
1388 {
1389 	int r;
1390 	if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
1391 		return;
1392 	vq->used_flags |= VRING_USED_F_NO_NOTIFY;
1393 	r = put_user(vq->used_flags, &vq->used->flags);
1394 	if (r)
1395 		vq_err(vq, "Failed to enable notification at %p: %d\n",
1396 		       &vq->used->flags, r);
1397 }
1398