1 // SPDX-License-Identifier: GPL-2.0-only
3 * Helpers for the host side of a virtio ring.
5 * Since these may be in userspace, we use (inline) accessors.
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>
21 #include <uapi/linux/virtio_config.h>
23 static __printf(1,2) __cold void vringh_bad(const char *fmt, ...)
25 static DEFINE_RATELIMIT_STATE(vringh_rs,
26 DEFAULT_RATELIMIT_INTERVAL,
27 DEFAULT_RATELIMIT_BURST);
28 if (__ratelimit(&vringh_rs)) {
31 printk(KERN_NOTICE "vringh:");
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),
43 u16 avail_idx, i, head;
46 err = getu16(vrh, &avail_idx, &vrh->vring.avail->idx);
48 vringh_bad("Failed to access avail idx at %p",
49 &vrh->vring.avail->idx);
53 if (*last_avail_idx == avail_idx)
54 return vrh->vring.num;
56 /* Only get avail ring entries after they have been exposed by guest. */
57 virtio_rmb(vrh->weak_barriers);
59 i = *last_avail_idx & (vrh->vring.num - 1);
61 err = getu16(vrh, &head, &vrh->vring.avail->ring[i]);
63 vringh_bad("Failed to read head: idx %d address %p",
64 *last_avail_idx, &vrh->vring.avail->ring[i]);
68 if (head >= vrh->vring.num) {
69 vringh_bad("Guest says index %u > %u is available",
70 head, vrh->vring.num);
78 /* Copy some bytes to/from the iovec. Returns num copied. */
79 static inline ssize_t vringh_iov_xfer(struct vringh *vrh,
80 struct vringh_kiov *iov,
81 void *ptr, size_t len,
82 int (*xfer)(const struct vringh *vrh,
83 void *addr, void *ptr,
88 while (len && iov->i < iov->used) {
91 partlen = min(iov->iov[iov->i].iov_len, len);
92 err = xfer(vrh, iov->iov[iov->i].iov_base, ptr, partlen);
98 iov->consumed += partlen;
99 iov->iov[iov->i].iov_len -= partlen;
100 iov->iov[iov->i].iov_base += partlen;
102 if (!iov->iov[iov->i].iov_len) {
103 /* Fix up old iov element then increment. */
104 iov->iov[iov->i].iov_len = iov->consumed;
105 iov->iov[iov->i].iov_base -= iov->consumed;
115 /* May reduce *len if range is shorter. */
116 static inline bool range_check(struct vringh *vrh, u64 addr, size_t *len,
117 struct vringh_range *range,
118 bool (*getrange)(struct vringh *,
119 u64, struct vringh_range *))
121 if (addr < range->start || addr > range->end_incl) {
122 if (!getrange(vrh, addr, range))
125 BUG_ON(addr < range->start || addr > range->end_incl);
127 /* To end of memory? */
128 if (unlikely(addr + *len == 0)) {
129 if (range->end_incl == -1ULL)
134 /* Otherwise, don't wrap. */
135 if (addr + *len < addr) {
136 vringh_bad("Wrapping descriptor %zu@0x%llx",
137 *len, (unsigned long long)addr);
141 if (unlikely(addr + *len - 1 > range->end_incl))
146 *len = range->end_incl + 1 - addr;
150 static inline bool no_range_check(struct vringh *vrh, u64 addr, size_t *len,
151 struct vringh_range *range,
152 bool (*getrange)(struct vringh *,
153 u64, struct vringh_range *))
158 /* No reason for this code to be inline. */
159 static int move_to_indirect(const struct vringh *vrh,
160 int *up_next, u16 *i, void *addr,
161 const struct vring_desc *desc,
162 struct vring_desc **descs, int *desc_max)
166 /* Indirect tables can't have indirect. */
167 if (*up_next != -1) {
168 vringh_bad("Multilevel indirect %u->%u", *up_next, *i);
172 len = vringh32_to_cpu(vrh, desc->len);
173 if (unlikely(len % sizeof(struct vring_desc))) {
174 vringh_bad("Strange indirect len %u", desc->len);
178 /* We will check this when we follow it! */
179 if (desc->flags & cpu_to_vringh16(vrh, VRING_DESC_F_NEXT))
180 *up_next = vringh16_to_cpu(vrh, desc->next);
184 *desc_max = len / sizeof(struct vring_desc);
186 /* Now, start at the first indirect. */
191 static int resize_iovec(struct vringh_kiov *iov, gfp_t gfp)
194 unsigned int flag, new_num = (iov->max_num & ~VRINGH_IOV_ALLOCATED) * 2;
199 flag = (iov->max_num & VRINGH_IOV_ALLOCATED);
201 new = krealloc(iov->iov, new_num * sizeof(struct iovec), gfp);
203 new = kmalloc_array(new_num, sizeof(struct iovec), gfp);
205 memcpy(new, iov->iov,
206 iov->max_num * sizeof(struct iovec));
207 flag = VRINGH_IOV_ALLOCATED;
213 iov->max_num = (new_num | flag);
217 static u16 __cold return_from_indirect(const struct vringh *vrh, int *up_next,
218 struct vring_desc **descs, int *desc_max)
223 *descs = vrh->vring.desc;
224 *desc_max = vrh->vring.num;
228 static int slow_copy(struct vringh *vrh, void *dst, const void *src,
229 bool (*rcheck)(struct vringh *vrh, u64 addr, size_t *len,
230 struct vringh_range *range,
231 bool (*getrange)(struct vringh *vrh,
233 struct vringh_range *)),
234 bool (*getrange)(struct vringh *vrh,
236 struct vringh_range *r),
237 struct vringh_range *range,
238 int (*copy)(const struct vringh *vrh,
239 void *dst, const void *src, size_t len))
241 size_t part, len = sizeof(struct vring_desc);
248 addr = (u64)(unsigned long)src - range->offset;
250 if (!rcheck(vrh, addr, &part, range, getrange))
253 err = copy(vrh, dst, src, part);
265 __vringh_iov(struct vringh *vrh, u16 i,
266 struct vringh_kiov *riov,
267 struct vringh_kiov *wiov,
268 bool (*rcheck)(struct vringh *vrh, u64 addr, size_t *len,
269 struct vringh_range *range,
270 bool (*getrange)(struct vringh *, u64,
271 struct vringh_range *)),
272 bool (*getrange)(struct vringh *, u64, struct vringh_range *),
274 int (*copy)(const struct vringh *vrh,
275 void *dst, const void *src, size_t len))
277 int err, count = 0, indirect_count = 0, up_next, desc_max;
278 struct vring_desc desc, *descs;
279 struct vringh_range range = { -1ULL, 0 }, slowrange;
282 /* We start traversing vring's descriptor table. */
283 descs = vrh->vring.desc;
284 desc_max = vrh->vring.num;
287 /* You must want something! */
288 if (WARN_ON(!riov && !wiov))
292 riov->i = riov->used = 0;
294 wiov->i = wiov->used = 0;
298 struct vringh_kiov *iov;
302 err = slow_copy(vrh, &desc, &descs[i], rcheck, getrange,
305 err = copy(vrh, &desc, &descs[i], sizeof(desc));
309 if (unlikely(desc.flags &
310 cpu_to_vringh16(vrh, VRING_DESC_F_INDIRECT))) {
311 u64 a = vringh64_to_cpu(vrh, desc.addr);
313 /* Make sure it's OK, and get offset. */
314 len = vringh32_to_cpu(vrh, desc.len);
315 if (!rcheck(vrh, a, &len, &range, getrange)) {
320 if (unlikely(len != vringh32_to_cpu(vrh, desc.len))) {
322 /* We need to save this range to use offset */
326 addr = (void *)(long)(a + range.offset);
327 err = move_to_indirect(vrh, &up_next, &i, addr, &desc,
339 if (count > vrh->vring.num || indirect_count > desc_max) {
340 vringh_bad("Descriptor loop in %p", descs);
345 if (desc.flags & cpu_to_vringh16(vrh, VRING_DESC_F_WRITE))
349 if (unlikely(wiov && wiov->used)) {
350 vringh_bad("Readable desc %p after writable",
358 vringh_bad("Unexpected %s desc",
359 !wiov ? "writable" : "readable");
365 /* Make sure it's OK, and get offset. */
366 len = vringh32_to_cpu(vrh, desc.len);
367 if (!rcheck(vrh, vringh64_to_cpu(vrh, desc.addr), &len, &range,
372 addr = (void *)(unsigned long)(vringh64_to_cpu(vrh, desc.addr) +
375 if (unlikely(iov->used == (iov->max_num & ~VRINGH_IOV_ALLOCATED))) {
376 err = resize_iovec(iov, gfp);
381 iov->iov[iov->used].iov_base = addr;
382 iov->iov[iov->used].iov_len = len;
385 if (unlikely(len != vringh32_to_cpu(vrh, desc.len))) {
386 desc.len = cpu_to_vringh32(vrh,
387 vringh32_to_cpu(vrh, desc.len) - len);
388 desc.addr = cpu_to_vringh64(vrh,
389 vringh64_to_cpu(vrh, desc.addr) + len);
393 if (desc.flags & cpu_to_vringh16(vrh, VRING_DESC_F_NEXT)) {
394 i = vringh16_to_cpu(vrh, desc.next);
396 /* Just in case we need to finish traversing above. */
397 if (unlikely(up_next > 0)) {
398 i = return_from_indirect(vrh, &up_next,
407 vringh_bad("Chained index %u > %u", i, desc_max);
419 static inline int __vringh_complete(struct vringh *vrh,
420 const struct vring_used_elem *used,
421 unsigned int num_used,
422 int (*putu16)(const struct vringh *vrh,
423 __virtio16 *p, u16 val),
424 int (*putused)(const struct vringh *vrh,
425 struct vring_used_elem *dst,
426 const struct vring_used_elem
429 struct vring_used *used_ring;
433 used_ring = vrh->vring.used;
434 used_idx = vrh->last_used_idx + vrh->completed;
436 off = used_idx % vrh->vring.num;
438 /* Compiler knows num_used == 1 sometimes, hence extra check */
439 if (num_used > 1 && unlikely(off + num_used >= vrh->vring.num)) {
440 u16 part = vrh->vring.num - off;
441 err = putused(vrh, &used_ring->ring[off], used, part);
443 err = putused(vrh, &used_ring->ring[0], used + part,
446 err = putused(vrh, &used_ring->ring[off], used, num_used);
449 vringh_bad("Failed to write %u used entries %u at %p",
450 num_used, off, &used_ring->ring[off]);
454 /* Make sure buffer is written before we update index. */
455 virtio_wmb(vrh->weak_barriers);
457 err = putu16(vrh, &vrh->vring.used->idx, used_idx + num_used);
459 vringh_bad("Failed to update used index at %p",
460 &vrh->vring.used->idx);
464 vrh->completed += num_used;
469 static inline int __vringh_need_notify(struct vringh *vrh,
470 int (*getu16)(const struct vringh *vrh,
472 const __virtio16 *p))
478 /* Flush out used index update. This is paired with the
479 * barrier that the Guest executes when enabling
481 virtio_mb(vrh->weak_barriers);
483 /* Old-style, without event indices. */
484 if (!vrh->event_indices) {
486 err = getu16(vrh, &flags, &vrh->vring.avail->flags);
488 vringh_bad("Failed to get flags at %p",
489 &vrh->vring.avail->flags);
492 return (!(flags & VRING_AVAIL_F_NO_INTERRUPT));
495 /* Modern: we know when other side wants to know. */
496 err = getu16(vrh, &used_event, &vring_used_event(&vrh->vring));
498 vringh_bad("Failed to get used event idx at %p",
499 &vring_used_event(&vrh->vring));
503 /* Just in case we added so many that we wrap. */
504 if (unlikely(vrh->completed > 0xffff))
507 notify = vring_need_event(used_event,
508 vrh->last_used_idx + vrh->completed,
511 vrh->last_used_idx += vrh->completed;
516 static inline bool __vringh_notify_enable(struct vringh *vrh,
517 int (*getu16)(const struct vringh *vrh,
518 u16 *val, const __virtio16 *p),
519 int (*putu16)(const struct vringh *vrh,
520 __virtio16 *p, u16 val))
524 if (!vrh->event_indices) {
525 /* Old-school; update flags. */
526 if (putu16(vrh, &vrh->vring.used->flags, 0) != 0) {
527 vringh_bad("Clearing used flags %p",
528 &vrh->vring.used->flags);
532 if (putu16(vrh, &vring_avail_event(&vrh->vring),
533 vrh->last_avail_idx) != 0) {
534 vringh_bad("Updating avail event index %p",
535 &vring_avail_event(&vrh->vring));
540 /* They could have slipped one in as we were doing that: make
541 * sure it's written, then check again. */
542 virtio_mb(vrh->weak_barriers);
544 if (getu16(vrh, &avail, &vrh->vring.avail->idx) != 0) {
545 vringh_bad("Failed to check avail idx at %p",
546 &vrh->vring.avail->idx);
550 /* This is unlikely, so we just leave notifications enabled
551 * (if we're using event_indices, we'll only get one
552 * notification anyway). */
553 return avail == vrh->last_avail_idx;
556 static inline void __vringh_notify_disable(struct vringh *vrh,
557 int (*putu16)(const struct vringh *vrh,
558 __virtio16 *p, u16 val))
560 if (!vrh->event_indices) {
561 /* Old-school; update flags. */
562 if (putu16(vrh, &vrh->vring.used->flags,
563 VRING_USED_F_NO_NOTIFY)) {
564 vringh_bad("Setting used flags %p",
565 &vrh->vring.used->flags);
570 /* Userspace access helpers: in this case, addresses are really userspace. */
571 static inline int getu16_user(const struct vringh *vrh, u16 *val, const __virtio16 *p)
574 int rc = get_user(v, (__force __virtio16 __user *)p);
575 *val = vringh16_to_cpu(vrh, v);
579 static inline int putu16_user(const struct vringh *vrh, __virtio16 *p, u16 val)
581 __virtio16 v = cpu_to_vringh16(vrh, val);
582 return put_user(v, (__force __virtio16 __user *)p);
585 static inline int copydesc_user(const struct vringh *vrh,
586 void *dst, const void *src, size_t len)
588 return copy_from_user(dst, (__force void __user *)src, len) ?
592 static inline int putused_user(const struct vringh *vrh,
593 struct vring_used_elem *dst,
594 const struct vring_used_elem *src,
597 return copy_to_user((__force void __user *)dst, src,
598 sizeof(*dst) * num) ? -EFAULT : 0;
601 static inline int xfer_from_user(const struct vringh *vrh, void *src,
602 void *dst, size_t len)
604 return copy_from_user(dst, (__force void __user *)src, len) ?
608 static inline int xfer_to_user(const struct vringh *vrh,
609 void *dst, void *src, size_t len)
611 return copy_to_user((__force void __user *)dst, src, len) ?
616 * vringh_init_user - initialize a vringh for a userspace vring.
617 * @vrh: the vringh to initialize.
618 * @features: the feature bits for this ring.
619 * @num: the number of elements.
620 * @weak_barriers: true if we only need memory barriers, not I/O.
621 * @desc: the userpace descriptor pointer.
622 * @avail: the userpace avail pointer.
623 * @used: the userpace used pointer.
625 * Returns an error if num is invalid: you should check pointers
628 int vringh_init_user(struct vringh *vrh, u64 features,
629 unsigned int num, bool weak_barriers,
630 vring_desc_t __user *desc,
631 vring_avail_t __user *avail,
632 vring_used_t __user *used)
634 /* Sane power of 2 please! */
635 if (!num || num > 0xffff || (num & (num - 1))) {
636 vringh_bad("Bad ring size %u", num);
640 vrh->little_endian = (features & (1ULL << VIRTIO_F_VERSION_1));
641 vrh->event_indices = (features & (1 << VIRTIO_RING_F_EVENT_IDX));
642 vrh->weak_barriers = weak_barriers;
644 vrh->last_avail_idx = 0;
645 vrh->last_used_idx = 0;
646 vrh->vring.num = num;
647 /* vring expects kernel addresses, but only used via accessors. */
648 vrh->vring.desc = (__force struct vring_desc *)desc;
649 vrh->vring.avail = (__force struct vring_avail *)avail;
650 vrh->vring.used = (__force struct vring_used *)used;
653 EXPORT_SYMBOL(vringh_init_user);
656 * vringh_getdesc_user - get next available descriptor from userspace ring.
657 * @vrh: the userspace vring.
658 * @riov: where to put the readable descriptors (or NULL)
659 * @wiov: where to put the writable descriptors (or NULL)
660 * @getrange: function to call to check ranges.
661 * @head: head index we received, for passing to vringh_complete_user().
663 * Returns 0 if there was no descriptor, 1 if there was, or -errno.
665 * Note that on error return, you can tell the difference between an
666 * invalid ring and a single invalid descriptor: in the former case,
667 * *head will be vrh->vring.num. You may be able to ignore an invalid
668 * descriptor, but there's not much you can do with an invalid ring.
670 * Note that you may need to clean up riov and wiov, even on error!
672 int vringh_getdesc_user(struct vringh *vrh,
673 struct vringh_iov *riov,
674 struct vringh_iov *wiov,
675 bool (*getrange)(struct vringh *vrh,
676 u64 addr, struct vringh_range *r),
681 *head = vrh->vring.num;
682 err = __vringh_get_head(vrh, getu16_user, &vrh->last_avail_idx);
687 if (err == vrh->vring.num)
690 /* We need the layouts to be the identical for this to work */
691 BUILD_BUG_ON(sizeof(struct vringh_kiov) != sizeof(struct vringh_iov));
692 BUILD_BUG_ON(offsetof(struct vringh_kiov, iov) !=
693 offsetof(struct vringh_iov, iov));
694 BUILD_BUG_ON(offsetof(struct vringh_kiov, i) !=
695 offsetof(struct vringh_iov, i));
696 BUILD_BUG_ON(offsetof(struct vringh_kiov, used) !=
697 offsetof(struct vringh_iov, used));
698 BUILD_BUG_ON(offsetof(struct vringh_kiov, max_num) !=
699 offsetof(struct vringh_iov, max_num));
700 BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
701 BUILD_BUG_ON(offsetof(struct iovec, iov_base) !=
702 offsetof(struct kvec, iov_base));
703 BUILD_BUG_ON(offsetof(struct iovec, iov_len) !=
704 offsetof(struct kvec, iov_len));
705 BUILD_BUG_ON(sizeof(((struct iovec *)NULL)->iov_base)
706 != sizeof(((struct kvec *)NULL)->iov_base));
707 BUILD_BUG_ON(sizeof(((struct iovec *)NULL)->iov_len)
708 != sizeof(((struct kvec *)NULL)->iov_len));
711 err = __vringh_iov(vrh, *head, (struct vringh_kiov *)riov,
712 (struct vringh_kiov *)wiov,
713 range_check, getrange, GFP_KERNEL, copydesc_user);
719 EXPORT_SYMBOL(vringh_getdesc_user);
722 * vringh_iov_pull_user - copy bytes from vring_iov.
723 * @riov: the riov as passed to vringh_getdesc_user() (updated as we consume)
724 * @dst: the place to copy.
725 * @len: the maximum length to copy.
727 * Returns the bytes copied <= len or a negative errno.
729 ssize_t vringh_iov_pull_user(struct vringh_iov *riov, void *dst, size_t len)
731 return vringh_iov_xfer(NULL, (struct vringh_kiov *)riov,
732 dst, len, xfer_from_user);
734 EXPORT_SYMBOL(vringh_iov_pull_user);
737 * vringh_iov_push_user - copy bytes into vring_iov.
738 * @wiov: the wiov as passed to vringh_getdesc_user() (updated as we consume)
739 * @src: the place to copy from.
740 * @len: the maximum length to copy.
742 * Returns the bytes copied <= len or a negative errno.
744 ssize_t vringh_iov_push_user(struct vringh_iov *wiov,
745 const void *src, size_t len)
747 return vringh_iov_xfer(NULL, (struct vringh_kiov *)wiov,
748 (void *)src, len, xfer_to_user);
750 EXPORT_SYMBOL(vringh_iov_push_user);
753 * vringh_abandon_user - we've decided not to handle the descriptor(s).
755 * @num: the number of descriptors to put back (ie. num
756 * vringh_get_user() to undo).
758 * The next vringh_get_user() will return the old descriptor(s) again.
760 void vringh_abandon_user(struct vringh *vrh, unsigned int num)
762 /* We only update vring_avail_event(vr) when we want to be notified,
763 * so we haven't changed that yet. */
764 vrh->last_avail_idx -= num;
766 EXPORT_SYMBOL(vringh_abandon_user);
769 * vringh_complete_user - we've finished with descriptor, publish it.
771 * @head: the head as filled in by vringh_getdesc_user.
772 * @len: the length of data we have written.
774 * You should check vringh_need_notify_user() after one or more calls
777 int vringh_complete_user(struct vringh *vrh, u16 head, u32 len)
779 struct vring_used_elem used;
781 used.id = cpu_to_vringh32(vrh, head);
782 used.len = cpu_to_vringh32(vrh, len);
783 return __vringh_complete(vrh, &used, 1, putu16_user, putused_user);
785 EXPORT_SYMBOL(vringh_complete_user);
788 * vringh_complete_multi_user - we've finished with many descriptors.
790 * @used: the head, length pairs.
791 * @num_used: the number of used elements.
793 * You should check vringh_need_notify_user() after one or more calls
796 int vringh_complete_multi_user(struct vringh *vrh,
797 const struct vring_used_elem used[],
800 return __vringh_complete(vrh, used, num_used,
801 putu16_user, putused_user);
803 EXPORT_SYMBOL(vringh_complete_multi_user);
806 * vringh_notify_enable_user - we want to know if something changes.
809 * This always enables notifications, but returns false if there are
810 * now more buffers available in the vring.
812 bool vringh_notify_enable_user(struct vringh *vrh)
814 return __vringh_notify_enable(vrh, getu16_user, putu16_user);
816 EXPORT_SYMBOL(vringh_notify_enable_user);
819 * vringh_notify_disable_user - don't tell us if something changes.
822 * This is our normal running state: we disable and then only enable when
823 * we're going to sleep.
825 void vringh_notify_disable_user(struct vringh *vrh)
827 __vringh_notify_disable(vrh, putu16_user);
829 EXPORT_SYMBOL(vringh_notify_disable_user);
832 * vringh_need_notify_user - must we tell the other side about used buffers?
833 * @vrh: the vring we've called vringh_complete_user() on.
835 * Returns -errno or 0 if we don't need to tell the other side, 1 if we do.
837 int vringh_need_notify_user(struct vringh *vrh)
839 return __vringh_need_notify(vrh, getu16_user);
841 EXPORT_SYMBOL(vringh_need_notify_user);
843 /* Kernelspace access helpers. */
844 static inline int getu16_kern(const struct vringh *vrh,
845 u16 *val, const __virtio16 *p)
847 *val = vringh16_to_cpu(vrh, READ_ONCE(*p));
851 static inline int putu16_kern(const struct vringh *vrh, __virtio16 *p, u16 val)
853 WRITE_ONCE(*p, cpu_to_vringh16(vrh, val));
857 static inline int copydesc_kern(const struct vringh *vrh,
858 void *dst, const void *src, size_t len)
860 memcpy(dst, src, len);
864 static inline int putused_kern(const struct vringh *vrh,
865 struct vring_used_elem *dst,
866 const struct vring_used_elem *src,
869 memcpy(dst, src, num * sizeof(*dst));
873 static inline int xfer_kern(const struct vringh *vrh, void *src,
874 void *dst, size_t len)
876 memcpy(dst, src, len);
880 static inline int kern_xfer(const struct vringh *vrh, void *dst,
881 void *src, size_t len)
883 memcpy(dst, src, len);
888 * vringh_init_kern - initialize a vringh for a kernelspace vring.
889 * @vrh: the vringh to initialize.
890 * @features: the feature bits for this ring.
891 * @num: the number of elements.
892 * @weak_barriers: true if we only need memory barriers, not I/O.
893 * @desc: the userpace descriptor pointer.
894 * @avail: the userpace avail pointer.
895 * @used: the userpace used pointer.
897 * Returns an error if num is invalid.
899 int vringh_init_kern(struct vringh *vrh, u64 features,
900 unsigned int num, bool weak_barriers,
901 struct vring_desc *desc,
902 struct vring_avail *avail,
903 struct vring_used *used)
905 /* Sane power of 2 please! */
906 if (!num || num > 0xffff || (num & (num - 1))) {
907 vringh_bad("Bad ring size %u", num);
911 vrh->little_endian = (features & (1ULL << VIRTIO_F_VERSION_1));
912 vrh->event_indices = (features & (1 << VIRTIO_RING_F_EVENT_IDX));
913 vrh->weak_barriers = weak_barriers;
915 vrh->last_avail_idx = 0;
916 vrh->last_used_idx = 0;
917 vrh->vring.num = num;
918 vrh->vring.desc = desc;
919 vrh->vring.avail = avail;
920 vrh->vring.used = used;
923 EXPORT_SYMBOL(vringh_init_kern);
926 * vringh_getdesc_kern - get next available descriptor from kernelspace ring.
927 * @vrh: the kernelspace vring.
928 * @riov: where to put the readable descriptors (or NULL)
929 * @wiov: where to put the writable descriptors (or NULL)
930 * @head: head index we received, for passing to vringh_complete_kern().
931 * @gfp: flags for allocating larger riov/wiov.
933 * Returns 0 if there was no descriptor, 1 if there was, or -errno.
935 * Note that on error return, you can tell the difference between an
936 * invalid ring and a single invalid descriptor: in the former case,
937 * *head will be vrh->vring.num. You may be able to ignore an invalid
938 * descriptor, but there's not much you can do with an invalid ring.
940 * Note that you may need to clean up riov and wiov, even on error!
942 int vringh_getdesc_kern(struct vringh *vrh,
943 struct vringh_kiov *riov,
944 struct vringh_kiov *wiov,
950 err = __vringh_get_head(vrh, getu16_kern, &vrh->last_avail_idx);
955 if (err == vrh->vring.num)
959 err = __vringh_iov(vrh, *head, riov, wiov, no_range_check, NULL,
966 EXPORT_SYMBOL(vringh_getdesc_kern);
969 * vringh_iov_pull_kern - copy bytes from vring_iov.
970 * @riov: the riov as passed to vringh_getdesc_kern() (updated as we consume)
971 * @dst: the place to copy.
972 * @len: the maximum length to copy.
974 * Returns the bytes copied <= len or a negative errno.
976 ssize_t vringh_iov_pull_kern(struct vringh_kiov *riov, void *dst, size_t len)
978 return vringh_iov_xfer(NULL, riov, dst, len, xfer_kern);
980 EXPORT_SYMBOL(vringh_iov_pull_kern);
983 * vringh_iov_push_kern - copy bytes into vring_iov.
984 * @wiov: the wiov as passed to vringh_getdesc_kern() (updated as we consume)
985 * @src: the place to copy from.
986 * @len: the maximum length to copy.
988 * Returns the bytes copied <= len or a negative errno.
990 ssize_t vringh_iov_push_kern(struct vringh_kiov *wiov,
991 const void *src, size_t len)
993 return vringh_iov_xfer(NULL, wiov, (void *)src, len, kern_xfer);
995 EXPORT_SYMBOL(vringh_iov_push_kern);
998 * vringh_abandon_kern - we've decided not to handle the descriptor(s).
1000 * @num: the number of descriptors to put back (ie. num
1001 * vringh_get_kern() to undo).
1003 * The next vringh_get_kern() will return the old descriptor(s) again.
1005 void vringh_abandon_kern(struct vringh *vrh, unsigned int num)
1007 /* We only update vring_avail_event(vr) when we want to be notified,
1008 * so we haven't changed that yet. */
1009 vrh->last_avail_idx -= num;
1011 EXPORT_SYMBOL(vringh_abandon_kern);
1014 * vringh_complete_kern - we've finished with descriptor, publish it.
1016 * @head: the head as filled in by vringh_getdesc_kern.
1017 * @len: the length of data we have written.
1019 * You should check vringh_need_notify_kern() after one or more calls
1022 int vringh_complete_kern(struct vringh *vrh, u16 head, u32 len)
1024 struct vring_used_elem used;
1026 used.id = cpu_to_vringh32(vrh, head);
1027 used.len = cpu_to_vringh32(vrh, len);
1029 return __vringh_complete(vrh, &used, 1, putu16_kern, putused_kern);
1031 EXPORT_SYMBOL(vringh_complete_kern);
1034 * vringh_notify_enable_kern - we want to know if something changes.
1037 * This always enables notifications, but returns false if there are
1038 * now more buffers available in the vring.
1040 bool vringh_notify_enable_kern(struct vringh *vrh)
1042 return __vringh_notify_enable(vrh, getu16_kern, putu16_kern);
1044 EXPORT_SYMBOL(vringh_notify_enable_kern);
1047 * vringh_notify_disable_kern - don't tell us if something changes.
1050 * This is our normal running state: we disable and then only enable when
1051 * we're going to sleep.
1053 void vringh_notify_disable_kern(struct vringh *vrh)
1055 __vringh_notify_disable(vrh, putu16_kern);
1057 EXPORT_SYMBOL(vringh_notify_disable_kern);
1060 * vringh_need_notify_kern - must we tell the other side about used buffers?
1061 * @vrh: the vring we've called vringh_complete_kern() on.
1063 * Returns -errno or 0 if we don't need to tell the other side, 1 if we do.
1065 int vringh_need_notify_kern(struct vringh *vrh)
1067 return __vringh_need_notify(vrh, getu16_kern);
1069 EXPORT_SYMBOL(vringh_need_notify_kern);
1071 #if IS_REACHABLE(CONFIG_VHOST_IOTLB)
1073 static int iotlb_translate(const struct vringh *vrh,
1074 u64 addr, u64 len, struct bio_vec iov[],
1075 int iov_size, u32 perm)
1077 struct vhost_iotlb_map *map;
1078 struct vhost_iotlb *iotlb = vrh->iotlb;
1085 if (unlikely(ret >= iov_size)) {
1090 map = vhost_iotlb_itree_first(iotlb, addr,
1092 if (!map || map->start > addr) {
1095 } else if (!(map->perm & perm)) {
1100 size = map->size - addr + map->start;
1101 pa = map->addr + addr - map->start;
1102 pfn = pa >> PAGE_SHIFT;
1103 iov[ret].bv_page = pfn_to_page(pfn);
1104 iov[ret].bv_len = min(len - s, size);
1105 iov[ret].bv_offset = pa & (PAGE_SIZE - 1);
1114 static inline int copy_from_iotlb(const struct vringh *vrh, void *dst,
1115 void *src, size_t len)
1117 struct iov_iter iter;
1118 struct bio_vec iov[16];
1121 ret = iotlb_translate(vrh, (u64)(uintptr_t)src,
1122 len, iov, 16, VHOST_MAP_RO);
1126 iov_iter_bvec(&iter, READ, iov, ret, len);
1128 ret = copy_from_iter(dst, len, &iter);
1133 static inline int copy_to_iotlb(const struct vringh *vrh, void *dst,
1134 void *src, size_t len)
1136 struct iov_iter iter;
1137 struct bio_vec iov[16];
1140 ret = iotlb_translate(vrh, (u64)(uintptr_t)dst,
1141 len, iov, 16, VHOST_MAP_WO);
1145 iov_iter_bvec(&iter, WRITE, iov, ret, len);
1147 return copy_to_iter(src, len, &iter);
1150 static inline int getu16_iotlb(const struct vringh *vrh,
1151 u16 *val, const __virtio16 *p)
1157 /* Atomic read is needed for getu16 */
1158 ret = iotlb_translate(vrh, (u64)(uintptr_t)p, sizeof(*p),
1159 &iov, 1, VHOST_MAP_RO);
1163 kaddr = kmap_atomic(iov.bv_page);
1164 from = kaddr + iov.bv_offset;
1165 *val = vringh16_to_cpu(vrh, READ_ONCE(*(__virtio16 *)from));
1166 kunmap_atomic(kaddr);
1171 static inline int putu16_iotlb(const struct vringh *vrh,
1172 __virtio16 *p, u16 val)
1178 /* Atomic write is needed for putu16 */
1179 ret = iotlb_translate(vrh, (u64)(uintptr_t)p, sizeof(*p),
1180 &iov, 1, VHOST_MAP_WO);
1184 kaddr = kmap_atomic(iov.bv_page);
1185 to = kaddr + iov.bv_offset;
1186 WRITE_ONCE(*(__virtio16 *)to, cpu_to_vringh16(vrh, val));
1187 kunmap_atomic(kaddr);
1192 static inline int copydesc_iotlb(const struct vringh *vrh,
1193 void *dst, const void *src, size_t len)
1197 ret = copy_from_iotlb(vrh, dst, (void *)src, len);
1204 static inline int xfer_from_iotlb(const struct vringh *vrh, void *src,
1205 void *dst, size_t len)
1209 ret = copy_from_iotlb(vrh, dst, src, len);
1216 static inline int xfer_to_iotlb(const struct vringh *vrh,
1217 void *dst, void *src, size_t len)
1221 ret = copy_to_iotlb(vrh, dst, src, len);
1228 static inline int putused_iotlb(const struct vringh *vrh,
1229 struct vring_used_elem *dst,
1230 const struct vring_used_elem *src,
1233 int size = num * sizeof(*dst);
1236 ret = copy_to_iotlb(vrh, dst, (void *)src, num * sizeof(*dst));
1244 * vringh_init_iotlb - initialize a vringh for a ring with IOTLB.
1245 * @vrh: the vringh to initialize.
1246 * @features: the feature bits for this ring.
1247 * @num: the number of elements.
1248 * @weak_barriers: true if we only need memory barriers, not I/O.
1249 * @desc: the userpace descriptor pointer.
1250 * @avail: the userpace avail pointer.
1251 * @used: the userpace used pointer.
1253 * Returns an error if num is invalid.
1255 int vringh_init_iotlb(struct vringh *vrh, u64 features,
1256 unsigned int num, bool weak_barriers,
1257 struct vring_desc *desc,
1258 struct vring_avail *avail,
1259 struct vring_used *used)
1261 return vringh_init_kern(vrh, features, num, weak_barriers,
1264 EXPORT_SYMBOL(vringh_init_iotlb);
1267 * vringh_set_iotlb - initialize a vringh for a ring with IOTLB.
1269 * @iotlb: iotlb associated with this vring
1271 void vringh_set_iotlb(struct vringh *vrh, struct vhost_iotlb *iotlb)
1275 EXPORT_SYMBOL(vringh_set_iotlb);
1278 * vringh_getdesc_iotlb - get next available descriptor from ring with
1280 * @vrh: the kernelspace vring.
1281 * @riov: where to put the readable descriptors (or NULL)
1282 * @wiov: where to put the writable descriptors (or NULL)
1283 * @head: head index we received, for passing to vringh_complete_iotlb().
1284 * @gfp: flags for allocating larger riov/wiov.
1286 * Returns 0 if there was no descriptor, 1 if there was, or -errno.
1288 * Note that on error return, you can tell the difference between an
1289 * invalid ring and a single invalid descriptor: in the former case,
1290 * *head will be vrh->vring.num. You may be able to ignore an invalid
1291 * descriptor, but there's not much you can do with an invalid ring.
1293 * Note that you may need to clean up riov and wiov, even on error!
1295 int vringh_getdesc_iotlb(struct vringh *vrh,
1296 struct vringh_kiov *riov,
1297 struct vringh_kiov *wiov,
1303 err = __vringh_get_head(vrh, getu16_iotlb, &vrh->last_avail_idx);
1308 if (err == vrh->vring.num)
1312 err = __vringh_iov(vrh, *head, riov, wiov, no_range_check, NULL,
1313 gfp, copydesc_iotlb);
1319 EXPORT_SYMBOL(vringh_getdesc_iotlb);
1322 * vringh_iov_pull_iotlb - copy bytes from vring_iov.
1324 * @riov: the riov as passed to vringh_getdesc_iotlb() (updated as we consume)
1325 * @dst: the place to copy.
1326 * @len: the maximum length to copy.
1328 * Returns the bytes copied <= len or a negative errno.
1330 ssize_t vringh_iov_pull_iotlb(struct vringh *vrh,
1331 struct vringh_kiov *riov,
1332 void *dst, size_t len)
1334 return vringh_iov_xfer(vrh, riov, dst, len, xfer_from_iotlb);
1336 EXPORT_SYMBOL(vringh_iov_pull_iotlb);
1339 * vringh_iov_push_iotlb - copy bytes into vring_iov.
1341 * @wiov: the wiov as passed to vringh_getdesc_iotlb() (updated as we consume)
1342 * @src: the place to copy from.
1343 * @len: the maximum length to copy.
1345 * Returns the bytes copied <= len or a negative errno.
1347 ssize_t vringh_iov_push_iotlb(struct vringh *vrh,
1348 struct vringh_kiov *wiov,
1349 const void *src, size_t len)
1351 return vringh_iov_xfer(vrh, wiov, (void *)src, len, xfer_to_iotlb);
1353 EXPORT_SYMBOL(vringh_iov_push_iotlb);
1356 * vringh_abandon_iotlb - we've decided not to handle the descriptor(s).
1358 * @num: the number of descriptors to put back (ie. num
1359 * vringh_get_iotlb() to undo).
1361 * The next vringh_get_iotlb() will return the old descriptor(s) again.
1363 void vringh_abandon_iotlb(struct vringh *vrh, unsigned int num)
1365 /* We only update vring_avail_event(vr) when we want to be notified,
1366 * so we haven't changed that yet.
1368 vrh->last_avail_idx -= num;
1370 EXPORT_SYMBOL(vringh_abandon_iotlb);
1373 * vringh_complete_iotlb - we've finished with descriptor, publish it.
1375 * @head: the head as filled in by vringh_getdesc_iotlb.
1376 * @len: the length of data we have written.
1378 * You should check vringh_need_notify_iotlb() after one or more calls
1381 int vringh_complete_iotlb(struct vringh *vrh, u16 head, u32 len)
1383 struct vring_used_elem used;
1385 used.id = cpu_to_vringh32(vrh, head);
1386 used.len = cpu_to_vringh32(vrh, len);
1388 return __vringh_complete(vrh, &used, 1, putu16_iotlb, putused_iotlb);
1390 EXPORT_SYMBOL(vringh_complete_iotlb);
1393 * vringh_notify_enable_iotlb - we want to know if something changes.
1396 * This always enables notifications, but returns false if there are
1397 * now more buffers available in the vring.
1399 bool vringh_notify_enable_iotlb(struct vringh *vrh)
1401 return __vringh_notify_enable(vrh, getu16_iotlb, putu16_iotlb);
1403 EXPORT_SYMBOL(vringh_notify_enable_iotlb);
1406 * vringh_notify_disable_iotlb - don't tell us if something changes.
1409 * This is our normal running state: we disable and then only enable when
1410 * we're going to sleep.
1412 void vringh_notify_disable_iotlb(struct vringh *vrh)
1414 __vringh_notify_disable(vrh, putu16_iotlb);
1416 EXPORT_SYMBOL(vringh_notify_disable_iotlb);
1419 * vringh_need_notify_iotlb - must we tell the other side about used buffers?
1420 * @vrh: the vring we've called vringh_complete_iotlb() on.
1422 * Returns -errno or 0 if we don't need to tell the other side, 1 if we do.
1424 int vringh_need_notify_iotlb(struct vringh *vrh)
1426 return __vringh_need_notify(vrh, getu16_iotlb);
1428 EXPORT_SYMBOL(vringh_need_notify_iotlb);
1432 MODULE_LICENSE("GPL");