From patchwork Mon Mar 17 15:30:03 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tamir Duberstein X-Patchwork-Id: 14019448 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 58933C282EC for ; Mon, 17 Mar 2025 15:30:22 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id BCACA10E0E0; Mon, 17 Mar 2025 15:30:21 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="O2WqQ59s"; dkim-atps=neutral Received: from mail-qt1-f182.google.com (mail-qt1-f182.google.com [209.85.160.182]) by gabe.freedesktop.org (Postfix) with ESMTPS id 1DFEC10E163 for ; Mon, 17 Mar 2025 15:30:17 +0000 (UTC) Received: by mail-qt1-f182.google.com with SMTP id d75a77b69052e-4766631a6a4so46376491cf.2 for ; Mon, 17 Mar 2025 08:30:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742225417; x=1742830217; darn=lists.freedesktop.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=GhDi7jicB3Gv5oqI3TBvIGb8fiLoyX3gf2bLY34vPcA=; b=O2WqQ59s0OoEV+FpOAES8BsvOaKejwh0isM0caqOHLCplahLNzXPtadBknHr8wZ7JF +CbCeGz6y9wupWvy+NNYvnu6HVFEMWNBfkW+6dxio9EkwTjAf5hnkzpazZ74xWt9UlvW gPlnDa87qKoOiACRllDqkxb3NbuHTrH9Ga62DHWCmyN2C+R5bYT03/yGg6LbATr4Ghz3 k80yKYrPLW/iuzN0GO/uY+ucC4V1Y/m00jqfhmR7KL2vzaSzLE98CxzhJMv+wSb6Ljsp Nt1KpLYMpW7EzvEij4PFuBsaXFDrZG97UZ7ecLu5oEK6OOU1goYoi4AJYREATp9v3WXU L6yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742225417; x=1742830217; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GhDi7jicB3Gv5oqI3TBvIGb8fiLoyX3gf2bLY34vPcA=; b=hhi38I9MuYAW7+E6yZ1yMrj3TSsm+vnYXZGRNnPrSxtMZYC6Nd8TmYZ8cumvt7WKeU lOFFM+PWlz4/C54oK855ci1sTcECKWSHsqX2M9MRzvzuf1dBLy+tk3afVk/2qc8axd6k PeLBUxDcE3vvvtuabD1zsImwFePRgQqdtFCGuZFLGsS4aBAGFRqec87ze6gsCWVtVHr4 Hvil4K/EqlKPG+OoG4447Yj4mA4lOsBiuON2a9qQClxUBgWJNafwFLDW4vhE5PXnoDdM funwTtraFxDmT4hD3EQ5jkFLDtDQUiOoe9xUP7IBibAqyR8tvJIvFkic9lHc9ccFT6fa FsHA== X-Forwarded-Encrypted: i=1; AJvYcCVtccOFJRzIhebV2KRhSQye5SZZR3lDVC1HW1L+8kLqN0Iac+z7cxBQVMU+UM5cvsQeUZtSiRnBHW4=@lists.freedesktop.org X-Gm-Message-State: AOJu0YxXqD74I//r7t0rzzcOlKARqgUVk6I0JHp89CqrUYqzxlCvfRcc zNP0E4f6VAx9gxj8+AbkLFWYPvP0mNSQqwg3l2gzOsqyobHwLfzW X-Gm-Gg: ASbGncvkgXU7NTTxnUUTmHDy79pvnkMCeqig6YdXPadsuj0I5IK1hz935JgfblVRk5v 90TvUU2/hYgcUsh0o7MKsMMR0VI+xTr4yJe87XVL1Wf8RDTI6mVgymJEBgDvyo1xVqIOHL3DsKR 92hmjY+aoMvMjX1YkYBd9TIguvAu8dU8SRAZX8Cu4YLHdKwfZsyEW6pNyn+LLV5yCifocJOhpP5 PK8puetIOTT/wOSerBY8EDhjHax1p5HQ8wktxgdbkA2iBEh3vRjj23N1q0Cy8qqgSQKD3Y4wlIu uN/iCw7CxFGM6ZniqkB+TV84aP+gCcdoRs5/13vB7l9gnVjRJu4vbtccFesP4Fa5Bn51FhlBSGZ 1NQt+Vg== X-Google-Smtp-Source: AGHT+IF6QKQ27zO/djSgLkh3SLlbu4CCHZ6bVaY3V5bdQy4+E/A82ztgFL3TtEYWb5+dRwVxvOIv5Q== X-Received: by 2002:a05:6214:c47:b0:6e6:5f08:e77d with SMTP id 6a1803df08f44-6eaeaa64726mr197312436d6.19.1742225416788; Mon, 17 Mar 2025 08:30:16 -0700 (PDT) Received: from 1.0.0.127.in-addr.arpa ([2600:4041:5be7:7c00:6895:24c6:e898:e320]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6eade34c51fsm55736766d6.109.2025.03.17.08.30.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Mar 2025 08:30:16 -0700 (PDT) From: Tamir Duberstein Date: Mon, 17 Mar 2025 11:30:03 -0400 Subject: [PATCH v9 1/4] rust: move `CStr`'s `Display` to helper struct MIME-Version: 1.0 Message-Id: <20250317-cstr-core-v9-1-51d6cc522f62@gmail.com> References: <20250317-cstr-core-v9-0-51d6cc522f62@gmail.com> In-Reply-To: <20250317-cstr-core-v9-0-51d6cc522f62@gmail.com> To: Michal Rostecki , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Brendan Higgins , David Gow , Rae Moar , Danilo Krummrich , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Greg Kroah-Hartman , "Rafael J. Wysocki" , Luis Chamberlain , Russ Weight , FUJITA Tomonori , Rob Herring , Saravana Kannan , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Andrew Lunn , Heiner Kallweit , Russell King , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Bjorn Helgaas , Arnd Bergmann Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, dri-devel@lists.freedesktop.org, netdev@vger.kernel.org, devicetree@vger.kernel.org, llvm@lists.linux.dev, linux-pci@vger.kernel.org, Tamir Duberstein X-Mailer: b4 0.15-dev 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" Remove `impl Display for CStr` in preparation for replacing `CStr` with `core::ffi::CStr` which doesn't impl `Display`. Add `CStr::display` returning a helper struct to replace the lost functionality; this matches the APIs exposed by `std::ffi::OSstr` and `std::path::Path` for printing non-Unicode data. Signed-off-by: Tamir Duberstein --- rust/kernel/kunit.rs | 9 +++++--- rust/kernel/str.rs | 63 ++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 57 insertions(+), 15 deletions(-) diff --git a/rust/kernel/kunit.rs b/rust/kernel/kunit.rs index 824da0e9738a..630b947c708c 100644 --- a/rust/kernel/kunit.rs +++ b/rust/kernel/kunit.rs @@ -56,6 +56,7 @@ macro_rules! kunit_assert { break 'out; } + static NAME: &'static $crate::str::CStr = $crate::c_str!($name); static FILE: &'static $crate::str::CStr = $crate::c_str!($file); static LINE: i32 = core::line!() as i32 - $diff; static CONDITION: &'static $crate::str::CStr = $crate::c_str!(stringify!($condition)); @@ -71,11 +72,13 @@ macro_rules! kunit_assert { // // This mimics KUnit's failed assertion format. $crate::kunit::err(format_args!( - " # {}: ASSERTION FAILED at {FILE}:{LINE}\n", - $name + " # {NAME}: ASSERTION FAILED at {FILE}:{LINE}\n", + NAME = NAME.display(), + FILE = FILE.display(), )); $crate::kunit::err(format_args!( - " Expected {CONDITION} to be true, but is false\n" + " Expected {CONDITION} to be true, but is false\n", + CONDITION = CONDITION.display(), )); $crate::kunit::err(format_args!( " Failure not reported to KUnit since this is a non-KUnit task\n" diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 28e2201604d6..50eb4266047a 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -376,27 +376,66 @@ pub fn to_ascii_uppercase(&self) -> Result { Ok(s) } -} -impl fmt::Display for CStr { - /// Formats printable ASCII characters, escaping the rest. + /// Returns an object that implements [`Display`] for safely printing a [`CStr`] that may + /// contain non-Unicode data. If you would like an implementation which escapes the [`CStr`] + /// please use [`Debug`] instead. + /// + /// [`Display`]: fmt::Display + /// [`Debug`]: fmt::Debug + /// + /// # Examples /// /// ``` /// # use kernel::c_str; /// # use kernel::fmt; - /// # use kernel::str::CStr; /// # use kernel::str::CString; /// let penguin = c_str!(" From patchwork Mon Mar 17 15:30:04 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tamir Duberstein X-Patchwork-Id: 14019450 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 4B9C9C28B30 for ; Mon, 17 Mar 2025 15:30:27 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C556210E42C; Mon, 17 Mar 2025 15:30:24 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="TKzwpCsa"; dkim-atps=neutral Received: from mail-qv1-f54.google.com (mail-qv1-f54.google.com [209.85.219.54]) by gabe.freedesktop.org (Postfix) with ESMTPS id 2056510E429 for ; Mon, 17 Mar 2025 15:30:20 +0000 (UTC) Received: by mail-qv1-f54.google.com with SMTP id 6a1803df08f44-6e8f43c1fa0so48117746d6.3 for ; Mon, 17 Mar 2025 08:30:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742225419; x=1742830219; darn=lists.freedesktop.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=X9k3CrvsBwVKLSsJegpaCIRHYQUzIcHt/dZ/Hy2my4Q=; b=TKzwpCsaZaVWV1hDJbDKOEi0tPH4f04lQznSlL98WYu9m/e2URL9D2s4oihl4panud UiYI0QzamL/4WnFhrpYgr1XCVMmIDAXcpw7DU9SMAJjIn7niaw3yQqBeePQ77RZcZfLn sKr7iB7DpciLE3FUIUc8O7QMzHOliylAKe1zJngxtlODPnd9n8TAHC8x0/hEDEbg7Re5 L5GBtVktKhKUmg/gZfKLU711daMQhQqzm4jYyC/lP254zjxxIe7Mx2wuYr/QU/LOyy9Y SEn0MSwplcqM+wriLBfUsyZJvLnBjG0ePXm+V2NMsY6GiSapOb+5AQqKf2M1n5J92khn a3iA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742225419; x=1742830219; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=X9k3CrvsBwVKLSsJegpaCIRHYQUzIcHt/dZ/Hy2my4Q=; b=XlwgBRMHeU5x0PqLx1YridN1qMpkgCjB6d4WZEYQsnXyEwv3c1OTs53GJnwQKYwnrE Cxy7ViOAioADDjhvMJvNiIblh7OTPoppqA9Y+hLIVSJkWTkck6XzfhHmZB3uBcLTkwLZ R5z9z7ceHL3E7rPaF4fz1x22ryGMPflRUYWpSQR5sbssaLsBlln8KDaZ06W8YsHo8BkL imKOTtGoZnkYA3bq0EjaONGeMfZGd/lHwP4Ai1OKvFVHLbZZZ2n85zrzhEje5iQ80UjS kDg9rbE9Fy9ZvhjcBEmaTYQi/E4bnGB+fROcr8pRgRU80FCTFG3ANX3fEGgU/Ao6GB17 trHw== X-Forwarded-Encrypted: i=1; AJvYcCXuMHqcT4t3p1yl9jdDboZ3D0/f2TPqGQRu3vwM6TjlnCVNxmnXEQtU83vEBJhuncnRZkL0eTl+LJs=@lists.freedesktop.org X-Gm-Message-State: AOJu0YzYpnhmBsChVx47Ww7S9d2DhQKWWnlxQQs//Ft6xw9f8UvmKYWO EU1hAUDIjyANVtnhQ25BlEIpGh6iTlfjc6bVI0xk3KFLRcWrLQVP X-Gm-Gg: ASbGnctdKWIwvkGx7/gHq/410D2ztQ6CTYrU+nMAbv/3DP2BPftZXVYLe5y/3hzOybz MgCCVB1aiqZqqyLO7MRhuG/ZLSD7jw2MDNdhKeWnSD8uT7TpsQU5N9Y8zD1+JhHCiLhuCbHL0NA 35ALGHDsVqSrHWDYe1cuvJ6jIpEdXOYwNdEnkQCioqns6D53SX7dmuyOokn3PkYjd1VFex+SUh8 P2P+qoO5COfI8I2QKrI+37tad04k3q+fkFR4KY1sf6E51ANY4KtqONA0IK6cFKMrDcOCrDn2ukw qXyLU6numYzeE+CkPFQt3szbfbrg8KRmUQ7mDf9mhSEbeyA4p+b0/x9VegqhcLVC3lXQYsczfUy kylgwXg== X-Google-Smtp-Source: AGHT+IEGZ0NWSlF63k4qzVGT7Fgq22uYO43K8byZGuJY4PGy+ha6FQf7Ei9E6N/mGH42x39QoW4qzg== X-Received: by 2002:a05:6214:194c:b0:6d8:846b:cd8d with SMTP id 6a1803df08f44-6eaeaa9afcbmr190487576d6.30.1742225419160; Mon, 17 Mar 2025 08:30:19 -0700 (PDT) Received: from 1.0.0.127.in-addr.arpa ([2600:4041:5be7:7c00:6895:24c6:e898:e320]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6eade34c51fsm55736766d6.109.2025.03.17.08.30.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Mar 2025 08:30:18 -0700 (PDT) From: Tamir Duberstein Date: Mon, 17 Mar 2025 11:30:04 -0400 Subject: [PATCH v9 2/4] rust: replace `CStr` with `core::ffi::CStr` MIME-Version: 1.0 Message-Id: <20250317-cstr-core-v9-2-51d6cc522f62@gmail.com> References: <20250317-cstr-core-v9-0-51d6cc522f62@gmail.com> In-Reply-To: <20250317-cstr-core-v9-0-51d6cc522f62@gmail.com> To: Michal Rostecki , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Brendan Higgins , David Gow , Rae Moar , Danilo Krummrich , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Greg Kroah-Hartman , "Rafael J. Wysocki" , Luis Chamberlain , Russ Weight , FUJITA Tomonori , Rob Herring , Saravana Kannan , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Andrew Lunn , Heiner Kallweit , Russell King , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Bjorn Helgaas , Arnd Bergmann Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, dri-devel@lists.freedesktop.org, netdev@vger.kernel.org, devicetree@vger.kernel.org, llvm@lists.linux.dev, linux-pci@vger.kernel.org, Tamir Duberstein X-Mailer: b4 0.15-dev 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" `std::ffi::CStr` was moved to `core::ffi::CStr` in Rust 1.64. Replace `kernel::str::CStr` with `core::ffi::CStr` now that we can. C-String literals were added in Rust 1.77. Opportunistically replace instances of `kernel::c_str!` with C-String literals where other code changes were already necessary; the rest will be done in a later commit. Signed-off-by: Tamir Duberstein --- drivers/gpu/drm/drm_panic_qr.rs | 2 +- rust/kernel/device.rs | 4 +- rust/kernel/error.rs | 4 +- rust/kernel/firmware.rs | 8 +- rust/kernel/kunit.rs | 4 +- rust/kernel/miscdevice.rs | 2 +- rust/kernel/net/phy.rs | 2 +- rust/kernel/of.rs | 2 +- rust/kernel/prelude.rs | 5 +- rust/kernel/seq_file.rs | 4 +- rust/kernel/str.rs | 432 +++++++++++----------------------------- rust/kernel/sync/condvar.rs | 2 +- rust/kernel/sync/lock.rs | 2 +- rust/kernel/sync/lock/global.rs | 2 +- 14 files changed, 146 insertions(+), 329 deletions(-) diff --git a/drivers/gpu/drm/drm_panic_qr.rs b/drivers/gpu/drm/drm_panic_qr.rs index ecd87e8ffe05..fa55547a1535 100644 --- a/drivers/gpu/drm/drm_panic_qr.rs +++ b/drivers/gpu/drm/drm_panic_qr.rs @@ -960,7 +960,7 @@ fn draw_all(&mut self, data: impl Iterator) { // nul-terminated string. let url_cstr: &CStr = unsafe { CStr::from_char_ptr(url) }; let segments = &[ - &Segment::Binary(url_cstr.as_bytes()), + &Segment::Binary(url_cstr.to_bytes()), &Segment::Numeric(&data_slice[0..data_len]), ]; match EncodedMsg::new(segments, tmp_slice) { diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index db2d9658ba47..df4bfa5f51ea 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -12,7 +12,7 @@ use core::{fmt, ptr}; #[cfg(CONFIG_PRINTK)] -use crate::c_str; +use crate::str::CStrExt as _; /// A reference-counted device. /// @@ -176,7 +176,7 @@ unsafe fn printk(&self, klevel: &[u8], msg: fmt::Arguments<'_>) { bindings::_dev_printk( klevel as *const _ as *const crate::ffi::c_char, self.as_raw(), - c_str!("%pA").as_char_ptr(), + c"%pA".as_char_ptr(), &msg as *const _ as *const crate::ffi::c_void, ) }; diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 376f6a6ae5e3..58157ec9403c 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -163,6 +163,8 @@ pub fn name(&self) -> Option<&'static CStr> { if ptr.is_null() { None } else { + use crate::str::CStrExt as _; + // SAFETY: The string returned by `errname` is static and `NUL`-terminated. Some(unsafe { CStr::from_char_ptr(ptr) }) } @@ -187,7 +189,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { Some(name) => f .debug_tuple( // SAFETY: These strings are ASCII-only. - unsafe { core::str::from_utf8_unchecked(name) }, + unsafe { core::str::from_utf8_unchecked(name.to_bytes()) }, ) .finish(), } diff --git a/rust/kernel/firmware.rs b/rust/kernel/firmware.rs index c5162fdc95ff..c18b75fc672e 100644 --- a/rust/kernel/firmware.rs +++ b/rust/kernel/firmware.rs @@ -4,7 +4,13 @@ //! //! C header: [`include/linux/firmware.h`](srctree/include/linux/firmware.h) -use crate::{bindings, device::Device, error::Error, error::Result, str::CStr}; +use crate::{ + bindings, + device::Device, + error::Error, + error::Result, + str::{CStr, CStrExt as _}, +}; use core::ptr::NonNull; /// # Invariants diff --git a/rust/kernel/kunit.rs b/rust/kernel/kunit.rs index 630b947c708c..d858757aeace 100644 --- a/rust/kernel/kunit.rs +++ b/rust/kernel/kunit.rs @@ -101,12 +101,12 @@ unsafe impl Sync for Location {} unsafe impl Sync for UnaryAssert {} static LOCATION: Location = Location($crate::bindings::kunit_loc { - file: FILE.as_char_ptr(), + file: $crate::str::as_char_ptr_in_const_context(FILE), line: LINE, }); static ASSERTION: UnaryAssert = UnaryAssert($crate::bindings::kunit_unary_assert { assert: $crate::bindings::kunit_assert {}, - condition: CONDITION.as_char_ptr(), + condition: $crate::str::as_char_ptr_in_const_context(CONDITION), expected_true: true, }); diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs index e14433b2ab9d..78c150270080 100644 --- a/rust/kernel/miscdevice.rs +++ b/rust/kernel/miscdevice.rs @@ -34,7 +34,7 @@ pub const fn into_raw(self) -> bindings::miscdevice { // SAFETY: All zeros is valid for this C type. let mut result: bindings::miscdevice = unsafe { MaybeUninit::zeroed().assume_init() }; result.minor = bindings::MISC_DYNAMIC_MINOR as _; - result.name = self.name.as_char_ptr(); + result.name = crate::str::as_char_ptr_in_const_context(self.name); result.fops = create_vtable::(); result } diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs index a59469c785e3..652e060e47bd 100644 --- a/rust/kernel/net/phy.rs +++ b/rust/kernel/net/phy.rs @@ -504,7 +504,7 @@ unsafe impl Sync for DriverVTable {} pub const fn create_phy_driver() -> DriverVTable { // INVARIANT: All the fields of `struct phy_driver` are initialized properly. DriverVTable(Opaque::new(bindings::phy_driver { - name: T::NAME.as_char_ptr().cast_mut(), + name: crate::str::as_char_ptr_in_const_context(T::NAME).cast_mut(), flags: T::FLAGS, phy_id: T::PHY_DEVICE_ID.id, phy_id_mask: T::PHY_DEVICE_ID.mask_as_int(), diff --git a/rust/kernel/of.rs b/rust/kernel/of.rs index 04f2d8ef29cb..12ea65df46de 100644 --- a/rust/kernel/of.rs +++ b/rust/kernel/of.rs @@ -29,7 +29,7 @@ fn index(&self) -> usize { impl DeviceId { /// Create a new device id from an OF 'compatible' string. pub const fn new(compatible: &'static CStr) -> Self { - let src = compatible.as_bytes_with_nul(); + let src = compatible.to_bytes_with_nul(); // Replace with `bindings::of_device_id::default()` once stabilized for `const`. // SAFETY: FFI type is valid to be zero-initialized. let mut of: bindings::of_device_id = unsafe { core::mem::zeroed() }; diff --git a/rust/kernel/prelude.rs b/rust/kernel/prelude.rs index baa774a351ce..97a66d29f48b 100644 --- a/rust/kernel/prelude.rs +++ b/rust/kernel/prelude.rs @@ -36,7 +36,10 @@ pub use super::error::{code::*, Error, Result}; -pub use super::{str::CStr, ThisModule}; +pub use super::{ + str::{CStr, CStrExt as _}, + ThisModule, +}; pub use super::init::InPlaceInit; diff --git a/rust/kernel/seq_file.rs b/rust/kernel/seq_file.rs index 4c03881a9eba..2ee298e2a2b3 100644 --- a/rust/kernel/seq_file.rs +++ b/rust/kernel/seq_file.rs @@ -4,7 +4,7 @@ //! //! C header: [`include/linux/seq_file.h`](srctree/include/linux/seq_file.h) -use crate::{bindings, c_str, types::NotThreadSafe, types::Opaque}; +use crate::{bindings, str::CStrExt as _, types::NotThreadSafe, types::Opaque}; /// A utility for generating the contents of a seq file. #[repr(transparent)] @@ -35,7 +35,7 @@ pub fn call_printf(&self, args: core::fmt::Arguments<'_>) { unsafe { bindings::seq_printf( self.inner.get(), - c_str!("%pA").as_char_ptr(), + c"%pA".as_char_ptr(), &args as *const _ as *const crate::ffi::c_void, ); } diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 50eb4266047a..53d1f2c26958 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -4,7 +4,7 @@ use crate::alloc::{flags::*, AllocError, KVec}; use core::fmt::{self, Write}; -use core::ops::{self, Deref, DerefMut, Index}; +use core::ops::{Deref, DerefMut}; use crate::error::{code::*, Error}; @@ -40,11 +40,11 @@ impl fmt::Display for BStr { /// # use kernel::{fmt, b_str, str::{BStr, CString}}; /// let ascii = b_str!("Hello, BStr!"); /// let s = CString::try_from_fmt(fmt!("{}", ascii))?; - /// assert_eq!(s.as_bytes(), "Hello, BStr!".as_bytes()); + /// assert_eq!(s.to_bytes(), "Hello, BStr!".as_bytes()); /// /// let non_ascii = b_str!(" From patchwork Mon Mar 17 15:30:05 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tamir Duberstein X-Patchwork-Id: 14019451 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 4711BC282EC for ; Mon, 17 Mar 2025 15:30:29 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id A7AAF10E429; Mon, 17 Mar 2025 15:30:28 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="m5/5cQkS"; dkim-atps=neutral Received: from mail-qk1-f174.google.com (mail-qk1-f174.google.com [209.85.222.174]) by gabe.freedesktop.org (Postfix) with ESMTPS id C411B10E163 for ; Mon, 17 Mar 2025 15:30:22 +0000 (UTC) Received: by mail-qk1-f174.google.com with SMTP id af79cd13be357-7c54c52d8easo612522985a.0 for ; Mon, 17 Mar 2025 08:30:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742225422; x=1742830222; darn=lists.freedesktop.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=z/+hgAgKrFtvgp9dnihutL5Y7eaYtGSxvU+aCTPahrk=; b=m5/5cQkSS2oWFLkKUWngrBfSaRHp3Ygx2DIW4ZEoYv1P370P+jA0jwe9NO6PGGcI3d vuHrf4Lb0BO3xoyGGEKy45twQkFl8OOiNAsffW3HTeUDqGLfKmk+YNRoCb1ylREEkxwo 16k6VjNQ0KCkv9CSXSGl/D11NWpTCzATGJcLfgNpJt+gCpIFMTB7ZoHOFyy/yG0PgKov KunVblGjj9M6Db3UVekoB8gqo/9DrgTp0XfVECh9MiLool0mo0YT1FCNWVhvdwv4dq11 UCngZZqkA3LzPzIwYiHzHw0ZYvC/Jh3Azsnz4bk9pvbekI18NXCivZaF+wJvFKsoTcob BvcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742225422; x=1742830222; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=z/+hgAgKrFtvgp9dnihutL5Y7eaYtGSxvU+aCTPahrk=; b=q7HHxTvC4KiuJlE70WYn2oLUBemj+8KYEEwVe5NrploxBS6wxxP2A34Vp6riTfJdfb JHW+vI25Bgbeqqh/hLWRGzr+w6go0YO+mxHwSdGNkk8YEo0fORiebzZgzMiWLKQt33ks 0eFBbNAqkzlZueMZr5rDR5oVkJgZd9Ei46myLpPr6e7W8BlQcf6IWHWCdM7DlqpzJYlo kDYsUjVwHtHLnbMgG7QZT8hN3Gkq4pDAXtWERnO0yTVKKkfOtN49MybKM1plJFDxG992 iT6ul0Wad3kd6Pq1bp6lc9paVFOErkPCURPP0G1OkqvwIKd3SzbZr0ZbAisawElUXleM MI+Q== X-Forwarded-Encrypted: i=1; AJvYcCW6blYUrnMF3q9tFWXiERn/Xl7fevKdyrwNmTYyC9VgB8mb7bjerLaRt3D9fMYtV9A47uehnM/SruE=@lists.freedesktop.org X-Gm-Message-State: AOJu0YwXa001RxIw+GvYQosKlDht7Xurm9MsQXMeAJOi7CRCv8+J+q/0 LnWnkCxiZmtHUEnAAm6/wvjAhj2zZWSMOU40vPQAduaWOKhKxgmg X-Gm-Gg: ASbGncuF0t3kP5t+ulB025xmLmp6vQM4Rdm99HpTmFBDZmwBPN7+yDPhHTkIr2W2qu+ GrueiLN6N0YDNaI5iMDkg2sGRmFIhcQf1SbdmMajuGgx1HVOaXlXxgYOB3qKqeQQ3Emrh2mMAO6 +CoKfZtKlsG34jUwRO+HrMVercTJXcdCAp5M+ZsquIBvm2g/wbq2ArRGpmmFMUJb+vNZaM1PSt9 LmN43RVjVIFMoH44EzFtfLOIM4DTFPXdB1PuyL7Mtjl7zAH1ocr3wjQfQ/2nV3HPhGGzMNuq5lN RKlr9rbiECnYGQabtCKUmlXVWiUd+YmXlNgor1VcUOEhOgTh+LmwwlBUQ2odqAXrpXu2donoU4g ddvjzkyijo4emb2d+ X-Google-Smtp-Source: AGHT+IHKCLJhzaRz6vWcZqN4ET6OKjd151Da0KrksecWD1rMm7euzj3nlV70nWQ8UJEgIz5m5PJZHw== X-Received: by 2002:a05:6214:319a:b0:6d9:ac3:e730 with SMTP id 6a1803df08f44-6eadded9779mr267368586d6.5.1742225421598; Mon, 17 Mar 2025 08:30:21 -0700 (PDT) Received: from 1.0.0.127.in-addr.arpa ([2600:4041:5be7:7c00:6895:24c6:e898:e320]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6eade34c51fsm55736766d6.109.2025.03.17.08.30.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Mar 2025 08:30:21 -0700 (PDT) From: Tamir Duberstein Date: Mon, 17 Mar 2025 11:30:05 -0400 Subject: [PATCH v9 3/4] rust: replace `kernel::c_str!` with C-Strings MIME-Version: 1.0 Message-Id: <20250317-cstr-core-v9-3-51d6cc522f62@gmail.com> References: <20250317-cstr-core-v9-0-51d6cc522f62@gmail.com> In-Reply-To: <20250317-cstr-core-v9-0-51d6cc522f62@gmail.com> To: Michal Rostecki , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Brendan Higgins , David Gow , Rae Moar , Danilo Krummrich , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Greg Kroah-Hartman , "Rafael J. Wysocki" , Luis Chamberlain , Russ Weight , FUJITA Tomonori , Rob Herring , Saravana Kannan , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Andrew Lunn , Heiner Kallweit , Russell King , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Bjorn Helgaas , Arnd Bergmann Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, dri-devel@lists.freedesktop.org, netdev@vger.kernel.org, devicetree@vger.kernel.org, llvm@lists.linux.dev, linux-pci@vger.kernel.org, Tamir Duberstein X-Mailer: b4 0.15-dev 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" C-String literals were added in Rust 1.77. Replace instances of `kernel::c_str!` with C-String literals where possible and rename `kernel::c_str!` to `c_str_avoid_literals` to clarify its intended use. Closes: https://github.com/Rust-for-Linux/linux/issues/1075 Signed-off-by: Tamir Duberstein --- drivers/net/phy/ax88796b_rust.rs | 7 +++--- drivers/net/phy/qt2025.rs | 5 ++--- rust/kernel/devres.rs | 2 +- rust/kernel/firmware.rs | 4 ++-- rust/kernel/kunit.rs | 7 +++--- rust/kernel/net/phy.rs | 6 ++--- rust/kernel/platform.rs | 4 ++-- rust/kernel/str.rs | 43 ++++++++++++++++++++++-------------- rust/kernel/sync.rs | 4 ++-- rust/kernel/sync/lock/global.rs | 3 ++- rust/macros/module.rs | 2 +- samples/rust/rust_driver_faux.rs | 4 ++-- samples/rust/rust_driver_pci.rs | 4 ++-- samples/rust/rust_driver_platform.rs | 4 ++-- samples/rust/rust_misc_device.rs | 3 +-- 15 files changed, 55 insertions(+), 47 deletions(-) diff --git a/drivers/net/phy/ax88796b_rust.rs b/drivers/net/phy/ax88796b_rust.rs index bc73ebccc2aa..2d24628a4e58 100644 --- a/drivers/net/phy/ax88796b_rust.rs +++ b/drivers/net/phy/ax88796b_rust.rs @@ -5,7 +5,6 @@ //! //! C version of this driver: [`drivers/net/phy/ax88796b.c`](./ax88796b.c) use kernel::{ - c_str, net::phy::{self, reg::C22, DeviceId, Driver}, prelude::*, uapi, @@ -41,7 +40,7 @@ fn asix_soft_reset(dev: &mut phy::Device) -> Result { #[vtable] impl Driver for PhyAX88772A { const FLAGS: u32 = phy::flags::IS_INTERNAL; - const NAME: &'static CStr = c_str!("Asix Electronics AX88772A"); + const NAME: &'static CStr = c"Asix Electronics AX88772A"; const PHY_DEVICE_ID: DeviceId = DeviceId::new_with_exact_mask(0x003b1861); // AX88772A is not working properly with some old switches (NETGEAR EN 108TP): @@ -105,7 +104,7 @@ fn link_change_notify(dev: &mut phy::Device) { #[vtable] impl Driver for PhyAX88772C { const FLAGS: u32 = phy::flags::IS_INTERNAL; - const NAME: &'static CStr = c_str!("Asix Electronics AX88772C"); + const NAME: &'static CStr = c"Asix Electronics AX88772C"; const PHY_DEVICE_ID: DeviceId = DeviceId::new_with_exact_mask(0x003b1881); fn suspend(dev: &mut phy::Device) -> Result { @@ -125,7 +124,7 @@ fn soft_reset(dev: &mut phy::Device) -> Result { #[vtable] impl Driver for PhyAX88796B { - const NAME: &'static CStr = c_str!("Asix Electronics AX88796B"); + const NAME: &'static CStr = c"Asix Electronics AX88796B"; const PHY_DEVICE_ID: DeviceId = DeviceId::new_with_model_mask(0x003b1841); fn soft_reset(dev: &mut phy::Device) -> Result { diff --git a/drivers/net/phy/qt2025.rs b/drivers/net/phy/qt2025.rs index 520daeb42089..78f52efd13e3 100644 --- a/drivers/net/phy/qt2025.rs +++ b/drivers/net/phy/qt2025.rs @@ -9,7 +9,6 @@ //! //! The QT2025 PHY integrates an Intel 8051 micro-controller. -use kernel::c_str; use kernel::error::code; use kernel::firmware::Firmware; use kernel::net::phy::{ @@ -36,7 +35,7 @@ #[vtable] impl Driver for PhyQT2025 { - const NAME: &'static CStr = c_str!("QT2025 10Gpbs SFP+"); + const NAME: &'static CStr = c"QT2025 10Gpbs SFP+"; const PHY_DEVICE_ID: phy::DeviceId = phy::DeviceId::new_with_exact_mask(0x0043a400); fn probe(dev: &mut phy::Device) -> Result<()> { @@ -69,7 +68,7 @@ fn probe(dev: &mut phy::Device) -> Result<()> { // The micro-controller will start running from the boot ROM. dev.write(C45::new(Mmd::PCS, 0xe854), 0x00c0)?; - let fw = Firmware::request(c_str!("qt2025-2.0.3.3.fw"), dev.as_ref())?; + let fw = Firmware::request(c"qt2025-2.0.3.3.fw", dev.as_ref())?; if fw.data().len() > SZ_16K + SZ_8K { return Err(code::EFBIG); } diff --git a/rust/kernel/devres.rs b/rust/kernel/devres.rs index 942376f6f3af..68af33ca2f25 100644 --- a/rust/kernel/devres.rs +++ b/rust/kernel/devres.rs @@ -45,7 +45,7 @@ struct DevresInner { /// # Example /// /// ```no_run -/// # use kernel::{bindings, c_str, device::Device, devres::Devres, io::{Io, IoRaw}}; +/// # use kernel::{bindings, device::Device, devres::Devres, io::{Io, IoRaw}}; /// # use core::ops::Deref; /// /// // See also [`pci::Bar`] for a real example. diff --git a/rust/kernel/firmware.rs b/rust/kernel/firmware.rs index c18b75fc672e..70449571aea6 100644 --- a/rust/kernel/firmware.rs +++ b/rust/kernel/firmware.rs @@ -46,13 +46,13 @@ fn request_nowarn() -> Self { /// # Examples /// /// ```no_run -/// # use kernel::{c_str, device::Device, firmware::Firmware}; +/// # use kernel::{device::Device, firmware::Firmware}; /// /// # fn no_run() -> Result<(), Error> { /// # // SAFETY: *NOT* safe, just for the example to get an `ARef` instance /// # let dev = unsafe { Device::get_device(core::ptr::null_mut()) }; /// -/// let fw = Firmware::request(c_str!("path/to/firmware.bin"), &dev)?; +/// let fw = Firmware::request(c"path/to/firmware.bin", &dev)?; /// let blob = fw.data(); /// /// # Ok(()) diff --git a/rust/kernel/kunit.rs b/rust/kernel/kunit.rs index d858757aeace..527794dcc439 100644 --- a/rust/kernel/kunit.rs +++ b/rust/kernel/kunit.rs @@ -56,10 +56,11 @@ macro_rules! kunit_assert { break 'out; } - static NAME: &'static $crate::str::CStr = $crate::c_str!($name); - static FILE: &'static $crate::str::CStr = $crate::c_str!($file); + static NAME: &'static $crate::str::CStr = $crate::c_str_avoid_literals!($name); + static FILE: &'static $crate::str::CStr = $crate::c_str_avoid_literals!($file); static LINE: i32 = core::line!() as i32 - $diff; - static CONDITION: &'static $crate::str::CStr = $crate::c_str!(stringify!($condition)); + static CONDITION: &'static $crate::str::CStr = + $crate::c_str_avoid_literals!(stringify!($condition)); // SAFETY: FFI call without safety requirements. let kunit_test = unsafe { $crate::bindings::kunit_get_current_test() }; diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs index 652e060e47bd..8129419a3931 100644 --- a/rust/kernel/net/phy.rs +++ b/rust/kernel/net/phy.rs @@ -780,7 +780,6 @@ const fn as_int(&self) -> u32 { /// /// ``` /// # mod module_phy_driver_sample { -/// use kernel::c_str; /// use kernel::net::phy::{self, DeviceId}; /// use kernel::prelude::*; /// @@ -799,7 +798,7 @@ const fn as_int(&self) -> u32 { /// /// #[vtable] /// impl phy::Driver for PhySample { -/// const NAME: &'static CStr = c_str!("PhySample"); +/// const NAME: &'static CStr = c"PhySample"; /// const PHY_DEVICE_ID: phy::DeviceId = phy::DeviceId::new_with_exact_mask(0x00000001); /// } /// # } @@ -808,7 +807,6 @@ const fn as_int(&self) -> u32 { /// This expands to the following code: /// /// ```ignore -/// use kernel::c_str; /// use kernel::net::phy::{self, DeviceId}; /// use kernel::prelude::*; /// @@ -828,7 +826,7 @@ const fn as_int(&self) -> u32 { /// /// #[vtable] /// impl phy::Driver for PhySample { -/// const NAME: &'static CStr = c_str!("PhySample"); +/// const NAME: &'static CStr = c"PhySample"; /// const PHY_DEVICE_ID: phy::DeviceId = phy::DeviceId::new_with_exact_mask(0x00000001); /// } /// diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs index 1297f5292ba9..474f8cb4587b 100644 --- a/rust/kernel/platform.rs +++ b/rust/kernel/platform.rs @@ -120,7 +120,7 @@ macro_rules! module_platform_driver { /// # Example /// ///``` -/// # use kernel::{bindings, c_str, of, platform}; +/// # use kernel::{bindings, of, platform}; /// /// struct MyDriver; /// @@ -129,7 +129,7 @@ macro_rules! module_platform_driver { /// MODULE_OF_TABLE, /// ::IdInfo, /// [ -/// (of::DeviceId::new(c_str!("test,device")), ()) +/// (of::DeviceId::new(c"test,device"), ()) /// ] /// ); /// diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 53d1f2c26958..f1b35fa5823a 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -225,14 +225,13 @@ pub trait CStrExt { /// # Examples /// /// ``` - /// # use kernel::c_str; /// # use kernel::fmt; /// # use kernel::str::CString; - /// let penguin = c_str!(" From patchwork Mon Mar 17 15:30:06 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tamir Duberstein X-Patchwork-Id: 14019452 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 AFC2BC282EC for ; Mon, 17 Mar 2025 15:30:32 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 160FB10E42D; Mon, 17 Mar 2025 15:30:32 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="ApKyF12j"; dkim-atps=neutral Received: from mail-qv1-f44.google.com (mail-qv1-f44.google.com [209.85.219.44]) by gabe.freedesktop.org (Postfix) with ESMTPS id 32A8010E429 for ; Mon, 17 Mar 2025 15:30:25 +0000 (UTC) Received: by mail-qv1-f44.google.com with SMTP id 6a1803df08f44-6e8f6970326so40460616d6.0 for ; Mon, 17 Mar 2025 08:30:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742225424; x=1742830224; darn=lists.freedesktop.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=x9zxS6CUa0/BHcuQ/0Pp0DCveQxLHdMMTHZcqu2K/XY=; b=ApKyF12jCpikwz90qioSzKx6aPOZIJV32WNk/4/JpSeo28Aw3Wr8kiLvHHvjeAos+g ltOzL3ybOSKey9w95W20hyAXlRSmKNCcIORGnj8yC0DxLkYmWl6SKp28C195uq6AoAuZ cSrfIOoutS5W5tiggOESjJdwmnV79B/qmbU5gOgz/f1D8P2IALttATAdlD/J00htqEga lgLGvFmdG/IWNZucUph1MIGrNB1x6eEin4E+wHXiq+VFzDsHJYt9GjSPfNsGMyUZrAcz ZyX2JmA9wSgAg3wpjECI5clu5YipNFMbycjxSoG2WbTzIe0LZkNUCZYWjpkELz1t5c8i imwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742225424; x=1742830224; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=x9zxS6CUa0/BHcuQ/0Pp0DCveQxLHdMMTHZcqu2K/XY=; b=B7P4LVvSXxRo+DRJHc3bh625/YWnWH1yBgec21bH9w1bJ/VrqDOTDTLhb2UflzFO0N Xzl2ylbvZTx80h/dZk4sfcJj+myPjXFSWBW0aKnHmPqM7WmaxDdq7GzNCgMbQZh9joik Hg8WfiTezfe+vH5ZR+Pm11CSFcyDM5iuW7GtgOrDB0DG7N+8alHDkOKYs6FlAIQxqL7R YK+oYxdMEcmL6Ph7b1dBC7L8w2U6/Q0q1/zU0QnAczyoUaXwdaPK9kzvjc2gQGjBBeEr uHNgdiQMi94yUM2pJr3wsLSXaY+i4Xc/Tw2rY/jYCvQoPvzIQTY1jYyM53MMz06m7mD9 stCA== X-Forwarded-Encrypted: i=1; AJvYcCWMMdKvRR21o89sASvnIGEYZqcSwNRauSVQTgk0mXnkZsFH1dckEilzthjKYd0V/jmUHvQRW7jzjl8=@lists.freedesktop.org X-Gm-Message-State: AOJu0YwAtesEcEeKvE8n4C0yP26s2WjuEXw/y0W1rNYtwx0z7YdU1sJB yi1HiV2capWEuatcsJB5cKjBqJ8ORfGPhDnyifnPiDrNP0IHZv3R X-Gm-Gg: ASbGncv5gfSaw/HL4nYf+wYEZ0aZy7LTjFPjmTytP4tMhG/L/G1kg2amlWiuLJlUI92 XjxbE95BOM56kipn3OmonlBNSMYDLhlA+iM2V1D4ZSx72uiCjyf7qGM59ad6aWj+gc9fprxkAT0 T/+ZVzdZoJDkNaVr0zmIpuj9w39cZ8pMTVOfBM8uRIkfu4PrSKBZuNs3pqSzHj0IaITvjtbvyV+ 0RGGH+1QvzKhCqZ6P6sbUx2gOTVcjNXq4HjH9X4UVnx9GabPxN27uAsGJCz18H0Temhs0VA4B6U 7eaZDgnSfQ0H0uXH3LdsM5H8NXiV3OxYwV996ugqTY+BjlHH0J55kBK0FMo79Te5r/BJMNAjcvj MV+o+Hw== X-Google-Smtp-Source: AGHT+IGKApfu++GDospg5BAghnZqA2Q3bluHqs4fOtKhT5CccsHZP+rcXPKZRpvMgiPxDwJYLIjXqw== X-Received: by 2002:ad4:5cc2:0:b0:6e8:fd03:abdc with SMTP id 6a1803df08f44-6eaeaa5dbe3mr249961486d6.18.1742225424080; Mon, 17 Mar 2025 08:30:24 -0700 (PDT) Received: from 1.0.0.127.in-addr.arpa ([2600:4041:5be7:7c00:6895:24c6:e898:e320]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6eade34c51fsm55736766d6.109.2025.03.17.08.30.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Mar 2025 08:30:23 -0700 (PDT) From: Tamir Duberstein Date: Mon, 17 Mar 2025 11:30:06 -0400 Subject: [PATCH v9 4/4] rust: remove core::ffi::CStr reexport MIME-Version: 1.0 Message-Id: <20250317-cstr-core-v9-4-51d6cc522f62@gmail.com> References: <20250317-cstr-core-v9-0-51d6cc522f62@gmail.com> In-Reply-To: <20250317-cstr-core-v9-0-51d6cc522f62@gmail.com> To: Michal Rostecki , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Brendan Higgins , David Gow , Rae Moar , Danilo Krummrich , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Greg Kroah-Hartman , "Rafael J. Wysocki" , Luis Chamberlain , Russ Weight , FUJITA Tomonori , Rob Herring , Saravana Kannan , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Andrew Lunn , Heiner Kallweit , Russell King , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Bjorn Helgaas , Arnd Bergmann Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, dri-devel@lists.freedesktop.org, netdev@vger.kernel.org, devicetree@vger.kernel.org, llvm@lists.linux.dev, linux-pci@vger.kernel.org, Tamir Duberstein X-Mailer: b4 0.15-dev 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" Clean up references to `kernel::str::CStr`. Signed-off-by: Tamir Duberstein --- drivers/gpu/drm/drm_panic_qr.rs | 4 ++-- drivers/net/phy/ax88796b_rust.rs | 1 + drivers/net/phy/qt2025.rs | 1 + rust/kernel/device.rs | 3 +-- rust/kernel/driver.rs | 4 ++-- rust/kernel/error.rs | 6 ++---- rust/kernel/faux.rs | 5 ++++- rust/kernel/firmware.rs | 10 ++-------- rust/kernel/kunit.rs | 6 +++--- rust/kernel/lib.rs | 2 +- rust/kernel/miscdevice.rs | 3 +-- rust/kernel/net/phy.rs | 4 +++- rust/kernel/of.rs | 3 ++- rust/kernel/pci.rs | 3 +-- rust/kernel/platform.rs | 3 +-- rust/kernel/prelude.rs | 5 +---- rust/kernel/str.rs | 19 +++++++++---------- rust/kernel/sync/condvar.rs | 3 ++- rust/kernel/sync/lock.rs | 4 ++-- rust/kernel/sync/lock/global.rs | 5 +++-- rust/kernel/sync/poll.rs | 1 + rust/kernel/workqueue.rs | 1 + rust/macros/module.rs | 2 +- 23 files changed, 47 insertions(+), 51 deletions(-) diff --git a/drivers/gpu/drm/drm_panic_qr.rs b/drivers/gpu/drm/drm_panic_qr.rs index fa55547a1535..61f878347be7 100644 --- a/drivers/gpu/drm/drm_panic_qr.rs +++ b/drivers/gpu/drm/drm_panic_qr.rs @@ -26,8 +26,8 @@ //! * //! * -use core::cmp; -use kernel::{prelude::*, str::CStr}; +use core::{cmp, ffi::CStr}; +use kernel::prelude::*; #[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd)] struct Version(usize); diff --git a/drivers/net/phy/ax88796b_rust.rs b/drivers/net/phy/ax88796b_rust.rs index 2d24628a4e58..68b8e30ae296 100644 --- a/drivers/net/phy/ax88796b_rust.rs +++ b/drivers/net/phy/ax88796b_rust.rs @@ -4,6 +4,7 @@ //! Rust Asix PHYs driver //! //! C version of this driver: [`drivers/net/phy/ax88796b.c`](./ax88796b.c) +use core::ffi::CStr; use kernel::{ net::phy::{self, reg::C22, DeviceId, Driver}, prelude::*, diff --git a/drivers/net/phy/qt2025.rs b/drivers/net/phy/qt2025.rs index 78f52efd13e3..07d026c1f9b5 100644 --- a/drivers/net/phy/qt2025.rs +++ b/drivers/net/phy/qt2025.rs @@ -9,6 +9,7 @@ //! //! The QT2025 PHY integrates an Intel 8051 micro-controller. +use core::ffi::CStr; use kernel::error::code; use kernel::firmware::Firmware; use kernel::net::phy::{ diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index df4bfa5f51ea..a1123206819b 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -6,10 +6,9 @@ use crate::{ bindings, - str::CStr, types::{ARef, Opaque}, }; -use core::{fmt, ptr}; +use core::{ffi::CStr, fmt, ptr}; #[cfg(CONFIG_PRINTK)] use crate::str::CStrExt as _; diff --git a/rust/kernel/driver.rs b/rust/kernel/driver.rs index ec9166cedfa7..9926664d9ba2 100644 --- a/rust/kernel/driver.rs +++ b/rust/kernel/driver.rs @@ -6,8 +6,8 @@ //! register using the [`Registration`] class. use crate::error::{Error, Result}; -use crate::{device, of, str::CStr, try_pin_init, types::Opaque, ThisModule}; -use core::pin::Pin; +use crate::{device, of, try_pin_init, types::Opaque, ThisModule}; +use core::{ffi::CStr, pin::Pin}; use pin_init::{pin_data, pinned_drop, PinInit}; /// The [`RegistrationOps`] trait serves as generic interface for subsystems (e.g., PCI, Platform, diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 58157ec9403c..a2a6f74904d3 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -4,11 +4,9 @@ //! //! C header: [`include/uapi/asm-generic/errno-base.h`](srctree/include/uapi/asm-generic/errno-base.h) -use crate::{ - alloc::{layout::LayoutError, AllocError}, - str::CStr, -}; +use crate::alloc::{layout::LayoutError, AllocError}; +use core::ffi::CStr; use core::fmt; use core::num::NonZeroI32; use core::num::TryFromIntError; diff --git a/rust/kernel/faux.rs b/rust/kernel/faux.rs index 5acc0c02d451..d932186f75b1 100644 --- a/rust/kernel/faux.rs +++ b/rust/kernel/faux.rs @@ -7,7 +7,10 @@ //! C header: [`include/linux/device/faux.h`] use crate::{bindings, device, error::code::*, prelude::*}; -use core::ptr::{addr_of_mut, null, null_mut, NonNull}; +use core::{ + ffi::CStr, + ptr::{addr_of_mut, null, null_mut, NonNull}, +}; /// The registration of a faux device. /// diff --git a/rust/kernel/firmware.rs b/rust/kernel/firmware.rs index 70449571aea6..f400ef81f495 100644 --- a/rust/kernel/firmware.rs +++ b/rust/kernel/firmware.rs @@ -4,14 +4,8 @@ //! //! C header: [`include/linux/firmware.h`](srctree/include/linux/firmware.h) -use crate::{ - bindings, - device::Device, - error::Error, - error::Result, - str::{CStr, CStrExt as _}, -}; -use core::ptr::NonNull; +use crate::{bindings, device::Device, error::Error, error::Result, str::CStrExt as _}; +use core::{ffi::CStr, ptr::NonNull}; /// # Invariants /// diff --git a/rust/kernel/kunit.rs b/rust/kernel/kunit.rs index 527794dcc439..f4e0e58877a0 100644 --- a/rust/kernel/kunit.rs +++ b/rust/kernel/kunit.rs @@ -56,10 +56,10 @@ macro_rules! kunit_assert { break 'out; } - static NAME: &'static $crate::str::CStr = $crate::c_str_avoid_literals!($name); - static FILE: &'static $crate::str::CStr = $crate::c_str_avoid_literals!($file); + static NAME: &'static core::ffi::CStr = $crate::c_str_avoid_literals!($name); + static FILE: &'static core::ffi::CStr = $crate::c_str_avoid_literals!($file); static LINE: i32 = core::line!() as i32 - $diff; - static CONDITION: &'static $crate::str::CStr = + static CONDITION: &'static core::ffi::CStr = $crate::c_str_avoid_literals!(stringify!($condition)); // SAFETY: FFI call without safety requirements. diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index c92497c7c655..2360cba7354b 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -133,7 +133,7 @@ fn init(module: &'static ThisModule) -> impl pin_init::PinInit u32 { /// /// ``` /// # mod module_phy_driver_sample { +/// use core::ffi::CStr; /// use kernel::net::phy::{self, DeviceId}; /// use kernel::prelude::*; /// @@ -807,6 +808,7 @@ const fn as_int(&self) -> u32 { /// This expands to the following code: /// /// ```ignore +/// use core::ffi::CStr; /// use kernel::net::phy::{self, DeviceId}; /// use kernel::prelude::*; /// diff --git a/rust/kernel/of.rs b/rust/kernel/of.rs index 12ea65df46de..087ac8e05551 100644 --- a/rust/kernel/of.rs +++ b/rust/kernel/of.rs @@ -2,7 +2,8 @@ //! Device Tree / Open Firmware abstractions. -use crate::{bindings, device_id::RawDeviceId, prelude::*}; +use crate::{bindings, device_id::RawDeviceId}; +use core::ffi::CStr; /// IdTable type for OF drivers. pub type IdTable = &'static dyn kernel::device_id::IdTable; diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index f7b2743828ae..1b4df605bd2b 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -13,11 +13,10 @@ error::{to_result, Result}, io::Io, io::IoRaw, - str::CStr, types::{ARef, ForeignOwnable, Opaque}, ThisModule, }; -use core::{ops::Deref, ptr::addr_of_mut}; +use core::{ffi::CStr, ops::Deref, ptr::addr_of_mut}; use kernel::prelude::*; /// An adapter for the registration of PCI drivers. diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs index 474f8cb4587b..ca439581129a 100644 --- a/rust/kernel/platform.rs +++ b/rust/kernel/platform.rs @@ -9,12 +9,11 @@ error::{to_result, Result}, of, prelude::*, - str::CStr, types::{ARef, ForeignOwnable, Opaque}, ThisModule, }; -use core::ptr::addr_of_mut; +use core::{ffi::CStr, ptr::addr_of_mut}; /// An adapter for the registration of platform drivers. pub struct Adapter(T); diff --git a/rust/kernel/prelude.rs b/rust/kernel/prelude.rs index 97a66d29f48b..5b530a25d0ff 100644 --- a/rust/kernel/prelude.rs +++ b/rust/kernel/prelude.rs @@ -36,10 +36,7 @@ pub use super::error::{code::*, Error, Result}; -pub use super::{ - str::{CStr, CStrExt as _}, - ThisModule, -}; +pub use super::{str::CStrExt as _, ThisModule}; pub use super::init::InPlaceInit; diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index f1b35fa5823a..fa44856b4ade 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -3,6 +3,7 @@ //! String representations. use crate::alloc::{flags::*, AllocError, KVec}; +use core::ffi::CStr; use core::fmt::{self, Write}; use core::ops::{Deref, DerefMut}; @@ -129,8 +130,6 @@ macro_rules! b_str { }}; } -pub use core::ffi::CStr; - /// Returns a C pointer to the string. // It is a free function rather than a method on an extension trait because: // @@ -371,8 +370,8 @@ fn as_ref(&self) -> &BStr { /// # Examples /// /// ``` +/// # use core::ffi::CStr; /// # use kernel::c_str_avoid_literals; -/// # use kernel::str::CStr; /// const MY_CSTR: &CStr = c_str_avoid_literals!(concat!(file!(), ":", line!(), ": My CStr!")); /// ``` #[macro_export] @@ -382,13 +381,13 @@ macro_rules! c_str_avoid_literals { // too limiting to macro authors, so we rely on the name as a hint instead. ($str:expr) => {{ const S: &'static str = concat!($str, "\0"); - const C: &'static $crate::str::CStr = - match $crate::str::CStr::from_bytes_with_nul(S.as_bytes()) { - Ok(v) => v, - Err(core::ffi::FromBytesWithNulError { .. }) => { - panic!("string contains interior NUL") - } - }; + const C: &'static core::ffi::CStr = match core::ffi::CStr::from_bytes_with_nul(S.as_bytes()) + { + Ok(v) => v, + Err(core::ffi::FromBytesWithNulError { .. }) => { + panic!("string contains interior NUL") + } + }; C }}; } diff --git a/rust/kernel/sync/condvar.rs b/rust/kernel/sync/condvar.rs index b3e7889db44b..8da123f9fb12 100644 --- a/rust/kernel/sync/condvar.rs +++ b/rust/kernel/sync/condvar.rs @@ -8,11 +8,12 @@ use super::{lock::Backend, lock::Guard, LockClassKey}; use crate::{ ffi::{c_int, c_long}, - str::{CStr, CStrExt as _}, + str::CStrExt as _, task::{MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE, TASK_NORMAL, TASK_UNINTERRUPTIBLE}, time::Jiffies, types::Opaque, }; +use core::ffi::CStr; use core::marker::PhantomPinned; use core::ptr; use pin_init::{pin_data, pin_init, PinInit}; diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs index 0b36d4a3d3cc..bf2b3465f390 100644 --- a/rust/kernel/sync/lock.rs +++ b/rust/kernel/sync/lock.rs @@ -7,10 +7,10 @@ use super::LockClassKey; use crate::{ - str::{CStr, CStrExt as _}, + str::CStrExt as _, types::{NotThreadSafe, Opaque, ScopeGuard}, }; -use core::{cell::UnsafeCell, marker::PhantomPinned}; +use core::{cell::UnsafeCell, ffi::CStr, marker::PhantomPinned}; use pin_init::{pin_data, pin_init, PinInit}; pub mod mutex; diff --git a/rust/kernel/sync/lock/global.rs b/rust/kernel/sync/lock/global.rs index 4a040c29faee..7d6d1abf9279 100644 --- a/rust/kernel/sync/lock/global.rs +++ b/rust/kernel/sync/lock/global.rs @@ -5,13 +5,14 @@ //! Support for defining statics containing locks. use crate::{ - str::{CStr, CStrExt as _}, + str::CStrExt as _, sync::lock::{Backend, Guard, Lock}, sync::{LockClassKey, LockedBy}, types::Opaque, }; use core::{ cell::UnsafeCell, + ffi::CStr, marker::{PhantomData, PhantomPinned}, }; @@ -266,7 +267,7 @@ macro_rules! global_lock { $pub enum $name {} impl $crate::sync::lock::GlobalLockBackend for $name { - const NAME: &'static $crate::str::CStr = + const NAME: &'static core::ffi::CStr = $crate::c_str_avoid_literals!(::core::stringify!($name)); type Item = $valuety; type Backend = $crate::global_lock_inner!(backend $kind); diff --git a/rust/kernel/sync/poll.rs b/rust/kernel/sync/poll.rs index e105477a3cb1..d23fd6998ecf 100644 --- a/rust/kernel/sync/poll.rs +++ b/rust/kernel/sync/poll.rs @@ -11,6 +11,7 @@ sync::{CondVar, LockClassKey}, types::Opaque, }; +use core::ffi::CStr; use core::ops::Deref; /// Creates a [`PollCondVar`] initialiser with the given name and a newly-created lock class. diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs index 0cd100d2aefb..25723207c2be 100644 --- a/rust/kernel/workqueue.rs +++ b/rust/kernel/workqueue.rs @@ -135,6 +135,7 @@ use crate::alloc::{AllocError, Flags}; use crate::{prelude::*, sync::Arc, sync::LockClassKey, types::Opaque}; +use core::ffi::CStr; use core::marker::PhantomData; /// Creates a [`Work`] initialiser with the given name and a newly-created lock class. diff --git a/rust/macros/module.rs b/rust/macros/module.rs index 5467a0d539f7..d764fc35dfe5 100644 --- a/rust/macros/module.rs +++ b/rust/macros/module.rs @@ -237,7 +237,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream { }}; impl kernel::ModuleMetadata for {type_} {{ - const NAME: &'static kernel::str::CStr = c\"{name}\"; + const NAME: &'static core::ffi::CStr = c\"{name}\"; }} // Double nested modules, since then nobody can access the public items inside.