Skip to contentSkip to conversation

bpf: clean up unnecessary conditional judgments

PATCH In review Jackie Liu 3 participants 3 messages Dec 22, 2021 – Dec 23, 2021

Subsystems (2)
lore
Share & tools
lore

Jackie Liu

author
#0 lore raw

PATCH 1/1 bpf: clean up unnecessary conditional judgments

From: Jackie Liu <[email protected]>

s32 is always true regardless of the values of its operands. let's cleanup.

Fixes e572ff80f05c ("bpf: Make 32->64 bounds propagation slightly more robust")
---
 kernel/bpf/verifier.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
kernel/bpf/verifier.c +1 -7
index b532f1058d35..43812ee58304 100644
@@ -1366,11 +1366,6 @@ static void __reg_bound_offset(struct bpf_reg_state *reg)
 	reg->var_off = tnum_or(tnum_clear_subreg(var64_off), var32_off);
 }
 
-static bool __reg32_bound_s64(s32 a)
-{
-	return a >= 0 && a <= S32_MAX;
-}
-
 static void __reg_assign_32_into_64(struct bpf_reg_state *reg)
 {
 	reg->umin_value = reg->u32_min_value;
@@ -1380,8 +1375,7 @@ static void __reg_assign_32_into_64(struct bpf_reg_state *reg)
 	 * be positive otherwise set to worse case bounds and refine later
 	 * from tnum.
 	 */
-	if (__reg32_bound_s64(reg->s32_min_value) &&
-	    __reg32_bound_s64(reg->s32_max_value)) {
+	if (reg->s32_min_value >= 0 && reg->s32_max_value >= 0) {
 		reg->smin_value = reg->s32_min_value;
 		reg->smax_value = reg->s32_max_value;
 	} else {
Signature · 1 line
2.25.1
#1 lore raw
Quoted 17 lines · Jackie Liu

From: Jackie Liu <[email protected]>

s32 is always true regardless of the values of its operands. let's cleanup.

Fixes e572ff80f05c ("bpf: Make 32->64 bounds propagation slightly more robust")
---
  kernel/bpf/verifier.c | 8 +-------
  1 file changed, 1 insertion(+), 7 deletions(-)
kernel/bpf/verifier.c +0 -0
index b532f1058d35..43812ee58304 100644

kernel/bpf/verifier.c static void __reg_bound_offset(struct bpf_reg_state *reg) · line 1372

Quoted 7 lines
kernel/bpf/verifier.c +0 -4
@@ -1366,11 +1366,6 @@ static void __reg_bound_offset(struct bpf_reg_state *reg)
  	reg->var_off = tnum_or(tnum_clear_subreg(var64_off), var32_off);
  }
 
-static bool __reg32_bound_s64(s32 a)
-{
-	return a >= 0 && a <= S32_MAX;
-}

The following bpf tree commit triggered the above change:

commit e572ff80f05c33cd0cb4860f864f5c9c044280b6

Author: Daniel Borkmann <[email protected]>
Date:   Wed Dec 15 22:28:48 2021 +0000
    bpf: Make 32->64 bounds propagation slightly more robust

There is no need to fix bpf tree since as this patch just a cleanup patch and there is no functionality change.

Maybe wait for the above patch available in bpf-next and submit this patch again? Daniel, do you have any suggestions for this patch?

Quoted 14 lines
kernel/bpf/verifier.c +1 -3
-
  static void __reg_assign_32_into_64(struct bpf_reg_state *reg)
  {
  	reg->umin_value = reg->u32_min_value;
@@ -1380,8 +1375,7 @@ static void __reg_assign_32_into_64(struct bpf_reg_state *reg)
  	 * be positive otherwise set to worse case bounds and refine later
  	 * from tnum.
  	 */
-	if (__reg32_bound_s64(reg->s32_min_value) &&
-	    __reg32_bound_s64(reg->s32_max_value)) {
+	if (reg->s32_min_value >= 0 && reg->s32_max_value >= 0) {
  		reg->smin_value = reg->s32_min_value;
  		reg->smax_value = reg->s32_max_value;
  	} else {
#2 lore raw
In reply to Jackie Liu
Quoted 18 lines · Jackie Liu

From: Jackie Liu <[email protected]>

s32 is always true regardless of the values of its operands. let's cleanup.

Fixes e572ff80f05c ("bpf: Make 32->64 bounds propagation slightly more robust")
---
 kernel/bpf/verifier.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
kernel/bpf/verifier.c +0 -0
index b532f1058d35..43812ee58304 100644

kernel/bpf/verifier.c static void __reg_bound_offset(struct bpf_reg_state *reg) · line 1373

Quoted 8 lines
kernel/bpf/verifier.c +0 -5
@@ -1366,11 +1366,6 @@ static void __reg_bound_offset(struct bpf_reg_state *reg)
        reg->var_off = tnum_or(tnum_clear_subreg(var64_off), var32_off);
 }
 
-static bool __reg32_bound_s64(s32 a)
-{
-       return a >= 0 && a <= S32_MAX;
-}
-

The code is the best documentation.
Here it clearly describes the intent.
Please ignore compiler warnings.