[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20231026113610.1425be1b@eugeo>
Date: Thu, 26 Oct 2023 11:36:10 +0100
From: Gary Guo <gary@...yguo.net>
To: Peter Zijlstra <peterz@...radead.org>
Cc: Boqun Feng <boqun.feng@...il.com>, rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org, linux-arch@...r.kernel.org,
llvm@...ts.linux.dev, Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>,
Wedson Almeida Filho <wedsonaf@...il.com>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <benno.lossin@...ton.me>,
Andreas Hindborg <a.hindborg@...sung.com>,
Alice Ryhl <aliceryhl@...gle.com>,
Alan Stern <stern@...land.harvard.edu>,
Andrea Parri <parri.andrea@...il.com>,
Will Deacon <will@...nel.org>,
Nicholas Piggin <npiggin@...il.com>,
David Howells <dhowells@...hat.com>,
Jade Alglave <j.alglave@....ac.uk>,
Luc Maranget <luc.maranget@...ia.fr>,
"Paul E. McKenney" <paulmck@...nel.org>,
Akira Yokosawa <akiyks@...il.com>,
Daniel Lustig <dlustig@...dia.com>,
Joel Fernandes <joel@...lfernandes.org>,
Nathan Chancellor <nathan@...nel.org>,
Nick Desaulniers <ndesaulniers@...gle.com>,
Tom Rix <trix@...hat.com>,
Alexander Viro <viro@...iv.linux.org.uk>,
Christian Brauner <brauner@...nel.org>,
kent.overstreet@...il.com,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
elver@...gle.com, Matthew Wilcox <willy@...radead.org>,
Dave Chinner <david@...morbit.com>,
linux-fsdevel@...r.kernel.org,
Linus Torvalds <torvalds@...ux-foundation.org>
Subject: Re: [RFC] rust: types: Add read_once and write_once
On Thu, 26 Oct 2023 10:13:45 +0200
Peter Zijlstra <peterz@...radead.org> wrote:
> On Wed, Oct 25, 2023 at 12:53:39PM -0700, Boqun Feng wrote:
> > In theory, `read_volatile` and `write_volatile` in Rust can have UB in
> > case of the data races [1]. However, kernel uses volatiles to implement
> > READ_ONCE() and WRITE_ONCE(), and expects races on these marked accesses
> > don't cause UB. And they are proven to have a lot of usages in kernel.
> >
> > To close this gap, `read_once` and `write_once` are introduced, they
> > have the same semantics as `READ_ONCE` and `WRITE_ONCE` especially
> > regarding data races under the assumption that `read_volatile` and
> > `write_volatile` have the same behavior as a volatile pointer in C from
> > a compiler point of view.
> >
> > Longer term solution is to work with Rust language side for a better way
> > to implement `read_once` and `write_once`. But so far, it should be good
> > enough.
>
> So the whole READ_ONCE()/WRITE_ONCE() thing does two things we care
> about (AFAIR):
>
> - single-copy-atomicy; this can also be achieved using the C11
> __atomic_load_n(.memorder=__ATOMIC_RELAXED) /
> __atomic_store_n(.memorder=__ATOMIC_RELAXED) thingies.
>
> - the ONCE thing; that is inhibits re-materialization, and here I'm not
> sure C11 atomics help, they might since re-reading an atomic is
> definitely dodgy -- after all it could've changed.
>
> Now, traditionally we've relied on the whole volatile thing simply
> because there was no C11, or our oldest compiler didn't do C11. But
> these days we actually *could*.
>
> Now, obviously C11 has issues vs LKMM, but perhaps the load/store
> semantics are near enough to be useful. (IIRC this also came up in the
> *very* long x86/percpu thread)
>
> So is there any distinction between the volatile load/store and the C11
> atomic load/store that we care about and could not Rust use the atomic
> load/store to avoid their UB ?
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.
* 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.
* Atomics doesn't work for complex structs. Although I am not quite sure
of the value of supporting it.
Powered by blists - more mailing lists