From patchwork Sun Feb 2 16:31:20 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tamir Duberstein X-Patchwork-Id: 13956542 Received: from mail-qk1-f173.google.com (mail-qk1-f173.google.com [209.85.222.173]) (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 7CF071FBC83; Sun, 2 Feb 2025 16:31:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738513896; cv=none; b=kiUwFbF8hsgnGEmykYJW+mnco0np2JsqkWiNlZIpw0PWF+TwrehfdJykun8wLBuw4XG/GpmvGZzmnH+ahupDrRFF6q2JAM+9GpnE6CCV9/q5a4j0QYV3ok3wBEOYM3ExB7uR8/jfHoQW4ZGJonJHHdB44fwnEkhpozwEobSuqyo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738513896; c=relaxed/simple; bh=I8KHV9qqtP8XO38JFSzGPLiRuygDD4fZVIlB80rmzL4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=l2wgnm4Du2llx7mksc3KIlwPv6xSyFDfUKh+0sBNfGBCDLm7NeH1K2lS34Ad4JaUeIicO3j/cZPxximHINHEcIvJNc7Aj0z23+C8FoGROr5C9j3wNd2O67ZFQy92yy8bQexBVABIFKslj09d67rvhN1FW2tZH3D6YxiNoeouSyc= 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=FI8mgijs; arc=none smtp.client-ip=209.85.222.173 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="FI8mgijs" Received: by mail-qk1-f173.google.com with SMTP id af79cd13be357-7b6e9317a2aso409443985a.0; Sun, 02 Feb 2025 08:31:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1738513893; x=1739118693; darn=vger.kernel.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=Wy2ZrB6fja+LbsX27rvFHvMz2FLWlQCHNF4NZp66JmE=; b=FI8mgijsKDJzzI5waNaYAB0Mo7+h/kZ2Th/ksc1Dw2pRDHS/YekFpC6Ffs78bR9OxI x7sqK88ssTMiyJ6VjMynMoADqrNqNhK327PxQIbFqRCMdXCtljZBLDuK477qZa6En2fQ Onnz1iu1Gq6j4o8sc8042K7HqVodPVdT/dZDfHIA/6g1QrUs0TGjv49gyHVvkxGQGork CchqZ8YvLC2pKG/IauvWZ+fnSNeA9cw6BfJtdd4OkmMV5fNEVTtGf3xj+6CFUfUW/Uvx 8ZpmCa4TPGZt1POHFBByA7aNNgIVRBIvoIH8LZX02pAC5f/sbnGwoXo5+tv5c2cNMkar P85A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738513893; x=1739118693; 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=Wy2ZrB6fja+LbsX27rvFHvMz2FLWlQCHNF4NZp66JmE=; b=vb5EKdWVuAZU1uVM+ti5O/35BhajPikvtqI+S/iivFiMJnMPO/DpyD8Rrqd7SBlxOf E4++DqqXyF7S/nxTUAnZzjPPESL9Thw8Iwxb745k4nzdXWo0hfDtEa7L6IsUrspZeafX euR5iVscEsgmXikEuRdxwK3QThZW5TjIAns7jC+XxThgnoUmJqMCnVU7/4/ucVfYXccF w9HLHBABhcbrBd+RNaJQ+QgNybms83XiC822QJl6DTMiYmgauV7K4zxZ/jaBpuPkI6u+ QjzyncwkJMEkExeQZVHl6eN4VVAN3He6wAXx6tRUjFQ8k7yLW2noy1jV3UKwNA3wx7vs SD6A== X-Forwarded-Encrypted: i=1; AJvYcCX7twWGExTaNI7PMq21WnktJvhNc+wMqRGETxGtsc4z6V9XvBh+veHWWKQIJRUFGnYK/P+mxAlJT2Mu2As=@vger.kernel.org X-Gm-Message-State: AOJu0YwfGceyACiTN+0qbBiQoDu8k/SOmYGQ8KwWzyWZUjfS4lA0nmwZ QXmfVJwH2WiCPdEIxI+wHRkyNgBknv8zMYkqgaTsDAqO7YHI8tnl X-Gm-Gg: ASbGncsFP2ibIaIlRMLTb0PXwP25t9nakdGNJ+yyl0oOyM7xTFpSXPtuHHuKI8x2Paa NZEKGGSsF/OeLsVo0ve0O1cUsGHtQITR4iswYvCtlA4L+FudTNscVvWy4s+ICrrivobClPO4/Rv bmB6PGPAVwNTZo5Kw2xPTQTi0xHDxLaqdgSUsGValX5sWm0MkBRczETpuulFnwdUtzo4vqPI4x3 JJMl/SWl5M18zgrShUyRVHwgy39dBI/PvTqDzOjd/HfnEOPZp2KANZ138qyrHH6Pw9QTWPFPKtD GM0cycFSQM0R0l0AKr+1c2p7rVAFfPZQAFTlO2I= X-Google-Smtp-Source: AGHT+IEfq5qfPIuleu/gKswH4zJkmmqYLeugQt/NbMerzELwRxsZwFGcr9flUGBRDsMgu09R/LLP6A== X-Received: by 2002:a05:620a:244e:b0:7b6:cef9:4f10 with SMTP id af79cd13be357-7c011d95f51mr1266766985a.3.1738513892959; Sun, 02 Feb 2025 08:31:32 -0800 (PST) Received: from tamird-mac.local ([2600:4041:5be7:7c00:f07f:fb14:288d:6f1e]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7c00a8bb957sm421307485a.14.2025.02.02.08.31.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Feb 2025 08:31:32 -0800 (PST) From: Tamir Duberstein Date: Sun, 02 Feb 2025 11:31:20 -0500 Subject: [PATCH v7 3/4] rust: replace `kernel::c_str!` with C-Strings Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250202-cstr-core-v7-3-da1802520438@gmail.com> References: <20250202-cstr-core-v7-0-da1802520438@gmail.com> In-Reply-To: <20250202-cstr-core-v7-0-da1802520438@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 Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Tamir Duberstein X-Mailer: b4 0.15-dev 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 | 37 +++++++++++++++++++++++------------- rust/kernel/sync.rs | 4 ++-- rust/kernel/sync/lock/global.rs | 3 ++- rust/macros/module.rs | 2 +- samples/rust/rust_driver_pci.rs | 4 ++-- samples/rust/rust_driver_platform.rs | 4 ++-- samples/rust/rust_misc_device.rs | 3 +-- 14 files changed, 50 insertions(+), 42 deletions(-) diff --git a/drivers/net/phy/ax88796b_rust.rs b/drivers/net/phy/ax88796b_rust.rs index 8c7eb009d9fc..b07d36f45a43 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 1ab065798175..2f4ba85a0d73 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 e75db4d825ce..93e375e75ca0 100644 --- a/rust/kernel/firmware.rs +++ b/rust/kernel/firmware.rs @@ -40,13 +40,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 9f40ea744fc2..3d812edec057 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 c60205606aee..631a81e79a9f 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 d1752a978af4..f23a433b2273 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 0c4189a478b4..27911eaede27 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -261,14 +261,13 @@ pub trait CStrExt { /// # Examples /// /// ``` - /// # use kernel::c_str; /// # use kernel::fmt; /// # use kernel::str::CString; - /// let penguin = c_str!("