Re: [PATCH v9 07/32] virtio_ring: split: extract the logic of alloc state and extra

Jason Wang
2022-04-12T03:27:16Z

在 2022/4/6 上午11:43, Xuan Zhuo 写道:
> Separate the logic of creating desc_state, desc_extra, and subsequent
> patches will call it independently.
>
> Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
> ---
> drivers/virtio/virtio_ring.c | 53 ++++++++++++++++++++++++++----------
> 1 file changed, 38 insertions(+), 15 deletions(-)
>
> diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
> index 72d5ae063fa0..6de67439cb57 100644
> --- a/drivers/virtio/virtio_ring.c
> +++ b/drivers/virtio/virtio_ring.c
> @@ -198,6 +198,7 @@ struct vring_virtqueue {
> #endif
> };
>
> +static struct vring_desc_extra *vring_alloc_desc_extra(unsigned int num);
>
> /*
> * Helpers.
> @@ -915,6 +916,33 @@ static void *virtqueue_detach_unused_buf_split(struct virtqueue *_vq)
> return NULL;
> }
>
> +static int vring_alloc_state_extra_split(u32 num,
> + struct vring_desc_state_split **desc_state,
> + struct vring_desc_extra **desc_extra)
> +{
> + struct vring_desc_state_split *state;
> + struct vring_desc_extra *extra;
> +
> + state = kmalloc_array(num, sizeof(struct vring_desc_state_split), GFP_KERNEL);
> + if (!state)
> + goto err_state;
> +
> + extra = vring_alloc_desc_extra(num);
> + if (!extra)
> + goto err_extra;
> +
> + memset(state, 0, num * sizeof(struct vring_desc_state_split));
> +
> + *desc_state = state;
> + *desc_extra = extra;
> + return 0;
> +
> +err_extra:
> + kfree(state);
> +err_state:
> + return -ENOMEM;
> +}
> +
> static void *vring_alloc_queue_split(struct virtio_device *vdev,
> dma_addr_t *dma_addr,
> u32 *n,
> @@ -2196,7 +2224,10 @@ struct virtqueue *__vring_new_virtqueue(unsigned int index,
> void (*callback)(struct virtqueue *),
> const char *name)
> {
> + struct vring_desc_state_split *state;
> + struct vring_desc_extra *extra;
> struct vring_virtqueue *vq;
> + int err;
>
> if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
> return NULL;
> @@ -2246,30 +2277,22 @@ struct virtqueue *__vring_new_virtqueue(unsigned int index,
> vq->split.avail_flags_shadow);
> }
>
> - vq->split.desc_state = kmalloc_array(vring.num,
> - sizeof(struct vring_desc_state_split), GFP_KERNEL);
> - if (!vq->split.desc_state)
> - goto err_state;
> + err = vring_alloc_state_extra_split(vring.num, &state, &extra);

Nit: we can pass e.g &vq->split.desc_state here to avoid extra temp
variable and assignment.

Other looks good.

Thanks

> + if (err) {
> + kfree(vq);
> + return NULL;
> + }
>
> - vq->split.desc_extra = vring_alloc_desc_extra(vring.num);
> - if (!vq->split.desc_extra)
> - goto err_extra;
> + vq->split.desc_state = state;
> + vq->split.desc_extra = extra;
>
> /* Put everything in free lists. */
> vq->free_head = 0;
> - memset(vq->split.desc_state, 0, vring.num *
> - sizeof(struct vring_desc_state_split));
>
> spin_lock(&vdev->vqs_list_lock);
> list_add_tail(&vq->vq.list, &vdev->vqs);
> spin_unlock(&vdev->vqs_list_lock);
> return &vq->vq;
> -
> -err_extra:
> - kfree(vq->split.desc_state);
> -err_state:
> - kfree(vq);
> - return NULL;
> }
> EXPORT_SYMBOL_GPL(__vring_new_virtqueue);
>