From patchwork Mon Apr 3 16:05:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: y86-dev X-Patchwork-Id: 78621 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2418420vqo; Mon, 3 Apr 2023 09:13:21 -0700 (PDT) X-Google-Smtp-Source: AKy350aFmTCNDHrqJsV2vYy0Gyy6k7asgH4pHuAxiKJSQjOrh5VnTQe6UTQheXRhzVoL3rEIebkB X-Received: by 2002:aa7:954a:0:b0:625:cb74:9e01 with SMTP id w10-20020aa7954a000000b00625cb749e01mr36627627pfq.25.1680538401256; Mon, 03 Apr 2023 09:13:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680538401; cv=none; d=google.com; s=arc-20160816; b=dR0KWu+pAsGFLpNinWUexXYLHQxwyV4zzcvpsAEB9D9lm0BKWHm91CVlgkuFLZFDJz yEm3CsQu5OYrJwrR0UQ12d2g/5JREFf//7Yd9C37SFpedbVhtfdzyWJo7LcJsxVHmXQM dpuzGo0GE2aPuJ93ofugjiVmjlde713339R5l/Q245J/NuJop5HROYz6wXEMDNQoY+w1 Rd6fxbVsCzV4kmnej1ieVFj+dEDWAXN9pkDTHSCysf6FN+VQRQZGcxpPqso7FA7w2Txn d9WEAyR6rt8Rgmig50jkYkJM0mB9X5LB6B5QsaUU4eWONw6wR1okR78ZCtHXVOnCSclK k/3w== 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=yfXd5D8BxAfgdr5rvbjqK+tp79cUPO+/3tQLSPK3UIg=; b=JeMsNnJNgYhW0ggta8TAzANcQbfct/a2G4OSrFCSLeQvzxz4UPPGKJWGAIPH9FwoLB 5kSgQuuKvVoEDiitjRtJafzquHRbyd98JWJ6pGWykzOr4uLPnPBEgCh8TG+YKqGkpcC0 W/mfWukUUPIZ9oF11aMe11P+7fWF3ioFJ0LMj5L0ld/7GWqA588h0k30P/mLucjlCa5M wnFhoRSzMhiHUDrfxSUA/RHLvU2tyop8JDSZgqPcZs3HZzRXY1qJkvH7soYr3ZLqX9Fi WNiZU2/8bAUtjwI2WOuFhM60F/eyo8/4Mx1MXoyexFmOHvYoipOXuxhdbbMm9r1JiPKO Eqlg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b=TCVLk2yu; 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 a27-20020aa795bb000000b00627ed0fa77csi8323705pfk.188.2023.04.03.09.13.08; Mon, 03 Apr 2023 09:13:21 -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=TCVLk2yu; 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 S233214AbjDCQGM (ORCPT + 99 others); Mon, 3 Apr 2023 12:06:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33520 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233202AbjDCQF5 (ORCPT ); Mon, 3 Apr 2023 12:05:57 -0400 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C41361FC6 for ; Mon, 3 Apr 2023 09:05:55 -0700 (PDT) Date: Mon, 03 Apr 2023 16:05:51 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1680537953; x=1680797153; bh=yfXd5D8BxAfgdr5rvbjqK+tp79cUPO+/3tQLSPK3UIg=; 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=TCVLk2yubXDKuQlr3pe5wZyiIV9qDsFS1iwqzHpJfktLn8TfIvPkX1U7vQW7s8eJ0 L652DT+NIWHREzxkRYZc2dhoYadI1BFb5yRx8zTR0JW0kdSnyIi6A+lTTkwgZ5smhe cUH2ZWbsnSg0SDz9c2LmoASMukzxddVPi9XKuWaaXh4sgY1KIcl88DrIdBwRlST/5x xbu2cmHykV1BYcbsJpUVDKRy9oluuB+eIX6ONwA8jW5J/VKuNnapEEVVXd+0Pb43vH mcdSNvd4C6Bfg0hl4EXKMw/YTr3GGOXShO74ey++rWY6GbjdXEHIHFx3zFupWilDtO QPyJWBWInyPQw== 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 , Alice Ryhl , Andreas Hindborg Subject: [PATCH v5 13/15] rust: types: add common init-helper functions for `Opaque` Message-ID: <20230403160511.174894-6-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?1762172234252875414?= X-GMAIL-MSGID: =?utf-8?q?1762172234252875414?= 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 09bba4e86ace..b4947e8cf731 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`.