From patchwork Tue Mar 21 19:50:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: y86-dev X-Patchwork-Id: 73037 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp1986173wrt; Tue, 21 Mar 2023 13:11:08 -0700 (PDT) X-Google-Smtp-Source: AK7set+4/FqmkF9KDtuIYQYI0NaDLgogn3fnTUAcpoL6AEIx6v76+wpFaPkgdY8OFYYZ+NdwK8by X-Received: by 2002:a17:903:1111:b0:1a1:db10:7ba5 with SMTP id n17-20020a170903111100b001a1db107ba5mr249321plh.41.1679429467900; Tue, 21 Mar 2023 13:11:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679429467; cv=none; d=google.com; s=arc-20160816; b=IYrF690J8mLouoisT6t2JLZ51XUrUwfLzreMtEUqzQuBg56S66FvwPPMUqMSRqzNpC miT32EW6pU5O9bAYoqNAZ/XUZoyuDoGSu3vC7BCAFyzDPa+8KEeiPGB3TW9hoiUPm7Su w3hCOBxdpVI279lzPYFlz7GZigNDItjM5P8DVgpUccJ6jYsEkhy4BNyLhmj7caTK6eWh q4aN4a7r++mvNWb5d9aDhbvPx05o5teJ+3hDF+DHMqFh1RGmR/dq1MsHfGWiMqcPBTSJ wgKWmRlw5aG+QxmGR6RymYrq5JjCIORvv+m9bGjbSriDRDvkmsXFbEJVA/HkGpcOkm8Y sEPQ== 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=+p+odlGt4Lpa7j8EN6KwSae2Ae/jbNLZRSBVIgNchK8=; b=QS0jikBglFWd72idOgJO98k+4gIriaxiuPYkfx7hXL9cGHZ3FofsX2Ra0c4MPm40T6 faRtz7ouZBA8zRJZ/LmVEeQEWNZdPjQRn6TESKCtLWnkncY4aFAaW3Y330tIZctiT8o5 Pd5Bprwv06Tx1SZrxd4xGdapeD0ISBrzYpKYiOYs5zEJTPuGe8hQzGYauZhuLA6ln9co upE21diD4J8h9jesnpE0b5peDbBCOgwBKVRdaxDFD7BbArADTFX1XDHT7ajWXyR84PAn YNdDltYMwuXheLnungcpzU3BI1lPuUlRgpSEJG+oI2iEfkQIoCM4xmOzSED0Amvljzio J1ng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b="ZWimU1u/"; 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 q13-20020a17090311cd00b001a0433208easi15249972plh.410.2023.03.21.13.10.53; Tue, 21 Mar 2023 13:11:07 -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="ZWimU1u/"; 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 S229839AbjCUTvT (ORCPT + 99 others); Tue, 21 Mar 2023 15:51:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50030 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229923AbjCUTvQ (ORCPT ); Tue, 21 Mar 2023 15:51:16 -0400 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7047858492; Tue, 21 Mar 2023 12:50:35 -0700 (PDT) Date: Tue, 21 Mar 2023 19:50:05 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1679428225; x=1679687425; bh=+p+odlGt4Lpa7j8EN6KwSae2Ae/jbNLZRSBVIgNchK8=; 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=ZWimU1u/tRce1Mxz5ecaVPMKqjL4yJUpOxb+ZGSdYyUVD6VkjzFJQjhS+kawVYSzv nw/t3h0YWanoOp6hEZtOc3V//NDlXM1wzgW7elSg4u5fPmUHykFyYvr3Byg/nRRvcw wNXK4RhrTKqBsj6MdgJyNjSiDZ0U92FOKPKmlCC/ks8rwhYJajqoFk4qV+cEUZ05AP B8rTj9FUHq/OXcnoQX8okfDxICJYkJWfB4Wp0v5A9F6Oun6HBMkGxWHfcrPD0kqSIm nQGqAti6lV6lsT++J9p5+yGi6Gs/ltT4YJMaYNFdyIyFeS8b+/6WDgWo4KU39uB7vg a8pZlbHLUerdg== To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= From: Benno Lossin Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH v2 4/5] rust: init: add common init-helper functions for `Opaque` Message-ID: <20230321194934.908891-5-y86-dev@protonmail.com> In-Reply-To: <20230321194934.908891-1-y86-dev@protonmail.com> References: <20230321194934.908891-1-y86-dev@protonmail.com> Feedback-ID: 40624463:user:proton MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_MSPIKE_H2, SPF_HELO_PASS,SPF_PASS,URIBL_BLOCKED autolearn=ham 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?1761009327383247398?= X-GMAIL-MSGID: =?utf-8?q?1761009433983179834?= Add helper functions to more easily initialize `Opaque` via FFI. These functions take a function pointer to the FFI-initialization function and take between 0-4 other arguments. It then returns an initializer that uses the FFI function along with the given arguments to initialize an `Opaque`. Signed-off-by: Benno Lossin --- rust/kernel/init.rs | 1 + rust/kernel/init/common.rs | 42 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 rust/kernel/init/common.rs -- 2.39.2 diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs index 895845db6f2b..5b8adb8727b2 100644 --- a/rust/kernel/init.rs +++ b/rust/kernel/init.rs @@ -207,6 +207,7 @@ use core::{ ptr, }; +pub mod common; #[doc(hidden)] pub mod macros; diff --git a/rust/kernel/init/common.rs b/rust/kernel/init/common.rs new file mode 100644 index 000000000000..f8c6e9dff786 --- /dev/null +++ b/rust/kernel/init/common.rs @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: Apache-2.0 OR MIT + +//! Module containing common kernel initializer functions. + +use crate::{ + init::{self, PinInit}, + types::Opaque, +}; + +macro_rules! create_func { + ($name:ident $(, $arg_name:ident: $arg_typ:ident)*) => { + /// Create an initializer using the given initializer function from C. + /// + /// # 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( + init_func: unsafe extern "C" fn(*mut T $(, $arg_name: $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(Opaque::raw_get(slot) $(, $arg_name)*); + Ok(()) + }) + } + } + } +} + +create_func!(ffi_init); +create_func!(ffi_init1, arg1: A1); +create_func!(ffi_init2, arg1: A1, arg2: A2); +create_func!(ffi_init3, arg1: A1, arg2: A2, arg3: A3); +create_func!(ffi_init4, arg1: A1, arg2: A2, arg3: A3, arg4: A4);