[PATCH bpf v1] selftests/bpf: Fix set but not used errors

Tiezhu Yang posted 1 patch 3 months, 3 weeks ago
tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c | 3 ++-
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c            | 3 ++-
tools/testing/selftests/bpf/prog_tests/find_vma.c              | 3 ++-
tools/testing/selftests/bpf/prog_tests/perf_branches.c         | 3 ++-
tools/testing/selftests/bpf/prog_tests/perf_link.c             | 3 ++-
tools/testing/selftests/bpf/test_maps.h                        | 1 +
tools/testing/selftests/bpf/test_progs.h                       | 1 +
7 files changed, 12 insertions(+), 5 deletions(-)
[PATCH bpf v1] selftests/bpf: Fix set but not used errors
Posted by Tiezhu Yang 3 months, 3 weeks ago
There are some set but not used errors under tools/testing/selftests/bpf
when compiling with the latest upstream mainline GCC, add the compiler
attribute __maybe_unused for the variables that may be used to fix the
errors, compile tested only.

Cc: stable@vger.kernel.org
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c | 3 ++-
 tools/testing/selftests/bpf/prog_tests/bpf_cookie.c            | 3 ++-
 tools/testing/selftests/bpf/prog_tests/find_vma.c              | 3 ++-
 tools/testing/selftests/bpf/prog_tests/perf_branches.c         | 3 ++-
 tools/testing/selftests/bpf/prog_tests/perf_link.c             | 3 ++-
 tools/testing/selftests/bpf/test_maps.h                        | 1 +
 tools/testing/selftests/bpf/test_progs.h                       | 1 +
 7 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c b/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c
index d32e4edac930..2b8edf996126 100644
--- a/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c
+++ b/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c
@@ -226,7 +226,8 @@ static void test_lpm_order(void)
 static void test_lpm_map(int keysize)
 {
 	LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);
-	volatile size_t n_matches, n_matches_after_delete;
+	/* To avoid a -Wunused-but-set-variable warning. */
+	__maybe_unused volatile size_t n_matches, n_matches_after_delete;
 	size_t i, j, n_nodes, n_lookups;
 	struct tlpm_node *t, *list = NULL;
 	struct bpf_lpm_trie_key_u8 *key;
diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c b/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c
index 75f4dff7d042..119fbe478941 100644
--- a/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c
+++ b/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c
@@ -423,7 +423,8 @@ static void tp_subtest(struct test_bpf_cookie *skel)
 
 static void burn_cpu(void)
 {
-	volatile int j = 0;
+	/* To avoid a -Wunused-but-set-variable warning. */
+	__maybe_unused volatile int j = 0;
 	cpu_set_t cpu_set;
 	int i, err;
 
diff --git a/tools/testing/selftests/bpf/prog_tests/find_vma.c b/tools/testing/selftests/bpf/prog_tests/find_vma.c
index f7619e0ade10..ba4b7cbc1dea 100644
--- a/tools/testing/selftests/bpf/prog_tests/find_vma.c
+++ b/tools/testing/selftests/bpf/prog_tests/find_vma.c
@@ -49,7 +49,8 @@ static bool find_vma_pe_condition(struct find_vma *skel)
 static void test_find_vma_pe(struct find_vma *skel)
 {
 	struct bpf_link *link = NULL;
-	volatile int j = 0;
+	/* To avoid a -Wunused-but-set-variable warning. */
+	__maybe_unused volatile int j = 0;
 	int pfd, i;
 	const int one_bn = 1000000000;
 
diff --git a/tools/testing/selftests/bpf/prog_tests/perf_branches.c b/tools/testing/selftests/bpf/prog_tests/perf_branches.c
index bc24f83339d6..7ce4df59b603 100644
--- a/tools/testing/selftests/bpf/prog_tests/perf_branches.c
+++ b/tools/testing/selftests/bpf/prog_tests/perf_branches.c
@@ -64,7 +64,8 @@ static void test_perf_branches_common(int perf_fd,
 	int err, i, duration = 0;
 	bool detached = false;
 	struct bpf_link *link;
-	volatile int j = 0;
+	/* To avoid a -Wunused-but-set-variable warning. */
+	__maybe_unused volatile int j = 0;
 	cpu_set_t cpu_set;
 
 	skel = test_perf_branches__open_and_load();
diff --git a/tools/testing/selftests/bpf/prog_tests/perf_link.c b/tools/testing/selftests/bpf/prog_tests/perf_link.c
index d940ff87fa08..6cbd5b7bcb57 100644
--- a/tools/testing/selftests/bpf/prog_tests/perf_link.c
+++ b/tools/testing/selftests/bpf/prog_tests/perf_link.c
@@ -12,7 +12,8 @@
 
 static void burn_cpu(void)
 {
-	volatile int j = 0;
+	/* To avoid a -Wunused-but-set-variable warning. */
+	__maybe_unused volatile int j = 0;
 	cpu_set_t cpu_set;
 	int i, err;
 
diff --git a/tools/testing/selftests/bpf/test_maps.h b/tools/testing/selftests/bpf/test_maps.h
index e4ac704a536c..8d7413bca13c 100644
--- a/tools/testing/selftests/bpf/test_maps.h
+++ b/tools/testing/selftests/bpf/test_maps.h
@@ -5,6 +5,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdbool.h>
+#include <linux/compiler.h>
 
 #define CHECK(condition, tag, format...) ({				\
 	int __ret = !!(condition);					\
diff --git a/tools/testing/selftests/bpf/test_progs.h b/tools/testing/selftests/bpf/test_progs.h
index eebfc18cdcd2..927c159d7fad 100644
--- a/tools/testing/selftests/bpf/test_progs.h
+++ b/tools/testing/selftests/bpf/test_progs.h
@@ -16,6 +16,7 @@
 #include <linux/types.h>
 typedef __u16 __sum16;
 #include <arpa/inet.h>
+#include <linux/compiler.h>
 #include <linux/if_ether.h>
 #include <linux/if_packet.h>
 #include <linux/ip.h>
-- 
2.42.0
Re: [PATCH bpf v1] selftests/bpf: Fix set but not used errors
Posted by Alexei Starovoitov 3 months, 3 weeks ago
On Fri, Oct 17, 2025 at 2:35 AM Tiezhu Yang <yangtiezhu@loongson.cn> wrote:
>
> There are some set but not used errors under tools/testing/selftests/bpf
> when compiling with the latest upstream mainline GCC, add the compiler
> attribute __maybe_unused for the variables that may be used to fix the
> errors, compile tested only.
>
> Cc: stable@vger.kernel.org
> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
> ---
>  tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c | 3 ++-
>  tools/testing/selftests/bpf/prog_tests/bpf_cookie.c            | 3 ++-
>  tools/testing/selftests/bpf/prog_tests/find_vma.c              | 3 ++-
>  tools/testing/selftests/bpf/prog_tests/perf_branches.c         | 3 ++-
>  tools/testing/selftests/bpf/prog_tests/perf_link.c             | 3 ++-
>  tools/testing/selftests/bpf/test_maps.h                        | 1 +
>  tools/testing/selftests/bpf/test_progs.h                       | 1 +
>  7 files changed, 12 insertions(+), 5 deletions(-)
>
> diff --git a/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c b/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c
> index d32e4edac930..2b8edf996126 100644
> --- a/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c
> +++ b/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c
> @@ -226,7 +226,8 @@ static void test_lpm_order(void)
>  static void test_lpm_map(int keysize)
>  {
>         LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);
> -       volatile size_t n_matches, n_matches_after_delete;
> +       /* To avoid a -Wunused-but-set-variable warning. */
> +       __maybe_unused volatile size_t n_matches, n_matches_after_delete;

I think it's better to disable the warning instead of hacking the tests.
Arguably it's a grey zone whether n_matches++ qualifies as a "use".
It's certainly not a nop, since it's a volatile variable.

pw-bot: cr
Re: [PATCH bpf v1] selftests/bpf: Fix set but not used errors
Posted by Eduard Zingerman 3 months, 3 weeks ago
On Fri, 2025-10-17 at 10:20 -0700, Alexei Starovoitov wrote:
> On Fri, Oct 17, 2025 at 2:35 AM Tiezhu Yang <yangtiezhu@loongson.cn> wrote:
> >
> > There are some set but not used errors under tools/testing/selftests/bpf
> > when compiling with the latest upstream mainline GCC, add the compiler
> > attribute __maybe_unused for the variables that may be used to fix the
> > errors, compile tested only.
> >
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
> > ---
> >  tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c | 3 ++-
> >  tools/testing/selftests/bpf/prog_tests/bpf_cookie.c            | 3 ++-
> >  tools/testing/selftests/bpf/prog_tests/find_vma.c              | 3 ++-
> >  tools/testing/selftests/bpf/prog_tests/perf_branches.c         | 3 ++-
> >  tools/testing/selftests/bpf/prog_tests/perf_link.c             | 3 ++-
> >  tools/testing/selftests/bpf/test_maps.h                        | 1 +
> >  tools/testing/selftests/bpf/test_progs.h                       | 1 +
> >  7 files changed, 12 insertions(+), 5 deletions(-)
> >
> > diff --git a/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c b/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c
> > index d32e4edac930..2b8edf996126 100644
> > --- a/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c
> > +++ b/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c
> > @@ -226,7 +226,8 @@ static void test_lpm_order(void)
> >  static void test_lpm_map(int keysize)
> >  {
> >         LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);
> > -       volatile size_t n_matches, n_matches_after_delete;
> > +       /* To avoid a -Wunused-but-set-variable warning. */
> > +       __maybe_unused volatile size_t n_matches, n_matches_after_delete;
>
> I think it's better to disable the warning instead of hacking the tests.
> Arguably it's a grey zone whether n_matches++ qualifies as a "use".
> It's certainly not a nop, since it's a volatile variable.
>
> pw-bot: cr

Maybe something like below?

--- a/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c
+++ b/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c
@@ -223,6 +223,8 @@ static void test_lpm_order(void)
        tlpm_clear(l2);
 }

+static int print_stats; /* debug knob */
+
 static void test_lpm_map(int keysize)
 {
        LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);
@@ -334,14 +336,14 @@ static void test_lpm_map(int keysize)
        tlpm_clear(list);

        /* With 255 random nodes in the map, we are pretty likely to match
-        * something on every lookup. For statistics, use this:
-        *
-        *     printf("          nodes: %zu\n"
-        *            "        lookups: %zu\n"
-        *            "        matches: %zu\n"
-        *            "matches(delete): %zu\n",
-        *            n_nodes, n_lookups, n_matches, n_matches_after_delete);
+        * something on every lookup.
         */
+       if (print_stats)
+               printf("          nodes: %zu\n"
+                      "        lookups: %zu\n"
+                      "        matches: %zu\n"
+                      "matches(delete): %zu\n",
+                      n_nodes, n_lookups, n_matches, n_matches_after_delete);
 }
Re: [PATCH bpf v1] selftests/bpf: Fix set but not used errors
Posted by Alexei Starovoitov 3 months, 3 weeks ago
On Fri, Oct 17, 2025 at 10:40 AM Eduard Zingerman <eddyz87@gmail.com> wrote:
>
> On Fri, 2025-10-17 at 10:20 -0700, Alexei Starovoitov wrote:
> > On Fri, Oct 17, 2025 at 2:35 AM Tiezhu Yang <yangtiezhu@loongson.cn> wrote:
> > >
> > > There are some set but not used errors under tools/testing/selftests/bpf
> > > when compiling with the latest upstream mainline GCC, add the compiler
> > > attribute __maybe_unused for the variables that may be used to fix the
> > > errors, compile tested only.
> > >
> > > Cc: stable@vger.kernel.org
> > > Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
> > > ---
> > >  tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c | 3 ++-
> > >  tools/testing/selftests/bpf/prog_tests/bpf_cookie.c            | 3 ++-
> > >  tools/testing/selftests/bpf/prog_tests/find_vma.c              | 3 ++-
> > >  tools/testing/selftests/bpf/prog_tests/perf_branches.c         | 3 ++-
> > >  tools/testing/selftests/bpf/prog_tests/perf_link.c             | 3 ++-
> > >  tools/testing/selftests/bpf/test_maps.h                        | 1 +
> > >  tools/testing/selftests/bpf/test_progs.h                       | 1 +
> > >  7 files changed, 12 insertions(+), 5 deletions(-)
> > >
> > > diff --git a/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c b/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c
> > > index d32e4edac930..2b8edf996126 100644
> > > --- a/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c
> > > +++ b/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c
> > > @@ -226,7 +226,8 @@ static void test_lpm_order(void)
> > >  static void test_lpm_map(int keysize)
> > >  {
> > >         LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);
> > > -       volatile size_t n_matches, n_matches_after_delete;
> > > +       /* To avoid a -Wunused-but-set-variable warning. */
> > > +       __maybe_unused volatile size_t n_matches, n_matches_after_delete;
> >
> > I think it's better to disable the warning instead of hacking the tests.
> > Arguably it's a grey zone whether n_matches++ qualifies as a "use".
> > It's certainly not a nop, since it's a volatile variable.
> >
> > pw-bot: cr
>
> Maybe something like below?
>
> --- a/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c
> +++ b/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c
> @@ -223,6 +223,8 @@ static void test_lpm_order(void)
>         tlpm_clear(l2);
>  }
>
> +static int print_stats; /* debug knob */
> +
>  static void test_lpm_map(int keysize)
>  {
>         LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);
> @@ -334,14 +336,14 @@ static void test_lpm_map(int keysize)
>         tlpm_clear(list);
>
>         /* With 255 random nodes in the map, we are pretty likely to match
> -        * something on every lookup. For statistics, use this:
> -        *
> -        *     printf("          nodes: %zu\n"
> -        *            "        lookups: %zu\n"
> -        *            "        matches: %zu\n"
> -        *            "matches(delete): %zu\n",
> -        *            n_nodes, n_lookups, n_matches, n_matches_after_delete);
> +        * something on every lookup.
>          */
> +       if (print_stats)
> +               printf("          nodes: %zu\n"
> +                      "        lookups: %zu\n"
> +                      "        matches: %zu\n"
> +                      "matches(delete): %zu\n",
> +                      n_nodes, n_lookups, n_matches, n_matches_after_delete);

For this particular one, yes. But other tests just do j++ to have
non-empty loop body that does something to make sure that
compilers don't remove the loop.