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

Commit a3937e3

Browse files
Carlos LlamasTreehugger Robot
authored andcommitted
Reapply "cgroup: Make operations on the cgroup root_list RCU safe"
This reverts commit 0475cf0. Bring back upstream commit dd9542a ("cgroup: Make operations on the cgroup root_list RCU safe"). The ABI issues introduced by this patch are addressed in a subsequent patch. Bug: 379227997 Change-Id: I136f602437262162b850d84d6750b2f0b5ccc856 Signed-off-by: Carlos Llamas <[email protected]>
1 parent f975b38 commit a3937e3

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
@@ -575,6 +575,7 @@ struct cgroup_root {
575575

576576
/* A list running through the active hierarchies */
577577
struct list_head root_list;
578+
struct rcu_head rcu;
578579

579580
/* Hierarchy-specific flags */
580581
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
@@ -1317,7 +1317,7 @@ static void cgroup_exit_root_id(struct cgroup_root *root)
13171317

13181318
void cgroup_free_root(struct cgroup_root *root)
13191319
{
1320-
kfree(root);
1320+
kfree_rcu(root, rcu);
13211321
}
13221322

13231323
static void cgroup_destroy_root(struct cgroup_root *root)
@@ -1350,7 +1350,7 @@ static void cgroup_destroy_root(struct cgroup_root *root)
13501350
spin_unlock_irq(&css_set_lock);
13511351

13521352
if (!list_empty(&root->root_list)) {
1353-
list_del(&root->root_list);
1353+
list_del_rcu(&root->root_list);
13541354
cgroup_root_count--;
13551355
}
13561356

@@ -1390,7 +1390,15 @@ static inline struct cgroup *__cset_cgroup_from_root(struct css_set *cset,
13901390
}
13911391
}
13921392

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

@@ -1449,15 +1457,16 @@ static struct cgroup *current_cgns_cgroup_dfl(void)
14491457
static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
14501458
struct cgroup_root *root)
14511459
{
1452-
lockdep_assert_held(&cgroup_mutex);
14531460
lockdep_assert_held(&css_set_lock);
14541461

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

14581465
/*
14591466
* Return the cgroup for "task" from the given hierarchy. Must be
1460-
* called with cgroup_mutex and css_set_lock held.
1467+
* called with css_set_lock held to prevent task's groups from being modified.
1468+
* Must be called with either cgroup_mutex or rcu read lock to prevent the
1469+
* cgroup root from being destroyed.
14611470
*/
14621471
struct cgroup *task_cgroup_from_root(struct task_struct *task,
14631472
struct cgroup_root *root)
@@ -2018,7 +2027,7 @@ void init_cgroup_root(struct cgroup_fs_context *ctx)
20182027
struct cgroup_root *root = ctx->root;
20192028
struct cgroup *cgrp = &root->cgrp;
20202029

2021-
INIT_LIST_HEAD(&root->root_list);
2030+
INIT_LIST_HEAD_RCU(&root->root_list);
20222031
atomic_set(&root->nr_cgrps, 1);
20232032
cgrp->root = root;
20242033
init_cgroup_housekeeping(cgrp);
@@ -2101,7 +2110,7 @@ int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask)
21012110
* care of subsystems' refcounts, which are explicitly dropped in
21022111
* the failure exit path.
21032112
*/
2104-
list_add(&root->root_list, &cgroup_roots);
2113+
list_add_rcu(&root->root_list, &cgroup_roots);
21052114
cgroup_root_count++;
21062115

21072116
/*

0 commit comments

Comments
 (0)