rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: Alice Ryhl <alice@ryhl.io>
Cc: "Alice Ryhl" <aliceryhl@google.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Wedson Almeida Filho" <wedsonaf@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Andreas Hindborg" <a.hindborg@samsung.com>,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Alexander Viro" <viro@zeniv.linux.org.uk>,
	"Christian Brauner" <brauner@kernel.org>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Arve Hjønnevåg" <arve@android.com>,
	"Todd Kjos" <tkjos@android.com>,
	"Martijn Coenen" <maco@android.com>,
	"Joel Fernandes" <joel@joelfernandes.org>,
	"Carlos Llamas" <cmllamas@google.com>,
	"Suren Baghdasaryan" <surenb@google.com>,
	"Dan Williams" <dan.j.williams@intel.com>,
	"Matthew Wilcox" <willy@infradead.org>,
	"Thomas Gleixner" <tglx@linutronix.de>,
	"Daniel Xu" <dxu@dxuuu.xyz>,
	"Martin Rodriguez Reboredo" <yakoyoku@gmail.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
	linux-fsdevel@vger.kernel.org, "Kees Cook" <kees@kernel.org>
Subject: Re: [PATCH v8 3/8] rust: file: add Rust abstraction for `struct file`
Date: Thu, 8 Aug 2024 09:04:26 -0700	[thread overview]
Message-ID: <ZrTsiiRIRSNyttRz@boqun-archlinux> (raw)
In-Reply-To: <51199e48-fd36-4669-a93a-97e5c10aea26@ryhl.io>

On Wed, Aug 07, 2024 at 11:59:47PM +0200, Alice Ryhl wrote:
> On 8/7/24 4:46 PM, Boqun Feng wrote:
> > On Wed, Aug 07, 2024 at 10:50:32AM +0200, Alice Ryhl wrote:
> > > On Tue, Aug 6, 2024 at 9:30 PM Boqun Feng <boqun.feng@gmail.com> wrote:
> > > > 
> > > > On Tue, Aug 06, 2024 at 10:48:11AM +0200, Alice Ryhl wrote:
> > > > [...]
> > > > > > > +    /// Returns the flags associated with the file.
> > > > > > > +    ///
> > > > > > > +    /// The flags are a combination of the constants in [`flags`].
> > > > > > > +    #[inline]
> > > > > > > +    pub fn flags(&self) -> u32 {
> > > > > > > +        // This `read_volatile` is intended to correspond to a READ_ONCE call.
> > > > > > > +        //
> > > > > > > +        // SAFETY: The file is valid because the shared reference guarantees a nonzero refcount.
> > > > > > > +        //
> > > > > > > +        // FIXME(read_once): Replace with `read_once` when available on the Rust side.
> > > > > > 
> > > > > > Do you know the status of this?
> > > > > 
> > > > > It's still unavailable.
> > > > > 
> > > > 
> > > > I think with our own Atomic API, we can just use atomic_read() here:
> > > > yes, I know that to make this is not a UB, we need the C side to also do
> > > > atomic write on this `f_flags`, however, my reading of C code seems to
> > > > suggest that FS relies on writes to this field is atomic, therefore
> > > > unless someone is willing to convert all writes to `f_flags` in C into
> > > > a WRITE_ONCE(), nothing more we can do on Rust side. So using
> > > > atomic_read() is the correct thing to begin with.
> > > 
> > > Huh? The C side uses atomic reads for this?
> > > 
> > 
> > Well, READ_ONCE(->f_flags) is atomic, so I thought you want to use
> > atomic here. However, after a quick look of `->f_flags` accesses, I find
> > out they should be protected by `->f_lock` (a few cases rely on
> > data race accesses, see p4_fd_open()), so I think what you should really
> > do here is the similar: make sure Rust code only accesses `->f_flags`
> > if `->f_lock` is held. Unless that's not the case for binder?
> 
> 
> Binder just has an `if (filp->f_flags & O_NONBLOCK)` block somewhere in the
> ioctl, where filp is the `struct file *` passed to the ioctl. Binder doesn't
> take the lock.
> 

Yep, that's my point, I think binder C driver relies on the behaviors of
data race today (or probably all `->f_flags`s accessed by binder don't
have any concurrent write to them). Either way, what you do here is
better than C code if there was a data race. I was simply suggesting
instead of `read_once`, we could just do a `atomic_read` on `->f_flags`
once we support *unsafe` usage of doing atomic accesses on normal data
fields (of course, such a usage will be limited).

In other words, nothing needs to be changed here right now.

Regards,
Boqun

> Alice

  reply	other threads:[~2024-08-08 16:05 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-25 14:27 [PATCH v8 0/8] File abstractions needed by Rust Binder Alice Ryhl
2024-07-25 14:27 ` [PATCH v8 1/8] rust: types: add `NotThreadSafe` Alice Ryhl
2024-07-25 14:37   ` Peter Zijlstra
2024-07-25 15:09     ` Alice Ryhl
2024-07-25 15:30       ` Peter Zijlstra
2024-07-25 15:32         ` Alice Ryhl
2024-08-07 10:59   ` Gary Guo
2024-07-25 14:27 ` [PATCH v8 2/8] rust: task: add `Task::current_raw` Alice Ryhl
2024-08-07 14:41   ` Gary Guo
2024-07-25 14:27 ` [PATCH v8 3/8] rust: file: add Rust abstraction for `struct file` Alice Ryhl
2024-08-06  8:44   ` Benno Lossin
2024-08-06  8:48     ` Alice Ryhl
2024-08-06 19:29       ` Boqun Feng
2024-08-07  8:50         ` Alice Ryhl
2024-08-07 14:46           ` Boqun Feng
2024-08-07 21:59             ` Alice Ryhl
2024-08-08 16:04               ` Boqun Feng [this message]
2024-07-25 14:27 ` [PATCH v8 4/8] rust: cred: add Rust abstraction for `struct cred` Alice Ryhl
2024-08-07 14:53   ` Gary Guo
2024-07-25 14:27 ` [PATCH v8 5/8] rust: security: add abstraction for secctx Alice Ryhl
2024-08-07 14:57   ` Gary Guo
2024-07-25 14:27 ` [PATCH v8 6/8] rust: file: add `FileDescriptorReservation` Alice Ryhl
2024-08-07 15:04   ` Gary Guo
2024-07-25 14:27 ` [PATCH v8 7/8] rust: file: add `Kuid` wrapper Alice Ryhl
2024-07-25 14:27 ` [PATCH v8 8/8] rust: file: add abstraction for `poll_table` Alice Ryhl

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=ZrTsiiRIRSNyttRz@boqun-archlinux \
    --to=boqun.feng@gmail.com \
    --cc=a.hindborg@samsung.com \
    --cc=alex.gaynor@gmail.com \
    --cc=alice@ryhl.io \
    --cc=aliceryhl@google.com \
    --cc=arve@android.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=brauner@kernel.org \
    --cc=cmllamas@google.com \
    --cc=dan.j.williams@intel.com \
    --cc=dxu@dxuuu.xyz \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=joel@joelfernandes.org \
    --cc=kees@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maco@android.com \
    --cc=ojeda@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=surenb@google.com \
    --cc=tglx@linutronix.de \
    --cc=tkjos@android.com \
    --cc=tmgross@umich.edu \
    --cc=viro@zeniv.linux.org.uk \
    --cc=wedsonaf@gmail.com \
    --cc=willy@infradead.org \
    --cc=yakoyoku@gmail.com \
    /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).