Message ID | 20230729090838.225225-11-benno.lossin@proton.me |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:918b:0:b0:3e4:2afc:c1 with SMTP id s11csp959981vqg; Sat, 29 Jul 2023 03:47:03 -0700 (PDT) X-Google-Smtp-Source: APBJJlF727FPbjG6d0uiEHumAG+P3VwFsNYip2qT7n3MTOQAzspOpo7mnN6PPnQJzxXk0/MlXoIb X-Received: by 2002:a17:902:f7cb:b0:1b8:9b5e:6697 with SMTP id h11-20020a170902f7cb00b001b89b5e6697mr3615971plw.68.1690627622927; Sat, 29 Jul 2023 03:47:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690627622; cv=none; d=google.com; s=arc-20160816; b=IWR/xXB5hsU4GfALjCylcV80QnG4EStSAf8gMbMtQyVrON1JsCAdg6MqM0om220Fh6 c4PJ2/VCYBabfzfcf20pedakGXT/VsRNSh8QWJpcC7hzyXpDmBtPHvRSzdJ+HVuxOegf xDQEsnk2ToPzX6k4TydGEDfuFZpLKgL6DM4/FLyWmFyiM4PA7OX5t7RDNDJjYi4Eeihi lHobhZN09QwQCAz0hgnE77RK9pKe+M+vutFqiImgSAoldzv4kYJEIfIiHcMDopa/2xUq itbuK6ODb9aOFFE5Q8BbUMZtAqU4AHC9ceCcIE/udTjoPWqAoyVgtdFUolh8Wan3sb/Y T6Hw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :feedback-id:references:in-reply-to:message-id:subject:cc:from:to :dkim-signature:date; bh=3zNIMZmZNDCpyXxxyP7KXlpj+oRhkgVcL+Ru9AKEqMQ=; fh=qm1bOEYzVYZKL4fg86TgWb8D+QJA9mGpUhKFp2A6Xio=; b=HpEfwwsTQn0m667XS/4kY+RUCppi0IJZ9yS9PHInccJXnkWmMmYXyYtBXpnNvPQlrb rp1HeVzImCi61ofPhgHP9DwmxSxcmnj9Kv0g4OUH2VYaqWNrgSJHJLdCv/KLmzwDeJl+ QG+daxhWZru61jeVMu54yhrDk+ojG2sBEcKzGVnK0pD4MFpl7M2Zy6WqpUBlZOL7RiPn gMiw1veSx/9Bb9xRHTyOc8cYAr+6hffpVL9NZGqdPBWJG2iTw6KagYFW+xAM6Y+rKwfT OaILHBLxwQvDmEUwJJoDahAv8DAbt37CVygHPZTSPLFpCwVwyJ7UJN1DsU78byjyqyXI t4Zg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=chbVs6fH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p4-20020a170902e74400b001b3d27ca12fsi4621920plf.207.2023.07.29.03.46.47; Sat, 29 Jul 2023 03:47:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=chbVs6fH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231206AbjG2JLo (ORCPT <rfc822;hanasaki@gmail.com> + 99 others); Sat, 29 Jul 2023 05:11:44 -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 <rfc822;linux-kernel@vger.kernel.org>); 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 <linux-kernel@vger.kernel.org>; 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 <ojeda@kernel.org>, Wedson Almeida Filho <wedsonaf@gmail.com>, Alex Gaynor <alex.gaynor@gmail.com> From: Benno Lossin <benno.lossin@proton.me> Cc: Boqun Feng <boqun.feng@gmail.com>, Gary Guo <gary@garyguo.net>, =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= <bjorn3_gh@protonmail.com>, Benno Lossin <benno.lossin@proton.me>, Alice Ryhl <aliceryhl@google.com>, Andreas Hindborg <nmi@metaspace.dk>, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 10/13] rust: init: implement `Zeroable` for `UnsafeCell<T>` and `Opaque<T>` 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-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=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772751549896776767 X-GMAIL-MSGID: 1772751549896776767 |
Series |
Quality of life improvements for pin-init
|
|
Commit Message
Benno Lossin
July 29, 2023, 9:10 a.m. UTC
`UnsafeCell<T>` and `T` have the same layout so if `T` is `Zeroable`
then so should `UnsafeCell<T>` be. This allows using the derive macro
for `Zeroable` on types that contain an `UnsafeCell<T>`.
Since `Opaque<T>` contains a `MaybeUninit<T>`, all bytes zero is a valid
bit pattern for that type.
Signed-off-by: Benno Lossin <benno.lossin@proton.me>
---
v2 -> v3:
- also implement Zeroable for `UnsafeCell<T>` when `T: Zeroable`,
- use `impl_zeroable!` instead of `derive(Zeroable)`.
rust/kernel/init.rs | 7 +++++++
1 file changed, 7 insertions(+)
Comments
On 7/29/23 06:10, Benno Lossin wrote: > `UnsafeCell<T>` and `T` have the same layout so if `T` is `Zeroable` > then so should `UnsafeCell<T>` be. This allows using the derive macro > for `Zeroable` on types that contain an `UnsafeCell<T>`. > Since `Opaque<T>` contains a `MaybeUninit<T>`, all bytes zero is a valid > bit pattern for that type. > > Signed-off-by: Benno Lossin <benno.lossin@proton.me> > --- > [...] Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
On Sat, 29 Jul 2023 09:10:19 +0000 Benno Lossin <benno.lossin@proton.me> wrote: > `UnsafeCell<T>` and `T` have the same layout so if `T` is `Zeroable` > then so should `UnsafeCell<T>` be. This allows using the derive macro > for `Zeroable` on types that contain an `UnsafeCell<T>`. > Since `Opaque<T>` contains a `MaybeUninit<T>`, all bytes zero is a valid > bit pattern for that type. > > Signed-off-by: Benno Lossin <benno.lossin@proton.me> Reviewed-by: Gary Guo <gary@garyguo.net> > --- > v2 -> v3: > - also implement Zeroable for `UnsafeCell<T>` 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 { > > // SAFETY: Type is allowed to take any value, including all zeros. > {<T>} MaybeUninit<T>, > + // SAFETY: Type is allowed to take any value, including all zeros. > + {<T>} Opaque<T>, > + > + // SAFETY: `T: Zeroable` and `UnsafeCell` is `repr(transparent)`. > + {<T: ?Sized + Zeroable>} UnsafeCell<T>, > > // SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee). > Option<NonZeroU8>, Option<NonZeroU16>, Option<NonZeroU32>, Option<NonZeroU64>,
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 { // SAFETY: Type is allowed to take any value, including all zeros. {<T>} MaybeUninit<T>, + // SAFETY: Type is allowed to take any value, including all zeros. + {<T>} Opaque<T>, + + // SAFETY: `T: Zeroable` and `UnsafeCell` is `repr(transparent)`. + {<T: ?Sized + Zeroable>} UnsafeCell<T>, // SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee). Option<NonZeroU8>, Option<NonZeroU16>, Option<NonZeroU32>, Option<NonZeroU64>,