Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit dd9542a

Browse files
laoargregkh
authored andcommitted
cgroup: Make operations on the cgroup root_list RCU safe
commit d23b5c5 upstream. 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]> To: Michal Koutný <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent bcd5148 commit dd9542a

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
@@ -558,6 +558,7 @@ struct cgroup_root {
558558

559559
/* A list running through the active hierarchies */
560560
struct list_head root_list;
561+
struct rcu_head rcu;
561562

562563
/* Hierarchy-specific flags */
563564
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
@@ -1313,7 +1313,7 @@ static void cgroup_exit_root_id(struct cgroup_root *root)
13131313

13141314
void cgroup_free_root(struct cgroup_root *root)
13151315
{
1316-
kfree(root);
1316+
kfree_rcu(root, rcu);
13171317
}
13181318

13191319
static void cgroup_destroy_root(struct cgroup_root *root)
@@ -1346,7 +1346,7 @@ static void cgroup_destroy_root(struct cgroup_root *root)
13461346
spin_unlock_irq(&css_set_lock);
13471347

13481348
if (!list_empty(&root->root_list)) {
1349-
list_del(&root->root_list);
1349+
list_del_rcu(&root->root_list);
13501350
cgroup_root_count--;
13511351
}
13521352

@@ -1386,7 +1386,15 @@ static inline struct cgroup *__cset_cgroup_from_root(struct css_set *cset,
13861386
}
13871387
}
13881388

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

@@ -1445,15 +1453,16 @@ static struct cgroup *current_cgns_cgroup_dfl(void)
14451453
static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
14461454
struct cgroup_root *root)
14471455
{
1448-
lockdep_assert_held(&cgroup_mutex);
14491456
lockdep_assert_held(&css_set_lock);
14501457

14511458
return __cset_cgroup_from_root(cset, root);
14521459
}
14531460

14541461
/*
14551462
* Return the cgroup for "task" from the given hierarchy. Must be
1456-
* called with cgroup_mutex and css_set_lock held.
1463+
* called with css_set_lock held to prevent task's groups from being modified.
1464+
* Must be called with either cgroup_mutex or rcu read lock to prevent the
1465+
* cgroup root from being destroyed.
14571466
*/
14581467
struct cgroup *task_cgroup_from_root(struct task_struct *task,
14591468
struct cgroup_root *root)
@@ -2014,7 +2023,7 @@ void init_cgroup_root(struct cgroup_fs_context *ctx)
20142023
struct cgroup_root *root = ctx->root;
20152024
struct cgroup *cgrp = &root->cgrp;
20162025

2017-
INIT_LIST_HEAD(&root->root_list);
2026+
INIT_LIST_HEAD_RCU(&root->root_list);
20182027
atomic_set(&root->nr_cgrps, 1);
20192028
cgrp->root = root;
20202029
init_cgroup_housekeeping(cgrp);
@@ -2097,7 +2106,7 @@ int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask)
20972106
* care of subsystems' refcounts, which are explicitly dropped in
20982107
* the failure exit path.
20992108
*/
2100-
list_add(&root->root_list, &cgroup_roots);
2109+
list_add_rcu(&root->root_list, &cgroup_roots);
21012110
cgroup_root_count++;
21022111

21032112
/*

0 commit comments

Comments
 (0)