[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-Id: <D9PW4M8X6TMK.33M9WF0S42KI4@kernel.org>
Date: Wed, 07 May 2025 13:26:52 +0200
From: "Benno Lossin" <lossin@...nel.org>
To: "Andreas Hindborg" <a.hindborg@...nel.org>, "Alice Ryhl"
<aliceryhl@...gle.com>
Cc: "Miguel Ojeda" <ojeda@...nel.org>, "Alex Gaynor"
<alex.gaynor@...il.com>, "Boqun Feng" <boqun.feng@...il.com>, "Gary Guo"
<gary@...yguo.net>, Björn Roy Baron
<bjorn3_gh@...tonmail.com>, "Benno Lossin" <benno.lossin@...ton.me>,
"Trevor Gross" <tmgross@...ch.edu>, "Danilo Krummrich" <dakr@...nel.org>,
"Oliver Mangold" <oliver.mangold@...me>, <rust-for-linux@...r.kernel.org>,
<linux-kernel@...r.kernel.org>
Subject: Re: [PATCH v2] rust: elaborate safety requirements for
`AlwaysReferenceCounted`
On Wed May 7, 2025 at 11:19 AM CEST, Andreas Hindborg wrote:
> "Alice Ryhl" <aliceryhl@...gle.com> writes:
>> On Wed, May 07, 2025 at 10:35:16AM +0200, Benno Lossin wrote:
>>> On Tue May 6, 2025 at 10:29 AM CEST, Andreas Hindborg wrote:
>>> > diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
>>> > index 9d0471afc964..52683d686c8a 100644
>>> > --- a/rust/kernel/types.rs
>>> > +++ b/rust/kernel/types.rs
>>> > @@ -409,6 +409,10 @@ pub const fn raw_get(this: *const Self) -> *mut T {
>>> > /// Implementers must also ensure that all instances are reference-counted. (Otherwise they
>>> > /// won't be able to honour the requirement that [`AlwaysRefCounted::inc_ref`] keep the object
>>> > /// alive.)
>>> > +///
>>> > +/// Implementers of this trait must ensure that values of types implementing this trait can never be
>>> > +/// owned by value. Instead, values must be owned and used through a pointer type. That is, a type
>>> > +/// that implements [`Deref`].
>>>
>>> I don't think this covers every case, if I modify your example above
>>> with Alice's suggestion and using `Box` instead of the stack, I get the
>>> same problem:
>>>
>>> struct Empty {}
>>>
>>> unsafe impl AlwaysRefCounted for Empty {
>>> fn inc_ref(&self) {}
>>> unsafe fn dec_ref(_obj: NonNull<Self>) {}
>>> }
>>>
>>> fn unsound() -> ARef<Empty> {
>>> use kernel::types::{ARef, RefCounted};
>>>
>>> let data = Box::new(Empty {});
>>> let aref = ARef::from(&data);
>>>
>>> aref
>>> }
>>>
>>> The same should be true if one uses `Arc` instead of `Box`. So, even
>>> though we store it in a "pointer type that implements `Deref`", it is
>>> unsound.
>>>
>>> I think that types that implement `AlwaysRefCounted` must only be store
>>> inside of `ARef<T>`. So something like "Values of this trait must only
>>> be exposed as `ARef<Self>` or `&Self`." I'm not satisfied with the
>>> wording 'exposed', maybe you have a better word or can expand the
>>> sentence.
>>
>> I mean, in some sense the problem is that Empty violates the existing
>> requirement:
>>
>> Implementers must also ensure that all instances are reference-counted.
>> (Otherwise they won't be able to honour the requirement that
>> [`AlwaysRefCounted::inc_ref`] keep the object alive.)
>
> The example holds, even if you implement `inc_ref`/`dec_ref` properly,
> right?
Yes.
Although one could interpret the part that Alice quoted above to mean
the same thing as I suggested (only giving access to `ARef<Self>`, as
any other smart pointer isn't aware of the inner refcounting of the
value).
With that interpretation, we could argue that there is nothing wrong
with the safety requirements. But I'd say that we should definitely
mention that one is only allowed to use `ARef<Self>`/`&Self` and not
hand out any other ways to access `Self`.
---
Cheers,
Benno
Powered by blists - more mailing lists