From: Fiona Behrens <me@kloenk.dev>
To: Andreas Hindborg <a.hindborg@kernel.org>
Cc: "Danilo Krummrich" <dakr@kernel.org>,
"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>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Joel Becker" <jlbec@evilplan.org>,
"Christoph Hellwig" <hch@lst.de>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 2/4] rust: sync: change `<Arc<T> as ForeignOwnable>::PointedTo` to `T`
Date: Wed, 05 Feb 2025 21:02:27 +0100 [thread overview]
Message-ID: <875xlop3cc.fsf@kloenk.dev> (raw)
In-Reply-To: <20250131-configfs-v1-2-87947611401c@kernel.org> (Andreas Hindborg's message of "Fri, 31 Jan 2025 14:30:09 +0100")
Andreas Hindborg <a.hindborg@kernel.org> writes:
> Using `ArcInner` as `PoinedTo` in the `ForeignOwnable` implementation for
> `Arc` is a bit unfortunate. Using `T` as `PointedTo` does not remove any
> functionality, but allows `ArcInner` to be private. Further, it allows
> downstream users to write code that is generic over `Box` and `Arc`, when
> downstream users need access to `T` after calling `into_foreign`.
>
> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
Bit unfortunate that we first in the patch before made it public and now
private again, but it makes sense to split patches.
Think I also found this weird when I wrote my LED patch, so this makes
a lot of sense as this is what I would expect.
Reviewed-by: Fiona Behrens <me@kloenk.dev>
>
> ---
>
> This patch is a dependency for Rust `configfs` abstractions. It allows both
> `Box` and `Arc` to be used as pointer types in the `configfs` hierarchy.
> ---
> rust/kernel/sync/arc.rs | 21 ++++++++++++++++-----
> 1 file changed, 16 insertions(+), 5 deletions(-)
>
> diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
> index dfe4abf82c25cf18398628d9cd5345a292419351..503e318b4c4effa2f1499b6fee0944079fde99aa 100644
> --- a/rust/kernel/sync/arc.rs
> +++ b/rust/kernel/sync/arc.rs
> @@ -143,7 +143,7 @@ pub struct Arc<T: ?Sized> {
> #[doc(hidden)]
> #[pin_data]
> #[repr(C)]
> -pub struct ArcInner<T: ?Sized> {
> +struct ArcInner<T: ?Sized> {
> refcount: Opaque<bindings::refcount_t>,
> data: T,
> }
> @@ -345,18 +345,25 @@ pub fn into_unique_or_drop(self) -> Option<Pin<UniqueArc<T>>> {
>
> // SAFETY: The `into_foreign` function returns a pointer that is well-aligned.
> unsafe impl<T: 'static> ForeignOwnable for Arc<T> {
> - type PointedTo = ArcInner<T>;
> + type PointedTo = T;
> type Borrowed<'a> = ArcBorrow<'a, T>;
> type BorrowedMut<'a> = Self::Borrowed<'a>;
>
> fn into_foreign(self) -> *mut Self::PointedTo {
> - ManuallyDrop::new(self).ptr.as_ptr()
> + let x = ManuallyDrop::new(self).ptr.as_ptr();
> + // SAFETY: `x` is a valid pointer to `Self` so the projection below is
> + // in bounds of the allocation.
> + unsafe { core::ptr::addr_of_mut!((*x).data) }
> }
>
> unsafe fn from_foreign(ptr: *mut Self::PointedTo) -> Self {
> + // SAFETY: We did the reverse offset calculation in `into_foreign`, so
> + // the offset calculation below is in bounds of the allocation.
> + let inner_ptr = unsafe { kernel::container_of!(ptr, ArcInner<T>, data).cast_mut() };
> +
> // SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
> // call to `Self::into_foreign`.
> - let inner = unsafe { NonNull::new_unchecked(ptr) };
> + let inner = unsafe { NonNull::new_unchecked(inner_ptr) };
>
> // SAFETY: By the safety requirement of this function, we know that `ptr` came from
> // a previous call to `Arc::into_foreign`, which guarantees that `ptr` is valid and
> @@ -365,9 +372,13 @@ unsafe fn from_foreign(ptr: *mut Self::PointedTo) -> Self {
> }
>
> unsafe fn borrow<'a>(ptr: *mut Self::PointedTo) -> ArcBorrow<'a, T> {
> + // SAFETY: We did the reverse offset calculation in `into_foreign`, so
> + // the offset calculation below is in bounds of the allocation.
> + let inner_ptr = unsafe { kernel::container_of!(ptr, ArcInner<T>, data).cast_mut() };
> +
> // SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
> // call to `Self::into_foreign`.
> - let inner = unsafe { NonNull::new_unchecked(ptr) };
> + let inner = unsafe { NonNull::new_unchecked(inner_ptr) };
>
> // SAFETY: The safety requirements of `from_foreign` ensure that the object remains alive
> // for the lifetime of the returned value.
next prev parent reply other threads:[~2025-02-05 20:02 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-31 13:30 [PATCH 0/4] rust: configfs abstractions Andreas Hindborg
2025-01-31 13:30 ` [PATCH 1/4] rust: types: add `ForeignOwnable::PointedTo` Andreas Hindborg
2025-02-05 19:59 ` Fiona Behrens
2025-02-06 12:18 ` Alice Ryhl
2025-01-31 13:30 ` [PATCH 2/4] rust: sync: change `<Arc<T> as ForeignOwnable>::PointedTo` to `T` Andreas Hindborg
2025-02-05 20:02 ` Fiona Behrens [this message]
2025-01-31 13:30 ` [PATCH 3/4] rust: configfs: introduce rust support for configfs Andreas Hindborg
2025-02-01 0:56 ` Charalampos Mitrodimas
2025-02-01 6:56 ` Andreas Hindborg
2025-02-05 21:19 ` Fiona Behrens
2025-02-06 11:37 ` Andreas Hindborg
2025-02-06 12:33 ` Andreas Hindborg
2025-01-31 13:30 ` [PATCH 4/4] MAINTAINERS: add entry for configfs Rust abstractions Andreas Hindborg
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=875xlop3cc.fsf@kloenk.dev \
--to=me@kloenk.dev \
--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=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=hch@lst.de \
--cc=jlbec@evilplan.org \
--cc=linux-kernel@vger.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.