Re: [PATCH] block: fix bio_alloc_bioset() percpu cache fallback for non-reclaim contexts

From: Joseph Qi

Date: Wed Jul 08 2026 - 21:21:48 EST




On 7/9/26 12:21 AM, Christoph Hellwig wrote:
> On Wed, Jul 08, 2026 at 05:49:21PM +0800, Joseph Qi wrote:
>> Looks sane. So commit b520c4eef83d exposed the bug but not introduced it.
>
> Well, there is a bug in virtio_pmem for sure. But I also think the
> bio_alloc_bioset behavior isn't quite optimal for non-sleeping
> allocations. But I'd probably go more for something like this there:
>
> diff --git a/block/bio.c b/block/bio.c
> index f2a5f4d0a967..bdca2e60514b 100644
> --- a/block/bio.c
> +++ b/block/bio.c
> @@ -555,6 +555,13 @@ struct bio *bio_alloc_bioset(struct block_device *bdev, unsigned short nr_vecs,
> bio = bio_alloc_percpu_cache(bs);
> } else {
> opf &= ~REQ_ALLOC_CACHE;
> + }
> +
> + /*
> + * If the percpu cache was empty, try an slab allocation with optimistic
> + * GFP_ flags ass well before falling back to the mempool.
> + */
> + if (!bio) {
> p = kmem_cache_alloc(bs->bio_slab, gfp);
> if (p)
> bio = p + bs->front_pad;

I wrote like this firstly, but to not mixed the bs->cache and normal
case so I dropped it.
Anyway I'm fine with this way. I'll send v2 later.

Thanks,
Joseph