[PATCH 2/2] virtio_blk: fit max_segments to the virtqueue without indirect descriptors

From: Stjepan Poljak

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


virtblk_read_limits() takes max_segments from the device's seg_max and
only clamps it to VIRTIO_BLK_MAX_SG_ELEMS - 2, which fits the largest
virtqueue the spec allows, not the one the device has. Without
VIRTIO_RING_F_INDIRECT_DESC every segment takes a descriptor in the
ring, next to the header and the status. A device that offers a seg_max
above its queue size minus two thus lets the block layer build requests
that can never be added: virtqueue_add_split() warns that the request
is larger than the ring and returns -ENOSPC, and the hardware queue is
stopped to wait for descriptors that even an empty ring does not have.
The I/O hangs.

Seen with a device that offers seg_max 254 on a 128-entry queue without
indirect descriptors, where the boot hangs loading /sbin/init:

WARNING: drivers/virtio/virtio_ring.c:688 at virtqueue_add_split+0x6c0/0x850
...
virtqueue_add_sgs+0xd1/0x120
virtblk_add_req+0xe2/0x140
virtio_queue_rq+0x79/0x1a0

With seg_max 126 it boots, with 127 it hangs.

Without indirect descriptors, clamp max_segments to the size of the
smallest virtqueue minus two. The previous commit makes the probe fail
for a virtqueue smaller than three, so at least one segment is left.

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

diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c
index 0a2af22f2365..5f05aaa079c2 100644
--- a/drivers/block/virtio_blk.c
+++ b/drivers/block/virtio_blk.c
@@ -1280,6 +1280,16 @@ static int virtblk_read_limits(struct virtio_blk *vblk,
/* Prevent integer overflows and honor max vq size */
sg_elems = min_t(u32, sg_elems, VIRTIO_BLK_MAX_SG_ELEMS - 2);

+ /*
+ * Without indirect descriptors a request takes one ring descriptor
+ * per segment, plus one for the header and one for the status, so
+ * it has to fit in the smallest of the virtqueues. A larger request
+ * would never fit and would wait for ring space forever.
+ * virtblk_probe() made sure every virtqueue holds at least three.
+ */
+ if (!virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC))
+ sg_elems = min(sg_elems, virtblk_min_vring_size(vblk) - 2);
+
/* We can handle whatever the host told us to handle. */
lim->max_segments = sg_elems;

--
2.53.0