Skip to contentSkip to conversation

Remove __COMPAT prefix from BPF compat wrappers

RFC PATCH In review Cheng-Yang Chou 2 participants 6 messages Mar 23 – Mar 24

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Remove __COMPAT prefix from BPF compat wrappers

As suggested [1], move away from the __COMPAT prefix and make compat wrappers transparent so schedulers don't need code changes for compatibility.

Patch 1:
- converts the __COMPAT prefixed macros and helpers in compat.bpf.h to transparent static inline functions using the ___compat suffix pattern (so libbpf resolves them to the original kernel symbols), and removes the now-redundant __ksym __weak declarations from common.bpf.h.

Patch 2:
- updates scx_qmap to call the renamed helpers directly.

Patch 3:
- updates the selftests (dsq_reenq, peek_dsq, numa) likewise. One exception: peek_dsq has a debug path that explicitly checks bpf_ksym_exists() to record which code path is taken at runtime; since scx_bpf_dsq_peek is now a static inline, the check is updated to use bpf_ksym_exists(scx_bpf_dsq_peek___compat) directly.

scx_qmap and the selftest changes have been built and tested on both v7.0 (via vng) and a v6.14 kernel. On v6.14, dsq_reenq is skipped since scx_bpf_dsq_reenq() is not available; everything else looks fine.

One thing I'm not sure about: the "Preserve until vX.XX" comments attached to each compat block. With the old __COMPAT macros, the intent was clearly to drop the macro at that version. Now that the wrappers are transparent static inlines, I assume the plan is still to drop the wrapper and move the plain __ksym declaration back into common.bpf.h at that point (similar to how scx_bpf_dsq_insert was handled).

Please let me know if I should update them.

[1]: https://lore.kernel.org/r/[email protected]/

Thanks,
Cheng-Yang

---
Cheng-Yang Chou (3):
tools/sched_ext: Remove __COMPAT prefix from compat.bpf.h wrappers tools/sched_ext: Update scx_qmap to drop __COMPAT prefix selftests/sched_ext: Update selftests to drop __COMPAT prefix

 tools/sched_ext/include/scx/common.bpf.h      |  10 +-
 tools/sched_ext/include/scx/compat.bpf.h      | 140 +++++++++++-------
 tools/sched_ext/scx_qmap.bpf.c                |   8 +-
 .../selftests/sched_ext/dsq_reenq.bpf.c       |   2 +-
 tools/testing/selftests/sched_ext/numa.bpf.c  |  22 ++-
 tools/testing/selftests/sched_ext/numa.c      |   2 +-
 .../selftests/sched_ext/peek_dsq.bpf.c        |   8 +-
 7 files changed, 104 insertions(+), 88 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH 1/3 tools/sched_ext: Remove __COMPAT prefix from compat.bpf.h wrappers

Convert __COMPAT prefix kfunc wrappers to transparent static inline functions using the ___compat suffix pattern, so schedulers can call them directly without needing to know about compat details.

Transparent kfunc wrappers:
- bpf_cpumask_populate
- scx_bpf_dsq_peek
- scx_bpf_events
- scx_bpf_nr_node_ids, scx_bpf_cpu_node
- scx_bpf_get_idle_cpumask_node, scx_bpf_get_idle_smtmask_node
- scx_bpf_pick_idle_cpu_node, scx_bpf_pick_any_cpu_node
- scx_bpf_cpu_curr

Each function is declared with a ___compat suffix as __ksym __weak so libbpf resolves it to the kernel's original symbol. The static inline wrapper checks bpf_ksym_exists() and falls back gracefully on older kernels. The corresponding __ksym __weak declarations are removed from common.bpf.h.

Helper renames:
- __COMPAT_is_enq_cpu_selected -> scx_bpf_is_enq_cpu_selected
- __COMPAT_HAS_scx_bpf_select_cpu_and -> scx_bpf_has_select_cpu_and
- __COMPAT_has_generic_reenq -> scx_bpf_has_generic_reenq
- __COMPAT_scx_bpf_reenqueue_local_from_anywhere: inlined into scx_bpf_reenqueue_local(), removing the separate helper

---
 tools/sched_ext/include/scx/common.bpf.h |   9 --
 tools/sched_ext/include/scx/compat.bpf.h | 139 ++++++++++++++---------
 2 files changed, 83 insertions(+), 65 deletions(-)
tools/sched_ext/include/scx/common.bpf.h +0 -9
index a63a98a96b86..ce35944fb86f 100644
@@ -68,7 +68,6 @@ void scx_bpf_dispatch_cancel(void) __ksym;
 void scx_bpf_kick_cpu(s32 cpu, u64 flags) __ksym;
 s32 scx_bpf_dsq_nr_queued(u64 dsq_id) __ksym;
 void scx_bpf_destroy_dsq(u64 dsq_id) __ksym;
-struct task_struct *scx_bpf_dsq_peek(u64 dsq_id) __ksym __weak;
 int bpf_iter_scx_dsq_new(struct bpf_iter_scx_dsq *it, u64 dsq_id, u64 flags) __ksym __weak;
 struct task_struct *bpf_iter_scx_dsq_next(struct bpf_iter_scx_dsq *it) __ksym __weak;
 void bpf_iter_scx_dsq_destroy(struct bpf_iter_scx_dsq *it) __ksym __weak;
@@ -78,29 +77,21 @@ void scx_bpf_dump_bstr(char *fmt, unsigned long long *data, u32 data_len) __ksym
 u32 scx_bpf_cpuperf_cap(s32 cpu) __ksym __weak;
 u32 scx_bpf_cpuperf_cur(s32 cpu) __ksym __weak;
 void scx_bpf_cpuperf_set(s32 cpu, u32 perf) __ksym __weak;
-u32 scx_bpf_nr_node_ids(void) __ksym __weak;
 u32 scx_bpf_nr_cpu_ids(void) __ksym __weak;
-int scx_bpf_cpu_node(s32 cpu) __ksym __weak;
 const struct cpumask *scx_bpf_get_possible_cpumask(void) __ksym __weak;
 const struct cpumask *scx_bpf_get_online_cpumask(void) __ksym __weak;
 void scx_bpf_put_cpumask(const struct cpumask *cpumask) __ksym __weak;
-const struct cpumask *scx_bpf_get_idle_cpumask_node(int node) __ksym __weak;
 const struct cpumask *scx_bpf_get_idle_cpumask(void) __ksym;
-const struct cpumask *scx_bpf_get_idle_smtmask_node(int node) __ksym __weak;
 const struct cpumask *scx_bpf_get_idle_smtmask(void) __ksym;
 void scx_bpf_put_idle_cpumask(const struct cpumask *cpumask) __ksym;
 bool scx_bpf_test_and_clear_cpu_idle(s32 cpu) __ksym;
-s32 scx_bpf_pick_idle_cpu_node(const cpumask_t *cpus_allowed, int node, u64 flags) __ksym __weak;
 s32 scx_bpf_pick_idle_cpu(const cpumask_t *cpus_allowed, u64 flags) __ksym;
-s32 scx_bpf_pick_any_cpu_node(const cpumask_t *cpus_allowed, int node, u64 flags) __ksym __weak;
 s32 scx_bpf_pick_any_cpu(const cpumask_t *cpus_allowed, u64 flags) __ksym;
 bool scx_bpf_task_running(const struct task_struct *p) __ksym;
 s32 scx_bpf_task_cpu(const struct task_struct *p) __ksym;
 struct rq *scx_bpf_cpu_rq(s32 cpu) __ksym;
 struct rq *scx_bpf_locked_rq(void) __ksym;
-struct task_struct *scx_bpf_cpu_curr(s32 cpu) __ksym __weak;
 u64 scx_bpf_now(void) __ksym __weak;
-void scx_bpf_events(struct scx_event_stats *events, size_t events__sz) __ksym __weak;
 bool scx_bpf_sub_dispatch(u64 cgroup_id) __ksym __weak;
 
 /*
tools/sched_ext/include/scx/compat.bpf.h +83 -56

208 lines not on this page · raw message

Signature · 1 line
2.48.1
#2 lore raw

PATCH 2/3 tools/sched_ext: Update scx_qmap to drop __COMPAT prefix

In reply to Cheng-Yang Chou

Replace __COMPAT prefix calls with the new transparent function names:
- __COMPAT_has_generic_reenq() -> scx_bpf_has_generic_reenq()
- __COMPAT_is_enq_cpu_selected() -> scx_bpf_is_enq_cpu_selected()
- __COMPAT_scx_bpf_events() -> scx_bpf_events()

---
 tools/sched_ext/scx_qmap.bpf.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
tools/sched_ext/scx_qmap.bpf.c +4 -4
index f3587fb709c9..3fca2e99775f 100644
@@ -267,14 +267,14 @@ void BPF_STRUCT_OPS(qmap_enqueue, struct task_struct *p, u64 enq_flags)
 	}
 
 	/* see lowpri_timerfn() */
-	if (__COMPAT_has_generic_reenq() &&
+	if (scx_bpf_has_generic_reenq() &&
 	    p->scx.weight < 2 && !(p->flags & PF_KTHREAD) && !(enq_flags & SCX_ENQ_REENQ)) {
 		scx_bpf_dsq_insert(p, LOWPRI_DSQ, slice_ns, enq_flags);
 		return;
 	}
 
 	/* if select_cpu() wasn't called, try direct dispatch */
-	if (!__COMPAT_is_enq_cpu_selected(enq_flags) &&
+	if (!scx_bpf_is_enq_cpu_selected(enq_flags) &&
 	    (cpu = pick_direct_dispatch_cpu(p, scx_bpf_task_cpu(p))) >= 0) {
 		__sync_fetch_and_add(&nr_ddsp_from_enq, 1);
 		scx_bpf_dsq_insert(p, SCX_DSQ_LOCAL_ON | cpu, slice_ns, enq_flags);
@@ -851,7 +851,7 @@ static int monitor_timerfn(void *map, int *key, struct bpf_timer *timer)
 
 		dump_shared_dsq();
 
-		__COMPAT_scx_bpf_events(&events, sizeof(events));
+		scx_bpf_events(&events, sizeof(events));
 
 		bpf_printk("%35s: %lld", "SCX_EV_SELECT_CPU_FALLBACK",
 			   scx_read_event(&events, SCX_EV_SELECT_CPU_FALLBACK));
@@ -931,7 +931,7 @@ s32 BPF_STRUCT_OPS_SLEEPABLE(qmap_init)
 	if (ret)
 		return ret;
 
-	if (__COMPAT_has_generic_reenq()) {
+	if (scx_bpf_has_generic_reenq()) {
 		/* see lowpri_timerfn() */
 		timer = bpf_map_lookup_elem(&lowpri_timer, &key);
 		if (!timer)
Signature · 1 line
2.48.1
#3 lore raw

PATCH 3/3 selftests/sched_ext: Update selftests to drop __COMPAT prefix

In reply to Cheng-Yang Chou

Replace __COMPAT prefix calls with the new transparent function names:

dsq_reenq.bpf.c:
- __COMPAT_has_generic_reenq() -> scx_bpf_has_generic_reenq()

peek_dsq.bpf.c:
- __COMPAT_scx_bpf_dsq_peek() -> scx_bpf_dsq_peek()
- bpf_ksym_exists(scx_bpf_dsq_peek) -> bpf_ksym_exists(scx_bpf_dsq_peek___compat) since scx_bpf_dsq_peek is now a static inline, not a __weak ksym

numa.bpf.c / numa.c:
- __COMPAT_scx_bpf_get_idle_cpumask_node() -> scx_bpf_get_idle_cpumask_node()
- __COMPAT_scx_bpf_cpu_node() -> scx_bpf_cpu_node()
- __COMPAT_scx_bpf_pick_idle_cpu_node() -> scx_bpf_pick_idle_cpu_node()
- __COMPAT_scx_bpf_pick_any_cpu_node() -> scx_bpf_pick_any_cpu_node()
- __COMPAT_scx_bpf_nr_node_ids() -> scx_bpf_nr_node_ids()
- __COMPAT_SCX_PICK_IDLE_IN_NODE rodata variable -> pick_idle_in_node

---
 .../selftests/sched_ext/dsq_reenq.bpf.c       |  2 +-
 tools/testing/selftests/sched_ext/numa.bpf.c  | 22 +++++++++----------
 tools/testing/selftests/sched_ext/numa.c      |  2 +-
 .../selftests/sched_ext/peek_dsq.bpf.c        |  8 +++----
 4 files changed, 16 insertions(+), 18 deletions(-)
tools/testing/selftests/sched_ext/dsq_reenq.bpf.c +1 -1
index 750bb10508df..131dc0c91f5c 100644
@@ -91,7 +91,7 @@ s32 BPF_STRUCT_OPS_SLEEPABLE(dsq_reenq_init)
 	if (ret)
 		return ret;
 
-	if (!__COMPAT_has_generic_reenq()) {
+	if (!scx_bpf_has_generic_reenq()) {
 		scx_bpf_error("scx_bpf_dsq_reenq() not available");
 		return -EOPNOTSUPP;
 	}
tools/testing/selftests/sched_ext/numa.bpf.c +10 -12
index 78cc49a7f9a6..a003a1f267fa 100644
@@ -17,14 +17,14 @@ char _license[] SEC("license") = "GPL";
 
 UEI_DEFINE(uei);
 
-const volatile unsigned int __COMPAT_SCX_PICK_IDLE_IN_NODE;
+const volatile unsigned int pick_idle_in_node;
 
 static bool is_cpu_idle(s32 cpu, int node)
 {
 	const struct cpumask *idle_cpumask;
 	bool idle;
 
-	idle_cpumask = __COMPAT_scx_bpf_get_idle_cpumask_node(node);
+	idle_cpumask = scx_bpf_get_idle_cpumask_node(node);
 	idle = bpf_cpumask_test_cpu(cpu, idle_cpumask);
 	scx_bpf_put_cpumask(idle_cpumask);
 
@@ -34,24 +34,22 @@ static bool is_cpu_idle(s32 cpu, int node)
 s32 BPF_STRUCT_OPS(numa_select_cpu,
 		   struct task_struct *p, s32 prev_cpu, u64 wake_flags)
 {
-	int node = __COMPAT_scx_bpf_cpu_node(scx_bpf_task_cpu(p));
+	int node = scx_bpf_cpu_node(scx_bpf_task_cpu(p));
 	s32 cpu;
 
 	/*
-	 * We could just use __COMPAT_scx_bpf_pick_any_cpu_node() here,
+	 * We could just use scx_bpf_pick_any_cpu_node() here,
 	 * since it already tries to pick an idle CPU within the node
 	 * first, but let's use both functions for better testing coverage.
 	 */
-	cpu = __COMPAT_scx_bpf_pick_idle_cpu_node(p->cpus_ptr, node,
-					__COMPAT_SCX_PICK_IDLE_IN_NODE);
+	cpu = scx_bpf_pick_idle_cpu_node(p->cpus_ptr, node, pick_idle_in_node);
 	if (cpu < 0)
-		cpu = __COMPAT_scx_bpf_pick_any_cpu_node(p->cpus_ptr, node,
-						__COMPAT_SCX_PICK_IDLE_IN_NODE);
+		cpu = scx_bpf_pick_any_cpu_node(p->cpus_ptr, node, pick_idle_in_node);
 
 	if (is_cpu_idle(cpu, node))
 		scx_bpf_error("CPU %d should be marked as busy", cpu);
 
-	if (__COMPAT_scx_bpf_cpu_node(cpu) != node)
+	if (scx_bpf_cpu_node(cpu) != node)
 		scx_bpf_error("CPU %d should be in node %d", cpu, node);
 
 	return cpu;
@@ -59,14 +57,14 @@ s32 BPF_STRUCT_OPS(numa_select_cpu,
 
 void BPF_STRUCT_OPS(numa_enqueue, struct task_struct *p, u64 enq_flags)
 {
-	int node = __COMPAT_scx_bpf_cpu_node(scx_bpf_task_cpu(p));
+	int node = scx_bpf_cpu_node(scx_bpf_task_cpu(p));
 
 	scx_bpf_dsq_insert(p, node, SCX_SLICE_DFL, enq_flags);
 }
 
 void BPF_STRUCT_OPS(numa_dispatch, s32 cpu, struct task_struct *prev)
 {
-	int node = __COMPAT_scx_bpf_cpu_node(cpu);
+	int node = scx_bpf_cpu_node(cpu);
 
 	scx_bpf_dsq_move_to_local(node, 0);
 }
@@ -75,7 +73,7 @@ s32 BPF_STRUCT_OPS_SLEEPABLE(numa_init)
 {
 	int node, err;
 
-	bpf_for(node, 0, __COMPAT_scx_bpf_nr_node_ids()) {
+	bpf_for(node, 0, scx_bpf_nr_node_ids()) {
 		err = scx_bpf_create_dsq(node, node);
 		if (err)
 			return err;
tools/testing/selftests/sched_ext/numa.c +1 -1
index b060c3b65c82..17f5c2b5bac1 100644
@@ -16,7 +16,7 @@ static enum scx_test_status setup(void **ctx)
 	skel = numa__open();
 	SCX_FAIL_IF(!skel, "Failed to open");
 	SCX_ENUM_INIT(skel);
-	skel->rodata->__COMPAT_SCX_PICK_IDLE_IN_NODE = SCX_PICK_IDLE_IN_NODE;
+	skel->rodata->pick_idle_in_node = SCX_PICK_IDLE_IN_NODE;
 	skel->struct_ops.numa_ops->flags = SCX_OPS_BUILTIN_IDLE_PER_NODE;
 	SCX_FAIL_IF(numa__load(skel), "Failed to load skel");
 
tools/testing/selftests/sched_ext/peek_dsq.bpf.c +4 -4
index 96e3a336a8a6..0c80c65239d4 100644
@@ -89,7 +89,7 @@ static int scan_dsq_pool(void)
 
 		total_peek_attempts++;
 
-		task = __COMPAT_scx_bpf_dsq_peek(dsq_id);
+		task = scx_bpf_dsq_peek(dsq_id);
 		if (task) {
 			successful_peeks++;
 			record_peek_result(task->pid);
@@ -119,7 +119,7 @@ void BPF_STRUCT_OPS(peek_dsq_enqueue, struct task_struct *p, u64 enq_flags)
 		bpf_printk("peek_dsq_enqueue beginning phase 1 peek test on cpu %d", cpu);
 
 		/* Test 1: Peek empty DSQ - should return NULL */
-		peek_result = __COMPAT_scx_bpf_dsq_peek(test_dsq_id);
+		peek_result = scx_bpf_dsq_peek(test_dsq_id);
 		dsq_peek_result1 = (long)peek_result; /* Should be 0 (NULL) */
 
 		/* Test 2: Insert task into test DSQ for testing in dispatch callback */
@@ -150,7 +150,7 @@ void BPF_STRUCT_OPS(peek_dsq_dispatch, s32 cpu, struct task_struct *prev)
 		bpf_printk("peek_dsq_dispatch completing phase 1 peek test on cpu %d", cpu);
 
 		/* Test 3: Peek DSQ after insert - should return the task we inserted */
-		peek_result = __COMPAT_scx_bpf_dsq_peek(test_dsq_id);
+		peek_result = scx_bpf_dsq_peek(test_dsq_id);
 		/* Store the PID of the peeked task for comparison */
 		dsq_peek_result2 = (long)peek_result;
 		dsq_peek_result2_pid = peek_result ? peek_result->pid : -1;
@@ -179,7 +179,7 @@ s32 BPF_STRUCT_OPS_SLEEPABLE(peek_dsq_init)
 	int i;
 
 	/* Always set debug values so we can see which version we're using */
-	debug_ksym_exists = bpf_ksym_exists(scx_bpf_dsq_peek) ? 1 : 0;
+	debug_ksym_exists = bpf_ksym_exists(scx_bpf_dsq_peek___compat) ? 1 : 0;
 
 	/* Initialize state first */
 	insert_test_cpu = -1;
Signature · 1 line
2.48.1
reviewer
#4 lore raw
In reply to Cheng-Yang Chou
Quoted 3 lines · Cheng-Yang Chou

As suggested [1], move away from the __COMPAT prefix and make compat wrappers transparent so schedulers don't need code changes for compatibility.

I don't think we need to create churn for this. We can just avoid adding __COMPAT on new ones and phase out existing ones over time.

Thanks.

Signature · 1 line
tejun
#5 lore raw

Hi Tejun,

Quoted 7 lines · Tejun Heo
Quoted 3 lines · Cheng-Yang Chou

As suggested [1], move away from the __COMPAT prefix and make compat wrappers transparent so schedulers don't need code changes for compatibility.

I don't think we need to create churn for this. We can just avoid adding __COMPAT on new ones and phase out existing ones over time.

Make sense.
Thanks for the clarification!

Signature · 2 lines
Thanks,
Cheng-Yang