Introduce virtqueue_reset() to release all buffer inside vq.
Signed-off-by: Xuan Zhuo <xuanzhuo at linux.alibaba.com>
---
drivers/virtio/virtio_ring.c | 50 ++++++++++++++++++++++++++++++++++++
include/linux/virtio.h | 2 ++
2 files changed, 52 insertions(+)
diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
index e32046fd15a5..7dfce7001f9f 100644
--- a/drivers/virtio/virtio_ring.c
+++ b/drivers/virtio/virtio_ring.c
@@ -2735,6 +2735,56 @@ int virtqueue_resize(struct virtqueue *_vq, u32 num,
}
EXPORT_SYMBOL_GPL(virtqueue_resize);
+/**
+ * virtqueue_reset - reset the vring of vq
+ * @_vq: the struct virtqueue we're talking about.
+ * @recycle: callback for recycle the useless buffer
+ *
+ * Caller must ensure we don't call this with other virtqueue operations
+ * at the same time (except where noted).
+ *
+ * Returns zero or a negative error.
+ * 0: success.
+ * -EBUSY: Failed to sync with device, vq may not work properly
+ * -ENOENT: Transport or device not supported
+ * -EPERM: Operation not permitted
+ */
+int virtqueue_reset(struct virtqueue *_vq,
+ void (*recycle)(struct virtqueue *vq, void *buf))
+{
+ struct vring_virtqueue *vq = to_vvq(_vq);
+ struct virtio_device *vdev = vq->vq.vdev;
+ void *buf;
+ int err;
+
+ if (!vq->we_own_ring)
+ return -EPERM;
+
+ if (!vdev->config->disable_vq_and_reset)
+ return -ENOENT;
+
+ if (!vdev->config->enable_vq_after_reset)
+ return -ENOENT;
+
+ err = vdev->config->disable_vq_and_reset(_vq);
+ if (err)
+ return err;
+
+ while ((buf = virtqueue_detach_unused_buf(_vq)) != NULL)
+ recycle(_vq, buf);
+
+ if (vq->packed_ring)
+ virtqueue_reinit_packed(vq);
+ else
+ virtqueue_reinit_split(vq);
+
+ if (vdev->config->enable_vq_after_reset(_vq))
+ return -EBUSY;
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(virtqueue_reset);
+
/* Only available for split ring */
struct virtqueue *vring_new_virtqueue(unsigned int index,
unsigned int num,
diff --git a/include/linux/virtio.h b/include/linux/virtio.h
index 3ebb346ebb7c..3ca2edb1aef3 100644
--- a/include/linux/virtio.h
+++ b/include/linux/virtio.h
@@ -105,6 +105,8 @@ dma_addr_t virtqueue_get_used_addr(struct virtqueue *vq);
int virtqueue_resize(struct virtqueue *vq, u32 num,
void (*recycle)(struct virtqueue *vq, void *buf));
+int virtqueue_reset(struct virtqueue *vq,
+ void (*recycle)(struct virtqueue *vq, void *buf));
/**
* struct virtio_device - representation of a device using virtio
--
2.32.0.3.g01195cf9f
Michael S. Tsirkin
2023-Feb-03 09:05 UTC
[PATCH 06/33] virtio_ring: introduce virtqueue_reset()
On Thu, Feb 02, 2023 at 07:00:31PM +0800, Xuan Zhuo wrote:> Introduce virtqueue_reset() to release all buffer inside vq. > > Signed-off-by: Xuan Zhuo <xuanzhuo at linux.alibaba.com> > --- > drivers/virtio/virtio_ring.c | 50 ++++++++++++++++++++++++++++++++++++ > include/linux/virtio.h | 2 ++ > 2 files changed, 52 insertions(+) > > diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c > index e32046fd15a5..7dfce7001f9f 100644 > --- a/drivers/virtio/virtio_ring.c > +++ b/drivers/virtio/virtio_ring.c > @@ -2735,6 +2735,56 @@ int virtqueue_resize(struct virtqueue *_vq, u32 num, > } > EXPORT_SYMBOL_GPL(virtqueue_resize); > > +/** > + * virtqueue_reset - reset the vring of vq..., detach and recycle all unused buffers after all this is why we are doing this reset, right?> + * @_vq: the struct virtqueue we're talking about. > + * @recycle: callback for recycle the useless buffernot useless :) unused: callback to recycle unused buffers I know we have the same confusion in virtqueue_resize, I will fix that.> + * > + * Caller must ensure we don't call this with other virtqueue operations > + * at the same time (except where noted). > + * > + * Returns zero or a negative error. > + * 0: success. > + * -EBUSY: Failed to sync with device, vq may not work properly > + * -ENOENT: Transport or device not supported > + * -EPERM: Operation not permitted > + */ > +int virtqueue_reset(struct virtqueue *_vq, > + void (*recycle)(struct virtqueue *vq, void *buf)) > +{ > + struct vring_virtqueue *vq = to_vvq(_vq); > + struct virtio_device *vdev = vq->vq.vdev; > + void *buf; > + int err; > + > + if (!vq->we_own_ring) > + return -EPERM; > + > + if (!vdev->config->disable_vq_and_reset) > + return -ENOENT; > + > + if (!vdev->config->enable_vq_after_reset) > + return -ENOENT; > + > + err = vdev->config->disable_vq_and_reset(_vq); > + if (err) > + return err; > + > + while ((buf = virtqueue_detach_unused_buf(_vq)) != NULL) > + recycle(_vq, buf); > + > + if (vq->packed_ring) > + virtqueue_reinit_packed(vq); > + else > + virtqueue_reinit_split(vq); > + > + if (vdev->config->enable_vq_after_reset(_vq)) > + return -EBUSY; > + > + return 0; > +} > +EXPORT_SYMBOL_GPL(virtqueue_reset); > + > /* Only available for split ring */ > struct virtqueue *vring_new_virtqueue(unsigned int index, > unsigned int num, > diff --git a/include/linux/virtio.h b/include/linux/virtio.h > index 3ebb346ebb7c..3ca2edb1aef3 100644 > --- a/include/linux/virtio.h > +++ b/include/linux/virtio.h > @@ -105,6 +105,8 @@ dma_addr_t virtqueue_get_used_addr(struct virtqueue *vq); > > int virtqueue_resize(struct virtqueue *vq, u32 num, > void (*recycle)(struct virtqueue *vq, void *buf)); > +int virtqueue_reset(struct virtqueue *vq, > + void (*recycle)(struct virtqueue *vq, void *buf)); > > /** > * struct virtio_device - representation of a device using virtio > -- > 2.32.0.3.g01195cf9f