Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp4885797rwl; Mon, 3 Apr 2023 11:00:21 -0700 (PDT) X-Google-Smtp-Source: AKy350b0AA2NdeG0DjrjP/keKMWdDoP4bfqEXYvcLZ/ERg0mUEadZroUckgrP7WKCQ+OxO9Zphts X-Received: by 2002:aa7:c157:0:b0:4a2:588f:b3c5 with SMTP id r23-20020aa7c157000000b004a2588fb3c5mr92674edp.21.1680544821167; Mon, 03 Apr 2023 11:00:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680544821; cv=none; d=google.com; s=arc-20160816; b=U1FYUvQZ1oMw9xs1pvgHZIfUJbRZ2iNuZy4Ahq02OYdKU4l+EN+feqvmgAIomXWuhH 86YAvdRQSsS/++eUY8wBsUrEEG4X8eoxPLG1IYqpVckikZ7EloEhm6Sy0dshzD3R1/OQ NkHhjLxS5JuvOV6wUMy7mA8yA3Mi/6EZAAzpyRIjfz5R+LJHCapXC9uV3E6bYk29Rx9a FumJH7jkOXk40ye+8/UPrgMiZuJ7qBXK7szBKShp0NSzRM9RCXj4QzhodKBIG0KrboHu 5fgplZS5R39oX8I6ntP+deKsYh74m9OevjX3PnPbbN/bNQHHTl8dcd3RfqNVDraLGxM4 T9Xw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :content-language:references:cc:to:subject:from:user-agent :mime-version:date:message-id:feedback-id:dkim-signature :dkim-signature; bh=sS+MOVaDNMXFgxiahtgnHlNntb5kLKGWRvsA/jgg7hQ=; b=kcF5CTrJMLMn6t2StfunwEOfRxRSgGqKh2DHiqxFr2EXLKNVVkiZrswEIZ2mruJPdI yIe8Y8ru+G+ApyiTrmKiHktU7izcg5p5bt2ouy3xjgNcqkTstn88bs+StT8G/+BFlDzu mFIV/1StqB+kavXucrt3aEny4oMjC6AGZnxphJddZXMAsdT6RLKvrSdSG2vWopO6ZnGo hWEsXacaJbMBM++de0HCwiYwUfYxqibt0P4bh+aAhZe9xJMXud1JXSXvFk+76XE3mPJ0 i3K2ZdEw6zDozRjh9WqntMBxLbYZOw7MSUlxneaie486EbyX5Da58ZjuJCkkDyiJnnQ8 23Rg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ryhl.io header.s=fm2 header.b=afzYgKG0; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=qOb9nWog; 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=NONE sp=NONE dis=NONE) header.from=ryhl.io Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v14-20020a056402184e00b004c5a8aa02fcsi8927273edy.435.2023.04.03.10.59.56; Mon, 03 Apr 2023 11:00:21 -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=@ryhl.io header.s=fm2 header.b=afzYgKG0; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=qOb9nWog; 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=NONE sp=NONE dis=NONE) header.from=ryhl.io Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233005AbjDCR6J (ORCPT + 99 others); Mon, 3 Apr 2023 13:58:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48058 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232911AbjDCR5y (ORCPT ); Mon, 3 Apr 2023 13:57:54 -0400 Received: from out5-smtp.messagingengine.com (out5-smtp.messagingengine.com [66.111.4.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2E6BA2134; Mon, 3 Apr 2023 10:57:29 -0700 (PDT) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id E390C5C00FD; Mon, 3 Apr 2023 13:57:22 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Mon, 03 Apr 2023 13:57:22 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ryhl.io; h=cc:cc :content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to; s=fm2; t= 1680544642; x=1680631042; bh=sS+MOVaDNMXFgxiahtgnHlNntb5kLKGWRvs A/jgg7hQ=; b=afzYgKG0aV7+pi1V5UGARP5Iz8LuehOTxGlDd/9zf2SrttyHwj6 2NtOP6SlulHrUFxdFTPXT1QJ+nYmGxiABCzDPZ6yEKLZZznAgl2LaExk+VZG7as4 NcSpPfmI0UwobjbIgn16XengzehciMWyXVNnqdXYJNjK0n5VRJHEMfmarR7/7Hen tunV30Hb7BqGv5e9XkK1OrjRiY5l+1SVtfWnVoNhtPj/P/6O+bWJcVhx/QsrSZeU 4YMTsghqWBAItco1yeQ70Y+y1LpZYWMuTZ93KX+sN4VT36XbJu9oZleDiyAqasqL uuE1a5gIbmoyDYCmA21Q8TDox9DjpILrXCg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1680544642; x=1680631042; bh=sS+MOVaDNMXFgxiahtgnHlNntb5kLKGWRvs A/jgg7hQ=; b=qOb9nWogM2ZEOfNTFTnsXEzZcaFSWOw+y0iXSKqrnvBBS/EXtj0 z1T8GO35tg96iB7ipxADMXmozu7xW2APBB8OYOPAu79obCwQGr2WebdEMy1LDEi7 hZMS3RgUH78sPGObUr7Uo1pWBnS/mhC7bP0LtvJWDM/rMk2CJ+QAG5kXpFQVUtVg hoScmXaGIGdKPbpQmzKhketSIIIhTIkndk8+2bUwmdFDzjMPDjho8pAIsoL0dxkP w67sLc/7MIyc+Pk23jxrTB+BIl9m7o8yvlBU0822jgqaUTDBZcIv1y0Se7c/odwo 8AiccpCdQnBaSKIJ50U3qPN/Twqw3daq22Q== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrvdeijedguddvtdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefkffggfgfhuffvvehfjggtgfesthejredttdefjeenucfhrhhomheptehl ihgtvgcutfihhhhluceorghlihgtvgesrhihhhhlrdhioheqnecuggftrfgrthhtvghrnh ephfehueeileevjeefkeetvdffveffudeuhffgtedvuefgiefgiedvjeegvdejleejnecu vehluhhsthgvrhfuihiivgepvdenucfrrghrrghmpehmrghilhhfrhhomheprghlihgtvg esrhihhhhlrdhioh X-ME-Proxy: Feedback-ID: i56684263:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 3 Apr 2023 13:57:20 -0400 (EDT) Message-ID: Date: Mon, 3 Apr 2023 19:56:29 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.0 From: Alice Ryhl Subject: Re: [PATCH v5 11/15] rust: init: add `Zeroable` trait and `init::zeroed` function To: Benno Lossin Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Alice Ryhl , Andreas Hindborg , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=c3=b6rn_Roy_Baron?= References: <20230403154422.168633-1-y86-dev@protonmail.com> <20230403160511.174894-4-y86-dev@protonmail.com> Content-Language: en-US In-Reply-To: <20230403160511.174894-4-y86-dev@protonmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.3 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS,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 On 4/3/23 18:05, Benno Lossin wrote: > 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 > Cc: Gary Guo > Cc: Alice Ryhl > Cc: Andreas Hindborg Reviewed-by: Alice Ryhl I have two minor suggestions - take them or leave them. > +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(()) > + }) > + } > +} You don't need `T: Unpin` here. > +macro_rules! impl_zeroable { > + ($($({$($generics:tt)*})? $t:ty, )*) => { > + $(unsafe impl$($($generics)*)? 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: These are ZSTs, there is nothing to zero. > + {} PhantomData, core::marker::PhantomPinned, Infallible, (), > + > + // SAFETY: Type is allowed to take any value, including all zeros. > + {} MaybeUninit, > + > + // SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee). > + Option, Option, Option, Option, > + Option, Option, > + Option, Option, Option, Option, > + Option, Option, > + > + // SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee). > + // > + // In this case we are allowed to use `T: ?Sized`, since all zeros is the `None` variant. > + {} Option>, > + {} Option>, > + > + // SAFETY: `null` pointer is valid. > + // > + // We cannot use `T: ?Sized`, since the VTABLE pointer part of fat pointers is not allowed to be > + // null. > + {} *mut T, {} *const T, > + > + // SAFETY: `null` pointer is valid and the metadata part of these fat pointers is allowed to be > + // zero. > + {} *mut [T], {} *const [T], *mut str, *const str, > + > + // SAFETY: `T` is `Zeroable`. > + {} [T; N], {} Wrapping, > +} Arguably, it would make sense to just expand this macro. The code doesn't become that much longer, and it removes the need to understand the macro.