diff mbox series

[bpf-next] selftests/bpf: fix a clang compilation error for send_signal.c

Message ID 20220311000508.2036640-1-yhs@fb.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series [bpf-next] selftests/bpf: fix a clang compilation error for send_signal.c | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR pending PR summary
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 10 maintainers not CCed: linux-kselftest@vger.kernel.org kpsingh@kernel.org john.fastabend@gmail.com kafai@fb.com shuah@kernel.org songliubraving@fb.com nathan@kernel.org llvm@lists.linux.dev netdev@vger.kernel.org ndesaulniers@google.com
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 8 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next pending VM_Test

Commit Message

Yonghong Song March 11, 2022, 12:05 a.m. UTC
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

Signed-off-by: Yonghong Song <yhs@fb.com>
---
 tools/testing/selftests/bpf/prog_tests/send_signal.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Andrii Nakryiko March 11, 2022, 12:17 a.m. UTC | #1
On Thu, Mar 10, 2022 at 4:05 PM Yonghong Song <yhs@fb.com> wrote:
>
> 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
>
> Signed-off-by: Yonghong Song <yhs@fb.com>
> ---
>  tools/testing/selftests/bpf/prog_tests/send_signal.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/send_signal.c b/tools/testing/selftests/bpf/prog_tests/send_signal.c
> index def50f1c5c31..05e303119151 100644
> --- a/tools/testing/selftests/bpf/prog_tests/send_signal.c
> +++ b/tools/testing/selftests/bpf/prog_tests/send_signal.c
> @@ -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.

>
>                 buf[0] = sigusr1_received ? '2' : '0';
>                 ASSERT_EQ(sigusr1_received, 1, "sigusr1_received");
> --
> 2.30.2
>
Yonghong Song March 11, 2022, 12:33 a.m. UTC | #2
On 3/10/22 4:17 PM, Andrii Nakryiko wrote:
> On Thu, Mar 10, 2022 at 4:05 PM Yonghong Song <yhs@fb.com> wrote:
>>
>> 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
>>
>> Signed-off-by: Yonghong Song <yhs@fb.com>
>> ---
>>   tools/testing/selftests/bpf/prog_tests/send_signal.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/tools/testing/selftests/bpf/prog_tests/send_signal.c b/tools/testing/selftests/bpf/prog_tests/send_signal.c
>> index def50f1c5c31..05e303119151 100644
>> --- a/tools/testing/selftests/bpf/prog_tests/send_signal.c
>> +++ b/tools/testing/selftests/bpf/prog_tests/send_signal.c
>> @@ -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.

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.

> 
>>
>>                  buf[0] = sigusr1_received ? '2' : '0';
>>                  ASSERT_EQ(sigusr1_received, 1, "sigusr1_received");
>> --
>> 2.30.2
>>
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/send_signal.c b/tools/testing/selftests/bpf/prog_tests/send_signal.c
index def50f1c5c31..05e303119151 100644
--- a/tools/testing/selftests/bpf/prog_tests/send_signal.c
+++ b/tools/testing/selftests/bpf/prog_tests/send_signal.c
@@ -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");