mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: George Guo <dongtai.guo@linux.dev>
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 v3 5/5] LoongArch: BPF: Add support for arena atomics
Date: Thu, 26 Nov 2026 12:40:47 +0900	[thread overview]
Message-ID: <20261126034048.7370-6-dongtai.guo@linux.dev> (raw)
In-Reply-To: <20261126034048.7370-1-dongtai.guo@linux.dev>

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


      parent reply	other threads:[~2026-11-26  3:41 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` [PATCH v3 3/5] LoongArch: BPF: Allow programs 2 KiB of stack 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 ` George Guo [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261126034048.7370-6-dongtai.guo@linux.dev \
    --to=dongtai.guo@linux.dev \
    --cc=bpf@vger.kernel.org \
    --cc=chenhuacai@kernel.org \
    --cc=guodongtai@kylinos.cn \
    --cc=hengqi.chen@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=loongarch@lists.linux.dev \
    --cc=yangtiezhu@loongson.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®