Skip to content

Commit ea280e7

Browse files
committed
memcg: update memcg_has_children() to use css_next_child()
Currently, memcg_has_children() and mem_cgroup_hierarchy_write() directly test cgroup->children for list emptiness. It's semantically correct in traditional hierarchies as it actually wants to test for any children dead or alive; however, cgroup->children is not a published field and scheduled to go away. This patch moves out .use_hierarchy test out of memcg_has_children() and updates it to use css_next_child() to test whether there exists any children. With .use_hierarchy test moved out, it can also be used by mem_cgroup_hierarchy_write(). A side note: As .use_hierarchy is going away, it doesn't really matter but I'm not sure about how it's used in __memcg_activate_kmem(). The condition tested by memcg_has_children() is mushy when seen from userland as its result is affected by dead csses which aren't visible from userland. I think the rule would be a lot clearer if we have a dedicated "freshly minted" flag which gets cleared when the first task is migrated into it or the first child is created and then gate activation with that. v2: Added comment noting that testing use_hierarchy is the responsibility of the callers of memcg_has_children() as suggested by Michal Hocko. Signed-off-by: Tejun Heo <tj@kernel.org> Acked-by: Michal Hocko <mhocko@suse.cz> Acked-by: Li Zefan <lizefan@huawei.com> Cc: Johannes Weiner <hannes@cmpxchg.org>
1 parent f61c42a commit ea280e7

1 file changed

Lines changed: 21 additions & 10 deletions

File tree

‎mm/memcontrol.c‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4834,18 +4834,28 @@ static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg)
48344834
} while (usage > 0);
48354835
}
48364836

4837+
/*
4838+
* Test whether @memcg has children, dead or alive. Note that this
4839+
* function doesn't care whether @memcg has use_hierarchy enabled and
4840+
* returns %true if there are child csses according to the cgroup
4841+
* hierarchy. Testing use_hierarchy is the caller's responsiblity.
4842+
*/
48374843
static inline bool memcg_has_children(struct mem_cgroup *memcg)
48384844
{
4839-
lockdep_assert_held(&memcg_create_mutex);
4845+
bool ret;
4846+
48404847
/*
4841-
* The lock does not prevent addition or deletion to the list
4842-
* of children, but it prevents a new child from being
4843-
* initialized based on this parent in css_online(), so it's
4844-
* enough to decide whether hierarchically inherited
4845-
* attributes can still be changed or not.
4848+
* The lock does not prevent addition or deletion of children, but
4849+
* it prevents a new child from being initialized based on this
4850+
* parent in css_online(), so it's enough to decide whether
4851+
* hierarchically inherited attributes can still be changed or not.
48464852
*/
4847-
return memcg->use_hierarchy &&
4848-
!list_empty(&memcg->css.cgroup->children);
4853+
lockdep_assert_held(&memcg_create_mutex);
4854+
4855+
rcu_read_lock();
4856+
ret = css_next_child(NULL, &memcg->css);
4857+
rcu_read_unlock();
4858+
return ret;
48494859
}
48504860

48514861
/*
@@ -4919,7 +4929,7 @@ static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css,
49194929
*/
49204930
if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
49214931
(val == 1 || val == 0)) {
4922-
if (list_empty(&memcg->css.cgroup->children))
4932+
if (!memcg_has_children(memcg))
49234933
memcg->use_hierarchy = val;
49244934
else
49254935
retval = -EBUSY;
@@ -5036,7 +5046,8 @@ static int __memcg_activate_kmem(struct mem_cgroup *memcg,
50365046
* of course permitted.
50375047
*/
50385048
mutex_lock(&memcg_create_mutex);
5039-
if (cgroup_has_tasks(memcg->css.cgroup) || memcg_has_children(memcg))
5049+
if (cgroup_has_tasks(memcg->css.cgroup) ||
5050+
(memcg->use_hierarchy && memcg_has_children(memcg)))
50405051
err = -EBUSY;
50415052
mutex_unlock(&memcg_create_mutex);
50425053
if (err)

0 commit comments

Comments
 (0)