Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp4321262rwl; Mon, 3 Apr 2023 03:19:29 -0700 (PDT) X-Google-Smtp-Source: AKy350bOZDJXrDrgEj8eH7oYAVcl/mBy0g36YM9pmG0hpp5v0RDubgzPyuHbNCF6BFWwv+HqBrp/ X-Received: by 2002:a17:907:2099:b0:8b0:ad0b:7ab8 with SMTP id pv25-20020a170907209900b008b0ad0b7ab8mr34447771ejb.14.1680517169304; Mon, 03 Apr 2023 03:19:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680517169; cv=none; d=google.com; s=arc-20160816; b=K57WQ7G3ESo6nUdeLXwpklOaD1pI4ThH83vu7VcgaU634YigTgckFYozJjdIsSOC/5 BuhSM0MBAHekS0x2VHnzYdEJfDNb/7hLBgrFb4omAAbyscC8A2Avds451Lx5NPV2tYP9 VQC3JKVA+QeNAB+IH+yTPytWiOmr5uzah/L1n1Oy3X68Safo6jHaPy4FjbosjJGKMYjd z0hIgvbLt2Y0sBCkjXvaqvC/Ixc0vKWC9hIeWQQ/6b21QD4BLOJ5Aqfn2af/FkjZ25IK gNNNNA9K35VwrhmEvBXT4rzUa4K4GKk50y4Tz8fWnerREC6b63zdifJcN4wjizokMOR5 L0zQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=QKnNO0YGC34XiCu/Oreu5B8FEvTqv24bPNAO90yOF/0=; b=i/Og+G6XNZ4GNT8WGI6VL3L9ZeDfrJ9RCDIO/3GWMcEZ7wYKOtWeR5wq+39I1gJ8RW 3gykE8u7jQPeRVqPaainrz+mMAPJZzrOlINo+N+gO4mt2cy0cUPaTZ90D/XJDANfj9Hn c1N1b28fyFEH0UZpZcoS26cNS4I3yac2dJQdf9GRKQcbHcFkuMclEFIp71A+naokj+81 9PXgha+17b/aYvn1UZFYzfgcibkpQuc2zM8odNa/HaeGg3dCzE4uT+Llssp5vrZOjN3q LT0POOFaKPKqa56rPqdH+nFtJuxlMYmax6Sav/goArsDHQCHnxptYOKhMv6vKZ8t6JnQ xYfQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b="PEBvY//M"; 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=asahilina.net Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u24-20020aa7db98000000b005024aa4628fsi4005366edt.358.2023.04.03.03.19.04; Mon, 03 Apr 2023 03:19:29 -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=@asahilina.net header.s=default header.b="PEBvY//M"; 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=asahilina.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231990AbjDCKCI (ORCPT + 99 others); Mon, 3 Apr 2023 06:02:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232163AbjDCKBv (ORCPT ); Mon, 3 Apr 2023 06:01:51 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A8778524D; Mon, 3 Apr 2023 03:01:49 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: linasend@asahilina.net) by mail.marcansoft.com (Postfix) with ESMTPSA id D7606423BA; Mon, 3 Apr 2023 10:01:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680516108; bh=+HaSZDGbivQJSY7YEFbLidqycutVbcVcWgZLNHkg3sc=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=PEBvY//MxjhXn7GofQK56jt6e0nLl+Cz8rwP5nuTiXfdMcbDcl9MNXNzupCuKIyOS EeWYAjJgQRMUkPkzVUbeKo9E+NXlQ8LiPxACOmWFafGWMFfc8xhPlXlL9Tjenfpv75 W31kEJZh3fji0IuhFrEFOPY+AkbrHjz48RQyWPC32fQJOE8VRmENcvKcxg2B2QhZ9d nA9sc+MvZ31pV60s9ZdwnPR7RQ5WBjqadpmcNxYsQbZRFV16grxPLJyeUNT88xaWYc CSxtgq8EkkCoYGtpoa/K39A+gnuBigcUCxasY9M1sDflFEnMRO+0on/e9qorkY7Jbc GEJgHiUoTEwAg== From: Asahi Lina Date: Mon, 03 Apr 2023 19:01:12 +0900 Subject: [PATCH v2 2/2] rust: sync: arc: Add UniqueArc::assume_init() MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230224-rust-arc-v2-2-5c97a865b276@asahilina.net> References: <20230224-rust-arc-v2-0-5c97a865b276@asahilina.net> In-Reply-To: <20230224-rust-arc-v2-0-5c97a865b276@asahilina.net> To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= Cc: Andreas Hindborg , Vincenzo Palazzo , Martin Rodriguez Reboredo , Benno Lossin , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, asahi@lists.linux.dev, Asahi Lina X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1680516096; l=1928; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=+HaSZDGbivQJSY7YEFbLidqycutVbcVcWgZLNHkg3sc=; b=D/SmqTHwoV04N4AZ+pjn10VxfxZKRJYh1xmzLnOtmSG/S67qFVwAk3MdCshzPNeavWKr3bfxk fxnk5n1jxLuCUZL+ITvXD8CsT/RYmCobT9gRBt8mc0zEpd/tyYjEIkt X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,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 We can already create `UniqueArc>` instances with `UniqueArc::try_new_uninit()` and write to them with `write()`. Add the missing unsafe `assume_init()` function to promote it to `UniqueArc`, so users can do piece-wise initialization of the contents instead of doing it all at once as long as they keep the invariants (the same requirements as `MaybeUninit::assume_init()`). This mirrors the std `Arc::assume_init()` function. In the kernel, since we have `UniqueArc`, arguably this only belongs there since most use cases will initialize it immediately after creating it, before demoting it to `Arc` to share it. Co-authored-by: Benno Lossin Signed-off-by: Benno Lossin Signed-off-by: Asahi Lina --- rust/kernel/sync/arc.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index b904ed898d3a..b664b41e473d 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -489,6 +489,17 @@ impl UniqueArc> { /// Converts a `UniqueArc>` into a `UniqueArc` by writing a value into it. pub fn write(mut self, value: T) -> UniqueArc { self.deref_mut().write(value); + // SAFETY: We have just written the contents fully. + unsafe { self.assume_init() } + } + + /// Unsafely assume that `self` is initialized. + /// + /// # Safety + /// + /// The caller guarantees that the value behind this pointer has been initialized. It is + /// *immediate* UB to call this when the value is not initialized. + pub unsafe fn assume_init(self) -> UniqueArc { let inner = ManuallyDrop::new(self).inner.ptr; UniqueArc { // SAFETY: The new `Arc` is taking over `ptr` from `self.inner` (which won't be -- 2.40.0