From patchwork Tue Jul 23 18:10:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13740268 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id DC315C3DA63 for ; Tue, 23 Jul 2024 18:12:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6B43B6B0085; Tue, 23 Jul 2024 14:12:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6642B6B00AB; Tue, 23 Jul 2024 14:12:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4DE226B00AD; Tue, 23 Jul 2024 14:12:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 2C4986B0085 for ; Tue, 23 Jul 2024 14:12:35 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id D885DA0305 for ; Tue, 23 Jul 2024 18:12:34 +0000 (UTC) X-FDA: 82371812628.19.36038B1 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf03.hostedemail.com (Postfix) with ESMTP id 931E420002 for ; Tue, 23 Jul 2024 18:12:32 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="RV28c3//"; spf=pass (imf03.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721758307; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=oebRsF15AzQ24+fB1SiATNDCryLueJvFEQGm4nTfKlI=; b=swmHwVkvb2YuXVX6SU02u5BbG6/4zCcTLZl5DZ9kB5uYU6dtrrv/5CfvBk2SCmID3Q7dZy hP1BqAtruS8ESiedcQ82+xNLOkrulE2AO9CvvQd5weR5DBA/ifi9W+GG8qKcKmMhtdwiBF dbBTTeE0OwzPf6Fsr51TSmL3YJxCLX8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721758307; a=rsa-sha256; cv=none; b=pkr8kdSbNVLQsObiT7IVr+GVP1cLQbQvPI+4alzPp+w5adihdQVrL6bgAkUJF5JTgc/U9U /0KF3cLm+En95IUSlmm4WmNlThCqG8n1IlpjzEkEFqo+vWF0vBiQlakKpNTZLlStXWPiUd /MPuzugxUa1NwEUGjH1OmBBdf/FBaFg= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="RV28c3//"; spf=pass (imf03.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 05A07CE0ED0; Tue, 23 Jul 2024 18:12:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 124D9C4AF0A; Tue, 23 Jul 2024 18:12:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721758349; bh=ZR4Fo+HZj9co1hH1bISCrg/JiIevl/irl2SNGK/8Ii4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RV28c3//uLcAxuxyQ4q2PzPazVc7pkV/uDYzo584ecF2wHHFrQu1m5aYbduVs8IXV 3a9VimG5b8laP5/9+vN/6hKebRM5UOo85EsqOvFtKaKCWD6oSv78iUsKjGz0tvxyOz 7jRFR5awXL0QQTpgcB7UbgMw4rjF9FmpijHFYBCFSEKqVGenxjo2296lW9rSQxf+bm n/hwej7dKn+JCDtCXjKInZEjFJeal1gEigVrHxw86rZP18+xeMk8EYmH9T/EzdiQuS 4ErY6STPAia77ZCcEkMs2vB764hDJWzWGznqPy4VgOjoLgE2tJtsAMPqJU2XtMZ7Bd zmaR24IdxV2sg== From: Danilo Krummrich To: ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, akpm@linux-foundation.org Cc: daniel.almeida@collabora.com, faith.ekstrand@collabora.com, boris.brezillon@collabora.com, lina@asahilina.net, mcanal@igalia.com, zhiw@nvidia.com, acurrid@nvidia.com, cjia@nvidia.com, jhubbard@nvidia.com, airlied@redhat.com, ajanulgu@redhat.com, lyude@redhat.com, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-mm@kvack.org, Danilo Krummrich Subject: [PATCH v2 17/23] rust: treewide: switch to the kernel `Vec` type Date: Tue, 23 Jul 2024 20:10:06 +0200 Message-ID: <20240723181024.21168-18-dakr@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240723181024.21168-1-dakr@kernel.org> References: <20240723181024.21168-1-dakr@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 931E420002 X-Stat-Signature: tj7w38hd3rpzjyhqyppboc7xaqs78c5w X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1721758352-761274 X-HE-Meta: U2FsdGVkX1/dG3QiVU/kBn3KjKDX5us6CuCqEXJTlADkkua0ac5XsHjiHxSLjTMtwVe0gCniYcB5gTA++xmk6kn9nKruIWgt7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SILVFLRhhpqHKED2Ir70dXnmiUAKj0DyW21yIzmYR02sn88FPeLy97fcCRit6BaduRkgAfm/teIFlMgaRhia+Tq9mWw6F9heHckheTHeCUK5V8Al/mpU0ne+YaIyGGg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Now that we got the kernel `Vec` in place, convert all existing `Vec` users to make use of it. Signed-off-by: Danilo Krummrich --- rust/kernel/str.rs | 12 +++++------- rust/kernel/sync/locked_by.rs | 2 +- rust/kernel/types.rs | 2 +- samples/rust/rust_minimal.rs | 4 ++-- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index bb8d4f41475b..0b6ffbade521 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -2,8 +2,7 @@ //! String representations. -use crate::alloc::{flags::*, vec_ext::VecExt, AllocError}; -use alloc::vec::Vec; +use crate::alloc::{flags::*, AllocError, KVec}; use core::fmt::{self, Write}; use core::ops::{self, Deref, DerefMut, Index}; @@ -790,7 +789,7 @@ fn write_str(&mut self, s: &str) -> fmt::Result { /// assert_eq!(s.is_ok(), false); /// ``` pub struct CString { - buf: Vec, + buf: KVec, } impl CString { @@ -803,7 +802,7 @@ pub fn try_from_fmt(args: fmt::Arguments<'_>) -> Result { let size = f.bytes_written(); // Allocate a vector with the required number of bytes, and write to it. - let mut buf = as VecExt<_>>::with_capacity(size, GFP_KERNEL)?; + let mut buf = KVec::with_capacity(size, GFP_KERNEL)?; // SAFETY: The buffer stored in `buf` is at least of size `size` and is valid for writes. let mut f = unsafe { Formatter::from_buffer(buf.as_mut_ptr(), size) }; f.write_fmt(args)?; @@ -850,10 +849,9 @@ impl<'a> TryFrom<&'a CStr> for CString { type Error = AllocError; fn try_from(cstr: &'a CStr) -> Result { - let mut buf = Vec::new(); + let mut buf = KVec::new(); - as VecExt<_>>::extend_from_slice(&mut buf, cstr.as_bytes_with_nul(), GFP_KERNEL) - .map_err(|_| AllocError)?; + KVec::extend_from_slice(&mut buf, cstr.as_bytes_with_nul(), GFP_KERNEL)?; // INVARIANT: The `CStr` and `CString` types have the same invariants for // the string data, and we copied it over without changes. diff --git a/rust/kernel/sync/locked_by.rs b/rust/kernel/sync/locked_by.rs index babc731bd5f6..b94517231fcc 100644 --- a/rust/kernel/sync/locked_by.rs +++ b/rust/kernel/sync/locked_by.rs @@ -43,7 +43,7 @@ /// struct InnerDirectory { /// /// The sum of the bytes used by all files. /// bytes_used: u64, -/// _files: Vec, +/// _files: KVec, /// } /// /// struct Directory { diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index ee0063a20d89..05521403ed2c 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -158,7 +158,7 @@ unsafe fn from_foreign(_: *const core::ffi::c_void) -> Self {} /// # use kernel::types::ScopeGuard; /// fn example3(arg: bool) -> Result { /// let mut vec = -/// ScopeGuard::new_with_data(Vec::new(), |v| pr_info!("vec had {} elements\n", v.len())); +/// ScopeGuard::new_with_data(KVec::new(), |v| pr_info!("vec had {} elements\n", v.len())); /// /// vec.push(10u8, GFP_KERNEL)?; /// if arg { diff --git a/samples/rust/rust_minimal.rs b/samples/rust/rust_minimal.rs index 2a9eaab62d1c..4aaf117bf8e3 100644 --- a/samples/rust/rust_minimal.rs +++ b/samples/rust/rust_minimal.rs @@ -13,7 +13,7 @@ } struct RustMinimal { - numbers: Vec, + numbers: KVec, } impl kernel::Module for RustMinimal { @@ -21,7 +21,7 @@ fn init(_module: &'static ThisModule) -> Result { pr_info!("Rust minimal sample (init)\n"); pr_info!("Am I built-in? {}\n", !cfg!(MODULE)); - let mut numbers = Vec::new(); + let mut numbers = KVec::new(); numbers.push(72, GFP_KERNEL)?; numbers.push(108, GFP_KERNEL)?; numbers.push(200, GFP_KERNEL)?;