Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp4886037rwl; Mon, 3 Apr 2023 11:00:31 -0700 (PDT) X-Google-Smtp-Source: AKy350ZIENS0DGFNvPiMuIgOvtyke2Es9SLmveWhgbXounXixuy1T4R9GmOpkInBCL58Bm8MtYAw X-Received: by 2002:a17:906:3da:b0:933:2f77:ca78 with SMTP id c26-20020a17090603da00b009332f77ca78mr35482991eja.28.1680544831414; Mon, 03 Apr 2023 11:00:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680544831; cv=none; d=google.com; s=arc-20160816; b=GGQiASGsDQKPKL44cV2J3kefVD9hqFbZrLAzFkePGKGRjzHksV0/nKTL6kUrGK4wg3 G9JeJEyyYWhdjQT5vk5Uy0XE5f83L5PftBZY6A8A5HzsHuNKM+Q/Fb3H+H/6qW4opTtm WgKzmeAs2EU+v96Sjfqr9hI4rEOgWcRqJtGbwR2M7DnebLndu2tVo25iYVatJU+QQJNA Em0h85KT+2uhdppB+N0hWkdZ+wlBN1ekf6q7MDRovvLX+ln1fA5GP+A8KQ8AAzLZhPVz HKLxZ8O2pXMSAtiNT07Zt5579HlITipMwCVm31sk4i+jZt+fTGHubupL54gow0RWnHel vcSg== 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=zQJWXe21ZtGhz7ERfdBSQKRhyX9hHVQosnDsirMnv1Q=; b=P3k6a0TZSjywC04NlHRCNa6UWlfHYL+iM0pl42picHTkyzwqCeQ6l+DgD0WlHQH5YF mI468T7ntKZLg8QNZt7n3VFBJQXV2xXaOclrtxY3V1AJ5f10jBJzVJZyK0FiwMOr6SJL U22shIHl4RmsrG8M0MLtnK0JpxYcHsPNOTNXdKVTLVC35vl96CI3RTjoVbLiG96hFgAQ G0ZLxwOt82HM46wIXu3XDBXGl8ejZFAbyRR9hGcIP3T3J249l19sphBOmxlLzPj3iyCZ M5XG1W4VEhKsNHEpe3iU5wbqlqK/2cu2bNCTvK5L99gazMV9h8Dvy+IPQALrcE2qqZqS Ex+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ryhl.io header.s=fm2 header.b=GtH56V66; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=JnG99aZU; 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 m2-20020a17090679c200b0093defbd628esi8430832ejo.1045.2023.04.03.11.00.06; Mon, 03 Apr 2023 11:00:31 -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=GtH56V66; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=JnG99aZU; 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 S233000AbjDCR6T (ORCPT + 99 others); Mon, 3 Apr 2023 13:58:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45664 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232126AbjDCR6F (ORCPT ); Mon, 3 Apr 2023 13:58:05 -0400 Received: from out5-smtp.messagingengine.com (out5-smtp.messagingengine.com [66.111.4.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 633383A8E; Mon, 3 Apr 2023 10:57:35 -0700 (PDT) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id DB3F05C01C7; Mon, 3 Apr 2023 13:57:26 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Mon, 03 Apr 2023 13:57:26 -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= 1680544646; x=1680631046; bh=zQJWXe21ZtGhz7ERfdBSQKRhyX9hHVQosnD sirMnv1Q=; b=GtH56V66t/qGKx2bjgDzOhNcjfnWapZbXtA8eZ22WaZElr3zShL mrbS9gluykAi+YRxTURgG2MMjA7bP6prpaBjG5LqbrO0m9JDDaizkgKWm8ZPzUn2 hZ1ZMku+q+G+hITKyzJcwuIhxZYeHn7hSGPdwVM+TjNOKTEGKN8qs3lP7irTEeAm Fp+cowLnEcPKKZr54FactmTEj7hrrx10dV1MK+G7d0z+/7RmOsn/S9QhYYn1Wyen STWfi3hP9Mp2qnm6kUUY50yYwhQ2GmA1AXFZZWiTFCY1luBHEyGKyFY2SRDxDscY TbxIufqYbKeHpSb00O8nNRdj/sZCyv7W1Uw== 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= 1680544646; x=1680631046; bh=zQJWXe21ZtGhz7ERfdBSQKRhyX9hHVQosnD sirMnv1Q=; b=JnG99aZU1ugvgpvgtPl1G55vxhYhYcVerkBBasZ99ifhR4dNJso zR2eURmEGmG1Zuxr/muOp9+/GMhAo4YQLU0veOHcZItF9i6CudFOihYIQhuQJzGa J+3KbW1TKwrxIrHzd8heJ5FfRha1HYEG1RmKptyxfwBD2kCTbAifIdrnFXAqBinw /UOFgLalr5hCUJk/M2eIDvzzx55bqRhw+/an/Kf6JvAZnh1Te7Qtf0SkTPlZb4Lp 0S6uwSChmSvAvltnQEp1b7u5dVCy2CSnra19GaNMPUJurrfqrbYG3gTz1bsnmaUr 3fQUlbu5eGk4RAjklhG/Y768vikBLNImS0w== 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:24 -0400 (EDT) Message-ID: Date: Mon, 3 Apr 2023 19:56:31 +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 14/15] rust: sync: reduce stack usage of `UniqueArc::try_new_uninit` 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-7-y86-dev@protonmail.com> Content-Language: en-US In-Reply-To: <20230403160511.174894-7-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: > `UniqueArc::try_new_uninit` calls `Arc::try_new(MaybeUninit::uninit())`. > This results in the uninitialized memory being placed on the stack, > which may be arbitrarily large due to the generic `T` and thus could > cause a stack overflow for large types. > > Change the implementation to use the pin-init API which enables in-place > initialization. In particular it avoids having to first construct and > then move the uninitialized memory from the stack into the final location. > > Signed-off-by: Benno Lossin > Cc: Gary Guo > Cc: Alice Ryhl > Cc: Andreas Hindborg Reviewed-by: Alice Ryhl > > /// Tries to allocate a new [`UniqueArc`] instance whose contents are not initialised yet. > pub fn try_new_uninit() -> Result>, AllocError> { > - Ok(UniqueArc::> { > + // INVARIANT: The refcount is initialised to a non-zero value. > + let inner = Box::try_init::(try_init!(ArcInner { > + // SAFETY: There are no safety requirements for this FFI call. > + refcount: Opaque::new(unsafe { bindings::REFCOUNT_INIT(1) }), > + data <- init::uninit::(), > + }? AllocError))?; > + Ok(UniqueArc { > // INVARIANT: The newly-created object has a ref-count of 1. > - inner: Arc::try_new(MaybeUninit::uninit())?, > + // SAFETY: The pointer from the `Box` is valid. > + inner: unsafe { Arc::from_inner(Box::leak(inner).into()) }, > }) > } > } I'm curious - do you know whether this compiles to the same machine code as this? pub fn try_new_uninit() -> Result>, AllocError> { let inner: Box>> = Box::try_new_uninit()?; let ptr = Box::into_raw(inner) as *mut ArcInner; addr_of_mut!((*ptr).refcount).write(bindings::REFCOUNT_INIT(1)); Ok(UniqueArc { inner: Arc { ptr: unsafe { NonNull::new_unchecked(ptr) }, _p: PhantomData, } }) }