diff options
| author | Alvin Sun <alvin.sun@linux.dev> | 2026-08-11 14:39:44 +0800 |
|---|---|---|
| committer | Miguel Ojeda <ojeda@kernel.org> | 2026-08-12 09:36:26 +0200 |
| commit | 54f846db7b35069959833d2f6ace998c2e11c908 (patch) | |
| tree | 229b11961009605242fb89241be7eb08dfcdfa88 /rust/kernel/module.rs | |
| parent | 0752a2e96e131ef1dff9740e1d4eb4a960778edb (diff) | |
rust: module: add `THIS_MODULE` const to `ModuleMetadata` trait
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>
Acked-by: Petr Pavlu <petr.pavlu@suse.com>
Signed-off-by: Alvin Sun <alvin.sun@linux.dev>
Link: https://patch.msgid.link/20260811-fix-fops-owner-v10-2-7e71776f9dbe@linux.dev
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Diffstat (limited to 'rust/kernel/module.rs')
| -rw-r--r-- | rust/kernel/module.rs | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/rust/kernel/module.rs b/rust/kernel/module.rs index be242a82e86d..d71370598447 100644 --- a/rust/kernel/module.rs +++ b/rust/kernel/module.rs @@ -42,6 +42,15 @@ impl<T: Module> InPlaceModule for T { 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. |
