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, 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,
334 if (count++ == vrh->vring.num) {
335 vringh_bad("Descriptor loop in %p", descs);
340 if (desc.flags & cpu_to_vringh16(vrh, VRING_DESC_F_WRITE))
344 if (unlikely(wiov && wiov->i)) {
345 vringh_bad("Readable desc %p after writable",
353 vringh_bad("Unexpected %s desc",
354 !wiov ? "writable" : "readable");
360 /* Make sure it's OK, and get offset. */
361 len = vringh32_to_cpu(vrh, desc.len);
362 if (!rcheck(vrh, vringh64_to_cpu(vrh, desc.addr), &len, &range,
367 addr = (void *)(unsigned long)(vringh64_to_cpu(vrh, desc.addr) +
370 if (unlikely(iov->used == (iov->max_num & ~VRINGH_IOV_ALLOCATED))) {
371 err = resize_iovec(iov, gfp);
376 iov->iov[iov->used].iov_base = addr;
377 iov->iov[iov->used].iov_len = len;
380 if (unlikely(len != vringh32_to_cpu(vrh, desc.len))) {
381 desc.len = cpu_to_vringh32(vrh,
382 vringh32_to_cpu(vrh, desc.len) - len);
383 desc.addr = cpu_to_vringh64(vrh,
384 vringh64_to_cpu(vrh, desc.addr) + len);
388 if (desc.flags & cpu_to_vringh16(vrh, VRING_DESC_F_NEXT)) {
389 i = vringh16_to_cpu(vrh, desc.next);
391 /* Just in case we need to finish traversing above. */
392 if (unlikely(up_next > 0)) {
393 i = return_from_indirect(vrh, &up_next,
401 vringh_bad("Chained index %u > %u", i, desc_max);
413 static inline int __vringh_complete(struct vringh *vrh,
414 const struct vring_used_elem *used,
415 unsigned int num_used,
416 int (*putu16)(const struct vringh *vrh,
417 __virtio16 *p, u16 val),
418 int (*putused)(const struct vringh *vrh,
419 struct vring_used_elem *dst,
420 const struct vring_used_elem
423 struct vring_used *used_ring;
427 used_ring = vrh->vring.used;
428 used_idx = vrh->last_used_idx + vrh->completed;
430 off = used_idx % vrh->vring.num;
432 /* Compiler knows num_used == 1 sometimes, hence extra check */
433 if (num_used > 1 && unlikely(off + num_used >= vrh->vring.num)) {
434 u16 part = vrh->vring.num - off;
435 err = putused(vrh, &used_ring->ring[off], used, part);
437 err = putused(vrh, &used_ring->ring[0], used + part,
440 err = putused(vrh, &used_ring->ring[off], used, num_used);
443 vringh_bad("Failed to write %u used entries %u at %p",
444 num_used, off, &used_ring->ring[off]);
448 /* Make sure buffer is written before we update index. */
449 virtio_wmb(vrh->weak_barriers);
451 err = putu16(vrh, &vrh->vring.used->idx, used_idx + num_used);
453 vringh_bad("Failed to update used index at %p",
454 &vrh->vring.used->idx);
458 vrh->completed += num_used;
463 static inline int __vringh_need_notify(struct vringh *vrh,
464 int (*getu16)(const struct vringh *vrh,
466 const __virtio16 *p))
472 /* Flush out used index update. This is paired with the
473 * barrier that the Guest executes when enabling
475 virtio_mb(vrh->weak_barriers);
477 /* Old-style, without event indices. */
478 if (!vrh->event_indices) {
480 err = getu16(vrh, &flags, &vrh->vring.avail->flags);
482 vringh_bad("Failed to get flags at %p",
483 &vrh->vring.avail->flags);
486 return (!(flags & VRING_AVAIL_F_NO_INTERRUPT));
489 /* Modern: we know when other side wants to know. */
490 err = getu16(vrh, &used_event, &vring_used_event(&vrh->vring));
492 vringh_bad("Failed to get used event idx at %p",
493 &vring_used_event(&vrh->vring));
497 /* Just in case we added so many that we wrap. */
498 if (unlikely(vrh->completed > 0xffff))
501 notify = vring_need_event(used_event,
502 vrh->last_used_idx + vrh->completed,
505 vrh->last_used_idx += vrh->completed;
510 static inline bool __vringh_notify_enable(struct vringh *vrh,
511 int (*getu16)(const struct vringh *vrh,
512 u16 *val, const __virtio16 *p),
513 int (*putu16)(const struct vringh *vrh,
514 __virtio16 *p, u16 val))
518 if (!vrh->event_indices) {
519 /* Old-school; update flags. */
520 if (putu16(vrh, &vrh->vring.used->flags, 0) != 0) {
521 vringh_bad("Clearing used flags %p",
522 &vrh->vring.used->flags);
526 if (putu16(vrh, &vring_avail_event(&vrh->vring),
527 vrh->last_avail_idx) != 0) {
528 vringh_bad("Updating avail event index %p",
529 &vring_avail_event(&vrh->vring));
534 /* They could have slipped one in as we were doing that: make
535 * sure it's written, then check again. */
536 virtio_mb(vrh->weak_barriers);
538 if (getu16(vrh, &avail, &vrh->vring.avail->idx) != 0) {
539 vringh_bad("Failed to check avail idx at %p",
540 &vrh->vring.avail->idx);
544 /* This is unlikely, so we just leave notifications enabled
545 * (if we're using event_indices, we'll only get one
546 * notification anyway). */
547 return avail == vrh->last_avail_idx;
550 static inline void __vringh_notify_disable(struct vringh *vrh,
551 int (*putu16)(const struct vringh *vrh,
552 __virtio16 *p, u16 val))
554 if (!vrh->event_indices) {
555 /* Old-school; update flags. */
556 if (putu16(vrh, &vrh->vring.used->flags,
557 VRING_USED_F_NO_NOTIFY)) {
558 vringh_bad("Setting used flags %p",
559 &vrh->vring.used->flags);
564 /* Userspace access helpers: in this case, addresses are really userspace. */
565 static inline int getu16_user(const struct vringh *vrh, u16 *val, const __virtio16 *p)
568 int rc = get_user(v, (__force __virtio16 __user *)p);
569 *val = vringh16_to_cpu(vrh, v);
573 static inline int putu16_user(const struct vringh *vrh, __virtio16 *p, u16 val)
575 __virtio16 v = cpu_to_vringh16(vrh, val);
576 return put_user(v, (__force __virtio16 __user *)p);
579 static inline int copydesc_user(const struct vringh *vrh,
580 void *dst, const void *src, size_t len)
582 return copy_from_user(dst, (__force void __user *)src, len) ?
586 static inline int putused_user(const struct vringh *vrh,
587 struct vring_used_elem *dst,
588 const struct vring_used_elem *src,
591 return copy_to_user((__force void __user *)dst, src,
592 sizeof(*dst) * num) ? -EFAULT : 0;
595 static inline int xfer_from_user(const struct vringh *vrh, void *src,
596 void *dst, size_t len)
598 return copy_from_user(dst, (__force void __user *)src, len) ?
602 static inline int xfer_to_user(const struct vringh *vrh,
603 void *dst, void *src, size_t len)
605 return copy_to_user((__force void __user *)dst, src, len) ?
610 * vringh_init_user - initialize a vringh for a userspace vring.
611 * @vrh: the vringh to initialize.
612 * @features: the feature bits for this ring.
613 * @num: the number of elements.
614 * @weak_barriers: true if we only need memory barriers, not I/O.
615 * @desc: the userpace descriptor pointer.
616 * @avail: the userpace avail pointer.
617 * @used: the userpace used pointer.
619 * Returns an error if num is invalid: you should check pointers
622 int vringh_init_user(struct vringh *vrh, u64 features,
623 unsigned int num, bool weak_barriers,
624 vring_desc_t __user *desc,
625 vring_avail_t __user *avail,
626 vring_used_t __user *used)
628 /* Sane power of 2 please! */
629 if (!num || num > 0xffff || (num & (num - 1))) {
630 vringh_bad("Bad ring size %u", num);
634 vrh->little_endian = (features & (1ULL << VIRTIO_F_VERSION_1));
635 vrh->event_indices = (features & (1 << VIRTIO_RING_F_EVENT_IDX));
636 vrh->weak_barriers = weak_barriers;
638 vrh->last_avail_idx = 0;
639 vrh->last_used_idx = 0;
640 vrh->vring.num = num;
641 /* vring expects kernel addresses, but only used via accessors. */
642 vrh->vring.desc = (__force struct vring_desc *)desc;
643 vrh->vring.avail = (__force struct vring_avail *)avail;
644 vrh->vring.used = (__force struct vring_used *)used;
647 EXPORT_SYMBOL(vringh_init_user);
650 * vringh_getdesc_user - get next available descriptor from userspace ring.
651 * @vrh: the userspace vring.
652 * @riov: where to put the readable descriptors (or NULL)
653 * @wiov: where to put the writable descriptors (or NULL)
654 * @getrange: function to call to check ranges.
655 * @head: head index we received, for passing to vringh_complete_user().
657 * Returns 0 if there was no descriptor, 1 if there was, or -errno.
659 * Note that on error return, you can tell the difference between an
660 * invalid ring and a single invalid descriptor: in the former case,
661 * *head will be vrh->vring.num. You may be able to ignore an invalid
662 * descriptor, but there's not much you can do with an invalid ring.
664 * Note that you may need to clean up riov and wiov, even on error!
666 int vringh_getdesc_user(struct vringh *vrh,
667 struct vringh_iov *riov,
668 struct vringh_iov *wiov,
669 bool (*getrange)(struct vringh *vrh,
670 u64 addr, struct vringh_range *r),
675 *head = vrh->vring.num;
676 err = __vringh_get_head(vrh, getu16_user, &vrh->last_avail_idx);
681 if (err == vrh->vring.num)
684 /* We need the layouts to be the identical for this to work */
685 BUILD_BUG_ON(sizeof(struct vringh_kiov) != sizeof(struct vringh_iov));
686 BUILD_BUG_ON(offsetof(struct vringh_kiov, iov) !=
687 offsetof(struct vringh_iov, iov));
688 BUILD_BUG_ON(offsetof(struct vringh_kiov, i) !=
689 offsetof(struct vringh_iov, i));
690 BUILD_BUG_ON(offsetof(struct vringh_kiov, used) !=
691 offsetof(struct vringh_iov, used));
692 BUILD_BUG_ON(offsetof(struct vringh_kiov, max_num) !=
693 offsetof(struct vringh_iov, max_num));
694 BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
695 BUILD_BUG_ON(offsetof(struct iovec, iov_base) !=
696 offsetof(struct kvec, iov_base));
697 BUILD_BUG_ON(offsetof(struct iovec, iov_len) !=
698 offsetof(struct kvec, iov_len));
699 BUILD_BUG_ON(sizeof(((struct iovec *)NULL)->iov_base)
700 != sizeof(((struct kvec *)NULL)->iov_base));
701 BUILD_BUG_ON(sizeof(((struct iovec *)NULL)->iov_len)
702 != sizeof(((struct kvec *)NULL)->iov_len));
705 err = __vringh_iov(vrh, *head, (struct vringh_kiov *)riov,
706 (struct vringh_kiov *)wiov,
707 range_check, getrange, GFP_KERNEL, copydesc_user);
713 EXPORT_SYMBOL(vringh_getdesc_user);
716 * vringh_iov_pull_user - copy bytes from vring_iov.
717 * @riov: the riov as passed to vringh_getdesc_user() (updated as we consume)
718 * @dst: the place to copy.
719 * @len: the maximum length to copy.
721 * Returns the bytes copied <= len or a negative errno.
723 ssize_t vringh_iov_pull_user(struct vringh_iov *riov, void *dst, size_t len)
725 return vringh_iov_xfer(NULL, (struct vringh_kiov *)riov,
726 dst, len, xfer_from_user);
728 EXPORT_SYMBOL(vringh_iov_pull_user);
731 * vringh_iov_push_user - copy bytes into vring_iov.
732 * @wiov: the wiov as passed to vringh_getdesc_user() (updated as we consume)
733 * @dst: the place to copy.
734 * @len: the maximum length to copy.
736 * Returns the bytes copied <= len or a negative errno.
738 ssize_t vringh_iov_push_user(struct vringh_iov *wiov,
739 const void *src, size_t len)
741 return vringh_iov_xfer(NULL, (struct vringh_kiov *)wiov,
742 (void *)src, len, xfer_to_user);
744 EXPORT_SYMBOL(vringh_iov_push_user);
747 * vringh_abandon_user - we've decided not to handle the descriptor(s).
749 * @num: the number of descriptors to put back (ie. num
750 * vringh_get_user() to undo).
752 * The next vringh_get_user() will return the old descriptor(s) again.
754 void vringh_abandon_user(struct vringh *vrh, unsigned int num)
756 /* We only update vring_avail_event(vr) when we want to be notified,
757 * so we haven't changed that yet. */
758 vrh->last_avail_idx -= num;
760 EXPORT_SYMBOL(vringh_abandon_user);
763 * vringh_complete_user - we've finished with descriptor, publish it.
765 * @head: the head as filled in by vringh_getdesc_user.
766 * @len: the length of data we have written.
768 * You should check vringh_need_notify_user() after one or more calls
771 int vringh_complete_user(struct vringh *vrh, u16 head, u32 len)
773 struct vring_used_elem used;
775 used.id = cpu_to_vringh32(vrh, head);
776 used.len = cpu_to_vringh32(vrh, len);
777 return __vringh_complete(vrh, &used, 1, putu16_user, putused_user);
779 EXPORT_SYMBOL(vringh_complete_user);
782 * vringh_complete_multi_user - we've finished with many descriptors.
784 * @used: the head, length pairs.
785 * @num_used: the number of used elements.
787 * You should check vringh_need_notify_user() after one or more calls
790 int vringh_complete_multi_user(struct vringh *vrh,
791 const struct vring_used_elem used[],
794 return __vringh_complete(vrh, used, num_used,
795 putu16_user, putused_user);
797 EXPORT_SYMBOL(vringh_complete_multi_user);
800 * vringh_notify_enable_user - we want to know if something changes.
803 * This always enables notifications, but returns false if there are
804 * now more buffers available in the vring.
806 bool vringh_notify_enable_user(struct vringh *vrh)
808 return __vringh_notify_enable(vrh, getu16_user, putu16_user);
810 EXPORT_SYMBOL(vringh_notify_enable_user);
813 * vringh_notify_disable_user - don't tell us if something changes.
816 * This is our normal running state: we disable and then only enable when
817 * we're going to sleep.
819 void vringh_notify_disable_user(struct vringh *vrh)
821 __vringh_notify_disable(vrh, putu16_user);
823 EXPORT_SYMBOL(vringh_notify_disable_user);
826 * vringh_need_notify_user - must we tell the other side about used buffers?
827 * @vrh: the vring we've called vringh_complete_user() on.
829 * Returns -errno or 0 if we don't need to tell the other side, 1 if we do.
831 int vringh_need_notify_user(struct vringh *vrh)
833 return __vringh_need_notify(vrh, getu16_user);
835 EXPORT_SYMBOL(vringh_need_notify_user);
837 /* Kernelspace access helpers. */
838 static inline int getu16_kern(const struct vringh *vrh,
839 u16 *val, const __virtio16 *p)
841 *val = vringh16_to_cpu(vrh, READ_ONCE(*p));
845 static inline int putu16_kern(const struct vringh *vrh, __virtio16 *p, u16 val)
847 WRITE_ONCE(*p, cpu_to_vringh16(vrh, val));
851 static inline int copydesc_kern(const struct vringh *vrh,
852 void *dst, const void *src, size_t len)
854 memcpy(dst, src, len);
858 static inline int putused_kern(const struct vringh *vrh,
859 struct vring_used_elem *dst,
860 const struct vring_used_elem *src,
863 memcpy(dst, src, num * sizeof(*dst));
867 static inline int xfer_kern(const struct vringh *vrh, void *src,
868 void *dst, size_t len)
870 memcpy(dst, src, len);
874 static inline int kern_xfer(const struct vringh *vrh, void *dst,
875 void *src, size_t len)
877 memcpy(dst, src, len);
882 * vringh_init_kern - initialize a vringh for a kernelspace vring.
883 * @vrh: the vringh to initialize.
884 * @features: the feature bits for this ring.
885 * @num: the number of elements.
886 * @weak_barriers: true if we only need memory barriers, not I/O.
887 * @desc: the userpace descriptor pointer.
888 * @avail: the userpace avail pointer.
889 * @used: the userpace used pointer.
891 * Returns an error if num is invalid.
893 int vringh_init_kern(struct vringh *vrh, u64 features,
894 unsigned int num, bool weak_barriers,
895 struct vring_desc *desc,
896 struct vring_avail *avail,
897 struct vring_used *used)
899 /* Sane power of 2 please! */
900 if (!num || num > 0xffff || (num & (num - 1))) {
901 vringh_bad("Bad ring size %u", num);
905 vrh->little_endian = (features & (1ULL << VIRTIO_F_VERSION_1));
906 vrh->event_indices = (features & (1 << VIRTIO_RING_F_EVENT_IDX));
907 vrh->weak_barriers = weak_barriers;
909 vrh->last_avail_idx = 0;
910 vrh->last_used_idx = 0;
911 vrh->vring.num = num;
912 vrh->vring.desc = desc;
913 vrh->vring.avail = avail;
914 vrh->vring.used = used;
917 EXPORT_SYMBOL(vringh_init_kern);
920 * vringh_getdesc_kern - get next available descriptor from kernelspace ring.
921 * @vrh: the kernelspace vring.
922 * @riov: where to put the readable descriptors (or NULL)
923 * @wiov: where to put the writable descriptors (or NULL)
924 * @head: head index we received, for passing to vringh_complete_kern().
925 * @gfp: flags for allocating larger riov/wiov.
927 * Returns 0 if there was no descriptor, 1 if there was, or -errno.
929 * Note that on error return, you can tell the difference between an
930 * invalid ring and a single invalid descriptor: in the former case,
931 * *head will be vrh->vring.num. You may be able to ignore an invalid
932 * descriptor, but there's not much you can do with an invalid ring.
934 * Note that you may need to clean up riov and wiov, even on error!
936 int vringh_getdesc_kern(struct vringh *vrh,
937 struct vringh_kiov *riov,
938 struct vringh_kiov *wiov,
944 err = __vringh_get_head(vrh, getu16_kern, &vrh->last_avail_idx);
949 if (err == vrh->vring.num)
953 err = __vringh_iov(vrh, *head, riov, wiov, no_range_check, NULL,
960 EXPORT_SYMBOL(vringh_getdesc_kern);
963 * vringh_iov_pull_kern - copy bytes from vring_iov.
964 * @riov: the riov as passed to vringh_getdesc_kern() (updated as we consume)
965 * @dst: the place to copy.
966 * @len: the maximum length to copy.
968 * Returns the bytes copied <= len or a negative errno.
970 ssize_t vringh_iov_pull_kern(struct vringh_kiov *riov, void *dst, size_t len)
972 return vringh_iov_xfer(NULL, riov, dst, len, xfer_kern);
974 EXPORT_SYMBOL(vringh_iov_pull_kern);
977 * vringh_iov_push_kern - copy bytes into vring_iov.
978 * @wiov: the wiov as passed to vringh_getdesc_kern() (updated as we consume)
979 * @dst: the place to copy.
980 * @len: the maximum length to copy.
982 * Returns the bytes copied <= len or a negative errno.
984 ssize_t vringh_iov_push_kern(struct vringh_kiov *wiov,
985 const void *src, size_t len)
987 return vringh_iov_xfer(NULL, wiov, (void *)src, len, kern_xfer);
989 EXPORT_SYMBOL(vringh_iov_push_kern);
992 * vringh_abandon_kern - we've decided not to handle the descriptor(s).
994 * @num: the number of descriptors to put back (ie. num
995 * vringh_get_kern() to undo).
997 * The next vringh_get_kern() will return the old descriptor(s) again.
999 void vringh_abandon_kern(struct vringh *vrh, unsigned int num)
1001 /* We only update vring_avail_event(vr) when we want to be notified,
1002 * so we haven't changed that yet. */
1003 vrh->last_avail_idx -= num;
1005 EXPORT_SYMBOL(vringh_abandon_kern);
1008 * vringh_complete_kern - we've finished with descriptor, publish it.
1010 * @head: the head as filled in by vringh_getdesc_kern.
1011 * @len: the length of data we have written.
1013 * You should check vringh_need_notify_kern() after one or more calls
1016 int vringh_complete_kern(struct vringh *vrh, u16 head, u32 len)
1018 struct vring_used_elem used;
1020 used.id = cpu_to_vringh32(vrh, head);
1021 used.len = cpu_to_vringh32(vrh, len);
1023 return __vringh_complete(vrh, &used, 1, putu16_kern, putused_kern);
1025 EXPORT_SYMBOL(vringh_complete_kern);
1028 * vringh_notify_enable_kern - we want to know if something changes.
1031 * This always enables notifications, but returns false if there are
1032 * now more buffers available in the vring.
1034 bool vringh_notify_enable_kern(struct vringh *vrh)
1036 return __vringh_notify_enable(vrh, getu16_kern, putu16_kern);
1038 EXPORT_SYMBOL(vringh_notify_enable_kern);
1041 * vringh_notify_disable_kern - don't tell us if something changes.
1044 * This is our normal running state: we disable and then only enable when
1045 * we're going to sleep.
1047 void vringh_notify_disable_kern(struct vringh *vrh)
1049 __vringh_notify_disable(vrh, putu16_kern);
1051 EXPORT_SYMBOL(vringh_notify_disable_kern);
1054 * vringh_need_notify_kern - must we tell the other side about used buffers?
1055 * @vrh: the vring we've called vringh_complete_kern() on.
1057 * Returns -errno or 0 if we don't need to tell the other side, 1 if we do.
1059 int vringh_need_notify_kern(struct vringh *vrh)
1061 return __vringh_need_notify(vrh, getu16_kern);
1063 EXPORT_SYMBOL(vringh_need_notify_kern);
1065 #if IS_REACHABLE(CONFIG_VHOST_IOTLB)
1067 static int iotlb_translate(const struct vringh *vrh,
1068 u64 addr, u64 len, struct bio_vec iov[],
1069 int iov_size, u32 perm)
1071 struct vhost_iotlb_map *map;
1072 struct vhost_iotlb *iotlb = vrh->iotlb;
1079 if (unlikely(ret >= iov_size)) {
1084 map = vhost_iotlb_itree_first(iotlb, addr,
1086 if (!map || map->start > addr) {
1089 } else if (!(map->perm & perm)) {
1094 size = map->size - addr + map->start;
1095 pa = map->addr + addr - map->start;
1096 pfn = pa >> PAGE_SHIFT;
1097 iov[ret].bv_page = pfn_to_page(pfn);
1098 iov[ret].bv_len = min(len - s, size);
1099 iov[ret].bv_offset = pa & (PAGE_SIZE - 1);
1108 static inline int copy_from_iotlb(const struct vringh *vrh, void *dst,
1109 void *src, size_t len)
1111 struct iov_iter iter;
1112 struct bio_vec iov[16];
1115 ret = iotlb_translate(vrh, (u64)(uintptr_t)src,
1116 len, iov, 16, VHOST_MAP_RO);
1120 iov_iter_bvec(&iter, READ, iov, ret, len);
1122 ret = copy_from_iter(dst, len, &iter);
1127 static inline int copy_to_iotlb(const struct vringh *vrh, void *dst,
1128 void *src, size_t len)
1130 struct iov_iter iter;
1131 struct bio_vec iov[16];
1134 ret = iotlb_translate(vrh, (u64)(uintptr_t)dst,
1135 len, iov, 16, VHOST_MAP_WO);
1139 iov_iter_bvec(&iter, WRITE, iov, ret, len);
1141 return copy_to_iter(src, len, &iter);
1144 static inline int getu16_iotlb(const struct vringh *vrh,
1145 u16 *val, const __virtio16 *p)
1151 /* Atomic read is needed for getu16 */
1152 ret = iotlb_translate(vrh, (u64)(uintptr_t)p, sizeof(*p),
1153 &iov, 1, VHOST_MAP_RO);
1157 kaddr = kmap_atomic(iov.bv_page);
1158 from = kaddr + iov.bv_offset;
1159 *val = vringh16_to_cpu(vrh, READ_ONCE(*(__virtio16 *)from));
1160 kunmap_atomic(kaddr);
1165 static inline int putu16_iotlb(const struct vringh *vrh,
1166 __virtio16 *p, u16 val)
1172 /* Atomic write is needed for putu16 */
1173 ret = iotlb_translate(vrh, (u64)(uintptr_t)p, sizeof(*p),
1174 &iov, 1, VHOST_MAP_WO);
1178 kaddr = kmap_atomic(iov.bv_page);
1179 to = kaddr + iov.bv_offset;
1180 WRITE_ONCE(*(__virtio16 *)to, cpu_to_vringh16(vrh, val));
1181 kunmap_atomic(kaddr);
1186 static inline int copydesc_iotlb(const struct vringh *vrh,
1187 void *dst, const void *src, size_t len)
1191 ret = copy_from_iotlb(vrh, dst, (void *)src, len);
1198 static inline int xfer_from_iotlb(const struct vringh *vrh, void *src,
1199 void *dst, size_t len)
1203 ret = copy_from_iotlb(vrh, dst, src, len);
1210 static inline int xfer_to_iotlb(const struct vringh *vrh,
1211 void *dst, void *src, size_t len)
1215 ret = copy_to_iotlb(vrh, dst, src, len);
1222 static inline int putused_iotlb(const struct vringh *vrh,
1223 struct vring_used_elem *dst,
1224 const struct vring_used_elem *src,
1227 int size = num * sizeof(*dst);
1230 ret = copy_to_iotlb(vrh, dst, (void *)src, num * sizeof(*dst));
1238 * vringh_init_iotlb - initialize a vringh for a ring with IOTLB.
1239 * @vrh: the vringh to initialize.
1240 * @features: the feature bits for this ring.
1241 * @num: the number of elements.
1242 * @weak_barriers: true if we only need memory barriers, not I/O.
1243 * @desc: the userpace descriptor pointer.
1244 * @avail: the userpace avail pointer.
1245 * @used: the userpace used pointer.
1247 * Returns an error if num is invalid.
1249 int vringh_init_iotlb(struct vringh *vrh, u64 features,
1250 unsigned int num, bool weak_barriers,
1251 struct vring_desc *desc,
1252 struct vring_avail *avail,
1253 struct vring_used *used)
1255 return vringh_init_kern(vrh, features, num, weak_barriers,
1258 EXPORT_SYMBOL(vringh_init_iotlb);
1261 * vringh_set_iotlb - initialize a vringh for a ring with IOTLB.
1263 * @iotlb: iotlb associated with this vring
1265 void vringh_set_iotlb(struct vringh *vrh, struct vhost_iotlb *iotlb)
1269 EXPORT_SYMBOL(vringh_set_iotlb);
1272 * vringh_getdesc_iotlb - get next available descriptor from ring with
1274 * @vrh: the kernelspace vring.
1275 * @riov: where to put the readable descriptors (or NULL)
1276 * @wiov: where to put the writable descriptors (or NULL)
1277 * @head: head index we received, for passing to vringh_complete_iotlb().
1278 * @gfp: flags for allocating larger riov/wiov.
1280 * Returns 0 if there was no descriptor, 1 if there was, or -errno.
1282 * Note that on error return, you can tell the difference between an
1283 * invalid ring and a single invalid descriptor: in the former case,
1284 * *head will be vrh->vring.num. You may be able to ignore an invalid
1285 * descriptor, but there's not much you can do with an invalid ring.
1287 * Note that you may need to clean up riov and wiov, even on error!
1289 int vringh_getdesc_iotlb(struct vringh *vrh,
1290 struct vringh_kiov *riov,
1291 struct vringh_kiov *wiov,
1297 err = __vringh_get_head(vrh, getu16_iotlb, &vrh->last_avail_idx);
1302 if (err == vrh->vring.num)
1306 err = __vringh_iov(vrh, *head, riov, wiov, no_range_check, NULL,
1307 gfp, copydesc_iotlb);
1313 EXPORT_SYMBOL(vringh_getdesc_iotlb);
1316 * vringh_iov_pull_iotlb - copy bytes from vring_iov.
1318 * @riov: the riov as passed to vringh_getdesc_iotlb() (updated as we consume)
1319 * @dst: the place to copy.
1320 * @len: the maximum length to copy.
1322 * Returns the bytes copied <= len or a negative errno.
1324 ssize_t vringh_iov_pull_iotlb(struct vringh *vrh,
1325 struct vringh_kiov *riov,
1326 void *dst, size_t len)
1328 return vringh_iov_xfer(vrh, riov, dst, len, xfer_from_iotlb);
1330 EXPORT_SYMBOL(vringh_iov_pull_iotlb);
1333 * vringh_iov_push_iotlb - copy bytes into vring_iov.
1335 * @wiov: the wiov as passed to vringh_getdesc_iotlb() (updated as we consume)
1336 * @dst: the place to copy.
1337 * @len: the maximum length to copy.
1339 * Returns the bytes copied <= len or a negative errno.
1341 ssize_t vringh_iov_push_iotlb(struct vringh *vrh,
1342 struct vringh_kiov *wiov,
1343 const void *src, size_t len)
1345 return vringh_iov_xfer(vrh, wiov, (void *)src, len, xfer_to_iotlb);
1347 EXPORT_SYMBOL(vringh_iov_push_iotlb);
1350 * vringh_abandon_iotlb - we've decided not to handle the descriptor(s).
1352 * @num: the number of descriptors to put back (ie. num
1353 * vringh_get_iotlb() to undo).
1355 * The next vringh_get_iotlb() will return the old descriptor(s) again.
1357 void vringh_abandon_iotlb(struct vringh *vrh, unsigned int num)
1359 /* We only update vring_avail_event(vr) when we want to be notified,
1360 * so we haven't changed that yet.
1362 vrh->last_avail_idx -= num;
1364 EXPORT_SYMBOL(vringh_abandon_iotlb);
1367 * vringh_complete_iotlb - we've finished with descriptor, publish it.
1369 * @head: the head as filled in by vringh_getdesc_iotlb.
1370 * @len: the length of data we have written.
1372 * You should check vringh_need_notify_iotlb() after one or more calls
1375 int vringh_complete_iotlb(struct vringh *vrh, u16 head, u32 len)
1377 struct vring_used_elem used;
1379 used.id = cpu_to_vringh32(vrh, head);
1380 used.len = cpu_to_vringh32(vrh, len);
1382 return __vringh_complete(vrh, &used, 1, putu16_iotlb, putused_iotlb);
1384 EXPORT_SYMBOL(vringh_complete_iotlb);
1387 * vringh_notify_enable_iotlb - we want to know if something changes.
1390 * This always enables notifications, but returns false if there are
1391 * now more buffers available in the vring.
1393 bool vringh_notify_enable_iotlb(struct vringh *vrh)
1395 return __vringh_notify_enable(vrh, getu16_iotlb, putu16_iotlb);
1397 EXPORT_SYMBOL(vringh_notify_enable_iotlb);
1400 * vringh_notify_disable_iotlb - don't tell us if something changes.
1403 * This is our normal running state: we disable and then only enable when
1404 * we're going to sleep.
1406 void vringh_notify_disable_iotlb(struct vringh *vrh)
1408 __vringh_notify_disable(vrh, putu16_iotlb);
1410 EXPORT_SYMBOL(vringh_notify_disable_iotlb);
1413 * vringh_need_notify_iotlb - must we tell the other side about used buffers?
1414 * @vrh: the vring we've called vringh_complete_iotlb() on.
1416 * Returns -errno or 0 if we don't need to tell the other side, 1 if we do.
1418 int vringh_need_notify_iotlb(struct vringh *vrh)
1420 return __vringh_need_notify(vrh, getu16_iotlb);
1422 EXPORT_SYMBOL(vringh_need_notify_iotlb);
1426 MODULE_LICENSE("GPL");