From nobody Fri Dec 19 03:43:20 2025 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 B1C32C001B0 for ; Mon, 14 Aug 2023 08:48:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234172AbjHNIsf (ORCPT ); Mon, 14 Aug 2023 04:48:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235074AbjHNIr5 (ORCPT ); Mon, 14 Aug 2023 04:47:57 -0400 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D8C9E1737; Mon, 14 Aug 2023 01:47:51 -0700 (PDT) Date: Mon, 14 Aug 2023 08:47:32 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1692002870; x=1692262070; bh=RmXK6p609GEJrLnFk9jfesnOhQMMA5F6nnV0NJXuXV4=; 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=BtvXsbftFR8N1LTgE1UOZEG9t4qE9EuuVvtX+vYczOwYQTDCI6do7dB9Oe+P+mXvC vO0kWckuR+N7GqoP1Mg1O2VQ4DL2GP6nmCsdCdiprbCXpVa6Ye7ySV0wRp216IV3TK 5vr/VEITqjSoZSiuxuvzygMTNeH5nDNVpkS4dBuuRf15eGe+vpTcNi6iF1HvltYKBw Rdbq5L3pndnFSRvAsvD8gA0FYBfhXin9XUlaV2f8RfixAcdl2OXBrBrjWFRoCpSteq 3eKTzrZ/Nh+Ffvxz30ouhZQUijNWuR9lSNqnyvsKOPx5UF2g7jfF3/0EhabGZXN6E0 UzN7Us8ciVtTw== 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, patches@lists.linux.dev, Martin Rodriguez Reboredo Subject: [PATCH v4 10/13] rust: init: implement `Zeroable` for `UnsafeCell` and `Opaque` Message-ID: <20230814084602.25699-11-benno.lossin@proton.me> In-Reply-To: <20230814084602.25699-1-benno.lossin@proton.me> References: <20230814084602.25699-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. Reviewed-by: Gary Guo Reviewed-by: Martin Rodriguez Reboredo Signed-off-by: Benno Lossin --- v3 -> v4: - added Reviewed-by's from Gary and Martin. v2 -> v3: - also implement Zeroable for `UnsafeCell` when `T: Zeroable`, - use `impl_zeroable!` instead of `derive(Zeroable)`. rust/kernel/init.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs index b54f32017d00..f91ecd3b1edf 100644 --- a/rust/kernel/init.rs +++ b/rust/kernel/init.rs @@ -212,11 +212,12 @@ use crate::{ error::{self, Error}, sync::UniqueArc, - types::ScopeGuard, + types::{Opaque, ScopeGuard}, }; use alloc::boxed::Box; use core::{ alloc::AllocError, + cell::UnsafeCell, convert::Infallible, marker::PhantomData, mem::MaybeUninit, @@ -1159,6 +1160,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