From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4D12F3B9952 for ; Mon, 3 Aug 2026 09:00:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.43 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785747622; cv=none; b=Dccso6r+7EM67feZsFOkQL/q8tMHwl5LbKuRDGikJJtmEWC00GMfXQ+0HC0moAQG3bewtHg1R4tjnhhdkwPOEiLmYereBnMEuX/dtYJGC86rG8INyXNoDk8N3Pbd5lBlJ06+dvOS8ZSC7twUNijBU5+g+8tWXF/Gbo8nOcSX3uo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785747622; c=relaxed/simple; bh=0zlJd9Yr6UpKiQThq/l7IWb1Lcot2jQVu687ixt9vMs=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=FZAFzUXEbFQsJiaBuY09zx4QqGOR2LbBsR8z3npdaYkh/YWa9k5PsjMv2pKZS6NCQw50Lih5Up+fzzv6PvvEsUuTvyPr485irmAowxprTbhYQMGg/XkV0NeMuxgtv13N+cr8EbUFPN7R/JTaqDYjn5NSowDmAGmtK2oNGuM8/PQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b=XW+D8Fvw; arc=none smtp.client-ip=209.85.128.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b="XW+D8Fvw" Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-49555a0e68bso8423765e9.2 for ; Mon, 03 Aug 2026 02:00:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1785747617; x=1786352417; darn=vger.kernel.org; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to :content-type; bh=/MEAqomLnokyVR9AqA64rl9ZYCVmUcv5C80fBRfsQys=; b=XW+D8FvwjzgcjUDkSi+A0ELSC7rPA4P4Ejct47bLSvBqMC/dKAeDev+KeubJawbkIO YUIRwVBy8ZwXNlMQLOfjugsDUMJFyfFjXr6xDxx4qD8IwdBU8RLho57NNV0Ein9y7KFg CqwpaZuViPwTNglZCGKO5O30o03nWWAfDNi3WFF9coTY/hj0BGCC4ia3CFTnDrUnMbv5 2lRJpiysPJoQRHepKyIHFwlgrZlqxzjuC/Z/YrxR3oJRNMad27PGC1x2NOl7rDe4zIr/ 3cGo0QNob2p2wmths/Y9UXh/yvOMBumGDQ3my3J/P5hqq6HS0NO/LjVccTtFmO9JPOSx X/BQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785747617; x=1786352417; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=/MEAqomLnokyVR9AqA64rl9ZYCVmUcv5C80fBRfsQys=; b=WgDSFywJRGwEarU7zfo1YIkeQmvM8Q5ObflrDhOPq89ozq3pA2nKbO0eZmlyEZU0Gi vCZ+B8+k5tHMG1wucfLE45p2Phavr9lQ9Os7ABIW2JBo5DStE5L1TMplWW8HjnYYsmmh Gmww6n304HAVUS0+X+nlQSutf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PA== X-Received: by 2002:a05:600c:871a:b0:498:952:e276 with SMTP id 5b1f17b1804b1-4980c66c991mr237479075e9.8.1785747617287; Mon, 03 Aug 2026 02:00:17 -0700 (PDT) Received: from ?IPV6:2a00:1028:838d:271e:8e3b:4aff:fe4c:a100? (dynamic-2a00-1028-838d-271e-8e3b-4aff-fe4c-a100.ipv6.o2.cz. [2a00:1028:838d:271e:8e3b:4aff:fe4c:a100]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-49807ba13e9sm314596535e9.13.2026.08.03.02.00.15 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 03 Aug 2026 02:00:16 -0700 (PDT) Message-ID: <6e9e8189-e27b-4279-9fed-b7da5da62419@suse.com> Date: Mon, 3 Aug 2026 11:00:14 +0200 Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v9 02/10] rust: module: add `THIS_MODULE` const to `ModuleMetadata` trait To: Alvin Sun Cc: 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 , 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 , 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 , 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 References: <20260723-fix-fops-owner-v9-0-c1c3af7f7bcb@linux.dev> <20260723-fix-fops-owner-v9-2-c1c3af7f7bcb@linux.dev> Content-Language: en-US From: Petr Pavlu In-Reply-To: <20260723-fix-fops-owner-v9-2-c1c3af7f7bcb@linux.dev> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 7/23/26 4:10 AM, Alvin Sun wrote: > 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. > > Assisted-by: opencode:glm-5.2 > Reviewed-by: Andreas Hindborg > Reviewed-by: Gary Guo > Acked-by: Danilo Krummrich > Reviewed-by: Alice Ryhl > Signed-off-by: Alvin Sun Acked-by: Petr Pavlu > --- > rust/kernel/module.rs | 9 +++++++++ > rust/macros/module.rs | 18 +++++++++++++++++- > 2 files changed, 26 insertions(+), 1 deletion(-) > > diff --git a/rust/kernel/module.rs b/rust/kernel/module.rs > index be242a82e86d2..d713705984477 100644 > --- a/rust/kernel/module.rs > +++ b/rust/kernel/module.rs > @@ -42,6 +42,15 @@ fn init(module: &'static ThisModule) -> impl pin_init::PinInit 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; > +} > + > +/// Returns a reference to the `THIS_MODULE` of the given module type. > +#[inline] > +pub const fn this_module() -> &'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 >