linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC 0/2] Initial LKMM atomics support in Rust
@ 2024-06-12 22:30 Boqun Feng
  2024-06-12 22:30 ` [RFC 1/2] rust: Introduce atomic API helpers Boqun Feng
  2024-06-12 22:30 ` [RFC 2/2] rust: sync: Add atomic support Boqun Feng
  0 siblings, 2 replies; 56+ messages in thread
From: Boqun Feng @ 2024-06-12 22:30 UTC (permalink / raw)
  To: rust-for-linux, linux-kernel, linux-arch, llvm
  Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Alice Ryhl, Alan Stern, Andrea Parri, Will Deacon, Peter Zijlstra,
	Nicholas Piggin, David Howells, Jade Alglave, Luc Maranget,
	Paul E. McKenney, Akira Yokosawa, Daniel Lustig, Joel Fernandes,
	Nathan Chancellor, Nick Desaulniers, kent.overstreet,
	Greg Kroah-Hartman, elver, Mark Rutland, Thomas Gleixner,
	Ingo Molnar, Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
	Catalin Marinas, torvalds, linux-arm-kernel, linux-fsdevel,
	Trevor Gross, dakr

Hi,

This is a follow-up of [1]. Thanks for all the inputs from that thread.
I use Mark's outline atomic scripts, but make them specific for atomics
in Rust. The reason is that I want to use Gary's work [2], and inline
atomics if possible in Rust code. My local test can confirm it works:

With RUST_LTO_HELPERS=n

  224edc:       52800180        mov     w0, #0xc                        // #12
  224ee0:       94000000        bl      219640 <rust_helper_atomic_fetch_add_relaxed>

With RUST_LTO_HELPERS=y

  222fd4:       52800189        mov     w9, #0xc                        // #12
  222fd8:       b8290108        ldadd   w9, w8, [x8]


Only AtomicI32 (atomic_t) and AtomicI64 (atomic64_t) are added, and for
AtomicPtr (atomic pointers) I plan to implement with compile-time
selection on either of these two.

You can find a branch contains this series and Gray's patchset at:

	https://github.com/fbq/linux.git dev/rust/atomic-rfc


For testing, I randomly picked up some function and inspected the
generated code, plus Rust code can use the function document as tests,
so I added two tests there. Ideally we should have something similar to
lib/atomic64_test.c, but I want to get some feedback on the
implementation part first, plus it's just using C functions, so as long
as C code passes the tests, it should be fine (famous last words).

ARM64 maintainers, I use the following to simulate cases where LSE is
configured but not available from hardware:

diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c
index 48e7029f1054..99e6e2b2867f 100644
--- a/arch/arm64/kernel/cpufeature.c
+++ b/arch/arm64/kernel/cpufeature.c
@@ -1601,6 +1601,8 @@ static bool
 has_cpuid_feature(const struct arm64_cpu_capabilities *entry, int scope)
 {
        u64 val = read_scoped_sysreg(entry, scope);
+       if (entry->capability == ARM64_HAS_LSE_ATOMICS)
+               return false;
        return feature_matches(val, entry);
 }

and my tests in a qemu emulated VM passed for both RUST_LTO_HELPERS=n
and =y cases. Let me know what I can also do to test this.


Notes for people who are working on Rust code and need atomics, my
target of this set of APIs is 6.11 or 6.12 (will work hard on it, but no
guarantee ;-)). If you are currently using Rust own atomics, it's OK, we
can always clean up quickly after this merged.

Regards,
Boqun

[1]: https://lore.kernel.org/rust-for-linux/20240322233838.868874-1-boqun.feng@gmail.com/
[2]: https://lore.kernel.org/rust-for-linux/20240529202817.3641974-1-gary@garyguo.net/

Boqun Feng (2):
  rust: Introduce atomic API helpers
  rust: sync: Add atomic support

 MAINTAINERS                               |    4 +-
 arch/arm64/kernel/cpufeature.c            |    2 +
 rust/atomic_helpers.h                     | 1035 ++++++++++++++++
 rust/helpers.c                            |    2 +
 rust/kernel/sync.rs                       |    1 +
 rust/kernel/sync/atomic.rs                |   63 +
 rust/kernel/sync/atomic/impl.rs           | 1375 +++++++++++++++++++++
 scripts/atomic/gen-atomics.sh             |    2 +
 scripts/atomic/gen-rust-atomic-helpers.sh |   64 +
 scripts/atomic/gen-rust-atomic.sh         |  136 ++
 10 files changed, 2683 insertions(+), 1 deletion(-)
 create mode 100644 rust/atomic_helpers.h
 create mode 100644 rust/kernel/sync/atomic.rs
 create mode 100644 rust/kernel/sync/atomic/impl.rs
 create mode 100755 scripts/atomic/gen-rust-atomic-helpers.sh
 create mode 100755 scripts/atomic/gen-rust-atomic.sh

-- 
2.45.2


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

end of thread, other threads:[~2024-06-19 15:00 UTC | newest]

Thread overview: 56+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-12 22:30 [RFC 0/2] Initial LKMM atomics support in Rust Boqun Feng
2024-06-12 22:30 ` [RFC 1/2] rust: Introduce atomic API helpers Boqun Feng
2024-06-13  5:38   ` Greg Kroah-Hartman
2024-06-13  9:17     ` Peter Zijlstra
2024-06-13 10:03       ` Greg Kroah-Hartman
2024-06-13 10:36       ` Mark Rutland
2024-06-14 10:31   ` Mark Rutland
2024-06-14 20:13     ` Boqun Feng
2024-06-12 22:30 ` [RFC 2/2] rust: sync: Add atomic support Boqun Feng
2024-06-13  5:40   ` Greg Kroah-Hartman
2024-06-13 13:44   ` Gary Guo
2024-06-13 16:30     ` Boqun Feng
2024-06-13 17:19       ` Gary Guo
2024-06-13 17:22       ` Miguel Ojeda
2024-06-13 19:05         ` Boqun Feng
2024-06-14  9:59           ` Miguel Ojeda
2024-06-14 14:33             ` Boqun Feng
2024-06-14 21:22               ` Benno Lossin
2024-06-15  1:33                 ` Boqun Feng
2024-06-15  7:09                   ` Benno Lossin
2024-06-15 22:12                     ` Boqun Feng
2024-06-16  9:46                       ` Benno Lossin
2024-06-16 14:08                         ` Boqun Feng
2024-06-16 15:06                           ` Benno Lossin
2024-06-16 15:34                             ` Boqun Feng
2024-06-16 15:55                               ` Benno Lossin
2024-06-16 16:30                                 ` Boqun Feng
2024-06-19  9:09                                   ` Benno Lossin
2024-06-19 15:00                                     ` Boqun Feng
2024-06-16 17:05                         ` Boqun Feng
2024-06-16  9:51                       ` Kent Overstreet
2024-06-16 14:16                         ` Boqun Feng
2024-06-16 14:35                           ` Boqun Feng
2024-06-16 15:14                           ` Miguel Ojeda
2024-06-16 15:32                             ` Kent Overstreet
2024-06-16 15:54                               ` Boqun Feng
2024-06-16 17:30                                 ` Boqun Feng
2024-06-16 17:59                                   ` Kent Overstreet
2024-06-16 15:50                             ` Boqun Feng
2024-06-16 15:23                           ` Kent Overstreet
2024-06-15  1:03             ` John Hubbard
2024-06-15  1:24               ` Boqun Feng
2024-06-15  1:28                 ` John Hubbard
2024-06-15  2:39                   ` Boqun Feng
2024-06-15  2:51                     ` John Hubbard
2024-06-16 14:51                     ` Gary Guo
2024-06-16 15:06                       ` Boqun Feng
2024-06-17  5:36                         ` Boqun Feng
2024-06-17  5:42                           ` Boqun Feng
2024-06-19  9:30                           ` Benno Lossin
2024-06-16  0:51             ` Andrew Lunn
2024-06-14  9:51       ` Peter Zijlstra
2024-06-14 14:18         ` Boqun Feng
2024-06-13 20:25   ` Boqun Feng
2024-06-14 10:40   ` Mark Rutland
2024-06-14 20:20     ` Boqun Feng

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 NNTP newsgroup(s).