From nobody Wed Feb 11 05:36:59 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 02134C0015E for ; Sat, 29 Jul 2023 09:11:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231622AbjG2JLp (ORCPT ); Sat, 29 Jul 2023 05:11:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40728 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231693AbjG2JLU (ORCPT ); Sat, 29 Jul 2023 05:11:20 -0400 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D689B44BC 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 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" `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 Reviewed-by: Gary Guo Reviewed-by: Martin Rodriguez Reboredo --- 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