LKMM generic atomics in Rust
Subsystems (5)
v4 cover LKMM generic atomics in Rust
Hi,
v4 for LKMM atomics in Rust, you can find the previous versions:
v3: https://lore.kernel.org/rust-for-linux/[email protected]/
v2: https://lore.kernel.org/rust-for-linux/[email protected]/
v1: https://lore.kernel.org/rust-for-linux/[email protected]/
wip: https://lore.kernel.org/rust-for-linux/[email protected]/
The reason of providing our own LKMM atomics is because memory model wise Rust native memory model is not guaranteed to work with LKMM and having only one memory model throughout the kernel is always better for reasoning.
I haven't gotten any review from last version but I got a few feedbacks during Rust-for-Linux weekly meeting. I trimmed two more patches to make the current series easier to review, the current version includes:
* Generic atomic support of i32, i64, u32, u64, isize and usize on:
* load() and store()
* xchg() and cmpxchg()
* add() and fetch_add()
* Atomic pointer support on:
* load() and store()
* xchg() and cmpxchg()
* Barrier and ordering support.
Any missing functionality can of course be added in a later patch. There are some use cases based on these API can be found at:
git://git.kernel.org/pub/scm/linux/kernel/git/boqun/linux.git rust-atomic-dev
for example, RCU protected pointers and AtomicFlag.
I think the current version is ready to merge (modulo some documentation improvement), and I would like to postpone small implementation improvement because we are seeing growing usages of atomics in Rust side. It's better to merge the API first so that we can clean up and help new users.
But I do have one question about how to route the patch, basically I have three options:
* via tip, I can send a pull request to Ingo at -rc4 or -rc5.
* via rust-next, I can send a pull request to Miguel at -rc4 or -rc5.
* via my own tree or atomic (Peter if you remember, we do have an atomic group in kernel.org and I can create a shared tree under that group), I can send a pull request to Linus for 6.17 merge window.
Please advise.
Regards,
Boqun
Boqun Feng (10):
rust: Introduce atomic API helpers
rust: sync: Add basic atomic operation mapping framework
rust: sync: atomic: Add ordering annotation types
rust: sync: atomic: Add generic atomics
rust: sync: atomic: Add atomic {cmp,}xchg operations
rust: sync: atomic: Add the framework of arithmetic operations
rust: sync: atomic: Add Atomic<u{32,64}>
rust: sync: atomic: Add Atomic<{usize,isize}>
rust: sync: atomic: Add Atomic<*mut T>
rust: sync: Add memory barriers
MAINTAINERS | 4 +- rust/helpers/atomic.c | 1038 +++++++++++++++++++++ rust/helpers/barrier.c | 18 + rust/helpers/helpers.c | 2 + rust/kernel/sync.rs | 2 + rust/kernel/sync/atomic.rs | 176 ++++ rust/kernel/sync/atomic/generic.rs | 524 +++++++++++ rust/kernel/sync/atomic/ops.rs | 199 ++++ rust/kernel/sync/atomic/ordering.rs | 94 ++ rust/kernel/sync/barrier.rs | 67 ++ scripts/atomic/gen-atomics.sh | 1 + scripts/atomic/gen-rust-atomic-helpers.sh | 65 ++ 12 files changed, 2189 insertions(+), 1 deletion(-) create mode 100644 rust/helpers/atomic.c create mode 100644 rust/helpers/barrier.c create mode 100644 rust/kernel/sync/atomic.rs create mode 100644 rust/kernel/sync/atomic/generic.rs create mode 100644 rust/kernel/sync/atomic/ops.rs create mode 100644 rust/kernel/sync/atomic/ordering.rs create mode 100644 rust/kernel/sync/barrier.rs create mode 100755 scripts/atomic/gen-rust-atomic-helpers.sh
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH v4 1/10 rust: Introduce atomic API helpers
In order to support LKMM atomics in Rust, add rust_helper_* for atomic APIs. These helpers ensure the implementation of LKMM atomics in Rust is the same as in C. This could save the maintenance burden of having two similar atomic implementations in asm.
--- rust/helpers/atomic.c | 1038 +++++++++++++++++++++ rust/helpers/helpers.c | 1 + scripts/atomic/gen-atomics.sh | 1 + scripts/atomic/gen-rust-atomic-helpers.sh | 65 ++ 4 files changed, 1105 insertions(+) create mode 100644 rust/helpers/atomic.c create mode 100755 scripts/atomic/gen-rust-atomic-helpers.sh
rust/helpers/atomic.c new file +1038 -0
@@ -0,0 +1,1038 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Generated by scripts/atomic/gen-rust-atomic-helpers.sh +// DO NOT MODIFY THIS FILE DIRECTLY + +/* +* This file provides helpers for the various atomic functions for Rust. +*/ +#ifndef _RUST_ATOMIC_API_H +#define _RUST_ATOMIC_API_H + +#include <linux/atomic.h> + +// TODO: Remove this after LTO helper support is added. +#define __rust_helper + +__rust_helper int +rust_helper_atomic_read (const atomic_t *v) +{ +return atomic_read (v); +} + +__rust_helper int +rust_helper_atomic_read_acquire (const atomic_t *v) +{ +return atomic_read_acquire (v); +} + +__rust_helper void +rust_helper_atomic_set (atomic_t *v,int i) +{ +atomic_set (v, i); +} + +__rust_helper void +rust_helper_atomic_set_release (atomic_t *v,int i) +{ +atomic_set_release (v, i); +} + +__rust_helper void +rust_helper_atomic_add (int i,atomic_t *v) +{ +atomic_add (i, v); +} + +__rust_helper int +rust_helper_atomic_add_return (int i,atomic_t *v) +{ +return atomic_add_return (i, v); +} + +__rust_helper int +rust_helper_atomic_add_return_acquire (int i,atomic_t *v) +{ +return atomic_add_return_acquire (i, v); +} + +__rust_helper int +rust_helper_atomic_add_return_release (int i,atomic_t *v) +{ +return atomic_add_return_release (i, v); +} + +__rust_helper int +rust_helper_atomic_add_return_relaxed (int i,atomic_t *v) +{ +return atomic_add_return_relaxed (i, v); +} + +__rust_helper int +rust_helper_atomic_fetch_add (int i,atomic_t *v) +{ +return atomic_fetch_add (i, v); +} + +__rust_helper int +rust_helper_atomic_fetch_add_acquire (int i,atomic_t *v) +{ +return atomic_fetch_add_acquire (i, v); +} + +__rust_helper int +rust_helper_atomic_fetch_add_release (int i,atomic_t *v) +{ +return atomic_fetch_add_release (i, v); +} + +__rust_helper int +rust_helper_atomic_fetch_add_relaxed (int i,atomic_t *v) +{ +return atomic_fetch_add_relaxed (i, v); +} + +__rust_helper void +rust_helper_atomic_sub (int i,atomic_t *v) +{ +atomic_sub (i, v); +} + +__rust_helper int +rust_helper_atomic_sub_return (int i,atomic_t *v) +{ +return atomic_sub_return (i, v); +} + +__rust_helper int +rust_helper_atomic_sub_return_acquire (int i,atomic_t *v) +{ +return atomic_sub_return_acquire (i, v); +} + +__rust_helper int +rust_helper_atomic_sub_return_release (int i,atomic_t *v) +{ +return atomic_sub_return_release (i, v); +} + +__rust_helper int +rust_helper_atomic_sub_return_relaxed (int i,atomic_t *v) +{ +return atomic_sub_return_relaxed (i, v); +} + +__rust_helper int +rust_helper_atomic_fetch_sub (int i,atomic_t *v) +{ +return atomic_fetch_sub (i, v); +} + +__rust_helper int +rust_helper_atomic_fetch_sub_acquire (int i,atomic_t *v) +{ +return atomic_fetch_sub_acquire (i, v); +} + +__rust_helper int +rust_helper_atomic_fetch_sub_release (int i,atomic_t *v) +{ +return atomic_fetch_sub_release (i, v); +} + +__rust_helper int +rust_helper_atomic_fetch_sub_relaxed (int i,atomic_t *v) +{ +return atomic_fetch_sub_relaxed (i, v); +} + +__rust_helper void +rust_helper_atomic_inc (atomic_t *v) +{ +atomic_inc (v); +} + +__rust_helper int +rust_helper_atomic_inc_return (atomic_t *v) +{ +return atomic_inc_return (v); +} + +__rust_helper int +rust_helper_atomic_inc_return_acquire (atomic_t *v) +{ +return atomic_inc_return_acquire (v); +} + +__rust_helper int +rust_helper_atomic_inc_return_release (atomic_t *v) +{ +return atomic_inc_return_release (v); +} + +__rust_helper int +rust_helper_atomic_inc_return_relaxed (atomic_t *v) +{ +return atomic_inc_return_relaxed (v); +} + +__rust_helper int +rust_helper_atomic_fetch_inc (atomic_t *v) +{ +return atomic_fetch_inc (v); +} + +__rust_helper int +rust_helper_atomic_fetch_inc_acquire (atomic_t *v) +{ +return atomic_fetch_inc_acquire (v); +} + +__rust_helper int +rust_helper_atomic_fetch_inc_release (atomic_t *v) +{ +return atomic_fetch_inc_release (v); +} + +__rust_helper int +rust_helper_atomic_fetch_inc_relaxed (atomic_t *v) +{ +return atomic_fetch_inc_relaxed (v); +} + +__rust_helper void +rust_helper_atomic_dec (atomic_t *v) +{ +atomic_dec (v); +} + +__rust_helper int +rust_helper_atomic_dec_return (atomic_t *v) +{ +return atomic_dec_return (v); +} + +__rust_helper int +rust_helper_atomic_dec_return_acquire (atomic_t *v) +{ +return atomic_dec_return_acquire (v); +} + +__rust_helper int +rust_helper_atomic_dec_return_release (atomic_t *v) +{ +return atomic_dec_return_release (v); +} + +__rust_helper int +rust_helper_atomic_dec_return_relaxed (atomic_t *v) +{ +return atomic_dec_return_relaxed (v); +} + +__rust_helper int +rust_helper_atomic_fetch_dec (atomic_t *v) +{ +return atomic_fetch_dec (v); +} + +__rust_helper int +rust_helper_atomic_fetch_dec_acquire (atomic_t *v) +{ +return atomic_fetch_dec_acquire (v); +} + +__rust_helper int +rust_helper_atomic_fetch_dec_release (atomic_t *v) +{ +return atomic_fetch_dec_release (v); +} + +__rust_helper int +rust_helper_atomic_fetch_dec_relaxed (atomic_t *v) +{ +return atomic_fetch_dec_relaxed (v); +} + +__rust_helper void +rust_helper_atomic_and (int i,atomic_t *v) +{ +atomic_and (i, v); +} + +__rust_helper int +rust_helper_atomic_fetch_and (int i,atomic_t *v) +{ +return atomic_fetch_and (i, v); +} + +__rust_helper int +rust_helper_atomic_fetch_and_acquire (int i,atomic_t *v) +{ +return atomic_fetch_and_acquire (i, v); +} + +__rust_helper int +rust_helper_atomic_fetch_and_release (int i,atomic_t *v) +{ +return atomic_fetch_and_release (i, v); +} + +__rust_helper int +rust_helper_atomic_fetch_and_relaxed (int i,atomic_t *v) +{ +return atomic_fetch_and_relaxed (i, v); +} + +__rust_helper void +rust_helper_atomic_andnot (int i,atomic_t *v) +{ +atomic_andnot (i, v); +} + +__rust_helper int +rust_helper_atomic_fetch_andnot (int i,atomic_t *v) +{ +return atomic_fetch_andnot (i, v); +} + +__rust_helper int +rust_helper_atomic_fetch_andnot_acquire (int i,atomic_t *v) +{ +return atomic_fetch_andnot_acquire (i, v); +} + +__rust_helper int +rust_helper_atomic_fetch_andnot_release (int i,atomic_t *v) +{ +return atomic_fetch_andnot_release (i, v); +} + +__rust_helper int +rust_helper_atomic_fetch_andnot_relaxed (int i,atomic_t *v) +{ +return atomic_fetch_andnot_relaxed (i, v); +} + +__rust_helper void +rust_helper_atomic_or (int i,atomic_t *v) +{ +atomic_or (i, v); +} + +__rust_helper int +rust_helper_atomic_fetch_or (int i,atomic_t *v) +{ +return atomic_fetch_or (i, v); +} + +__rust_helper int +rust_helper_atomic_fetch_or_acquire (int i,atomic_t *v) +{ +return atomic_fetch_or_acquire (i, v); +} + +__rust_helper int +rust_helper_atomic_fetch_or_release (int i,atomic_t *v) +{ +return atomic_fetch_or_release (i, v); +} + +__rust_helper int +rust_helper_atomic_fetch_or_relaxed (int i,atomic_t *v) +{ +return atomic_fetch_or_relaxed (i, v); +} + +__rust_helper void +rust_helper_atomic_xor (int i,atomic_t *v) +{ +atomic_xor (i, v); +} + +__rust_helper int +rust_helper_atomic_fetch_xor (int i,atomic_t *v) +{ +return atomic_fetch_xor (i, v); +} + +__rust_helper int +rust_helper_atomic_fetch_xor_acquire (int i,atomic_t *v) +{ +return atomic_fetch_xor_acquire (i, v); +} + +__rust_helper int +rust_helper_atomic_fetch_xor_release (int i,atomic_t *v) +{ +return atomic_fetch_xor_release (i, v); +} + +__rust_helper int +rust_helper_atomic_fetch_xor_relaxed (int i,atomic_t *v) +{ +return atomic_fetch_xor_relaxed (i, v); +} + +__rust_helper int +rust_helper_atomic_xchg (atomic_t *v,int new) +{ +return atomic_xchg (v, new); +} + +__rust_helper int +rust_helper_atomic_xchg_acquire (atomic_t *v,int new) +{ +return atomic_xchg_acquire (v, new); +} + +__rust_helper int +rust_helper_atomic_xchg_release (atomic_t *v,int new) +{ +return atomic_xchg_release (v, new); +} + +__rust_helper int +rust_helper_atomic_xchg_relaxed (atomic_t *v,int new) +{ +return atomic_xchg_relaxed (v, new); +} + +__rust_helper int +rust_helper_atomic_cmpxchg (atomic_t *v,int old,int new) +{ +return atomic_cmpxchg (v, old, new); +} + +__rust_helper int +rust_helper_atomic_cmpxchg_acquire (atomic_t *v,int old,int new) +{ +return atomic_cmpxchg_acquire (v, old, new); +} + +__rust_helper int +rust_helper_atomic_cmpxchg_release (atomic_t *v,int old,int new) +{ +return atomic_cmpxchg_release (v, old, new); +} + +__rust_helper int +rust_helper_atomic_cmpxchg_relaxed (atomic_t *v,int old,int new) +{ +return atomic_cmpxchg_relaxed (v, old, new); +} + +__rust_helper bool +rust_helper_atomic_try_cmpxchg (atomic_t *v,int *old,int new) +{ +return atomic_try_cmpxchg (v, old, new); +} + +__rust_helper bool +rust_helper_atomic_try_cmpxchg_acquire (atomic_t *v,int *old,int new) +{ +return atomic_try_cmpxchg_acquire (v, old, new); +} + +__rust_helper bool +rust_helper_atomic_try_cmpxchg_release (atomic_t *v,int *old,int new) +{ +return atomic_try_cmpxchg_release (v, old, new); +} + +__rust_helper bool +rust_helper_atomic_try_cmpxchg_relaxed (atomic_t *v,int *old,int new) +{ +return atomic_try_cmpxchg_relaxed (v, old, new); +} + +__rust_helper bool +rust_helper_atomic_sub_and_test (int i,atomic_t *v) +{ +return atomic_sub_and_test (i, v); +} + +__rust_helper bool +rust_helper_atomic_dec_and_test (atomic_t *v) +{ +return atomic_dec_and_test (v); +} + +__rust_helper bool +rust_helper_atomic_inc_and_test (atomic_t *v) +{ +return atomic_inc_and_test (v); +} + +__rust_helper bool +rust_helper_atomic_add_negative (int i,atomic_t *v) +{ +return atomic_add_negative (i, v); +} + +__rust_helper bool +rust_helper_atomic_add_negative_acquire (int i,atomic_t *v) +{ +return atomic_add_negative_acquire (i, v); +} + +__rust_helper bool +rust_helper_atomic_add_negative_release (int i,atomic_t *v) +{ +return atomic_add_negative_release (i, v); +} + +__rust_helper bool +rust_helper_atomic_add_negative_relaxed (int i,atomic_t *v) +{ +return atomic_add_negative_relaxed (i, v); +} + +__rust_helper int +rust_helper_atomic_fetch_add_unless (atomic_t *v,int a,int u) +{ +return atomic_fetch_add_unless (v, a, u); +} + +__rust_helper bool +rust_helper_atomic_add_unless (atomic_t *v,int a,int u) +{ +return atomic_add_unless (v, a, u); +} + +__rust_helper bool +rust_helper_atomic_inc_not_zero (atomic_t *v) +{ +return atomic_inc_not_zero (v); +} + +__rust_helper bool +rust_helper_atomic_inc_unless_negative (atomic_t *v) +{ +return atomic_inc_unless_negative (v); +} + +__rust_helper bool +rust_helper_atomic_dec_unless_positive (atomic_t *v) +{ +return atomic_dec_unless_positive (v); +} + +__rust_helper int +rust_helper_atomic_dec_if_positive (atomic_t *v) +{ +return atomic_dec_if_positive (v); +} + +__rust_helper s64 +rust_helper_atomic64_read (const atomic64_t *v) +{ +return atomic64_read (v); +} + +__rust_helper s64 +rust_helper_atomic64_read_acquire (const atomic64_t *v) +{ +return atomic64_read_acquire (v); +} + +__rust_helper void +rust_helper_atomic64_set (atomic64_t *v,s64 i) +{ +atomic64_set (v, i); +} + +__rust_helper void +rust_helper_atomic64_set_release (atomic64_t *v,s64 i) +{ +atomic64_set_release (v, i); +} + +__rust_helper void +rust_helper_atomic64_add (s64 i,atomic64_t *v) +{ +atomic64_add (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_add_return (s64 i,atomic64_t *v) +{ +return atomic64_add_return (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_add_return_acquire (s64 i,atomic64_t *v) +{ +return atomic64_add_return_acquire (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_add_return_release (s64 i,atomic64_t *v) +{ +return atomic64_add_return_release (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_add_return_relaxed (s64 i,atomic64_t *v) +{ +return atomic64_add_return_relaxed (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_add (s64 i,atomic64_t *v) +{ +return atomic64_fetch_add (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_add_acquire (s64 i,atomic64_t *v) +{ +return atomic64_fetch_add_acquire (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_add_release (s64 i,atomic64_t *v) +{ +return atomic64_fetch_add_release (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_add_relaxed (s64 i,atomic64_t *v) +{ +return atomic64_fetch_add_relaxed (i, v); +} + +__rust_helper void +rust_helper_atomic64_sub (s64 i,atomic64_t *v) +{ +atomic64_sub (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_sub_return (s64 i,atomic64_t *v) +{ +return atomic64_sub_return (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_sub_return_acquire (s64 i,atomic64_t *v) +{ +return atomic64_sub_return_acquire (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_sub_return_release (s64 i,atomic64_t *v) +{ +return atomic64_sub_return_release (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_sub_return_relaxed (s64 i,atomic64_t *v) +{ +return atomic64_sub_return_relaxed (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_sub (s64 i,atomic64_t *v) +{ +return atomic64_fetch_sub (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_sub_acquire (s64 i,atomic64_t *v) +{ +return atomic64_fetch_sub_acquire (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_sub_release (s64 i,atomic64_t *v) +{ +return atomic64_fetch_sub_release (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_sub_relaxed (s64 i,atomic64_t *v) +{ +return atomic64_fetch_sub_relaxed (i, v); +} + +__rust_helper void +rust_helper_atomic64_inc (atomic64_t *v) +{ +atomic64_inc (v); +} + +__rust_helper s64 +rust_helper_atomic64_inc_return (atomic64_t *v) +{ +return atomic64_inc_return (v); +} + +__rust_helper s64 +rust_helper_atomic64_inc_return_acquire (atomic64_t *v) +{ +return atomic64_inc_return_acquire (v); +} + +__rust_helper s64 +rust_helper_atomic64_inc_return_release (atomic64_t *v) +{ +return atomic64_inc_return_release (v); +} + +__rust_helper s64 +rust_helper_atomic64_inc_return_relaxed (atomic64_t *v) +{ +return atomic64_inc_return_relaxed (v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_inc (atomic64_t *v) +{ +return atomic64_fetch_inc (v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_inc_acquire (atomic64_t *v) +{ +return atomic64_fetch_inc_acquire (v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_inc_release (atomic64_t *v) +{ +return atomic64_fetch_inc_release (v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_inc_relaxed (atomic64_t *v) +{ +return atomic64_fetch_inc_relaxed (v); +} + +__rust_helper void +rust_helper_atomic64_dec (atomic64_t *v) +{ +atomic64_dec (v); +} + +__rust_helper s64 +rust_helper_atomic64_dec_return (atomic64_t *v) +{ +return atomic64_dec_return (v); +} + +__rust_helper s64 +rust_helper_atomic64_dec_return_acquire (atomic64_t *v) +{ +return atomic64_dec_return_acquire (v); +} + +__rust_helper s64 +rust_helper_atomic64_dec_return_release (atomic64_t *v) +{ +return atomic64_dec_return_release (v); +} + +__rust_helper s64 +rust_helper_atomic64_dec_return_relaxed (atomic64_t *v) +{ +return atomic64_dec_return_relaxed (v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_dec (atomic64_t *v) +{ +return atomic64_fetch_dec (v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_dec_acquire (atomic64_t *v) +{ +return atomic64_fetch_dec_acquire (v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_dec_release (atomic64_t *v) +{ +return atomic64_fetch_dec_release (v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_dec_relaxed (atomic64_t *v) +{ +return atomic64_fetch_dec_relaxed (v); +} + +__rust_helper void +rust_helper_atomic64_and (s64 i,atomic64_t *v) +{ +atomic64_and (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_and (s64 i,atomic64_t *v) +{ +return atomic64_fetch_and (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_and_acquire (s64 i,atomic64_t *v) +{ +return atomic64_fetch_and_acquire (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_and_release (s64 i,atomic64_t *v) +{ +return atomic64_fetch_and_release (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_and_relaxed (s64 i,atomic64_t *v) +{ +return atomic64_fetch_and_relaxed (i, v); +} + +__rust_helper void +rust_helper_atomic64_andnot (s64 i,atomic64_t *v) +{ +atomic64_andnot (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_andnot (s64 i,atomic64_t *v) +{ +return atomic64_fetch_andnot (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_andnot_acquire (s64 i,atomic64_t *v) +{ +return atomic64_fetch_andnot_acquire (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_andnot_release (s64 i,atomic64_t *v) +{ +return atomic64_fetch_andnot_release (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_andnot_relaxed (s64 i,atomic64_t *v) +{ +return atomic64_fetch_andnot_relaxed (i, v); +} + +__rust_helper void +rust_helper_atomic64_or (s64 i,atomic64_t *v) +{ +atomic64_or (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_or (s64 i,atomic64_t *v) +{ +return atomic64_fetch_or (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_or_acquire (s64 i,atomic64_t *v) +{ +return atomic64_fetch_or_acquire (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_or_release (s64 i,atomic64_t *v) +{ +return atomic64_fetch_or_release (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_or_relaxed (s64 i,atomic64_t *v) +{ +return atomic64_fetch_or_relaxed (i, v); +} + +__rust_helper void +rust_helper_atomic64_xor (s64 i,atomic64_t *v) +{ +atomic64_xor (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_xor (s64 i,atomic64_t *v) +{ +return atomic64_fetch_xor (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_xor_acquire (s64 i,atomic64_t *v) +{ +return atomic64_fetch_xor_acquire (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_xor_release (s64 i,atomic64_t *v) +{ +return atomic64_fetch_xor_release (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_xor_relaxed (s64 i,atomic64_t *v) +{ +return atomic64_fetch_xor_relaxed (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_xchg (atomic64_t *v,s64 new) +{ +return atomic64_xchg (v, new); +} + +__rust_helper s64 +rust_helper_atomic64_xchg_acquire (atomic64_t *v,s64 new) +{ +return atomic64_xchg_acquire (v, new); +} + +__rust_helper s64 +rust_helper_atomic64_xchg_release (atomic64_t *v,s64 new) +{ +return atomic64_xchg_release (v, new); +} + +__rust_helper s64 +rust_helper_atomic64_xchg_relaxed (atomic64_t *v,s64 new) +{ +return atomic64_xchg_relaxed (v, new); +} + +__rust_helper s64 +rust_helper_atomic64_cmpxchg (atomic64_t *v,s64 old,s64 new) +{ +return atomic64_cmpxchg (v, old, new); +} + +__rust_helper s64 +rust_helper_atomic64_cmpxchg_acquire (atomic64_t *v,s64 old,s64 new) +{ +return atomic64_cmpxchg_acquire (v, old, new); +} + +__rust_helper s64 +rust_helper_atomic64_cmpxchg_release (atomic64_t *v,s64 old,s64 new) +{ +return atomic64_cmpxchg_release (v, old, new); +} + +__rust_helper s64 +rust_helper_atomic64_cmpxchg_relaxed (atomic64_t *v,s64 old,s64 new) +{ +return atomic64_cmpxchg_relaxed (v, old, new); +} + +__rust_helper bool +rust_helper_atomic64_try_cmpxchg (atomic64_t *v,s64 *old,s64 new) +{ +return atomic64_try_cmpxchg (v, old, new); +} + +__rust_helper bool +rust_helper_atomic64_try_cmpxchg_acquire (atomic64_t *v,s64 *old,s64 new) +{ +return atomic64_try_cmpxchg_acquire (v, old, new); +} + +__rust_helper bool +rust_helper_atomic64_try_cmpxchg_release (atomic64_t *v,s64 *old,s64 new) +{ +return atomic64_try_cmpxchg_release (v, old, new); +} + +__rust_helper bool +rust_helper_atomic64_try_cmpxchg_relaxed (atomic64_t *v,s64 *old,s64 new) +{ +return atomic64_try_cmpxchg_relaxed (v, old, new); +} + +__rust_helper bool +rust_helper_atomic64_sub_and_test (s64 i,atomic64_t *v) +{ +return atomic64_sub_and_test (i, v); +} + +__rust_helper bool +rust_helper_atomic64_dec_and_test (atomic64_t *v) +{ +return atomic64_dec_and_test (v); +} + +__rust_helper bool +rust_helper_atomic64_inc_and_test (atomic64_t *v) +{ +return atomic64_inc_and_test (v); +} + +__rust_helper bool +rust_helper_atomic64_add_negative (s64 i,atomic64_t *v) +{ +return atomic64_add_negative (i, v); +} + +__rust_helper bool +rust_helper_atomic64_add_negative_acquire (s64 i,atomic64_t *v) +{ +return atomic64_add_negative_acquire (i, v); +} + +__rust_helper bool +rust_helper_atomic64_add_negative_release (s64 i,atomic64_t *v) +{ +return atomic64_add_negative_release (i, v); +} + +__rust_helper bool +rust_helper_atomic64_add_negative_relaxed (s64 i,atomic64_t *v) +{ +return atomic64_add_negative_relaxed (i, v); +} + +__rust_helper s64 +rust_helper_atomic64_fetch_add_unless (atomic64_t *v,s64 a,s64 u) +{ +return atomic64_fetch_add_unless (v, a, u); +} + +__rust_helper bool +rust_helper_atomic64_add_unless (atomic64_t *v,s64 a,s64 u) +{ +return atomic64_add_unless (v, a, u); +} + +__rust_helper bool +rust_helper_atomic64_inc_not_zero (atomic64_t *v) +{ +return atomic64_inc_not_zero (v); +} + +__rust_helper bool +rust_helper_atomic64_inc_unless_negative (atomic64_t *v) +{ +return atomic64_inc_unless_negative (v); +} + +__rust_helper bool +rust_helper_atomic64_dec_unless_positive (atomic64_t *v) +{ +return atomic64_dec_unless_positive (v); +} + +__rust_helper s64 +rust_helper_atomic64_dec_if_positive (atomic64_t *v) +{ +return atomic64_dec_if_positive (v); +} + +#endif /* _RUST_ATOMIC_API_H */ +// b032d261814b3e119b72dbf7d21447f6731325ee
rust/helpers/helpers.c +1 -0
@@ -7,6 +7,7 @@* Sorted alphabetically. */ +#include "atomic.c" #include "auxiliary.c" #include "blk.c" #include "bug.c"
scripts/atomic/gen-atomics.sh +1 -0
@@ -11,6 +11,7 @@ cat <<EOF | gen-atomic-instrumented.sh linux/atomic/atomic-instrumented.h gen-atomic-long.sh linux/atomic/atomic-long.h gen-atomic-fallback.sh linux/atomic/atomic-arch-fallback.h +gen-rust-atomic-helpers.sh ../rust/helpers/atomic.c EOF while read script header args; do /bin/sh ${ATOMICDIR}/${script} ${ATOMICTBL} ${args} > ${LINUXDIR}/include/${header}
scripts/atomic/gen-rust-atomic-helpers.sh new file +65 -0
65 lines not on this page · raw message
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH v4 2/10 rust: sync: Add basic atomic operation mapping framework
In reply to Boqun FengPreparation for generic atomic implementation. To unify the implementation of a generic method over i32 and i64, the C side atomic methods need to be grouped so that in a generic method, they can be referred as <type>::<method>, otherwise their parameters and return value are different between i32 and i64, which would require using transmute() to unify the type into a T.
Introduce AtomicImpl to represent a basic type in Rust that has the direct mapping to an atomic implementation from C. This trait is sealed, and currently only i32 and i64 impl this.
Further, different methods are put into different *Ops trait groups, and this is for the future when smaller types like i8/i16 are supported but only with a limited set of API (e.g. only set(), load(), xchg() and cmpxchg(), no add() or sub() etc).
While the atomic mod is introduced, documentation is also added for memory models and data races.
Also bump my role to the maintainer of ATOMIC INFRASTRUCTURE to reflect my responsiblity on the Rust atomic mod.
--- MAINTAINERS | 4 +- rust/kernel/sync.rs | 1 + rust/kernel/sync/atomic.rs | 19 ++++ rust/kernel/sync/atomic/ops.rs | 199 +++++++++++++++++++++++++++++++++ 4 files changed, 222 insertions(+), 1 deletion(-) create mode 100644 rust/kernel/sync/atomic.rs create mode 100644 rust/kernel/sync/atomic/ops.rs
MAINTAINERS +3 -1
@@ -3894,7 +3894,7 @@ F: drivers/input/touchscreen/atmel_mxt_ts.c ATOMIC INFRASTRUCTURE M: Will Deacon <[email protected]> M: Peter Zijlstra <[email protected]> -R: Boqun Feng <[email protected]> +M: Boqun Feng <[email protected]> R: Mark Rutland <[email protected]> L: [email protected] S: Maintained
@@ -3903,6 +3903,8 @@ F: arch/*/include/asm/atomic*.h F: include/*/atomic*.h F: include/linux/refcount.h F: scripts/atomic/ +F: rust/kernel/sync/atomic.rs +F: rust/kernel/sync/atomic/ ATTO EXPRESSSAS SAS/SATA RAID SCSI DRIVER M: Bradley Grove <[email protected]>
rust/kernel/sync.rs +1 -0
@@ -10,6 +10,7 @@use pin_init;mod arc; +pub mod atomic;mod condvar;pub mod lock;mod locked_by;
rust/kernel/sync/atomic.rs new file +19 -0
@@ -0,0 +1,19 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Atomic primitives. +//! +//! These primitives have the same semantics as their C counterparts: and the precise definitions of +//! semantics can be found at [`LKMM`]. Note that Linux Kernel Memory (Consistency) Model is the +//! only model for Rust code in kernel, and Rust's own atomics should be avoided. +//! +//! # Data races +//! +//! [`LKMM`] atomics have different rules regarding data races: +//! +//! - A normal write from C side is treated as an atomic write if +//! CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC=y. +//! - Mixed-size atomic accesses don't cause data races. +//! +//! [`LKMM`]: srctree/tools/memory-mode/ + +pub mod ops;
rust/kernel/sync/atomic/ops.rs new file +199 -0
199 lines not on this page · raw message
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH v4 3/10 rust: sync: atomic: Add ordering annotation types
In reply to Boqun FengPreparation for atomic primitives. Instead of a suffix like _acquire, a method parameter along with the corresponding generic parameter will be used to specify the ordering of an atomic operations. For example, atomic load() can be defined as:
impl<T: ...> Atomic<T> {
pub fn load<O: AcquireOrRelaxed>(&self, _o: O) -> T { ... }
}and acquire users would do:
let r = x.load(Acquire);
relaxed users:
let r = x.load(Relaxed);
doing the following:
let r = x.load(Release);
will cause a compiler error.
Compared to suffixes, it's easier to tell what ordering variants an operation has, and it also make it easier to unify the implementation of all ordering variants in one method via generic. The IS_RELAXED and ORDER associate consts are for generic function to pick up the particular implementation specified by an ordering annotation.
--- rust/kernel/sync/atomic.rs | 3 + rust/kernel/sync/atomic/ordering.rs | 94 +++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 rust/kernel/sync/atomic/ordering.rs
rust/kernel/sync/atomic.rs +3 -0
@@ -17,3 +17,6 @@//! [`LKMM`]: srctree/tools/memory-mode/ pub mod ops; +pub mod ordering; + +pub use ordering::{Acquire ,Full ,Relaxed ,Release };
rust/kernel/sync/atomic/ordering.rs new file +94 -0
94 lines not on this page · raw message
Signature · 1 line
2.39.5 (Apple Git-154)
Benno Lossin
reviewerRe: [PATCH v4 03/10] rust: sync: atomic: Add ordering annotation types
Quoted 23 lines · Boqun Feng
Preparation for atomic primitives. Instead of a suffix like
_acquire, a method parameter along with the corresponding generic parameter will be used to specify the ordering of an atomic operations. For example, atomicload()can be defined as:impl<T: ...> Atomic<T> { pub fn load<O: AcquireOrRelaxed>(&self, _o: O) -> T { ... } }and acquire users would do:
let r = x.load(Acquire);relaxed users:
let r = x.load(Relaxed);doing the following:
let r = x.load(Release);will cause a compiler error.
Quoted 7 lines
Compared to suffixes, it's easier to tell what ordering variants an operation has, and it also make it easier to unify the implementation of all ordering variants in one method via generic. The
IS_RELAXEDandORDERassociate consts are for generic function to pick up the particular implementation specified by an ordering annotation.Signed-off-by Boqun Feng <[email protected]>
Looks good, I got a few comments on the details below.
Quoted 26 lines
--- rust/kernel/sync/atomic.rs | 3 + rust/kernel/sync/atomic/ordering.rs | 94 +++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 rust/kernel/sync/atomic/ordering.rsrust/kernel/sync/atomic.rs +3
-0@@ -17,3 +17,6 @@//! [`LKMM`]: srctree/tools/memory-mode/ pub mod ops; +pub mod ordering; + +pub use ordering::{Acquire ,Full ,Relaxed ,Release };rust/kernel/sync/atomic/ordering.rs new file +3
-0@@ -0,0 +1,94 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Memory orderings.
rust/kernel/sync/atomic/ordering.rs line 13
Quoted 10 lines
rust/kernel/sync/atomic/ordering.rs new file +10
-0+//! +//! The semantics of these orderings follows the [`LKMM`] definitions and rules. +//! +//! - [`Acquire`] and [`Release`] are similar to their counterpart in Rust memory model. +//! - [`Full`] means "fully-ordered", that is: +//! - It provides ordering between all the preceding memory accesses and the annotated operation. +//! - It provides ordering between the annotated operation and all the following memory accesses. +//! - It provides ordering between all the preceding memory accesses and all the fllowing memory +//! accesses. +//! - All the orderings are the same strong as a full memory barrier (i.e. `smp_mb()`).
s/strong/strength/ ?
Quoted 8 lines
rust/kernel/sync/atomic/ordering.rs new file +8
-0+//! - [`Relaxed`] is similar to the counterpart in Rust memory model, except that dependency +//! orderings are also honored in [`LKMM`]. Dependency orderings are described in "DEPENDENCY +//! RELATIONS" in [`LKMM`]'s [`explanation`]. +//! +//! [`LKMM`]: srctree/tools/memory-model/ +//! [`explanation`]: srctree/tools/memory-model/Documentation/explanation.txt + +/// The annotation type for relaxed memory ordering.
rust/kernel/sync/atomic/ordering.rs line 31
Quoted 10 lines
rust/kernel/sync/atomic/ordering.rs new file +10
-0+pub struct Relaxed ; + +/// The annotation type for acquire memory ordering. +pub struct Acquire ; + +/// The annotation type for release memory ordering. +pub struct Release ; + +/// The annotation type for fully-order memory ordering. +pub struct Full ;
Is this ordering only ever used in combination with itself? (Since you don't have a FullOrAcquire trait)
Quoted 22 lines
rust/kernel/sync/atomic/ordering.rs new file +22
-0+ +/// The trait bound for operations that only support relaxed ordering. +pub trait RelaxedOnly :AcquireOrRelaxed +ReleaseOrRelaxed +All {} + +impl RelaxedOnly for Relaxed {} + +/// The trait bound for operations that only support acquire or relaxed ordering. +pub trait AcquireOrRelaxed :All { +/// Describes whether an ordering is relaxed or not. +const IS_RELAXED :bool =false ; +} + +impl AcquireOrRelaxed for Acquire {} + +impl AcquireOrRelaxed for Relaxed { +const IS_RELAXED :bool =true ; +} + +/// The trait bound for operations that only support release or relaxed ordering. +pub trait ReleaseOrRelaxed :All { +/// Describes whether an ordering is relaxed or not. +const IS_RELAXED :bool =false ;
rust/kernel/sync/atomic/ordering.rs line 63
Quoted 10 lines
rust/kernel/sync/atomic/ordering.rs new file +10
-0+} + +impl ReleaseOrRelaxed for Release {} + +impl ReleaseOrRelaxed for Relaxed { +const IS_RELAXED :bool =true ; +} + +/// Describes the exact memory ordering of an `impl` [`All`]. +pub enum OrderingDesc {
Why not name this Ordering?
Quoted 4 lines
rust/kernel/sync/atomic/ordering.rs new file +4
-0+/// Relaxed ordering. + Relaxed, +/// Acquire ordering. + Acquire,
rust/kernel/sync/atomic/ordering.rs line 77
Quoted 10 lines
rust/kernel/sync/atomic/ordering.rs new file +10
-0+/// Release ordering. + Release, +/// Fully-ordered. + Full, +} + +/// The trait bound for annotating operations that should support all orderings. +pub trait All { +/// Describes the exact memory ordering. +const ORDER :OrderingDesc ;
And then here: ORDERING.
---
Cheers,
Benno
Quoted 17 lines
rust/kernel/sync/atomic/ordering.rs new file +17
-0+} + +impl All for Relaxed { +const ORDER :OrderingDesc =OrderingDesc ::Relaxed ; +} + +impl All for Acquire { +const ORDER :OrderingDesc =OrderingDesc ::Acquire ; +} + +impl All for Release { +const ORDER :OrderingDesc =OrderingDesc ::Release ; +} + +impl All for Full { +const ORDER :OrderingDesc =OrderingDesc ::Full ; +}
Re: [PATCH v4 03/10] rust: sync: atomic: Add ordering annotation types
Quoted 30 lines · Benno Lossin
Quoted 29 lines · Boqun Feng
Preparation for atomic primitives. Instead of a suffix like
_acquire, a method parameter along with the corresponding generic parameter will be used to specify the ordering of an atomic operations. For example, atomicload()can be defined as:impl<T: ...> Atomic<T> { pub fn load<O: AcquireOrRelaxed>(&self, _o: O) -> T { ... } }and acquire users would do:
let r = x.load(Acquire);relaxed users:
let r = x.load(Relaxed);doing the following:
let r = x.load(Release);will cause a compiler error.
Compared to suffixes, it's easier to tell what ordering variants an operation has, and it also make it easier to unify the implementation of all ordering variants in one method via generic. The
IS_RELAXEDandORDERassociate consts are for generic function to pick up the particular implementation specified by an ordering annotation.
Quoted 4 lines
Quoted 1 line
Signed-off-by Boqun Feng <[email protected]>Looks good, I got a few comments on the details below.
Thanks for taking a look!
Quoted 30 lines
Quoted 30 lines
--- rust/kernel/sync/atomic.rs | 3 + rust/kernel/sync/atomic/ordering.rs | 94 +++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 rust/kernel/sync/atomic/ordering.rsrust/kernel/sync/atomic.rs +3
-0@@ -17,3 +17,6 @@//! [`LKMM`]: srctree/tools/memory-mode/ pub mod ops; +pub mod ordering; + +pub use ordering::{Acquire ,Full ,Relaxed ,Release };rust/kernel/sync/atomic/ordering.rs new file +7
-0@@ -0,0 +1,94 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Memory orderings. +//! +//! The semantics of these orderings follows the [`LKMM`] definitions and rules. +//! +//! - [`Acquire`] and [`Release`] are similar to their counterpart in Rust memory model.
Quoted 9 lines
rust/kernel/sync/atomic/ordering.rs line 13
Quoted 6 lines
rust/kernel/sync/atomic/ordering.rs new file +6
-0+//! - [`Full`] means "fully-ordered", that is: +//! - It provides ordering between all the preceding memory accesses and the annotated operation. +//! - It provides ordering between the annotated operation and all the following memory accesses. +//! - It provides ordering between all the preceding memory accesses and all the fllowing memory +//! accesses. +//! - All the orderings are the same strong as a full memory barrier (i.e. `smp_mb()`). s/strong/strength/ ?
Good catch.
Quoted 13 lines
Quoted 13 lines
rust/kernel/sync/atomic/ordering.rs new file +13
-0+//! - [`Relaxed`] is similar to the counterpart in Rust memory model, except that dependency +//! orderings are also honored in [`LKMM`]. Dependency orderings are described in "DEPENDENCY +//! RELATIONS" in [`LKMM`]'s [`explanation`]. +//! +//! [`LKMM`]: srctree/tools/memory-model/ +//! [`explanation`]: srctree/tools/memory-model/Documentation/explanation.txt + +/// The annotation type for relaxed memory ordering. +pub struct Relaxed ; + +/// The annotation type for acquire memory ordering. +pub struct Acquire ; +
Quoted 9 lines
rust/kernel/sync/atomic/ordering.rs line 31
Quoted 5 lines
rust/kernel/sync/atomic/ordering.rs new file +5
-0+/// The annotation type for release memory ordering. +pub struct Release ; + +/// The annotation type for fully-order memory ordering. +pub struct Full ;Is this ordering only ever used in combination with itself? (Since you don't have a
FullOrAcquiretrait)
Yes, Full is an ordering that is stronger than Acquire.
Quoted 26 lines
Quoted 26 lines
rust/kernel/sync/atomic/ordering.rs new file +26
-0+ +/// The trait bound for operations that only support relaxed ordering. +pub trait RelaxedOnly :AcquireOrRelaxed +ReleaseOrRelaxed +All {} + +impl RelaxedOnly for Relaxed {} + +/// The trait bound for operations that only support acquire or relaxed ordering. +pub trait AcquireOrRelaxed :All { +/// Describes whether an ordering is relaxed or not. +const IS_RELAXED :bool =false ; +} + +impl AcquireOrRelaxed for Acquire {} + +impl AcquireOrRelaxed for Relaxed { +const IS_RELAXED :bool =true ; +} + +/// The trait bound for operations that only support release or relaxed ordering. +pub trait ReleaseOrRelaxed :All { +/// Describes whether an ordering is relaxed or not. +const IS_RELAXED :bool =false ; +} + +impl ReleaseOrRelaxed for Release {} +
Quoted 9 lines
rust/kernel/sync/atomic/ordering.rs line 63
Quoted 6 lines
rust/kernel/sync/atomic/ordering.rs new file +6
-0+impl ReleaseOrRelaxed for Relaxed { +const IS_RELAXED :bool =true ; +} + +/// Describes the exact memory ordering of an `impl` [`All`]. +pub enum OrderingDesc {Why not name this
Ordering?
I was trying to avoid having an Ordering enum in a ordering mod. Also I want to save the name "Ordering" for the generic type parameter of an atomic operation, e.g.
pub fn xchg<Ordering: ALL>(..)
this enum is more of an internal implementation detail, and users should not use this enum directly, so I would like to avoid potential confusion.
I have played a few sealed trait tricks on my end, but seems I cannot achieve:
1) OrderingDesc is only accessible in the atomic mod.
2) All is only impl-able in the atomic mod, while it can be used as a trait bound outside kernel crate.
Maybe there is a trick I'm missing?
Quoted 8 lines
Quoted 8 lines
rust/kernel/sync/atomic/ordering.rs new file +8
-0+/// Relaxed ordering. + Relaxed, +/// Acquire ordering. + Acquire, +/// Release ordering. + Release, +/// Fully-ordered. + Full,
Quoted 8 lines
rust/kernel/sync/atomic/ordering.rs line 77
Quoted 6 lines
rust/kernel/sync/atomic/ordering.rs new file +6
-0+} + +/// The trait bound for annotating operations that should support all orderings. +pub trait All { +/// Describes the exact memory ordering. +const ORDER :OrderingDesc ;And then here:
ORDERING.
Make sense, thanks!
Regards,
Boqun
Quoted 23 lines
---
Cheers,
BennoQuoted 17 lines
rust/kernel/sync/atomic/ordering.rs new file +17
-0+} + +impl All for Relaxed { +const ORDER :OrderingDesc =OrderingDesc ::Relaxed ; +} + +impl All for Acquire { +const ORDER :OrderingDesc =OrderingDesc ::Acquire ; +} + +impl All for Release { +const ORDER :OrderingDesc =OrderingDesc ::Release ; +} + +impl All for Full { +const ORDER :OrderingDesc =OrderingDesc ::Full ; +}
Re: [PATCH v4 03/10] rust: sync: atomic: Add ordering annotation types
On Tue, Jun 10, 2025 at 10:30:55AM -0700, Boqun Feng wrote:
[...]
Quoted 16 lines
Quoted 5 lines
rust/kernel/sync/atomic/ordering.rs line 63
Quoted 2 lines
rust/kernel/sync/atomic/ordering.rs new file +2
-0+/// Describes the exact memory ordering of an `impl` [`All`]. +pub enum OrderingDesc {Why not name this
Ordering?I was trying to avoid having an
Orderingenum in aorderingmod. Also I want to save the name "Ordering" for the generic type parameter of an atomic operation, e.g.pub fn xchg<Ordering: ALL>(..)
this enum is more of an internal implementation detail, and users should not use this enum directly, so I would like to avoid potential confusion.
Quoted 9 lines
I have played a few sealed trait tricks on my end, but seems I cannot achieve:
1)
OrderingDescis only accessible in the atomic mod.
2)Allis only impl-able in the atomic mod, while it can be used as a trait bound outside kernel crate.Maybe there is a trick I'm missing?
Something like this seems to work:
pub(super) mod private {
/// Describes the exact memory ordering of an `impl` [`All`].
pub enum Ordering {
/// Relaxed ordering.
Relaxed,
/// Acquire ordering.
Acquire,
/// Release ordering.
Release,
/// Fully-ordered.
Full,
}
pub trait HasOrderingDesc {
/// Describes the exact memory ordering.
const ORDERING: Ordering;
}
}
/// The trait bound for annotating operations that should support all orderings.
pub trait All: private::HasOrderingDesc { }
impl private::HasOrderingDesc for Relaxed {
const ORDERING: private::Ordering = private::Ordering::Relaxed;
}the trick is to seal the enum and the trait together.
Regards,
Boqun
Quoted 17 lines
Quoted 16 lines
Quoted 4 lines
rust/kernel/sync/atomic/ordering.rs new file +4
-0+/// Relaxed ordering. + Relaxed, +/// Acquire ordering. + Acquire,rust/kernel/sync/atomic/ordering.rs line 77
Quoted 10 lines
rust/kernel/sync/atomic/ordering.rs new file +10
-0+/// Release ordering. + Release, +/// Fully-ordered. + Full, +} + +/// The trait bound for annotating operations that should support all orderings. +pub trait All { +/// Describes the exact memory ordering. +const ORDER :OrderingDesc ;And then here:
ORDERING.
[..]
Re: [PATCH v4 03/10] rust: sync: atomic: Add ordering annotation types
Quoted 56 lines · Boqun Feng
On Tue, Jun 10, 2025 at 10:30:55AM -0700, Boqun Feng wrote:
[...]Quoted 16 lines
Quoted 5 lines
rust/kernel/sync/atomic/ordering.rs line 63
Quoted 2 lines
rust/kernel/sync/atomic/ordering.rs new file +2
-0+/// Describes the exact memory ordering of an `impl` [`All`]. +pub enum OrderingDesc {Why not name this
Ordering?I was trying to avoid having an
Orderingenum in aorderingmod. Also I want to save the name "Ordering" for the generic type parameter of an atomic operation, e.g.pub fn xchg<Ordering: ALL>(..)
this enum is more of an internal implementation detail, and users should not use this enum directly, so I would like to avoid potential confusion.
Quoted 9 lines
I have played a few sealed trait tricks on my end, but seems I cannot achieve:
1)
OrderingDescis only accessible in the atomic mod.
2)Allis only impl-able in the atomic mod, while it can be used as a trait bound outside kernel crate.Maybe there is a trick I'm missing?
Something like this seems to work:
pub(super) mod private { /// Describes the exact memory ordering of an `impl` [`All`]. pub enum Ordering { /// Relaxed ordering. Relaxed, /// Acquire ordering. Acquire, /// Release ordering. Release, /// Fully-ordered. Full, } pub trait HasOrderingDesc { /// Describes the exact memory ordering. const ORDERING: Ordering; } } /// The trait bound for annotating operations that should support all orderings. pub trait All: private::HasOrderingDesc { } impl private::HasOrderingDesc for Relaxed { const ORDERING: private::Ordering = private::Ordering::Relaxed; }
Quoted 22 lines
the trick is to seal the enum and the trait together.
Regards,
BoqunQuoted 17 lines
Quoted 16 lines
Quoted 4 lines
rust/kernel/sync/atomic/ordering.rs new file +4
-0+/// Relaxed ordering. + Relaxed, +/// Acquire ordering. + Acquire,rust/kernel/sync/atomic/ordering.rs line 77
Quoted 10 lines
rust/kernel/sync/atomic/ordering.rs new file +10
-0+/// Release ordering. + Release, +/// Fully-ordered. + Full, +} + +/// The trait bound for annotating operations that should support all orderings. +pub trait All { +/// Describes the exact memory ordering. +const ORDER :OrderingDesc ;And then here:
ORDERING.
After a second thought, the following is probably what I will go for:
/// The annotation type for relaxed memory ordering.
pub struct Relaxed;
/// The annotation type for acquire memory ordering.
pub struct Acquire;
/// The annotation type for release memory ordering.
pub struct Release;
/// The annotation type for fully-order memory ordering.
pub struct Full;
/// Describes the exact memory ordering.
pub enum OrderingType {
/// Relaxed ordering.
Relaxed,
/// Acquire ordering.
Acquire,
/// Release ordering.
Release,
/// Fully-ordered.
Full,
}
mod internal {
/// Unit types for ordering annotation.
///
/// Sealed trait, can be only implemented inside atomic mod.
pub trait OrderingUnit {
/// Describes the exact memory ordering.
const TYPE: super::OrderingType;
}
}
impl internal::OrderingUnit for Relaxed {
const TYPE: OrderingType = OrderingType::Relaxed;
}
impl internal::OrderingUnit for Acquire {
const TYPE: OrderingType = OrderingType::Acquire;
}
impl internal::OrderingUnit for Release {
const TYPE: OrderingType = OrderingType::Release;
}
impl internal::OrderingUnit for Full {
const TYPE: OrderingType = OrderingType::Full;
}That is:
1) Rename "OrderingDesc" into "OrderingType", and make it public.
2) Provide a sealed trait (OrderingUnit) for all the unit types that describe ordering.
3) Instead of "ORDER" or "ORDERING", name the enum constant "TYPE".
An example shows why is probably an xchg() implementation, if I was to follow the previous naming suggestion, it will be:
match Ordering::ORDERING {
<some mode path>::Ordering::Relaxed => atomic_xchg_relaxed(...),
...
}with the current one, it will be:
match Ordering::TYPE {
// assume we "use ordering::OrderingType"
OrderingType::Relaxed => atomic_xchg_relaxed(...),
...
}I think this version is much better.
Regards,
Boqun
Quoted 1 line
[..]
Benno Lossin
reviewerRe: [PATCH v4 03/10] rust: sync: atomic: Add ordering annotation types
Quoted 150 lines · Boqun Feng
Quoted 56 lines · Boqun Feng
On Tue, Jun 10, 2025 at 10:30:55AM -0700, Boqun Feng wrote:
[...]Quoted 16 lines
Quoted 5 lines
rust/kernel/sync/atomic/ordering.rs line 63
Quoted 2 lines
rust/kernel/sync/atomic/ordering.rs new file +2
-0+/// Describes the exact memory ordering of an `impl` [`All`]. +pub enum OrderingDesc {Why not name this
Ordering?I was trying to avoid having an
Orderingenum in aorderingmod. Also I want to save the name "Ordering" for the generic type parameter of an atomic operation, e.g.pub fn xchg<Ordering: ALL>(..)
this enum is more of an internal implementation detail, and users should not use this enum directly, so I would like to avoid potential confusion.
Quoted 9 lines
I have played a few sealed trait tricks on my end, but seems I cannot achieve:
1)
OrderingDescis only accessible in the atomic mod.
2)Allis only impl-able in the atomic mod, while it can be used as a trait bound outside kernel crate.Maybe there is a trick I'm missing?
Something like this seems to work:
pub(super) mod private { /// Describes the exact memory ordering of an `impl` [`All`]. pub enum Ordering { /// Relaxed ordering. Relaxed, /// Acquire ordering. Acquire, /// Release ordering. Release, /// Fully-ordered. Full, } pub trait HasOrderingDesc { /// Describes the exact memory ordering. const ORDERING: Ordering; } } /// The trait bound for annotating operations that should support all orderings. pub trait All: private::HasOrderingDesc { } impl private::HasOrderingDesc for Relaxed { const ORDERING: private::Ordering = private::Ordering::Relaxed; }Quoted 22 lines
the trick is to seal the enum and the trait together.
Regards,
BoqunQuoted 17 lines
Quoted 16 lines
Quoted 4 lines
rust/kernel/sync/atomic/ordering.rs new file +4
-0+/// Relaxed ordering. + Relaxed, +/// Acquire ordering. + Acquire,rust/kernel/sync/atomic/ordering.rs line 77
Quoted 10 lines
rust/kernel/sync/atomic/ordering.rs new file +10
-0+/// Release ordering. + Release, +/// Fully-ordered. + Full, +} + +/// The trait bound for annotating operations that should support all orderings. +pub trait All { +/// Describes the exact memory ordering. +const ORDER :OrderingDesc ;And then here:
ORDERING.After a second thought, the following is probably what I will go for:
/// The annotation type for relaxed memory ordering. pub struct Relaxed; /// The annotation type for acquire memory ordering. pub struct Acquire; /// The annotation type for release memory ordering. pub struct Release; /// The annotation type for fully-order memory ordering. pub struct Full; /// Describes the exact memory ordering. pub enum OrderingType { /// Relaxed ordering. Relaxed, /// Acquire ordering. Acquire, /// Release ordering. Release, /// Fully-ordered. Full, } mod internal { /// Unit types for ordering annotation. /// /// Sealed trait, can be only implemented inside atomic mod. pub trait OrderingUnit { /// Describes the exact memory ordering. const TYPE: super::OrderingType; } } impl internal::OrderingUnit for Relaxed { const TYPE: OrderingType = OrderingType::Relaxed; } impl internal::OrderingUnit for Acquire { const TYPE: OrderingType = OrderingType::Acquire; } impl internal::OrderingUnit for Release { const TYPE: OrderingType = OrderingType::Release; } impl internal::OrderingUnit for Full { const TYPE: OrderingType = OrderingType::Full; }That is:
1) Rename "OrderingDesc" into "OrderingType", and make it public.
2) Provide a sealed trait (OrderingUnit) for all the unit types that describe ordering.
3) Instead of "ORDER" or "ORDERING", name the enum constant "TYPE".An example shows why is probably an
xchg()implementation, if I was to follow the previous naming suggestion, it will be:match Ordering::ORDERING { <some mode path>::Ordering::Relaxed => atomic_xchg_relaxed(...), ... }with the current one, it will be:
Quoted 7 lines
match Ordering::TYPE { // assume we "use ordering::OrderingType" OrderingType::Relaxed => atomic_xchg_relaxed(...), ... }I think this version is much better.
Agreed :)
---
Cheers,
Benno
PATCH v4 4/10 rust: sync: atomic: Add generic atomics
In reply to Boqun FengTo provide using LKMM atomics for Rust code, a generic Atomic<T> is added, currently T needs to be Send + Copy because these are the straightforward usages and all basic types support this. The trait AllowAtomic should be only implemented inside atomic mod until the generic atomic framework is mature enough (unless the implementer is a #[repr(transparent)] new type).
AtomicImpl types are automatically AllowAtomic, and so far only basic operations load() and store() are introduced.
--- rust/kernel/sync/atomic.rs | 2 + rust/kernel/sync/atomic/generic.rs | 258 +++++++++++++++++++++++++++++ 2 files changed, 260 insertions(+) create mode 100644 rust/kernel/sync/atomic/generic.rs
rust/kernel/sync/atomic.rs +2 -0
@@ -16,7 +16,9 @@//! //! [`LKMM`]: srctree/tools/memory-mode/ +pub mod generic;pub mod ops;pub mod ordering; +pub use generic::Atomic ;pub use ordering::{Acquire ,Full ,Relaxed ,Release };
rust/kernel/sync/atomic/generic.rs new file +258 -0
258 lines not on this page · raw message
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH v4 5/10 rust: sync: atomic: Add atomic {cmp,}xchg operations
In reply to Boqun Fengxchg() and cmpxchg() are basic operations on atomic. Provide these based on C APIs.
Note that cmpxchg() use the similar function signature as compare_exchange() in Rust std: returning a Result, Ok(old) means the operation succeeds and Err(old) means the operation fails.
--- rust/kernel/sync/atomic/generic.rs | 154 +++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+)
rust/kernel/sync/atomic/generic.rs +154 -0
@@ -256,3 +256,157 @@ pub fn store<Ordering: ReleaseOrRelaxed>(&self, v: T, _: Ordering) { }; } } + +impl <T :AllowAtomic >Atomic <T > +where +T ::Repr :AtomicHasXchgOps , +{ +/// Atomic exchange. +/// +/// # Examples +/// +/// ```rust +/// use kernel::sync::atomic::{Atomic, Acquire, Relaxed}; +/// +/// let x = Atomic::new(42); +/// +/// assert_eq!(42, x.xchg(52, Acquire)); +/// assert_eq!(52, x.load(Relaxed)); +/// ``` +#[doc(alias( "atomic_xchg" , "atomic64_xchg" ))] +#[inline(always)] +pub fn xchg <Ordering :All >(&self, v:T , _:Ordering ) ->T { +let v =T ::into_repr (v); +let a = self.as_ptr ().cast ::<T ::Repr >(); + +// SAFETY: +// - For calling the atomic_xchg*() function: +// - `self.as_ptr()` is a valid pointer, and per the safety requirement of `AllocAtomic`, +// a `*mut T` is a valid `*mut T::Repr`. Therefore `a` is a valid pointer, +// - per the type invariants, the following atomic operation won't cause data races. +// - For extra safety requirement of usage on pointers returned by `self.as_ptr(): +// - atomic operations are used here. +let ret =unsafe { +match Ordering ::ORDER { +OrderingDesc ::Full =>T ::Repr ::atomic_xchg (a, v), +OrderingDesc ::Acquire =>T ::Repr ::atomic_xchg_acquire (a, v), +OrderingDesc ::Release =>T ::Repr ::atomic_xchg_release (a, v), +OrderingDesc ::Relaxed =>T ::Repr ::atomic_xchg_relaxed (a, v), + } + }; + +T ::from_repr (ret) + } + +/// Atomic compare and exchange. +/// +/// Compare: The comparison is done via the byte level comparison between the atomic variables +/// with the `old` value. +/// +/// Ordering: When succeeds, provides the corresponding ordering as the `Ordering` type +/// parameter indicates, and a failed one doesn't provide any ordering, the read part of a +/// failed cmpxchg should be treated as a relaxed read. +/// +/// Returns `Ok(value)` if cmpxchg succeeds, and `value` is guaranteed to be equal to `old`, +/// otherwise returns `Err(value)`, and `value` is the value of the atomic variable when +/// cmpxchg was happening. +/// +/// # Examples +/// +/// ```rust +/// use kernel::sync::atomic::{Atomic, Full, Relaxed}; +/// +/// let x = Atomic::new(42); +/// +/// // Checks whether cmpxchg succeeded. +/// let success = x.cmpxchg(52, 64, Relaxed).is_ok(); +/// # assert!(!success); +/// +/// // Checks whether cmpxchg failed. +/// let failure = x.cmpxchg(52, 64, Relaxed).is_err(); +/// # assert!(failure); +/// +/// // Uses the old value if failed, probably re-try cmpxchg. +/// match x.cmpxchg(52, 64, Relaxed) { +/// Ok(_) => { }, +/// Err(old) => { +/// // do something with `old`. +/// # assert_eq!(old, 42); +/// } +/// } +/// +/// // Uses the latest value regardlessly, same as atomic_cmpxchg() in C. +/// let latest = x.cmpxchg(42, 64, Full).unwrap_or_else(|old| old); +/// # assert_eq!(42, latest); +/// assert_eq!(64, x.load(Relaxed)); +/// ``` +#[doc(alias( +"atomic_cmpxchg" , +"atomic64_cmpxchg" , +"atomic_try_cmpxchg" , +"atomic64_try_cmpxchg" +))] +#[inline(always)] +pub fn cmpxchg <Ordering :All >(&self,mut old:T , new:T , o:Ordering ) ->Result <T ,T > { +// Note on code generation: +// +// try_cmpxchg() is used to implement cmpxchg(), and if the helper functions are inlined, +// the compiler is able to figure out that branch is not needed if the users don't care +// about whether the operation succeeds or not. One exception is on x86, due to commit +// 44fe84459faf ("locking/atomic: Fix atomic_try_cmpxchg() semantics"), the +// atomic_try_cmpxchg() on x86 has a branch even if the caller doesn't care about the +// success of cmpxchg and only wants to use the old value. For example, for code like: +// +// let latest = x.cmpxchg(42, 64, Full).unwrap_or_else(|old| old); +// +// It will still generate code: +// +// movl $0x40, %ecx +// movl $0x34, %eax +// lock +// cmpxchgl %ecx, 0x4(%rsp) +// jne 1f +// 2: +// ... +// 1: movl %eax, %ecx +// jmp 2b +// +// This might be "fixed" by introducing a try_cmpxchg_exclusive() that knows the "*old" +// location in the C function is always safe to write. +if self.try_cmpxchg (&mut old, new, o) { +Ok (old) + }else { +Err (old) + } + } + +/// Atomic compare and exchange and returns whether the operation succeeds. +/// +/// "Compare" and "Ordering" part are the same as [`Atomic::cmpxchg()`]. +/// +/// Returns `true` means the cmpxchg succeeds otherwise returns `false` with `old` updated to +/// the value of the atomic variable when cmpxchg was happening. +#[inline(always)] +fn try_cmpxchg <Ordering :All >(&self, old: &mut T , new:T , _:Ordering ) ->bool { +let old = (oldas *mut T ).cast ::<T ::Repr >(); +let new =T ::into_repr (new); +let a = self.0 .get ().cast ::<T ::Repr >(); + +// SAFETY: +// - For calling the atomic_try_cmpchg*() function: +// - `self.as_ptr()` is a valid pointer, and per the safety requirement of `AllowAtomic`, +// a `*mut T` is a valid `*mut T::Repr`. Therefore `a` is a valid pointer, +// - per the type invariants, the following atomic operation won't cause data races. +// - `old` is a valid pointer to write because it comes from a mutable reference. +// - For extra safety requirement of usage on pointers returned by `self.as_ptr(): +// - atomic operations are used here. +unsafe { +match Ordering ::ORDER { +OrderingDesc ::Full =>T ::Repr ::atomic_try_cmpxchg (a, old, new), +OrderingDesc ::Acquire =>T ::Repr ::atomic_try_cmpxchg_acquire (a, old, new), +OrderingDesc ::Release =>T ::Repr ::atomic_try_cmpxchg_release (a, old, new), +OrderingDesc ::Relaxed =>T ::Repr ::atomic_try_cmpxchg_relaxed (a, old, new), + } + } + } +}
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH v4 6/10 rust: sync: atomic: Add the framework of arithmetic operations
In reply to Boqun FengOne important set of atomic operations is the arithmetic operations, i.e. add(), sub(), fetch_add(), add_return(), etc. However it may not make senses for all the types that AllowAtomic to have arithmetic operations, for example a Foo(u32) may not have a reasonable add() or sub(), plus subword types (u8 and u16) currently don't have atomic arithmetic operations even on C side and might not have them in the future in Rust (because they are usually suboptimal on a few architecures). Therefore add a subtrait of AllowAtomic describing which types have and can do atomic arithemtic operations.
A few things about this AllowAtomicArithmetic trait:
* It has an associate type Delta instead of using AllowAllowAtomic::Repr because, a Bar(u32) (whose Repr is i32) may not wants an add(&self, i32), but an add(&self, u32).
* AtomicImpl types already implement an AtomicHasArithmeticOps trait, so add blanket implementation for them. In the future, i8 and i16 may impl AtomicImpl but not AtomicHasArithmeticOps if arithemtic operations are not available.
Only add() and fetch_add() are added. The rest will be added in the future.
--- rust/kernel/sync/atomic/generic.rs | 102 +++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+)
rust/kernel/sync/atomic/generic.rs +102 -0
@@ -3,6 +3,7 @@//! Generic atomic primitives. use super ::ops::*; +use super ::ordering;use super ::ordering::*;use crate ::types::Opaque ;
@@ -57,6 +58,23 @@ fn from_repr(repr: Self::Repr) -> Self { } } +/// Atomics that allows arithmetic operations with an integer type. +pub trait AllowAtomicArithmetic :AllowAtomic { +/// The delta types for arithmetic operations. +type Delta ; + +/// Converts [`Self::Delta`] into the representation of the atomic type. +fn delta_into_repr (d:Self ::Delta ) ->Self ::Repr ; +} + +impl <T :AtomicImpl +AtomicHasArithmeticOps >AllowAtomicArithmetic for T { +type Delta =Self ; + +fn delta_into_repr (d:Self ::Delta ) ->Self ::Repr { + d + } +} +impl <T :AllowAtomic >Atomic <T > {/// Creates a new atomic. pub const fn new (v:T ) ->Self {
@@ -410,3 +428,87 @@ fn try_cmpxchg<Ordering: All>(&self, old: &mut T, new: T, _: Ordering) -> bool { } } } + +impl <T :AllowAtomicArithmetic >Atomic <T > +where +T ::Repr :AtomicHasArithmeticOps , +{ +/// Atomic add. +/// +/// The addition is a wrapping addition. +/// +/// # Examples +/// +/// ```rust +/// use kernel::sync::atomic::{Atomic, Relaxed}; +/// +/// let x = Atomic::new(42); +/// +/// assert_eq!(42, x.load(Relaxed)); +/// +/// x.add(12, Relaxed); +/// +/// assert_eq!(54, x.load(Relaxed)); +/// ``` +#[inline(always)] +pub fn add <Ordering :RelaxedOnly >(&self, v:T ::Delta , _:Ordering ) { +let v =T ::delta_into_repr (v); +let a = self.as_ptr ().cast ::<T ::Repr >(); + +// SAFETY: +// - For calling the atomic_add() function: +// - `self.as_ptr()` is a valid pointer, and per the safety requirement of `AllocAtomic`, +// a `*mut T` is a valid `*mut T::Repr`. Therefore `a` is a valid pointer, +// - per the type invariants, the following atomic operation won't cause data races. +// - For extra safety requirement of usage on pointers returned by `self.as_ptr(): +// - atomic operations are used here. +unsafe { +T ::Repr ::atomic_add (a, v); + } + } + +/// Atomic fetch and add. +/// +/// The addition is a wrapping addition. +/// +/// # Examples +/// +/// ```rust +/// use kernel::sync::atomic::{Atomic, Acquire, Full, Relaxed}; +/// +/// let x = Atomic::new(42); +/// +/// assert_eq!(42, x.load(Relaxed)); +/// +/// assert_eq!(54, { x.fetch_add(12, Acquire); x.load(Relaxed) }); +/// +/// let x = Atomic::new(42); +/// +/// assert_eq!(42, x.load(Relaxed)); +/// +/// assert_eq!(54, { x.fetch_add(12, Full); x.load(Relaxed) } ); +/// ``` +#[inline(always)] +pub fn fetch_add <Ordering :All >(&self, v:T ::Delta , _:Ordering ) ->T { +let v =T ::delta_into_repr (v); +let a = self.as_ptr ().cast ::<T ::Repr >(); + +// SAFETY: +// - For calling the atomic_fetch_add*() function: +// - `self.as_ptr()` is a valid pointer, and per the safety requirement of `AllocAtomic`, +// a `*mut T` is a valid `*mut T::Repr`. Therefore `a` is a valid pointer, +// - per the type invariants, the following atomic operation won't cause data races. +// - For extra safety requirement of usage on pointers returned by `self.as_ptr(): +// - atomic operations are used here. +let ret =unsafe { +match Ordering ::ORDER { + ordering::OrderingDesc ::Full =>T ::Repr ::atomic_fetch_add (a, v), + ordering::OrderingDesc ::Acquire =>T ::Repr ::atomic_fetch_add_acquire (a, v), + ordering::OrderingDesc ::Release =>T ::Repr ::atomic_fetch_add_release (a, v), + ordering::OrderingDesc ::Relaxed =>T ::Repr ::atomic_fetch_add_relaxed (a, v), + } + }; + +T ::from_repr (ret) + } +}
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH v4 7/10 rust: sync: atomic: Add Atomic<u{32,64}>
In reply to Boqun FengAdd generic atomic support for basic unsigned types that have an AtomicImpl with the same size and alignment.
Unit tests are added including Atomic<i32> and Atomic<i64>.
--- rust/kernel/sync/atomic.rs | 83 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+)
rust/kernel/sync/atomic.rs +83 -0
@@ -22,3 +22,86 @@pub use generic::Atomic ;pub use ordering::{Acquire ,Full ,Relaxed ,Release }; + +// SAFETY: `u64` and `i64` has the same size and alignment. +unsafe impl generic::AllowAtomic for u64 { +type Repr =i64 ; + +fn into_repr (self) ->Self ::Repr { + selfas _ + } + +fn from_repr (repr:Self ::Repr ) ->Self { + repras _ + } +} + +impl generic::AllowAtomicArithmetic for u64 { +type Delta =u64 ; + +fn delta_into_repr (d:Self ::Delta ) ->Self ::Repr { + das _ + } +} + +// SAFETY: `u32` and `i32` has the same size and alignment. +unsafe impl generic::AllowAtomic for u32 { +type Repr =i32 ; + +fn into_repr (self) ->Self ::Repr { + selfas _ + } + +fn from_repr (repr:Self ::Repr ) ->Self { + repras _ + } +} + +impl generic::AllowAtomicArithmetic for u32 { +type Delta =u32 ; + +fn delta_into_repr (d:Self ::Delta ) ->Self ::Repr { + das _ + } +} + +use crate ::macros::kunit_tests; + +#[kunit_tests(rust_atomics)] +mod tests { +use super ::*; + +// Call $fn($val) with each $type of $val. +macro_rules! for_each_type { + ($val:literal in [$($type:ty),*] $fn:expr) => { + $({ +let v: $type = $val; + + $fn(v); + })* + } + } + +#[test] +fn atomic_basic_tests () { +for_each_type! (42 in [i32 ,i64 ,u32 ,u64 ] |v| { +let x =Atomic ::new (v); + +assert_eq! (v, x.load (Relaxed )); + }); + } + +#[test] +fn atomic_arithmetic_tests () { +for_each_type! (42 in [i32 ,i64 ,u32 ,u64 ] |v| { +let x =Atomic ::new (v); + +assert_eq! (v, x.fetch_add (12 ,Full )); +assert_eq! (v +12 , x.load (Relaxed )); + + x.add (13 ,Relaxed ); + +assert_eq! (v +25 , x.load (Relaxed )); + }); + } +}
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH v4 8/10 rust: sync: atomic: Add Atomic<{usize,isize}>
In reply to Boqun FengAdd generic atomic support for usize and isize. Note that instead of mapping directly to atomic_long_t, the represention type (AllowAtomic::Repr) is selected based on CONFIG_64BIT. This reduces the necessarity of creating atomic_long_* helpers, which could save the binary size of kernel if inline helpers are not available.
--- rust/kernel/sync/atomic.rs | 54 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-)
rust/kernel/sync/atomic.rs +52 -2
@@ -65,6 +65,56 @@ fn delta_into_repr(d: Self::Delta) -> Self::Repr { } } +// SAFETY: `usize` has the same size and the alignment as `i64` for 64bit and the same as `i32` for +// 32bit. +unsafe impl generic::AllowAtomic for usize { +#[cfg( CONFIG_64BIT )] +type Repr =i64 ; +#[cfg(not( CONFIG_64BIT ))] +type Repr =i32 ; + +fn into_repr (self) ->Self ::Repr { + selfas Self ::Repr + } + +fn from_repr (repr:Self ::Repr ) ->Self { + repras Self + } +} + +impl generic::AllowAtomicArithmetic for usize { +type Delta =usize ; + +fn delta_into_repr (d:Self ::Delta ) ->Self ::Repr { + das Self ::Repr + } +} + +// SAFETY: `isize` has the same size and the alignment as `i64` for 64bit and the same as `i32` for +// 32bit. +unsafe impl generic::AllowAtomic for isize { +#[cfg( CONFIG_64BIT )] +type Repr =i64 ; +#[cfg(not( CONFIG_64BIT ))] +type Repr =i32 ; + +fn into_repr (self) ->Self ::Repr { + selfas Self ::Repr + } + +fn from_repr (repr:Self ::Repr ) ->Self { + repras Self + } +} + +impl generic::AllowAtomicArithmetic for isize { +type Delta =isize ; + +fn delta_into_repr (d:Self ::Delta ) ->Self ::Repr { + das Self ::Repr + } +} +use crate ::macros::kunit_tests;#[kunit_tests(rust_atomics)]
@@ -84,7 +134,7 @@ macro_rules! for_each_type {#[test] fn atomic_basic_tests () { -for_each_type! (42 in [i32 ,i64 ,u32 ,u64 ] |v| { +for_each_type! (42 in [i32 ,i64 ,u32 ,u64 ,isize ,usize ] |v| {let x =Atomic ::new (v);assert_eq! (v, x.load (Relaxed ));
@@ -93,7 +143,7 @@ fn atomic_basic_tests() {#[test] fn atomic_arithmetic_tests () { -for_each_type! (42 in [i32 ,i64 ,u32 ,u64 ] |v| { +for_each_type! (42 in [i32 ,i64 ,u32 ,u64 ,isize ,usize ] |v| {let x =Atomic ::new (v);assert_eq! (v, x.fetch_add (12 ,Full ));
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH v4 9/10 rust: sync: atomic: Add Atomic<*mut T>
In reply to Boqun FengAdd atomic support for raw pointer values, similar to isize and usize, the representation type is selected based on CONFIG_64BIT.
*mut T is not Send, however Atomic<*mut T> definitely needs to be a Sync, and that's the whole point of atomics: being able to have multiple shared references in different threads so that they can sync with each other. As a result, a pointer value will be transferred from one thread to another via Atomic<*mut T>:
<thread 1> <thread 2> x.store(p1, Relaxed); let p = x.load(p1, Relaxed);
This means a raw pointer value (*mut T) needs to be able to transfer across thread boundaries, which is essentially Send.
To reflect this in the type system, and based on the fact that pointer values can be transferred safely (only using them to dereference is unsafe), as suggested by Alice, extend the AllowAtomic trait to include a customized Send semantics, that is: impl AllowAtomic has to be safe to be transferred across thread boundaries.
--- rust/kernel/sync/atomic.rs | 19 +++++++++++++++++++ rust/kernel/sync/atomic/generic.rs | 16 +++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-)
rust/kernel/sync/atomic.rs +19 -0
@@ -114,6 +114,22 @@ fn delta_into_repr(d: Self::Delta) -> Self::Repr { das Self ::Repr } } +// SAFETY: A `*mut T` has the same size and the alignment as `i64` for 64bit and the same as `i32` +// for 32bit. And it's safe to transfer the ownership of a pointer value to another thread. +unsafe impl <T > generic::AllowAtomic for *mut T { +#[cfg( CONFIG_64BIT )] +type Repr =i64 ; +#[cfg(not( CONFIG_64BIT ))] +type Repr =i32 ; + +fn into_repr (self) ->Self ::Repr { + selfas Self ::Repr + } + +fn from_repr (repr:Self ::Repr ) ->Self { + repras Self + } +}use crate ::macros::kunit_tests;
@@ -139,6 +155,9 @@ fn atomic_basic_tests() {assert_eq! (v, x.load (Relaxed )); }); + +let x =Atomic ::new (core::ptr::null_mut ::<i32 >()); +assert! (x.load (Relaxed ).is_null ()); }#[test]
rust/kernel/sync/atomic/generic.rs +13 -3
@@ -19,6 +19,10 @@#[repr(transparent)] pub struct Atomic <T :AllowAtomic >(Opaque <T >); +// SAFETY: `Atomic<T>` is safe to send between execution contexts, because `T` is `AllowAtomic` and +// `AllowAtomic`'s safety requirement guarantees that. +unsafe impl <T :AllowAtomic >Send for Atomic <T > {} +// SAFETY: `Atomic<T>` is safe to share among execution contexts because all accesses are atomic. unsafe impl <T :AllowAtomic >Sync for Atomic <T > {}
@@ -31,8 +35,13 @@ unsafe impl<T: AllowAtomic> Sync for Atomic<T> {}/// /// # Safety /// -/// [`Self`] must have the same size and alignment as [`Self::Repr`]. -pub unsafe trait AllowAtomic :Sized +Send +Copy { +/// - [`Self`] must have the same size and alignment as [`Self::Repr`]. +/// - The implementer must guarantee it's safe to transfer ownership from one execution context to +/// another, this means it has to be a [`Send`], but because `*mut T` is not [`Send`] and that's +/// the basic type needs to support atomic operations, so this safety requirement is added to +/// [`AllowAtomic`] trait. This safety requirement is automatically satisfied if the type is a +/// [`Send`]. +pub unsafe trait AllowAtomic :Sized +Copy {/// The backing atomic implementation type. type Repr :AtomicImpl ;
@@ -45,7 +54,8 @@ pub unsafe trait AllowAtomic: Sized + Send + Copy {// An `AtomicImpl` is automatically an `AllowAtomic`. // -// SAFETY: `T::Repr` is `Self` (i.e. `T`), so they have the same size and alignment. +// SAFETY: `T::Repr` is `Self` (i.e. `T`), so they have the same size and alignment. And all +// `AtomicImpl` types are `Send`. unsafe impl <T :AtomicImpl >AllowAtomic for T {type Repr =Self ;
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH v4 10/10 rust: sync: Add memory barriers
In reply to Boqun FengMemory barriers are building blocks for concurrent code, hence provide a minimal set of them.
The compiler barrier, barrier(), is implemented in inline asm instead of using core::sync::atomic::compiler_fence() because memory models are
different: kernel's atomics are implemented in inline asm therefore the compiler barrier should be implemented in inline asm as well. Also it's currently only public to the kernel crate until there's a reasonable driver usage.
--- rust/helpers/barrier.c | 18 ++++++++++ rust/helpers/helpers.c | 1 + rust/kernel/sync.rs | 1 + rust/kernel/sync/barrier.rs | 67 +++++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 rust/helpers/barrier.c create mode 100644 rust/kernel/sync/barrier.rs
rust/helpers/barrier.c new file +18 -0
@@ -0,0 +1,18 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <asm/barrier.h> + +void rust_helper_smp_mb (void ) +{ +smp_mb (); +} + +void rust_helper_smp_wmb (void ) +{ +smp_wmb (); +} + +void rust_helper_smp_rmb (void ) +{ +smp_rmb (); +}
rust/helpers/helpers.c +1 -0
@@ -9,6 +9,7 @@#include "atomic.c" #include "auxiliary.c" +#include "barrier.c" #include "blk.c" #include "bug.c" #include "build_assert.c"
rust/kernel/sync.rs +1 -0
@@ -11,6 +11,7 @@mod arc;pub mod atomic; +pub mod barrier;mod condvar;pub mod lock;mod locked_by;
rust/kernel/sync/barrier.rs new file +67 -0
67 lines not on this page · raw message
Signature · 1 line
2.39.5 (Apple Git-154)