Skip to content

Commit 82648b8

Browse files
committed
sched_ext: Convert cgroup BPF support to use cgroup_lifetime_notifier
Replace explicit cgroup_bpf_inherit/offline() calls from cgroup creation/destruction paths with notification callback registered on cgroup_lifetime_notifier. Signed-off-by: Tejun Heo <tj@kernel.org>
1 parent 9e8c67a commit 82648b8

3 files changed

Lines changed: 44 additions & 23 deletions

File tree

include/linux/bpf-cgroup.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ struct bpf_prog_list {
114114
u32 flags;
115115
};
116116

117-
int cgroup_bpf_inherit(struct cgroup *cgrp);
118-
void cgroup_bpf_offline(struct cgroup *cgrp);
117+
void __init cgroup_bpf_lifetime_notifier_init(void);
119118

120119
int __cgroup_bpf_run_filter_skb(struct sock *sk,
121120
struct sk_buff *skb,
@@ -431,8 +430,10 @@ const struct bpf_func_proto *
431430
cgroup_current_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog);
432431
#else
433432

434-
static inline int cgroup_bpf_inherit(struct cgroup *cgrp) { return 0; }
435-
static inline void cgroup_bpf_offline(struct cgroup *cgrp) {}
433+
static inline void cgroup_bpf_lifetime_notifier_init(void)
434+
{
435+
return;
436+
}
436437

437438
static inline int cgroup_bpf_prog_attach(const union bpf_attr *attr,
438439
enum bpf_prog_type ptype,

kernel/bpf/cgroup.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ static int __init cgroup_bpf_wq_init(void)
4141
}
4242
core_initcall(cgroup_bpf_wq_init);
4343

44+
static int cgroup_bpf_lifetime_notify(struct notifier_block *nb,
45+
unsigned long action, void *data);
46+
47+
static struct notifier_block cgroup_bpf_lifetime_nb = {
48+
.notifier_call = cgroup_bpf_lifetime_notify,
49+
};
50+
51+
void __init cgroup_bpf_lifetime_notifier_init(void)
52+
{
53+
BUG_ON(blocking_notifier_chain_register(&cgroup_lifetime_notifier,
54+
&cgroup_bpf_lifetime_nb));
55+
}
56+
4457
/* __always_inline is necessary to prevent indirect call through run_prog
4558
* function pointer.
4659
*/
@@ -206,7 +219,7 @@ bpf_cgroup_atype_find(enum bpf_attach_type attach_type, u32 attach_btf_id)
206219
}
207220
#endif /* CONFIG_BPF_LSM */
208221

209-
void cgroup_bpf_offline(struct cgroup *cgrp)
222+
static void cgroup_bpf_offline(struct cgroup *cgrp)
210223
{
211224
cgroup_get(cgrp);
212225
percpu_ref_kill(&cgrp->bpf.refcnt);
@@ -491,7 +504,7 @@ static void activate_effective_progs(struct cgroup *cgrp,
491504
* cgroup_bpf_inherit() - inherit effective programs from parent
492505
* @cgrp: the cgroup to modify
493506
*/
494-
int cgroup_bpf_inherit(struct cgroup *cgrp)
507+
static int cgroup_bpf_inherit(struct cgroup *cgrp)
495508
{
496509
/* has to use marco instead of const int, since compiler thinks
497510
* that array below is variable length
@@ -534,6 +547,27 @@ int cgroup_bpf_inherit(struct cgroup *cgrp)
534547
return -ENOMEM;
535548
}
536549

550+
static int cgroup_bpf_lifetime_notify(struct notifier_block *nb,
551+
unsigned long action, void *data)
552+
{
553+
struct cgroup *cgrp = data;
554+
int ret = 0;
555+
556+
if (cgrp->root != &cgrp_dfl_root)
557+
return NOTIFY_OK;
558+
559+
switch (action) {
560+
case CGROUP_LIFETIME_ONLINE:
561+
ret = cgroup_bpf_inherit(cgrp);
562+
break;
563+
case CGROUP_LIFETIME_OFFLINE:
564+
cgroup_bpf_offline(cgrp);
565+
break;
566+
}
567+
568+
return notifier_from_errno(ret);
569+
}
570+
537571
static int update_effective_progs(struct cgroup *cgrp,
538572
enum cgroup_bpf_attach_type atype)
539573
{

kernel/cgroup/cgroup.c

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,11 +2143,6 @@ int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask)
21432143
if (ret)
21442144
goto exit_stats;
21452145

2146-
if (root == &cgrp_dfl_root) {
2147-
ret = cgroup_bpf_inherit(root_cgrp);
2148-
WARN_ON_ONCE(ret);
2149-
}
2150-
21512146
ret = blocking_notifier_call_chain(&cgroup_lifetime_notifier,
21522147
CGROUP_LIFETIME_ONLINE, root_cgrp);
21532148
WARN_ON_ONCE(notifier_to_errno(ret));
@@ -5739,20 +5734,12 @@ static struct cgroup *cgroup_create(struct cgroup *parent, const char *name,
57395734

57405735
cgrp->self.serial_nr = css_serial_nr_next++;
57415736

5742-
if (cgrp->root == &cgrp_dfl_root) {
5743-
ret = cgroup_bpf_inherit(cgrp);
5744-
if (ret)
5745-
goto out_psi_free;
5746-
}
5747-
57485737
ret = blocking_notifier_call_chain_robust(&cgroup_lifetime_notifier,
57495738
CGROUP_LIFETIME_ONLINE,
57505739
CGROUP_LIFETIME_OFFLINE, cgrp);
57515740
ret = notifier_to_errno(ret);
5752-
if (ret) {
5753-
cgroup_bpf_offline(cgrp);
5741+
if (ret)
57545742
goto out_psi_free;
5755-
}
57565743

57575744
/* allocation complete, commit to creation */
57585745
spin_lock_irq(&css_set_lock);
@@ -6045,9 +6032,6 @@ static int cgroup_destroy_locked(struct cgroup *cgrp)
60456032

60466033
cgroup1_check_for_release(parent);
60476034

6048-
if (cgrp->root == &cgrp_dfl_root)
6049-
cgroup_bpf_offline(cgrp);
6050-
60516035
ret = blocking_notifier_call_chain(&cgroup_lifetime_notifier,
60526036
CGROUP_LIFETIME_OFFLINE, cgrp);
60536037
WARN_ON_ONCE(notifier_to_errno(ret));
@@ -6206,6 +6190,8 @@ int __init cgroup_init(void)
62066190
hash_add(css_set_table, &init_css_set.hlist,
62076191
css_set_hash(init_css_set.subsys));
62086192

6193+
cgroup_bpf_lifetime_notifier_init();
6194+
62096195
BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
62106196

62116197
cgroup_unlock();

0 commit comments

Comments
 (0)