From patchwork Thu Apr 6 06:56:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: y86-dev X-Patchwork-Id: 80083 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp822791vqo; Wed, 5 Apr 2023 23:58:30 -0700 (PDT) X-Google-Smtp-Source: AKy350ZIe0P5NgbeZI/NN3+pNWEozu5pntZ7DWkH2Y01sGO+WvZfPv7Sl6dH2imdqmHAk4EXAFcO X-Received: by 2002:a17:902:cf45:b0:1a1:b7fc:eeba with SMTP id e5-20020a170902cf4500b001a1b7fceebamr7750133plg.19.1680764309960; Wed, 05 Apr 2023 23:58:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680764309; cv=none; d=google.com; s=arc-20160816; b=Q5RjpLnD8IVqK+fIvb2TLXhyTjcCoDs+HQMqw4PfIfrE5xU/KxNs8/MIeziu42vUnl s0BtuS3CiEyrS0WJ9v6z05MZPrz0udEYmvEIlMOj/9PuH+6JRrrRZGULspTWk32tQQsZ /qCVDa9nxeU+WZTEg7sh4AxaqKmSc/bFTVDE/uWyMaWXBZM0mRNeZ/6aFskHC35EtoRw USrYCtaradvLSRu6zCHW6VZ9+M1e1SqankSwgkKFwb9BItasfTZUoUqXML3RCekVSog3 4G95yWL2PkadHugW2XAirE4lY0VKGxHvwtMzMEyudAibWXfChoE+R8+VimThjT4gdbqf 29cA== 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=x9eH5jLzlqlD1oeTC6skO4fRxBR6eOlDpRkl8IwVxoE=; b=iQR07xS/97XZ88qcyLyv5zMAqVs4BXHVk7nOFPIqH+JPzdlkh7eFD7trU94qx/pRqM V8qDkpTT9r80+hm2OEORYopzxpK4Hd8tuntipEPcflbRVWX+uZYTytXPjf2P0Y2QzpmB 64wCl8xgphzxDbshSargTFoWbKrWqYZ7UbZ0XIJYwQSNPNoTuIfB+X8Y8lNZZ2eUcteS Jc7ebC4hArnSEAmCwKPC+em65Ypsq3d+9y+IGBAJt53bNwPz3YvM10DvFs4N7hyck2WZ TrGYLiFm8qNlcyDiBLv71R8KEW0Z6Q9HAWG19U556oP2bQTCkDLJPu6L2cSIx1YEDK85 K4vQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b=bACR+JIc; 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 k16-20020a170902761000b0019e6763b213si974149pll.99.2023.04.05.23.58.17; Wed, 05 Apr 2023 23:58: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=@protonmail.com header.s=protonmail3 header.b=bACR+JIc; 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 S232252AbjDFG5i (ORCPT + 99 others); Thu, 6 Apr 2023 02:57:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33742 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235383AbjDFG5g (ORCPT ); Thu, 6 Apr 2023 02:57:36 -0400 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CA7A8AD11; Wed, 5 Apr 2023 23:57:09 -0700 (PDT) Date: Thu, 06 Apr 2023 06:56:09 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1680764174; x=1681023374; bh=x9eH5jLzlqlD1oeTC6skO4fRxBR6eOlDpRkl8IwVxoE=; 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=bACR+JIcC17dQkUDTOdz3igPN7NdemtDMVCzZ9hrPxMBnu4Mc0kiG1Cy9Ru+wU8cn 1jiZsOHRVr1JoF6hkeyTq4S/3xn7F8kIEVf7q37xeNSRpLETOALYDxKutilYck4OyR kXvod+SRn7yF4H9AU1NsJblvg6tId10aJd5eFe6McqlmYLM2vEYxovQo5ouw7Lc3bA RBQU2g2dYhRAmLwyJsxlR53JMN/yEh6akzWNywUE/+l+N/7axerMOt3yciStV5CkbA dgkT0S0TWKcWe+5U8VZ1JlhWiqq9ivCieB9AW3t1ruk7TxYh8nsimAU9dpUA89Cijd CLEFtR3gl3h3g== To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Alice Ryhl , Andreas Hindborg From: Benno Lossin Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Benno Lossin , Alice Ryhl , Andreas Hindborg Subject: [PATCH v6.1] rust: types: add `Opaque::pin_init` Message-ID: <20230406065546.787669-1-y86-dev@protonmail.com> In-Reply-To: <20230405193445.745024-14-y86-dev@protonmail.com> References: <20230405193445.745024-1-y86-dev@protonmail.com> <20230405193445.745024-14-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?1762409117084806342?= X-GMAIL-MSGID: =?utf-8?q?1762409117084806342?= Add support for pin-init in combination with `Opaque`, the `pin_init` function initializes the contents via a user-supplied initializer for `T`. Signed-off-by: Benno Lossin Cc: Alice Ryhl Cc: Andreas Hindborg Cc: Gary Guo --- rust/kernel/init.rs | 3 +++ rust/kernel/types.rs | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) -- 2.39.2 diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs index ffd539e2f5ef..23d879e53b26 100644 --- a/rust/kernel/init.rs +++ b/rust/kernel/init.rs @@ -177,6 +177,9 @@ //! } //! ``` //! +//! For more information on how to use [`pin_init_from_closure()`], take a look at the uses inside +//! the `kernel` crate. The [`sync`] module is a good starting point. +//! //! [`sync`]: kernel::sync //! [pinning]: https://doc.rust-lang.org/std/pin/index.html //! [structurally pinned fields]: diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index ff2b2fac951d..cf1cbd6a1447 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -2,6 +2,7 @@ //! Kernel types. +use crate::init::{self, PinInit}; use alloc::boxed::Box; use core::{ cell::UnsafeCell, @@ -234,6 +235,23 @@ impl Opaque { Self(MaybeUninit::uninit()) } + /// Creates a pin-initializer for `Opaque`. + /// + /// # Safety + /// + /// [`PinInit`] promises that the [`Pin` `Drop` guarantee] is upheld for the type `T` if + /// initialization succeeds. `Opaque` is a wrapper that allows holding uninitialized values of + /// `T`, so it does not drop the `T` when it is dropped. When calling this function the caller + /// needs to guarantee that: + /// - either `T` does not have drop glue ([`core::mem::needs_drop`] retuns `false`), + /// - or the caller ensures the drop guarantee manually. + /// + /// [`Pin` `Drop` guarantee]: https://doc.rust-lang.org/core/pin/index.html#drop-guarantee + pub unsafe fn pin_init(init: impl PinInit) -> impl PinInit { + // SAFETY: `init` initializes the only field of this struct. + unsafe { init::pin_init_from_closure(|slot| init.__pinned_init(Self::raw_get(slot))) } + } + /// Returns a raw pointer to the opaque data. pub fn get(&self) -> *mut T { UnsafeCell::raw_get(self.0.as_ptr())