selftests/bpf: Fix "expression result unused" warnings with icecc
Subsystems (4)
v2 cover selftests/bpf: Fix "expression result unused" warnings with icecc
v1: https://lore.kernel.org/bpf/[email protected]/
v1 -> v2: Annotate bpf_obj_new_impl() with __must_check (Alexei).
Add an explanation about icecc.
Hi,
I took another look at the "expression result unused" warnings I've been seeing, and it turned out that the root cause was the icecc compiler wrapper and what I consider a clang bug. Back then I've reported that the problem was reproducible with plain clang, but now I see that it was clearly a mixup, sorry about that.
In this series I implement Alexei's suggestion to annotate bpf_obj_new_impl() with __must_check and add (void) casts to the respective testcase.
There remain two awkward (void) casts and I'm not sure if I can somehow make them look nicer. But I've added a detailed explanation how they are helpful to the commit message.
Best regards,
Ilya
Ilya Leoshkevich (2):
selftests/bpf: Annotate bpf_obj_new_impl() with __must_check selftests/bpf: Fix "expression result unused" warnings with icecc
tools/lib/bpf/bpf_helpers.h | 4 ++++ tools/testing/selftests/bpf/bpf_experimental.h | 2 +- tools/testing/selftests/bpf/progs/bpf_arena_spin_lock.h | 4 ++-- tools/testing/selftests/bpf/progs/linked_list_fail.c | 8 ++++---- 4 files changed, 11 insertions(+), 7 deletions(-)
Signature · 1 line
2.50.1
PATCH v2 1/2 selftests/bpf: Annotate bpf_obj_new_impl() with __must_check
The verifier requires that pointers returned by bpf_obj_new_impl() are either dropped or stored in a map. Therefore programs that do not use its return values will fail to load. Make the compiler point out these issues. Adjust selftests that check that the verifier does indeed spot these bugs.
--- tools/lib/bpf/bpf_helpers.h | 4 ++++ tools/testing/selftests/bpf/bpf_experimental.h | 2 +- tools/testing/selftests/bpf/progs/linked_list_fail.c | 8 ++++---- 3 files changed, 9 insertions(+), 5 deletions(-)
tools/lib/bpf/bpf_helpers.h +4 -0
@@ -69,6 +69,10 @@*/ #define __hidden __attribute__((visibility("hidden"))) +#ifndef __must_check +#define __must_check __attribute__((__warn_unused_result__)) +#endif +/* When utilizing vmlinux.h with BPF CO-RE, user BPF programs can't include * any system-level headers (such as stddef.h, linux/version.h, etc), and * commonly-used macros like NULL and KERNEL_VERSION aren't available through
tools/testing/selftests/bpf/bpf_experimental.h +1 -1
@@ -20,7 +20,7 @@* A pointer to an object of the type corresponding to the passed in * 'local_type_id', or NULL on failure. */ -extern void *bpf_obj_new_impl (__u64 local_type_id,void *meta) __ksym; +extern __must_check void *bpf_obj_new_impl (__u64 local_type_id,void *meta) __ksym;/* Convenience macro to wrap over bpf_obj_new_impl */ #define bpf_obj_new (type) ((type *)bpf_obj_new_impl(bpf_core_type_id_local(type), NULL))
tools/testing/selftests/bpf/progs/linked_list_fail.c +4 -4
@@ -212,14 +212,14 @@ int map_compat_raw_tp_w(void *ctx) SEC("?tc") int obj_type_id_oor(void *ctx) { - bpf_obj_new_impl(~0UL, NULL); + (void)bpf_obj_new_impl(~0UL, NULL); return 0; } SEC("?tc")int obj_new_no_composite (void *ctx) { -bpf_obj_new_impl (bpf_core_type_id_local (int), (void *)42 ); + (void )bpf_obj_new_impl (bpf_core_type_id_local (int), (void *)42 );return 0 ; }
@@ -227,7 +227,7 @@ SEC("?tc")int obj_new_no_struct (void *ctx) { -bpf_obj_new (union {int data;unsigned udata; }); + (void)bpf_obj_new(union { int data; unsigned udata; });return 0 ; }
@@ -252,7 +252,7 @@ int new_null_ret(void *ctx) SEC("?tc")int obj_new_acq (void *ctx) { -bpf_obj_new (struct foo ); + (void)bpf_obj_new(struct foo);return 0 ; }
Signature · 1 line
2.50.1
Re: [PATCH bpf-next v2 1/2] selftests/bpf: Annotate bpf_obj_new_impl() with __must_check
Quoted 21 lines · Ilya Leoshkevich
The verifier requires that pointers returned by
bpf_obj_new_impl()are either dropped or stored in a map. Therefore programs that do not use its return values will fail to load. Make the compiler point out these issues. Adjust selftests that check that the verifier does indeed spot these bugs.Link https://lore.kernel.org/bpf/CAADnVQL6Q+QRv3_JwEd26biwGpFYcwD_=BjBJWLAtpgOP9CKRw@mail.gmail.com/ open hereSuggested-by Alexei Starovoitov <[email protected]>Signed-off-by Ilya Leoshkevich <[email protected]>--- tools/lib/bpf/bpf_helpers.h | 4 ++++ tools/testing/selftests/bpf/bpf_experimental.h | 2 +- tools/testing/selftests/bpf/progs/linked_list_fail.c | 8 ++++---- 3 files changed, 9 insertions(+), 5 deletions(-)tools/lib/bpf/bpf_helpers.h +0
-0
tools/lib/bpf/bpf_helpers.h line 75
Quoted 7 lines
tools/lib/bpf/bpf_helpers.h +4
-0@@ -69,6 +69,10 @@*/ #define __hidden __attribute__((visibility("hidden"))) +#ifndef __must_check +#define __must_check __attribute__((__warn_unused_result__)) +#endif +
do we need to add this to libbpf UAPI? let's put it in selftests header somewhere instead?
tools/testing/selftests/bpf/bpf_experimental.h line 23
Quoted 13 lines
tools/lib/bpf/bpf_helpers.h +0
-0/* When utilizing vmlinux.h with BPF CO-RE, user BPF programs can't include * any system-level headers (such as stddef.h, linux/version.h, etc), and * commonly-used macros like NULL and KERNEL_VERSION aren't available through tools/testing/selftests/bpf/bpf_experimental.h +1
-1@@ -20,7 +20,7 @@* A pointer to an object of the type corresponding to the passed in * 'local_type_id', or NULL on failure. */ -extern void *bpf_obj_new_impl (__u64 local_type_id,void *meta) __ksym; +extern __must_check void *bpf_obj_new_impl (__u64 local_type_id,void *meta) __ksym;
bpf_obj_new_impl will generally come from vmlinux.h nowadays, and that one won't have __must_check annotation, is that a problem?
Quoted 45 lines
tools/testing/selftests/bpf/bpf_experimental.h +0
-0/* Convenience macro to wrap over bpf_obj_new_impl */ #define bpf_obj_new (type) ((type *)bpf_obj_new_impl(bpf_core_type_id_local(type), NULL))tools/testing/selftests/bpf/progs/linked_list_fail.c +4
-5@@ -212,14 +212,14 @@ int map_compat_raw_tp_w(void *ctx) SEC("?tc") int obj_type_id_oor(void *ctx) { - bpf_obj_new_impl(~0UL, NULL); + (void)bpf_obj_new_impl(~0UL, NULL); return 0; } SEC("?tc")int obj_new_no_composite (void *ctx) { -bpf_obj_new_impl (bpf_core_type_id_local (int), (void *)42 ); + (void )bpf_obj_new_impl (bpf_core_type_id_local (int), (void *)42 );return 0 ; }@@ -227,7 +227,7 @@ SEC("?tc")int obj_new_no_struct (void *ctx) { -bpf_obj_new (union {int data;unsigned udata; }); + (void)bpf_obj_new(union { int data; unsigned udata; });return 0 ; }@@ -252,7 +252,7 @@ int new_null_ret(void *ctx) SEC("?tc")int obj_new_acq (void *ctx) { -bpf_obj_new (struct foo ); + (void)bpf_obj_new(struct foo);return 0 ; } --2.50.1
Re: [PATCH bpf-next v2 1/2] selftests/bpf: Annotate bpf_obj_new_impl() with __must_check
Quoted 13 lines · Andrii Nakryiko
Quoted 11 lines · Ilya Leoshkevich
The verifier requires that pointers returned by
bpf_obj_new_impl()are
either dropped or stored in a map. Therefore programs that do not use
its return values will fail to load. Make the compiler point out these
issues. Adjust selftests that check that the verifier does indeed spot
these bugs.
Quoted 9 lines
Quoted 9 lines
Link:
https://lore.kernel.org/bpf/CAADnVQL6Q+QRv3_JwEd26biwGpFYcwD_=BjBJWLAtpgOP9CKRw@mail.gmail.com/Suggested-by Alexei Starovoitov <[email protected]>Signed-off-by Ilya Leoshkevich <[email protected]>---
tools/lib/bpf/bpf_helpers.h | 4 ++++ tools/testing/selftests/bpf/bpf_experimental.h | 2 +-tools/testing/selftests/bpf/progs/linked_list_fail.c| 8 ++++----3 files changed, 9 insertions(+), 5 deletions(-)
The CI found an issue with bpf-gcc in the meantime, I will fix this in v3.
Quoted 9 lines
Quoted 9 lines
tools/lib/bpf/bpf_helpers.h +0
-0@@ -69,6 +69,10 @@*/ #define __hidden __attribute__((visibility("hidden")))
Quoted 7 lines
tools/lib/bpf/bpf_helpers.h line 75
Quoted 4 lines
tools/lib/bpf/bpf_helpers.h +4
-0+#ifndef __must_check +#define __must_check __attribute__((__warn_unused_result__)) +#endif +do we need to add this to libbpf UAPI? let's put it in selftests header somewhere instead?
Will do.
Quoted 17 lines
Quoted 17 lines
tools/lib/bpf/bpf_helpers.h +0
-0/* When utilizing vmlinux.h with BPF CO-RE, user BPF programscan't include
* any system-level headers (such asstddef.h,linux/version.h, etc), and
* commonly-used macros like NULL andKERNEL_VERSIONaren't available throughtools/testing/selftests/bpf/bpf_experimental.h +0
-0@@ -20,7 +20,7 @@ * A pointer to an object of the type corresponding to thepassed in
* 'local_type_id', or NULL on failure.
*/
Quoted 8 lines
Quoted 4 lines
-extern void *bpf_obj_new_impl(__u64
local_type_id, void *meta)__ksym;
+extern__must_checkvoid *bpf_obj_new_impl(__u64local_type_id, void *meta)__ksym;
bpf_obj_new_implwill generally come fromvmlinux.hnowadays, and that
one won't have__must_checkannotation, is that a problem?
It should be fine according to [1]:
Compatible attribute specifications on distinct declarations of the same function are merged.
I will add this to the commit message in v3.
[1]
https://gcc.gnu.org/onlinedocs/gcc-12.4.0/gcc/Function-Attributes.html
[...]
Re: [PATCH bpf-next v2 1/2] selftests/bpf: Annotate bpf_obj_new_impl() with __must_check
Quoted 73 lines · Ilya Leoshkevich
Quoted 13 lines · Andrii Nakryiko
Quoted 11 lines · Ilya Leoshkevich
The verifier requires that pointers returned by
bpf_obj_new_impl()are
either dropped or stored in a map. Therefore programs that do not use
its return values will fail to load. Make the compiler point out these
issues. Adjust selftests that check that the verifier does indeed spot
these bugs.Quoted 9 lines
Quoted 9 lines
Link:
https://lore.kernel.org/bpf/CAADnVQL6Q+QRv3_JwEd26biwGpFYcwD_=BjBJWLAtpgOP9CKRw@mail.gmail.com/Suggested-by Alexei Starovoitov <[email protected]>Signed-off-by Ilya Leoshkevich <[email protected]>--- tools/lib/bpf/bpf_helpers.h | 4 ++++ tools/testing/selftests/bpf/bpf_experimental.h | 2 +- tools/testing/selftests/bpf/progs/linked_list_fail.c | 8 ++++---- 3 files changed, 9 insertions(+), 5 deletions(-)The CI found an issue with bpf-gcc in the meantime, I will fix this in v3.
Quoted 9 lines
Quoted 9 lines
tools/lib/bpf/bpf_helpers.h +0
-0@@ -69,6 +69,10 @@*/ #define __hidden __attribute__((visibility("hidden")))Quoted 7 lines
tools/lib/bpf/bpf_helpers.h line 75
Quoted 4 lines
tools/lib/bpf/bpf_helpers.h +4
-0+#ifndef __must_check +#define __must_check __attribute__((__warn_unused_result__)) +#endif +do we need to add this to libbpf UAPI? let's put it in selftests header somewhere instead?
Will do.
Quoted 17 lines
Quoted 17 lines
tools/lib/bpf/bpf_helpers.h +0
-0/* When utilizing vmlinux.h with BPF CO-RE, user BPF programs can't include
* any system-level headers (such asstddef.h,linux/version.h,
etc), and
* commonly-used macros like NULL andKERNEL_VERSIONaren't
available throughtools/testing/selftests/bpf/bpf_experimental.h +0
-0@@ -20,7 +20,7 @@* A pointer to an object of the type corresponding to the passed in
* 'local_type_id', or NULL on failure.
*/Quoted 8 lines
Quoted 4 lines
-extern void *bpf_obj_new_impl(__u64
local_type_id, void *meta)__ksym;
+extern__must_checkvoid *bpf_obj_new_impl(__u64local_type_id, void *meta)__ksym;
bpf_obj_new_implwill generally come fromvmlinux.hnowadays, and that
one won't have__must_checkannotation, is that a problem?
Quoted 6 lines
It should be fine according to [1]:
Compatible attribute specifications on distinct declarations of the same function are merged.
I will add this to the commit message in v3.
Sure, for BPF selftests it will work. My question was broader, for anyone using bpf_obj_new in the wild, they won't have __must_check annotation from vmlinux.h (and I doubt they will manually add it like we do here for BPF selftests), so if that's important, I guess we need to think how to wire that up so that it happens automatically through vmlinux.h.
"It's not that important to bother" is a fine answer as well :)
Quoted 5 lines
[1]
https://gcc.gnu.org/onlinedocs/gcc-12.4.0/gcc/Function-Attributes.html[...]
Re: [PATCH bpf-next v2 1/2] selftests/bpf: Annotate bpf_obj_new_impl() with __must_check
Quoted 32 lines · Andrii Nakryiko
Quoted 30 lines · Ilya Leoshkevich
Quoted 28 lines · Andrii Nakryiko
Quoted 25 lines · Ilya Leoshkevich
The verifier requires that pointers returned by
bpf_obj_new_impl()
are
either dropped or stored in a map. Therefore programs that do not
use
its return values will fail to load. Make the compiler point out
these
issues. Adjust selftests that check that the verifier does indeed
spot
these bugs.Link:
https://lore.kernel.org/bpf/CAADnVQL6Q+QRv3_JwEd26biwGpFYcwD_=BjBJWLAtpgOP9CKRw@mail.gmail.com/Suggested-by Alexei Starovoitov <[email protected]>Signed-off-by Ilya Leoshkevich <[email protected]>---
tools/lib/bpf/bpf_helpers.h | 4 ++++ tools/testing/selftests/bpf/bpf_experimental.h | 2 +-tools/testing/selftests/bpf/progs/linked_list_fail.c| 8 ++++-
---3 files changed, 9 insertions(+), 5 deletions(-)
[...]
Quoted 42 lines
Quoted 27 lines
Quoted 18 lines
Quoted 18 lines
/* When utilizing
vmlinux.hwith BPF CO-RE, user BPF programs can't include
* any system-level headers (such asstddef.h,linux/version.h,
etc), and
* commonly-used macros like NULL andKERNEL_VERSIONaren't available throughtools/testing/selftests/bpf/bpf_experimental.h +0
-0@@ -20,7 +20,7 @@ * A pointer to an object of the type corresponding to thepassed in
* 'local_type_id', or NULL on failure.
*/
-extern void *bpf_obj_new_impl(__u64local_type_id, void *meta)Quoted 8 lines
Quoted 4 lines
__ksym;
+extern__must_checkvoid *bpf_obj_new_impl(__u64local_type_id,
void *meta)__ksym;
bpf_obj_new_implwill generally come fromvmlinux.hnowadays, and that
one won't have__must_checkannotation, is that a problem?Quoted 6 lines
It should be fine according to [1]:
Compatible attribute specifications on distinct declarations of the same function are merged.
I will add this to the commit message in v3.
Sure, for BPF selftests it will work. My question was broader, for anyone using
bpf_obj_newin the wild, they won't have__must_checkannotation fromvmlinux.h(and I doubt they will manually add it like we do here for BPF selftests), so if that's important, I guess we need
to think how to wire that up so that it happens automatically throughvmlinux.h.
Quoted 1 line
"It's not that important to bother" is a fine answer as well :)
I see. Seems like it's tough:
- The attribute is not available in DWARF
- But we could introduce KF_MUST_CHECK flag
- Which pahole would extract from .BTF_ids and convert to
a btf_decl_tag
- This will make pahole depend on .BTF_ids format though, which might be undesirable
- Then bpftool would convert this btf_decl_tag to __must_check
Seems like they are attempting to upstream the new DW_TAG_GNU_annotation right now [1], if that lands and is available for non-BPF targets, we could put __attribute((btf_decl_tag("must_check"))) on kernel's bpf_obj_new_impl() and directly access it from bpftool.
So for now I would propose to limit the solution to selftests.
[1] https://gcc.gnu.org/pipermail/gcc-patches/2025-August/692445.html
Quoted 4 lines
Quoted 4 lines
[1]
https://gcc.gnu.org/onlinedocs/gcc-12.4.0/gcc/Function-Attributes.html[...]
Re: [PATCH bpf-next v2 1/2] selftests/bpf: Annotate bpf_obj_new_impl() with __must_check
Quoted 92 lines · Ilya Leoshkevich
Quoted 35 lines · Andrii Nakryiko
Quoted 32 lines · Ilya Leoshkevich
Quoted 30 lines · Andrii Nakryiko
Quoted 27 lines · Ilya Leoshkevich
The verifier requires that pointers returned by
bpf_obj_new_impl()
are
either dropped or stored in a map. Therefore programs that do not
use
its return values will fail to load. Make the compiler point out
these
issues. Adjust selftests that check that the verifier does indeed
spot
these bugs.Link:
https://lore.kernel.org/bpf/CAADnVQL6Q+QRv3_JwEd26biwGpFYcwD_=BjBJWLAtpgOP9CKRw@mail.gmail.com/Suggested-by Alexei Starovoitov <[email protected]>Signed-off-by Ilya Leoshkevich <[email protected]>---
tools/lib/bpf/bpf_helpers.h | 4
++++
tools/testing/selftests/bpf/bpf_experimental.h | 2 +-tools/testing/selftests/bpf/progs/linked_list_fail.c| 8
++++-
---3 files changed, 9 insertions(+), 5 deletions(-)[...]
Quoted 49 lines
Quoted 31 lines
Quoted 22 lines
Quoted 22 lines
/* When utilizing
vmlinux.hwith BPF CO-RE, user BPF programs
can't include
* any system-level headers (such asstddef.h,linux/version.h,
etc), and
* commonly-used macros like NULL andKERNEL_VERSIONaren't available throughtools/testing/selftests/bpf/bpf_experimental.h +0
-0@@ -20,7 +20,7 @@ * A pointer to an object of the type corresponding tothe
passed in
* 'local_type_id', or NULL on failure.
*/
-extern void *bpf_obj_new_impl(__u64local_type_id, void *meta)__ksym;Quoted 8 lines
Quoted 3 lines
+extern
__must_checkvoid *bpf_obj_new_impl(__u64local_type_id,
void *meta)__ksym;
bpf_obj_new_implwill generally come fromvmlinux.hnowadays, and
that
one won't have__must_checkannotation, is that a problem?Quoted 7 lines
It should be fine according to [1]:
Compatible attribute specifications on distinct declarations of the
same function are merged.I will add this to the commit message in v3.
Sure, for BPF selftests it will work. My question was broader, for anyone using
bpf_obj_newin the wild, they won't have__must_checkannotation fromvmlinux.h(and I doubt they will manually add it like
we do here for BPF selftests), so if that's important, I guess we need
to think how to wire that up so that it happens automatically throughvmlinux.h.Quoted 1 line
"It's not that important to bother" is a fine answer as well :)
I see. Seems like it's tough:
Quoted 7 lines
- The attribute is not available in DWARF
- But we could introduceKF_MUST_CHECKflag
- Which pahole would extract from .BTF_ids and convert to a btf_decl_tag
- This will make pahole depend on .BTF_ids format though, which might
be undesirable
Hm, I should have checked that before hitting "send": apparently pahole already parses both .BTF_ids and __BTF_ID__set8__*.
Still, DW_TAG_GNU_annotation looks like a better long-term solution.
Quoted 16 lines
- Then bpftool would convert this
btf_decl_tagto__must_checkSeems like they are attempting to upstream the new
DW_TAG_GNU_annotationright now [1], if that lands and is available for non-BPF targets, we could put __attribute((btf_decl_tag("must_check"))) on kernel'sbpf_obj_new_impl()and directly access it from bpftool.So for now I would propose to limit the solution to selftests.
[1] https://gcc.gnu.org/pipermail/gcc-patches/2025-August/692445.html
Quoted 4 lines
Quoted 4 lines
[1]
https://gcc.gnu.org/onlinedocs/gcc-12.4.0/gcc/Function-Attributes.html[...]
Re: [PATCH bpf-next v2 1/2] selftests/bpf: Annotate bpf_obj_new_impl() with __must_check
Quoted 102 lines · Ilya Leoshkevich
Quoted 92 lines · Ilya Leoshkevich
Quoted 35 lines · Andrii Nakryiko
Quoted 32 lines · Ilya Leoshkevich
Quoted 30 lines · Andrii Nakryiko
Quoted 27 lines · Ilya Leoshkevich
The verifier requires that pointers returned by
bpf_obj_new_impl()
are
either dropped or stored in a map. Therefore programs that do not
use
its return values will fail to load. Make the compiler point out
these
issues. Adjust selftests that check that the verifier does indeed
spot
these bugs.Link:
https://lore.kernel.org/bpf/CAADnVQL6Q+QRv3_JwEd26biwGpFYcwD_=BjBJWLAtpgOP9CKRw@mail.gmail.com/Suggested-by Alexei Starovoitov <[email protected]>Signed-off-by Ilya Leoshkevich <[email protected]>--- tools/lib/bpf/bpf_helpers.h | 4 ++++ tools/testing/selftests/bpf/bpf_experimental.h | 2 +- tools/testing/selftests/bpf/progs/linked_list_fail.c | 8 ++++- --- 3 files changed, 9 insertions(+), 5 deletions(-)[...]
Quoted 49 lines
Quoted 31 lines
Quoted 22 lines
Quoted 22 lines
/* When utilizing
vmlinux.hwith BPF CO-RE, user BPF
programs
can't include
* any system-level headers (such asstddef.h,linux/version.h,
etc), and
* commonly-used macros like NULL andKERNEL_VERSIONaren't
available throughtools/testing/selftests/bpf/bpf_experimental.h +0
-0@@ -20,7 +20,7 @@* A pointer to an object of the type corresponding to the
passed in
* 'local_type_id', or NULL on failure.
*/
-extern void *bpf_obj_new_impl(__u64local_type_id, void *meta)__ksym;Quoted 8 lines
Quoted 3 lines
+extern
__must_checkvoid *bpf_obj_new_impl(__u64local_type_id,
void *meta)__ksym;
bpf_obj_new_implwill generally come fromvmlinux.hnowadays, and
that
one won't have__must_checkannotation, is that a problem?Quoted 7 lines
It should be fine according to [1]:
Compatible attribute specifications on distinct declarations of the
same function are merged.I will add this to the commit message in v3.
Sure, for BPF selftests it will work. My question was broader, for anyone using
bpf_obj_newin the wild, they won't have__must_checkannotation fromvmlinux.h(and I doubt they will manually add it like
we do here for BPF selftests), so if that's important, I guess we need
to think how to wire that up so that it happens automatically throughvmlinux.h.Quoted 1 line
"It's not that important to bother" is a fine answer as well :)
I see. Seems like it's tough:
Quoted 7 lines
- The attribute is not available in DWARF
- But we could introduceKF_MUST_CHECKflag
- Which pahole would extract from .BTF_ids and convert to abtf_decl_tag
- This will make pahole depend on .BTF_ids format though, which
might
be undesirable
Quoted 2 lines
Hm, I should have checked that before hitting "send": apparently pahole already parses both .BTF_ids and __BTF_ID__set8__*.
Correct, this isn't any new dependency.
Quoted 1 line
Still,
DW_TAG_GNU_annotationlooks like a better long-term solution.
Ihor a bit earlier added BTF-specific way to attach any random attribute to BTF type (it's a special form of BTF_TYPE_TAG/BTF_DECL_TAG), I think it's still on his plate to wire up the use of that for a few long-standing issues with vmlinux.h, so this might be yet another reason and use case for that.
But agreed, for now I'd go with BPF selftests-specific mitigation.
Quoted 17 lines
Quoted 17 lines
- Then bpftool would convert this
btf_decl_tagto__must_checkSeems like they are attempting to upstream the new
DW_TAG_GNU_annotationright now [1], if that lands and is available for non-BPF targets, we could put __attribute((btf_decl_tag("must_check"))) on kernel'sbpf_obj_new_impl()and directly access it from bpftool.So for now I would propose to limit the solution to selftests.
[1] https://gcc.gnu.org/pipermail/gcc-patches/2025-August/692445.html
Quoted 4 lines
Quoted 4 lines
[1]
https://gcc.gnu.org/onlinedocs/gcc-12.4.0/gcc/Function-Attributes.html[...]
Ihor Solodrai
reviewerRe: [PATCH bpf-next v2 1/2] selftests/bpf: Annotate bpf_obj_new_impl() with __must_check
Quoted 108 lines · Andrii Nakryiko
Quoted 102 lines · Ilya Leoshkevich
Quoted 92 lines · Ilya Leoshkevich
Quoted 35 lines · Andrii Nakryiko
Quoted 32 lines · Ilya Leoshkevich
Quoted 30 lines · Andrii Nakryiko
Quoted 27 lines · Ilya Leoshkevich
The verifier requires that pointers returned by
bpf_obj_new_impl()
are
either dropped or stored in a map. Therefore programs that do not
use
its return values will fail to load. Make the compiler point out
these
issues. Adjust selftests that check that the verifier does indeed
spot
these bugs.Link:
https://lore.kernel.org/bpf/CAADnVQL6Q+QRv3_JwEd26biwGpFYcwD_=BjBJWLAtpgOP9CKRw@mail.gmail.com/Suggested-by Alexei Starovoitov <[email protected]>Signed-off-by Ilya Leoshkevich <[email protected]>--- tools/lib/bpf/bpf_helpers.h | 4 ++++ tools/testing/selftests/bpf/bpf_experimental.h | 2 +- tools/testing/selftests/bpf/progs/linked_list_fail.c | 8 ++++- --- 3 files changed, 9 insertions(+), 5 deletions(-)[...]
Quoted 49 lines
Quoted 31 lines
Quoted 22 lines
Quoted 22 lines
/* When utilizing
vmlinux.hwith BPF CO-RE, user BPF
programs
can't include
* any system-level headers (such asstddef.h,linux/version.h,
etc), and
* commonly-used macros like NULL andKERNEL_VERSIONaren't
available throughtools/testing/selftests/bpf/bpf_experimental.h +0
-0@@ -20,7 +20,7 @@* A pointer to an object of the type corresponding to the
passed in
* 'local_type_id', or NULL on failure.
*/
-extern void *bpf_obj_new_impl(__u64local_type_id, void *meta)__ksym;Quoted 8 lines
Quoted 3 lines
+extern
__must_checkvoid *bpf_obj_new_impl(__u64local_type_id,
void *meta)__ksym;
bpf_obj_new_implwill generally come fromvmlinux.hnowadays, and
that
one won't have__must_checkannotation, is that a problem?Quoted 7 lines
It should be fine according to [1]:
Compatible attribute specifications on distinct declarations of the
same function are merged.I will add this to the commit message in v3.
Sure, for BPF selftests it will work. My question was broader, for anyone using
bpf_obj_newin the wild, they won't have__must_checkannotation fromvmlinux.h(and I doubt they will manually add it like
we do here for BPF selftests), so if that's important, I guess we need
to think how to wire that up so that it happens automatically throughvmlinux.h.Quoted 1 line
"It's not that important to bother" is a fine answer as well :)
I see. Seems like it's tough:
Quoted 7 lines
- The attribute is not available in DWARF
- But we could introduceKF_MUST_CHECKflag
- Which pahole would extract from .BTF_ids and convert to abtf_decl_tag
- This will make pahole depend on .BTF_ids format though, which
might
be undesirableQuoted 2 lines
Hm, I should have checked that before hitting "send": apparently pahole already parses both .BTF_ids and __BTF_ID__set8__*.
Correct, this isn't any new dependency.
Quoted 7 lines
Quoted 1 line
Still,
DW_TAG_GNU_annotationlooks like a better long-term solution.Ihor a bit earlier added BTF-specific way to attach any random attribute to BTF type (it's a special form of BTF_TYPE_TAG/BTF_DECL_TAG), I think it's still on his plate to wire up the use of that for a few long-standing issues with
vmlinux.h, so this might be yet another reason and use case for that.
Acked. Thanks for the reminder...
Although in this particular case (__must_check) there is still an issue of passing through the attribute from kernel source to pahole, and AFAIU the only feasible way to do that right now is via KF_ flags.
So this DW_TAG_GNU_annotation looks promising, because with that pahole could read annotations from DWARF directly, without special handling of .BTF_ids data.
But then we probably don't want pahole to put _all_ the attributes in BTF, there might be too many of attributes we don't care about. Still, a filter on a generic DWARF tag is much simpler then what we have now.
Ilya, thanks for sharing. I wasn't aware of DW_TAG_GNU_annotation development.
Quoted 20 lines
But agreed, for now I'd go with BPF selftests-specific mitigation.
Quoted 17 lines
Quoted 17 lines
- Then bpftool would convert this
btf_decl_tagto__must_checkSeems like they are attempting to upstream the new
DW_TAG_GNU_annotationright now [1], if that lands and is available for non-BPF targets, we could put __attribute((btf_decl_tag("must_check"))) on kernel'sbpf_obj_new_impl()and directly access it from bpftool.So for now I would propose to limit the solution to selftests.
[1] https://gcc.gnu.org/pipermail/gcc-patches/2025-August/692445.html
Quoted 4 lines
Quoted 4 lines
[1]
https://gcc.gnu.org/onlinedocs/gcc-12.4.0/gcc/Function-Attributes.html[...]
PATCH v2 2/2 selftests/bpf: Fix "expression result unused" warnings with icecc
In reply to Ilya Leoshkevichicecc is a compiler wrapper that distributes compile jobs over a build farm [1]. It works by sending toolchain binaries and preprocessed source code to remote machines.
Unfortunately using it with BPF selftests causes build failures due to a clang bug [2]. The problem is that clang suppresses the -Wunused-value warning if the unused expression comes from a macro expansion. Since icecc compiles preprocessed source code, this information is not available. This leads to -Wunused-value false positives.
arena_spin_lock_slowpath() uses two macros that produce values and ignores the results. Add (void) cast to explicitly indicate that this is intentional and suppress the warning.
An alternative solution is to change the macros to not produce values. This would work today, but in the future there may appear users who need them. Another potential solution is to replace these macros with functions. Unfortunately this would not work, because these macros work with unknown types and control flow.
[1] https://github.com/icecc/icecream
[2] https://github.com/llvm/llvm-project/issues/142614
--- tools/testing/selftests/bpf/progs/bpf_arena_spin_lock.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/progs/bpf_arena_spin_lock.h +2 -2
@@ -302,7 +302,7 @@ int arena_spin_lock_slowpath(arena_spinlock_t __arena __arg_arena *lock, u32 val* barriers. */ if (val & _Q_LOCKED_MASK) -smp_cond_load_acquire_label (&lock->locked, !VAL , release_err); + (void )smp_cond_load_acquire_label (&lock->locked, !VAL , release_err);/* * take ownership and clear the pending bit.
@@ -380,7 +380,7 @@ int arena_spin_lock_slowpath(arena_spinlock_t __arena __arg_arena *lock, u32 val/* Link @node into the waitqueue. */ WRITE_ONCE (prev->next, node); -arch_mcs_spin_lock_contended_label (&node->locked, release_node_err); + (void )arch_mcs_spin_lock_contended_label (&node->locked, release_node_err);/* * While waiting for the MCS lock, the next pointer may have
Signature · 1 line
2.50.1