Message ID | 20230329223239.138757-3-y86-dev@protonmail.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp732303vqo; Wed, 29 Mar 2023 15:43:15 -0700 (PDT) X-Google-Smtp-Source: AKy350bsL8Y7X/OccrayKntTGdaIslX1/NZ9gGFqOecm98qg/2yuOqsw9dvMyYGL3zsLLgYNiyGw X-Received: by 2002:a05:6402:206:b0:4fa:ac78:7f2a with SMTP id t6-20020a056402020600b004faac787f2amr17850278edv.41.1680129795546; Wed, 29 Mar 2023 15:43:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680129795; cv=none; d=google.com; s=arc-20160816; b=GTtmNhFfG85+65xOCY814ILTplJNQhaPjRn5pndQnpIJLKZfVYq6lHLL8Rp7VoGq5X GQvWqinrC2un7okoDJ/lQ/sIQC083oonIYvgnFoGy+AaA7DLiwC97r2lhbX7qkZ41F3T A5zL5H+Lg+Myrd5T5Yp0/ZsS+Os8+9Z7izrQFv9QjI6405kQ55xKvI4gQZmtIZeQpnWT lnDlqpdHVO+wOHBwAXGtDwF6uqJOF2yKmQ+HfurxnQ/nk0arw9WtJXtUl9vdtrbQqwx/ lhfEQZaHlYUh68h17wzpvhIUwN7jWyE9p2hg7ESJE6b9h1pVAy11YVdN2PcNVIJp5M0x ZNrw== 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=sT/rlttzNSF7UJt0mIvVFloU5vc3XfQjYu+rGHSgh10=; b=tcHap3E+Aie/gqIyMTF0+1AnyDnX53Moxzuen5uYzXDZC9UwdJN9IB2aHny2FijF4Q bNm+94ifWUwXo0w1hjni1SEe9F+YtvXby/uqs5KLrywOlZxroW8ryPmoLLDOnmtjk+NG pOQokPze4J+MdYEoJG4Oy8i8UOY+eJ30aIKBz2MTnK6PSvBEUA8iFIqu5j/T0Zryqbjt jTInGmFICNI5Gca71W7izfMvqbphDXVUYQicfF0e3/bT9b1Ay42SnZRsswN0jKZUSC/+ dfcY4I7l4EebXyXBUlCvNWkhqDCO4i0MLu+xa4rxpQYaygGA4tk4MjJa1HU5uSx8+QDH L4mg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b=Tn7sB5nJ; 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 n25-20020aa7db59000000b005021cf63eacsi15112063edt.265.2023.03.29.15.42.50; Wed, 29 Mar 2023 15:43:15 -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=Tn7sB5nJ; 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 S230150AbjC2WdN (ORCPT <rfc822;rua109.linux@gmail.com> + 99 others); Wed, 29 Mar 2023 18:33:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47432 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229678AbjC2WdJ (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 29 Mar 2023 18:33:09 -0400 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 27F005B85; Wed, 29 Mar 2023 15:33:08 -0700 (PDT) Date: Wed, 29 Mar 2023 22:32:58 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1680129186; x=1680388386; bh=sT/rlttzNSF7UJt0mIvVFloU5vc3XfQjYu+rGHSgh10=; 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=Tn7sB5nJn4N2i7HWNE/zjhOZpRWOd6S0DMu6MgG5akvtvuHDXIMQntUfXxB7q8YFV ww54eUY7myQqcAwFR7VLBnTJTYefMwwIJUSd+2AvdYeIzSkTteD8eSVbrwMy0TkhKX bm6D5yLgYepYQXtfCdLvSC/u8be2ZT1Il99R8/invz9CO7ra4RUXBVVKTI1+W/eZV0 0GULkuJc0PI+XbKrRe06xlv447qC+Wd5YlyjG3y+uesNyVLx/41cwZmwPKvNUSg3d0 yjgz38rp3F0VIWeivFzxTGKsqB/1Sk1y44SEDCduZiTPdsjZ4RsqMtPW3Z7rHkVwU2 8adQcBLMH0gHg== To: Miguel Ojeda <ojeda@kernel.org>, Alex Gaynor <alex.gaynor@gmail.com>, Wedson Almeida Filho <wedsonaf@gmail.com>, Boqun Feng <boqun.feng@gmail.com>, Gary Guo <gary@garyguo.net>, =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= <bjorn3_gh@protonmail.com>, Alice Ryhl <alice@ryhl.io> From: y86-dev@protonmail.com Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH v3 02/13] rust: sync: add `assume_init` to `UniqueArc` Message-ID: <20230329223239.138757-3-y86-dev@protonmail.com> In-Reply-To: <20230329223239.138757-1-y86-dev@protonmail.com> References: <20230329223239.138757-1-y86-dev@protonmail.com> Feedback-ID: 40624463:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761743780308297766?= X-GMAIL-MSGID: =?utf-8?q?1761743780308297766?= |
Series |
Rust pin-init API for pinned initialization of structs
|
|
Commit Message
y86-dev
March 29, 2023, 10:32 p.m. UTC
From: Benno Lossin <y86-dev@protonmail.com> Adds the `assume_init` function to `UniqueArc<MaybeUninit<T>>` that unsafely assumes the value to be initialized and yields a value of type `UniqueArc<T>`. This function is used when manually initializing the pointee of an `UniqueArc`. Signed-off-by: Benno Lossin <y86-dev@protonmail.com> --- rust/kernel/sync/arc.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) -- 2.39.2
Comments
On Wed, 29 Mar 2023 at 19:33, <y86-dev@protonmail.com> wrote: > > From: Benno Lossin <y86-dev@protonmail.com> > > Adds the `assume_init` function to `UniqueArc<MaybeUninit<T>>` that > unsafely assumes the value to be initialized and yields a value of type > `UniqueArc<T>`. This function is used when manually initializing the > pointee of an `UniqueArc`. > > Signed-off-by: Benno Lossin <y86-dev@protonmail.com> Reviewed-by: Wedson Almeida Filho <walmeida@microsoft.com>
y86-dev@protonmail.com writes: > From: Benno Lossin <y86-dev@protonmail.com> > > Adds the `assume_init` function to `UniqueArc<MaybeUninit<T>>` that > unsafely assumes the value to be initialized and yields a value of type > `UniqueArc<T>`. This function is used when manually initializing the > pointee of an `UniqueArc`. > > Signed-off-by: Benno Lossin <y86-dev@protonmail.com> > --- Reviewed-by: Andreas Hindborg <a.hindborg@samsung.com>
On 3/30/23 00:32, y86-dev@protonmail.com wrote: > From: Benno Lossin <y86-dev@protonmail.com> > > Adds the `assume_init` function to `UniqueArc<MaybeUninit<T>>` that > unsafely assumes the value to be initialized and yields a value of type > `UniqueArc<T>`. This function is used when manually initializing the > pointee of an `UniqueArc`. > > Signed-off-by: Benno Lossin <y86-dev@protonmail.com> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
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<T> UniqueArc<MaybeUninit<T>> { /// Converts a `UniqueArc<MaybeUninit<T>>` into a `UniqueArc<T>` by writing a value into it. pub fn write(mut self, value: T) -> UniqueArc<T> { 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<T> { let inner = ManuallyDrop::new(self).inner.ptr; UniqueArc { // SAFETY: The new `Arc` is taking over `ptr` from `self.inner` (which won't be