From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (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 4DDC23BA222 for ; Mon, 3 Aug 2026 09:00:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785747622; cv=none; b=WevpBUqG8RW3n179POomNM4cZfQpJG4KJgqw96AjUaO6/ujybelJB5/8qquKHTfUdvQCYzL8D4c+CMoNCZ6vQDSXD9Kiwye8a6CN0ksL2ppWgBFUhw1QfC1gWIZpl9wLMy6seWnEQo5k3qD2fJs726BFG84z6eK3SRWo12PKQro= 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=UUjs8neQ; arc=none smtp.client-ip=209.85.128.41 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="UUjs8neQ" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-4954aff6088so15453415e9.3 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=lists.linux.dev; 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=UUjs8neQCo0OsAVL+rcaWmy0Rs5HCjbVg//6Mwm4yu+n9cEIko432y/f/6FolotANC A2RJW2J5g6JSUh6KGdaJO69kIUq6ro0+Qa6lpKabMghSwT1bQGs++NvC1wBLnm73stnb zCXBZ8TSt3mUtYROrF3VEtcs60YnLgXnQjHtoXxZ76k3klT2eqtcd1TgoP76iUxI0CyJ HE/m2sxI1MrEoNhu/e5XPpJtJWpMoZufWuKTPMZA+SOI2xQ7FuHcQMiOzn5m8CKdfdLL W295beYJD3Q5j+aJHXnMm9MxXDFJzpwsoRMNcrnCK15bxKaI/DUJm/0XQho62ff4FIC9 zPHA== 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=duXpkC9PIk3FRXzy0FiR8bGVo4t0YB6RLc0fikEDZGUOx2SMQIcsDHrXs0ETuKKaDH B8DAzc5mCnsiVVl34Anf6rhQvZ8JoEQRPSw6XF3xAqd5GiTb4Ex0H7QURccTjLVSwoZL Qp0D1QSJaLBYWcog6A815sRCcvErjfs7np5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wA== 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: nova-gpu@lists.linux.dev 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 >