Skip to content

Commit d23b5c5

Browse files
laoarhtejun
authored andcommitted
cgroup: Make operations on the cgroup root_list RCU safe
At present, when we perform operations on the cgroup root_list, we must hold the cgroup_mutex, which is a relatively heavyweight lock. In reality, we can make operations on this list RCU-safe, eliminating the need to hold the cgroup_mutex during traversal. Modifications to the list only occur in the cgroup root setup and destroy paths, which should be infrequent in a production environment. In contrast, traversal may occur frequently. Therefore, making it RCU-safe would be beneficial. Signed-off-by: Yafang Shao <[email protected]> Signed-off-by: Tejun Heo <[email protected]>
1 parent 96a2b48 commit d23b5c5

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

include/linux/cgroup-defs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ struct cgroup_root {
563563

564564
/* A list running through the active hierarchies */
565565
struct list_head root_list;
566+
struct rcu_head rcu;
566567

567568
/* Hierarchy-specific flags */
568569
unsigned int flags;

kernel/cgroup/cgroup-internal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ extern struct list_head cgroup_roots;
170170

171171
/* iterate across the hierarchies */
172172
#define for_each_root(root) \
173-
list_for_each_entry((root), &cgroup_roots, root_list)
173+
list_for_each_entry_rcu((root), &cgroup_roots, root_list, \
174+
lockdep_is_held(&cgroup_mutex))
174175

175176
/**
176177
* for_each_subsys - iterate all enabled cgroup subsystems

kernel/cgroup/cgroup.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,7 @@ static void cgroup_exit_root_id(struct cgroup_root *root)
13151315

13161316
void cgroup_free_root(struct cgroup_root *root)
13171317
{
1318-
kfree(root);
1318+
kfree_rcu(root, rcu);
13191319
}
13201320

13211321
static void cgroup_destroy_root(struct cgroup_root *root)
@@ -1348,7 +1348,7 @@ static void cgroup_destroy_root(struct cgroup_root *root)
13481348
spin_unlock_irq(&css_set_lock);
13491349

13501350
WARN_ON_ONCE(list_empty(&root->root_list));
1351-
list_del(&root->root_list);
1351+
list_del_rcu(&root->root_list);
13521352
cgroup_root_count--;
13531353

13541354
if (!have_favordynmods)
@@ -1389,7 +1389,15 @@ static inline struct cgroup *__cset_cgroup_from_root(struct css_set *cset,
13891389
}
13901390
}
13911391

1392-
BUG_ON(!res_cgroup);
1392+
/*
1393+
* If cgroup_mutex is not held, the cgrp_cset_link will be freed
1394+
* before we remove the cgroup root from the root_list. Consequently,
1395+
* when accessing a cgroup root, the cset_link may have already been
1396+
* freed, resulting in a NULL res_cgroup. However, by holding the
1397+
* cgroup_mutex, we ensure that res_cgroup can't be NULL.
1398+
* If we don't hold cgroup_mutex in the caller, we must do the NULL
1399+
* check.
1400+
*/
13931401
return res_cgroup;
13941402
}
13951403

@@ -1448,15 +1456,16 @@ static struct cgroup *current_cgns_cgroup_dfl(void)
14481456
static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
14491457
struct cgroup_root *root)
14501458
{
1451-
lockdep_assert_held(&cgroup_mutex);
14521459
lockdep_assert_held(&css_set_lock);
14531460

14541461
return __cset_cgroup_from_root(cset, root);
14551462
}
14561463

14571464
/*
14581465
* Return the cgroup for "task" from the given hierarchy. Must be
1459-
* called with cgroup_mutex and css_set_lock held.
1466+
* called with css_set_lock held to prevent task's groups from being modified.
1467+
* Must be called with either cgroup_mutex or rcu read lock to prevent the
1468+
* cgroup root from being destroyed.
14601469
*/
14611470
struct cgroup *task_cgroup_from_root(struct task_struct *task,
14621471
struct cgroup_root *root)
@@ -2031,7 +2040,7 @@ void init_cgroup_root(struct cgroup_fs_context *ctx)
20312040
struct cgroup_root *root = ctx->root;
20322041
struct cgroup *cgrp = &root->cgrp;
20332042

2034-
INIT_LIST_HEAD(&root->root_list);
2043+
INIT_LIST_HEAD_RCU(&root->root_list);
20352044
atomic_set(&root->nr_cgrps, 1);
20362045
cgrp->root = root;
20372046
init_cgroup_housekeeping(cgrp);
@@ -2114,7 +2123,7 @@ int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask)
21142123
* care of subsystems' refcounts, which are explicitly dropped in
21152124
* the failure exit path.
21162125
*/
2117-
list_add(&root->root_list, &cgroup_roots);
2126+
list_add_rcu(&root->root_list, &cgroup_roots);
21182127
cgroup_root_count++;
21192128

21202129
/*

0 commit comments

Comments
 (0)