Skip to content

Commit c697289

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
bpf: move is_branch_taken() down
Move is_branch_taken() slightly down. In subsequent patched we'll need both flip_opcode() and is_pkt_ptr_branch_taken() for is_branch_taken(), but instead of sprinkling forward declarations around, it makes more sense to move is_branch_taken() lower below is_pkt_ptr_branch_taken(), and also keep it closer to very tightly related reg_set_min_max(), as they are two critical parts of the same SCALAR range tracking logic. Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent c315342 commit c697289

File tree

1 file changed

+42
-42
lines changed

1 file changed

+42
-42
lines changed

kernel/bpf/verifier.c

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14358,48 +14358,6 @@ static int is_branch64_taken(struct bpf_reg_state *reg1, struct bpf_reg_state *r
1435814358
return -1;
1435914359
}
1436014360

14361-
/* compute branch direction of the expression "if (<reg1> opcode <reg2>) goto target;"
14362-
* and return:
14363-
* 1 - branch will be taken and "goto target" will be executed
14364-
* 0 - branch will not be taken and fall-through to next insn
14365-
* -1 - unknown. Example: "if (reg1 < 5)" is unknown when register value
14366-
* range [0,10]
14367-
*/
14368-
static int is_branch_taken(struct bpf_reg_state *reg1, struct bpf_reg_state *reg2,
14369-
u8 opcode, bool is_jmp32)
14370-
{
14371-
struct tnum reg2_tnum = is_jmp32 ? tnum_subreg(reg2->var_off) : reg2->var_off;
14372-
u64 val;
14373-
14374-
if (!tnum_is_const(reg2_tnum))
14375-
return -1;
14376-
val = reg2_tnum.value;
14377-
14378-
if (__is_pointer_value(false, reg1)) {
14379-
if (!reg_not_null(reg1))
14380-
return -1;
14381-
14382-
/* If pointer is valid tests against zero will fail so we can
14383-
* use this to direct branch taken.
14384-
*/
14385-
if (val != 0)
14386-
return -1;
14387-
14388-
switch (opcode) {
14389-
case BPF_JEQ:
14390-
return 0;
14391-
case BPF_JNE:
14392-
return 1;
14393-
default:
14394-
return -1;
14395-
}
14396-
}
14397-
14398-
if (is_jmp32)
14399-
return is_branch32_taken(reg1, reg2, opcode);
14400-
return is_branch64_taken(reg1, reg2, opcode);
14401-
}
14402-
1440314361
static int flip_opcode(u32 opcode)
1440414362
{
1440514363
/* How can we transform "a <op> b" into "b <op> a"? */
@@ -14461,6 +14419,48 @@ static int is_pkt_ptr_branch_taken(struct bpf_reg_state *dst_reg,
1446114419
return -1;
1446214420
}
1446314421

14422+
/* compute branch direction of the expression "if (<reg1> opcode <reg2>) goto target;"
14423+
* and return:
14424+
* 1 - branch will be taken and "goto target" will be executed
14425+
* 0 - branch will not be taken and fall-through to next insn
14426+
* -1 - unknown. Example: "if (reg1 < 5)" is unknown when register value
14427+
* range [0,10]
14428+
*/
14429+
static int is_branch_taken(struct bpf_reg_state *reg1, struct bpf_reg_state *reg2,
14430+
u8 opcode, bool is_jmp32)
14431+
{
14432+
struct tnum reg2_tnum = is_jmp32 ? tnum_subreg(reg2->var_off) : reg2->var_off;
14433+
u64 val;
14434+
14435+
if (!tnum_is_const(reg2_tnum))
14436+
return -1;
14437+
val = reg2_tnum.value;
14438+
14439+
if (__is_pointer_value(false, reg1)) {
14440+
if (!reg_not_null(reg1))
14441+
return -1;
14442+
14443+
/* If pointer is valid tests against zero will fail so we can
14444+
* use this to direct branch taken.
14445+
*/
14446+
if (val != 0)
14447+
return -1;
14448+
14449+
switch (opcode) {
14450+
case BPF_JEQ:
14451+
return 0;
14452+
case BPF_JNE:
14453+
return 1;
14454+
default:
14455+
return -1;
14456+
}
14457+
}
14458+
14459+
if (is_jmp32)
14460+
return is_branch32_taken(reg1, reg2, opcode);
14461+
return is_branch64_taken(reg1, reg2, opcode);
14462+
}
14463+
1446414464
/* Adjusts the register min/max values in the case that the dst_reg is the
1446514465
* variable register that we are working on, and src_reg is a constant or we're
1446614466
* simply doing a BPF_K check.

0 commit comments

Comments
 (0)