From nobody Sat Feb 7 22:16:11 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 C40241EE021 for ; Mon, 3 Mar 2025 08:45:50 +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=1740991552; cv=none; b=J6kwnaSqZ6y8FLOKOOSJeAyYdKrRG9jPSqdXzHUsfXOsZEB2tf02AEb87gzEyXuKH3pPk9irpD+lGDqE9E735MXwkBIaekQviyH710bvFJcjo2teFE6VJmC9dXyUsWAdbwgHkHQN/54DNBBNAhnwXttdkMozAftb6pMoWLzCJa4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740991552; c=relaxed/simple; bh=rXOlJP2KwbZg6HLcbrBJs+P0GV/Go4oAq5PQVguOe0Q=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=N3+D8xqymy2U6HNs9rwpHFxEY3l5g9GDGaAQv8Sp4vFFRUZA9oFJBdVvMh7idvAjlMhVDmL1e18TSYRRD1B/3nF2VlcIBBqLIJliVOSLv7tjrFcq62F75+07905qoE0CL6bRo6m47g3XLct4fMsTBp7SVYM2vr7VouWBjgSGwZU= 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=Zd3xqMn7; 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="Zd3xqMn7" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43ab456333aso35869445e9.1 for ; Mon, 03 Mar 2025 00:45:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740991549; x=1741596349; 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=Uszf9jNzimaGC7A+MjcMbFfOoksGjtwuorOL4my3lD8=; b=Zd3xqMn7RXIz8sj1odvqq8+CEJsOSoZJLNB2Mk0FOc/AxFdaA9dcBZsR0EkADQFMkl zBGexVf5TILZQrfxMeJ+kqaSG65a29QjrbdUMx79U3PJvJYnSswJDDjZr5lNhhsHFe8b 5RaX68lichSToLh+xhpfQnx9h2vBC0QTEP/X04Yk8u1xqVTkqtZRGsb80q5LjSyDiycI /5Lop8+qSjn0lQaJ/ByD7hfVTBv3Usb8KsW01VuXabMGKnQw98kUC2VuQhSWl+6gbmN8 dvhiUbflt49sJIkGrtYgLXXp4nP9PODnfUPzTN8LsE6cEIhvLV+ceG4u9Td6KLj528Qq oNTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740991549; x=1741596349; 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=Uszf9jNzimaGC7A+MjcMbFfOoksGjtwuorOL4my3lD8=; b=jLbTTQ2srSpVooSza2Dj1n5UFBruK3krjpU4sM+vGUE4iere175JfsLioQDDKSerlh WKGS/ntdg6tkXfyrXjWihMNKoY3JqD+agAIL5UjCTu4cOi/jCwlEIBM7FgRV9Z5AqHfa i0k0equ5LXxEDUi8kOe9M19nsaOW4u+/LBf7H9ccDf94FCpSKZITgJ/YNcUV6yG3m3z2 35tXhpW0uRBXbfoqBbQT1dqf0ttXrqffIKvDiBdLF6VPj/RLss7i3a/lcjax3Rm/+qrx 7iUJjvoX/vIW29nSEJjWUtMwg4lW/q55j3d16q0xCTvpuB7FT9BX7j5dsPm9mSPbZV/2 LdWA== X-Forwarded-Encrypted: i=1; AJvYcCWcyDUdJxIJXRFLvyqe3C6SPBxTGrEg1WHOq1Fopq63SqOMj/GRkDbwIv9zW+1c3EDGg/VLu+KvUgdOBaE=@vger.kernel.org X-Gm-Message-State: AOJu0YxaK//drlLFzFqJMjYQ8rQYKnrV3oPTHcDjiz9B0Ew+sUsWn523 Mk9m8bSJjuiaXsRW+VAlAVG+KmIiX+SUUarxNct/F1gqIrf6MkwbWPoRjpDfA04yYyLJyROT2OL hJ9jS6JgiA6DKPg== X-Google-Smtp-Source: AGHT+IECCbQDp007YY5H9TzV4VTf2BNZ8iJKkXWl3w/rGfL/ku4+FSGRABIL2DQ9M+TCDMAM8FaLElccEauucog= X-Received: from wmpz20.prod.google.com ([2002:a05:600c:a14:b0:439:80fc:8bce]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:3ca8:b0:439:9424:1b70 with SMTP id 5b1f17b1804b1-43ba675caafmr134644415e9.30.1740991549163; Mon, 03 Mar 2025 00:45:49 -0800 (PST) Date: Mon, 03 Mar 2025 08:45:12 +0000 In-Reply-To: <20250303-export-macro-v3-0-41fbad85a27f@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250303-export-macro-v3-0-41fbad85a27f@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=2238; i=aliceryhl@google.com; h=from:subject:message-id; bh=rXOlJP2KwbZg6HLcbrBJs+P0GV/Go4oAq5PQVguOe0Q=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnxWw0XCXyKCpx73jSQ2riTrt89M1w4DSLnlZ1+ Cb235TozJGJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ8VsNAAKCRAEWL7uWMY5 RgRRD/9PrC+8ReqGY3MVcspxlBxm4PzYImTmwadXxvv6K8NDFtqSgszoxEfNsDJXIFFzYRHYa+N mJdl98HU7r6JRSNL6VV/+oRKhnoHhYbIAna1gFnE/RYTvvWwyUeyO/3CFv7nowjzYM2AnjxOWtM OOmg43zFRY6b/s1XnuR8F/qR30rygGjeSdK7pbE+PygOwoeoXMLh7Yr/UQ0him2uoJXBP0F/4Jt NhUEJHPhJV2Uts4nr2e42HpL35fnn8SH+eaBo+nrTG3wXgOEK3ItExaFsD8x6eYCRN9tONisJjH xBz+9+5gzepyskX2bOZ0ZhqRPhCSdiZBHxPdJAqs30uzVl7n2MQW5GzBrqu06sCxQ3iY4km60MC QkK3CQsguGeSvbMPuYpoIKd+Ld7jxqM27Pe8XJHI2H+nVz+/C5Ps7rZriN2QV4HCqc93vLTtGpB bfT33fQo4J5G/ediSmFwtU9NbUEyDlI3/LNY8azVt58TysT3PPWfLU/jtyifkjgDeFs1Tzwlv58 AoTz7LeIRR8/ZK/Y4q/Nq9NF8+vnZf9gm4eXxu81s7bnfqXUiKc/+UoLzwpoBElajRnbah8+nQ/ 6A9eT6t+gCWE1hGbbP3vLG1gJgVICGT/4nZEhu8Ujark/AVtP1xoIh0B/sjLHEAQDPC8Rc5IGkh NtML/q9eIA2gOFg== X-Mailer: b4 0.14.1 Message-ID: <20250303-export-macro-v3-1-41fbad85a27f@google.com> Subject: [PATCH v3 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 , Tamir Duberstein , 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") Reviewed-by: Tamir Duberstein Acked-by: Greg Kroah-Hartman Signed-off-by: Alice Ryhl Acked-by: Petr Mladek --- 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 22:16:11 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 DC4921EFFBA for ; Mon, 3 Mar 2025 08:45:52 +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=1740991554; cv=none; b=ArYxpPKS2PkK/9ow2YtcfkYttFIk6RYxBpixZa0oqdCfOkSZtq55Jlkw3+oS2B0Q0Q2Vrm+d3JHBLDaENOo5+0T+ofnNUj/Ul9mUgI9Y3bLjnpxHoGorEGMF/LU2JMlnobRej6R15PWtSw+5wAmIRfc6elK9bDDd+sr5M8RjbsA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740991554; c=relaxed/simple; bh=GN9iRw4vP0biBC3qh1br1TYeqEjFY0JaW5FPcfgG69g=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Dd5l8XQUbCmPWMHdqrruwLXlU9ZHqFoXNhbP0OE23p28DbIE7+ivTGKizNPsyH9ygWyG+cXJ8fNSegeWlRfX8fm+4Ky3VfOEwU4Iz1sidEerbJYPOquALXqjJVvc+CzoOLnWCNuYrr1T4zPpyBSvmp/eHGGUcyDAFka4C8LlcE0= 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=UQjX45Bb; 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="UQjX45Bb" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43947a0919aso36022885e9.0 for ; Mon, 03 Mar 2025 00:45:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740991551; x=1741596351; 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=Yl6OXiLxc2DNrX6mWydkEen+W3a+U6q5HOC8/Fd7HQw=; b=UQjX45BbmOuzFCwAXHzsrAeP+DcZZ94uw3I8CFSpzW6Nq/RXU0H9rUrG91zqqtos93 K6wRZisMj36oWysO7sKlB8iGU6AWoOCfkymZy0TgN9cqbEIzSFCw0nqMxMlFKn8Av7PU 3K+mbF4uMs2+RmYnmx0WqrZ+slJbxrlX2hB5u8tM5QQ7XbFD3hh0IjxJaOFzdvzEBF4l BvRfB51Cf+2l0zXZmpibI6qTSw31zMigq+9ir1oO2/egoqS0pmqwrqGB6G4nwXhTWZy8 AGxtSRnlUUa8iGgmEuvCDArUYdyhW/TzHMzI11vZcL/geEMWRdPBWAQUsFpiKv12rmwu bgsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740991551; x=1741596351; 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=Yl6OXiLxc2DNrX6mWydkEen+W3a+U6q5HOC8/Fd7HQw=; b=lI0VrdnzRGECMeBPSqqZPL+ZHzlfP00NWXEsQG7/6NAMQ0nhzyFK+95QI2tYLWk33A 7TCMBbBtOAosz1+GWDZVDO4GYdc4oT3SqWRdq8KiGkon1ZPyhSo1jRf7psvJ93/fFiZH JZeR18lcb0/LI+bP1LCrCVEYH9jOq7JkjF/OSxySPAswzn+8QUoegO3DazhWjNB2kOQv w9X+VguEt3HS/XYxZ96DHVPvy/IlDHCT+FJCqYKGx7dft6YasfpJUAfQzaKgJoADBzCh +N+U75uD0RhKpwwvkj+wlBzUPG29J1DPAPtoFWb2GgP1BinIYk0jUSVLTHdnFHONMy4y UYAQ== X-Forwarded-Encrypted: i=1; AJvYcCX+qutougMaYxlc3/4yETXW7Buxv8k3sv/sYR5UDg+Jayc0Yf21Cz+3WZjCHhA4pvZMLGqSo+BJN2ypH/0=@vger.kernel.org X-Gm-Message-State: AOJu0YzlEm2rwH3nZVGRebLuAqtIQHCWD4dCaqN6PAMi5eERJstJf5a7 Y78IQXxUhQdfn4MUAkW41UKtm/QGXpHxzOXCtVFagN5Fw9H44wlMyx0qTnAqzU5PSsbFRUndPz0 0gsKLDKE1+WHijQ== X-Google-Smtp-Source: AGHT+IHTziRho0PdDiqY+w7INFWExFlSE+JgzgeyQW5xfAW6NBxpwCji0bm/eXLdqeaM8WMzyupzDJI8C6vUsII= X-Received: from wmqe20.prod.google.com ([2002:a05:600c:4e54:b0:43b:c7e5:66e0]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1990:b0:439:9b80:ca6f with SMTP id 5b1f17b1804b1-43bad205987mr103494675e9.5.1740991551369; Mon, 03 Mar 2025 00:45:51 -0800 (PST) Date: Mon, 03 Mar 2025 08:45:13 +0000 In-Reply-To: <20250303-export-macro-v3-0-41fbad85a27f@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250303-export-macro-v3-0-41fbad85a27f@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3689; i=aliceryhl@google.com; h=from:subject:message-id; bh=GN9iRw4vP0biBC3qh1br1TYeqEjFY0JaW5FPcfgG69g=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnxWw1V7qzGAYepkGLB5056taWAfpA48W2E+6j7 36pkLuCplKJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ8VsNQAKCRAEWL7uWMY5 Rp0OD/0YxQOPsE7P9lwb5wr1WtaICm79Mv5E1VBseN+IJVPkRaMtI9PZOPMYl2nkkxIthFrBfB5 VA9z7R/Ayu59wN8WJG0KaAP+4b6+Hf/7NK4Qja/DBtDxkgk7NBz4suJ1OIfC++krRwnTX1Sja+m W3tz69mVlP+mq3G4KB/KJ2krJ4m7nmpcQLxjnmyKbjhR4J6FQaYv6HOmelCuvKdkWJGFar4mhe4 6JPThxK3D45Jmy5k5LnfYQaRNn1xB6IdprUAVEt85bp4HvqgSjMof/Cvv/+ygRmAHijGM6ikJnG eCQGzRajiTA5q6rWl1HC0EmbVELa4Wl8ytL5rj+N4RJBCCV9+n5SJeXFy0V3D/6VoY6qDbObxj7 /y1pjasux7b8tEe3ffOrYqDKK3j+rx6rmzOuYs/Yuv2EnMMCIttl6GuxwQfkKfGXGJrYWrGRTRs iyFHvwo4C8MJEvQaG4P/wGwxyM2aSRD7MJoIh8neJFojUpASlc7g/zA+MfArJlfmeuP6ktX2SKv xSeAIu6RAEVXp2hC7SQJ2jKkt2GQw3RGvWKosp9L1Jl6kjxUUXV8F1wUTtYAcKB5/ovsh9tqORV opNlnGdFoiM4XyttGOBynM7Eq8Aci+A/UPofTC7lxRjHejSQjL98l136YwRA1Mi1eOOV9Bppl5d pjkjLpnriIPhq5Q== X-Mailer: b4 0.14.1 Message-ID: <20250303-export-macro-v3-2-41fbad85a27f@google.com> Subject: [PATCH v3 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 , Tamir Duberstein , 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. * The # token. (when not followed by an identifier) * 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, there are zero `.push` calls, so the compiler can't infer the item type and also emits a warning about it not needing to be mutable. 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. Reviewed-by: Andreas Hindborg Reviewed-by: Tamir Duberstein Acked-by: Greg Kroah-Hartman Signed-off-by: Alice Ryhl --- rust/macros/quote.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/rust/macros/quote.rs b/rust/macros/quote.rs index 33a199e4f176..31b7ebe504f4 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,22 @@ 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::Punct( + ::proc_macro::Punct::new('#', ::proc_macro::Spacing::Alone) + )); + 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 22:16:11 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 ED3991F03F8 for ; Mon, 3 Mar 2025 08:45:54 +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=1740991556; cv=none; b=OkUdr4+Q96tuan/y89FaT865G8SnGjRduUeSBBRdbR7+DbLtKxqECD7FLU3/pYqQC7aKwEN6u3vRsjZwhXrQZcdsVkM9/p2A8U2Yu+NSToa+7d+U++rPIjzDYWaAl6rXgyfXlyDce8VW1FGQw88w9qruReW1OfRgKpXtk8TjcJg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740991556; c=relaxed/simple; bh=1SNAXOe9oyT3qRvAmlmyXdBbsP3AQD5dhDmNwYVOio8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=A4Ns7EqX9OqKZh6Mc+hGPZf6HRCQunp8GHEHYxWtHoAWONDlRhijNXaMbLise3Np/WEHuS4Hj21YWMax2FLIsFcR5QGkWd+SrgW+54g/6eL5qcYMahnRH/6Ponsl3bGVj4mF7bGjwxRsdnC/LKZBgMNy9rq1Zi5/7Ba/dPAhnso= 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=IM+P1vHr; 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="IM+P1vHr" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-390e8df7ab6so1369938f8f.3 for ; Mon, 03 Mar 2025 00:45:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740991553; x=1741596353; 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=dgRB7R3QxdYuF4ALv/yYMTfwUqi5oUHSBNOS9q0wOcQ=; b=IM+P1vHrFiUqhcC9xDjRvUZHkGQzJbarIU9rW4ZMrGjzRB1OkFCzyjWsCABsrsWoz0 aOyhLeJ2OfN5GsnIiVc2RJfeYzhQ1KsA3YYLjoN/kMlu+PdDd52Z/uzGBgPeLaUOVmyJ sOtaxcQZoL/vYgp9fqZuMsI0wG85JzDZGMQiifYvzok5eMIBNy0ptp3xTaOx1qXgtiDJ 4sKUejrJP00yS4NGqTQjdcTpWVO57SMbaKYH5sGBvej03P6RQU8S/9TrtqFNDYZ6rkHd T6GbrCtU35MrmnAm59Amf1PbbJM2ec4drHU0YoFOEu2QNJQim4aoOOdzYAs1MyEmQ/9h kNLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740991553; x=1741596353; 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=dgRB7R3QxdYuF4ALv/yYMTfwUqi5oUHSBNOS9q0wOcQ=; b=fEiuI+UzQwJNSsj3Y0praltns1+tPlbIN6udaT8FBJbt6xCVFAxQEpNfgN8f2rneBO 9MqYNrPK6vl/hUZAqOK7YGv9Qjg7uUT8/1HxyJjqv8d8BaRN9iXoF0OwVFJ7UTT5cyOH tR6HWyuiYlT2Q80GBzjWV9Q7ZlxYYHOCgPwKJhCPs2P3DHA2EWcHCgNf4yi1jjgapOcf Xt/L3zBg0KSzxTt/rIglTv/GO8LqugiibNB1FA+7/Lyyk0BC/Vg01EVGnFOy8FVzysVL Q2rEJVhvGvEhcbxvVdWJmGl9tZrZv6NmqbDRe7OC6xl2lWTSjaBdfmmiZuDtCD4xDzX9 OHSA== X-Forwarded-Encrypted: i=1; AJvYcCX9czHLeCF1AQNsmdUWfjc75iq/H+c9TtThTHKnYZEmoPFn1lYtaf7rwHChoRbTyFQL+UJP53VucEqFhjo=@vger.kernel.org X-Gm-Message-State: AOJu0Ywkko9OSq3M5TQQ6XP8asuNKfBARknsHzmslD5rFx067SGVmdd0 IUA3TRRsiRjRvbj4Uz0NBi/sjRAZE9LLhu6IiDAtfyv6N3aVMhkvP0wiRp8sn86UsH1O8v80Dlt LJX8938uPG+vBAQ== X-Google-Smtp-Source: AGHT+IEbagZM/EJ0D/zs3j/Sycg3tpD2lbmYa29N6g/8/oZyQL05ic4DnohKXYym9qS/gDiNrS+7p/b4SDKBM28= 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:a5d:588c:0:b0:390:df02:47f0 with SMTP id ffacd0b85a97d-390eca414c6mr7792866f8f.42.1740991553493; Mon, 03 Mar 2025 00:45:53 -0800 (PST) Date: Mon, 03 Mar 2025 08:45:14 +0000 In-Reply-To: <20250303-export-macro-v3-0-41fbad85a27f@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250303-export-macro-v3-0-41fbad85a27f@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=6732; i=aliceryhl@google.com; h=from:subject:message-id; bh=1SNAXOe9oyT3qRvAmlmyXdBbsP3AQD5dhDmNwYVOio8=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnxWw2t+6Dxj44koarvcgY9JDKUuMhLzySqp2yS kaGfNpNK3eJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ8VsNgAKCRAEWL7uWMY5 RgwPD/9IdLzG+1nDOK5S9Nc3Wnyk6K1CZNtu7cX/HaCmzPhgLgoTKD3RncHw/rHaM0HaQpwUC0p wyoHfvjTiJB7C+/wx+uF3UJygcVabx1GU9ri9ZtkoRWaTcAbKXXB56Dr/a2N7nUKiDqTg8QSvR6 efpNr+G5neqz+5RF7eN75GEIVYv1K3VTK5KzOslfmVBYybYNVFIXDtbbOaFvMgdHCRInkE79H/f 3BaBPmUIvFHIKwTxBZIcGjaQpYopL6TdaM2Vn21eTxPsCWtowuJg91TGsrDFvBE1canWkG1Ie3C WswfS09Hr1ZX+ab0L/uTjunIWwi+2rkeeaceEAp023ESvo1Ei1Kd2LpVmsHL6/62BpDN85cp853 I/6Y8sTLQheFSozz5bfOneb+Q4xfM+rfyaw8/bN33JkEUPbrXSb0XK/4JWFNuRnCQ45xk5rBjyD V0vRrVwLugEKOApFh7opcO1uovnWsmI2CzzvGv8+q45n0vqNIvnoWiwOyiBk60OHZFHb/yvCIQq 5ntMZslk52d5qT9WCJefLx0JsmjeBhRyE8Kkj7qCrAeO1Mr85OFau9BnHRfm8bZ1sutuHaSiOYR GB4IuLgfwEc7zUOUmaDeGQ2IheDFUE9g+UnUgflSXUZUUF1q/tZCdoR22R8YrcnN835DzntiJZm Qo6fUXFEi7wHgrw== X-Mailer: b4 0.14.1 Message-ID: <20250303-export-macro-v3-3-41fbad85a27f@google.com> Subject: [PATCH v3 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 , Tamir Duberstein , 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. However, we only use bindgen in the kernel. 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. Introducing cbindgen as a mandatory dependency to build the kernel would be a rather complex and large change, so we do not consider that at this time. Instead, 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. Reviewed-by: Tamir Duberstein Reviewed-by: Andreas Hindborg Acked-by: Greg Kroah-Hartman Signed-off-by: Alice Ryhl --- rust/kernel/prelude.rs | 2 +- rust/macros/export.rs | 29 +++++++++++++++++++++++++++++ rust/macros/helpers.rs | 19 ++++++++++++++++++- rust/macros/lib.rs | 24 ++++++++++++++++++++++++ 4 files changed, 72 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..a08f6337d5c8 --- /dev/null +++ b/rust/macros/export.rs @@ -0,0 +1,29 @@ +// 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 quote!(#[no_mangle]); + + 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..a52443a3dbb9 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_*`. All Ru= st 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 22:16:11 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 28F771F0E43 for ; Mon, 3 Mar 2025 08:45:56 +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=1740991558; cv=none; b=HZ7jFFWvCY85V6lagGC4kNDvdZ7FwipNyN63FqOIxiY1qL1PKq1NjSGQKDFXXK4i7UGvCGMoR0tgEGKfyfsNa3xLE+rlYHobOECtTps3WJmU4YkeYttsBd2/ewxYuOPlpDIHBmhcUNXoHIRHymUKfW551FeXrjsxzHGBLFhUDoo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740991558; c=relaxed/simple; bh=27rIJnpKMgqp3S9uOE1r8d3kMA+evBJFEj9qZVD/zkk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=krK+Ljmgf2RbhzRNanzuKjW2rEvC0b/ccOhuuKsJyvOgqqJTH6ZM6A45JFDneSRncyKqriPrflAILWomm8QuhZZqGQk1ev1a04HDMLdJtnFIX+p1eYsz9g4QD7g+MAJGKEZX4hK04zFpmYmGOT7d0EyoFpACVFXNxbYjv4HFQwo= 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=p48O97by; 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="p48O97by" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4399a5afcb3so35745675e9.3 for ; Mon, 03 Mar 2025 00:45:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740991555; x=1741596355; 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=TlZATerwOi5j8gaqWad51my4T/CPZsM36wEeCkoHEk0=; b=p48O97by5p6mUj8nZfDGwi4iuM/oROgGLYrNRlsmfg6Xblg0vmQEGagz+wu+Wh01LH m8rBVI7TP/ZiJk+jV5cUaDYTIdUVCnoD6XM1gFHXQqiz4mubtzpXCO2muQtqGyYyfwmz 5OjN6p4JOlpM8tX7/EwcFjG9HawDckUiVATXkFdUBMg+A1aBGwIX8LuOcZVo7hzqD1Lz m9eSsnBGiAo5TUmA/3lPHXzhrxKovn7IkXbauit28Le0eJrHagDLbdRP6ioSGWK/PhGw hMEsXZe3Kbw5sKGoqnJ/6PKUN/P3DppszCTiOjEu2on03kBnRYk0bRUxzHahpMYMYy/7 s5xQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740991555; x=1741596355; 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=TlZATerwOi5j8gaqWad51my4T/CPZsM36wEeCkoHEk0=; b=WHvb7cSLnvp0jDLhGBo0OlS5p9+udo82eY2pZBiip4d6s7ptZ+yJuQQr8KR3tgv1mB m2DaWgELgrYm7QO3f/3Oh5J9o8KvMXILJyH2uybvhf57onLWY722H9InrOVVQIlkd9xU TDMwRH1t4Ed3xifQ7WZUeK+yNjetHsTYhtNWi60jRd9SkRjQkNyC16fYvTEjbrnubb/R DdipLyv/GGPpVzHigIFQvnhHeEaF9NZ8FBtcT33AhFphRBmtcoKL3/NXYPNKW9Hlpd40 hjkB+Dn8n+XwsO6CUX4Jh+69PXqohPQ4Z5ksC81m1GcdBOuxwh4s9tU/MITMt3j7Bgem loPQ== X-Forwarded-Encrypted: i=1; AJvYcCVjWYsndhjuqhFoS82ORSgmYUlQdnRIrmnmghdpCaTFUe2vWIuPDjj6l8UoQNv0rcpZr8gCf+pRcCMAYHM=@vger.kernel.org X-Gm-Message-State: AOJu0YyWkWnuRW+egCmEwvZ5lMWoKKCvh2FKk3N2XKjZ5Cg7Yfog6/38 11gtjIeSz8wCDrQ49/1kDVpwjRwvzBm9gJYOMMG1aLJB1pdhx74BXourgBWmiyj5KksBC5wu6Is DyRUvpREBbsavIg== X-Google-Smtp-Source: AGHT+IF/OlL6SSDUrA711Q70XX+2KHCrfpkEqlIN+HJZdKgaK09VyIf3il1gLPXIbqk4NQNUA9/BHyQB6JirT4I= X-Received: from wmbhc11.prod.google.com ([2002:a05:600c:870b:b0:43b:6943:f013]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:154f:b0:43b:b933:33e4 with SMTP id 5b1f17b1804b1-43bb9333572mr39999305e9.15.1740991555606; Mon, 03 Mar 2025 00:45:55 -0800 (PST) Date: Mon, 03 Mar 2025 08:45:15 +0000 In-Reply-To: <20250303-export-macro-v3-0-41fbad85a27f@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250303-export-macro-v3-0-41fbad85a27f@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=2101; i=aliceryhl@google.com; h=from:subject:message-id; bh=27rIJnpKMgqp3S9uOE1r8d3kMA+evBJFEj9qZVD/zkk=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnxWw2Tay53acZ34Qn0eFiLPW+Lb9vqKq4R/vG+ fx9bpMrWpmJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ8VsNgAKCRAEWL7uWMY5 RnTbD/9YsPVC7+afuFs+98V5aJHhzrUsrsUFek7EYVLlN7anvgFXZxC5zDoVi/nKouxeYNlUrkc nfExRYI0ZxQ54u4GfOXQlinasxJJwgXnwcymxJNwO4mg0St1dJaIgInwn4tvjtASj5I9aEOwcXE u45D6rRKGtyWOJkwGSh76n3mNuwWSLejj6TrTQeItBP2dK1djpNdyvqjWE1NT6bijf79nFgDOCG b5rQ/f253UATffkklrsB+DVFQ2C3Qwke3eDB9sZsNNlOUzX2FPFxuUhc/g0iMsp74jbh+mPLauc MDp6YC0mvqDWt9VykZdmjb9WhvgOQxktmuMMmszdOBgpu2Q1zJ0yXZ7bEqtwPgLzGS2e3RfUvII QSGkt4/jG/5vCO7daJ6gnQJrOHdK6oh5SvbjPLOhOnDqo6ZiQ94EBhmdnc9lZXFFLLixX57HALS hxF+9lS0IGeqfadsMhaBnBU2iKF7M5zcIv2GP4xS93UMUuDLv9zXn6jR8tdAxHnRVPwlzvb3LQ2 kljkwSsO+yYAsFZogs2EyYIs3myE57AmrK5QsztGscSBm0GmYx75alc7vCq2XKeDsVVa3Ysmn4G 22cKUJot+xuuQtxOslAt3ovEaDbED+emWSenu2ChOBo+AvJ6KAl2UMB5JzN3ejJFsF7d3X4oQJz dmH/R6fzrwpBmvg== X-Mailer: b4 0.14.1 Message-ID: <20250303-export-macro-v3-4-41fbad85a27f@google.com> Subject: [PATCH v3 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 , Tamir Duberstein , 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 Reviewed-by: Tamir Duberstein Acked-by: Greg Kroah-Hartman Signed-off-by: Alice Ryhl Acked-by: Petr Mladek --- 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 22:16:11 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 3C1961F12E7 for ; Mon, 3 Mar 2025 08:45:58 +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=1740991561; cv=none; b=PcYNEhYcAHmbJYTBd02XM1qotKBSh4p15Tw/mw+9dPkZwmfIWK4OlGsQ/ReR2kTbVVW2YIzArcyoqmZzamft+RuitxXVmdcI6gXGF4zI1XB518pGeZKUcYm6fBmTq42KIp3DM5hR4c7sOxaynil2G0Nf9llppmCwFT2S57tfh20= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740991561; c=relaxed/simple; bh=h39madShj1asyLtbf2sfVdBNK2RyKkwe8T8CD9+EWUU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=a7gPtkBXwPsL2Jq2QjVlgCLKH2hZgQ/WjevRvvfL1SAOa0ohmbGNNdiMv8J43ae5wPd3kx9353ridPwIunob0wSSI9o1Cv3qbj6K2PvlO9bJekIfAi7NqhJt8K9p+hqX6QWNw88Ec/R0INUtxUYuW8JL7z9UVFpzrdDBaM5ZsmI= 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=pRVew8xk; 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="pRVew8xk" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-43941ad86d4so18162215e9.2 for ; Mon, 03 Mar 2025 00:45:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740991557; x=1741596357; 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=0DQHcaO0nvqPOcD0xXA6BfduaWtfQQJcuNoquBNRtQM=; b=pRVew8xkZ3dx76UDMEb5V5Ln0Zrn19RnI5UNBN3imF+cdAd3P5dKdKXSD097jduQ1Z hZfQsnVy5qXTmvDUQuSeT+7hm8iab/iHttka6yYU8AvtbdxDjRC7WG4+k/FM9VXhYmRd qg3JW79D2XsRY+IFG1p9KlEAhSA/5Es8qCluW2j1lg/JbPwtYLqaVb3iKxpXeLh6C3Nm kcO29QyghbQouvkyRiEx1nTOW9f+uwTRnmG/0lOUu7kJZXSYcaP6ffAR6+teNa5AwY9v FtL4/CB4/2ZsHeBqN8O5pKm1OSMZVUMFwUL0NXJBE3k5tE+JDzyqm76RKjhTYpR1JkKb 6acg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740991557; x=1741596357; 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=0DQHcaO0nvqPOcD0xXA6BfduaWtfQQJcuNoquBNRtQM=; b=mx4YYY69mm77Dwe2RhBKoa6HEwVya5REa/RE/Ing4Py1uF+4epn9BOTN2sRIxHzH8A 3n/mdsENPLPtN8Bm+J3/wHMvQe7rZz2GgV15WdfGvv1V/wnpgTvC64ZlKHfuENvgFlzV Inapi/h/RA1MQJ89i308YYyBRcIFJROuJblg0ZNZPAyoLt2dH7RHTFGmAp+45QmGQUlH NJTq9Lj8OUjDLtVMVm8NLVRKhQ7QsDWnk2TIqd7GQP3YFUmdrggNfnVGUxjfFH9VrcwI h4FyfasPMQ5hPrJx86u3A5JXtKQfTri50Um+KfWYXB7BnnTafJsf8V3StYfgULooRo8C Hzwg== X-Forwarded-Encrypted: i=1; AJvYcCUrlYmYVMDNaZkCpOGO2yzC6nS7+nWC2JblI1Rl/ZqTtWHfyeiVJLCUmnzoP/IX1evgJ4hjVzruwzJ5IIk=@vger.kernel.org X-Gm-Message-State: AOJu0Yw5wQ3LqBkKnxZJ9yeobPH2v7MEwBNTnzSeI/58dWma9X9zlICl 3Fxk8jglUh5X5FXkcBSNLY4/z8hMzOtJ280MlZ9hzYBihnZgDWR93G5jMTpHRBA8N44A0us9hoe Qkwn5gdCu56iHow== X-Google-Smtp-Source: AGHT+IEbR3/WcyPiaNNIulZcuIWaKGsu5gY2WjUpjPLpgdJov23gFYjscM1b2KomiFKt1HIexbDsjk8ll3zgBPY= X-Received: from wmbay26.prod.google.com ([2002:a05:600c:1e1a:b0:439:80fc:8bb2]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1c25:b0:439:5a37:8157 with SMTP id 5b1f17b1804b1-43ba6774a03mr109919695e9.30.1740991557659; Mon, 03 Mar 2025 00:45:57 -0800 (PST) Date: Mon, 03 Mar 2025 08:45:16 +0000 In-Reply-To: <20250303-export-macro-v3-0-41fbad85a27f@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250303-export-macro-v3-0-41fbad85a27f@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=4809; i=aliceryhl@google.com; h=from:subject:message-id; bh=h39madShj1asyLtbf2sfVdBNK2RyKkwe8T8CD9+EWUU=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnxWw3pj1BuO7nm85LYW8CAGocu7fhqRw9nvE8r O7rU8v0FnuJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ8VsNwAKCRAEWL7uWMY5 Ru5uEACnfnvDNKGTdfDWkGpV2cmEz+XPIKAv+OAav/nOsh2wf+SfXkLPHDH7ruxHfwIZIgBxSN0 XNYeBV/QOAcBcfj7kCEnALSOYbYa68MODYU3lWLzoYQGcTKnIvgBpZcQNcIw6XoiTggnSfS4pRN twnRo0DybJXquoJRODCF7pPH1zuKCeDtLN4pEBmlSQz4JJr4lm9jHA2UxFUYxzGNmRBYpPxK3sA gzDGC1QnvX1VjNHlwTJV9otv6N0zCdO3zdQ/FW0byi7n9wxNs91XO1qcbwOraS/zqXnSJujthF9 g/DF9vk9C9vtZYhOUPPUdeYjapB4wG9+h8TlKGembKDI4h7zk/XPUxfp6siqoMycq+lGTAEepNz +sT9UyQN+jUXepDM2K2FM4AA1cu3hMqJkfDZgI4x49hrXRmBJ1VGEt308d5DPco6Um7EQtheFGc 0fNhGeafPpTduaf2Ebi+xrdhZQ2ZBTzGDxRFgRMTOFSANfmum5+BX4ZuThqe9scF5REbMHD65y0 nvytaWAsv4lSnYIg9wMuWBKn+4uCuI1QWlTyjvLtl4aOcceJFpdKBShLjTO+8UvL6H9CqvjDm0y VQ0iys7hDYKZ+bk5RgqB3PZWRsc8fbJlsZUk8pdSqFSdC4ibbPV67kpPGD7QcElJrgJYkllt3no E+HSkBYCbHVjBeQ== X-Mailer: b4 0.14.1 Message-ID: <20250303-export-macro-v3-5-41fbad85a27f@google.com> Subject: [PATCH v3 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 , Tamir Duberstein , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, dri-devel@lists.freedesktop.org, Alice Ryhl , Simona Vetter 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}` The signature declarations are moved to a header file so it can be included in the Rust bindings helper, and the extern keyword is removed as it is unnecessary. Reviewed-by: Andreas Hindborg Reviewed-by: Tamir Duberstein Acked-by: Simona Vetter Acked-by: Greg Kroah-Hartman Signed-off-by: Alice Ryhl Reviewed-by: Jocelyn Falempe --- 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 | 5 +++++ 4 files changed, 23 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..906943b02beb 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] // required to be unsafe due to this annotation +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..ff78d00c3da5 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) +size_t drm_panic_qr_max_data_size(u8 version, size_t url_len); + +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..607e90a682ca 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -36,6 +36,11 @@ #include #include =20 +#if defined(CONFIG_DRM_PANIC_SCREEN_QR_CODE) +// Used by #[export] in drivers/gpu/drm/drm_panic_qr.rs +#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