Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp1740601rwl; Wed, 5 Apr 2023 23:58:25 -0700 (PDT) X-Google-Smtp-Source: AKy350ad10VxgIcBRaX+FO4jqpOlWKip/S5kEbEMYbwSyzsRHlJKhvPIvsUoovIk/coAy8QQtAzS X-Received: by 2002:a05:6a00:5a:b0:62d:b4ae:e48c with SMTP id i26-20020a056a00005a00b0062db4aee48cmr9397678pfk.23.1680764304803; Wed, 05 Apr 2023 23:58:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680764304; cv=none; d=google.com; s=arc-20160816; b=iKRQjH+wtUTUAfiozGzXgogFw0ScDGCgyeGSGUjqWlEKuiKMSXBzDwdtxweHRfy7gj LFrBsU/wuUvFoXZhduvFVmTRXILmkjlUXtOx5q0JUxSKtXJKMij7Iho4ksbbhUg3/aGa Mu0ZaBcRfqt4/jOfrMDCupous9ozywi0/uQLGaBXR3pqcSJ0pmDkPN2SqA8QhNT9mMYH 0CTywD3WLWH7kX2uBnYqbWX2sNMhHTq/d3DPb1xtONQdZsbE9SoLNO6nPXNqT60YGXg2 bTVY7uNo4mCX27CXYuBF5oBPYaLscH38PObXzxadaowfdOZ01HnfkFmz6YgRT4MocU2U f1qQ== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h30-20020aa79f5e000000b0062c31053b64si745377pfr.56.2023.04.05.23.58.12; Wed, 05 Apr 2023 23:58:24 -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 S235463AbjDFG5i (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 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: X-Mailing-List: linux-kernel@vger.kernel.org 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(+) 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 up= held 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`] re= tuns `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(Sel= f::raw_get(slot))) } + } + /// Returns a raw pointer to the opaque data. pub fn get(&self) -> *mut T { UnsafeCell::raw_get(self.0.as_ptr()) -- 2.39.2