Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

cgroup: clarify css sibling linkage is protected by cgroup_mutex or RCU

Patch series "Improve mem_cgroup_iter()", v4.

Incremental cgroup iteration is being used again [1]. This patchset
improves the reliability of mem_cgroup_iter(). It also improves
simplicity and code readability.

[1] https://lore.kernel.org/20240514202641.2821494-1-hannes@cmpxchg.org/


This patch (of 5):

Explicitly document that css sibling/descendant linkage is protected by
cgroup_mutex or RCU. Also, document in css_next_descendant_pre() and
similar functions that it isn't necessary to hold a ref on @pos.

The following changes in this patchset rely on this clarification for
simplification in memcg iteration code.

Link: https://lkml.kernel.org/r/20240905003058.1859929-1-kinseyho@google.com
Link: https://lkml.kernel.org/r/20240905003058.1859929-2-kinseyho@google.com
Suggested-by: Yosry Ahmed <yosryahmed@google.com>
Reviewed-by: Michal Koutný <mkoutny@suse.com>
Signed-off-by: Kinsey Ho <kinseyho@google.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Shakeel Butt <shakeel.butt@linux.dev>
Cc: Tejun Heo <tj@kernel.org>
Cc: Zefan Li <lizefan.x@bytedance.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: T.J. Mercier <tjmercier@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Kinsey Ho and committed by
Andrew Morton
0e40cf2a 6e94da94

+14 -8
+5 -1
include/linux/cgroup-defs.h
··· 172 172 /* reference count - access via css_[try]get() and css_put() */ 173 173 struct percpu_ref refcnt; 174 174 175 - /* siblings list anchored at the parent's ->children */ 175 + /* 176 + * siblings list anchored at the parent's ->children 177 + * 178 + * linkage is protected by cgroup_mutex or RCU 179 + */ 176 180 struct list_head sibling; 177 181 struct list_head children; 178 182
+9 -7
kernel/cgroup/cgroup.c
··· 4602 4602 * 4603 4603 * While this function requires cgroup_mutex or RCU read locking, it 4604 4604 * doesn't require the whole traversal to be contained in a single critical 4605 - * section. This function will return the correct next descendant as long 4606 - * as both @pos and @root are accessible and @pos is a descendant of @root. 4605 + * section. Additionally, it isn't necessary to hold onto a reference to @pos. 4606 + * This function will return the correct next descendant as long as both @pos 4607 + * and @root are accessible and @pos is a descendant of @root. 4607 4608 * 4608 4609 * If a subsystem synchronizes ->css_online() and the start of iteration, a 4609 4610 * css which finished ->css_online() is guaranteed to be visible in the ··· 4652 4651 * 4653 4652 * While this function requires cgroup_mutex or RCU read locking, it 4654 4653 * doesn't require the whole traversal to be contained in a single critical 4655 - * section. This function will return the correct rightmost descendant as 4656 - * long as @pos is accessible. 4654 + * section. Additionally, it isn't necessary to hold onto a reference to @pos. 4655 + * This function will return the correct rightmost descendant as long as @pos 4656 + * is accessible. 4657 4657 */ 4658 4658 struct cgroup_subsys_state * 4659 4659 css_rightmost_descendant(struct cgroup_subsys_state *pos) ··· 4698 4696 * 4699 4697 * While this function requires cgroup_mutex or RCU read locking, it 4700 4698 * doesn't require the whole traversal to be contained in a single critical 4701 - * section. This function will return the correct next descendant as long 4702 - * as both @pos and @cgroup are accessible and @pos is a descendant of 4703 - * @cgroup. 4699 + * section. Additionally, it isn't necessary to hold onto a reference to @pos. 4700 + * This function will return the correct next descendant as long as both @pos 4701 + * and @cgroup are accessible and @pos is a descendant of @cgroup. 4704 4702 * 4705 4703 * If a subsystem synchronizes ->css_online() and the start of iteration, a 4706 4704 * css which finished ->css_online() is guaranteed to be visible in the