Skip to content

Memory model and atomic API in Rust

Boqun Feng linux-arch linux-arm-kernel linux-fsdevel lkml llvm rust-for-linux 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest3 patches 72 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/3rust: Introduce atomic modulerust/kernel/sync.rs rust/kernel/sync/atomic.rs rust/kernel/sync/atomic/arch.rs +1 more+95 -08 replies
2/3rust: atomic: Add ARM64 fetch_add_relaxed()rust/kernel/sync/atomic/arch.rs rust/kernel/sync/atomic/arch/arm64.rs+32 -00 replies
3/3rust: atomic: Add fetch_sub_release()rust/kernel/sync/atomic.rs rust/kernel/sync/atomic/arch/arm64.rs rust/kernel/sync/atomic/arch/x86.rs+48 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [WIP 0/3] Memory model and atomic API in Rust

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
Signature · 1 line
2.44.0