Skip to content

Commit a2595ed

Browse files
Danilo Krummrichojeda
authored andcommitted
rust: kernel: add LocalModule fallback for #[vtable] impls
Like commit 98f256e ("rust: doctest: add LocalModule fallback for #[vtable] ThisModule"), add another `LocalModule` struct with a null-pointer `ModuleMetadata` `impl` for the `kernel` crate, so that `crate::LocalModule` (auto-inserted by `#[vtable]`) resolves correctly when there is no `module!` macro. This will be needed by DRM to use `#[vtable]` `impl` blocks in KUnit tests within the `kernel` crate [1]. Signed-off-by: Danilo Krummrich <dakr@kernel.org> Link: https://lore.kernel.org/rust-for-linux/DKNAS52KYWLD.M15VEC6U0F6R@kernel.org/ [1] [ Created commit out of the diff in the link above. Fixed the `clippy::undocumented_unsafe_blocks` lint by wrapping with a block like in the other commit too. Added `#[allow(dead_code)]` until we actually (and unconditionally, i.e. KUnit tests may be not enabled) use it. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 643a7c3 commit a2595ed

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

rust/kernel/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,20 @@ pub use uapi;
152152
/// Prefix to appear before log messages printed from within the `kernel` crate.
153153
const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
154154

155+
/// Dummy module type for `#[vtable]` `impl` blocks within the `kernel` crate (e.g. KUnit tests).
156+
// The `allow` is needed since it may be unused (e.g. KUnit tests may be disabled).
157+
#[allow(dead_code)]
158+
struct LocalModule;
159+
160+
impl ModuleMetadata for LocalModule {
161+
const NAME: &'static str::CStr = c"rust_kernel";
162+
163+
const THIS_MODULE: ThisModule = {
164+
// SAFETY: `try_module_get`/`module_put` handle null module pointers gracefully.
165+
unsafe { ThisModule::from_ptr(core::ptr::null_mut()) }
166+
};
167+
}
168+
155169
#[cfg(not(testlib))]
156170
#[panic_handler]
157171
fn panic(info: &core::panic::PanicInfo<'_>) -> ! {

0 commit comments

Comments
 (0)