[PATCH] virtio_ring: reject NULL recycle callback in reset/resize helpers

Shengwen Cheng posted 1 patch 3 weeks, 1 day ago
drivers/virtio/virtio_ring.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
[PATCH] virtio_ring: reject NULL recycle callback in reset/resize helpers
Posted by Shengwen Cheng 3 weeks, 1 day ago
virtqueue_reset() and virtqueue_resize() rely on the recycle callback
to release each detached unused buffer after disable_vq_and_reset().

As a defensive change, reject a NULL recycle callback explicitly to make
this requirement clear. Update virtqueue_disable_and_recycle() to warn
and return -EINVAL when @recycle is NULL, and update the descriptions
for virtqueue_reset() and virtqueue_resize() to document that @recycle
must not be %NULL.
---
 drivers/virtio/virtio_ring.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
index 335692d41617..563cee634426 100644
--- a/drivers/virtio/virtio_ring.c
+++ b/drivers/virtio/virtio_ring.c
@@ -2698,6 +2698,9 @@ static int virtqueue_disable_and_recycle(struct virtqueue *_vq,
 	void *buf;
 	int err;
 
+	if (WARN_ON_ONCE(!recycle))
+		return -EINVAL;
+
 	if (!vq->we_own_ring)
 		return -EPERM;
 
@@ -3311,7 +3314,7 @@ EXPORT_SYMBOL_GPL(vring_create_virtqueue_map);
  * virtqueue_resize - resize the vring of vq
  * @_vq: the struct virtqueue we're talking about.
  * @num: new ring num
- * @recycle: callback to recycle unused buffers
+ * @recycle: callback to recycle unused buffers, must not be %NULL
  * @recycle_done: callback to be invoked when recycle for all unused buffers done
  *
  * When it is really necessary to create a new vring, it will set the current vq
@@ -3367,7 +3370,7 @@ EXPORT_SYMBOL_GPL(virtqueue_resize);
 /**
  * virtqueue_reset - detach and recycle all unused buffers
  * @_vq: the struct virtqueue we're talking about.
- * @recycle: callback to recycle unused buffers
+ * @recycle: callback to recycle unused buffers, must not be %NULL
  * @recycle_done: callback to be invoked when recycle for all unused buffers done
  *
  * Caller must ensure we don't call this with other virtqueue operations
-- 
2.34.1
Re: [PATCH] virtio_ring: reject NULL recycle callback in reset/resize helpers
Posted by Michael S. Tsirkin 3 weeks, 1 day ago
On Sun, Mar 15, 2026 at 11:24:03PM +0800, Shengwen Cheng wrote:
> virtqueue_reset() and virtqueue_resize() rely on the recycle callback
> to release each detached unused buffer after disable_vq_and_reset().
> 
> As a defensive change, reject a NULL recycle callback explicitly to make
> this requirement clear. Update virtqueue_disable_and_recycle() to warn
> and return -EINVAL when @recycle is NULL, and update the descriptions
> for virtqueue_reset() and virtqueue_resize() to document that @recycle
> must not be %NULL.

I see little point in this change, sorry.

> ---
>  drivers/virtio/virtio_ring.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
> index 335692d41617..563cee634426 100644
> --- a/drivers/virtio/virtio_ring.c
> +++ b/drivers/virtio/virtio_ring.c
> @@ -2698,6 +2698,9 @@ static int virtqueue_disable_and_recycle(struct virtqueue *_vq,
>  	void *buf;
>  	int err;
>  
> +	if (WARN_ON_ONCE(!recycle))
> +		return -EINVAL;
> +
>  	if (!vq->we_own_ring)
>  		return -EPERM;
>  
> @@ -3311,7 +3314,7 @@ EXPORT_SYMBOL_GPL(vring_create_virtqueue_map);
>   * virtqueue_resize - resize the vring of vq
>   * @_vq: the struct virtqueue we're talking about.
>   * @num: new ring num
> - * @recycle: callback to recycle unused buffers
> + * @recycle: callback to recycle unused buffers, must not be %NULL
>   * @recycle_done: callback to be invoked when recycle for all unused buffers done
>   *
>   * When it is really necessary to create a new vring, it will set the current vq
> @@ -3367,7 +3370,7 @@ EXPORT_SYMBOL_GPL(virtqueue_resize);
>  /**
>   * virtqueue_reset - detach and recycle all unused buffers
>   * @_vq: the struct virtqueue we're talking about.
> - * @recycle: callback to recycle unused buffers
> + * @recycle: callback to recycle unused buffers, must not be %NULL
>   * @recycle_done: callback to be invoked when recycle for all unused buffers done
>   *
>   * Caller must ensure we don't call this with other virtqueue operations
> -- 
> 2.34.1