Skip to content

Commit

Permalink
virtio-ring: factor out desc_extra allocation
Browse files Browse the repository at this point in the history
A helper is introduced for the logic of allocating the descriptor
extra data. This will be reused by split virtqueue.

Signed-off-by: Jason Wang <jasowang@redhat.com>
Link: https://lore.kernel.org/r/20210604055350.58753-4-jasowang@redhat.com
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
  • Loading branch information
Jason Wang authored and Michael S. Tsirkin committed Jul 8, 2021
1 parent 1f28750 commit 5a22242
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions drivers/virtio/virtio_ring.c
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,25 @@ static void *virtqueue_detach_unused_buf_packed(struct virtqueue *_vq)
return NULL;
}

static struct vring_desc_extra *vring_alloc_desc_extra(struct vring_virtqueue *vq,
unsigned int num)
{
struct vring_desc_extra *desc_extra;
unsigned int i;

desc_extra = kmalloc_array(num, sizeof(struct vring_desc_extra),
GFP_KERNEL);
if (!desc_extra)
return NULL;

memset(desc_extra, 0, num * sizeof(struct vring_desc_extra));

for (i = 0; i < num - 1; i++)
desc_extra[i].next = i + 1;

return desc_extra;
}

static struct virtqueue *vring_create_virtqueue_packed(
unsigned int index,
unsigned int num,
Expand All @@ -1573,7 +1592,6 @@ static struct virtqueue *vring_create_virtqueue_packed(
struct vring_packed_desc_event *driver, *device;
dma_addr_t ring_dma_addr, driver_event_dma_addr, device_event_dma_addr;
size_t ring_size_in_bytes, event_size_in_bytes;
unsigned int i;

ring_size_in_bytes = num * sizeof(struct vring_packed_desc);

Expand Down Expand Up @@ -1657,18 +1675,10 @@ static struct virtqueue *vring_create_virtqueue_packed(
/* Put everything in free lists. */
vq->free_head = 0;

vq->packed.desc_extra = kmalloc_array(num,
sizeof(struct vring_desc_extra),
GFP_KERNEL);
vq->packed.desc_extra = vring_alloc_desc_extra(vq, num);
if (!vq->packed.desc_extra)
goto err_desc_extra;

memset(vq->packed.desc_extra, 0,
num * sizeof(struct vring_desc_extra));

for (i = 0; i < num - 1; i++)
vq->packed.desc_extra[i].next = i + 1;

/* No callback? Tell other side not to bother us. */
if (!callback) {
vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE;
Expand Down

0 comments on commit 5a22242

Please sign in to comment.