Skip to contentSkip to conversation

virtio_ring: reject NULL recycle callback in reset/resize helpers

PATCH In review Shengwen Cheng 2 participants 2 messages Mar 15 virtio core

lore
Share & tools
lore
#0 lore raw

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

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(-)
drivers/virtio/virtio_ring.c +5 -2
index 335692d41617..563cee634426 100644
@@ -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
Signature · 1 line
2.34.1
#1 lore raw
Quoted 8 lines · Shengwen Cheng

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.

Quoted 38 lines
---
 drivers/virtio/virtio_ring.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/virtio/virtio_ring.c +5 -2
index 335692d41617..563cee634426 100644
@@ -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
Signature · 1 line
2.34.1