Skip to contentSkip to conversation

tools/sched_ext: Add __COMPAT wrapper for scx_bpf_sub_dispatch()

PATCH In review Cheng-Yang Chou 3 participants 5 messages Mar 22 – Mar 23 Scheduler - sched_ext

lore
Share & tools
lore
#0 lore raw

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
index 83b3425e63b2..f24b9a1c75ad 100644
@@ -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
index f3587fb709c9..dd44d8db0341 100644
@@ -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
reviewer
#1 lore raw

Hello,

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

#2 lore raw
Quoted 5 lines · Tejun Heo

Hello,

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.

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 ___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;
  }
Quoted 7 lines

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.

Got it.
I'll send a v2 patch implementing this approach.

Signature · 2 lines
Thanks,
Cheng-Yang
reviewer
#3 lore raw
In reply to Cheng-Yang Chou

Hi Cheng-Yang,

Quoted 22 lines · Cheng-Yang Chou

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 +0 -0
index 83b3425e63b2..f24b9a1c75ad 100644
@@ -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
index f3587fb709c9..dd44d8db0341 100644
@@ -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
#4 lore raw
Quoted 41 lines · Andrea Righi

Hi Cheng-Yang,

Quoted 22 lines · Cheng-Yang Chou

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 +0 -0
index 83b3425e63b2..f24b9a1c75ad 100644
@@ -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().

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_SCHED guard is not really needed here.

Indeed, this CONFIG_EXT_SUB_SCHED is redundant.
Thanks for the explanation!

Quoted 26 lines

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
index f3587fb709c9..dd44d8db0341 100644
@@ -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