* [PATCH v3 1/5] LoongArch: BPF: Add support for private stack
2026-11-26 3:40 [PATCH v3 0/5] LoongArch: BPF: Add private stack, exceptions, large stack and arena support George Guo
@ 2026-11-26 3:40 ` George Guo
2026-11-26 3:40 ` [PATCH v3 2/5] LoongArch: BPF: Add support for exceptions George Guo
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: George Guo @ 2026-11-26 3:40 UTC (permalink / raw)
To: chenhuacai, yangtiezhu
Cc: hengqi.chen, guodongtai, bpf, loongarch, linux-kernel
From: George Guo <guodongtai@kylinos.cn>
Currently, the BPF stack of a program always lives on the kernel stack.
If the JIT supports private stack, the verifier marks a deep tracing or
struct_ops program with jits_use_priv_stack, so that its BPF stack can
be moved into a per-CPU allocation to reduce the kernel stack pressure.
Allocate a per-CPU private stack with guard regions to detect overflow
and underflow, and check the guards in bpf_jit_free(). In the prologue,
use $r21 to locate the current CPU's private stack and set the BPF frame
pointer, instead of allocating the BPF stack on the kernel stack.
This is similar to commit 6c17a882d380 ("bpf, arm64: JIT support for
private stack").
Signed-off-by: George Guo <guodongtai@kylinos.cn>
---
arch/loongarch/net/bpf_jit.c | 111 ++++++++++++++++++++++++++++++++++-
arch/loongarch/net/bpf_jit.h | 1 +
2 files changed, 109 insertions(+), 3 deletions(-)
diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
index 567729921d6d..3ff15b64925f 100644
--- a/arch/loongarch/net/bpf_jit.c
+++ b/arch/loongarch/net/bpf_jit.c
@@ -30,6 +30,10 @@
#define REG_ARENA LOONGARCH_GPR_S6 /* For storing arena_vm_start */
#define BPF_TAIL_CALL_CNT_PTR_STACK_OFF(stack) (round_up(stack, 16) - 80)
+/* Memory size/value to protect private stack overflow/underflow */
+#define PRIV_STACK_GUARD_SZ 16
+#define PRIV_STACK_GUARD_VAL 0xEB9F12345678eb9fULL
+
static const int regmap[] = {
/* return value from in-kernel function, and exit value for eBPF program */
[BPF_REG_0] = LOONGARCH_GPR_A5,
@@ -50,6 +54,15 @@ static const int regmap[] = {
[BPF_REG_AX] = LOONGARCH_GPR_T0,
};
+static void emit_percpu_ptr(struct jit_ctx *ctx, u8 dst, void __percpu *ptr)
+{
+ move_imm(ctx, dst, (__force long)ptr);
+#ifdef CONFIG_SMP
+ /* dst += __my_cpu_offset, held in $r21 */
+ emit_insn(ctx, addd, dst, dst, LOONGARCH_GPR_U0);
+#endif
+}
+
static void prepare_bpf_tail_call_cnt(struct jit_ctx *ctx, int *store_offset)
{
const struct bpf_prog *prog = ctx->prog;
@@ -133,7 +146,14 @@ static void build_prologue(struct jit_ctx *ctx)
stack_adjust += sizeof(long);
stack_adjust = round_up(stack_adjust, 16);
- stack_adjust += bpf_stack_adjust;
+
+ /*
+ * When a private stack is used the BPF stack lives in a per-CPU
+ * allocation rather than on the kernel stack, so only the non-BPF
+ * part is reserved here.
+ */
+ if (!ctx->priv_sp_used)
+ stack_adjust += bpf_stack_adjust;
/*
* Save the original return address to a temporary register to prevent
@@ -188,8 +208,16 @@ static void build_prologue(struct jit_ctx *ctx)
emit_insn(ctx, addid, LOONGARCH_GPR_FP, LOONGARCH_GPR_SP, stack_adjust);
- if (bpf_stack_adjust)
+ if (ctx->priv_sp_used) {
+ /* Point the BPF frame pointer at the top of this CPU's private stack */
+ void __percpu *priv_stack_ptr;
+
+ priv_stack_ptr = prog->aux->priv_stack_ptr + PRIV_STACK_GUARD_SZ;
+ emit_percpu_ptr(ctx, regmap[BPF_REG_FP], priv_stack_ptr);
+ emit_insn(ctx, addid, regmap[BPF_REG_FP], regmap[BPF_REG_FP], bpf_stack_adjust);
+ } else if (bpf_stack_adjust) {
emit_insn(ctx, addid, regmap[BPF_REG_FP], LOONGARCH_GPR_SP, bpf_stack_adjust);
+ }
ctx->stack_size = stack_adjust;
@@ -2280,6 +2308,39 @@ int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
return ret < 0 ? ret : ret * LOONGARCH_INSN_SIZE;
}
+static void priv_stack_init_guard(void __percpu *priv_stack_ptr, int alloc_size)
+{
+ int cpu, underflow_idx = (alloc_size - PRIV_STACK_GUARD_SZ) >> 3;
+ u64 *stack_ptr;
+
+ for_each_possible_cpu(cpu) {
+ stack_ptr = per_cpu_ptr(priv_stack_ptr, cpu);
+ stack_ptr[0] = PRIV_STACK_GUARD_VAL;
+ stack_ptr[1] = PRIV_STACK_GUARD_VAL;
+ stack_ptr[underflow_idx] = PRIV_STACK_GUARD_VAL;
+ stack_ptr[underflow_idx + 1] = PRIV_STACK_GUARD_VAL;
+ }
+}
+
+static void priv_stack_check_guard(void __percpu *priv_stack_ptr, int alloc_size,
+ struct bpf_prog *prog)
+{
+ int cpu, underflow_idx = (alloc_size - PRIV_STACK_GUARD_SZ) >> 3;
+ u64 *stack_ptr;
+
+ for_each_possible_cpu(cpu) {
+ stack_ptr = per_cpu_ptr(priv_stack_ptr, cpu);
+ if (stack_ptr[0] != PRIV_STACK_GUARD_VAL ||
+ stack_ptr[1] != PRIV_STACK_GUARD_VAL ||
+ stack_ptr[underflow_idx] != PRIV_STACK_GUARD_VAL ||
+ stack_ptr[underflow_idx + 1] != PRIV_STACK_GUARD_VAL) {
+ pr_err("BPF private stack overflow/underflow detected for prog %s\n",
+ bpf_jit_get_prog_name(prog));
+ break;
+ }
+ }
+}
+
struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog)
{
bool extra_pass = false;
@@ -2288,7 +2349,9 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
struct jit_ctx ctx;
struct jit_data *jit_data;
struct bpf_binary_header *header;
- struct bpf_binary_header *ro_header;
+ struct bpf_binary_header *ro_header = NULL;
+ void __percpu *priv_stack_ptr = NULL;
+ int priv_stack_alloc_sz;
/*
* If BPF JIT was not enabled then we must fall back to
@@ -2304,6 +2367,22 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
return prog;
prog->aux->jit_data = jit_data;
}
+ priv_stack_ptr = prog->aux->priv_stack_ptr;
+ if (!priv_stack_ptr && prog->aux->jits_use_priv_stack) {
+ /*
+ * Allocate the actual private stack: the verifier-calculated
+ * stack size plus two guard regions to detect overflow and
+ * underflow.
+ */
+ priv_stack_alloc_sz = round_up(prog->aux->stack_depth, 16) +
+ 2 * PRIV_STACK_GUARD_SZ;
+ priv_stack_ptr = __alloc_percpu_gfp(priv_stack_alloc_sz, 16, GFP_KERNEL);
+ if (!priv_stack_ptr)
+ goto out_priv_stack;
+
+ priv_stack_init_guard(priv_stack_ptr, priv_stack_alloc_sz);
+ prog->aux->priv_stack_ptr = priv_stack_ptr;
+ }
if (jit_data->ctx.offset) {
ctx = jit_data->ctx;
ro_header = jit_data->ro_header;
@@ -2321,6 +2400,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
ctx.arena_vm_start = bpf_arena_get_kern_vm_start(prog->aux->arena);
ctx.user_vm_start = bpf_arena_get_user_vm_start(prog->aux->arena);
}
+ ctx.priv_sp_used = priv_stack_ptr ? true : false;
ctx.offset = kvzalloc_objs(u32, prog->len + 1);
if (ctx.offset == NULL)
@@ -2422,7 +2502,17 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
bpf_prog_update_insn_ptrs(prog, ctx.offset, ctx.ro_image);
out_offset:
+ /*
+ * A NULL ro_header here means the JIT failed, so release the
+ * private stack that was allocated above; on success the
+ * program keeps it until bpf_jit_free().
+ */
+ if (!ro_header && priv_stack_ptr) {
+ free_percpu(priv_stack_ptr);
+ prog->aux->priv_stack_ptr = NULL;
+ }
kvfree(ctx.offset);
+out_priv_stack:
kfree(jit_data);
prog->aux->jit_data = NULL;
}
@@ -2439,6 +2529,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
if (header) {
bpf_arch_text_copy(&ro_header->size, &header->size, sizeof(header->size));
bpf_jit_binary_pack_free(ro_header, header);
+ ro_header = NULL;
}
goto out_offset;
}
@@ -2448,6 +2539,8 @@ void bpf_jit_free(struct bpf_prog *prog)
if (prog->jited) {
struct jit_data *jit_data = prog->aux->jit_data;
struct bpf_binary_header *hdr;
+ void __percpu *priv_stack_ptr;
+ int priv_stack_alloc_sz;
/*
* If we fail the final pass of JIT (from jit_subprogs), the
@@ -2461,6 +2554,13 @@ void bpf_jit_free(struct bpf_prog *prog)
}
hdr = bpf_jit_binary_pack_hdr(prog);
bpf_jit_binary_pack_free(hdr, NULL);
+ priv_stack_ptr = prog->aux->priv_stack_ptr;
+ if (priv_stack_ptr) {
+ priv_stack_alloc_sz = round_up(prog->aux->stack_depth, 16) +
+ 2 * PRIV_STACK_GUARD_SZ;
+ priv_stack_check_guard(priv_stack_ptr, priv_stack_alloc_sz, prog);
+ free_percpu(prog->aux->priv_stack_ptr);
+ }
WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(prog));
}
@@ -2544,6 +2644,11 @@ bool bpf_jit_supports_ptr_xchg(void)
return true;
}
+bool bpf_jit_supports_private_stack(void)
+{
+ return true;
+}
+
/* Indicate the JIT backend supports mixing bpf2bpf and tailcalls. */
bool bpf_jit_supports_subprog_tailcalls(void)
{
diff --git a/arch/loongarch/net/bpf_jit.h b/arch/loongarch/net/bpf_jit.h
index 777c76112d95..bd11ca8fec53 100644
--- a/arch/loongarch/net/bpf_jit.h
+++ b/arch/loongarch/net/bpf_jit.h
@@ -22,6 +22,7 @@ struct jit_ctx {
u32 stack_size;
u64 arena_vm_start;
u64 user_vm_start;
+ bool priv_sp_used;
};
struct jit_data {
--
2.53.0
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v3 2/5] LoongArch: BPF: Add support for exceptions
2026-11-26 3:40 [PATCH v3 0/5] LoongArch: BPF: Add private stack, exceptions, large stack and arena support George Guo
2026-11-26 3:40 ` [PATCH v3 1/5] LoongArch: BPF: Add support for private stack George Guo
@ 2026-11-26 3:40 ` George Guo
2026-11-26 3:40 ` [PATCH v3 3/5] LoongArch: BPF: Allow programs 2 KiB of stack George Guo
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: George Guo @ 2026-11-26 3:40 UTC (permalink / raw)
To: chenhuacai, yangtiezhu
Cc: hengqi.chen, guodongtai, bpf, loongarch, linux-kernel
From: George Guo <guodongtai@kylinos.cn>
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.
This is similar to commit 22fc0e80aeb5 ("bpf, arm64: support
exceptions").
Signed-off-by: George Guo <guodongtai@kylinos.cn>
---
arch/loongarch/net/bpf_jit.c | 45 ++++++++++++++++++++++++++++++++----
1 file changed, 40 insertions(+), 5 deletions(-)
diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
index 3ff15b64925f..19907612a51c 100644
--- a/arch/loongarch/net/bpf_jit.c
+++ b/arch/loongarch/net/bpf_jit.c
@@ -120,8 +120,8 @@ static void prepare_bpf_tail_call_cnt(struct jit_ctx *ctx, int *store_offset)
* +-------------------------+
* | tcc_ptr |
* +-------------------------+
- * | arena |
- * | (optional) |
+ * | $s6 (arena/exception) |
+ * | (optional) |
* +-------------------------+ <--BPF_REG_FP
* | prog->aux->stack_depth |
* | (optional) |
@@ -133,6 +133,13 @@ static void build_prologue(struct jit_ctx *ctx)
int i, stack_adjust = 0, store_offset, bpf_stack_adjust;
const struct bpf_prog *prog = ctx->prog;
const bool is_main_prog = !bpf_is_subprog(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 slot is always reserved for them.
+ */
+ const bool is_exception_prog = prog->aux->exception_boundary ||
+ prog->aux->exception_cb;
bpf_stack_adjust = round_up(ctx->prog->aux->stack_depth, 16);
@@ -142,7 +149,7 @@ static void build_prologue(struct jit_ctx *ctx)
/* To store tcc and tcc_ptr */
stack_adjust += sizeof(long) * 2;
- if (ctx->arena_vm_start)
+ if (ctx->arena_vm_start || is_exception_prog)
stack_adjust += sizeof(long);
stack_adjust = round_up(stack_adjust, 16);
@@ -173,6 +180,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, -stack_adjust);
+ goto setup_bpf_fp;
+ }
+
emit_insn(ctx, addid, LOONGARCH_GPR_SP, LOONGARCH_GPR_SP, -stack_adjust);
store_offset = stack_adjust - sizeof(long);
@@ -201,13 +221,14 @@ 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);
}
emit_insn(ctx, addid, LOONGARCH_GPR_FP, LOONGARCH_GPR_SP, stack_adjust);
+setup_bpf_fp:
if (ctx->priv_sp_used) {
/* Point the BPF frame pointer at the top of this CPU's private stack */
void __percpu *priv_stack_ptr;
@@ -229,6 +250,9 @@ static void __build_epilogue(struct jit_ctx *ctx, bool is_tail_call)
{
int stack_adjust = ctx->stack_size;
int load_offset;
+ const struct bpf_prog *prog = ctx->prog;
+ const bool is_exception_prog = prog->aux->exception_boundary ||
+ prog->aux->exception_cb;
load_offset = stack_adjust - sizeof(long);
emit_insn(ctx, ldd, LOONGARCH_GPR_RA, LOONGARCH_GPR_SP, load_offset);
@@ -261,7 +285,7 @@ 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);
}
@@ -2649,6 +2673,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
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v3 3/5] LoongArch: BPF: Allow programs 2 KiB of stack
2026-11-26 3:40 [PATCH v3 0/5] LoongArch: BPF: Add private stack, exceptions, large stack and arena support George Guo
2026-11-26 3:40 ` [PATCH v3 1/5] LoongArch: BPF: Add support for private stack George Guo
2026-11-26 3:40 ` [PATCH v3 2/5] LoongArch: BPF: Add support for exceptions George Guo
@ 2026-11-26 3:40 ` George Guo
2026-11-26 3:40 ` [PATCH v3 4/5] LoongArch: BPF: Add support for signed arena loads George Guo
2026-11-26 3:40 ` [PATCH v3 5/5] LoongArch: BPF: Add support for arena atomics George Guo
4 siblings, 0 replies; 6+ messages in thread
From: George Guo @ 2026-11-26 3:40 UTC (permalink / raw)
To: chenhuacai, yangtiezhu
Cc: hengqi.chen, guodongtai, bpf, loongarch, linux-kernel
From: George Guo <guodongtai@kylinos.cn>
Currently, the LoongArch JIT allocates the whole frame of a program with
a single addi.d, and saves and restores the callee-saved registers at
$sp-relative offsets computed from the whole frame size. All of these
instructions take a signed 12-bit immediate, so the frame must stay
below 2 KiB, otherwise emit_insn() silently truncates the offsets. The
same applies to loading tcc_ptr from $sp and to adding the private stack
depth to the BPF frame pointer with addi.d.
To fix this, allocate the save area and the BPF stack separately. The
save area is still addressed with small offsets from $sp, and the BPF
stack below it is allocated with addi.d if its size fits the immediate,
otherwise with move_imm() and sub.d. The epilogue releases the BPF stack
by deriving $sp from $fp before restoring the registers. tcc_ptr is at a
fixed offset from $fp, so load it from $fp for tail calls, direct and
indirect bpf2bpf calls. Fold the depth of a private stack into the
per-CPU address of its top.
The frame layout does not change, and a program whose BPF stack is on
the kernel stack gets one more instruction in both the prologue and the
epilogue. Then make bpf_jit_supports_large_stack() return true to give
the JITed programs the same 2 KiB budget as on x86-64 and arm64. Since
LoongArch has 16 KiB kernel stacks, the worst case of a tail call chain
is the same as on x86-64, a little over 10 KiB.
This is similar to commit 1d5306c11da2 ("bpf, arm64: Allow programs
2 KiB of stack").
Signed-off-by: George Guo <guodongtai@kylinos.cn>
---
Documentation/bpf/bpf_design_QA.rst | 13 ++--
arch/loongarch/net/bpf_jit.c | 110 +++++++++++++++++-----------
2 files changed, 76 insertions(+), 47 deletions(-)
diff --git a/Documentation/bpf/bpf_design_QA.rst b/Documentation/bpf/bpf_design_QA.rst
index 98d70b8a2533..cbedc56379c3 100644
--- a/Documentation/bpf/bpf_design_QA.rst
+++ b/Documentation/bpf/bpf_design_QA.rst
@@ -223,12 +223,13 @@ Q: How much stack space a BPF program uses?
-------------------------------------------
A: A program may use up to 2 KiB of stack, combined over its call
chain, when the JIT of the architecture reports support for large
-stacks (currently x86-64 and arm64); a single function may use all of
-it, and every frame of a program running on a private stack gets the
-whole amount. Elsewhere, and for programs verified for the interpreter,
-the limit is 512 bytes; the interpreter itself never runs a frame
-larger than that. The verifier computes the actual amount of stack
-used and both interpreter and most JITed code consume necessary amount.
+stacks (currently x86-64, arm64 and LoongArch); a single function may
+use all of it, and every frame of a program running on a private stack
+gets the whole amount. Elsewhere, and for programs verified for the
+interpreter, the limit is 512 bytes; the interpreter itself never runs
+a frame larger than that. The verifier computes the actual amount of
+stack used and both interpreter and most JITed code consume necessary
+amount.
Q: Can BPF be offloaded to HW?
------------------------------
diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
index 19907612a51c..20f29ea01bda 100644
--- a/arch/loongarch/net/bpf_jit.c
+++ b/arch/loongarch/net/bpf_jit.c
@@ -28,7 +28,8 @@
#define REG_TCC LOONGARCH_GPR_A6
#define REG_ARENA LOONGARCH_GPR_S6 /* For storing arena_vm_start */
-#define BPF_TAIL_CALL_CNT_PTR_STACK_OFF(stack) (round_up(stack, 16) - 80)
+/* tcc_ptr sits below $ra, $fp, $s0-$s5 and tcc, see the stack layout below */
+#define BPF_TAIL_CALL_CNT_PTR_FP_OFF (-80)
/* Memory size/value to protect private stack overflow/underflow */
#define PRIV_STACK_GUARD_SZ 16
@@ -127,12 +128,15 @@ static void prepare_bpf_tail_call_cnt(struct jit_ctx *ctx, int *store_offset)
* | (optional) |
* current $sp -------------> +-------------------------+
* low
+ *
+ * The area from $ra down to the optional $s6 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 void build_prologue(struct jit_ctx *ctx)
+static int jit_save_area_size(const struct jit_ctx *ctx)
{
- int i, stack_adjust = 0, store_offset, bpf_stack_adjust;
const struct bpf_prog *prog = ctx->prog;
- const bool is_main_prog = !bpf_is_subprog(prog);
/*
* Exception boundary and callback programs must agree on the frame
* layout: the callback reuses the boundary's frame to restore its
@@ -140,27 +144,25 @@ static void build_prologue(struct jit_ctx *ctx)
*/
const bool is_exception_prog = prog->aux->exception_boundary ||
prog->aux->exception_cb;
-
- bpf_stack_adjust = round_up(ctx->prog->aux->stack_depth, 16);
-
- /* To store ra, fp, s0, s1, s2, s3, s4, s5 */
- stack_adjust += sizeof(long) * 8;
-
- /* To store tcc and tcc_ptr */
- stack_adjust += sizeof(long) * 2;
+ /* To store ra, fp, s0, s1, s2, s3, s4, s5, tcc and tcc_ptr */
+ int size = sizeof(long) * 10;
if (ctx->arena_vm_start || is_exception_prog)
- stack_adjust += sizeof(long);
+ size += sizeof(long);
- stack_adjust = round_up(stack_adjust, 16);
+ return round_up(size, 16);
+}
- /*
- * When a private stack is used the BPF stack lives in a per-CPU
- * allocation rather than on the kernel stack, so only the non-BPF
- * part is reserved here.
- */
- if (!ctx->priv_sp_used)
- stack_adjust += bpf_stack_adjust;
+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);
/*
* Save the original return address to a temporary register to prevent
@@ -189,13 +191,13 @@ static void build_prologue(struct jit_ctx *ctx)
* 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, -stack_adjust);
+ 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, -stack_adjust);
+ emit_insn(ctx, addid, LOONGARCH_GPR_SP, LOONGARCH_GPR_SP, -save_size);
- store_offset = stack_adjust - sizeof(long);
+ store_offset = save_size - sizeof(long);
emit_insn(ctx, std, LOONGARCH_GPR_RA, LOONGARCH_GPR_SP, store_offset);
store_offset -= sizeof(long);
@@ -226,35 +228,57 @@ static void build_prologue(struct jit_ctx *ctx)
emit_insn(ctx, std, REG_ARENA, LOONGARCH_GPR_SP, store_offset);
}
- emit_insn(ctx, addid, LOONGARCH_GPR_FP, LOONGARCH_GPR_SP, stack_adjust);
+ 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) {
- /* Point the BPF frame pointer at the top of this CPU's private stack */
+ /*
+ * Point the BPF frame pointer at the top of this CPU's private
+ * stack. The BPF stack lives in that per-CPU allocation rather
+ * than on the kernel stack, so nothing more is reserved here.
+ */
void __percpu *priv_stack_ptr;
- priv_stack_ptr = prog->aux->priv_stack_ptr + PRIV_STACK_GUARD_SZ;
+ priv_stack_ptr = prog->aux->priv_stack_ptr + PRIV_STACK_GUARD_SZ +
+ bpf_stack_adjust;
emit_percpu_ptr(ctx, regmap[BPF_REG_FP], priv_stack_ptr);
- emit_insn(ctx, addid, regmap[BPF_REG_FP], regmap[BPF_REG_FP], bpf_stack_adjust);
} else if (bpf_stack_adjust) {
- emit_insn(ctx, addid, regmap[BPF_REG_FP], LOONGARCH_GPR_SP, bpf_stack_adjust);
+ /* The BPF stack starts right below the save area */
+ move_reg(ctx, regmap[BPF_REG_FP], LOONGARCH_GPR_SP);
+ if (is_signed_imm12(-bpf_stack_adjust)) {
+ emit_insn(ctx, addid, LOONGARCH_GPR_SP, LOONGARCH_GPR_SP,
+ -bpf_stack_adjust);
+ } else {
+ move_imm(ctx, LOONGARCH_GPR_T1, bpf_stack_adjust);
+ emit_insn(ctx, subd, LOONGARCH_GPR_SP, LOONGARCH_GPR_SP,
+ LOONGARCH_GPR_T1);
+ }
+ ctx->stack_size += bpf_stack_adjust;
}
- ctx->stack_size = stack_adjust;
-
if (ctx->arena_vm_start)
move_imm(ctx, REG_ARENA, ctx->arena_vm_start);
}
static void __build_epilogue(struct jit_ctx *ctx, bool is_tail_call)
{
- int stack_adjust = ctx->stack_size;
+ 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;
- load_offset = stack_adjust - sizeof(long);
+ /*
+ * Release the BPF stack, if it is on the kernel stack, by deriving
+ * $sp from $fp: unlike its size, the offset of the save area from
+ * $fp always fits an immediate.
+ */
+ if (ctx->stack_size != save_size)
+ emit_insn(ctx, addid, LOONGARCH_GPR_SP, LOONGARCH_GPR_FP, -save_size);
+
+ load_offset = save_size - sizeof(long);
emit_insn(ctx, ldd, LOONGARCH_GPR_RA, LOONGARCH_GPR_SP, load_offset);
load_offset -= sizeof(long);
@@ -290,7 +314,7 @@ static void __build_epilogue(struct jit_ctx *ctx, bool is_tail_call)
emit_insn(ctx, ldd, REG_ARENA, LOONGARCH_GPR_SP, load_offset);
}
- emit_insn(ctx, addid, LOONGARCH_GPR_SP, LOONGARCH_GPR_SP, stack_adjust);
+ emit_insn(ctx, addid, LOONGARCH_GPR_SP, LOONGARCH_GPR_SP, save_size);
if (!is_tail_call) {
/* Set return value */
@@ -325,7 +349,6 @@ bool bpf_jit_supports_far_kfunc_call(void)
static int emit_bpf_tail_call(struct jit_ctx *ctx, int insn)
{
int off, jmp_offset;
- int tcc_ptr_off = BPF_TAIL_CALL_CNT_PTR_STACK_OFF(ctx->stack_size);
u8 a1 = LOONGARCH_GPR_A1;
u8 a2 = LOONGARCH_GPR_A2;
u8 t1 = LOONGARCH_GPR_T1;
@@ -353,7 +376,7 @@ static int emit_bpf_tail_call(struct jit_ctx *ctx, int insn)
* if ((*tcc_ptr)++ >= MAX_TAIL_CALL_CNT)
* goto out;
*/
- emit_insn(ctx, ldd, REG_TCC, LOONGARCH_GPR_SP, tcc_ptr_off);
+ emit_insn(ctx, ldd, REG_TCC, LOONGARCH_GPR_FP, BPF_TAIL_CALL_CNT_PTR_FP_OFF);
emit_insn(ctx, ldd, t3, REG_TCC, 0);
emit_insn(ctx, addid, t2, LOONGARCH_GPR_ZERO, MAX_TAIL_CALL_CNT);
jmp_offset = ctx->image ? (ctx->offset[insn + 1] - ctx->idx) : 0;
@@ -741,7 +764,7 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
u64 func_addr;
bool func_addr_fixed, sign_extend;
int i = insn - ctx->prog->insnsi;
- int ret, jmp_offset, tcc_ptr_off;
+ int ret, jmp_offset;
const u8 code = insn->code;
const u8 cond = BPF_OP(code);
const u8 t1 = LOONGARCH_GPR_T1;
@@ -1274,8 +1297,8 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
return ret;
if (insn->src_reg == BPF_PSEUDO_CALL) {
- tcc_ptr_off = BPF_TAIL_CALL_CNT_PTR_STACK_OFF(ctx->stack_size);
- emit_insn(ctx, ldd, REG_TCC, LOONGARCH_GPR_SP, tcc_ptr_off);
+ emit_insn(ctx, ldd, REG_TCC, LOONGARCH_GPR_FP,
+ BPF_TAIL_CALL_CNT_PTR_FP_OFF);
move_addr(ctx, t1, func_addr);
emit_insn(ctx, jirl, LOONGARCH_GPR_RA, t1, 0);
break;
@@ -1312,8 +1335,8 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
/* indirect call to a bpf subprog via the address in dst */
case BPF_JMP | BPF_CALL | BPF_X:
- tcc_ptr_off = BPF_TAIL_CALL_CNT_PTR_STACK_OFF(ctx->stack_size);
- emit_insn(ctx, ldd, REG_TCC, LOONGARCH_GPR_SP, tcc_ptr_off);
+ emit_insn(ctx, ldd, REG_TCC, LOONGARCH_GPR_FP,
+ BPF_TAIL_CALL_CNT_PTR_FP_OFF);
emit_insn(ctx, jirl, LOONGARCH_GPR_RA, dst, 0);
break;
@@ -2690,6 +2713,11 @@ bool bpf_jit_supports_subprog_tailcalls(void)
return true;
}
+bool bpf_jit_supports_large_stack(void)
+{
+ return true;
+}
+
bool bpf_jit_supports_timed_may_goto(void)
{
return true;
--
2.53.0
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v3 4/5] LoongArch: BPF: Add support for signed arena loads
2026-11-26 3:40 [PATCH v3 0/5] LoongArch: BPF: Add private stack, exceptions, large stack and arena support George Guo
` (2 preceding siblings ...)
2026-11-26 3:40 ` [PATCH v3 3/5] LoongArch: BPF: Allow programs 2 KiB of stack George Guo
@ 2026-11-26 3:40 ` George Guo
2026-11-26 3:40 ` [PATCH v3 5/5] LoongArch: BPF: Add support for arena atomics George Guo
4 siblings, 0 replies; 6+ messages in thread
From: George Guo @ 2026-11-26 3:40 UTC (permalink / raw)
To: chenhuacai, yangtiezhu
Cc: hengqi.chen, guodongtai, bpf, loongarch, linux-kernel
From: George Guo <guodongtai@kylinos.cn>
Currently, the verifier rejects sign-extending loads from arena on
LoongArch with "sign extending loads from arena are not supported yet",
because the JIT does not implement bpf_jit_supports_insn() and the weak
default returns false.
In order to support them, implement bpf_jit_supports_insn() and handle
BPF_PROBE_MEM32SX like BPF_PROBE_MEM32: add the arena base held in
REG_ARENA to the address, use sign-extending load instructions, and add
an exception table entry for the load.
This is similar to commit b4757714cc63 ("bpf, riscv: Add support for
signed arena loads").
Signed-off-by: George Guo <guodongtai@kylinos.cn>
---
arch/loongarch/net/bpf_jit.c | 18 +++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)
diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
index 20f29ea01bda..03c88cd9c96a 100644
--- a/arch/loongarch/net/bpf_jit.c
+++ b/arch/loongarch/net/bpf_jit.c
@@ -709,7 +709,8 @@ static int add_exception_handler(const struct bpf_insn *insn,
if (BPF_MODE(insn->code) != BPF_PROBE_MEM &&
BPF_MODE(insn->code) != BPF_PROBE_MEMSX &&
- BPF_MODE(insn->code) != BPF_PROBE_MEM32)
+ BPF_MODE(insn->code) != BPF_PROBE_MEM32 &&
+ BPF_MODE(insn->code) != BPF_PROBE_MEM32SX)
return 0;
if (WARN_ON_ONCE(ctx->num_exentries >= ctx->prog->aux->num_exentries))
@@ -1389,10 +1390,16 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
case BPF_LDX | BPF_PROBE_MEM32 | BPF_H:
case BPF_LDX | BPF_PROBE_MEM32 | BPF_W:
case BPF_LDX | BPF_PROBE_MEM32 | BPF_DW:
+ /* LDX | PROBE_MEM32SX: dst = *(signed size *)(src + REG_ARENA + off) */
+ case BPF_LDX | BPF_PROBE_MEM32SX | BPF_B:
+ case BPF_LDX | BPF_PROBE_MEM32SX | BPF_H:
+ case BPF_LDX | BPF_PROBE_MEM32SX | BPF_W:
sign_extend = BPF_MODE(code) == BPF_MEMSX ||
- BPF_MODE(code) == BPF_PROBE_MEMSX;
+ BPF_MODE(code) == BPF_PROBE_MEMSX ||
+ BPF_MODE(code) == BPF_PROBE_MEM32SX;
- if (BPF_MODE(code) == BPF_PROBE_MEM32) {
+ if (BPF_MODE(code) == BPF_PROBE_MEM32 ||
+ BPF_MODE(code) == BPF_PROBE_MEM32SX) {
emit_insn(ctx, addd, t2, src, REG_ARENA);
src = t2;
}
@@ -2676,6 +2683,11 @@ bool bpf_jit_supports_arena(void)
#endif
}
+bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)
+{
+ return true;
+}
+
bool bpf_jit_supports_fsession(void)
{
return true;
--
2.53.0
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v3 5/5] LoongArch: BPF: Add support for arena atomics
2026-11-26 3:40 [PATCH v3 0/5] LoongArch: BPF: Add private stack, exceptions, large stack and arena support George Guo
` (3 preceding siblings ...)
2026-11-26 3:40 ` [PATCH v3 4/5] LoongArch: BPF: Add support for signed arena loads George Guo
@ 2026-11-26 3:40 ` George Guo
4 siblings, 0 replies; 6+ messages in thread
From: George Guo @ 2026-11-26 3:40 UTC (permalink / raw)
To: chenhuacai, yangtiezhu
Cc: hengqi.chen, guodongtai, bpf, loongarch, linux-kernel
From: George Guo <guodongtai@kylinos.cn>
In order to support the atomic instructions on arena pointers
(BPF_PROBE_ATOMIC), add the arena base held in REG_ARENA to the address
and add an exception table entry for the memory access instruction of
read-modify-write, fetch, xchg, cmpxchg, load-acquire and store-release.
If the memory access faults, clear the register that the instruction
loads into, as the verifier expects: dst_reg for load-acquire, r0 for
cmpxchg and src_reg for the fetch instructions. The zero-extension of
the 32-bit fetch instructions is emitted after the memory access, so the
cleared register remains zero.
The ll/sc loop of cmpxchg is not suitable for arena: both ll and sc can
fault if another CPU frees the page between them, but the verifier only
reserves one exception table entry for each instruction. So emit a
single amcas_db.{w/d} instruction for cmpxchg on arena, and make
bpf_jit_supports_insn() return cpu_has_lamcas for it, just like RISC-V
does with Zacas.
This is similar to commit fb7cefabae81 ("riscv, bpf: Add support arena
atomics for RV64") and commit 1519f488e8ce ("bpf, riscv: Clear fetch
destination on faulting arena atomic").
Signed-off-by: George Guo <guodongtai@kylinos.cn>
---
arch/loongarch/include/asm/inst.h | 4 +
arch/loongarch/net/bpf_jit.c | 137 +++++++++++++++++++++++++++---
2 files changed, 130 insertions(+), 11 deletions(-)
diff --git a/arch/loongarch/include/asm/inst.h b/arch/loongarch/include/asm/inst.h
index e061f966f7d1..1fc99b7d70be 100644
--- a/arch/loongarch/include/asm/inst.h
+++ b/arch/loongarch/include/asm/inst.h
@@ -198,6 +198,8 @@ enum reg3_op {
fstxs_op = 0x7070,
fstxd_op = 0x7078,
scq_op = 0x70ae,
+ amcasdbw_op = 0x70b6,
+ amcasdbd_op = 0x70b7,
amswapb_op = 0x70b8,
amswaph_op = 0x70b9,
amaddb_op = 0x70ba,
@@ -817,6 +819,8 @@ DEF_EMIT_REG3_FORMAT(amorw, amorw_op)
DEF_EMIT_REG3_FORMAT(amord, amord_op)
DEF_EMIT_REG3_FORMAT(amxorw, amxorw_op)
DEF_EMIT_REG3_FORMAT(amxord, amxord_op)
+DEF_EMIT_REG3_FORMAT(amcasdbw, amcasdbw_op)
+DEF_EMIT_REG3_FORMAT(amcasdbd, amcasdbd_op)
DEF_EMIT_REG3_FORMAT(amswapb, amswapb_op)
DEF_EMIT_REG3_FORMAT(amswaph, amswaph_op)
DEF_EMIT_REG3_FORMAT(amswapw, amswapw_op)
diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
index 03c88cd9c96a..ebe603131396 100644
--- a/arch/loongarch/net/bpf_jit.c
+++ b/arch/loongarch/net/bpf_jit.c
@@ -418,6 +418,13 @@ static void emit_store_stack_imm64(struct jit_ctx *ctx, int reg, int stack_off,
emit_insn(ctx, std, reg, LOONGARCH_GPR_FP, stack_off);
}
+#define BPF_FIXUP_REG_MASK GENMASK(31, 27)
+#define BPF_FIXUP_OFFSET_MASK GENMASK(26, 0)
+#define REG_DONT_CLEAR_MARKER 0
+
+static int add_exception_handler(const struct bpf_insn *insn,
+ struct jit_ctx *ctx, int dst_reg);
+
static int emit_atomic_rmw(const struct bpf_insn *insn, struct jit_ctx *ctx)
{
const u8 t1 = LOONGARCH_GPR_T1;
@@ -429,9 +436,15 @@ static int emit_atomic_rmw(const struct bpf_insn *insn, struct jit_ctx *ctx)
const s16 off = insn->off;
const s32 imm = insn->imm;
const bool isdw = BPF_SIZE(insn->code) == BPF_DW;
+ const bool arena = BPF_MODE(insn->code) == BPF_PROBE_ATOMIC;
+ bool zext = false;
+ u8 zext_reg = src;
+ int ret;
move_imm(ctx, t1, off);
emit_insn(ctx, addd, t1, dst, t1);
+ if (arena)
+ emit_insn(ctx, addd, t1, t1, REG_ARENA);
move_reg(ctx, t3, src);
switch (imm) {
@@ -502,15 +515,14 @@ static int emit_atomic_rmw(const struct bpf_insn *insn, struct jit_ctx *ctx)
emit_insn(ctx, amadddbd, src, t1, t3);
break;
}
- if (!isdw)
- emit_insn(ctx, bstrpickd, src, src, 31, 0);
+ zext = !isdw;
break;
case BPF_AND | BPF_FETCH:
if (isdw) {
emit_insn(ctx, amanddbd, src, t1, t3);
} else {
emit_insn(ctx, amanddbw, src, t1, t3);
- emit_insn(ctx, bstrpickd, src, src, 31, 0);
+ zext = true;
}
break;
case BPF_OR | BPF_FETCH:
@@ -518,7 +530,7 @@ static int emit_atomic_rmw(const struct bpf_insn *insn, struct jit_ctx *ctx)
emit_insn(ctx, amordbd, src, t1, t3);
} else {
emit_insn(ctx, amordbw, src, t1, t3);
- emit_insn(ctx, bstrpickd, src, src, 31, 0);
+ zext = true;
}
break;
case BPF_XOR | BPF_FETCH:
@@ -526,7 +538,7 @@ static int emit_atomic_rmw(const struct bpf_insn *insn, struct jit_ctx *ctx)
emit_insn(ctx, amxordbd, src, t1, t3);
} else {
emit_insn(ctx, amxordbw, src, t1, t3);
- emit_insn(ctx, bstrpickd, src, src, 31, 0);
+ zext = true;
}
break;
/* src = atomic_xchg(dst + off, src); */
@@ -553,11 +565,30 @@ static int emit_atomic_rmw(const struct bpf_insn *insn, struct jit_ctx *ctx)
emit_insn(ctx, amswapdbd, src, t1, t3);
break;
}
- if (!isdw)
- emit_insn(ctx, bstrpickd, src, src, 31, 0);
+ zext = !isdw;
break;
/* r0 = atomic_cmpxchg(dst + off, r0, src); */
case BPF_CMPXCHG:
+ if (arena) {
+ /*
+ * Another CPU can free the arena page between the ll and
+ * the sc of an ll/sc loop, so both can fault, but the
+ * verifier reserves a single exception table entry per
+ * instruction. Use amcas, which bpf_jit_supports_insn()
+ * requires for cmpxchg on arena.
+ */
+ if (!cpu_has_lamcas) {
+ pr_err_once("bpf-jit: amcas instruction is not supported\n");
+ return -EINVAL;
+ }
+ if (isdw)
+ emit_insn(ctx, amcasdbd, r0, t1, t3);
+ else
+ emit_insn(ctx, amcasdbw, r0, t1, t3);
+ zext = !isdw;
+ zext_reg = r0;
+ break;
+ }
move_reg(ctx, t2, r0);
if (isdw) {
emit_insn(ctx, lld, r0, t1, 0);
@@ -582,6 +613,24 @@ static int emit_atomic_rmw(const struct bpf_insn *insn, struct jit_ctx *ctx)
return -EINVAL;
}
+ /*
+ * On arena, the memory access is the last instruction emitted so far.
+ * If it faults, clear the register it loads into, as the verifier
+ * expects, and resume at the next instruction: zero-extending the
+ * cleared register keeps it zero.
+ */
+ if (arena) {
+ int load_reg = bpf_atomic_load_reg(insn);
+
+ ret = add_exception_handler(insn, ctx, load_reg < 0 ?
+ REG_DONT_CLEAR_MARKER : regmap[load_reg]);
+ if (ret)
+ return ret;
+ }
+
+ if (zext)
+ emit_insn(ctx, bstrpickd, zext_reg, zext_reg, 31, 0);
+
return 0;
}
@@ -592,10 +641,37 @@ static int emit_atomic_ld_st(const struct bpf_insn *insn, struct jit_ctx *ctx)
const u8 dst = regmap[insn->dst_reg];
const s16 off = insn->off;
const s32 imm = insn->imm;
+ const bool arena = BPF_MODE(insn->code) == BPF_PROBE_ATOMIC;
+ int ret;
switch (imm) {
/* dst_reg = load_acquire(src_reg + off16) */
case BPF_LOAD_ACQ:
+ if (arena) {
+ /* t1 = src + off + arena_vm_start; load from [t1]. */
+ move_imm(ctx, t1, off);
+ emit_insn(ctx, addd, t1, src, t1);
+ emit_insn(ctx, addd, t1, t1, REG_ARENA);
+ switch (BPF_SIZE(insn->code)) {
+ case BPF_B:
+ emit_insn(ctx, ldbu, dst, t1, 0);
+ break;
+ case BPF_H:
+ emit_insn(ctx, ldhu, dst, t1, 0);
+ break;
+ case BPF_W:
+ emit_insn(ctx, ldwu, dst, t1, 0);
+ break;
+ case BPF_DW:
+ emit_insn(ctx, ldd, dst, t1, 0);
+ break;
+ }
+ ret = add_exception_handler(insn, ctx, dst);
+ if (ret)
+ return ret;
+ emit_insn(ctx, dbar, 0b10100);
+ break;
+ }
switch (BPF_SIZE(insn->code)) {
case BPF_B:
if (is_signed_imm12(off)) {
@@ -634,6 +710,31 @@ static int emit_atomic_ld_st(const struct bpf_insn *insn, struct jit_ctx *ctx)
break;
/* store_release(dst_reg + off16, src_reg) */
case BPF_STORE_REL:
+ if (arena) {
+ /* t1 = dst + off + arena_vm_start; store to [t1]. */
+ emit_insn(ctx, dbar, 0b10010);
+ move_imm(ctx, t1, off);
+ emit_insn(ctx, addd, t1, dst, t1);
+ emit_insn(ctx, addd, t1, t1, REG_ARENA);
+ switch (BPF_SIZE(insn->code)) {
+ case BPF_B:
+ emit_insn(ctx, stb, src, t1, 0);
+ break;
+ case BPF_H:
+ emit_insn(ctx, sth, src, t1, 0);
+ break;
+ case BPF_W:
+ emit_insn(ctx, stw, src, t1, 0);
+ break;
+ case BPF_DW:
+ emit_insn(ctx, std, src, t1, 0);
+ break;
+ }
+ ret = add_exception_handler(insn, ctx, REG_DONT_CLEAR_MARKER);
+ if (ret)
+ return ret;
+ break;
+ }
emit_insn(ctx, dbar, 0b10010);
switch (BPF_SIZE(insn->code)) {
case BPF_B:
@@ -678,9 +779,6 @@ static int emit_atomic_ld_st(const struct bpf_insn *insn, struct jit_ctx *ctx)
return 0;
}
-#define BPF_FIXUP_REG_MASK GENMASK(31, 27)
-#define BPF_FIXUP_OFFSET_MASK GENMASK(26, 0)
-#define REG_DONT_CLEAR_MARKER 0
bool ex_handler_bpf(const struct exception_table_entry *ex,
struct pt_regs *regs)
@@ -710,7 +808,8 @@ static int add_exception_handler(const struct bpf_insn *insn,
if (BPF_MODE(insn->code) != BPF_PROBE_MEM &&
BPF_MODE(insn->code) != BPF_PROBE_MEMSX &&
BPF_MODE(insn->code) != BPF_PROBE_MEM32 &&
- BPF_MODE(insn->code) != BPF_PROBE_MEM32SX)
+ BPF_MODE(insn->code) != BPF_PROBE_MEM32SX &&
+ BPF_MODE(insn->code) != BPF_PROBE_ATOMIC)
return 0;
if (WARN_ON_ONCE(ctx->num_exentries >= ctx->prog->aux->num_exentries))
@@ -1591,6 +1690,10 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
case BPF_STX | BPF_ATOMIC | BPF_H:
case BPF_STX | BPF_ATOMIC | BPF_W:
case BPF_STX | BPF_ATOMIC | BPF_DW:
+ case BPF_STX | BPF_PROBE_ATOMIC | BPF_B:
+ case BPF_STX | BPF_PROBE_ATOMIC | BPF_H:
+ case BPF_STX | BPF_PROBE_ATOMIC | BPF_W:
+ case BPF_STX | BPF_PROBE_ATOMIC | BPF_DW:
if (!bpf_atomic_is_load_store(insn))
ret = emit_atomic_rmw(insn, ctx);
else
@@ -2685,6 +2788,18 @@ bool bpf_jit_supports_arena(void)
bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)
{
+ if (!in_arena)
+ return true;
+
+ switch (insn->code) {
+ case BPF_STX | BPF_ATOMIC | BPF_W:
+ case BPF_STX | BPF_ATOMIC | BPF_DW:
+ /* cmpxchg on arena is emitted as amcas, see emit_atomic_rmw() */
+ if (insn->imm == BPF_CMPXCHG)
+ return cpu_has_lamcas;
+ break;
+ }
+
return true;
}
--
2.53.0
^ permalink raw reply [flat|nested] 6+ messages in thread