Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.241 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796118698; b=b5k7jmXqg7jfB2tH1oqDZ+Ep6BnXf76TiFyqQX/tBH8Ff/GD2qpNkIvydsnacdPkvFBz EQJ8pKC/8cxs5D/B7FxnzChKRBNLiQIdh9HCAUDFdllV6d/nMljG3BUuzYp8hT2SJIwcn DJwzJbKU7BjriFXjP/+yEF8xqocQ18JNk9zoNJOzQ3/W7+vgc1lq7RsXHSU4X8ZOXQn3D 22DtpFNgONVsuWxQe5Up0jVfCxcskqXHof1ERpD8Pil4GqPDtwy0DBthcrkNnxRl7w5lC 6eh+Pbk/8BUqHTdHpqbVCKy0sH/jP6LlLuIW8qSN9ijbQ5S0zf7NF7t3VUaQ6rNwnSg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796118698; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=I+Jvl+HKbqRkemzw6LVQ8apVN6McqRO/NKmXupVkOy4=; b=ljUL7C+5TiJqOX9IxAjiUO3dnizBUwlYsRhv1OIaTyf+tY3pJ/sZ1m04oNab2lT44Dio rz3qVaG7axpjootd/sxOxgyFycHAkpEKKAb0v9Dji10GP/h9yLle8BhGei92PQwaZGoPr 7NFJlZNd+oKQtsGzCxJkI7TZMY+wMmevvrsgzZF3yRGwyjNAaEd5myvx24Zn5orkdtM/d nw7V3REsasmnh0sfMKdF15vgiyvQwKU7Dlb+TUM/P0UI7lG7hxoeziWb5/28jtbe09Ca7 z8jvWoLTHIRv7bd67OEni7Vv35xXX5vLqCbq1fuuFxHzTyIcwcNhhUfy8y385pBi4lw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=ujMQ8JIw; arc=none smtp.remote-ip=95.215.58.241 Received: from mta1.migadu.com (out-241.mta1.migadu.com [95.215.58.241]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7D480606A338 for ; Tue, 1 Dec 2026 09:51:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7D480606A338 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=ujMQ8JIw X-Envelope-To: bpf@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=I749RGt87low8CJCaMguZoJXex0ME8xehhb5RZLCYS0=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796118692; v=1; x=1796723492; b=ujMQ8JIwzHBrfdefbB9HnxBw23wklQQsncHlUKx69ibOcowlPjGLgoNY6CDGBS5MUMxOfk/M 6+wo2ysZGRpoTdsxOENzjZfyoBtFOEH1AZtxzyEsKy0uAR/XAwz8ZHWRJPllqd3d8O2KsaEODDx VUg+DZmabdrsu21CNCgnctWA= X-Envelope-To: bpf@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id a794800d614123c8; Tue, 01 Dec 2026 09:51:32 +0000 X-Mizu-Trace-ID: a794800d614123c8 X-Migadu-Flow: FLOW_OUT From: George Guo To: chenhuacai@kernel.org, yangtiezhu@loongson.cn Cc: hengqi.chen@gmail.com, guodongtai@kylinos.cn, bpf@vger.kernel.org, loongarch@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v4 3/6] LoongArch: BPF: Add support for exceptions Date: Tue, 1 Dec 2026 01:51:03 -0800 Message-ID: <20261201095106.468000-4-dongtai.guo@linux.dev> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261201095106.468000-1-dongtai.guo@linux.dev> References: <20261201095106.468000-1-dongtai.guo@linux.dev> X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: George Guo In order to implement JIT support for BPF exceptions, bpf_throw() needs to find the frame of the exception boundary program by walking the stack, and then call the exception callback with the stack pointer and frame pointer of that frame. The stack walking is done by arch_bpf_stack_walk(), which was added by commit e7103e5e3bde ("LoongArch: BPF: Add arch_bpf_stack_walk() implementation") and only works with the ORC unwinder, so make bpf_jit_supports_exceptions() return true only if CONFIG_UNWINDER_ORC is enabled. The exception callback does not set up a frame of its own: it takes the frame pointer of the boundary program from its third argument (a2) and reuses that frame, so its epilogue restores the callee-saved registers of the boundary program and returns to the caller of the boundary program. In order to keep the same stack layout for the boundary program and the callback, always reserve the $s6 slot for them, just like for arena programs. bpf_throw() does not return, so the C functions it unwinds never restore the callee-saved registers they use. The JIT does not use $s7 and $s8 otherwise, but save and restore them as well for these programs, so that all callee-saved registers are restored when returning to the caller of the boundary program. This is similar to commit 22fc0e80aeb5 ("bpf, arm64: support exceptions"). Signed-off-by: George Guo --- arch/loongarch/net/bpf_jit.c | 82 +++++++++++++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 6 deletions(-) diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c index eafa8e8d3e15..04b40558a859 100644 --- a/arch/loongarch/net/bpf_jit.c +++ b/arch/loongarch/net/bpf_jit.c @@ -121,27 +121,45 @@ static void prepare_bpf_tail_call_cnt(struct jit_ctx *ctx, int *store_offset) * +-------------------------+ * | tcc_ptr | * +-------------------------+ - * | arena | - * | (optional) | + * | $s6 (arena/exception) | + * | (optional) | + * +-------------------------+ + * | $s7 | + * +-------------------------+ + * | $s8 | + * | (exception, optional) | * +-------------------------+ <--BPF_REG_FP * | prog->aux->stack_depth | * | (optional) | * current $sp -------------> +-------------------------+ * low * - * The area from $ra down to the optional arena slot is allocated on its own + * The area from $ra down to the optional $s8 slot is allocated on its own * and addressed with small offsets from $sp or $fp. The BPF stack below * it can be up to MAX_BPF_STACK_JIT bytes plus the may_goto slots, which * do not fit the 12-bit immediate of addi.d, ld.d and st.d. */ static int jit_save_area_size(const struct jit_ctx *ctx) { + const struct bpf_prog *prog = ctx->prog; + /* + * Exception boundary and callback programs must agree on the frame + * layout: the callback reuses the boundary's frame to restore its + * callee-saved registers, so the s6, s7 and s8 slots are always + * reserved for them. + */ + const bool is_exception_prog = prog->aux->exception_boundary || + prog->aux->exception_cb; /* To store ra, fp, s0, s1, s2, s3, s4, s5, tcc and tcc_ptr */ int size = sizeof(long) * 10; - if (ctx->arena_vm_start) + if (ctx->arena_vm_start || is_exception_prog) size += sizeof(long); + /* To store s7 and s8 */ + if (is_exception_prog) + size += sizeof(long) * 2; + return round_up(size, 16); } @@ -150,6 +168,8 @@ static void build_prologue(struct jit_ctx *ctx) int i, save_size, store_offset, bpf_stack_adjust; const struct bpf_prog *prog = ctx->prog; const bool is_main_prog = !bpf_is_subprog(prog); + const bool is_exception_prog = prog->aux->exception_boundary || + prog->aux->exception_cb; bpf_stack_adjust = round_up(ctx->prog->aux->stack_depth, 16); save_size = jit_save_area_size(ctx); @@ -172,6 +192,19 @@ static void build_prologue(struct jit_ctx *ctx) if (is_main_prog) emit_insn(ctx, addid, REG_TCC, LOONGARCH_GPR_ZERO, 0); + if (prog->aux->exception_cb) { + /* + * The exception callback receives the boundary program's frame + * pointer as its third argument (a2). Reuse that frame so the + * (FP-anchored) epilogue restores the boundary's callee-saved + * registers and returns to the boundary's caller. The boundary + * already saved them, so nothing is pushed here. + */ + move_reg(ctx, LOONGARCH_GPR_FP, LOONGARCH_GPR_A2); + emit_insn(ctx, addid, LOONGARCH_GPR_SP, LOONGARCH_GPR_FP, -save_size); + goto setup_bpf_fp; + } + emit_insn(ctx, addid, LOONGARCH_GPR_SP, LOONGARCH_GPR_SP, -save_size); store_offset = save_size - sizeof(long); @@ -200,13 +233,28 @@ static void build_prologue(struct jit_ctx *ctx) prepare_bpf_tail_call_cnt(ctx, &store_offset); - if (ctx->arena_vm_start) { + if (ctx->arena_vm_start || is_exception_prog) { store_offset -= sizeof(long); emit_insn(ctx, std, REG_ARENA, LOONGARCH_GPR_SP, store_offset); } + /* + * bpf_throw() does not return, so the C functions it unwinds never + * restore the callee-saved registers they use. The JIT does not use + * s7 and s8, but save them here too, so the exception callback can + * restore all of them before returning to the boundary's caller. + */ + if (is_exception_prog) { + store_offset -= sizeof(long); + emit_insn(ctx, std, LOONGARCH_GPR_S7, LOONGARCH_GPR_SP, store_offset); + + store_offset -= sizeof(long); + emit_insn(ctx, std, LOONGARCH_GPR_S8, LOONGARCH_GPR_SP, store_offset); + } + emit_insn(ctx, addid, LOONGARCH_GPR_FP, LOONGARCH_GPR_SP, save_size); +setup_bpf_fp: ctx->stack_size = save_size; if (ctx->priv_sp_used) { @@ -242,6 +290,9 @@ static void __build_epilogue(struct jit_ctx *ctx, bool is_tail_call) { int save_size = jit_save_area_size(ctx); int load_offset; + const struct bpf_prog *prog = ctx->prog; + const bool is_exception_prog = prog->aux->exception_boundary || + prog->aux->exception_cb; /* * Release the BPF stack, if it is on the kernel stack, by deriving @@ -282,11 +333,19 @@ static void __build_epilogue(struct jit_ctx *ctx, bool is_tail_call) /* Skip the unused local 'tcc_ptr' slot to align with arena */ load_offset -= sizeof(long); - if (ctx->arena_vm_start) { + if (ctx->arena_vm_start || is_exception_prog) { load_offset -= sizeof(long); emit_insn(ctx, ldd, REG_ARENA, LOONGARCH_GPR_SP, load_offset); } + if (is_exception_prog) { + load_offset -= sizeof(long); + emit_insn(ctx, ldd, LOONGARCH_GPR_S7, LOONGARCH_GPR_SP, load_offset); + + load_offset -= sizeof(long); + emit_insn(ctx, ldd, LOONGARCH_GPR_S8, LOONGARCH_GPR_SP, load_offset); + } + emit_insn(ctx, addid, LOONGARCH_GPR_SP, LOONGARCH_GPR_SP, save_size); if (!is_tail_call) { @@ -2669,6 +2728,17 @@ bool bpf_jit_supports_private_stack(void) return true; } +bool bpf_jit_supports_exceptions(void) +{ + /* + * Walking kernel and BPF frames from within bpf_throw() relies on + * arch_bpf_stack_walk(), which is only implemented for the ORC + * unwinder. ORC reports each frame's stack and frame pointer and + * walks JITed BPF frames via its frame-pointer fallback. + */ + return IS_ENABLED(CONFIG_UNWINDER_ORC); +} + /* Indicate the JIT backend supports mixing bpf2bpf and tailcalls. */ bool bpf_jit_supports_subprog_tailcalls(void) { -- 2.53.0