From: Fiona Behrens <me@kloenk.dev>
To: Benno Lossin <benno.lossin@proton.me>
Cc: "Danilo Krummrich" <dakr@kernel.org>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 09/22] rust: pin-init: move impl `Zeroable` for `Opaque` and `Option<KBox<T>>` into the kernel crate
Date: Thu, 13 Mar 2025 12:16:36 +0100 [thread overview]
Message-ID: <m2zfhpxjsb.fsf@kloenk.dev> (raw)
In-Reply-To: <20250308110339.2997091-10-benno.lossin@proton.me> (Benno Lossin's message of "Sat, 08 Mar 2025 11:04:38 +0000")
Benno Lossin <benno.lossin@proton.me> writes:
> In order to make pin-init a standalone crate, move kernel-specific code
> directly into the kernel crate. Since `Opaque<T>` and `KBox<T>` are part
> of the kernel, move their `Zeroable` implementation into the kernel
> crate.
>
> Signed-off-by: Benno Lossin <benno.lossin@proton.me>
> Tested-by: Andreas Hindborg <a.hindborg@kernel.org>
Reviewed-by: Fiona Behrens <me@kloenk.dev>
> ---
> rust/kernel/alloc/kbox.rs | 8 +++++++-
> rust/kernel/types.rs | 5 ++++-
> rust/pin-init/src/lib.rs | 8 +-------
> 3 files changed, 12 insertions(+), 9 deletions(-)
>
> diff --git a/rust/kernel/alloc/kbox.rs b/rust/kernel/alloc/kbox.rs
> index 39a3ea7542da..9861433559dc 100644
> --- a/rust/kernel/alloc/kbox.rs
> +++ b/rust/kernel/alloc/kbox.rs
> @@ -15,7 +15,7 @@
> use core::ptr::NonNull;
> use core::result::Result;
>
> -use crate::init::{InPlaceWrite, Init, PinInit};
> +use crate::init::{InPlaceWrite, Init, PinInit, Zeroable};
> use crate::init_ext::InPlaceInit;
> use crate::types::ForeignOwnable;
>
> @@ -100,6 +100,12 @@
> /// ```
> pub type KVBox<T> = Box<T, super::allocator::KVmalloc>;
>
> +// SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee).
> +//
> +// In this case we are allowed to use `T: ?Sized`, since all zeros is the `None` variant and there
> +// is no problem with a VTABLE pointer being null.
> +unsafe impl<T: ?Sized, A: Allocator> Zeroable for Option<Box<T, A>> {}
> +
> // SAFETY: `Box` is `Send` if `T` is `Send` because the `Box` owns a `T`.
> unsafe impl<T, A> Send for Box<T, A>
> where
> diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
> index 2bbaab83b9d6..9f75bd3866e8 100644
> --- a/rust/kernel/types.rs
> +++ b/rust/kernel/types.rs
> @@ -2,7 +2,7 @@
>
> //! Kernel types.
>
> -use crate::init::{self, PinInit};
> +use crate::init::{self, PinInit, Zeroable};
> use core::{
> cell::UnsafeCell,
> marker::{PhantomData, PhantomPinned},
> @@ -309,6 +309,9 @@ pub struct Opaque<T> {
> _pin: PhantomPinned,
> }
>
> +// SAFETY: `Opaque<T>` allows the inner value to be any bit pattern, including all zeros.
> +unsafe impl<T> Zeroable for Opaque<T> {}
> +
> impl<T> Opaque<T> {
> /// Creates a new opaque value.
> pub const fn new(value: T) -> Self {
> diff --git a/rust/pin-init/src/lib.rs b/rust/pin-init/src/lib.rs
> index f88465e0bb76..aad6486d33fc 100644
> --- a/rust/pin-init/src/lib.rs
> +++ b/rust/pin-init/src/lib.rs
> @@ -211,10 +211,7 @@
> //! [`pin_data`]: ::macros::pin_data
> //! [`pin_init!`]: crate::pin_init!
>
> -use crate::{
> - alloc::KBox,
> - types::{Opaque, ScopeGuard},
> -};
> +use crate::{alloc::KBox, types::ScopeGuard};
> use core::{
> cell::UnsafeCell,
> convert::Infallible,
> @@ -1342,8 +1339,6 @@ macro_rules! impl_zeroable {
>
> // SAFETY: Type is allowed to take any value, including all zeros.
> {<T>} MaybeUninit<T>,
> - // SAFETY: Type is allowed to take any value, including all zeros.
> - {<T>} Opaque<T>,
>
> // SAFETY: `T: Zeroable` and `UnsafeCell` is `repr(transparent)`.
> {<T: ?Sized + Zeroable>} UnsafeCell<T>,
> @@ -1358,7 +1353,6 @@ macro_rules! impl_zeroable {
> //
> // In this case we are allowed to use `T: ?Sized`, since all zeros is the `None` variant.
> {<T: ?Sized>} Option<NonNull<T>>,
> - {<T: ?Sized>} Option<KBox<T>>,
>
> // SAFETY: `null` pointer is valid.
> //
next prev parent reply other threads:[~2025-03-13 11:16 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20250308110339.2997091-1-benno.lossin@proton.me>
2025-03-08 11:03 ` [PATCH v2 01/22] rust: init: disable doctests Benno Lossin
2025-03-08 11:04 ` [PATCH v2 02/22] rust: move pin-init API into its own directory Benno Lossin
2025-03-08 11:04 ` [PATCH v2 03/22] rust: add extensions to the pin-init crate and move relevant documentation there Benno Lossin
2025-03-18 11:32 ` Andreas Hindborg
2025-03-08 11:04 ` [PATCH v2 04/22] rust: pin-init: move proc-macro documentation into pin-init crate Benno Lossin
2025-03-18 11:33 ` Andreas Hindborg
2025-03-19 13:14 ` Miguel Ojeda
2025-03-08 11:04 ` [PATCH v2 05/22] rust: pin-init: change examples to the user-space version Benno Lossin
2025-03-08 11:04 ` [PATCH v2 06/22] rust: pin-init: call `try_[pin_]init!` from `[pin_]init!` instead of `__init_internal!` Benno Lossin
2025-03-08 11:04 ` [PATCH v2 07/22] rust: pin-init: move the default error behavior of `try_[pin_]init` Benno Lossin
2025-03-08 11:04 ` [PATCH v2 08/22] rust: pin-init: move `InPlaceInit` and impls of `InPlaceWrite` into the kernel crate Benno Lossin
2025-03-18 11:37 ` Andreas Hindborg
2025-03-08 11:04 ` [PATCH v2 09/22] rust: pin-init: move impl `Zeroable` for `Opaque` and `Option<KBox<T>>` " Benno Lossin
2025-03-13 11:16 ` Fiona Behrens [this message]
2025-03-08 11:04 ` [PATCH v2 10/22] rust: add `ZeroableOption` and implement it instead of `Zeroable` for `Option<Box<T, A>>` Benno Lossin
2025-03-08 11:04 ` [PATCH v2 11/22] rust: pin-init: fix documentation links Benno Lossin
2025-03-18 11:38 ` Andreas Hindborg
2025-03-08 11:04 ` [PATCH v2 12/22] rust: pin-init: remove kernel-crate dependency Benno Lossin
2025-03-08 11:04 ` [PATCH v2 13/22] rust: pin-init: change the way the `paste!` macro is called Benno Lossin
2025-03-08 11:05 ` [PATCH v2 14/22] rust: add pin-init crate build infrastructure Benno Lossin
2025-03-08 11:05 ` [PATCH v2 15/22] rust: make pin-init its own crate Benno Lossin
2025-03-18 11:40 ` Andreas Hindborg
2025-03-08 11:05 ` [PATCH v2 16/22] rust: pin-init: add `std` and `alloc` support from the user-space version Benno Lossin
2025-03-08 11:05 ` [PATCH v2 17/22] rust: pin-init: synchronize documentation with " Benno Lossin
2025-03-08 11:05 ` [PATCH v2 18/22] rust: pin-init: internal: synchronize with " Benno Lossin
2025-03-13 11:22 ` Fiona Behrens
2025-03-08 11:05 ` [PATCH v2 19/22] rust: pin-init: miscellaneous synchronization with the " Benno Lossin
2025-03-13 11:24 ` Fiona Behrens
2025-03-08 11:05 ` [PATCH v2 20/22] rust: pin-init: add miscellaneous files from " Benno Lossin
2025-03-08 11:05 ` [PATCH v2 21/22] rust: pin-init: re-enable doctests Benno Lossin
2025-03-08 11:05 ` [PATCH v2 22/22] MAINTAINERS: add entry for the `pin-init` crate Benno Lossin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=m2zfhpxjsb.fsf@kloenk.dev \
--to=me@kloenk.dev \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox