From: Boqun Feng <boqun.feng@gmail.com>
To: Peter Zijlstra <peterz@infradead.org>
Cc: "Gary Guo" <gary@garyguo.net>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-arch@vger.kernel.org, llvm@lists.linux.dev,
"Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Wedson Almeida Filho" <wedsonaf@gmail.com>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Andreas Hindborg" <a.hindborg@samsung.com>,
"Alice Ryhl" <aliceryhl@google.com>,
"Alan Stern" <stern@rowland.harvard.edu>,
"Andrea Parri" <parri.andrea@gmail.com>,
"Will Deacon" <will@kernel.org>,
"Nicholas Piggin" <npiggin@gmail.com>,
"David Howells" <dhowells@redhat.com>,
"Jade Alglave" <j.alglave@ucl.ac.uk>,
"Luc Maranget" <luc.maranget@inria.fr>,
"Paul E. McKenney" <paulmck@kernel.org>,
"Akira Yokosawa" <akiyks@gmail.com>,
"Daniel Lustig" <dlustig@nvidia.com>,
"Joel Fernandes" <joel@joelfernandes.org>,
"Nathan Chancellor" <nathan@kernel.org>,
"Nick Desaulniers" <ndesaulniers@google.com>,
"Tom Rix" <trix@redhat.com>,
"Alexander Viro" <viro@zeniv.linux.org.uk>,
"Christian Brauner" <brauner@kernel.org>,
kent.overstreet@gmail.com,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
elver@google.com, "Matthew Wilcox" <willy@infradead.org>,
"Dave Chinner" <david@fromorbit.com>,
linux-fsdevel@vger.kernel.org,
"Linus Torvalds" <torvalds@linux-foundation.org>
Subject: Re: [RFC] rust: types: Add read_once and write_once
Date: Thu, 26 Oct 2023 07:23:21 -0700 [thread overview]
Message-ID: <ZTp2WVlMxqmlC4Rb@Boquns-Mac-mini.home> (raw)
In-Reply-To: <ZTp2AVY5twZOKEtX@Boquns-Mac-mini.home>
On Thu, Oct 26, 2023 at 07:21:53AM -0700, Boqun Feng wrote:
> On Thu, Oct 26, 2023 at 01:16:25PM +0200, Peter Zijlstra wrote:
> > On Thu, Oct 26, 2023 at 11:36:10AM +0100, Gary Guo wrote:
> >
> > > There's two reasons that we are using volatile read/write as opposed to
> > > relaxed atomic:
> > > * Rust lacks volatile atomics at the moment. Non-volatile atomics are
> > > not sufficient because the compiler is allowed (although they
> > > currently don't) optimise atomics. If you have two adjacent relaxed
> > > loads, they could be merged into one.
> >
> > Ah yes, that would be problematic, eg, if lifted out of a loop things
> > could go sideways fast.
> >
>
> Maybe we can workaround this limitation by using compiler barriers, i.e.
>
> compiler_fence(SeqCst);
> load(Relaxed);
> compiler_fence(Acquire);
>
> this is slightly stronger than a volatile atomic.
>
> > > * Atomics only works for integer types determined by the platform. On
> > > some 32-bit platforms you wouldn't be able to use 64-bit atomics at
> > > all, and on x86 you get less optimal sequence since volatile load is
> > > permitted to tear while atomic load needs to use LOCK CMPXCHG8B.
> >
> > We only grudgingly allowed u64 READ_ONCE() on 32bit platforms because
> > the fallout was too numerous to fix. Some of them are probably bugs.
> >
> > Also, I think cmpxchg8b without lock prefix would be sufficient, but
> > I've got too much of a head-ache to be sure. Worse is that we still
> > support targets without cmpxchg8b.
> >
> > It might be interesting to make the Rust side more strict in this regard
> > and see where/when we run into trouble.
> >
>
> Sounds good to me. If the compiler barriers make sense for now, then
> we can do:
>
> pub unsafe fn read_once_usize(ptr: *const usize) -> usize {
> core::sync::atomic::compiler_fence(SeqCst);
> let r = unsafe { *ptr.cast::<AtomicUsize>() }.load(Relaxed);
> core::sync::atomic::compiler_fence(Acquire);
> r
> }
>
I forgot to mention, this can also resolve the comments from Marco, i.e.
switching implemention to Acquire if ARM64 & LTO.
Regards,
Boqun
> and if the other side (i.e. write) is also atomic (e.g. WRITE_ONCE()),
> we don't have data race.
>
> However, there are still cases where data races are ignored in C code,
> for example inode::i_state: reads out of locks race with writes inside
> locks, since writes are done by plain accesses. Nothing can be done to
> fix that from Rust side only, and fixing the C side is a separate topic.
>
> Thoughts?
>
> Regards,
> Boqun
>
> > > * Atomics doesn't work for complex structs. Although I am not quite sure
> > > of the value of supporting it.
> >
> > So on the C side we mandate the size is no larger than machine word,
> > with the exception of the u64 on 32bit thing. We don't mandate strict
> > integer types because things like pte_t are wrapper types.
> >
> >
next prev parent reply other threads:[~2023-10-26 14:23 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-25 19:53 Boqun Feng
2023-10-25 21:51 ` Benno Lossin
2023-10-25 23:02 ` Boqun Feng
2023-10-26 3:31 ` Boqun Feng
2023-10-26 7:30 ` Benno Lossin
2023-10-30 13:58 ` Alice Ryhl
2023-10-30 16:36 ` Benno Lossin
2023-10-26 8:13 ` Peter Zijlstra
2023-10-26 10:36 ` Gary Guo
2023-10-26 11:16 ` Peter Zijlstra
2023-10-26 14:21 ` Boqun Feng
2023-10-26 14:23 ` Boqun Feng [this message]
2023-10-26 17:08 ` Paul E. McKenney
2023-10-26 11:16 ` Marco Elver
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZTp2WVlMxqmlC4Rb@Boquns-Mac-mini.home \
--to=boqun.feng@gmail.com \
--cc=a.hindborg@samsung.com \
--cc=akiyks@gmail.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=brauner@kernel.org \
--cc=david@fromorbit.com \
--cc=dhowells@redhat.com \
--cc=dlustig@nvidia.com \
--cc=elver@google.com \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=j.alglave@ucl.ac.uk \
--cc=joel@joelfernandes.org \
--cc=kent.overstreet@gmail.com \
--cc=linux-arch@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=llvm@lists.linux.dev \
--cc=luc.maranget@inria.fr \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=npiggin@gmail.com \
--cc=ojeda@kernel.org \
--cc=parri.andrea@gmail.com \
--cc=paulmck@kernel.org \
--cc=peterz@infradead.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=stern@rowland.harvard.edu \
--cc=torvalds@linux-foundation.org \
--cc=trix@redhat.com \
--cc=viro@zeniv.linux.org.uk \
--cc=wedsonaf@gmail.com \
--cc=will@kernel.org \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®