]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
LoongArch: BPF: Fix off-by-one error in build_prologue()
authorHengqi Chen <hengqi.chen@gmail.com>
Sun, 30 Mar 2025 08:31:09 +0000 (16:31 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 10 Apr 2025 12:33:41 +0000 (14:33 +0200)
commit 7e2586991e36663c9bc48c828b83eab180ad30a9 upstream.

Vincent reported that running BPF progs with tailcalls on LoongArch
causes kernel hard lockup. Debugging the issues shows that the JITed
image missing a jirl instruction at the end of the epilogue.

There are two passes in JIT compiling, the first pass set the flags and
the second pass generates JIT code based on those flags. With BPF progs
mixing bpf2bpf and tailcalls, build_prologue() generates N insns in the
first pass and then generates N+1 insns in the second pass. This makes
epilogue_offset off by one and we will jump to some unexpected insn and
cause lockup. Fix this by inserting a nop insn.

Cc: stable@vger.kernel.org
Fixes: 5dc615520c4d ("LoongArch: Add BPF JIT support")
Fixes: bb035ef0cc91 ("LoongArch: BPF: Support mixing bpf2bpf and tailcalls")
Reported-by: Vincent Li <vincent.mc.li@gmail.com>
Tested-by: Vincent Li <vincent.mc.li@gmail.com>
Closes: https://lore.kernel.org/loongarch/CAK3+h2w6WESdBN3UCr3WKHByD7D6Q_Ve1EDAjotVrnx6Or_c8g@mail.gmail.com/
Closes: https://lore.kernel.org/bpf/CAK3+h2woEjG_N=-XzqEGaAeCmgu2eTCUc7p6bP4u8Q+DFHm-7g@mail.gmail.com/
Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
arch/loongarch/net/bpf_jit.c
arch/loongarch/net/bpf_jit.h

index 2567916370b4e801949e8743ad5f38ee0dafa241..7a8d3c1487066f0c63dab2cd8b0f7df6e6cbc25f 100644 (file)
@@ -142,6 +142,8 @@ static void build_prologue(struct jit_ctx *ctx)
         */
        if (seen_tail_call(ctx) && seen_call(ctx))
                move_reg(ctx, TCC_SAVED, REG_TCC);
+       else
+               emit_insn(ctx, nop);
 
        ctx->stack_size = stack_adjust;
 }
index 684b22f54ccc63dc47d2d6e02dd02e7f64e7e30c..d9421469a731b96785279773b2a69596e8078820 100644 (file)
@@ -25,6 +25,11 @@ struct jit_data {
        struct jit_ctx ctx;
 };
 
+static inline void emit_nop(union loongarch_instruction *insn)
+{
+       insn->word = INSN_NOP;
+}
+
 #define emit_insn(ctx, func, ...)                                              \
 do {                                                                           \
        if (ctx->image != NULL) {                                               \