Re: [PATCH v5 1/4] rust: sync: change `<Arc<T> as ForeignOwnable>::PointedTo` to `T`

From: Andreas Hindborg
Date: Thu Feb 27 2025 - 13:26:44 EST


Andreas Hindborg <a.hindborg@xxxxxxxxxx> writes:

> "Tamir Duberstein" <tamird@xxxxxxxxx> writes:
>
>> On Thu, Feb 27, 2025 at 7:37 AM Andreas Hindborg <a.hindborg@xxxxxxxxxx> wrote:
>>>
>>> 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`.
>>>
>>> Reviewed-by: Fiona Behrens <me@xxxxxxxxxx>
>>> Reviewed-by: Daniel Almeida <daniel.almeida@xxxxxxxxxxxxx>
>>> Tested-by: Daniel Almeida <daniel.almeida@xxxxxxxxxxxxx>
>>> Signed-off-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>
>>> ---
>>>
>>> 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 dfe4abf82c25..3d77a31e116f 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 this = ManuallyDrop::new(self).ptr.as_ptr();
>>> + // SAFETY: `x` is a valid pointer to `Self` so the projection below is
>>> + // in bounds of the allocation.
>>
>> Isn't the unsafe bit `*this`, which is what this comment should
>> justify?
>
> Yes, not sure what the origin of that name is. Good catch.

Referring to the `x` part here.


Best regards,
Andreas Hindborg