From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-40133.protonmail.ch (mail-40133.protonmail.ch [185.70.40.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1990124502F for ; Wed, 5 Mar 2025 12:17:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.40.133 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741177046; cv=none; b=W7U+GBetYw9vKorBCTzkbHQNATrJa1Gebymv1yHT/m3k4EjQC9PvsTz6MkhHCsXkXYLBuqJ6xRU2rWNPb5B9j6Q3m7jJzelm3TRnbyuZv4Db0yL5CgJFZOCdpXuqgNTx4jgDKOf/i3OuVlmyDeDOT8q9KJ9KwenmXaaeWiQZv+Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741177046; c=relaxed/simple; bh=h3LIfw0I/qKn/+CSvk5G5jME+xih+7mH8xxfN82sOxE=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ppEY2lnMuP1DlzCUadwux9HhX1zVHjP+QLJqOszmsQRgW9HZZyUg9KL3flAhqw0K3GJTP6Brqg8xBKzdA4qeEtAToc/wNVDsGWgpjPF43dvvGsrrFepVsx782Kgchas2YhYCED+DUSOt2+8CQK+4dFocXMdBR8VzVMCLU7CUUGU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me; spf=pass smtp.mailfrom=proton.me; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b=f3NtY/bM; arc=none smtp.client-ip=185.70.40.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=proton.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="f3NtY/bM" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1741177042; x=1741436242; bh=+GSH/hGM0OM2xxx0GMJ7cgHDZ+8cY8JDPb0xY0nCJMc=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector:List-Unsubscribe:List-Unsubscribe-Post; b=f3NtY/bMlHjj1xRn53fMb6LBZ95iZDwS/cyXyohz46VfWXb7+B50S6ZNioi2B5TwS SFL8OdKv6uDC0h9BcKAzWBrmXtiBEgye5ZpXrzYvSGn/aYyn1RqgB08GNrD7u/n2FJ 0hLrCAmhtXCVpFLyeQdNTeicRm8d6KcydTSOdFParG2l9Oe+BfibwIq7STInHoLfSV nVpvv08dM3MmskXYLQdAnTcSf98Hc/osog6/MrH+87Qp/Kyybj1T5DE6wrMS+QYxMP GIz0hhY8TCxlmteJ5XMdNfDGjpMW1GkKZ83Lylm5iyhSv0FvBMz/zj8aGVjU45wnK4 u2/LQeDOi75qw== Date: Wed, 05 Mar 2025 12:17:18 +0000 To: Alice Ryhl From: Benno Lossin Cc: Andreas Hindborg , Danilo Krummrich , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Trevor Gross , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 09/22] rust: pin-init: move impl `Zeroable` for `Opaque` and `Option>` into the kernel crate Message-ID: In-Reply-To: References: <20250304225245.2033120-1-benno.lossin@proton.me> <20250304225245.2033120-10-benno.lossin@proton.me> <87a59zen0l.fsf@kernel.org> Feedback-ID: 71780778:user:proton X-Pm-Message-ID: f5e96b7eb593c9b24e4fd2b57a30ba5166e79fe2 Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Wed Mar 5, 2025 at 1:11 PM CET, Alice Ryhl wrote: > On Wed, Mar 5, 2025 at 1:05=E2=80=AFPM Benno Lossin wrote: >> >> On Wed Mar 5, 2025 at 12:26 PM CET, Andreas Hindborg wrote: >> > "Benno Lossin" writes: >> > >> >> In order to make pin-init a standalone crate, move kernel-specific co= de >> >> directly into the kernel crate. Since `Opaque` and `KBox` are p= art >> >> of the kernel, move their `Zeroable` implementation into the kernel >> >> crate. >> >> >> >> Signed-off-by: Benno Lossin >> >> --- >> >> 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 =3D Box; >> >> >> >> +// SAFETY: All zeros is equivalent to `None` (option layout optimiza= tion guarantee). >> >> +// >> >> +// In this case we are allowed to use `T: ?Sized`, since all zeros i= s the `None` variant and there >> >> +// is no problem with a VTABLE pointer being null. >> >> +unsafe impl Zeroable for Option> = {} >> > >> > Could you elaborate the statement related to vtable pointers? How does >> > that come into play for `Option>`? Is it for fat pointers to >> > trait objects? >> >> Yes it is for fat pointers, if you have a `x: *mut dyn Trait`, then you >> aren't allowed to write all zeroes to `x`, because the VTABLE pointer >> (that is part of the fat pointer) is not allowed to be null. >> >> Now for `Option>`, this doesn't matter, as there if the normal >> pointer part of the fat pointer is all zeroes, then the VTABLE pointer >> part is considered padding bytes, as it's the `None` variant. > > The standard library only guarantees that all zeros is valid for > Option> when T:Sized and A=3DGlobal. > https://doc.rust-lang.org/stable/std/option/index.html#representation Oh! That's a problem then... I'll remove that then (and I can also get rid of the `ZeroableOption` trait). We should also backport (& fix it in mainline), I'll submit a patch shortly. --- Cheers, Benno