sched_ext: allow ops.cgroup_set_bandwidth() to be sleepable
Subsystems (2)
Changwoo Min
reviewerPATCH 1/1 sched_ext: allow ops.cgroup_set_bandwidth() to be sleepable
ops.cgroup_set_bandwidth() is delivered from scx_group_set_bandwidth(), which runs from the cpu.max cgroup interface write path (tg_set_bandwidth()) in process context. tg_set_cfs_bandwidth() has already returned by then, so its cpus_read_lock and cfs_constraints_mutex are released, and the only lock held is percpu_down_read(&scx_cgroup_ops_rwsem), whose read side may sleep. The call site is therefore sleepable, like ops.cgroup_init().
bpf_scx_check_member() rejects a sleepable program on any member not on its allow-list, so a BPF scheduler cannot allocate -- which is sleepable -- when a cgroup gains a cpu.max limit at runtime; it must instead pre-reserve memory for a callback that cannot allocate. Add cgroup_set_bandwidth() to the allow-list so the callback can allocate on demand, and document that it may block.
A scheduler must decide at load time whether to mark the callback sleepable, but the allow-list entry is a verifier property with no symbol to probe. Add scx_cgroup_set_bandwidth_may_sleep() as a marker whose presence in the kernel's BTF lets userspace detect this support; it has no callers and does nothing.
--- kernel/sched/ext/ext.c | 10 ++++++++++ kernel/sched/ext/ext.h | 1 + kernel/sched/ext/internal.h | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-)
kernel/sched/ext/ext.c +10 -0
@@ -4960,6 +4960,15 @@ void scx_group_set_bandwidth(struct task_group *tg, percpu_up_read(&scx_cgroup_ops_rwsem); } + +/* +* Capability marker for userspace. The sleepable allowance for +* ops.cgroup_set_bandwidth() (see bpf_scx_check_member()) is a verifier +* property with no other symbol a scheduler can probe, so this no-op function +* exists solely so its presence in the kernel's BTF can be detected. It has no +* callers; __used keeps it from being optimized away. +*/ +__used void scx_cgroup_set_bandwidth_may_sleep (void ) {}#endif /* CONFIG_EXT_GROUP_SCHED */ #if defined(CONFIG_EXT_GROUP_SCHED ) || defined(CONFIG_EXT_SUB_SCHED )
@@ -8079,6 +8088,7 @@ static int bpf_scx_check_member(const struct btf_type *t,case offsetof(struct sched_ext_ops , cgroup_init):case offsetof(struct sched_ext_ops , cgroup_exit):case offsetof(struct sched_ext_ops , cgroup_prep_move): +case offsetof(struct sched_ext_ops , cgroup_set_bandwidth):#endif case offsetof(struct sched_ext_ops , cpu_online):case offsetof(struct sched_ext_ops , cpu_offline):
kernel/sched/ext/ext.h +1 -0
@@ -81,6 +81,7 @@ void scx_cgroup_cancel_attach(struct cgroup_taskset *tset);void scx_group_set_weight (struct task_group *tg,unsigned long cgrp_weight);void scx_group_set_idle (struct task_group *tg,bool idle);void scx_group_set_bandwidth (struct task_group *tg,u64 period_us,u64 quota_us,u64 burst_us); +void scx_cgroup_set_bandwidth_may_sleep (void );#else /* CONFIG_EXT_GROUP_SCHED */ static inline void scx_tg_init (struct task_group *tg) {}static inline int scx_tg_online (struct task_group *tg) {return 0 ; }
kernel/sched/ext/internal.h +1 -1
@@ -753,7 +753,7 @@ struct sched_ext_ops {* @burst_us: bandwidth control burst * * Update @cgrp's bandwidth control parameters. This is from the cpu.max -* cgroup interface. +* cgroup interface. This operation may block. * * @quota_us / @period_us determines the CPU bandwidth @cgrp is entitled * to. For example, if @period_us is 1_000_000 and @quota_us is
Signature · 1 line
2.55.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Concurrent writes to the cgroup cpu.max interface can race, causing desynchronization between CFS and SCX bandwidth states and out-of-order invocations of the BPF callback. --
--- Patch [1]: [PATCH] sched_ext: allow ops.cgroup_set_bandwidth() to be sleepable --- commit 1a287753fea54decaf90a63415735694726dc348
Author: Changwoo Min <[email protected]>
sched_ext: allow ops.cgroup_set_bandwidth() to be sleepable
This commit allows the cgroup_set_bandwidth BPF scheduler operation to sleep. It adds a BTF marker function for userspace to detect this capability and documents the potential blocking behavior.
kernel/sched/ext/ext.c void scx_group_set_bandwidth(struct task_group *tg, · line 4574
Quoted 8 lines
kernel/sched/ext/ext.c +0
-0@@ -4572,6 +4572,15 @@ void scx_group_set_bandwidth(struct task_group *tg,percpu_up_read (&scx_cgroup_ops_rwsem); }
[Severity: High]
This is a pre-existing issue, but does making this operation sleepable widen the window for a race condition between concurrent cpu.max writers?
The commit message notes that cfs_constraints_mutex has already been released by tg_set_cfs_bandwidth(), leaving only percpu_down_read(&scx_cgroup_ops_rwsem) held. Since this is a read lock, concurrent writers to the same cgroup's cpu.max interface can execute scx_group_set_bandwidth() at the same time.
If two threads write to cpu.max, they are serialized in tg_set_cfs_bandwidth(), but can race here:
CPU1 CPU2tg_set_cfs_bandwidth() (writer A)
tg_set_cfs_bandwidth() (writer B)scx_group_set_bandwidth()
percpu_down_read()
SCX_CALL_OP(...) // blocks
scx_group_set_bandwidth()
percpu_down_read()
SCX_CALL_OP(...) // returns quickly
tg->scx.bw_quota_us = ... tg->scx.bw_quota_us = ...
Can this lead to the BPF callback executing out of order, causing desynchronization between the CFS bandwidth state and the SCX scheduler state?
Additionally, on 32-bit architectures, could updating the 64-bit tg->scx.bw_period_us, bw_quota_us, and bw_burst_us fields without a write lock result in torn writes?
Quoted 9 lines
kernel/sched/ext/ext.c +9
-0+ +/* +* Capability marker for userspace. The sleepable allowance for +* ops.cgroup_set_bandwidth() (see bpf_scx_check_member()) is a verifier +* property with no other symbol a scheduler can probe, so this no-op function +* exists solely so its presence in the kernel's BTF can be detected. It has no +* callers; __used keeps it from being optimized away. +*/ +__used void scx_cgroup_set_bandwidth_may_sleep (void ) {}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Tejun Heo
reviewerHello,
kernel/sched/ext/ext.c void scx_group_set_bandwidth(struct task_group *tg, · line 4971
Quoted 8 lines · Changwoo Min
kernel/sched/ext/ext.c +8
-0+/* +* Capability marker for userspace. The sleepable allowance for +* ops.cgroup_set_bandwidth() (see bpf_scx_check_member()) is a verifier +* property with no other symbol a scheduler can probe, so this no-op function +* exists solely so its presence in the kernel's BTF can be detected. It has no +* callers; __used keeps it from being optimized away. +*/ +__used void scx_cgroup_set_bandwidth_may_sleep (void ) {}
__used only prevents the compiler from dropping the function. With CONFIG_LD_DEAD_CODE_DATA_ELIMINATION, the linker can still garbage collect its section, so please add __retain too (cf. __bpf_kfunc).
Also, we'll likely accumulate more of these markers over time, so let's give them a shared prefix - scx_compat_marker_ or something like that - and collect them in one place, maybe towards the end of ext.c next to the module init code.
Thanks.
--
tejun
Changwoo Min
reviewerHello,
Quoted 16 lines · Tejun Heo
Hello,
kernel/sched/ext/ext.c void scx_group_set_bandwidth(struct task_group *tg, · line 4971
Quoted 8 lines · Changwoo Min
kernel/sched/ext/ext.c +8
-0+/* +* Capability marker for userspace. The sleepable allowance for +* ops.cgroup_set_bandwidth() (see bpf_scx_check_member()) is a verifier +* property with no other symbol a scheduler can probe, so this no-op function +* exists solely so its presence in the kernel's BTF can be detected. It has no +* callers; __used keeps it from being optimized away. +*/ +__used void scx_cgroup_set_bandwidth_may_sleep (void ) {}
__usedonly prevents the compiler from dropping the function. WithCONFIG_LD_DEAD_CODE_DATA_ELIMINATION, the linker can still garbage collect its section, so please add__retaintoo (cf.__bpf_kfunc).
Quoted 4 lines
Also, we'll likely accumulate more of these markers over time, so let's give them a shared prefix -
scx_compat_marker_or something like that - and collect them in one place, maybe towards the end ofext.cnext to the module init code.
Thanks for the comments. I will address these two and send v2 shortly. Regarding the sashiko review, I think that needs to be addressed separately.
Regards,
Changwoo Min