From: Vladimir Kondratiev <Vladimir.Kondratiev@mobileye.com>
To: Conor Dooley <conor@kernel.org>
Cc: Paul Walmsley <pjw@kernel.org>,
Palmer Dabbelt <palmer@dabbelt.com>,
Albert Ou <aou@eecs.berkeley.edu>,
Alexandre Ghiti <alex@ghiti.fr>, Will Deacon <will@kernel.org>,
Peter Zijlstra <peterz@infradead.org>,
Mark Rutland <mark.rutland@arm.com>, Gary Guo <gary@garyguo.net>,
Yury Norov <yury.norov@gmail.com>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
cfu@wavecomp.com, torvalds@linux-foundation.org, olof@lixom.net,
aleksa.paunovic@htecgroup.com, arikalo@gmail.com,
Boqun Feng <boqun@kernel.org>,
linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 1/2] riscv: build kernel atomics from LR/SC when Zaamo is disabled
Date: Tue, 3 Nov 2026 14:57:29 +0200 [thread overview]
Message-ID: <1bcc885c-b013-4ab3-9b08-1d1f89b09762@mobileye.com> (raw)
In-Reply-To: <20261102-persuader-catering-5ed81c35ae0e@spud>
Some of issues already fixed on v4.
- Link to v4:
https://patch.msgid.link/20261101-lrsc-only-v4-0-4e1d010463b3@mobileye.com
On 11/2/26 20:46, Conor Dooley wrote:
> On Mon, Oct 26, 2026 at 11:20:02AM +0200, Vladimir Kondratiev wrote:
>> The A extension is Zaamo plus Zalrsc. MIPS P8700 implements only
>> Zalrsc, and its AMO instructions trap to SBI. A typical atomic is
>> about 40 cycles with LR/SC and about 600 cycles with an emulated AMO,
>> so keep the existing AMO sequences when the toolchain defines
>> __riscv_zaamo or __riscv_atomic, and use an LR/SC loop otherwise.
>>
>> Add RISCV_ISA_ZAAMO and RISCV_ISA_ZALRSC. Both default on, and that
>> selection still passes "a" in -march. Zaamo can be turned off only
>> with CONFIG_NONPORTABLE; Zalrsc remains required because cmpxchg and
>> the conditional atomics are LR/SC sequences. An LR/SC-only build
>> passes _zalrsc instead of "a".
>>
>> Suggested-by: Chao-ying Fu <cfu@wavecomp.com>
>> Signed-off-by: Vladimir Kondratiev <vladimir.kondratiev@mobileye.com>
>
> As you can see by the lkp reports, this is not the right way to do this.
I fixed these lkp reported bugs in v4 by TOOLCHAIN_HAS_ZALRSC that test
compiler support of _zalrsc in -march
>> +config RISCV_ISA_ZAAMO
>> + bool "Zaamo extension support for AMO atomics" if NONPORTABLE
>> + default y
>> + help
>> + Use AMO instructions for kernel atomic memory operations. When
>> + disabled, those operations are built from LR/SC and the kernel
>> + is compiled with zalrsc instead of the A extension.
>> +
>> + Disable this only for a hart that implements Zalrsc but not
>> + Zaamo, such as the MIPS P8700. The resulting kernel cannot run
>> + on a hart without LR/SC.
>
> The text here doesn't really make much sense, given it is default
> disabled as it depends on NONPORTABLE that is default disabled.
> If this is retained, it should be default n and the text reworded.
Not exactly. RISCV_ISA_ZAAMO is visible if NONPORTABLE &&
TOOLCHAIN_HAS_ZALRSC (later was added in v4). Otherwise it is not
visible and set to "y". If one disables NONPORTABLE, RISCV_ISA_ZAAMO set
to "y" with no option to change it. Same for added in v4
TOOLCHAIN_HAS_ZALRSC
>> +
>> + If you don't know what to do here, say Y.
>> +
>> +config RISCV_ISA_ZALRSC
>> + def_bool y
>> + help
>> + Zalrsc provides LR/SC. cmpxchg and the conditional atomics
>> + require it, so it is always enabled.
>
> So why have a kconfig option for it then? It's pointless if always
> enabled.
My logic when adding it:
There are 2 sets of extensions related defines: one is Kconfig ones,
CONFIG_RISCV_ISA_<ext>; and another one found in the
arch/riscv/include/asm/hwcap.h, RISCV_ISA_EXT_<ext>
In the hwcap.h, there's already
#define RISCV_ISA_EXT_ZAAMO 97
#define RISCV_ISA_EXT_ZALRSC 98
added by commit 35173b666a16 (riscv: add parsing for Zaamo and Zalrsc
extensions, 2024-06-19)
These 2 sets are logically coupled, and used in the BPF JUT code like
#define rv_ext_enabled(ext) \
(IS_ENABLED(CONFIG_RISCV_ISA_##ext) &&
riscv_has_extension_likely(RISCV_ISA_EXT_##ext))
So I thought if RISCV_ISA_EXT_ZALRSC exists although it is mandatory,
CONFIG_RISCV_ISA_ZALRSC counterpart should exist for the same reason.
Do you suggest this is different case and it is worth to remove
CONFIG_RISCV_ISA_ZALRSC anyway?
>> +
>> +config RISCV_ISA_A
>> + def_bool RISCV_ISA_ZAAMO && RISCV_ISA_ZALRSC
>
> I think this all needs a redo...
>
>> diff --git a/arch/riscv/include/asm/atomic.h b/arch/riscv/include/asm/atomic.h
>> index 616b8b332ac5..dc6a8c95c975 100644
>> --- a/arch/riscv/include/asm/atomic.h
>> +++ b/arch/riscv/include/asm/atomic.h
>> @@ -50,6 +50,7 @@ static __always_inline void arch_atomic64_set(atomic64_t *v, s64 i)
>> * have the AQ or RL bits set. These don't return anything, so there's only
>> * one version to worry about.
>> */
>> +#if defined(__riscv_atomic) || defined(__riscv_zaamo)
>> #define ATOMIC_OP(op, asm_op, I, asm_type, c_type, prefix) \
>> static __always_inline \
>> void arch_atomic##prefix##_##op(c_type i, atomic##prefix##_t *v) \
>> @@ -59,7 +60,25 @@ void arch_atomic##prefix##_##op(c_type i, atomic##prefix##_t *v) \
>> : "+A" (v->counter) \
>> : "r" (I) \
>> : "memory"); \
>> -} \
>> +}
>> +#elif defined(__riscv_zalrsc)
>
> ...why are we making this a compiletime decision in the first place?
> Why can't this be an alternative, starting with the zalrsc stuff and
> moving to the better implementation if available?
I started with alternatives approach, see
https://lore.kernel.org/all/20260114072552.731974-1-vladimir.kondratiev@mobileye.com/
Then I converted it to the static compile-time decision following input
from Gary Guo that I think is valid, here:
https://lore.kernel.org/all/DFSQIY6WV4U7.3B3RY31KE739B@garyguo.net/
> Is there some zaamo code that gets generated automatically that we lose
> out on by always building the kernel with zalrsc instead of with all of a?
There's BPF JIT that emits AMO instructions, patch for this is not ready
yet. These AMO instructions will be emulated and work correctly but very
slowly.
>> -KBUILD_BASE_ISA = -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64ima)fd([^v_]*)v?/\1\2/')
>> +KBUILD_BASE_ISA = -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima?|rv64ima?)fd([^v_]*)v?/\1\2/')
>> export KBUILD_BASE_ISA
>>
>> +# if "A" not supported, toolchain shall support zaamo/zalrsc
>
> Obviously this is not true, disabling some Kconfig option has no bearing
> on what a toolchain is capable of. You cannot put _foo in here without
> checking if the toolchain actually supports it.
> If no zaamo instructions are automatically generated, this dance isn't
> needed, so my suggestions for how to proceed depend on the answer to
> that question.
Fixed in v4
Thanks, Vladimir
next prev parent reply other threads:[~2026-11-03 12:57 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-26 9:20 [PATCH v3 0/2] riscv: support CPUs with Zalrsc but not Zaamo Vladimir Kondratiev
2026-10-26 9:20 ` [PATCH v3 1/2] riscv: build kernel atomics from LR/SC when Zaamo is disabled Vladimir Kondratiev
2026-10-31 1:54 ` kernel test robot
2026-10-31 2:14 ` kernel test robot
2026-11-02 18:46 ` Conor Dooley
2026-11-03 12:57 ` Vladimir Kondratiev [this message]
2026-10-26 9:20 ` [PATCH v3 2/2] riscv: accept the atomic extensions this kernel was built with Vladimir Kondratiev
2026-11-02 18:21 ` Conor Dooley
2026-11-03 14:31 ` Vladimir Kondratiev
2026-10-26 10:30 ` [PATCH v3 0/2] riscv: support CPUs with Zalrsc but not Zaamo Peter Zijlstra
2026-10-26 11:06 ` Vladimir Kondratiev
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=1bcc885c-b013-4ab3-9b08-1d1f89b09762@mobileye.com \
--to=vladimir.kondratiev@mobileye.com \
--cc=aleksa.paunovic@htecgroup.com \
--cc=alex@ghiti.fr \
--cc=aou@eecs.berkeley.edu \
--cc=arikalo@gmail.com \
--cc=boqun@kernel.org \
--cc=cfu@wavecomp.com \
--cc=conor@kernel.org \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=linux@rasmusvillemoes.dk \
--cc=mark.rutland@arm.com \
--cc=olof@lixom.net \
--cc=palmer@dabbelt.com \
--cc=peterz@infradead.org \
--cc=pjw@kernel.org \
--cc=torvalds@linux-foundation.org \
--cc=will@kernel.org \
--cc=yury.norov@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
all inboxes | Powered by JetHome®