Re: [PATCH v9 02/10] rust: module: add `THIS_MODULE` const to `ModuleMetadata` trait
Petr Pavlu <[email protected]> Mon, 3 Aug 2026 11:00:14 +0200
| Newsgroups | dev.linux.lists.driver-core,dev.linux.lists.nova-gpu,org.freedesktop.lists.dri-devel,org.kernel.vger.linux-block,org.kernel.vger.linux-kernel,org.kernel.vger.linux-kselftest,org.kernel.vger.linux-modules,org.kernel.vger.linux-pci,org.kernel.vger.netdev,org.kernel.vger.rust-for-linux |
|---|---|
| Message-ID | <[email protected]> |
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 <[email protected]> > Reviewed-by: Gary Guo <[email protected]> > Acked-by: Danilo Krummrich <[email protected]> > Reviewed-by: Alice Ryhl <[email protected]> > Signed-off-by: Alvin Sun <[email protected]> Acked-by: Petr Pavlu <[email protected]> > --- > 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 >