From patchwork Mon Apr 3 15:45:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: y86-dev X-Patchwork-Id: 78606 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2413278vqo; Mon, 3 Apr 2023 09:06:46 -0700 (PDT) X-Google-Smtp-Source: AKy350Y4RDkOtS1l4A7XVA9EOWZAcMQ7ACaQb3mHsfhZDUIjdT8EBcXyQg4Ytjfcirz+xaachU2J X-Received: by 2002:a17:90b:38cc:b0:23d:4e0e:cf34 with SMTP id nn12-20020a17090b38cc00b0023d4e0ecf34mr41177123pjb.34.1680538005786; Mon, 03 Apr 2023 09:06:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680538005; cv=none; d=google.com; s=arc-20160816; b=ICSSvtdayH5B9y/rZTbXVSwqHKbcP+WJLZpQh4UlBMGUY2mvCg8F0diIAdkydXKtXQ B1UXFSRVD4XtXuT4g8zIIFUJoznUzUMpufN8lc6Ge2+qCtZnBCJuVfmnfR3zy+MRW3vy ndu/5xKpOKWsMdwEmyOjr9LIhRSQuCRSGn81+idNrmmjHhu4jUdOinT3aqm8bzs7L/WD mOJEHUx0DY5sDOeDMQuQH0Z6HUJdjUivGPIFRMzZfNn7lE2zpWmy8iHFKSKqySnt3hah Hq1zXJJM705mjwprzREoEh82X4LC/5dqH37rx/ZwvgfbohIDGNDRfcONForgsflCRiHn iNDw== 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=35ec0Ze6x4vGPDCyxTq1eqzjMeEQtTPCRJPMgIkdSpQ=; b=jllwAyKRKtKMgIaTLuIuN5mAgiJ9QS+OgR6YXzm3q4NNKnEaQDX43IKzc2I+9ie5oi OYdlMp03ns1ERtGcZ6PJecaxWUlqXWuCDtoqxHbv83sjDM7LIIWSlW+enjGSpiZ9fjD/ I0+jGaJjN3Ufx/1t5oUBPmu3Ialf21l8nQ394OcyX+eAalJKQbAeTxSD+eE/MLaYD+pw 7EudLhbHT3UTKte23HZAzq7gGCL7IuNmxn2b7EtJWjsTNQrg0BtM9vfj8phTKCt6Pgq+ bCqku7qRefYKyreeTRomydZovb3q12SSA9LpYqlazojztli7AbRMQCrQqj9KfLsHnvqB U6lg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b=HaHTlTt9; 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 y1-20020a636401000000b0050f6ab16eedsi8504044pgb.688.2023.04.03.09.06.32; Mon, 03 Apr 2023 09:06:45 -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=HaHTlTt9; 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 S232616AbjDCPpe (ORCPT + 99 others); Mon, 3 Apr 2023 11:45:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50428 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232854AbjDCPpR (ORCPT ); Mon, 3 Apr 2023 11:45:17 -0400 Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 640B030C6; Mon, 3 Apr 2023 08:45:10 -0700 (PDT) Date: Mon, 03 Apr 2023 15:45:03 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1680536708; x=1680795908; bh=35ec0Ze6x4vGPDCyxTq1eqzjMeEQtTPCRJPMgIkdSpQ=; 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=HaHTlTt9RSHOjaXodVwlogJDYof8wlINXYIWJvZIXqWNQKQJRw9oegtUdq8iQJW5E A1OMEJnbBycjBmPmKaiPFIjPceEZfQGndBIluAkPU7Niiy2IQUcvzAxQN5lTiyN7oZ G5WGsQHokgx3pkZh6JrMhdpJfSscuirVe+A0uLImh0jZuXUwDgOPJO3P2X2c1e3GfY LP5FYfllEU18iGx5ASZXZfxO0chv5or4skrDYFKv4wJpChS3SV7tV/HsH0DiLvjw/6 WAj7s5GV0wAwQ070UHAikl3FETHbsWAA3urmob4ZRB45WOZ5f/7RVQiyNB+fLkxYN7 BKwq6bHDp9Z2w== To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Alice Ryhl From: Benno Lossin Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Benno Lossin , Wedson Almeida Filho , Andreas Hindborg , Alice Ryhl Subject: [PATCH v5 04/15] rust: sync: add `assume_init` to `UniqueArc` Message-ID: <20230403154422.168633-5-y86-dev@protonmail.com> In-Reply-To: <20230403154422.168633-1-y86-dev@protonmail.com> References: <20230403154422.168633-1-y86-dev@protonmail.com> Feedback-ID: 40624463:user:proton MIME-Version: 1.0 X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_MSPIKE_H2, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762171820099093841?= X-GMAIL-MSGID: =?utf-8?q?1762171820099093841?= 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 Reviewed-by: Wedson Almeida Filho Reviewed-by: Andreas Hindborg Reviewed-by: Alice Ryhl --- 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 aa7135f0f238..eee7008e5e3e 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