Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp405415lqh; Thu, 28 Mar 2024 05:56:46 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXlmQ1AWD+MFmhsEJQbicbQvgOASWx2t7iQ52skmhjmiqB7gMkDq2UIp6chuA3KQBeq9+Rp4SnAyUU4QJ3M/AzVVgDJhoaciXVxN8ZrSA== X-Google-Smtp-Source: AGHT+IGpvtocEnI8MZMZnrq/vm4cRhwByDdZjxR0hygon6xymhk66RNWGp3+7imOJqitDMsOUvPG X-Received: by 2002:a25:2c3:0:b0:dc7:4460:878a with SMTP id 186-20020a2502c3000000b00dc74460878amr2625873ybc.3.1711630606493; Thu, 28 Mar 2024 05:56:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711630606; cv=pass; d=google.com; s=arc-20160816; b=rotiahDjLmGgJ4Vrmn88T6Z/Z1zEocKj6JqH0rQZpyv5wqqM4XGTC0ErWi+IXDyvmx nyzHNiGovdsRkAIiY/RGjKjUUSh84KhzwTtQ9KqcztCCfRgS9TH56z47wNPxp4+zLqZJ gSFcwGkxR8EwGjJ1Mi+zxhiuasQYzRACRtghFH3L1fE9qeprF5S0BNvIaXeoMNAEpj94 lIm9uDovKwkp919PUn2J184Qf+cc/1DHth8+skYqgLsCeuHYLZiMyYalDZlDdlzDdCj8 J289KmYJ6EErpgn9JJlHN/TLfepnj238Io5wY/yUm3DGx1EuOe+dffxurltyNwpOwigY P+ZA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :dkim-signature; bh=53348LneH/B5psreK0+yHl8Gf7XqOlbsOvfHAb0AlJY=; fh=9GI+1qvK+WLDJdkc1HzllQKBJjXLYaYOrjHVAMVFblc=; b=MogtnFJ/W3oD6ic6sLX1q6OcXm3JDtwFFymWt1Iwp5if5amTZoWOPywtX4/1m1U1SM orNd8O/rgi35Tk+OFisNzbtcwdbGpOZ9ugURRzT5mOBhZQ1xNSgym7uzX4h9rKRhe/Te EomARb2eSEtEm8X0DZyuml9DvbIkEDKAK/ovAaFWOdZ+BLEq41FJULDpP6hdBKJ1ike8 JreWbi9C+MyStU3pA/lDkLd5h6urpnKJcVvwWAH2vXzIRZgEhSG3VqJpjSafMFtw/6L8 AcvcsncADv33jCWW9TeKSHxkhurYfwk9k3I/6oAq2jKycHkelaiG2QDcRe2CH0ccAHpd p/rg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=SEQ+2Jlv; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-122930-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122930-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id v7-20020ad45287000000b0068fff3e72ccsi1317276qvr.372.2024.03.28.05.56.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 05:56:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-122930-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=SEQ+2Jlv; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-122930-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122930-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id F2BE41C295C6 for ; Thu, 28 Mar 2024 12:56:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 845967FBB3; Thu, 28 Mar 2024 12:56:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="SEQ+2Jlv" Received: from mail-yw1-f170.google.com (mail-yw1-f170.google.com [209.85.128.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EE5A37BB14; Thu, 28 Mar 2024 12:56:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711630593; cv=none; b=ALBTDnbG/6A2Ma1pyi181TYgZOKELjFNDKG5vaGkAgvpolUbDEgHvZ8AMKE6+Ld1OAC/A9WtIbI9I/iXSFer3XvqJnIKBU9wNlayxFOS7WIMQyfLd/PeSVBMjeBzWK4Ib7rmp7vzDJvo2+zVMK9v7w4UFf2J/35lwo8+3U8k2rg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711630593; c=relaxed/simple; bh=hQzmvbxhNNSAwoVfixFw3/NX8Dks0Gpga5/5/VJjYVc=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=PmIeRv18cTAo2mYaVHjbbXypX2UT6+6YVxtUSmgVRPq3qMOHtr2nWLtNVAo69JJ1UQLFMaKnoHI67PRcYM3PlJBP/2ONPJWhEGF068LS/lC4xUjALuP0vuPWgBn6quT/tPZ8gykNzlfn03DEubq+0tpzIEZd9/yFqV+V2HVWKpM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=SEQ+2Jlv; arc=none smtp.client-ip=209.85.128.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-yw1-f170.google.com with SMTP id 00721157ae682-6114c9b4d83so8203027b3.3; Thu, 28 Mar 2024 05:56:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711630591; x=1712235391; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=53348LneH/B5psreK0+yHl8Gf7XqOlbsOvfHAb0AlJY=; b=SEQ+2Jlv3BSulrNHt6iBAXJUx0oUUurrzJMw780JAVGQCyTF0GUdIUtM/0qsaN1E9l JOC8X9AWEQebHB+sbb6yWY8/UWVrpIuJNylPgJ+Gyxkqpt5AgcSr5fbtKpbG34S193Gp w210meMdXcq2E6OVdqRNecCYsLV4JEQe1T58onOMgP0VCkeErlA4dH5ARgQ0ZnQ6Of9B GN0hma2r0HE6UEtG2DzkSSh4kQ/DcJWpAPS4sglBfAKtu3g3cpeJKB2FoaQaweqTCeDd PknHjpFFVTp0br9cZL/sCCueJ/rRew9lmBTZfvJ8XrdQDFaavPUfthH9qRHi6QHCGhz1 7vzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711630591; x=1712235391; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=53348LneH/B5psreK0+yHl8Gf7XqOlbsOvfHAb0AlJY=; b=CT2jOaD/Gwgd4jawaAIjZ3klaY2VTxnfU2HU9R/eJmKpPvb3wmX7PkzoHJh8fX+gS0 XucjiUJbaXhcbgNW7Z+D1ny2C1t5OxfG/YfsUi9mfXp+ZoK+pF+T8iKdAJx6nzJQxeC4 6/Y/ooFVerwiVGIQzNuZHkF0b6u8x7E8q2BwTrvPdQScSSZ2ag18b1z7xkjysNKpX/6s ml0q4Ju506MQkqbQoD62HveYwAqYNwM1PN/2yr9SeDue1+tShVXfSWurYIfNDOc6D53r BpOA8LmXleKHtPqg3k0qE0tL+dWfvLMyEq+JG30liBqsKwwa2Mo85cwC88fXFNR8HjZj ZNZA== X-Forwarded-Encrypted: i=1; AJvYcCWYEQ0e1zGjD1DkgU2zsOMXab1EHbdD/Xr+UnX+pTl9O2HLZxQCgofKtuy/ljIdwlPAASi9I+uApLSWBVShSelpeLSew5b+R6NC0y2L X-Gm-Message-State: AOJu0YxWz61yX/EglNsUC2QYdtFb6jUsFH5jd6c7oxjRk6jQxryBGzC6 TX1XRtJfoKZVCnhBc2hK4E4vK9ncEvdX0PQnotoonJI17TyW9JnN4tLvwy42R6hnIV1EQxP1+rm vVJ0y5bK8v1TPceW4vZ4iLXmb2sNTw3Qq X-Received: by 2002:a0d:eb8c:0:b0:611:9a29:b2f2 with SMTP id u134-20020a0deb8c000000b006119a29b2f2mr2641958ywe.34.1711630590768; Thu, 28 Mar 2024 05:56:30 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240327032337.188938-1-wedsonaf@gmail.com> <20240327032337.188938-2-wedsonaf@gmail.com> In-Reply-To: From: Wedson Almeida Filho Date: Thu, 28 Mar 2024 09:56:19 -0300 Message-ID: Subject: Re: [PATCH 1/2] rust: introduce `InPlaceModule` To: Benno Lossin Cc: rust-for-linux@vger.kernel.org, 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 Content-Type: text/plain; charset="UTF-8" On Wed, 27 Mar 2024 at 13:16, Benno Lossin wrote: > > On 27.03.24 04:23, Wedson Almeida Filho wrote: > > From: Wedson Almeida Filho > > > > 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. > > > > Drivers that don't need this may continue to implement `Module` without > > any changes. > > > > 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(-) > > > > 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 [`Drop`]. > > -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 module > > @@ -72,6 +72,29 @@ pub trait Module: Sized + Sync { > > fn init(module: &'static ThisModule) -> error::Result; > > } > > > > +/// 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 = move |slot: *mut Self| { > > + let m = ::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 instance 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: () = (); > > > > - static mut __MOD: Option<{type_}> = None; > > + static mut __MOD: core::mem::MaybeUninit<{type_}> = core::mem::MaybeUninit::uninit(); > > I would prefer `::core::mem::MaybeUninit`, since that prevents > accidentally referring to a module named `core`. Makes sense, changed in v3. I also added a patch to v3 that adds `::` to all cases in the code generated by the macro. > > > > > // SAFETY: `__this_module` is constructed by the kernel at load time and will not be > > // freed until the module is unloaded. > > @@ -275,23 +275,17 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream { > > }} > > > > fn __init() -> core::ffi::c_int {{ > > - match <{type_} as kernel::Module>::init(&THIS_MODULE) {{ > > - Ok(m) => {{ > > - unsafe {{ > > - __MOD = Some(m); > > - }} > > - return 0; > > - }} > > - Err(e) => {{ > > - return e.to_errno(); > > - }} > > + let initer = <{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.as_mut_ptr()) }} {{ Also changed in v3. > -- > Cheers, > Benno > > > + Ok(m) => 0, > > + Err(e) => e.to_errno(), > > }} > > }} > > > > fn __exit() {{ > > unsafe {{ > > // Invokes `drop()` on `__MOD`, which should be used for cleanup. > > - __MOD = None; > > + __MOD.assume_init_drop(); > > }} > > }} > > > > -- > > 2.34.1 > > >