tools/sched_ext: Add __COMPAT wrapper for scx_bpf_sub_dispatch()
PATCH 1/1 tools/sched_ext: Add __COMPAT wrapper for scx_bpf_sub_dispatch()
Add __COMPAT_scx_bpf_sub_dispatch() to compat.bpf.h so that schedulers using sub-sched dispatch can build and run on older kernels that lack the kfunc. Guard the call with CONFIG_EXT_SUB_SCHED and bpf_ksym_exists(), falling back to false when unavailable.
Update scx_qmap to use the new wrapper instead of calling scx_bpf_sub_dispatch() directly.
--- tools/sched_ext/include/scx/compat.bpf.h | 13 +++++++++++++ tools/sched_ext/scx_qmap.bpf.c | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-)
tools/sched_ext/include/scx/compat.bpf.h +13 -0
@@ -108,6 +108,19 @@ static inline struct task_struct *__COMPAT_scx_bpf_dsq_peek(u64 dsq_id)return p; } +/* +* v7.1: scx_bpf_sub_dispatch() for sub-sched dispatch. Preserve until we drop +* the compat layer for older kernels that lack the kfunc. +*/ +static inline bool __COMPAT_scx_bpf_sub_dispatch (u64 cgroup_id) +{ +#ifdef CONFIG_EXT_SUB_SCHED +if (bpf_ksym_exists (scx_bpf_sub_dispatch)) +return scx_bpf_sub_dispatch (cgroup_id); +#endif +return false; +} +/** * __COMPAT_is_enq_cpu_selected - Test if SCX_ENQ_CPU_SELECTED is on * in a compatible way. We will preserve this __COMPAT helper until v6.16.
tools/sched_ext/scx_qmap.bpf.c +1 -1
@@ -490,7 +490,7 @@ void BPF_STRUCT_OPS(qmap_dispatch, s32 cpu, struct task_struct *prev)for (i =0 ; i <MAX_SUB_SCHEDS ; i++) {if (sub_sched_cgroup_ids[i] && -scx_bpf_sub_dispatch (sub_sched_cgroup_ids[i])) +__COMPAT_scx_bpf_sub_dispatch (sub_sched_cgroup_ids[i]))return ; }
Signature · 1 line
2.48.1
Tejun Heo
reviewerHello,
Let's not use the __COMPAT prefix. I'd like to move toward making compat wrappers transparent so that schedulers don't need code changes for compatibility as much as reasonably possible.
Instead, declare the kfunc with a ___compat suffix and provide a static inline wrapper with the original name, like scx_bpf_dsq_insert() does:
bool scx_bpf_sub_dispatch___compat(u64 cgroup_id) __ksym __weak;
static inline bool scx_bpf_sub_dispatch(u64 cgroup_id)
{
if (bpf_ksym_exists(scx_bpf_sub_dispatch___compat))
return scx_bpf_sub_dispatch___compat(cgroup_id);
return false;
}See tools/sched_ext/include/scx/compat.bpf.h around line 326-342 for the full pattern.
This way schedulers just call scx_bpf_sub_dispatch() directly and the compat layer handles everything. The no-op fallback (returning false) is fine here since without sub-sched support the dispatch path can't do anything useful anyway.
Thanks.
--
tejun
Quoted 5 lines · Tejun Heo
Hello,
Let's not use the
__COMPATprefix. I'd like to move toward making compat wrappers transparent so that schedulers don't need code changes for compatibility as much as reasonably possible.
I think this might be another good first issue for me to tackle next :-) I'll keep the v2 patch focused only on the scx_bpf_sub_dispatch() change, and then I can go on to take a look at refactoring the rest of these __COMPAT wrappers.
Quoted 13 lines
Instead, declare the kfunc with a
___compatsuffix and provide a static inline wrapper with the original name, likescx_bpf_dsq_insert()does:bool scx_bpf_sub_dispatch___compat(u64 cgroup_id) __ksym __weak; static inline bool scx_bpf_sub_dispatch(u64 cgroup_id) { if (bpf_ksym_exists(scx_bpf_sub_dispatch___compat)) return scx_bpf_sub_dispatch___compat(cgroup_id); return false; }
Quoted 7 lines
See
tools/sched_ext/include/scx/compat.bpf.haround line 326-342 for the full pattern.This way schedulers just call
scx_bpf_sub_dispatch()directly and the compat layer handles everything. The no-op fallback (returning false) is fine here since without sub-sched support the dispatch path can't do anything useful anyway.
Got it.
I'll send a v2 patch implementing this approach.
Signature · 2 lines
Thanks, Cheng-Yang
Andrea Righi
reviewerHi Cheng-Yang,
Quoted 22 lines · Cheng-Yang Chou
Add
__COMPAT_scx_bpf_sub_dispatch()tocompat.bpf.hso that schedulers using sub-sched dispatch can build and run on older kernels that lack the kfunc. Guard the call withCONFIG_EXT_SUB_SCHEDandbpf_ksym_exists(), falling back to false when unavailable.Update
scx_qmapto use the new wrapper instead of callingscx_bpf_sub_dispatch()directly.Signed-off-by Cheng-Yang Chou <[email protected]>--- tools/sched_ext/include/scx/compat.bpf.h | 13 +++++++++++++ tools/sched_ext/scx_qmap.bpf.c | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-)tools/sched_ext/include/scx/compat.bpf.h +0
-0@@ -108,6 +108,19 @@ static inline struct task_struct *__COMPAT_scx_bpf_dsq_peek(u64 dsq_id)return p; }
tools/sched_ext/include/scx/compat.bpf.h static inline struct task_struct *__COMPAT_scx_bpf_dsq_peek(u64 dsq_id) · line 120
Quoted 10 lines
tools/sched_ext/include/scx/compat.bpf.h +10
-0+/* +* v7.1: scx_bpf_sub_dispatch() for sub-sched dispatch. Preserve until we drop +* the compat layer for older kernels that lack the kfunc. +*/ +static inline bool __COMPAT_scx_bpf_sub_dispatch (u64 cgroup_id) +{ +#ifdef CONFIG_EXT_SUB_SCHED +if (bpf_ksym_exists (scx_bpf_sub_dispatch)) +return scx_bpf_sub_dispatch (cgroup_id); +#endif
Isn't bpf_ksym_exists() enough? The CONFIG_EXT_SUB_SCHED guard takes effect only when we build an scx scheduler using __COMPAT_scx_bpf_sub_dispatch(), instead we want to build the scheduler anywhere and then the BPF verifier should evaluate the availability of scx_bpf_sub_dispatch().
Then the symbol will be resolved by this (from include/scx/common.bpf.h):
bool scx_bpf_sub_dispatch(u64 cgroup_id) __ksym __weak;
So, IIUC the #ifdef CONFIG_EXT_SUB_SCHED guard is not really needed here.
Thanks,
-Andrea
Quoted 22 lines
tools/sched_ext/include/scx/compat.bpf.h +3
-0+return false; +} +/** * __COMPAT_is_enq_cpu_selected - Test if SCX_ENQ_CPU_SELECTED is on * in a compatible way. We will preserve this __COMPAT helper until v6.16. tools/sched_ext/scx_qmap.bpf.c +1
-1@@ -490,7 +490,7 @@ void BPF_STRUCT_OPS(qmap_dispatch, s32 cpu, struct task_struct *prev)for (i =0 ; i <MAX_SUB_SCHEDS ; i++) {if (sub_sched_cgroup_ids[i] && -scx_bpf_sub_dispatch (sub_sched_cgroup_ids[i])) +__COMPAT_scx_bpf_sub_dispatch (sub_sched_cgroup_ids[i]))return ; }Signature · 1 line
2.48.1
Quoted 41 lines · Andrea Righi
Hi Cheng-Yang,
Quoted 22 lines · Cheng-Yang Chou
Add
__COMPAT_scx_bpf_sub_dispatch()tocompat.bpf.hso that schedulers using sub-sched dispatch can build and run on older kernels that lack the kfunc. Guard the call withCONFIG_EXT_SUB_SCHEDandbpf_ksym_exists(), falling back to false when unavailable.Update
scx_qmapto use the new wrapper instead of callingscx_bpf_sub_dispatch()directly.Signed-off-by Cheng-Yang Chou <[email protected]>--- tools/sched_ext/include/scx/compat.bpf.h | 13 +++++++++++++ tools/sched_ext/scx_qmap.bpf.c | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-)tools/sched_ext/include/scx/compat.bpf.h +0
-0@@ -108,6 +108,19 @@ static inline struct task_struct *__COMPAT_scx_bpf_dsq_peek(u64 dsq_id)return p; }tools/sched_ext/include/scx/compat.bpf.h static inline struct task_struct *__COMPAT_scx_bpf_dsq_peek(u64 dsq_id) · line 120
Quoted 10 lines
tools/sched_ext/include/scx/compat.bpf.h +10
-0+/* +* v7.1: scx_bpf_sub_dispatch() for sub-sched dispatch. Preserve until we drop +* the compat layer for older kernels that lack the kfunc. +*/ +static inline bool __COMPAT_scx_bpf_sub_dispatch (u64 cgroup_id) +{ +#ifdef CONFIG_EXT_SUB_SCHED +if (bpf_ksym_exists (scx_bpf_sub_dispatch)) +return scx_bpf_sub_dispatch (cgroup_id); +#endif Isn't
bpf_ksym_exists()enough? TheCONFIG_EXT_SUB_SCHEDguard takes effect only when we build an scx scheduler using__COMPAT_scx_bpf_sub_dispatch(), instead we want to build the scheduler anywhere and then the BPF verifier should evaluate the availability ofscx_bpf_sub_dispatch().
Quoted 5 lines
Then the symbol will be resolved by this (from
include/scx/common.bpf.h):bool
scx_bpf_sub_dispatch(u64 cgroup_id)__ksym__weak;So, IIUC the #ifdef
CONFIG_EXT_SUB_SCHEDguard is not really needed here.
Indeed, this CONFIG_EXT_SUB_SCHED is redundant.
Thanks for the explanation!
Quoted 26 lines
Thanks,
-AndreaQuoted 22 lines
tools/sched_ext/include/scx/compat.bpf.h +3
-0+return false; +} +/** * __COMPAT_is_enq_cpu_selected - Test if SCX_ENQ_CPU_SELECTED is on * in a compatible way. We will preserve this __COMPAT helper until v6.16. tools/sched_ext/scx_qmap.bpf.c +1
-1@@ -490,7 +490,7 @@ void BPF_STRUCT_OPS(qmap_dispatch, s32 cpu, struct task_struct *prev)for (i =0 ; i <MAX_SUB_SCHEDS ; i++) {if (sub_sched_cgroup_ids[i] && -scx_bpf_sub_dispatch (sub_sched_cgroup_ids[i])) +__COMPAT_scx_bpf_sub_dispatch (sub_sched_cgroup_ids[i]))return ; }Signature · 1 line
2.48.1
Signature · 2 lines
Thanks, Cheng-Yang