-
Notifications
You must be signed in to change notification settings - Fork 435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ARef::into_raw
#1044
Comments
I'll take this one. |
Sure! Note that there currently are no instances of |
Are you currently working on it? |
Yes @Redhawk18, I've the patch ready here, which I'm planning to submit in couple of days once I complete testing of changes in |
Hi @y86-dev, I've drafted a PR for initial review. Please take a look when you can. |
Add the function `into_raw` to `ARef<T>`. This method can be used to turn an `ARef` into a raw pointer. Link: Rust-for-Linux#1044 Co-developed-by: Vincenzo Palazzo <[email protected]> Signed-off-by: Kartik Prajapati <[email protected]>
Add the function `into_raw` to `ARef<T>`. This method can be used to turn an `ARef` into a raw pointer. Link: Rust-for-Linux#1044 Co-developed-by: Vincenzo Palazzo <[email protected]> Signed-off-by: Kartik Prajapati <[email protected]>
The PR is sent upstream through ML https://lore.kernel.org/all/[email protected]/ |
Add the function `into_raw` to `ARef<T>`. This method can be used to turn an `ARef` into a raw pointer. Link: Rust-for-Linux#1044 Co-developed-by: Vincenzo Palazzo <[email protected]> Signed-off-by: Kartik Prajapati <[email protected]>
Add the function `into_raw` to `ARef<T>`. This method can be used to turn an `ARef` into a raw pointer. Link: Rust-for-Linux#1044 Co-developed-by: Vincenzo Palazzo <[email protected]> Signed-off-by: Kartik Prajapati <[email protected]>
Add the function `into_raw` to `ARef<T>`. This method can be used to turn an `ARef` into a raw pointer. Link: Rust-for-Linux#1044 Co-developed-by: Vincenzo Palazzo <[email protected]> Signed-off-by: Kartik Prajapati <[email protected]>
@Kartik1397 We are thinking of using https://lore.kernel.org/rust-for-linux/[email protected]/ Alice is happy sending the patch (with you as the primary author), but if you want to send it yourself, that is also fine. Thanks! |
Something like (from Alice): 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
+ } |
By the way, I think @vincenzopalazzo did not change the code when he sent @Kartik1397 patch to the mailing list. But if Vincenzo contributed to the patch and should have a |
@ojeda you are right, I did not change the code I just push it to the finish line through the ML :) |
@ojeda I'm OK with using Thanks considering my patch! |
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/[email protected] [1] Closes: Rust-for-Linux#1044 Signed-off-by: Kartik Prajapati <[email protected]> Co-developed-by: Alice Ryhl <[email protected]> Signed-off-by: Alice Ryhl <[email protected]>
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/[email protected] [1] Closes: Rust-for-Linux#1044 Signed-off-by: Kartik Prajapati <[email protected]> Co-developed-by: Alice Ryhl <[email protected]> Signed-off-by: Alice Ryhl <[email protected]>
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/[email protected] [1] Closes: Rust-for-Linux#1044 Signed-off-by: Kartik Prajapati <[email protected]> Co-developed-by: Alice Ryhl <[email protected]> Signed-off-by: Alice Ryhl <[email protected]>
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/[email protected] [1] Closes: Rust-for-Linux#1044 Signed-off-by: Kartik Prajapati <[email protected]> Co-developed-by: Alice Ryhl <[email protected]> Signed-off-by: Alice Ryhl <[email protected]>
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())`. Alice 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/[email protected] [1] Closes: Rust-for-Linux#1044 Signed-off-by: Kartik Prajapati <[email protected]> Co-developed-by: Alice Ryhl <[email protected]> Signed-off-by: Alice Ryhl <[email protected]> Reviewed-by: Benno Lossin <[email protected]> [ Reworded to correct the name. - Miguel ] Signed-off-by: Miguel Ojeda <[email protected]>
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())`. Alice 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/[email protected] [1] Link: #1044 Signed-off-by: Kartik Prajapati <[email protected]> Co-developed-by: Alice Ryhl <[email protected]> Signed-off-by: Alice Ryhl <[email protected]> Reviewed-by: Benno Lossin <[email protected]> [ Reworded to correct the author reference and changed tag to Link since it is not a bug. - Miguel ] Signed-off-by: Miguel Ojeda <[email protected]>
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())`. Alice 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/[email protected] [1] Link: #1044 Signed-off-by: Kartik Prajapati <[email protected]> Co-developed-by: Alice Ryhl <[email protected]> Signed-off-by: Alice Ryhl <[email protected]> Reviewed-by: Benno Lossin <[email protected]> [ Reworded to correct the author reference and changed tag to Link since it is not a bug. - Miguel ] Signed-off-by: Miguel Ojeda <[email protected]>
Applied to |
Add the function
into_raw
toARef<T>
, the function should:ARef<T>
by value, but not run the destructor, so you need tomem::forget
theARef<T>
.NonNull<T>
or a*mut T
, figure out which one works better.Also adjust instances where
ARef
s are manually forgotten to use this new function.This requires submitting a proper patch to the LKML and the Rust for Linux mailing list. Please recall to test your changes, to use a proper title for the commit, to sign your commit under the Developer's Certificate of Origin and so on. Please see https://docs.kernel.org/process/submitting-patches.html and https://rust-for-linux.com/contributing for details.
Please take this issue only if you are new to the kernel development process and you would like to use it as a test to submit your first patch to the kernel.
The text was updated successfully, but these errors were encountered: