Xuan Zhuo
2023-Feb-14 07:26 UTC
[PATCH vhost 02/10] virtio_ring: packed: separate prepare code from virtuque_add_indirect_packed()
Separating the logic of allocating indirect desc and checking queue status to the upper layer function. The proposal of this is convenient to refactor virtqueue_add_packed() for premapped. Signed-off-by: Xuan Zhuo <xuanzhuo at linux.alibaba.com> --- drivers/virtio/virtio_ring.c | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index 560ee30d942c..42b1ff87518e 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -1330,25 +1330,14 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, unsigned int out_sgs, unsigned int in_sgs, void *data, - gfp_t gfp) + struct vring_packed_desc *desc) { - struct vring_packed_desc *desc; struct scatterlist *sg; unsigned int i, n, err_idx; u16 head, id; dma_addr_t addr; head = vq->packed.next_avail_idx; - desc = alloc_indirect_packed(total_sg, gfp); - if (!desc) - return -ENOMEM; - - if (unlikely(vq->vq.num_free < 1)) { - pr_debug("Can't add buf len 1 - avail = 0\n"); - kfree(desc); - END_USE(vq); - return -ENOSPC; - } i = 0; id = vq->free_head; @@ -1470,11 +1459,17 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq, BUG_ON(total_sg == 0); if (virtqueue_use_indirect(vq, total_sg)) { - err = virtqueue_add_indirect_packed(vq, sgs, total_sg, out_sgs, - in_sgs, data, gfp); - if (err != -ENOMEM) { - END_USE(vq); - return err; + desc = alloc_indirect_packed(total_sg, gfp); + if (desc) { + if (unlikely(vq->vq.num_free < 1)) { + pr_debug("Can't add buf len 1 - avail = 0\n"); + kfree(desc); + END_USE(vq); + return -ENOSPC; + } + + return virtqueue_add_indirect_packed(vq, sgs, total_sg, out_sgs, + in_sgs, data, desc); } /* fall back on direct */ -- 2.32.0.3.g01195cf9f
Jason Wang
2023-Feb-20 05:37 UTC
[PATCH vhost 02/10] virtio_ring: packed: separate prepare code from virtuque_add_indirect_packed()
On Tue, Feb 14, 2023 at 3:27 PM Xuan Zhuo <xuanzhuo at linux.alibaba.com> wrote:> > Separating the logic of allocating indirect desc and checking queue > status to the upper layer function. > > The proposal of this is convenient to refactor virtqueue_add_packed() > for premapped. >Ok, so this is another hint that we should do the same thing for split. Thanks> Signed-off-by: Xuan Zhuo <xuanzhuo at linux.alibaba.com> > --- > drivers/virtio/virtio_ring.c | 29 ++++++++++++----------------- > 1 file changed, 12 insertions(+), 17 deletions(-) > > diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c > index 560ee30d942c..42b1ff87518e 100644 > --- a/drivers/virtio/virtio_ring.c > +++ b/drivers/virtio/virtio_ring.c > @@ -1330,25 +1330,14 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, > unsigned int out_sgs, > unsigned int in_sgs, > void *data, > - gfp_t gfp) > + struct vring_packed_desc *desc) > { > - struct vring_packed_desc *desc; > struct scatterlist *sg; > unsigned int i, n, err_idx; > u16 head, id; > dma_addr_t addr; > > head = vq->packed.next_avail_idx; > - desc = alloc_indirect_packed(total_sg, gfp); > - if (!desc) > - return -ENOMEM; > - > - if (unlikely(vq->vq.num_free < 1)) { > - pr_debug("Can't add buf len 1 - avail = 0\n"); > - kfree(desc); > - END_USE(vq); > - return -ENOSPC; > - } > > i = 0; > id = vq->free_head; > @@ -1470,11 +1459,17 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq, > BUG_ON(total_sg == 0); > > if (virtqueue_use_indirect(vq, total_sg)) { > - err = virtqueue_add_indirect_packed(vq, sgs, total_sg, out_sgs, > - in_sgs, data, gfp); > - if (err != -ENOMEM) { > - END_USE(vq); > - return err; > + desc = alloc_indirect_packed(total_sg, gfp); > + if (desc) { > + if (unlikely(vq->vq.num_free < 1)) { > + pr_debug("Can't add buf len 1 - avail = 0\n"); > + kfree(desc); > + END_USE(vq); > + return -ENOSPC; > + } > + > + return virtqueue_add_indirect_packed(vq, sgs, total_sg, out_sgs, > + in_sgs, data, desc); > } > > /* fall back on direct */ > -- > 2.32.0.3.g01195cf9f >