]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
rust: module: add trait `ModuleMetadata`
authorDanilo Krummrich <dakr@kernel.org>
Thu, 19 Dec 2024 17:04:03 +0000 (18:04 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 20 Dec 2024 16:19:25 +0000 (17:19 +0100)
In order to access static metadata of a Rust kernel module, add the
`ModuleMetadata` trait.

In particular, this trait provides the name of a Rust kernel module as
specified by the `module!` macro.

Signed-off-by: Danilo Krummrich <dakr@kernel.org>
Tested-by: Dirk Behme <dirk.behme@de.bosch.com>
Tested-by: Fabien Parent <fabien.parent@linaro.org>
Link: https://lore.kernel.org/r/20241219170425.12036-2-dakr@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
rust/kernel/lib.rs
rust/macros/module.rs

index e1065a7551a39e68d6379031d80d4be336e652a3..61b82b78b915bddc55fcad2a47f7091aa7c002ad 100644 (file)
@@ -116,6 +116,12 @@ impl<T: Module> InPlaceModule for T {
     }
 }
 
+/// Metadata attached to a [`Module`] or [`InPlaceModule`].
+pub trait ModuleMetadata {
+    /// The name of the module as specified in the `module!` macro.
+    const NAME: &'static crate::str::CStr;
+}
+
 /// Equivalent to `THIS_MODULE` in the C API.
 ///
 /// C header: [`include/linux/init.h`](srctree/include/linux/init.h)
index 2587f41b0d3929af7ceac5f42b4711f70b4f8749..cdf94f4982dfc109ac5c7e9001897555de19d67e 100644 (file)
@@ -228,6 +228,10 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
                 kernel::ThisModule::from_ptr(core::ptr::null_mut())
             }};
 
+            impl kernel::ModuleMetadata for {type_} {{
+                const NAME: &'static kernel::str::CStr = kernel::c_str!(\"{name}\");
+            }}
+
             // Double nested modules, since then nobody can access the public items inside.
             mod __module_init {{
                 mod __module_init {{