sched_ext: Sync tools headers with scx and fix unsigned enum reads
Tejun Heo
reviewerPATCH 1/1 sched_ext: Sync tools headers with scx and fix unsigned enum reads
Hello,
This patchset fixes __COMPAT_read_enum() sign-extending unsigned 4-byte enums, which made SCX_RESCUE_DISABLE read as 0xffffffffffffffff, syncs the shared headers in tools/sched_ext/include with the scx repo, and drops the enum lookup table entries for enumerators the kernel no longer has. The scx side is https://github.com/sched-ext/scx/pull/3930, merged, and
https://github.com/sched-ext/scx/pull/3932.
With both applied, the shared headers in tools/sched_ext/include are identical to scx's except the two generated tables, enum_defs.autogen.h and enums_abi.autogen.h, which each side regenerates from its own vmlinux.h.
Based on sched_ext/for-7.5 (dd3b1d9be27c).
This patchset contains the following 4 patches.
0001 sched_ext: Don't sign-extend unsigned enums in __COMPAT_read_enum() 0002 sched_ext: Sync tools autogen enum lookup tables from the scx repo 0003 sched_ext: Sync common and compat headers from the scx repo 0004 sched_ext: Drop stale entries from the enum lookup tables
The patchset is also available in the following git branch:
git://git.kernel.org/pub/scm/linux/kernel/git/tj/sched_ext.git enums-sync
diffstat follows. Thanks.
tools/sched_ext/include/scx/bpf_arena_common.bpf.h | 23 +++++++++++++++++++++- tools/sched_ext/include/scx/common.bpf.h | 9 +++++++++ tools/sched_ext/include/scx/compat.bpf.h | 16 +++++++-------- tools/sched_ext/include/scx/compat.h | 6 +++++- tools/sched_ext/include/scx/enums.autogen.bpf.h | 18 +++-------------- tools/sched_ext/include/scx/enums.autogen.h | 6 +----- tools/sched_ext/scx_flatcg.bpf.c | 5 ----- 7 files changed, 48 insertions(+), 35 deletions(-)
--
tejun
Tejun Heo
reviewerPATCH 1/4 sched_ext: Don't sign-extend unsigned enums in __COMPAT_read_enum()
__COMPAT_read_enum() looks an enumerator up in the running kernel's BTF. For a 4-byte enum, it assigns struct btf_enum.val, an __s32, to the u64 result, which sign-extends. BTF records the enum's signedness in the type's kflag. The code ignores it, so an unsigned enumerator with bit 31 set is sign-extended too.
b1c2a6b0d90d ("sched_ext: scx_qmap: Read SCX_RESCUE_DISABLE through the enum lookup tables") added SCX_RESCUE_DISABLE to the enum lookup tables. It is U32_MAX in enum scx_consts and the only such enumerator in the tables. BPF code sees it as 0xffffffffffffffff instead of 0xffffffff, so a comparison against a u32 holding the real value never matches.
scx_qmap, its only user, narrows the value to u32 in userspace and is unaffected.
Sign-extend only when the kflag says the enum is signed and zero-extend otherwise. A signed enum keeps the sign extension, which is what C gives when the enumerator converts to u64. SCX_TASK_CURSOR, 1 << 31 in a signed enum, still reads 0xffffffff80000000. Verified with a probe against a built kernel's BTF.
--- tools/sched_ext/include/scx/compat.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
tools/sched_ext/include/scx/compat.h +5 -1
@@ -143,7 +143,11 @@ static inline bool __COMPAT_read_enum(const char *type, const char *name, u64 *vif (t->size ==8 && !strncmp (type,"scx_" ,4 ))return __COMPAT_recover_truncated_enum64 (type, name, (u32 )e[i].val, v); - *v = e[i].val; +/* kflag marks a signed enum */ +if (btf_kflag (t)) + *v = (s64 )e[i].val; +else + *v = (u32 )e[i].val;return true; } }
Signature · 1 line
2.56.0
Tejun Heo
reviewerPATCH 2/4 sched_ext: Sync tools autogen enum lookup tables from the scx repo
In reply to Tejun HeoSync the two enum lookup tables, enums.autogen.h and enums.autogen.bpf.h, with the scx repo at a094b6ec68c9 ("scheds/include: Sync with kernel sched_ext/for-7.5 (b1c2a6b0d90d)").
The kernel copy gains SCX_ENQ_CPU_SELECTED, which scx added in f2fd24198edc ("scheds/include: Add SCX_ENQ_CPU_SELECTED to the enum tables"). The flag's value changed in 376f8963bbda ("sched: Re-arrange the {EN,DE}QUEUE flags"), so BPF code should read it from the running kernel.
ebf993609d70 ("sched_ext: Regenerate tools autogen enum headers") regenerated the other two tables, enum_defs.autogen.h and enums_abi.autogen.h, from this tree's vmlinux.h. They are newer than scx's copies and stay as they are.
--- tools/sched_ext/include/scx/enums.autogen.bpf.h | 3 +++ tools/sched_ext/include/scx/enums.autogen.h | 1 + 2 files changed, 4 insertions(+)
tools/sched_ext/include/scx/enums.autogen.bpf.h +3 -0
@@ -130,6 +130,9 @@ const volatile u64 __SCX_ENQ_WAKEUP __weak;const volatile u64 __SCX_ENQ_HEAD__weak ;#define SCX_ENQ_HEAD __SCX_ENQ_HEAD +const volatile u64 __SCX_ENQ_CPU_SELECTED__weak ; +#define SCX_ENQ_CPU_SELECTED __SCX_ENQ_CPU_SELECTED +const volatile u64 __SCX_ENQ_PREEMPT__weak ;#define SCX_ENQ_PREEMPT __SCX_ENQ_PREEMPT
tools/sched_ext/include/scx/enums.autogen.h +1 -0
@@ -48,6 +48,7 @@SCX_ENUM_SET (skel, scx_kick_flags,SCX_KICK_ALL_FLAGS ); \SCX_ENUM_SET (skel, scx_enq_flags,SCX_ENQ_WAKEUP ); \SCX_ENUM_SET (skel, scx_enq_flags,SCX_ENQ_HEAD ); \ +SCX_ENUM_SET (skel, scx_enq_flags,SCX_ENQ_CPU_SELECTED ); \SCX_ENUM_SET (skel, scx_enq_flags,SCX_ENQ_PREEMPT ); \SCX_ENUM_SET (skel, scx_enq_flags,SCX_ENQ_PREEMPT_LAZY ); \SCX_ENUM_SET (skel, scx_enq_flags,SCX_ENQ_IMMED ); \
Signature · 1 line
2.56.0
Tejun Heo
reviewerPATCH 3/4 sched_ext: Sync common and compat headers from the scx repo
In reply to Tejun HeoSync common.bpf.h, compat.bpf.h and bpf_arena_common.bpf.h with the scx repo at a094b6ec68c9 ("scheds/include: Sync with kernel sched_ext/for-7.5 (b1c2a6b0d90d)"), which accumulated the following since the last sync:
- common.bpf.h gained div_round_up(), the bpf_local_irq_save() and bpf_local_irq_restore() declarations for scx's irqsave spinlock guard, and the bpf_call_rcu() declaration, whose bpf_rcu_callback_t came into for-7.5 with the bpf-next-7.4 merge.
- The scx_bpf_task_set_slice() and scx_bpf_task_set_dsq_vtime() compat wrappers return the kfunc's bool instead of dropping it.
- bpf_arena_common.bpf.h has an opt-in arena mode, SCX_ARENA_SCALAR, for loaders that set BPF_F_ARENA_SCALAR.
scx_flatcg.bpf.c defined a static div_round_up() of its own, which the macro of the same name now breaks. Drop it, as the macro computes the same value.
--- .../include/scx/bpf_arena_common.bpf.h | 23 ++++++++++++++++++- tools/sched_ext/include/scx/common.bpf.h | 9 ++++++++ tools/sched_ext/include/scx/compat.bpf.h | 16 ++++++------- tools/sched_ext/scx_flatcg.bpf.c | 5 ---- 4 files changed, 39 insertions(+), 14 deletions(-)
tools/sched_ext/include/scx/bpf_arena_common.bpf.h +22 -1
@@ -14,7 +14,28 @@*/ #endif -#if defined(__BPF_FEATURE_ADDR_SPACE_CAST) && !defined(BPF_ARENA_FORCE_ASM ) +#if defined(SCX_ARENA_SCALAR ) +/* +* Arena pointers without address_space(1). +* +* Under BPF_F_ARENA_SCALAR a load or store through a plain scalar is an arena +* access: the JIT adds the arena base to the address's low 32 bits. A pointer +* loaded from arena memory is a scalar to the verifier anyway, so the tag and +* the casts LLVM emits for it can go, which makes the code a lot tidier. +* +* On a global, address_space(1) does two things: it places the variable in +* .addr_space.1, which libbpf maps into the arena, and it makes LLVM cast every +* access to it. SEC(".addr_space.1") does only the first, which is all that is +* still needed. An object that lives in the arena is therefore declared +* __arena_global, since __arena on it now places nothing. +*/ +#define __arena +#define __arena_global SEC(".addr_space.1") +#define cast_kern (ptr) +#define cast_user (ptr) +/* the loader keys BPF_F_ARENA_SCALAR on this marker */ +const volatile bool __SCX_MARKER_arena_scalar__weak = true; +#elif defined(__BPF_FEATURE_ADDR_SPACE_CAST) && !defined(BPF_ARENA_FORCE_ASM )#ifndef __arena#define __arena __attribute__((address_space(1)))#endif
tools/sched_ext/include/scx/common.bpf.h +9 -0
@@ -29,6 +29,8 @@#include "bpf_arena_common.bpf.h" #include "const-defs.h" +#define div_round_up (a, b) (((a) + (b) - 1) / (b)) +#define PF_IDLE 0x00000002/* I am an IDLE thread */ #define PF_IO_WORKER 0x00000010/* Task is an IO worker */ #define PF_WQ_WORKER 0x00000020/* I'm a workqueue worker */
@@ -127,6 +129,9 @@ void scx_bpf_sub_revoke(u64 cgroup_id, u64 caps, const struct scx_cmask __arena s32 scx_bpf_sub_caps(u64 cgroup_id, u64 caps, struct scx_cmask __arena *out__arena) __ksym __weak; s32 scx_bpf_sub_kill_bstr(u64 cgroup_id, char *fmt, unsigned long long *data, u32 data__sz) __ksym __weak; +int bpf_call_rcu(struct bpf_rcu_head *rh, void *map__const_map, + bpf_rcu_callback_t callback) __ksym __weak; +/* * Use the following as @it__iter when calling scx_bpf_dsq_move[_vtime]() from * within bpf_for_each() loops.
@@ -645,6 +650,10 @@ static inline bool is_migration_disabled(const struct task_struct *p) void bpf_rcu_read_lock(void) __ksym; void bpf_rcu_read_unlock(void) __ksym; +/* irq */ +void bpf_local_irq_save(unsigned long *flags) __ksym __weak; +void bpf_local_irq_restore(unsigned long *flags) __ksym __weak; +/* resilient qspinlock */ int bpf_res_spin_lock(struct bpf_res_spin_lock *lock) __ksym __weak; void bpf_res_spin_unlock(struct bpf_res_spin_lock *lock) __ksym __weak;
tools/sched_ext/include/scx/compat.bpf.h +8 -8
@@ -462,20 +462,20 @@ scx_bpf_dsq_insert(struct task_struct *p, u64 dsq_id, u64 slice, u64 enq_flags) bool scx_bpf_task_set_slice___new(struct task_struct *p, u64 slice) __ksym __weak; bool scx_bpf_task_set_dsq_vtime___new(struct task_struct *p, u64 vtime) __ksym __weak; -static inline void scx_bpf_task_set_slice (struct task_struct *p,u64 slice) +static inline bool scx_bpf_task_set_slice (struct task_struct *p,u64 slice) {if (bpf_ksym_exists (scx_bpf_task_set_slice___new)) -scx_bpf_task_set_slice___new (p, slice); -else - p->scx.slice = slice; +return scx_bpf_task_set_slice___new (p, slice); + p->scx.slice = slice; +return true; } -static inline void scx_bpf_task_set_dsq_vtime (struct task_struct *p,u64 vtime) +static inline bool scx_bpf_task_set_dsq_vtime (struct task_struct *p,u64 vtime) {if (bpf_ksym_exists (scx_bpf_task_set_dsq_vtime___new)) -scx_bpf_task_set_dsq_vtime___new (p, vtime); -else - p->scx.dsq_vtime = vtime; +return scx_bpf_task_set_dsq_vtime___new (p, vtime); + p->scx.dsq_vtime = vtime; +return true; }/* v7.4: Add scx_bpf_task_set_lazy_resched(). */
tools/sched_ext/scx_flatcg.bpf.c +0 -5
@@ -132,11 +132,6 @@ struct {/* gets inc'd on weight tree changes to expire the cached hweights */ u64 hweight_gen =1 ; -static u64 div_round_up (u64 dividend,u64 divisor) -{ -return (dividend + divisor -1 ) / divisor; -} -static bool cgv_node_less (struct bpf_rb_node *a,const struct bpf_rb_node *b) {struct cgv_node *cgc_a, *cgc_b;
Signature · 1 line
2.56.0
Tejun Heo
reviewerPATCH 4/4 sched_ext: Drop stale entries from the enum lookup tables
In reply to Tejun HeoThe enum lookup tables, enums.autogen.h and enums.autogen.bpf.h, are maintained by hand despite their names. Five entries name enums the kernel no longer has. Their lookups in the running kernel's BTF store 0, and nothing in tools/ uses them. Drop them. No behavior change.
--- tools/sched_ext/include/scx/enums.autogen.bpf.h | 15 --------------- tools/sched_ext/include/scx/enums.autogen.h | 5 ----- 2 files changed, 20 deletions(-)
tools/sched_ext/include/scx/enums.autogen.bpf.h +0 -15
@@ -22,24 +22,12 @@ const volatile u64 __SCX_RQ_ONLINE __weak;const volatile u64 __SCX_RQ_CAN_STOP_TICK__weak ;#define SCX_RQ_CAN_STOP_TICK __SCX_RQ_CAN_STOP_TICK -const volatile u64 __SCX_RQ_BAL_PENDING__weak ; -#define SCX_RQ_BAL_PENDING __SCX_RQ_BAL_PENDING - -const volatile u64 __SCX_RQ_BAL_KEEP__weak ; -#define SCX_RQ_BAL_KEEP __SCX_RQ_BAL_KEEP - -const volatile u64 __SCX_RQ_BYPASSING__weak ; -#define SCX_RQ_BYPASSING __SCX_RQ_BYPASSING -const volatile u64 __SCX_RQ_CLK_VALID__weak ;#define SCX_RQ_CLK_VALID __SCX_RQ_CLK_VALIDconst volatile u64 __SCX_RQ_IN_WAKEUP__weak ;#define SCX_RQ_IN_WAKEUP __SCX_RQ_IN_WAKEUP -const volatile u64 __SCX_RQ_IN_BALANCE__weak ; -#define SCX_RQ_IN_BALANCE __SCX_RQ_IN_BALANCE -const volatile u64 __SCX_RQ_IN_DISPATCH__weak ;#define SCX_RQ_IN_DISPATCH __SCX_RQ_IN_DISPATCH
@@ -103,9 +91,6 @@ const volatile u64 __SCX_TASK_READY __weak;const volatile u64 __SCX_TASK_ENABLED__weak ;#define SCX_TASK_ENABLED __SCX_TASK_ENABLED -const volatile u64 __SCX_TASK_NR_STATES__weak ; -#define SCX_TASK_NR_STATES __SCX_TASK_NR_STATES -const volatile u64 __SCX_TASK_DSQ_ON_PRIQ__weak ;#define SCX_TASK_DSQ_ON_PRIQ __SCX_TASK_DSQ_ON_PRIQ
tools/sched_ext/include/scx/enums.autogen.h +0 -5
@@ -12,12 +12,8 @@SCX_ENUM_SET (skel, scx_consts,SCX_RESCUE_DISABLE ); \SCX_ENUM_SET (skel, scx_rq_flags,SCX_RQ_ONLINE ); \SCX_ENUM_SET (skel, scx_rq_flags,SCX_RQ_CAN_STOP_TICK ); \ -SCX_ENUM_SET (skel, scx_rq_flags,SCX_RQ_BAL_PENDING ); \ -SCX_ENUM_SET (skel, scx_rq_flags,SCX_RQ_BAL_KEEP ); \ -SCX_ENUM_SET (skel, scx_rq_flags,SCX_RQ_BYPASSING ); \SCX_ENUM_SET (skel, scx_rq_flags,SCX_RQ_CLK_VALID ); \SCX_ENUM_SET (skel, scx_rq_flags,SCX_RQ_IN_WAKEUP ); \ -SCX_ENUM_SET (skel, scx_rq_flags,SCX_RQ_IN_BALANCE ); \SCX_ENUM_SET (skel, scx_rq_flags,SCX_RQ_IN_DISPATCH ); \SCX_ENUM_SET (skel, scx_dsq_id_flags,SCX_DSQ_FLAG_BUILTIN ); \SCX_ENUM_SET (skel, scx_dsq_id_flags,SCX_DSQ_FLAG_LOCAL_ON ); \
@@ -39,7 +35,6 @@SCX_ENUM_SET (skel, scx_ent_flags,SCX_TASK_INIT ); \SCX_ENUM_SET (skel, scx_ent_flags,SCX_TASK_READY ); \SCX_ENUM_SET (skel, scx_ent_flags,SCX_TASK_ENABLED ); \ -SCX_ENUM_SET (skel, scx_ent_flags,SCX_TASK_NR_STATES ); \SCX_ENUM_SET (skel, scx_ent_dsq_flags,SCX_TASK_DSQ_ON_PRIQ ); \SCX_ENUM_SET (skel, scx_kick_flags,SCX_KICK_IDLE ); \SCX_ENUM_SET (skel, scx_kick_flags,SCX_KICK_PREEMPT ); \
Signature · 1 line
2.56.0
Re: [PATCH 1/4] sched_ext: Don't sign-extend unsigned enums in __COMPAT_read_enum()
In reply to Tejun HeoHi Tejun,
Quoted 31 lines · Tejun Heo
__COMPAT_read_enum()looks an enumerator up in the running kernel's BTF. For a 4-byte enum, it assigns struct btf_enum.val, an__s32, to the u64 result, which sign-extends. BTF records the enum's signedness in the type's kflag. The code ignores it, so an unsigned enumerator with bit 31 set is sign-extended too.
b1c2a6b0d90d("sched_ext:scx_qmap: ReadSCX_RESCUE_DISABLEthrough the enum lookup tables") addedSCX_RESCUE_DISABLEto the enum lookup tables. It isU32_MAXin enumscx_constsand the only such enumerator in the tables. BPF code sees it as0xffffffffffffffffinstead of0xffffffff, so a comparison against a u32 holding the real value never matches.
scx_qmap, its only user, narrows the value to u32 in userspace and is unaffected.Sign-extend only when the kflag says the enum is signed and zero-extend otherwise. A signed enum keeps the sign extension, which is what C gives when the enumerator converts to u64.
SCX_TASK_CURSOR, 1 << 31 in a signed enum, still reads0xffffffff80000000. Verified with a probe against a built kernel's BTF.Signed-off-by Tejun Heo <[email protected]>--- tools/sched_ext/include/scx/compat.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)tools/sched_ext/include/scx/compat.h +0
-0
tools/sched_ext/include/scx/compat.h static inline bool __COMPAT_read_enum(const char *type, const char *name, u64 *v · line 150
Quoted 9 lines
tools/sched_ext/include/scx/compat.h +5
-1@@ -143,7 +143,11 @@ static inline bool __COMPAT_read_enum(const char *type, const char *name, u64 *vif (t->size ==8 && !strncmp (type,"scx_" ,4 ))return __COMPAT_recover_truncated_enum64 (type, name, (u32 )e[i].val, v); - *v = e[i].val; +/* kflag marks a signed enum */ +if (btf_kflag (t)) + *v = (s64 )e[i].val; +else + *v = (u32 )e[i].val;
nit: BTF generated by pahole < 1.24 never sets the enum kflag, so on those kernels signed enums are zero-extended as well. It's harmless today (SCX_TASK_CURSOR is the only affected entry and it's only used as a u32 flag), but maybe it's worth mentioning it in the comment? Something like:
/* * kflag marks a signed enum. BTF from pahole < 1.24 never sets it, * so signed enums are zero-extended there too. */
Thanks,
-Andrea
Quoted 6 lines
tools/sched_ext/include/scx/compat.h +0
-0return true; } }Signature · 1 line
2.56.0
Hi Tejun,
Quoted 13 lines · Tejun Heo
Hello,
This patchset fixes
__COMPAT_read_enum()sign-extending unsigned 4-byte enums, which madeSCX_RESCUE_DISABLEread as0xffffffffffffffff, syncs the shared headers intools/sched_ext/includewith the scx repo, and drops the enum lookup table entries for enumerators the kernel no longer has. The scx side is https://github.com/sched-ext/scx/pull/3930, merged, and
https://github.com/sched-ext/scx/pull/3932.With both applied, the shared headers in
tools/sched_ext/includeare identical to scx's except the two generated tables,enum_defs.autogen.handenums_abi.autogen.h, which each side regenerates from its ownvmlinux.h.
Quoted 8 lines
Based on sched_ext/for-7.5 (
dd3b1d9be27c).This patchset contains the following 4 patches.
0001
sched_ext: Don't sign-extend unsigned enums in__COMPAT_read_enum()0002sched_ext: Sync tools autogen enum lookup tables from the scx repo 0003sched_ext: Sync common and compat headers from the scx repo 0004sched_ext: Drop stale entries from the enum lookup tables
Left a comment (nit) on 1/4. For 2-4:
Thanks,
-Andrea
Tejun Heo
reviewerPATCH v2 1/4 sched_ext: Don't sign-extend unsigned enums in __COMPAT_read_enum()
In reply to Tejun Heo__COMPAT_read_enum() looks an enumerator up in the running kernel's BTF. For a 4-byte enum, it assigns struct btf_enum.val, an __s32, to the u64 result, which sign-extends. BTF records the enum's signedness in the type's kflag. The code ignores it, so an unsigned enumerator with bit 31 set is sign-extended too.
b1c2a6b0d90d ("sched_ext: scx_qmap: Read SCX_RESCUE_DISABLE through the enum lookup tables") added SCX_RESCUE_DISABLE to the enum lookup tables. It is U32_MAX in enum scx_consts and the only such enumerator in the tables. BPF code sees it as 0xffffffffffffffff instead of 0xffffffff, so a comparison against a u32 holding the real value never matches.
scx_qmap, its only user, narrows the value to u32 in userspace and is unaffected.
Sign-extend only when the kflag says the enum is signed and zero-extend otherwise. A signed enum keeps the sign extension, which is what C gives when the enumerator converts to u64. SCX_TASK_CURSOR, 1 << 31 in a signed enum, still reads 0xffffffff80000000. Verified with a probe against a built kernel's BTF.
v2: Note in the comment that pahole < 1.24 never sets the kflag (Andrea).
---
Applied to sched_ext/for-7.5.
tools/sched_ext/include/scx/compat.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
tools/sched_ext/include/scx/compat.h +9 -1
@@ -143,7 +143,15 @@ static inline bool __COMPAT_read_enum(const char *type, const char *name, u64 *vif (t->size ==8 && !strncmp (type,"scx_" ,4 ))return __COMPAT_recover_truncated_enum64 (type, name, (u32 )e[i].val, v); - *v = e[i].val; +/* +* kflag marks a signed enum. BTF generated by +* pahole < 1.24 never sets it, so signed enums +* are zero-extended there too. +*/ +if (btf_kflag (t)) + *v = (s64 )e[i].val; +else + *v = (u32 )e[i].val;return true; } }
Signature · 1 line
2.56.0
Quoted 4 lines
0001
sched_ext: Don't sign-extend unsigned enums in__COMPAT_read_enum()0002sched_ext: Sync tools autogen enum lookup tables from the scx repo 0003sched_ext: Sync common and compat headers from the scx repo 0004sched_ext: Drop stale entries from the enum lookup tables
Applied 1-4 to sched_ext/for-7.5 with the following changes:
- 1 is the v2 posted as a reply to the original.
- Added to all:
Thanks.
--
tejun