Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp3047571lqp; Mon, 25 Mar 2024 18:39:07 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVW/bUxmbfUxt+zoopayBKpxc3cfzRBXZo/n5P+sQIBPj6id54efEzT58M/FSG8XLxDRIbw2cG/gm9L3oFhTUJgm8D8Nq64rpHX6GnTeQ== X-Google-Smtp-Source: AGHT+IG2n5dbgGNk7dFV5usQdyzoEKCmbeGhUtXQMtXiVJO0ODmU+QeVOBXnvz9q0NrKEiusy/Ez X-Received: by 2002:a05:6358:2910:b0:17f:5c86:b2d8 with SMTP id y16-20020a056358291000b0017f5c86b2d8mr9663580rwb.27.1711417147105; Mon, 25 Mar 2024 18:39:07 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711417147; cv=pass; d=google.com; s=arc-20160816; b=PM8MLUNUnkkpHUswF4K+tEznmFEAqvxzvgLCVwfa7sYlOZnYJP3NlvoFHIcH6veJUa DcLN3yF6Za7UIVNWIjC/uvWoAGxhlPeJ9NfHF0TnUt18kiZg/RjMb9p5A9osPX8usDLh 4GxO1GEtN7Omnau34+AJgg81NLi7QAKw37SxtNkcv6ISUbtM0S0CDpGjj6T0ECK1d4Wx FKztb8mrjPK/1luCxSlnBkRiyOEvGxMxo04YPjDbAxi0dOSHox5rSJODZP6Cfv/pi+JY Uhtv3LGDRt9Q6J/ATdyKzkLMjRoXfzBqQz6vKFQ9xp1dSW0Sww6f2j047XvSQYIuh5iZ /S6g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :dkim-signature; bh=0ZSaVSDwUkk79xkN09Vf+/rrtBLYHXzzW+Z8vmjcWb0=; fh=gS+lZMiIBbaC0jkIR4ovxYLkRR4XpQsAAGkEV5NQFEQ=; b=JvyxKyAsVWr492lG165AE6R9fbgPed9aKqyfmIfXurpxiisGH0iXoZNC6QQSK4DOTv MG36AzL3oJW7G9HjWbQTsk5vBHgisGTGDVfANcc6CyvUDU5BvxSlMAi2MPqyDijlYG4s DRSc8ImrhseeFy9NwbBBxijpz5D2Ygv9fOZ2Xax9N/e3HUPqHnM1KCT+VnVfa8TO3BFH HOo/taHlprTqFFT6TZ1upkPB8uy2zMeme1jcrBa7htlIbGpGIeapTVlhmYSy1pjs24wU nZ+KRWBW1Rhf0X6Ik2xnk0u3kJOJZodE/XoNwihGhM0m/dbkzw73Uj6+ImFpaz3G2BYO Lp4A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="S3icT/pb"; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-118200-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-118200-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id p23-20020a63f457000000b005cf5bf8e7a5si8366061pgk.430.2024.03.25.18.39.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 18:39:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-118200-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="S3icT/pb"; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-118200-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-118200-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id C433FB280B3 for ; Tue, 26 Mar 2024 00:02:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DC0BC2CA8; Tue, 26 Mar 2024 00:02:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="S3icT/pb" Received: from mail-yb1-f172.google.com (mail-yb1-f172.google.com [209.85.219.172]) (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 4E721367; Tue, 26 Mar 2024 00:02:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711411366; cv=none; b=pxQQhScaamt6bNi6QRyeBIwwjRy6MCahfVKI46CiNdF6ZWXR/rRzcXxEsfvUUWFb/KthXeVz89UDGIixIMxrBhMUiqG1F9gM3plLKMU+XLrUhJ/e/E3b+tEzRtminHc6F7qUlDyJUQ47NtFz5PNDMztkvMU4qa+HewWLO/vNc6k= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711411366; c=relaxed/simple; bh=Swt6+93KTELzBLQj7zUwl5HIGVhxbZse+TMgVXv8e0Y=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=AbFM32ABHuVZk8/f8HkV5qjeiScNOy/g6PTCXhPGQ1+hkdTXzNgchy1CbIOCN7G2263x9/Rgp3Sbl/Wm7GwHO0UpfJIk8h1dmVK20YnobBuKz8PqGbUQFOOYsdOHFObR0dvnO2eiDVc+NDfRkGP+BmkxqWFNEjqHspL+baSm55M= 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=S3icT/pb; arc=none smtp.client-ip=209.85.219.172 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 Received: by mail-yb1-f172.google.com with SMTP id 3f1490d57ef6-dc6dcd9124bso4757480276.1; Mon, 25 Mar 2024 17:02:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711411364; x=1712016164; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=0ZSaVSDwUkk79xkN09Vf+/rrtBLYHXzzW+Z8vmjcWb0=; b=S3icT/pb4Y4T9bZrSidffyuyodQTjOQHDb74QiQjZffJ4Evn4JpnrVZwydAkdcymkw w418xl1pzYCuuLFDI9DxTnUi73oVvH9URxFZRUweWNPFMZD6tR4GwPPsh9CO1C/u9zb2 IRbHIq/EWOn+rPJhzywLKgoFJ/L0PqmVzWVRUlbu//9XF11/hS0qd3VcOR3sCljqNsvt SysIBLPWsA62+xA4oewG9O5cLeEvNpygiUvLM2LaWswd/BauOZMgU5JQG02kHl1wgpIu vryuP+CKjFtgoAViT4pTYoq0g5LHvbIt7bUtSuuehDfCEOwiIK9VVNHvqLzxKpfBp0DP s8Zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711411364; x=1712016164; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=0ZSaVSDwUkk79xkN09Vf+/rrtBLYHXzzW+Z8vmjcWb0=; b=JrJ95UowMd7O0OxRAcBQvTmeL3pqNyS3qMocbxAvFqvQR7QgbCTskG4V78PNvEPhEq MQeqH0jbNqdEF033tELc2rAumlPaiyXzM28Biid1iRmHygY/EaVpLi6V2Nid4cWG9nao 61j0ZImdp8lTmmVUIV2iQ5aYkWH4xyDdse1MPcU5wUS0xZTo2y5keTXDOYzFR+3WcfdL iEE3w3LTNhwE2JwJ3icn8HIQDiFAI/E6tYum26uFZYMR86IZj1BY3IOAI3Fa8lJRHt7/ zt2MTFOVxGpGpB8DNE94VvtbDl3+or5zdWzGa4vt2LM847xFgTpZzfJDCLnDqKOI0ar3 tjKA== X-Forwarded-Encrypted: i=1; AJvYcCX7BiqLiH/dwOIhoZu8aIWNQwGX9XabZYLodDtBZ1KgQp+ebMCHOz/poctdF2MVtA6erkp9wbvIKtHgkjQMQHN4Pu7+3t0uI9wgBqew X-Gm-Message-State: AOJu0YxmTIW9NyEFSeLdlj4HmE5fZM2hZl9DBJH8k1A98qJ1b2HosBKG 2Ug5itUgVT2DeorzUXxMlcKVn5xZTYDkZtB23OIMB1gk2cRiskN3V0bq4hesIc23WsFcTGYkoJf wLg+qta4z3pzQHWdK1UywNnazllfzQtaPMVQ= X-Received: by 2002:a25:361e:0:b0:dcc:9dcc:a433 with SMTP id d30-20020a25361e000000b00dcc9dcca433mr6546513yba.54.1711411364125; Mon, 25 Mar 2024 17:02:44 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240325195418.166013-1-wedsonaf@gmail.com> <20240325195418.166013-3-wedsonaf@gmail.com> In-Reply-To: From: Wedson Almeida Filho Date: Mon, 25 Mar 2024 21:02:33 -0300 Message-ID: Subject: Re: [PATCH 02/10] rust: alloc: introduce the `VecExt` trait To: Benno Lossin Cc: rust-for-linux@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , Alice Ryhl , linux-kernel@vger.kernel.org, Wedson Almeida Filho Content-Type: text/plain; charset="UTF-8" On Mon, 25 Mar 2024 at 19:05, Benno Lossin wrote: > > On 25.03.24 20:54, Wedson Almeida Filho wrote:> From: Wedson Almeida Filho > > > > Make `try_with_capacity`, `try_push`, and `try_extend_from_slice` > > methods available in `Vec` even though it doesn't implement them. It is > > implemented with `try_reserve` and `push_within_capacity`. > > > > This is in preparation for switching to the upstream `alloc` crate. > > > > Suggested-by: Gary Guo > > Signed-off-by: Wedson Almeida Filho > > --- > > rust/kernel/alloc.rs | 1 + > > rust/kernel/alloc/vecext.rs | 52 +++++++++++++++++++++++++++++++++++++ > > rust/kernel/lib.rs | 1 + > > rust/kernel/prelude.rs | 2 ++ > > 4 files changed, 56 insertions(+) > > create mode 100644 rust/kernel/alloc/vecext.rs > > > > diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs > > index ccd4149932c3..8ad57a2e693e 100644 > > --- a/rust/kernel/alloc.rs > > +++ b/rust/kernel/alloc.rs > > @@ -5,3 +5,4 @@ > > #[cfg(not(test))] > > #[cfg(not(testlib))] > > mod allocator; > > +pub mod vecext; > > diff --git a/rust/kernel/alloc/vecext.rs b/rust/kernel/alloc/vecext.rs > > new file mode 100644 > > index 000000000000..59e92bab534e > > --- /dev/null > > +++ b/rust/kernel/alloc/vecext.rs > > @@ -0,0 +1,52 @@ > > +// SPDX-License-Identifier: GPL-2.0 > > + > > +//! Extensions to [`Vec`] for fallible allocations. > > + > > +use alloc::{collections::TryReserveError, vec::Vec}; > > +use core::result::Result; > > + > > +/// Extensions to [`Vec`]. > > +pub trait VecExt: Sized { > > + /// Creates a new [`Vec`] instance with at least the given capacity. > > + fn try_with_capacity(capacity: usize) -> Result; > > + > > + /// Appends an element to the back of the [`Vec`] instance. > > + fn try_push(&mut self, v: T) -> Result<(), TryReserveError>; > > + > > + /// Pushes clones of the elements of slice into the [`Vec`] instance. > > + fn try_extend_from_slice(&mut self, other: &[T]) -> Result<(), TryReserveError> > > + where > > + T: Clone; > > All of the non `try_` prefix functions require > `not(no_global_oom_handling)`, so we could also drop the `try_` prefix > here. > What do you think? Yes, I do drop the `try_` prefixes in patch 7. Patches 1 through 4 remove the fork with minimal changes. Patches 5 to 10 introduce new functions with flags and drop `allocator_api`. > > +} > > + > > +impl VecExt for Vec { > > + fn try_with_capacity(capacity: usize) -> Result { > > + let mut v = Vec::new(); > > + v.try_reserve(capacity)?; > > + Ok(v) > > + } > > + > > + fn try_push(&mut self, v: T) -> Result<(), TryReserveError> { > > + if let Err(retry) = self.push_within_capacity(v) { > > + self.try_reserve(1)?; > > + let _ = self.push_within_capacity(retry); > > + } > > + Ok(()) > > + } > > + > > + fn try_extend_from_slice(&mut self, other: &[T]) -> Result<(), TryReserveError> > > + where > > + T: Clone, > > + { > > + let extra_cap = self.capacity() - self.len(); > > + if extra_cap > 0 { > > + self.try_reserve(extra_cap)?; > > + } > > I am confused, why are you doing this? Did you mean to do this?: > let extra_cap = self.capacity() - self.len() - other.len(); I originally meant `extracap = other.len() - self.capacity() + self.len()` because originally I thought `additional` meant additional capacity (so the final capacity would be original capacity plus `additional`). But after I looked at the code I learned that `additional` is really the minimum capacity. Then I decided that we don't need this check at all in `try_extend_from_slice`, which I removed in patch 7, I just call `reserve` directly with `other.len()`. The code happens to work now because the `try_push` calls below ensure the capacity gets expanded. In any case, I'll fix this in v2. > -- > Cheers, > Benno > > > + > > + for item in other { > > + self.try_push(item.clone())?; > > + } > > + > > + Ok(()) > > + } > > +} > > diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs > > index 51f30e55bd00..7f2841a18d05 100644 > > --- a/rust/kernel/lib.rs > > +++ b/rust/kernel/lib.rs > > @@ -19,6 +19,7 @@ > > #![feature(offset_of)] > > #![feature(receiver_trait)] > > #![feature(unsize)] > > +#![feature(vec_push_within_capacity)] > > > > // Ensure conditional compilation based on the kernel configuration works; > > // otherwise we may silently break things like initcall handling. > > diff --git a/rust/kernel/prelude.rs b/rust/kernel/prelude.rs > > index ae21600970b3..a0177f195dec 100644 > > --- a/rust/kernel/prelude.rs > > +++ b/rust/kernel/prelude.rs > > @@ -14,6 +14,8 @@ > > #[doc(no_inline)] > > pub use core::pin::Pin; > > > > +pub use crate::alloc::vecext::VecExt; > > + > > #[doc(no_inline)] > > pub use alloc::{boxed::Box, vec::Vec}; > > > > -- > > 2.34.1 > >