Initial LKMM atomics support in Rust
Subsystems (6)
cover Initial LKMM atomics support in Rust
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:
arch/arm64/kernel/cpufeature.c +2 -0
@@ -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/[email protected]/
[2]: https://lore.kernel.org/rust-for-linux/[email protected]/
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
Signature · 1 line
2.45.2
RFC 1/2 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/atomic_helpers.h | 1035 +++++++++++++++++++++ rust/helpers.c | 2 + scripts/atomic/gen-atomics.sh | 1 + scripts/atomic/gen-rust-atomic-helpers.sh | 64 ++ 4 files changed, 1102 insertions(+) create mode 100644 rust/atomic_helpers.h create mode 100755 scripts/atomic/gen-rust-atomic-helpers.sh
rust/atomic_helpers.h new file +1035 -0
@@ -0,0 +1,1035 @@ +// 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> + +__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 */ +// e4edb6174dd42a265284958f00a7cea7ddb464b1
rust/helpers.c +2 -0
@@ -34,6 +34,8 @@#include <linux/workqueue.h> #include "helpers.h" +#include "atomic_helpers.h" + __rust_helper __noreturn void rust_helper_BUG(void) {BUG ();
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/atomic_helpers.h 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 +64 -0
64 lines not on this page · raw message
Signature · 1 line
2.45.2
Re: [RFC 1/2] rust: Introduce atomic API helpers
rust/atomic_helpers.h line 4
Quoted 2 lines · Boqun Feng
rust/atomic_helpers.h new file +2
-0+// Generated by scripts/atomic/gen-rust-atomic-helpers.sh +// DO NOT MODIFY THIS FILE DIRECTLY
Why not just build this at build time and not check the file into the tree if it is always automatically generated? That way it never gets out of sync. We do this for other types of auto-generated files in the kernel today already.
thanks,
greg k-h
Peter Zijlstra
maintainerRe: [RFC 1/2] rust: Introduce atomic API helpers
Quoted 8 lines · Greg Kroah-Hartman
rust/atomic_helpers.h line 4
Quoted 2 lines · Boqun Feng
rust/atomic_helpers.h new file +2
-0+// Generated by scripts/atomic/gen-rust-atomic-helpers.sh +// DO NOT MODIFY THIS FILE DIRECTLY Why not just build this at build time and not check the file into the tree if it is always automatically generated? That way it never gets out of sync. We do this for other types of auto-generated files in the kernel today already.
Part of the problem is, is that a *TON* of files depend on the atomic.h headers. If we'd generate it on every build, you'd basically get to rebuild the whole kernel every single time.
Also, these files don't change too often. And if you look, there's a hash in those files which is used to check if things somehow got stale.
Re: [RFC 1/2] rust: Introduce atomic API helpers
Quoted 10 lines · Peter Zijlstra
Quoted 8 lines · Greg Kroah-Hartman
rust/atomic_helpers.h line 4
Quoted 2 lines · Boqun Feng
rust/atomic_helpers.h new file +2
-0+// Generated by scripts/atomic/gen-rust-atomic-helpers.sh +// DO NOT MODIFY THIS FILE DIRECTLY Why not just build this at build time and not check the file into the tree if it is always automatically generated? That way it never gets out of sync. We do this for other types of auto-generated files in the kernel today already.
Quoted 6 lines
Part of the problem is, is that a *TON* of files depend on the
atomic.hheaders. If we'd generate it on every build, you'd basically get to rebuild the whole kernel every single time.Also, these files don't change too often. And if you look, there's a hash in those files which is used to check if things somehow got stale.
Ok, fair enough, if you all don't think this will change often...
Mark Rutland
reviewerRe: [RFC 1/2] rust: Introduce atomic API helpers
In reply to Peter ZijlstraQuoted 10 lines · Peter Zijlstra
Quoted 8 lines · Greg Kroah-Hartman
rust/atomic_helpers.h line 4
Quoted 2 lines · Boqun Feng
rust/atomic_helpers.h new file +2
-0+// Generated by scripts/atomic/gen-rust-atomic-helpers.sh +// DO NOT MODIFY THIS FILE DIRECTLY Why not just build this at build time and not check the file into the tree if it is always automatically generated? That way it never gets out of sync. We do this for other types of auto-generated files in the kernel today already.
Quoted 6 lines
Part of the problem is, is that a *TON* of files depend on the
atomic.hheaders. If we'd generate it on every build, you'd basically get to rebuild the whole kernel every single time.Also, these files don't change too often. And if you look, there's a hash in those files which is used to check if things somehow got stale.
That and:
1) The generation is currently slow (multiple seconds), which gets people angry if they have to wait for it for a clean build. Improving that would require changing the way we generate the headers (e.g. rewrite that in something else to avoid the subshell fork problem).
Last I looked there wasn't an obvious better way to do this, because nice options ended up pulling in more build-time dependencies.
2) Linus wanted git grep to work, which meant checking in the generated files:
https://lore.kernel.org/all/CA+55aFxjU0op8QLLu0n-RjHBs7gQsLvD8jcyedgw6jeZFN7B+Q@mail.gmail.com/
Mark.
Mark Rutland
reviewerRe: [RFC 1/2] rust: Introduce atomic API helpers
In reply to Boqun FengQuoted 7 lines · Boqun Feng
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.
Originally-by Mark Rutland <[email protected]>Signed-off-by Boqun Feng <[email protected]>
FWIW, I'm happy with the concept; I have a couple of minor comments below.
Quoted 8 lines
--- rust/atomic_helpers.h | 1035 +++++++++++++++++++++ rust/helpers.c | 2 + scripts/atomic/gen-atomics.sh | 1 + scripts/atomic/gen-rust-atomic-helpers.sh | 64 ++ 4 files changed, 1102 insertions(+) create mode 100644 rust/atomic_helpers.h create mode 100755 scripts/atomic/gen-rust-atomic-helpers.sh
[...]
scripts/atomic/gen-rust-atomic-helpers.sh line 19
Quoted 12 lines
scripts/atomic/gen-rust-atomic-helpers.sh new file +12
-0+#gen_proto_order_variant(meta, pfx, name, sfx, order, atomic, int, raw, arg...) +gen_proto_order_variant() +{ + local meta="$1"; shift + local pfx="$1"; shift + local name="$1"; shift + local sfx="$1"; shift + local order="$1"; shift + local atomic="$1"; shift + local int="$1"; shift + local raw="$1"; shift + local attrs="${raw:+noinstr }"
You removed the 'raw_' atomic generation below, so you can drop the 'raw' parameter and the 'attrs' variable (both here and in the template)...
scripts/atomic/gen-rust-atomic-helpers.sh line 29
Quoted 9 lines
scripts/atomic/gen-rust-atomic-helpers.sh new file +9
-0+ local atomicname="${raw}${atomic}_${pfx}${name}${sfx}${order}" + + local ret="$(gen_ret_type "${meta}" "${int}")" + local params="$(gen_params "${int}" "${atomic}" "$@")" + local args="$(gen_args "$@")" + local retstmt="$(gen_ret_stmt "${meta}")" + +cat <<EOF +__rust_helper ${attrs}${ret}
... e.g. you can remove '${attrs}' here.
[...]
scripts/atomic/gen-rust-atomic-helpers.sh line 60
Quoted 7 lines
scripts/atomic/gen-rust-atomic-helpers.sh new file +7
-0+grep '^[a-z]' "$1" | while read name meta args; do + gen_proto "${meta}" "${name}" "atomic" "int" "" ${args} +done + +grep '^[a-z]' "$1" | while read name meta args; do + gen_proto "${meta}" "${name}" "atomic64" "s64" "" ${args} +done
With the 'raw' parameter removed above, the '""' argument can be dropped.
Any reason to not have the atomic_long_*() API? It seems like an odd ommision.
Mark.
Re: [RFC 1/2] rust: Introduce atomic API helpers
Quoted 10 lines · Mark Rutland
Quoted 7 lines · Boqun Feng
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.
Originally-by Mark Rutland <[email protected]>Signed-off-by Boqun Feng <[email protected]>FWIW, I'm happy with the concept; I have a couple of minor comments
;-)
Quoted 44 lines
below.
Quoted 8 lines
--- rust/atomic_helpers.h | 1035 +++++++++++++++++++++ rust/helpers.c | 2 + scripts/atomic/gen-atomics.sh | 1 + scripts/atomic/gen-rust-atomic-helpers.sh | 64 ++ 4 files changed, 1102 insertions(+) create mode 100644 rust/atomic_helpers.h create mode 100755 scripts/atomic/gen-rust-atomic-helpers.sh[...]
scripts/atomic/gen-rust-atomic-helpers.sh line 19
Quoted 12 lines
scripts/atomic/gen-rust-atomic-helpers.sh new file +12
-0+#gen_proto_order_variant(meta, pfx, name, sfx, order, atomic, int, raw, arg...) +gen_proto_order_variant() +{ + local meta="$1"; shift + local pfx="$1"; shift + local name="$1"; shift + local sfx="$1"; shift + local order="$1"; shift + local atomic="$1"; shift + local int="$1"; shift + local raw="$1"; shift + local attrs="${raw:+noinstr }"You removed the '
raw_' atomic generation below, so you can drop the 'raw' parameter and the 'attrs' variable (both here and in the template)...scripts/atomic/gen-rust-atomic-helpers.sh line 29
Quoted 9 lines
scripts/atomic/gen-rust-atomic-helpers.sh new file +9
-0+ local atomicname="${raw}${atomic}_${pfx}${name}${sfx}${order}" + + local ret="$(gen_ret_type "${meta}" "${int}")" + local params="$(gen_params "${int}" "${atomic}" "$@")" + local args="$(gen_args "$@")" + local retstmt="$(gen_ret_stmt "${meta}")" + +cat <<EOF +__rust_helper ${attrs}${ret}... e.g. you can remove '${attrs}' here.
[...]
Quoted 11 lines
scripts/atomic/gen-rust-atomic-helpers.sh line 60
Quoted 7 lines
scripts/atomic/gen-rust-atomic-helpers.sh new file +7
-0+grep '^[a-z]' "$1" | while read name meta args; do + gen_proto "${meta}" "${name}" "atomic" "int" "" ${args} +done + +grep '^[a-z]' "$1" | while read name meta args; do + gen_proto "${meta}" "${name}" "atomic64" "s64" "" ${args} +doneWith the 'raw' parameter removed above, the '""' argument can be dropped.
Fix all above locally.
Quoted 3 lines
Any reason to not have the atomic_long_*() API? It seems like an odd ommision.
See my reply to Peter, but there's also a more technical reason: right now, we use core::ffi::c_long for bindgen to translate C's long. But instead of isize (Rust's version of pointer-sized integer) core::ffi::c_long is i64 on 64bit and i32 on 32bit. So right now, atomic_long_add_return()'s helper signature would be (on 64bit):
extern "C" {
#[link_name="rust_helper_atomic_long_add_return"]
pub fn atomic_long_add_return(
i: i64,
v: *mut atomic_long_t,
) -> i64;
}and I would need to cast the types in order to put it in an AtomicIsize method:
impl AtomicIsize {
pub fn add_return(&self, i: isize) -> isize {
unsafe {
return atomic_long_add_return(i as i64, self.0.get()) as isize
}
}
}see these two ass. I want to avoid handling them in a bash script ;-)
A better solution would be what Gary has:
https://github.com/nbdd0121/linux/commit/b604a43db56f149a90084fa8aed7988a8066894b
, which defines kernel's own ffi types and teach bindgen to pick the right type for c_long. If we prefer script code generating to Rust macro code generating, I will work with Gary on getting that done first and then add atomic_long_t support unless we feel atomic_long_t support is urgent.
Regards,
Boqun
Quoted 1 line
Mark.
RFC 2/2 rust: sync: Add atomic support
In reply to Boqun FengProvide two atomic types: AtomicI32 and AtomicI64 with the existing implemenation of C atomics. These atomics have the same semantics of the corresponding LKMM C atomics, and using one memory (ordering) model certainly reduces the reasoning difficulty and potential bugs from the interaction of two different memory models.
Also bump my role to the maintainer of ATOMIC INFRASTRUCTURE to reflect my responsiblity on these Rust APIs.
Note that Atomic*::new()s are implemented vi open coding on struct atomic*_t. This allows new() being a const function, so that it can be used in constant contexts.
--- MAINTAINERS | 4 +- arch/arm64/kernel/cpufeature.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 | 1 + scripts/atomic/gen-rust-atomic.sh | 136 +++ 7 files changed, 1581 insertions(+), 1 deletion(-) 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.sh
MAINTAINERS +3 -1
@@ -3458,7 +3458,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
@@ -3467,6 +3467,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]>
arch/arm64/kernel/cpufeature.c +2 -0
@@ -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); }
rust/kernel/sync.rs +1 -0
@@ -8,6 +8,7 @@use crate ::types::Opaque ;mod arc; +pub mod atomic;mod condvar;pub mod lock;mod locked_by;
rust/kernel/sync/atomic.rs new file +63 -0
63 lines not on this page · raw message
rust/kernel/sync/atomic/impl.rs new file +1375 -0
1,375 lines not on this page · raw message
scripts/atomic/gen-atomics.sh +1 -0
@@ -12,6 +12,7 @@ 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/atomic_helpers.h +gen-rust-atomic.sh ../rust/kernel/sync/atomic/impl.rs EOF while read script header args; do /bin/sh ${ATOMICDIR}/${script} ${ATOMICTBL} ${args} > ${LINUXDIR}/include/${header}
scripts/atomic/gen-rust-atomic.sh new file +136 -0
136 lines not on this page · raw message
Signature · 1 line
2.45.2
Re: [RFC 2/2] rust: sync: Add atomic support
rust/kernel/sync/atomic/impl.rs line 4
Quoted 7 lines · Boqun Feng
rust/kernel/sync/atomic/impl.rs new file +4
-0@@ -0,0 +1,1375 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Generated by scripts/atomic/gen-rust-atomic.sh +//! DO NOT MODIFY THIS FILE DIRECTLY
Again, why not generate at build time?
thanks,
greg k-h
Gary Guo
maintainerRe: [RFC 2/2] rust: sync: Add atomic support
In reply to Boqun FengQuoted 89 lines · On Wed, 12 Jun 2024 15:30:25 -0700 Boqun Feng <[email protected]> wrote:
Provide two atomic types: AtomicI32 and AtomicI64 with the existing implemenation of C atomics. These atomics have the same semantics of the corresponding LKMM C atomics, and using one memory (ordering) model certainly reduces the reasoning difficulty and potential bugs from the interaction of two different memory models.
Also bump my role to the maintainer of ATOMIC INFRASTRUCTURE to reflect my responsiblity on these Rust APIs.
Note that
Atomic*::new()s are implemented vi open coding on struct atomic*_t. This allowsnew()being aconstfunction, so that it can be used in constant contexts.Signed-off-by Boqun Feng <[email protected]>--- MAINTAINERS | 4 +- arch/arm64/kernel/cpufeature.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 | 1 + scripts/atomic/gen-rust-atomic.sh | 136 +++ 7 files changed, 1581 insertions(+), 1 deletion(-) 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.shMAINTAINERS +3
-1@@ -3458,7 +3458,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@@ -3467,6 +3467,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]>arch/arm64/kernel/cpufeature.c +2
-0@@ -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); }rust/kernel/sync.rs +1
-0@@ -8,6 +8,7 @@use crate ::types::Opaque ;mod arc; +pub mod atomic;mod condvar;pub mod lock;mod locked_by;rust/kernel/sync/atomic.rs new file +9
-0@@ -0,0 +1,63 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Atomic primitives. +//! +//! These primitives have the same semantics as their C counterparts, for precise definitions of +//! the semantics, please refer to tools/memory-model. Note that Linux Kernel Memory (Consistency) +//! Model is the only model for Rust development in kernel right now, please avoid to use Rust's +//! own atomics. +
rust/kernel/sync/atomic.rs line 19
Quoted 10 lines
rust/kernel/sync/atomic.rs new file +10
-0+use crate ::bindings::{atomic64_t, atomic_t}; +use crate ::types::Opaque ; + +mod r#impl; + +/// Atomic 32bit signed integers. +pub struct AtomicI32 (Opaque <atomic_t >); + +/// Atomic 64bit signed integers. +pub struct AtomicI64 (Opaque <atomic64_t >);
Can we avoid two types and use a generic Atomic<T> and then implement on Atomic<i32> and Atomic<i64> instead? Like the recent generic NonZero in Rust standard library or the atomic crate
(https://docs.rs/atomic/).
I think this is better for ergonomics. The impl do need some extra casting though.
Best,
Gary
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 100 lines · Gary Guo
Quoted 97 lines · On Wed, 12 Jun 2024 15:30:25 -0700 Boqun Feng <[email protected]> wrote:
Provide two atomic types: AtomicI32 and AtomicI64 with the existing implemenation of C atomics. These atomics have the same semantics of the corresponding LKMM C atomics, and using one memory (ordering) model certainly reduces the reasoning difficulty and potential bugs from the interaction of two different memory models.
Also bump my role to the maintainer of ATOMIC INFRASTRUCTURE to reflect my responsiblity on these Rust APIs.
Note that
Atomic*::new()s are implemented vi open coding on struct atomic*_t. This allowsnew()being aconstfunction, so that it can be used in constant contexts.Signed-off-by Boqun Feng <[email protected]>--- MAINTAINERS | 4 +- arch/arm64/kernel/cpufeature.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 | 1 + scripts/atomic/gen-rust-atomic.sh | 136 +++ 7 files changed, 1581 insertions(+), 1 deletion(-) 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.shMAINTAINERS +3
-1@@ -3458,7 +3458,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@@ -3467,6 +3467,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]>arch/arm64/kernel/cpufeature.c +2
-0@@ -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); }rust/kernel/sync.rs +1
-0@@ -8,6 +8,7 @@use crate ::types::Opaque ;mod arc; +pub mod atomic;mod condvar;pub mod lock;mod locked_by;rust/kernel/sync/atomic.rs new file +17
-0@@ -0,0 +1,63 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Atomic primitives. +//! +//! These primitives have the same semantics as their C counterparts, for precise definitions of +//! the semantics, please refer to tools/memory-model. Note that Linux Kernel Memory (Consistency) +//! Model is the only model for Rust development in kernel right now, please avoid to use Rust's +//! own atomics. + +use crate ::bindings::{atomic64_t, atomic_t}; +use crate ::types::Opaque ; + +mod r#impl; + +/// Atomic 32bit signed integers. +pub struct AtomicI32 (Opaque <atomic_t >); +
Quoted 9 lines
rust/kernel/sync/atomic.rs line 19
Quoted 2 lines
rust/kernel/sync/atomic.rs new file +2
-0+/// Atomic 64bit signed integers. +pub struct AtomicI64 (Opaque <atomic64_t >);Can we avoid two types and use a generic
Atomic<T>and then implement onAtomic<i32>andAtomic<i64>instead? Like the recent generic NonZero in Rust standard library or the atomic crate
(https://docs.rs/atomic/).
We can always add a layer on top of what we have here to provide the generic Atomic<T>. However, I personally don't think generic Atomic<T> is a good idea, for a few reasons:
* I'm not sure it will bring benefits to users, the current atomic
users in kernel are pretty specific on the size of atomic they use, so they want to directly use AtomicI32 or AtomicI64 in their type definitions rather than use a Atomic<T> where their users can provide type later.
* I can also see the future where we have different APIs on
different types of atomics, for example, we could have a:
impl AtomicI64 {
pub fn split(&self) -> (&AtomicI32, &AtomicI32)
}
which doesn't exist for AtomicI32. Note this is not a UB because we write our atomic implementation in asm, so it's perfectly fine for mix-sized atomics.
So let's start with some basic and simple until we really have a need for generic Atomic<T>. Thoughts?
Regards,
Boqun
Quoted 5 lines
I think this is better for ergonomics. The impl do need some extra casting though.
Best,
Gary
Gary Guo
maintainerRe: [RFC 2/2] rust: sync: Add atomic support
Quoted 37 lines · On Thu, 13 Jun 2024 09:30:26 -0700 Boqun Feng <[email protected]> wrote:
Quoted 23 lines
Quoted 23 lines
rust/kernel/sync/atomic.rs new file +17
-0@@ -0,0 +1,63 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Atomic primitives. +//! +//! These primitives have the same semantics as their C counterparts, for precise definitions of +//! the semantics, please refer to tools/memory-model. Note that Linux Kernel Memory (Consistency) +//! Model is the only model for Rust development in kernel right now, please avoid to use Rust's +//! own atomics. + +use crate ::bindings::{atomic64_t, atomic_t}; +use crate ::types::Opaque ; + +mod r#impl; + +/// Atomic 32bit signed integers. +pub struct AtomicI32 (Opaque <atomic_t >); +Quoted 9 lines
rust/kernel/sync/atomic.rs line 19
Quoted 2 lines
rust/kernel/sync/atomic.rs new file +2
-0+/// Atomic 64bit signed integers. +pub struct AtomicI64 (Opaque <atomic64_t >);Can we avoid two types and use a generic
Atomic<T>and then implement onAtomic<i32>andAtomic<i64>instead? Like the recent generic NonZero in Rust standard library or the atomic crate
(https://docs.rs/atomic/).We can always add a layer on top of what we have here to provide the generic
Atomic<T>. However, I personally don't think genericAtomic<T>is a good idea, for a few reasons:
Quoted 5 lines
* I'm not sure it will bring benefits to users, the current atomic
users in kernel are pretty specific on the size of atomic they use, so they want to directly use AtomicI32 or AtomicI64 in their type definitions rather than use aAtomic<T>where their users can provide type later.
You can write Atomic<i32> and Atomic<i64>.
Quoted 11 lines
* I can also see the future where we have different APIs on
different types of atomics, for example, we could have a:impl AtomicI64 {
pub fnsplit(&self)-> (&AtomicI32, &AtomicI32)
}which doesn't exist for AtomicI32. Note this is not a UB because we write our atomic implementation in asm, so it's perfectly fine for mix-sized atomics.
You can still have
impl Atomic<i64> {
pub fn split(&self) -> (&Atomic<i32>, &Atomic<i32>)
}I see Atomic<i32>/Atomic<i64> being generally more flexible than AtomicI32/AtomicI64, without very little downsides. We may not have generic users but I think it doesn't do any harm to have it in a form that makes future generics easy.
Best,
Gary
Re: [RFC 2/2] rust: sync: Add atomic support
In reply to Boqun FengQuoted 3 lines · Boqun Feng
So let's start with some basic and simple until we really have a need for generic
Atomic<T>. Thoughts?
I don't want to delay this, but using generics would be more flexible, right? e.g. it could allow us to have atomics of, say, newtypes, if that were to be useful.
Is there a particular disadvantage of using the generics? The two cases you mentioned would just be written explicitly, right?
One disadvantage would be that they are different from the Rust standard library ones, e.g. in case we wanted third-party code to use them, but could be provided if needed later on.
Cheers,
Miguel
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 9 lines · Miguel Ojeda
Quoted 3 lines · Boqun Feng
So let's start with some basic and simple until we really have a need for generic
Atomic<T>. Thoughts?I don't want to delay this, but using generics would be more flexible, right? e.g. it could allow us to have atomics of, say, newtypes, if that were to be useful.
Quoted 7 lines
Is there a particular disadvantage of using the generics? The two cases you mentioned would just be written explicitly, right?
One disadvantage would be that they are different from the Rust standard library ones, e.g. in case we wanted third-party code to use them, but could be provided if needed later on.
Well, the other thing is AtomicI32 -> atomic_t and AtomicI64 -> atomic64_t are perfect mappings, and we can treat AtomicI32 and AtomicI64 as a separate layer that wires C atomics into Rust. As I said, we can build Atomic<T> on top of this layer, like:
Atomic<T>
|
V
AtomicI{32,64}
|
V
atomic{,64}_tand if we drop this layer, the dependencies become:
Atomic<i32,i64> <- Atomic<u32,u64>
|
V
atomic{,64}_ti.e. in the same layer of Atomic<T>, some of them directly depend on some other Atomic<T> types, which doesn't look very clean to me. And it might be difficult for architecture maintainers to track the exact dependency for Rust code.
This is also the reason why I didn't use Rust macros to generate AtomicI32 and AtomicI64 implementation: I use a script to generate .rs file. This ensures AtomicI32 and AtomicI64 stay with the exact same set of APIs as atomic{,64}_t (described by scripts/atomic/atomics.tbl. Put it in another way, I guess you can think AtomicI32 and AtomicI64 as some sort of intrinsic layer provided by C. And should we need it, we can build an Atomic<T> layer on top of it.
Does this make sense?
Regards,
Boqun
Quoted 2 lines
Cheers,
Miguel
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 2 lines · Boqun Feng
Does this make sense?
Implementation-wise, if you think it is simpler or more clear/elegant to have the extra lower level layer, then that sounds fine.
However, I was mainly talking about what we would eventually expose to users, i.e. do we want to provide Atomic<T> to begin with? If yes, then we could make the lower layer private already.
We can defer that extra layer/work if needed even if we go for Atomic<T>, but it would be nice to understand if we have consensus for an eventual user-facing API, or if someone has any other opinion or concerns on one vs. the other.
Cheers,
Miguel
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 9 lines · Miguel Ojeda
Quoted 2 lines · Boqun Feng
Does this make sense?
Implementation-wise, if you think it is simpler or more clear/elegant to have the extra lower level layer, then that sounds fine.
However, I was mainly talking about what we would eventually expose to users, i.e. do we want to provide
Atomic<T>to begin with? If yes,
The truth is I don't know ;-) I don't have much data on which one is better. Personally, I think AtomicI32 and AtomicI64 make the users have to think about size, alignment, etc, and I think that's important for atomic users and people who review their code, because before one uses atomics, one should ask themselves: why don't I use a lock? Atomics provide the ablities to do low level stuffs and when doing low level stuffs, you want to be more explicit than ergonomic.
That said, I keep an open mind on Atomic<T>, maybe it will show its value at last. But right now, I'm not convinced personally.
Quoted 7 lines
then we could make the lower layer private already.
We can defer that extra layer/work if needed even if we go for
Atomic<T>, but it would be nice to understand if we have consensus for an eventual user-facing API, or if someone has any other opinion or concerns on one vs. the other.
Yes, that'll be great. I'd love to see others' inputs!
Regards,
Boqun
Quoted 2 lines
Cheers,
Miguel
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 11 lines · Boqun Feng
Quoted 9 lines · Miguel Ojeda
Quoted 2 lines · Boqun Feng
Does this make sense?
Implementation-wise, if you think it is simpler or more clear/elegant to have the extra lower level layer, then that sounds fine.
However, I was mainly talking about what we would eventually expose to users, i.e. do we want to provide
Atomic<T>to begin with? If yes,
Quoted 7 lines
The truth is I don't know ;-) I don't have much data on which one is better. Personally, I think AtomicI32 and AtomicI64 make the users have to think about size, alignment, etc, and I think that's important for atomic users and people who review their code, because before one uses atomics, one should ask themselves: why don't I use a lock? Atomics provide the ablities to do low level stuffs and when doing low level stuffs, you want to be more explicit than ergonomic.
How would this be different with Atomic<i32> and Atomic<i64>? Just because the underlying Atomic<I> type is generic shouldn't change this, right?
---
Cheers,
Benno
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 20 lines · Benno Lossin
Quoted 11 lines · Boqun Feng
Quoted 9 lines · Miguel Ojeda
Quoted 2 lines · Boqun Feng
Does this make sense?
Implementation-wise, if you think it is simpler or more clear/elegant to have the extra lower level layer, then that sounds fine.
However, I was mainly talking about what we would eventually expose to users, i.e. do we want to provide
Atomic<T>to begin with? If yes,Quoted 7 lines
The truth is I don't know ;-) I don't have much data on which one is better. Personally, I think AtomicI32 and AtomicI64 make the users have to think about size, alignment, etc, and I think that's important for atomic users and people who review their code, because before one uses atomics, one should ask themselves: why don't I use a lock? Atomics provide the ablities to do low level stuffs and when doing low level stuffs, you want to be more explicit than ergonomic.
Quoted 1 line
How would this be different with
Atomic<i32>andAtomic<i64>? Just
The difference is that with Atomic{I32,I64} APIs, one has to choose (and think about) the size when using atomics, and cannot leave that option open. It's somewhere unconvenient, but as I said, atomics variables are different. For example, if someone is going to implement a reference counter struct, they can define as follow:
struct Refcount<T> {
refcount: AtomicI32,
data: UnsafeCell<T>
}but with atomic generic, people can leave that option open and do:
struct Refcount<R, T> {
refcount: Atomic<R>,
data: UnsafeCell<T>
}while it provides configurable options for experienced users, but it also provides opportunities for sub-optimal types, e.g. Refcount<u8, T>:
on ll/sc architectures, because data and refcount can be in the same machine-word, the accesses of refcount are affected by the accesses of data.
The point I'm trying to make here is: when you are using atomics, you care about performance a lot (otherwise, why don't you use a lock?), and because of that, you should care about the size of the atomics, because it may affect the performance significantly.
Regards,
Boqun
Quoted 7 lines
because the underlying
Atomic<I>type is generic shouldn't change this, right?---
Cheers,
Benno
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 41 lines · Boqun Feng
Quoted 20 lines · Benno Lossin
Quoted 11 lines · Boqun Feng
Quoted 9 lines · Miguel Ojeda
Quoted 2 lines · Boqun Feng
Does this make sense?
Implementation-wise, if you think it is simpler or more clear/elegant to have the extra lower level layer, then that sounds fine.
However, I was mainly talking about what we would eventually expose to users, i.e. do we want to provide
Atomic<T>to begin with? If yes,Quoted 7 lines
The truth is I don't know ;-) I don't have much data on which one is better. Personally, I think AtomicI32 and AtomicI64 make the users have to think about size, alignment, etc, and I think that's important for atomic users and people who review their code, because before one uses atomics, one should ask themselves: why don't I use a lock? Atomics provide the ablities to do low level stuffs and when doing low level stuffs, you want to be more explicit than ergonomic.
Quoted 1 line
How would this be different with
Atomic<i32>andAtomic<i64>? JustThe difference is that with Atomic{I32,I64} APIs, one has to choose (and think about) the size when using atomics, and cannot leave that option open. It's somewhere unconvenient, but as I said, atomics variables are different. For example, if someone is going to implement a reference counter struct, they can define as follow:
struct Refcount<T> { refcount: AtomicI32, data: UnsafeCell<T> }but with atomic generic, people can leave that option open and do:
struct Refcount<R, T> { refcount: Atomic<R>, data: UnsafeCell<T> }
Quoted 5 lines
while it provides configurable options for experienced users, but it also provides opportunities for sub-optimal types, e.g. Refcount<u8, T>:
on ll/sc architectures, becausedataandrefcountcan be in the same machine-word, the accesses ofrefcountare affected by the accesses ofdata.
I think this is a non-issue. We have two options of counteracting this:
1. We can just point this out in reviews and force people to use Atomic<T> with a concrete type. In cases where there really is the need to be generic, we can have it.
2. We can add a private trait in the bounds for the generic, nobody outside of the module can access it and thus they need to use a concrete type:
// needs a better name
trait Integer {}
impl Integer for i32 {}
impl Integer for i64 {}
pub struct Atomic<T: Integer> {
/* ... */
}And then in the other module, you can't do this (with compiler error):
pub struct Refcount<R: Integer, T> {
// ^^^^^^^ not found in this scope
// note: trait `crate::atomic::Integer` exists but is inaccessible
refcount: Atomic<R>,
data: UnsafeCell<T>,
}I think that we can start with approach 2 and if we find a use-case where generics are really unavoidable, we can either put it in the same module as Atomic<T>, or change the access of Integer.
---
Cheers,
Benno
Quoted 4 lines
The point I'm trying to make here is: when you are using atomics, you care about performance a lot (otherwise, why don't you use a lock?), and because of that, you should care about the size of the atomics, because it may affect the performance significantly.
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 74 lines · Benno Lossin
Quoted 41 lines · Boqun Feng
Quoted 20 lines · Benno Lossin
Quoted 11 lines · Boqun Feng
Quoted 9 lines · Miguel Ojeda
Quoted 2 lines · Boqun Feng
Does this make sense?
Implementation-wise, if you think it is simpler or more clear/elegant to have the extra lower level layer, then that sounds fine.
However, I was mainly talking about what we would eventually expose to users, i.e. do we want to provide
Atomic<T>to begin with? If yes,Quoted 7 lines
The truth is I don't know ;-) I don't have much data on which one is better. Personally, I think AtomicI32 and AtomicI64 make the users have to think about size, alignment, etc, and I think that's important for atomic users and people who review their code, because before one uses atomics, one should ask themselves: why don't I use a lock? Atomics provide the ablities to do low level stuffs and when doing low level stuffs, you want to be more explicit than ergonomic.
Quoted 1 line
How would this be different with
Atomic<i32>andAtomic<i64>? JustThe difference is that with Atomic{I32,I64} APIs, one has to choose (and think about) the size when using atomics, and cannot leave that option open. It's somewhere unconvenient, but as I said, atomics variables are different. For example, if someone is going to implement a reference counter struct, they can define as follow:
struct Refcount<T> { refcount: AtomicI32, data: UnsafeCell<T> }but with atomic generic, people can leave that option open and do:
struct Refcount<R, T> { refcount: Atomic<R>, data: UnsafeCell<T> }Quoted 5 lines
while it provides configurable options for experienced users, but it also provides opportunities for sub-optimal types, e.g. Refcount<u8, T>:
on ll/sc architectures, becausedataandrefcountcan be in the same machine-word, the accesses ofrefcountare affected by the accesses ofdata.I think this is a non-issue. We have two options of counteracting this:
1. We can just point this out in reviews and force people to useAtomic<T>with a concrete type. In cases where there really is the need to be generic, we can have it.
2. We can add a private trait in the bounds for the generic, nobody outside of the module can access it and thus they need to use a concrete type:// needs a better name trait Integer {} impl Integer for i32 {} impl Integer for i64 {} pub struct Atomic<T: Integer> { /* ... */ }And then in the other module, you can't do this (with compiler error):
pub struct Refcount<R: Integer, T> { // ^^^^^^^ not found in this scope // note: trait `crate::atomic::Integer` exists but is inaccessible refcount: Atomic<R>, data: UnsafeCell<T>, }
Quoted 4 lines
I think that we can start with approach 2 and if we find a use-case where generics are really unavoidable, we can either put it in the same module as
Atomic<T>, or change the access ofInteger.
What's the issue of having AtomicI32 and AtomicI64 first then? We don't need to do 1 or 2 until the real users show up.
And I'd like also to point out that there are a few more trait bound designs needed for Atomic<T>, for example, Atomic<u32> and Atomic<i32> have different sets of API (no inc_unless_negative() for u32).
Don't make me wrong, I have no doubt we can handle this in the type system, but given the design work need, won't it make sense that we take baby steps on this? We can first introduce AtomicI32 and AtomicI64 which already have real users, and then if there are some values of generic atomics, we introduce them and have proper discussion on design.
To me, it's perfectly fine that Atomic{I32,I64} co-exist with Atomic<T>. What's the downside? A bit specific example would help me understand the real concern here.
Regards,
Boqun
Quoted 9 lines
---
Cheers,
BennoQuoted 4 lines
The point I'm trying to make here is: when you are using atomics, you care about performance a lot (otherwise, why don't you use a lock?), and because of that, you should care about the size of the atomics, because it may affect the performance significantly.
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 75 lines · Boqun Feng
Quoted 74 lines · Benno Lossin
Quoted 41 lines · Boqun Feng
Quoted 20 lines · Benno Lossin
Quoted 11 lines · Boqun Feng
Quoted 9 lines · Miguel Ojeda
Quoted 2 lines · Boqun Feng
Does this make sense?
Implementation-wise, if you think it is simpler or more clear/elegant to have the extra lower level layer, then that sounds fine.
However, I was mainly talking about what we would eventually expose to users, i.e. do we want to provide
Atomic<T>to begin with? If yes,Quoted 7 lines
The truth is I don't know ;-) I don't have much data on which one is better. Personally, I think AtomicI32 and AtomicI64 make the users have to think about size, alignment, etc, and I think that's important for atomic users and people who review their code, because before one uses atomics, one should ask themselves: why don't I use a lock? Atomics provide the ablities to do low level stuffs and when doing low level stuffs, you want to be more explicit than ergonomic.
Quoted 1 line
How would this be different with
Atomic<i32>andAtomic<i64>? JustThe difference is that with Atomic{I32,I64} APIs, one has to choose (and think about) the size when using atomics, and cannot leave that option open. It's somewhere unconvenient, but as I said, atomics variables are different. For example, if someone is going to implement a reference counter struct, they can define as follow:
struct Refcount<T> { refcount: AtomicI32, data: UnsafeCell<T> }but with atomic generic, people can leave that option open and do:
struct Refcount<R, T> { refcount: Atomic<R>, data: UnsafeCell<T> }Quoted 5 lines
while it provides configurable options for experienced users, but it also provides opportunities for sub-optimal types, e.g. Refcount<u8, T>:
on ll/sc architectures, becausedataandrefcountcan be in the same machine-word, the accesses ofrefcountare affected by the accesses ofdata.I think this is a non-issue. We have two options of counteracting this:
1. We can just point this out in reviews and force people to useAtomic<T>with a concrete type. In cases where there really is the need to be generic, we can have it.
2. We can add a private trait in the bounds for the generic, nobody outside of the module can access it and thus they need to use a concrete type:// needs a better name trait Integer {} impl Integer for i32 {} impl Integer for i64 {} pub struct Atomic<T: Integer> { /* ... */ }And then in the other module, you can't do this (with compiler error):
pub struct Refcount<R: Integer, T> { // ^^^^^^^ not found in this scope // note: trait `crate::atomic::Integer` exists but is inaccessible refcount: Atomic<R>, data: UnsafeCell<T>, }
Quoted 7 lines
Quoted 4 lines
I think that we can start with approach 2 and if we find a use-case where generics are really unavoidable, we can either put it in the same module as
Atomic<T>, or change the access ofInteger.What's the issue of having AtomicI32 and AtomicI64 first then? We don't need to do 1 or 2 until the real users show up.
Generics allow you to avoid code duplication (I don't think that you want to create the Atomic{I32,I64} types via macros...). We would have to do a lot of refactoring, when we want to introduce it. I don't see the harm of introducing generics from the get-go.
Quoted 3 lines
And I'd like also to point out that there are a few more trait bound designs needed for Atomic<T>, for example, Atomic<u32> and Atomic<i32> have different sets of API (no
inc_unless_negative()for u32).
Sure, just like Gary said, you can just do:
impl Atomic<i32> {
pub fn inc_unless_negative(&self, ordering: Ordering) -> bool;
}Or add a HasNegative trait.
Quoted 5 lines
Don't make me wrong, I have no doubt we can handle this in the type system, but given the design work need, won't it make sense that we take baby steps on this? We can first introduce AtomicI32 and AtomicI64 which already have real users, and then if there are some values of generic atomics, we introduce them and have proper discussion on design.
I don't understand this point, why can't we put in the effort for a good design? AFAIK we normally spend considerable time to get the API right and I think in this case it would include making it generic.
Quoted 3 lines
To me, it's perfectly fine that Atomic{I32,I64} co-exist with Atomic<T>. What's the downside? A bit specific example would help me understand the real concern here.
I don't like that, why have two ways of doing the same thing? People will be confused whether they should use AtomicI32 vs Atomic<i32>...
---
Cheers,
Benno
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 81 lines · Benno Lossin
Quoted 80 lines · Boqun Feng
Quoted 78 lines · Benno Lossin
Quoted 41 lines · Boqun Feng
Quoted 20 lines · Benno Lossin
Quoted 11 lines · Boqun Feng
Quoted 9 lines · Miguel Ojeda
Quoted 2 lines · Boqun Feng
Does this make sense?
Implementation-wise, if you think it is simpler or more clear/elegant to have the extra lower level layer, then that sounds fine.
However, I was mainly talking about what we would eventually expose to users, i.e. do we want to provide
Atomic<T>to begin with? If yes,Quoted 7 lines
The truth is I don't know ;-) I don't have much data on which one is better. Personally, I think AtomicI32 and AtomicI64 make the users have to think about size, alignment, etc, and I think that's important for atomic users and people who review their code, because before one uses atomics, one should ask themselves: why don't I use a lock? Atomics provide the ablities to do low level stuffs and when doing low level stuffs, you want to be more explicit than ergonomic.
Quoted 1 line
How would this be different with
Atomic<i32>andAtomic<i64>? JustThe difference is that with Atomic{I32,I64} APIs, one has to choose (and think about) the size when using atomics, and cannot leave that option open. It's somewhere unconvenient, but as I said, atomics variables are different. For example, if someone is going to implement a reference counter struct, they can define as follow:
struct Refcount<T> { refcount: AtomicI32, data: UnsafeCell<T> }but with atomic generic, people can leave that option open and do:
struct Refcount<R, T> { refcount: Atomic<R>, data: UnsafeCell<T> }Quoted 5 lines
while it provides configurable options for experienced users, but it also provides opportunities for sub-optimal types, e.g. Refcount<u8, T>:
on ll/sc architectures, becausedataandrefcountcan be in the same machine-word, the accesses ofrefcountare affected by the accesses ofdata.I think this is a non-issue. We have two options of counteracting this:
1. We can just point this out in reviews and force people to useAtomic<T>with a concrete type. In cases where there really is the need to be generic, we can have it.
2. We can add a private trait in the bounds for the generic, nobody outside of the module can access it and thus they need to use a concrete type:// needs a better name trait Integer {} impl Integer for i32 {} impl Integer for i64 {} pub struct Atomic<T: Integer> { /* ... */ }And then in the other module, you can't do this (with compiler error):
pub struct Refcount<R: Integer, T> { // ^^^^^^^ not found in this scope // note: trait `crate::atomic::Integer` exists but is inaccessible refcount: Atomic<R>, data: UnsafeCell<T>, }I think that we can start with approach 2 and if we find a use-case where generics are really unavoidable, we can either put it in the same module as
Atomic<T>, or change the access ofInteger.
Quoted 6 lines
Quoted 2 lines
What's the issue of having AtomicI32 and AtomicI64 first then? We don't need to do 1 or 2 until the real users show up.
Generics allow you to avoid code duplication (I don't think that you want to create the
Atomic{I32,I64}types via macros...). We would have to do a lot of refactoring, when we want to introduce it. I don't see
You can simply do
type AtomicI32=Atomic<i32>;
Plus, we always do refactoring in kernel, because it's impossible to get everything right at the first time. TBH, it's too confident to think one can.
Quoted 20 lines
the harm of introducing generics from the get-go.
Quoted 3 lines
And I'd like also to point out that there are a few more trait bound designs needed for Atomic<T>, for example, Atomic<u32> and Atomic<i32> have different sets of API (no
inc_unless_negative()for u32).Sure, just like Gary said, you can just do:
impl Atomic<i32> { pub fn inc_unless_negative(&self, ordering: Ordering) -> bool; }Or add a
HasNegativetrait.Quoted 5 lines
Don't make me wrong, I have no doubt we can handle this in the type system, but given the design work need, won't it make sense that we take baby steps on this? We can first introduce AtomicI32 and AtomicI64 which already have real users, and then if there are some values of generic atomics, we introduce them and have proper discussion on design.
Quoted 4 lines
I don't understand this point, why can't we put in the effort for a good design? AFAIK we normally spend considerable time to get the API right and I think in this case it would include making it generic.
What's the design you propose here? Well, the conversation between us is only the design bit I saw, elsewhere it's all handwaving that "generics are overall really good". I'm happy to get the API right, and it's easy and simple to do on concrete types. But IIUC, Gary's suggestion is to only have Atomic<i32> and Atomic<i64> first, and do the design later, which I really don't like. It may not be a complete design, but I need to see the design now to understand whether we need to go to that direction. I cannot just introduce a TBD generic.
Regards,
Boqun
Quoted 11 lines
Quoted 3 lines
To me, it's perfectly fine that Atomic{I32,I64} co-exist with Atomic<T>. What's the downside? A bit specific example would help me understand the real concern here.
I don't like that, why have two ways of doing the same thing? People will be confused whether they should use
AtomicI32vsAtomic<i32>...---
Cheers,
Benno
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 85 lines · Boqun Feng
Quoted 84 lines · Benno Lossin
Quoted 82 lines · Boqun Feng
Quoted 74 lines · Benno Lossin
Quoted 41 lines · Boqun Feng
Quoted 20 lines · Benno Lossin
Quoted 11 lines · Boqun Feng
Quoted 9 lines · Miguel Ojeda
Quoted 2 lines · Boqun Feng
Does this make sense?
Implementation-wise, if you think it is simpler or more clear/elegant to have the extra lower level layer, then that sounds fine.
However, I was mainly talking about what we would eventually expose to users, i.e. do we want to provide
Atomic<T>to begin with? If yes,Quoted 7 lines
The truth is I don't know ;-) I don't have much data on which one is better. Personally, I think AtomicI32 and AtomicI64 make the users have to think about size, alignment, etc, and I think that's important for atomic users and people who review their code, because before one uses atomics, one should ask themselves: why don't I use a lock? Atomics provide the ablities to do low level stuffs and when doing low level stuffs, you want to be more explicit than ergonomic.
Quoted 1 line
How would this be different with
Atomic<i32>andAtomic<i64>? JustThe difference is that with Atomic{I32,I64} APIs, one has to choose (and think about) the size when using atomics, and cannot leave that option open. It's somewhere unconvenient, but as I said, atomics variables are different. For example, if someone is going to implement a reference counter struct, they can define as follow:
struct Refcount<T> { refcount: AtomicI32, data: UnsafeCell<T> }but with atomic generic, people can leave that option open and do:
struct Refcount<R, T> { refcount: Atomic<R>, data: UnsafeCell<T> }Quoted 5 lines
while it provides configurable options for experienced users, but it also provides opportunities for sub-optimal types, e.g. Refcount<u8, T>:
on ll/sc architectures, becausedataandrefcountcan be in the same machine-word, the accesses ofrefcountare affected by the accesses ofdata.I think this is a non-issue. We have two options of counteracting this:
1. We can just point this out in reviews and force people to useAtomic<T>with a concrete type. In cases where there really is the need to be generic, we can have it.
2. We can add a private trait in the bounds for the generic, nobody outside of the module can access it and thus they need to use a concrete type:// needs a better name trait Integer {} impl Integer for i32 {} impl Integer for i64 {} pub struct Atomic<T: Integer> { /* ... */ }And then in the other module, you can't do this (with compiler error):
pub struct Refcount<R: Integer, T> { // ^^^^^^^ not found in this scope // note: trait `crate::atomic::Integer` exists but is inaccessible refcount: Atomic<R>, data: UnsafeCell<T>, }Quoted 4 lines
I think that we can start with approach 2 and if we find a use-case where generics are really unavoidable, we can either put it in the same module as
Atomic<T>, or change the access ofInteger.What's the issue of having AtomicI32 and AtomicI64 first then? We don't need to do 1 or 2 until the real users show up.
Quoted 7 lines
Quoted 3 lines
Generics allow you to avoid code duplication (I don't think that you want to create the
Atomic{I32,I64}types via macros...). We would have to do a lot of refactoring, when we want to introduce it. I don't seeYou can simply do
type AtomicI32=Atomic<i32>;
Eh, I would think that we could just do a text replacement in this case. Or if that doesn't work, Coccinelle should be able to do this...
Quoted 3 lines
Plus, we always do refactoring in kernel, because it's impossible to get everything right at the first time. TBH, it's too confident to think one can.
I don't think that we're at the "let's just put it in" stage. This is an RFC version, so it should be fine to completely change the approach. I agree, that we can't get it 100% right the first time, but we should at least strive to get a good version.
Quoted 25 lines
Quoted 20 lines
the harm of introducing generics from the get-go.
Quoted 3 lines
And I'd like also to point out that there are a few more trait bound designs needed for Atomic<T>, for example, Atomic<u32> and Atomic<i32> have different sets of API (no
inc_unless_negative()for u32).Sure, just like Gary said, you can just do:
impl Atomic<i32> { pub fn inc_unless_negative(&self, ordering: Ordering) -> bool; }Or add a
HasNegativetrait.Quoted 5 lines
Don't make me wrong, I have no doubt we can handle this in the type system, but given the design work need, won't it make sense that we take baby steps on this? We can first introduce AtomicI32 and AtomicI64 which already have real users, and then if there are some values of generic atomics, we introduce them and have proper discussion on design.
Quoted 4 lines
I don't understand this point, why can't we put in the effort for a good design? AFAIK we normally spend considerable time to get the API right and I think in this case it would include making it generic.
Quoted 8 lines
What's the design you propose here? Well, the conversation between us is only the design bit I saw, elsewhere it's all handwaving that "generics are overall really good". I'm happy to get the API right, and it's easy and simple to do on concrete types. But IIUC, Gary's suggestion is to only have Atomic<i32> and Atomic<i64> first, and do the design later, which I really don't like. It may not be a complete design, but I need to see the design now to understand whether we need to go to that direction. I cannot just introduce a TBD generic.
I don't think that the idea was to "do the design later". I don't even know how you would do that, since you need the design to submit a patch.
I can't offer you a complete API description, since that would require me writing it up myself. But I would recommend trying to get it to work with generics. I got a few other comments:
- I don't think that we should resort to a script to generate the Rust code since it prevents adding good documentation & examples to the various methods. AFAIU you want to generate the functions from scripts/atomic/atomics.tbl to keep it in sync with the C side. I looked at the git log of that file and it hasn't been changed significantly since its inception. I don't think that there is any benefit to generating the functions from that file.
- most of the documented functions say "See c_function", I don't like this, can we either copy the C documentation (I imagine it not changing that often, or is that assumption wrong?) or write our own?
- we should try to use either const generic or normal parameters for the access ordering instead of putting it in the function name.
- why do we need both non-return and return variants?
I think it is probably a good idea to discuss this in our meeting.
---
Cheers,
Benno
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 97 lines · Benno Lossin
Quoted 85 lines · Boqun Feng
Quoted 84 lines · Benno Lossin
Quoted 82 lines · Boqun Feng
Quoted 74 lines · Benno Lossin
Quoted 41 lines · Boqun Feng
Quoted 20 lines · Benno Lossin
Quoted 11 lines · Boqun Feng
Quoted 9 lines · Miguel Ojeda
Quoted 2 lines · Boqun Feng
Does this make sense?
Implementation-wise, if you think it is simpler or more clear/elegant to have the extra lower level layer, then that sounds fine.
However, I was mainly talking about what we would eventually expose to users, i.e. do we want to provide
Atomic<T>to begin with? If yes,Quoted 7 lines
The truth is I don't know ;-) I don't have much data on which one is better. Personally, I think AtomicI32 and AtomicI64 make the users have to think about size, alignment, etc, and I think that's important for atomic users and people who review their code, because before one uses atomics, one should ask themselves: why don't I use a lock? Atomics provide the ablities to do low level stuffs and when doing low level stuffs, you want to be more explicit than ergonomic.
Quoted 1 line
How would this be different with
Atomic<i32>andAtomic<i64>? JustThe difference is that with Atomic{I32,I64} APIs, one has to choose (and think about) the size when using atomics, and cannot leave that option open. It's somewhere unconvenient, but as I said, atomics variables are different. For example, if someone is going to implement a reference counter struct, they can define as follow:
struct Refcount<T> { refcount: AtomicI32, data: UnsafeCell<T> }but with atomic generic, people can leave that option open and do:
struct Refcount<R, T> { refcount: Atomic<R>, data: UnsafeCell<T> }Quoted 5 lines
while it provides configurable options for experienced users, but it also provides opportunities for sub-optimal types, e.g. Refcount<u8, T>:
on ll/sc architectures, becausedataandrefcountcan be in the same machine-word, the accesses ofrefcountare affected by the accesses ofdata.I think this is a non-issue. We have two options of counteracting this:
1. We can just point this out in reviews and force people to useAtomic<T>with a concrete type. In cases where there really is the need to be generic, we can have it.
2. We can add a private trait in the bounds for the generic, nobody outside of the module can access it and thus they need to use a concrete type:// needs a better name trait Integer {} impl Integer for i32 {} impl Integer for i64 {} pub struct Atomic<T: Integer> { /* ... */ }And then in the other module, you can't do this (with compiler error):
pub struct Refcount<R: Integer, T> { // ^^^^^^^ not found in this scope // note: trait `crate::atomic::Integer` exists but is inaccessible refcount: Atomic<R>, data: UnsafeCell<T>, }Quoted 4 lines
I think that we can start with approach 2 and if we find a use-case where generics are really unavoidable, we can either put it in the same module as
Atomic<T>, or change the access ofInteger.What's the issue of having AtomicI32 and AtomicI64 first then? We don't need to do 1 or 2 until the real users show up.
Quoted 7 lines
Quoted 3 lines
Generics allow you to avoid code duplication (I don't think that you want to create the
Atomic{I32,I64}types via macros...). We would have to do a lot of refactoring, when we want to introduce it. I don't seeYou can simply do
type AtomicI32=Atomic<i32>;Eh, I would think that we could just do a text replacement in this case. Or if that doesn't work, Coccinelle should be able to do this...
Quoted 6 lines
Quoted 3 lines
Plus, we always do refactoring in kernel, because it's impossible to get everything right at the first time. TBH, it's too confident to think one can.
I don't think that we're at the "let's just put it in" stage. This is an RFC version, so it should be fine to completely change the approach.
I'm fine as well. I wasn't trying to rush anything, but as I mentioned below, I need some more design from people who want it to understand whether that's a good idea.
Quoted 37 lines
I agree, that we can't get it 100% right the first time, but we should at least strive to get a good version.
Quoted 25 lines
Quoted 20 lines
the harm of introducing generics from the get-go.
Quoted 3 lines
And I'd like also to point out that there are a few more trait bound designs needed for Atomic<T>, for example, Atomic<u32> and Atomic<i32> have different sets of API (no
inc_unless_negative()for u32).Sure, just like Gary said, you can just do:
impl Atomic<i32> { pub fn inc_unless_negative(&self, ordering: Ordering) -> bool; }Or add a
HasNegativetrait.Quoted 5 lines
Don't make me wrong, I have no doubt we can handle this in the type system, but given the design work need, won't it make sense that we take baby steps on this? We can first introduce AtomicI32 and AtomicI64 which already have real users, and then if there are some values of generic atomics, we introduce them and have proper discussion on design.
Quoted 4 lines
I don't understand this point, why can't we put in the effort for a good design? AFAIK we normally spend considerable time to get the API right and I think in this case it would include making it generic.
Quoted 8 lines
What's the design you propose here? Well, the conversation between us is only the design bit I saw, elsewhere it's all handwaving that "generics are overall really good". I'm happy to get the API right, and it's easy and simple to do on concrete types. But IIUC, Gary's suggestion is to only have Atomic<i32> and Atomic<i64> first, and do the design later, which I really don't like. It may not be a complete design, but I need to see the design now to understand whether we need to go to that direction. I cannot just introduce a TBD generic.
Quoted 3 lines
I don't think that the idea was to "do the design later". I don't even know how you would do that, since you need the design to submit a patch.
Then I might mis-understand Gary? He said:
"Can we avoid two types and use a generic Atomic<T> and then implement on Atomic<i32> and Atomic<i64> instead?"
, which means just replace impl AtomicI32 with impl Atomic<i32> to me.
Quoted 3 lines
I can't offer you a complete API description, since that would require me writing it up myself. But I would recommend trying to get it to work with generics. I got a few other comments:
We should work on things that are concrete, right? It's fine that the design is not complete, and it's fine if you just recommend. But without a somewhat concrete design (doesn't have to be complete), I cannot be sure about whether we have the same vision of the future of generic atomics (see my question to Gary), that's a bit hard for me to try to work something out (plus I personally don't think it's a good idea, it's OK to me, but not good). Anyway, I wasn't trying to refuse to do this just based on personal reasons, but I do need to understand what you are all proposing, because I don't have one myself.
Quoted 7 lines
- I don't think that we should resort to a script to generate the Rust code since it prevents adding good documentation & examples to the various methods. AFAIU you want to generate the functions from
scripts/atomic/atomics.tblto keep it in sync with the C side. I looked at the git log of that file and it hasn't been changed significantly since its inception. I don't think that there is any benefit to generating the functions from that file.
I'll leave this to other atomic maintainers.
Quoted 3 lines
- most of the documented functions say "See
c_function", I don't like this, can we either copy the C documentation (I imagine it not changing that often, or is that assumption wrong?) or write our own?
You're not wrong. AN in C side, we do have some documentation template to generate the comments (see scripts/atomic/kerneldoc). But first the format is for doxygen(I guess?), and second as you just bring up, the templates are tied with the bash script.
Quoted 2 lines
- we should try to use either const generic or normal parameters for the access ordering instead of putting it in the function name.
Why is it important? Keeping it in the current way brings the value that it's not too much different than their C counterparts. Could you explain a bit the pros and cons on suffix vs const generic approach?
Quoted 2 lines
- why do we need both non-return and return variants?
Historical reason I guess. Plus maybe some architectures have a better implementation on non-return atomics IIRC.
Regards,
Boqun
Quoted 6 lines
I think it is probably a good idea to discuss this in our meeting.
---
Cheers,
Benno
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 150 lines · Boqun Feng
Quoted 97 lines · Benno Lossin
Quoted 85 lines · Boqun Feng
Quoted 84 lines · Benno Lossin
Quoted 82 lines · Boqun Feng
Quoted 74 lines · Benno Lossin
Quoted 41 lines · Boqun Feng
Quoted 20 lines · Benno Lossin
Quoted 11 lines · Boqun Feng
Quoted 9 lines · Miguel Ojeda
Quoted 2 lines · Boqun Feng
Does this make sense?
Implementation-wise, if you think it is simpler or more clear/elegant to have the extra lower level layer, then that sounds fine.
However, I was mainly talking about what we would eventually expose to users, i.e. do we want to provide
Atomic<T>to begin with? If yes,Quoted 7 lines
The truth is I don't know ;-) I don't have much data on which one is better. Personally, I think AtomicI32 and AtomicI64 make the users have to think about size, alignment, etc, and I think that's important for atomic users and people who review their code, because before one uses atomics, one should ask themselves: why don't I use a lock? Atomics provide the ablities to do low level stuffs and when doing low level stuffs, you want to be more explicit than ergonomic.
Quoted 1 line
How would this be different with
Atomic<i32>andAtomic<i64>? JustThe difference is that with Atomic{I32,I64} APIs, one has to choose (and think about) the size when using atomics, and cannot leave that option open. It's somewhere unconvenient, but as I said, atomics variables are different. For example, if someone is going to implement a reference counter struct, they can define as follow:
struct Refcount<T> { refcount: AtomicI32, data: UnsafeCell<T> }but with atomic generic, people can leave that option open and do:
struct Refcount<R, T> { refcount: Atomic<R>, data: UnsafeCell<T> }Quoted 5 lines
while it provides configurable options for experienced users, but it also provides opportunities for sub-optimal types, e.g. Refcount<u8, T>:
on ll/sc architectures, becausedataandrefcountcan be in the same machine-word, the accesses ofrefcountare affected by the accesses ofdata.I think this is a non-issue. We have two options of counteracting this:
1. We can just point this out in reviews and force people to useAtomic<T>with a concrete type. In cases where there really is the need to be generic, we can have it.
2. We can add a private trait in the bounds for the generic, nobody outside of the module can access it and thus they need to use a concrete type:// needs a better name trait Integer {} impl Integer for i32 {} impl Integer for i64 {} pub struct Atomic<T: Integer> { /* ... */ }And then in the other module, you can't do this (with compiler error):
pub struct Refcount<R: Integer, T> { // ^^^^^^^ not found in this scope // note: trait `crate::atomic::Integer` exists but is inaccessible refcount: Atomic<R>, data: UnsafeCell<T>, }Quoted 4 lines
I think that we can start with approach 2 and if we find a use-case where generics are really unavoidable, we can either put it in the same module as
Atomic<T>, or change the access ofInteger.What's the issue of having AtomicI32 and AtomicI64 first then? We don't need to do 1 or 2 until the real users show up.
Quoted 7 lines
Quoted 3 lines
Generics allow you to avoid code duplication (I don't think that you want to create the
Atomic{I32,I64}types via macros...). We would have to do a lot of refactoring, when we want to introduce it. I don't seeYou can simply do
type AtomicI32=Atomic<i32>;Eh, I would think that we could just do a text replacement in this case. Or if that doesn't work, Coccinelle should be able to do this...
Quoted 6 lines
Quoted 3 lines
Plus, we always do refactoring in kernel, because it's impossible to get everything right at the first time. TBH, it's too confident to think one can.
I don't think that we're at the "let's just put it in" stage. This is an RFC version, so it should be fine to completely change the approach.
I'm fine as well. I wasn't trying to rush anything, but as I mentioned below, I need some more design from people who want it to understand whether that's a good idea.
Quoted 37 lines
I agree, that we can't get it 100% right the first time, but we should at least strive to get a good version.
Quoted 25 lines
Quoted 20 lines
the harm of introducing generics from the get-go.
Quoted 3 lines
And I'd like also to point out that there are a few more trait bound designs needed for Atomic<T>, for example, Atomic<u32> and Atomic<i32> have different sets of API (no
inc_unless_negative()for u32).Sure, just like Gary said, you can just do:
impl Atomic<i32> { pub fn inc_unless_negative(&self, ordering: Ordering) -> bool; }Or add a
HasNegativetrait.Quoted 5 lines
Don't make me wrong, I have no doubt we can handle this in the type system, but given the design work need, won't it make sense that we take baby steps on this? We can first introduce AtomicI32 and AtomicI64 which already have real users, and then if there are some values of generic atomics, we introduce them and have proper discussion on design.
Quoted 4 lines
I don't understand this point, why can't we put in the effort for a good design? AFAIK we normally spend considerable time to get the API right and I think in this case it would include making it generic.
Quoted 8 lines
What's the design you propose here? Well, the conversation between us is only the design bit I saw, elsewhere it's all handwaving that "generics are overall really good". I'm happy to get the API right, and it's easy and simple to do on concrete types. But IIUC, Gary's suggestion is to only have Atomic<i32> and Atomic<i64> first, and do the design later, which I really don't like. It may not be a complete design, but I need to see the design now to understand whether we need to go to that direction. I cannot just introduce a TBD generic.
Quoted 3 lines
I don't think that the idea was to "do the design later". I don't even know how you would do that, since you need the design to submit a patch.
Quoted 7 lines
Then I might mis-understand Gary? He said:
"Can we avoid two types and use a generic
Atomic<T>and then implement onAtomic<i32>andAtomic<i64>instead?", which means just replace
impl AtomicI32withimpl Atomic<i32>to me.
This is a fair interpretation, but what prevents you from merging the impls of functions that can be? I assumed that you would do that automatically.
Quoted 9 lines
Quoted 3 lines
I can't offer you a complete API description, since that would require me writing it up myself. But I would recommend trying to get it to work with generics. I got a few other comments:
We should work on things that are concrete, right? It's fine that the design is not complete, and it's fine if you just recommend. But without a somewhat concrete design (doesn't have to be complete), I cannot be sure about whether we have the same vision of the future of generic atomics (see my question to Gary), that's a bit hard for me to try to
Sorry, which question?
Also to be aligned on the vision, I think we should rather talk about the vision and not the design, since the design that we want to have now can be different from the vision. On that note, what do you envision the future of the atomic API?
Quoted 4 lines
work something out (plus I personally don't think it's a good idea, it's OK to me, but not good). Anyway, I wasn't trying to refuse to do this just based on personal reasons, but I do need to understand what you are all proposing, because I don't have one myself.
I went back through the thread and here is what I think your argument against generics is: people should think about size and alignment when using atomics, which is problematic when allowing users to leave the atomic generic.
But as I argued before, this is not an issue. Have I overlooked another argument? Because I don't see anything else.
Quoted 10 lines
Quoted 7 lines
- I don't think that we should resort to a script to generate the Rust code since it prevents adding good documentation & examples to the various methods. AFAIU you want to generate the functions from
scripts/atomic/atomics.tblto keep it in sync with the C side. I looked at the git log of that file and it hasn't been changed significantly since its inception. I don't think that there is any benefit to generating the functions from that file.I'll leave this to other atomic maintainers.
Quoted 8 lines
Quoted 3 lines
- most of the documented functions say "See
c_function", I don't like this, can we either copy the C documentation (I imagine it not changing that often, or is that assumption wrong?) or write our own?You're not wrong. AN in C side, we do have some documentation template to generate the comments (see
scripts/atomic/kerneldoc). But first the format is for doxygen(I guess?), and second as you just bring up, the templates are tied with the bash script.
I see a bash script similarly to how Wedson sees proc macros ;) We should try *hard* to avoid them and only use them when there is no other way.
Quoted 6 lines
Quoted 2 lines
- we should try to use either const generic or normal parameters for the access ordering instead of putting it in the function name.
Why is it important? Keeping it in the current way brings the value that it's not too much different than their C counterparts. Could you explain a bit the pros and cons on suffix vs const generic approach?
Reduce code duplication, instead of 3 different variants, we can have one. It allows people to build ergonomic APIs that allows the user to decide which synchronization they use under the hood.
Quoted 5 lines
Quoted 2 lines
- why do we need both non-return and return variants?
Historical reason I guess. Plus maybe some architectures have a better implementation on non-return atomics IIRC.
Could we get some more concrete arguments for why we would need these in rust? If the reason is purely historical, then we shouldn't expose the non-return variant IMO. If it is because of performance, then we can only expose them in the respective arches.
---
Cheers,
Benno
Re: [RFC 2/2] rust: sync: Add atomic support
On Sun, Jun 16, 2024 at 03:55:12PM +0000, Benno Lossin wrote:
[...]
Quoted 10 lines
Quoted 10 lines
Quoted 4 lines
I don't think that the idea was to "do the design later". I don't even know how you would do that, since you need the design to submit a patch.
Then I might mis-understand Gary? He said:
"Can we avoid two types and use a generic
Atomic<T>and then implement onAtomic<i32>andAtomic<i64>instead?"
Quoted 7 lines
Quoted 2 lines
, which means just replace
impl AtomicI32withimpl Atomic<i32>to me.This is a fair interpretation, but what prevents you from merging the impls of functions that can be? I assumed that you would do that automatically.
I think you missed the point, Gary's suggestion at that time sounds
like: let's impl Atomic<i32> and Atomic<i64> first, and leave rest of the work for later, that is a "do the design later" to me.
Quoted 11 lines
Quoted 9 lines
Quoted 3 lines
I can't offer you a complete API description, since that would require me writing it up myself. But I would recommend trying to get it to work with generics. I got a few other comments:
We should work on things that are concrete, right? It's fine that the design is not complete, and it's fine if you just recommend. But without a somewhat concrete design (doesn't have to be complete), I cannot be sure about whether we have the same vision of the future of generic atomics (see my question to Gary), that's a bit hard for me to try to
Sorry, which question?
https://lore.kernel.org/rust-for-linux/[email protected]/
Quoted 5 lines
Also to be aligned on the vision, I think we should rather talk about the vision and not the design, since the design that we want to have now can be different from the vision. On that note, what do you envision the future of the atomic API?
Mine is simple, just providing AtomicI32 and AtomicI64 first, since there are immediate users right now, and should we learn more needs from the users, we get more idea about what a good API is, and we evolve from there.
Quoted 10 lines
Quoted 4 lines
work something out (plus I personally don't think it's a good idea, it's OK to me, but not good). Anyway, I wasn't trying to refuse to do this just based on personal reasons, but I do need to understand what you are all proposing, because I don't have one myself.
I went back through the thread and here is what I think your argument against generics is: people should think about size and alignment when using atomics, which is problematic when allowing users to leave the atomic generic.
But as I argued before, this is not an issue. Have I overlooked another
You mean you said it's a non-issue but gave me two counteract? If it's really a non-issue, you won't need a couneraction, right? In other words non-generic atomics do provide some value.
Quoted 16 lines
argument? Because I don't see anything else.
Quoted 14 lines
Quoted 7 lines
- I don't think that we should resort to a script to generate the Rust code since it prevents adding good documentation & examples to the various methods. AFAIU you want to generate the functions from
scripts/atomic/atomics.tblto keep it in sync with the C side. I looked at the git log of that file and it hasn't been changed significantly since its inception. I don't think that there is any benefit to generating the functions from that file.I'll leave this to other atomic maintainers.
Quoted 3 lines
- most of the documented functions say "See
c_function", I don't like this, can we either copy the C documentation (I imagine it not changing that often, or is that assumption wrong?) or write our own?
Quoted 9 lines
Quoted 4 lines
You're not wrong. AN in C side, we do have some documentation template to generate the comments (see
scripts/atomic/kerneldoc). But first the format is for doxygen(I guess?), and second as you just bring up, the templates are tied with the bash script.I see a bash script similarly to how Wedson sees proc macros ;) We should try *hard* to avoid them and only use them when there is no other way.
I will just start with the existing mechanism and try to evolve, whether it's a script or proc macro, I don't mind, I want to get the work done and most relevant people can understand in the way the they prefer and step-by-step, move it to the place I think is the best for the project.
Quoted 11 lines
Quoted 6 lines
Quoted 2 lines
- we should try to use either const generic or normal parameters for the access ordering instead of putting it in the function name.
Why is it important? Keeping it in the current way brings the value that it's not too much different than their C counterparts. Could you explain a bit the pros and cons on suffix vs const generic approach?
Reduce code duplication, instead of 3 different variants, we can have one. It allows people to build ergonomic APIs that allows the user to decide which synchronization they use under the hood.
I already mentioned why I think it's good in the current way, I will defer it to others on their inputs.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
- why do we need both non-return and return variants?
Historical reason I guess. Plus maybe some architectures have a better implementation on non-return atomics IIRC.
Could we get some more concrete arguments for why we would need these in rust? If the reason is purely historical, then we shouldn't expose the
Sure. Look like my memory is correct, at least on ARM64 they are different instructions (see arch/arm64/include/asm/atomic_lse.h)
non-return atomics on ARM64:
#define ATOMIC_OP(op, asm_op) \
static __always_inline void \
__lse_atomic_##op(int i, atomic_t *v) \
{ \
asm volatile( \
__LSE_PREAMBLE \
" " #asm_op " %w[i], %[v]\n" \
: [v] "+Q" (v->counter) \
: [i] "r" (i)); \
}value-return atomics on ARM64:
#define ATOMIC_FETCH_OP(name, mb, op, asm_op, cl...) \
static __always_inline int \
__lse_atomic_fetch_##op##name(int i, atomic_t *v) \
{ \
int old; \
\
asm volatile( \
__LSE_PREAMBLE \
" " #asm_op #mb " %w[i], %w[old], %[v]" \
: [v] "+Q" (v->counter), \
[old] "=r" (old) \
: [i] "r" (i) \
: cl); \
\
return old; \
}It may not be easy to see the different instructions from the pasted code, but you can find them in the header file, also you could notice that the number of operands is different?
Quoted 3 lines
non-return variant IMO. If it is because of performance, then we can only expose them in the respective arches.
Hmm.. so we ask user to write arch-specific code like:
pub fn increase_counter(&self) {
#[cfg(CONFIG_ARM64)]
self.counter.inc();
#[cfg(CONFIG_X86_64)]
let _ = self.counter.inc_return();
}are you sure it's a good idea?
Regards,
Boqun
Quoted 4 lines
---
Cheers,
Benno
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 15 lines · Boqun Feng
On Sun, Jun 16, 2024 at 03:55:12PM +0000, Benno Lossin wrote:
[...]Quoted 13 lines
Quoted 12 lines
Quoted 4 lines
I don't think that the idea was to "do the design later". I don't even know how you would do that, since you need the design to submit a patch.
Then I might mis-understand Gary? He said:
"Can we avoid two types and use a generic
Atomic<T>and then implement onAtomic<i32>andAtomic<i64>instead?", which means just replace
impl AtomicI32withimpl Atomic<i32>to me.
Quoted 8 lines
Quoted 4 lines
This is a fair interpretation, but what prevents you from merging the impls of functions that can be? I assumed that you would do that automatically.
I think you missed the point, Gary's suggestion at that time sounds
like: let's impl Atomic<i32> and Atomic<i64> first, and leave rest of the work for later, that is a "do the design later" to me.
Hmm, but wouldn't that just be less work for you?
Quoted 20 lines
Quoted 11 lines
Quoted 9 lines
Quoted 3 lines
I can't offer you a complete API description, since that would require me writing it up myself. But I would recommend trying to get it to work with generics. I got a few other comments:
We should work on things that are concrete, right? It's fine that the design is not complete, and it's fine if you just recommend. But without a somewhat concrete design (doesn't have to be complete), I cannot be sure about whether we have the same vision of the future of generic atomics (see my question to Gary), that's a bit hard for me to try to
Sorry, which question?
https://lore.kernel.org/rust-for-linux/[email protected]/
Quoted 5 lines
Also to be aligned on the vision, I think we should rather talk about the vision and not the design, since the design that we want to have now can be different from the vision. On that note, what do you envision the future of the atomic API?
Quoted 4 lines
Mine is simple, just providing AtomicI32 and AtomicI64 first, since there are immediate users right now, and should we learn more needs from the users, we get more idea about what a good API is, and we evolve from there.
That is fine, but since we want to get generics in the future anyways, I think it would be good to already implement them now to also gather feedback on them.
Quoted 11 lines
Quoted 10 lines
Quoted 4 lines
work something out (plus I personally don't think it's a good idea, it's OK to me, but not good). Anyway, I wasn't trying to refuse to do this just based on personal reasons, but I do need to understand what you are all proposing, because I don't have one myself.
I went back through the thread and here is what I think your argument against generics is: people should think about size and alignment when using atomics, which is problematic when allowing users to leave the atomic generic.
But as I argued before, this is not an issue. Have I overlooked another
Quoted 3 lines
You mean you said it's a non-issue but gave me two counteract? If it's really a non-issue, you won't need a couneraction, right? In other words non-generic atomics do provide some value.
The second counteractions would provide exactly the same API surface as your non-generic version, so I don't see how going non-generic provides any value over going generic.
The first approach was intended for a future in which we are not scared of people using generic atomics in their structs. I don't know if we are going to be in that future, so I think we should go with the second approach for the time being.
Quoted 26 lines
Quoted 16 lines
argument? Because I don't see anything else.
Quoted 14 lines
Quoted 7 lines
- I don't think that we should resort to a script to generate the Rust code since it prevents adding good documentation & examples to the various methods. AFAIU you want to generate the functions from
scripts/atomic/atomics.tblto keep it in sync with the C side. I looked at the git log of that file and it hasn't been changed significantly since its inception. I don't think that there is any benefit to generating the functions from that file.I'll leave this to other atomic maintainers.
Quoted 3 lines
- most of the documented functions say "See
c_function", I don't like this, can we either copy the C documentation (I imagine it not changing that often, or is that assumption wrong?) or write our own?Quoted 9 lines
Quoted 4 lines
You're not wrong. AN in C side, we do have some documentation template to generate the comments (see
scripts/atomic/kerneldoc). But first the format is for doxygen(I guess?), and second as you just bring up, the templates are tied with the bash script.I see a bash script similarly to how Wedson sees proc macros ;) We should try *hard* to avoid them and only use them when there is no other way.
Quoted 4 lines
I will just start with the existing mechanism and try to evolve, whether it's a script or proc macro, I don't mind, I want to get the work done and most relevant people can understand in the way the they prefer and step-by-step, move it to the place I think is the best for the project.
I don't think that we need a script or a proc macro. A few declarative macros probably suffice if we go the way of generics.
Quoted 59 lines
Quoted 11 lines
Quoted 6 lines
Quoted 2 lines
- we should try to use either const generic or normal parameters for the access ordering instead of putting it in the function name.
Why is it important? Keeping it in the current way brings the value that it's not too much different than their C counterparts. Could you explain a bit the pros and cons on suffix vs const generic approach?
Reduce code duplication, instead of 3 different variants, we can have one. It allows people to build ergonomic APIs that allows the user to decide which synchronization they use under the hood.
I already mentioned why I think it's good in the current way, I will defer it to others on their inputs.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
- why do we need both non-return and return variants?
Historical reason I guess. Plus maybe some architectures have a better implementation on non-return atomics IIRC.
Could we get some more concrete arguments for why we would need these in rust? If the reason is purely historical, then we shouldn't expose the
Sure. Look like my memory is correct, at least on ARM64 they are different instructions (see
arch/arm64/include/asm/atomic_lse.h)non-return atomics on ARM64:
#define ATOMIC_OP(op, asm_op) \ static __always_inline void \ __lse_atomic_##op(int i, atomic_t *v) \ { \ asm volatile( \ __LSE_PREAMBLE \ " " #asm_op " %w[i], %[v]\n" \ : [v] "+Q" (v->counter) \ : [i] "r" (i)); \ }value-return atomics on ARM64:
#define ATOMIC_FETCH_OP(name, mb, op, asm_op, cl...) \ static __always_inline int \ __lse_atomic_fetch_##op##name(int i, atomic_t *v) \ { \ int old; \ \ asm volatile( \ __LSE_PREAMBLE \ " " #asm_op #mb " %w[i], %w[old], %[v]" \ : [v] "+Q" (v->counter), \ [old] "=r" (old) \ : [i] "r" (i) \ : cl); \ \ return old; \ }
Quoted 3 lines
It may not be easy to see the different instructions from the pasted code, but you can find them in the header file, also you could notice that the number of operands is different?
This is not my expertise, so I believe you :)
Quoted 14 lines
Quoted 3 lines
non-return variant IMO. If it is because of performance, then we can only expose them in the respective arches.
Hmm.. so we ask user to write arch-specific code like:
pub fn increase_counter(&self) { #[cfg(CONFIG_ARM64)] self.counter.inc(); #[cfg(CONFIG_X86_64)] let _ = self.counter.inc_return(); }
Quoted 1 line
are you sure it's a good idea?
No that looks horrible.
Maybe there is something that we can do with generics, but I don't know if it is worth it. I guess we can leave that open for the time being.
---
Cheers,
Benno
Re: [RFC 2/2] rust: sync: Add atomic support
On Wed, Jun 19, 2024 at 09:09:46AM +0000, Benno Lossin wrote:
[...]
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Quoted 7 lines
Then I might mis-understand Gary? He said:
"Can we avoid two types and use a generic
Atomic<T>and then implement onAtomic<i32>andAtomic<i64>instead?", which means just replace
impl AtomicI32withimpl Atomic<i32>to me.This is a fair interpretation, but what prevents you from merging the impls of functions that can be? I assumed that you would do that automatically.
Quoted 6 lines
Quoted 3 lines
I think you missed the point, Gary's suggestion at that time sounds
like: let's impl Atomic<i32> and Atomic<i64> first, and leave rest of the work for later, that is a "do the design later" to me.Hmm, but wouldn't that just be less work for you?
Not really ;-) Committing to a generic interface without a vision/design is going to be a maintenace nightmare. I will need to deal the soundness issues and different ideas about what can be put in <..>. So I'd suggest we take great caution before we make our decision, and I may even ask for a formal proof of the soundness of a generic interface for key components such as atomics. Because last time I check, safety is what Rust brings on the table, right?
Someone may say that non generic atomics is one of the biggest mistakes that Rust ever made. But I disagree. In fact when I saw this, my first impression was "these guys know their stuff": there are only a few types and operations that make sense, so putting them in a generic interface would be over-engineer (at least at the beginning). It's better correct than convenient/popular/aesthetical.
So would you and Gary be interested at working on such a design and proof? Maybe someone else Cced would also be interested. Eventually we will need the tool of soundness proof for other types as well, that's going to be very vaulable. And having that would be the real "less work for me" ;-)
Right now, I still plan to do in the current way (i.e. non-generic atomcis for i32, i64, isize) because there are already users [1], the correctness is easy to confirm and we won't confuse users with half-baked generic design. But should we have a sound Atomic<T> design, I'm happy to adopt it ;-)
[...]
Quoted 15 lines
Quoted 10 lines
Quoted 5 lines
Also to be aligned on the vision, I think we should rather talk about the vision and not the design, since the design that we want to have now can be different from the vision. On that note, what do you envision the future of the atomic API?
Mine is simple, just providing AtomicI32 and AtomicI64 first, since there are immediate users right now, and should we learn more needs from the users, we get more idea about what a good API is, and we evolve from there.
That is fine, but since we want to get generics in the future anyways, I think it would be good to already implement them now to also gather feedback on them.
[...]
Quoted 5 lines
Quoted 4 lines
You mean you said it's a non-issue but gave me two counteract? If it's really a non-issue, you won't need a couneraction, right? In other words non-generic atomics do provide some value.
Quoted 8 lines
The second counteractions would provide exactly the same API surface as your non-generic version, so I don't see how going non-generic provides any value over going generic.
The first approach was intended for a future in which we are not scared of people using generic atomics in their structs. I don't know if we are going to be in that future, so I think we should go with the second approach for the time being.
Not going to reply the above right now, because I'm leaning more torwards switching when we have a sound Atomic<T> design, but I want you to know I appreciate your input and explanation.
Quoted 26 lines
Quoted 26 lines
Quoted 25 lines
argument? Because I don't see anything else.
Quoted 10 lines
Quoted 7 lines
- I don't think that we should resort to a script to generate the Rust code since it prevents adding good documentation & examples to the various methods. AFAIU you want to generate the functions from
scripts/atomic/atomics.tblto keep it in sync with the C side. I looked at the git log of that file and it hasn't been changed significantly since its inception. I don't think that there is any benefit to generating the functions from that file.I'll leave this to other atomic maintainers.
Quoted 8 lines
Quoted 3 lines
- most of the documented functions say "See
c_function", I don't like this, can we either copy the C documentation (I imagine it not changing that often, or is that assumption wrong?) or write our own?You're not wrong. AN in C side, we do have some documentation template to generate the comments (see
scripts/atomic/kerneldoc). But first the format is for doxygen(I guess?), and second as you just bring up, the templates are tied with the bash script.I see a bash script similarly to how Wedson sees proc macros ;) We should try *hard* to avoid them and only use them when there is no other way.
Quoted 8 lines
Quoted 4 lines
I will just start with the existing mechanism and try to evolve, whether it's a script or proc macro, I don't mind, I want to get the work done and most relevant people can understand in the way the they prefer and step-by-step, move it to the place I think is the best for the project.
I don't think that we need a script or a proc macro. A few declarative macros probably suffice if we go the way of generics.
Ah right. And even without generics we could use some declarative macros, and I'm happy to take a look at this and provide the alternative approach so we can choose a better one.
[1]: https://lore.kernel.org/rust-for-linux/[email protected]/
Regards,
Boqun
Re: [RFC 2/2] rust: sync: Add atomic support
In reply to Benno LossinOn Sun, Jun 16, 2024 at 09:46:45AM +0000, Benno Lossin wrote:
[...]
Quoted 7 lines
Quoted 3 lines
To me, it's perfectly fine that Atomic{I32,I64} co-exist with Atomic<T>. What's the downside? A bit specific example would help me understand the real concern here.
I don't like that, why have two ways of doing the same thing? People will be confused whether they should use
AtomicI32vsAtomic<i32>...
BTW, we already have something similar like this in kernel, we have SpinLock<T> and Lock<T, SpinLockBackend>, how should we do about this?
Regards,
Boqun
Quoted 4 lines
---
Cheers,
Benno
Re: [RFC 2/2] rust: sync: Add atomic support
In reply to Boqun FengQuoted 13 lines · Boqun Feng
What's the issue of having AtomicI32 and AtomicI64 first then? We don't need to do 1 or 2 until the real users show up.
And I'd like also to point out that there are a few more trait bound designs needed for Atomic<T>, for example, Atomic<u32> and Atomic<i32> have different sets of API (no
inc_unless_negative()for u32).Don't make me wrong, I have no doubt we can handle this in the type system, but given the design work need, won't it make sense that we take baby steps on this? We can first introduce AtomicI32 and AtomicI64 which already have real users, and then if there are some values of generic atomics, we introduce them and have proper discussion on design.
Quoted 3 lines
To me, it's perfectly fine that Atomic{I32,I64} co-exist with Atomic<T>. What's the downside? A bit specific example would help me understand the real concern here.
Err, what?
Of course we want generic atomics, and we need that for properly supporting cmpxchg.
Bogun, you've got all the rust guys pushing for doing this with generics, I'm not sure why you're being stubborn here?
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 14 lines · Kent Overstreet
Quoted 13 lines · Boqun Feng
What's the issue of having AtomicI32 and AtomicI64 first then? We don't need to do 1 or 2 until the real users show up.
And I'd like also to point out that there are a few more trait bound designs needed for Atomic<T>, for example, Atomic<u32> and Atomic<i32> have different sets of API (no
inc_unless_negative()for u32).Don't make me wrong, I have no doubt we can handle this in the type system, but given the design work need, won't it make sense that we take baby steps on this? We can first introduce AtomicI32 and AtomicI64 which already have real users, and then if there are some values of generic atomics, we introduce them and have proper discussion on design.
Quoted 9 lines
Quoted 3 lines
To me, it's perfectly fine that Atomic{I32,I64} co-exist with Atomic<T>. What's the downside? A bit specific example would help me understand the real concern here.
Err, what?
Of course we want generic atomics, and we need that for properly supporting cmpxchg.
Nope. Note this series only introduces the atomic types (atomic_ C APIs), but cmpxchg C APIs (no atomic_ prefix) are probably presented via a different API, where we need to make it easier to interact with normal types, and we may use generic there.
Quoted 2 lines
Bogun, you've got all the rust guys pushing for doing this with generics, I'm not sure why you're being stubborn here?
Hmm? Have you seen the email I replied to John, a broader Rust community seems doesn't appreciate the idea of generic atomics.
Regards,
Boqun
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 21 lines · Boqun Feng
Quoted 20 lines · Kent Overstreet
Quoted 13 lines · Boqun Feng
What's the issue of having AtomicI32 and AtomicI64 first then? We don't need to do 1 or 2 until the real users show up.
And I'd like also to point out that there are a few more trait bound designs needed for Atomic<T>, for example, Atomic<u32> and Atomic<i32> have different sets of API (no
inc_unless_negative()for u32).Don't make me wrong, I have no doubt we can handle this in the type system, but given the design work need, won't it make sense that we take baby steps on this? We can first introduce AtomicI32 and AtomicI64 which already have real users, and then if there are some values of generic atomics, we introduce them and have proper discussion on design.
Quoted 3 lines
To me, it's perfectly fine that Atomic{I32,I64} co-exist with Atomic<T>. What's the downside? A bit specific example would help me understand the real concern here.
Err, what?
Quoted 9 lines
Quoted 3 lines
Of course we want generic atomics, and we need that for properly supporting cmpxchg.
Nope. Note this series only introduces the atomic types (
atomic_C APIs), but cmpxchg C APIs (noatomic_prefix) are probably presented via a different API, where we need to make it easier to interact with normal types, and we may use generic there.
Or it could be a generic function instead of generic type like:
pub unsafe fn cmpxchg<T>(ptr: * mut T, old: T, new T) -> T
the "unsafe" part is due to ptr may not be a valid pointer or this may make normal accesses data race.
Regards,
Boqun
Quoted 8 lines
Quoted 2 lines
Bogun, you've got all the rust guys pushing for doing this with generics, I'm not sure why you're being stubborn here?
Hmm? Have you seen the email I replied to John, a broader Rust community seems doesn't appreciate the idea of generic atomics.
Regards,
Boqun
Re: [RFC 2/2] rust: sync: Add atomic support
In reply to Boqun FengQuoted 3 lines · Boqun Feng
Hmm? Have you seen the email I replied to John, a broader Rust community seems doesn't appreciate the idea of generic atomics.
I don't think we can easily draw that conclusion from those download numbers / dependent crates.
portable-atomic may be more popular simply because it provides features for platforms the standard library does not. The interface being generic or not may have nothing to do with it. Or perhaps because it has a 1.x version, while the other doesn't, etc.
In fact, the atomic crate is essentially about providing Atomic<T>, so one could argue that all those downloads are precisely from people that want a generic atomic.
Moreover, I noticed portable-atomic's issue #1 in GitHub is, precisely, adding Atomic<T> support. The maintainer has a PR for that updated over time, most recently a few hours ago.
There is also AtomicCell<T> from crossbeam, which is the first feature listed in its docs.
Anyway...
The way I see it, both approaches seem similar (i.e. for what we are going to use them for today, at least) and neither apparently has a major downside today for those use cases (apart from needed refactors later to go to another approach).
(By the "generic approach", by the way, I mean just providing Atomic<{i32,i64}>, not a complex design)
So it is up to you on what you send for the non-RFC patches, of course, and if nobody has the time / wants to do the work for the "simple" generic approach, then we can just go ahead with this for the moment. But I think it would be nice to at least consider the "simple" generic approach to see how much worse it would be.
Other bits to consider, that perhaps give you arguments for one or the
other: consequences on the compilation time, on inlining, on the error messages for new users, on the generated documentation, on how easy to grep they are, etc.
Cheers,
Miguel
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 40 lines · Miguel Ojeda
Quoted 3 lines · Boqun Feng
Hmm? Have you seen the email I replied to John, a broader Rust community seems doesn't appreciate the idea of generic atomics.
I don't think we can easily draw that conclusion from those download numbers / dependent crates.
portable-atomic may be more popular simply because it provides features for platforms the standard library does not. The interface being generic or not may have nothing to do with it. Or perhaps because it has a 1.x version, while the other doesn't, etc.
In fact, the atomic crate is essentially about providing
Atomic<T>, so one could argue that all those downloads are precisely from people that want a generic atomic.Moreover, I noticed portable-atomic's issue #1 in GitHub is, precisely, adding
Atomic<T>support. The maintainer has a PR for that updated over time, most recently a few hours ago.There is also
AtomicCell<T>from crossbeam, which is the first feature listed in its docs.Anyway...
The way I see it, both approaches seem similar (i.e. for what we are going to use them for today, at least) and neither apparently has a major downside today for those use cases (apart from needed refactors later to go to another approach).
(By the "generic approach", by the way, I mean just providing
Atomic<{i32,i64}>, not a complex design)So it is up to you on what you send for the non-RFC patches, of course, and if nobody has the time / wants to do the work for the "simple" generic approach, then we can just go ahead with this for the moment. But I think it would be nice to at least consider the "simple" generic approach to see how much worse it would be.
Quoted 4 lines
Other bits to consider, that perhaps give you arguments for one or the
other: consequences on the compilation time, on inlining, on the error messages for new users, on the generated documentation, on how easy to grep they are, etc.
Yeah, rereading the thread - I'm with Miguel and Gary.
Generics are simply the correct way to do it, if the wider rust community didn't do it that way I think that can be chalked up more to historical baggage or needlessly copying the base integer type scheme.
Let's please do it right here, and generics are the correct approach.
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 46 lines · Kent Overstreet
Quoted 40 lines · Miguel Ojeda
Quoted 3 lines · Boqun Feng
Hmm? Have you seen the email I replied to John, a broader Rust community seems doesn't appreciate the idea of generic atomics.
I don't think we can easily draw that conclusion from those download numbers / dependent crates.
portable-atomic may be more popular simply because it provides features for platforms the standard library does not. The interface being generic or not may have nothing to do with it. Or perhaps because it has a 1.x version, while the other doesn't, etc.
In fact, the atomic crate is essentially about providing
Atomic<T>, so one could argue that all those downloads are precisely from people that want a generic atomic.Moreover, I noticed portable-atomic's issue #1 in GitHub is, precisely, adding
Atomic<T>support. The maintainer has a PR for that updated over time, most recently a few hours ago.There is also
AtomicCell<T>from crossbeam, which is the first feature listed in its docs.Anyway...
The way I see it, both approaches seem similar (i.e. for what we are going to use them for today, at least) and neither apparently has a major downside today for those use cases (apart from needed refactors later to go to another approach).
(By the "generic approach", by the way, I mean just providing
Atomic<{i32,i64}>, not a complex design)So it is up to you on what you send for the non-RFC patches, of course, and if nobody has the time / wants to do the work for the "simple" generic approach, then we can just go ahead with this for the moment. But I think it would be nice to at least consider the "simple" generic approach to see how much worse it would be.
Quoted 4 lines
Other bits to consider, that perhaps give you arguments for one or the
other: consequences on the compilation time, on inlining, on the error messages for new users, on the generated documentation, on how easy to grep they are, etc.
Quoted 7 lines
Yeah, rereading the thread - I'm with Miguel and Gary.
Generics are simply the correct way to do it, if the wider rust community didn't do it that way I think that can be chalked up more to historical baggage or needlessly copying the base integer type scheme.
Let's please do it right here, and generics are the correct approach.
If so, maybe we should do u<Wide> instead of u8, u16, oh, and probably just Integer<Sign, Wide> instead of i{8,16,32,64) and u{8,16,32,64} ;-)
Regards,
Boqun
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 47 lines · Boqun Feng
Quoted 46 lines · Kent Overstreet
Quoted 44 lines · Miguel Ojeda
Quoted 3 lines · Boqun Feng
Hmm? Have you seen the email I replied to John, a broader Rust community seems doesn't appreciate the idea of generic atomics.
I don't think we can easily draw that conclusion from those download numbers / dependent crates.
portable-atomic may be more popular simply because it provides features for platforms the standard library does not. The interface being generic or not may have nothing to do with it. Or perhaps because it has a 1.x version, while the other doesn't, etc.
In fact, the atomic crate is essentially about providing
Atomic<T>, so one could argue that all those downloads are precisely from people that want a generic atomic.Moreover, I noticed portable-atomic's issue #1 in GitHub is, precisely, adding
Atomic<T>support. The maintainer has a PR for that updated over time, most recently a few hours ago.There is also
AtomicCell<T>from crossbeam, which is the first feature listed in its docs.Anyway...
The way I see it, both approaches seem similar (i.e. for what we are going to use them for today, at least) and neither apparently has a major downside today for those use cases (apart from needed refactors later to go to another approach).
(By the "generic approach", by the way, I mean just providing
Atomic<{i32,i64}>, not a complex design)So it is up to you on what you send for the non-RFC patches, of course, and if nobody has the time / wants to do the work for the "simple" generic approach, then we can just go ahead with this for the moment. But I think it would be nice to at least consider the "simple" generic approach to see how much worse it would be.
Other bits to consider, that perhaps give you arguments for one or the
other: consequences on the compilation time, on inlining, on the error messages for new users, on the generated documentation, on how easy to grep they are, etc.
Quoted 8 lines
Quoted 7 lines
Yeah, rereading the thread - I'm with Miguel and Gary.
Generics are simply the correct way to do it, if the wider rust community didn't do it that way I think that can be chalked up more to historical baggage or needlessly copying the base integer type scheme.
Let's please do it right here, and generics are the correct approach.
I think the disagreement here is not non-generic atomic vs generic atomic, it's pure generic atomic vs. AtomicI{32,64} etc + generic atomic. I said multiple times that I'm OK with generic atomics if there are real users, just I'm not sure it's something we want to do right now (or we have enough information to go fully on that direction). And I think it's fine to have non-generic atomic and generic atomic coexist.
Regards,
Boqun
Quoted 5 lines
If so, maybe we should do u<Wide> instead of u8, u16, oh, and probably just Integer<Sign, Wide> instead of i{8,16,32,64) and u{8,16,32,64} ;-)
Regards,
Boqun
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 6 lines · Boqun Feng
I think the disagreement here is not non-generic atomic vs generic atomic, it's pure generic atomic vs. AtomicI{32,64} etc + generic atomic. I said multiple times that I'm OK with generic atomics if there are real users, just I'm not sure it's something we want to do right now (or we have enough information to go fully on that direction). And I think it's fine to have non-generic atomic and generic atomic coexist.
Well, having the generic interface from the start does matter, that's what we (myself included) want to code to.
No need to overcomplicate it, just
Atomic<u8>
Atomic<u16>
etc...
As long as that's available, the internal implementation shouldn't have to change.
Re: [RFC 2/2] rust: sync: Add atomic support
In reply to Miguel OjedaQuoted 5 lines · Miguel Ojeda
Quoted 3 lines · Boqun Feng
Hmm? Have you seen the email I replied to John, a broader Rust community seems doesn't appreciate the idea of generic atomics.
Quoted 8 lines
I don't think we can easily draw that conclusion from those download numbers / dependent crates.
portable-atomic may be more popular simply because it provides features for platforms the standard library does not. The interface being generic or not may have nothing to do with it. Or perhaps because it has a 1.x version, while the other doesn't, etc.
Totally agreed, but that was the information all I could get at that time ;-)
Quoted 8 lines
In fact, the atomic crate is essentially about providing
Atomic<T>, so one could argue that all those downloads are precisely from people that want a generic atomic.Moreover, I noticed portable-atomic's issue #1 in GitHub is, precisely, adding
Atomic<T>support. The maintainer has a PR for that updated over time, most recently a few hours ago.
Wait! Could it be because of me? Or I'm thinking too much about myself ;-)
Quoted 10 lines
There is also
AtomicCell<T>from crossbeam, which is the first feature listed in its docs.Anyway...
The way I see it, both approaches seem similar (i.e. for what we are going to use them for today, at least) and neither apparently has a major downside today for those use cases (apart from needed refactors later to go to another approach).
Quoted 9 lines
(By the "generic approach", by the way, I mean just providing
Atomic<{i32,i64}>, not a complex design)So it is up to you on what you send for the non-RFC patches, of course, and if nobody has the time / wants to do the work for the "simple" generic approach, then we can just go ahead with this for the moment. But I think it would be nice to at least consider the "simple" generic approach to see how much worse it would be.
What would work for me is a somewhat concrete design consensus (on what sizes we are going to support, the expectation on how many traits we are going to introduce, etc.) And then a simple generic approach is not a problem. (But I remain my right to say "I told you so" ;-)) As I said, we cannot just do generic because of generic, we have to have at least some idea abou the things we are generify on (the scope and the cost in term of how many more traits people need to understand).
Quoted 5 lines
Other bits to consider, that perhaps give you arguments for one or the
other: consequences on the compilation time, on inlining, on the error messages for new users, on the generated documentation, on how easy to grep they are, etc.
These seem non-issues to me (except the grep part), but I'm relatively more familiar with atomics and Rust, so it's good to hear others thought, or wait the feedback until we have the patchset to review.
Regards,
Boqun
Quoted 2 lines
Cheers,
Miguel
Re: [RFC 2/2] rust: sync: Add atomic support
In reply to Boqun FengQuoted 30 lines · Boqun Feng
Quoted 14 lines · Kent Overstreet
Quoted 13 lines · Boqun Feng
What's the issue of having AtomicI32 and AtomicI64 first then? We don't need to do 1 or 2 until the real users show up.
And I'd like also to point out that there are a few more trait bound designs needed for Atomic<T>, for example, Atomic<u32> and Atomic<i32> have different sets of API (no
inc_unless_negative()for u32).Don't make me wrong, I have no doubt we can handle this in the type system, but given the design work need, won't it make sense that we take baby steps on this? We can first introduce AtomicI32 and AtomicI64 which already have real users, and then if there are some values of generic atomics, we introduce them and have proper discussion on design.
Quoted 9 lines
Quoted 3 lines
To me, it's perfectly fine that Atomic{I32,I64} co-exist with Atomic<T>. What's the downside? A bit specific example would help me understand the real concern here.
Err, what?
Of course we want generic atomics, and we need that for properly supporting cmpxchg.
Nope. Note this series only introduces the atomic types (
atomic_C APIs), but cmpxchg C APIs (noatomic_prefix) are probably presented via a different API, where we need to make it easier to interact with normal types, and we may use generic there.
Quoted 5 lines
Quoted 2 lines
Bogun, you've got all the rust guys pushing for doing this with generics, I'm not sure why you're being stubborn here?
Hmm? Have you seen the email I replied to John, a broader Rust community seems doesn't appreciate the idea of generic atomics.
Apologies, I appear to have gotten things backwards in my pre-coffee reading, I'll have to catch up on the whole thread :)
Re: [RFC 2/2] rust: sync: Add atomic support
In reply to Miguel OjedaQuoted 7 lines · Miguel Ojeda
Quoted 2 lines · Boqun Feng
Does this make sense?
Implementation-wise, if you think it is simpler or more clear/elegant to have the extra lower level layer, then that sounds fine.
Quoted 8 lines
However, I was mainly talking about what we would eventually expose to users, i.e. do we want to provide
Atomic<T>to begin with? If yes, then we could make the lower layer private already.We can defer that extra layer/work if needed even if we go for
Atomic<T>, but it would be nice to understand if we have consensus for an eventual user-facing API, or if someone has any other opinion or concerns on one vs. the other.
Well, here's one:
The reason that we have things like atomic64_read() in the C code is because C doesn't have generics.
In Rust, we should simply move directly to Atomic<T>, as there are, after all, associated benefits. And it's very easy to see the connection between the C types and the Atomic<T> types.
thanks,
Signature · 2 lines
John Hubbard NVIDIA
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 17 lines · John Hubbard
Quoted 7 lines · Miguel Ojeda
Quoted 2 lines · Boqun Feng
Does this make sense?
Implementation-wise, if you think it is simpler or more clear/elegant to have the extra lower level layer, then that sounds fine.
Quoted 8 lines
However, I was mainly talking about what we would eventually expose to users, i.e. do we want to provide
Atomic<T>to begin with? If yes, then we could make the lower layer private already.We can defer that extra layer/work if needed even if we go for
Atomic<T>, but it would be nice to understand if we have consensus for an eventual user-facing API, or if someone has any other opinion or concerns on one vs. the other.
Quoted 7 lines
Well, here's one:
The reason that we have things like
atomic64_read()in the C code is because C doesn't have generics.In Rust, we should simply move directly to Atomic<T>, as there are, after all, associated benefits. And it's very easy to see the connection
What are the associated benefits you are referring to? Rust std doesn't use Atomic<T>, that somewhat proves that we don't need it.
Regards,
Boqun
Quoted 8 lines
between the C types and the Atomic<T> types.
thanks,
Signature · 2 lines
John Hubbard NVIDIA
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 20 lines · Boqun Feng
Quoted 19 lines · John Hubbard
Quoted 7 lines · Miguel Ojeda
Quoted 2 lines · Boqun Feng
Does this make sense?
Implementation-wise, if you think it is simpler or more clear/elegant to have the extra lower level layer, then that sounds fine.
Quoted 8 lines
However, I was mainly talking about what we would eventually expose to users, i.e. do we want to provide
Atomic<T>to begin with? If yes, then we could make the lower layer private already.We can defer that extra layer/work if needed even if we go for
Atomic<T>, but it would be nice to understand if we have consensus for an eventual user-facing API, or if someone has any other opinion or concerns on one vs. the other.Well, here's one:
Quoted 8 lines
Quoted 5 lines
The reason that we have things like
atomic64_read()in the C code is because C doesn't have generics.In Rust, we should simply move directly to Atomic<T>, as there are, after all, associated benefits. And it's very easy to see the connection
What are the associated benefits you are referring to? Rust std doesn't use Atomic<T>, that somewhat proves that we don't need it.
Just the stock things that a generic provides: less duplicated code, automatic support for future types (although here it's really just integer types we care about of course).
thanks,
Signature · 2 lines
John Hubbard NVIDIA
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 24 lines · John Hubbard
Quoted 23 lines · Boqun Feng
Quoted 22 lines · John Hubbard
Quoted 7 lines · Miguel Ojeda
Quoted 2 lines · Boqun Feng
Does this make sense?
Implementation-wise, if you think it is simpler or more clear/elegant to have the extra lower level layer, then that sounds fine.
Quoted 8 lines
However, I was mainly talking about what we would eventually expose to users, i.e. do we want to provide
Atomic<T>to begin with? If yes, then we could make the lower layer private already.We can defer that extra layer/work if needed even if we go for
Atomic<T>, but it would be nice to understand if we have consensus for an eventual user-facing API, or if someone has any other opinion or concerns on one vs. the other.Well, here's one:
The reason that we have things like
atomic64_read()in the C code is because C doesn't have generics.
Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
In Rust, we should simply move directly to Atomic<T>, as there are, after all, associated benefits. And it's very easy to see the connection
What are the associated benefits you are referring to? Rust std doesn't use Atomic<T>, that somewhat proves that we don't need it.
Just the stock things that a generic provides: less duplicated code,
It's still a bit handwavy, sorry.
Admittedly, I haven't looked into too much Rust concurrent code, maybe it's even true for C code ;-) So I took a look at the crate that Gary mentioned (the one provides generic atomic APIs):
https://crates.io/crates/atomic
there's a "Dependent" tab where you can see the other crates that depends on it. With a quick look, I haven't found any Rust concurrent project I'm aware of (no crossbeam, no tokio, no futures). On the other hand, there is a non-generic based atomic library:
https://crates.io/crates/portable-atomic
which has more projects depend on it, and there are some Rust concurrent projects that I'm aware of: futures, async-task etc. Note that people can get the non-generic based atomic API from Rust std library, and the "portable-atomic" crate is only 2-year old, while "atomic" crate is 8-year old.
More interestingly, the same author of "atomic" crate, who is an expert in concurrent areas, has another project (there are a lot projects from the author, but this is the one I'm mostly aware of) "parking_lot", which "provides implementations of Mutex, RwLock, Condvar and Once that are smaller, faster and more flexible than those in the Rust standard library, as well as a ReentrantMutex type which supports recursive locking.", and it doesn't use the "atomic" crate either.
These data could mean nothing, there are multiple reasons affecting the popularity of a library. But all the above seems to suggests that you don't really need generic on atomic, at least for a lot of meaningful concurent code.
So if we were to make a decision right now, I don't see that generic atomics are winning. Of course, as I said previously, we can always add them if we have learned more and have the consensus.
(Don't make me wrong, I love generic in general, I just want to avoid the "I have a generic hammer and everything looks like generic nails" situation.)
Regards,
Boqun
Quoted 9 lines
automatic support for future types (although here it's really just integer types we care about of course).
thanks,
Signature · 2 lines
John Hubbard NVIDIA
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 61 lines · Boqun Feng
Quoted 24 lines · John Hubbard
Quoted 23 lines · Boqun Feng
Quoted 22 lines · John Hubbard
Quoted 7 lines · Miguel Ojeda
Quoted 2 lines · Boqun Feng
Does this make sense?
Implementation-wise, if you think it is simpler or more clear/elegant to have the extra lower level layer, then that sounds fine.
Quoted 8 lines
However, I was mainly talking about what we would eventually expose to users, i.e. do we want to provide
Atomic<T>to begin with? If yes, then we could make the lower layer private already.We can defer that extra layer/work if needed even if we go for
Atomic<T>, but it would be nice to understand if we have consensus for an eventual user-facing API, or if someone has any other opinion or concerns on one vs. the other.Well, here's one:
The reason that we have things like
atomic64_read()in the C code is because C doesn't have generics.Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
In Rust, we should simply move directly to Atomic<T>, as there are, after all, associated benefits. And it's very easy to see the connection
What are the associated benefits you are referring to? Rust std doesn't use Atomic<T>, that somewhat proves that we don't need it.
Just the stock things that a generic provides: less duplicated code,
It's still a bit handwavy, sorry.
Admittedly, I haven't looked into too much Rust concurrent code, maybe it's even true for C code ;-) So I took a look at the crate that Gary mentioned (the one provides generic atomic APIs):
https://crates.io/crates/atomic
there's a "Dependent" tab where you can see the other crates that depends on it. With a quick look, I haven't found any Rust concurrent project I'm aware of (no crossbeam, no tokio, no futures). On the other hand, there is a non-generic based atomic library:
https://crates.io/crates/portable-atomic
which has more projects depend on it, and there are some Rust concurrent projects that I'm aware of: futures, async-task etc. Note that people can get the non-generic based atomic API from Rust std library, and the "portable-atomic" crate is only 2-year old, while "atomic" crate is 8-year old.
More interestingly, the same author of "atomic" crate, who is an expert in concurrent areas, has another project (there are a lot projects from the author, but this is the one I'm mostly aware of) "
parking_lot", which "provides implementations of Mutex, RwLock, Condvar and Once that are smaller, faster and more flexible than those in the Rust standard library, as well as a ReentrantMutex type which supports recursive locking.", and it doesn't use the "atomic" crate either.
Quoted 8 lines
These data could mean nothing, there are multiple reasons affecting the popularity of a library. But all the above seems to suggests that you don't really need generic on atomic, at least for a lot of meaningful concurent code.
So if we were to make a decision right now, I don't see that generic atomics are winning. Of course, as I said previously, we can always add
That does seem to be the case: the non-generic flavor looks more popular so far.
Quoted 1 line
them if we have learned more and have the consensus.
Yes, I suppose waiting might be better. I expected the Atomic<T> to be more popular than it actually is...
thanks,
Signature · 2 lines
John Hubbard NVIDIA
Gary Guo
maintainerRe: [RFC 2/2] rust: sync: Add atomic support
In reply to Boqun FengQuoted 53 lines · On Fri, 14 Jun 2024 19:39:27 -0700 Boqun Feng <[email protected]> wrote:
Quoted 24 lines · John Hubbard
Quoted 23 lines · Boqun Feng
Quoted 22 lines · John Hubbard
Quoted 7 lines · Miguel Ojeda
Quoted 2 lines · Boqun Feng
Does this make sense?
Implementation-wise, if you think it is simpler or more clear/elegant to have the extra lower level layer, then that sounds fine.
Quoted 8 lines
However, I was mainly talking about what we would eventually expose to users, i.e. do we want to provide
Atomic<T>to begin with? If yes, then we could make the lower layer private already.We can defer that extra layer/work if needed even if we go for
Atomic<T>, but it would be nice to understand if we have consensus for an eventual user-facing API, or if someone has any other opinion or concerns on one vs. the other.Well, here's one:
The reason that we have things like
atomic64_read()in the C code is because C doesn't have generics.Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
In Rust, we should simply move directly to Atomic<T>, as there are, after all, associated benefits. And it's very easy to see the connection
What are the associated benefits you are referring to? Rust std doesn't use Atomic<T>, that somewhat proves that we don't need it.
Just the stock things that a generic provides: less duplicated code,
It's still a bit handwavy, sorry.
Admittedly, I haven't looked into too much Rust concurrent code, maybe it's even true for C code ;-) So I took a look at the crate that Gary mentioned (the one provides generic atomic APIs):
https://crates.io/crates/atomic
there's a "Dependent" tab where you can see the other crates that depends on it. With a quick look, I haven't found any Rust concurrent project I'm aware of (no crossbeam, no tokio, no futures). On the other hand, there is a non-generic based atomic library:
https://crates.io/crates/portable-atomic
which has more projects depend on it, and there are some Rust concurrent projects that I'm aware of: futures, async-task etc. Note that people can get the non-generic based atomic API from Rust std library, and the "portable-atomic" crate is only 2-year old, while "atomic" crate is 8-year old.
Quoted 7 lines
More interestingly, the same author of "atomic" crate, who is an expert in concurrent areas, has another project (there are a lot projects from the author, but this is the one I'm mostly aware of) "
parking_lot", which "provides implementations of Mutex, RwLock, Condvar and Once that are smaller, faster and more flexible than those in the Rust standard library, as well as a ReentrantMutex type which supports recursive locking.", and it doesn't use the "atomic" crate either.
Note that crossbeam's AtomicCell is also generic, and crossbeam is used by tons of crates. As Miguel mentioned, I think it's very likely that in the future we want be able to do atomics on new types (e.g. for seqlocks perhaps). We probably don't need the non-lock-free fallback of crossbeam's AtomicCell, but the lock-free subset with newtype support is desirable.
People in general don't use the atomic crate because it provides no additional feature compared to the standard library. But it doesn't really mean that the standard library's atomic design is good.
People decided to use AtomicT and NonZeroT instead of Atomic<T> or NonZero<T> long time ago, but many now thinks the decision was bad. Introduction of NonZero<T> is a good example of it. NonZeroT are now all type aliases of NonZero<T>.
I also don't see any downside in using generics. We can provide type aliases so people can use AtomicI32 and AtomicI64 when they want their code to be compatible with userspace Rust can still do so.
Atomic<i32> is also just aesthetically better than AtomicI32 IMO. When all other types look like NonZero<i32>, Wrapping<i32>, I don't think we should have AtomicI32 just because "it's done this way in Rust std". Our alloc already deviates a lot from Rust std.
Best,
Gary
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 64 lines · Gary Guo
Quoted 53 lines · On Fri, 14 Jun 2024 19:39:27 -0700 Boqun Feng <[email protected]> wrote:
Quoted 24 lines · John Hubbard
Quoted 23 lines · Boqun Feng
Quoted 22 lines · John Hubbard
Quoted 7 lines · Miguel Ojeda
Quoted 2 lines · Boqun Feng
Does this make sense?
Implementation-wise, if you think it is simpler or more clear/elegant to have the extra lower level layer, then that sounds fine.
Quoted 8 lines
However, I was mainly talking about what we would eventually expose to users, i.e. do we want to provide
Atomic<T>to begin with? If yes, then we could make the lower layer private already.We can defer that extra layer/work if needed even if we go for
Atomic<T>, but it would be nice to understand if we have consensus for an eventual user-facing API, or if someone has any other opinion or concerns on one vs. the other.Well, here's one:
The reason that we have things like
atomic64_read()in the C code is because C doesn't have generics.Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
In Rust, we should simply move directly to Atomic<T>, as there are, after all, associated benefits. And it's very easy to see the connection
What are the associated benefits you are referring to? Rust std doesn't use Atomic<T>, that somewhat proves that we don't need it.
Just the stock things that a generic provides: less duplicated code,
It's still a bit handwavy, sorry.
Admittedly, I haven't looked into too much Rust concurrent code, maybe it's even true for C code ;-) So I took a look at the crate that Gary mentioned (the one provides generic atomic APIs):
https://crates.io/crates/atomic
there's a "Dependent" tab where you can see the other crates that depends on it. With a quick look, I haven't found any Rust concurrent project I'm aware of (no crossbeam, no tokio, no futures). On the other hand, there is a non-generic based atomic library:
https://crates.io/crates/portable-atomic
which has more projects depend on it, and there are some Rust concurrent projects that I'm aware of: futures, async-task etc. Note that people can get the non-generic based atomic API from Rust std library, and the "portable-atomic" crate is only 2-year old, while "atomic" crate is 8-year old.
Quoted 7 lines
More interestingly, the same author of "atomic" crate, who is an expert in concurrent areas, has another project (there are a lot projects from the author, but this is the one I'm mostly aware of) "
parking_lot", which "provides implementations of Mutex, RwLock, Condvar and Once that are smaller, faster and more flexible than those in the Rust standard library, as well as a ReentrantMutex type which supports recursive locking.", and it doesn't use the "atomic" crate either.
Quoted 4 lines
Note that crossbeam's AtomicCell is also generic, and crossbeam is used by tons of crates. As Miguel mentioned, I think it's very likely that in the future we want be able to do atomics on new types (e.g. for seqlocks perhaps). We probably don't need the non-lock-free fallback of
Good, another design bit, thank you!
What's our overall idea on sub-word types, like Atomic<u8> and Atomic<u16>, do we plan to say no to them, or they could have a limited APIs? IIUC, some operations on them are relatively sub-optimal on some architectures, supporting the same set of API as i32 and i64 is probably a bad idea.
Another thing in my mind is making Atomic<T>
pub struct Atomic<T: Send + ...> { ... }so that Atomic<T> will always be Sync, because quite frankly, an atomic type that cannot Sync is pointless.
Regards,
Boqun
Quoted 23 lines
crossbeam's AtomicCell, but the lock-free subset with newtype support is desirable.
People in general don't use the
atomiccrate because it provides no additional feature compared to the standard library. But it doesn't really mean that the standard library's atomic design is good.People decided to use AtomicT and NonZeroT instead of Atomic<T> or NonZero<T> long time ago, but many now thinks the decision was bad. Introduction of NonZero<T> is a good example of it. NonZeroT are now all type aliases of NonZero<T>.
I also don't see any downside in using generics. We can provide type aliases so people can use
AtomicI32andAtomicI64when they want their code to be compatible with userspace Rust can still do so.
Atomic<i32>is also just aesthetically better thanAtomicI32IMO. When all other types look likeNonZero<i32>,Wrapping<i32>, I don't think we should haveAtomicI32just because "it's done this way in Rust std". Our alloc already deviates a lot from Rust std.Best,
Gary
Re: [RFC 2/2] rust: sync: Add atomic support
On Sun, Jun 16, 2024 at 08:06:05AM -0700, Boqun Feng wrote:
[...]
Quoted 14 lines
Quoted 5 lines
Note that crossbeam's AtomicCell is also generic, and crossbeam is used by tons of crates. As Miguel mentioned, I think it's very likely that in the future we want be able to do atomics on new types (e.g. for seqlocks perhaps). We probably don't need the non-lock-free fallback of
Good, another design bit, thank you!
What's our overall idea on sub-word types, like Atomic<u8> and Atomic<u16>, do we plan to say no to them, or they could have a limited APIs? IIUC, some operations on them are relatively sub-optimal on some architectures, supporting the same set of API as i32 and i64 is probably a bad idea.
Quoted 7 lines
Another thing in my mind is making
Atomic<T>pub struct Atomic<T: Send + ...> { ... }so that
Atomic<T>will always beSync, because quite frankly, an atomic type that cannotSyncis pointless.
Also, how do we avoid this issue [1] in kernel?
atomic_load() in C is implemented as READ_ONCE() and it's, at most time, a volatile read, so the eventual code is:
let a: (u8, u16) = (1, 2);
let b = unsafe { core::ptr::read_volatile::<i32>(&a as *const _ as *const i32) };I know we probably ignore data race here and treat read_volatile as a dependency read per LKMM [2]. But this is an using of uninitialized data, so it's a bit different.
We can do what https://crates.io/crates/atomic does:
pub struct Atomic<T: NoUninit + ..> { ... }, where NoUinit means no internal padding bytes, but it loses the ability to put a
#[repr(u32)]
pub enum Foo { .. }into Atomic<T>, right? Which is probably a case you want to support?
Regards,
Boqun
[1]: https://github.com/crossbeam-rs/crossbeam/issues/748#issuecomment-1133926617
[2]: tools/memory-model/Documentation/access-marking.txt
Quoted 26 lines
Regards,
BoqunQuoted 23 lines
crossbeam's AtomicCell, but the lock-free subset with newtype support is desirable.
People in general don't use the
atomiccrate because it provides no additional feature compared to the standard library. But it doesn't really mean that the standard library's atomic design is good.People decided to use AtomicT and NonZeroT instead of Atomic<T> or NonZero<T> long time ago, but many now thinks the decision was bad. Introduction of NonZero<T> is a good example of it. NonZeroT are now all type aliases of NonZero<T>.
I also don't see any downside in using generics. We can provide type aliases so people can use
AtomicI32andAtomicI64when they want their code to be compatible with userspace Rust can still do so.
Atomic<i32>is also just aesthetically better thanAtomicI32IMO. When all other types look likeNonZero<i32>,Wrapping<i32>, I don't think we should haveAtomicI32just because "it's done this way in Rust std". Our alloc already deviates a lot from Rust std.Best,
Gary
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 24 lines · Boqun Feng
On Sun, Jun 16, 2024 at 08:06:05AM -0700, Boqun Feng wrote:
[...]Quoted 14 lines
Quoted 5 lines
Note that crossbeam's AtomicCell is also generic, and crossbeam is used by tons of crates. As Miguel mentioned, I think it's very likely that in the future we want be able to do atomics on new types (e.g. for seqlocks perhaps). We probably don't need the non-lock-free fallback of
Good, another design bit, thank you!
What's our overall idea on sub-word types, like Atomic<u8> and Atomic<u16>, do we plan to say no to them, or they could have a limited APIs? IIUC, some operations on them are relatively sub-optimal on some architectures, supporting the same set of API as i32 and i64 is probably a bad idea.
Quoted 7 lines
Another thing in my mind is making
Atomic<T>pub struct Atomic<T: Send + ...> { ... }so that
Atomic<T>will always beSync, because quite frankly, an atomic type that cannotSyncis pointless.
Quoted 8 lines
Also, how do we avoid this issue [1] in kernel?
atomic_load()in C is implemented asREAD_ONCE()and it's, at most time, a volatile read, so the eventual code is:let a: (u8, u16) = (1, 2); let b = unsafe { core::ptr::read_volatile::<i32>(&a as *const _ as *const i32) };
^^^^ this line should really be:
let b: (u8, u16) = unsafe { transmute_copy(&read_volatile::<i32>(&a as *const _ as *const i32)) };but you get the idea.
Regards,
Boqun
Quoted 48 lines
I know we probably ignore data race here and treat
read_volatileas a dependency read per LKMM [2]. But this is an using of uninitialized data, so it's a bit different.We can do what https://crates.io/crates/atomic does:
pub struct Atomic<T: NoUninit + ..> { ... }, where
NoUinitmeans no internal padding bytes, but it loses the ability to put a#[repr(u32)] pub enum Foo { .. }into
Atomic<T>, right? Which is probably a case you want to support?Regards,
Boqun[1]: https://github.com/crossbeam-rs/crossbeam/issues/748#issuecomment-1133926617
[2]:tools/memory-model/Documentation/access-marking.txtQuoted 26 lines
Regards,
BoqunQuoted 23 lines
crossbeam's AtomicCell, but the lock-free subset with newtype support is desirable.
People in general don't use the
atomiccrate because it provides no additional feature compared to the standard library. But it doesn't really mean that the standard library's atomic design is good.People decided to use AtomicT and NonZeroT instead of Atomic<T> or NonZero<T> long time ago, but many now thinks the decision was bad. Introduction of NonZero<T> is a good example of it. NonZeroT are now all type aliases of NonZero<T>.
I also don't see any downside in using generics. We can provide type aliases so people can use
AtomicI32andAtomicI64when they want their code to be compatible with userspace Rust can still do so.
Atomic<i32>is also just aesthetically better thanAtomicI32IMO. When all other types look likeNonZero<i32>,Wrapping<i32>, I don't think we should haveAtomicI32just because "it's done this way in Rust std". Our alloc already deviates a lot from Rust std.Best,
Gary
Re: [RFC 2/2] rust: sync: Add atomic support
In reply to Boqun FengQuoted 22 lines · Boqun Feng
On Sun, Jun 16, 2024 at 08:06:05AM -0700, Boqun Feng wrote:
[...]Quoted 20 lines
Quoted 5 lines
Note that crossbeam's AtomicCell is also generic, and crossbeam is used by tons of crates. As Miguel mentioned, I think it's very likely that in the future we want be able to do atomics on new types (e.g. for seqlocks perhaps). We probably don't need the non-lock-free fallback of
Good, another design bit, thank you!
What's our overall idea on sub-word types, like Atomic<u8> and Atomic<u16>, do we plan to say no to them, or they could have a limited APIs? IIUC, some operations on them are relatively sub-optimal on some architectures, supporting the same set of API as i32 and i64 is probably a bad idea.
Another thing in my mind is making
Atomic<T>pub struct Atomic<T: Send + ...> { ... }so that
Atomic<T>will always beSync, because quite frankly, an atomic type that cannotSyncis pointless.
That is true, but adding semantically "unnecessary" bounds can be bad. This is because they infect everything that wants to use Atomic<T>, since they also need to add that bound.
Quoted 1 line
Also, how do we avoid this issue [1] in kernel?
I think that we can first go the way of my second approach (ie adding a private trait as a bound on Atomic<T> to prevent generic usage). And only allow primitives.
If we then see that people would like to put their own (u8, u16) tuple structs into Atomic<T>, we have multiple options:
1. Field projection:
Only primitives can be loaded and storeed, to access the values of the tuple, one would need to project to each field and read them.
2. Disallow padding:
We add an unsafe trait that asserts there are no padding bytes in there (like NoUinit from below) and also add a macro that implements the trait safely.
3. Use MaybeUninit under the hood:
I don't know if this would fix the issue entirely, since that is what crossbeam currently uses (but the issue remains open).
But I don't think that we should encourage large structs to be put into Atomic<T>, since that would be bad for perf, right? So I think that going the way of 1 would be great (if we had FP, otherwise 2 seems fine).
Quoted 9 lines
atomic_load()in C is implemented asREAD_ONCE()and it's, at most time, a volatile read, so the eventual code is:let a: (u8, u16) = (1, 2); let b = unsafe { core::ptr::read_volatile::<i32>(&a as *const _ as *const i32) };I know we probably ignore data race here and treat
read_volatileas a dependency read per LKMM [2]. But this is an using of uninitialized data, so it's a bit different.
But would we implement it this way? Or would it go through a C function? If we entirely do it in Rust, then yes this is bad.
---
Cheers,
Benno
Quoted 17 lines
We can do what https://crates.io/crates/atomic does:
pub struct Atomic<T: NoUninit + ..> { ... }, where
NoUinitmeans no internal padding bytes, but it loses the ability to put a#[repr(u32)] pub enum Foo { .. }into
Atomic<T>, right? Which is probably a case you want to support?Regards,
Boqun[1]: https://github.com/crossbeam-rs/crossbeam/issues/748#issuecomment-1133926617
[2]:tools/memory-model/Documentation/access-marking.txt
Re: [RFC 2/2] rust: sync: Add atomic support
In reply to Miguel OjedaQuoted 7 lines · Miguel Ojeda
Quoted 2 lines · Boqun Feng
Does this make sense?
Implementation-wise, if you think it is simpler or more clear/elegant to have the extra lower level layer, then that sounds fine.
Quoted 8 lines
However, I was mainly talking about what we would eventually expose to users, i.e. do we want to provide
Atomic<T>to begin with? If yes, then we could make the lower layer private already.We can defer that extra layer/work if needed even if we go for
Atomic<T>, but it would be nice to understand if we have consensus for an eventual user-facing API, or if someone has any other opinion or concerns on one vs. the other.
Since this is fully compatible to LKMM atomic operations, is there a use case for C and Rust operating on the same atomic value? And then you will need to specify the size, or odd things are likely to happen if they disagree on size. With Atomic<T> can we easily say what type the underlying implementation uses?
Andrew
Peter Zijlstra
maintainerRe: [RFC 2/2] rust: sync: Add atomic support
In reply to Boqun FengQuoted 17 lines · Boqun Feng
We can always add a layer on top of what we have here to provide the generic
Atomic<T>. However, I personally don't think genericAtomic<T>is a good idea, for a few reasons:* I'm not sure it will bring benefits to users, the current atomic
users in kernel are pretty specific on the size of atomic they use, so they want to directly use AtomicI32 or AtomicI64 in their type definitions rather than use aAtomic<T>where their users can provide type later.* I can also see the future where we have different APIs on
different types of atomics, for example, we could have a:impl AtomicI64 {
pub fnsplit(&self)-> (&AtomicI32, &AtomicI32)
}
Quoted 6 lines
which doesn't exist for AtomicI32. Note this is not a UB because we write our atomic implementation in asm, so it's perfectly fine for mix-sized atomics.
So let's start with some basic and simple until we really have a need for generic
Atomic<T>. Thoughts?
Not on the generic thing, but on the lack of long. atomic_long_t is often used when we have pointers with extra bits on. Then you want a number type in order to be able to manipulate the low bits.
Re: [RFC 2/2] rust: sync: Add atomic support
Quoted 23 lines · Peter Zijlstra
Quoted 21 lines · Boqun Feng
We can always add a layer on top of what we have here to provide the generic
Atomic<T>. However, I personally don't think genericAtomic<T>is a good idea, for a few reasons:* I'm not sure it will bring benefits to users, the current atomic
users in kernel are pretty specific on the size of atomic they use, so they want to directly use AtomicI32 or AtomicI64 in their type definitions rather than use aAtomic<T>where their users can provide type later.* I can also see the future where we have different APIs on
different types of atomics, for example, we could have a:impl AtomicI64 {
pub fnsplit(&self)-> (&AtomicI32, &AtomicI32)
}which doesn't exist for AtomicI32. Note this is not a UB because we write our atomic implementation in asm, so it's perfectly fine for mix-sized atomics.
Quoted 6 lines
Quoted 2 lines
So let's start with some basic and simple until we really have a need for generic
Atomic<T>. Thoughts?Not on the generic thing, but on the lack of long.
atomic_long_tis often used when we have pointers with extra bits on. Then you want a number type in order to be able to manipulate the low bits.
I mentioned my plan on AtomicPtr, but I think I should have clarified this more. My plan is:
pub struct AtomicIsize {
#[cfg(CONFIG_64BIT)]
inner: AtomicI64
#[cfg(not(CONFIG_64BIT))]
inner: AtomicI32
}i.e. building AtomicIsize (Rust's atomic_long_t) based on AtomicI64 and AtomicI32. And we can a AtomicPtr type on it:
pub struct AtomicPtr<T> {
inner: AtomicIsize,
_type: PhantomData<*mut T>
}
Of course, I need to do some code generating work for AtomicIsize and AtomicPtr, I plan to do that in Rust not in scripts, this will keep the rust/kernel/sync/atomic/impl.rs relatively small (i.e. the Rust/C interface is smaller). I can include this part in the next version, if you want to see it.
Regards,
Boqun
Re: [RFC 2/2] rust: sync: Add atomic support
In reply to Boqun FengOn Wed, Jun 12, 2024 at 03:30:25PM -0700, Boqun Feng wrote:
[...]
arch/arm64/kernel/cpufeature.c static bool · line 1608
Quoted 13 lines
arch/arm64/kernel/cpufeature.c +2
-0@@ -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); }
Yeah, this part was mis-committed, will remove it in the next version.
Regards,
Boqun
[...]
Mark Rutland
reviewerRe: [RFC 2/2] rust: sync: Add atomic support
In reply to Boqun FengQuoted 6 lines · Boqun Feng
Provide two atomic types: AtomicI32 and AtomicI64 with the existing implemenation of C atomics. These atomics have the same semantics of the corresponding LKMM C atomics, and using one memory (ordering) model certainly reduces the reasoning difficulty and potential bugs from the interaction of two different memory models.
Quoted 8 lines
Also bump my role to the maintainer of ATOMIC INFRASTRUCTURE to reflect my responsiblity on these Rust APIs.
Note that
Atomic*::new()s are implemented vi open coding on struct atomic*_t. This allowsnew()being aconstfunction, so that it can be used in constant contexts.Signed-off-by Boqun Feng <[email protected]>
I have a few minor comments below.
Quoted 40 lines
--- MAINTAINERS | 4 +- arch/arm64/kernel/cpufeature.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 | 1 + scripts/atomic/gen-rust-atomic.sh | 136 +++ 7 files changed, 1581 insertions(+), 1 deletion(-) 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.shMAINTAINERS +3
-1@@ -3458,7 +3458,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@@ -3467,6 +3467,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]>arch/arm64/kernel/cpufeature.c +0
-0
arch/arm64/kernel/cpufeature.c static bool · line 1607
Quoted 7 lines
arch/arm64/kernel/cpufeature.c +2
-0@@ -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); }
As per other replies, this'll obviously need to go.
Quoted 6 lines
rust/kernel/sync/atomic.rs new file +0
-0
rust/kernel/sync/atomic.rs line 10
Quoted 10 lines
rust/kernel/sync/atomic.rs new file +10
-0@@ -0,0 +1,63 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Atomic primitives. +//! +//! These primitives have the same semantics as their C counterparts, for precise definitions of +//! the semantics, please refer to tools/memory-model. Note that Linux Kernel Memory (Consistency) +//! Model is the only model for Rust development in kernel right now, please avoid to use Rust's +//! own atomics. + +use crate ::bindings::{atomic64_t, atomic_t};
As with the last patch, why no atomic_long_t?
[...]
Quoted 22 lines
rust/kernel/sync/atomic.rs new file +22
-0+#gen_proto_order_variant(meta, pfx, name, sfx, order, atomic, ty, int, raw, arg...) +gen_proto_order_variant() +{ + local meta="$1"; shift + local pfx="$1"; shift + local name="$1"; shift + local sfx="$1"; shift + local order="$1"; shift + local atomic="$1"; shift + local ty="$1"; shift + local int="$1"; shift + local raw="$1"; shift + + local fn_name="${raw}${pfx}${name}${sfx}${order}" + local atomicname="${raw}${atomic}_${pfx}${name}${sfx}${order}" + + local ret="$(gen_ret_type "${meta}" "${int}")" + local params="$(gen_params "${int}" $@)" + local args="$(gen_args "$@")" + local retstmt="$(gen_ret_stmt "${meta}")" + +cat <<EOF
rust/kernel/sync/atomic.rs line 42
Quoted 10 lines
rust/kernel/sync/atomic.rs new file +10
-0+/// See \`${atomicname}\`. +#[inline(always)] + pub fn ${fn_name}(&self${params}) ${ret}{ +// SAFETY:\`self.0.get()\` is a valid pointer. +unsafe { + ${retstmt}${atomicname}(${args}); + } + } +EOF +}
AFAICT the 'ty' argument (AtomicI32/AtomicI64) isn't used and can be removed.
Likewise for 'raw'.
Quoted 5 lines
rust/kernel/sync/atomic.rs new file +5
-0+ +cat <<EOF +// SPDX-License-Identifier: GPL-2.0 + +//! Generated by $0
rust/kernel/sync/atomic.rs line 57
Quoted 10 lines
rust/kernel/sync/atomic.rs new file +10
-0+//! DO NOT MODIFY THIS FILE DIRECTLY + +use super ::*; +use crate ::bindings::*; + +impl AtomicI32 { +EOF + +grep '^[a-z]' "$1" | while read name meta args; do + gen_proto "${meta}" "${name}" "atomic" "AtomicI32" "i32" "" ${args}
With 'ty' and 'raw' gone, this'd be:
gen_proto "${meta}" "${name}" "atomic" "i32" ${args}rust/kernel/sync/atomic.rs
Quoted 10 lines
rust/kernel/sync/atomic.rs new file +10
-0+done + +cat <<EOF +} + +impl AtomicI64 { +EOF + +grep '^[a-z]' "$1" | while read name meta args; do + gen_proto "${meta}" "${name}" "atomic64" "AtomicI64" "i64" "" ${args}
With 'ty' and 'raw' gone, this'd be:
gen_proto "${meta}" "${name}" "atomic64" "i64" ${args}Mark.
Quoted 9 lines
rust/kernel/sync/atomic.rs new file +6
-0+done + +cat <<EOF +} + +EOF Signature · 1 line
2.45.2
Re: [RFC 2/2] rust: sync: Add atomic support
On Fri, Jun 14, 2024 at 11:40:47AM +0100, Mark Rutland wrote:
[...]
Quoted 27 lines
Quoted 27 lines
scripts/atomic/gen-rust-atomic.sh new file +27
-0+#gen_proto_order_variant(meta, pfx, name, sfx, order, atomic, ty, int, raw, arg...) +gen_proto_order_variant() +{ + local meta="$1"; shift + local pfx="$1"; shift + local name="$1"; shift + local sfx="$1"; shift + local order="$1"; shift + local atomic="$1"; shift + local ty="$1"; shift + local int="$1"; shift + local raw="$1"; shift + + local fn_name="${raw}${pfx}${name}${sfx}${order}" + local atomicname="${raw}${atomic}_${pfx}${name}${sfx}${order}" + + local ret="$(gen_ret_type "${meta}" "${int}")" + local params="$(gen_params "${int}" $@)" + local args="$(gen_args "$@")" + local retstmt="$(gen_ret_stmt "${meta}")" + +cat <<EOF + /// See \`${atomicname}\`. + #[inline(always)] + pub fn ${fn_name}(&self${params}) ${ret}{ + // SAFETY:\`self.0.get()\` is a valid pointer. + unsafe {
Quoted 9 lines
scripts/atomic/gen-rust-atomic.sh line 105
Quoted 5 lines
scripts/atomic/gen-rust-atomic.sh new file +5
-0+ ${retstmt}${atomicname}(${args}); + } + } +EOF +}AFAICT the 'ty' argument (AtomicI32/AtomicI64) isn't used and can be removed.
Good catch.
Quoted 35 lines
Likewise for 'raw'.
Quoted 5 lines
scripts/atomic/gen-rust-atomic.sh new file +5
-0+ +cat << EOF +// SPDX-License-Identifier: GPL-2.0 + +//! Generated by $0scripts/atomic/gen-rust-atomic.sh line 120
Quoted 10 lines
scripts/atomic/gen-rust-atomic.sh new file +10
-0+//! DO NOT MODIFY THIS FILE DIRECTLY + +use super::*; +use crate::bindings::*; + +impl AtomicI32 { +EOF + +grep '^[a-z]' "$1" | while read name meta args; do + gen_proto "${meta}" "${name}" "atomic" "AtomicI32" "i32" "" ${args}With 'ty' and 'raw' gone, this'd be:
gen_proto "${meta}" "${name}" "atomic" "i32" ${args}scripts/atomic/gen-rust-atomic.sh line 130
Quoted 10 lines
scripts/atomic/gen-rust-atomic.sh new file +10
-0+done + +cat << EOF +} + +impl AtomicI64 { +EOF + +grep '^[a-z]' "$1" | while read name meta args; do + gen_proto "${meta}" "${name}" "atomic64" "AtomicI64" "i64" "" ${args}With 'ty' and 'raw' gone, this'd be:
Quoted 2 lines
gen_proto "${meta}" "${name}" "atomic64" "i64" ${args}All fixed locally, thanks!
Regards,
Boqun
Quoted 11 lines
Mark.
Quoted 9 lines
scripts/atomic/gen-rust-atomic.sh new file +6
-0+done + +cat << EOF +} + +EOFSignature · 1 line
2.45.2