cregit-Linux how code gets into the kernel

Release 4.14 drivers/vhost/vringh.c

Directory: drivers/vhost
/*
 * Helpers for the host side of a virtio ring.
 *
 * Since these may be in userspace, we use (inline) accessors.
 */
#include <linux/compiler.h>
#include <linux/module.h>
#include <linux/vringh.h>
#include <linux/virtio_ring.h>
#include <linux/kernel.h>
#include <linux/ratelimit.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/export.h>
#include <uapi/linux/virtio_config.h>


static __printf(1,2) __cold void vringh_bad(const char *fmt, ...)
{
	static DEFINE_RATELIMIT_STATE(vringh_rs,
				      DEFAULT_RATELIMIT_INTERVAL,
				      DEFAULT_RATELIMIT_BURST);
	if (__ratelimit(&vringh_rs)) {
		va_list ap;
		va_start(ap, fmt);
		printk(KERN_NOTICE "vringh:");
		vprintk(fmt, ap);
		va_end(ap);
	}
}

/* Returns vring->num if empty, -ve on error. */

static inline int __vringh_get_head(const struct vringh *vrh, int (*getu16)(const struct vringh *vrh, u16 *val, const __virtio16 *p), u16 *last_avail_idx) { u16 avail_idx, i, head; int err; err = getu16(vrh, &avail_idx, &vrh->vring.avail->idx); if (err) { vringh_bad("Failed to access avail idx at %p", &vrh->vring.avail->idx); return err; } if (*last_avail_idx == avail_idx) return vrh->vring.num; /* Only get avail ring entries after they have been exposed by guest. */ virtio_rmb(vrh->weak_barriers); i = *last_avail_idx & (vrh->vring.num - 1); err = getu16(vrh, &head, &vrh->vring.avail->ring[i]); if (err) { vringh_bad("Failed to read head: idx %d address %p", *last_avail_idx, &vrh->vring.avail->ring[i]); return err; } if (head >= vrh->vring.num) { vringh_bad("Guest says index %u > %u is available", head, vrh->vring.num); return -EINVAL; } (*last_avail_idx)++; return head; }

Contributors

PersonTokensPropCommitsCommitProp
Rusty Russell20694.93%150.00%
Michael S. Tsirkin115.07%150.00%
Total217100.00%2100.00%

/* Copy some bytes to/from the iovec. Returns num copied. */
static inline ssize_t vringh_iov_xfer(struct vringh_kiov *iov, void *ptr, size_t len, int (*xfer)(void *addr, void *ptr, size_t len)) { int err, done = 0; while (len && iov->i < iov->used) { size_t partlen; partlen = min(iov->iov[iov->i].iov_len, len); err = xfer(iov->iov[iov->i].iov_base, ptr, partlen); if (err) return err; done += partlen; len -= partlen; ptr += partlen; iov->consumed += partlen; iov->iov[iov->i].iov_len -= partlen; iov->iov[iov->i].iov_base += partlen; if (!iov->iov[iov->i].iov_len) { /* Fix up old iov element then increment. */ iov->iov[iov->i].iov_len = iov->consumed; iov->iov[iov->i].iov_base -= iov->consumed; iov->consumed = 0; iov->i++; } } return done; }

Contributors

PersonTokensPropCommitsCommitProp
Rusty Russell211100.00%1100.00%
Total211100.00%1100.00%

/* May reduce *len if range is shorter. */
static inline bool range_check(struct vringh *vrh, u64 addr, size_t *len, struct vringh_range *range, bool (*getrange)(struct vringh *, u64, struct vringh_range *)) { if (addr < range->start || addr > range->end_incl) { if (!getrange(vrh, addr, range)) return false; } BUG_ON(addr < range->start || addr > range->end_incl); /* To end of memory? */ if (unlikely(addr + *len == 0)) { if (range->end_incl == -1ULL) return true; goto truncate; } /* Otherwise, don't wrap. */ if (addr + *len < addr) { vringh_bad("Wrapping descriptor %zu@0x%llx", *len, (unsigned long long)addr); return false; } if (unlikely(addr + *len - 1 > range->end_incl)) goto truncate; return true; truncate: *len = range->end_incl + 1 - addr; return true; }

Contributors

PersonTokensPropCommitsCommitProp
Rusty Russell185100.00%1100.00%
Total185100.00%1100.00%


static inline bool no_range_check(struct vringh *vrh, u64 addr, size_t *len, struct vringh_range *range, bool (*getrange)(struct vringh *, u64, struct vringh_range *)) { return true; }

Contributors

PersonTokensPropCommitsCommitProp
Rusty Russell44100.00%1100.00%
Total44100.00%1100.00%

/* No reason for this code to be inline. */
static int move_to_indirect(const struct vringh *vrh, int *up_next, u16 *i, void *addr, const struct vring_desc *desc, struct vring_desc **descs, int *desc_max) { u32 len; /* Indirect tables can't have indirect. */ if (*up_next != -1) { vringh_bad("Multilevel indirect %u->%u", *up_next, *i); return -EINVAL; } len = vringh32_to_cpu(vrh, desc->len); if (unlikely(len % sizeof(struct vring_desc))) { vringh_bad("Strange indirect len %u", desc->len); return -EINVAL; } /* We will check this when we follow it! */ if (desc->flags & cpu_to_vringh16(vrh, VRING_DESC_F_NEXT)) *up_next = vringh16_to_cpu(vrh, desc->next); else *up_next = -2; *descs = addr; *desc_max = len / sizeof(struct vring_desc); /* Now, start at the first indirect. */ *i = 0; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Rusty Russell13681.93%150.00%
Michael S. Tsirkin3018.07%150.00%
Total166100.00%2100.00%


static int resize_iovec(struct vringh_kiov *iov, gfp_t gfp) { struct kvec *new; unsigned int flag, new_num = (iov->max_num & ~VRINGH_IOV_ALLOCATED) * 2; if (new_num < 8) new_num = 8; flag = (iov->max_num & VRINGH_IOV_ALLOCATED); if (flag) new = krealloc(iov->iov, new_num * sizeof(struct iovec), gfp); else { new = kmalloc(new_num * sizeof(struct iovec), gfp); if (new) { memcpy(new, iov->iov, iov->max_num * sizeof(struct iovec)); flag = VRINGH_IOV_ALLOCATED; } } if (!new) return -ENOMEM; iov->iov = new; iov->max_num = (new_num | flag); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Rusty Russell154100.00%1100.00%
Total154100.00%1100.00%


static u16 __cold return_from_indirect(const struct vringh *vrh, int *up_next, struct vring_desc **descs, int *desc_max) { u16 i = *up_next; *up_next = -1; *descs = vrh->vring.desc; *desc_max = vrh->vring.num; return i; }

Contributors

PersonTokensPropCommitsCommitProp
Rusty Russell60100.00%1100.00%
Total60100.00%1100.00%


static int slow_copy(struct vringh *vrh, void *dst, const void *src, bool (*rcheck)(struct vringh *vrh, u64 addr, size_t *len, struct vringh_range *range, bool (*getrange)(struct vringh *vrh, u64, struct vringh_range *)), bool (*getrange)(struct vringh *vrh, u64 addr, struct vringh_range *r), struct vringh_range *range, int (*copy)(void *dst, const void *src, size_t len)) { size_t part, len = sizeof(struct vring_desc); do { u64 addr; int err; part = len; addr = (u64)(unsigned long)src - range->offset; if (!rcheck(vrh, addr, &part, range, getrange)) return -EINVAL; err = copy(dst, src, part); if (err) return err; dst += part; src += part; len -= part; } while (len); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Rusty Russell204100.00%1100.00%
Total204100.00%1100.00%


static inline int __vringh_iov(struct vringh *vrh, u16 i, struct vringh_kiov *riov, struct vringh_kiov *wiov, bool (*rcheck)(struct vringh *vrh, u64 addr, size_t *len, struct vringh_range *range, bool (*getrange)(struct vringh *, u64, struct vringh_range *)), bool (*getrange)(struct vringh *, u64, struct vringh_range *), gfp_t gfp, int (*copy)(void *dst, const void *src, size_t len)) { int err, count = 0, up_next, desc_max; struct vring_desc desc, *descs; struct vringh_range range = { -1ULL, 0 }, slowrange; bool slow = false; /* We start traversing vring's descriptor table. */ descs = vrh->vring.desc; desc_max = vrh->vring.num; up_next = -1; if (riov) riov->i = riov->used = 0; else if (wiov) wiov->i = wiov->used = 0; else /* You must want something! */ BUG(); for (;;) { void *addr; struct vringh_kiov *iov; size_t len; if (unlikely(slow)) err = slow_copy(vrh, &desc, &descs[i], rcheck, getrange, &slowrange, copy); else err = copy(&desc, &descs[i], sizeof(desc)); if (unlikely(err)) goto fail; if (unlikely(desc.flags & cpu_to_vringh16(vrh, VRING_DESC_F_INDIRECT))) { u64 a = vringh64_to_cpu(vrh, desc.addr); /* Make sure it's OK, and get offset. */ len = vringh32_to_cpu(vrh, desc.len); if (!rcheck(vrh, a, &len, &range, getrange)) { err = -EINVAL; goto fail; } if (unlikely(len != vringh32_to_cpu(vrh, desc.len))) { slow = true; /* We need to save this range to use offset */ slowrange = range; } addr = (void *)(long)(a + range.offset); err = move_to_indirect(vrh, &up_next, &i, addr, &desc, &descs, &desc_max); if (err) goto fail; continue; } if (count++ == vrh->vring.num) { vringh_bad("Descriptor loop in %p", descs); err = -ELOOP; goto fail; } if (desc.flags & cpu_to_vringh16(vrh, VRING_DESC_F_WRITE)) iov = wiov; else { iov = riov; if (unlikely(wiov && wiov->i)) { vringh_bad("Readable desc %p after writable", &descs[i]); err = -EINVAL; goto fail; } } if (!iov) { vringh_bad("Unexpected %s desc", !wiov ? "writable" : "readable"); err = -EPROTO; goto fail; } again: /* Make sure it's OK, and get offset. */ len = vringh32_to_cpu(vrh, desc.len); if (!rcheck(vrh, vringh64_to_cpu(vrh, desc.addr), &len, &range, getrange)) { err = -EINVAL; goto fail; } addr = (void *)(unsigned long)(vringh64_to_cpu(vrh, desc.addr) + range.offset); if (unlikely(iov->used == (iov->max_num & ~VRINGH_IOV_ALLOCATED))) { err = resize_iovec(iov, gfp); if (err) goto fail; } iov->iov[iov->used].iov_base = addr; iov->iov[iov->used].iov_len = len; iov->used++; if (unlikely(len != vringh32_to_cpu(vrh, desc.len))) { desc.len = cpu_to_vringh32(vrh, vringh32_to_cpu(vrh, desc.len) - len); desc.addr = cpu_to_vringh64(vrh, vringh64_to_cpu(vrh, desc.addr) + len); goto again; } if (desc.flags & cpu_to_vringh16(vrh, VRING_DESC_F_NEXT)) { i = vringh16_to_cpu(vrh, desc.next); } else { /* Just in case we need to finish traversing above. */ if (unlikely(up_next > 0)) { i = return_from_indirect(vrh, &up_next, &descs, &desc_max); slow = false; } else break; } if (i >= desc_max) { vringh_bad("Chained index %u > %u", i, desc_max); err = -EINVAL; goto fail; } } return 0; fail: return err; }

Contributors

PersonTokensPropCommitsCommitProp
Rusty Russell73788.48%150.00%
Michael S. Tsirkin9611.52%150.00%
Total833100.00%2100.00%


static inline int __vringh_complete(struct vringh *vrh, const struct vring_used_elem *used, unsigned int num_used, int (*putu16)(const struct vringh *vrh, __virtio16 *p, u16 val), int (*putused)(struct vring_used_elem *dst, const struct vring_used_elem *src, unsigned num)) { struct vring_used *used_ring; int err; u16 used_idx, off; used_ring = vrh->vring.used; used_idx = vrh->last_used_idx + vrh->completed; off = used_idx % vrh->vring.num; /* Compiler knows num_used == 1 sometimes, hence extra check */ if (num_used > 1 && unlikely(off + num_used >= vrh->vring.num)) { u16 part = vrh->vring.num - off; err = putused(&used_ring->ring[off], used, part); if (!err) err = putused(&used_ring->ring[0], used + part, num_used - part); } else err = putused(&used_ring->ring[off], used, num_used); if (err) { vringh_bad("Failed to write %u used entries %u at %p", num_used, off, &used_ring->ring[off]); return err; } /* Make sure buffer is written before we update index. */ virtio_wmb(vrh->weak_barriers); err = putu16(vrh, &vrh->vring.used->idx, used_idx + num_used); if (err) { vringh_bad("Failed to update used index at %p", &vrh->vring.used->idx); return err; } vrh->completed += num_used; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Rusty Russell27596.83%150.00%
Michael S. Tsirkin93.17%150.00%
Total284100.00%2100.00%


static inline int __vringh_need_notify(struct vringh *vrh, int (*getu16)(const struct vringh *vrh, u16 *val, const __virtio16 *p)) { bool notify; u16 used_event; int err; /* Flush out used index update. This is paired with the * barrier that the Guest executes when enabling * interrupts. */ virtio_mb(vrh->weak_barriers); /* Old-style, without event indices. */ if (!vrh->event_indices) { u16 flags; err = getu16(vrh, &flags, &vrh->vring.avail->flags); if (err) { vringh_bad("Failed to get flags at %p", &vrh->vring.avail->flags); return err; } return (!(flags & VRING_AVAIL_F_NO_INTERRUPT)); } /* Modern: we know when other side wants to know. */ err = getu16(vrh, &used_event, &vring_used_event(&vrh->vring)); if (err) { vringh_bad("Failed to get used event idx at %p", &vring_used_event(&vrh->vring)); return err; } /* Just in case we added so many that we wrap. */ if (unlikely(vrh->completed > 0xffff)) notify = true; else notify = vring_need_event(used_event, vrh->last_used_idx + vrh->completed, vrh->last_used_idx); vrh->last_used_idx += vrh->completed; vrh->completed = 0; return notify; }

Contributors

PersonTokensPropCommitsCommitProp
Rusty Russell20194.81%150.00%
Michael S. Tsirkin115.19%150.00%
Total212100.00%2100.00%


static inline bool __vringh_notify_enable(struct vringh *vrh, int (*getu16)(const struct vringh *vrh, u16 *val, const __virtio16 *p), int (*putu16)(const struct vringh *vrh, __virtio16 *p, u16 val)) { u16 avail; if (!vrh->event_indices) { /* Old-school; update flags. */ if (putu16(vrh, &vrh->vring.used->flags, 0) != 0) { vringh_bad("Clearing used flags %p", &vrh->vring.used->flags); return true; } } else { if (putu16(vrh, &vring_avail_event(&vrh->vring), vrh->last_avail_idx) != 0) { vringh_bad("Updating avail event index %p", &vring_avail_event(&vrh->vring)); return true; } } /* They could have slipped one in as we were doing that: make * sure it's written, then check again. */ virtio_mb(vrh->weak_barriers); if (getu16(vrh, &avail, &vrh->vring.avail->idx) != 0) { vringh_bad("Failed to check avail idx at %p", &vrh->vring.avail->idx); return true; } /* This is unlikely, so we just leave notifications enabled * (if we're using event_indices, we'll only get one * notification anyway). */ return avail == vrh->last_avail_idx; }

Contributors

PersonTokensPropCommitsCommitProp
Rusty Russell18690.29%150.00%
Michael S. Tsirkin209.71%150.00%
Total206100.00%2100.00%


static inline void __vringh_notify_disable(struct vringh *vrh, int (*putu16)(const struct vringh *vrh, __virtio16 *p, u16 val)) { if (!vrh->event_indices) { /* Old-school; update flags. */ if (putu16(vrh, &vrh->vring.used->flags, VRING_USED_F_NO_NOTIFY)) { vringh_bad("Setting used flags %p", &vrh->vring.used->flags); } } }

Contributors

PersonTokensPropCommitsCommitProp
Rusty Russell6788.16%150.00%
Michael S. Tsirkin911.84%150.00%
Total76100.00%2100.00%

/* Userspace access helpers: in this case, addresses are really userspace. */
static inline int getu16_user(const struct vringh *vrh, u16 *val, const __virtio16 *p) { __virtio16 v = 0; int rc = get_user(v, (__force __virtio16 __user *)p); *val = vringh16_to_cpu(vrh, v); return rc; }

Contributors

PersonTokensPropCommitsCommitProp
Michael S. Tsirkin3053.57%150.00%
Rusty Russell2646.43%150.00%
Total56100.00%2100.00%


static inline int putu16_user(const struct vringh *vrh, __virtio16 *p, u16 val) { __virtio16 v = cpu_to_vringh16(vrh, val); return put_user(v