From nobody Sat Feb 7 23:05:43 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 E4B351DC9AB for ; Thu, 27 Feb 2025 17:02:41 +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=1740675763; cv=none; b=eHLvHi6RonJgQxO8FHjE3z2u/wwbtVAa3l2M5EBod3xrO0bSH94L4+PMDaTgx8ZgrOaVgxPz/nbi21RXE+L76zuMOUuv+TePBeHansSu3fwoKvurAuRfuYNJIkfe1dtys+cvXtN5QZYprywjp0lv9qZ+gDgkqe1LWOV8n91Mvl8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740675763; c=relaxed/simple; bh=DX4aj6g1+3gi1MzmclI/m0bUFCVi048l6tZnNSfBMrY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MHqj7aW09MLVuhKtMUUkwaPVS9kwYnkGzkGsiteozJPfo6551v/GCKCKJqKsulg6p7Y3fkbRMEyJo1WHXeSc8iMfbaP0ZQdnik0UaNAQexjoYNweoK3rImZqfpl+fF2A9JJUykpjWx7nYh1ReawTzk6P8CFTryIjoUc/AnNRnPw= 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=UkKOfAU3; 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="UkKOfAU3" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-439858c5911so6011595e9.2 for ; Thu, 27 Feb 2025 09:02:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740675760; x=1741280560; 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=uyTw/awpQ2b8W4zrJVV8H3S2MlbDfUjPlgXrev/cyno=; b=UkKOfAU3uyQ3KJEpEbNuq3JVFkdb1LL4VA2wcSn/RRcDdYeg9n5SFZdk/pSZ2T8ugm bnsQanrfOlyp4eOzoQ07sYqIUEdsL5HcyXSH3qseZoKNzegrROt4D0zpOa3ECEOJ5LYF 8FZBynFl1Ip5f91Yr21G7ZQONGzVvm0TzzXD5rPh3U7WXrHQ/m+8Nlx59sUGrCp1sTPQ aaaIR0LzUcW6mIcpKxLpzd2P76rw+5BTUFc6YfkgDTCA1p3WzyYRHAwrQWoELJrHQOPI 15wXOwbS28dxndETNCdRo5j/xrSF7oZ1Ry431WK90Y8b7ySoOL6i1nGroLgbuK/wUP/e V9Cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740675760; x=1741280560; 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=uyTw/awpQ2b8W4zrJVV8H3S2MlbDfUjPlgXrev/cyno=; b=bg2GmMGW6WkKNJLAZYtc5EB8SUb4sqdtA8lLGaVXdkbkUDYs+E7h72swBD1dKDOdQo 0DUqKmLIvZmYJpNHhn9AYuht6wZDPzgNVsNuYdM94rGHOvOoIxI9UqdVdCB6m/9uIZSP srAwVrpa5KAszqlRWd2/3DqwvixaPmZyfPlMBbWbIif+L6k985YS95wAHxd7GQD0LdsE WBc3fwO9JKn+SyK7hTHqJOLxgXTfYNWhUXCBKbzpAVHhKTpiqdAYFfIc710aHIY/6WuF anFUD8W6NKadoG9+vEeFU9HFavqvpnZmIGGmpWSDw2mg7pwX6vwNCVXcPm3Po8GV6BBd a3Fw== X-Forwarded-Encrypted: i=1; AJvYcCVwhdM087YHEuWpWC42bHoZAFs5J/KNn/n1aHI6IN3uiAenavMribph8K/auIReUkCtsHA6Y6n5f7XNnxg=@vger.kernel.org X-Gm-Message-State: AOJu0YznjA/0LODQpRNmQZQ2XOpycSxT2TNd+8hm+jmGw4D/BHoEtQFw SYXBXV+5E4Il3VqZDN1ShaximXA6NTGLolc3O+cAqWhAIpT7PfzWi9OOXTbaIgK4w35pVm6uacL 0lFWh/N1MjCzfFQ== X-Google-Smtp-Source: AGHT+IETYd45wqLlFSGYfA2r23+tMfJyS7mFkY2/XSj51L1vJ2+M6lbfKeR3YEjy5LaJ4YSnfFn0fdn05icFoqY= X-Received: from wmbez4.prod.google.com ([2002:a05:600c:83c4:b0:439:8ada:836c]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4f0d:b0:439:98ca:e3a4 with SMTP id 5b1f17b1804b1-43ab90169d5mr68313845e9.19.1740675760403; Thu, 27 Feb 2025 09:02:40 -0800 (PST) Date: Thu, 27 Feb 2025 17:01:59 +0000 In-Reply-To: <20250227-export-macro-v1-0-948775fc37aa@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250227-export-macro-v1-0-948775fc37aa@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=1992; i=aliceryhl@google.com; h=from:subject:message-id; bh=DX4aj6g1+3gi1MzmclI/m0bUFCVi048l6tZnNSfBMrY=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnwJqpf2Qi8QLFv2OSIJifmJDT7I63pnqPsJGib 8/RB4+fSH+JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ8CaqQAKCRAEWL7uWMY5 Rud9D/9PV7zhQlnXuqvTuDYGJ1Hq8bZynWlH6JAsl8T6q78NM8tFf5f6hi8fs8YgHDZK0PJiCHC WFUgxUIf4kpzyTV/hwu4KMggH5sRMnfmNFzQJEFjtB0AZZ99shzceswR+Yl3R6TDvrpPwZhOvBT s70bBkW9mkmsR9TLjEzaNDIWkjRUNcQ30JvAlTV1Vqn1Cx4b2q44eSW0Xi5aiwIgSEoroVHbOkL rYLzkF7iB+HEQ5C2POOf0D9IJVunNMhgVfr6d5BavKOsvsBU4JacOybGeg7N8VxFkHuMjwZrnN8 Pu6NZWIaCE895cyrJIV+31NFb0HUZveAlqUsDaPFcqRXmqr9ZcAv1PllPbVFFDNtmtcJIiEg2Uy rKoO3uzNVjtvZev2qj3Sh8/C7xiR7uG53F1Vbgd+o36WLOG9yZ9ZwVu9LBTVMvbMk2ZPkyi+qgA LrrdvpoXtFSGM0Um9Dt+segnzH+EaQW0JLjKaEWUKHVOQFmj0hmKrBEj5QS4XMw2I7tVr4OtbTz fbrNsz2bIYOgwq6R8lakVGZt8txCmiAUYVuAkaJIjNnVLMNOyONgwYN8wNIA+IZ4jN6pOQhIjGv L/DlXoYP5ctDsboWl+CIK9STl/jyLn06rIexpwkPOed5fEIGAPcZQKEMKh7b0GxYFZLYR5gWkaK n/NrKkkl0ayaLTg== X-Mailer: b4 0.14.1 Message-ID: <20250227-export-macro-v1-1-948775fc37aa@google.com> Subject: [PATCH 1/4] 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. Fixes: 787983da7718 ("vsprintf: add new `%pA` format specifier") Signed-off-by: Alice Ryhl Reviewed-by: Greg Kroah-Hartman --- lib/vsprintf.c | 2 +- rust/kernel/print.rs | 8 ++++---- 2 files changed, 5 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..8551631dedf1 100644 --- a/rust/kernel/print.rs +++ b/rust/kernel/print.rs @@ -6,13 +6,13 @@ //! //! Reference: =20 -use core::{ +use core::fmt; + +use crate::{ ffi::{c_char, c_void}, - fmt, + str::RawFormatter, }; =20 -use crate::str::RawFormatter; - // Called from `vsprintf` with format specifier `%pA`. #[expect(clippy::missing_safety_doc)] #[no_mangle] --=20 2.48.1.658.g4767266eb4-goog From nobody Sat Feb 7 23:05:43 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 232D21DE2C3 for ; Thu, 27 Feb 2025 17:02:43 +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=1740675766; cv=none; b=q9yKzw8ZMZlxgdBcICUwRmhHZNh3e+D/mPpZCKBfJq1y3BAH8F2Bt8lao8yER4AIAf/tvkrqrOSs0eJDSshNwRM3clBvNFOaPG9JCElED732OKnRK3LjOPf+2YesYYBBsTypfHwL1/P/KMWAN53Wzb/gffa4ySLBXOVTIHrrhv0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740675766; c=relaxed/simple; bh=TeIglh23ISS4+Bdf8fwiVWAv4hzE5dSrVK2+5fzc9Q4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=N5kg7xjk1VR9zob9EooEOEWvacOi4Y5FNgbwQXdL6KrvCgRBY5YnF8Y0o2ruvhdumMtKSFJwqrVLc0CCzCG/NjvECaSMETdmuC0taabELfunxve77ojx4AlCoK633VOJXJi/8Vm33otmq4K1fD0MKqNW2673mENTjqRvZhgWPmk= 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=K+ECxKFO; 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="K+ECxKFO" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-4399c5baac3so9118005e9.2 for ; Thu, 27 Feb 2025 09:02:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740675762; x=1741280562; 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=42C/cfETiu38SYicrBEGEovTzMG6FpVCMB243TZ2o6c=; b=K+ECxKFO5xUr5F9klWnR/SJ9Oe/g2f4MQ5sh+qMwrcH5hSVrddFvjQoadaTcnlFgZU cuuRaIgjcy6FltLYYZ08708T4mlCZdpNlGiHcLPIec6g/k0U7WFdOhoSkekeJ+fl4RUb adMdgXNvVx3vF7doMA/mgMIxKuxjJZCluTq4aP8JG+e4SPel/WKWIHlAx7aoGOjbmboI raLjmle60N7Xr43orUyV6scZ0OvHxgTw2YCmvoEo+IkyFpHsDktJ5Kb/95y+CIZdTuwj JCUXwWFN1VtRtX4hO7rgl8uSPxOX5htleRBWRIeEcABpoMetKhEovnBSSNOOz/gLBamH vibw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740675762; x=1741280562; 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=42C/cfETiu38SYicrBEGEovTzMG6FpVCMB243TZ2o6c=; b=EbH00dG7hmoNkzy93Ez7LMxF6pdUPWvAQHdCC5UV8thDl5wMgiFx90vZDSy++eMgmz kNoFeV88u5N6ZK1vBhSwv6YNclQVfljdO0kMD4mekWmpb3yoe71eFI2n8xucwrs7Ltj5 JFW2LTt61TDNUnCYUOhDqGvTBTKSAFEhqkEvxEx2SZ2Q2W9l+lnrfyAFkw0oCrD3eeXa ESnXzcPuGqxxdPr8rteUf58HI7/BvxumeW51DFi3nnaRLK0JFzPZstOqv6e7T/33W+Rf g6ey/AfcAVZqau9KOSjCleZZhydvvIIUUZM2ulxEVpfM/mUUXWE+HE7eHv42eXOyY+yl l7oA== X-Forwarded-Encrypted: i=1; AJvYcCXlqfcC4vLkcigG04gRSkzscG8upoCzHz5ZNC1gHV7idfLV62mbbzk5yy11so8aF6iLRtL9qj6szSELOcU=@vger.kernel.org X-Gm-Message-State: AOJu0YzBCWMKoFLMYgjWMi0lHbWsxTuscdEenU6dsqFDDIcksWNbzb22 Mj3kdPxo7+ULWmmSHuiwtTJmEO0X4BWUUAYR8HcTQBjCtoiNAW9owb6pW7Kk158WQ5++LnkOfIa g7ahCWPX+L+nz6w== X-Google-Smtp-Source: AGHT+IFzLINQM/tozG5T/bypCONq44msz+BgD8zzYBfO02beaTjZ1yBkcJrZzgdsFFs0KxwRXhAakR4lmalPgDY= X-Received: from wmbek12.prod.google.com ([2002:a05:600c:3ecc:b0:439:948d:4146]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:548f:b0:439:9274:81dd with SMTP id 5b1f17b1804b1-43ab8fd1f3amr63472185e9.1.1740675762400; Thu, 27 Feb 2025 09:02:42 -0800 (PST) Date: Thu, 27 Feb 2025 17:02:00 +0000 In-Reply-To: <20250227-export-macro-v1-0-948775fc37aa@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250227-export-macro-v1-0-948775fc37aa@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=7286; i=aliceryhl@google.com; h=from:subject:message-id; bh=TeIglh23ISS4+Bdf8fwiVWAv4hzE5dSrVK2+5fzc9Q4=; b=owEBbAKT/ZANAwAKAQRYvu5YxjlGAcsmYgBnwJqqKr8/onOvuGjaOiYUGmqozHC77FCgBSok4 nPET3stwHaJAjIEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ8CaqgAKCRAEWL7uWMY5 RitPD/df0G8ivb0s8H1GZAyBpLj9Ic/1m5Re3XwiQcTT1V21Xh3S4Lvjue6PX/niFQsEJYdCofH kQDXIyYLK+u2jF7/IJ15TuvhMuSgZWuReLcazWLkvCU5bR7DXejkkfUxpMWngszVyo5Hy9/fdiS NLpxymgICMdUuomMdfTHaGLmhD2Z3B30Le9kQLo2DNkf108z9nFXGaPdy006lQnd0n6xyao8Mf5 LAo7oTuYw3kFAXPMBKyZGgWx/Y6cv9iD/3kBd6PQofrEbL7bITBQZulZ1VQEM+3lTnvmoz3AaHy HRxqJulYVZ2VKpMrOBQhbqLyKS0SzZ/D3LuwoOvhCzka2yNUxqyAjWVG1ls6JYCEqySN53mvkNy Ihtmwng10x2KiOFZtY7PGzNZKufO1+iIfJbc8BeiSvvYa6pmQoh79cO0FeRqkM2fjgzVcD39BDq LPYIlAJ2i1hVaRulVaj5Z+6Jq19XnPqNTfnn/YvkGFhw51yv6WsuGi6zRMMpuLhfWzkcpVDEOk1 tY5nLAftB3kFq2IGMfXHov2BEIWgzrORXG1keq7ij50N1Sf/zdL5hyRuVOsZ/hR0QH3hPR4PGXj ndhAjBFXZPqL10srqO8k/iuLFIwCweZ6UrLnJde0Ho/vv3rmhRAnAzElmOz5Iz4vffvRnaItlVn hOS4J2pFaD/02 X-Mailer: b4 0.14.1 Message-ID: <20250227-export-macro-v1-2-948775fc37aa@google.com> Subject: [PATCH 2/4] 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 This macro behaves like #[no_mangle], but also performs an assertion that the Rust function has the same signature as what is declared in the C header. 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}` Signed-off-by: Alice Ryhl Reviewed-by: Greg Kroah-Hartman --- rust/kernel/prelude.rs | 2 +- rust/macros/export.rs | 25 +++++++++++++++++++++++++ rust/macros/helpers.rs | 19 ++++++++++++++++++- rust/macros/lib.rs | 18 ++++++++++++++++++ rust/macros/quote.rs | 21 +++++++++++++++++++-- 5 files changed, 81 insertions(+), 4 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..3398e1655124 --- /dev/null +++ b/rust/macros/export.rs @@ -0,0 +1,25 @@ +// SPDX-License-Identifier: GPL-2.0 + +use crate::helpers::function_name; +use proc_macro::TokenStream; + +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(); + }; + + 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..3cbf7705c4c1 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,23 @@ pub fn vtable(attr: TokenStream, ts: TokenStream) -> T= okenStream { vtable::vtable(attr, ts) } =20 +/// Export a function so that C code can call it. +/// +/// This macro has the following effect: +/// +/// * Disables name mangling for this function. +/// * Verifies at compile-time that the function signature matches what's = in the header file. +/// +/// This macro requires that the function is mentioned in a C header file,= and that the header file +/// is included in `rust/bindings/bindings_helper.h`. +/// +/// This macro is *not* the same as the C macro `EXPORT_SYMBOL*`, since al= l 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 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.658.g4767266eb4-goog From nobody Sat Feb 7 23:05:43 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 32BCD1DE4DD for ; Thu, 27 Feb 2025 17:02:45 +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=1740675768; cv=none; b=Z9DXVeeCnb26SeFq2LLXoYIj67roOzHGjzhlU8xUZH/K3wnldCLUNeSkAWerUd6s13DOPmmrcjwXgoVg/15XsSGvoRbS5ErsGxc9A1vazPwaEAT7qBs4NFwLMMXsLshv1AXmF/wey8ZYp6TAuf9sj3Bb2n3MYtuuyY/M9xY0HY8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740675768; c=relaxed/simple; bh=WejprDMEIQUzDrArt2ooNXimTyl9N7VACCV0VzTpMyE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=EDOZ13aQybvakQGE0CDZkF2sXhQqOV7ftpCIBZMixKvADUzLmbOz4WUN+uyHeAUut2wgEoX3BeZdKH+6b/cviMdlA1pxk9WQ7mRlwYbC0/7RR96ECiSeOpoM3v5LmeEOJOU2aWBnP75FefHsmwtfsE5Q6rAfXj7Q1eWuLVSObmA= 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=okGXItWB; 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="okGXItWB" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43aafafe6b7so8355205e9.1 for ; Thu, 27 Feb 2025 09:02:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740675764; x=1741280564; 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=M18jgxT7tnSkTmh7oE3ekKDBcrX1GdNxU2BvtBQtbPI=; b=okGXItWBt24xfZXeNYRhm/oXMxoYGG/XUskY9gSyWWJ64dxDe+r0DV3R4Xf2A4z9MO Z6iXvj1tcdJy593AkfMqq2iVg4S8uxKYtd0WDiCTElGrOXIjBRH9QZmYOm1iGEWZHv6N 49KzHkpZ6kXRm6bSoM7ojnSkm5+hjgTjRwESxOb65brT5IrMbJCGfJnPOwS/0hk+bOir YrYW9vH1+S/TcLTgYYpdVrF0cXDWldgJR/gXBVSScKFZ1inVZIw4+8Jkw2DAYfpSgyZN VYpP1ytd+EHi6clLPfMWZ65pQX9KiX0ATYWBu/ehtv79gt7U1epYX2LW+YiWF1y4jXt1 2eGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740675764; x=1741280564; 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=M18jgxT7tnSkTmh7oE3ekKDBcrX1GdNxU2BvtBQtbPI=; b=AZ18mJe+G4ED2Oui8kKuZtyzFdihhbR17gDUrUe+IqbxwuVyzsgJGJh5Nb9+3Z1KHP GmoxEsnDtJBCYfCqtlCFojxc6TZBb8AMfL3EQtvpjD0ATffltCz7+CCMDESN4dCR7jQM Qwq5MfUdehK0OYzjLN9/tZrdzy8E03Ckk+INOAWoaZv6XTatJGG/qa7UyXUkGELr6HH+ gZH26em9tWyYDnbmgvP5SaoWHbblupHF0preLNuU/FOa72o/NQei6pQtGwLPDU6Shd9W QPXjRpDVX8dzSOeF1BxZgS3XFGjHYSAuhc4SVRhY3CWcPf9eluiG43xRyjBv4WS1Cwa2 MBPA== X-Forwarded-Encrypted: i=1; AJvYcCWCgOdkLaEyB+yDjtmhfBXgwzqup8cBSLulp+5SjZiHnZ6ajCvOFqIqmHTfhCJWNg6IvBqtmmLEpTSREss=@vger.kernel.org X-Gm-Message-State: AOJu0Yy6yqwiNhG8U5EiJFWuof8UCObtIkJio/OX/FMWNzCBpFy/DJae U2ObHrTO7fxd3ncrJjkeP4j+apXrcSWyiHm4tiRbTZwdW+4KPKWPCRvl0EgwF1AioYLX93borV+ 8TTz8PO9VKuW1Jg== X-Google-Smtp-Source: AGHT+IEphO8gIim2bcuv5Bf2tpO1swqJw4dD5lYIsIGMeeIuuhCWaHMPHVVzXCRHdiC8IK5JFxCh3QT+3OEKqQQ= X-Received: from wmbfm10.prod.google.com ([2002:a05:600c:c0a:b0:439:81e4:9ca6]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:3548:b0:439:8a44:1e68 with SMTP id 5b1f17b1804b1-43ab9046de3mr67646995e9.28.1740675764506; Thu, 27 Feb 2025 09:02:44 -0800 (PST) Date: Thu, 27 Feb 2025 17:02:01 +0000 In-Reply-To: <20250227-export-macro-v1-0-948775fc37aa@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250227-export-macro-v1-0-948775fc37aa@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=1987; i=aliceryhl@google.com; h=from:subject:message-id; bh=WejprDMEIQUzDrArt2ooNXimTyl9N7VACCV0VzTpMyE=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnwJqrX4yD0aRuTAEvksdQv/JdAcIIRxq40i3kE kyNPKhnnO+JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ8CaqwAKCRAEWL7uWMY5 Rk+KD/9OHexlQDh/sjs/LljOilHmTVeqSEG+2/0LGuddjP7sVfDu0w7erVLX6648VhTWE6haCcF g4TShRiclyduAp0aO10vRk7Fkji2A9jrcO72I3XNaxERPg1evCBCdGpki/jZA+LVOC+Y52cp4Ed r7MLCXFZoBEvRB0uakhQZ0rVRlrm2Ivy87OTSFGmeig7jSUesYHNKZJ5r9Lh5afiGZ6DhEO0qsj syioBHPS3ebev2YwjRE1u/1DVQVVhtL2bnRCAm58Du0vf+jhgjksMixBOF0P7anJPaRrigNfbiB zLWFdzGj6Xz11k0tRWq1ZaZDXhIdbvHCrc/tTTS1aOVGiQ562ZNG9FpcqOlEalFV31KqM+8B0CL AxNwRCvIei+OO6ipfX3C7QcnxTl+VbVzoYhC7w0Sp72ZaCxMjrZ9tZ3Fk+nO56aer0joyjrN5ks HI7mJCi3lBW1xybgGuW33YLefNVF9sCndati539VaHz8dKk4mLsa7DI+MCPumLONmhRj5Gs4IAg 0FkpT84g2ZjvDeLh5LaBnU6ZFed8h6UbYUZ9LkkUSbTLK2hpa2Y/GDG94Wz7tTM43AoMquWx9rz WlqcnzFE2KNyq0k2D1qvj7RTz0EDRgv6g72CM1XnEzdORX88UCGzAtljJqx8QR6nMqMKuGPkDL4 rZjpIYrju99OoRw== X-Mailer: b4 0.14.1 Message-ID: <20250227-export-macro-v1-3-948775fc37aa@google.com> Subject: [PATCH 3/4] 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. Signed-off-by: Alice Ryhl Reviewed-by: Andreas Hindborg Reviewed-by: Greg Kroah-Hartman --- 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 8551631dedf1..e1a5ff3f34a9 100644 --- a/rust/kernel/print.rs +++ b/rust/kernel/print.rs @@ -10,12 +10,13 @@ =20 use crate::{ ffi::{c_char, c_void}, + prelude::*, str::RawFormatter, }; =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.658.g4767266eb4-goog From nobody Sat Feb 7 23:05:43 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 6C68A2309AF for ; Thu, 27 Feb 2025 17:02:50 +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=1740675772; cv=none; b=fLJMBbnNYDWxCic3BSekx0bNcj1Vj/PGyDSfAzHpPOUpXVJKOvXQ1nQGcvOTHc5dRs0rT2GEdx0q2cEx2mtZYLf5lHlg8L4DsN6EWuVJ9VmQ8snqPI+/yFF/2dYs7FErQTDf4R7sZfYjP+WzQ3tJuwwkJwoJPgpRco0CkB2iEUA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740675772; c=relaxed/simple; bh=S/CadH3DvJMHRT4OjtgeHq3HpKEISiEOD3rbSMx9ntQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jYgvoj0Ol3eQyMvyRXA3kVOMkEzElJAamMAh8dQ892opoCMm5ZUTS58caRciJ2fr2MclZBnBqHi7llhB0jDxHxA+1PENuo4EvLV0oES7C1QVWK82ffLgvF4QkvSdUWUnXJFrDWNeONxG9AE6ysQBaujhaWWWxgg4fSEG7Yycdf8= 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=RrqwkKI1; 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="RrqwkKI1" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-43941ad86d4so6468565e9.2 for ; Thu, 27 Feb 2025 09:02:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740675769; x=1741280569; 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=8s0UWUw+NP4XU/nMlgmR6Pxopak35wQY5LWhcc4hBQ8=; b=RrqwkKI1jezpTI/29BINLYaXl8AMXrDnF4tVuP/Ert5SFb4LpIH9ywR+07ILRrqDcV A1kRC8RYgMMeDUxipeoKIso0nA9HLtvn3/DcpT/SYIDNW7iYA75XeIkDGK8QlZM/HC+U W+LH/BwwbKbJSi+tpe1La4SzRAwGDWne8LCxiXa8j6BXyVJv2ghYDRDD8qXGY0UmKfCK CJd6YGmF2DEGNuA0FbONbJga4B83WmsIko5Y7aJe1/HXBJqdYfxRlB98RZXvUXoW5EUo /HEpc+CMXicdnHrgYnCLJOm3eeE6bqWIYl/3fBs8M7fQWh4hwzhxaN2f0Us/pRarzjTG vgng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740675769; x=1741280569; 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=8s0UWUw+NP4XU/nMlgmR6Pxopak35wQY5LWhcc4hBQ8=; b=GoPWO1RA3RvuduukBQIaKfLg8v+6aU6CZ/2sWHD9cqEEBSw441J7lNah/+jruy15YL cz90mkOkc+KJzHde5Nf44omE7I5yl0tWibB9s7LV6TQpoNZ+9gX3ECzUsyZnGi8EmI/0 RGGET3x9KTbKK8xxEnbwtm+Y9drWDwD0rIHVTg6jGLnD2/+EyvKk7L0fYf1YDoXhklOP 1YxlOTl6iEoIm+LOhZyV3slE/UGTq6w0ebypqOEpuH2L9YBwyBKSkwr9PuXHMkSeGiX4 xJLdDQSFQaFt3J6w/V0lXk0KWJVauuYUgYsKdPdBwsJtE3j8rylwFLZeKlzvdrpkV4xn 0KWw== X-Forwarded-Encrypted: i=1; AJvYcCUoixcW5xRFCHYnLq8T/5s1SjgTKXppit8YLZI50HC35sWQf58rbhCHUABWhlOjKHre8pKSW0QiMzOEjqA=@vger.kernel.org X-Gm-Message-State: AOJu0YxsJfm7HH8iMB/LG2DwbpyEKnv6CX6xMeXoiHU67UT3fXZFIM4d fHe0KIPoUHALaF4FRermmCmiycji+h1arWnKLYPkfRur0hVveqGuVRi1gXuDBH3gmN5PuIRLP5U IbwWhVKhtLYlHwg== X-Google-Smtp-Source: AGHT+IF2Xw7X7IQT2rkcDypOfKFxDqjIrX2qnVlYKcqy8xjIfF21Kp4rgbVw3IFZsGDwNrVp+GAGk1g3IkDcgqA= X-Received: from wmrn38.prod.google.com ([2002:a05:600c:5026:b0:439:5529:c5a]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4683:b0:439:8523:36cc with SMTP id 5b1f17b1804b1-43ab07ab212mr130473315e9.11.1740675766999; Thu, 27 Feb 2025 09:02:46 -0800 (PST) Date: Thu, 27 Feb 2025 17:02:02 +0000 In-Reply-To: <20250227-export-macro-v1-0-948775fc37aa@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250227-export-macro-v1-0-948775fc37aa@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=4336; i=aliceryhl@google.com; h=from:subject:message-id; bh=S/CadH3DvJMHRT4OjtgeHq3HpKEISiEOD3rbSMx9ntQ=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnwJqrjNOcllCa9cciW+U2KURIxpkWZ2ke2YQw2 eOs1U8wRVqJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ8CaqwAKCRAEWL7uWMY5 RrfjEACiQeNf/FN/WO6ae595R5V8X8mlq6tDi2z14jscIkTR6tdD4hB/GYZ8LqguvjmV66eWRr8 hp365vPCtX8J6i8ip1k1500a0m1MOrFZMdSLEm1OOEQZR1mAK5COY9B8dNWwPGOuiNCWFHxZJgm pXdbJCX+hCjLoXE8ffYnDdesNFpiF2PykKFcT3rq5pt3fkgVsztN1UVf/70455og0U2lVlkuA8m +jEslSYIm150xzTKA/ZP7cNbG4AxiME4FE8MMHX2PWY/NH46PHbhQK44+wiMmzBjqrrWxRHzll/ Chfyy+ijKbNNsosJmTFxd0UtSV3Ev4HHtt89WUVb40vynVnOeNcIWdDU5T2m5Fjfc+Coz6XLl78 789yncBz0QE4U1IPfMl39G29+j+6O6djWyjsjiFjP0rWjXEAbm0cXxBgTAf5PMttLGfeWugmBXK 0i7HI5yY79+iaoyLnrd34l2JGXq7h3PQXTg2fp+BAWUHAN59/PEU6vof9qH2hvyjYY0UF3wtHwn +lSCpiVyZGe+hh7xnb8T0+bQhen5styxzN+f3ogFnvWojl3D/FcZbTpc0yVz3OeiFrjEyQ04F9p s9YMfmxo31ssqCcXPJm4toHCJ2NVRtdsUusbsQjLc2uzIeCIDUUkTQ+ZQNDhaKV/9+XAm/PJiad fl3aahhNhEiEwig== X-Mailer: b4 0.14.1 Message-ID: <20250227-export-macro-v1-4-948775fc37aa@google.com> Subject: [PATCH 4/4] 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}` Signed-off-by: Alice Ryhl Reviewed-by: Andreas Hindborg Reviewed-by: Greg Kroah-Hartman --- 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.658.g4767266eb4-goog