Skip to contentSkip to conversation

bpf, riscv: Fix tail call count off by one in RV32 BPF JIT

PATCH Applied Luke Nelson 3 participants 3 messages Apr 21, 2020 – Apr 23, 2020

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf, riscv: Fix tail call count off by one in RV32 BPF JIT

This patch fixes an off by one error in the RV32 JIT handling for BPF tail call. Currently, the code decrements TCC before checking if it is less than zero. This limits the maximum number of tail calls to 32 instead of 33 as in other JITs. The fix is to instead check the old value of TCC before decrementing.

Fixes 5f316b65e99f ("riscv, bpf: Add RV32G eBPF JIT")
---
 arch/riscv/net/bpf_jit_comp32.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/riscv/net/bpf_jit_comp32.c +3 -2
index 302934177760..11083d4d5f2d 100644
@@ -770,12 +770,13 @@ static int emit_bpf_tail_call(int insn, struct rv_jit_context *ctx)
 	emit_bcc(BPF_JGE, lo(idx_reg), RV_REG_T1, off, ctx);
 
 	/*
-	 * if ((temp_tcc = tcc - 1) < 0)
+	 * temp_tcc = tcc - 1;
+	 * if (tcc < 0)
 	 *   goto out;
 	 */
 	emit(rv_addi(RV_REG_T1, RV_REG_TCC, -1), ctx);
 	off = (tc_ninsn - (ctx->ninsns - start_insn)) << 2;
-	emit_bcc(BPF_JSLT, RV_REG_T1, RV_REG_ZERO, off, ctx);
+	emit_bcc(BPF_JSLT, RV_REG_TCC, RV_REG_ZERO, off, ctx);
 
 	/*
 	 * prog = array->ptrs[index];
Signature · 1 line
2.17.1
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 19 lines · Luke Nelson

This patch fixes an off by one error in the RV32 JIT handling for BPF tail call. Currently, the code decrements TCC before checking if it is less than zero. This limits the maximum number of tail calls to 32 instead of 33 as in other JITs. The fix is to instead check the old value of TCC before decrementing.

Fixes 5f316b65e99f ("riscv, bpf: Add RV32G eBPF JIT")
---
 arch/riscv/net/bpf_jit_comp32.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/riscv/net/bpf_jit_comp32.c +0 -0
index 302934177760..11083d4d5f2d 100644
@@ -770,12 +770,13 @@ static int emit_bpf_tail_call(int insn, struct rv_jit_context *ctx)
        emit_bcc(BPF_JGE, lo(idx_reg), RV_REG_T1, off, ctx);
 

arch/riscv/net/bpf_jit_comp32.c static int emit_bpf_tail_call(int insn, struct rv_jit_context *ctx) · line 779

Quoted 10 lines
arch/riscv/net/bpf_jit_comp32.c +3 -2
        /*
-        * if ((temp_tcc = tcc - 1) < 0)
+        * temp_tcc = tcc - 1;
+        * if (tcc < 0)
         *   goto out;
         */
        emit(rv_addi(RV_REG_T1, RV_REG_TCC, -1), ctx);
        off = (tc_ninsn - (ctx->ninsns - start_insn)) << 2;
-       emit_bcc(BPF_JSLT, RV_REG_T1, RV_REG_ZERO, off, ctx);
+       emit_bcc(BPF_JSLT, RV_REG_TCC, RV_REG_ZERO, off, ctx);

Nice catch!

#2 lore raw
In reply to Luke Nelson
Quoted 9 lines · Luke Nelson

This patch fixes an off by one error in the RV32 JIT handling for BPF tail call. Currently, the code decrements TCC before checking if it is less than zero. This limits the maximum number of tail calls to 32 instead of 33 as in other JITs. The fix is to instead check the old value of TCC before decrementing.

Fixes 5f316b65e99f ("riscv, bpf: Add RV32G eBPF JIT")

Applied. Thanks