All of lore.kernel.org
 help / color / mirror / Atom feed
* [WIP 0/3] Memory model and atomic API in Rust
@ 2024-03-22 23:38 ` Boqun Feng
  0 siblings, 0 replies; 152+ messages in thread
From: Boqun Feng @ 2024-03-22 23:38 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

Hi,

Since I see more and more Rust code is comming in, I feel like this
should be sent sooner rather than later, so here is a WIP to open the
discussion and get feedback.

One of the most important questions we need to answer is: which
memory (ordering) model we should use when developing Rust in Linux
kernel, given Rust has its own memory ordering model[1]. I had some
discussion with Rust language community to understand their position
on this:

	https://github.com/rust-lang/unsafe-code-guidelines/issues/348#issuecomment-1218407557
	https://github.com/rust-lang/unsafe-code-guidelines/issues/476#issue-2001382992

My takeaway from these discussions, along with other offline discussion
is that supporting two memory models is challenging for both correctness
reasoning (some one needs to provide a model) and implementation (one
model needs to be aware of the other model). So that's not wise to do
(at least at the beginning). So the most reasonable option to me is:

	we only use LKMM for Rust code in kernel (i.e. avoid using
	Rust's own atomic).

Because kernel developers are more familiar with LKMM and when Rust code
interacts with C code, it has to use the model that C code uses.


And this patchset is the result of that option. I introduced an atomic
library to wrap and implement LKMM atomics (of course, given it's a WIP,
so it's unfinished). Things to notice:

* I know I could use Rust macro to generate the whole set of atomics,
  but I choose not to in the beginning, as I want to make it easier to
  review.

* Very likely, we will only have AtomicI32, AtomicI64 and AtomicUsize
  (i.e no atomic for bool, u8, u16, etc), with limited support for
  atomic load and store on 8/16 bits.

* I choose to re-implement atomics in Rust `asm` because we are still
  figuring out how we can make it easy and maintainable for Rust to call
  a C function _inlinely_ (Gary makes some progress [2]). Otherwise,
  atomic primitives would be function calls, and that can be performance
  bottleneck in a few cases.

* I only have two API implemented and two architecture supported yet,
  the complete support surely can be added when everyone is on the same
  page.


Any suggestion, question, review, help is welcome!

Regards,
Boqun

[1]: https://doc.rust-lang.org/std/sync/atomic/#memory-model-for-atomic-accesses
[2]: https://rust-for-linux.zulipchat.com/#narrow/stream/288089-General/topic/LTO.20Rust.20modules.20with.20C.20helpers/near/425361365

Boqun Feng (3):
  rust: Introduce atomic module
  rust: atomic: Add ARM64 fetch_add_relaxed()
  rust: atomic: Add fetch_sub_release()

 rust/kernel/sync.rs                   |  1 +
 rust/kernel/sync/atomic.rs            | 65 +++++++++++++++++++++++++++
 rust/kernel/sync/atomic/arch.rs       | 15 +++++++
 rust/kernel/sync/atomic/arch/arm64.rs | 46 +++++++++++++++++++
 rust/kernel/sync/atomic/arch/x86.rs   | 48 ++++++++++++++++++++
 5 files changed, 175 insertions(+)
 create mode 100644 rust/kernel/sync/atomic.rs
 create mode 100644 rust/kernel/sync/atomic/arch.rs
 create mode 100644 rust/kernel/sync/atomic/arch/arm64.rs
 create mode 100644 rust/kernel/sync/atomic/arch/x86.rs

-- 
2.44.0


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

end of thread, other threads:[~2024-04-16 18:12 UTC | newest]

Thread overview: 152+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-22 23:38 [WIP 0/3] Memory model and atomic API in Rust Boqun Feng
2024-03-22 23:38 ` Boqun Feng
2024-03-22 23:38 ` [WIP 1/3] rust: Introduce atomic module Boqun Feng
2024-03-22 23:38   ` Boqun Feng
2024-03-22 23:52   ` Andrew Lunn
2024-03-22 23:52     ` Andrew Lunn
2024-03-23  0:03     ` Boqun Feng
2024-03-23  0:03       ` Boqun Feng
2024-03-23 19:13       ` Miguel Ojeda
2024-03-23 19:13         ` Miguel Ojeda
2024-03-23 19:30         ` Boqun Feng
2024-03-23 19:30           ` Boqun Feng
2024-03-23  9:58     ` Alice Ryhl
2024-03-23  9:58       ` Alice Ryhl
2024-03-23 14:10       ` Andrew Lunn
2024-03-23 14:10         ` Andrew Lunn
2024-03-23 19:09         ` Miguel Ojeda
2024-03-23 19:09           ` Miguel Ojeda
2024-03-26  5:56         ` Trevor Gross
2024-03-26  5:56           ` Trevor Gross
2024-03-22 23:38 ` [WIP 2/3] rust: atomic: Add ARM64 fetch_add_relaxed() Boqun Feng
2024-03-22 23:38   ` Boqun Feng
2024-03-22 23:38 ` [WIP 3/3] rust: atomic: Add fetch_sub_release() Boqun Feng
2024-03-22 23:38   ` Boqun Feng
2024-03-22 23:57 ` [WIP 0/3] Memory model and atomic API in Rust Kent Overstreet
2024-03-22 23:57   ` Kent Overstreet
2024-03-23  0:12   ` Linus Torvalds
2024-03-23  0:12     ` Linus Torvalds
2024-03-23  0:21     ` Kent Overstreet
2024-03-23  0:21       ` Kent Overstreet
2024-03-23  0:36       ` Linus Torvalds
2024-03-23  0:36         ` Linus Torvalds
2024-03-23  2:07         ` Kent Overstreet
2024-03-23  2:07           ` Kent Overstreet
2024-03-23  2:26           ` Boqun Feng
2024-03-23  2:26             ` Boqun Feng
2024-03-23  2:33             ` Kent Overstreet
2024-03-23  2:33               ` Kent Overstreet
2024-03-23  2:57               ` Boqun Feng
2024-03-23  2:57                 ` Boqun Feng
2024-03-23  3:10                 ` Kent Overstreet
2024-03-23  3:10                   ` Kent Overstreet
2024-03-23  3:51                   ` Boqun Feng
2024-03-23  3:51                     ` Boqun Feng
2024-03-23  4:16                     ` Kent Overstreet
2024-03-23  4:16                       ` Kent Overstreet
2024-03-25 13:56         ` Philipp Stanner
2024-03-25 13:56           ` Philipp Stanner
2024-03-25 17:44           ` Linus Torvalds
2024-03-25 17:44             ` Linus Torvalds
2024-03-25 18:59             ` Kent Overstreet
2024-03-25 18:59               ` Kent Overstreet
2024-03-25 19:44               ` Linus Torvalds
2024-03-25 19:44                 ` Linus Torvalds
2024-03-25 21:14                 ` Kent Overstreet
2024-03-25 21:14                   ` Kent Overstreet
2024-03-25 21:37                   ` Boqun Feng
2024-03-25 21:37                     ` Boqun Feng
2024-03-25 22:09                     ` Kent Overstreet
2024-03-25 22:09                       ` Kent Overstreet
2024-03-25 22:38                       ` Boqun Feng
2024-03-25 22:38                         ` Boqun Feng
2024-03-25 23:02                         ` Kent Overstreet
2024-03-25 23:02                           ` Kent Overstreet
2024-03-25 23:41                           ` Boqun Feng
2024-03-25 23:41                             ` Boqun Feng
2024-03-26  0:05                 ` Dr. David Alan Gilbert
2024-03-26  0:05                   ` Dr. David Alan Gilbert
2024-03-26  0:36                   ` Kent Overstreet
2024-03-26  0:36                     ` Kent Overstreet
2024-03-26  1:35                     ` Dr. David Alan Gilbert
2024-03-26  1:35                       ` Dr. David Alan Gilbert
2024-03-26  3:28                       ` Kent Overstreet
2024-03-26  3:28                         ` Kent Overstreet
2024-03-26  2:51                   ` Boqun Feng
2024-03-26  2:51                     ` Boqun Feng
2024-03-26  3:49                   ` Linus Torvalds
2024-03-26  3:49                     ` Linus Torvalds
2024-03-26 14:35                     ` Dr. David Alan Gilbert
2024-03-26 14:35                       ` Dr. David Alan Gilbert
2024-03-27 16:16                     ` comex
2024-03-27 16:16                       ` comex
2024-03-27 18:50                       ` Kent Overstreet
2024-03-27 18:50                         ` Kent Overstreet
2024-03-27 19:07                         ` Linus Torvalds
2024-03-27 19:07                           ` Linus Torvalds
2024-03-27 19:41                           ` Kent Overstreet
2024-03-27 19:41                             ` Kent Overstreet
2024-03-27 20:45                             ` Linus Torvalds
2024-03-27 20:45                               ` Linus Torvalds
2024-03-27 21:41                               ` Kent Overstreet
2024-03-27 21:41                                 ` Kent Overstreet
2024-03-27 22:57                                 ` Linus Torvalds
2024-03-27 22:57                                   ` Linus Torvalds
2024-03-27 23:35                                   ` Kent Overstreet
2024-03-27 23:35                                     ` Kent Overstreet
2024-03-27 21:21                             ` Boqun Feng
2024-03-27 21:21                               ` Boqun Feng
2024-03-27 21:49                               ` Kent Overstreet
2024-03-27 21:49                                 ` Kent Overstreet
2024-03-27 22:26                                 ` Boqun Feng
2024-03-27 22:26                                   ` Boqun Feng
2024-03-27 21:56                               ` comex
2024-03-27 21:56                                 ` comex
2024-03-27 22:02                                 ` comex
2024-03-27 22:02                                   ` comex
2024-04-05 17:13                           ` Philipp Stanner
2024-04-05 17:13                             ` Philipp Stanner
2024-04-08 16:02             ` Matthew Wilcox
2024-04-08 16:02               ` Matthew Wilcox
2024-04-08 16:55               ` Paul E. McKenney
2024-04-08 16:55                 ` Paul E. McKenney
2024-04-08 17:03                 ` Matthew Wilcox
2024-04-08 17:03                   ` Matthew Wilcox
2024-04-08 18:47                   ` Paul E. McKenney
2024-04-08 18:47                     ` Paul E. McKenney
2024-04-09  0:58                   ` Kent Overstreet
2024-04-09  0:58                     ` Kent Overstreet
2024-04-09  4:47                     ` Paul E. McKenney
2024-04-09  4:47                       ` Paul E. McKenney
2024-04-08 17:01               ` Linus Torvalds
2024-04-08 17:01                 ` Linus Torvalds
2024-04-08 18:14                 ` Al Viro
2024-04-08 18:14                   ` Al Viro
2024-04-08 20:05                   ` Linus Torvalds
2024-04-08 20:05                     ` Linus Torvalds
2024-03-23 21:40     ` comex
2024-03-23 21:40       ` comex
2024-03-24 15:22       ` Alan Stern
2024-03-24 15:22         ` Alan Stern
2024-03-24 17:37         ` comex
2024-03-24 17:37           ` comex
2024-03-23  0:15   ` Boqun Feng
2024-03-23  0:15     ` Boqun Feng
2024-03-23  0:49     ` Boqun Feng
2024-03-23  0:49       ` Boqun Feng
2024-03-23  1:42       ` Kent Overstreet
2024-03-23  1:42         ` Kent Overstreet
2024-03-23 14:29     ` Andrew Lunn
2024-03-23 14:29       ` Andrew Lunn
2024-03-23 14:41       ` Boqun Feng
2024-03-23 14:41         ` Boqun Feng
2024-03-23 14:55         ` Boqun Feng
2024-03-23 14:55           ` Boqun Feng
2024-03-25 10:44 ` Mark Rutland
2024-03-25 10:44   ` Mark Rutland
2024-03-25 20:59   ` Boqun Feng
2024-03-25 20:59     ` Boqun Feng
2024-04-09 10:50     ` Peter Zijlstra
2024-04-09 10:50       ` Peter Zijlstra
2024-04-16 18:12       ` Boqun Feng
2024-04-16 18:12         ` Boqun Feng

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.