* [PATCH] rust: stop using ptr_metadata feature
@ 2024-02-05 9:18 Alice Ryhl
2024-02-05 12:25 ` Benno Lossin
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Alice Ryhl @ 2024-02-05 9:18 UTC (permalink / raw)
To: Miguel Ojeda
Cc: Wedson Almeida Filho, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Alice Ryhl, rust-for-linux,
linux-kernel
The `byte_sub` method was stabilized in Rust 1.75.0. By using that
method, we no longer need the unstable `ptr_metadata` feature for
implementing `Arc::from_raw`.
This brings us one step closer towards not using unstable compiler
features.
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
This patch is based on rust-next because it depends on the patch [1]
that upgrades to Rust 1.75.0.
[1]: https://lore.kernel.org/all/20231224172128.271447-1-ojeda@kernel.org/
rust/kernel/lib.rs | 1 -
rust/kernel/sync/arc.rs | 10 ++++------
2 files changed, 4 insertions(+), 7 deletions(-)
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index b89ecf4e97a0..b8d4c8167a29 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -17,7 +17,6 @@
#![feature(dispatch_from_dyn)]
#![feature(new_uninit)]
#![feature(offset_of)]
-#![feature(ptr_metadata)]
#![feature(receiver_trait)]
#![feature(unsize)]
diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
index 77cdbcf7bd2e..16309c3a9a01 100644
--- a/rust/kernel/sync/arc.rs
+++ b/rust/kernel/sync/arc.rs
@@ -30,7 +30,7 @@
mem::{ManuallyDrop, MaybeUninit},
ops::{Deref, DerefMut},
pin::Pin,
- ptr::{NonNull, Pointee},
+ ptr::NonNull,
};
use macros::pin_data;
@@ -239,18 +239,16 @@ pub unsafe fn from_raw(ptr: *const T) -> Self {
// binary, so its layout is not so large that it can trigger arithmetic overflow.
let val_offset = unsafe { refcount_layout.extend(val_layout).unwrap_unchecked().1 };
- let metadata: <T as Pointee>::Metadata = core::ptr::metadata(ptr);
// SAFETY: The metadata of `T` and `ArcInner<T>` is the same because `ArcInner` is a struct
// with `T` as its last field.
//
// This is documented at:
// <https://doc.rust-lang.org/std/ptr/trait.Pointee.html>.
- let metadata: <ArcInner<T> as Pointee>::Metadata =
- unsafe { core::mem::transmute_copy(&metadata) };
+ let ptr = ptr as *mut ArcInner<T>;
+
// SAFETY: The pointer is in-bounds of an allocation both before and after offsetting the
// pointer, since it originates from a previous call to `Arc::into_raw` and is still valid.
- let ptr = unsafe { (ptr as *mut u8).sub(val_offset) as *mut () };
- let ptr = core::ptr::from_raw_parts_mut(ptr, metadata);
+ let ptr = unsafe { ptr.byte_sub(val_offset) };
// SAFETY: By the safety requirements we know that `ptr` came from `Arc::into_raw`, so the
// reference count held then will be owned by the new `Arc` object.
base-commit: f090f0d0eea9666a96702b29bc9a64cbabee85c5
--
2.43.0.594.gd9cf4e227d-goog
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] rust: stop using ptr_metadata feature
2024-02-05 9:18 [PATCH] rust: stop using ptr_metadata feature Alice Ryhl
@ 2024-02-05 12:25 ` Benno Lossin
2024-02-05 13:54 ` Martin Rodriguez Reboredo
2024-02-05 21:02 ` Trevor Gross
2 siblings, 0 replies; 5+ messages in thread
From: Benno Lossin @ 2024-02-05 12:25 UTC (permalink / raw)
To: Alice Ryhl, Miguel Ojeda
Cc: Wedson Almeida Filho, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, rust-for-linux, linux-kernel
On 2/5/24 10:18, Alice Ryhl wrote:
> The `byte_sub` method was stabilized in Rust 1.75.0. By using that
> method, we no longer need the unstable `ptr_metadata` feature for
> implementing `Arc::from_raw`.
>
> This brings us one step closer towards not using unstable compiler
> features.
>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>
> ---
> This patch is based on rust-next because it depends on the patch [1]
> that upgrades to Rust 1.75.0.
>
> [1]: https://lore.kernel.org/all/20231224172128.271447-1-ojeda@kernel.org/
>
> rust/kernel/lib.rs | 1 -
> rust/kernel/sync/arc.rs | 10 ++++------
> 2 files changed, 4 insertions(+), 7 deletions(-)
Good to see this!
Reviewed-by: Benno Lossin <benno.lossin@proton.me>
--
Cheers,
Benno
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] rust: stop using ptr_metadata feature
2024-02-05 9:18 [PATCH] rust: stop using ptr_metadata feature Alice Ryhl
2024-02-05 12:25 ` Benno Lossin
@ 2024-02-05 13:54 ` Martin Rodriguez Reboredo
2024-02-05 21:02 ` Trevor Gross
2 siblings, 0 replies; 5+ messages in thread
From: Martin Rodriguez Reboredo @ 2024-02-05 13:54 UTC (permalink / raw)
To: Alice Ryhl, Miguel Ojeda
Cc: Wedson Almeida Filho, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, rust-for-linux, linux-kernel
On 2/5/24 06:18, Alice Ryhl wrote:
> The `byte_sub` method was stabilized in Rust 1.75.0. By using that
> method, we no longer need the unstable `ptr_metadata` feature for
> implementing `Arc::from_raw`.
>
> This brings us one step closer towards not using unstable compiler
> features.
>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>
> ---
> [...]
Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] rust: stop using ptr_metadata feature
2024-02-05 9:18 [PATCH] rust: stop using ptr_metadata feature Alice Ryhl
2024-02-05 12:25 ` Benno Lossin
2024-02-05 13:54 ` Martin Rodriguez Reboredo
@ 2024-02-05 21:02 ` Trevor Gross
2024-02-08 14:31 ` Alice Ryhl
2 siblings, 1 reply; 5+ messages in thread
From: Trevor Gross @ 2024-02-05 21:02 UTC (permalink / raw)
To: Alice Ryhl
Cc: Miguel Ojeda, Wedson Almeida Filho, Alex Gaynor, Boqun Feng,
Gary Guo, Björn Roy Baron, Benno Lossin, rust-for-linux,
linux-kernel
On Mon, Feb 5, 2024 at 3:19 AM Alice Ryhl <aliceryhl@google.com> wrote:
>
> The `byte_sub` method was stabilized in Rust 1.75.0. By using that
> method, we no longer need the unstable `ptr_metadata` feature for
> implementing `Arc::from_raw`.
>
> This brings us one step closer towards not using unstable compiler
> features.
>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>
> ---
> This patch is based on rust-next because it depends on the patch [1]
> that upgrades to Rust 1.75.0.
>
> [1]: https://lore.kernel.org/all/20231224172128.271447-1-ojeda@kernel.org/
>
> rust/kernel/lib.rs | 1 -
> rust/kernel/sync/arc.rs | 10 ++++------
> 2 files changed, 4 insertions(+), 7 deletions(-)
>
> diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
> [...]
> @@ -239,18 +239,16 @@ pub unsafe fn from_raw(ptr: *const T) -> Self {
> // binary, so its layout is not so large that it can trigger arithmetic overflow.
> let val_offset = unsafe { refcount_layout.extend(val_layout).unwrap_unchecked().1 };
>
> - let metadata: <T as Pointee>::Metadata = core::ptr::metadata(ptr);
> // SAFETY: The metadata of `T` and `ArcInner<T>` is the same because `ArcInner` is a struct
> // with `T` as its last field.
> //
> // This is documented at:
> // <https://doc.rust-lang.org/std/ptr/trait.Pointee.html>.
The comment should be reworded, no more metadata and no unsafe block
so it doesn't have to be SAFETY.
> - let metadata: <ArcInner<T> as Pointee>::Metadata =
> - unsafe { core::mem::transmute_copy(&metadata) };
> + let ptr = ptr as *mut ArcInner<T>;
Nit: this could be `.cast::<ArcInner<T>>().cast_mut()` to make the
intentional mutability change clear.
> // SAFETY: The pointer is in-bounds of an allocation both before and after offsetting the
> // pointer, since it originates from a previous call to `Arc::into_raw` and is still valid.
> - let ptr = unsafe { (ptr as *mut u8).sub(val_offset) as *mut () };
> - let ptr = core::ptr::from_raw_parts_mut(ptr, metadata);
> + let ptr = unsafe { ptr.byte_sub(val_offset) };
>
> // SAFETY: By the safety requirements we know that `ptr` came from `Arc::into_raw`, so the
> // reference count held then will be owned by the new `Arc` object.
>
> base-commit: f090f0d0eea9666a96702b29bc9a64cbabee85c5
> --
> 2.43.0.594.gd9cf4e227d-goog
>
>
Worth noting that the same change has been in upstream for a while,
since https://github.com/rust-lang/rust/pull/99113.
With the above changed or otherwise clarified:
Reviewed-by: Trevor Gross <tmgross@umich.edu>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] rust: stop using ptr_metadata feature
2024-02-05 21:02 ` Trevor Gross
@ 2024-02-08 14:31 ` Alice Ryhl
0 siblings, 0 replies; 5+ messages in thread
From: Alice Ryhl @ 2024-02-08 14:31 UTC (permalink / raw)
To: Trevor Gross
Cc: Miguel Ojeda, Wedson Almeida Filho, Alex Gaynor, Boqun Feng,
Gary Guo, Björn Roy Baron, Benno Lossin, rust-for-linux,
linux-kernel
On Mon, Feb 5, 2024 at 10:02 PM Trevor Gross <tmgross@umich.edu> wrote:
>
> On Mon, Feb 5, 2024 at 3:19 AM Alice Ryhl <aliceryhl@google.com> wrote:
> > // SAFETY: The metadata of `T` and `ArcInner<T>` is the same because `ArcInner` is a struct
> > // with `T` as its last field.
> > //
> > // This is documented at:
> > // <https://doc.rust-lang.org/std/ptr/trait.Pointee.html>.
>
> The comment should be reworded, no more metadata and no unsafe block
> so it doesn't have to be SAFETY.
How about this?
// Pointer casts leave the metadata unchanged. This is okay because
the metadata of `T` and
// `ArcInner<T>` is the same since `ArcInner` is a struct with `T` as
its last field.
//
// This is documented at:
// <https://doc.rust-lang.org/std/ptr/trait.Pointee.html>.
> > - let metadata: <ArcInner<T> as Pointee>::Metadata =
> > - unsafe { core::mem::transmute_copy(&metadata) };
> > + let ptr = ptr as *mut ArcInner<T>;
>
> Nit: this could be `.cast::<ArcInner<T>>().cast_mut()` to make the
> intentional mutability change clear.
The `.cast()` method can't be used here. It only works for sized types.
Alice
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2024-02-08 14:31 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-05 9:18 [PATCH] rust: stop using ptr_metadata feature Alice Ryhl
2024-02-05 12:25 ` Benno Lossin
2024-02-05 13:54 ` Martin Rodriguez Reboredo
2024-02-05 21:02 ` Trevor Gross
2024-02-08 14:31 ` Alice Ryhl
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).