* [PATCH] tools/sched_ext: Improve BPF verifier arena detection workaround
@ 2026-02-06 4:18 zhidao su
2026-02-06 7:03 ` Andrea Righi
0 siblings, 1 reply; 8+ messages in thread
From: zhidao su @ 2026-02-06 4:18 UTC (permalink / raw)
To: tj, void, arighi, changwoo; +Cc: sched-ext, linux-kernel, suzhidao
Replace bpf_printk() with volatile access in scx_sdt scheduler's
BPF verifier workaround to eliminate console output while maintaining
the required LD.IMM instruction generation for arena detection.
This addresses the side effect issue of the previous hack while
preserving the essential functionality needed by the BPF verifier.
Signed-off-by: zhidao su <suzhidao@xiaomi.com>
---
tools/sched_ext/scx_sdt.bpf.c | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/tools/sched_ext/scx_sdt.bpf.c b/tools/sched_ext/scx_sdt.bpf.c
index 31b09958e8d5..13d3060c99ff 100644
--- a/tools/sched_ext/scx_sdt.bpf.c
+++ b/tools/sched_ext/scx_sdt.bpf.c
@@ -64,14 +64,10 @@ DEFINE_SDT_STAT(select_busy_cpu);
static __u64 zero = 0;
/*
- * XXX Hack to get the verifier to find the arena for sdt_exit_task.
- * As of 6.12-rc5, The verifier associates arenas with programs by
- * checking LD.IMM instruction operands for an arena and populating
- * the program state with the first instance it finds. This requires
- * accessing our global arena variable, but scx methods do not necessarily
- * do so while still using pointers from that arena. Insert a bpf_printk
- * statement that triggers at most once to generate an LD.IMM instruction
- * to access the arena and help the verifier.
+ * Workaround to help BPF verifier track arena usage.
+ * The verifier needs to see an explicit reference to the arena variable
+ * to properly track arena memory usage. This generates the required
+ * LD.IMM instruction without producing unnecessary output.
*/
static volatile bool scx_arena_verify_once;
@@ -80,7 +76,11 @@ __hidden void scx_arena_subprog_init(void)
if (scx_arena_verify_once)
return;
- bpf_printk("%s: arena pointer %p", __func__, &arena);
+ /*
+ * Use volatile access to generate LD.IMM instruction without
+ * producing console output like bpf_printk does.
+ */
+ (void)*(volatile void **)&arena;
scx_arena_verify_once = true;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH] tools/sched_ext: Improve BPF verifier arena detection workaround
2026-02-06 4:18 [PATCH] tools/sched_ext: Improve BPF verifier arena detection workaround zhidao su
@ 2026-02-06 7:03 ` Andrea Righi
2026-02-06 16:03 ` Emil Tsalapatis
0 siblings, 1 reply; 8+ messages in thread
From: Andrea Righi @ 2026-02-06 7:03 UTC (permalink / raw)
To: zhidao su
Cc: tj, void, changwoo, sched-ext, linux-kernel, suzhidao,
Emil Tsalapatis
Hi,
On Fri, Feb 06, 2026 at 12:18:08PM +0800, zhidao su wrote:
> Replace bpf_printk() with volatile access in scx_sdt scheduler's
> BPF verifier workaround to eliminate console output while maintaining
> the required LD.IMM instruction generation for arena detection.
>
> This addresses the side effect issue of the previous hack while
> preserving the essential functionality needed by the BPF verifier.
>
> Signed-off-by: zhidao su <suzhidao@xiaomi.com>
Adding Emil in cc.
> ---
> tools/sched_ext/scx_sdt.bpf.c | 18 +++++++++---------
> 1 file changed, 9 insertions(+), 9 deletions(-)
>
> diff --git a/tools/sched_ext/scx_sdt.bpf.c b/tools/sched_ext/scx_sdt.bpf.c
> index 31b09958e8d5..13d3060c99ff 100644
> --- a/tools/sched_ext/scx_sdt.bpf.c
> +++ b/tools/sched_ext/scx_sdt.bpf.c
> @@ -64,14 +64,10 @@ DEFINE_SDT_STAT(select_busy_cpu);
> static __u64 zero = 0;
>
> /*
> - * XXX Hack to get the verifier to find the arena for sdt_exit_task.
> - * As of 6.12-rc5, The verifier associates arenas with programs by
> - * checking LD.IMM instruction operands for an arena and populating
> - * the program state with the first instance it finds. This requires
> - * accessing our global arena variable, but scx methods do not necessarily
> - * do so while still using pointers from that arena. Insert a bpf_printk
> - * statement that triggers at most once to generate an LD.IMM instruction
> - * to access the arena and help the verifier.
> + * Workaround to help BPF verifier track arena usage.
> + * The verifier needs to see an explicit reference to the arena variable
> + * to properly track arena memory usage. This generates the required
> + * LD.IMM instruction without producing unnecessary output.
> */
> static volatile bool scx_arena_verify_once;
>
> @@ -80,7 +76,11 @@ __hidden void scx_arena_subprog_init(void)
> if (scx_arena_verify_once)
> return;
>
> - bpf_printk("%s: arena pointer %p", __func__, &arena);
> + /*
> + * Use volatile access to generate LD.IMM instruction without
> + * producing console output like bpf_printk does.
> + */
> + (void)*(volatile void **)&arena;
Makes sense to me.
If we want to be extra picky we can do something like this:
volatile void *arena_ref = &arena;
(void)arena_ref;
In this way we take the address of the arena map descriptor and store it
in a volatile void * variable, so the compiler can't optimize away and we
never read the content of the map descriptor, we only use its address. So,
we're not reinterpreting the bytes of the struct as another type (no type
punning, no strict-aliasing violation). Even if it's probably just a
theoretical thing.
> scx_arena_verify_once = true;
> }
>
> --
> 2.43.0
>
Thanks,
-Andrea
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] tools/sched_ext: Improve BPF verifier arena detection workaround
2026-02-06 7:03 ` Andrea Righi
@ 2026-02-06 16:03 ` Emil Tsalapatis
2026-02-12 8:00 ` [PATCH v2] " zhidao su
0 siblings, 1 reply; 8+ messages in thread
From: Emil Tsalapatis @ 2026-02-06 16:03 UTC (permalink / raw)
To: Andrea Righi, zhidao su
Cc: tj, void, changwoo, sched-ext, linux-kernel, suzhidao
On Fri Feb 6, 2026 at 2:03 AM EST, Andrea Righi wrote:
> Hi,
>
> On Fri, Feb 06, 2026 at 12:18:08PM +0800, zhidao su wrote:
>> Replace bpf_printk() with volatile access in scx_sdt scheduler's
>> BPF verifier workaround to eliminate console output while maintaining
>> the required LD.IMM instruction generation for arena detection.
>>
>> This addresses the side effect issue of the previous hack while
>> preserving the essential functionality needed by the BPF verifier.
>>
>> Signed-off-by: zhidao su <suzhidao@xiaomi.com>
>
> Adding Emil in cc.
>
This still doesn't pass verification for me. @suzhidao is this working for you?
If so please let me know your setup so I can replicate this. I am
running this on for-6.20.
>> ---
>> tools/sched_ext/scx_sdt.bpf.c | 18 +++++++++---------
>> 1 file changed, 9 insertions(+), 9 deletions(-)
>>
>> diff --git a/tools/sched_ext/scx_sdt.bpf.c b/tools/sched_ext/scx_sdt.bpf.c
>> index 31b09958e8d5..13d3060c99ff 100644
>> --- a/tools/sched_ext/scx_sdt.bpf.c
>> +++ b/tools/sched_ext/scx_sdt.bpf.c
>> @@ -64,14 +64,10 @@ DEFINE_SDT_STAT(select_busy_cpu);
>> static __u64 zero = 0;
>>
>> /*
>> - * XXX Hack to get the verifier to find the arena for sdt_exit_task.
>> - * As of 6.12-rc5, The verifier associates arenas with programs by
>> - * checking LD.IMM instruction operands for an arena and populating
>> - * the program state with the first instance it finds. This requires
>> - * accessing our global arena variable, but scx methods do not necessarily
>> - * do so while still using pointers from that arena. Insert a bpf_printk
>> - * statement that triggers at most once to generate an LD.IMM instruction
>> - * to access the arena and help the verifier.
>> + * Workaround to help BPF verifier track arena usage.
>> + * The verifier needs to see an explicit reference to the arena variable
>> + * to properly track arena memory usage. This generates the required
>> + * LD.IMM instruction without producing unnecessary output.
>> */
>> static volatile bool scx_arena_verify_once;
>>
>> @@ -80,7 +76,11 @@ __hidden void scx_arena_subprog_init(void)
>> if (scx_arena_verify_once)
>> return;
>>
>> - bpf_printk("%s: arena pointer %p", __func__, &arena);
>> + /*
>> + * Use volatile access to generate LD.IMM instruction without
>> + * producing console output like bpf_printk does.
>> + */
>> + (void)*(volatile void **)&arena;
>
> Makes sense to me.
>
> If we want to be extra picky we can do something like this:
>
> volatile void *arena_ref = &arena;
> (void)arena_ref;
>
> In this way we take the address of the arena map descriptor and store it
> in a volatile void * variable, so the compiler can't optimize away and we
> never read the content of the map descriptor, we only use its address. So,
> we're not reinterpreting the bytes of the struct as another type (no type
> punning, no strict-aliasing violation). Even if it's probably just a
> theoretical thing.
>
>> scx_arena_verify_once = true;
>> }
>>
>> --
>> 2.43.0
>>
>
> Thanks,
> -Andrea
^ permalink raw reply [flat|nested] 8+ messages in thread* [PATCH v2] tools/sched_ext: Improve BPF verifier arena detection workaround
2026-02-06 16:03 ` Emil Tsalapatis
@ 2026-02-12 8:00 ` zhidao su
0 siblings, 0 replies; 8+ messages in thread
From: zhidao su @ 2026-02-12 8:00 UTC (permalink / raw)
To: Tejun Heo; +Cc: scx, linux-kernel, emil, zhidao su
Replace bpf_printk() with inline assembly in scx_sdt scheduler's
BPF verifier workaround to eliminate console output while ensuring
the required LD.IMM instruction generation for arena detection.
The BPF verifier associates arenas with programs by checking LD.IMM
instruction operands for an arena map. The previous workaround using
bpf_printk() achieved this but polluted the kernel log.
A simple volatile access cast ((void)*(volatile void **)&arena) was
found to be unreliable, as some compiler versions (e.g., Clang 18)
optimized it away, resulting in missing LD.IMM instructions and
verifier failures.
This patch uses an empty inline assembly block with the arena address
as an input constraint. This forces the compiler to generate an
LD_IMM64 instruction for the arena address to satisfy the constraint,
guaranteeing detection by the verifier without any runtime side effects.
Signed-off-by: zhidao su <suzhidao@xiaomi.com>
---
v2:
- Replaced volatile pointer cast with inline assembly to prevent compiler
optimization (Clang) from eliminating the arena reference.
- Updated commit message to reflect the change and the reason for it.
tools/sched_ext/scx_sdt.bpf.c | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/tools/sched_ext/scx_sdt.bpf.c b/tools/sched_ext/scx_sdt.bpf.c
index 31b09958e8d5..a8a611d1bc75 100644
--- a/tools/sched_ext/scx_sdt.bpf.c
+++ b/tools/sched_ext/scx_sdt.bpf.c
@@ -64,14 +64,10 @@ DEFINE_SDT_STAT(select_busy_cpu);
static __u64 zero = 0;
/*
- * XXX Hack to get the verifier to find the arena for sdt_exit_task.
- * As of 6.12-rc5, The verifier associates arenas with programs by
- * checking LD.IMM instruction operands for an arena and populating
- * the program state with the first instance it finds. This requires
- * accessing our global arena variable, but scx methods do not necessarily
- * do so while still using pointers from that arena. Insert a bpf_printk
- * statement that triggers at most once to generate an LD.IMM instruction
- * to access the arena and help the verifier.
+ * Workaround to help BPF verifier track arena usage.
+ * The verifier needs to see an explicit reference to the arena variable
+ * to properly track arena memory usage. This generates the required
+ * track arena usage. This is a robust alternative to bpf_printk producing unnecessary output.
*/
static volatile bool scx_arena_verify_once;
@@ -80,7 +76,11 @@ __hidden void scx_arena_subprog_init(void)
if (scx_arena_verify_once)
return;
- bpf_printk("%s: arena pointer %p", __func__, &arena);
+ /*
+ * Generate an LD.IMM instruction to the arena to help the verifier track arena usage. This is a robust alternative to bpf_printk
+ * that produces no output.
+ */
+ asm volatile ("" : : "r"(&arena));
scx_arena_verify_once = true;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH] tools/sched_ext: Improve BPF verifier arena detection workaround
@ 2026-02-03 3:19 zhidao su
2026-02-03 3:54 ` Emil Tsalapatis
0 siblings, 1 reply; 8+ messages in thread
From: zhidao su @ 2026-02-03 3:19 UTC (permalink / raw)
To: tj, void, arighi, changwoo; +Cc: sched-ext, linux-kernel, suzhidao
Replace the BPF verifier workaround in scx_sdt scheduler with a more
elegant solution that:
1. Uses volatile cast instead of bpf_printk to generate LD.IMM instruction
without producing unnecessary output
2. Adds conditional compilation based on __BPF_FEATURE_ADDR_SPACE_CAST
to eliminate the workaround entirely on modern toolchains
3. Updates documentation to reflect broader compatibility concerns
This eliminates the side effects of the previous hack while maintaining
compatibility across different kernel/BPF toolchain versions.
Signed-off-by: zhidao su <suzhidao@xiaomi.com>
---
tools/sched_ext/scx_sdt.bpf.c | 24 +++++++++++++++---------
1 file changed, 15 insertions(+), 9 deletions(-)
diff --git a/tools/sched_ext/scx_sdt.bpf.c b/tools/sched_ext/scx_sdt.bpf.c
index 31b09958e8d5..88ac3043a643 100644
--- a/tools/sched_ext/scx_sdt.bpf.c
+++ b/tools/sched_ext/scx_sdt.bpf.c
@@ -64,15 +64,15 @@ DEFINE_SDT_STAT(select_busy_cpu);
static __u64 zero = 0;
/*
- * XXX Hack to get the verifier to find the arena for sdt_exit_task.
- * As of 6.12-rc5, The verifier associates arenas with programs by
- * checking LD.IMM instruction operands for an arena and populating
- * the program state with the first instance it finds. This requires
- * accessing our global arena variable, but scx methods do not necessarily
- * do so while still using pointers from that arena. Insert a bpf_printk
- * statement that triggers at most once to generate an LD.IMM instruction
- * to access the arena and help the verifier.
+ * Helper to ensure BPF verifier can track arena usage.
+ * On older toolchains, the verifier may not automatically detect arena usage
+ * through indirect references, so we provide an explicit reference.
*/
+#if defined(__BPF_FEATURE_ADDR_SPACE_CAST)
+/* Modern toolchains don't need the workaround */
+#define scx_arena_subprog_init() do { } while (0)
+#else
+/* Older toolchains need explicit arena reference for verifier */
static volatile bool scx_arena_verify_once;
__hidden void scx_arena_subprog_init(void)
@@ -80,9 +80,15 @@ __hidden void scx_arena_subprog_init(void)
if (scx_arena_verify_once)
return;
- bpf_printk("%s: arena pointer %p", __func__, &arena);
+ /*
+ * Generate LD.IMM instruction to help BPF verifier track arena usage.
+ * The volatile cast ensures the compiler doesn't optimize away the reference.
+ */
+ (void)*(volatile void **)&arena;
+
scx_arena_verify_once = true;
}
+#endif
private(LOCK) struct bpf_spin_lock alloc_lock;
--
2.43.0
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH] tools/sched_ext: Improve BPF verifier arena detection workaround
2026-02-03 3:19 [PATCH] " zhidao su
@ 2026-02-03 3:54 ` Emil Tsalapatis
2026-02-03 6:44 ` zhidao su
0 siblings, 1 reply; 8+ messages in thread
From: Emil Tsalapatis @ 2026-02-03 3:54 UTC (permalink / raw)
To: zhidao su, tj, void, arighi, changwoo; +Cc: sched-ext, linux-kernel, suzhidao
On Mon Feb 2, 2026 at 10:19 PM EST, zhidao su wrote:
> Replace the BPF verifier workaround in scx_sdt scheduler with a more
> elegant solution that:
>
> 1. Uses volatile cast instead of bpf_printk to generate LD.IMM instruction
> without producing unnecessary output
> 2. Adds conditional compilation based on __BPF_FEATURE_ADDR_SPACE_CAST
> to eliminate the workaround entirely on modern toolchains
> 3. Updates documentation to reflect broader compatibility concerns
>
> This eliminates the side effects of the previous hack while maintaining
> compatibility across different kernel/BPF toolchain versions.
>
This code change is a bit of a non-sequitur (BPF_FEATURE_ADDR_SPACE_CAST
is unrelated to the problem scx_arena_subprog_init solves) and causes
the scheduler to fail to load for me. Could you please explain the logic behind
this patch? Does it work on your machine and if so could you let me know what
toolchain and kernel you're using?
> Signed-off-by: zhidao su <suzhidao@xiaomi.com>
> ---
> tools/sched_ext/scx_sdt.bpf.c | 24 +++++++++++++++---------
> 1 file changed, 15 insertions(+), 9 deletions(-)
>
> diff --git a/tools/sched_ext/scx_sdt.bpf.c b/tools/sched_ext/scx_sdt.bpf.c
> index 31b09958e8d5..88ac3043a643 100644
> --- a/tools/sched_ext/scx_sdt.bpf.c
> +++ b/tools/sched_ext/scx_sdt.bpf.c
> @@ -64,15 +64,15 @@ DEFINE_SDT_STAT(select_busy_cpu);
> static __u64 zero = 0;
>
> /*
> - * XXX Hack to get the verifier to find the arena for sdt_exit_task.
> - * As of 6.12-rc5, The verifier associates arenas with programs by
> - * checking LD.IMM instruction operands for an arena and populating
> - * the program state with the first instance it finds. This requires
> - * accessing our global arena variable, but scx methods do not necessarily
> - * do so while still using pointers from that arena. Insert a bpf_printk
> - * statement that triggers at most once to generate an LD.IMM instruction
> - * to access the arena and help the verifier.
> + * Helper to ensure BPF verifier can track arena usage.
> + * On older toolchains, the verifier may not automatically detect arena usage
> + * through indirect references, so we provide an explicit reference.
> */
> +#if defined(__BPF_FEATURE_ADDR_SPACE_CAST)
> +/* Modern toolchains don't need the workaround */
> +#define scx_arena_subprog_init() do { } while (0)
> +#else
> +/* Older toolchains need explicit arena reference for verifier */
> static volatile bool scx_arena_verify_once;
>
> __hidden void scx_arena_subprog_init(void)
> @@ -80,9 +80,15 @@ __hidden void scx_arena_subprog_init(void)
> if (scx_arena_verify_once)
> return;
>
> - bpf_printk("%s: arena pointer %p", __func__, &arena);
> + /*
> + * Generate LD.IMM instruction to help BPF verifier track arena usage.
> + * The volatile cast ensures the compiler doesn't optimize away the reference.
> + */
> + (void)*(volatile void **)&arena;
> +
> scx_arena_verify_once = true;
> }
> +#endif
>
>
> private(LOCK) struct bpf_spin_lock alloc_lock;
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] tools/sched_ext: Improve BPF verifier arena detection workaround
2026-02-03 3:54 ` Emil Tsalapatis
@ 2026-02-03 6:44 ` zhidao su
2026-02-03 6:57 ` zhidao su
0 siblings, 1 reply; 8+ messages in thread
From: zhidao su @ 2026-02-03 6:44 UTC (permalink / raw)
To: Emil Tsalapatis; +Cc: tj, void, arighi, changwoo, sched-ext, linux-kernel
Thanks for catching this.To be honest, this change was primarily a result of my attempt to comb through and reorganize the code logic. I mistakenly thought it was applicable here and did not perform a complete runtime test for this specific modification.I apologize for the breakage. I will revert/fix this logic in the next revision to ensure the scheduler loads correctly.
> 2026年2月3日 11:54,Emil Tsalapatis <emil@etsalapatis.com> 写道:
>
> This code change is a bit of a non-sequitur (BPF_FEATURE_ADDR_SPACE_CAST
> is unrelated to the problem scx_arena_subprog_init solves) and causes
> the scheduler to fail to load for me. Could you please explain the logic behind
> this patch? Does it work on your machine and if so could you let me know what
> toolchain and kernel you're using?
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH] tools/sched_ext: Improve BPF verifier arena detection workaround
2026-02-03 6:44 ` zhidao su
@ 2026-02-03 6:57 ` zhidao su
0 siblings, 0 replies; 8+ messages in thread
From: zhidao su @ 2026-02-03 6:57 UTC (permalink / raw)
To: soolaugust
Cc: arighi, changwoo, emil, linux-kernel, sched-ext, tj, void,
zhidao su
Replace bpf_printk() with volatile access in scx_sdt scheduler's
BPF verifier workaround to eliminate console output while maintaining
the required LD.IMM instruction generation for arena detection.
This addresses the side effect issue of the previous hack while
preserving the essential functionality needed by the BPF verifier.
Signed-off-by: zhidao su <suzhidao@xiaomi.com>
---
tools/sched_ext/scx_sdt.bpf.c | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/tools/sched_ext/scx_sdt.bpf.c b/tools/sched_ext/scx_sdt.bpf.c
index 31b09958e8d5..13d3060c99ff 100644
--- a/tools/sched_ext/scx_sdt.bpf.c
+++ b/tools/sched_ext/scx_sdt.bpf.c
@@ -64,14 +64,10 @@ DEFINE_SDT_STAT(select_busy_cpu);
static __u64 zero = 0;
/*
- * XXX Hack to get the verifier to find the arena for sdt_exit_task.
- * As of 6.12-rc5, The verifier associates arenas with programs by
- * checking LD.IMM instruction operands for an arena and populating
- * the program state with the first instance it finds. This requires
- * accessing our global arena variable, but scx methods do not necessarily
- * do so while still using pointers from that arena. Insert a bpf_printk
- * statement that triggers at most once to generate an LD.IMM instruction
- * to access the arena and help the verifier.
+ * Workaround to help BPF verifier track arena usage.
+ * The verifier needs to see an explicit reference to the arena variable
+ * to properly track arena memory usage. This generates the required
+ * LD.IMM instruction without producing unnecessary output.
*/
static volatile bool scx_arena_verify_once;
@@ -80,7 +76,11 @@ __hidden void scx_arena_subprog_init(void)
if (scx_arena_verify_once)
return;
- bpf_printk("%s: arena pointer %p", __func__, &arena);
+ /*
+ * Use volatile access to generate LD.IMM instruction without
+ * producing console output like bpf_printk does.
+ */
+ (void)*(volatile void **)&arena;
scx_arena_verify_once = true;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-02-12 8:01 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-06 4:18 [PATCH] tools/sched_ext: Improve BPF verifier arena detection workaround zhidao su
2026-02-06 7:03 ` Andrea Righi
2026-02-06 16:03 ` Emil Tsalapatis
2026-02-12 8:00 ` [PATCH v2] " zhidao su
-- strict thread matches above, loose matches on Subject: below --
2026-02-03 3:19 [PATCH] " zhidao su
2026-02-03 3:54 ` Emil Tsalapatis
2026-02-03 6:44 ` zhidao su
2026-02-03 6:57 ` zhidao su
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox