Skip to content

Commit df27cef

Browse files
BennoLossinojeda
authored andcommitted
rust: init: fix Zeroable implementation for Option<NonNull<T>> and Option<KBox<T>>
According to [1], `NonNull<T>` and `#[repr(transparent)]` wrapper types such as our custom `KBox<T>` have the null pointer optimization only if `T: Sized`. Thus remove the `Zeroable` implementation for the unsized case. Link: https://doc.rust-lang.org/stable/std/option/index.html#representation [1] Reported-by: Alice Ryhl <aliceryhl@google.com> Closes: https://lore.kernel.org/rust-for-linux/CAH5fLghL+qzrD8KiCF1V3vf2YcC6aWySzkmaE2Zzrnh1gKj-hw@mail.gmail.com/ Cc: stable@vger.kernel.org # v6.12+ (a custom patch will be needed for 6.6.y) Fixes: 38cde0b ("rust: init: add `Zeroable` trait and `init::zeroed` function") Signed-off-by: Benno Lossin <benno.lossin@proton.me> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org> Link: https://lore.kernel.org/r/20250305132836.2145476-1-benno.lossin@proton.me [ Added Closes tag and moved up the Reported-by one. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 374908a commit df27cef

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

rust/kernel/init.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,17 +1418,14 @@ impl_zeroable! {
14181418
// SAFETY: `T: Zeroable` and `UnsafeCell` is `repr(transparent)`.
14191419
{<T: ?Sized + Zeroable>} UnsafeCell<T>,
14201420

1421-
// SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee).
1421+
// SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee:
1422+
// https://doc.rust-lang.org/stable/std/option/index.html#representation).
14221423
Option<NonZeroU8>, Option<NonZeroU16>, Option<NonZeroU32>, Option<NonZeroU64>,
14231424
Option<NonZeroU128>, Option<NonZeroUsize>,
14241425
Option<NonZeroI8>, Option<NonZeroI16>, Option<NonZeroI32>, Option<NonZeroI64>,
14251426
Option<NonZeroI128>, Option<NonZeroIsize>,
1426-
1427-
// SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee).
1428-
//
1429-
// In this case we are allowed to use `T: ?Sized`, since all zeros is the `None` variant.
1430-
{<T: ?Sized>} Option<NonNull<T>>,
1431-
{<T: ?Sized>} Option<KBox<T>>,
1427+
{<T>} Option<NonNull<T>>,
1428+
{<T>} Option<KBox<T>>,
14321429

14331430
// SAFETY: `null` pointer is valid.
14341431
//

0 commit comments

Comments
 (0)