From patchwork Thu Feb 27 17:01:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13994923 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1F90EC282C5 for ; Thu, 27 Feb 2025 17:02:45 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id F096710EB41; Thu, 27 Feb 2025 17:02:43 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="uIZ5Becu"; dkim-atps=neutral Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by gabe.freedesktop.org (Postfix) with ESMTPS id BB95510EB41 for ; Thu, 27 Feb 2025 17:02:41 +0000 (UTC) Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-439a5c4dfb2so6046855e9.1 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=lists.freedesktop.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=uIZ5BecuP/WqOEWvs/mzlRgMbYKo0vCrFKMDz9FOVN3bzrH+c07wb+hIf/Xd2/JaA6 fdNj9GaNn2pTHvuqH5h+i6HSlfGnx/eBCRCeLyGsCBTS6YcxhMtAKWUvECidjffhqRYJ PNwdVOahDqQsroAKZV3+SSM5T6E+7iYPdGHJEASSb2TU7cypw2/zZSIk2cSJ3qgLSFR6 RXZvjVFbxsMFmDzY3NdWj/hXm6sBI8EaJB3uB06g3Pwr5NxLcufEYom3Dh6Z08j53YR1 gD/X7sancnAI1MbD6WxeDkst9k43kZvmo+/FfSyvRgJKyXaoTroLQKboB6ITCWvyfy2C Uzpg== 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=raH44L+Enkn2KGpgAGQsAlHk7Tpsa1F6dZSSEHtP2EbuHnx/omsJMzO4eGpa8nlRPW nQ2eu63mxmeJPFNFwn6n1/Stipoi2FXjVwoZRO9ZBqytKotro+BMDCrKlDak7/qD7i0D X8kcVLTJ2xDIckWbfIwNDpgEW6Q+xvvkYcecCwGoe+xWX9DEdF2bNlo4jvlJfr+YiNgG N0q47LCERRGWgyRubtoEHUsJPs0oDUmsfR3Rvus0JrbAI8h9VFDTo4hl9TKOF2yROuHx q08FSVSmwpg5OupEySvcBFg8vp29E9TV211BuIDYD8kGPxUUuj/U0DuSiGjOR+jn8+TP Z1NA== X-Forwarded-Encrypted: i=1; AJvYcCX0pf8qN2curBkqdWjYhudVna6JXPu7Qi+39bY5F8pdKNt1d81IPtan4RBUwSTMMPAvamLiBJ10JGU=@lists.freedesktop.org X-Gm-Message-State: AOJu0YyY2Bmbag0KMlDIkj6rSpbyORT8P+m5cWJqaffrJA3bL3AlRm4y gsqqxBY6TGxJGYjOO1J7AqRd4lD2p/5BQ9k2Tn3xnzjTFqYLy70ybx9j0avl/w1/xMH18Y8BphH eZ5SHlnoxqUuO1Q== 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> 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 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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` | = 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 --- 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); /* 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); /* * 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: -use core::{ +use core::fmt; + +use crate::{ ffi::{c_char, c_void}, - fmt, + str::RawFormatter, }; -use crate::str::RawFormatter; - // Called from `vsprintf` with format specifier `%pA`. #[expect(clippy::missing_safety_doc)] #[no_mangle] From patchwork Thu Feb 27 17:02:00 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13994924 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 45635C282C1 for ; Thu, 27 Feb 2025 17:02:51 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id B63E310EB44; Thu, 27 Feb 2025 17:02:50 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="neqSHVKn"; dkim-atps=neutral Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by gabe.freedesktop.org (Postfix) with ESMTPS id C855E10EB41 for ; Thu, 27 Feb 2025 17:02:43 +0000 (UTC) Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-43ab5baf62cso9286035e9.0 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=lists.freedesktop.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=neqSHVKnUsDmij7yhZqKUQCf0JXhnL0X7IgdnCWDRIbIi/H3km++lLsV9CWN1CGgA0 2m8X5T+G+zyWt5B90wipJnmSoJ35porvVO9a1ZX3CyWkgu6bcfV0NIlFaeZhwsoufCOd ijXdjjMjvvjXYS2C/s/dcBI32u86v3l1hC+QDia+2HUoIu4uW+vitCsKa+wDvJSTq2lw I2+Kx3YwSqWsYmLnVUWZtvwnzJxLDHVJrDU/alEThFRvtbohPOi67Mwwoc84UUlqvTmO +2fu7c2CZpIRpYhwdrbvFdTmamglSuKjvvhFN//WzgTBxF1UUzi3xgsZKGGsb+LPz6KC 320A== 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=aGcjSj500gBJp5cd6H9aAh2v9BDMZ+yJVgaOBxQCfN0uAtLP3m//16DLZxUzixN3pd 6sJztmxjirnaoArS7EWPOW0f4IOxn9p1Q2rTINDyeNzj0hkeoipSQHgLEmcoqi3EQcfM a71L7pLKGMCdfRDIM2+i90ixrtpjXJ65OYH2vzcpaSTbEA/oGa11Iqa2J3u4o3eVKhHW XIlLBQrqRMlvTSzhA4Xgj/FcxJ9+JVoYbQoOQIvL7yjOjM9ZiR9nOEDXvhxljrHB4Ecg R4K87Dwdkaj5iJtBbX/2jRNG0685flFn7WbUIKIvLHtIK36WbGnfI3O6+zsZdPZjJx1v mMrA== X-Forwarded-Encrypted: i=1; AJvYcCUCYi9SQNKjzBnBG/bZLyWC28XS723nN23aYZhEuhrsXoYSVQKIqorp7Yy7Zf0HuS+8LVGNJUr3Cco=@lists.freedesktop.org X-Gm-Message-State: AOJu0YwS7O0fbELnytVXntK10QBCQfRp2uKr1PYTjBqTpIubx/ywN78G AB1MQEzO1hOad7/uEr/gOJV81O09vKOqj+BaFjzO6zU/ycsA6yy2CPYzqMFLDMINCDJOr7uvYqv vYcSy13uyL8f3MA== 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> 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 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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` | = 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 --- 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}; #[doc(no_inline)] -pub use macros::{module, pin_data, pinned_drop, vtable, Zeroable}; +pub use macros::{export, module, pin_data, pinned_drop, vtable, Zeroable}; pub use super::{build_assert, build_error}; 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) = function_name(ts.clone()) else { + return "::core::compile_error!(\"The #[export] attribute must be used on a function.\");" + .parse::() + .unwrap(); + }; + + let signature_check = quote!( + const _: () = { + if true { + ::kernel::bindings::#name + } else { + #name + }; + }; + ); + + let no_mangle = "#[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 -use proc_macro::{token_stream, Group, TokenStream, TokenTree}; +use proc_macro::{token_stream, Group, Ident, TokenStream, TokenTree}; pub(crate) fn try_ident(it: &mut token_stream::IntoIter) -> Option { if let Some(TokenTree::Ident(ident)) = it.next() { @@ -215,3 +215,20 @@ pub(crate) fn parse_generics(input: TokenStream) -> (Generics, Vec) { rest, ) } + +/// Given a function declaration, finds the name of the function. +pub(crate) fn function_name(input: TokenStream) -> Option { + let mut input = input.into_iter(); + while let Some(token) = input.next() { + match token { + TokenTree::Ident(i) if i.to_string() == "fn" => { + if let Some(TokenTree::Ident(i)) = input.next() { + return Some(i); + } + return None; + } + _ => 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) -> TokenStream { vtable::vtable(attr, ts) } +/// 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 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 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) { } } +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 the current `macros` crate. macro_rules! quote_spanned { ($span:expr => $($tt:tt)*) => {{ - let mut tokens; + let mut tokens: ::std::vec::Vec<::proc_macro::TokenTree>; #[allow(clippy::vec_init_then_push)] { tokens = ::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)*) => { - let mut tokens = ::std::vec::Vec::new(); + #[allow(unused_mut)] + let mut tokens = ::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 = $($tt:tt)*) => { + $v.push(::proc_macro::TokenTree::Punct( + ::proc_macro::Punct::new('=', ::proc_macro::Spacing::Alone) + )); + quote_spanned!(@proc $v $span $($tt)*); + }; + (@proc $v:ident $span:ident _ $($tt:tt)*) => { + $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)*) => { $v.push(::proc_macro::TokenTree::Ident(::proc_macro::Ident::new(stringify!($id), $span))); quote_spanned!(@proc $v $span $($tt)*); From patchwork Thu Feb 27 17:02:01 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13994925 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 87ED2C282C5 for ; Thu, 27 Feb 2025 17:02:52 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id F1B2910EB46; Thu, 27 Feb 2025 17:02:51 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="06s7lNKq"; dkim-atps=neutral Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by gabe.freedesktop.org (Postfix) with ESMTPS id 022F410EB43 for ; Thu, 27 Feb 2025 17:02:45 +0000 (UTC) Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-438da39bb69so8444165e9.0 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=lists.freedesktop.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=06s7lNKqmEJIzqm1oq5gwRRM9tqte9wrKkX5AIlr1mpTMx2hDbHWLcFJaE/ldtp/D2 f3NXustLThbD69GIvxCrXA+RexgbYIQEcoZLfgZuvNlfTdwki/CvzJ54p0Q1EiHGtThI hgekYU0t8+1gb/GmcDLrDsDHQnYDfVOkK6UD5MgFHPQGt2MCbwUQHYXQO2fRHRwbG+SU KWnEk9GjHCDKqetN0mGbDrQPY7w88ERF8aPHRIaF4Jqf85mr0QzisqakNUimPhQD3zza fpdfWk3Ciw3jY6wg7+T3qnmvjlmPyN+FF1lB5VXvHkKWW5686pv24p81eHXnfjOtvqRc K/0A== 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=HNsHLMlYh0oBxUMXIdptPg3WqYg7g9wx9WtQDGtopmYIVw/+N1Ta2TYrBkhfocMU7O UiE2QFgflx5yhd4n3DdiGrVO1LY8sL69buNPcfTBOlw+SGrWRm/10tmQ6WTAt9U8gROJ TCSWgHZlciLxRRBEcg0Fj8I7lIAK+wnKacjQeOxf6RwV3UUryr8peW5zHaLSLcq6fQC2 zoS5GGNp83zmidM8SXePBnOzzMUdGLD9L6x3t7dP8byrBRcosXIxHc3m1x66NrAD1+1c k7nFlUFRamRcnhtA+2OK7xXuiqU7CAC/nO/VumKgbhImH1CVGDK0Uu6kfPYv+2Gi67aN H6Qg== X-Forwarded-Encrypted: i=1; AJvYcCUEvmG9ezMKAqL7GfLpYxYa4y9ZgpMsUovYzwziD2Vm7ddVjzcp7pgSObwfaYoyudH/i3zEHBpvkJg=@lists.freedesktop.org X-Gm-Message-State: AOJu0YxoJwwsCqsjfkoXYmW9PV1LfzjaXjmCuochMDLFFaFUtRUE8Bcn 98I1yL2UuBvzPg9eOFzBsoav5lWojQq5UPvNmydFJmjp5yD9BCTC3lwx9BqQhXrJmI7ujZujeoO FN0GmZwUl6PvgRg== 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> 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 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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 --- 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); +/* 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); -/* 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 @@ use crate::{ ffi::{c_char, c_void}, + prelude::*, str::RawFormatter, }; // 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, From patchwork Thu Feb 27 17:02:02 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13994926 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0D802C19F32 for ; Thu, 27 Feb 2025 17:02:59 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 83ADB10EB47; Thu, 27 Feb 2025 17:02:58 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="p1UF61cR"; dkim-atps=neutral Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by gabe.freedesktop.org (Postfix) with ESMTPS id 3C1BD10EB43 for ; Thu, 27 Feb 2025 17:02:50 +0000 (UTC) Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4398ed35b10so6518725e9.1 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=lists.freedesktop.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=p1UF61cRBURE0H1pnfcnVmsS/wpEZcXHPBCLpbc3vPUjw7L3UgmbPwODLtEk4ZEGd9 DRIGjSY12CaFeFQ0pqhyv3Pff14d98aAV+gELcxz4lrhDRPLmPeJ6/vke7x+g3Lb9jn1 2K9UIdlX+2QBI7SNdy6GTsx1Sv4xDRMPxGRXueby90SxP9RybKaaejExzGcAXgieD5Bt avgdeM9/qbswZGtP/NP0HORj2ufWFw+IutvMRpH2lTQnSq64wY8Rm+QGFhxarQW4MhIw UOQUTWHJscG1nN9fHPbwjZlsvgz8yxOYHo0It9nih37LHpSCryJ8YyyYTnPkzfd5m/d3 nilg== 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=l2vNPgjstGvG2RQPNkRs77qjUzt7HDocHA/bcbw9wcTocY7koX9PqqOvyXl7CAFP6H UXlyPnxNziUX5PAOIpOQssmxQQNiDofzMdZrosXrv56Q4yIiyP7dEeAAmhP4hUMGHfHY Npmchki0zp+2/HvN3/VXJjEpeuLp73YSLRI53gaZVgrt34P3Rgw03UxZ3XBB+SjSTqxa U86dXWL3sBiTWGNMdAi8e+SqYLAvS+vqvXStcQjk5GbBllx99/AU2KVeaO6NWN5PA2W4 l+fdpkxQdkKiAsYEuYyFDJ2wXyh2D9W4k6+6BwgJHXsVdxrnRMIWwYzkVgrJKhSWUnSA BcuQ== X-Forwarded-Encrypted: i=1; AJvYcCVkR4L5t1UwG/P7gZ3YeWl4NdTppFKWi07DkqB3Uf7A7+iN101gIYvO80qXvdJUVH2ex2QL1y9AcsA=@lists.freedesktop.org X-Gm-Message-State: AOJu0YzKeoxh918mX79yGaGxVTZ+OB1dsXDlmJVYg1dY90V5hRqtOwNV Ie/IVpxGIKbboDe3OKbc3aK91drk4USRunhNB0AAn3JWh4FIhsjdYKTR0e7F5bhJreFf6gcUyfp SDI2QNlfELDRYMw== 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> 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 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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: usize) -> usize { | ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected unsafe fn, found safe fn | = note: expected fn item `unsafe extern "C" fn(_, _) -> _ {kernel::bindings::drm_panic_qr_max_data_size}` found fn item `extern "C" fn(_, _) -> _ {drm_panic_qr_max_data_size}` Signed-off-by: Alice Ryhl Reviewed-by: Andreas Hindborg --- 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 = NULL; } -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 @@ //! * use core::cmp; -use kernel::str::CStr; +use kernel::{ + prelude::*, + str::CStr, +}; #[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` = 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) #endif +#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_helper.h index 55354e4dec14..5345aa93fb8a 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -36,6 +36,10 @@ #include #include +#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 = ARCH_SLAB_MINALIGN; const size_t RUST_CONST_HELPER_PAGE_SIZE = PAGE_SIZE;