[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CALNs47t63qAU2OpokvN6KykT0_NKpDLTBQGhLm-+zsvR1pcUAQ@mail.gmail.com>
Date: Mon, 5 Feb 2024 15:02:46 -0600
From: Trevor Gross <tmgross@...ch.edu>
To: Alice Ryhl <aliceryhl@...gle.com>
Cc: Miguel Ojeda <ojeda@...nel.org>, Wedson Almeida Filho <wedsonaf@...il.com>,
Alex Gaynor <alex.gaynor@...il.com>, Boqun Feng <boqun.feng@...il.com>,
Gary Guo <gary@...yguo.net>, Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <benno.lossin@...ton.me>, rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH] rust: stop using ptr_metadata feature
On Mon, Feb 5, 2024 at 3:19 AM Alice Ryhl <aliceryhl@...gle.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@...gle.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@...ch.edu>
Powered by blists - more mailing lists