Generic Linux architectural discussions
 help / color / mirror / Atom feed
* [PATCH v1 0/4] rust: Add i8/i16 atomic try_cmpxchg helpers
@ 2025-12-27 11:59 FUJITA Tomonori
  2025-12-27 11:59 ` [PATCH v1 1/4] rust: helpers: " FUJITA Tomonori
                   ` (4 more replies)
  0 siblings, 5 replies; 8+ messages in thread
From: FUJITA Tomonori @ 2025-12-27 11:59 UTC (permalink / raw)
  To: boqun.feng, ojeda
  Cc: a.hindborg, aliceryhl, bjorn3_gh, dakr, gary, lossin, tmgross,
	acourbot, rust-for-linux, linux-arch

This series adds Rust helpers for atomic try_cmpxchg on i8/i16 with
full, acquire, release, and relaxed orderings in preparation for
supporting Rust-side cmpxchg on those types.

Rust atomic cmpxchg is implemented on top of the kernel C APIs,
try_cmpxchg.

On architectures that support Rust today, the kernel already provides
try_cmpxchg implementations that work for i8/i16, using
architecture-specific instructions.

Tested on QEMU (86_64, arm64, riscv, loongarch, and armv7).

Follow-up patches will add Rust users of these helpers.

Boqun, feel free to drop the ordering-related notes from each patch.

FUJITA Tomonori (4):
  rust: helpers: Add i8/i16 atomic try_cmpxchg helpers
  rust: helpers: Add i8/i16 atomic try_cmpxchg_acquire helpers
  rust: helpers: Add i8/i16 atomic try_cmpxchg_release helpers
  rust: helpers: Add i8/i16 atomic try_cmpxchg_relaxed helpers

 rust/helpers/atomic_ext.c | 40 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)


base-commit: 30f5de001fb2ffacdb61e82c8626cae2d68b4d03
-- 
2.43.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v1 1/4] rust: helpers: Add i8/i16 atomic try_cmpxchg helpers
  2025-12-27 11:59 [PATCH v1 0/4] rust: Add i8/i16 atomic try_cmpxchg helpers FUJITA Tomonori
@ 2025-12-27 11:59 ` FUJITA Tomonori
  2025-12-29 12:06   ` Boqun Feng
  2025-12-27 11:59 ` [PATCH v1 2/4] rust: helpers: Add i8/i16 atomic try_cmpxchg_acquire helpers FUJITA Tomonori
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 8+ messages in thread
From: FUJITA Tomonori @ 2025-12-27 11:59 UTC (permalink / raw)
  To: boqun.feng, ojeda
  Cc: a.hindborg, aliceryhl, bjorn3_gh, dakr, gary, lossin, tmgross,
	acourbot, rust-for-linux, linux-arch

Add i8/i16 atomic try_cmpxchg helpers that call raw_try_cmpxchg()
macro implementing atomic try_cmpxchg using architecture-specific
instructions.

x86_64 implements try_cmpxchg() with full ordering.

On other architectures, try_cmpxchg() isn't implemented; so calling
try_cmpxchg() ends up using cmpxchg() implementation.

loongarch, arm64, and riscv implement cmpxchg with full ordering.

arm v7 only supports relaxed-ordering cmpxchg; __atomic_op_fence()
macro is used to add barriers before and after the relaxed cmpxchg.

Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
 rust/helpers/atomic_ext.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/rust/helpers/atomic_ext.c b/rust/helpers/atomic_ext.c
index 089f31bc8de8..9d62f659c8d2 100644
--- a/rust/helpers/atomic_ext.c
+++ b/rust/helpers/atomic_ext.c
@@ -90,3 +90,13 @@ __rust_helper s16 rust_helper_atomic_i16_xchg_relaxed(s16 *ptr, s16 new)
 {
 	return raw_xchg_relaxed(ptr, new);
 }
+
+__rust_helper bool rust_helper_atomic_i8_try_cmpxchg(s8 *ptr, s8 *old, s8 new)
+{
+	return raw_try_cmpxchg(ptr, old, new);
+}
+
+__rust_helper bool rust_helper_atomic_i16_try_cmpxchg(s16 *ptr, s16 *old, s16 new)
+{
+	return raw_try_cmpxchg(ptr, old, new);
+}
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v1 2/4] rust: helpers: Add i8/i16 atomic try_cmpxchg_acquire helpers
  2025-12-27 11:59 [PATCH v1 0/4] rust: Add i8/i16 atomic try_cmpxchg helpers FUJITA Tomonori
  2025-12-27 11:59 ` [PATCH v1 1/4] rust: helpers: " FUJITA Tomonori
@ 2025-12-27 11:59 ` FUJITA Tomonori
  2025-12-27 11:59 ` [PATCH v1 3/4] rust: helpers: Add i8/i16 atomic try_cmpxchg_release helpers FUJITA Tomonori
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: FUJITA Tomonori @ 2025-12-27 11:59 UTC (permalink / raw)
  To: boqun.feng, ojeda
  Cc: a.hindborg, aliceryhl, bjorn3_gh, dakr, gary, lossin, tmgross,
	acourbot, rust-for-linux, linux-arch

Add i8/i16 atomic try_cmpxchg_acquire helpers that call
raw_try_cmpxchg_acquire() macro implementing atomic
try_cmpxchg_acquire using architecture-specific instructions.

x86_64 uses full-ordering try_cmpxchg().

On other architectures, try_cmpxchg_acquire() isn't implemented; so
calling try_cmpxchg_acquire() ends up using cmpxchg_acquire()
implementation.

arm64 and riscv implement acquire-ordering cmpxchg.

loongarch uses full-ordering cmpxchg().

arm v7 only supports relaxed-ordering cmpxchg; __atomic_op_fence()
macro is used to add barriers after the relaxed cmpxchg.

Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
 rust/helpers/atomic_ext.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/rust/helpers/atomic_ext.c b/rust/helpers/atomic_ext.c
index 9d62f659c8d2..69a258ebbfa0 100644
--- a/rust/helpers/atomic_ext.c
+++ b/rust/helpers/atomic_ext.c
@@ -100,3 +100,13 @@ __rust_helper bool rust_helper_atomic_i16_try_cmpxchg(s16 *ptr, s16 *old, s16 ne
 {
 	return raw_try_cmpxchg(ptr, old, new);
 }
+
+__rust_helper bool rust_helper_atomic_i8_try_cmpxchg_acquire(s8 *ptr, s8 *old, s8 new)
+{
+	return raw_try_cmpxchg_acquire(ptr, old, new);
+}
+
+__rust_helper bool rust_helper_atomic_i16_try_cmpxchg_acquire(s16 *ptr, s16 *old, s16 new)
+{
+	return raw_try_cmpxchg_acquire(ptr, old, new);
+}
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v1 3/4] rust: helpers: Add i8/i16 atomic try_cmpxchg_release helpers
  2025-12-27 11:59 [PATCH v1 0/4] rust: Add i8/i16 atomic try_cmpxchg helpers FUJITA Tomonori
  2025-12-27 11:59 ` [PATCH v1 1/4] rust: helpers: " FUJITA Tomonori
  2025-12-27 11:59 ` [PATCH v1 2/4] rust: helpers: Add i8/i16 atomic try_cmpxchg_acquire helpers FUJITA Tomonori
@ 2025-12-27 11:59 ` FUJITA Tomonori
  2025-12-27 11:59 ` [PATCH v1 4/4] rust: helpers: Add i8/i16 atomic try_cmpxchg_relaxed helpers FUJITA Tomonori
  2025-12-29 11:27 ` [PATCH v1 0/4] rust: Add i8/i16 atomic try_cmpxchg helpers Boqun Feng
  4 siblings, 0 replies; 8+ messages in thread
From: FUJITA Tomonori @ 2025-12-27 11:59 UTC (permalink / raw)
  To: boqun.feng, ojeda
  Cc: a.hindborg, aliceryhl, bjorn3_gh, dakr, gary, lossin, tmgross,
	acourbot, rust-for-linux, linux-arch

Add i8/i16 atomic try_cmpxchg_release helpers that call
raw_try_cmpxchg_release() macro implementing atomic
try_cmpxchg_release using architecture-specific instructions.

x86_64 uses full-ordering try_cmpxchg().

On other architectures, try_cmpxchg_release() isn't implemented; so
calling try_cmpxchg_release() ends up using cmpxchg_release()
implementation.

arm64 and riscv implement release-ordering cmpxchg.

loongarch uses full-ordering cmpxchg().

arm v7 only supports relaxed-ordering cmpxchg; __atomic_op_fence()
macro is used to add barriers before the relaxed cmpxchg.

Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
 rust/helpers/atomic_ext.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/rust/helpers/atomic_ext.c b/rust/helpers/atomic_ext.c
index 69a258ebbfa0..589c070f589b 100644
--- a/rust/helpers/atomic_ext.c
+++ b/rust/helpers/atomic_ext.c
@@ -110,3 +110,13 @@ __rust_helper bool rust_helper_atomic_i16_try_cmpxchg_acquire(s16 *ptr, s16 *old
 {
 	return raw_try_cmpxchg_acquire(ptr, old, new);
 }
+
+__rust_helper bool rust_helper_atomic_i8_try_cmpxchg_release(s8 *ptr, s8 *old, s8 new)
+{
+	return raw_try_cmpxchg_release(ptr, old, new);
+}
+
+__rust_helper bool rust_helper_atomic_i16_try_cmpxchg_release(s16 *ptr, s16 *old, s16 new)
+{
+	return raw_try_cmpxchg_release(ptr, old, new);
+}
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v1 4/4] rust: helpers: Add i8/i16 atomic try_cmpxchg_relaxed helpers
  2025-12-27 11:59 [PATCH v1 0/4] rust: Add i8/i16 atomic try_cmpxchg helpers FUJITA Tomonori
                   ` (2 preceding siblings ...)
  2025-12-27 11:59 ` [PATCH v1 3/4] rust: helpers: Add i8/i16 atomic try_cmpxchg_release helpers FUJITA Tomonori
@ 2025-12-27 11:59 ` FUJITA Tomonori
  2025-12-29 11:27 ` [PATCH v1 0/4] rust: Add i8/i16 atomic try_cmpxchg helpers Boqun Feng
  4 siblings, 0 replies; 8+ messages in thread
From: FUJITA Tomonori @ 2025-12-27 11:59 UTC (permalink / raw)
  To: boqun.feng, ojeda
  Cc: a.hindborg, aliceryhl, bjorn3_gh, dakr, gary, lossin, tmgross,
	acourbot, rust-for-linux, linux-arch

Add i8/i16 atomic try_cmpxchg_relaxed helpers that call
raw_try_cmpxchg_relaxed() macro implementing atomic
try_cmpxchg_relaxed using architecture-specific instructions.

x86_64 uses full-ordering try_cmpxchg().

On other architectures, try_cmpxchg_relaxed() isn't implemented; so
calling try_cmpxchg_relaxed() ends up using cmpxchg_relaxed()
implementation.

arm64, riscv, and arm v7 implement relaxed-ordering cmpxchg.

loongarch uses full-ordering cmpxchg().

Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
 rust/helpers/atomic_ext.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/rust/helpers/atomic_ext.c b/rust/helpers/atomic_ext.c
index 589c070f589b..3a5ef6bb2776 100644
--- a/rust/helpers/atomic_ext.c
+++ b/rust/helpers/atomic_ext.c
@@ -120,3 +120,13 @@ __rust_helper bool rust_helper_atomic_i16_try_cmpxchg_release(s16 *ptr, s16 *old
 {
 	return raw_try_cmpxchg_release(ptr, old, new);
 }
+
+__rust_helper bool rust_helper_atomic_i8_try_cmpxchg_relaxed(s8 *ptr, s8 *old, s8 new)
+{
+	return raw_try_cmpxchg_relaxed(ptr, old, new);
+}
+
+__rust_helper bool rust_helper_atomic_i16_try_cmpxchg_relaxed(s16 *ptr, s16 *old, s16 new)
+{
+	return raw_try_cmpxchg_relaxed(ptr, old, new);
+}
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v1 0/4] rust: Add i8/i16 atomic try_cmpxchg helpers
  2025-12-27 11:59 [PATCH v1 0/4] rust: Add i8/i16 atomic try_cmpxchg helpers FUJITA Tomonori
                   ` (3 preceding siblings ...)
  2025-12-27 11:59 ` [PATCH v1 4/4] rust: helpers: Add i8/i16 atomic try_cmpxchg_relaxed helpers FUJITA Tomonori
@ 2025-12-29 11:27 ` Boqun Feng
  4 siblings, 0 replies; 8+ messages in thread
From: Boqun Feng @ 2025-12-29 11:27 UTC (permalink / raw)
  To: FUJITA Tomonori
  Cc: ojeda, a.hindborg, aliceryhl, bjorn3_gh, dakr, gary, lossin,
	tmgross, acourbot, rust-for-linux, linux-arch

On Sat, Dec 27, 2025 at 08:59:47PM +0900, FUJITA Tomonori wrote:
> This series adds Rust helpers for atomic try_cmpxchg on i8/i16 with
> full, acquire, release, and relaxed orderings in preparation for
> supporting Rust-side cmpxchg on those types.
> 
> Rust atomic cmpxchg is implemented on top of the kernel C APIs,
> try_cmpxchg.
> 
> On architectures that support Rust today, the kernel already provides
> try_cmpxchg implementations that work for i8/i16, using
> architecture-specific instructions.
> 
> Tested on QEMU (86_64, arm64, riscv, loongarch, and armv7).
> 
> Follow-up patches will add Rust users of these helpers.
> 
> Boqun, feel free to drop the ordering-related notes from each patch.
> 

Queued, thanks!

Regards,
Boqun

> FUJITA Tomonori (4):
>   rust: helpers: Add i8/i16 atomic try_cmpxchg helpers
>   rust: helpers: Add i8/i16 atomic try_cmpxchg_acquire helpers
>   rust: helpers: Add i8/i16 atomic try_cmpxchg_release helpers
>   rust: helpers: Add i8/i16 atomic try_cmpxchg_relaxed helpers
> 
>  rust/helpers/atomic_ext.c | 40 +++++++++++++++++++++++++++++++++++++++
>  1 file changed, 40 insertions(+)
> 
> 
> base-commit: 30f5de001fb2ffacdb61e82c8626cae2d68b4d03
> -- 
> 2.43.0
> 

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v1 1/4] rust: helpers: Add i8/i16 atomic try_cmpxchg helpers
  2025-12-27 11:59 ` [PATCH v1 1/4] rust: helpers: " FUJITA Tomonori
@ 2025-12-29 12:06   ` Boqun Feng
  2025-12-29 12:57     ` FUJITA Tomonori
  0 siblings, 1 reply; 8+ messages in thread
From: Boqun Feng @ 2025-12-29 12:06 UTC (permalink / raw)
  To: FUJITA Tomonori
  Cc: ojeda, a.hindborg, aliceryhl, bjorn3_gh, dakr, gary, lossin,
	tmgross, acourbot, rust-for-linux, linux-arch

On Sat, Dec 27, 2025 at 08:59:48PM +0900, FUJITA Tomonori wrote:
> Add i8/i16 atomic try_cmpxchg helpers that call raw_try_cmpxchg()
> macro implementing atomic try_cmpxchg using architecture-specific
> instructions.
> 
> x86_64 implements try_cmpxchg() with full ordering.
> 
> On other architectures, try_cmpxchg() isn't implemented; so calling
> try_cmpxchg() ends up using cmpxchg() implementation.
> 
> loongarch, arm64, and riscv implement cmpxchg with full ordering.
> 
> arm v7 only supports relaxed-ordering cmpxchg; __atomic_op_fence()
> macro is used to add barriers before and after the relaxed cmpxchg.
> 
> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
> ---

One more thing, we should add this to comments in atomic_ext.c as well:

diff --git a/rust/helpers/atomic_ext.c b/rust/helpers/atomic_ext.c
index 10733bb4a75e..5ef81d2b47cf 100644
--- a/rust/helpers/atomic_ext.c
+++ b/rust/helpers/atomic_ext.c
@@ -91,6 +91,13 @@ __rust_helper s16 rust_helper_atomic_i16_xchg_relaxed(s16 *ptr, s16 new)
        return raw_xchg_relaxed(ptr, new);
 }

+/*
+ * try_cmpxchg helpers depend on ARCH_SUPPORTS_ATOMIC_RMW and on the
+ * architecture provding try_cmpxchg() support for i8 and i16.
+ *
+ * The architectures that currently support Rust (x86_64, armv7,
+ * arm64, riscv, and loongarch) satisfy these requirements.
+ */
 __rust_helper bool rust_helper_atomic_i8_try_cmpxchg(s8 *ptr, s8 *old, s8 new)
 {
        return raw_try_cmpxchg(ptr, old, new);

I can add it myself if it works for you.

Regards,
Boqun

>  rust/helpers/atomic_ext.c | 10 ++++++++++
>  1 file changed, 10 insertions(+)
> 
> diff --git a/rust/helpers/atomic_ext.c b/rust/helpers/atomic_ext.c
> index 089f31bc8de8..9d62f659c8d2 100644
> --- a/rust/helpers/atomic_ext.c
> +++ b/rust/helpers/atomic_ext.c
> @@ -90,3 +90,13 @@ __rust_helper s16 rust_helper_atomic_i16_xchg_relaxed(s16 *ptr, s16 new)
>  {
>  	return raw_xchg_relaxed(ptr, new);
>  }
> +
> +__rust_helper bool rust_helper_atomic_i8_try_cmpxchg(s8 *ptr, s8 *old, s8 new)
> +{
> +	return raw_try_cmpxchg(ptr, old, new);
> +}
> +
> +__rust_helper bool rust_helper_atomic_i16_try_cmpxchg(s16 *ptr, s16 *old, s16 new)
> +{
> +	return raw_try_cmpxchg(ptr, old, new);
> +}
> -- 
> 2.43.0
> 

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v1 1/4] rust: helpers: Add i8/i16 atomic try_cmpxchg helpers
  2025-12-29 12:06   ` Boqun Feng
@ 2025-12-29 12:57     ` FUJITA Tomonori
  0 siblings, 0 replies; 8+ messages in thread
From: FUJITA Tomonori @ 2025-12-29 12:57 UTC (permalink / raw)
  To: boqun.feng
  Cc: fujita.tomonori, ojeda, a.hindborg, aliceryhl, bjorn3_gh, dakr,
	gary, lossin, tmgross, acourbot, rust-for-linux, linux-arch

On Mon, 29 Dec 2025 20:06:25 +0800
Boqun Feng <boqun.feng@gmail.com> wrote:

> On Sat, Dec 27, 2025 at 08:59:48PM +0900, FUJITA Tomonori wrote:
>> Add i8/i16 atomic try_cmpxchg helpers that call raw_try_cmpxchg()
>> macro implementing atomic try_cmpxchg using architecture-specific
>> instructions.
>> 
>> x86_64 implements try_cmpxchg() with full ordering.
>> 
>> On other architectures, try_cmpxchg() isn't implemented; so calling
>> try_cmpxchg() ends up using cmpxchg() implementation.
>> 
>> loongarch, arm64, and riscv implement cmpxchg with full ordering.
>> 
>> arm v7 only supports relaxed-ordering cmpxchg; __atomic_op_fence()
>> macro is used to add barriers before and after the relaxed cmpxchg.
>> 
>> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
>> ---
> 
> One more thing, we should add this to comments in atomic_ext.c as well:
> 
> diff --git a/rust/helpers/atomic_ext.c b/rust/helpers/atomic_ext.c
> index 10733bb4a75e..5ef81d2b47cf 100644
> --- a/rust/helpers/atomic_ext.c
> +++ b/rust/helpers/atomic_ext.c
> @@ -91,6 +91,13 @@ __rust_helper s16 rust_helper_atomic_i16_xchg_relaxed(s16 *ptr, s16 new)
>         return raw_xchg_relaxed(ptr, new);
>  }
> 
> +/*
> + * try_cmpxchg helpers depend on ARCH_SUPPORTS_ATOMIC_RMW and on the
> + * architecture provding try_cmpxchg() support for i8 and i16.
> + *
> + * The architectures that currently support Rust (x86_64, armv7,
> + * arm64, riscv, and loongarch) satisfy these requirements.
> + */
>  __rust_helper bool rust_helper_atomic_i8_try_cmpxchg(s8 *ptr, s8 *old, s8 new)
>  {
>         return raw_try_cmpxchg(ptr, old, new);
> 
> I can add it myself if it works for you.

Thanks, please add it.


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2025-12-29 12:57 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-27 11:59 [PATCH v1 0/4] rust: Add i8/i16 atomic try_cmpxchg helpers FUJITA Tomonori
2025-12-27 11:59 ` [PATCH v1 1/4] rust: helpers: " FUJITA Tomonori
2025-12-29 12:06   ` Boqun Feng
2025-12-29 12:57     ` FUJITA Tomonori
2025-12-27 11:59 ` [PATCH v1 2/4] rust: helpers: Add i8/i16 atomic try_cmpxchg_acquire helpers FUJITA Tomonori
2025-12-27 11:59 ` [PATCH v1 3/4] rust: helpers: Add i8/i16 atomic try_cmpxchg_release helpers FUJITA Tomonori
2025-12-27 11:59 ` [PATCH v1 4/4] rust: helpers: Add i8/i16 atomic try_cmpxchg_relaxed helpers FUJITA Tomonori
2025-12-29 11:27 ` [PATCH v1 0/4] rust: Add i8/i16 atomic try_cmpxchg helpers Boqun Feng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox