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 73136C3DA4A for ; Wed, 14 Aug 2024 12:29:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B89DF6B0082; Wed, 14 Aug 2024 08:29:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B1CC16B0083; Wed, 14 Aug 2024 08:29:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9B3956B0085; Wed, 14 Aug 2024 08:29:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 7F4DC6B0082 for ; Wed, 14 Aug 2024 08:29:58 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 1DADF80E7C for ; Wed, 14 Aug 2024 12:29:58 +0000 (UTC) X-FDA: 82450782876.21.2F889A2 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf01.hostedemail.com (Postfix) with ESMTP id B8E694001F for ; Wed, 14 Aug 2024 12:29:55 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=nbXHeAIq; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf01.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=dakr@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723638584; a=rsa-sha256; cv=none; b=KdEhtHtLqUTySZBAVW0cZL9940Yzs+6nnmRBM8kaDAefD4eOwx89GYh7tSX/JMNHNUFPVl pzVMzBGadUMNPPjz6VZdxJp/tKesVbLdkv3I44ybp+TdEJGBj80gio/QoMMIUM1ki2lbCw LMRUsM6rzjqZhazxuGidDVGeS/OMQNo= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=nbXHeAIq; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf01.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=dakr@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723638584; 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=opiAQymSCbk8atl2w2gA70D2TWZIypX4WquTYLErMh0=; b=4RJ9fegX5aBhppRh44SEJW23e6/HQ67P4xBwnSLPaJKIbQ20E4i2MrTkhm7ApCp+oiqULB ffWmKf6lIn6qgNBD82DcgdZFqxF7P19KdMr3Mdbh6sSM0wWMB/6wlF9W/dWshHtqxQAb3S bGA+gQBGUVwM3Fhj1Bl9ZXMl6Z8/zVI= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 11422CE10B9; Wed, 14 Aug 2024 12:29:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2EE6EC32786; Wed, 14 Aug 2024 12:29:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723638590; bh=3LlbGTb9LvSez9xYIWSrKLg3gD7ng+0n0fCCGGjqAzo=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=nbXHeAIq4z6mlPAJxN2IvSnvD+yt2Bu1jABxUKDlDZjhOvcW0rMpCfq/O2kigLrUO rd23IRTDGhYxr+3vwXry5lc99AcUbb6KFbCsvyS2ddmpVrAuwqNKkBwoH1e5ssdwws nXtcLlpIGSo3A3oZYsIlnUFGEASw8bB9jAB/pprY5b0eT18ppR3GxybBvbJiCi0vRQ 17gZRzE60NOqSxcG/VKvtd07PPTlW+ThLewv2Exor5gRdVQjJ4/mcEkZEnKaop0zcn i8N9Qz248eWIAxJOOVqFEJUgj0DMiqpnxp1mkvuAvfw43yGiNjsHR39jjJribvwpzX tdgdlgqmreJnw== Date: Wed, 14 Aug 2024 14:29:43 +0200 From: Danilo Krummrich To: Alice Ryhl 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, 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 v5 13/26] rust: alloc: implement kernel `Vec` type Message-ID: References: <20240812182355.11641-1-dakr@kernel.org> <20240812182355.11641-14-dakr@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Rspam-User: X-Rspamd-Queue-Id: B8E694001F X-Rspamd-Server: rspam01 X-Stat-Signature: em9q6cianjox5nw569ficje7j4sfifr4 X-HE-Tag: 1723638595-166865 X-HE-Meta: U2FsdGVkX1//SP+i2kGC1bprdYRPKVec8oor13jeZdj5QbMayC7B8s5cWVgrKOoqPy8um6OYxMKtrSYBAFRapF1zf8g8MVLW5qjDUpb/hc0JwkB3ERLGXYCrCjOb2r16LRV+Ej+hMa30ukTITtt6aN7sKy17EODm9QFXmlHDNpLRTyTEMs1dRxpTd68s+Ts1cOofJw1x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XaLZ4McPs5eT2Q918CNhuJDwnLyx9Bu3fRMRJBfac7kWwKy+tiL/4kq6iJbwTmR+O4POC5A5hKi79QsYt6Hc6l7CdcGuvsa06TVliQBmx+5VJnuW08bffXdl/S X-Bogosity: Ham, tests=bogofilter, spamicity=0.000190, 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, Aug 14, 2024 at 10:42:28AM +0200, Alice Ryhl wrote: > On Mon, Aug 12, 2024 at 8:25 PM Danilo Krummrich wrote: > > > > `Vec` provides a contiguous growable array type (such as `Vec`) with > > contents allocated with the kernel's allocators (e.g. `Kmalloc`, > > `Vmalloc` or `KVmalloc`). > > > > In contrast to Rust's `Vec` type, the kernel `Vec` type considers the > > kernel's GFP flags for all appropriate functions, always reports > > allocation failures through `Result<_, AllocError>` and remains > > independent from unstable features. > > > > Signed-off-by: Danilo Krummrich > > [...] > > +impl Box<[T; N], A> > > +where > > + A: Allocator, > > +{ > > + /// Convert a `Box<[T, N], A>` to a `Vec`. > > + pub fn into_vec(b: Self) -> Vec { > > Nit: I would probably make this a From impl. Please see my reply to patch 9 of this series. > > > +#[macro_export] > > +macro_rules! kvec { > > + () => ( > > + { > > + $crate::alloc::KVec::new() > > + } > > + ); > > + ($elem:expr; $n:expr) => ( > > + { > > + $crate::alloc::KVec::from_elem($elem, $n, GFP_KERNEL) > > + } > > + ); > > + ($($x:expr),+ $(,)?) => ( > > + { > > + match $crate::alloc::KBox::new([$($x),+], GFP_KERNEL) { > > + Ok(b) => Ok($crate::alloc::KBox::into_vec(b)), > > + Err(e) => Err(e), > > Hmm. This currently generates code that: > > 1. Creates the array. > 2. Allocates the memory. > 3. Moves the array into the box. > > Whereas the stdlib macro swaps step 1 and 2. You can do the same by > utilizing new_uninit. A sketch: > > match KBox::<[_; _]>::new_uninit(GFP_KERNEL) { > Ok(b) => Ok(KVec::from(KBox::write(b, [$($x),+]))), > Err(e) => Err(e), > } Generally, I'm fine changing that, but what's the reason for the suggestion? It shouldn't make a difference, does it? > > > +// SAFETY: `Vec` is `Send` if `T` is `Send` because the data referenced by `self.ptr` is unaliased. > > +unsafe impl Send for Vec > > +where > > + T: Send, > > + A: Allocator, > > +{ > > +} > > + > > +// SAFETY: `Vec` is `Sync` if `T` is `Sync` because the data referenced by `self.ptr` is unaliased. > > +unsafe impl Sync for Vec > > +where > > + T: Send, > > + A: Allocator, > > Same comment as Box. Ditto about "unaliased". Needs `T: Sync` instead. > > > +impl Vec { > > + /// Extend the vector by `n` clones of value. > > + pub fn extend_with(&mut self, n: usize, value: T, flags: Flags) -> Result<(), AllocError> { > > + self.reserve(n, flags)?; > > + > > + let spare = self.spare_capacity_mut(); > > + > > + for item in spare.iter_mut().take(n - 1) { > > You need to handle `n == 0` here. Gonna fix. > > Alice >