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 95575CF6493 for ; Sat, 28 Sep 2024 12:43:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9747A6B01F1; Sat, 28 Sep 2024 08:43:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8D6626B01F2; Sat, 28 Sep 2024 08:43:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 750296B01F3; Sat, 28 Sep 2024 08:43:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 537FF6B01F1 for ; Sat, 28 Sep 2024 08:43:41 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 99D59141224 for ; Sat, 28 Sep 2024 12:43:40 +0000 (UTC) X-FDA: 82614113400.22.60C8156 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf10.hostedemail.com (Postfix) with ESMTP id EFF00C000B for ; Sat, 28 Sep 2024 12:43:38 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=kBuolGPM; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf10.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=dakr@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727527381; a=rsa-sha256; cv=none; b=77LDBXKqFJul9eNZ76U9Fi5WADKbp7NB42wbTXSkJjckfCTijpDD4ZH00uA02p55xsTVqY RfXtOK0DUhDh62atZyvwZNc4ZsL0NpDB3bJZyp3li6wSCpC2mOLqv7phG2F6xDimEVgWL6 Ol/zbkKl+sylohqcgyAemjLqp7BwMZM= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=kBuolGPM; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf10.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 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=1727527381; 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=S0N2MAxWAYJxnTKXi9feTKitUV4PC0PnUwQpG7pN9uA=; b=HtXmeoWLD5LY+ASwJBveqgiZSY5mLqdwEOgH3NGYNtS2SLY0ZNxjfeKvjTHFegVPkY/ypG KxxVwGeg/i36pE459h9poGsTaApWP71o3TiP2Av8ZENg3F47shX/Gyt71AjKqKH0rBjPIr x8UiLYLCUCIo/xGHdVggQi7FyQBGTMg= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 949885C0684; Sat, 28 Sep 2024 12:43:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4B278C4CEC3; Sat, 28 Sep 2024 12:43:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727527417; bh=tAmNwQrFqOnGLWoMuKORR3I292iyiJ/rRwbQKe4bVow=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=kBuolGPMW47cT/Yn85jsdRWnYfmrzPAW5OTz2y/1t1pNa6mEczXjbUqYNxrcpXut0 RHPFA5Vrigx34oDjPV6X8hBZ2dKYe78s4o9MXkC/X45XVFgdQ/IG4S8qrQlXrl0cIn wF8IuqQvfv0ZcGesyfwp6yVL3ZRNMKYsc7RdqDdfBKC+1PX/x01dJPbXWC9e2vTYeS /66A09Gjb+zT0sAfTRFyeYK3Faoi9HnYowTQiv/o9mZ2WbRd5/q3JrzY5VAV66cwLi ieDPNYIc/xRuolVDoDeGTdrdH7eugb35mdOhfPH5n4FOLSyUcE1iiF8lSVwUgaTurC CWzRipSylxPnw== Date: Sat, 28 Sep 2024 14:43:29 +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 v7 13/26] rust: alloc: implement kernel `Vec` type Message-ID: References: <20240911225449.152928-1-dakr@kernel.org> <20240911225449.152928-14-dakr@kernel.org> <1a06b767-ca02-41ec-840a-47e73f7876d8@proton.me> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1a06b767-ca02-41ec-840a-47e73f7876d8@proton.me> X-Rspam-User: X-Rspamd-Queue-Id: EFF00C000B X-Rspamd-Server: rspam01 X-Stat-Signature: atbchqdkm1kaad6pguod3xbd4ppde3hs X-HE-Tag: 1727527418-53377 X-HE-Meta: U2FsdGVkX19Oo2im/+J/WWhTBgdWTL87L405JKYtmV3fteP/3buAMEr2ygg019LlaOILRVVWZYjdMjYrz2ZJX3Glpa283JtibVRiGS7ICISS72J1K9qNwEA8iat0wj6/T3ng15pnLPJVg2bKgp1S6qQed+1AyJcH0X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p55neuSxcfPw3H0k7wNeVYL3kU95DU8eqbwKXFLJjZT6LE7jhlXmojg378Bityw2Xk0LylF 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 Thu, Sep 26, 2024 at 01:47:04PM +0000, Benno Lossin wrote: > On 12.09.24 00:52, Danilo Krummrich wrote: > > + /// Appends an element to the back of the [`Vec`] instance. > > + /// > > + /// # Examples > > + /// > > + /// ``` > > + /// let mut v = KVec::new(); > > + /// v.push(1, GFP_KERNEL)?; > > + /// assert_eq!(&v, &[1]); > > + /// > > + /// v.push(2, GFP_KERNEL)?; > > + /// assert_eq!(&v, &[1, 2]); > > + /// # Ok::<(), Error>(()) > > + /// ``` > > + pub fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError> { > > + Vec::reserve(self, 1, flags)?; > > + > > + // SAFETY: > > + // - `self.len` is smaller than `self.capacity` and hence, the resulting pointer is > > + // guaranteed to be part of the same allocated object. > > + // - `self.len` can not overflow `isize`. > > + let ptr = unsafe { self.as_mut_ptr().add(self.len) }; > > + > > + // SAFETY: > > + // - `ptr` is properly aligned and valid for writes. > > + unsafe { core::ptr::write(ptr, v) }; > > Why not use `self.spare_capacity_mut()[0].write(v);`? Before v7 I did exactly that, but in v6 you suggested to use the raw pointer instead to avoid the bounds check. > > If you want to avoid the bounds check, you can do > > let first = self.spare_capacity_mut().first(); > // SAFETY: the call to `Vec::reserve` above ensures that `spare_capacity_mut()` is non-empty. > unsafe { first.unwrap_unchecked() }.write(v); `first` does a similar check to create the `Option<&T>`, right?. I'd rather keep the raw pointer access as suggested in v6.