From patchwork Mon Aug 19 15:36:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Rostecki X-Patchwork-Id: 13768504 Received: from mail-ed1-f65.google.com (mail-ed1-f65.google.com [209.85.208.65]) (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 2303412EBDB; Mon, 19 Aug 2024 15:39:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.65 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724081993; cv=none; b=FW+8iEUnaaqn4/4VEuJFH80/slZYNRqYRtU4g3qPuwUWvg0+5IgJHnXhkGNtsnEl3u7xLdBzhXsrWW7BKv3mODh/y0EurZuH1IDw9v7/nVEJK2dGgHcJu+WXqYxAi3aZrnRnbR/+KjryzPl82Pebn4fw7NWplSDh+qZGcpx78pI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724081993; c=relaxed/simple; bh=EQrzrpEWS0Msjzd7Lg1pS6gIdsyAJw24yaQ6qXtEOME=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=Aghx+NZphP0MTAP/4ybDHjHkXs/KZlne1EKQuPUMmCOeIgi5fV3gzW6s0RcG2Px4DyEj4zjjN6KXltDhUJkPbItnTSPxa6XS4IFvTkW520ii/LP7KstsQGI6a13xhfOnBCqQWTPlDkpZG39anKalQoFYIJJGr/+F0DaTxMjRbKY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=mn6yUnj3; arc=none smtp.client-ip=209.85.208.65 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mn6yUnj3" Received: by mail-ed1-f65.google.com with SMTP id 4fb4d7f45d1cf-5bec7ee6f44so3915394a12.0; Mon, 19 Aug 2024 08:39:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724081987; x=1724686787; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=5UENWL/7CHDqF5f98/TWU/Ijyp7+vq9Ti1n3vzOBH38=; b=mn6yUnj3xaJwASvDSsweErEAbvTJWUjyyLwGkj9vkKYRG4s9ZXLUaP4qfBG6U9IkCZ lF1g4EW0A6WurpvCTszLSaBuEXfT81ecyN6GxkqKJl8x4sgOnBtwUkM2n6ujt8uMRrE6 pYcPX7hqbpI9Ykt8FpI2IyeLKaVEWWnDX+BkXeLJ/B6ZsGIzqSRNpe5ugHJDIq0NqDOj rAkdR+nIAse7z8X8yXRiF8EPHVxnkxhVZSL3mVN8O8fN6z9n2Q0mtFbcHuXAYDbtahPU 6aqpYm1X9IW4TDm9q0VNwWJeDt4HAVLpnDLGIGTmd2CTIbuws2lSJZAnuv5kj/fxgG9e EjNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724081987; x=1724686787; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=5UENWL/7CHDqF5f98/TWU/Ijyp7+vq9Ti1n3vzOBH38=; b=TzdIshSOS1a4lLh5t4xZYbNJK9A/BI13ZwNacHax4Wf6Fv5AHy/MAIw7/GEc4rA91c zyQ+Vr7wz1WCLUKwBV6c9qBeoxTYFgHNOAlfUVO43MNA0QGenGEX76VFVvF/TeUYyq3m mI4KMQxykD4ivpTNB2GoC0nWkbVNOGmETtczSvTwb14EXKPYy8xzAls+8+4OsBwi8m03 zs1N5p/VA9a3Aqw5qPd26hcbm9YQSZLGgmSPc+C3hehjbf3C+oeEL7b8Bpai9cjDPfZ0 qA9EaH30B/Lpvd6phjZPukWn7H18khtQBBh83MwSjmEtdMnyruQppGRmy1ppZMBDjyyI /2EA== X-Forwarded-Encrypted: i=1; AJvYcCWU2JU7dDuqIJggI8Ufg+68etsB58WgW87VhGZKBmBs8MBmy2g/CdWP0J4SrsCJjiCvCDhyKHPqZNVxKRlo5ydgats87ZT5X+gvFygnrWYmN5B+ydx6WncnUS7fMhdYrNPiygRUeoVbZbPc5vQ2aRp64BAck0N6VvYLDIW3F7IdEXcY3HTzC/zxHefOYBZLSBP/u8bEOpZgtYGk5TOX9UBlSfKI164= X-Gm-Message-State: AOJu0Yw3wOeoFHuOpsrw0Naih8zy5wRfCTbhZiVdnNtuvlqwXLNkDOKY efvuR1QmmxuP9ekpHlpVtAuhyk4m0kgZJC0dqNwG7qxuiJDYTFCh X-Google-Smtp-Source: AGHT+IFlntUuZMVFKiqJGIFNQs+PmnLPvyBpd4e7M8ngMvbMLO/mP8M9AFJEMCuShjOYqOcWwebPGA== X-Received: by 2002:a17:907:f719:b0:a72:8762:1f5d with SMTP id a640c23a62f3a-a8392a11c1bmr783811366b.55.1724081986486; Mon, 19 Aug 2024 08:39:46 -0700 (PDT) Received: from navi.fritz.box ([2a02:2454:299:1800:db9c:cb9b:300b:5a61]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a8383935698sm643232266b.102.2024.08.19.08.39.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Aug 2024 08:39:45 -0700 (PDT) From: Michal Rostecki X-Google-Original-From: Michal Rostecki To: Cc: Michal Rostecki , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Brendan Higgins , David Gow , Rae Moar , FUJITA Tomonori , Trevor Gross , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Martin Rodriguez Reboredo , Finn Behrens , Manmohan Shukla , Valentin Obst , Yutaro Ohno , Asahi Lina , Danilo Krummrich , Tiago Lam , Charalampos Mitrodimas , Tejun Heo , Roland Xu , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, netdev@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH RESEND v5] rust: str: Use `core::CStr`, remove the custom `CStr` implementation Date: Mon, 19 Aug 2024 17:36:35 +0200 Message-ID: <20240819153656.28807-2-vadorovsky@protonmail.com> X-Mailer: git-send-email 2.44.2 Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Michal Rostecki `CStr` became a part of `core` library in Rust 1.75. This change replaces the custom `CStr` implementation with the one from `core`. `core::CStr` behaves generally the same as the removed implementation, with the following differences: - It does not implement `Display`. - It does not provide `from_bytes_with_nul_unchecked_mut` method. - It has `as_ptr()` method instead of `as_char_ptr()`, which also returns `*const c_char`. The first two differences are handled by providing the `CStrExt` trait, with `display()` and `from_bytes_with_nul_unchecked_mut()` methods. `display()` returns a `CStrDisplay` wrapper, with a custom `Display` implementation. `DerefMut` implementation for `CString` is removed here, as it's not being used anywhere. Signed-off-by: Michal Rostecki --- rust/kernel/error.rs | 7 +- rust/kernel/kunit.rs | 18 +- rust/kernel/net/phy.rs | 2 +- rust/kernel/prelude.rs | 4 +- rust/kernel/str.rs | 465 ++++++------------------------------ rust/kernel/sync/condvar.rs | 5 +- rust/kernel/sync/lock.rs | 6 +- rust/kernel/workqueue.rs | 2 +- scripts/rustdoc_test_gen.rs | 4 +- 9 files changed, 93 insertions(+), 420 deletions(-) diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 6f1587a2524e..6a2bb11ac09c 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -4,10 +4,11 @@ //! //! C header: [`include/uapi/asm-generic/errno-base.h`](srctree/include/uapi/asm-generic/errno-base.h) -use crate::{alloc::AllocError, str::CStr}; +use crate::alloc::AllocError; use alloc::alloc::LayoutError; +use core::ffi::CStr; use core::fmt; use core::num::TryFromIntError; use core::str::Utf8Error; @@ -151,7 +152,7 @@ pub fn name(&self) -> Option<&'static CStr> { None } else { // SAFETY: The string returned by `errname` is static and `NUL`-terminated. - Some(unsafe { CStr::from_char_ptr(ptr) }) + Some(unsafe { CStr::from_ptr(ptr) }) } } @@ -173,7 +174,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { None => f.debug_tuple("Error").field(&-self.0).finish(), // SAFETY: These strings are ASCII-only. Some(name) => f - .debug_tuple(unsafe { core::str::from_utf8_unchecked(name) }) + .debug_tuple(unsafe { core::str::from_utf8_unchecked(name.to_bytes()) }) .finish(), } } diff --git a/rust/kernel/kunit.rs b/rust/kernel/kunit.rs index 0ba77276ae7e..79a50ab59af0 100644 --- a/rust/kernel/kunit.rs +++ b/rust/kernel/kunit.rs @@ -56,13 +56,15 @@ macro_rules! kunit_assert { break 'out; } - static FILE: &'static $crate::str::CStr = $crate::c_str!($file); + static FILE: &'static core::ffi::CStr = $file; static LINE: i32 = core::line!() as i32 - $diff; - static CONDITION: &'static $crate::str::CStr = $crate::c_str!(stringify!($condition)); + static CONDITION: &'static core::ffi::CStr = $crate::c_str!(stringify!($condition)); // SAFETY: FFI call without safety requirements. let kunit_test = unsafe { $crate::bindings::kunit_get_current_test() }; if kunit_test.is_null() { + use kernel::str::CStrExt; + // The assertion failed but this task is not running a KUnit test, so we cannot call // KUnit, but at least print an error to the kernel log. This may happen if this // macro is called from an spawned thread in a test (see @@ -71,11 +73,13 @@ macro_rules! kunit_assert { // // This mimics KUnit's failed assertion format. $crate::kunit::err(format_args!( - " # {}: ASSERTION FAILED at {FILE}:{LINE}\n", - $name + " # {}: ASSERTION FAILED at {}:{LINE}\n", + $name.display(), + FILE.display(), )); $crate::kunit::err(format_args!( - " Expected {CONDITION} to be true, but is false\n" + " Expected {} to be true, but is false\n", + CONDITION.display(), )); $crate::kunit::err(format_args!( " Failure not reported to KUnit since this is a non-KUnit task\n" @@ -98,12 +102,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: FILE.as_ptr(), line: LINE, }); static ASSERTION: UnaryAssert = UnaryAssert($crate::bindings::kunit_unary_assert { assert: $crate::bindings::kunit_assert {}, - condition: CONDITION.as_char_ptr(), + condition: CONDITION.as_ptr(), expected_true: true, }); diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs index fd40b703d224..19f45922ec42 100644 --- a/rust/kernel/net/phy.rs +++ b/rust/kernel/net/phy.rs @@ -502,7 +502,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: T::NAME.as_ptr().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/prelude.rs b/rust/kernel/prelude.rs index b37a0b3180fb..b0969ca78f10 100644 --- a/rust/kernel/prelude.rs +++ b/rust/kernel/prelude.rs @@ -12,7 +12,7 @@ //! ``` #[doc(no_inline)] -pub use core::pin::Pin; +pub use core::{ffi::CStr, pin::Pin}; pub use crate::alloc::{box_ext::BoxExt, flags::*, vec_ext::VecExt}; @@ -35,7 +35,7 @@ pub use super::error::{code::*, Error, Result}; -pub use super::{str::CStr, ThisModule}; +pub use super::ThisModule; pub use super::init::{InPlaceInit, Init, PinInit}; diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index bb8d4f41475b..97a298a44b96 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -4,8 +4,9 @@ use crate::alloc::{flags::*, vec_ext::VecExt, AllocError}; use alloc::vec::Vec; +use core::ffi::CStr; use core::fmt::{self, Write}; -use core::ops::{self, Deref, DerefMut, Index}; +use core::ops::Deref; use crate::error::{code::*, Error}; @@ -41,11 +42,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)).unwrap(); - /// assert_eq!(s.as_bytes(), "Hello, BStr!".as_bytes()); + /// assert_eq!(s.to_bytes(), "Hello, BStr!".as_bytes()); /// /// let non_ascii = b_str!("