rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] rust: add `ARef::into_raw`
@ 2024-08-21 19:58 Alice Ryhl
  2024-08-21 20:00 ` Alice Ryhl
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Alice Ryhl @ 2024-08-21 19:58 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Alex Gaynor, Wedson Almeida Filho, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	rust-for-linux, linux-kernel, Kartik Prajapati, Alice Ryhl

From: Kartik Prajapati <kartikprajapati987@gmail.com>

Add a method for `ARef` that is analogous to `Arc::into_raw`. It is the
inverse operation of `ARef::from_raw`, and allows you to convert the
`ARef` back into a raw pointer while retaining ownership of the
refcount.

This new function will be used by [1] for converting the type in an
`ARef` using `ARef::from_raw(ARef::into_raw(me).cast())`. The author has
also needed the same function for other use-cases in the past, but [1]
is the first to go upstream.

This was implemented independently by Kartik and Alice. The two versions
were merged by Alice, so all mistakes are Alice's.

Link: https://lore.kernel.org/r/20240801-vma-v3-1-db6c1c0afda9@google.com [1]
Closes: https://github.com/Rust-for-Linux/linux/issues/1044
Signed-off-by: Kartik Prajapati <kartikprajapati987@gmail.com>
Co-developed-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
Sending a v2 that is merged with [1]. I didn't realize that I was
duplicating work, sorry! I'll update the author information to list
Kartik as the primary author. I kept my function body so that it returns
NonNull to mirror the type used by `ARef::from_raw`.

This change was previously included in
https://lore.kernel.org/all/20240801-vma-v3-1-db6c1c0afda9@google.com/
but is being split out in its own commit at Danilo's suggestion.

Link: https://github.com/Rust-for-Linux/linux/pull/1056 [1]
---
Changes in v2:
- Add example from [1], and fix the imports in the example so it compiles.
- Update author information to reflect merge with [1].
- Link to v1: https://lore.kernel.org/r/20240801-aref-into-raw-v1-1-33401e2fbac8@google.com
---
 rust/kernel/types.rs | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)

diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
index ee7dd1f963ef..9e7ca066355c 100644
--- a/rust/kernel/types.rs
+++ b/rust/kernel/types.rs
@@ -7,7 +7,7 @@
 use core::{
     cell::UnsafeCell,
     marker::{PhantomData, PhantomPinned},
-    mem::MaybeUninit,
+    mem::{ManuallyDrop, MaybeUninit},
     ops::{Deref, DerefMut},
     pin::Pin,
     ptr::NonNull,
@@ -396,6 +396,35 @@ pub unsafe fn from_raw(ptr: NonNull<T>) -> Self {
             _p: PhantomData,
         }
     }
+
+    /// Consumes the `ARef`, returning a raw pointer.
+    ///
+    /// This function does not change the refcount. After calling this function, the caller is
+    /// responsible for the refcount previously managed by the `ARef`.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use core::ptr::NonNull;
+    /// use kernel::types::{ARef, AlwaysRefCounted};
+    ///
+    /// struct Empty {}
+    ///
+    /// unsafe impl AlwaysRefCounted for Empty {
+    ///     fn inc_ref(&self) {}
+    ///     unsafe fn dec_ref(_obj: NonNull<Self>) {}
+    /// }
+    ///
+    /// let mut data = Empty {};
+    /// let ptr = NonNull::<Empty>::new(&mut data as *mut _).unwrap();
+    /// let data_ref: ARef<Empty> = unsafe { ARef::from_raw(ptr) };
+    /// let raw_ptr: NonNull<Empty> = ARef::into_raw(data_ref);
+    ///
+    /// assert_eq!(ptr, raw_ptr);
+    /// ```
+    pub fn into_raw(me: Self) -> NonNull<T> {
+        ManuallyDrop::new(me).ptr
+    }
 }
 
 impl<T: AlwaysRefCounted> Clone for ARef<T> {

---
base-commit: e26fa546042add70944d018b930530d16b3cf626
change-id: 20240801-aref-into-raw-e0518131442f

Best regards,
-- 
Alice Ryhl <aliceryhl@google.com>


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] rust: add `ARef::into_raw`
  2024-08-21 19:58 [PATCH v2] rust: add `ARef::into_raw` Alice Ryhl
@ 2024-08-21 20:00 ` Alice Ryhl
  2024-08-21 20:01 ` Benno Lossin
  2024-08-25 12:50 ` Miguel Ojeda
  2 siblings, 0 replies; 4+ messages in thread
From: Alice Ryhl @ 2024-08-21 20:00 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Alex Gaynor, Wedson Almeida Filho, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	rust-for-linux, linux-kernel, Kartik Prajapati

On Wed, Aug 21, 2024 at 9:59 PM Alice Ryhl <aliceryhl@google.com> wrote:
>
> From: Kartik Prajapati <kartikprajapati987@gmail.com>
>
> Add a method for `ARef` that is analogous to `Arc::into_raw`. It is the
> inverse operation of `ARef::from_raw`, and allows you to convert the
> `ARef` back into a raw pointer while retaining ownership of the
> refcount.
>
> This new function will be used by [1] for converting the type in an
> `ARef` using `ARef::from_raw(ARef::into_raw(me).cast())`. The author has

Oh, this should refer to me, oops.

> also needed the same function for other use-cases in the past, but [1]
> is the first to go upstream.
>
> This was implemented independently by Kartik and Alice. The two versions
> were merged by Alice, so all mistakes are Alice's.
>
> Link: https://lore.kernel.org/r/20240801-vma-v3-1-db6c1c0afda9@google.com [1]
> Closes: https://github.com/Rust-for-Linux/linux/issues/1044
> Signed-off-by: Kartik Prajapati <kartikprajapati987@gmail.com>
> Co-developed-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>
> ---
> Sending a v2 that is merged with [1]. I didn't realize that I was
> duplicating work, sorry! I'll update the author information to list
> Kartik as the primary author. I kept my function body so that it returns
> NonNull to mirror the type used by `ARef::from_raw`.
>
> This change was previously included in
> https://lore.kernel.org/all/20240801-vma-v3-1-db6c1c0afda9@google.com/
> but is being split out in its own commit at Danilo's suggestion.
>
> Link: https://github.com/Rust-for-Linux/linux/pull/1056 [1]
> ---
> Changes in v2:
> - Add example from [1], and fix the imports in the example so it compiles.
> - Update author information to reflect merge with [1].
> - Link to v1: https://lore.kernel.org/r/20240801-aref-into-raw-v1-1-33401e2fbac8@google.com
> ---
>  rust/kernel/types.rs | 31 ++++++++++++++++++++++++++++++-
>  1 file changed, 30 insertions(+), 1 deletion(-)
>
> diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
> index ee7dd1f963ef..9e7ca066355c 100644
> --- a/rust/kernel/types.rs
> +++ b/rust/kernel/types.rs
> @@ -7,7 +7,7 @@
>  use core::{
>      cell::UnsafeCell,
>      marker::{PhantomData, PhantomPinned},
> -    mem::MaybeUninit,
> +    mem::{ManuallyDrop, MaybeUninit},
>      ops::{Deref, DerefMut},
>      pin::Pin,
>      ptr::NonNull,
> @@ -396,6 +396,35 @@ pub unsafe fn from_raw(ptr: NonNull<T>) -> Self {
>              _p: PhantomData,
>          }
>      }
> +
> +    /// Consumes the `ARef`, returning a raw pointer.
> +    ///
> +    /// This function does not change the refcount. After calling this function, the caller is
> +    /// responsible for the refcount previously managed by the `ARef`.
> +    ///
> +    /// # Examples
> +    ///
> +    /// ```
> +    /// use core::ptr::NonNull;
> +    /// use kernel::types::{ARef, AlwaysRefCounted};
> +    ///
> +    /// struct Empty {}
> +    ///
> +    /// unsafe impl AlwaysRefCounted for Empty {
> +    ///     fn inc_ref(&self) {}
> +    ///     unsafe fn dec_ref(_obj: NonNull<Self>) {}
> +    /// }
> +    ///
> +    /// let mut data = Empty {};
> +    /// let ptr = NonNull::<Empty>::new(&mut data as *mut _).unwrap();
> +    /// let data_ref: ARef<Empty> = unsafe { ARef::from_raw(ptr) };
> +    /// let raw_ptr: NonNull<Empty> = ARef::into_raw(data_ref);
> +    ///
> +    /// assert_eq!(ptr, raw_ptr);
> +    /// ```
> +    pub fn into_raw(me: Self) -> NonNull<T> {
> +        ManuallyDrop::new(me).ptr
> +    }
>  }
>
>  impl<T: AlwaysRefCounted> Clone for ARef<T> {
>
> ---
> base-commit: e26fa546042add70944d018b930530d16b3cf626
> change-id: 20240801-aref-into-raw-e0518131442f
>
> Best regards,
> --
> Alice Ryhl <aliceryhl@google.com>
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] rust: add `ARef::into_raw`
  2024-08-21 19:58 [PATCH v2] rust: add `ARef::into_raw` Alice Ryhl
  2024-08-21 20:00 ` Alice Ryhl
@ 2024-08-21 20:01 ` Benno Lossin
  2024-08-25 12:50 ` Miguel Ojeda
  2 siblings, 0 replies; 4+ messages in thread
From: Benno Lossin @ 2024-08-21 20:01 UTC (permalink / raw)
  To: Alice Ryhl, Miguel Ojeda
  Cc: Alex Gaynor, Wedson Almeida Filho, Boqun Feng, Gary Guo,
	Björn Roy Baron, Andreas Hindborg, rust-for-linux,
	linux-kernel, Kartik Prajapati

On 21.08.24 21:58, Alice Ryhl wrote:
> From: Kartik Prajapati <kartikprajapati987@gmail.com>
> 
> Add a method for `ARef` that is analogous to `Arc::into_raw`. It is the
> inverse operation of `ARef::from_raw`, and allows you to convert the
> `ARef` back into a raw pointer while retaining ownership of the
> refcount.
> 
> This new function will be used by [1] for converting the type in an
> `ARef` using `ARef::from_raw(ARef::into_raw(me).cast())`. The author has
> also needed the same function for other use-cases in the past, but [1]
> is the first to go upstream.
> 
> This was implemented independently by Kartik and Alice. The two versions
> were merged by Alice, so all mistakes are Alice's.
> 
> Link: https://lore.kernel.org/r/20240801-vma-v3-1-db6c1c0afda9@google.com [1]
> Closes: https://github.com/Rust-for-Linux/linux/issues/1044
> Signed-off-by: Kartik Prajapati <kartikprajapati987@gmail.com>
> Co-developed-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>

Reviewed-by: Benno Lossin <benno.lossin@proton.me>

---
Cheers,
Benno


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] rust: add `ARef::into_raw`
  2024-08-21 19:58 [PATCH v2] rust: add `ARef::into_raw` Alice Ryhl
  2024-08-21 20:00 ` Alice Ryhl
  2024-08-21 20:01 ` Benno Lossin
@ 2024-08-25 12:50 ` Miguel Ojeda
  2 siblings, 0 replies; 4+ messages in thread
From: Miguel Ojeda @ 2024-08-25 12:50 UTC (permalink / raw)
  To: Alice Ryhl
  Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	rust-for-linux, linux-kernel, Kartik Prajapati

On Wed, Aug 21, 2024 at 9:59 PM Alice Ryhl <aliceryhl@google.com> wrote:
>
> From: Kartik Prajapati <kartikprajapati987@gmail.com>
>
> Add a method for `ARef` that is analogous to `Arc::into_raw`. It is the
> inverse operation of `ARef::from_raw`, and allows you to convert the
> `ARef` back into a raw pointer while retaining ownership of the
> refcount.
>
> This new function will be used by [1] for converting the type in an
> `ARef` using `ARef::from_raw(ARef::into_raw(me).cast())`. The author has
> also needed the same function for other use-cases in the past, but [1]
> is the first to go upstream.
>
> This was implemented independently by Kartik and Alice. The two versions
> were merged by Alice, so all mistakes are Alice's.
>
> Link: https://lore.kernel.org/r/20240801-vma-v3-1-db6c1c0afda9@google.com [1]
> Closes: https://github.com/Rust-for-Linux/linux/issues/1044
> Signed-off-by: Kartik Prajapati <kartikprajapati987@gmail.com>
> Co-developed-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>

Applied to `rust-next` -- thanks everyone!

    [ Reworded to correct the author reference and changed tag to Link
      since it is not a bug. - Miguel ]

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-08-25 12:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-21 19:58 [PATCH v2] rust: add `ARef::into_raw` Alice Ryhl
2024-08-21 20:00 ` Alice Ryhl
2024-08-21 20:01 ` Benno Lossin
2024-08-25 12:50 ` Miguel Ojeda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).