Skip to contentSkip to conversation

bpf, x64: Check imm32 first at BPF_CALL in do_jit()

PATCH Superseded Leon Hwang 1 participant 2 messages Sep 13, 2023 – Sep 14, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Leon Hwang

author
#0 lore raw

PATCH 1/1 bpf, x64: Check imm32 first at BPF_CALL in do_jit()

It's unnecessary to check imm32 in both 'if' and 'else'.

It's better to check it first.

Meanwhile, refactor the code for 'offs' calculation.

---
 arch/x86/net/bpf_jit_comp.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
arch/x86/net/bpf_jit_comp.c +7 -9
index 2846c21d75bfa..f06e9a48afe52 100644
@@ -1629,17 +1629,15 @@ st:			if (is_imm8(insn->off))
 		case BPF_JMP | BPF_CALL: {
 			int offs;
 
+			if (!imm32)
+				return -EINVAL;
+
 			func = (u8 *) __bpf_call_base + imm32;
-			if (tail_call_reachable) {
+			if (tail_call_reachable)
 				RESTORE_TAIL_CALL_CNT(bpf_prog->aux->stack_depth);
-				if (!imm32)
-					return -EINVAL;
-				offs = 7 + x86_call_depth_emit_accounting(&prog, func);
-			} else {
-				if (!imm32)
-					return -EINVAL;
-				offs = x86_call_depth_emit_accounting(&prog, func);
-			}
+
+			offs = (tail_call_reachable ? 7 : 0);
+			offs += x86_call_depth_emit_accounting(&prog, func);
 			if (emit_call(&prog, func, image + addrs[i - 1] + offs))
 				return -EINVAL;
 			break;

base-commit: e4f30c666b4933dcd140d5110073aa01a69d2b39

Signature · 1 line
2.41.0
author
#1 lore raw
Quoted 26 lines · Leon Hwang

It's unnecessary to check imm32 in both 'if' and 'else'.

It's better to check it first.

Meanwhile, refactor the code for 'offs' calculation.

---
 arch/x86/net/bpf_jit_comp.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
arch/x86/net/bpf_jit_comp.c +4 -1
index 2846c21d75bfa..f06e9a48afe52 100644
@@ -1629,17 +1629,15 @@ st:			if (is_imm8(insn->off))
 		case BPF_JMP | BPF_CALL: {
 			int offs;
 
+			if (!imm32)
+				return -EINVAL;
+
 			func = (u8 *) __bpf_call_base + imm32;
-			if (tail_call_reachable) {
+			if (tail_call_reachable)
 				RESTORE_TAIL_CALL_CNT(bpf_prog->aux->stack_depth);

arch/x86/net/bpf_jit_comp.c st: if (is_imm8(insn->off)) · line 1639

Quoted 10 lines
arch/x86/net/bpf_jit_comp.c +2 -8
-				if (!imm32)
-					return -EINVAL;
-				offs = 7 + x86_call_depth_emit_accounting(&prog, func);
-			} else {
-				if (!imm32)
-					return -EINVAL;
-				offs = x86_call_depth_emit_accounting(&prog, func);
-			}
+
+			offs = (tail_call_reachable ? 7 : 0);

This 7 is the byte count of RESTORE_TAIL_CALL_CNT instructions.

I'll update it to RESTORE_TAIL_CALL_CNT_INSN_SIZE in v2 patch.

Thanks,
Leon

Quoted 6 lines
arch/x86/net/bpf_jit_comp.c +1 -0
+			offs += x86_call_depth_emit_accounting(&prog, func);
 			if (emit_call(&prog, func, image + addrs[i - 1] + offs))
 				return -EINVAL;
 			break;

base-commit: e4f30c666b4933dcd140d5110073aa01a69d2b39