[<prev] [next>] [day] [month] [year] [list]
Message-Id: <20221226061619.1161646-1-liangchen.linux@gmail.com>
Date: Mon, 26 Dec 2022 14:16:19 +0800
From: Liang Chen <liangchen.linux@...il.com>
To: virtualization@...ts.linux-foundation.org
Cc: mst@...hat.com, jasowang@...hat.com, linux-kernel@...r.kernel.org,
Liang Chen <liangchen.linux@...il.com>
Subject: [PATCH] virtio_ring: share __vring_new_virtqueue with packed ring
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@...il.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
Powered by blists - more mailing lists