From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E30DA3C1F5E; Tue, 23 Jun 2026 06:30:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782196210; cv=none; b=oJmYm0O9Amys3x6CUwKbasU2TZxHI4Fs4iSYO/KZS+RjSRuqCmoZcB1gFm64okOpBXfDT1H8Q+FTTKy+xGcQugk1tP3VcmwaxDuTOBk+u4YvyqqNGSnVsupVcCfS9sOM5OcyXm8xJstF6Z7kFSwLsu4c8VBCz5Qd510JW/LB4Qk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782196210; c=relaxed/simple; bh=4OYDXOyyu09vAJ0Ux9fR4L+xgeb09K0RJRxAiy1sHn4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=o2+3stYDFBqMiBP/ANFkFcRFAt6zcuqO0iIO0xX/WvsCbDg0UGZiWfqwLNhp0G0Dbz6/k3oT9w+y/eAl0H+CI8FU6FaacrGeDEIuOLJ/6SNdYWxQR1rUGKTh5xLpyp9FUOBnclVsETr7PP3zF589oqhhLH27uR+nv762evyjiNI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=w+rtkiDz; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="w+rtkiDz" Received: by smtp.kernel.org (Postfix) with ESMTPS id 7FB36C2BCF5; Tue, 23 Jun 2026 06:30:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux.dev; s=korg; t=1782196209; bh=4OYDXOyyu09vAJ0Ux9fR4L+xgeb09K0RJRxAiy1sHn4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=w+rtkiDzAw5tWieURtwK87xzu7UPt+XYOwHkbwPhdt4o0Z38NDMxybjywuaJGGTWG nKWTnGD927p9yZikWnppmaOx5JZyS0dAH7Ecg0TDyZ/ldd4+tOMBzsnBqMu6YWz7gz Mt0fpwp3DdrQrD5tbfmsE+SlHlsn6sDokXO6xjgM= Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 65839CD4F26; Tue, 23 Jun 2026 06:30:09 +0000 (UTC) From: Alvin Sun Date: Tue, 23 Jun 2026 14:29:45 +0800 Subject: [PATCH v4 2/9] rust: module: add `THIS_MODULE` const to `ModuleMetadata` trait Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260623-fix-fops-owner-v4-2-0daf5f077d5c@linux.dev> References: <20260623-fix-fops-owner-v4-0-0daf5f077d5c@linux.dev> In-Reply-To: <20260623-fix-fops-owner-v4-0-0daf5f077d5c@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 , Brendan Higgins , David Gow , Rae Moar , Breno Leitao , Jens Axboe , Dave Ertman , Ira Weiny , Leon Romanovsky , Igor Korotin , FUJITA Tomonori , Bjorn Helgaas , =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= , =?utf-8?q?Arve_Hj=C3=B8nnev=C3=A5g?= , Todd Kjos , Christian Brauner , Carlos Llamas Cc: rust-for-linux@vger.kernel.org, linux-modules@vger.kernel.org, driver-core@lists.linux.dev, dri-devel@lists.freedesktop.org, nova-gpu@lists.linux.dev, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-pci@vger.kernel.org, Alvin Sun X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1782196206; l=3407; i=alvin.sun@linux.dev; s=20260317; h=from:subject:message-id; bh=4OYDXOyyu09vAJ0Ux9fR4L+xgeb09K0RJRxAiy1sHn4=; b=zdgUdpDPp3eOByd+72nMOSczbS5S5f+bBmAvMFJRai9zi+TqqOOHP4xYDT4eRHrvi6d4RhxfZ 4g3qwrH3TJDAgD55ieoaL1ukUGgXlljf/C5D+knxmpdrO5Nm+qiyP29 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 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. Signed-off-by: Alvin Sun --- rust/kernel/module.rs | 8 ++++++++ rust/macros/module.rs | 18 +++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/rust/kernel/module.rs b/rust/kernel/module.rs index be242a82e86d2..5aca42f7a33fc 100644 --- a/rust/kernel/module.rs +++ b/rust/kernel/module.rs @@ -42,6 +42,14 @@ fn init(module: &'static ThisModule) -> impl pin_init::PinInit() -> &'static ThisModule { + &M::THIS_MODULE } /// Equivalent to `THIS_MODULE` in the C API. diff --git a/rust/macros/module.rs b/rust/macros/module.rs index 06c18e2075083..aa9a618d5d19e 100644 --- a/rust/macros/module.rs +++ b/rust/macros/module.rs @@ -519,6 +519,22 @@ pub(crate) fn module(info: ModuleInfo) -> Result { 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 +632,7 @@ pub extern "C" fn #ident_exit() { /// This function must only be called once. unsafe fn __init() -> ::kernel::ffi::c_int { let initer = ::init( - &super::super::THIS_MODULE + ::kernel::module::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