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

- From: Nilay Shroff \<hidden\>
- Date: 2026-09-13 13:08:33 
- List: linux-block
- Page: https://ratatoskr.run/linux-block/2026/09/17568865
- Thread: https://ratatoskr.run/linux-block/2026/09/17568303/t.md

```text
On 9/13/26 12:24 PM, Yu Kuai wrote:
> From: Yu Kuai<yukuai@fygo.io>
>
> 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.
>
> 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 a policy path has already pinned BIO_BLKG_REF. 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.
>
> Do not include BIO_BLKG_REF in device-mapper saved bio flags. Remapping
> drops the associated reference, and restoring the bit later would claim
> ownership without reacquiring the reference.
>
> 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@fygo.io>
> Reviewed-by: Tao Cui<redacted>

Looks good to me.
Reviewed-by: Nilay Shroff <redacted>

```
