[PATCH 2/2] cgroup: Remove nr_cgrps

From: T.J. Mercier
Date: Tue May 28 2024 - 12:38:13 EST


nr_cgrps now largely overlaps with nr_css. Use nr_css instead of
nr_cgrps for v1 so that nr_cgrps can be removed.

Signed-off-by: T.J. Mercier <tjmercier@xxxxxxxxxx>
---
include/linux/cgroup-defs.h | 3 ---
kernel/cgroup/cgroup-v1.c | 8 ++------
kernel/cgroup/cgroup.c | 31 +++++++++++++++++++++++++------
3 files changed, 27 insertions(+), 15 deletions(-)

diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h
index bc1dbf7652c4..dcd47a717eac 100644
--- a/include/linux/cgroup-defs.h
+++ b/include/linux/cgroup-defs.h
@@ -576,9 +576,6 @@ struct cgroup_root {
/* must follow cgrp for cgrp->ancestors[0], see above */
struct cgroup *cgrp_ancestor_storage;

- /* Number of cgroups in the hierarchy, used only for /proc/cgroups */
- atomic_t nr_cgrps;
-
/*
* Number of cgroups using each controller. Includes online and zombies.
* Used only for /proc/cgroups.
diff --git a/kernel/cgroup/cgroup-v1.c b/kernel/cgroup/cgroup-v1.c
index 9bad59486c46..d52dc62803c3 100644
--- a/kernel/cgroup/cgroup-v1.c
+++ b/kernel/cgroup/cgroup-v1.c
@@ -675,15 +675,11 @@ int proc_cgroupstats_show(struct seq_file *m, void *v)
* cgroup_mutex contention.
*/

- for_each_subsys(ss, i) {
- int count = cgroup_on_dfl(&ss->root->cgrp) ?
- atomic_read(&ss->root->nr_css[i]) : atomic_read(&ss->root->nr_cgrps);
-
+ for_each_subsys(ss, i)
seq_printf(m, "%s\t%d\t%d\t%d\n",
ss->legacy_name, ss->root->hierarchy_id,
- count,
+ atomic_read(&ss->root->nr_css[i]),
cgroup_ssid_enabled(i));
- }

return 0;
}
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index 1bacd7cf7551..fb4510a28ea3 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -1322,12 +1322,15 @@ static void cgroup_destroy_root(struct cgroup_root *root)
{
struct cgroup *cgrp = &root->cgrp;
struct cgrp_cset_link *link, *tmp_link;
+ struct cgroup_subsys *ss;
+ int ssid;

trace_cgroup_destroy_root(root);

cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);

- BUG_ON(atomic_read(&root->nr_cgrps));
+ for_each_subsys(ss, ssid)
+ BUG_ON(atomic_read(&root->nr_css[ssid]));
BUG_ON(!list_empty(&cgrp->self.children));

/* Rebind all subsystems back to the default hierarchy */
@@ -1874,6 +1877,7 @@ int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask)
} else {
dcgrp->subtree_control |= 1 << ssid;
static_branch_disable(cgroup_subsys_on_dfl_key[ssid]);
+ atomic_set(&ss->root->nr_css[ssid], 1);
}

ret = cgroup_apply_control(dcgrp);
@@ -2046,7 +2050,6 @@ void init_cgroup_root(struct cgroup_fs_context *ctx)
struct cgroup *cgrp = &root->cgrp;

INIT_LIST_HEAD_RCU(&root->root_list);
- atomic_set(&root->nr_cgrps, 1);
cgrp->root = root;
init_cgroup_housekeeping(cgrp);

@@ -2065,6 +2068,7 @@ int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask)
LIST_HEAD(tmp_links);
struct cgroup *root_cgrp = &root->cgrp;
struct kernfs_syscall_ops *kf_sops;
+ struct cgroup_subsys *ss;
struct css_set *cset;
int i, ret;

@@ -2144,7 +2148,9 @@ int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask)
spin_unlock_irq(&css_set_lock);

BUG_ON(!list_empty(&root_cgrp->self.children));
- BUG_ON(atomic_read(&root->nr_cgrps) != 1);
+ do_each_subsys_mask(ss, i, ss_mask) {
+ BUG_ON(atomic_read(&root->nr_css[i]) != 1);
+ } while_each_subsys_mask();

ret = 0;
goto out;
@@ -5368,7 +5374,6 @@ static void css_free_rwork_fn(struct work_struct *work)
css_put(parent);
} else {
/* cgroup free path */
- atomic_dec(&cgrp->root->nr_cgrps);
if (!cgroup_on_dfl(cgrp))
cgroup1_pidlist_destroy_all(cgrp);
cancel_work_sync(&cgrp->release_agent_work);
@@ -5387,12 +5392,27 @@ static void css_free_rwork_fn(struct work_struct *work)
cgroup_rstat_exit(cgrp);
kfree(cgrp);
} else {
+ struct cgroup_root *root = cgrp->root;
/*
* This is root cgroup's refcnt reaching zero,
* which indicates that the root should be
* released.
*/
- cgroup_destroy_root(cgrp->root);
+
+ /*
+ * v1 root css are first onlined as v2, then rebound
+ * to v1 (without re-onlining) where their count is
+ * initialized to 1. Drop the root counters to 0
+ * before destroying v1 roots.
+ */
+ if (root != &cgrp_dfl_root) {
+ int ssid;
+
+ do_each_subsys_mask(ss, ssid, root->subsys_mask) {
+ atomic_dec(&root->nr_css[ssid]);
+ } while_each_subsys_mask();
+ }
+ cgroup_destroy_root(root);
}
}
}
@@ -5678,7 +5698,6 @@ static struct cgroup *cgroup_create(struct cgroup *parent, const char *name,

/* allocation complete, commit to creation */
list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
- atomic_inc(&root->nr_cgrps);
cgroup_get_live(parent);

/*
--
2.45.1.288.g0e0cd299f1-goog