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 F34073B0ACD; Wed, 24 Jun 2026 12:57:34 +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=1782305855; cv=none; b=D6SBEMI0JeTKHNKDt105nTx53SHgGDL5NPRWOB0CWp2frk3DPXqf6KdSZcamNJdE12Y9L9YViRHgLbahZcnW3hlOnwvBqoEL+0FVlmW+rs2QlfxGuJ2TnVp3UcmgKMdFBlrWD2MqAbU2fxqeXDUXzoIm8kccYzqdQE55Vb6QyN4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782305855; c=relaxed/simple; bh=C1FMa0qVq/S97m9Viz8hjYFdAfdHoRPHBepu6SriAjU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=UtJnMzq96iwUS6TtcDXLFa2M8cSLd2MBh59+HQlR73VCohVsZlr0DDi1t3liwN7+EGUAuCVVSK8eBN3Gg6tP1GRhVwTdOZvxDMHN7bFyRekc/SboJlbh4h/FBVva2j9romdV5f0kcNv7l1Px+cQHFNKGMNBUqmorpVwbJbbGr50= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=jkJfBuFe; 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="jkJfBuFe" Received: by smtp.kernel.org (Postfix) with ESMTPS id C8FC0C2BCFD; Wed, 24 Jun 2026 12:57:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux.dev; s=korg; t=1782305854; bh=C1FMa0qVq/S97m9Viz8hjYFdAfdHoRPHBepu6SriAjU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=jkJfBuFe/8OMp1vAA005OsCZPleWruMZP8xca8HdCNBYV1Lsp7Z/e0GbMZxXSOCop MrsjaUK2NTYn8H4h1wCiSy0dAATnVOkbyqA7P60JIse1iozDPX02U2JhyA3Oq1Jc7x 8xKok985UpOVFsCm87IzCDM8lD/O3X0ByUXfsagY= 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 B99F8CDB471; Wed, 24 Jun 2026 12:57:34 +0000 (UTC) From: Alvin Sun Date: Wed, 24 Jun 2026 20:57:27 +0800 Subject: [PATCH v5 4/9] rust: macros: auto-insert OwnerModule in #[vtable] 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: <20260624-fix-fops-owner-v5-4-aa1cba242f05@linux.dev> References: <20260624-fix-fops-owner-v5-0-aa1cba242f05@linux.dev> In-Reply-To: <20260624-fix-fops-owner-v5-0-aa1cba242f05@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 , 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=1782305851; l=4532; i=alvin.sun@linux.dev; s=20260317; h=from:subject:message-id; bh=C1FMa0qVq/S97m9Viz8hjYFdAfdHoRPHBepu6SriAjU=; b=9JbBVO62bRF1oZq4pLp3gXZXlduyLuXZnVVNLfIsbS3PJ0IOfJfxmhONrS2sg7HdI4fNh2yHJ lZIP9h7jPtFCEFEw2vSnRUxDocgc28PPsE025Efx9rci0QptAo/fREc 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 Auto-add `type OwnerModule: ::kernel::ModuleMetadata;` as a required associated type on the trait side if not already defined, and auto-insert `type OwnerModule = crate::LocalModule;` on the impl side if not explicitly provided, eliminating the need to manually declare and implement `OwnerModule` in every vtable trait and impl. Assisted-by: opencode:glm-5.2 Reviewed-by: Andreas Hindborg Suggested-by: Gary Guo Link: https://lore.kernel.org/all/DIMMWHUOLPSH.13JFRHDKDQJGO@garyguo.net Reviewed-by: Gary Guo Signed-off-by: Alvin Sun --- rust/macros/lib.rs | 6 ++++++ rust/macros/vtable.rs | 41 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs index 2cfd59e0f9e7c..bc7ded353c5ca 100644 --- a/rust/macros/lib.rs +++ b/rust/macros/lib.rs @@ -176,6 +176,12 @@ pub fn module(input: TokenStream) -> TokenStream { /// /// This macro should not be used when all functions are required. /// +/// Additionally, this macro automatically handles the `OwnerModule` +/// associated type: on the trait side, `type OwnerModule: ModuleMetadata;` +/// is added as a required associated type if not already defined; on the +/// impl side, `type OwnerModule = LocalModule;` is automatically inserted +/// if not explicitly defined. +/// /// # Examples /// /// ``` diff --git a/rust/macros/vtable.rs b/rust/macros/vtable.rs index c6510b0c4ea1d..be9a5ed8abe5e 100644 --- a/rust/macros/vtable.rs +++ b/rust/macros/vtable.rs @@ -30,6 +30,22 @@ fn handle_trait(mut item: ItemTrait) -> Result { const USE_VTABLE_ATTR: (); }); + // Add `type OwnerModule: ModuleMetadata` as a required associated type if + // the trait does not already define it. + if !item + .items + .iter() + .any(|i| matches!(i, TraitItem::Type(t) if t.ident == "OwnerModule")) + { + gen_items.push(parse_quote! { + /// The module implementing this vtable trait. + /// + /// Automatically set to `crate::LocalModule` by the `#[vtable]` + /// impl macro. + type OwnerModule: ::kernel::ModuleMetadata; + }); + } + for item in &item.items { if let TraitItem::Fn(fn_item) = item { let name = &fn_item.sig.ident; @@ -57,12 +73,18 @@ fn handle_trait(mut item: ItemTrait) -> Result { fn handle_impl(mut item: ItemImpl) -> Result { let mut gen_items = Vec::new(); - let mut defined_consts = HashSet::new(); + let mut defined_items = HashSet::new(); - // Iterate over all user-defined constants to gather any possible explicit overrides. + // Iterate over all user-defined items to gather any possible explicit overrides. for item in &item.items { - if let ImplItem::Const(const_item) = item { - defined_consts.insert(const_item.ident.clone()); + match item { + ImplItem::Const(const_item) => { + defined_items.insert(const_item.ident.clone()); + } + ImplItem::Type(type_item) => { + defined_items.insert(type_item.ident.clone()); + } + _ => {} } } @@ -70,6 +92,15 @@ fn handle_impl(mut item: ItemImpl) -> Result { const USE_VTABLE_ATTR: () = (); }); + // Auto-insert `type OwnerModule = crate::LocalModule` if not explicitly defined. + // `crate::LocalModule` resolves to the real module type (via `module!`) or a + // dummy fallback in non-module contexts (e.g., doctests). + if !defined_items.contains(&parse_quote!(OwnerModule)) { + gen_items.push(parse_quote! { + type OwnerModule = crate::LocalModule; + }); + } + for item in &item.items { if let ImplItem::Fn(fn_item) = item { let name = &fn_item.sig.ident; @@ -78,7 +109,7 @@ fn handle_impl(mut item: ItemImpl) -> Result { name.span(), ); // Skip if it's declared already -- this allows user override. - if defined_consts.contains(&gen_const_name) { + if defined_items.contains(&gen_const_name) { continue; } let cfg_attrs = crate::helpers::gather_cfg_attrs(&fn_item.attrs); -- 2.43.0