From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (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 4DC273B9DBB for ; Mon, 3 Aug 2026 09:00:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.54 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785747622; cv=none; b=E+A/QG9dLX/vVSCiuA+wKNveaYW2+o4ylsMbrxuj2JkDhuSg4o+dwrznFxuvcTdsPCYtxZohfR2uSEE5p77YzuFUkisij26hU52JI6ZV0cf87grNxMTJ140YzpZRn5Qk0zOX/wZMmkxglMI+FUr10i5kTteg7GT3zo0Wkewm2wI= 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.54 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-f54.google.com with SMTP id 5b1f17b1804b1-49556f97a9dso11376185e9.1 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=nHVv8wwIxQLvzRM12Q9U0PZEE67EhNENsEWhSX/m4YpUGC6w1sacRS+WrZkfll1mRt BwJtao2rVcyP22WTOJ0GiA8oj8ClzmCZ2J6JxCzgrJ0FjcbFiWs2mowU3BWadXiTzAup V/Ui86ldHCrThermMW1PtPiDTxpVU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2fGAuHIA== 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: linux-pci@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 >