From nobody Sat Feb 7 07:07:39 2026 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (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 A46CB26BDBA for ; Fri, 28 Feb 2025 12:40:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740746412; cv=none; b=Uc2N4PSjOjHdaXIhmhugiyafTEIZdz8ID5cXN8Ud+KZ8VnJcvUphvaVo5hDFNS3mOzM4sAGT3ZsqZHiG+yfnPaKjWAw5MCzCnhF1lDOKnMeacWPM0lWw0qQo7KcxnWyjWDZ+8HCLYkZE1j1s26CwKbgE9mqSZw0ddq6nFYlIWng= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740746412; c=relaxed/simple; bh=ZcOD6O+e4opbd5kCSvi62otmJFeNmwXLjRXcW+if/rs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=H7QKHF2Fd4svFtTaKBDCLutQrP6eDrnP2nk9D7z2F6xSZ09O+597P5eOd5DhXJs+OHQmb5eySpjf3Mcfd0VcG97YG5KiGvzcejdgownakWa0kuJmdpngQlN0d3hAwhyR6mYfL5c9zjmi/caqdiHrmmvnhg1hiAjaJVO0dIm9Im0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Eohs6RMb; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Eohs6RMb" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-38f4e47d0b2so919411f8f.2 for ; Fri, 28 Feb 2025 04:40:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740746408; x=1741351208; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=7pofL3PjyOttZbsi+AAPKYxkRFtjnFNjm+r6OKyU3Jc=; b=Eohs6RMbGF0ws9o3QcDbrYYz/9sLSvJrfMpFrwb3xMOYXES7wZUoQwmfSJMFPgwxnN a7jM8b/Hj+Hr4PryaqWKJrkV+8vEnXLIRXikaVw/vQYNKkbHMwCQULm3nT99MjpjtKlN FQZ6VAxDfosLKxSMF5UPw2o/ZQ8F1ajae22W01w6V3Cha5eKiTaEEd3sJ9VhgyMDp2Ib qF5N65Jh89KOC0WTdKeNIocS9TY+d7MrKZejFRYaHXhfd0XmfTX6xyldKMXFUAUIpwqA Nrc8LdcqqeaMf1G8EIzYeUL5jaRoXZ9/VbbISOkEB+YE4KpKMCmqLKrWsiJXhADMTsRf tGlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740746408; x=1741351208; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7pofL3PjyOttZbsi+AAPKYxkRFtjnFNjm+r6OKyU3Jc=; b=uyB1lVHaurtkJrabB8JJPfwEZAAxOP7LuTjgI7abwfNfVvISJ0V3CUDD6wi2n464KU 2vX+T9D1+IAmz5czva+MP5m5k0P5E5BiwzhOQDGqTal77G42Ksd5aot5q7RNaWYPJBQz fB4+dgwC9siRaOcHAeqVyAM9ea7Uxi3f6kbtRyG4YPJxeQEBd0MmiFfcZp4tLBRCIB0L jp61yRGISo3ahEo8X7Oy8IbMe1Mvi76NTkJdokbqhFHiAwgUl6nDVce+QSIeGfS3Pf68 eNdJs7ijgHK/b0Xs++VJrPQiRbqa4wOslvmOpd4Ix53D/4c8GQBgIWIZbxRnPXLTG7Tl m1bA== X-Forwarded-Encrypted: i=1; AJvYcCVPjMQpaIaGgu8GLT0qpyBFAeKi+SPdgn6dTvclvUIDXfSOYcsHMU+GHv+VXEsDmdy+7YRFNFrYRXaZ9k8=@vger.kernel.org X-Gm-Message-State: AOJu0Yxg5c1S6dPutyUixUm74f+Er0t6TgP8BILAhOIhiLLLBBtXv1Yi 2oHreWKRBZRmxqg/rzJbG2V9Q+jZsTNv9nTmAkBWYoJvGe1Lq5/l9nbBlJRHKA7ZxE8wkbYbr4x rg+yTS2cWNjS/zQ== X-Google-Smtp-Source: AGHT+IEB4CLEaJ+qcbV7KVAUJWH8lLFxAEU0yznQEm575/JtfTpZwy4AyFvnI6lctCz2coNH3yp6bCwEPD/9KY0= X-Received: from wmbgx24.prod.google.com ([2002:a05:600c:8598:b0:439:4366:35c2]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a5d:64c8:0:b0:38f:452f:9fa9 with SMTP id ffacd0b85a97d-390ec7c999fmr2855371f8f.2.1740746407926; Fri, 28 Feb 2025 04:40:07 -0800 (PST) Date: Fri, 28 Feb 2025 12:39:30 +0000 In-Reply-To: <20250228-export-macro-v2-0-569cc7e8926c@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250228-export-macro-v2-0-569cc7e8926c@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=2129; i=aliceryhl@google.com; h=from:subject:message-id; bh=ZcOD6O+e4opbd5kCSvi62otmJFeNmwXLjRXcW+if/rs=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnwa6gD60PImwfkkxa2KFpiXq9/w2BWpeDHAYuF HY9+9v6Jo6JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ8GuoAAKCRAEWL7uWMY5 RlA/D/4xOjjAKDHvotub7uSZH7J7bXp09wrMC83XiONmJ8ooDIBNM40VRIhnD9fsRBHxwRlyWJ2 Kwti5p3Z3nl4+yLiulPukfaUxTp/Rb3vmImVzX+bcZj4my8iKDQzyA36aQssqbbtH1noQHBC+pG Lk4eSHE+kem98SXqPQ6mM+++r5oFHZp7XmddA4VFXjm/RNWheJCcVxddh4pIZaFR0gkCzgKjaoM F6ebM3hEUH3i4mk3LT+89cVLV6pUlQ0YeFPIpDmLySdH6qOzv7RPYzVc7D5opfVFo2OkzvUJqah eeWLqrufXB1afblwaqI3xSx40Hde+wthgNEmd+wHFJTCM45s+KJNs9q885dspBKBRJSk9qH1qJM GM3BDXno6f4+/IlcpjoDqhJxZJiaqLhn6UKP1JRg+YNjvxvmtbaE9zquS5W/zJkF7mr4OvHYzT4 JTQs3MmMxVtWLjEIpqFUz1sbZFuW1u7QTDUE2xVEtjdupnXBP+dP+geMGIOMK2kHtQOV86Ez2IQ OTdEASyuSSy95MldW9457o32AKMWM4/K1TZUlf3XITFOlxrXHn2dzoo6DNZDHS2RejEWdc6kYIU UK+N55oBRhryIBOLjNK8l9oNxn+vZ23Bi+oiULFoeSfygjizwNEEZQlxlAhrzXdN3SAlL6I5xNe KMISLLVgrNS99XA== X-Mailer: b4 0.14.1 Message-ID: <20250228-export-macro-v2-1-569cc7e8926c@google.com> Subject: [PATCH v2 1/5] rust: fix signature of rust_fmt_argument From: Alice Ryhl To: Greg Kroah-Hartman , Miguel Ojeda Cc: Petr Mladek , Steven Rostedt , Andy Shevchenko , Rasmus Villemoes , Sergey Senozhatsky , Andrew Morton , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, dri-devel@lists.freedesktop.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Without this change, the rest of this series will emit the following error message: error[E0308]: `if` and `else` have incompatible types --> /rust/kernel/print.rs:22:22 | 21 | #[export] | --------- expected because of this 22 | unsafe extern "C" fn rust_fmt_argument( | ^^^^^^^^^^^^^^^^^ expected `u8`, found `i8` | =3D note: expected fn item `unsafe extern "C" fn(*mut u8, *mut u8, *mut = c_void) -> *mut u8 {bindings::rust_fmt_argument}` found fn item `unsafe extern "C" fn(*mut i8, *mut i8, *const = c_void) -> *mut i8 {print::rust_fmt_argument}` The error may be different depending on the architecture. To fix this, change the void pointer argument to use a const pointer, and change the imports to use crate::ffi instead of core::ffi for integer types. Fixes: 787983da7718 ("vsprintf: add new `%pA` format specifier") Signed-off-by: Alice Ryhl Acked-by: Greg Kroah-Hartman Reviewed-by: Tamir Duberstein --- lib/vsprintf.c | 2 +- rust/kernel/print.rs | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 56fe96319292..a8ac4c4fffcf 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -2285,7 +2285,7 @@ int __init no_hash_pointers_enable(char *str) early_param("no_hash_pointers", no_hash_pointers_enable); =20 /* Used for Rust formatting ('%pA'). */ -char *rust_fmt_argument(char *buf, char *end, void *ptr); +char *rust_fmt_argument(char *buf, char *end, const void *ptr); =20 /* * Show a '%p' thing. A kernel extension is that the '%p' is followed diff --git a/rust/kernel/print.rs b/rust/kernel/print.rs index b19ee490be58..61ee36c5e5f5 100644 --- a/rust/kernel/print.rs +++ b/rust/kernel/print.rs @@ -6,12 +6,11 @@ //! //! Reference: =20 -use core::{ +use crate::{ ffi::{c_char, c_void}, - fmt, + str::RawFormatter, }; - -use crate::str::RawFormatter; +use core::fmt; =20 // Called from `vsprintf` with format specifier `%pA`. #[expect(clippy::missing_safety_doc)] --=20 2.48.1.711.g2feabab25a-goog From nobody Sat Feb 7 07:07:39 2026 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (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 90FA926D5A2 for ; Fri, 28 Feb 2025 12:40:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740746414; cv=none; b=D5K6OrNMTBmLZAuK+ZE6dVNGipZlyVaN2751GLjFMGe1BoR8/kqeFh7luRM/EsOsNye2yhGzX5eYNeOs/7Mp2BpUOxfos90t6i9BNup5M5/BsyiKEk2WcQOO+OE5biyvfM/hnSteB3ziUy4yymEMO2pPAW9updnkgCMItLGSCCY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740746414; c=relaxed/simple; bh=2kxCqgatimm9DkO+86c5Mx3XyGNaZZFNb0jwXIeUduQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mH9pi0REOhuVKUwJtohaCMw9kSsLK/b5VEJZfpeerqohmSqqscabzbqBVigeC5coGmb+N4SK72/l1/PQJMb/9FCvEDQRk2E63jjVFqhWH6jgGQfBpgrbs7TvRr4TqwKIMc+IA5jc4e6Uh379TSZJa/gMjYNn0UDj51dLkfuB+mc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Mz1u5BPl; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Mz1u5BPl" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-438e180821aso11335305e9.1 for ; Fri, 28 Feb 2025 04:40:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740746410; x=1741351210; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ihZxooYknzT0g7MIwrtZdZDoHsnETfvcwFPVB5oE20g=; b=Mz1u5BPlTn1EyJPtEYazVKxoAu0MUMfM7xIRW3ev8A2pDpszZejna2SHgvyv0GiajK 0mtMGz9HwKxZJOURg4i68Nw46BLAHf6TuDdR9JzX8M0WGpMIYVchIe111MzUq2ACEbZe 73R/at4Wj06tr96TswTzNniDYrsH+M4VUM6R6w/ul2tDp8FCKSDOj1gZLROj58r1cYt0 R0dzSGAJ+763stNF3ItlcnnI9y9kdkGA32hpl85407EGuX2TRB5J5B134bMbae7ZIJhM S8ZxDbQwWj76x/YMjDBk4/OOPLVXTcM5y49IEUT2A2f+Kd7VyIt3YKyZ5LQwc7SnYRGF G80Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740746410; x=1741351210; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ihZxooYknzT0g7MIwrtZdZDoHsnETfvcwFPVB5oE20g=; b=aISa7onXNUE/R/o/haj8xrB/KdnQfGeUhzR2rxx+JDHHT324PlBAUaOmtfJ8Ww1f8H GRE0/COSLrH24mjrh4ZCwoU4PW1QJoyZQ9ssU0M+Evzy57/jtjOTPshBDebUIkQu2UTC TcfixSQqufm5powdGxQH9Ywz2whysJ+BzoUxmwWVwFOqW/k2AZAOVsxPmAtQaeEersMg yy/NlIIoeV1ZGfimwJRn/faAMB+FaAJkNKnMvocEDtZQyVHWAY31Ix8inW8aXxm47Gij FoaEdCZHJ44QrpYu1oCZuGCyXcQrYOYZCH3F/DLe/0zKbcx/5jebFK0KIdzQMMUMkUd1 GnhA== X-Forwarded-Encrypted: i=1; AJvYcCU+H+lMzCohymab09lyQ0PYN21ZtLkF4AACeLdm88Hj7lgUils4lyh32e0C6qrwRVcqZbsmtoBkxXwgPM4=@vger.kernel.org X-Gm-Message-State: AOJu0Yx3RDKJdeDqlyOJZeXEUtewOupsXR2YoxU3q3ot4e1/ioyBFybw AW35R2viP7zEPVpt0vqvBUgWu2nwEXjsxKLfEu1kJFSChh01GQGWSBUdEyyJcdouGAuSluNg6FZ /1QLlsOwUCpWzuQ== X-Google-Smtp-Source: AGHT+IGMEITDLjU4ASlaZS6GD8Wn7exwSBzOaUHr7x/3v0wRj0xk5+dqwptvCzuTjq4s4FptkTDfbSPgA1O7jVk= X-Received: from wmbjg14.prod.google.com ([2002:a05:600c:a00e:b0:439:942c:b180]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:a03:b0:439:9b19:9e2d with SMTP id 5b1f17b1804b1-43ba6702becmr29268005e9.16.1740746410006; Fri, 28 Feb 2025 04:40:10 -0800 (PST) Date: Fri, 28 Feb 2025 12:39:31 +0000 In-Reply-To: <20250228-export-macro-v2-0-569cc7e8926c@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250228-export-macro-v2-0-569cc7e8926c@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3131; i=aliceryhl@google.com; h=from:subject:message-id; bh=2kxCqgatimm9DkO+86c5Mx3XyGNaZZFNb0jwXIeUduQ=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnwa6gi4HcYLwOcjP1+4ywczOPXPU6Ct2zzIOxr Bd2hpe101yJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ8GuoAAKCRAEWL7uWMY5 RpM/D/4hjhqbZcF9DblDJ2gtwYChZ7TjKidwXbqaZS2uFBN1bIFeweV0YoVoeZcFHADd8cv7bfs Ub11cChO1fW+ZTL0mjSIyMS040tnKhjG6eRgkq2emew0vBTo8K1FVrJYcTQIA0HJMfogwOWexr8 oA/kxR+bUlhTOJn1fXdositwbuiBsu+2ZDkkeocob8oFDkfgsGoctAU30UKauDwdE2HNAytT2TS 7eGXelDyCe2HCYBuY7N2f1j/sInwm+eudSi+6jJynDQUg0RNUrGYmTb1zPrX6AZN8jeAMfsegQY zpw3e3lIYu/augysePE1HG95uNFNL9fYvh8jFr72nuLlP7qtpu3/fcB3wpHhxcSkv+lyG9Cpjbu alfvEgF26SVpdND0F5ng4XtNROT/ZLKYOnUPJLHrroAO3N5m0dvhfu1/D164xIzvZymhyeEByiF x3rYz8qQNqJOvypQ2PpcTciMJshzHlraOEoMtpONn7C1Dnk6nG8tSNquB9ffhF9xhY8yGajyNKV uJck4NUgxNadRWg4JrHJS/X5uSChoHCZmqsG0028wjlDhRGu/B3dvBOhRrhFWX4kpVbxTqyK3Os 01jSp9gEmFWSGRJhaJROwxY88GQyKPhXJkqTlFZMyW/lq+4ypr52F/jJcmp3V/S+L9/O5uSKxhJ 0wTtrurvO1EjnWA== X-Mailer: b4 0.14.1 Message-ID: <20250228-export-macro-v2-2-569cc7e8926c@google.com> Subject: [PATCH v2 2/5] rust: macros: support additional tokens in quote! From: Alice Ryhl To: Greg Kroah-Hartman , Miguel Ojeda Cc: Petr Mladek , Steven Rostedt , Andy Shevchenko , Rasmus Villemoes , Sergey Senozhatsky , Andrew Morton , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, dri-devel@lists.freedesktop.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This gives the quote! macro support for the following additional tokens: * The =3D token. * The _ token. * Using #my_var with variables of type Ident. Additionally, some type annotations are added to allow cases where groups are empty. For example, quote! does support () in the input, but only when it is *not* empty. When it is empty, the compiler cannot infer the item type of `tokens`. These additional quote! features are used by a new proc macro that generates code looking like this: const _: () =3D { if true { ::kernel::bindings::#name } else { #name }; }; where #name has type Ident. Signed-off-by: Alice Ryhl Acked-by: Greg Kroah-Hartman Reviewed-by: Andreas Hindborg Reviewed-by: Tamir Duberstein --- rust/macros/quote.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/rust/macros/quote.rs b/rust/macros/quote.rs index 33a199e4f176..c18960a91082 100644 --- a/rust/macros/quote.rs +++ b/rust/macros/quote.rs @@ -20,6 +20,12 @@ fn to_tokens(&self, tokens: &mut TokenStream) { } } =20 +impl ToTokens for proc_macro::Ident { + fn to_tokens(&self, tokens: &mut TokenStream) { + tokens.extend([TokenTree::from(self.clone())]); + } +} + impl ToTokens for TokenTree { fn to_tokens(&self, tokens: &mut TokenStream) { tokens.extend([self.clone()]); @@ -40,7 +46,7 @@ fn to_tokens(&self, tokens: &mut TokenStream) { /// `quote` crate but provides only just enough functionality needed by th= e current `macros` crate. macro_rules! quote_spanned { ($span:expr =3D> $($tt:tt)*) =3D> {{ - let mut tokens; + let mut tokens: ::std::vec::Vec<::proc_macro::TokenTree>; #[allow(clippy::vec_init_then_push)] { tokens =3D ::std::vec::Vec::new(); @@ -65,7 +71,8 @@ macro_rules! quote_spanned { quote_spanned!(@proc $v $span $($tt)*); }; (@proc $v:ident $span:ident ( $($inner:tt)* ) $($tt:tt)*) =3D> { - let mut tokens =3D ::std::vec::Vec::new(); + #[allow(unused_mut)] + let mut tokens =3D ::std::vec::Vec::<::proc_macro::TokenTree>::new= (); quote_spanned!(@proc tokens $span $($inner)*); $v.push(::proc_macro::TokenTree::Group(::proc_macro::Group::new( ::proc_macro::Delimiter::Parenthesis, @@ -136,6 +143,16 @@ macro_rules! quote_spanned { )); quote_spanned!(@proc $v $span $($tt)*); }; + (@proc $v:ident $span:ident =3D $($tt:tt)*) =3D> { + $v.push(::proc_macro::TokenTree::Punct( + ::proc_macro::Punct::new('=3D', ::proc_macro::Spacing::Alo= ne) + )); + quote_spanned!(@proc $v $span $($tt)*); + }; + (@proc $v:ident $span:ident _ $($tt:tt)*) =3D> { + $v.push(::proc_macro::TokenTree::Ident(::proc_macro::Ident::new("_= ", $span))); + quote_spanned!(@proc $v $span $($tt)*); + }; (@proc $v:ident $span:ident $id:ident $($tt:tt)*) =3D> { $v.push(::proc_macro::TokenTree::Ident(::proc_macro::Ident::new(st= ringify!($id), $span))); quote_spanned!(@proc $v $span $($tt)*); --=20 2.48.1.711.g2feabab25a-goog From nobody Sat Feb 7 07:07:39 2026 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (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 AD48D26D5D3 for ; Fri, 28 Feb 2025 12:40:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740746415; cv=none; b=cX7IgYzCQLnY08qUPLFRn3Our1te58l0WBXvNKrHlb88VnHbD0ljSnOaE3uFe6X8HSv3Lzx+HhOUtzjLpdzA8me+vJMj1SR1F8vFS/RJGXj9mjn3yV5hbOx2cs1ivcKN90UiIl2ULMAHpsOhqBI4LS2M9nnkeP9+cyR8NPJohjc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740746415; c=relaxed/simple; bh=5WVJDDoOMWVTToGZqS6EzXIOd2Gz+/3HoUezV/SekI8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rM2HKx91Ge3Oaidt8+XpMNN39KGfGOgMfvPR2/NILPrG0l0jFxfKSy92tUYND589s01TdKbYBv4KLEv1SwI1FvispT/o5lwzro7bOi5xeI1izgnLI0AqRbmyV9YRNdGn0Ll5tb7G+uN3Ae4ogegqfaDFsw7BTqnCnEhAxCS5q7k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=4AleCqwK; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="4AleCqwK" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-4394040fea1so10904065e9.0 for ; Fri, 28 Feb 2025 04:40:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740746412; x=1741351212; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=l3jVWPGGKqLCvuenpCukdw2BdIHWqKd1uEI4muVogSg=; b=4AleCqwKB7Y7bB84UAMNwT1q9frjP5D35i4DJlBuE7R3gSrljybVEGj5smGpCeIwCq dvnK90oWZTOZe35XcuYogmZoC8qTo2lZw4N5jgixb2E6rGTdpll65u9Z1Id1gznxzCbd Io160W4IdZxqH+TH4J/rDptSJjTY18QDEJ3rbCV/O4iucLpP/FpyE5epyKH/vD51DYEF x4P17MnjGwfztUcTCnEW+/ahE5AeT0ZhftkcM/ux4eF/2P4nT6rx10QcsCyCTzsyvd5K TzgxyXr51h6SgykiR4sdcipw+dK57Nucko4izif2AtPShla/GijKJsgy3Qa8/W/1wpJb Yxhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740746412; x=1741351212; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=l3jVWPGGKqLCvuenpCukdw2BdIHWqKd1uEI4muVogSg=; b=VRYUKLsHrKKnv6Zyar7V2l7kK/bUpkOWoMiV3YRV7F41KEfP9m0d4OrQ0K15nK1NND Ec/2lph2H7VCQl+K8yXXAdSiJCm8Uf40Lyo45H+fk20pJrNnmxYAngG5JIeDjPQ0BBs2 vIKl2qWKkucUijxQ542sQXsJdpDuCM9LiOdbNell/O8/GOIphr+OyG/xQ2O4pzmTr4iC ccwKHC43gJK0wOcq1VNEEseXslSEyIPOB1Xop5nKRZxg7hSsAOzh+XSZtGY9Du/kv/jD S5hlCNZr3BJjHdxSG1gQnW/UFFNOu5lsWUj5F3Ak01PeqWQnepNGPVeB1KJjuYDdd62i 6HHA== X-Forwarded-Encrypted: i=1; AJvYcCVVKlrjmmXQS5fGtC7mV+wedQMwJP+RHvk0maLSH81eB2buqQIZn0diEi5D/A+b+LbtGmMAl82NuSQEf+k=@vger.kernel.org X-Gm-Message-State: AOJu0YwoGDePeTRAcGackndZy2NWbxUSChXXqapzlSUwP5XPsYxEObvf JXX3kzIw0dwqjTRCJd7sGbWEbL3ScLcAMYeqxHcT0tIjPqSRIvcDErYF7XY+LIthf4b1PZsMrdc Sle/p99OQQXbICA== X-Google-Smtp-Source: AGHT+IGiO67Lsqlqu2zH4k6evB04PeH28TLpEEsqPRIVHE93/W85JAQLdE41QCH630255Ni8glO+u11+ewCTl8g= X-Received: from wmsd10.prod.google.com ([2002:a05:600c:3aca:b0:439:9541:1cf5]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1c9f:b0:434:9e17:190c with SMTP id 5b1f17b1804b1-43ba6188e94mr26636865e9.0.1740746412195; Fri, 28 Feb 2025 04:40:12 -0800 (PST) Date: Fri, 28 Feb 2025 12:39:32 +0000 In-Reply-To: <20250228-export-macro-v2-0-569cc7e8926c@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250228-export-macro-v2-0-569cc7e8926c@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=6541; i=aliceryhl@google.com; h=from:subject:message-id; bh=5WVJDDoOMWVTToGZqS6EzXIOd2Gz+/3HoUezV/SekI8=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnwa6hUITYWJl/dPCaT3DkAHUJoqZpyVu6b2hRy /7EqajnYoyJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ8GuoQAKCRAEWL7uWMY5 Rn6GD/9qthSYpENpEq926mPspcUYP5BVOc1VoMWtdTSk2Gbc6ytG9cgiVWr1qh6h0pis4Rlv6GE iCys28XgoAwtyPwUJyouL5vLN69zhjQ9A1bG5/7KdlWu+PQKpeQijeufFnt7VTAOceVE0soo3bm 3UDNjaK/RMuBntw1pkOn7e2gXLdrlsGQEiMGQZTDaoWzKOYjtC7aUCzyJ53a2K0L64Zluk2+5in akMOnB5ywQbnvsWaf9ax7ZC7JA2k2jJxUq32ECwTd7qu4uoHqFaY7Shxryvxp8GCCioblbZPgo9 NNOqSrDW4++cffxUog3Zfq0j81QgGUkio4nl6k5kJwsjdW38+79IkwkM3HgS+Rz8SeMvDvquLFQ XvD6OLEgRXVbnBfURzwDufAhpHElGq10OMT1txFrxc7RgQgpES+HqNZ33JsoMir2ROLhMgn0SAA 2cAHKLJwlunm84rd7ZcPt9o+Glm/Ar0oUksZpbEOI5z+HNsgYF2jE/gg9fLzq+VkzWxKLnEzLGx ahs9Rlp4WfyhfriizEC0Dp1oHst/0VsBpOEJL7olDIl+WCAZ8hnlG6ABHeJ4B0sY12z31QFzICL 73Mg4qh8RVEd0TlTS+ZKthzzkWyJx+bKVkddrJeRGkruXRN6cXuYeZO+Wpo4FXMpQxXzItHZuFa Mav3YeJON9IyQuQ== X-Mailer: b4 0.14.1 Message-ID: <20250228-export-macro-v2-3-569cc7e8926c@google.com> Subject: [PATCH v2 3/5] rust: add #[export] macro From: Alice Ryhl To: Greg Kroah-Hartman , Miguel Ojeda Cc: Petr Mladek , Steven Rostedt , Andy Shevchenko , Rasmus Villemoes , Sergey Senozhatsky , Andrew Morton , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, dri-devel@lists.freedesktop.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Rust has two different tools for generating function declarations to call across the FFI boundary: * bindgen. Generates Rust declarations from a C header. * cbindgen. Generates C headers from Rust declarations. In the kernel, we only use bindgen. This is because cbindgen assumes a cargo-based buildsystem, so it is not compatible with the kernel's build system. This means that when C code calls a Rust function by name, its signature must be duplicated in both Rust code and a C header, and the signature needs to be kept in sync manually. To eliminate this manual checking, introduce a new macro that verifies at compile time that the two function declarations use the same signature. The idea is to run the C declaration through bindgen, and then have rustc verify that the function pointers have the same type. The signature must still be written twice, but at least you can no longer get it wrong. If the signatures don't match, you will get errors that look like this: error[E0308]: `if` and `else` have incompatible types --> /rust/kernel/print.rs:22:22 | 21 | #[export] | --------- expected because of this 22 | unsafe extern "C" fn rust_fmt_argument( | ^^^^^^^^^^^^^^^^^ expected `u8`, found `i8` | =3D note: expected fn item `unsafe extern "C" fn(*mut u8, *mut u8, *mut = c_void) -> *mut u8 {bindings::rust_fmt_argument}` found fn item `unsafe extern "C" fn(*mut i8, *mut i8, *const = c_void) -> *mut i8 {print::rust_fmt_argument}` It is unfortunate that the error message starts out by saying "`if` and `else` have incompatible types", but I believe the rest of the error message is reasonably clear and not too confusing. Signed-off-by: Alice Ryhl Acked-by: Greg Kroah-Hartman Reviewed-by: Andreas Hindborg Reviewed-by: Tamir Duberstein --- rust/kernel/prelude.rs | 2 +- rust/macros/export.rs | 28 ++++++++++++++++++++++++++++ rust/macros/helpers.rs | 19 ++++++++++++++++++- rust/macros/lib.rs | 24 ++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 2 deletions(-) diff --git a/rust/kernel/prelude.rs b/rust/kernel/prelude.rs index dde2e0649790..889102f5a81e 100644 --- a/rust/kernel/prelude.rs +++ b/rust/kernel/prelude.rs @@ -17,7 +17,7 @@ pub use crate::alloc::{flags::*, Box, KBox, KVBox, KVVec, KVec, VBox, VVec= , Vec}; =20 #[doc(no_inline)] -pub use macros::{module, pin_data, pinned_drop, vtable, Zeroable}; +pub use macros::{export, module, pin_data, pinned_drop, vtable, Zeroable}; =20 pub use super::{build_assert, build_error}; =20 diff --git a/rust/macros/export.rs b/rust/macros/export.rs new file mode 100644 index 000000000000..c5ec75f2b07f --- /dev/null +++ b/rust/macros/export.rs @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-2.0 + +use crate::helpers::function_name; +use proc_macro::TokenStream; + +/// Please see [`crate::export`] for documentation. +pub(crate) fn export(_attr: TokenStream, ts: TokenStream) -> TokenStream { + let Some(name) =3D function_name(ts.clone()) else { + return "::core::compile_error!(\"The #[export] attribute must be u= sed on a function.\");" + .parse::() + .unwrap(); + }; + + // This verifies that the function has the same signature as the decla= ration generated by + // bindgen. It makes use of the fact that all branches of an if/else m= ust have the same type. + let signature_check =3D quote!( + const _: () =3D { + if true { + ::kernel::bindings::#name + } else { + #name + }; + }; + ); + + let no_mangle =3D "#[no_mangle]".parse::().unwrap(); + TokenStream::from_iter([signature_check, no_mangle, ts]) +} diff --git a/rust/macros/helpers.rs b/rust/macros/helpers.rs index 563dcd2b7ace..3e04f8ecfc74 100644 --- a/rust/macros/helpers.rs +++ b/rust/macros/helpers.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 =20 -use proc_macro::{token_stream, Group, TokenStream, TokenTree}; +use proc_macro::{token_stream, Group, Ident, TokenStream, TokenTree}; =20 pub(crate) fn try_ident(it: &mut token_stream::IntoIter) -> Option= { if let Some(TokenTree::Ident(ident)) =3D it.next() { @@ -215,3 +215,20 @@ pub(crate) fn parse_generics(input: TokenStream) -> (G= enerics, Vec) { rest, ) } + +/// Given a function declaration, finds the name of the function. +pub(crate) fn function_name(input: TokenStream) -> Option { + let mut input =3D input.into_iter(); + while let Some(token) =3D input.next() { + match token { + TokenTree::Ident(i) if i.to_string() =3D=3D "fn" =3D> { + if let Some(TokenTree::Ident(i)) =3D input.next() { + return Some(i); + } + return None; + } + _ =3D> continue, + } + } + None +} diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs index d61bc6a56425..fbb2860e991f 100644 --- a/rust/macros/lib.rs +++ b/rust/macros/lib.rs @@ -9,6 +9,7 @@ #[macro_use] mod quote; mod concat_idents; +mod export; mod helpers; mod module; mod paste; @@ -174,6 +175,29 @@ pub fn vtable(attr: TokenStream, ts: TokenStream) -> T= okenStream { vtable::vtable(attr, ts) } =20 +/// Export a function so that C code can call it via a header file. +/// +/// Functions exported using this macro can be called from C code using th= e declaration in the +/// appropriate header file. It should only be used in cases where C calls= the function through a +/// header file; cases where C calls into Rust via a function pointer in a= vtable (such as +/// `file_operations`) should not use this macro. +/// +/// This macro has the following effect: +/// +/// * Disables name mangling for this function. +/// * Verifies at compile-time that the function signature matches the dec= laration in the header +/// file. +/// +/// You must declare the signature of the Rust function in a header file t= hat is included by +/// `rust/bindings/bindings_helper.h`. +/// +/// This macro is *not* the same as the C macros `EXPORT_SYMBOL_*`, since = all Rust symbols are +/// currently automatically exported with `EXPORT_SYMBOL_GPL`. +#[proc_macro_attribute] +pub fn export(attr: TokenStream, ts: TokenStream) -> TokenStream { + export::export(attr, ts) +} + /// Concatenate two identifiers. /// /// This is useful in macros that need to declare or reference items with = names --=20 2.48.1.711.g2feabab25a-goog From nobody Sat Feb 7 07:07:39 2026 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (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 0353D26E16C for ; Fri, 28 Feb 2025 12:40:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740746417; cv=none; b=WrBgkNwP/1rHp3kXFtqpoJK0G+Zg7hdpl034G+cKyiR+JwZGt+3DRywuTPePk+bTBwZDvMhGqZrB4C4c+/c8rCtmuyb1Ur6jK3ByXIgacXnKftpYn2Jl45UDJ0j5k15yY3RrJzqW/2uI1fyhA79EkMZTxUNaGMX1X2fvtNKKv5M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740746417; c=relaxed/simple; bh=r7jgVsiiJzFKABmvsoHtJDihUf1aN5aOfGCQnlUXLR8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=h2DMh5V/VLtLh9Ld7WVA+09pwgrbwNWpaq2On+QrZOYq5B4w7AjEpjEmiNtW097piX6FcaODpv4wP7RuVRmNFks/9k+xii/O0urfDgOU0Hi1h9qfqOOiH+5ZoySw6DcmwF6nAmWlo/a9tZlieKyEcMvcvQMSbEOWydul1ICOOg8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Bzm1HW/W; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Bzm1HW/W" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-4399a5afcb3so20122185e9.3 for ; Fri, 28 Feb 2025 04:40:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740746414; x=1741351214; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=G+5gHFsUk/zR3QwRdy/hSALMqHlURbjWQTJYZwElRBw=; b=Bzm1HW/WaiU8mt0VN/aGZLpWWHwT6ZzHUwCAZzA9IhOjKIzWMUhjGl7aLfBJ0U9OtT 0fFlTIFaV/AR1aruvNuzKRwgQ1yMI/ah/WpbRMya5Q1ZooHG+SXz0FTteIQ3/JJOlHMM wTDDYBRM1y5wqTnOMUZXuzVXAglFCDDIhksj+3VwBwSHwy/wnjIqyV9FLfxjkwMm4Dpz Cld4/hNOi2hhVqkCy9Dq4sAH/N8+S6nRtcVzHMOcvOOzCeyjd8skKzRyHpExGuvI3Vto pZELgW8lR8qPtNyyutNoC3YQaHQmR5keP1HexIbiGUKZWZ4RgM83Gsajt0NOPk3NnOPU 5bmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740746414; x=1741351214; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=G+5gHFsUk/zR3QwRdy/hSALMqHlURbjWQTJYZwElRBw=; b=dcHBGYzq1Qzw3nRGi+pBW2iVdZWbw/DJ6lUXCMzyRBSHm5nIPVPesqa35uYzG3xvRu PNSdf012jRmLJKE8P8Nuv1LR9xxJHeYHKq9SOyl234HX13VE+sWAEQAoLNqPDNUnOCNh SKjdXjukf4dGO7br+HCwVdX/zczS3FPSn+u6h0pmrQBCpjRtmxoKRj/B5xAkUTcuzIvH AEyYF7l7zvZvEcdcn+51HUhtY9AvlAcvufPh6xtgCKeGJaOSJ6PeVAPMnxcC8CbiNFJo K47jZGGAaw/Y7+SSmus12CQ0SFdjF2/bi1jjojRdhlPflAw8GWNotLgVk/jbxWqAc26K +k5A== X-Forwarded-Encrypted: i=1; AJvYcCV2Wmulri4fuODb7NOsJMTggL9oy5gFwb3/SsVLFqTuMdm8ryYzdr+qOZfKjMhboxonZFjfXNYKjJLu/Ds=@vger.kernel.org X-Gm-Message-State: AOJu0YydPuAE9s8dcDgHkv83UzROJVnaJwLIP8ggBHA0gM1SfJ77Os0H mtYXDyS1ahqKdcXqEjmWh8y2NH0k6rWSUP1FX4qjnsLVfmr4XG3JQ474NQZGY1V7e30OcTPMfx1 bX/mLPb6iF3OK5A== X-Google-Smtp-Source: AGHT+IHbIphIQcEXilOr7Ntw6WaNAr5phX1XmvUcyGqxwt+X3Cd6rOdQsMiRHilLofVXJl6ZHWfjzIcpQ0HSDVU= X-Received: from wmcn18-n1.prod.google.com ([2002:a05:600c:c0d2:10b0:436:fdb2:fa5]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:3507:b0:439:3d5c:8c19 with SMTP id 5b1f17b1804b1-43ba674c911mr25914985e9.24.1740746414470; Fri, 28 Feb 2025 04:40:14 -0800 (PST) Date: Fri, 28 Feb 2025 12:39:33 +0000 In-Reply-To: <20250228-export-macro-v2-0-569cc7e8926c@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250228-export-macro-v2-0-569cc7e8926c@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=2057; i=aliceryhl@google.com; h=from:subject:message-id; bh=r7jgVsiiJzFKABmvsoHtJDihUf1aN5aOfGCQnlUXLR8=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnwa6iZBH4LXAf1My/2Bb4bMU2tOy+URmQaqy+I 0YgolPCIOyJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ8GuogAKCRAEWL7uWMY5 RntsEAC4uGdMp+3gOxWauz1JDyiZzR/Atujxvqo0jIjAoxK2HzkZY9QWUrC7RzFUKejI9Kba7dK 6I3yW8hwYThfVdITfDRhoegt/cJ4p/rBCktrQ/bDhIz0Gtg8Je8371Rg+tfgNJW15cdXD4eWKBf UUPBZLV/CtFQyCyhALtXPzhVfZrWxTqko8eE5bZP7rDrNZcPCUqqdGmYDLf2uyuSUkeW/L139bb DZqLXQ8oIjoGwuT6lvqTCQtZiBc9csEB0JKTywoJAO96QK8BmySRWsEBoBsBeoL5wUWrfk3Vd9b SSMMVT8XIJqKL/DxtvnppRQWiXZbdRaTaNVmh4hFbeOKPp9W2mbYaLlkXBqX/rRrYoOBE1jYoD7 n6MAIRSn4PoKhXOjfHr2ZqZFlEqrVpbZ33bzuIFnXnA0kzjTyO5QCwUIXghkuHdS6FjHa+yaAtV M2OIf2DAn0roOnGvIAu8HKCa/RY2fc7oItuhyF727kP4bWhnRUpm+0vi4O5Sdveq0W77yQgQS2B hqWpZba4ARnFFAT0uSS+CpMVdFPYwqhpREFLcw7qXmfVWTUYcjEMYxnrHoHf7qqtOhKE4Vc3QSS aCIyd8EwbEPwOybRVuVFiJMrTqtMEI+50Fm7LZ5wFZLOOevDJSvKQbuJlzgVE+zHoTuJxhz0ijN QgT9cliZsSNsMag== X-Mailer: b4 0.14.1 Message-ID: <20250228-export-macro-v2-4-569cc7e8926c@google.com> Subject: [PATCH v2 4/5] print: use new #[export] macro for rust_fmt_argument From: Alice Ryhl To: Greg Kroah-Hartman , Miguel Ojeda Cc: Petr Mladek , Steven Rostedt , Andy Shevchenko , Rasmus Villemoes , Sergey Senozhatsky , Andrew Morton , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, dri-devel@lists.freedesktop.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This moves the rust_fmt_argument function over to use the new #[export] macro, which will verify at compile-time that the function signature matches what is in the header file. Reviewed-by: Andreas Hindborg Signed-off-by: Alice Ryhl Acked-by: Greg Kroah-Hartman Reviewed-by: Tamir Duberstein --- I'm not sure which header file to put this in. Any advice? --- include/linux/sprintf.h | 3 +++ lib/vsprintf.c | 3 --- rust/kernel/print.rs | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/include/linux/sprintf.h b/include/linux/sprintf.h index 33dcbec71925..029ad83efd74 100644 --- a/include/linux/sprintf.h +++ b/include/linux/sprintf.h @@ -24,4 +24,7 @@ __scanf(2, 0) int vsscanf(const char *, const char *, va_= list); extern bool no_hash_pointers; int no_hash_pointers_enable(char *str); =20 +/* Used for Rust formatting ('%pA'). */ +char *rust_fmt_argument(char *buf, char *end, const void *ptr); + #endif /* _LINUX_KERNEL_SPRINTF_H */ diff --git a/lib/vsprintf.c b/lib/vsprintf.c index a8ac4c4fffcf..1da61c3e011f 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -2284,9 +2284,6 @@ int __init no_hash_pointers_enable(char *str) } early_param("no_hash_pointers", no_hash_pointers_enable); =20 -/* Used for Rust formatting ('%pA'). */ -char *rust_fmt_argument(char *buf, char *end, const void *ptr); - /* * Show a '%p' thing. A kernel extension is that the '%p' is followed * by an extra set of alphanumeric characters that are extended format diff --git a/rust/kernel/print.rs b/rust/kernel/print.rs index 61ee36c5e5f5..cf4714242e14 100644 --- a/rust/kernel/print.rs +++ b/rust/kernel/print.rs @@ -8,13 +8,14 @@ =20 use crate::{ ffi::{c_char, c_void}, + prelude::*, str::RawFormatter, }; use core::fmt; =20 // Called from `vsprintf` with format specifier `%pA`. #[expect(clippy::missing_safety_doc)] -#[no_mangle] +#[export] unsafe extern "C" fn rust_fmt_argument( buf: *mut c_char, end: *mut c_char, --=20 2.48.1.711.g2feabab25a-goog From nobody Sat Feb 7 07:07:39 2026 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (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 16E4326B972 for ; Fri, 28 Feb 2025 12:40:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740746420; cv=none; b=UAwiPEFWVjDqkg+SsUXoTQHpbUsUGOAM6SnGTY2ynC0ZTQYhvkd8mLNAqqkA/pgo0AGZV2spoU/lJoc+2t3MZfsjJEzPTqIE3g4tBuYW9CGJZaObtP56nT60wpZ5mk8VJMqGpR6ufIcpZNUgppX7qs2XOSDkNq2awvOor+enyOs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740746420; c=relaxed/simple; bh=lzB/op9EvTIfS1pnVWB0B3izr6Rw961OsgJ28NlS2cw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=CJ2SK3aY5GxEm9KhfUpgXtcxQE2edv/Gk0V4MWbtROqB45Vk3aEfkj3LcnAYAZyaS7UOL2j4LcDCHs/igvHL5nrk2rruqJYwoRgkU6FkwWY/UqIEMKq9uFOY9QC2vkqz7TqZwyAFPnH2tpEHL5r0qaJ8SSVzUHRzGxpNkgxrG2w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=W7numUTg; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="W7numUTg" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-390f365274dso147515f8f.0 for ; Fri, 28 Feb 2025 04:40:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740746416; x=1741351216; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=bKdCOruHgStYFRjw1OWEfRvPiQaLPkJaqZbPvH06xZY=; b=W7numUTg4VHj23F4uHUaN7xc5Y7v4PBACfAG56Fl8qjhYZQOGqi14kjONu7IF6WxcW EVswn5T4+89a8qzpiCQv6I5FjBVu5HKjvO9K5sMFe+0EmNnbpJ2XQxLH/e9dY7nEG/Ae 4QPcA8isiTThzhG0FANOI6TF6RkTkyWZYh3riVF0PA2ubKBkbnWY3GY/jMYttyX0UTjQ i1TUbCckxsRQh4qjxBMDE/ZEvVHiFlFOsoiYCCjsUDy/zYsKqsiFaELwHkRutdVvNNNa N66+qI8sBNJ9hgm1UJ6bn6lWhI7LyxlX3ykNTpueYpmX8qfBRLeNt+yHI+aOzzQvxQAG ncEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740746416; x=1741351216; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=bKdCOruHgStYFRjw1OWEfRvPiQaLPkJaqZbPvH06xZY=; b=gtpm6hfVCwlBUD7m4h92ORphcQdQ1cUF69p+u+m5jdj4XudYfFxnmVg0CJbGjl4v/8 C1XVySPMFUeQl/LibRh75gPZLT4srb8vrMheTzdGoxpFF90PIZlNH27KQfCAoS1GU26P 9x7K01x/9m70kd2gDTshaHav77fTQqUAbEHGg1d/jHAVuFTrPk24q2turHvx1hFiGj8x uSjzCQRPPYiJhV8IVqJcu/f9Xgg2qjII9a2WOHneZAt1TDWfbfoLHk2UkTcTFsXAWNqa bt6FTQGFK4xDnOfa9dlQpBckOHO8HLi0/WDyrWjcjD9v8w5u4X1IY0cQ5HdipznL3Ch0 +8/A== X-Forwarded-Encrypted: i=1; AJvYcCW50CJuwZe+d1Nj0LjI/NOFqUPcYK2jMPh/qHy+3nMzupUQLNYdNSMne1UZlrzUxx2YskLugJN8yvzcJV8=@vger.kernel.org X-Gm-Message-State: AOJu0Ywsrf4A238A8YeJ9t1PDaM8TttTt26g+hozej7A80GG8qq2aXup 3md1ZLAygXIfD4OGWPzxbocI0XsTUnjKD4/XJAOFZGQO3SbeQWbjZ3VGh7/V1Bi6GPC7YmtDa6u sw1RGb8k2W3D6jw== X-Google-Smtp-Source: AGHT+IGAooNngUR6F5E/Jf8s4LC0YsqlsbQHIiPJaRDR2Z8JjTKZ1EiRXmYu1cG1inA9uIBqBtJ1Q5wUohwQEBs= X-Received: from wmqa13.prod.google.com ([2002:a05:600c:348d:b0:439:64f9:d801]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:400a:b0:38f:23c4:208c with SMTP id ffacd0b85a97d-390e18c8b0amr5646269f8f.18.1740746416493; Fri, 28 Feb 2025 04:40:16 -0800 (PST) Date: Fri, 28 Feb 2025 12:39:34 +0000 In-Reply-To: <20250228-export-macro-v2-0-569cc7e8926c@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250228-export-macro-v2-0-569cc7e8926c@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=4391; i=aliceryhl@google.com; h=from:subject:message-id; bh=lzB/op9EvTIfS1pnVWB0B3izr6Rw961OsgJ28NlS2cw=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnwa6i0KzBrUws4mz1R1VW1D9TxSn72HLiqHpJR ZCRZT3AF3mJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ8GuogAKCRAEWL7uWMY5 Rn+wD/0RyHzL949Lt17UGWI6WZmlRtRFdhXsWk8dwmUL3pTAjhMNHcSkL+/UnC0ieyheuZIskCq Zj9whzeOEdqIAvKuTybm4nrO3wlr9Ub2HqLOXnjVhG0pLJEgzOgI2+b849oWrWVTwA3oUXTvIaI LdWL4ZpXcv8ExvLxawdGYmKPshMkVytyRdqa8U77ea3ermzIeryGAJAIfOH7Lepic7ytK5s091O LgMpB/ONmXPnUtZ5hTfBt7670ADKL9u6TY8st/doIQDRtzQhlOl+rp6JRUrbQ83YhNnqhYV0rKq cPE3iYpkevaMaz178BE9yJBSyyGDmZGi3zGNUkcNvQuteKV5LvDdt5LkHeP6vq8chtkIRLI4v2u jbPpfgQ9TevWf6P7qjX+zLT2OPcujpJeQYL8i87KzswA2CLporYAycPNjeXGnAO0swAzTnkIupR pUIZx0cHok7eIB+aQMKbmTJ+ll4sQj5svV60Fh7+t0bsn27VvbBYh6gkGj4pSwrYUMPos4/4wmx 5qg8JPiN08hqo8Mwmvc56mROWbHnfhQ8+M8cgZLe0qg0p8HKJSahax6nqP+stenJuVsT0LxqDDy nH2pF0Q1rTPc2kSGHG/HXCeY2yrEhabXztuHQLggzlkGHSNZxp3L+8dk8d1eNSs/A7XhAjVsc1j FplYmT7vKagKAaw== X-Mailer: b4 0.14.1 Message-ID: <20250228-export-macro-v2-5-569cc7e8926c@google.com> Subject: [PATCH v2 5/5] panic_qr: use new #[export] macro From: Alice Ryhl To: Greg Kroah-Hartman , Miguel Ojeda Cc: Petr Mladek , Steven Rostedt , Andy Shevchenko , Rasmus Villemoes , Sergey Senozhatsky , Andrew Morton , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, dri-devel@lists.freedesktop.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This validates at compile time that the signatures match what is in the header file. It highlights one annoyance with the compile-time check, which is that it can only be used with functions marked unsafe. If the function is not unsafe, then this error is emitted: error[E0308]: `if` and `else` have incompatible types --> /drivers/gpu/drm/drm_panic_qr.rs:987:19 | 986 | #[export] | --------- expected because of this 987 | pub extern "C" fn drm_panic_qr_max_data_size(version: u8, url_len: us= ize) -> usize { | ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected unsafe fn, foun= d safe fn | =3D note: expected fn item `unsafe extern "C" fn(_, _) -> _ {kernel::bi= ndings::drm_panic_qr_max_data_size}` found fn item `extern "C" fn(_, _) -> _ {drm_panic_qr_max_da= ta_size}` Reviewed-by: Andreas Hindborg Signed-off-by: Alice Ryhl Acked-by: Greg Kroah-Hartman Acked-by: Simona Vetter Reviewed-by: Tamir Duberstein --- drivers/gpu/drm/drm_panic.c | 5 ----- drivers/gpu/drm/drm_panic_qr.rs | 15 +++++++++++---- include/drm/drm_panic.h | 7 +++++++ rust/bindings/bindings_helper.h | 4 ++++ 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/drivers/gpu/drm/drm_panic.c b/drivers/gpu/drm/drm_panic.c index f128d345b16d..dee5301dd729 100644 --- a/drivers/gpu/drm/drm_panic.c +++ b/drivers/gpu/drm/drm_panic.c @@ -486,11 +486,6 @@ static void drm_panic_qr_exit(void) stream.workspace =3D NULL; } =20 -extern size_t drm_panic_qr_max_data_size(u8 version, size_t url_len); - -extern u8 drm_panic_qr_generate(const char *url, u8 *data, size_t data_len= , size_t data_size, - u8 *tmp, size_t tmp_size); - static int drm_panic_get_qr_code_url(u8 **qr_image) { struct kmsg_dump_iter iter; diff --git a/drivers/gpu/drm/drm_panic_qr.rs b/drivers/gpu/drm/drm_panic_qr= .rs index bcf248f69252..d055655aa0cd 100644 --- a/drivers/gpu/drm/drm_panic_qr.rs +++ b/drivers/gpu/drm/drm_panic_qr.rs @@ -27,7 +27,10 @@ //! * =20 use core::cmp; -use kernel::str::CStr; +use kernel::{ + prelude::*, + str::CStr, +}; =20 #[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd)] struct Version(usize); @@ -929,7 +932,7 @@ fn draw_all(&mut self, data: impl Iterator= ) { /// * `tmp` must be valid for reading and writing for `tmp_size` bytes. /// /// They must remain valid for the duration of the function call. -#[no_mangle] +#[export] pub unsafe extern "C" fn drm_panic_qr_generate( url: *const kernel::ffi::c_char, data: *mut u8, @@ -980,8 +983,12 @@ fn draw_all(&mut self, data: impl Iterator) { /// * If `url_len` > 0, remove the 2 segments header/length and also count= the /// conversion to numeric segments. /// * If `url_len` =3D 0, only removes 3 bytes for 1 binary segment. -#[no_mangle] -pub extern "C" fn drm_panic_qr_max_data_size(version: u8, url_len: usize) = -> usize { +/// +/// # Safety +/// +/// Always safe to call. +#[export] +pub unsafe extern "C" fn drm_panic_qr_max_data_size(version: u8, url_len: = usize) -> usize { #[expect(clippy::manual_range_contains)] if version < 1 || version > 40 { return 0; diff --git a/include/drm/drm_panic.h b/include/drm/drm_panic.h index f4e1fa9ae607..2a1536e0229a 100644 --- a/include/drm/drm_panic.h +++ b/include/drm/drm_panic.h @@ -163,4 +163,11 @@ static inline void drm_panic_unlock(struct drm_device = *dev, unsigned long flags) =20 #endif =20 +#if defined(CONFIG_DRM_PANIC_SCREEN_QR_CODE) +extern size_t drm_panic_qr_max_data_size(u8 version, size_t url_len); + +extern u8 drm_panic_qr_generate(const char *url, u8 *data, size_t data_len= , size_t data_size, + u8 *tmp, size_t tmp_size); +#endif + #endif /* __DRM_PANIC_H__ */ diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helpe= r.h index 55354e4dec14..5345aa93fb8a 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -36,6 +36,10 @@ #include #include =20 +#if defined(CONFIG_DRM_PANIC_SCREEN_QR_CODE) +#include +#endif + /* `bindgen` gets confused at certain things. */ const size_t RUST_CONST_HELPER_ARCH_SLAB_MINALIGN =3D ARCH_SLAB_MINALIGN; const size_t RUST_CONST_HELPER_PAGE_SIZE =3D PAGE_SIZE; --=20 2.48.1.711.g2feabab25a-goog