Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp1282576rwl; Wed, 29 Mar 2023 15:37:50 -0700 (PDT) X-Google-Smtp-Source: AKy350YE93lanCcZOPphVL0Lq9WJx1q3BGWg9qOJbYfwl+SFFo6glpLR6lx9XtzaZFI4Du5kSjgx X-Received: by 2002:a17:90b:33ca:b0:23f:1c6d:2b60 with SMTP id lk10-20020a17090b33ca00b0023f1c6d2b60mr3733602pjb.5.1680129470451; Wed, 29 Mar 2023 15:37:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680129470; cv=none; d=google.com; s=arc-20160816; b=e/NXQSu0r/onMinThIODu7dm1zeuitZUF0bbki075hJQ/ToBL/le9ubkgxDcD3OxZq 4hUwVJCdwTotDjFPPTtvK74M7Fe6hHTWqzcMWXESWc7FKkef6ZS0iHfOr5t8OPo31uQ3 iW9n8ypcKDrHkg4MbA8ypyhJDHYbhUB8CC8ZffSs/T+nEfx1DvxlGnIm5WdEBfmcPiRU 2pbi/J3C1eaeaoBHtynJ0fv5kDf+AZRnK6No3LUaJKEdf6eT/mDF5AiK/c+sZZb0GYiF 3RaS+E09PjxIYOIA2PShe0vzVhzRQ+/oMgHeie0x0lAAEBX2Kp6rwcOoWPoCJCJhFp19 0OYA== 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:message-id:subject:cc:from:to:dkim-signature:date; bh=CqqTxRO7PGWaLkf8AosWoBeoOehZuoxHX0UaJT8k+Lg=; b=HCF9jmSepHm5mQ9Cvt0ujq7QBA+HxUBKkExqFQ8r1viNmoimbYHb28VKEVUz0cOsOQ l6kVcS6Kham4AuhcpKkxBhB2I6K8BbHIms7TleLWFO/pqBWN8+vfq0nSKeg3i/AZDp1b Roz/9sQ/+CS9JiSrXpj7DPCe3SMHH+m9rtjYZvn1rpyZa41L+YedVteZpuMp/FPGoeNB 4a8OAUuXIaHPCCWZY/yau7W1Bv0f+Vx6tVrZvL4ghzz26IbHjKJ/yGUOjUHUwe/dSv14 XU4buypYIBmAhHHY0iL50qRa0mj4ZBzGP/Qm9CtZ0vY0I7EGX695sNfK5EwDPa9qaM5X sXow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b=koq4wetA; 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=protonmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ob11-20020a17090b390b00b002403f301ebfsi2406403pjb.15.2023.03.29.15.37.37; Wed, 29 Mar 2023 15:37:50 -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=@protonmail.com header.s=protonmail3 header.b=koq4wetA; 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=protonmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231204AbjC2We4 (ORCPT + 99 others); Wed, 29 Mar 2023 18:34:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230386AbjC2Wed (ORCPT ); Wed, 29 Mar 2023 18:34:33 -0400 Received: from mail-0301.mail-europe.com (mail-0301.mail-europe.com [188.165.51.139]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 26AD16A42 for ; Wed, 29 Mar 2023 15:34:01 -0700 (PDT) Date: Wed, 29 Mar 2023 22:33:35 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1680129221; x=1680388421; bh=CqqTxRO7PGWaLkf8AosWoBeoOehZuoxHX0UaJT8k+Lg=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=koq4wetAKqlFccSGms/+P/N5JD44HC5I7nSJGPQL9gze0kK9Xiqzgnc/DxM/P9WtS CVbYV1dyO3bkSBI7v123sA+9UyWX+UsePaMP6564+UMQzq/fjzQrKkAVktY/5KydCQ TzoNFxV172B+DcyBMIqgXXF4oiMVU5WqO0nYS2RJZE97nUbhhZSjJsa5AlAGVh/wZh 2EnduvYS4iP+56bcmI1+VYq3yDR03G2CHBhmYA3EH4X/3WqCoUuJvOJt+esUXSf1c3 7la0Zs9JcQibKI7GKKFzo/wuUBykw3gYI/4pspUTsUaq3ZStwbjhUXmi//1XigS6xr QGRpoy64ii58g== To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Alice Ryhl From: y86-dev@protonmail.com Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH v3 09/13] rust: init: add `Zeroable` trait and `init::zeroed` function Message-ID: <20230329223239.138757-10-y86-dev@protonmail.com> Feedback-ID: 40624463:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_PASS 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: X-Mailing-List: linux-kernel@vger.kernel.org From: Benno Lossin Add the `Zeroable` trait which marks types that can be initialized by writing `0x00` to every byte of the type. Also add the `init::zeroed` function that creates an initializer for a `Zeroable` type that writes `0x00` to every byte. Signed-off-by: Benno Lossin --- rust/kernel/init.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs index 3358f14beffb..a923546696ce 100644 --- a/rust/kernel/init.rs +++ b/rust/kernel/init.rs @@ -1254,3 +1254,68 @@ pub unsafe trait PinnedDrop: __internal::HasPinData = { /// automatically. fn drop(self: Pin<&mut Self>, only_call_from_drop: __internal::OnlyCal= lFromDrop); } + +/// Marker trait for types that can be initialized by writing just zeroes. +/// +/// # Safety +/// +/// The bit pattern consisting of only zeroes is a valid bit pattern for t= his type. In other words, +/// this is not UB: +/// +/// ```rust,ignore +/// let val: Self =3D unsafe { core::mem::zeroed() }; +/// ``` +pub unsafe trait Zeroable {} + +/// Create a new zeroed T. +/// +/// The returned initializer will write `0x00` to every byte of the given = `slot`. +#[inline] +pub fn zeroed() -> impl Init { + // SAFETY: Because `T: Zeroable`, all bytes zero is a valid bit patter= n for `T` + // and because we write all zeroes, the memory is initialized. + unsafe { + init_from_closure(|slot: *mut T| { + slot.write_bytes(0, 1); + Ok(()) + }) + } +} + +macro_rules! impl_zeroable { + ($($t:ty, )*) =3D> { + $(unsafe impl Zeroable for $t {})* + }; +} + +impl_zeroable! { + // SAFETY: All primitives that are allowed to be zero. + bool, + char, + u8, u16, u32, u64, u128, usize, + i8, i16, i32, i64, i128, isize, + f32, f64, + // SAFETY: There is nothing to zero. + core::marker::PhantomPinned, Infallible, (), +} + +// SAFETY: We are allowed to zero padding bytes. +unsafe impl Zeroable for [T; N] {} + +// SAFETY: There is nothing to zero. +unsafe impl Zeroable for PhantomData {} + +// SAFETY: `null` pointer is valid. +unsafe impl Zeroable for *mut T {} +unsafe impl Zeroable for *const T {} + +macro_rules! impl_tuple_zeroable { + ($(,)?) =3D> {}; + ($first:ident, $($t:ident),* $(,)?) =3D> { + // SAFETY: All elements are zeroable and padding can be zero. + unsafe impl<$first: Zeroable, $($t: Zeroable),*> Zeroable for ($fi= rst, $($t),*) {} + impl_tuple_zeroable!($($t),* ,); + } +} + +impl_tuple_zeroable!(A, B, C, D, E, F, G, H, I, J); -- 2.39.2