Skip to contentSkip to conversation

bpf: Fix compilation warning with -Wparentheses

PATCH Applied Yonghong Song 3 participants 4 messages Jul 28, 2023

Subsystems (4)
lore
Share & tools
lore

Yonghong Song

author reviewer
#0 lore raw

PATCH 1/2 bpf: Fix compilation warning with -Wparentheses

The kernel test robot reported compilation warnings when -Wparentheses is added to KBUILD_CFLAGS with gcc compiler. The following is the error message:

.../bpf-next/kernel/bpf/verifier.c: In function ‘coerce_reg_to_size_sx’:
.../bpf-next/kernel/bpf/verifier.c:5901:14:

    error: suggest parentheses around comparison in operand of ‘==’ [-Werror=parentheses]
    if (s64_max >= 0 == s64_min >= 0) {
        ~~~~~~~~^~~~
  .../bpf-next/kernel/bpf/verifier.c: In function ‘coerce_subreg_to_size_sx’:
  .../bpf-next/kernel/bpf/verifier.c:5965:14:
    error: suggest parentheses around comparison in operand of ‘==’ [-Werror=parentheses]
    if (s32_min >= 0 == s32_max >= 0) {
        ~~~~~~~~^~~~

To fix the issue, add proper parentheses for the above '>=' condition to silence the warning/error.

I tried a few clang compilers like clang16 and clang18 and they do not emit such warnings with -Wparentheses.

---
 kernel/bpf/core.c     | 4 ++--
 kernel/bpf/verifier.c | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
kernel/bpf/core.c +2 -2
index db0b631908c2..baccdec22f19 100644
@@ -1877,7 +1877,7 @@ static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn)
 		case 1:
 			AX = abs((s32)DST);
 			do_div(AX, abs((s32)SRC));
-			if ((s32)DST < 0 == (s32)SRC < 0)
+			if (((s32)DST < 0) == ((s32)SRC < 0))
 				DST = (u32)AX;
 			else
 				DST = (u32)-AX;
@@ -1904,7 +1904,7 @@ static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn)
 		case 1:
 			AX = abs((s32)DST);
 			do_div(AX, abs((s32)IMM));
-			if ((s32)DST < 0 == (s32)IMM < 0)
+			if (((s32)DST < 0) == ((s32)IMM < 0))
 				DST = (u32)AX;
 			else
 				DST = (u32)-AX;
kernel/bpf/verifier.c +2 -2
index 0b1ada93582b..e7b1af016841 100644
@@ -5898,7 +5898,7 @@ static void coerce_reg_to_size_sx(struct bpf_reg_state *reg, int size)
 	s64_min = min(init_s64_max, init_s64_min);
 
 	/* both of s64_max/s64_min positive or negative */
-	if (s64_max >= 0 == s64_min >= 0) {
+	if ((s64_max >= 0) == (s64_min >= 0)) {
 		reg->smin_value = reg->s32_min_value = s64_min;
 		reg->smax_value = reg->s32_max_value = s64_max;
 		reg->umin_value = reg->u32_min_value = s64_min;
@@ -5962,7 +5962,7 @@ static void coerce_subreg_to_size_sx(struct bpf_reg_state *reg, int size)
 	s32_max = max(init_s32_max, init_s32_min);
 	s32_min = min(init_s32_max, init_s32_min);
 
-	if (s32_min >= 0 == s32_max >= 0) {
+	if ((s32_min >= 0) == (s32_max >= 0)) {
 		reg->s32_min_value = s32_min;
 		reg->s32_max_value = s32_max;
 		reg->u32_min_value = (u32)s32_min;
Signature · 1 line
2.34.1
author reviewer
#1 lore raw

PATCH 2/2 selftests/bpf: Enable test test_progs-cpuv4 for gcc build kernel

Currently, test_progs-cpuv4 is generated with clang build kernel when bpf cpu=v4 is supported by the clang compiler. Let us enable test_progs-cpuv4 for gcc build kernel as well.

---
 tools/testing/selftests/bpf/Makefile | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/Makefile +3 -2
index 6a45719a8d47..73a6ca80fa16 100644
@@ -36,11 +36,12 @@ LDLIBS += -lelf -lz -lrt -lpthread
 ifneq ($(LLVM),)
 # Silence some warnings when compiled with clang
 CFLAGS += -Wno-unused-command-line-argument
-# Check whether cpu=v4 is supported or not by clang
+endif
+
+# Check whether bpf cpu=v4 is supported or not by clang
 ifneq ($(shell $(CLANG) --target=bpf -mcpu=help 2>&1 | grep 'v4'),)
 CLANG_CPUV4 := 1
 endif
-endif
 
 # Order correspond to 'make run_tests' order
 TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \
Signature · 1 line
2.34.1
Acked-by on v1 1/2
#2 lore raw
In reply to Yonghong Song
Quoted 14 lines · Yonghong Song

The kernel test robot reported compilation warnings when -Wparentheses is added to KBUILD_CFLAGS with gcc compiler. The following is the error message:

.../bpf-next/kernel/bpf/verifier.c: In function ‘coerce_reg_to_size_sx’:
.../bpf-next/kernel/bpf/verifier.c:5901:14:

    error: suggest parentheses around comparison in operand of ‘==’ [-Werror=parentheses]
    if (s64_max >= 0 == s64_min >= 0) {
        ~~~~~~~~^~~~
  .../bpf-next/kernel/bpf/verifier.c: In function ‘coerce_subreg_to_size_sx’:
  .../bpf-next/kernel/bpf/verifier.c:5965:14:
    error: suggest parentheses around comparison in operand of ‘==’ [-Werror=parentheses]
    if (s32_min >= 0 == s32_max >= 0) {
        ~~~~~~~~^~~~
Quoted 5 lines

To fix the issue, add proper parentheses for the above '>=' condition to silence the warning/error.

I tried a few clang compilers like clang16 and clang18 and they do not emit such warnings with -Wparentheses.

I just hit it with gcc and this fixes it for me

thanks,
jirka

Quoted 57 lines
---
 kernel/bpf/core.c     | 4 ++--
 kernel/bpf/verifier.c | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
kernel/bpf/core.c +2 -2
index db0b631908c2..baccdec22f19 100644
@@ -1877,7 +1877,7 @@ static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn)
 		case 1:
 			AX = abs((s32)DST);
 			do_div(AX, abs((s32)SRC));
-			if ((s32)DST < 0 == (s32)SRC < 0)
+			if (((s32)DST < 0) == ((s32)SRC < 0))
 				DST = (u32)AX;
 			else
 				DST = (u32)-AX;
@@ -1904,7 +1904,7 @@ static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn)
 		case 1:
 			AX = abs((s32)DST);
 			do_div(AX, abs((s32)IMM));
-			if ((s32)DST < 0 == (s32)IMM < 0)
+			if (((s32)DST < 0) == ((s32)IMM < 0))
 				DST = (u32)AX;
 			else
 				DST = (u32)-AX;
kernel/bpf/verifier.c +2 -2
index 0b1ada93582b..e7b1af016841 100644
@@ -5898,7 +5898,7 @@ static void coerce_reg_to_size_sx(struct bpf_reg_state *reg, int size)
 	s64_min = min(init_s64_max, init_s64_min);
 
 	/* both of s64_max/s64_min positive or negative */
-	if (s64_max >= 0 == s64_min >= 0) {
+	if ((s64_max >= 0) == (s64_min >= 0)) {
 		reg->smin_value = reg->s32_min_value = s64_min;
 		reg->smax_value = reg->s32_max_value = s64_max;
 		reg->umin_value = reg->u32_min_value = s64_min;
@@ -5962,7 +5962,7 @@ static void coerce_subreg_to_size_sx(struct bpf_reg_state *reg, int size)
 	s32_max = max(init_s32_max, init_s32_min);
 	s32_min = min(init_s32_max, init_s32_min);
 
-	if (s32_min >= 0 == s32_max >= 0) {
+	if ((s32_min >= 0) == (s32_max >= 0)) {
 		reg->s32_min_value = s32_min;
 		reg->s32_max_value = s32_max;
 		reg->u32_min_value = (u32)s32_min;
Signature · 1 line
2.34.1
#3 lore raw
In reply to Yonghong Song
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 14 lines · On Thu, 27 Jul 2023 22:57:40 -0700 you wrote:

The kernel test robot reported compilation warnings when -Wparentheses is added to KBUILD_CFLAGS with gcc compiler. The following is the error message:

.../bpf-next/kernel/bpf/verifier.c: In function ‘coerce_reg_to_size_sx’:
.../bpf-next/kernel/bpf/verifier.c:5901:14:

    error: suggest parentheses around comparison in operand of ‘==’ [-Werror=parentheses]
    if (s64_max >= 0 == s64_min >= 0) {
        ~~~~~~~~^~~~
  .../bpf-next/kernel/bpf/verifier.c: In function ‘coerce_subreg_to_size_sx’:
  .../bpf-next/kernel/bpf/verifier.c:5965:14:
    error: suggest parentheses around comparison in operand of ‘==’ [-Werror=parentheses]
    if (s32_min >= 0 == s32_max >= 0) {
        ~~~~~~~~^~~~
Quoted 1 line

[...]

Here is the summary with links:
- [bpf-next,1/2] bpf: Fix compilation warning with -Wparentheses

    https://git.kernel.org/bpf/bpf-next/c/09fedc731874
  - [bpf-next,2/2] selftests/bpf: Enable test test_progs-cpuv4 for gcc build kernel
    https://git.kernel.org/bpf/bpf-next/c/a76584fc9ff6

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html