Received: by 2002:ab2:6991:0:b0:1f2:fff1:ace7 with SMTP id v17csp176835lqo; Wed, 27 Mar 2024 09:54:00 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVJB5W8RwNV20I8WEyzEVTNtGZ6ibnyee06u9NMMQi6wYGUFc6JwMX0nqafgB2x6EetEvU4EHLjriScS1dYyoeVeZQHyZMqvs3ASNYZig== X-Google-Smtp-Source: AGHT+IEaT9oMJt8RMlnCmFBNboui10yPPsaswxSCVgiaBUhooFfGwt4eOwAGQclpNXUmqj0YaCm9 X-Received: by 2002:a05:6a20:9593:b0:1a3:6704:8885 with SMTP id iu19-20020a056a20959300b001a367048885mr657614pzb.51.1711558439691; Wed, 27 Mar 2024 09:53:59 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711558439; cv=pass; d=google.com; s=arc-20160816; b=mYyU16jkVaLaAlvsKNBEzqTOOxfW0wnYW8E3YPgv6XzyHyAnnAR+0hn2chQY20LLe2 P3nP6pDF3SsIZfJrjDwA0rGjONaUjNeOUuBF1+kJsLBCcFyxyxtvbMNzLZlegRnUsb9L r6bA90clYoDso+X81ckcgXpYKrh36QoGUkNhtdnVOiss2fR2484hc6oDrLJyRWClNGgc VnCmzmTBTBaQZyXcCLMUgh3QinJRXmWYMUSogh/fbf3PZyUyyDnK1dhS6BzYUe0EotCC 24j0MwECGxnRws7u6cDmdn9J1IBkmv5NyaLztIxtsPb5V7j9b0ix5V3lddQIoIldJ5ZF wMnA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:feedback-id:references :in-reply-to:message-id:subject:cc:from:to:date:dkim-signature; bh=G/e/npas1/bGg+7eT8JQjC5kkF+PlgUlu2xBXL7JzFM=; fh=ILXzPT3nbZXhvwVwwPDMnlxaRbCVxHwkAsH+01sTnAQ=; b=xdFw8KR9K+0QEgPr+BfgRg1X68anKdf7CxIZcg1oAjTqJTrBcT7zOZuv6laK6RiCj+ WtbDpX2hY9uv0KJxvP974PtgtzzGBGSXpd0SiAJp1LQzBUOJ8401NNUAS88t+a6e9uxx zm0GS2tWKYm/GI39VgP5H6bS+hbmTanHe4HuCgnfYxd/AHGVe6i+SOAMBRgksy42SN/k 4/L0s8b6DexcKdD3mclLXHnKpn4NmjKUvg6Nr0ydH8S76GUOYYfLY/zJNT1zX1fZxICM kMpppFRQxJabKnga11zfnteNUpu/iUYKN1aaM4aWZDwmWhfpU22MQH4qAfWny3iGl331 kqcQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=CmtSSXme; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-121618-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-121618-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id x66-20020a626345000000b006e9f3c634a2si9314177pfb.321.2024.03.27.09.53.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 09:53:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-121618-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=CmtSSXme; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-121618-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-121618-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id EB66DB2FFBE for ; Wed, 27 Mar 2024 16:17:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 142AB12F584; Wed, 27 Mar 2024 16:16:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="CmtSSXme" Received: from mail-4322.protonmail.ch (mail-4322.protonmail.ch [185.70.43.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BC7D712F595 for ; Wed, 27 Mar 2024 16:16:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.43.22 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711556213; cv=none; b=WCoJuNlY3zOjztjWITxXBIzWRK2ywoqK3YEgnI17W2Z++Lkm2T1ooMfpI4kdOno6ORwVhdCOAoNYcqq25fxHSmfKLQVfjEdnIwgGEAbjAQp8/bvbQGKhd2kvtk4PA+BSC6xtLlyyZDBUIX6cbY3JCoHSwzK+ROhMrVBnpD3X6Cc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711556213; c=relaxed/simple; bh=sT+XSyK8g3hoLB6WyZQzp0atORjqibEBbeZ/n0I3qqE=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=FEj0tZbqIqVw0WZ0vB9H0RMIktLwkan9uexEb/7K67zPUZdgk0nd8IkpbA9wbod9BF0c313GGBVflm5DUWKOS/HrKsV5YjgyUiOWSHMw8xYzQqul+qanK1TsRgaOVHj8enJy1J5xazHJny5C7p5IgBCxu/bi0s3ofF0NhZWWA/I= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me; spf=pass smtp.mailfrom=proton.me; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b=CmtSSXme; arc=none smtp.client-ip=185.70.43.22 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=proton.me DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1711556208; x=1711815408; bh=G/e/npas1/bGg+7eT8JQjC5kkF+PlgUlu2xBXL7JzFM=; 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=CmtSSXmeJqyCDvrxz/ujRxKklqo3n5yinfdAzeFaCpXNwM+/LouWzkuH1MMkkl4vR e1WCWozqUYgIsd2blozs0Nhfo/YmBNfhjyrulekfZxQ8lGnn5Mj20ShGWX++AujijA yuZ905Ge6s2KeqTdeH+E5vxk/afjyrI/faqnSSTey18eXQOq2sGqeroRpE0evDK+CI PlOAfYgNr/mY4C0sdz4OAIj6opFxya+hNVuIvT6wjt5Pm/LTvPrCfkZx1U+IQ2CruN er0mphtVZ8dCEGhkdhbvMKUGGtQXO+k6wUBuy5UVqUwshiZcdj/jOdUfyyjLnZ4rw+ IiwujdrO/mohw== Date: Wed, 27 Mar 2024 16:16:33 +0000 To: Wedson Almeida Filho , rust-for-linux@vger.kernel.org From: Benno Lossin Cc: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , Alice Ryhl , linux-kernel@vger.kernel.org, Wedson Almeida Filho Subject: Re: [PATCH 1/2] rust: introduce `InPlaceModule` Message-ID: In-Reply-To: <20240327032337.188938-2-wedsonaf@gmail.com> References: <20240327032337.188938-1-wedsonaf@gmail.com> <20240327032337.188938-2-wedsonaf@gmail.com> Feedback-ID: 71780778:user:proton Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 27.03.24 04:23, Wedson Almeida Filho wrote: > From: Wedson Almeida Filho >=20 > This allows modules to be initialised in-place in pinned memory, which > enables the usage of pinned types (e.g., mutexes, spinlocks, driver > registrations, etc.) in modules without any extra allocations. >=20 > Drivers that don't need this may continue to implement `Module` without > any changes. >=20 > Signed-off-by: Wedson Almeida Filho I have some suggestions below, with those fixed, Reviewed-by: Benno Lossin > --- > rust/kernel/lib.rs | 25 ++++++++++++++++++++++++- > rust/macros/module.rs | 18 ++++++------------ > 2 files changed, 30 insertions(+), 13 deletions(-) >=20 > diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs > index 5c641233e26d..64aee4fbc53b 100644 > --- a/rust/kernel/lib.rs > +++ b/rust/kernel/lib.rs > @@ -62,7 +62,7 @@ > /// The top level entrypoint to implementing a kernel module. > /// > /// For any teardown or cleanup operations, your type may implement [`Dr= op`]. > -pub trait Module: Sized + Sync { > +pub trait Module: Sized + Sync + Send { > /// Called at module initialization time. > /// > /// Use this method to perform whatever setup or registration your m= odule > @@ -72,6 +72,29 @@ pub trait Module: Sized + Sync { > fn init(module: &'static ThisModule) -> error::Result; > } >=20 > +/// A module that is pinned and initialised in-place. > +pub trait InPlaceModule: Sync + Send { > + /// Creates an initialiser for the module. > + /// > + /// It is called when the module is loaded. > + fn init(module: &'static ThisModule) -> impl init::PinInit; > +} > + > +impl InPlaceModule for T { > + fn init(module: &'static ThisModule) -> impl init::PinInit { > + let initer =3D move |slot: *mut Self| { > + let m =3D ::init(module)?; > + > + // SAFETY: `slot` is valid for write per the contract with `= pin_init_from_closure`. > + unsafe { slot.write(m) }; > + Ok(()) > + }; > + > + // SAFETY: On success, `initer` always fully initialises an inst= ance of `Self`. > + unsafe { init::pin_init_from_closure(initer) } > + } > +} > + > /// Equivalent to `THIS_MODULE` in the C API. > /// > /// C header: [`include/linux/export.h`](srctree/include/linux/export.h) > diff --git a/rust/macros/module.rs b/rust/macros/module.rs > index 27979e582e4b..0b2bb4ec2fba 100644 > --- a/rust/macros/module.rs > +++ b/rust/macros/module.rs > @@ -208,7 +208,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream = { > #[used] > static __IS_RUST_MODULE: () =3D (); >=20 > - static mut __MOD: Option<{type_}> =3D None; > + static mut __MOD: core::mem::MaybeUninit<{type_}> =3D core::= mem::MaybeUninit::uninit(); I would prefer `::core::mem::MaybeUninit`, since that prevents accidentally referring to a module named `core`. >=20 > // SAFETY: `__this_module` is constructed by the kernel at l= oad time and will not be > // freed until the module is unloaded. > @@ -275,23 +275,17 @@ pub(crate) fn module(ts: TokenStream) -> TokenStrea= m { > }} >=20 > fn __init() -> core::ffi::c_int {{ > - match <{type_} as kernel::Module>::init(&THIS_MODULE) {{ > - Ok(m) =3D> {{ > - unsafe {{ > - __MOD =3D Some(m); > - }} > - return 0; > - }} > - Err(e) =3D> {{ > - return e.to_errno(); > - }} > + let initer =3D <{type_} as kernel::InPlaceModule>::init(= &THIS_MODULE); Ditto with `::kernel::InPlaceModule`. > + match unsafe {{ initer.__pinned_init(__MOD.as_mut_ptr())= }} {{ This requires that the `PinInit` trait is in scope, I would use: match unsafe {{ ::kernel::init::PinInit::__pinned_init(initer, __MOD.a= s_mut_ptr()) }} {{ --=20 Cheers, Benno > + Ok(m) =3D> 0, > + Err(e) =3D> e.to_errno(), > }} > }} >=20 > fn __exit() {{ > unsafe {{ > // Invokes `drop()` on `__MOD`, which should be used= for cleanup. > - __MOD =3D None; > + __MOD.assume_init_drop(); > }} > }} >=20 > -- > 2.34.1 >=20