Re: [PATCH] tools/sched_ext: use btf_vlen() helper in compat.h
From: Andrea Righi
Date: Tue Jun 30 2026 - 01:14:19 EST
Hi,
On Tue, Jun 30, 2026 at 11:12:46AM +0800, luoliang@xxxxxxxxxx wrote:
> From: luoliang <luoliang@xxxxxxxxxx>
>
> __COMPAT_read_enum() and __COMPAT_struct_has_field() open-code the
> vlen lookup via the raw BTF_INFO_VLEN(t->info) UAPI macro, whose
> result type is __u32 (t->info is __u32 and 0xffff is promoted to
> unsigned). Comparing it against the int loop counter triggers
> -Wsign-compare in every example scheduler that includes compat.h.
>
> libbpf already exposes btf_vlen() for exactly this purpose; it
> returns __u16, the natural width of the vlen field, and the usual
> integer promotions turn the 'int < __u16' comparison into a plain
> 'int < int' so the warning goes away without any cast. This matches
> the pattern already used in-kernel (e.g. kernel/bpf/inode.c) and in
> tools/bpf/bpftool. Replace the three open-coded lookups with
> btf_vlen(t). No functional change.
btf_vlen() currently returns __u32, not __u16. This changed in commit
cacd6729c092 ("libbpf: Adjust btf_vlen() to return a __u32"), because BTF vlen
was expanded to 24 bits. We should update the description to match this. And i
should be changed to __u32 for consistency.
With these changes:
Reviewed-by: Andrea Righi <arighi@xxxxxxxxxx>
Thanks,
-Andrea
>
> Signed-off-by: Liang Luo <luoliang@xxxxxxxxxx>
> ---
> tools/sched_ext/include/scx/compat.h | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/tools/sched_ext/include/scx/compat.h b/tools/sched_ext/include/scx/compat.h
> index 039854c490d5..df5ca1ce20f6 100644
> --- a/tools/sched_ext/include/scx/compat.h
> +++ b/tools/sched_ext/include/scx/compat.h
> @@ -42,7 +42,7 @@ static inline bool __COMPAT_read_enum(const char *type, const char *name, u64 *v
> if (btf_is_enum(t)) {
> struct btf_enum *e = btf_enum(t);
>
> - for (i = 0; i < BTF_INFO_VLEN(t->info); i++) {
> + for (i = 0; i < btf_vlen(t); i++) {
> n = btf__name_by_offset(__COMPAT_vmlinux_btf, e[i].name_off);
> SCX_BUG_ON(!n, "btf__name_by_offset()");
> if (!strcmp(n, name)) {
> @@ -53,7 +53,7 @@ static inline bool __COMPAT_read_enum(const char *type, const char *name, u64 *v
> } else if (btf_is_enum64(t)) {
> struct btf_enum64 *e = btf_enum64(t);
>
> - for (i = 0; i < BTF_INFO_VLEN(t->info); i++) {
> + for (i = 0; i < btf_vlen(t); i++) {
> n = btf__name_by_offset(__COMPAT_vmlinux_btf, e[i].name_off);
> SCX_BUG_ON(!n, "btf__name_by_offset()");
> if (!strcmp(n, name)) {
> @@ -97,7 +97,7 @@ static inline bool __COMPAT_struct_has_field(const char *type, const char *field
>
> m = btf_members(t);
>
> - for (i = 0; i < BTF_INFO_VLEN(t->info); i++) {
> + for (i = 0; i < btf_vlen(t); i++) {
> n = btf__name_by_offset(__COMPAT_vmlinux_btf, m[i].name_off);
> SCX_BUG_ON(!n, "btf__name_by_offset()");
> if (!strcmp(n, field))
> --
> 2.43.0
>