Commit 3c9c825b authored by Tejun Heo's avatar Tejun Heo

cgroup: rename cgroupfs_root->number_of_cgroups to ->nr_cgrps and make it atomic_t

root->number_of_cgroups is currently an integer protected with
cgroup_mutex.  Except for sanity checks and proc reporting, the only
place it's used is to check whether the root has any child during
remount; however, this is a bit flawed as the counter is not
decremented when the cgroup is unlinked but when it's released,
meaning that there could be an extended period where all cgroups are
removed but remount is still not allowed because some internal objects
are lingering.  While not perfect either, it'd be better to use
emptiness test on root->top_cgroup.children.

This patch updates cgroup_remount() to test top_cgroup's children
instead, which makes number_of_cgroups only actual usage statistics
printing in proc implemented in proc_cgroupstats_show().  Let's
shorten its name and make it an atomic_t so that we don't have to
worry about its synchronization.  It's purely auxiliary at this point.
Signed-off-by: default avatarTejun Heo <tj@kernel.org>
Acked-by: default avatarLi Zefan <lizefan@huawei.com>
parent e61734c5
...@@ -290,8 +290,8 @@ struct cgroupfs_root { ...@@ -290,8 +290,8 @@ struct cgroupfs_root {
/* The root cgroup for this hierarchy */ /* The root cgroup for this hierarchy */
struct cgroup top_cgroup; struct cgroup top_cgroup;
/* Tracks how many cgroups are currently defined in hierarchy.*/ /* Number of cgroups in the hierarchy, used only for /proc/cgroups */
int number_of_cgroups; atomic_t nr_cgrps;
/* A list running through the active hierarchies */ /* A list running through the active hierarchies */
struct list_head root_list; struct list_head root_list;
......
...@@ -758,7 +758,7 @@ static void cgroup_put_root(struct cgroupfs_root *root) ...@@ -758,7 +758,7 @@ static void cgroup_put_root(struct cgroupfs_root *root)
} }
mutex_lock(&cgroup_mutex); mutex_lock(&cgroup_mutex);
BUG_ON(root->number_of_cgroups != 1); BUG_ON(atomic_read(&root->nr_cgrps) != 1);
BUG_ON(!list_empty(&cgrp->children)); BUG_ON(!list_empty(&cgrp->children));
/* Rebind all subsystems back to the default hierarchy */ /* Rebind all subsystems back to the default hierarchy */
...@@ -928,9 +928,7 @@ static void cgroup_free_fn(struct work_struct *work) ...@@ -928,9 +928,7 @@ static void cgroup_free_fn(struct work_struct *work)
{ {
struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
mutex_lock(&cgroup_mutex); atomic_dec(&cgrp->root->nr_cgrps);
cgrp->root->number_of_cgroups--;
mutex_unlock(&cgroup_mutex);
/* /*
* We get a ref to the parent, and put the ref when this cgroup is * We get a ref to the parent, and put the ref when this cgroup is
...@@ -1320,7 +1318,7 @@ static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data) ...@@ -1320,7 +1318,7 @@ static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
} }
/* remounting is not allowed for populated hierarchies */ /* remounting is not allowed for populated hierarchies */
if (root->number_of_cgroups > 1) { if (!list_empty(&root->top_cgroup.children)) {
ret = -EBUSY; ret = -EBUSY;
goto out_unlock; goto out_unlock;
} }
...@@ -1360,7 +1358,7 @@ static void init_cgroup_root(struct cgroupfs_root *root) ...@@ -1360,7 +1358,7 @@ static void init_cgroup_root(struct cgroupfs_root *root)
atomic_set(&root->refcnt, 1); atomic_set(&root->refcnt, 1);
INIT_LIST_HEAD(&root->root_list); INIT_LIST_HEAD(&root->root_list);
root->number_of_cgroups = 1; atomic_set(&root->nr_cgrps, 1);
cgrp->root = root; cgrp->root = root;
init_cgroup_housekeeping(cgrp); init_cgroup_housekeeping(cgrp);
idr_init(&root->cgroup_idr); idr_init(&root->cgroup_idr);
...@@ -1463,7 +1461,7 @@ static int cgroup_setup_root(struct cgroupfs_root *root) ...@@ -1463,7 +1461,7 @@ static int cgroup_setup_root(struct cgroupfs_root *root)
write_unlock(&css_set_lock); write_unlock(&css_set_lock);
BUG_ON(!list_empty(&root_cgrp->children)); BUG_ON(!list_empty(&root_cgrp->children));
BUG_ON(root->number_of_cgroups != 1); BUG_ON(atomic_read(&root->nr_cgrps) != 1);
kernfs_activate(root_cgrp->kn); kernfs_activate(root_cgrp->kn);
ret = 0; ret = 0;
...@@ -3709,7 +3707,7 @@ static long cgroup_create(struct cgroup *parent, const char *name, ...@@ -3709,7 +3707,7 @@ static long cgroup_create(struct cgroup *parent, const char *name,
/* allocation complete, commit to creation */ /* allocation complete, commit to creation */
list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
root->number_of_cgroups++; atomic_inc(&root->nr_cgrps);
/* /*
* Grab a reference on the root and parent so that they don't get * Grab a reference on the root and parent so that they don't get
...@@ -4281,7 +4279,7 @@ static int proc_cgroupstats_show(struct seq_file *m, void *v) ...@@ -4281,7 +4279,7 @@ static int proc_cgroupstats_show(struct seq_file *m, void *v)
for_each_subsys(ss, i) for_each_subsys(ss, i)
seq_printf(m, "%s\t%d\t%d\t%d\n", seq_printf(m, "%s\t%d\t%d\t%d\n",
ss->name, ss->root->hierarchy_id, ss->name, ss->root->hierarchy_id,
ss->root->number_of_cgroups, !ss->disabled); atomic_read(&ss->root->nr_cgrps), !ss->disabled);
mutex_unlock(&cgroup_mutex); mutex_unlock(&cgroup_mutex);
return 0; return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment