tools/sched_ext: fix scx_qmap BPF verifier rejection
cover tools/sched_ext: fix scx_qmap BPF verifier rejection
scx_qmap fails to load with modern LLVM due to LLVM liveness killing the PTR_TO_ARENA register alias before arena stores. The BPF verifier rejects these stores with "RN invalid mem access 'scalar'".
Patch 1: moves bpf_addr_space_cast() before the #if block so it is always available regardless of LLVM version.
Patch 2: calls bpf_addr_space_cast() at every arena store site in scx_qmap where the PTR_TO_ARENA alias dies before the write.
Failure observed on sched_ext/for-next with vng (without this series):
root@virtme-ng:/home/ubuntu/sched_ext/tools/sched_ext# ./build/bin/scx_qmap
libbpf: prog 'qmap_dequeue': BPF program load failed: -EACCES
libbpf: prog 'qmap_dequeue': -- BEGIN PROG LOAD LOG --
0: R1=ctx() R10=fp0
; void BPF_STRUCT_OPS(qmap_dequeue, struct task_struct *p, u64 deq_flags) @ scx_qmap.bpf.c:467
0: (79) r2 = *(u64 *)(r1 +0)
func 'dequeue' arg0 has btf_id 115922 type STRUCT 'task_struct'
1: R1=ctx() R2=trusted_ptr_task_struct()
1: (79) r4 = *(u64 *)(r1 +8) ; R1=ctx() R4=scalar()
; __sync_fetch_and_add(&qa.nr_dequeued, 1); @ scx_qmap.bpf.c:471
2: (18) r1 = 0x10000fff9000 ; R1=scalar()
4: (18) r3 = 0x10000fff9000 ; R3=scalar()
6: (07) r3 += 32 ; R3=scalar()
7: (bf) r5 = addr_space_cast(r3, 0, 1) ; R3=scalar() R5=arena
8: (b7) r3 = 1 ; R3=1
9: (b7) r0 = 1 ; R0=1
10: (db) r0 = atomic64_fetch_add((u64 *)(r5 +0), r0) ; R0=scalar() R5=arena
11: (18) r5 = 0x100000000 ; R5=0x100000000
; if (deq_flags & SCX_DEQ_CORE_SCHED_EXEC) @ scx_qmap.bpf.c:472
13: (5f) r4 &= r5 ; R4=scalar(smin=smin32=0,smax=umax=0x100000000,smax32=umax32=0,var_off=(0x0; 0x100000000))
14: (15) if r4 == 0x0 goto pc+3 ; R4=0x100000000
; __sync_fetch_and_add(&qa.nr_core_sched_execed, 1); @ scx_qmap.bpf.c:473
15: (07) r1 += 48 ; R1=scalar()
16: (bf) r1 = addr_space_cast(r1, 0, 1) ; R1=arena
17: (db) r3 = atomic64_fetch_add((u64 *)(r1 +0), r3) ;
; QMAP_TOUCH_ARENA(); @ scx_qmap.bpf.c:247
18: (18) r1 = 0xff110001062b6800 ; R1=map_ptr(map=arena,ks=0,vs=0)
; v = bpf_task_storage_get(&task_ctx_stor, p, 0, 0); @ scx_qmap.bpf.c:249
20: (18) r1 = 0xff11000105cafc00 ; R1=map_ptr(map=task_ctx_stor,ks=4,vs=8)
22: (b7) r3 = 0 ; R3=0
23: (b7) r4 = 0 ; R4=0
24: (85) call bpf_task_storage_get#156 ; R0=map_value_or_null(id=1,map=task_ctx_stor,ks=4,vs=8)
; if (!v || !v->taskc) @ scx_qmap.bpf.c:250
25: (15) if r0 == 0x0 goto pc+77 ; R0=map_value(map=task_ctx_stor,ks=4,vs=8)
26: (79) r7 = *(u64 *)(r0 +0) ; R0=map_value(map=task_ctx_stor,ks=4,vs=8) R7=scalar()
27: (15) if r7 == 0x0 goto pc+75 ; R7=scalar(umin=1)
28: (bf) r8 = addr_space_cast(r7, 0, 1) ; R7=scalar(umin=1) R8=arena
; if (taskc && taskc->fifo) { @ scx_qmap.bpf.c:476
29: (79) r9 = *(u64 *)(r8 +24) ; R8=arena R9=scalar()
30: (15) if r9 == 0x0 goto pc+72 ; R9=scalar(umin=1)
; if (taskc->highpri) @ scx_qmap.bpf.c:477
31: (71) r1 = *(u8 *)(r8 +45) ; R1=scalar(smin=smin32=0,smax=umax=smax32=umax32=255,var_off=(0x0; 0xff)) R8=arena
32: (56) if w1 != 0x1 goto pc+9 ; R1=1
33: (b7) r1 = 1 ; R1=1
; __sync_fetch_and_sub(&qa.nr_highpri_queued, 1); @ scx_qmap.bpf.c:478
34: (87) r1 = -r1 ; R1=-1
35: (18) r2 = 0x10000fff9000 ; R2=scalar()
37: (07) r2 += 88 ; R2=scalar()
38: (bf) r2 = addr_space_cast(r2, 0, 1) ; R2=arena
39: (db) r1 = atomic64_fetch_add((u64 *)(r2 +0), r1) ; R1=scalar() R2=arena
; struct qmap_fifo __arena *fifo = taskc->fifo; @ scx_qmap.bpf.c:299
40: (79) r9 = *(u64 *)(r8 +24) ; R8=arena R9=scalar()
; if (!fifo) @ scx_qmap.bpf.c:302
41: (15) if r9 == 0x0 goto pc+61 ; R9=scalar(umin=1)
; lock = qa_q_lock(fifo->idx); @ scx_qmap.bpf.c:305
42: (bf) r2 = addr_space_cast(r9, 0, 1) ; R2=arena R9=scalar(umin=1)
43: (61) r1 = *(u32 *)(r2 +16) ; R1=scalar(smin=0,smax=umax=0xffffffff,var_off=(0x0; 0xffffffff)) R2=arena
; switch (qid) { @ scx_qmap.bpf.c:110
44: (66) if w1 s> 0x1 goto pc+8 ; R1=scalar(smin=0,smax=umax=0xffffffff,smax32=1,var_off=(0x0; 0xffffffff))
45: (18) r6 = 0xff11000105caf1e8 ; R6=map_value(map=.data.qa_q_lock,ks=4,vs=4)
47: (16) if w1 == 0x0 goto pc+17 ; R1=scalar(smin=umin=umin32=1,smax=umax=0xffffffff,smax32=1,var_off=(0x0; 0xffffffff))
48: (16) if w1 == 0x1 goto pc+1 50: R2=arena R7=scalar(umin=1) R8=arena R9=scalar(umin=1) R10=fp0
; switch (qid) { @ scx_qmap.bpf.c:110
50: (18) r6 = 0xff11000105caede8 ; R6=map_value(map=.data.qa_q_lock,ks=4,vs=4)
52: (05) goto pc+12
65: (7b) *(u64 *)(r10 -16) = r2 ; R2=arena R10=fp0 fp-16=arena
; if (bpf_res_spin_lock(lock)) { @ scx_qmap.bpf.c:193
66: (bf) r1 = r6 ; R1=map_value(map=.data.qa_q_lock,ks=4,vs=4) R6=map_value(map=.data.qa_q_lock,ks=4,vs=4)
67: (85) call bpf_res_spin_lock#69027 ; R0=0
68: (16) if w0 == 0x0 goto pc+9 ; R0=0
; if (taskc->fifo != fifo) { @ scx_qmap.bpf.c:310
78: (79) r1 = *(u64 *)(r8 +24) ; R1=scalar() R8=arena
79: (1d) if r1 == r9 goto pc+1 81: R6=map_value(map=.data.qa_q_lock,ks=4,vs=4) R7=scalar(umin=1) R8=arena R10=fp0 fp-16=arena
; bpf_res_spin_unlock(lock); @ scx_qmap.bpf.c:311
81: (79) r2 = *(u64 *)(r8 +16) ; R2=scalar() R8=arena
; if (taskc->q_next) @ scx_qmap.bpf.c:315
82: (79) r1 = *(u64 *)(r8 +8) ; R1=scalar() R8=arena
83: (15) if r1 == 0x0 goto pc+4 ; R1=scalar(umin=1)
84: (bf) r3 = addr_space_cast(r1, 0, 1) ; R1=scalar(umin=1) R3=arena
; taskc->q_next->q_prev = taskc->q_prev; @ scx_qmap.bpf.c:316
85: (7b) *(u64 *)(r3 +16) = r2 ; R2=scalar() R3=arena
; if (taskc->q_prev) @ scx_qmap.bpf.c:319
86: (79) r2 = *(u64 *)(r8 +16) ; R2=scalar() R8=arena
; taskc->q_next->q_prev = taskc->q_prev; @ scx_qmap.bpf.c:316
87: (05) goto pc+2
; fifo->tail = taskc->q_prev; @ scx_qmap.bpf.c:318
90: (07) r7 += 8 ; R7=scalar()
; if (taskc->q_prev) @ scx_qmap.bpf.c:319
91: (15) if r2 == 0x0 goto pc+3 ; R2=scalar(umin=1)
92: (bf) r2 = addr_space_cast(r2, 0, 1) ; R2=arena
; taskc->q_prev->q_next = taskc->q_next; @ scx_qmap.bpf.c:320
93: (7b) *(u64 *)(r2 +8) = r1 ; R1=scalar(umin=1) R2=arena
94: (05) goto pc+2
; fifo->head = taskc->q_next; @ scx_qmap.bpf.c:322
97: (b7) r1 = 0 ; R1=0
; taskc->q_prev = NULL; @ scx_qmap.bpf.c:324
98: (7b) *(u64 *)(r7 +16) = r1
R7 invalid mem access 'scalar'
processed 70 insns (limit 1000000) max_states_per_insn 1 total_states 5 peak_states 5 mark_read 0
-- END PROG LOAD LOG --
libbpf: prog 'qmap_dequeue': failed to load: -EACCES
libbpf: failed to load object 'scx_qmap'
libbpf: failed to load BPF skeleton 'scx_qmap': -EACCES
[SCX_BUG] scx_qmap.c:193 (Permission denied)
Failed to load skelThanks,
Cheng-Yang
---
Cheng-Yang Chou (2):
tools/sched_ext: always define bpf_addr_space_cast tools/sched_ext: scx_qmap: fix arena stores
.../include/scx/bpf_arena_common.bpf.h | 21 +++-- tools/sched_ext/scx_qmap.bpf.c | 85 +++++++++++++------ 2 files changed, 71 insertions(+), 35 deletions(-)
--
2.48.1
PATCH 1/2 tools/sched_ext: always define bpf_addr_space_cast
With __BPF_FEATURE_ADDR_SPACE_CAST set, bpf_addr_space_cast() was defined only in the #else branch, making it unavailable to BPF programs compiled with modern LLVM.
The macro emits an addr_space_cast instruction as raw bytes, which the BPF verifier uses to assign PTR_TO_ARENA to a register. This is needed when LLVM's liveness analysis kills the PTR_TO_ARENA alias before a store, which can occur even with modern LLVM.
Move bpf_addr_space_cast() before the #if block so it is always available. cast_kern() and cast_user() remain no-ops under modern LLVM. The macro is now accessible for explicit workaround use.
--- .../include/scx/bpf_arena_common.bpf.h | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-)
tools/sched_ext/include/scx/bpf_arena_common.bpf.h +12 -9
@@ -14,15 +14,6 @@*/ #endif -#if defined(__BPF_FEATURE_ADDR_SPACE_CAST) && !defined(BPF_ARENA_FORCE_ASM ) -#ifndef __arena -#define __arena __attribute__((address_space(1))) -#endif -#define __arena_global __attribute__((address_space(1))) -#define cast_kern (ptr)/* nop for bpf prog. emitted by LLVM */ -#define cast_user (ptr)/* nop for bpf prog. emitted by LLVM */ -#else -/* emit instruction: * rX = rX .off = BPF_ADDR_SPACE_CAST .imm32 = (dst_as << 16) | src_as *
@@ -33,6 +24,10 @@* tools/testing/selftests/bpf includes tests that use these macros to implement * linked lists and hashtables backed by arena memory. In sched_ext, we use * cast_kern() and cast_user() for compatibility with older LLVM toolchains. +* +* With __BPF_FEATURE_ADDR_SPACE_CAST, LLVM's liveness analysis may kill the +* PTR_TO_ARENA register alias before a store. Call bpf_addr_space_cast() +* explicitly at such sites to re-establish PTR_TO_ARENA. */ #ifndef bpf_addr_space_cast#define bpf_addr_space_cast (var, dst_as, src_as)\
@@ -74,6 +69,14 @@ , [as]"i"((dst_as << 16) | src_as));#endif +#if defined(__BPF_FEATURE_ADDR_SPACE_CAST) && !defined(BPF_ARENA_FORCE_ASM ) +#ifndef __arena +#define __arena __attribute__((address_space(1))) +#endif +#define __arena_global __attribute__((address_space(1))) +#define cast_kern (ptr)/* nop for bpf prog. emitted by LLVM */ +#define cast_user (ptr)/* nop for bpf prog. emitted by LLVM */ +#else #define __arena#define __arena_global SEC(".addr_space.1")#define cast_kern (ptr) bpf_addr_space_cast(ptr, 0, 1)
Signature · 1 line
2.48.1
PATCH 2/2 tools/sched_ext: scx_qmap: fix arena stores
In reply to Cheng-Yang ChouWith __BPF_FEATURE_ADDR_SPACE_CAST, LLVM maintains two aliases for each arena pointer: a scalar (from the map_value load) and a PTR_TO_ARENA (from the addr_space_cast it auto-emits). Liveness analysis kills the PTR_TO_ARENA alias at the last arena read. After that point only the scalar survives. When LLVM generates arena field stores, it uses the scalar register, and the BPF verifier rejects the program with "RN invalid mem access 'scalar'".
This manifests in qmap_fifo_remove() (qmap_dequeue), qmap_fifo_pop() (qmap_dispatch), and qmap_init_task()/qmap_exit_task() after a spin-lock or unlock call.
Casting to scalar, opacifying with barrier_var(), and casting back to arena does not work. With __BPF_FEATURE_ADDR_SPACE_CAST, LLVM understands both ends of the round-trip and silently eliminates it even across the barrier.
Use bpf_addr_space_cast() at every site where the arena alias dies before a write: qmap_fifo_enqueue(), qmap_fifo_pop(), qmap_fifo_remove(), qmap_init_task(), and qmap_exit_task().
--- tools/sched_ext/scx_qmap.bpf.c | 85 +++++++++++++++++++++++----------- 1 file changed, 59 insertions(+), 26 deletions(-)
tools/sched_ext/scx_qmap.bpf.c +59 -26
@@ -256,17 +256,21 @@ static task_ctx_t *lookup_task_ctx(struct task_struct *p) static void qmap_fifo_enqueue(struct qmap_fifo __arena *fifo, task_ctx_t *taskc) {struct bpf_res_spin_lock *lock =qa_q_lock (fifo->idx); +struct task_ctx *__tc;if (!lock ||qmap_spin_lock (lock))return ; - taskc->fifo = fifo; - taskc->q_next =NULL ; - taskc->q_prev = fifo->tail; + __tc = (struct task_ctx *)taskc; +barrier_var (__tc); +bpf_addr_space_cast (__tc,0 ,1 ); + __tc->fifo = fifo; + __tc->q_next =NULL ; + __tc->q_prev = fifo->tail;if (fifo->tail) - fifo->tail->q_next = taskc; + fifo->tail->q_next = (task_ctx_t *)__tc;else - fifo->head = taskc; - fifo->tail = taskc; + fifo->head = (task_ctx_t *)__tc; + fifo->tail = (task_ctx_t *)__tc;bpf_res_spin_unlock (lock); }
@@ -274,6 +278,7 @@ static void qmap_fifo_enqueue(struct qmap_fifo __arena *fifo, task_ctx_t *taskc) static task_ctx_t *qmap_fifo_pop(struct qmap_fifo __arena *fifo) {struct bpf_res_spin_lock *lock =qa_q_lock (fifo->idx); +struct task_ctx *__tc;task_ctx_t *taskc;if (!lock ||qmap_spin_lock (lock))
@@ -285,9 +290,12 @@ static task_ctx_t *qmap_fifo_pop(struct qmap_fifo __arena *fifo) taskc->q_next->q_prev =NULL ; else fifo->tail = NULL; - taskc->q_next =NULL ; - taskc->q_prev =NULL ; - taskc->fifo =NULL ; + __tc = (struct task_ctx *)taskc; +barrier_var (__tc); +bpf_addr_space_cast (__tc,0 ,1 ); + __tc->q_next =NULL ; + __tc->q_prev =NULL ; + __tc->fifo =NULL ; }bpf_res_spin_unlock (lock);return taskc;
@@ -298,6 +306,8 @@ static void qmap_fifo_remove(task_ctx_t *taskc) { struct qmap_fifo __arena *fifo = taskc->fifo;struct bpf_res_spin_lock *lock; +task_ctx_t *q_next, *q_prev; +struct task_ctx *__tc;if (!fifo)return ;
@@ -312,17 +322,29 @@ static void qmap_fifo_remove(task_ctx_t *taskc)return ; } -if (taskc->q_next) - taskc->q_next->q_prev = taskc->q_prev; + q_next = taskc->q_next; + q_prev = taskc->q_prev; + +/* +* NULL out @taskc's queue pointers before updating its neighbors. +* The arena alias dies after loading q_next/q_prev. Refresh it via +* bpf_addr_space_cast() - see bpf_arena_common.bpf.h. +*/ + __tc = (struct task_ctx *)taskc; +barrier_var (__tc); +bpf_addr_space_cast (__tc,0 ,1 ); + __tc->q_next =NULL ; + __tc->q_prev =NULL ; + __tc->fifo =NULL ; + +if (q_next) + q_next->q_prev = q_prev;else - fifo->tail = taskc->q_prev; -if (taskc->q_prev) - taskc->q_prev->q_next = taskc->q_next; + fifo->tail = q_prev; +if (q_prev) + q_prev->q_next = q_next;else - fifo->head = taskc->q_next; - taskc->q_next =NULL ; - taskc->q_prev =NULL ; - taskc->fifo =NULL ; + fifo->head = q_next;bpf_res_spin_unlock (lock); }
@@ -762,6 +784,7 @@ s32 BPF_STRUCT_OPS_SLEEPABLE(qmap_init_task, struct task_struct *p, struct scx_init_task_args *args) {struct task_ctx_stor_val *v; +struct task_ctx *__tc;task_ctx_t *taskc;if (p->tgid == disallow_tgid)
@@ -779,10 +802,13 @@ s32 BPF_STRUCT_OPS_SLEEPABLE(qmap_init_task, struct task_struct *p,return -ENOMEM ; } - taskc->next_free =NULL ; - taskc->q_next =NULL ; - taskc->q_prev =NULL ; - taskc->fifo =NULL ; + __tc = (struct task_ctx *)taskc; +barrier_var (__tc); +bpf_addr_space_cast (__tc,0 ,1 ); + __tc->next_free =NULL ; + __tc->q_next =NULL ; + __tc->q_prev =NULL ; + __tc->fifo =NULL ; taskc->tid = p->scx.tid; taskc->pid = p->pid; taskc->force_local = false;
@@ -798,8 +824,11 @@ s32 BPF_STRUCT_OPS_SLEEPABLE(qmap_init_task, struct task_struct *p,if (!v) {/* push back to the free list */ if (!qmap_spin_lock (&qa_task_lock)) { - taskc->next_free = qa.task_free_head; - qa.task_free_head = taskc; + __tc = (struct task_ctx *)taskc; +barrier_var (__tc); +bpf_addr_space_cast (__tc,0 ,1 ); + __tc->next_free = qa.task_free_head; + qa.task_free_head = (task_ctx_t *)__tc;bpf_res_spin_unlock (&qa_task_lock); }return -ENOMEM ;
@@ -812,6 +841,7 @@ void BPF_STRUCT_OPS(qmap_exit_task, struct task_struct *p, struct scx_exit_task_args *args) {struct task_ctx_stor_val *v; +struct task_ctx *__tc;task_ctx_t *taskc; v =bpf_task_storage_get (&task_ctx_stor, p,NULL ,0 );
@@ -822,8 +852,11 @@ void BPF_STRUCT_OPS(qmap_exit_task, struct task_struct *p,if (qmap_spin_lock (&qa_task_lock))return ; - taskc->next_free = qa.task_free_head; - qa.task_free_head = taskc; + __tc = (struct task_ctx *)taskc; +barrier_var (__tc); +bpf_addr_space_cast (__tc,0 ,1 ); + __tc->next_free = qa.task_free_head; + qa.task_free_head = (task_ctx_t *)__tc;bpf_res_spin_unlock (&qa_task_lock); }
Signature · 1 line
2.48.1
[email protected]
AI reviewRe: [PATCH 2/2] tools/sched_ext: scx_qmap: fix arena stores
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Incomplete refactoring leaves arena field stores using the dead scalar alias, causing BPF verifier rejection. --
commit fcbdf79612bdb874457d684cdbba45646312d3d5
Author: Cheng-Yang Chou <[email protected]>
tools/sched_ext: scx_qmap: fix arena stores
This patch addresses a BPF verifier rejection in scx_qmap when compiled with __BPF_FEATURE_ADDR_SPACE_CAST. It introduces bpf_addr_space_cast() and a barrier_var() to ensure LLVM preserves the PTR_TO_ARENA alias for arena pointer stores, fixing errors across spin-lock and unlock calls.
Quoted 4 lines
tools/sched_ext/scx_qmap.bpf.c +0
-0
[ ... ]
Quoted 16 lines
tools/sched_ext/scx_qmap.bpf.c +1
-4@@ -762,6 +784,7 @@ s32 BPF_STRUCT_OPS_SLEEPABLE(qmap_init_task, struct task_struct *p, struct scx_init_task_args *args) {struct task_ctx_stor_val *v; +struct task_ctx *__tc;task_ctx_t *taskc;if (p->tgid == disallow_tgid)@@ -779,10 +802,13 @@ s32 BPF_STRUCT_OPS_SLEEPABLE(qmap_init_task, struct task_struct *p,return -ENOMEM ; } - taskc->next_free =NULL ; - taskc->q_next =NULL ; - taskc->q_prev =NULL ; - taskc->fifo =NULL ;
tools/sched_ext/scx_qmap.bpf.c s32 BPF_STRUCT_OPS_SLEEPABLE(qmap_init_task, struct task_struct *p, · line 814
Quoted 10 lines
tools/sched_ext/scx_qmap.bpf.c +7
-0+ __tc = (struct task_ctx *)taskc; +barrier_var (__tc); +bpf_addr_space_cast (__tc,0 ,1 ); + __tc->next_free =NULL ; + __tc->q_next =NULL ; + __tc->q_prev =NULL ; + __tc->fifo =NULL ; taskc->tid = p->scx.tid; taskc->pid = p->pid; taskc->force_local = false;
[Severity: High]
This isn't a bug introduced by this patch, but does this incomplete refactoring leave the remaining field stores vulnerable to the same BPF verifier rejection?
The patch applies the __tc cast to the first four pointer fields (next_free, q_next, q_prev, fifo), but leaves tid, pid, force_local, highpri, core_sched_seq, and cpus_allowed using the original taskc pointer.
Since LLVM tracks liveness at the register level, will the original taskc pointer still use the dead scalar alias for these remaining stores, causing the verifier to continue rejecting the program?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Quoted 11 lines · Cheng-Yang Chou
scx_qmapfails to load with modern LLVM due to LLVM liveness killing thePTR_TO_ARENAregister alias before arena stores. The BPF verifier rejects these stores with "RN invalid mem access 'scalar'".Patch 1: moves
bpf_addr_space_cast()before the #if block so it is always available regardless of LLVM version.Patch 2: calls
bpf_addr_space_cast()at every arena store site inscx_qmapwhere thePTR_TO_ARENAalias dies before the write.Failure observed on sched_ext/for-next with vng (without this series):
I do not think this is the way to go about this problem. Annotations, especially heavily BPF-specific like this, are too obscure for us to expect people to use. If anything this seems like something we should handle in the verifier.
Can you elaborate on "LLVM liveness killing the register alias"? This seems like missing addr_space_cast() instructions added by the compiler, for some reason it tries to use r7 directly instead of either reusing or rebuilding r8. Also, which Clang version are you using?
Quoted 122 lines
root@virtme-ng:/home/ubuntu/sched_ext/tools/sched_ext# ./build/bin/scx_qmap libbpf: prog 'qmap_dequeue': BPF program load failed: -EACCES libbpf: prog 'qmap_dequeue': -- BEGIN PROG LOAD LOG -- 0: R1=ctx() R10=fp0 ; void BPF_STRUCT_OPS(qmap_dequeue, struct task_struct *p, u64 deq_flags) @ scx_qmap.bpf.c:467 0: (79) r2 = *(u64 *)(r1 +0) func 'dequeue' arg0 has btf_id 115922 type STRUCT 'task_struct' 1: R1=ctx() R2=trusted_ptr_task_struct() 1: (79) r4 = *(u64 *)(r1 +8) ; R1=ctx() R4=scalar() ; __sync_fetch_and_add(&qa.nr_dequeued, 1); @ scx_qmap.bpf.c:471 2: (18) r1 = 0x10000fff9000 ; R1=scalar() 4: (18) r3 = 0x10000fff9000 ; R3=scalar() 6: (07) r3 += 32 ; R3=scalar() 7: (bf) r5 = addr_space_cast(r3, 0, 1) ; R3=scalar() R5=arena 8: (b7) r3 = 1 ; R3=1 9: (b7) r0 = 1 ; R0=1 10: (db) r0 = atomic64_fetch_add((u64 *)(r5 +0), r0) ; R0=scalar() R5=arena 11: (18) r5 = 0x100000000 ; R5=0x100000000 ; if (deq_flags & SCX_DEQ_CORE_SCHED_EXEC) @ scx_qmap.bpf.c:472 13: (5f) r4 &= r5 ; R4=scalar(smin=smin32=0,smax=umax=0x100000000,smax32=umax32=0,var_off=(0x0; 0x100000000)) 14: (15) if r4 == 0x0 goto pc+3 ; R4=0x100000000 ; __sync_fetch_and_add(&qa.nr_core_sched_execed, 1); @ scx_qmap.bpf.c:473 15: (07) r1 += 48 ; R1=scalar() 16: (bf) r1 = addr_space_cast(r1, 0, 1) ; R1=arena 17: (db) r3 = atomic64_fetch_add((u64 *)(r1 +0), r3) ; ; QMAP_TOUCH_ARENA(); @ scx_qmap.bpf.c:247 18: (18) r1 = 0xff110001062b6800 ; R1=map_ptr(map=arena,ks=0,vs=0) ; v = bpf_task_storage_get(&task_ctx_stor, p, 0, 0); @ scx_qmap.bpf.c:249 20: (18) r1 = 0xff11000105cafc00 ; R1=map_ptr(map=task_ctx_stor,ks=4,vs=8) 22: (b7) r3 = 0 ; R3=0 23: (b7) r4 = 0 ; R4=0 24: (85) call bpf_task_storage_get#156 ; R0=map_value_or_null(id=1,map=task_ctx_stor,ks=4,vs=8) ; if (!v || !v->taskc) @ scx_qmap.bpf.c:250 25: (15) if r0 == 0x0 goto pc+77 ; R0=map_value(map=task_ctx_stor,ks=4,vs=8) 26: (79) r7 = *(u64 *)(r0 +0) ; R0=map_value(map=task_ctx_stor,ks=4,vs=8) R7=scalar() 27: (15) if r7 == 0x0 goto pc+75 ; R7=scalar(umin=1) 28: (bf) r8 = addr_space_cast(r7, 0, 1) ; R7=scalar(umin=1) R8=arena ; if (taskc && taskc->fifo) { @ scx_qmap.bpf.c:476 29: (79) r9 = *(u64 *)(r8 +24) ; R8=arena R9=scalar() 30: (15) if r9 == 0x0 goto pc+72 ; R9=scalar(umin=1) ; if (taskc->highpri) @ scx_qmap.bpf.c:477 31: (71) r1 = *(u8 *)(r8 +45) ; R1=scalar(smin=smin32=0,smax=umax=smax32=umax32=255,var_off=(0x0; 0xff)) R8=arena 32: (56) if w1 != 0x1 goto pc+9 ; R1=1 33: (b7) r1 = 1 ; R1=1 ; __sync_fetch_and_sub(&qa.nr_highpri_queued, 1); @ scx_qmap.bpf.c:478 34: (87) r1 = -r1 ; R1=-1 35: (18) r2 = 0x10000fff9000 ; R2=scalar() 37: (07) r2 += 88 ; R2=scalar() 38: (bf) r2 = addr_space_cast(r2, 0, 1) ; R2=arena 39: (db) r1 = atomic64_fetch_add((u64 *)(r2 +0), r1) ; R1=scalar() R2=arena ; struct qmap_fifo __arena *fifo = taskc->fifo; @ scx_qmap.bpf.c:299 40: (79) r9 = *(u64 *)(r8 +24) ; R8=arena R9=scalar() ; if (!fifo) @ scx_qmap.bpf.c:302 41: (15) if r9 == 0x0 goto pc+61 ; R9=scalar(umin=1) ; lock = qa_q_lock(fifo->idx); @ scx_qmap.bpf.c:305 42: (bf) r2 = addr_space_cast(r9, 0, 1) ; R2=arena R9=scalar(umin=1) 43: (61) r1 = *(u32 *)(r2 +16) ; R1=scalar(smin=0,smax=umax=0xffffffff,var_off=(0x0; 0xffffffff)) R2=arena ; switch (qid) { @ scx_qmap.bpf.c:110 44: (66) if w1 s> 0x1 goto pc+8 ; R1=scalar(smin=0,smax=umax=0xffffffff,smax32=1,var_off=(0x0; 0xffffffff)) 45: (18) r6 = 0xff11000105caf1e8 ; R6=map_value(map=.data.qa_q_lock,ks=4,vs=4) 47: (16) if w1 == 0x0 goto pc+17 ; R1=scalar(smin=umin=umin32=1,smax=umax=0xffffffff,smax32=1,var_off=(0x0; 0xffffffff)) 48: (16) if w1 == 0x1 goto pc+1 50: R2=arena R7=scalar(umin=1) R8=arena R9=scalar(umin=1) R10=fp0 ; switch (qid) { @ scx_qmap.bpf.c:110 50: (18) r6 = 0xff11000105caede8 ; R6=map_value(map=.data.qa_q_lock,ks=4,vs=4) 52: (05) goto pc+12 65: (7b) *(u64 *)(r10 -16) = r2 ; R2=arena R10=fp0 fp-16=arena ; if (bpf_res_spin_lock(lock)) { @ scx_qmap.bpf.c:193 66: (bf) r1 = r6 ; R1=map_value(map=.data.qa_q_lock,ks=4,vs=4) R6=map_value(map=.data.qa_q_lock,ks=4,vs=4) 67: (85) call bpf_res_spin_lock#69027 ; R0=0 68: (16) if w0 == 0x0 goto pc+9 ; R0=0 ; if (taskc->fifo != fifo) { @ scx_qmap.bpf.c:310 78: (79) r1 = *(u64 *)(r8 +24) ; R1=scalar() R8=arena 79: (1d) if r1 == r9 goto pc+1 81: R6=map_value(map=.data.qa_q_lock,ks=4,vs=4) R7=scalar(umin=1) R8=arena R10=fp0 fp-16=arena ; bpf_res_spin_unlock(lock); @ scx_qmap.bpf.c:311 81: (79) r2 = *(u64 *)(r8 +16) ; R2=scalar() R8=arena ; if (taskc->q_next) @ scx_qmap.bpf.c:315 82: (79) r1 = *(u64 *)(r8 +8) ; R1=scalar() R8=arena 83: (15) if r1 == 0x0 goto pc+4 ; R1=scalar(umin=1) 84: (bf) r3 = addr_space_cast(r1, 0, 1) ; R1=scalar(umin=1) R3=arena ; taskc->q_next->q_prev = taskc->q_prev; @ scx_qmap.bpf.c:316 85: (7b) *(u64 *)(r3 +16) = r2 ; R2=scalar() R3=arena ; if (taskc->q_prev) @ scx_qmap.bpf.c:319 86: (79) r2 = *(u64 *)(r8 +16) ; R2=scalar() R8=arena ; taskc->q_next->q_prev = taskc->q_prev; @ scx_qmap.bpf.c:316 87: (05) goto pc+2 ; fifo->tail = taskc->q_prev; @ scx_qmap.bpf.c:318 90: (07) r7 += 8 ; R7=scalar() ; if (taskc->q_prev) @ scx_qmap.bpf.c:319 91: (15) if r2 == 0x0 goto pc+3 ; R2=scalar(umin=1) 92: (bf) r2 = addr_space_cast(r2, 0, 1) ; R2=arena ; taskc->q_prev->q_next = taskc->q_next; @ scx_qmap.bpf.c:320 93: (7b) *(u64 *)(r2 +8) = r1 ; R1=scalar(umin=1) R2=arena 94: (05) goto pc+2 ; fifo->head = taskc->q_next; @ scx_qmap.bpf.c:322 97: (b7) r1 = 0 ; R1=0 ; taskc->q_prev = NULL; @ scx_qmap.bpf.c:324 98: (7b) *(u64 *)(r7 +16) = r1 R7 invalid mem access 'scalar' processed 70 insns (limit 1000000) max_states_per_insn 1 total_states 5 peak_states 5 mark_read 0 -- END PROG LOAD LOG -- libbpf: prog 'qmap_dequeue': failed to load: -EACCES libbpf: failed to load object 'scx_qmap' libbpf: failed to load BPF skeleton 'scx_qmap': -EACCES [SCX_BUG] scx_qmap.c:193 (Permission denied) Failed to load skelThanks,
Cheng-Yang---
Cheng-Yang Chou (2):
tools/sched_ext: always definebpf_addr_space_casttools/sched_ext:scx_qmap: fix arena stores.../include/scx/bpf_arena_common.bpf.h | 21 +++-- tools/sched_ext/scx_qmap.bpf.c | 85 +++++++++++++------ 2 files changed, 71 insertions(+), 35 deletions(-)--
2.48.1
Hi Emil,
Quoted 11 lines · Emil Tsalapatis
Quoted 10 lines · Cheng-Yang Chou
scx_qmapfails to load with modern LLVM due to LLVM liveness killing thePTR_TO_ARENAregister alias before arena stores. The BPF verifier rejects these stores with "RN invalid mem access 'scalar'".Patch 1: moves
bpf_addr_space_cast()before the #if block so it is always available regardless of LLVM version.Patch 2: calls
bpf_addr_space_cast()at every arena store site inscx_qmapwhere thePTR_TO_ARENAalias dies before the write.
Quoted 6 lines
Quoted 1 line
Failure observed on sched_ext/for-next with vng (without this series):
I do not think this is the way to go about this problem. Annotations, especially heavily BPF-specific like this, are too obscure for us to expect people to use. If anything this seems like something we should handle in the verifier.
Agreed.
Quoted 5 lines
Can you elaborate on "LLVM liveness killing the register alias"? This seems like missing
addr_space_cast()instructions added by the compiler, for some reason it tries to use r7 directly instead of either reusing or rebuilding r8.
Liveness analysis marks r8 dead after insn 86. When generating the stores at insn 98, LLVM uses r7 without re-emitting addr_space_cast. From the IR perspective the store target is the same pointer. IIUC, LLVM relies on the address-space annotation on the type rather than on the register alias, so no second cast is emitted. The BPF backend translates that to a scalar store, which the verifier rejects.
26: (79) r7 = *(u64 *)(r0 +0) ; R7=scalar() 28: (bf) r8 = addr_space_cast(r7, 0, 1) ; R7=scalar(umin=1) R8=arena ... 86: (79) r2 = *(u64 *)(r8 +16) ; R8=arena <- last use of r8 ... 90: (07) r7 += 8 ; R7=scalar <- r8 dead, LLVM uses r7 ... 98: (7b) *(u64 *)(r7 +16) = r1 R7 invalid mem access 'scalar'
Quoted 1 line
Also, which Clang version are you using?
$ clang --version Ubuntu clang version 20.1.2 (0ubuntu1) Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/lib/llvm-20/bin
Thanks.
Quoted 123 lines
Quoted 122 lines
root@virtme-ng:/home/ubuntu/sched_ext/tools/sched_ext# ./build/bin/scx_qmap libbpf: prog 'qmap_dequeue': BPF program load failed: -EACCES libbpf: prog 'qmap_dequeue': -- BEGIN PROG LOAD LOG -- 0: R1=ctx() R10=fp0 ; void BPF_STRUCT_OPS(qmap_dequeue, struct task_struct *p, u64 deq_flags) @ scx_qmap.bpf.c:467 0: (79) r2 = *(u64 *)(r1 +0) func 'dequeue' arg0 has btf_id 115922 type STRUCT 'task_struct' 1: R1=ctx() R2=trusted_ptr_task_struct() 1: (79) r4 = *(u64 *)(r1 +8) ; R1=ctx() R4=scalar() ; __sync_fetch_and_add(&qa.nr_dequeued, 1); @ scx_qmap.bpf.c:471 2: (18) r1 = 0x10000fff9000 ; R1=scalar() 4: (18) r3 = 0x10000fff9000 ; R3=scalar() 6: (07) r3 += 32 ; R3=scalar() 7: (bf) r5 = addr_space_cast(r3, 0, 1) ; R3=scalar() R5=arena 8: (b7) r3 = 1 ; R3=1 9: (b7) r0 = 1 ; R0=1 10: (db) r0 = atomic64_fetch_add((u64 *)(r5 +0), r0) ; R0=scalar() R5=arena 11: (18) r5 = 0x100000000 ; R5=0x100000000 ; if (deq_flags & SCX_DEQ_CORE_SCHED_EXEC) @ scx_qmap.bpf.c:472 13: (5f) r4 &= r5 ; R4=scalar(smin=smin32=0,smax=umax=0x100000000,smax32=umax32=0,var_off=(0x0; 0x100000000)) 14: (15) if r4 == 0x0 goto pc+3 ; R4=0x100000000 ; __sync_fetch_and_add(&qa.nr_core_sched_execed, 1); @ scx_qmap.bpf.c:473 15: (07) r1 += 48 ; R1=scalar() 16: (bf) r1 = addr_space_cast(r1, 0, 1) ; R1=arena 17: (db) r3 = atomic64_fetch_add((u64 *)(r1 +0), r3) ; ; QMAP_TOUCH_ARENA(); @ scx_qmap.bpf.c:247 18: (18) r1 = 0xff110001062b6800 ; R1=map_ptr(map=arena,ks=0,vs=0) ; v = bpf_task_storage_get(&task_ctx_stor, p, 0, 0); @ scx_qmap.bpf.c:249 20: (18) r1 = 0xff11000105cafc00 ; R1=map_ptr(map=task_ctx_stor,ks=4,vs=8) 22: (b7) r3 = 0 ; R3=0 23: (b7) r4 = 0 ; R4=0 24: (85) call bpf_task_storage_get#156 ; R0=map_value_or_null(id=1,map=task_ctx_stor,ks=4,vs=8) ; if (!v || !v->taskc) @ scx_qmap.bpf.c:250 25: (15) if r0 == 0x0 goto pc+77 ; R0=map_value(map=task_ctx_stor,ks=4,vs=8) 26: (79) r7 = *(u64 *)(r0 +0) ; R0=map_value(map=task_ctx_stor,ks=4,vs=8) R7=scalar() 27: (15) if r7 == 0x0 goto pc+75 ; R7=scalar(umin=1) 28: (bf) r8 = addr_space_cast(r7, 0, 1) ; R7=scalar(umin=1) R8=arena ; if (taskc && taskc->fifo) { @ scx_qmap.bpf.c:476 29: (79) r9 = *(u64 *)(r8 +24) ; R8=arena R9=scalar() 30: (15) if r9 == 0x0 goto pc+72 ; R9=scalar(umin=1) ; if (taskc->highpri) @ scx_qmap.bpf.c:477 31: (71) r1 = *(u8 *)(r8 +45) ; R1=scalar(smin=smin32=0,smax=umax=smax32=umax32=255,var_off=(0x0; 0xff)) R8=arena 32: (56) if w1 != 0x1 goto pc+9 ; R1=1 33: (b7) r1 = 1 ; R1=1 ; __sync_fetch_and_sub(&qa.nr_highpri_queued, 1); @ scx_qmap.bpf.c:478 34: (87) r1 = -r1 ; R1=-1 35: (18) r2 = 0x10000fff9000 ; R2=scalar() 37: (07) r2 += 88 ; R2=scalar() 38: (bf) r2 = addr_space_cast(r2, 0, 1) ; R2=arena 39: (db) r1 = atomic64_fetch_add((u64 *)(r2 +0), r1) ; R1=scalar() R2=arena ; struct qmap_fifo __arena *fifo = taskc->fifo; @ scx_qmap.bpf.c:299 40: (79) r9 = *(u64 *)(r8 +24) ; R8=arena R9=scalar() ; if (!fifo) @ scx_qmap.bpf.c:302 41: (15) if r9 == 0x0 goto pc+61 ; R9=scalar(umin=1) ; lock = qa_q_lock(fifo->idx); @ scx_qmap.bpf.c:305 42: (bf) r2 = addr_space_cast(r9, 0, 1) ; R2=arena R9=scalar(umin=1) 43: (61) r1 = *(u32 *)(r2 +16) ; R1=scalar(smin=0,smax=umax=0xffffffff,var_off=(0x0; 0xffffffff)) R2=arena ; switch (qid) { @ scx_qmap.bpf.c:110 44: (66) if w1 s> 0x1 goto pc+8 ; R1=scalar(smin=0,smax=umax=0xffffffff,smax32=1,var_off=(0x0; 0xffffffff)) 45: (18) r6 = 0xff11000105caf1e8 ; R6=map_value(map=.data.qa_q_lock,ks=4,vs=4) 47: (16) if w1 == 0x0 goto pc+17 ; R1=scalar(smin=umin=umin32=1,smax=umax=0xffffffff,smax32=1,var_off=(0x0; 0xffffffff)) 48: (16) if w1 == 0x1 goto pc+1 50: R2=arena R7=scalar(umin=1) R8=arena R9=scalar(umin=1) R10=fp0 ; switch (qid) { @ scx_qmap.bpf.c:110 50: (18) r6 = 0xff11000105caede8 ; R6=map_value(map=.data.qa_q_lock,ks=4,vs=4) 52: (05) goto pc+12 65: (7b) *(u64 *)(r10 -16) = r2 ; R2=arena R10=fp0 fp-16=arena ; if (bpf_res_spin_lock(lock)) { @ scx_qmap.bpf.c:193 66: (bf) r1 = r6 ; R1=map_value(map=.data.qa_q_lock,ks=4,vs=4) R6=map_value(map=.data.qa_q_lock,ks=4,vs=4) 67: (85) call bpf_res_spin_lock#69027 ; R0=0 68: (16) if w0 == 0x0 goto pc+9 ; R0=0 ; if (taskc->fifo != fifo) { @ scx_qmap.bpf.c:310 78: (79) r1 = *(u64 *)(r8 +24) ; R1=scalar() R8=arena 79: (1d) if r1 == r9 goto pc+1 81: R6=map_value(map=.data.qa_q_lock,ks=4,vs=4) R7=scalar(umin=1) R8=arena R10=fp0 fp-16=arena ; bpf_res_spin_unlock(lock); @ scx_qmap.bpf.c:311 81: (79) r2 = *(u64 *)(r8 +16) ; R2=scalar() R8=arena ; if (taskc->q_next) @ scx_qmap.bpf.c:315 82: (79) r1 = *(u64 *)(r8 +8) ; R1=scalar() R8=arena 83: (15) if r1 == 0x0 goto pc+4 ; R1=scalar(umin=1) 84: (bf) r3 = addr_space_cast(r1, 0, 1) ; R1=scalar(umin=1) R3=arena ; taskc->q_next->q_prev = taskc->q_prev; @ scx_qmap.bpf.c:316 85: (7b) *(u64 *)(r3 +16) = r2 ; R2=scalar() R3=arena ; if (taskc->q_prev) @ scx_qmap.bpf.c:319 86: (79) r2 = *(u64 *)(r8 +16) ; R2=scalar() R8=arena ; taskc->q_next->q_prev = taskc->q_prev; @ scx_qmap.bpf.c:316 87: (05) goto pc+2 ; fifo->tail = taskc->q_prev; @ scx_qmap.bpf.c:318 90: (07) r7 += 8 ; R7=scalar() ; if (taskc->q_prev) @ scx_qmap.bpf.c:319 91: (15) if r2 == 0x0 goto pc+3 ; R2=scalar(umin=1) 92: (bf) r2 = addr_space_cast(r2, 0, 1) ; R2=arena ; taskc->q_prev->q_next = taskc->q_next; @ scx_qmap.bpf.c:320 93: (7b) *(u64 *)(r2 +8) = r1 ; R1=scalar(umin=1) R2=arena 94: (05) goto pc+2 ; fifo->head = taskc->q_next; @ scx_qmap.bpf.c:322 97: (b7) r1 = 0 ; R1=0 ; taskc->q_prev = NULL; @ scx_qmap.bpf.c:324 98: (7b) *(u64 *)(r7 +16) = r1 R7 invalid mem access 'scalar' processed 70 insns (limit 1000000) max_states_per_insn 1 total_states 5 peak_states 5 mark_read 0 -- END PROG LOAD LOG -- libbpf: prog 'qmap_dequeue': failed to load: -EACCES libbpf: failed to load object 'scx_qmap' libbpf: failed to load BPF skeleton 'scx_qmap': -EACCES [SCX_BUG] scx_qmap.c:193 (Permission denied) Failed to load skelThanks,
Cheng-Yang---
Cheng-Yang Chou (2):
tools/sched_ext: always definebpf_addr_space_casttools/sched_ext:scx_qmap: fix arena stores.../include/scx/bpf_arena_common.bpf.h | 21 +++-- tools/sched_ext/scx_qmap.bpf.c | 85 +++++++++++++------ 2 files changed, 71 insertions(+), 35 deletions(-)--
2.48.1
Signature · 2 lines
Cheers, Cheng-Yang