From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=52.101.95.95 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796313482; b=iudPbujxlEq/RgkfoUw83giIZXivBCe9NspAC9/xMpZSZLcLHl4MCfzkxAjDpomEsgeN cJHZSI2bt1bHe4dO2GO95+3EH/ePzcKf7q8n0oPx+Z5YovDrCuO1R4FWfeNbB+CQ9mfEw 3W6nDiaxPYyfdj73FWH6juks/Q28QpN8v5xAAyIBAxJ3KGCPWFLfAILnp9qMb4NOpH4/O hxQgqNnJ6ge1EyNZdDeCmIBBbCnrmwPeD0+WQyFc4EXhHNgtvzkgWkxwH1ajYB2og0ojc AinPaXcpUUJ4y9BgWEBH/jx7Xvkw13PY9JnwjIO3ip/22SLYaJZZNA+Ue6LccfPad8Q== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796313482; h=DMARC-Filter:DKIM-Signature:Received:Received:From:Date:Subject: Content-Type:Content-Transfer-Encoding:Message-Id:References: In-Reply-To:To:Cc:X-Mailer:X-Developer-Signature:X-Developer-Key: X-ClientProxiedBy:MIME-Version:X-MS-PublicTrafficType: X-MS-TrafficTypeDiagnostic:X-MS-Office365-Filtering-Correlation-Id: X-MS-Exchange-SenderADCheck:X-MS-Exchange-AntiSpam-Relay: X-Microsoft-Antispam:X-Microsoft-Antispam-Message-Info: X-Forefront-Antispam-Report: X-MS-Exchange-AntiSpam-MessageData-ChunkCount: X-MS-Exchange-AntiSpam-MessageData-0:X-OriginatorOrg: X-MS-Exchange-CrossTenant-Network-Message-Id: X-MS-Exchange-CrossTenant-AuthSource:X-MS-Exchange-CrossTenant-AuthAs: X-MS-Exchange-CrossTenant-OriginalArrivalTime: X-MS-Exchange-CrossTenant-FromEntityHeader: X-MS-Exchange-CrossTenant-Id:X-MS-Exchange-CrossTenant-MailboxType: X-MS-Exchange-CrossTenant-UserPrincipalName: X-MS-Exchange-Transport-CrossTenantHeadersStamped; bh=oSbfSxNReDubZh/n3XnjISdD1q6fPTcfVbdV2IZNq4s=; b=O5Y9INJstxjE9t437ZHKBt5vPHXJxYtYSeS8pcTivgcV4qxJlwCk5rRVUZ8lFZAano9q AKKSqP1WMhRKqSgOGkhVnJf2MHKIq1rYZCV6iHUXDxshmxaSkm9jJnnxKR1/RBifGxd+j AbyHIOQ6hmnXbSPdMMdY1REJcq0j7dh5dk39q4GuKCQHoqs2PoHrHRm1T5z4/tWywdb9d Zy1qxntfg7iAKE8dLJ6Q1fvDjDzTOzK6EwWTv1o9JVYzLBzkqAesQqrj+xVizzJZlGy4o aWpQj265woFXPTvKB0DWTfd3eX47UyNaaZfCuBQz+37Ye31fwlF44RLgvSPtqY1wfvg== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=garyguo.net; spf=pass smtp.mailfrom=garyguo.net; dkim=pass header.d=garyguo.net header.i=@garyguo.net header.a=rsa-sha256 header.s=selector1 header.b=soH7Z6aO; arc=pass header.oldest-pass=0 smtp.remote-ip=52.101.95.95 Received: from LO2P265CU024.outbound.protection.outlook.com (mail-uksouthazon11021095.outbound.protection.outlook.com [52.101.95.95]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (secp384r1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F3CDE606A681; Thu, 3 Dec 2026 15:58:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org F3CDE606A681 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=garyguo.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=garyguo.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=garyguo.net header.i=@garyguo.net header.a=rsa-sha256 header.s=selector1 header.b=soH7Z6aO ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ot8Uxx+co2/Zvsvu3/PPXizANwoLgIa7m6fh28pXOJkRLjAYSG/ctq28aStyFfdNnSSanSUOoHhh1UJ6qnW12FSuKT09BPovtPfiptlkJa7EtvIwV2FOq+/XEl2MdKHGbA06lO+VHQmS6AUq2T7QS7wHnOJNItlRM2jPrII3b8OmM5U7GLMn7dNNz01qqvlVvwpDyYnPC3yhLx61jjrYsye7H2G1QpSbuEPxUZ6TFZvvHS1zfxcrsdwXEnq1F0C6AAzVU7vuhR074LuD+jpo+QIPKDU3DBvKrBZ1GnlXse+yzYavmvlSVZ7F7E7ZW/u7Rm6nM+7YjQU0hvqLkygf1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=oSbfSxNReDubZh/n3XnjISdD1q6fPTcfVbdV2IZNq4s=; b=Wzq6HCw7371EVZjJe8bz9t5m6Cb2tAKx79uNZyfMimh2fEs+OdymqO859aJwYU2c0MoHinfG2jyzrJ0EU2FojRmJ3i7b2g02977YopI+ORYHKXbRlV9UoKKRM8skbgbNV6nOBe6kkxAXqcwmiOEdWLCAs4qXT3x/0CeIHvauctapdd3yuySg03QaptlOBgyoc6lS3K7kXeMCA1/G5CIP8/R9HjxQrxHGFA5iqqMAQ6Ru/HaFahgMLLxmnbS1d/QUJ5y1aimJnF57Zjf9Yu+qp6Wdw7sZ4iFr9XY/nyLodl5RF3lA4PahMB+sT0k5tbItizGLzbpjqUOTg1HxtGXFug== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=garyguo.net; dmarc=pass action=none header.from=garyguo.net; dkim=pass header.d=garyguo.net; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=garyguo.net; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=oSbfSxNReDubZh/n3XnjISdD1q6fPTcfVbdV2IZNq4s=; b=soH7Z6aOhP9hptHkjbRlHKbbgsl7jCnCFHg2x2xBH8qWJjBUDhkc+cmXFGW175snRH6eMFKfJvneG79JXAkZibWcN6F5GDEYkO8zimJBvgyIJXUGeLnZTsnXOVD6x3FDBGTGtbM/kAYfpVJE52I/9kDZGHWBrh9pwfNy9sOHxRY= Authentication-Results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=garyguo.net; Received: from CWLP265MB6051.GBRP265.PROD.OUTLOOK.COM (2603:10a6:400:1cb::9) by LO0P265MB7377.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:2d6::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.11; Thu, 3 Dec 2026 15:57:56 +0000 Received: from CWLP265MB6051.GBRP265.PROD.OUTLOOK.COM ([fe80::55f6:f98b:7fad:c6e3]) by CWLP265MB6051.GBRP265.PROD.OUTLOOK.COM ([fe80::55f6:f98b:7fad:c6e3%2]) with mapi id 15.21.0688.011; Thu, 3 Dec 2026 15:57:56 +0000 From: Gary Guo Date: Thu, 03 Dec 2026 15:57:43 +0000 Subject: [PATCH v2 1/4] rust: macros: add `#[macro_export_scoped]` Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261203-macro_export_scoped-v2-1-55d1b09238e2@garyguo.net> References: <20261203-macro_export_scoped-v2-0-55d1b09238e2@garyguo.net> In-Reply-To: <20261203-macro_export_scoped-v2-0-55d1b09238e2@garyguo.net> To: Miguel Ojeda , Boqun Feng , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Breno Leitao , Luis Chamberlain , Russ Weight , Lyude Paul Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, driver-core@lists.linux.dev, Gary Guo X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796313475; l=6340; i=gary@garyguo.net; s=20221204; h=from:subject:message-id; bh=ukvBFfCy28tEltoOk7/QAqFbhsVDI1HFiTH50L+LhQ4=; b=ezlut2IgMYC/vh5U1ef5DoPC+IW4ke8QJAcoHfj6p2nZkynJ0omAsp1frgFtOLq0tDK1Q8spi EmsgXxUndpgDTEL9o6Z2hrDMOi63YZ0DH2Rn/dJuRwJeKmHH9+EYo6Q X-Developer-Key: i=gary@garyguo.net; a=ed25519; pk=vB3uIX95SM4eVrIqo1DWNWKDKD2xzB+yLLLr0yOPYMo= X-ClientProxiedBy: LO4P265CA0136.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:2c4::12) To CWLP265MB6051.GBRP265.PROD.OUTLOOK.COM (2603:10a6:400:1cb::9) X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CWLP265MB6051:EE_|LO0P265MB7377:EE_ X-MS-Office365-Filtering-Correlation-Id: 90f96c71-11b3-4bea-36ab-08df51563247 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|376014|7416014|23010399003|1800799024|10070799003|5023799004|261009223027099003|10067099003|921020|22082099003|18002099003|56012099006|261022113327099003|261021123027099003|261021130227099003|261102123227099003|261021121427099003|261028144727099003|261026121527099003|261022075727099003|261102130727099003|261022115727099003|261026140227099003|261022105027099003; X-Microsoft-Antispam-Message-Info: S5XiTot4msg2fTrp/aaDxoGfDpWP8+mbUfVLVEMkiAslybuIAPGu68RsTogXv8BQkGrRSvKIKJkqQrik9UumtCprFAuQJImuR4Oi4PZM+EHTrV4sDhGIo37GJaPdKJQexButIbacBUMLwhqNAF5CN9WHhrfJBTqkpSKdK+F5xSi6SyCoW6Xs7UVlPBU3v/yd0P6GAdjEkFcz37dd0l4aAo+FMV2YQfy4Z9w590NFJes9PqxV7fGDs4Qr24nA4cC92trjdMpbO3wLUU1vTttCZID9eJDSSSWcPXzUzORbQYFILU667IGeJevqd7MDf9zOMsB2KCI5/XwB/YS+wdD4sU8W88FPcELbx2HBFQDoZ884dKtknIv9MsxIZDu6/nl0ZMX7P8jKJxsn7rnhWflc3x/pXDkX+47SklSr66CeNUczBMnO6P2XA7lscn59MdntQHcgmT4IAOp0gKVGYYR0AKCbopMGdKLtFi+1i2yMR+8g6wABFBG8dp2QldmF7BOcyeP0TU9hRvkedNvulKTI5sGo2QmQZl3Z9o7d1kgyrnNPlu+oX8V+bPINJJeCk2ZNV2Xt0OYo64vPsyNSHp2+szKtbSvj+Algdtu4jWMjYZqwQjNNutLz9WrGDSuctDBCzFWl6DjiWalFBPIpw/70ShVAWAOo+TCQWr6dqZ+WukI+D+y7RsRu2xPj/zsO3Vqe2gQmGr5FDI3T0g0gaiQnOw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CWLP265MB6051.GBRP265.PROD.OUTLOOK.COM;PTR:;CAT:NONE;SFS:(13230040)(366016)(376014)(7416014)(23010399003)(1800799024)(10070799003)(5023799004)(261009223027099003)(10067099003)(921020)(22082099003)(18002099003)(56012099006)(261022113327099003)(261021123027099003)(261021130227099003)(261102123227099003)(261021121427099003)(261028144727099003)(261026121527099003)(261022075727099003)(261102130727099003)(261022115727099003)(261026140227099003)(261022105027099003);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange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3lRVnRlNy9GaXF3RW1IbTFCNFdsTHlvSDN0ZGo3WnoxRm9aM0N6ZmFz?= =?utf-8?B?ajN6SnE2Z2hTZzBFNXovSkIrSjFsbFZDalBMTXo3QmJXcEZkaXNJbEMyVTNJ?= =?utf-8?B?ZVl0bU53ckFHYkhHYkx4Ty93c3prWHJoVzNIT0xwdG1JVnpXL1ZKOHZieGpW?= =?utf-8?B?NnVXOHlUa2JHQTliMGx5bTRqM2wvNFM3OUMva3NLSWFRSTNoVnpqbHB5R0FD?= =?utf-8?B?WkQ0azliVTJ5bDkzQUVNeS9JVy83Z3dVMWo3MjQwVThJVkcxSWFGQS9QaWkz?= =?utf-8?B?Q3JwL2R5UnRJcDhaOWZRNE5aS29VdnBITXpjeWlmcGJPU096ckJBVjNOZDQ1?= =?utf-8?B?RDlwVk0wSUFUODRXd0kwWWRKWWcxV0g0dDRXbXE3cmhoUTN2ZjU0a0Y2ZEgx?= =?utf-8?B?WEdSNWFzTDdjZC9QbGF5WkYxSmtmUmQ3VE1oQVlNeXYvZzkxRis2U0NSbjhi?= =?utf-8?B?Tk1veE14VmlKZE9hd3dlZVBSeUJveDVHUlVHVitkZjE1NFBzOUxxSUhIRS9S?= =?utf-8?B?eDBETDNvaHBueDlvdzFEdmQ1UkVocXdUbjk0M1g4WWVWZ21DMHEyQT09?= X-OriginatorOrg: garyguo.net X-MS-Exchange-CrossTenant-Network-Message-Id: 90f96c71-11b3-4bea-36ab-08df51563247 X-MS-Exchange-CrossTenant-AuthSource: CWLP265MB6051.GBRP265.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Dec 2026 15:57:56.4056 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: bbc898ad-b10f-4e10-8552-d9377b823d45 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: slk3lcA3TB6sQXU2hQ9ZgswgnXC+xbLBfekO7gyS5+ZDG/1m9Kv+vG4oXGrGoXmvUjplkTzRqXdyWTxIIoL4CA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: LO0P265MB7377 Due to Rust macro scoping rules, macros that are exported with `#[macro_export]` gets added to the crate root, and this causes the crate root to get crowded with various macros. We used a trick of `#[doc(hidden)]` + `#[doc(inline)] pub use` to make these macros appear to be only defined in the documentation, but this does not actually prevent them from being referenced from crate root. Create a macro `#[macro_export_scoped]`, that automates these definition and re-export and also give these macros unique and non-guessable names by hashing the file name and macro name (in theory this can collide if two macros are defined with the same name in the same file, but this is very unlikely; this could be fixed by including line number or macro definition in the hash, but doing so is less reproducible because the hash could change due to reformatting or macro-internal refactoring). Signed-off-by: Gary Guo --- rust/macros/helpers.rs | 16 +++++++++++ rust/macros/lib.rs | 28 +++++++++++++++++++ rust/macros/macro_export_scoped.rs | 56 ++++++++++++++++++++++++++++++++++++++ scripts/Kconfig.toolchain | 3 ++ scripts/generate_rust_analyzer.py | 1 + 5 files changed, 104 insertions(+) diff --git a/rust/macros/helpers.rs b/rust/macros/helpers.rs index 8cc9a20f470e..d18fbf4daa0a 100644 --- a/rust/macros/helpers.rs +++ b/rust/macros/helpers.rs @@ -38,6 +38,22 @@ pub(crate) fn value(&self) -> String { } } +pub(crate) fn file() -> String { + #[cfg(not(CONFIG_RUSTC_HAS_SPAN_FILE))] + { + proc_macro::Span::call_site() + .source_file() + .path() + .to_string_lossy() + .into_owned() + } + + #[cfg(CONFIG_RUSTC_HAS_SPAN_FILE)] + { + proc_macro::Span::call_site().file() + } +} + /// Obtain all `#[cfg]` attributes. pub(crate) fn gather_cfg_attrs(attr: &[Attribute]) -> impl Iterator + '_ { attr.iter().filter(|a| a.path().is_ident("cfg")) diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs index f448e7f21c83..41bc11abb5cd 100644 --- a/rust/macros/lib.rs +++ b/rust/macros/lib.rs @@ -8,6 +8,11 @@ // Stable since Rust 1.87.0. #![feature(extract_if)] +// +// Stable since Rust 1.88.0 under a different name, `proc_macro_span_file`, +// which was added in Rust 1.88.0. This is why `cfg_attr` is used here, i.e. +// to avoid depending on the full `proc_macro_span` on Rust >= 1.88.0. +#![cfg_attr(not(CONFIG_RUSTC_HAS_SPAN_FILE), feature(proc_macro_span))] mod concat_idents; mod export; @@ -16,6 +21,7 @@ mod helpers; mod io; mod kunit; +mod macro_export_scoped; mod module; mod paste; mod vtable; @@ -319,6 +325,28 @@ pub fn concat_idents(input: TokenStream) -> TokenStream { concat_idents::concat_idents(parse_macro_input!(input)).into() } +/// Export a macro publicly, but from the current module instead of crate root. +/// +/// # Examples +/// +/// ``` +/// mod foo { +/// #[kernel::macros::macro_export_scoped] +/// macro_rules! my_macro { +/// () => {} +/// } +/// } +/// +/// foo::my_macro!(); +/// ``` +#[proc_macro_attribute] +pub fn macro_export_scoped(attr: TokenStream, input: TokenStream) -> TokenStream { + parse_macro_input!(attr as syn::parse::Nothing); + macro_export_scoped::macro_export_scoped(parse_macro_input!(input)) + .unwrap_or_else(|e| e.into_compile_error()) + .into() +} + /// Paste identifiers together. /// /// Within the `paste!` macro, identifiers inside `[<` and `>]` are concatenated together to form a diff --git a/rust/macros/macro_export_scoped.rs b/rust/macros/macro_export_scoped.rs new file mode 100644 index 000000000000..3bbd7dbc1c28 --- /dev/null +++ b/rust/macros/macro_export_scoped.rs @@ -0,0 +1,56 @@ +// SPDX-License-Identifier: GPL-2.0 + +use std::hash::{ + DefaultHasher, + Hash, + Hasher, // +}; + +use proc_macro2::TokenStream; +use quote::{ + format_ident, + quote, // +}; +use syn::{ + Error, + ItemMacro, + Result, // +}; + +pub(crate) fn macro_export_scoped(mut input: ItemMacro) -> Result { + if !input.mac.path.is_ident("macro_rules") { + return Err(Error::new_spanned( + input, + "#[macro_export_scoped] can only be used on `macro_rules!`", + )); + } + + let Some(name) = input.ident else { + Err(Error::new_spanned( + input, + "`macro_rules!` definition missing an identifier", + ))? + }; + + // Hash together file name and macro name to create a hash that is likely to be unique. Use + // `DefaultHasher::new` which is free from RNG so the build is still reproducible. + let mut hasher = DefaultHasher::new(); + crate::helpers::file().hash(&mut hasher); + name.hash(&mut hasher); + let hash = hasher.finish(); + + let unique_name = format_ident!("{name}_{hash:x}"); + input.ident = Some(unique_name.clone()); + + let cfg_attrs = crate::helpers::gather_cfg_attrs(&input.attrs); + + Ok(quote!( + #[doc(hidden)] + #[macro_export] + #input + + #[doc(inline)] + #(#cfg_attrs)* + pub use #unique_name as #name; + )) +} diff --git a/scripts/Kconfig.toolchain b/scripts/Kconfig.toolchain index 1d68075f0469..c25767b34e06 100644 --- a/scripts/Kconfig.toolchain +++ b/scripts/Kconfig.toolchain @@ -275,6 +275,9 @@ config LD_CAN_USE_KEEP_IN_OVERLAY # https://github.com/llvm/llvm-project/pull/130661 def_bool LD_IS_BFD || LLD_VERSION >= 210000 +config RUSTC_HAS_SPAN_FILE + def_bool RUSTC_VERSION >= 108800 + config RUSTC_HAS_UNNECESSARY_TRANSMUTES def_bool RUSTC_VERSION >= 108800 diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py index dc1219736f77..621895bae128 100755 --- a/scripts/generate_rust_analyzer.py +++ b/scripts/generate_rust_analyzer.py @@ -250,6 +250,7 @@ def generate_crates( "macros", srctree / "rust" / "macros" / "lib.rs", [std, proc_macro, proc_macro2, quote, syn], + cfg=generated_cfg, ) zerocopy_derive = append_proc_macro_crate( -- 2.56.1