Rust-for-linux archive mirror
 help / color / mirror / Atom feed
From: Benno Lossin <benno.lossin@proton.me>
To: Alice Ryhl <aliceryhl@google.com>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Wedson Almeida Filho" <wedsonaf@gmail.com>,
	"Boqun Feng" <boqun.feng@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>,
	"Kees Cook" <keescook@chromium.org>,
	"Matthew Wilcox" <willy@infradead.org>,
	"Thomas Gleixner" <tglx@linutronix.de>,
	"Daniel Xu" <dxu@dxuuu.xyz>,
	linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
	linux-fsdevel@vger.kernel.org
Subject: Re: [PATCH v3 1/9] rust: file: add Rust abstraction for `struct file`
Date: Thu, 01 Feb 2024 09:48:04 +0000	[thread overview]
Message-ID: <038748e0-22ce-4455-ba08-e8ae30e357df@proton.me> (raw)
In-Reply-To: <CAH5fLghQAn5JYeeG0MDO-acwQHdX7CTkr_-5SzGOzrdFs2SfNw@mail.gmail.com>

On 01.02.24 10:41, Alice Ryhl wrote:
> On Thu, Feb 1, 2024 at 10:38 AM Benno Lossin <benno.lossin@proton.me> wrote:
>>
>> On 01.02.24 10:33, Alice Ryhl wrote:
>>> On Thu, Feb 1, 2024 at 10:31 AM Benno Lossin <benno.lossin@proton.me> wrote:
>>>>
>>>> On 29.01.24 17:34, Alice Ryhl wrote:
>>>>> On Fri, Jan 26, 2024 at 4:04 PM Benno Lossin <benno.lossin@proton.me> wrote:
>>>>>>> +///   closed.
>>>>>>> +/// * A light refcount must be dropped before returning to userspace.
>>>>>>> +#[repr(transparent)]
>>>>>>> +pub struct File(Opaque<bindings::file>);
>>>>>>> +
>>>>>>> +// SAFETY: By design, the only way to access a `File` is via an immutable reference or an `ARef`.
>>>>>>> +// This means that the only situation in which a `File` can be accessed mutably is when the
>>>>>>> +// refcount drops to zero and the destructor runs. It is safe for that to happen on any thread, so
>>>>>>> +// it is ok for this type to be `Send`.
>>>>>>
>>>>>> Technically, `drop` is never called for `File`, since it is only used
>>>>>> via `ARef<File>` which calls `dec_ref` instead. Also since it only contains
>>>>>> an `Opaque`, dropping it is a noop.
>>>>>> But what does `Send` mean for this type? Since it is used together with
>>>>>> `ARef`, being `Send` means that `File::dec_ref` can be called from any
>>>>>> thread. I think we are missing this as a safety requirement on
>>>>>> `AlwaysRefCounted`, do you agree?
>>>>>> I think the safety justification here could be (with the requirement added
>>>>>> to `AlwaysRefCounted`):
>>>>>>
>>>>>>         SAFETY:
>>>>>>         - `File::drop` can be called from any thread.
>>>>>>         - `File::dec_ref` can be called from any thread.
>>>>>
>>>>> This wording was taken from rust/kernel/task.rs. I think it's out of
>>>>> scope to reword it.
>>>>
>>>> Rewording the safety docs on `AlwaysRefCounted`, yes that is out of scope,
>>>> I was just checking if you agree that the current wording is incomplete.
>>>
>>> That's not what I meant. The wording of this safety comment is
>>> identical to the wording in other existing safety comments in the
>>> kernel, such as e.g. the one for `impl Send for Task`.
>>
>> Ah I see. But I still think changing it is better, since it would only get
>> shorter. The comment on `Task` can be fixed later.
>> Or do you want to keep consistency here? Because I would prefer to make
>> this right and then change `Task` later.
> 
> What would you like me to change it to?
> 
> For example:
> // SAFETY: It is okay to send references to a File across thread boundaries.

That would fit better as the safety comment for `Sync`, since
it refers to "references".

For `Send` I think this would be better:
// SAFETY:
// - `File::dec_ref` can be called from any thread.
// - It is okay to send ownership of `File` across thread boundaries.

-- 
Cheers,
Benno



  reply	other threads:[~2024-02-01  9:48 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-18 14:36 [PATCH v3 0/9] File abstractions needed by Rust Binder Alice Ryhl
2024-01-18 14:36 ` [PATCH v3 1/9] rust: file: add Rust abstraction for `struct file` Alice Ryhl
2024-01-18 22:37   ` Valentin Obst
2024-01-26 15:04   ` Benno Lossin
2024-01-29 16:34     ` Alice Ryhl
2024-02-01  9:30       ` Benno Lossin
2024-02-01  9:33         ` Alice Ryhl
2024-02-01  9:38           ` Benno Lossin
2024-02-01  9:41             ` Alice Ryhl
2024-02-01  9:48               ` Benno Lossin [this message]
2024-01-18 14:36 ` [PATCH v3 2/9] rust: cred: add Rust abstraction for `struct cred` Alice Ryhl
2024-01-19  9:37   ` Benno Lossin
2024-01-19  9:52     ` Alice Ryhl
2024-01-24  9:51       ` Benno Lossin
2024-01-18 14:36 ` [PATCH v3 3/9] rust: security: add abstraction for secctx Alice Ryhl
2024-01-19  9:41   ` Benno Lossin
2024-01-18 14:36 ` [PATCH v3 4/9] rust: types: add `NotThreadSafe` Alice Ryhl
2024-01-19  9:43   ` Benno Lossin
2024-01-18 14:36 ` [PATCH v3 5/9] rust: file: add `FileDescriptorReservation` Alice Ryhl
2024-01-19  9:48   ` Benno Lossin
2024-01-18 14:36 ` [PATCH v3 6/9] rust: task: add `Task::current_raw` Alice Ryhl
2024-01-19  9:52   ` Benno Lossin
2024-01-18 14:36 ` [PATCH v3 7/9] rust: file: add `Kuid` wrapper Alice Ryhl
2024-01-24  9:55   ` Benno Lossin
2024-01-18 14:36 ` [PATCH v3 8/9] rust: file: add `DeferredFdCloser` Alice Ryhl
2024-01-22 17:59   ` Boqun Feng
2024-01-24 10:07   ` Benno Lossin
2024-01-18 14:36 ` [PATCH v3 9/9] rust: file: add abstraction for `poll_table` Alice Ryhl
2024-01-24 10:11   ` Benno Lossin
2024-01-29 17:08     ` Alice Ryhl
2024-02-01  9:33       ` Benno Lossin

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=038748e0-22ce-4455-ba08-e8ae30e357df@proton.me \
    --to=benno.lossin@proton.me \
    --cc=a.hindborg@samsung.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=arve@android.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.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=keescook@chromium.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=viro@zeniv.linux.org.uk \
    --cc=wedsonaf@gmail.com \
    --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 read-only IMAP folder(s) and NNTP newsgroup(s).