From: Andreas Hindborg <a.hindborg@kernel.org>
To: "Boqun Feng" <boqun.feng@gmail.com>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Oliver Mangold" <oliver.mangold@pm.me>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] rust: elaborate safety requirements for `AlwaysReferenceCounted`
Date: Tue, 06 May 2025 16:45:45 +0200 [thread overview]
Message-ID: <87r011pzl2.fsf@kernel.org> (raw)
In-Reply-To: <aBoYYzj7sGEbsQzw@Mac.home> (Boqun Feng's message of "Tue, 06 May 2025 07:10:43 -0700")
"Boqun Feng" <boqun.feng@gmail.com> writes:
> On Tue, May 06, 2025 at 10:29:02AM +0200, Andreas Hindborg wrote:
>> Clarify that implementers of `AlwaysReferenceCounted` must prevent the
>> implementer from being directly initialized by users.
>>
>> It is a violation of the safety requirements of `AlwaysReferenceCounted` if
>> its implementers can be initialized on the stack by users. Although this
>> follows from the safety requirements, it is not immediately obvious.
>>
>> The following example demonstrates the issue. Note that the safety
>> requirements for implementing `AlwaysRefCounted` and for calling
>> `ARef::from_raw` are satisfied.
>>
>> struct Empty {}
>>
>> unsafe impl AlwaysRefCounted for Empty {
>> fn inc_ref(&self) {}
>> unsafe fn dec_ref(_obj: NonNull<Self>) {}
>> }
>>
>> fn unsound() -> ARef<Empty> {
>> use core::ptr::NonNull;
>> use kernel::types::{ARef, RefCounted};
>>
>> let mut data = Empty {};
>> let ptr = NonNull::<Empty>::new(&mut data).unwrap();
>> let aref: ARef<Empty> = unsafe { ARef::from_raw(ptr) };
>>
>
> Hmm.. I would say in this case, what gets violated is the safe
> requirement of ARef::from_raw(), because callers are supposed to
> guarantee that an refcount increment was passed to `ARef` and in this
> case, and unsound() cannot guarantee that here because it's going to
> clean up `data` when the it returns.
Right, `unsound` is not following this part of the safety requirement:
and that they are properly relinquishing one increment ...
callers must not use the underlying object anymore
Freeing the object by returning is using the object.
So maybe we don't need to change anything, we can just fix up the
example for `ARef::into_raw`.
Best regards,
Andreas Hindborg
next prev parent reply other threads:[~2025-05-06 14:46 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-06 8:29 [PATCH v2] rust: elaborate safety requirements for `AlwaysReferenceCounted` Andreas Hindborg
2025-05-06 14:10 ` Boqun Feng
2025-05-06 14:45 ` Andreas Hindborg [this message]
2025-05-07 6:24 ` Alice Ryhl
2025-05-07 8:35 ` Benno Lossin
2025-05-07 8:41 ` Alice Ryhl
2025-05-07 9:19 ` Andreas Hindborg
2025-05-07 11:26 ` 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=87r011pzl2.fsf@kernel.org \
--to=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=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=oliver.mangold@pm.me \
--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