From mboxrd@z Thu Jan 1 00:00:00 1970 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 CA32DC3DA63 for ; Wed, 24 Jul 2024 20:32:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3DB2C6B0083; Wed, 24 Jul 2024 16:32:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 38B3B6B0088; Wed, 24 Jul 2024 16:32:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 279896B0089; Wed, 24 Jul 2024 16:32:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 0A7646B0083 for ; Wed, 24 Jul 2024 16:32:57 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 96AD980BE2 for ; Wed, 24 Jul 2024 20:32:56 +0000 (UTC) X-FDA: 82375795152.23.15B9DE3 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf22.hostedemail.com (Postfix) with ESMTP id D6CC7C0023 for ; Wed, 24 Jul 2024 20:32:53 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="NiJ6/9QS"; spf=pass (imf22.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 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=1721853125; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=KK3G5OVmXhZDblmfUMXlPsDdrcDRVwaDKnGORVXPk5M=; b=jWkW1DcTp9KASVMPITAJYbunos+2zhqIpSoTFsWspNu7tuF93G5XeNEo7vNDM7uABER2Y0 JcS2bKbirsSBGRdQLEAnAkNMd7jckXPn1/ieUbM0pSIy4ipi2zivzJPz0CvxXv1quJpT// CAMLm5NFHLauclLSZiNSh1yABM4uT0E= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721853125; a=rsa-sha256; cv=none; b=fkyjxzOkBxXvXYPZALaP6oLV4LVPRIwzyesYOTjlB4wwvhVF8EtjDNasONvfc2TNTPwxmR Ybh3f0/siPhC3dBem+/G6O5nYqIt8dszXr3r1qGNnegow0Gn78rUeyWKlTLQno+ef7qBvj ab8oXk6CiBRkidOmGvGCypeQSR3La4g= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="NiJ6/9QS"; spf=pass (imf22.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 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 dfw.source.kernel.org (Postfix) with ESMTP id 5BF6B61232; Wed, 24 Jul 2024 20:32:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BCA8AC32781; Wed, 24 Jul 2024 20:32:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721853171; bh=aMn98SjH3IZ9bjl+8IXsiVppV9Tm41peLtZlAfFNBk4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=NiJ6/9QSMaxY8aTVfL+EY8OPRr9GHh5Pq767MwZ9SVpg3YHBkDmGmq2qWrjqEjbjJ 70q+61abPXwFFuqsbuW6On8V7ln+4QeqVPz/F71uS+vTq8a111e+5I2AFN8l+atgdA OBZF3Qse3vU5ajFzLJx3U8zIUQqOktqnev5MPaA7YLa21BaW8Ywxi3ZK2MG+rVcvMp j7o6AZPPBX9x4afSw5bpvhevG9Grpin1G6PbII6/W4G7SdCAUvCDaRMtA7cxWCHw5Q XbbynSDoShB2iAqrgu/VSQcemu9RXjAfoeSQG5wM/PcO/XxIl3o6cMIWvecL3l6lWP RAolo696nW11Q== Date: Wed, 24 Jul 2024 22:32:41 +0200 From: Danilo Krummrich To: Heghedus Razvan Cc: 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, 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 Subject: Re: [PATCH v2 16/23] rust: alloc: implement `collect` for `IntoIter` Message-ID: References: <20240723181024.21168-1-dakr@kernel.org> <20240723181024.21168-17-dakr@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: D6CC7C0023 X-Stat-Signature: gjimeg4wfdw6wb9me75gxjxypjo395ti X-HE-Tag: 1721853173-85547 X-HE-Meta: U2FsdGVkX1+ByQLdJMwd46vRa4vNuEgzTnmKaOcz4hjhd6hURC4aJubjk6C2IMfMU0c4SleOqyNMgFJfcz1bjGpvh/WazeiG0jq9k53TD3FhgxNT76HLrWopukDm3OYnOcn5MGWnSDIjGn8rvy8Pa/GgIhtLpWi5wdSLXaPLr0yw/aLYD3CdIRO7PKJ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4WkOKH31G75T47mdeOcebVGpdH9JNzhGpuGf+ovH/pjAvQZNw71GG3UzCjGcC8Sii3BSV5sXz7MnvMuKi/UCowm4ztGqhFrnmRXhlhTfaduLl9iwTHqr4XGk0js= 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: On Wed, Jul 24, 2024 at 02:35:46PM +0000, Heghedus Razvan wrote: > On Tue Jul 23, 2024 at 9:10 PM EEST, Danilo Krummrich wrote: > > Currently, we can't implement `FromIterator`. There are a couple of > > issues with this trait in the kernel, namely: > > > > - Rust's specialization feature is unstable. This prevents us to > > optimze for the special case where `I::IntoIter` equals `Vec`'s > > `IntoIter` type. > > - We also can't use `I::IntoIter`'s type ID either to work around this, > > since `FromIterator` doesn't require this type to be `'static`. > > - `FromIterator::from_iter` does return `Self` instead of > > `Result`, hence we can't properly handle allocation > > failures. > > - Neither `Iterator::collect` nor `FromIterator::from_iter` can handle > > additional allocation flags. > Well that's a shame, so I guess collecting a Filter or Map is not possible. Not with the Rust's `FromIterator` trait, unfortunately. However, once we have other collectable data structures in place, we can come up with a kernel compatible version of the `FromIterator` trait and implement against it. > > > > > Instead, provide `IntoIter::collect`, such that we can at least convert > > `IntoIter` into a `Vec` again. > > > > Signed-off-by: Danilo Krummrich > > --- > > rust/kernel/alloc/kvec.rs | 80 ++++++++++++++++++++++++++++++++++++++- > > 1 file changed, 79 insertions(+), 1 deletion(-) > > > > diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs > > index fbfb755b252d..5c317931e14c 100644 > > --- a/rust/kernel/alloc/kvec.rs > > +++ b/rust/kernel/alloc/kvec.rs > > @@ -2,7 +2,7 @@ > > > > //! Implementation of [`Vec`]. > > > > -use super::{AllocError, Allocator, Flags}; > > +use super::{flags::*, AllocError, Allocator, Flags}; > > use crate::types::Unique; > > use core::{ > > fmt, > > @@ -633,6 +633,84 @@ impl IntoIter > > fn as_raw_mut_slice(&mut self) -> *mut [T] { > > ptr::slice_from_raw_parts_mut(self.ptr, self.len) > > } > > + > > + fn into_raw_parts(self) -> (*mut T, NonNull, usize, usize) { > > + let me = ManuallyDrop::new(self); > > + let ptr = me.ptr; > > + let buf = me.buf; > > + let len = me.len; > > + let cap = me.cap; > > + (ptr, buf, len, cap) > > + } > > + > > + /// Same as `Iterator::collect` but specialized for `Vec`'s `IntoIter`. > > + /// > > + /// Currently, we can't implement `FromIterator`. There are a couple of issues with this trait > > + /// in the kernel, namely: > > + /// > > + /// - Rust's specialization feature is unstable. This prevents us to optimze for the special > > + /// case where `I::IntoIter` equals `Vec`'s `IntoIter` type. > > + /// - We also can't use `I::IntoIter`'s type ID either to work around this, since `FromIterator` > > + /// doesn't require this type to be `'static`. > > + /// - `FromIterator::from_iter` does return `Self` instead of `Result`, hence > > + /// we can't properly handle allocation failures. > > + /// - Neither `Iterator::collect` nor `FromIterator::from_iter` can handle additional allocation > > + /// flags. > > + /// > > + /// Instead, provide `IntoIter::collect`, such that we can at least convert a `IntoIter` into a > > + /// `Vec` again. > > + /// > > + /// Note that `IntoIter::collect` doesn't require `Flags`, since it re-uses the existing backing > > + /// buffer. However, this backing buffer may be shrunk to the actual count of elements. > > + /// > > + /// # Examples > > + /// > > + /// ``` > > + /// let v = kernel::kvec![1, 2, 3]?; > > + /// let mut it = v.into_iter(); > > + /// > > + /// assert_eq!(it.next(), Some(1)); > > + /// > > + /// let v = it.collect(); > > + /// assert_eq!(v, [2, 3]); > > + /// > > + /// # Ok::<(), Error>(()) > > + /// ``` > > + pub fn collect(self) -> Vec { > > + let (mut ptr, buf, len, mut cap) = self.into_raw_parts(); > > + let has_advanced = ptr != buf.as_ptr(); > > + > > + if has_advanced { > > + // SAFETY: Copy the contents we have advanced to at the beginning of the buffer. > > + // `ptr` is guaranteed to be between `buf` and `buf.add(cap)` and `ptr.add(len)` is > > + // guaranteed to be smaller than `buf.add(cap)`. > > + unsafe { ptr::copy(ptr, buf.as_ptr(), len) }; > > + ptr = buf.as_ptr(); > > + } > > + > > + // This can never fail, `len` is guaranteed to be smaller than `cap`. > > + let layout = core::alloc::Layout::array::(len).unwrap(); > > + > > + // SAFETY: `buf` points to the start of the backing buffer and `len` is guaranteed to be > > + // smaller than `cap`. Depending on `alloc` this operation may shrink the buffer or leaves > > + // it as it is. > > + ptr = match unsafe { A::realloc(Some(buf.cast()), layout, GFP_KERNEL) } { > Here you use `GFP_KERNEL` flag directly. Shouldn't this be an argument of `collect` function? Even though we only ever ask the allocator to shrink the buffer (or keep it as it is), it should be, good catch! > > > + // If we fail to shrink, which likely can't even happen, continue with the existing > > + // buffer. > > + Err(_) => ptr, > > + Ok(ptr) => { > > + cap = len; > > + ptr.as_ptr().cast() > > + } > > + }; > > + > > + // SAFETY: If the iterator has been advanced, the advanced elements have been copied to > > + // the beginning of the buffer and `len` has been adjusted accordingly. `ptr` is guaranteed > > + // to point to the start of the backing buffer. `cap` is either the original capacity or, > > + // after shrinking the buffer, equal to `len`. `alloc` is guaranteed to be unchanged since > > + // `into_iter` has been called on the original `Vec`. > > + unsafe { Vec::from_raw_parts(ptr, len, cap) } > > + } > > } > > > > impl Iterator for IntoIter > > -- > > 2.45.2 > >