]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
rust: add `ZeroableOption` and implement it instead of `Zeroable` for `Option<Box...
authorBenno Lossin <benno.lossin@proton.me>
Sat, 8 Mar 2025 11:04:43 +0000 (11:04 +0000)
committerMiguel Ojeda <ojeda@kernel.org>
Sun, 16 Mar 2025 20:59:18 +0000 (21:59 +0100)
When making pin-init its own crate, `Zeroable` will no longer be defined
by the kernel crate and thus implementing it for `Option<Box<T, A>>` is
no longer possible due to the orphan rule.
For this reason introduce a new `ZeroableOption` trait that circumvents
this problem.

Signed-off-by: Benno Lossin <benno.lossin@proton.me>
Reviewed-by: Fiona Behrens <me@kloenk.dev>
Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org>
Tested-by: Andreas Hindborg <a.hindborg@kernel.org>
Link: https://lore.kernel.org/r/20250308110339.2997091-11-benno.lossin@proton.me
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
rust/kernel/alloc/kbox.rs
rust/pin-init/src/lib.rs

index 9861433559dc2c0de62757f75c03f64abeae5630..07150c038e3fff921d33d82d51554e4c86ef8a72 100644 (file)
@@ -15,7 +15,7 @@ use core::pin::Pin;
 use core::ptr::NonNull;
 use core::result::Result;
 
-use crate::init::{InPlaceWrite, Init, PinInit, Zeroable};
+use crate::init::{InPlaceWrite, Init, PinInit, ZeroableOption};
 use crate::init_ext::InPlaceInit;
 use crate::types::ForeignOwnable;
 
@@ -104,7 +104,7 @@ pub type KVBox<T> = Box<T, super::allocator::KVmalloc>;
 //
 // 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>> {}
+unsafe impl<T: ?Sized, A: Allocator> ZeroableOption for 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>
index aad6486d33fc7a78009b53869b9557be9e6c59ab..ca6be982b522065fdcd6b28a881c96003ca3cba3 100644 (file)
@@ -1297,6 +1297,17 @@ pub unsafe trait PinnedDrop: __internal::HasPinData {
 /// ```
 pub unsafe trait Zeroable {}
 
+/// Marker trait for types that allow `Option<Self>` to be set to all zeroes in order to write
+/// `None` to that location.
+///
+/// # Safety
+///
+/// The implementer needs to ensure that `unsafe impl Zeroable for Option<Self> {}` is sound.
+pub unsafe trait ZeroableOption {}
+
+// SAFETY: by the safety requirement of `ZeroableOption`, this is valid.
+unsafe impl<T: ZeroableOption> Zeroable for Option<T> {}
+
 /// Create a new zeroed T.
 ///
 /// The returned initializer will write `0x00` to every byte of the given `slot`.