From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 1DB4E7C for ; Sat, 29 Jul 2023 09:11:11 +0000 (UTC) Received: from mail-4322.protonmail.ch (mail-4322.protonmail.ch [185.70.43.22]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D4F5F449F for ; Sat, 29 Jul 2023 02:10:37 -0700 (PDT) Date: Sat, 29 Jul 2023 09:10:19 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1690621832; x=1690881032; bh=3zNIMZmZNDCpyXxxyP7KXlpj+oRhkgVcL+Ru9AKEqMQ=; 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; b=chbVs6fHFDVnXKLqa/IdGHFF5wg9kxo0llJBCweB/b7pRciP4kY9xe5QSS3DJ97WN uuwVujcl8aAnoN7sMarSEODG0XBsxPWGk9vkwzEf1TDMbgU3t0UkktB6ubMvudcaR3 BF98jn3lt4nCyIhWqxzhgqIWflFdHbSk1MQNDtq43mkoW3FgD+CFxTTmkmL/3nJqwZ 2dIBXYQJrjoe4V9Pv99c2YkGP75+1+LdHEUcsqT4XLwp18HuOtsubnoibAjy5VKiLb pZHaZwjcpy1ndewgQ6f1t361S+Hu83O7O2ERAQxw0mnZsFJnRg+CvcW2VwMXcrVKNE zzLMZTVZagUSQ== To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor From: Benno Lossin Cc: Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Andreas Hindborg , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 10/13] rust: init: implement `Zeroable` for `UnsafeCell` and `Opaque` Message-ID: <20230729090838.225225-11-benno.lossin@proton.me> In-Reply-To: <20230729090838.225225-1-benno.lossin@proton.me> References: <20230729090838.225225-1-benno.lossin@proton.me> Feedback-ID: 71780778:user:proton 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 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net `UnsafeCell` and `T` have the same layout so if `T` is `Zeroable` then so should `UnsafeCell` be. This allows using the derive macro for `Zeroable` on types that contain an `UnsafeCell`. Since `Opaque` contains a `MaybeUninit`, all bytes zero is a valid bit pattern for that type. Signed-off-by: Benno Lossin --- v2 -> v3: - also implement Zeroable for `UnsafeCell` when `T: Zeroable`, - use `impl_zeroable!` instead of `derive(Zeroable)`. rust/kernel/init.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs index af96d4acc26b..06ecab4901f2 100644 --- a/rust/kernel/init.rs +++ b/rust/kernel/init.rs @@ -212,10 +212,12 @@ use crate::{ error::{self, Error}, sync::UniqueArc, + types::Opaque, }; use alloc::boxed::Box; use core::{ alloc::AllocError, + cell::UnsafeCell, convert::Infallible, marker::PhantomData, mem::MaybeUninit, @@ -1157,6 +1159,11 @@ macro_rules! impl_zeroable { =20 // SAFETY: Type is allowed to take any value, including all zeros. {} MaybeUninit, + // SAFETY: Type is allowed to take any value, including all zeros. + {} Opaque, + + // SAFETY: `T: Zeroable` and `UnsafeCell` is `repr(transparent)`. + {} UnsafeCell, =20 // SAFETY: All zeros is equivalent to `None` (option layout optimizati= on guarantee). Option, Option, Option, Option, --=20 2.41.0