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);
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
83 void vringh_kiov_advance(struct vringh_kiov *iov, size_t len)
85 while (len && iov->i < iov->used) {
86 size_t partlen = min(iov->iov[iov->i].iov_len, len);
88 iov->consumed += partlen;
89 iov->iov[iov->i].iov_len -= partlen;
90 iov->iov[iov->i].iov_base += partlen;
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;
104 EXPORT_SYMBOL(vringh_kiov_advance);
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,
116 while (len && iov->i < iov->used) {
119 partlen = min(iov->iov[iov->i].iov_len, len);
120 err = xfer(vrh, iov->iov[iov->i].iov_base, ptr, partlen);
127 vringh_kiov_advance(iov, partlen);
132 /* May reduce *len if range is shorter. */
133 static inline bool range_check(struct vringh *vrh, u64 addr, size_t *len,
134 struct vringh_range *range,
135 bool (*getrange)(struct vringh *,
136 u64, struct vringh_range *))
138 if (addr < range->start || addr > range->end_incl) {
139 if (!getrange(vrh, addr, range))
142 BUG_ON(addr < range->start || addr > range->end_incl);
144 /* To end of memory? */
145 if (unlikely(addr + *len == 0)) {
146 if (range->end_incl == -1ULL)
151 /* Otherwise, don't wrap. */
152 if (addr + *len < addr) {
153 vringh_bad("Wrapping descriptor %zu@0x%llx",
154 *len, (unsigned long long)addr);
158 if (unlikely(addr + *len - 1 > range->end_incl))
163 *len = range->end_incl + 1 - addr;
167 static inline bool no_range_check(struct vringh *vrh, u64 addr, size_t *len,
168 struct vringh_range *range,
169 bool (*getrange)(struct vringh *,
170 u64, struct vringh_range *))
175 /* No reason for this code to be inline. */
176 static int move_to_indirect(const struct vringh *vrh,
177 int *up_next, u16 *i, void *addr,
178 const struct vring_desc *desc,
179 struct vring_desc **descs, int *desc_max)
183 /* Indirect tables can't have indirect. */
184 if (*up_next != -1) {
185 vringh_bad("Multilevel indirect %u->%u", *up_next, *i);
189 len = vringh32_to_cpu(vrh, desc->len);
190 if (unlikely(len % sizeof(struct vring_desc))) {
191 vringh_bad("Strange indirect len %u", desc->len);
195 /* We will check this when we follow it! */
196 if (desc->flags & cpu_to_vringh16(vrh, VRING_DESC_F_NEXT))
197 *up_next = vringh16_to_cpu(vrh, desc->next);
201 *desc_max = len / sizeof(struct vring_desc);
203 /* Now, start at the first indirect. */
208 static int resize_iovec(struct vringh_kiov *iov, gfp_t gfp)
211 unsigned int flag, new_num = (iov->max_num & ~VRINGH_IOV_ALLOCATED) * 2;
216 flag = (iov->max_num & VRINGH_IOV_ALLOCATED);
218 new = krealloc_array(iov->iov, new_num,
219 sizeof(struct iovec), gfp);
221 new = kmalloc_array(new_num, sizeof(struct iovec), gfp);
223 memcpy(new, iov->iov,
224 iov->max_num * sizeof(struct iovec));
225 flag = VRINGH_IOV_ALLOCATED;
231 iov->max_num = (new_num | flag);
235 static u16 __cold return_from_indirect(const struct vringh *vrh, int *up_next,
236 struct vring_desc **descs, int *desc_max)
241 *descs = vrh->vring.desc;
242 *desc_max = vrh->vring.num;
246 static int slow_copy(struct vringh *vrh, void *dst, const void *src,
247 bool (*rcheck)(struct vringh *vrh, u64 addr, size_t *len,
248 struct vringh_range *range,
249 bool (*getrange)(struct vringh *vrh,
251 struct vringh_range *)),
252 bool (*getrange)(struct vringh *vrh,
254 struct vringh_range *r),
255 struct vringh_range *range,
256 int (*copy)(const struct vringh *vrh,
257 void *dst, const void *src, size_t len))
259 size_t part, len = sizeof(struct vring_desc);
266 addr = (u64)(unsigned long)src - range->offset;
268 if (!rcheck(vrh, addr, &part, range, getrange))
271 err = copy(vrh, dst, src, part);
283 __vringh_iov(struct vringh *vrh, u16 i,
284 struct vringh_kiov *riov,
285 struct vringh_kiov *wiov,
286 bool (*rcheck)(struct vringh *vrh, u64 addr, size_t *len,
287 struct vringh_range *range,
288 bool (*getrange)(struct vringh *, u64,
289 struct vringh_range *)),
290 bool (*getrange)(struct vringh *, u64, struct vringh_range *),
292 int (*copy)(const struct vringh *vrh,
293 void *dst, const void *src, size_t len))
295 int err, count = 0, up_next, desc_max;
296 struct vring_desc desc, *descs;
297 struct vringh_range range = { -1ULL, 0 }, slowrange;
300 /* We start traversing vring's descriptor table. */
301 descs = vrh->vring.desc;
302 desc_max = vrh->vring.num;
305 /* You must want something! */
306 if (WARN_ON(!riov && !wiov))
310 riov->i = riov->used = riov->consumed = 0;
312 wiov->i = wiov->used = wiov->consumed = 0;
316 struct vringh_kiov *iov;
320 err = slow_copy(vrh, &desc, &descs[i], rcheck, getrange,
323 err = copy(vrh, &desc, &descs[i], sizeof(desc));
327 if (unlikely(desc.flags &
328 cpu_to_vringh16(vrh, VRING_DESC_F_INDIRECT))) {
329 u64 a = vringh64_to_cpu(vrh, desc.addr);
331 /* Make sure it's OK, and get offset. */
332 len = vringh32_to_cpu(vrh, desc.len);
333 if (!rcheck(vrh, a, &len, &range, getrange)) {
338 if (unlikely(len != vringh32_to_cpu(vrh, desc.len))) {
340 /* We need to save this range to use offset */
344 addr = (void *)(long)(a + range.offset);
345 err = move_to_indirect(vrh, &up_next, &i, addr, &desc,
352 if (count++ == vrh->vring.num) {
353 vringh_bad("Descriptor loop in %p", descs);
358 if (desc.flags & cpu_to_vringh16(vrh, VRING_DESC_F_WRITE))
362 if (unlikely(wiov && wiov->i)) {
363 vringh_bad("Readable desc %p after writable",
371 vringh_bad("Unexpected %s desc",
372 !wiov ? "writable" : "readable");
378 /* Make sure it's OK, and get offset. */
379 len = vringh32_to_cpu(vrh, desc.len);
380 if (!rcheck(vrh, vringh64_to_cpu(vrh, desc.addr), &len, &range,
385 addr = (void *)(unsigned long)(vringh64_to_cpu(vrh, desc.addr) +
388 if (unlikely(iov->used == (iov->max_num & ~VRINGH_IOV_ALLOCATED))) {
389 err = resize_iovec(iov, gfp);
394 iov->iov[iov->used].iov_base = addr;
395 iov->iov[iov->used].iov_len = len;
398 if (unlikely(len != vringh32_to_cpu(vrh, desc.len))) {
399 desc.len = cpu_to_vringh32(vrh,
400 vringh32_to_cpu(vrh, desc.len) - len);
401 desc.addr = cpu_to_vringh64(vrh,
402 vringh64_to_cpu(vrh, desc.addr) + len);
406 if (desc.flags & cpu_to_vringh16(vrh, VRING_DESC_F_NEXT)) {
407 i = vringh16_to_cpu(vrh, desc.next);
409 /* Just in case we need to finish traversing above. */
410 if (unlikely(up_next > 0)) {
411 i = return_from_indirect(vrh, &up_next,
419 vringh_bad("Chained index %u > %u", i, desc_max);
431 static inline int __vringh_complete(struct vringh *vrh,
432 const struct vring_used_elem *used,
433 unsigned int num_used,
434 int (*putu16)(const struct vringh *vrh,
435 __virtio16 *p, u16 val),
436 int (*putused)(const struct vringh *vrh,
437 struct vring_used_elem *dst,
438 const struct vring_used_elem
441 struct vring_used *used_ring;
445 used_ring = vrh->vring.used;
446 used_idx = vrh->last_used_idx + vrh->completed;
448 off = used_idx % vrh->vring.num;
450 /* Compiler knows num_used == 1 sometimes, hence extra check */
451 if (num_used > 1 && unlikely(off + num_used >= vrh->vring.num)) {
452 u16 part = vrh->vring.num - off;
453 err = putused(vrh, &used_ring->ring[off], used, part);
455 err = putused(vrh, &used_ring->ring[0], used + part,
458 err = putused(vrh, &used_ring->ring[off], used, num_used);
461 vringh_bad("Failed to write %u used entries %u at %p",
462 num_used, off, &used_ring->ring[off]);
466 /* Make sure buffer is written before we update index. */
467 virtio_wmb(vrh->weak_barriers);
469 err = putu16(vrh, &vrh->vring.used->idx, used_idx + num_used);
471 vringh_bad("Failed to update used index at %p",
472 &vrh->vring.used->idx);
476 vrh->completed += num_used;
481 static inline int __vringh_need_notify(struct vringh *vrh,
482 int (*getu16)(const struct vringh *vrh,
484 const __virtio16 *p))
490 /* Flush out used index update. This is paired with the
491 * barrier that the Guest executes when enabling
493 virtio_mb(vrh->weak_barriers);
495 /* Old-style, without event indices. */
496 if (!vrh->event_indices) {
498 err = getu16(vrh, &flags, &vrh->vring.avail->flags);
500 vringh_bad("Failed to get flags at %p",
501 &vrh->vring.avail->flags);
504 return (!(flags & VRING_AVAIL_F_NO_INTERRUPT));
507 /* Modern: we know when other side wants to know. */
508 err = getu16(vrh, &used_event, &vring_used_event(&vrh->vring));
510 vringh_bad("Failed to get used event idx at %p",
511 &vring_used_event(&vrh->vring));
515 /* Just in case we added so many that we wrap. */
516 if (unlikely(vrh->completed > 0xffff))
519 notify = vring_need_event(used_event,
520 vrh->last_used_idx + vrh->completed,
523 vrh->last_used_idx += vrh->completed;
528 static inline bool __vringh_notify_enable(struct vringh *vrh,
529 int (*getu16)(const struct vringh *vrh,
530 u16 *val, const __virtio16 *p),
531 int (*putu16)(const struct vringh *vrh,
532 __virtio16 *p, u16 val))
536 if (!vrh->event_indices) {
537 /* Old-school; update flags. */
538 if (putu16(vrh, &vrh->vring.used->flags, 0) != 0) {
539 vringh_bad("Clearing used flags %p",
540 &vrh->vring.used->flags);
544 if (putu16(vrh, &vring_avail_event(&vrh->vring),
545 vrh->last_avail_idx) != 0) {
546 vringh_bad("Updating avail event index %p",
547 &vring_avail_event(&vrh->vring));
552 /* They could have slipped one in as we were doing that: make
553 * sure it's written, then check again. */
554 virtio_mb(vrh->weak_barriers);
556 if (getu16(vrh, &avail, &vrh->vring.avail->idx) != 0) {
557 vringh_bad("Failed to check avail idx at %p",
558 &vrh->vring.avail->idx);
562 /* This is unlikely, so we just leave notifications enabled
563 * (if we're using event_indices, we'll only get one
564 * notification anyway). */
565 return avail == vrh->last_avail_idx;
568 static inline void __vringh_notify_disable(struct vringh *vrh,
569 int (*putu16)(const struct vringh *vrh,
570 __virtio16 *p, u16 val))
572 if (!vrh->event_indices) {
573 /* Old-school; update flags. */
574 if (putu16(vrh, &vrh->vring.used->flags,
575 VRING_USED_F_NO_NOTIFY)) {
576 vringh_bad("Setting used flags %p",
577 &vrh->vring.used->flags);
582 /* Userspace access helpers: in this case, addresses are really userspace. */
583 static inline int getu16_user(const struct vringh *vrh, u16 *val, const __virtio16 *p)
586 int rc = get_user(v, (__force __virtio16 __user *)p);
587 *val = vringh16_to_cpu(vrh, v);
591 static inline int putu16_user(const struct vringh *vrh, __virtio16 *p, u16 val)
593 __virtio16 v = cpu_to_vringh16(vrh, val);
594 return put_user(v, (__force __virtio16 __user *)p);
597 static inline int copydesc_user(const struct vringh *vrh,
598 void *dst, const void *src, size_t len)
600 return copy_from_user(dst, (__force void __user *)src, len) ?
604 static inline int putused_user(const struct vringh *vrh,
605 struct vring_used_elem *dst,
606 const struct vring_used_elem *src,
609 return copy_to_user((__force void __user *)dst, src,
610 sizeof(*dst) * num) ? -EFAULT : 0;
613 static inline int xfer_from_user(const struct vringh *vrh, void *src,
614 void *dst, size_t len)
616 return copy_from_user(dst, (__force void __user *)src, len) ?
620 static inline int xfer_to_user(const struct vringh *vrh,
621 void *dst, void *src, size_t len)
623 return copy_to_user((__force void __user *)dst, src, len) ?
628 * vringh_init_user - initialize a vringh for a userspace vring.
629 * @vrh: the vringh to initialize.
630 * @features: the feature bits for this ring.
631 * @num: the number of elements.
632 * @weak_barriers: true if we only need memory barriers, not I/O.
633 * @desc: the userpace descriptor pointer.
634 * @avail: the userpace avail pointer.
635 * @used: the userpace used pointer.
637 * Returns an error if num is invalid: you should check pointers
640 int vringh_init_user(struct vringh *vrh, u64 features,
641 unsigned int num, bool weak_barriers,
642 vring_desc_t __user *desc,
643 vring_avail_t __user *avail,
644 vring_used_t __user *used)
646 /* Sane power of 2 please! */
647 if (!num || num > 0xffff || (num & (num - 1))) {
648 vringh_bad("Bad ring size %u", num);
652 vrh->little_endian = (features & (1ULL << VIRTIO_F_VERSION_1));
653 vrh->event_indices = (features & (1 << VIRTIO_RING_F_EVENT_IDX));
654 vrh->weak_barriers = weak_barriers;
656 vrh->last_avail_idx = 0;
657 vrh->last_used_idx = 0;
658 vrh->vring.num = num;
659 /* vring expects kernel addresses, but only used via accessors. */
660 vrh->vring.desc = (__force struct vring_desc *)desc;
661 vrh->vring.avail = (__force struct vring_avail *)avail;
662 vrh->vring.used = (__force struct vring_used *)used;
665 EXPORT_SYMBOL(vringh_init_user);
668 * vringh_getdesc_user - get next available descriptor from userspace ring.
669 * @vrh: the userspace vring.
670 * @riov: where to put the readable descriptors (or NULL)
671 * @wiov: where to put the writable descriptors (or NULL)
672 * @getrange: function to call to check ranges.
673 * @head: head index we received, for passing to vringh_complete_user().
675 * Returns 0 if there was no descriptor, 1 if there was, or -errno.
677 * Note that on error return, you can tell the difference between an
678 * invalid ring and a single invalid descriptor: in the former case,
679 * *head will be vrh->vring.num. You may be able to ignore an invalid
680 * descriptor, but there's not much you can do with an invalid ring.
682 * Note that you can reuse riov and wiov with subsequent calls. Content is
683 * overwritten and memory reallocated if more space is needed.
684 * When you don't have to use riov and wiov anymore, you should clean up them
685 * calling vringh_iov_cleanup() to release the memory, even on error!
687 int vringh_getdesc_user(struct vringh *vrh,
688 struct vringh_iov *riov,
689 struct vringh_iov *wiov,
690 bool (*getrange)(struct vringh *vrh,
691 u64 addr, struct vringh_range *r),
696 *head = vrh->vring.num;
697 err = __vringh_get_head(vrh, getu16_user, &vrh->last_avail_idx);
702 if (err == vrh->vring.num)
705 /* We need the layouts to be the identical for this to work */
706 BUILD_BUG_ON(sizeof(struct vringh_kiov) != sizeof(struct vringh_iov));
707 BUILD_BUG_ON(offsetof(struct vringh_kiov, iov) !=
708 offsetof(struct vringh_iov, iov));
709 BUILD_BUG_ON(offsetof(struct vringh_kiov, i) !=
710 offsetof(struct vringh_iov, i));
711 BUILD_BUG_ON(offsetof(struct vringh_kiov, used) !=
712 offsetof(struct vringh_iov, used));
713 BUILD_BUG_ON(offsetof(struct vringh_kiov, max_num) !=
714 offsetof(struct vringh_iov, max_num));
715 BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
716 BUILD_BUG_ON(offsetof(struct iovec, iov_base) !=
717 offsetof(struct kvec, iov_base));
718 BUILD_BUG_ON(offsetof(struct iovec, iov_len) !=
719 offsetof(struct kvec, iov_len));
720 BUILD_BUG_ON(sizeof(((struct iovec *)NULL)->iov_base)
721 != sizeof(((struct kvec *)NULL)->iov_base));
722 BUILD_BUG_ON(sizeof(((struct iovec *)NULL)->iov_len)
723 != sizeof(((struct kvec *)NULL)->iov_len));
726 err = __vringh_iov(vrh, *head, (struct vringh_kiov *)riov,
727 (struct vringh_kiov *)wiov,
728 range_check, getrange, GFP_KERNEL, copydesc_user);
734 EXPORT_SYMBOL(vringh_getdesc_user);
737 * vringh_iov_pull_user - copy bytes from vring_iov.
738 * @riov: the riov as passed to vringh_getdesc_user() (updated as we consume)
739 * @dst: the place to copy.
740 * @len: the maximum length to copy.
742 * Returns the bytes copied <= len or a negative errno.
744 ssize_t vringh_iov_pull_user(struct vringh_iov *riov, void *dst, size_t len)
746 return vringh_iov_xfer(NULL, (struct vringh_kiov *)riov,
747 dst, len, xfer_from_user);
749 EXPORT_SYMBOL(vringh_iov_pull_user);
752 * vringh_iov_push_user - copy bytes into vring_iov.
753 * @wiov: the wiov as passed to vringh_getdesc_user() (updated as we consume)
754 * @src: the place to copy from.
755 * @len: the maximum length to copy.
757 * Returns the bytes copied <= len or a negative errno.
759 ssize_t vringh_iov_push_user(struct vringh_iov *wiov,
760 const void *src, size_t len)
762 return vringh_iov_xfer(NULL, (struct vringh_kiov *)wiov,
763 (void *)src, len, xfer_to_user);
765 EXPORT_SYMBOL(vringh_iov_push_user);
768 * vringh_abandon_user - we've decided not to handle the descriptor(s).
770 * @num: the number of descriptors to put back (ie. num
771 * vringh_get_user() to undo).
773 * The next vringh_get_user() will return the old descriptor(s) again.
775 void vringh_abandon_user(struct vringh *vrh, unsigned int num)
777 /* We only update vring_avail_event(vr) when we want to be notified,
778 * so we haven't changed that yet. */
779 vrh->last_avail_idx -= num;
781 EXPORT_SYMBOL(vringh_abandon_user);
784 * vringh_complete_user - we've finished with descriptor, publish it.
786 * @head: the head as filled in by vringh_getdesc_user.
787 * @len: the length of data we have written.
789 * You should check vringh_need_notify_user() after one or more calls
792 int vringh_complete_user(struct vringh *vrh, u16 head, u32 len)
794 struct vring_used_elem used;
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);
800 EXPORT_SYMBOL(vringh_complete_user);
803 * vringh_complete_multi_user - we've finished with many descriptors.
805 * @used: the head, length pairs.
806 * @num_used: the number of used elements.
808 * You should check vringh_need_notify_user() after one or more calls
811 int vringh_complete_multi_user(struct vringh *vrh,
812 const struct vring_used_elem used[],
815 return __vringh_complete(vrh, used, num_used,
816 putu16_user, putused_user);
818 EXPORT_SYMBOL(vringh_complete_multi_user);
821 * vringh_notify_enable_user - we want to know if something changes.
824 * This always enables notifications, but returns false if there are
825 * now more buffers available in the vring.
827 bool vringh_notify_enable_user(struct vringh *vrh)
829 return __vringh_notify_enable(vrh, getu16_user, putu16_user);
831 EXPORT_SYMBOL(vringh_notify_enable_user);
834 * vringh_notify_disable_user - don't tell us if something changes.
837 * This is our normal running state: we disable and then only enable when
838 * we're going to sleep.
840 void vringh_notify_disable_user(struct vringh *vrh)
842 __vringh_notify_disable(vrh, putu16_user);
844 EXPORT_SYMBOL(vringh_notify_disable_user);
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.
850 * Returns -errno or 0 if we don't need to tell the other side, 1 if we do.
852 int vringh_need_notify_user(struct vringh *vrh)
854 return __vringh_need_notify(vrh, getu16_user);
856 EXPORT_SYMBOL(vringh_need_notify_user);
858 /* Kernelspace access helpers. */
859 static inline int getu16_kern(const struct vringh *vrh,
860 u16 *val, const __virtio16 *p)
862 *val = vringh16_to_cpu(vrh, READ_ONCE(*p));
866 static inline int putu16_kern(const struct vringh *vrh, __virtio16 *p, u16 val)
868 WRITE_ONCE(*p, cpu_to_vringh16(vrh, val));
872 static inline int copydesc_kern(const struct vringh *vrh,
873 void *dst, const void *src, size_t len)
875 memcpy(dst, src, len);
879 static inline int putused_kern(const struct vringh *vrh,
880 struct vring_used_elem *dst,
881 const struct vring_used_elem *src,
884 memcpy(dst, src, num * sizeof(*dst));
888 static inline int xfer_kern(const struct vringh *vrh, void *src,
889 void *dst, size_t len)
891 memcpy(dst, src, len);
895 static inline int kern_xfer(const struct vringh *vrh, void *dst,
896 void *src, size_t len)
898 memcpy(dst, src, len);
903 * vringh_init_kern - initialize a vringh for a kernelspace vring.
904 * @vrh: the vringh to initialize.
905 * @features: the feature bits for this ring.
906 * @num: the number of elements.
907 * @weak_barriers: true if we only need memory barriers, not I/O.
908 * @desc: the userpace descriptor pointer.
909 * @avail: the userpace avail pointer.
910 * @used: the userpace used pointer.
912 * Returns an error if num is invalid.
914 int vringh_init_kern(struct vringh *vrh, u64 features,
915 unsigned int num, bool weak_barriers,
916 struct vring_desc *desc,
917 struct vring_avail *avail,
918 struct vring_used *used)
920 /* Sane power of 2 please! */
921 if (!num || num > 0xffff || (num & (num - 1))) {
922 vringh_bad("Bad ring size %u", num);
926 vrh->little_endian = (features & (1ULL << VIRTIO_F_VERSION_1));
927 vrh->event_indices = (features & (1 << VIRTIO_RING_F_EVENT_IDX));
928 vrh->weak_barriers = weak_barriers;
930 vrh->last_avail_idx = 0;
931 vrh->last_used_idx = 0;
932 vrh->vring.num = num;
933 vrh->vring.desc = desc;
934 vrh->vring.avail = avail;
935 vrh->vring.used = used;
938 EXPORT_SYMBOL(vringh_init_kern);
941 * vringh_getdesc_kern - get next available descriptor from kernelspace ring.
942 * @vrh: the kernelspace vring.
943 * @riov: where to put the readable descriptors (or NULL)
944 * @wiov: where to put the writable descriptors (or NULL)
945 * @head: head index we received, for passing to vringh_complete_kern().
946 * @gfp: flags for allocating larger riov/wiov.
948 * Returns 0 if there was no descriptor, 1 if there was, or -errno.
950 * Note that on error return, you can tell the difference between an
951 * invalid ring and a single invalid descriptor: in the former case,
952 * *head will be vrh->vring.num. You may be able to ignore an invalid
953 * descriptor, but there's not much you can do with an invalid ring.
955 * Note that you can reuse riov and wiov with subsequent calls. Content is
956 * overwritten and memory reallocated if more space is needed.
957 * When you don't have to use riov and wiov anymore, you should clean up them
958 * calling vringh_kiov_cleanup() to release the memory, even on error!
960 int vringh_getdesc_kern(struct vringh *vrh,
961 struct vringh_kiov *riov,
962 struct vringh_kiov *wiov,
968 err = __vringh_get_head(vrh, getu16_kern, &vrh->last_avail_idx);
973 if (err == vrh->vring.num)
977 err = __vringh_iov(vrh, *head, riov, wiov, no_range_check, NULL,
984 EXPORT_SYMBOL(vringh_getdesc_kern);
987 * vringh_iov_pull_kern - copy bytes from vring_iov.
988 * @riov: the riov as passed to vringh_getdesc_kern() (updated as we consume)
989 * @dst: the place to copy.
990 * @len: the maximum length to copy.
992 * Returns the bytes copied <= len or a negative errno.
994 ssize_t vringh_iov_pull_kern(struct vringh_kiov *riov, void *dst, size_t len)
996 return vringh_iov_xfer(NULL, riov, dst, len, xfer_kern);
998 EXPORT_SYMBOL(vringh_iov_pull_kern);
1001 * vringh_iov_push_kern - copy bytes into vring_iov.
1002 * @wiov: the wiov as passed to vringh_getdesc_kern() (updated as we consume)
1003 * @src: the place to copy from.
1004 * @len: the maximum length to copy.
1006 * Returns the bytes copied <= len or a negative errno.
1008 ssize_t vringh_iov_push_kern(struct vringh_kiov *wiov,
1009 const void *src, size_t len)
1011 return vringh_iov_xfer(NULL, wiov, (void *)src, len, kern_xfer);
1013 EXPORT_SYMBOL(vringh_iov_push_kern);
1016 * vringh_abandon_kern - we've decided not to handle the descriptor(s).
1018 * @num: the number of descriptors to put back (ie. num
1019 * vringh_get_kern() to undo).
1021 * The next vringh_get_kern() will return the old descriptor(s) again.
1023 void vringh_abandon_kern(struct vringh *vrh, unsigned int num)
1025 /* We only update vring_avail_event(vr) when we want to be notified,
1026 * so we haven't changed that yet. */
1027 vrh->last_avail_idx -= num;
1029 EXPORT_SYMBOL(vringh_abandon_kern);
1032 * vringh_complete_kern - we've finished with descriptor, publish it.
1034 * @head: the head as filled in by vringh_getdesc_kern.
1035 * @len: the length of data we have written.
1037 * You should check vringh_need_notify_kern() after one or more calls
1040 int vringh_complete_kern(struct vringh *vrh, u16 head, u32 len)
1042 struct vring_used_elem used;
1044 used.id = cpu_to_vringh32(vrh, head);
1045 used.len = cpu_to_vringh32(vrh, len);
1047 return __vringh_complete(vrh, &used, 1, putu16_kern, putused_kern);
1049 EXPORT_SYMBOL(vringh_complete_kern);
1052 * vringh_notify_enable_kern - we want to know if something changes.
1055 * This always enables notifications, but returns false if there are
1056 * now more buffers available in the vring.
1058 bool vringh_notify_enable_kern(struct vringh *vrh)
1060 return __vringh_notify_enable(vrh, getu16_kern, putu16_kern);
1062 EXPORT_SYMBOL(vringh_notify_enable_kern);
1065 * vringh_notify_disable_kern - don't tell us if something changes.
1068 * This is our normal running state: we disable and then only enable when
1069 * we're going to sleep.
1071 void vringh_notify_disable_kern(struct vringh *vrh)
1073 __vringh_notify_disable(vrh, putu16_kern);
1075 EXPORT_SYMBOL(vringh_notify_disable_kern);
1078 * vringh_need_notify_kern - must we tell the other side about used buffers?
1079 * @vrh: the vring we've called vringh_complete_kern() on.
1081 * Returns -errno or 0 if we don't need to tell the other side, 1 if we do.
1083 int vringh_need_notify_kern(struct vringh *vrh)
1085 return __vringh_need_notify(vrh, getu16_kern);
1087 EXPORT_SYMBOL(vringh_need_notify_kern);
1089 #if IS_REACHABLE(CONFIG_VHOST_IOTLB)
1091 static int iotlb_translate(const struct vringh *vrh,
1092 u64 addr, u64 len, struct bio_vec iov[],
1093 int iov_size, u32 perm)
1095 struct vhost_iotlb_map *map;
1096 struct vhost_iotlb *iotlb = vrh->iotlb;
1100 spin_lock(vrh->iotlb_lock);
1105 if (unlikely(ret >= iov_size)) {
1110 map = vhost_iotlb_itree_first(iotlb, addr,
1112 if (!map || map->start > addr) {
1115 } else if (!(map->perm & perm)) {
1120 size = map->size - addr + map->start;
1121 pa = map->addr + addr - map->start;
1122 pfn = pa >> PAGE_SHIFT;
1123 iov[ret].bv_page = pfn_to_page(pfn);
1124 iov[ret].bv_len = min(len - s, size);
1125 iov[ret].bv_offset = pa & (PAGE_SIZE - 1);
1131 spin_unlock(vrh->iotlb_lock);
1136 static inline int copy_from_iotlb(const struct vringh *vrh, void *dst,
1137 void *src, size_t len)
1139 struct iov_iter iter;
1140 struct bio_vec iov[16];
1143 ret = iotlb_translate(vrh, (u64)(uintptr_t)src,
1144 len, iov, 16, VHOST_MAP_RO);
1148 iov_iter_bvec(&iter, READ, iov, ret, len);
1150 ret = copy_from_iter(dst, len, &iter);
1155 static inline int copy_to_iotlb(const struct vringh *vrh, void *dst,
1156 void *src, size_t len)
1158 struct iov_iter iter;
1159 struct bio_vec iov[16];
1162 ret = iotlb_translate(vrh, (u64)(uintptr_t)dst,
1163 len, iov, 16, VHOST_MAP_WO);
1167 iov_iter_bvec(&iter, WRITE, iov, ret, len);
1169 return copy_to_iter(src, len, &iter);
1172 static inline int getu16_iotlb(const struct vringh *vrh,
1173 u16 *val, const __virtio16 *p)
1179 /* Atomic read is needed for getu16 */
1180 ret = iotlb_translate(vrh, (u64)(uintptr_t)p, sizeof(*p),
1181 &iov, 1, VHOST_MAP_RO);
1185 kaddr = kmap_atomic(iov.bv_page);
1186 from = kaddr + iov.bv_offset;
1187 *val = vringh16_to_cpu(vrh, READ_ONCE(*(__virtio16 *)from));
1188 kunmap_atomic(kaddr);
1193 static inline int putu16_iotlb(const struct vringh *vrh,
1194 __virtio16 *p, u16 val)
1200 /* Atomic write is needed for putu16 */
1201 ret = iotlb_translate(vrh, (u64)(uintptr_t)p, sizeof(*p),
1202 &iov, 1, VHOST_MAP_WO);
1206 kaddr = kmap_atomic(iov.bv_page);
1207 to = kaddr + iov.bv_offset;
1208 WRITE_ONCE(*(__virtio16 *)to, cpu_to_vringh16(vrh, val));
1209 kunmap_atomic(kaddr);
1214 static inline int copydesc_iotlb(const struct vringh *vrh,
1215 void *dst, const void *src, size_t len)
1219 ret = copy_from_iotlb(vrh, dst, (void *)src, len);
1226 static inline int xfer_from_iotlb(const struct vringh *vrh, void *src,
1227 void *dst, size_t len)
1231 ret = copy_from_iotlb(vrh, dst, src, len);
1238 static inline int xfer_to_iotlb(const struct vringh *vrh,
1239 void *dst, void *src, size_t len)
1243 ret = copy_to_iotlb(vrh, dst, src, len);
1250 static inline int putused_iotlb(const struct vringh *vrh,
1251 struct vring_used_elem *dst,
1252 const struct vring_used_elem *src,
1255 int size = num * sizeof(*dst);
1258 ret = copy_to_iotlb(vrh, dst, (void *)src, num * sizeof(*dst));
1266 * vringh_init_iotlb - initialize a vringh for a ring with IOTLB.
1267 * @vrh: the vringh to initialize.
1268 * @features: the feature bits for this ring.
1269 * @num: the number of elements.
1270 * @weak_barriers: true if we only need memory barriers, not I/O.
1271 * @desc: the userpace descriptor pointer.
1272 * @avail: the userpace avail pointer.
1273 * @used: the userpace used pointer.
1275 * Returns an error if num is invalid.
1277 int vringh_init_iotlb(struct vringh *vrh, u64 features,
1278 unsigned int num, bool weak_barriers,
1279 struct vring_desc *desc,
1280 struct vring_avail *avail,
1281 struct vring_used *used)
1283 return vringh_init_kern(vrh, features, num, weak_barriers,
1286 EXPORT_SYMBOL(vringh_init_iotlb);
1289 * vringh_set_iotlb - initialize a vringh for a ring with IOTLB.
1291 * @iotlb: iotlb associated with this vring
1292 * @iotlb_lock: spinlock to synchronize the iotlb accesses
1294 void vringh_set_iotlb(struct vringh *vrh, struct vhost_iotlb *iotlb,
1295 spinlock_t *iotlb_lock)
1298 vrh->iotlb_lock = iotlb_lock;
1300 EXPORT_SYMBOL(vringh_set_iotlb);
1303 * vringh_getdesc_iotlb - get next available descriptor from ring with
1305 * @vrh: the kernelspace vring.
1306 * @riov: where to put the readable descriptors (or NULL)
1307 * @wiov: where to put the writable descriptors (or NULL)
1308 * @head: head index we received, for passing to vringh_complete_iotlb().
1309 * @gfp: flags for allocating larger riov/wiov.
1311 * Returns 0 if there was no descriptor, 1 if there was, or -errno.
1313 * Note that on error return, you can tell the difference between an
1314 * invalid ring and a single invalid descriptor: in the former case,
1315 * *head will be vrh->vring.num. You may be able to ignore an invalid
1316 * descriptor, but there's not much you can do with an invalid ring.
1318 * Note that you can reuse riov and wiov with subsequent calls. Content is
1319 * overwritten and memory reallocated if more space is needed.
1320 * When you don't have to use riov and wiov anymore, you should clean up them
1321 * calling vringh_kiov_cleanup() to release the memory, even on error!
1323 int vringh_getdesc_iotlb(struct vringh *vrh,
1324 struct vringh_kiov *riov,
1325 struct vringh_kiov *wiov,
1331 err = __vringh_get_head(vrh, getu16_iotlb, &vrh->last_avail_idx);
1336 if (err == vrh->vring.num)
1340 err = __vringh_iov(vrh, *head, riov, wiov, no_range_check, NULL,
1341 gfp, copydesc_iotlb);
1347 EXPORT_SYMBOL(vringh_getdesc_iotlb);
1350 * vringh_iov_pull_iotlb - copy bytes from vring_iov.
1352 * @riov: the riov as passed to vringh_getdesc_iotlb() (updated as we consume)
1353 * @dst: the place to copy.
1354 * @len: the maximum length to copy.
1356 * Returns the bytes copied <= len or a negative errno.
1358 ssize_t vringh_iov_pull_iotlb(struct vringh *vrh,
1359 struct vringh_kiov *riov,
1360 void *dst, size_t len)
1362 return vringh_iov_xfer(vrh, riov, dst, len, xfer_from_iotlb);
1364 EXPORT_SYMBOL(vringh_iov_pull_iotlb);
1367 * vringh_iov_push_iotlb - copy bytes into vring_iov.
1369 * @wiov: the wiov as passed to vringh_getdesc_iotlb() (updated as we consume)
1370 * @src: the place to copy from.
1371 * @len: the maximum length to copy.
1373 * Returns the bytes copied <= len or a negative errno.
1375 ssize_t vringh_iov_push_iotlb(struct vringh *vrh,
1376 struct vringh_kiov *wiov,
1377 const void *src, size_t len)
1379 return vringh_iov_xfer(vrh, wiov, (void *)src, len, xfer_to_iotlb);
1381 EXPORT_SYMBOL(vringh_iov_push_iotlb);
1384 * vringh_abandon_iotlb - we've decided not to handle the descriptor(s).
1386 * @num: the number of descriptors to put back (ie. num
1387 * vringh_get_iotlb() to undo).
1389 * The next vringh_get_iotlb() will return the old descriptor(s) again.
1391 void vringh_abandon_iotlb(struct vringh *vrh, unsigned int num)
1393 /* We only update vring_avail_event(vr) when we want to be notified,
1394 * so we haven't changed that yet.
1396 vrh->last_avail_idx -= num;
1398 EXPORT_SYMBOL(vringh_abandon_iotlb);
1401 * vringh_complete_iotlb - we've finished with descriptor, publish it.
1403 * @head: the head as filled in by vringh_getdesc_iotlb.
1404 * @len: the length of data we have written.
1406 * You should check vringh_need_notify_iotlb() after one or more calls
1409 int vringh_complete_iotlb(struct vringh *vrh, u16 head, u32 len)
1411 struct vring_used_elem used;
1413 used.id = cpu_to_vringh32(vrh, head);
1414 used.len = cpu_to_vringh32(vrh, len);
1416 return __vringh_complete(vrh, &used, 1, putu16_iotlb, putused_iotlb);
1418 EXPORT_SYMBOL(vringh_complete_iotlb);
1421 * vringh_notify_enable_iotlb - we want to know if something changes.
1424 * This always enables notifications, but returns false if there are
1425 * now more buffers available in the vring.
1427 bool vringh_notify_enable_iotlb(struct vringh *vrh)
1429 return __vringh_notify_enable(vrh, getu16_iotlb, putu16_iotlb);
1431 EXPORT_SYMBOL(vringh_notify_enable_iotlb);
1434 * vringh_notify_disable_iotlb - don't tell us if something changes.
1437 * This is our normal running state: we disable and then only enable when
1438 * we're going to sleep.
1440 void vringh_notify_disable_iotlb(struct vringh *vrh)
1442 __vringh_notify_disable(vrh, putu16_iotlb);
1444 EXPORT_SYMBOL(vringh_notify_disable_iotlb);
1447 * vringh_need_notify_iotlb - must we tell the other side about used buffers?
1448 * @vrh: the vring we've called vringh_complete_iotlb() on.
1450 * Returns -errno or 0 if we don't need to tell the other side, 1 if we do.
1452 int vringh_need_notify_iotlb(struct vringh *vrh)
1454 return __vringh_need_notify(vrh, getu16_iotlb);
1456 EXPORT_SYMBOL(vringh_need_notify_iotlb);
1460 MODULE_LICENSE("GPL");