Re: [RFC PATCH v2 1/8] block: associate blkg in submit_bio instead of bio_set_dev

"yu kuai" <[email protected]> Wed, 29 Jul 2026 17:49:34 +0800
Newsgroups org.kernel.vger.linux-block,org.kernel.vger.cgroups
Message-ID <[email protected]>
Hi,

在 2026/7/29 16:05, Nilay Shroff 写道:
> On 7/27/26 1:46 PM, yu kuai wrote:
>> Hi,
>>
>> 在 2026/7/24 20:30, Yu Kuai 写道:
>>> From: Yu Kuai<[email protected]>
>>>
>>> bio_set_dev(), bio_init() and bio_reset() associate a bio with a 
>>> blkg for
>>> its target queue. That association may have to create a new blkg, and
>>> currently there are lots of callers that are under atomic context.
>>>
>>> Move the association out of those helpers and into the submit path, 
>>> which
>>> is always sleepable (submit_bio_noacct() already does might_sleep()):
>>>
>>>     - submit_bio() associates new I/O before bio_set_ioprio(), whose
>>>       blkcg_set_ioprio() reads the policy from bio->bi_blkg.
>>>
>>>     - submit_bio_noacct() (re)associates when a bio has no blkg yet 
>>> or was
>>>       remapped to a different queue.  blk_throtl_bio() and the rq_qos
>>>       throttlers (iocost, iolatency) pair bio->bi_blkg with the 
>>> queue of
>>>       bio->bi_bdev, so a remapped bio must be reassociated to the 
>>> new queue.
>>>
>>>     - bio_set_dev() no longer associates; instead it drops the 
>>> existing blkg
>>>       when the device changes, since that blkg is tied to the old 
>>> queue.
>>>       bio_init()/bio_reset() leave bi_blkg NULL.
>>>
>>> Introduce bio_disassociate_blkg() to drop a bio's blkg reference and 
>>> use it
>>> from bio_set_dev(), bio_uninit() and the bio freeing path, replacing 
>>> their
>>> open-coded blkg_put().
>> Turns out bio will be submitted by kworker for some drivers and for
>> blkcg_punt_bio_submit(). Is it possible to record blkcg during bio 
>> initialization,
>> as blkcg must exist, and then covert it to blkg during submission? I 
>> can use union
>> for blkcg and blkg to avoid new field in struct bio.
>
> Since blkcg_punt_bio_submit() is the only deferred submission path, 
> and it seems to
> me that all of its callers (btrfs mostly) appear to be in sleepable 
> context, would
> it be sufficient to associate the blkg before punting the bio rather 
> than changing the
> bio lifetime semantics?

blkcg_punt_bio_submit() must be called in sleepable context, itself is not a problem, we
can definitely associate a blkg there.

However, some drivers will also defer bio submission to kworker like dm and md raid5, dm
is also the key participants to call bio_set_dev under atomic context. I like the idea to
just associate blkcg first, because most bio-based drivers will not enable blk-throttle,
which is the only blkcg policy for bio-based driver, hence blkg for bio-based drivers is
not needed at all.

>
> Thanks,
> --Nilay

-- 
Thanks,
Kuai