Skip to contentSkip to conversation

tools/sched_ext: Compatible testing of SCX_ENQ_CPU_SELECTED

PATCH Applied Changwoo Min 2 participants 2 messages Feb 9, 2025 Scheduler - sched_ext

lore
Share & tools
lore

Changwoo Min

author reviewer
#0 lore raw

PATCH v2 1/1 tools/sched_ext: Compatible testing of SCX_ENQ_CPU_SELECTED

This provides compatible testing of SCX_ENQ_CPU_SELECTED. More specifically, it handles two cases:

1. a BPF scheduler is compiled against vmlinux.h where SCX_ENQ_CPU_SELECTED is defined, but it runs on a kernel that does not have SCX_ENQ_CPU_SELECTED. In this case, the test result of 'enq_flags & SCX_ENQ_CPU_SELECTED' will always be false. That test result is semantically incorrect because the kernel before SCX_ENQ_CPU_SELECTED has never skipped select_task_rq_scx(), so the result should be true.

2. a BPF scheduler is compiling against vmlinux.h where SCX_ENQ_CPU_SELECTED is not defined. In this case, directly using SCX_ENQ_CPU_SELECTED causes compilation errors.

To hide such complexity, introduce __COMPAT_is_enq_cpu_selected(), which checks if SCX_ENQ_CPU_SELECTED exists in runtime using BPF CO-RE. This consists of three parts:

1. Add enum_defs.autogen.h, which has macros (HAVE_{enum name}) denoting whether SCX enums are defined in the vmlinux.h or not.

2. Implement __COMPAT_is_enq_cpu_selected(), which provide the test of SCX_ENQ_CPU_SELECTED in a compatible way.

3. Use __COMPAT_is_enq_cpu_selected() in scx_qmap.

Note that this is a sync of the relevant PR [1] in the scx repo.

[1] https://github.com/sched-ext/scx/pull/1314

---

ChangeLog v1 -> v2:
- Squash four commits into one, and revise the commit message.

 tools/sched_ext/include/scx/common.bpf.h      |   1 +
 tools/sched_ext/include/scx/common.h          |   1 +
 tools/sched_ext/include/scx/compat.bpf.h      |  52 ++++++++
 .../sched_ext/include/scx/enum_defs.autogen.h | 120 ++++++++++++++++++
 tools/sched_ext/scx_qmap.bpf.c                |   2 +-
 5 files changed, 175 insertions(+), 1 deletion(-)
 create mode 100644 tools/sched_ext/include/scx/enum_defs.autogen.h
tools/sched_ext/include/scx/common.bpf.h +1 -0
index ae717f4d6ede..f1caf9fc8f8c 100644
@@ -18,6 +18,7 @@
 #include <bpf/bpf_tracing.h>
 #include <asm-generic/errno.h>
 #include "user_exit_info.h"
+#include "enum_defs.autogen.h"
 
 #define PF_WQ_WORKER			0x00000020	/* I'm a workqueue worker */
 #define PF_KTHREAD			0x00200000	/* I am a kernel thread */
tools/sched_ext/include/scx/common.h +1 -0
index dc18b99e55cd..1dc76bd84296 100644
@@ -16,6 +16,7 @@
 #include <stdlib.h>
 #include <stdint.h>
 #include <errno.h>
+#include "enum_defs.autogen.h"
 
 typedef uint8_t u8;
 typedef uint16_t u16;
tools/sched_ext/include/scx/compat.bpf.h +52 -0

58 lines not on this page · raw message

tools/sched_ext/include/scx/enum_defs.autogen.h new file +120 -0

120 lines not on this page · raw message

tools/sched_ext/scx_qmap.bpf.c +1 -1
index 5edb79742e37..267fe15d934a 100644
@@ -231,7 +231,7 @@ void BPF_STRUCT_OPS(qmap_enqueue, struct task_struct *p, u64 enq_flags)
 	}
 
 	/* if select_cpu() wasn't called, try direct dispatch */
-	if (!(enq_flags & SCX_ENQ_CPU_SELECTED) &&
+	if (!__COMPAT_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);
Signature · 1 line
2.48.1
reviewer Applied
#1 lore raw
Quoted 24 lines · Changwoo Min

This provides compatible testing of SCX_ENQ_CPU_SELECTED. More specifically, it handles two cases:

1. a BPF scheduler is compiled against vmlinux.h where SCX_ENQ_CPU_SELECTED is defined, but it runs on a kernel that does not have SCX_ENQ_CPU_SELECTED. In this case, the test result of 'enq_flags & SCX_ENQ_CPU_SELECTED' will always be false. That test result is semantically incorrect because the kernel before SCX_ENQ_CPU_SELECTED has never skipped select_task_rq_scx(), so the result should be true.

2. a BPF scheduler is compiling against vmlinux.h where SCX_ENQ_CPU_SELECTED is not defined. In this case, directly using SCX_ENQ_CPU_SELECTED causes compilation errors.

To hide such complexity, introduce __COMPAT_is_enq_cpu_selected(), which checks if SCX_ENQ_CPU_SELECTED exists in runtime using BPF CO-RE. This consists of three parts:

1. Add enum_defs.autogen.h, which has macros (HAVE_{enum name}) denoting whether SCX enums are defined in the vmlinux.h or not.

2. Implement __COMPAT_is_enq_cpu_selected(), which provide the test of SCX_ENQ_CPU_SELECTED in a compatible way.

Quoted 7 lines

3. Use __COMPAT_is_enq_cpu_selected() in scx_qmap.

Note that this is a sync of the relevant PR [1] in the scx repo.

[1] https://github.com/sched-ext/scx/pull/1314

Applied to sched_ext/for-6.15 with a very minor edit:

Quoted 5 lines
tools/sched_ext/include/scx/enum_defs.autogen.h new file +0 -0
new file mode 100644
index 000000000000..a32e534a4012

...

tools/sched_ext/include/scx/enum_defs.autogen.h line 120

Quoted 2 lines
tools/sched_ext/include/scx/enum_defs.autogen.h new file +2 -0
+#endif /* __ENUM_DEFS_AUTOGEN_H__ */
+

The extra newline triggers git warning on commit. I dropped the extra newline. Maybe adjust the script too? Also, if we aren't including the script in the kernel tree, maybe a good idea for the generated file to point to where the script is?

Thanks.

Signature · 1 line
tejun