From: Petr Pavlu <petr.pavlu@suse.com>
To: Alvin Sun <alvin.sun@linux.dev>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Luis Chamberlain" <mcgrof@kernel.org>,
"Daniel Gomez" <da.gomez@kernel.org>,
"Sami Tolvanen" <samitolvanen@google.com>,
"Aaron Tomlin" <atomlin@atomlin.com>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Arnd Bergmann" <arnd@arndb.de>,
"Brendan Higgins" <brendan.higgins@linux.dev>,
"David Gow" <david@davidgow.net>,
"Rae Moar" <raemoar63@gmail.com>,
"Breno Leitao" <leitao@debian.org>,
"Jens Axboe" <axboe@kernel.dk>,
"Dave Ertman" <david.m.ertman@intel.com>,
"Leon Romanovsky" <leon@kernel.org>,
"Igor Korotin" <igor.korotin@linux.dev>,
"FUJITA Tomonori" <fujita.tomonori@gmail.com>,
"Bjorn Helgaas" <bhelgaas@google.com>,
"Krzysztof Wilczyński" <kwilczynski@kernel.org>,
"Arve Hjønnevåg" <arve@android.com>,
"Todd Kjos" <tkjos@android.com>,
"Christian Brauner" <brauner@kernel.org>,
"Carlos Llamas" <cmllamas@google.com>,
rust-for-linux@vger.kernel.org, linux-modules@vger.kernel.org,
driver-core@lists.linux.dev, dri-devel@lists.freedesktop.org,
nova-gpu@lists.linux.dev, linux-kselftest@vger.kernel.org,
kunit-dev@googlegroups.com, linux-block@vger.kernel.org,
linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
linux-pci@vger.kernel.org
Subject: Re: [PATCH v9 02/10] rust: module: add `THIS_MODULE` const to `ModuleMetadata` trait
Date: Mon, 3 Aug 2026 11:00:14 +0200 [thread overview]
Message-ID: <6e9e8189-e27b-4279-9fed-b7da5da62419@suse.com> (raw)
In-Reply-To: <20260723-fix-fops-owner-v9-2-c1c3af7f7bcb@linux.dev>
On 7/23/26 4:10 AM, Alvin Sun wrote:
> Since `const_refs_to_static` has been stable as of the MSRV bump, a
> `ThisModule` pointer can now be used in const contexts.
>
> Add a `THIS_MODULE` const to the `ModuleMetadata` trait so that modules
> can provide their `ThisModule` pointer in const contexts such as static
> `file_operations`.
>
> Add a `this_module()` helper to retrieve the `THIS_MODULE` pointer of a
> given module type, and update `__init` to use it instead of the
> `THIS_MODULE` static generated by the `module!` macro.
>
> The `static THIS_MODULE` generated by the `module!` macro is retained
> for backwards compatibility with existing users and removed in a later
> patch once all references have been migrated.
>
> Assisted-by: opencode:glm-5.2
> Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org>
> Reviewed-by: Gary Guo <gary@garyguo.net>
> Acked-by: Danilo Krummrich <dakr@kernel.org>
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Alvin Sun <alvin.sun@linux.dev>
Acked-by: Petr Pavlu <petr.pavlu@suse.com>
> ---
> rust/kernel/module.rs | 9 +++++++++
> rust/macros/module.rs | 18 +++++++++++++++++-
> 2 files changed, 26 insertions(+), 1 deletion(-)
>
> diff --git a/rust/kernel/module.rs b/rust/kernel/module.rs
> index be242a82e86d2..d713705984477 100644
> --- a/rust/kernel/module.rs
> +++ b/rust/kernel/module.rs
> @@ -42,6 +42,15 @@ fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, crate::erro
> pub trait ModuleMetadata {
> /// The name of the module as specified in the `module!` macro.
> const NAME: &'static crate::str::CStr;
> +
> + /// The module's `THIS_MODULE` pointer.
> + const THIS_MODULE: ThisModule;
> +}
> +
> +/// Returns a reference to the `THIS_MODULE` of the given module type.
> +#[inline]
> +pub const fn this_module<M: ModuleMetadata>() -> &'static ThisModule {
> + &M::THIS_MODULE
> }
>
> /// Equivalent to `THIS_MODULE` in the C API.
> diff --git a/rust/macros/module.rs b/rust/macros/module.rs
> index 06c18e2075083..aa9a618d5d19e 100644
> --- a/rust/macros/module.rs
> +++ b/rust/macros/module.rs
> @@ -519,6 +519,22 @@ pub(crate) fn module(info: ModuleInfo) -> Result<TokenStream> {
>
> impl ::kernel::ModuleMetadata for #type_ {
> const NAME: &'static ::kernel::str::CStr = #name_cstr;
> +
> + #[cfg(MODULE)]
> + const THIS_MODULE: ::kernel::ThisModule = {
> + extern "C" {
> + static __this_module: ::kernel::types::Opaque<::kernel::bindings::module>;
> + }
> +
> + // SAFETY: `__this_module` is constructed by the kernel at load time
> + // and lives until the module is unloaded.
> + unsafe { ::kernel::ThisModule::from_ptr(__this_module.get()) }
> + };
> +
> + #[cfg(not(MODULE))]
> + const THIS_MODULE: ::kernel::ThisModule = unsafe {
> + ::kernel::ThisModule::from_ptr(::core::ptr::null_mut())
> + };
> }
>
> // Double nested modules, since then nobody can access the public items inside.
> @@ -616,7 +632,7 @@ pub extern "C" fn #ident_exit() {
> /// This function must only be called once.
> unsafe fn __init() -> ::kernel::ffi::c_int {
> let initer = <super::super::LocalModule as ::kernel::InPlaceModule>::init(
> - &super::super::THIS_MODULE
> + ::kernel::module::this_module::<super::super::LocalModule>()
> );
> // SAFETY: No data race, since `__MOD` can only be accessed by this module
> // and there only `__init` and `__exit` access it. These functions are only
>
next prev parent reply other threads:[~2026-08-03 9:00 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-23 2:10 [PATCH v9 00/10] Fix missing fops.owner in Rust DRM/misc abstractions Alvin Sun
2026-07-23 2:10 ` [PATCH v9 01/10] rust: module: move module types into `module.rs` Alvin Sun
2026-08-03 8:58 ` Petr Pavlu
2026-07-23 2:10 ` [PATCH v9 02/10] rust: module: add `THIS_MODULE` const to `ModuleMetadata` trait Alvin Sun
2026-08-03 9:00 ` Petr Pavlu [this message]
2026-07-23 2:10 ` [PATCH v9 03/10] rust: doctest: add LocalModule fallback for #[vtable] ThisModule Alvin Sun
2026-07-23 2:10 ` [PATCH v9 04/10] rust: macros: auto-insert OwnerModule in #[vtable] Alvin Sun
2026-07-23 2:10 ` [PATCH v9 05/10] rust: drm: set fops.owner from driver module pointer Alvin Sun
2026-07-23 2:10 ` [PATCH v9 06/10] rust: miscdevice: " Alvin Sun
2026-07-23 2:10 ` [PATCH v9 07/10] rust: configfs: use `LocalModule` for `THIS_MODULE` Alvin Sun
2026-07-23 2:10 ` [PATCH v9 08/10] rust_binder: " Alvin Sun
2026-07-23 2:10 ` [PATCH v9 09/10] rust: macros: remove `THIS_MODULE` static from `module!` Alvin Sun
2026-08-03 9:01 ` Petr Pavlu
2026-07-23 2:10 ` [PATCH v9 10/10] rust: module: update MAINTAINERS to cover module.rs Alvin Sun
2026-08-03 8:54 ` Petr Pavlu
2026-07-28 8:38 ` [PATCH v9 00/10] Fix missing fops.owner in Rust DRM/misc abstractions Alvin Sun
2026-07-31 6:29 ` Miguel Ojeda
2026-08-03 9:10 ` Petr Pavlu
2026-07-31 6:46 ` Miguel Ojeda
2026-07-31 16:34 ` Alvin Sun
2026-07-31 16:43 ` Gary Guo
2026-07-31 16:57 ` Alvin Sun
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6e9e8189-e27b-4279-9fed-b7da5da62419@suse.com \
--to=petr.pavlu@suse.com \
--cc=a.hindborg@kernel.org \
--cc=airlied@gmail.com \
--cc=aliceryhl@google.com \
--cc=alvin.sun@linux.dev \
--cc=arnd@arndb.de \
--cc=arve@android.com \
--cc=atomlin@atomlin.com \
--cc=axboe@kernel.dk \
--cc=bhelgaas@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=brauner@kernel.org \
--cc=brendan.higgins@linux.dev \
--cc=cmllamas@google.com \
--cc=da.gomez@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=david.m.ertman@intel.com \
--cc=david@davidgow.net \
--cc=dri-devel@lists.freedesktop.org \
--cc=driver-core@lists.linux.dev \
--cc=fujita.tomonori@gmail.com \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=igor.korotin@linux.dev \
--cc=kunit-dev@googlegroups.com \
--cc=kwilczynski@kernel.org \
--cc=leitao@debian.org \
--cc=leon@kernel.org \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-modules@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=mcgrof@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=nova-gpu@lists.linux.dev \
--cc=ojeda@kernel.org \
--cc=raemoar63@gmail.com \
--cc=rafael@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=samitolvanen@google.com \
--cc=simona@ffwll.ch \
--cc=tkjos@android.com \
--cc=tmgross@umich.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox