From patchwork Fri Mar 31 21:54:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: y86-dev X-Patchwork-Id: 78020 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp877136vqo; Fri, 31 Mar 2023 15:25:55 -0700 (PDT) X-Google-Smtp-Source: AK7set8dd+Y25sDIuL1gxlr49Mp8DN1WC1p5CHn8gN0bMMoXoVvgcJgeUolBIzu7hLRtKmGGrEax X-Received: by 2002:a05:6a20:8009:b0:d9:e5db:5287 with SMTP id e9-20020a056a20800900b000d9e5db5287mr23452264pza.4.1680301555704; Fri, 31 Mar 2023 15:25:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680301555; cv=none; d=google.com; s=arc-20160816; b=IeCUN+UsPL7CHgkTmy+xsDxJxEY00mgxtnEIpvgh08J5X/8IklV1UPWckQbIdoppzw BLABtvZ30398ycUpw0hvfz9GHz3DEpam0pmSbiM1nqDQHx2UjI99H6mYL2+MHJk8FWvP 9KlNCZ8up0zY4G550Y4vlyE3MFZNZhluI5z7rVPoaIBzj7v/qEtrNcNTwUx+48SCUXvp c4AHIddDdO2P5ECCHiDQB73yzupHV/oMroTem0CyTA5l7Ask/OMP8k8MKG9Ryf/gtFxp LBumTuc4tTiWkVZyot2ABVv5FmW7SPVUQCCzpjy5+XnjlmA870t/sK6A4vwpEknPxwNA 9u0Q== 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:message-id:subject:cc:from:to:dkim-signature:date; bh=5Fn50wx9SjwjEk+7PFvrVXZ6Yt0uZEDngF0z84FHnKc=; b=VHyPaBGWbs/ILamXOCWEfcphDMSv3obc/wsiZoMhYOVhfT6UuDPw0nXhE6pzoetO4o +2ez4h82d3JOglv6x1jfjejpC6vZb/ak/SSucshz6IwAbgi908GCVemlcqZ8K7ws2tqx nMTYcoSr8+k5qaW0rznbN+o0GEDqs0CXyzBtzAH8ES9DNV6F2z9V+4nagzDUuxZ6ESDF 56zt8AyecYHkDqsuCWksVgbi/2wDDeRsXmP9ZCXiAvknSe3KqgbUy2bQAQ/Y7c1w+1Vv CSpHdn/wNDxw0D4jvzLE1fGYmkD+amyHJaIe+VWMth2xg2c1wEh/gsweoOo3CQFzBZoe 4s2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b=rpl7HOJl; 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 i12-20020a056a00004c00b0062a4722bb2asi3487900pfk.278.2023.03.31.15.25.43; Fri, 31 Mar 2023 15:25:55 -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=rpl7HOJl; 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 S233224AbjCaWGH (ORCPT + 99 others); Fri, 31 Mar 2023 18:06:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35410 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233153AbjCaWGE (ORCPT ); Fri, 31 Mar 2023 18:06:04 -0400 Received: from mail-41104.protonmail.ch (mail-41104.protonmail.ch [185.70.41.104]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9CC6B1FD3E for ; Fri, 31 Mar 2023 15:06:03 -0700 (PDT) Date: Fri, 31 Mar 2023 21:54:29 +0000 Authentication-Results: mail-41104.protonmail.ch; dkim=pass (2048-bit key) header.d=protonmail.com header.i=@protonmail.com header.b="rpl7HOJl" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1680299680; x=1680558880; bh=5Fn50wx9SjwjEk+7PFvrVXZ6Yt0uZEDngF0z84FHnKc=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=rpl7HOJlJTDiEG+BmuTzQHlWSHu2+U/blhvLlzXyQhzz6HEl0LLvfR+RJZaLdgU5x wy4YjiDBPq3QrCgHdYLrpaBKuPgSDp/UTF02H07Gg7s/hWGahPBI7C2UfhhalDMH9z V9j0Bh5KZWdTTXkuugNCdeNGj8ZDa0RcrXmURX4W09gfCCoYeS7bfkX3d7KaFF5xsx u7bQhmnHgWTudRL9qRHFvo+d1crdrVx6Q1cllbsnYOPztVMm5F2VuUT4oIgUMBl8r0 5yU9XftQvtDwWeqZbdSBDlv6WzRTwN1k2Ps8T03vZdkqNOXQusNPMkirvlJp59ylG0 xeYIRQ7xpT97g== To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Alice Ryhl From: y86-dev@protonmail.com Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Benno Lossin , Alice Ryhl , Andreas Hindborg Subject: [PATCH v4 13/15] rust: types: add common init-helper functions for `Opaque` Message-ID: <20230331215053.585759-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,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?1761923883551728854?= X-GMAIL-MSGID: =?utf-8?q?1761923883551728854?= From: Benno Lossin Add helper functions to more easily initialize `Opaque` via FFI and rust raw initializer functions. These functions take a function pointer to the FFI/raw initialization function and take between 0-4 other arguments. It then returns an initializer that uses the FFI/raw initialization function along with the given arguments to initialize an `Opaque`. Signed-off-by: Benno Lossin Reviewed-by: Alice Ryhl Reviewed-by: Andreas Hindborg Cc: Gary Guo --- rust/kernel/init.rs | 9 +++++++++ rust/kernel/types.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) -- 2.39.2 diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs index fef14c3c0642..7b452e458391 100644 --- a/rust/kernel/init.rs +++ b/rust/kernel/init.rs @@ -177,6 +177,14 @@ //! } //! ``` //! +//! For the special case where initializing a field is a single FFI-function call that cannot fail, +//! there exist helper functions [`Opaque::ffi_init`]. These functions initialize a single +//! [`Opaque`] field by just delegating to the FFI-function. You can use these in combination with +//! [`pin_init!`]. +//! +//! 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]: @@ -187,6 +195,7 @@ //! [`impl PinInit`]: PinInit //! [`impl Init`]: Init //! [`Opaque`]: kernel::types::Opaque +//! [`Opaque::ffi_init`]: kernel::types::Opaque::ffi_init //! [`pin_data`]: ::macros::pin_data use crate::{ diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index ff2b2fac951d..dbfae9bb97ce 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, @@ -248,6 +249,52 @@ impl Opaque { } } +macro_rules! opaque_init_funcs { + ($($abi:literal $name:ident($($arg_name:ident: $arg_typ:ident),*);)*) => { + impl Opaque { + $( + /// Create an initializer using the given initializer function. + /// + /// # Safety + /// + /// The given function **must** under all circumstances initialize the memory + /// location to a valid `T`. If it fails to do so it results in UB. + /// + /// If any parameters are given, those need to be valid for the function. Valid + /// means that calling the function with those parameters complies with the above + /// requirement **and** every other requirement on the function itself. + pub unsafe fn $name<$($arg_typ),*>( + init_func: unsafe extern $abi fn(*mut T $(, $arg_typ)*), + $($arg_name: $arg_typ,)* + ) -> impl PinInit { + // SAFETY: The safety contract of this function ensures that `init_func` fully + // initializes `slot`. + unsafe { + init::pin_init_from_closure(move |slot| { + init_func(Self::raw_get(slot) $(, $arg_name)*); + Ok(()) + }) + } + } + )* + } + } +} + +opaque_init_funcs! { + "C" ffi_init(); + "C" ffi_init1(arg1: A1); + "C" ffi_init2(arg1: A1, arg2: A2); + "C" ffi_init3(arg1: A1, arg2: A2, arg3: A3); + "C" ffi_init4(arg1: A1, arg2: A2, arg3: A3, arg4: A4); + + "Rust" manual_init(); + "Rust" manual_init1(arg1: A1); + "Rust" manual_init2(arg1: A1, arg2: A2); + "Rust" manual_init3(arg1: A1, arg2: A2, arg3: A3); + "Rust" manual_init4(arg1: A1, arg2: A2, arg3: A3, arg4: A4); +} + /// A sum type that always holds either a value of type `L` or `R`. pub enum Either { /// Constructs an instance of [`Either`] containing a value of type `L`.