BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Kuan-Wei Chiu" <visitorckw@gmail.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next 3/3] riscv, bpf: Add 32 bit atomic operations to RV32 JIT
Date: Wed, 29 Apr 2026 19:25:44 +0000	[thread overview]
Message-ID: <20260429192544.DCEA5C19425@smtp.kernel.org> (raw)
In-Reply-To: <20260429123513.3477780-4-visitorckw@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] BPF atomic fetch operations and BPF_XCHG are emitted with relaxed memory ordering, violating the BPF memory model.
--

commit f7db41188f6daa7740f66e1c205fcef2107a88ae
Author: Kuan-Wei Chiu <visitorckw@gmail.com>

riscv, bpf: Add 32 bit atomic operations to RV32 JIT

This commit adds native support for 32-bit BPF atomic operations to the RV32
BPF JIT compiler. It maps BPF_ADD, BPF_AND, BPF_OR, BPF_XOR, and BPF_XCHG
directly to their corresponding RISC-V amo*.w instructions.

> diff --git a/arch/riscv/net/bpf_jit_comp32.c b/arch/riscv/net/bpf_jit_comp32.c
> index f8509950fed4..710c56b20f98 100644
> --- a/arch/riscv/net/bpf_jit_comp32.c
> +++ b/arch/riscv/net/bpf_jit_comp32.c
[ ... ]
> @@ -902,11 +902,40 @@ static int emit_store_r64(const s8 *dst, const s8 *src, s16 off,
>  		case BPF_MEM:
>  			emit(rv_sw(RV_REG_T0, 0, lo(rs)), ctx);
>  			break;
> -		case BPF_ATOMIC: /* Only BPF_ADD supported */
> -			emit(rv_amoadd_w(RV_REG_ZERO, lo(rs), RV_REG_T0, 0, 0),
> -			     ctx);
> +		case BPF_ATOMIC:
> +		{
> +			s8 fetch_reg = (imm & BPF_FETCH) ? lo(rs) : RV_REG_ZERO;
> +
> +			switch (imm) {
> +			case BPF_ADD:
> +			case BPF_ADD | BPF_FETCH:
> +				emit(rv_amoadd_w(fetch_reg, lo(rs), RV_REG_T0, 0, 0), ctx);
> +				break;
> +			case BPF_AND:
> +			case BPF_AND | BPF_FETCH:
> +				emit(rv_amoand_w(fetch_reg, lo(rs), RV_REG_T0, 0, 0), ctx);
> +				break;
> +			case BPF_OR:
> +			case BPF_OR | BPF_FETCH:
> +				emit(rv_amoor_w(fetch_reg, lo(rs), RV_REG_T0, 0, 0), ctx);
> +				break;
> +			case BPF_XOR:
> +			case BPF_XOR | BPF_FETCH:
> +				emit(rv_amoxor_w(fetch_reg, lo(rs), RV_REG_T0, 0, 0), ctx);
> +				break;
> +			case BPF_XCHG:
> +				emit(rv_amoswap_w(fetch_reg, lo(rs), RV_REG_T0, 0, 0), ctx);
> +				break;

According to the BPF instruction set specification, atomic operations that
return a value (like BPF_FETCH variants and BPF_XCHG) must have fully
ordered semantics and act as full memory barriers.

Since the aq (acquire) and rl (release) bits are passed as 0 to the
rv_amo*_w functions here, doesn't this emit the instructions with relaxed
memory ordering? Could this allow the CPU to reorder memory accesses around
the atomic operations and lead to data races or synchronization failures in
BPF programs?

> +			default:
> +				return -1;
> +			}
> +			if (imm & BPF_FETCH) {
> +				emit(rv_addi(hi(rs), RV_REG_ZERO, 0), ctx);
> +				bpf_put_reg64(src, rs, ctx);
> +			}
>  			break;
>  		}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260429123513.3477780-1-visitorckw@gmail.com?part=3

      parent reply	other threads:[~2026-04-29 19:25 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-29 12:35 [PATCH bpf-next 0/3] riscv, bpf: Fix signed operations and add 32 bit atomics Kuan-Wei Chiu
2026-04-29 12:35 ` [PATCH bpf-next 1/3] riscv, bpf: Fix support for BPF_SDIV and BPF_SMOD in RV32 JIT Kuan-Wei Chiu
2026-04-29 13:13   ` bot+bpf-ci
2026-04-29 12:35 ` [PATCH bpf-next 2/3] riscv, bpf: Fix support for BPF_MOVSX " Kuan-Wei Chiu
2026-04-29 13:13   ` bot+bpf-ci
2026-04-29 12:35 ` [PATCH bpf-next 3/3] riscv, bpf: Add 32 bit atomic operations to " Kuan-Wei Chiu
2026-04-29 13:13   ` bot+bpf-ci
2026-04-29 19:25   ` sashiko-bot [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=20260429192544.DCEA5C19425@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=sashiko@lists.linux.dev \
    --cc=visitorckw@gmail.com \
    /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