[PATCH v2 1/7] rust: module: add `THIS_MODULE` const to `ModuleMetadata` trait

0 views
Skip to first unread message

Alvin Sun

unread,
May 21, 2026, 3:52:59 AM (4 days ago) May 21
to Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich, Luis Chamberlain, Petr Pavlu, Daniel Gomez, Sami Tolvanen, Aaron Tomlin, Greg Kroah-Hartman, Rafael J. Wysocki, David Airlie, Simona Vetter, Daniel Almeida, Arnd Bergmann, Brendan Higgins, David Gow, Rae Moar, Breno Leitao, Jens Axboe, rust-fo...@vger.kernel.org, linux-...@vger.kernel.org, drive...@lists.linux.dev, dri-...@lists.freedesktop.org, nova...@lists.linux.dev, linux-k...@vger.kernel.org, kuni...@googlegroups.com, linux...@vger.kernel.org, Alvin Sun
Add a `THIS_MODULE` const to the `ModuleMetadata` trait so that
modules can provide their `ThisModule` pointer usable in const
contexts such as static file_operations.

Move the `THIS_MODULE` static from the `module!` macro into the
`ModuleMetadata` impl, and update `__init` to use
`LocalModule::THIS_MODULE` instead.

Signed-off-by: Alvin Sun <alvi...@linux.dev>
---
rust/kernel/lib.rs | 3 +++
rust/macros/module.rs | 34 +++++++++++++++++-----------------
2 files changed, 20 insertions(+), 17 deletions(-)

diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index b72b2fbe046d6..f0cf0705d9697 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -184,6 +184,9 @@ fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, error::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;
}

/// Equivalent to `THIS_MODULE` in the C API.
diff --git a/rust/macros/module.rs b/rust/macros/module.rs
index 06c18e2075083..b6d7b3299fbf9 100644
--- a/rust/macros/module.rs
+++ b/rust/macros/module.rs
@@ -497,28 +497,28 @@ pub(crate) fn module(info: ModuleInfo) -> Result<TokenStream> {
/// Used by the printing macros, e.g. [`info!`].
const __LOG_PREFIX: &[u8] = #name_cstr.to_bytes_with_nul();

- // SAFETY: `__this_module` is constructed by the kernel at load time and will not be
- // freed until the module is unloaded.
- #[cfg(MODULE)]
- static THIS_MODULE: ::kernel::ThisModule = unsafe {
- extern "C" {
- static __this_module: ::kernel::types::Opaque<::kernel::bindings::module>;
- };
-
- ::kernel::ThisModule::from_ptr(__this_module.get())
- };
-
- #[cfg(not(MODULE))]
- static THIS_MODULE: ::kernel::ThisModule = unsafe {
- ::kernel::ThisModule::from_ptr(::core::ptr::null_mut())
- };
-
/// The `LocalModule` type is the type of the module created by `module!`,
/// `module_pci_driver!`, `module_platform_driver!`, etc.
type LocalModule = #type_;

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 +616,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
+ &<super::super::LocalModule as ::kernel::ModuleMetadata>::THIS_MODULE
);
// 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

--
2.43.0


Reply all
Reply to author
Forward
0 new messages