Skip to content

Commit 447cfed

Browse files
josephhzaxboe
authored andcommitted
block: try slab allocation in bio_alloc_bioset() before mempool
When the per-CPU bio cache is enabled but empty, bio_alloc_percpu_cache() returns NULL and bio_alloc_bioset() falls straight through to the mempool fallback: if (unlikely(!bio)) { if (!(saved_gfp & __GFP_DIRECT_RECLAIM)) return NULL; ... } For non-sleeping allocations (no __GFP_DIRECT_RECLAIM) this returns NULL without ever attempting a slab allocation, even when there is plenty of free memory. Commit b520c4e ("block: split bio_alloc_bioset more clearly into a fast and slowpath") introduced this. Before it, a percpu cache miss fell through to mempool_alloc(), which attempted the underlying slab allocation first and only failed when that slab allocation failed. The restructuring dropped the slab attempt that non-sleeping callers of a cache-enabled bioset (such as the default fs_bio_set used by bio_alloc()) relied on. Try a slab allocation with optimistic GFP_ flags before falling back to the mempool whenever the bio is still NULL, so both the cache-empty and non-cache paths share the same slab attempt. This restores the previous behavior for non-sleeping allocations. Fixes: b520c4e ("block: split bio_alloc_bioset more clearly into a fast and slowpath") Suggested-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Joseph Qi <joseph.qi@linux.alibaba.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Link: https://patch.msgid.link/20260709020145.4011533-1-joseph.qi@linux.alibaba.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent f94de43 commit 447cfed

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

block/bio.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,14 @@ struct bio *bio_alloc_bioset(struct block_device *bdev, unsigned short nr_vecs,
555555
bio = bio_alloc_percpu_cache(bs);
556556
} else {
557557
opf &= ~REQ_ALLOC_CACHE;
558+
}
559+
560+
/*
561+
* For a bioset without a percpu cache, or when the percpu cache was
562+
* empty, try a slab allocation with optimistic GFP_ flags before
563+
* falling back to the mempool.
564+
*/
565+
if (!bio) {
558566
p = kmem_cache_alloc(bs->bio_slab, gfp);
559567
if (p)
560568
bio = p + bs->front_pad;

0 commit comments

Comments
 (0)