[PATCH v6 03/14] rust: sync: add `Arc::as_ptr`

Andreas Hindborg posted 14 patches 11 months, 1 week ago
There is a newer version of this series
[PATCH v6 03/14] rust: sync: add `Arc::as_ptr`
Posted by Andreas Hindborg 11 months, 1 week ago
Add a method to get a pointer to the data contained in an `Arc`.

Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
 rust/kernel/sync/arc.rs | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
index fa4509406ee909ca0677b78d5ece966089ce6366..3d6111ddb007285b26eca2177a412033f4ac5dcb 100644
--- a/rust/kernel/sync/arc.rs
+++ b/rust/kernel/sync/arc.rs
@@ -233,6 +233,15 @@ pub fn into_raw(self) -> *const T {
         unsafe { core::ptr::addr_of!((*ptr).data) }
     }
 
+    /// Return a raw pointer to the data in this arc.
+    pub fn as_ptr(this: &Self) -> *const T {
+        let ptr = this.ptr.as_ptr();
+
+        // SAFETY: As `ptr` points to a valid allocation of type `ArcInner`,
+        // field projection to `data`is within bounds of the allocation.
+        unsafe { core::ptr::addr_of!((*ptr).data) }
+    }
+
     /// Recreates an [`Arc`] instance previously deconstructed via [`Arc::into_raw`].
     ///
     /// # Safety
@@ -508,11 +517,11 @@ unsafe fn new(inner: NonNull<ArcInner<T>>) -> Self {
     }
 
     /// Creates an [`ArcBorrow`] to an [`Arc`] that has previously been deconstructed with
-    /// [`Arc::into_raw`].
+    /// [`Arc::into_raw`] or [`Arc::as_ptr`].
     ///
     /// # Safety
     ///
-    /// * The provided pointer must originate from a call to [`Arc::into_raw`].
+    /// * The provided pointer must originate from a call to [`Arc::into_raw`] or [`Arc::as_ptr`].
     /// * For the duration of the lifetime annotated on this `ArcBorrow`, the reference count must
     ///   not hit zero.
     /// * For the duration of the lifetime annotated on this `ArcBorrow`, there must not be a

-- 
2.47.0
Re: [PATCH v6 03/14] rust: sync: add `Arc::as_ptr`
Posted by Alice Ryhl 11 months ago
On Fri, Jan 10, 2025 at 9:17 PM Andreas Hindborg <a.hindborg@kernel.org> wrote:
>
> Add a method to get a pointer to the data contained in an `Arc`.
>
> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>

A reason for why it's needed would be nice, but:

Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Re: [PATCH v6 03/14] rust: sync: add `Arc::as_ptr`
Posted by Tamir Duberstein 11 months, 1 week ago
On Fri, Jan 10, 2025 at 3:19 PM Andreas Hindborg <a.hindborg@kernel.org> wrote:
>
> Add a method to get a pointer to the data contained in an `Arc`.
>
> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>

Can this mention why it is needed?
Re: [PATCH v6 03/14] rust: sync: add `Arc::as_ptr`
Posted by Andreas Hindborg 11 months ago
"Tamir Duberstein" <tamird@gmail.com> writes:

> On Fri, Jan 10, 2025 at 3:19 PM Andreas Hindborg <a.hindborg@kernel.org> wrote:
>>
>> Add a method to get a pointer to the data contained in an `Arc`.
>>
>> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
>
> Can this mention why it is needed?

I can add that after the cut. It is for impleminting `HrTimerPointer`
for `Arc`.


Best regards,
Andreas Hindborg
Re: [PATCH v6 03/14] rust: sync: add `Arc::as_ptr`
Posted by Tamir Duberstein 10 months, 2 weeks ago
On Thu, Jan 16, 2025 at 2:23 PM Andreas Hindborg <a.hindborg@kernel.org> wrote:
>
> "Tamir Duberstein" <tamird@gmail.com> writes:
>
> > On Fri, Jan 10, 2025 at 3:19 PM Andreas Hindborg <a.hindborg@kernel.org> wrote:
> >>
> >> Add a method to get a pointer to the data contained in an `Arc`.
> >>
> >> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
> >
> > Can this mention why it is needed?
>
> I can add that after the cut. It is for impleminting `HrTimerPointer`
> for `Arc`.

The broader question is: why do we need to implement `/HrTimerPointer
` for `Arc`? Even broader is: why do we need to use raw pointers so
much?
Re: [PATCH v6 03/14] rust: sync: add `Arc::as_ptr`
Posted by Andreas Hindborg 10 months, 2 weeks ago
"Tamir Duberstein" <tamird@gmail.com> writes:

> On Thu, Jan 16, 2025 at 2:23 PM Andreas Hindborg <a.hindborg@kernel.org> wrote:
>>
>> "Tamir Duberstein" <tamird@gmail.com> writes:
>>
>> > On Fri, Jan 10, 2025 at 3:19 PM Andreas Hindborg <a.hindborg@kernel.org> wrote:
>> >>
>> >> Add a method to get a pointer to the data contained in an `Arc`.
>> >>
>> >> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
>> >
>> > Can this mention why it is needed?
>>
>> I can add that after the cut. It is for impleminting `HrTimerPointer`
>> for `Arc`.
>
> The broader question is: why do we need to implement `/HrTimerPointer
> ` for `Arc`? Even broader is: why do we need to use raw pointers so
> much?

The C APIs work with raw pointers. One important detail here is that the
consumers of the Rust APIs will never work with raw pointers. That is
all encapsulated within the abstractions.


Best regards,
Andreas Hindborg
Re: [PATCH v6 03/14] rust: sync: add `Arc::as_ptr`
Posted by Boqun Feng 10 months, 2 weeks ago
On Mon, Feb 03, 2025 at 05:46:42PM -0500, Tamir Duberstein wrote:
> On Thu, Jan 16, 2025 at 2:23 PM Andreas Hindborg <a.hindborg@kernel.org> wrote:
> >
> > "Tamir Duberstein" <tamird@gmail.com> writes:
> >
> > > On Fri, Jan 10, 2025 at 3:19 PM Andreas Hindborg <a.hindborg@kernel.org> wrote:
> > >>
> > >> Add a method to get a pointer to the data contained in an `Arc`.
> > >>
> > >> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
> > >
> > > Can this mention why it is needed?
> >
> > I can add that after the cut. It is for impleminting `HrTimerPointer`
> > for `Arc`.
> 
> The broader question is: why do we need to implement `/HrTimerPointer
> ` for `Arc`? Even broader is: why do we need to use raw pointers so
> much?

Because to use the C function for hrtimer operation (start, cancel,
etc.), we need to pass a raw pointer of "struct hrtimer", which is
embedded in the object in an `Arc`.

Regards,
Boqun