[PATCH 1/2] virtio_blk: fail the probe when a virtqueue is too small for a request

From: Stjepan Poljak

Date: Sun Nov 01 2026 - 11:54:07 EST


Without VIRTIO_RING_F_INDIRECT_DESC a request takes at least three ring
descriptors: the header, one segment and the status. Nothing checks
that the virtqueues hold that many, and a smaller one fails in a way
that depends on the queue depth. With the default depth, num_free / 2,
a virtqueue of one descriptor gives a depth of 0, which
blk_mq_alloc_tag_set() refuses, and the probe fails without saying why:

virtio_blk virtio0: probe with driver virtio_blk failed with error -22

With a depth of at least one, from a virtqueue of two or from
virtio_blk.queue_depth, the probe succeeds and the first request never
fits: virtqueue_add_split() warns and returns -ENOSPC, and the hardware
queue is stopped to wait for descriptors that even an empty ring does
not have.

Check the virtqueue sizes right after the virtqueues are set up, before
the queue depth is derived from them, and fail the probe with a reason
when the smallest one cannot hold a request:

virtio_blk virtio0: virtqueue size 1 is too small for a request

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Stjepan Poljak <stjepan.poljak@xxxxxxxxx>
---
drivers/block/virtio_blk.c | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)

diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c
index c8bfbf8435ac..0a2af22f2365 100644
--- a/drivers/block/virtio_blk.c
+++ b/drivers/block/virtio_blk.c
@@ -1244,6 +1244,18 @@ static const struct blk_mq_ops virtio_mq_ops = {
static unsigned int virtblk_queue_depth;
module_param_named(queue_depth, virtblk_queue_depth, uint, 0444);

+/* The size of the smallest virtqueue, which every request has to fit. */
+static unsigned int virtblk_min_vring_size(struct virtio_blk *vblk)
+{
+ unsigned int vring_size = UINT_MAX;
+ int i;
+
+ for (i = 0; i < vblk->num_vqs; i++)
+ vring_size = min(vring_size,
+ virtqueue_get_vring_size(vblk->vqs[i].vq));
+ return vring_size;
+}
+
static int virtblk_read_limits(struct virtio_blk *vblk,
struct queue_limits *lim)
{
@@ -1474,6 +1486,21 @@ static int virtblk_probe(struct virtio_device *vdev)
if (err)
goto out_free_vblk;

+ /*
+ * Without indirect descriptors the smallest request, a header, one
+ * segment and the status, takes three descriptors. Check before the
+ * queue depth is derived from the ring, which would come out as 0
+ * for a ring of one and fail without saying why.
+ */
+ if (!virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
+ virtblk_min_vring_size(vblk) < 3) {
+ dev_err(&vdev->dev,
+ "virtqueue size %u is too small for a request\n",
+ virtblk_min_vring_size(vblk));
+ err = -EINVAL;
+ goto out_free_vq;
+ }
+
/* Default queue sizing is to fill the ring. */
if (!virtblk_queue_depth) {
queue_depth = vblk->vqs[0].vq->num_free;
--
2.53.0