Skip to contentSkip to conversation

selftests/bpf: fix a clang compilation error for send_signal.c

PATCH Superseded Yonghong Song 2 participants 3 messages Mar 11, 2022

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/bpf: fix a clang compilation error for send_signal.c

Building selftests/bpf with latest clang compiler (clang15 built
from source), I hit the following compilation error:
/.../prog_tests/send_signal.c:43:16: error: variable 'j' set but not used [-Werror,-Wunused-but-set-variable]

                  volatile int j = 0;
                               ^

1 error generated.
The problem also exists with clang13 and clang14. clang12 is okay.

In send_signal.c, we have the following code
volatile int j = 0;
...
for (int i = 0; i < 100000000 && !sigusr1_received; i++)

    j /= i + 1;

to burn cpu cycles so bpf_send_signal() helper can be tested in nmi mode.

Slightly changing 'j /= i + 1' to 'j /= i + j' or 'j++' can fix the problem. Further investigation indicated this should be a clang bug ([1]). The upstream fix will be proposed later. But it is a good idea to workaround the issue to unblock people who build kernel/selftests with clang.

[1] https://discourse.llvm.org/t/strange-clang-unused-but-set-variable-error-with-volatile-variables/60841

---
 tools/testing/selftests/bpf/prog_tests/send_signal.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/send_signal.c +1 -1
index def50f1c5c31..05e303119151 100644
@@ -65,7 +65,7 @@ static void test_send_signal_common(struct perf_event_attr *attr,
 
 		/* wait a little for signal handler */
 		for (int i = 0; i < 100000000 && !sigusr1_received; i++)
-			j /= i + 1;
+			j /= i + j;
 
 		buf[0] = sigusr1_received ? '2' : '0';
 		ASSERT_EQ(sigusr1_received, 1, "sigusr1_received");
Signature · 1 line
2.30.2
#1 lore raw
Quoted 35 lines · Yonghong Song

Building selftests/bpf with latest clang compiler (clang15 built
from source), I hit the following compilation error:
/.../prog_tests/send_signal.c:43:16: error: variable 'j' set but not used [-Werror,-Wunused-but-set-variable]

                  volatile int j = 0;
                               ^

1 error generated.
The problem also exists with clang13 and clang14. clang12 is okay.

In send_signal.c, we have the following code
volatile int j = 0;
...
for (int i = 0; i < 100000000 && !sigusr1_received; i++)

    j /= i + 1;

to burn cpu cycles so bpf_send_signal() helper can be tested in nmi mode.

Slightly changing 'j /= i + 1' to 'j /= i + j' or 'j++' can fix the problem. Further investigation indicated this should be a clang bug ([1]). The upstream fix will be proposed later. But it is a good idea to workaround the issue to unblock people who build kernel/selftests with clang.

[1] https://discourse.llvm.org/t/strange-clang-unused-but-set-variable-error-with-volatile-variables/60841

---
 tools/testing/selftests/bpf/prog_tests/send_signal.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/send_signal.c +0 -0
index def50f1c5c31..05e303119151 100644

tools/testing/selftests/bpf/prog_tests/send_signal.c static void test_send_signal_common(struct perf_event_attr *attr, · line 68

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/send_signal.c +1 -1
@@ -65,7 +65,7 @@ static void test_send_signal_common(struct perf_event_attr *attr,
 
                /* wait a little for signal handler */
                for (int i = 0; i < 100000000 && !sigusr1_received; i++)
-                       j /= i + 1;
+                       j /= i + j;

+ 1 was there to avoid division by zero. Let's make it i + j + 1 then.

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/send_signal.c +0 -1
                buf[0] = sigusr1_received ? '2' : '0';
                ASSERT_EQ(sigusr1_received, 1, "sigusr1_received");
--

2.30.2

#2 lore raw
Quoted 37 lines · Andrii Nakryiko
Quoted 36 lines · Yonghong Song

Building selftests/bpf with latest clang compiler (clang15 built
from source), I hit the following compilation error:
/.../prog_tests/send_signal.c:43:16: error: variable 'j' set but not used [-Werror,-Wunused-but-set-variable]

                   volatile int j = 0;
                                ^

1 error generated.
The problem also exists with clang13 and clang14. clang12 is okay.

In send_signal.c, we have the following code

   volatile int j = 0;
   ...
   for (int i = 0; i < 100000000 && !sigusr1_received; i++)
     j /= i + 1;

to burn cpu cycles so bpf_send_signal() helper can be tested in nmi mode.

Slightly changing 'j /= i + 1' to 'j /= i + j' or 'j++' can fix the problem. Further investigation indicated this should be a clang bug ([1]). The upstream fix will be proposed later. But it is a good idea to workaround the issue to unblock people who build kernel/selftests with clang.

[1] https://discourse.llvm.org/t/strange-clang-unused-but-set-variable-error-with-volatile-variables/60841

---
  tools/testing/selftests/bpf/prog_tests/send_signal.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/send_signal.c +0 -0
index def50f1c5c31..05e303119151 100644
@@ -65,7 +65,7 @@ static void test_send_signal_common(struct perf_event_attr *attr,
 
Quoted 6 lines

tools/testing/selftests/bpf/prog_tests/send_signal.c static void test_send_signal_common(struct perf_event_attr *attr, · line 68

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/send_signal.c +1 -1
                 /* wait a little for signal handler */
                 for (int i = 0; i < 100000000 && !sigusr1_received; i++)
-                       j /= i + 1;
+                       j /= i + j;

+ 1 was there to avoid division by zero. Let's make it i + j + 1 then.

Good point. Previously I did this 'i + 1 + j' and run the selftests and it works fine. And for preparing the final patch, I removed "+ 1" to simplify the code but didn't actually run the test which is not great.

Will fix in v2.

Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/send_signal.c +0 -1
                 buf[0] = sigusr1_received ? '2' : '0';
                 ASSERT_EQ(sigusr1_received, 1, "sigusr1_received");
--

2.30.2