All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] virtio_ring: share __vring_new_virtqueue with packed ring
@ 2022-12-26  6:16 Liang Chen
  0 siblings, 0 replies; only message in thread
From: Liang Chen @ 2022-12-26  6:16 UTC (permalink / raw)
  To: virtualization; +Cc: mst, jasowang, linux-kernel, Liang Chen

Packed ring allocation and initialization was explicitly done in
vring_create_virtqueue_packed, which resulted in much of the logic
implemented in __vring_new_virtqueue to be duplicated there. With some
adjustment to __vring_new_virtqueue, it can now be used for packed ring
creation as well.

Signed-off-by: Liang Chen <liangchen.linux@gmail.com>
---
 drivers/virtio/virtio_ring.c | 83 +++++++++++-------------------------
 1 file changed, 25 insertions(+), 58 deletions(-)

diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
index 2e7689bb933b..991af334ac6a 100644
--- a/drivers/virtio/virtio_ring.c
+++ b/drivers/virtio/virtio_ring.c
@@ -213,7 +213,7 @@ struct vring_virtqueue {
 };
 
 static struct virtqueue *__vring_new_virtqueue(unsigned int index,
-					       struct vring_virtqueue_split *vring_split,
+					       void *vring,
 					       struct virtio_device *vdev,
 					       bool weak_barriers,
 					       bool context,
@@ -1990,57 +1990,20 @@ static struct virtqueue *vring_create_virtqueue_packed(
 	const char *name)
 {
 	struct vring_virtqueue_packed vring_packed = {};
-	struct vring_virtqueue *vq;
-	int err;
+	struct virtqueue *vq;
 
 	if (vring_alloc_queue_packed(&vring_packed, vdev, num))
 		goto err_ring;
 
-	vq = kmalloc(sizeof(*vq), GFP_KERNEL);
-	if (!vq)
-		goto err_vq;
-
-	vq->vq.callback = callback;
-	vq->vq.vdev = vdev;
-	vq->vq.name = name;
-	vq->vq.index = index;
-	vq->vq.reset = false;
-	vq->we_own_ring = true;
-	vq->notify = notify;
-	vq->weak_barriers = weak_barriers;
-#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
-	vq->broken = true;
-#else
-	vq->broken = false;
-#endif
-	vq->packed_ring = true;
-	vq->use_dma_api = vring_use_dma_api(vdev);
-
-	vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
-		!context;
-	vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
-
-	if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
-		vq->weak_barriers = false;
-
-	err = vring_alloc_state_extra_packed(&vring_packed);
-	if (err)
-		goto err_state_extra;
-
-	virtqueue_vring_init_packed(&vring_packed, !!callback);
-
-	virtqueue_init(vq, num);
-	virtqueue_vring_attach_packed(vq, &vring_packed);
+	vq = __vring_new_virtqueue(index, &vring_packed, vdev, weak_barriers,
+				   context, notify, callback, name);
+	if (!vq) {
+		vring_free_packed(&vring_packed, vdev);
+		return NULL;
+	}
 
-	spin_lock(&vdev->vqs_list_lock);
-	list_add_tail(&vq->vq.list, &vdev->vqs);
-	spin_unlock(&vdev->vqs_list_lock);
-	return &vq->vq;
+	return vq;
 
-err_state_extra:
-	kfree(vq);
-err_vq:
-	vring_free_packed(&vring_packed, vdev);
 err_ring:
 	return NULL;
 }
@@ -2473,9 +2436,8 @@ irqreturn_t vring_interrupt(int irq, void *_vq)
 }
 EXPORT_SYMBOL_GPL(vring_interrupt);
 
-/* Only available for split ring */
 static struct virtqueue *__vring_new_virtqueue(unsigned int index,
-					       struct vring_virtqueue_split *vring_split,
+					       void *vring,
 					       struct virtio_device *vdev,
 					       bool weak_barriers,
 					       bool context,
@@ -2486,20 +2448,17 @@ static struct virtqueue *__vring_new_virtqueue(unsigned int index,
 	struct vring_virtqueue *vq;
 	int err;
 
-	if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
-		return NULL;
-
 	vq = kmalloc(sizeof(*vq), GFP_KERNEL);
 	if (!vq)
 		return NULL;
 
-	vq->packed_ring = false;
+	vq->packed_ring = virtio_has_feature(vdev, VIRTIO_F_RING_PACKED);
 	vq->vq.callback = callback;
 	vq->vq.vdev = vdev;
 	vq->vq.name = name;
 	vq->vq.index = index;
 	vq->vq.reset = false;
-	vq->we_own_ring = false;
+	vq->we_own_ring = vq->packed_ring;
 	vq->notify = notify;
 	vq->weak_barriers = weak_barriers;
 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
@@ -2516,16 +2475,24 @@ static struct virtqueue *__vring_new_virtqueue(unsigned int index,
 	if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
 		vq->weak_barriers = false;
 
-	err = vring_alloc_state_extra_split(vring_split);
+	if (vq->packed_ring)
+		err = vring_alloc_state_extra_packed(vring);
+	else
+		err = vring_alloc_state_extra_split(vring);
 	if (err) {
 		kfree(vq);
 		return NULL;
 	}
 
-	virtqueue_vring_init_split(vring_split, vq);
-
-	virtqueue_init(vq, vring_split->vring.num);
-	virtqueue_vring_attach_split(vq, vring_split);
+	if (vq->packed_ring) {
+		virtqueue_vring_init_packed(vring, !!callback);
+		virtqueue_init(vq, ((struct vring_virtqueue_packed *)vring)->vring.num);
+		virtqueue_vring_attach_packed(vq, vring);
+	} else {
+		virtqueue_vring_init_split(vring, vq);
+		virtqueue_init(vq, ((struct vring_virtqueue_split *)vring)->vring.num);
+		virtqueue_vring_attach_split(vq, vring);
+	}
 
 	spin_lock(&vdev->vqs_list_lock);
 	list_add_tail(&vq->vq.list, &vdev->vqs);
-- 
2.32.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2022-12-26  6:17 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-26  6:16 [PATCH] virtio_ring: share __vring_new_virtqueue with packed ring Liang Chen

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.