From: Alvin Sun Date: Tue, 19 May 2026 14:26:13 +0800 Subject: [PATCH 1/8] rust: module: add `MODULE_PTR` const to `ModuleMetadata` trait MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260519-fix-fops-owner-v1-1-2ded9830da14@linux.dev> References: <20260519-fix-fops-owner-v1-0-2ded9830da14@linux.dev> In-Reply-To: <20260519-fix-fops-owner-v1-0-2ded9830da14@linux.dev> To: Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_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 Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, driver-core@lists.linux.dev, dri-devel@lists.freedesktop.org, nova-gpu@lists.linux.dev, Alvin Sun X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1779171975; l=2011; i=alvin.sun@linux.dev; s=20260317; h=from:subject:message-id; bh=ZxczDP1VleS2E+h97aaS99GWrdXeqA4F9FKMUPVF+a8=; b=nZswdxITOGGBNtXxziHC46ym73dmkEpgBRSKXECBrIkKx63Q6fXF2BcQ9Wu99HlqtRv32iuS7 nenSNwiKIxRDsSSvybK6NrQ/NmLMOgegWv7aRnPdYLyeBWx7+qzexiI X-Developer-Key: i=alvin.sun@linux.dev; a=ed25519; pk=CHcwQp8GSoj25V/L1ZWNSQjWp9eSIb0s9LKr0Nm3WuE= X-Endpoint-Received: by B4 Relay for alvin.sun@linux.dev/20260317 with auth_id=684 List-Id: B4 Relay Submissions Add a `MODULE_PTR` const to the `ModuleMetadata` trait so that modules can provide a constant pointer to their `struct module` usable in const contexts such as static file_operations. Signed-off-by: Alvin Sun --- rust/kernel/lib.rs | 3 +++ rust/macros/module.rs | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index b72b2fbe046d6..c7e809636e1a9 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -184,6 +184,9 @@ fn init(module: &'static ThisModule) -> impl pin_init::PinInit Result { impl ::kernel::ModuleMetadata for #type_ { const NAME: &'static ::kernel::str::CStr = #name_cstr; + + #[cfg(MODULE)] + const MODULE_PTR: *mut ::kernel::bindings::module = { + 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 { __this_module.get() } + }; + + #[cfg(not(MODULE))] + const MODULE_PTR: *mut ::kernel::bindings::module = ::core::ptr::null_mut(); } // Double nested modules, since then nobody can access the public items inside. -- 2.43.0