The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] tools/sched_ext: use btf_vlen() helper in compat.h
@ 2026-06-30  3:12 luoliang
  2026-06-30  5:13 ` Andrea Righi
  2026-06-30  8:17 ` [PATCH v2] " luoliang
  0 siblings, 2 replies; 6+ messages in thread
From: luoliang @ 2026-06-30  3:12 UTC (permalink / raw)
  To: Tejun Heo
  Cc: David Vernet, Andrea Righi, Changwoo Min, sched-ext, linux-kernel,
	bpf, Liang Luo

From: luoliang <luoliang@kylinos.cn>

__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.

Signed-off-by: Liang Luo <luoliang@kylinos.cn>
---
 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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] tools/sched_ext: use btf_vlen() helper in compat.h
  2026-06-30  3:12 [PATCH] tools/sched_ext: use btf_vlen() helper in compat.h luoliang
@ 2026-06-30  5:13 ` Andrea Righi
  2026-06-30  8:15   ` luoliang
  2026-06-30  8:17 ` [PATCH v2] " luoliang
  1 sibling, 1 reply; 6+ messages in thread
From: Andrea Righi @ 2026-06-30  5:13 UTC (permalink / raw)
  To: luoliang
  Cc: Tejun Heo, David Vernet, Changwoo Min, sched-ext, linux-kernel,
	bpf

Hi,

On Tue, Jun 30, 2026 at 11:12:46AM +0800, luoliang@kylinos.cn wrote:
> From: luoliang <luoliang@kylinos.cn>
> 
> __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@nvidia.com>

Thanks,
-Andrea

> 
> Signed-off-by: Liang Luo <luoliang@kylinos.cn>
> ---
>  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
> 

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] tools/sched_ext: use btf_vlen() helper in compat.h
  2026-06-30  5:13 ` Andrea Righi
@ 2026-06-30  8:15   ` luoliang
  0 siblings, 0 replies; 6+ messages in thread
From: luoliang @ 2026-06-30  8:15 UTC (permalink / raw)
  To: Andrea Righi
  Cc: Tejun Heo, David Vernet, Changwoo Min, sched-ext, linux-kernel,
	bpf, Liang Luo

From: Liang Luo <luoliang@kylinos.cn>

On Tue, Jun 30, 2026 at 07:13:51AM +0200, Andrea Righi wrote:
> btf_vlen() currently returns __u32, not __u16. This changed in commit
> cacd6729c0923 ("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@nvidia.com>

Thanks Andrea, and thanks to Sashiko for catching this first. You're
both right, and I apologize for the noise on the list.

I made a basic mistake: I developed and verified v1 against a v7.1
baseline, where btf_vlen() still returns __u16 and the warning does
go away there. But I did not re-check against current mainline,
which is what the patch actually targets. On mainline, cacd6729c0923
already widened btf_vlen() to __u32 (the BTF vlen field itself was
expanded from 16 to 24 bits), so on mainline v1 does not silence the
warning, and the commit message's core claim is false there. Sashiko
flagged the return-type discrepancy, and Andrea identified both the
root cause and the fix (declare the loop counters as __u32). I
should have caught this myself before sending.

v2 incoming, rebased on v7.2-rc1, with the description corrected and
the two loop counters declared as __u32 so the comparison is a plain
'__u32 < __u32'. Verified on v7.2-rc1 that all -Wsign-compare
instances from compat.h are gone across the 8 userspace schedulers,
with no new warnings introduced.

Sorry again for the extra review cycles, and thanks for the careful
reviews.

Thanks,
Liang

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2] tools/sched_ext: use btf_vlen() helper in compat.h
  2026-06-30  3:12 [PATCH] tools/sched_ext: use btf_vlen() helper in compat.h luoliang
  2026-06-30  5:13 ` Andrea Righi
@ 2026-06-30  8:17 ` luoliang
  2026-06-30  8:27   ` Andrea Righi
  2026-06-30 14:24   ` Tejun Heo
  1 sibling, 2 replies; 6+ messages in thread
From: luoliang @ 2026-06-30  8:17 UTC (permalink / raw)
  To: Tejun Heo
  Cc: David Vernet, Andrea Righi, Changwoo Min, sched-ext, linux-kernel,
	bpf, Liang Luo

From: luoliang <luoliang@kylinos.cn>

__COMPAT_read_enum() and __COMPAT_struct_has_field() open-code the
vlen lookup via the raw BTF_INFO_VLEN(t->info) UAPI macro. libbpf
exposes btf_vlen() for exactly this purpose; use it in the three
call sites, matching the pattern in kernel/bpf/inode.c and
tools/bpf/bpftool.

btf_vlen() returns __u32 (since commit cacd6729c0923, "libbpf:
Adjust btf_vlen() to return a __u32", which expanded the BTF vlen
field from 16 to 24 bits). Declare the loop counters as __u32 to
match the return type, keeping the comparison as a plain
'__u32 < __u32' and silencing the -Wsign-compare warnings.

No functional change.

Suggested-by: Andrea Righi <arighi@nvidia.com>
Signed-off-by: Liang Luo <luoliang@kylinos.cn>

---

Changes in v2:
- Correct the commit message: btf_vlen() returns __u32 (not __u16)
  on mainline since cacd6729c0923. The v1 description was accurate
  only on a v7.1 baseline. (Thanks to Sashiko AI review and Andrea
  Righi for catching this.)
- Declare the loop counters as __u32 to match btf_vlen()'s return
  type, as suggested by Andrea Righi. This properly silences
  -Wsign-compare on mainline.
---
 tools/sched_ext/include/scx/compat.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/tools/sched_ext/include/scx/compat.h b/tools/sched_ext/include/scx/compat.h
index 602f07061ee3..23d9ef3e4c9d 100644
--- a/tools/sched_ext/include/scx/compat.h
+++ b/tools/sched_ext/include/scx/compat.h
@@ -28,7 +28,7 @@ static inline bool __COMPAT_read_enum(const char *type, const char *name, u64 *v
 	const struct btf_type *t;
 	const char *n;
 	s32 tid;
-	int i;
+	__u32 i;
 
 	__COMPAT_load_vmlinux_btf();
 
@@ -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)) {
@@ -85,7 +85,7 @@ static inline bool __COMPAT_struct_has_field(const char *type, const char *field
 	const struct btf_member *m;
 	const char *n;
 	s32 tid;
-	int i;
+	__u32 i;
 
 	__COMPAT_load_vmlinux_btf();
 	tid = btf__find_by_name_kind(__COMPAT_vmlinux_btf, type, BTF_KIND_STRUCT);
@@ -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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] tools/sched_ext: use btf_vlen() helper in compat.h
  2026-06-30  8:17 ` [PATCH v2] " luoliang
@ 2026-06-30  8:27   ` Andrea Righi
  2026-06-30 14:24   ` Tejun Heo
  1 sibling, 0 replies; 6+ messages in thread
From: Andrea Righi @ 2026-06-30  8:27 UTC (permalink / raw)
  To: luoliang
  Cc: Tejun Heo, David Vernet, Changwoo Min, sched-ext, linux-kernel,
	bpf

On Tue, Jun 30, 2026 at 04:17:20PM +0800, luoliang@kylinos.cn wrote:
> From: luoliang <luoliang@kylinos.cn>
> 
> __COMPAT_read_enum() and __COMPAT_struct_has_field() open-code the
> vlen lookup via the raw BTF_INFO_VLEN(t->info) UAPI macro. libbpf
> exposes btf_vlen() for exactly this purpose; use it in the three
> call sites, matching the pattern in kernel/bpf/inode.c and
> tools/bpf/bpftool.
> 
> btf_vlen() returns __u32 (since commit cacd6729c0923, "libbpf:
> Adjust btf_vlen() to return a __u32", which expanded the BTF vlen
> field from 16 to 24 bits). Declare the loop counters as __u32 to
> match the return type, keeping the comparison as a plain
> '__u32 < __u32' and silencing the -Wsign-compare warnings.
> 
> No functional change.
> 
> Suggested-by: Andrea Righi <arighi@nvidia.com>
> Signed-off-by: Liang Luo <luoliang@kylinos.cn>

Looks good.

Reviewed-by: Andrea Righi <arighi@nvidia.com>

Thanks!
-Andrea

> 
> ---
> 
> Changes in v2:
> - Correct the commit message: btf_vlen() returns __u32 (not __u16)
>   on mainline since cacd6729c0923. The v1 description was accurate
>   only on a v7.1 baseline. (Thanks to Sashiko AI review and Andrea
>   Righi for catching this.)
> - Declare the loop counters as __u32 to match btf_vlen()'s return
>   type, as suggested by Andrea Righi. This properly silences
>   -Wsign-compare on mainline.
> ---
>  tools/sched_ext/include/scx/compat.h | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)
> 
> diff --git a/tools/sched_ext/include/scx/compat.h b/tools/sched_ext/include/scx/compat.h
> index 602f07061ee3..23d9ef3e4c9d 100644
> --- a/tools/sched_ext/include/scx/compat.h
> +++ b/tools/sched_ext/include/scx/compat.h
> @@ -28,7 +28,7 @@ static inline bool __COMPAT_read_enum(const char *type, const char *name, u64 *v
>  	const struct btf_type *t;
>  	const char *n;
>  	s32 tid;
> -	int i;
> +	__u32 i;
>  
>  	__COMPAT_load_vmlinux_btf();
>  
> @@ -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)) {
> @@ -85,7 +85,7 @@ static inline bool __COMPAT_struct_has_field(const char *type, const char *field
>  	const struct btf_member *m;
>  	const char *n;
>  	s32 tid;
> -	int i;
> +	__u32 i;
>  
>  	__COMPAT_load_vmlinux_btf();
>  	tid = btf__find_by_name_kind(__COMPAT_vmlinux_btf, type, BTF_KIND_STRUCT);
> @@ -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
> 

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] tools/sched_ext: use btf_vlen() helper in compat.h
  2026-06-30  8:17 ` [PATCH v2] " luoliang
  2026-06-30  8:27   ` Andrea Righi
@ 2026-06-30 14:24   ` Tejun Heo
  1 sibling, 0 replies; 6+ messages in thread
From: Tejun Heo @ 2026-06-30 14:24 UTC (permalink / raw)
  To: Liang Luo, David Vernet, Andrea Righi, Changwoo Min, sched-ext
  Cc: linux-kernel, bpf

Applied to sched_ext/for-7.3.

Thanks.
--
tejun

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-06-30 14:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-30  3:12 [PATCH] tools/sched_ext: use btf_vlen() helper in compat.h luoliang
2026-06-30  5:13 ` Andrea Righi
2026-06-30  8:15   ` luoliang
2026-06-30  8:17 ` [PATCH v2] " luoliang
2026-06-30  8:27   ` Andrea Righi
2026-06-30 14:24   ` Tejun Heo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox