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 C6141CDE009 for ; Thu, 26 Sep 2024 13:57:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2BC836B0099; Thu, 26 Sep 2024 09:57:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 244A06B009A; Thu, 26 Sep 2024 09:57:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0E6196B009B; Thu, 26 Sep 2024 09:57:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id E2C1D6B0099 for ; Thu, 26 Sep 2024 09:57:43 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id A683D1A17E6 for ; Thu, 26 Sep 2024 13:57:43 +0000 (UTC) X-FDA: 82607042406.01.71332A9 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by imf09.hostedemail.com (Postfix) with ESMTP id BBBFB14000A for ; Thu, 26 Sep 2024 13:57:41 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=proton.me header.s=protonmail header.b=nHYUjAin; dmarc=pass (policy=quarantine) header.from=proton.me; spf=pass (imf09.hostedemail.com: domain of benno.lossin@proton.me designates 185.70.43.16 as permitted sender) smtp.mailfrom=benno.lossin@proton.me ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727358963; a=rsa-sha256; cv=none; b=r92bb64UXTGvG+//IQwe80n7j5MLM7sngpTEzoPmjeVu8k7U+8eTCjsaH9AS/61/bSuwkm G3oxW8zCEbp4wL8RoY2aD/ftOW1yv6rqn0ofV1gi4poWSkX42KDuFVOxeN/9n9AItLOuMO PiSVKAa6Mu6eZKd2oCCYqWgR+/2eWpA= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=proton.me header.s=protonmail header.b=nHYUjAin; dmarc=pass (policy=quarantine) header.from=proton.me; spf=pass (imf09.hostedemail.com: domain of benno.lossin@proton.me designates 185.70.43.16 as permitted sender) smtp.mailfrom=benno.lossin@proton.me ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727358963; 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=czkVTqGn4chEF/J0VNZDWOv9U4Fd+kX8nK5PomYrYB8=; b=Fg1qvAPGRh2sejLdyLAG5u0POIJrr10l7XxUGqx7UHRVikanSa5/pWWRKW3fRMCcN81bT1 Ho4YvCUECStYGv+PVLrfNtPBZRX54oTDbPfTR+E/Cq7GmOTVNw1Bs93w1Pn3K6BJdOMWZZ dI5/iygJ2u+uSfrWoH1XehWXoOLlyIw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1727359060; x=1727618260; bh=czkVTqGn4chEF/J0VNZDWOv9U4Fd+kX8nK5PomYrYB8=; 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=nHYUjAinItLYXodB2Cvy4L9bsRrVklBTIUqyJOTmMNZKlAE8+TOTtHx2JT1MhCiGb Ik84FPvBxobRU/Y0+Y09mspsdUHWlNamkPPFqbKjPDcVimK1f7TuXcceYM/B74W3V5 nvAXZNLvni+Le6JlkMlHdzf8HZpvFw25JX2fUaE4iNtO6xncuaZVyCRUZXzbrO1jTA fhFBmlm5+Zaax9+LMlwVp3lD76Z49tIYTPlMdcdyS7WOQsmiUbGCOlqSOZV0JPcvXw kW9FO+YMnRioROu40U3Pa13nM+NxWNvSFnCS1h+AO4nr7y02AsQpxBuHU90h71T/5n VXU5gFGoafVZQ== Date: Thu, 26 Sep 2024 13:57:36 +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, a.hindborg@samsung.com, aliceryhl@google.com, akpm@linux-foundation.org From: Benno Lossin Cc: 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 v7 15/26] rust: alloc: implement `collect` for `IntoIter` Message-ID: In-Reply-To: <20240911225449.152928-16-dakr@kernel.org> References: <20240911225449.152928-1-dakr@kernel.org> <20240911225449.152928-16-dakr@kernel.org> Feedback-ID: 71780778:user:proton X-Pm-Message-ID: 7d27dc04614ce399c67efe45c3100564b8f1b095 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: BBBFB14000A X-Stat-Signature: xw3cm448shdrs4t4yiro6369er5fiwip X-Rspam-User: X-HE-Tag: 1727359061-873085 X-HE-Meta: U2FsdGVkX19EKxXGRkeDag6i2UksfqmR/vvdfifmJzdBStA8Nh5wVk2UNa7jIXMGBuceFPQf/bn/dbTVqTOmfSfcXAjiLmce8bHcwV7Gy+jkCf1IPJie7RuebcWAOBPJIf+BSJEPzlz8YLy/DgIkWL7j9Px0i5SVne4V8dJ885bkyvXjFNQwVX4m4zb0yKJlLhxXbiqz6jXV3R8Nz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FteRdnFlmRU+2WGbRBfkMakyvlkFGRveZtT+HSmbyMSeCGyGJJOEpQ9qa/dtuQJMokCLhtinnrPz1wVUJGajyah4QDVB/La8UrMel4Fn25JgEF2iM0PyX4nUGw6znu4owWc9Ee4bJkACf9DJ61JqlyNB00Ujpq9UA== 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 12.09.24 00:52, Danilo Krummrich wrote: > Currently, we can't implement `FromIterator`. There are a couple of > issues with this trait in the kernel, namely: >=20 > - 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. >=20 > Instead, provide `IntoIter::collect`, such that we can at least convert > `IntoIter` into a `Vec` again. >=20 > Reviewed-by: Alice Ryhl > Signed-off-by: Danilo Krummrich > --- > rust/kernel/alloc/kvec.rs | 86 +++++++++++++++++++++++++++++++++++++++ > 1 file changed, 86 insertions(+) One comment below, but feel free to keep it as-is. Reviewed-by: Benno Lossin > + pub fn collect(self, flags: Flags) -> 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 { > + // Copy the contents we have advanced to at the beginning of= the buffer. > + // > + // SAFETY: > + // - `ptr` is valid for reads of `len * size_of::()` byte= s, > + // - `buf.as_ptr()` is valid for writes of `len * size_of::<= T>()` bytes, > + // - `ptr` and `buf.as_ptr()` are not be subject to aliasing= restrictions relative to > + // each other, > + // - both `ptr` and `buf.ptr()` are properly aligned. > + 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, flag= s) } { > + // 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() > + } > + }; Would it make sense to only do the resize if the iterator has advanced? If it hasn't, doing `into_iter().collect()` would be a no-op, which would make sense IMO. --- Cheers, Benno > + > + // 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 bu= ffer. > + // - `cap` is either the original capacity or, after shrinking t= he 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) } > + } > } >=20 > impl Iterator for IntoIter > -- > 2.46.0 >=20