Re: [PATCH] block: fix bio_alloc_bioset() percpu cache fallback for non-reclaim contexts
From: Joseph Qi
Date: Wed Jul 08 2026 - 05:50:51 EST
On 7/8/26 4:45 PM, Christoph Hellwig wrote:
> On Wed, Jul 08, 2026 at 09:14:13AM +0800, Joseph Qi wrote:
>> This causes virtio-pmem flush (async_pmem_flush) to fail with -ENOMEM
>> whenever the percpu bio cache happens to be empty (common right after
>> boot), making the device effectively unmountable:
>
> Please fix that to not sure GFP_ATOMIC instead. Flushes are used
> in file system writeabck and must not use potential failing allocations.
>
> Even if you slightly increase the chance of it not failing, it still
> can and this code is simply broken.
>
Looks sane. So commit b520c4eef83d exposed the bug but not introduced it.
>> Fix this by restructuring the allocation so that the percpu cache is
>> tried first when applicable, and the slab allocation is always attempted
>> as a common fallback path when the bio is still NULL.
>
> I don;
>
>>
>> Fixes: b520c4eef83d ("block: split bio_alloc_bioset more clearly into a fast and slowpath")
>> Signed-off-by: Joseph Qi <joseph.qi@xxxxxxxxxxxxxxxxx>
>> ---
>> block/bio.c | 5 +++++
>> 1 file changed, 5 insertions(+)
>>
>> diff --git a/block/bio.c b/block/bio.c
>> index f2a5f4d0a9672..9e7939861b94a 100644
>> --- a/block/bio.c
>> +++ b/block/bio.c
>> @@ -553,6 +553,11 @@ struct bio *bio_alloc_bioset(struct block_device *bdev, unsigned short nr_vecs,
>> */
>> opf |= REQ_ALLOC_CACHE;
>> bio = bio_alloc_percpu_cache(bs);
>> + if (!bio) {
>> + p = kmem_cache_alloc(bs->bio_slab, gfp);
>> + if (p)
>> + bio = p + bs->front_pad;
>> + }
>
> But even if we wanted this, this code should not be duplicated by
> share the common version.
This is because I want to keep REQ_ALLOC_CACHE set.
Thanks for comments. I'll try to fix it in virtio-pmem driver.
Thanks,
Joseph