From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-10629.protonmail.ch (mail-10629.protonmail.ch [79.135.106.29]) (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 91B4123370F for ; Wed, 5 Mar 2025 12:05:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=79.135.106.29 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741176339; cv=none; b=AplU9t6PZqD9z/6Euw7Ozov5fdWwVsaZumDUgi51y3lgTpUOPqdLJ39Uvr6CCd7fdCu+rKurua/5TnvBaTUBVzGEq9XOVnX9X9OVdoWxtCQApfeHzGSmirFCDM25zYpS+ISXRLE3hmNysDvGW+mv1EsgZnHqT6DLvIoFmtl8Hlg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741176339; c=relaxed/simple; bh=9pybT0/UMgEz4/wJUXE2cHJv2mhc5VAlGwBfHsQcdhA=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ZtfbrlnpOlT/8EcUQHrSm+nqraIlV/vC2L1x0pOoB6DNhfgofIpyS537Y7oOgNrPucBilax5neZ4JnUW2TZqH7dpVRkFZ0JfN4kGb3n1bQzYHCxjEre7+W8OBu5qi89L+Uxh7FSGAwv1nkw9oE29hu6XOgTICn/BtXOFVk4Qozk= 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=KfAZBhIk; arc=none smtp.client-ip=79.135.106.29 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="KfAZBhIk" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1741176334; x=1741435534; bh=l3OwM3zQ666uofylTyexnRNIzrNX4/a4VSd9lnoA0s4=; 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=KfAZBhIkHAdtsWMP4u7MfD5ESw+1VGRWCiNJjszAptWoHh8wPMmYvHqP8Ig5zrSN3 CJHjJKhkXwKq9lJMwNPZpIHbW4JUWlTI+uhKgZd8tlzc39RgxoQW/GlKPDhfi6Eoxf ye8+Z0p0/QaLHdHV7Y3hOstP4kiWnxXM7iw4UwsDAPOmOHNcCVKqLQdnlHp0fPfS1p ofVBPu76wNk4AP05mWRqdTcZkfu9wBCIziySv3nKTMTyExOTPaV5NTKY4roD7y3TWs I92GHvuAR8t98pLi3OXdws6l4CVrWNJa722REOaOh41kgX8g+TuT8VFqjK7AWkoDMZ TntoKgtHUXOYw== Date: Wed, 05 Mar 2025 12:05:28 +0000 To: Andreas Hindborg From: Benno Lossin Cc: Danilo Krummrich , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Alice Ryhl , 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: <87a59zen0l.fsf@kernel.org> 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: 56d0fd40d987fa40038aa6364ee7ae75ae9f27d9 Precedence: bulk X-Mailing-List: linux-kernel@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 12:26 PM CET, Andreas Hindborg wrote: > "Benno Lossin" writes: > >> In order to make pin-init a standalone crate, move kernel-specific code >> directly into the kernel crate. Since `Opaque` and `KBox` are part >> 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 optimizatio= n guarantee). >> +// >> +// In this case we are allowed to use `T: ?Sized`, since all zeros is t= he `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. --- Cheers, Benno