[PATCH] block: fix bio_alloc_bioset slowpath GFP handling

Vasily Gorbik posted 1 patch 1 week, 5 days ago
block/bio.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
[PATCH] block: fix bio_alloc_bioset slowpath GFP handling
Posted by Vasily Gorbik 1 week, 5 days ago
bio_alloc_bioset() first strips __GFP_DIRECT_RECLAIM from the optimistic
fast allocation attempt with try_alloc_gfp(). If that fast path fails,
the slowpath checks saved_gfp to decide whether blocking allocation is
allowed, but then still calls mempool_alloc() with the stripped gfp mask.
That can lead to a NULL bio pointer being passed into bio_init().

Fix the slowpath by using saved_gfp for the bio and bvec mempool
allocations.

Fixes: b520c4eef83d ("block: split bio_alloc_bioset more clearly into a fast and slowpath")
Reported-by: syzbot+09ddb593eea76a158f42@syzkaller.appspotmail.com
Signed-off-by: Vasily Gorbik <gor@linux.ibm.com>
---
If this looks fine, feel free to squash it into the offending commit or
pick it up separately.

 block/bio.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/block/bio.c b/block/bio.c
index 5057047194c4..77067fa346d3 100644
--- a/block/bio.c
+++ b/block/bio.c
@@ -581,11 +581,11 @@ struct bio *bio_alloc_bioset(struct block_device *bdev, unsigned short nr_vecs,
 		 */
 		opf &= ~REQ_ALLOC_CACHE;
 
-		p = mempool_alloc(&bs->bio_pool, gfp);
+		p = mempool_alloc(&bs->bio_pool, saved_gfp);
 		bio = p + bs->front_pad;
 		if (nr_vecs > BIO_INLINE_VECS) {
 			nr_vecs = BIO_MAX_VECS;
-			bvecs = mempool_alloc(&bs->bvec_pool, gfp);
+			bvecs = mempool_alloc(&bs->bvec_pool, saved_gfp);
 		}
 	}
 
-- 
2.53.0
Re: [PATCH] block: fix bio_alloc_bioset slowpath GFP handling
Posted by Jens Axboe 1 week, 4 days ago
On Sun, 22 Mar 2026 03:35:10 +0100, Vasily Gorbik wrote:
> bio_alloc_bioset() first strips __GFP_DIRECT_RECLAIM from the optimistic
> fast allocation attempt with try_alloc_gfp(). If that fast path fails,
> the slowpath checks saved_gfp to decide whether blocking allocation is
> allowed, but then still calls mempool_alloc() with the stripped gfp mask.
> That can lead to a NULL bio pointer being passed into bio_init().
> 
> Fix the slowpath by using saved_gfp for the bio and bvec mempool
> allocations.
> 
> [...]

Applied, thanks!

[1/1] block: fix bio_alloc_bioset slowpath GFP handling
      commit: 67807fbaf12719fca46a622d759484652b79c7c3

Best regards,
-- 
Jens Axboe
Re: [PATCH] block: fix bio_alloc_bioset slowpath GFP handling
Posted by Christoph Hellwig 1 week, 4 days ago
On Sun, Mar 22, 2026 at 03:35:10AM +0100, Vasily Gorbik wrote:
> bio_alloc_bioset() first strips __GFP_DIRECT_RECLAIM from the optimistic
> fast allocation attempt with try_alloc_gfp(). If that fast path fails,
> the slowpath checks saved_gfp to decide whether blocking allocation is
> allowed, but then still calls mempool_alloc() with the stripped gfp mask.
> That can lead to a NULL bio pointer being passed into bio_init().
> 
> Fix the slowpath by using saved_gfp for the bio and bvec mempool
> allocations.

Looks good, thanks:

Reviewed-by: Christoph Hellwig <hch@lst.de>