xref: /linux/drivers/vhost/vringh.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Helpers for the host side of a virtio ring.
4  *
5  * Since these may be in userspace, we use (inline) accessors.
6  */
7 #include <linux/compiler.h>
8 #include <linux/module.h>
9 #include <linux/vringh.h>
10 #include <linux/virtio_ring.h>
11 #include <linux/kernel.h>
12 #include <linux/ratelimit.h>
13 #include <linux/uaccess.h>
14 #include <linux/slab.h>
15 #include <linux/export.h>
16 #if IS_REACHABLE(CONFIG_VHOST_IOTLB)
17 #include <linux/bvec.h>
18 #include <linux/highmem.h>
19 #include <linux/vhost_iotlb.h>
20 #endif
21 #include <uapi/linux/virtio_config.h>
22 
23 static __printf(1,2) __cold void vringh_bad(const char *fmt, ...)
24 {
25 	static DEFINE_RATELIMIT_STATE(vringh_rs,
26 				      DEFAULT_RATELIMIT_INTERVAL,
27 				      DEFAULT_RATELIMIT_BURST);
28 	if (__ratelimit(&vringh_rs)) {
29 		va_list ap;
30 		va_start(ap, fmt);
31 		printk(KERN_NOTICE "vringh:");
32 		vprintk(fmt, ap);
33 		va_end(ap);
34 	}
35 }
36 
37 /* Returns vring->num if empty, -ve on error. */
38 static inline int __vringh_get_head(const struct vringh *vrh,
39 				    int (*getu16)(const struct vringh *vrh,
40 						  u16 *val, const __virtio16 *p),
41 				    u16 *last_avail_idx)
42 {
43 	u16 avail_idx, i, head;
44 	int err;
45 
46 	err = getu16(vrh, &avail_idx, &vrh->vring.avail->idx);
47 	if (err) {
48 		vringh_bad("Failed to access avail idx at %p",
49 			   &vrh->vring.avail->idx);
50 		return err;
51 	}
52 
53 	if (*last_avail_idx == avail_idx)
54 		return vrh->vring.num;
55 
56 	/* Only get avail ring entries after they have been exposed by guest. */
57 	virtio_rmb(vrh->weak_barriers);
58 
59 	i = *last_avail_idx & (vrh->vring.num - 1);
60 
61 	err = getu16(vrh, &head, &vrh->vring.avail->ring[i]);
62 	if (err) {
63 		vringh_bad("Failed to read head: idx %d address %p",
64 			   *last_avail_idx, &vrh->vring.avail->ring[i]);
65 		return err;
66 	}
67 
68 	if (head >= vrh->vring.num) {
69 		vringh_bad("Guest says index %u > %u is available",
70 			   head, vrh->vring.num);
71 		return -EINVAL;
72 	}
73 
74 	(*last_avail_idx)++;
75 	return head;
76 }
77 
78 /**
79  * vringh_kiov_advance - skip bytes from vring_kiov
80  * @iov: an iov passed to vringh_getdesc_*() (updated as we consume)
81  * @len: the maximum length to advance
82  */
83 void vringh_kiov_advance(struct vringh_kiov *iov, size_t len)
84 {
85 	while (len && iov->i < iov->used) {
86 		size_t partlen = min(iov->iov[iov->i].iov_len, len);
87 
88 		iov->consumed += partlen;
89 		iov->iov[iov->i].iov_len -= partlen;
90 		iov->iov[iov->i].iov_base += partlen;
91 
92 		if (!iov->iov[iov->i].iov_len) {
93 			/* Fix up old iov element then increment. */
94 			iov->iov[iov->i].iov_len = iov->consumed;
95 			iov->iov[iov->i].iov_base -= iov->consumed;
96 
97 			iov->consumed = 0;
98 			iov->i++;
99 		}
100 
101 		len -= partlen;
102 	}
103 }
104 EXPORT_SYMBOL(vringh_kiov_advance);
105 
106 /* Copy some bytes to/from the iovec.  Returns num copied. */
107 static inline ssize_t vringh_iov_xfer(struct vringh *vrh,
108 				      struct vringh_kiov *iov,
109 				      void *ptr, size_t len,
110 				      int (*xfer)(const struct vringh *vrh,
111 						  void *addr, void *ptr,
112 						  size_t len))
113 {
114 	int err, done = 0;
115 
116 	while (len && iov->i < iov->used) {
117 		size_t partlen;
118 
119 		partlen = min(iov->iov[iov->i].iov_len, len);
120 		err = xfer(vrh, iov->iov[iov->i].iov_base, ptr, partlen);
121 		if (err)
122 			return err;
123 		done += partlen;
124 		len -= partlen;
125 		ptr += partlen;
126 		iov->consumed += partlen;
127 		iov->iov[iov->i].iov_len -= partlen;
128 		iov->iov[iov->i].iov_base += partlen;
129 
130 		if (!iov->iov[iov->i].iov_len) {
131 			/* Fix up old iov element then increment. */
132 			iov->iov[iov->i].iov_len = iov->consumed;
133 			iov->iov[iov->i].iov_base -= iov->consumed;
134 
135 			iov->consumed = 0;
136 			iov->i++;
137 		}
138 	}
139 	return done;
140 }
141 
142 /* May reduce *len if range is shorter. */
143 static inline bool range_check(struct vringh *vrh, u64 addr, size_t *len,
144 			       struct vringh_range *range,
145 			       bool (*getrange)(struct vringh *,
146 						u64, struct vringh_range *))
147 {
148 	if (addr < range->start || addr > range->end_incl) {
149 		if (!getrange(vrh, addr, range))
150 			return false;
151 	}
152 	BUG_ON(addr < range->start || addr > range->end_incl);
153 
154 	/* To end of memory? */
155 	if (unlikely(addr + *len == 0)) {
156 		if (range->end_incl == -1ULL)
157 			return true;
158 		goto truncate;
159 	}
160 
161 	/* Otherwise, don't wrap. */
162 	if (addr + *len < addr) {
163 		vringh_bad("Wrapping descriptor %zu@0x%llx",
164 			   *len, (unsigned long long)addr);
165 		return false;
166 	}
167 
168 	if (unlikely(addr + *len - 1 > range->end_incl))
169 		goto truncate;
170 	return true;
171 
172 truncate:
173 	*len = range->end_incl + 1 - addr;
174 	return true;
175 }
176 
177 static inline bool no_range_check(struct vringh *vrh, u64 addr, size_t *len,
178 				  struct vringh_range *range,
179 				  bool (*getrange)(struct vringh *,
180 						   u64, struct vringh_range *))
181 {
182 	return true;
183 }
184 
185 /* No reason for this code to be inline. */
186 static int move_to_indirect(const struct vringh *vrh,
187 			    int *up_next, u16 *i, void *addr,
188 			    const struct vring_desc *desc,
189 			    struct vring_desc **descs, int *desc_max)
190 {
191 	u32 len;
192 
193 	/* Indirect tables can't have indirect. */
194 	if (*up_next != -1) {
195 		vringh_bad("Multilevel indirect %u->%u", *up_next, *i);
196 		return -EINVAL;
197 	}
198 
199 	len = vringh32_to_cpu(vrh, desc->len);
200 	if (unlikely(len < sizeof(struct vring_desc) ||
201 		     len % sizeof(struct vring_desc))) {
202 		vringh_bad("Invalid indirect len %u", desc->len);
203 		return -EINVAL;
204 	}
205 
206 	/* We will check this when we follow it! */
207 	if (desc->flags & cpu_to_vringh16(vrh, VRING_DESC_F_NEXT))
208 		*up_next = vringh16_to_cpu(vrh, desc->next);
209 	else
210 		*up_next = -2;
211 	*descs = addr;
212 	*desc_max = len / sizeof(struct vring_desc);
213 
214 	/* Now, start at the first indirect. */
215 	*i = 0;
216 	return 0;
217 }
218 
219 static int resize_iovec(struct vringh_kiov *iov, gfp_t gfp)
220 {
221 	struct kvec *new;
222 	unsigned int flag, new_num = (iov->max_num & ~VRINGH_IOV_ALLOCATED) * 2;
223 
224 	if (new_num < 8)
225 		new_num = 8;
226 
227 	flag = (iov->max_num & VRINGH_IOV_ALLOCATED);
228 	if (flag)
229 		new = krealloc_array(iov->iov, new_num, sizeof(*new), gfp);
230 	else {
231 		new = kmalloc_objs(*new, new_num, gfp);
232 		if (new) {
233 			memcpy(new, iov->iov,
234 			       iov->max_num * sizeof(struct iovec));
235 			flag = VRINGH_IOV_ALLOCATED;
236 		}
237 	}
238 	if (!new)
239 		return -ENOMEM;
240 	iov->iov = new;
241 	iov->max_num = (new_num | flag);
242 	return 0;
243 }
244 
245 static u16 __cold return_from_indirect(const struct vringh *vrh, int *up_next,
246 				       struct vring_desc **descs, int *desc_max)
247 {
248 	u16 i = *up_next;
249 
250 	*up_next = -1;
251 	*descs = vrh->vring.desc;
252 	*desc_max = vrh->vring.num;
253 	return i;
254 }
255 
256 static int slow_copy(struct vringh *vrh, void *dst, const void *src,
257 		     bool (*rcheck)(struct vringh *vrh, u64 addr, size_t *len,
258 				    struct vringh_range *range,
259 				    bool (*getrange)(struct vringh *vrh,
260 						     u64,
261 						     struct vringh_range *)),
262 		     bool (*getrange)(struct vringh *vrh,
263 				      u64 addr,
264 				      struct vringh_range *r),
265 		     struct vringh_range *range,
266 		     int (*copy)(const struct vringh *vrh,
267 				 void *dst, const void *src, size_t len))
268 {
269 	size_t part, len = sizeof(struct vring_desc);
270 
271 	do {
272 		u64 addr;
273 		int err;
274 
275 		part = len;
276 		addr = (u64)(unsigned long)src - range->offset;
277 
278 		if (!rcheck(vrh, addr, &part, range, getrange))
279 			return -EINVAL;
280 
281 		err = copy(vrh, dst, src, part);
282 		if (err)
283 			return err;
284 
285 		dst += part;
286 		src += part;
287 		len -= part;
288 	} while (len);
289 	return 0;
290 }
291 
292 static inline int
293 __vringh_iov(struct vringh *vrh, u16 i,
294 	     struct vringh_kiov *riov,
295 	     struct vringh_kiov *wiov,
296 	     bool (*rcheck)(struct vringh *vrh, u64 addr, size_t *len,
297 			    struct vringh_range *range,
298 			    bool (*getrange)(struct vringh *, u64,
299 					     struct vringh_range *)),
300 	     bool (*getrange)(struct vringh *, u64, struct vringh_range *),
301 	     gfp_t gfp,
302 	     int (*copy)(const struct vringh *vrh,
303 			 void *dst, const void *src, size_t len))
304 {
305 	int err, count = 0, indirect_count = 0, up_next, desc_max;
306 	struct vring_desc desc, *descs;
307 	struct vringh_range range = { -1ULL, 0 }, slowrange;
308 	bool slow = false;
309 
310 	/* We start traversing vring's descriptor table. */
311 	descs = vrh->vring.desc;
312 	desc_max = vrh->vring.num;
313 	up_next = -1;
314 
315 	/* You must want something! */
316 	if (WARN_ON(!riov && !wiov))
317 		return -EINVAL;
318 
319 	if (riov)
320 		riov->i = riov->used = riov->consumed = 0;
321 	if (wiov)
322 		wiov->i = wiov->used = wiov->consumed = 0;
323 
324 	for (;;) {
325 		void *addr;
326 		struct vringh_kiov *iov;
327 		size_t len;
328 
329 		if (unlikely(slow))
330 			err = slow_copy(vrh, &desc, &descs[i], rcheck, getrange,
331 					&slowrange, copy);
332 		else
333 			err = copy(vrh, &desc, &descs[i], sizeof(desc));
334 		if (unlikely(err))
335 			goto fail;
336 
337 		if (up_next == -1)
338 			count++;
339 		else
340 			indirect_count++;
341 
342 		if (count > vrh->vring.num || indirect_count > desc_max) {
343 			vringh_bad("Descriptor loop in %p", descs);
344 			err = -ELOOP;
345 			goto fail;
346 		}
347 
348 		if (unlikely(desc.flags &
349 			     cpu_to_vringh16(vrh, VRING_DESC_F_INDIRECT))) {
350 			u64 a = vringh64_to_cpu(vrh, desc.addr);
351 
352 			/* Make sure it's OK, and get offset. */
353 			len = vringh32_to_cpu(vrh, desc.len);
354 			if (!rcheck(vrh, a, &len, &range, getrange)) {
355 				err = -EINVAL;
356 				goto fail;
357 			}
358 
359 			if (unlikely(len != vringh32_to_cpu(vrh, desc.len))) {
360 				slow = true;
361 				/* We need to save this range to use offset */
362 				slowrange = range;
363 			}
364 
365 			addr = (void *)(long)(a + range.offset);
366 			err = move_to_indirect(vrh, &up_next, &i, addr, &desc,
367 					       &descs, &desc_max);
368 			if (err)
369 				goto fail;
370 			continue;
371 		}
372 
373 		if (desc.flags & cpu_to_vringh16(vrh, VRING_DESC_F_WRITE))
374 			iov = wiov;
375 		else {
376 			iov = riov;
377 			if (unlikely(wiov && wiov->used)) {
378 				vringh_bad("Readable desc %p after writable",
379 					   &descs[i]);
380 				err = -EINVAL;
381 				goto fail;
382 			}
383 		}
384 
385 		if (!iov) {
386 			vringh_bad("Unexpected %s desc",
387 				   !wiov ? "writable" : "readable");
388 			err = -EPROTO;
389 			goto fail;
390 		}
391 
392 	again:
393 		/* Make sure it's OK, and get offset. */
394 		len = vringh32_to_cpu(vrh, desc.len);
395 		if (!rcheck(vrh, vringh64_to_cpu(vrh, desc.addr), &len, &range,
396 			    getrange)) {
397 			err = -EINVAL;
398 			goto fail;
399 		}
400 		addr = (void *)(unsigned long)(vringh64_to_cpu(vrh, desc.addr) +
401 					       range.offset);
402 
403 		if (unlikely(iov->used == (iov->max_num & ~VRINGH_IOV_ALLOCATED))) {
404 			err = resize_iovec(iov, gfp);
405 			if (err)
406 				goto fail;
407 		}
408 
409 		iov->iov[iov->used].iov_base = addr;
410 		iov->iov[iov->used].iov_len = len;
411 		iov->used++;
412 
413 		if (unlikely(len != vringh32_to_cpu(vrh, desc.len))) {
414 			desc.len = cpu_to_vringh32(vrh,
415 				   vringh32_to_cpu(vrh, desc.len) - len);
416 			desc.addr = cpu_to_vringh64(vrh,
417 				    vringh64_to_cpu(vrh, desc.addr) + len);
418 			goto again;
419 		}
420 
421 		if (desc.flags & cpu_to_vringh16(vrh, VRING_DESC_F_NEXT)) {
422 			i = vringh16_to_cpu(vrh, desc.next);
423 		} else {
424 			/* Just in case we need to finish traversing above. */
425 			if (unlikely(up_next > 0)) {
426 				i = return_from_indirect(vrh, &up_next,
427 							 &descs, &desc_max);
428 				slow = false;
429 				indirect_count = 0;
430 			} else
431 				break;
432 		}
433 
434 		if (i >= desc_max) {
435 			vringh_bad("Chained index %u > %u", i, desc_max);
436 			err = -EINVAL;
437 			goto fail;
438 		}
439 	}
440 
441 	return 0;
442 
443 fail:
444 	return err;
445 }
446 
447 static inline int __vringh_complete(struct vringh *vrh,
448 				    const struct vring_used_elem *used,
449 				    unsigned int num_used,
450 				    int (*putu16)(const struct vringh *vrh,
451 						  __virtio16 *p, u16 val),
452 				    int (*putused)(const struct vringh *vrh,
453 						   struct vring_used_elem *dst,
454 						   const struct vring_used_elem
455 						   *src, unsigned num))
456 {
457 	struct vring_used *used_ring;
458 	int err;
459 	u16 used_idx, off;
460 
461 	used_ring = vrh->vring.used;
462 	used_idx = vrh->last_used_idx + vrh->completed;
463 
464 	off = used_idx % vrh->vring.num;
465 
466 	/* Compiler knows num_used == 1 sometimes, hence extra check */
467 	if (num_used > 1 && unlikely(off + num_used >= vrh->vring.num)) {
468 		u16 part = vrh->vring.num - off;
469 		err = putused(vrh, &used_ring->ring[off], used, part);
470 		if (!err)
471 			err = putused(vrh, &used_ring->ring[0], used + part,
472 				      num_used - part);
473 	} else
474 		err = putused(vrh, &used_ring->ring[off], used, num_used);
475 
476 	if (err) {
477 		vringh_bad("Failed to write %u used entries %u at %p",
478 			   num_used, off, &used_ring->ring[off]);
479 		return err;
480 	}
481 
482 	/* Make sure buffer is written before we update index. */
483 	virtio_wmb(vrh->weak_barriers);
484 
485 	err = putu16(vrh, &vrh->vring.used->idx, used_idx + num_used);
486 	if (err) {
487 		vringh_bad("Failed to update used index at %p",
488 			   &vrh->vring.used->idx);
489 		return err;
490 	}
491 
492 	vrh->completed += num_used;
493 	return 0;
494 }
495 
496 
497 static inline int __vringh_need_notify(struct vringh *vrh,
498 				       int (*getu16)(const struct vringh *vrh,
499 						     u16 *val,
500 						     const __virtio16 *p))
501 {
502 	bool notify;
503 	u16 used_event;
504 	int err;
505 
506 	/* Flush out used index update. This is paired with the
507 	 * barrier that the Guest executes when enabling
508 	 * interrupts. */
509 	virtio_mb(vrh->weak_barriers);
510 
511 	/* Old-style, without event indices. */
512 	if (!vrh->event_indices) {
513 		u16 flags;
514 		err = getu16(vrh, &flags, &vrh->vring.avail->flags);
515 		if (err) {
516 			vringh_bad("Failed to get flags at %p",
517 				   &vrh->vring.avail->flags);
518 			return err;
519 		}
520 		return (!(flags & VRING_AVAIL_F_NO_INTERRUPT));
521 	}
522 
523 	/* Modern: we know when other side wants to know. */
524 	err = getu16(vrh, &used_event, &vring_used_event(&vrh->vring));
525 	if (err) {
526 		vringh_bad("Failed to get used event idx at %p",
527 			   &vring_used_event(&vrh->vring));
528 		return err;
529 	}
530 
531 	/* Just in case we added so many that we wrap. */
532 	if (unlikely(vrh->completed > 0xffff))
533 		notify = true;
534 	else
535 		notify = vring_need_event(used_event,
536 					  vrh->last_used_idx + vrh->completed,
537 					  vrh->last_used_idx);
538 
539 	vrh->last_used_idx += vrh->completed;
540 	vrh->completed = 0;
541 	return notify;
542 }
543 
544 static inline bool __vringh_notify_enable(struct vringh *vrh,
545 					  int (*getu16)(const struct vringh *vrh,
546 							u16 *val, const __virtio16 *p),
547 					  int (*putu16)(const struct vringh *vrh,
548 							__virtio16 *p, u16 val))
549 {
550 	u16 avail;
551 
552 	if (!vrh->event_indices) {
553 		/* Old-school; update flags. */
554 		if (putu16(vrh, &vrh->vring.used->flags, 0) != 0) {
555 			vringh_bad("Clearing used flags %p",
556 				   &vrh->vring.used->flags);
557 			return true;
558 		}
559 	} else {
560 		if (putu16(vrh, &vring_avail_event(&vrh->vring),
561 			   vrh->last_avail_idx) != 0) {
562 			vringh_bad("Updating avail event index %p",
563 				   &vring_avail_event(&vrh->vring));
564 			return true;
565 		}
566 	}
567 
568 	/* They could have slipped one in as we were doing that: make
569 	 * sure it's written, then check again. */
570 	virtio_mb(vrh->weak_barriers);
571 
572 	if (getu16(vrh, &avail, &vrh->vring.avail->idx) != 0) {
573 		vringh_bad("Failed to check avail idx at %p",
574 			   &vrh->vring.avail->idx);
575 		return true;
576 	}
577 
578 	/* This is unlikely, so we just leave notifications enabled
579 	 * (if we're using event_indices, we'll only get one
580 	 * notification anyway). */
581 	return avail == vrh->last_avail_idx;
582 }
583 
584 static inline void __vringh_notify_disable(struct vringh *vrh,
585 					   int (*putu16)(const struct vringh *vrh,
586 							 __virtio16 *p, u16 val))
587 {
588 	if (!vrh->event_indices) {
589 		/* Old-school; update flags. */
590 		if (putu16(vrh, &vrh->vring.used->flags,
591 			   VRING_USED_F_NO_NOTIFY)) {
592 			vringh_bad("Setting used flags %p",
593 				   &vrh->vring.used->flags);
594 		}
595 	}
596 }
597 
598 /* Userspace access helpers: in this case, addresses are really userspace. */
599 static inline int getu16_user(const struct vringh *vrh, u16 *val, const __virtio16 *p)
600 {
601 	__virtio16 v = 0;
602 	int rc = get_user(v, (__force __virtio16 __user *)p);
603 	*val = vringh16_to_cpu(vrh, v);
604 	return rc;
605 }
606 
607 static inline int putu16_user(const struct vringh *vrh, __virtio16 *p, u16 val)
608 {
609 	__virtio16 v = cpu_to_vringh16(vrh, val);
610 	return put_user(v, (__force __virtio16 __user *)p);
611 }
612 
613 static inline int copydesc_user(const struct vringh *vrh,
614 				void *dst, const void *src, size_t len)
615 {
616 	return copy_from_user(dst, (__force void __user *)src, len) ?
617 		-EFAULT : 0;
618 }
619 
620 static inline int putused_user(const struct vringh *vrh,
621 			       struct vring_used_elem *dst,
622 			       const struct vring_used_elem *src,
623 			       unsigned int num)
624 {
625 	return copy_to_user((__force void __user *)dst, src,
626 			    sizeof(*dst) * num) ? -EFAULT : 0;
627 }
628 
629 static inline int xfer_from_user(const struct vringh *vrh, void *src,
630 				 void *dst, size_t len)
631 {
632 	return copy_from_user(dst, (__force void __user *)src, len) ?
633 		-EFAULT : 0;
634 }
635 
636 static inline int xfer_to_user(const struct vringh *vrh,
637 			       void *dst, void *src, size_t len)
638 {
639 	return copy_to_user((__force void __user *)dst, src, len) ?
640 		-EFAULT : 0;
641 }
642 
643 /**
644  * vringh_init_user - initialize a vringh for a userspace vring.
645  * @vrh: the vringh to initialize.
646  * @features: the feature bits for this ring.
647  * @num: the number of elements.
648  * @weak_barriers: true if we only need memory barriers, not I/O.
649  * @desc: the userspace descriptor pointer.
650  * @avail: the userspace avail pointer.
651  * @used: the userspace used pointer.
652  *
653  * Returns an error if num is invalid: you should check pointers
654  * yourself!
655  */
656 int vringh_init_user(struct vringh *vrh, u64 features,
657 		     unsigned int num, bool weak_barriers,
658 		     vring_desc_t __user *desc,
659 		     vring_avail_t __user *avail,
660 		     vring_used_t __user *used)
661 {
662 	/* Sane power of 2 please! */
663 	if (!num || num > 0xffff || (num & (num - 1))) {
664 		vringh_bad("Bad ring size %u", num);
665 		return -EINVAL;
666 	}
667 
668 	vrh->little_endian = (features & (1ULL << VIRTIO_F_VERSION_1));
669 	vrh->event_indices = (features & (1 << VIRTIO_RING_F_EVENT_IDX));
670 	vrh->weak_barriers = weak_barriers;
671 	vrh->completed = 0;
672 	vrh->last_avail_idx = 0;
673 	vrh->last_used_idx = 0;
674 	vrh->vring.num = num;
675 	/* vring expects kernel addresses, but only used via accessors. */
676 	vrh->vring.desc = (__force struct vring_desc *)desc;
677 	vrh->vring.avail = (__force struct vring_avail *)avail;
678 	vrh->vring.used = (__force struct vring_used *)used;
679 	return 0;
680 }
681 EXPORT_SYMBOL(vringh_init_user);
682 
683 /**
684  * vringh_getdesc_user - get next available descriptor from userspace ring.
685  * @vrh: the userspace vring.
686  * @riov: where to put the readable descriptors (or NULL)
687  * @wiov: where to put the writable descriptors (or NULL)
688  * @getrange: function to call to check ranges.
689  * @head: head index we received, for passing to vringh_complete_user().
690  *
691  * Returns 0 if there was no descriptor, 1 if there was, or -errno.
692  *
693  * Note that on error return, you can tell the difference between an
694  * invalid ring and a single invalid descriptor: in the former case,
695  * *head will be vrh->vring.num.  You may be able to ignore an invalid
696  * descriptor, but there's not much you can do with an invalid ring.
697  *
698  * Note that you can reuse riov and wiov with subsequent calls. Content is
699  * overwritten and memory reallocated if more space is needed.
700  * When you don't have to use riov and wiov anymore, you should clean up them
701  * calling vringh_iov_cleanup() to release the memory, even on error!
702  */
703 int vringh_getdesc_user(struct vringh *vrh,
704 			struct vringh_iov *riov,
705 			struct vringh_iov *wiov,
706 			bool (*getrange)(struct vringh *vrh,
707 					 u64 addr, struct vringh_range *r),
708 			u16 *head)
709 {
710 	int err;
711 
712 	*head = vrh->vring.num;
713 	err = __vringh_get_head(vrh, getu16_user, &vrh->last_avail_idx);
714 	if (err < 0)
715 		return err;
716 
717 	/* Empty... */
718 	if (err == vrh->vring.num)
719 		return 0;
720 
721 	/* We need the layouts to be the identical for this to work */
722 	BUILD_BUG_ON(sizeof(struct vringh_kiov) != sizeof(struct vringh_iov));
723 	BUILD_BUG_ON(offsetof(struct vringh_kiov, iov) !=
724 		     offsetof(struct vringh_iov, iov));
725 	BUILD_BUG_ON(offsetof(struct vringh_kiov, i) !=
726 		     offsetof(struct vringh_iov, i));
727 	BUILD_BUG_ON(offsetof(struct vringh_kiov, used) !=
728 		     offsetof(struct vringh_iov, used));
729 	BUILD_BUG_ON(offsetof(struct vringh_kiov, max_num) !=
730 		     offsetof(struct vringh_iov, max_num));
731 	BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
732 	BUILD_BUG_ON(offsetof(struct iovec, iov_base) !=
733 		     offsetof(struct kvec, iov_base));
734 	BUILD_BUG_ON(offsetof(struct iovec, iov_len) !=
735 		     offsetof(struct kvec, iov_len));
736 	BUILD_BUG_ON(sizeof(((struct iovec *)NULL)->iov_base)
737 		     != sizeof(((struct kvec *)NULL)->iov_base));
738 	BUILD_BUG_ON(sizeof(((struct iovec *)NULL)->iov_len)
739 		     != sizeof(((struct kvec *)NULL)->iov_len));
740 
741 	*head = err;
742 	err = __vringh_iov(vrh, *head, (struct vringh_kiov *)riov,
743 			   (struct vringh_kiov *)wiov,
744 			   range_check, getrange, GFP_KERNEL, copydesc_user);
745 	if (err)
746 		return err;
747 
748 	return 1;
749 }
750 EXPORT_SYMBOL(vringh_getdesc_user);
751 
752 /**
753  * vringh_iov_pull_user - copy bytes from vring_iov.
754  * @riov: the riov as passed to vringh_getdesc_user() (updated as we consume)
755  * @dst: the place to copy.
756  * @len: the maximum length to copy.
757  *
758  * Returns the bytes copied <= len or a negative errno.
759  */
760 ssize_t vringh_iov_pull_user(struct vringh_iov *riov, void *dst, size_t len)
761 {
762 	return vringh_iov_xfer(NULL, (struct vringh_kiov *)riov,
763 			       dst, len, xfer_from_user);
764 }
765 EXPORT_SYMBOL(vringh_iov_pull_user);
766 
767 /**
768  * vringh_iov_push_user - copy bytes into vring_iov.
769  * @wiov: the wiov as passed to vringh_getdesc_user() (updated as we consume)
770  * @src: the place to copy from.
771  * @len: the maximum length to copy.
772  *
773  * Returns the bytes copied <= len or a negative errno.
774  */
775 ssize_t vringh_iov_push_user(struct vringh_iov *wiov,
776 			     const void *src, size_t len)
777 {
778 	return vringh_iov_xfer(NULL, (struct vringh_kiov *)wiov,
779 			       (void *)src, len, xfer_to_user);
780 }
781 EXPORT_SYMBOL(vringh_iov_push_user);
782 
783 /**
784  * vringh_complete_user - we've finished with descriptor, publish it.
785  * @vrh: the vring.
786  * @head: the head as filled in by vringh_getdesc_user.
787  * @len: the length of data we have written.
788  *
789  * You should check vringh_need_notify_user() after one or more calls
790  * to this function.
791  */
792 int vringh_complete_user(struct vringh *vrh, u16 head, u32 len)
793 {
794 	struct vring_used_elem used;
795 
796 	used.id = cpu_to_vringh32(vrh, head);
797 	used.len = cpu_to_vringh32(vrh, len);
798 	return __vringh_complete(vrh, &used, 1, putu16_user, putused_user);
799 }
800 EXPORT_SYMBOL(vringh_complete_user);
801 
802 /**
803  * vringh_complete_multi_user - we've finished with many descriptors.
804  * @vrh: the vring.
805  * @used: the head, length pairs.
806  * @num_used: the number of used elements.
807  *
808  * You should check vringh_need_notify_user() after one or more calls
809  * to this function.
810  */
811 int vringh_complete_multi_user(struct vringh *vrh,
812 			       const struct vring_used_elem used[],
813 			       unsigned num_used)
814 {
815 	return __vringh_complete(vrh, used, num_used,
816 				 putu16_user, putused_user);
817 }
818 EXPORT_SYMBOL(vringh_complete_multi_user);
819 
820 /**
821  * vringh_notify_enable_user - we want to know if something changes.
822  * @vrh: the vring.
823  *
824  * This always enables notifications, but returns false if there are
825  * now more buffers available in the vring.
826  */
827 bool vringh_notify_enable_user(struct vringh *vrh)
828 {
829 	return __vringh_notify_enable(vrh, getu16_user, putu16_user);
830 }
831 EXPORT_SYMBOL(vringh_notify_enable_user);
832 
833 /**
834  * vringh_notify_disable_user - don't tell us if something changes.
835  * @vrh: the vring.
836  *
837  * This is our normal running state: we disable and then only enable when
838  * we're going to sleep.
839  */
840 void vringh_notify_disable_user(struct vringh *vrh)
841 {
842 	__vringh_notify_disable(vrh, putu16_user);
843 }
844 EXPORT_SYMBOL(vringh_notify_disable_user);
845 
846 /**
847  * vringh_need_notify_user - must we tell the other side about used buffers?
848  * @vrh: the vring we've called vringh_complete_user() on.
849  *
850  * Returns -errno or 0 if we don't need to tell the other side, 1 if we do.
851  */
852 int vringh_need_notify_user(struct vringh *vrh)
853 {
854 	return __vringh_need_notify(vrh, getu16_user);
855 }
856 EXPORT_SYMBOL(vringh_need_notify_user);
857 
858 /* Kernelspace access helpers. */
859 static inline int getu16_kern(const struct vringh *vrh,
860 			      u16 *val, const __virtio16 *p)
861 {
862 	*val = vringh16_to_cpu(vrh, READ_ONCE(*p));
863 	return 0;
864 }
865 
866 static inline int putu16_kern(const struct vringh *vrh, __virtio16 *p, u16 val)
867 {
868 	WRITE_ONCE(*p, cpu_to_vringh16(vrh, val));
869 	return 0;
870 }
871 
872 static inline int copydesc_kern(const struct vringh *vrh,
873 				void *dst, const void *src, size_t len)
874 {
875 	memcpy(dst, src, len);
876 	return 0;
877 }
878 
879 static inline int putused_kern(const struct vringh *vrh,
880 			       struct vring_used_elem *dst,
881 			       const struct vring_used_elem *src,
882 			       unsigned int num)
883 {
884 	memcpy(dst, src, num * sizeof(*dst));
885 	return 0;
886 }
887 
888 /**
889  * vringh_init_kern - initialize a vringh for a kernelspace vring.
890  * @vrh: the vringh to initialize.
891  * @features: the feature bits for this ring.
892  * @num: the number of elements.
893  * @weak_barriers: true if we only need memory barriers, not I/O.
894  * @desc: the userspace descriptor pointer.
895  * @avail: the userspace avail pointer.
896  * @used: the userspace used pointer.
897  *
898  * Returns an error if num is invalid.
899  */
900 int vringh_init_kern(struct vringh *vrh, u64 features,
901 		     unsigned int num, bool weak_barriers,
902 		     struct vring_desc *desc,
903 		     struct vring_avail *avail,
904 		     struct vring_used *used)
905 {
906 	/* Sane power of 2 please! */
907 	if (!num || num > 0xffff || (num & (num - 1))) {
908 		vringh_bad("Bad ring size %u", num);
909 		return -EINVAL;
910 	}
911 
912 	vrh->little_endian = (features & (1ULL << VIRTIO_F_VERSION_1));
913 	vrh->event_indices = (features & (1 << VIRTIO_RING_F_EVENT_IDX));
914 	vrh->weak_barriers = weak_barriers;
915 	vrh->completed = 0;
916 	vrh->last_avail_idx = 0;
917 	vrh->last_used_idx = 0;
918 	vrh->vring.num = num;
919 	vrh->vring.desc = desc;
920 	vrh->vring.avail = avail;
921 	vrh->vring.used = used;
922 	return 0;
923 }
924 EXPORT_SYMBOL(vringh_init_kern);
925 
926 /**
927  * vringh_getdesc_kern - get next available descriptor from kernelspace ring.
928  * @vrh: the kernelspace vring.
929  * @riov: where to put the readable descriptors (or NULL)
930  * @wiov: where to put the writable descriptors (or NULL)
931  * @head: head index we received, for passing to vringh_complete_kern().
932  * @gfp: flags for allocating larger riov/wiov.
933  *
934  * Returns 0 if there was no descriptor, 1 if there was, or -errno.
935  *
936  * Note that on error return, you can tell the difference between an
937  * invalid ring and a single invalid descriptor: in the former case,
938  * *head will be vrh->vring.num.  You may be able to ignore an invalid
939  * descriptor, but there's not much you can do with an invalid ring.
940  *
941  * Note that you can reuse riov and wiov with subsequent calls. Content is
942  * overwritten and memory reallocated if more space is needed.
943  * When you don't have to use riov and wiov anymore, you should clean up them
944  * calling vringh_kiov_cleanup() to release the memory, even on error!
945  */
946 int vringh_getdesc_kern(struct vringh *vrh,
947 			struct vringh_kiov *riov,
948 			struct vringh_kiov *wiov,
949 			u16 *head,
950 			gfp_t gfp)
951 {
952 	int err;
953 
954 	err = __vringh_get_head(vrh, getu16_kern, &vrh->last_avail_idx);
955 	if (err < 0)
956 		return err;
957 
958 	/* Empty... */
959 	if (err == vrh->vring.num)
960 		return 0;
961 
962 	*head = err;
963 	err = __vringh_iov(vrh, *head, riov, wiov, no_range_check, NULL,
964 			   gfp, copydesc_kern);
965 	if (err)
966 		return err;
967 
968 	return 1;
969 }
970 EXPORT_SYMBOL(vringh_getdesc_kern);
971 
972 /**
973  * vringh_complete_kern - we've finished with descriptor, publish it.
974  * @vrh: the vring.
975  * @head: the head as filled in by vringh_getdesc_kern.
976  * @len: the length of data we have written.
977  *
978  * You should check vringh_need_notify_kern() after one or more calls
979  * to this function.
980  */
981 int vringh_complete_kern(struct vringh *vrh, u16 head, u32 len)
982 {
983 	struct vring_used_elem used;
984 
985 	used.id = cpu_to_vringh32(vrh, head);
986 	used.len = cpu_to_vringh32(vrh, len);
987 
988 	return __vringh_complete(vrh, &used, 1, putu16_kern, putused_kern);
989 }
990 EXPORT_SYMBOL(vringh_complete_kern);
991 
992 /**
993  * vringh_notify_enable_kern - we want to know if something changes.
994  * @vrh: the vring.
995  *
996  * This always enables notifications, but returns false if there are
997  * now more buffers available in the vring.
998  */
999 bool vringh_notify_enable_kern(struct vringh *vrh)
1000 {
1001 	return __vringh_notify_enable(vrh, getu16_kern, putu16_kern);
1002 }
1003 EXPORT_SYMBOL(vringh_notify_enable_kern);
1004 
1005 /**
1006  * vringh_notify_disable_kern - don't tell us if something changes.
1007  * @vrh: the vring.
1008  *
1009  * This is our normal running state: we disable and then only enable when
1010  * we're going to sleep.
1011  */
1012 void vringh_notify_disable_kern(struct vringh *vrh)
1013 {
1014 	__vringh_notify_disable(vrh, putu16_kern);
1015 }
1016 EXPORT_SYMBOL(vringh_notify_disable_kern);
1017 
1018 /**
1019  * vringh_need_notify_kern - must we tell the other side about used buffers?
1020  * @vrh: the vring we've called vringh_complete_kern() on.
1021  *
1022  * Returns -errno or 0 if we don't need to tell the other side, 1 if we do.
1023  */
1024 int vringh_need_notify_kern(struct vringh *vrh)
1025 {
1026 	return __vringh_need_notify(vrh, getu16_kern);
1027 }
1028 EXPORT_SYMBOL(vringh_need_notify_kern);
1029 
1030 #if IS_REACHABLE(CONFIG_VHOST_IOTLB)
1031 
1032 struct iotlb_vec {
1033 	union {
1034 		struct iovec *iovec;
1035 		struct bio_vec *bvec;
1036 	} iov;
1037 	size_t count;
1038 };
1039 
1040 static int iotlb_translate(const struct vringh *vrh,
1041 			   u64 addr, u64 len, u64 *translated,
1042 			   struct iotlb_vec *ivec, u32 perm)
1043 {
1044 	struct vhost_iotlb_map *map;
1045 	struct vhost_iotlb *iotlb = vrh->iotlb;
1046 	int ret = 0;
1047 	u64 s = 0, last = addr + len - 1;
1048 
1049 	spin_lock(vrh->iotlb_lock);
1050 
1051 	while (len > s) {
1052 		uintptr_t io_addr;
1053 		size_t io_len;
1054 		u64 size;
1055 
1056 		if (unlikely(ret >= ivec->count)) {
1057 			ret = -ENOBUFS;
1058 			break;
1059 		}
1060 
1061 		map = vhost_iotlb_itree_first(iotlb, addr, last);
1062 		if (!map || map->start > addr) {
1063 			ret = -EINVAL;
1064 			break;
1065 		} else if (!(map->perm & perm)) {
1066 			ret = -EPERM;
1067 			break;
1068 		}
1069 
1070 		size = map->size - addr + map->start;
1071 		io_len = min(len - s, size);
1072 		io_addr = map->addr - map->start + addr;
1073 
1074 		if (vrh->use_va) {
1075 			struct iovec *iovec = ivec->iov.iovec;
1076 
1077 			iovec[ret].iov_len = io_len;
1078 			iovec[ret].iov_base = (void __user *)io_addr;
1079 		} else {
1080 			u64 pfn = io_addr >> PAGE_SHIFT;
1081 			struct bio_vec *bvec = ivec->iov.bvec;
1082 
1083 			bvec_set_page(&bvec[ret], pfn_to_page(pfn), io_len,
1084 				      io_addr & (PAGE_SIZE - 1));
1085 		}
1086 
1087 		s += size;
1088 		addr += size;
1089 		++ret;
1090 	}
1091 
1092 	spin_unlock(vrh->iotlb_lock);
1093 
1094 	if (translated)
1095 		*translated = min(len, s);
1096 
1097 	return ret;
1098 }
1099 
1100 #define IOTLB_IOV_STRIDE 16
1101 
1102 static inline int copy_from_iotlb(const struct vringh *vrh, void *dst,
1103 				  void *src, size_t len)
1104 {
1105 	struct iotlb_vec ivec;
1106 	union {
1107 		struct iovec iovec[IOTLB_IOV_STRIDE];
1108 		struct bio_vec bvec[IOTLB_IOV_STRIDE];
1109 	} iov;
1110 	u64 total_translated = 0;
1111 
1112 	ivec.iov.iovec = iov.iovec;
1113 	ivec.count = IOTLB_IOV_STRIDE;
1114 
1115 	while (total_translated < len) {
1116 		struct iov_iter iter;
1117 		u64 translated;
1118 		int ret;
1119 		size_t size;
1120 
1121 		ret = iotlb_translate(vrh, (u64)(uintptr_t)src,
1122 				      len - total_translated, &translated,
1123 				      &ivec, VHOST_MAP_RO);
1124 		if (ret == -ENOBUFS)
1125 			ret = IOTLB_IOV_STRIDE;
1126 		else if (ret < 0)
1127 			return ret;
1128 
1129 		if (vrh->use_va) {
1130 			iov_iter_init(&iter, ITER_SOURCE, ivec.iov.iovec, ret,
1131 				      translated);
1132 		} else {
1133 			iov_iter_bvec(&iter, ITER_SOURCE, ivec.iov.bvec, ret,
1134 				      translated);
1135 		}
1136 
1137 		size = copy_from_iter(dst, translated, &iter);
1138 		if (size != translated)
1139 			return -EFAULT;
1140 
1141 		src += translated;
1142 		dst += translated;
1143 		total_translated += translated;
1144 	}
1145 
1146 	return total_translated;
1147 }
1148 
1149 static inline int copy_to_iotlb(const struct vringh *vrh, void *dst,
1150 				void *src, size_t len)
1151 {
1152 	struct iotlb_vec ivec;
1153 	union {
1154 		struct iovec iovec[IOTLB_IOV_STRIDE];
1155 		struct bio_vec bvec[IOTLB_IOV_STRIDE];
1156 	} iov;
1157 	u64 total_translated = 0;
1158 
1159 	ivec.iov.iovec = iov.iovec;
1160 	ivec.count = IOTLB_IOV_STRIDE;
1161 
1162 	while (total_translated < len) {
1163 		struct iov_iter iter;
1164 		u64 translated;
1165 		int ret;
1166 		size_t size;
1167 
1168 		ret = iotlb_translate(vrh, (u64)(uintptr_t)dst,
1169 				      len - total_translated, &translated,
1170 				      &ivec, VHOST_MAP_WO);
1171 		if (ret == -ENOBUFS)
1172 			ret = IOTLB_IOV_STRIDE;
1173 		else if (ret < 0)
1174 			return ret;
1175 
1176 		if (vrh->use_va) {
1177 			iov_iter_init(&iter, ITER_DEST, ivec.iov.iovec, ret,
1178 				      translated);
1179 		} else {
1180 			iov_iter_bvec(&iter, ITER_DEST, ivec.iov.bvec, ret,
1181 				      translated);
1182 		}
1183 
1184 		size = copy_to_iter(src, translated, &iter);
1185 		if (size != translated)
1186 			return -EFAULT;
1187 
1188 		src += translated;
1189 		dst += translated;
1190 		total_translated += translated;
1191 	}
1192 
1193 	return total_translated;
1194 }
1195 
1196 static inline int getu16_iotlb(const struct vringh *vrh,
1197 			       u16 *val, const __virtio16 *p)
1198 {
1199 	struct iotlb_vec ivec;
1200 	union {
1201 		struct iovec iovec[1];
1202 		struct bio_vec bvec[1];
1203 	} iov;
1204 	__virtio16 tmp;
1205 	int ret;
1206 
1207 	ivec.iov.iovec = iov.iovec;
1208 	ivec.count = 1;
1209 
1210 	/* Atomic read is needed for getu16 */
1211 	ret = iotlb_translate(vrh, (u64)(uintptr_t)p, sizeof(*p),
1212 			      NULL, &ivec, VHOST_MAP_RO);
1213 	if (ret < 0)
1214 		return ret;
1215 
1216 	if (vrh->use_va) {
1217 		ret = get_user(tmp, (__virtio16 __user *)ivec.iov.iovec[0].iov_base);
1218 		if (ret)
1219 			return ret;
1220 	} else {
1221 		__virtio16 *from = bvec_kmap_local(&ivec.iov.bvec[0]);
1222 
1223 		tmp = READ_ONCE(*from);
1224 		kunmap_local(from);
1225 	}
1226 
1227 	*val = vringh16_to_cpu(vrh, tmp);
1228 
1229 	return 0;
1230 }
1231 
1232 static inline int putu16_iotlb(const struct vringh *vrh,
1233 			       __virtio16 *p, u16 val)
1234 {
1235 	struct iotlb_vec ivec;
1236 	union {
1237 		struct iovec iovec;
1238 		struct bio_vec bvec;
1239 	} iov;
1240 	__virtio16 tmp;
1241 	int ret;
1242 
1243 	ivec.iov.iovec = &iov.iovec;
1244 	ivec.count = 1;
1245 
1246 	/* Atomic write is needed for putu16 */
1247 	ret = iotlb_translate(vrh, (u64)(uintptr_t)p, sizeof(*p),
1248 			      NULL, &ivec, VHOST_MAP_WO);
1249 	if (ret < 0)
1250 		return ret;
1251 
1252 	tmp = cpu_to_vringh16(vrh, val);
1253 
1254 	if (vrh->use_va) {
1255 		ret = put_user(tmp, (__virtio16 __user *)ivec.iov.iovec[0].iov_base);
1256 		if (ret)
1257 			return ret;
1258 	} else {
1259 		__virtio16 *to = bvec_kmap_local(&ivec.iov.bvec[0]);
1260 
1261 		WRITE_ONCE(*to, tmp);
1262 		kunmap_local(to);
1263 	}
1264 
1265 	return 0;
1266 }
1267 
1268 static inline int copydesc_iotlb(const struct vringh *vrh,
1269 				 void *dst, const void *src, size_t len)
1270 {
1271 	int ret;
1272 
1273 	ret = copy_from_iotlb(vrh, dst, (void *)src, len);
1274 	if (ret != len)
1275 		return -EFAULT;
1276 
1277 	return 0;
1278 }
1279 
1280 static inline int xfer_from_iotlb(const struct vringh *vrh, void *src,
1281 				  void *dst, size_t len)
1282 {
1283 	int ret;
1284 
1285 	ret = copy_from_iotlb(vrh, dst, src, len);
1286 	if (ret != len)
1287 		return -EFAULT;
1288 
1289 	return 0;
1290 }
1291 
1292 static inline int xfer_to_iotlb(const struct vringh *vrh,
1293 			       void *dst, void *src, size_t len)
1294 {
1295 	int ret;
1296 
1297 	ret = copy_to_iotlb(vrh, dst, src, len);
1298 	if (ret != len)
1299 		return -EFAULT;
1300 
1301 	return 0;
1302 }
1303 
1304 static inline int putused_iotlb(const struct vringh *vrh,
1305 				struct vring_used_elem *dst,
1306 				const struct vring_used_elem *src,
1307 				unsigned int num)
1308 {
1309 	int size = num * sizeof(*dst);
1310 	int ret;
1311 
1312 	ret = copy_to_iotlb(vrh, dst, (void *)src, num * sizeof(*dst));
1313 	if (ret != size)
1314 		return -EFAULT;
1315 
1316 	return 0;
1317 }
1318 
1319 /**
1320  * vringh_init_iotlb - initialize a vringh for a ring with IOTLB.
1321  * @vrh: the vringh to initialize.
1322  * @features: the feature bits for this ring.
1323  * @num: the number of elements.
1324  * @weak_barriers: true if we only need memory barriers, not I/O.
1325  * @desc: the userspace descriptor pointer.
1326  * @avail: the userspace avail pointer.
1327  * @used: the userspace used pointer.
1328  *
1329  * Returns an error if num is invalid.
1330  */
1331 int vringh_init_iotlb(struct vringh *vrh, u64 features,
1332 		      unsigned int num, bool weak_barriers,
1333 		      struct vring_desc *desc,
1334 		      struct vring_avail *avail,
1335 		      struct vring_used *used)
1336 {
1337 	vrh->use_va = false;
1338 
1339 	return vringh_init_kern(vrh, features, num, weak_barriers,
1340 				desc, avail, used);
1341 }
1342 EXPORT_SYMBOL(vringh_init_iotlb);
1343 
1344 /**
1345  * vringh_init_iotlb_va - initialize a vringh for a ring with IOTLB containing
1346  *                        user VA.
1347  * @vrh: the vringh to initialize.
1348  * @features: the feature bits for this ring.
1349  * @num: the number of elements.
1350  * @weak_barriers: true if we only need memory barriers, not I/O.
1351  * @desc: the userspace descriptor pointer.
1352  * @avail: the userspace avail pointer.
1353  * @used: the userspace used pointer.
1354  *
1355  * Returns an error if num is invalid.
1356  */
1357 int vringh_init_iotlb_va(struct vringh *vrh, u64 features,
1358 			 unsigned int num, bool weak_barriers,
1359 			 struct vring_desc *desc,
1360 			 struct vring_avail *avail,
1361 			 struct vring_used *used)
1362 {
1363 	vrh->use_va = true;
1364 
1365 	return vringh_init_kern(vrh, features, num, weak_barriers,
1366 				desc, avail, used);
1367 }
1368 EXPORT_SYMBOL(vringh_init_iotlb_va);
1369 
1370 /**
1371  * vringh_set_iotlb - initialize a vringh for a ring with IOTLB.
1372  * @vrh: the vring
1373  * @iotlb: iotlb associated with this vring
1374  * @iotlb_lock: spinlock to synchronize the iotlb accesses
1375  */
1376 void vringh_set_iotlb(struct vringh *vrh, struct vhost_iotlb *iotlb,
1377 		      spinlock_t *iotlb_lock)
1378 {
1379 	vrh->iotlb = iotlb;
1380 	vrh->iotlb_lock = iotlb_lock;
1381 }
1382 EXPORT_SYMBOL(vringh_set_iotlb);
1383 
1384 /**
1385  * vringh_getdesc_iotlb - get next available descriptor from ring with
1386  * IOTLB.
1387  * @vrh: the kernelspace vring.
1388  * @riov: where to put the readable descriptors (or NULL)
1389  * @wiov: where to put the writable descriptors (or NULL)
1390  * @head: head index we received, for passing to vringh_complete_iotlb().
1391  * @gfp: flags for allocating larger riov/wiov.
1392  *
1393  * Returns 0 if there was no descriptor, 1 if there was, or -errno.
1394  *
1395  * Note that on error return, you can tell the difference between an
1396  * invalid ring and a single invalid descriptor: in the former case,
1397  * *head will be vrh->vring.num.  You may be able to ignore an invalid
1398  * descriptor, but there's not much you can do with an invalid ring.
1399  *
1400  * Note that you can reuse riov and wiov with subsequent calls. Content is
1401  * overwritten and memory reallocated if more space is needed.
1402  * When you don't have to use riov and wiov anymore, you should clean up them
1403  * calling vringh_kiov_cleanup() to release the memory, even on error!
1404  */
1405 int vringh_getdesc_iotlb(struct vringh *vrh,
1406 			 struct vringh_kiov *riov,
1407 			 struct vringh_kiov *wiov,
1408 			 u16 *head,
1409 			 gfp_t gfp)
1410 {
1411 	int err;
1412 
1413 	err = __vringh_get_head(vrh, getu16_iotlb, &vrh->last_avail_idx);
1414 	if (err < 0)
1415 		return err;
1416 
1417 	/* Empty... */
1418 	if (err == vrh->vring.num)
1419 		return 0;
1420 
1421 	*head = err;
1422 	err = __vringh_iov(vrh, *head, riov, wiov, no_range_check, NULL,
1423 			   gfp, copydesc_iotlb);
1424 	if (err)
1425 		return err;
1426 
1427 	return 1;
1428 }
1429 EXPORT_SYMBOL(vringh_getdesc_iotlb);
1430 
1431 /**
1432  * vringh_iov_pull_iotlb - copy bytes from vring_iov.
1433  * @vrh: the vring.
1434  * @riov: the riov as passed to vringh_getdesc_iotlb() (updated as we consume)
1435  * @dst: the place to copy.
1436  * @len: the maximum length to copy.
1437  *
1438  * Returns the bytes copied <= len or a negative errno.
1439  */
1440 ssize_t vringh_iov_pull_iotlb(struct vringh *vrh,
1441 			      struct vringh_kiov *riov,
1442 			      void *dst, size_t len)
1443 {
1444 	return vringh_iov_xfer(vrh, riov, dst, len, xfer_from_iotlb);
1445 }
1446 EXPORT_SYMBOL(vringh_iov_pull_iotlb);
1447 
1448 /**
1449  * vringh_iov_push_iotlb - copy bytes into vring_iov.
1450  * @vrh: the vring.
1451  * @wiov: the wiov as passed to vringh_getdesc_iotlb() (updated as we consume)
1452  * @src: the place to copy from.
1453  * @len: the maximum length to copy.
1454  *
1455  * Returns the bytes copied <= len or a negative errno.
1456  */
1457 ssize_t vringh_iov_push_iotlb(struct vringh *vrh,
1458 			      struct vringh_kiov *wiov,
1459 			      const void *src, size_t len)
1460 {
1461 	return vringh_iov_xfer(vrh, wiov, (void *)src, len, xfer_to_iotlb);
1462 }
1463 EXPORT_SYMBOL(vringh_iov_push_iotlb);
1464 
1465 /**
1466  * vringh_complete_iotlb - we've finished with descriptor, publish it.
1467  * @vrh: the vring.
1468  * @head: the head as filled in by vringh_getdesc_iotlb.
1469  * @len: the length of data we have written.
1470  *
1471  * You should check vringh_need_notify_iotlb() after one or more calls
1472  * to this function.
1473  */
1474 int vringh_complete_iotlb(struct vringh *vrh, u16 head, u32 len)
1475 {
1476 	struct vring_used_elem used;
1477 
1478 	used.id = cpu_to_vringh32(vrh, head);
1479 	used.len = cpu_to_vringh32(vrh, len);
1480 
1481 	return __vringh_complete(vrh, &used, 1, putu16_iotlb, putused_iotlb);
1482 }
1483 EXPORT_SYMBOL(vringh_complete_iotlb);
1484 
1485 /**
1486  * vringh_need_notify_iotlb - must we tell the other side about used buffers?
1487  * @vrh: the vring we've called vringh_complete_iotlb() on.
1488  *
1489  * Returns -errno or 0 if we don't need to tell the other side, 1 if we do.
1490  */
1491 int vringh_need_notify_iotlb(struct vringh *vrh)
1492 {
1493 	return __vringh_need_notify(vrh, getu16_iotlb);
1494 }
1495 EXPORT_SYMBOL(vringh_need_notify_iotlb);
1496 
1497 #endif
1498 
1499 MODULE_DESCRIPTION("host side of a virtio ring");
1500 MODULE_LICENSE("GPL");
1501