linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Gary Guo <gary@garyguo.net>
To: Peter Zijlstra <peterz@infradead.org>
Cc: "Boqun Feng" <boqun.feng@gmail.com>,
	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 11:36:10 +0100	[thread overview]
Message-ID: <20231026113610.1425be1b@eugeo> (raw)
In-Reply-To: <20231026081345.GJ31411@noisy.programming.kicks-ass.net>

On Thu, 26 Oct 2023 10:13:45 +0200
Peter Zijlstra <peterz@infradead.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.


  reply	other threads:[~2023-10-26 10:36 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-25 19:53 [RFC] rust: types: Add read_once and write_once 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 [this message]
2023-10-26 11:16     ` Peter Zijlstra
2023-10-26 14:21       ` Boqun Feng
2023-10-26 14:23         ` Boqun Feng
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=20231026113610.1425be1b@eugeo \
    --to=gary@garyguo.net \
    --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=boqun.feng@gmail.com \
    --cc=brauner@kernel.org \
    --cc=david@fromorbit.com \
    --cc=dhowells@redhat.com \
    --cc=dlustig@nvidia.com \
    --cc=elver@google.com \
    --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;
as well as URLs for NNTP newsgroup(s).