rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Oliver Mangold <oliver.mangold@pm.me>
To: Benno Lossin <lossin@kernel.org>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Asahi Lina" <lina@asahilina.net>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v10 1/5] rust: types: Add Ownable/Owned types
Date: Fri, 20 Jun 2025 07:01:49 +0000	[thread overview]
Message-ID: <aFUHWtE2dm3_-Rbs@mango> (raw)
In-Reply-To: <DAPZ3WLBCBVL.3KA57Y90UKNRT@kernel.org>

On 250618 2322, Benno Lossin wrote:
> On Tue Jun 17, 2025 at 11:58 AM CEST, Oliver Mangold wrote:
> > On 250514 1132, Benno Lossin wrote:
> >> On Fri May 2, 2025 at 11:02 AM CEST, Oliver Mangold wrote:
> >> > +///
> >> > +/// # Safety
> >> > +///
> >> > +/// Implementers must ensure that:
> >> > +/// - Any objects owned by Rust as [`Owned<T>`] stay alive while that owned reference exists (i.e.
> >> > +///   until the [`release()`](Ownable::release) trait method is called).
> >>
> >> I don't immediately understand what this means. How about "Any value of
> >> type `Self` needs to be stored as [`Owned<Self>`]."?
> >
> > Let me think. The safety requirements here talk about safety of
> > implementing the trait.  But if you have a `Self` which is not wrapped, you
> > still cannot create an `Owned<Self>` in safe code. It's different from an
> > `AlwaysRefCounted`, where an `ARef<Self>` can be created from a `&Self`.
> 
> That might be true, but AFAIK this trait is designed to be used for
> stuff that has a `create_foo` and `destroy_foo` function in C returning
> and taking a raw pointer to `foo` respectively. So creating it on the
> stack doesn't make sense.

I didn't mean creating one on the stack, but keeping it in a raw pointer or
`NonNull<T>`, not bothering to wrap in in an `Owned<T>`. But doesn't
matter. In any case in v11 (which predates your answer), I moved this
requirement to `Owned::from_raw()`, as, you asked below, which should be
okay as that function is the only way to create an `Owned<T>`. But I can
add the "needs to be stored as `Owned<Self>`" requirement, if you think it
is important.


> If we do want to make this trait more general, then we can do so, but
> this is my current understanding.
> 
> >> And then ask in
> >> `Owned::from_raw` for a pointer that is valid indefinitely (or at least
> >> until `release` is called).
> >
> > So, hmm, I think one could even move this safety requirement to `Owned::from_raw()`.
> >
> >> > +/// - That the C code follows the usual mutable reference requirements. That is, the kernel will
> >> > +///   never mutate the [`Ownable`] (excluding internal mutability that follows the usual rules)
> >> > +///   while Rust owns it.
> >>
> >> I feel like this requirement is better put on the `Owned::from_raw`
> >> function.
> >
> > Together with the above, this would leave to safety requirements for `Ownable.
> > Make `Ownable` a safe trait, then? Instead of safety requirements just add an invariant:
> >
> >     # Invariant
> >
> >     An `Owned<Self>` represents a unique reference to a `Self`, thus holding
> >     an `Owned<Self>` or `&mut Owned<Self>` allows one to assume that the object
> >     is not accessed concurrently from elsewhere.
> >
> > Not sure what is best. Would that make sense?
> 
> Making it safe makes sense, when we can move all requirements to
> `Owned::from_raw`. I don't think the invariants section makes sense, how
> would the trait have any influence in that when `Owned::from_raw`
> already guarantees it?

I think you are right on that. Let's not do that.

Best,

Oliver


  reply	other threads:[~2025-06-20  7:02 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-02  9:02 [PATCH v10 0/5] New trait OwnableRefCounted for ARef<->Owned conversion Oliver Mangold
2025-05-02  9:02 ` [PATCH v10 1/5] rust: types: Add Ownable/Owned types Oliver Mangold
2025-05-02  9:57   ` Andreas Hindborg
2025-06-16 11:43     ` Oliver Mangold
2025-06-17 11:42       ` Andreas Hindborg
2025-05-02 11:29   ` Alice Ryhl
2025-05-06 11:20     ` Andreas Hindborg
2025-05-07  6:20       ` Alice Ryhl
2025-05-08 12:24   ` Andreas Hindborg
2025-05-14  9:32   ` Benno Lossin
2025-06-17  9:58     ` Oliver Mangold
2025-06-18 21:22       ` Benno Lossin
2025-06-20  7:01         ` Oliver Mangold [this message]
2025-06-20  8:09           ` Benno Lossin
2025-06-18  9:34     ` Oliver Mangold
2025-06-18 21:19       ` Benno Lossin
2025-06-19  9:33         ` Andreas Hindborg
2025-06-19 12:18           ` Benno Lossin
2025-05-02  9:02 ` [PATCH v10 2/5] rust: Rename AlwaysRefCounted to RefCounted Oliver Mangold
2025-05-02 10:10   ` Andreas Hindborg
2025-05-02 11:32   ` Alice Ryhl
2025-06-16 11:56     ` Oliver Mangold
2025-06-16 12:13       ` Alice Ryhl
2025-05-02  9:02 ` [PATCH v10 3/5] rust: Add missing SAFETY documentation for ARef example Oliver Mangold
2025-05-02 10:41   ` Andreas Hindborg
2025-05-02 11:12     ` Oliver Mangold
2025-05-02 12:01       ` Andreas Hindborg
2025-05-02  9:02 ` [PATCH v10 4/5] rust: kbuild: provide `RUSTC_HAS_DO_NOT_RECOMMEND` symbol Oliver Mangold
2025-05-02  9:03 ` [PATCH v10 5/5] rust: Add OwnableRefCounted and SimpleOwnableRefCounted Oliver Mangold
2025-05-02 11:43   ` Alice Ryhl
2025-05-06 11:42     ` Oliver Mangold
2025-05-07  6:19       ` Alice Ryhl
2025-06-13 13:10 ` [PATCH v10 0/5] New trait OwnableRefCounted for ARef<->Owned conversion Andreas Hindborg
2025-06-13 13:27   ` Oliver Mangold

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=aFUHWtE2dm3_-Rbs@mango \
    --to=oliver.mangold@pm.me \
    --cc=a.hindborg@kernel.org \
    --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=gary@garyguo.net \
    --cc=lina@asahilina.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /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).