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 1D368C3DA61 for ; Wed, 24 Jul 2024 14:36:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A49BF6B0093; Wed, 24 Jul 2024 10:35:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9F9D66B0095; Wed, 24 Jul 2024 10:35:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8C09C6B0096; Wed, 24 Jul 2024 10:35:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 6D4EF6B0093 for ; Wed, 24 Jul 2024 10:35:59 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 21E93C0A79 for ; Wed, 24 Jul 2024 14:35:59 +0000 (UTC) X-FDA: 82374895638.01.02CE39E Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) by imf14.hostedemail.com (Postfix) with ESMTP id 38FB0100026 for ; Wed, 24 Jul 2024 14:35:56 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=protonmail.com header.s=protonmail3 header.b=x1n1V5JF; dmarc=pass (policy=quarantine) header.from=protonmail.com; spf=pass (imf14.hostedemail.com: domain of heghedus.razvan@protonmail.com designates 185.70.40.131 as permitted sender) smtp.mailfrom=heghedus.razvan@protonmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721831722; a=rsa-sha256; cv=none; b=iSMMnNLdeoF2P4aI7Ry72PQhzN7XAofNFyVCqhghaxynYSlTuNz61uvLf5IYDAuOnorbeu J7F2cY+7FCKvFrMQkUL5yMH5Kyl7+c2rPgNeADXPsSi1LCepp3HdNlB8Bjml5PQrAnt2B0 QWlazne8pUqtqSKxiN4TYpccHsUENP4= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=protonmail.com header.s=protonmail3 header.b=x1n1V5JF; dmarc=pass (policy=quarantine) header.from=protonmail.com; spf=pass (imf14.hostedemail.com: domain of heghedus.razvan@protonmail.com designates 185.70.40.131 as permitted sender) smtp.mailfrom=heghedus.razvan@protonmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721831722; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=G5JBK4hg6swPkj4jaEo2fVZzCn/NvlSPZRnie21RW3k=; b=jqd61JnihVyM05X3k2fhC1mnS6ZUy0zIkbamXJM7RqDzQ3/srRSq5N6A49Ll/lpFwnqoC2 DA9lar/0jlVIBt8+BRtaX/WWb5dR+zVIN4tKtlHl2bZK2r8Sy2IM77K63fGPXN2bLEjhMx 3BDkcOsnEIjt8QdNhozvIZ5X6jefLa8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1721831753; x=1722090953; bh=G5JBK4hg6swPkj4jaEo2fVZzCn/NvlSPZRnie21RW3k=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=x1n1V5JFnjmkg7piqrTjbZBtHJBx0u3Y9tj1peU5X9/UGkXBURstrTd62rOv2MwPc JiUQwOrxG0OXkhX1jqY2vK0w/loO9wwyScG2NUWsk5orVPe4gQAnuOLTRkeR7G2ZYr zXyvfk9PK9nywKzesIgS0lcYDX+PM13tuVLjeew2Ah6ZyXydLJ+T0GA8vNWHamhaXi Zq37MRWTuD0UdGIYHm7mVk7bap8x0ZoSmwbbDKRNaEYusblxDsyf5m0x3vjvzAte1E zeUrI3WigVFigOyvQz3Z225ToLeVm/IYYVuxR69i5/sPv9vzj2bhETRdPPRLdXj20r sA9khH+gFUGuA== Date: Wed, 24 Jul 2024 14:35:46 +0000 To: Danilo Krummrich , 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 From: Heghedus Razvan 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 Subject: Re: [PATCH v2 16/23] rust: alloc: implement `collect` for `IntoIter` Message-ID: In-Reply-To: <20240723181024.21168-17-dakr@kernel.org> References: <20240723181024.21168-1-dakr@kernel.org> <20240723181024.21168-17-dakr@kernel.org> Feedback-ID: 1233518:user:proton X-Pm-Message-ID: 34d1f58a09b4a2bc6f6f2c4b80d22796d51431a5 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 38FB0100026 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: iquruy9mxjodcc9p6uojdwrp6oa8gebb X-HE-Tag: 1721831756-188690 X-HE-Meta: U2FsdGVkX19qNMV15cdL4uwiNZQ9FCLSuD3LHaoykxvhNEe94qCJNqIf9B7MxSvGFOhDaIdd9kVMxiHBihoYZfuW4BEIgOihKtVh2vZLyRRtH+zNlu6THF+74B4ytBmlQMBOuRN4/AfB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Hr+/VE5KwiCihWhznTEW343RiOye8b4OP00Bmw4sX8lmwH5IWEGJohcy2y8fcGhcCzXvpTJihMs0nzcaJFMwQlRsam+qXSKlS1pfXD9/vCWuEAPBZtpEkJWTWPlJyduF1Tw9fGNthAQHHUgUSA/+N2emiNRiCpE= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, 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 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. > > 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 =3D ManuallyDrop::new(self); > + let ptr =3D me.ptr; > + let buf =3D me.buf; > + let len =3D me.len; > + let cap =3D me.cap; > + (ptr, buf, len, cap) > + } > + > + /// Same as `Iterator::collect` but specialized for `Vec`'s `IntoIte= r`. > + /// > + /// 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 aroun= d this, since `FromIterator` > + /// doesn't require this type to be `'static`. > + /// - `FromIterator::from_iter` does return `Self` instead of `Resul= t`, 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 =3D kernel::kvec![1, 2, 3]?; > + /// let mut it =3D v.into_iter(); > + /// > + /// assert_eq!(it.next(), Some(1)); > + /// > + /// let v =3D it.collect(); > + /// assert_eq!(v, [2, 3]); > + /// > + /// # Ok::<(), Error>(()) > + /// ``` > + pub fn collect(self) -> Vec { > + let (mut ptr, buf, len, mut cap) =3D self.into_raw_parts(); > + let has_advanced =3D ptr !=3D buf.as_ptr(); > + > + if has_advanced { > + // SAFETY: Copy the contents we have advanced to at the begi= nning 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 =3D buf.as_ptr(); > + } > + > + // This can never fail, `len` is guaranteed to be smaller than `= cap`. > + let layout =3D 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 s= hrink the buffer or leaves > + // it as it is. > + ptr =3D 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?=20 > + // If we fail to shrink, which likely can't even happen, con= tinue with the existing > + // buffer. > + Err(_) =3D> ptr, > + Ok(ptr) =3D> { > + cap =3D len; > + ptr.as_ptr().cast() > + } > + }; > + > + // SAFETY: If the iterator has been advanced, the advanced eleme= nts have been copied to > + // the beginning of the buffer and `len` has been adjusted accor= dingly. `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 guaran= teed 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