Commit 3b720b8c authored by Rusty Russell's avatar Rusty Russell

virtio: avoid modulus operation.

Since we know vq->vring.num is a power of 2, modulus is lazy (it's asserted
in vring_new_virtqueue()).
Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent 41f0377f
...@@ -250,8 +250,8 @@ int virtqueue_add_buf(struct virtqueue *_vq, ...@@ -250,8 +250,8 @@ int virtqueue_add_buf(struct virtqueue *_vq,
vq->data[head] = data; vq->data[head] = data;
/* Put entry in available array (but don't update avail->idx until they /* Put entry in available array (but don't update avail->idx until they
* do sync). FIXME: avoid modulus here? */ * do sync). */
avail = (vq->vring.avail->idx + vq->num_added++) % vq->vring.num; avail = ((vq->vring.avail->idx + vq->num_added++) & (vq->vring.num-1));
vq->vring.avail->ring[avail] = head; vq->vring.avail->ring[avail] = head;
pr_debug("Added buffer head %i to %p\n", head, vq); pr_debug("Added buffer head %i to %p\n", head, vq);
...@@ -384,6 +384,7 @@ void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len) ...@@ -384,6 +384,7 @@ void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
struct vring_virtqueue *vq = to_vvq(_vq); struct vring_virtqueue *vq = to_vvq(_vq);
void *ret; void *ret;
unsigned int i; unsigned int i;
u16 last_used;
START_USE(vq); START_USE(vq);
...@@ -401,8 +402,9 @@ void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len) ...@@ -401,8 +402,9 @@ void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
/* Only get used array entries after they have been exposed by host. */ /* Only get used array entries after they have been exposed by host. */
virtio_rmb(vq); virtio_rmb(vq);
i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id; last_used = (vq->last_used_idx & (vq->vring.num - 1));
*len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len; i = vq->vring.used->ring[last_used].id;
*len = vq->vring.used->ring[last_used].len;
if (unlikely(i >= vq->vring.num)) { if (unlikely(i >= vq->vring.num)) {
BAD_RING(vq, "id %u out of range\n", i); BAD_RING(vq, "id %u out of range\n", i);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment