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 31C00EE01F1 for ; Wed, 11 Sep 2024 00:22:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B16988E0002; Tue, 10 Sep 2024 20:22:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A9DFD8D00E2; Tue, 10 Sep 2024 20:22:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9179E8E0002; Tue, 10 Sep 2024 20:22:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 6EA4F8D00E2 for ; Tue, 10 Sep 2024 20:22:29 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 20380160BFE for ; Wed, 11 Sep 2024 00:22:29 +0000 (UTC) X-FDA: 82550556018.01.8B0588C Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf26.hostedemail.com (Postfix) with ESMTP id 7297A140010 for ; Wed, 11 Sep 2024 00:22:27 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=hAJYtQne; spf=pass (imf26.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726014095; 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=CMkIRBoKqAnfFzsspp7XWwptyzNVpx6r9s4pDgI6Tzs=; b=03RH+DflQYpGMninw5juKJAfAIiacoaEkr8nmqyV0SQTPEAkTdzlumBcyzII+4ZuO190Ki SWyIvSwMjS+NgEH1ZVH2YRU/QsnODbSNzb4UMI/NVyZe54Ry6zDv7R0YZglUwfHaQ1Tiu4 Z+jR37ZAHDXEtxJTvNK3AhDi1M5War8= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=hAJYtQne; spf=pass (imf26.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726014095; a=rsa-sha256; cv=none; b=H6SFvaSEkEED97C3aeOVyDvPsucmlx0XnsY+SZaxV8s1Sh0mfiM0HdAjSr10fgw6/B5d0q 8i0F78brTyt3rSDKUpP1dKDTb136CkqHuVq47/6KPYdX17E6vwZAIunqqgYhn2kPSS9ujY ymAVK37IstE50/BKJ/78j837hSEthWk= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id C99C65C02E6; Wed, 11 Sep 2024 00:22:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5253FC4CEC3; Wed, 11 Sep 2024 00:22:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1726014146; bh=+LNBlMyxvbRaAdNBPQigbMl3HJSQC/MdjwNU5oKAVHk=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=hAJYtQneMjP2OTggHTC04Y8ifcN8qpmSKrlfbNZBRBUS1YOLFp6Tw0Pg2M5ofNBON rgYZ3fTXmv6FWzOWn4J9+dHgLT1vyLVm558Q707l+6gTid+GW9xud7siZbunA3quaq 4z2wymSbLAj9BrDwnxvuOinbbeY7gKYJigBbt3h+xqNP6CyEqwp8OmRBplnS8oBKLs iwOFz7AgmitwtSxMfd7S7YmGEkrQ2ZrJPxbdSFEVUF5R/6XMWhOEqF7hqdHO7RwLky 2E7FnESyKsFOP9CC6nECWz1y7RiZs8dqbw3x9oKnKp05Fxc0W8rM4kx6vih2sF6O6F +yKnC5/hNir4w== Date: Wed, 11 Sep 2024 02:22:18 +0200 From: Danilo Krummrich To: Benno Lossin Cc: ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, 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, 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 v6 15/26] rust: alloc: implement `collect` for `IntoIter` Message-ID: References: <20240816001216.26575-1-dakr@kernel.org> <20240816001216.26575-16-dakr@kernel.org> <747b8c1c-cb7b-422e-b6a0-ea863cc37f0a@proton.me> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <747b8c1c-cb7b-422e-b6a0-ea863cc37f0a@proton.me> X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 7297A140010 X-Stat-Signature: ks888bpoj6siadd3acbuc751fk9961iq X-HE-Tag: 1726014147-322439 X-HE-Meta: U2FsdGVkX18NvE3B1/VGApjpX7ZOKdIKUUd7EionX6h0i05cbn7NUfj7rXuHoJDynEkrlZinV8OPozqOoYw2i4yiiaVKZ5Tg+SaavGI9OKMINEBTC008uOV8STJuHoY4k5PqrMBY/lhCSF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Hdk7mWTyfTsBJwloZdM1jXDnZOKcjsCIhpZPj0Rb2UTpI/m3jAhwMXH5SBwVxtF+pQX1f9qbbBbwStm9PURPJsCRfVSVPZ5UAVl0TmC2w3xW0Vxr6ljiRorDrexK03L/TIW67q8YqQDg1zMUN3I1K1Dj6HkZvdBMmETHlF9al5YSc89Ohzr+eI0/CNWOWoSm2+UNUCLWG/ye7aRGHx/kAaXQwEOxEPhP0uOQpdozWw16PfaAk39sA== 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 Tue, Sep 10, 2024 at 08:12:24PM +0000, Benno Lossin wrote: > On 16.08.24 02:10, 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. > > > > Instead, provide `IntoIter::collect`, such that we can at least convert > > `IntoIter` into a `Vec` again. > > > > Reviewed-by: Alice Ryhl > > Signed-off-by: Danilo Krummrich > > --- > > rust/kernel/alloc/kvec.rs | 78 +++++++++++++++++++++++++++++++++++++++ > > 1 file changed, 78 insertions(+) > > > > diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs > > index 3b79f977b65e..ad96f4c3af9e 100644 > > --- a/rust/kernel/alloc/kvec.rs > > +++ b/rust/kernel/alloc/kvec.rs > > @@ -681,6 +681,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. > > I think it's great that you include this in the code, but I don't think > that it should be visible in the documentation, Why not? I think this information is valuable for users of this API. > can you move it under > the `Examples` section and turn it into normal comments? > > > + /// > > + /// 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(GFP_KERNEL); > > + /// assert_eq!(v, [2, 3]); > > + /// > > + /// # Ok::<(), Error>(()) > > + /// ``` > > + pub fn collect(self, flags: Flags) -> 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. > > This first sentence should not be part of the SAFETY comment. > > > + // `ptr` is guaranteed to be between `buf` and `buf.add(cap)` and `ptr.add(len)` is > > + // guaranteed to be smaller than `buf.add(cap)`. > > This doesn't justify all the requirements documented in [1]. > > [1]: https://doc.rust-lang.org/core/ptr/fn.copy.html#safety > > > + 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, flags) } { > > + // 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`. > > Turn this into bullet points please. > > --- > Cheers, > Benno > > > + unsafe { Vec::from_raw_parts(ptr, len, cap) } > > + } > > } > > > > impl Iterator for IntoIter > > -- > > 2.46.0 > > >