[Devel] [PATCH VZ9 2/3] vhost-blk: fix bio allocation failure on excess bio vector count

Pavel Tikhomirov ptikhomirov at virtuozzo.com
Wed Nov 6 12:20:30 MSK 2024


The bio_alloc() function does not allow to allocate bio-s with more than
BIO_MAX_VECS bio vectors. Currently we give to it the nr_vecs argument
equal to the number of pages in all iovecs of the processed request,
which can be much bigger than BIO_MAX_VECS (256), this is incorrect and
leads to the bug in biovec_slab(): "kernel BUG at block/bio.c:62!".

Fix it by restricting the nr_vecs argument properly by bio_max_segs()
helper, also let's decrease pages_nr_total by the number of already
processed pages, so that we can use bio with less vectors when possible.

Fixes: 17bfe6e0400da ("drivers/vhost: vhost-blk accelerator for virtio-blk guests")
https://virtuozzo.atlassian.net/browse/VSTOR-94596
Signed-off-by: Pavel Tikhomirov <ptikhomirov at virtuozzo.com>

Feature: vhost-blk: in-kernel accelerator for virtio-blk guests
---
 drivers/vhost/blk.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/vhost/blk.c b/drivers/vhost/blk.c
index cd8d71c28bbd3..d77e5fc494dee 100644
--- a/drivers/vhost/blk.c
+++ b/drivers/vhost/blk.c
@@ -297,7 +297,7 @@ static int vhost_blk_bio_make(struct vhost_blk_req *req,
 				len = iov_len;
 
 			while (!bio || !bio_add_page(bio, page, len, off)) {
-				bio = bio_alloc(bdev, pages_nr_total,
+				bio = bio_alloc(bdev, bio_max_segs(pages_nr_total),
 						req->bi_opf, GFP_KERNEL);
 				if (!bio)
 					goto fail;
@@ -309,6 +309,7 @@ static int vhost_blk_bio_make(struct vhost_blk_req *req,
 
 			iov_base	+= len;
 			iov_len		-= len;
+			pages_nr_total--;
 
 			pos = (iov_base & VHOST_BLK_SECTOR_MASK) + iov_len;
 			req->sector += pos >> VHOST_BLK_SECTOR_BITS;
-- 
2.46.2



More information about the Devel mailing list