From: Tiezhu Yang <yangtiezhu@loongson.cn>
To: Huacai Chen <chenhuacai@kernel.org>, Hengqi Chen <hengqi.chen@gmail.com>
Cc: loongarch@lists.linux.dev, bpf@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH v3 3/5] LoongArch: BPF: Support 8 and 16 bit read-modify-write instructions
Date: Mon, 13 Apr 2026 12:05:04 +0800 [thread overview]
Message-ID: <20260413040506.26640-4-yangtiezhu@loongson.cn> (raw)
In-Reply-To: <20260413040506.26640-1-yangtiezhu@loongson.cn>
The 8 and 16 bit read-modify-write instructions {amadd/amswap}.{b/h}
were newly added in the latest LoongArch Reference Manual, use them
to avoid the error of unknown opcode if possible.
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
arch/loongarch/net/bpf_jit.c | 83 ++++++++++++++++++++++++++++++++----
1 file changed, 74 insertions(+), 9 deletions(-)
diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
index fefda4050a20..c9a32f124f5e 100644
--- a/arch/loongarch/net/bpf_jit.c
+++ b/arch/loongarch/net/bpf_jit.c
@@ -363,10 +363,30 @@ static int emit_atomic_rmw(const struct bpf_insn *insn, struct jit_ctx *ctx)
switch (imm) {
/* lock *(size *)(dst + off) <op>= src */
case BPF_ADD:
- if (isdw)
- emit_insn(ctx, amaddd, t2, t1, src);
- else
+ switch (BPF_SIZE(insn->code)) {
+ case BPF_B:
+ if (cpu_has_lam_bh) {
+ emit_insn(ctx, amaddb, t2, t1, src);
+ } else {
+ pr_err_once("bpf-jit: amadd.b instruction is not supported\n");
+ return -EINVAL;
+ }
+ break;
+ case BPF_H:
+ if (cpu_has_lam_bh) {
+ emit_insn(ctx, amaddh, t2, t1, src);
+ } else {
+ pr_err_once("bpf-jit: amadd.h instruction is not supported\n");
+ return -EINVAL;
+ }
+ break;
+ case BPF_W:
emit_insn(ctx, amaddw, t2, t1, src);
+ break;
+ case BPF_DW:
+ emit_insn(ctx, amaddd, t2, t1, src);
+ break;
+ }
break;
case BPF_AND:
if (isdw)
@@ -388,11 +408,32 @@ static int emit_atomic_rmw(const struct bpf_insn *insn, struct jit_ctx *ctx)
break;
/* src = atomic_fetch_<op>(dst + off, src) */
case BPF_ADD | BPF_FETCH:
- if (isdw) {
- emit_insn(ctx, amaddd, src, t1, t3);
- } else {
+ switch (BPF_SIZE(insn->code)) {
+ case BPF_B:
+ if (cpu_has_lam_bh) {
+ emit_insn(ctx, amaddb, src, t1, t3);
+ emit_zext_32(ctx, src, true);
+ } else {
+ pr_err_once("bpf-jit: amadd.b instruction is not supported\n");
+ return -EINVAL;
+ }
+ break;
+ case BPF_H:
+ if (cpu_has_lam_bh) {
+ emit_insn(ctx, amaddh, src, t1, t3);
+ emit_zext_32(ctx, src, true);
+ } else {
+ pr_err_once("bpf-jit: amadd.h instruction is not supported\n");
+ return -EINVAL;
+ }
+ break;
+ case BPF_W:
emit_insn(ctx, amaddw, src, t1, t3);
emit_zext_32(ctx, src, true);
+ break;
+ case BPF_DW:
+ emit_insn(ctx, amaddd, src, t1, t3);
+ break;
}
break;
case BPF_AND | BPF_FETCH:
@@ -421,11 +462,32 @@ static int emit_atomic_rmw(const struct bpf_insn *insn, struct jit_ctx *ctx)
break;
/* src = atomic_xchg(dst + off, src); */
case BPF_XCHG:
- if (isdw) {
- emit_insn(ctx, amswapd, src, t1, t3);
- } else {
+ switch (BPF_SIZE(insn->code)) {
+ case BPF_B:
+ if (cpu_has_lam_bh) {
+ emit_insn(ctx, amswapb, src, t1, t3);
+ emit_zext_32(ctx, src, true);
+ } else {
+ pr_err_once("bpf-jit: amswap.b instruction is not supported\n");
+ return -EINVAL;
+ }
+ break;
+ case BPF_H:
+ if (cpu_has_lam_bh) {
+ emit_insn(ctx, amswaph, src, t1, t3);
+ emit_zext_32(ctx, src, true);
+ } else {
+ pr_err_once("bpf-jit: amswap.h instruction is not supported\n");
+ return -EINVAL;
+ }
+ break;
+ case BPF_W:
emit_insn(ctx, amswapw, src, t1, t3);
emit_zext_32(ctx, src, true);
+ break;
+ case BPF_DW:
+ emit_insn(ctx, amswapd, src, t1, t3);
+ break;
}
break;
/* r0 = atomic_cmpxchg(dst + off, r0, src); */
@@ -1259,6 +1321,9 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
return ret;
break;
+ /* Atomics */
+ case BPF_STX | BPF_ATOMIC | BPF_B:
+ case BPF_STX | BPF_ATOMIC | BPF_H:
case BPF_STX | BPF_ATOMIC | BPF_W:
case BPF_STX | BPF_ATOMIC | BPF_DW:
ret = emit_atomic_rmw(insn, ctx);
--
2.42.0
next prev parent reply other threads:[~2026-04-13 4:05 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-13 4:05 [PATCH v3 0/5] LoongArch: BPF: Support more atomic instructions Tiezhu Yang
2026-04-13 4:05 ` [PATCH v3 1/5] LoongArch: Define instruction formats for AM{SWAP/ADD}.{B/H} and DBAR Tiezhu Yang
2026-04-15 2:24 ` Hengqi Chen
2026-04-13 4:05 ` [PATCH v3 2/5] LoongArch: BPF: Add the default case in emit_atomic() and rename it Tiezhu Yang
2026-04-15 2:24 ` Hengqi Chen
2026-04-13 4:05 ` Tiezhu Yang [this message]
2026-04-13 4:49 ` [PATCH v3 3/5] LoongArch: BPF: Support 8 and 16 bit read-modify-write instructions bot+bpf-ci
2026-04-13 8:05 ` WANG Rui
2026-04-13 4:05 ` [PATCH v3 4/5] LoongArch: BPF: Support load-acquire and store-release instructions Tiezhu Yang
2026-04-15 2:25 ` Hengqi Chen
2026-04-13 4:05 ` [PATCH v3 5/5] selftests/bpf: Make CAN_USE_LOAD_ACQ_STORE_REL usable for LoongArch Tiezhu Yang
2026-04-15 2:27 ` Hengqi Chen
2026-04-21 12:38 ` [PATCH v3 0/5] LoongArch: BPF: Support more atomic instructions Huacai Chen
2026-04-21 13:25 ` Tiezhu Yang
2026-04-21 13:46 ` Tiezhu Yang
2026-04-22 8:15 ` Huacai Chen
2026-04-23 2:56 ` Tiezhu Yang
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=20260413040506.26640-4-yangtiezhu@loongson.cn \
--to=yangtiezhu@loongson.cn \
--cc=bpf@vger.kernel.org \
--cc=chenhuacai@kernel.org \
--cc=hengqi.chen@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=loongarch@lists.linux.dev \
/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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).