Skip to content

Commit 9e8c67a

Browse files
committed
sched_ext: Introduce cgroup_lifetime_notifier
Other subsystems may make use of the cgroup hierarchy with the cgroup_bpf support being one such example. For such a feature, it's useful to be able to hook into cgroup creation and destruction paths to perform feature-specific initializations and cleanups. Add cgroup_lifetime_notifier which generates CGROUP_LIFETIME_ONLINE and CGROUP_LIFETIME_OFFLINE events whenever cgroups are created and destroyed, respectively. The next patch will convert cgroup_bpf to use the new notifier and other uses are planned. Signed-off-by: Tejun Heo <tj@kernel.org>
1 parent cd22cba commit 9e8c67a

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

include/linux/cgroup.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/kernfs.h>
2020
#include <linux/jump_label.h>
2121
#include <linux/types.h>
22+
#include <linux/notifier.h>
2223
#include <linux/ns_common.h>
2324
#include <linux/nsproxy.h>
2425
#include <linux/user_namespace.h>
@@ -40,7 +41,7 @@ struct kernel_clone_args;
4041

4142
#ifdef CONFIG_CGROUPS
4243

43-
enum {
44+
enum css_task_iter_flags {
4445
CSS_TASK_ITER_PROCS = (1U << 0), /* walk only threadgroup leaders */
4546
CSS_TASK_ITER_THREADED = (1U << 1), /* walk all threaded css_sets in the domain */
4647
CSS_TASK_ITER_SKIPPED = (1U << 16), /* internal flags */
@@ -66,10 +67,16 @@ struct css_task_iter {
6667
struct list_head iters_node; /* css_set->task_iters */
6768
};
6869

70+
enum cgroup_lifetime_events {
71+
CGROUP_LIFETIME_ONLINE,
72+
CGROUP_LIFETIME_OFFLINE,
73+
};
74+
6975
extern struct file_system_type cgroup_fs_type;
7076
extern struct cgroup_root cgrp_dfl_root;
7177
extern struct css_set init_css_set;
7278
extern spinlock_t css_set_lock;
79+
extern struct blocking_notifier_head cgroup_lifetime_notifier;
7380

7481
#define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys;
7582
#include <linux/cgroup_subsys.h>

kernel/cgroup/cgroup.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ EXPORT_SYMBOL_GPL(cgroup_mutex);
9595
EXPORT_SYMBOL_GPL(css_set_lock);
9696
#endif
9797

98+
struct blocking_notifier_head cgroup_lifetime_notifier =
99+
BLOCKING_NOTIFIER_INIT(cgroup_lifetime_notifier);
100+
98101
DEFINE_SPINLOCK(trace_cgroup_path_lock);
99102
char trace_cgroup_path[TRACE_CGROUP_PATH_LEN];
100103
static bool cgroup_debug __read_mostly;
@@ -1326,6 +1329,7 @@ static void cgroup_destroy_root(struct cgroup_root *root)
13261329
{
13271330
struct cgroup *cgrp = &root->cgrp;
13281331
struct cgrp_cset_link *link, *tmp_link;
1332+
int ret;
13291333

13301334
trace_cgroup_destroy_root(root);
13311335

@@ -1334,6 +1338,10 @@ static void cgroup_destroy_root(struct cgroup_root *root)
13341338
BUG_ON(atomic_read(&root->nr_cgrps));
13351339
BUG_ON(!list_empty(&cgrp->self.children));
13361340

1341+
ret = blocking_notifier_call_chain(&cgroup_lifetime_notifier,
1342+
CGROUP_LIFETIME_OFFLINE, cgrp);
1343+
WARN_ON_ONCE(notifier_to_errno(ret));
1344+
13371345
/* Rebind all subsystems back to the default hierarchy */
13381346
WARN_ON(rebind_subsystems(&cgrp_dfl_root, root->subsys_mask));
13391347

@@ -2140,6 +2148,10 @@ int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask)
21402148
WARN_ON_ONCE(ret);
21412149
}
21422150

2151+
ret = blocking_notifier_call_chain(&cgroup_lifetime_notifier,
2152+
CGROUP_LIFETIME_ONLINE, root_cgrp);
2153+
WARN_ON_ONCE(notifier_to_errno(ret));
2154+
21432155
trace_cgroup_setup_root(root);
21442156

21452157
/*
@@ -5733,6 +5745,15 @@ static struct cgroup *cgroup_create(struct cgroup *parent, const char *name,
57335745
goto out_psi_free;
57345746
}
57355747

5748+
ret = blocking_notifier_call_chain_robust(&cgroup_lifetime_notifier,
5749+
CGROUP_LIFETIME_ONLINE,
5750+
CGROUP_LIFETIME_OFFLINE, cgrp);
5751+
ret = notifier_to_errno(ret);
5752+
if (ret) {
5753+
cgroup_bpf_offline(cgrp);
5754+
goto out_psi_free;
5755+
}
5756+
57365757
/* allocation complete, commit to creation */
57375758
spin_lock_irq(&css_set_lock);
57385759
for (i = 0; i < level; i++) {
@@ -5966,7 +5987,7 @@ static int cgroup_destroy_locked(struct cgroup *cgrp)
59665987
struct cgroup *tcgrp, *parent = cgroup_parent(cgrp);
59675988
struct cgroup_subsys_state *css;
59685989
struct cgrp_cset_link *link;
5969-
int ssid;
5990+
int ssid, ret;
59705991

59715992
lockdep_assert_held(&cgroup_mutex);
59725993

@@ -6027,6 +6048,10 @@ static int cgroup_destroy_locked(struct cgroup *cgrp)
60276048
if (cgrp->root == &cgrp_dfl_root)
60286049
cgroup_bpf_offline(cgrp);
60296050

6051+
ret = blocking_notifier_call_chain(&cgroup_lifetime_notifier,
6052+
CGROUP_LIFETIME_OFFLINE, cgrp);
6053+
WARN_ON_ONCE(notifier_to_errno(ret));
6054+
60306055
/* put the base reference */
60316056
percpu_ref_kill(&cgrp->self.refcnt);
60326057

0 commit comments

Comments
 (0)