Re: [PATCH v4 2/3] blk-cgroup: store blkcg in bio instead of blkg

From: Nilay Shroff

Date: Mon Sep 21 2026 - 05:40:39 EST


On 9/20/26 2:53 PM, Yu Kuai wrote:
From: Yu Kuai<yukuai@xxxxxxx>

A bio currently stores and pins a queue-local blkg. This forces bio
association and remap paths to look up or create a blkg even when no
blkcg policy will use the bio, and ties the stored state to the current
block device.

Store and reference the queue-independent blkcg in the bio instead. Add
helpers that lazily look up or create the queue-local blkg when a policy
needs it, and pin the result until the bio changes devices or releases
its cgroup state. Look up and pin existing blkgs under RCU, taking the
queue lock only when a blkg may need to be created.

Keep completion and accounting users lookup-only when they do not create
a blkg themselves. In particular, blk_cgroup_bio_start() only accounts a
bio when an online blkg already exists. This avoids creating a blkg
from the unconditional accounting hook when no policy is enabled.

Protect every rhashtable lookup with an RCU read-side critical section.
Annotate the lookup helpers and recursive wrappers with
__must_hold_shared(RCU) so Clang can verify that contract.

Before entering a bio-based driver's ->submit_bio(), drop the pinned blkg
after blk_cgroup_bio_start() has completed bio accounting. blk-throttle is
the only policy supported by bio-based queues, so no queue-local blkg
state needs to enter stacking drivers. The queue-independent blkcg
association remains attached to the bio.

If blkg creation fails while walking down the hierarchy, use the closest
available ancestor and update the bio's blkcg association before
recording the blkg reference. This keeps later CSS ID hash lookups
matched with the pinned blkg.

Signed-off-by: Yu Kuai<yukuai@xxxxxxx>

Looks good to me.

Reviewed-by: Nilay Shroff <nilay@xxxxxxxxxxxxx>