From patchwork Wed Mar 29 22:33:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: y86-dev X-Patchwork-Id: 76841 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp730760vqo; Wed, 29 Mar 2023 15:39:14 -0700 (PDT) X-Google-Smtp-Source: AKy350b8sC7M0EEpwaaahqxMBcDZmYg5ZUh/4SH0KhU+fquJDIsjZ3X0Y7J834ImXaGI4ascRP0M X-Received: by 2002:a17:90b:4d83:b0:23f:9445:318e with SMTP id oj3-20020a17090b4d8300b0023f9445318emr3864695pjb.3.1680129554027; Wed, 29 Mar 2023 15:39:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680129554; cv=none; d=google.com; s=arc-20160816; b=soJCBqyn5tHPnuoqnypM8XD+eEjtUrIjijkZ+3IJqiymT0droUkWi0fEsU539dhIM7 QPs5hNPg7RfiBWLUO8VAy+H4ZsGtdAOlGoQswjUNQIHpE11wBcPZE8VzFaOD88nyPMbo lx0dCkjrqz960fgudiaEoZt+hE31NfYCccBTNB2jRnY0K5k5sLZf9GhpnQR8LPRc/dd+ 8Az6ZIikXjtRKfIg6Y+/Jve3U8A+HkT+khdW4UCP+afbx/LU8fuLsKcZDQmowbYQc7gK j9VcNbaRLV4c6yI0Uj/Nc/L3D1boO7n275Q5HAF4n4k9FIkiiMzo/P//7I9xQ0femdsX xD+A== 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 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.38.59; Wed, 29 Mar 2023 15:39:14 -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 S229874AbjC2We5 (ORCPT + 99 others); Wed, 29 Mar 2023 18:34:57 -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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761743527391168153?= X-GMAIL-MSGID: =?utf-8?q?1761743527391168153?= 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 Reviewed-by: Gary Guo Reviewed-by: Alice Ryhl --- rust/kernel/init.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) -- 2.39.2 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::OnlyCallFromDrop); } + +/// 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 this type. In other words, +/// this is not UB: +/// +/// ```rust,ignore +/// let val: Self = 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 pattern 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, )*) => { + $(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 { + ($(,)?) => {}; + ($first:ident, $($t:ident),* $(,)?) => { + // SAFETY: All elements are zeroable and padding can be zero. + unsafe impl<$first: Zeroable, $($t: Zeroable),*> Zeroable for ($first, $($t),*) {} + impl_tuple_zeroable!($($t),* ,); + } +} + +impl_tuple_zeroable!(A, B, C, D, E, F, G, H, I, J);