From patchwork Tue Mar 21 19:49:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: y86-dev X-Patchwork-Id: 73034 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp1980093wrt; Tue, 21 Mar 2023 12:58:29 -0700 (PDT) X-Google-Smtp-Source: AK7set8hF3Hx4I6M2i0p/T6yv3QRiApDJfUEYDkJe9ry16O4CuEtqR+EINkCvnuzLfS8FJOF0zj7 X-Received: by 2002:a05:6a20:389e:b0:d9:d8e4:4cad with SMTP id n30-20020a056a20389e00b000d9d8e44cadmr3250530pzf.16.1679428708928; Tue, 21 Mar 2023 12:58:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679428708; cv=none; d=google.com; s=arc-20160816; b=Lkbx3OtQsWFJ6pvZMAm7H2vIXrbh8kI9+wUsrbfLJN46uFD5Aca+Z3BoRKKuDHgF9w 36YEN6OmrYyyDBIXFOX+3qUW4hc9I9xwgNur2JXOmE27FYUv1vCyeBR+Q2sj+pxHnGDc lQDB+dra/NhgIEo010EnCAiVFMBJlvtQ8s9vXhsdcDf649FB1rik43ltFsUa4H4QILxX bcEVKNQeGLrh9fUJd5OY7fRLT/dK1JaQWMbihspcPzTPpGBOLBoCmZBFIGXvaFAGvYGy Rby+zj6722JYHQRClQA+YAyN+aCCRj+a8s0VeHVgoHh5wlT/bGZN2bsvv03GSBkT4Mpc P8LQ== 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:references:in-reply-to:message-id:subject:cc:from:to :dkim-signature:date; bh=5wFTfQvkkunlIrsARX4Vq7hYHVmiJWCfu28xcLcdviI=; b=UuWKHizwxoraNSB73fLJRhWY0y7FtCWRHR5q+a+EkX0XQzp2S1Nsux6dBtkNZqk+yk G259H5K5jyAVrwFfKLmiaO+YRJed41JLg1IjOWndfWN2JYaRKh1bmkLb+HYJHhk3ZpTy CTzc9JYj/wXRr+AFM6cB36VNrp6dG8kFlA78/ngHVNxjF2cNp0QE3uezm+/pR6Eey2UD horE/YhPygGS5utqMXPRPlPuSuwPFX6qTIMt7U+Q+FK6adQIGfT/hdNQwZe9MYk1hYpu bsAGqMwvN4WQ4BSDJQWTfErraIrERXa2BZw72EeMVMoIu6WuTxPC1NOOKHwxLu5yjWSa ShhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b=yo3gFfxM; 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 j18-20020a056a00131200b0062806504839si4615099pfu.168.2023.03.21.12.58.14; Tue, 21 Mar 2023 12:58:28 -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=yo3gFfxM; 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 S229904AbjCUTuQ (ORCPT + 99 others); Tue, 21 Mar 2023 15:50:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229841AbjCUTuG (ORCPT ); Tue, 21 Mar 2023 15:50:06 -0400 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 594A69EF2 for ; Tue, 21 Mar 2023 12:50:02 -0700 (PDT) Date: Tue, 21 Mar 2023 19:49:55 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1679428200; x=1679687400; bh=5wFTfQvkkunlIrsARX4Vq7hYHVmiJWCfu28xcLcdviI=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=yo3gFfxMfaxuil6kpYNQ51W526MtY1XzriLAqtUMsU0wfTlOKpcjd6bWdEB4zJvCK EO9rf+vJFjiQCEsarmSXWn1yMJLfbMI+/d+ufWy9ZgpjQRMnVu0eymjMAT6UJ9kPmF LiNAF8i9KPRDecEJlTM5LEaW0nlUmq78H/IFkQXYSRGamiWBT448a9HLgwC2QZZoLu sIE/ivUqLaPUY5pKp8CL1RGEPoH72x0VjHLExdvu/7Ga+LX1U8sbDGK5L4MdDzZ2ed xZzPxbWPYTiTZP9KSs6dNPwJqlalOo1Zoysefsd0mVxf45dCAA4LI6KH0ahkrPnTJX MVu5aeUxjc+aA== To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= From: Benno Lossin Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH v2 2/5] rust: sync: add `assume_init` to `UniqueArc` Message-ID: <20230321194934.908891-3-y86-dev@protonmail.com> In-Reply-To: <20230321194934.908891-1-y86-dev@protonmail.com> References: <20230321194934.908891-1-y86-dev@protonmail.com> Feedback-ID: 40624463:user:proton MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_MSPIKE_H2, SPF_HELO_PASS,SPF_PASS,URIBL_BLOCKED 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?1761008638094783153?= X-GMAIL-MSGID: =?utf-8?q?1761008638094783153?= Adds the `assume_init` function to `UniqueArc>` that unsafely assumes the value to be initialized and yields a value of type `UniqueArc`. This function is used when manually initializing the pointee of an `UniqueArc`. Signed-off-by: Benno Lossin --- rust/kernel/sync/arc.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) -- 2.39.2 diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index f2f1c83d72ba..16ec174637b2 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 just wrote the value to be initialized. + 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