virtio: use MemoryRegionCache to access descriptors

For now, the cache is created on every virtqueue_pop.  Later on,
direct descriptors will be able to reuse it.

Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
This commit is contained in:
Paolo Bonzini 2017-01-27 16:40:16 +01:00 committed by Michael S. Tsirkin
parent 91047df38d
commit 5eba0404b9
2 changed files with 41 additions and 41 deletions

View File

@ -120,9 +120,10 @@ void virtio_queue_update_rings(VirtIODevice *vdev, int n)
} }
static void vring_desc_read(VirtIODevice *vdev, VRingDesc *desc, static void vring_desc_read(VirtIODevice *vdev, VRingDesc *desc,
uint8_t *desc_ptr, int i) MemoryRegionCache *cache, int i)
{ {
memcpy(desc, desc_ptr + i * sizeof(VRingDesc), sizeof(VRingDesc)); address_space_read_cached(cache, i * sizeof(VRingDesc),
desc, sizeof(VRingDesc));
virtio_tswap64s(vdev, &desc->addr); virtio_tswap64s(vdev, &desc->addr);
virtio_tswap32s(vdev, &desc->len); virtio_tswap32s(vdev, &desc->len);
virtio_tswap16s(vdev, &desc->flags); virtio_tswap16s(vdev, &desc->flags);
@ -407,7 +408,7 @@ enum {
}; };
static int virtqueue_read_next_desc(VirtIODevice *vdev, VRingDesc *desc, static int virtqueue_read_next_desc(VirtIODevice *vdev, VRingDesc *desc,
void *desc_ptr, unsigned int max, MemoryRegionCache *desc_cache, unsigned int max,
unsigned int *next) unsigned int *next)
{ {
/* If this descriptor says it doesn't chain, we're done. */ /* If this descriptor says it doesn't chain, we're done. */
@ -425,7 +426,7 @@ static int virtqueue_read_next_desc(VirtIODevice *vdev, VRingDesc *desc,
return VIRTQUEUE_READ_DESC_ERROR; return VIRTQUEUE_READ_DESC_ERROR;
} }
vring_desc_read(vdev, desc, desc_ptr, *next); vring_desc_read(vdev, desc, desc_cache, *next);
return VIRTQUEUE_READ_DESC_MORE; return VIRTQUEUE_READ_DESC_MORE;
} }
@ -436,24 +437,25 @@ void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
VirtIODevice *vdev = vq->vdev; VirtIODevice *vdev = vq->vdev;
unsigned int max, idx; unsigned int max, idx;
unsigned int total_bufs, in_total, out_total; unsigned int total_bufs, in_total, out_total;
void *vring_desc_ptr; MemoryRegionCache vring_desc_cache;
void *indirect_desc_ptr = NULL; MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID;
hwaddr len = 0; int64_t len = 0;
int rc; int rc;
idx = vq->last_avail_idx; idx = vq->last_avail_idx;
total_bufs = in_total = out_total = 0; total_bufs = in_total = out_total = 0;
max = vq->vring.num; max = vq->vring.num;
len = max * sizeof(VRingDesc); len = address_space_cache_init(&vring_desc_cache, vdev->dma_as,
vring_desc_ptr = address_space_map(vdev->dma_as, vq->vring.desc, &len, false); vq->vring.desc, max * sizeof(VRingDesc),
false);
if (len < max * sizeof(VRingDesc)) { if (len < max * sizeof(VRingDesc)) {
virtio_error(vdev, "Cannot map descriptor ring"); virtio_error(vdev, "Cannot map descriptor ring");
goto err; goto err;
} }
while ((rc = virtqueue_num_heads(vq, idx)) > 0) { while ((rc = virtqueue_num_heads(vq, idx)) > 0) {
void *desc_ptr = vring_desc_ptr; MemoryRegionCache *desc_cache = &vring_desc_cache;
unsigned int num_bufs; unsigned int num_bufs;
VRingDesc desc; VRingDesc desc;
unsigned int i; unsigned int i;
@ -464,10 +466,9 @@ void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
goto err; goto err;
} }
vring_desc_read(vdev, &desc, desc_ptr, i); vring_desc_read(vdev, &desc, desc_cache, i);
if (desc.flags & VRING_DESC_F_INDIRECT) { if (desc.flags & VRING_DESC_F_INDIRECT) {
len = desc.len;
if (desc.len % sizeof(VRingDesc)) { if (desc.len % sizeof(VRingDesc)) {
virtio_error(vdev, "Invalid size for indirect buffer table"); virtio_error(vdev, "Invalid size for indirect buffer table");
goto err; goto err;
@ -480,9 +481,10 @@ void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
} }
/* loop over the indirect descriptor table */ /* loop over the indirect descriptor table */
indirect_desc_ptr = address_space_map(vdev->dma_as, desc.addr, len = address_space_cache_init(&indirect_desc_cache,
&len, false); vdev->dma_as,
desc_ptr = indirect_desc_ptr; desc.addr, desc.len, false);
desc_cache = &indirect_desc_cache;
if (len < desc.len) { if (len < desc.len) {
virtio_error(vdev, "Cannot map indirect buffer"); virtio_error(vdev, "Cannot map indirect buffer");
goto err; goto err;
@ -490,7 +492,7 @@ void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
max = desc.len / sizeof(VRingDesc); max = desc.len / sizeof(VRingDesc);
num_bufs = i = 0; num_bufs = i = 0;
vring_desc_read(vdev, &desc, desc_ptr, i); vring_desc_read(vdev, &desc, desc_cache, i);
} }
do { do {
@ -509,16 +511,15 @@ void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
goto done; goto done;
} }
rc = virtqueue_read_next_desc(vdev, &desc, desc_ptr, max, &i); rc = virtqueue_read_next_desc(vdev, &desc, desc_cache, max, &i);
} while (rc == VIRTQUEUE_READ_DESC_MORE); } while (rc == VIRTQUEUE_READ_DESC_MORE);
if (rc == VIRTQUEUE_READ_DESC_ERROR) { if (rc == VIRTQUEUE_READ_DESC_ERROR) {
goto err; goto err;
} }
if (desc_ptr == indirect_desc_ptr) { if (desc_cache == &indirect_desc_cache) {
address_space_unmap(vdev->dma_as, desc_ptr, len, false, 0); address_space_cache_destroy(&indirect_desc_cache);
indirect_desc_ptr = NULL;
total_bufs++; total_bufs++;
} else { } else {
total_bufs = num_bufs; total_bufs = num_bufs;
@ -530,10 +531,8 @@ void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
} }
done: done:
if (indirect_desc_ptr) { address_space_cache_destroy(&indirect_desc_cache);
address_space_unmap(vdev->dma_as, indirect_desc_ptr, len, false, 0); address_space_cache_destroy(&vring_desc_cache);
}
address_space_unmap(vdev->dma_as, vring_desc_ptr, len, false, 0);
if (in_bytes) { if (in_bytes) {
*in_bytes = in_total; *in_bytes = in_total;
} }
@ -673,10 +672,10 @@ static void *virtqueue_alloc_element(size_t sz, unsigned out_num, unsigned in_nu
void *virtqueue_pop(VirtQueue *vq, size_t sz) void *virtqueue_pop(VirtQueue *vq, size_t sz)
{ {
unsigned int i, head, max; unsigned int i, head, max;
void *vring_desc_ptr; MemoryRegionCache vring_desc_cache;
void *indirect_desc_ptr = NULL; MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID;
void *desc_ptr; MemoryRegionCache *desc_cache;
hwaddr len; int64_t len;
VirtIODevice *vdev = vq->vdev; VirtIODevice *vdev = vq->vdev;
VirtQueueElement *elem = NULL; VirtQueueElement *elem = NULL;
unsigned out_num, in_num; unsigned out_num, in_num;
@ -715,15 +714,16 @@ void *virtqueue_pop(VirtQueue *vq, size_t sz)
i = head; i = head;
len = max * sizeof(VRingDesc); len = address_space_cache_init(&vring_desc_cache, vdev->dma_as,
vring_desc_ptr = address_space_map(vdev->dma_as, vq->vring.desc, &len, false); vq->vring.desc, max * sizeof(VRingDesc),
false);
if (len < max * sizeof(VRingDesc)) { if (len < max * sizeof(VRingDesc)) {
virtio_error(vdev, "Cannot map descriptor ring"); virtio_error(vdev, "Cannot map descriptor ring");
goto done; goto done;
} }
desc_ptr = vring_desc_ptr; desc_cache = &vring_desc_cache;
vring_desc_read(vdev, &desc, desc_ptr, i); vring_desc_read(vdev, &desc, desc_cache, i);
if (desc.flags & VRING_DESC_F_INDIRECT) { if (desc.flags & VRING_DESC_F_INDIRECT) {
if (desc.len % sizeof(VRingDesc)) { if (desc.len % sizeof(VRingDesc)) {
virtio_error(vdev, "Invalid size for indirect buffer table"); virtio_error(vdev, "Invalid size for indirect buffer table");
@ -731,9 +731,9 @@ void *virtqueue_pop(VirtQueue *vq, size_t sz)
} }
/* loop over the indirect descriptor table */ /* loop over the indirect descriptor table */
len = desc.len; len = address_space_cache_init(&indirect_desc_cache, vdev->dma_as,
indirect_desc_ptr = address_space_map(vdev->dma_as, desc.addr, &len, false); desc.addr, desc.len, false);
desc_ptr = indirect_desc_ptr; desc_cache = &indirect_desc_cache;
if (len < desc.len) { if (len < desc.len) {
virtio_error(vdev, "Cannot map indirect buffer"); virtio_error(vdev, "Cannot map indirect buffer");
goto done; goto done;
@ -741,7 +741,7 @@ void *virtqueue_pop(VirtQueue *vq, size_t sz)
max = desc.len / sizeof(VRingDesc); max = desc.len / sizeof(VRingDesc);
i = 0; i = 0;
vring_desc_read(vdev, &desc, desc_ptr, i); vring_desc_read(vdev, &desc, desc_cache, i);
} }
/* Collect all the descriptors */ /* Collect all the descriptors */
@ -772,7 +772,7 @@ void *virtqueue_pop(VirtQueue *vq, size_t sz)
goto err_undo_map; goto err_undo_map;
} }
rc = virtqueue_read_next_desc(vdev, &desc, desc_ptr, max, &i); rc = virtqueue_read_next_desc(vdev, &desc, desc_cache, max, &i);
} while (rc == VIRTQUEUE_READ_DESC_MORE); } while (rc == VIRTQUEUE_READ_DESC_MORE);
if (rc == VIRTQUEUE_READ_DESC_ERROR) { if (rc == VIRTQUEUE_READ_DESC_ERROR) {
@ -795,10 +795,8 @@ void *virtqueue_pop(VirtQueue *vq, size_t sz)
trace_virtqueue_pop(vq, elem, elem->in_num, elem->out_num); trace_virtqueue_pop(vq, elem, elem->in_num, elem->out_num);
done: done:
if (indirect_desc_ptr) { address_space_cache_destroy(&indirect_desc_cache);
address_space_unmap(vdev->dma_as, indirect_desc_ptr, len, false, 0); address_space_cache_destroy(&vring_desc_cache);
}
address_space_unmap(vdev->dma_as, vring_desc_ptr, len, false, 0);
return elem; return elem;

View File

@ -1426,6 +1426,8 @@ struct MemoryRegionCache {
bool is_write; bool is_write;
}; };
#define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .mr = NULL })
/* address_space_cache_init: prepare for repeated access to a physical /* address_space_cache_init: prepare for repeated access to a physical
* memory region * memory region
* *