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.cgroups,org.kernel.vger.linux-block
Message-ID <[email protected]>
Hi,

=E5=9C=A8 2026/7/29 16:05, Nilay Shroff =E5=86=99=E9=81=93:
> On 7/27/26 1:46 PM, yu kuai wrote:
>> Hi,
>>
>> =E5=9C=A8 2026/7/24 20:30, Yu Kuai =E5=86=99=E9=81=93:
>>> From: Yu Kuai<[email protected]>
>>>
>>> bio_set_dev(), bio_init() and bio_reset() associate a bio with a=20
>>> 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,=20
>>> which
>>> is always sleepable (submit_bio_noacct() already does might_sleep()):
>>>
>>> =C2=A0=C2=A0=C2=A0 - submit_bio() associates new I/O before bio_set_iop=
rio(), whose
>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 blkcg_set_ioprio() reads the policy from=
 bio->bi_blkg.
>>>
>>> =C2=A0=C2=A0=C2=A0 - submit_bio_noacct() (re)associates when a bio has =
no blkg yet=20
>>> or was
>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 remapped to a different queue.=C2=A0 blk=
_throtl_bio() and the rq_qos
>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 throttlers (iocost, iolatency) pair bio-=
>bi_blkg with the=20
>>> queue of
>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 bio->bi_bdev, so a remapped bio must be =
reassociated to the=20
>>> new queue.
>>>
>>> =C2=A0=C2=A0=C2=A0 - bio_set_dev() no longer associates; instead it dro=
ps the=20
>>> existing blkg
>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 when the device changes, since that blkg=
 is tied to the old=20
>>> queue.
>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 bio_init()/bio_reset() leave bi_blkg NUL=
L.
>>>
>>> Introduce bio_disassociate_blkg() to drop a bio's blkg reference and=20
>>> use it
>>> from bio_set_dev(), bio_uninit() and the bio freeing path, replacing=20
>>> 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=20
>> initialization,
>> as blkcg must exist, and then covert it to blkg during submission? I=20
>> 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,=20
> and it seems to
> me that all of its callers (btrfs mostly) appear to be in sleepable=20
> context, would
> it be sufficient to associate the blkg before punting the bio rather=20
> 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 li=
ke 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-bas=
ed drivers is
not needed at all.

>
> Thanks,
> --Nilay

--=20
Thanks,
Kuai