From nobody Sat Feb 7 23:24:09 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ACE6D2FF643; Fri, 6 Feb 2026 21:12:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770412326; cv=none; b=oI8yIS5A8Ng5kp565FPHvAkgnRDcmm0j4wfF34uIQIz48lH6WODSJKvwow8qf8oDoQUC5FaFj//HFngz40Ydig8qgO4Va7EhS/w8IKdxcwCX3HncFNisXA86X39zfuOLOYJTFEqonPUbls9EPHMIo4z1DXtuEqdfpqSZ2K+KI1I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770412326; c=relaxed/simple; bh=IuZIeAbMbsdgaivN59tt1avMvbqBct88rIBvqOT1fR4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=luI7c1Z92qigXA5rCapg+BVPla6t2HNB/RDEZBEmdyaWlHW4HP4ZGEDhRxvRLNxb1THRSQAUP0lr1I+uPV3nviRkjq/dlEMs0EIvrnXuK4MYjKYMXpNsO904MP0LwmSXqxCSXRt7aDO0sacBYd8pmzVdRlMHSTiWi/dxQE+7DT4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Q8788c6n; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Q8788c6n" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 12093C116C6; Fri, 6 Feb 2026 21:12:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770412326; bh=IuZIeAbMbsdgaivN59tt1avMvbqBct88rIBvqOT1fR4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Q8788c6nMKGPWcNP4b2+qR18xaW8Tr47gxoIdCzt/HQ9QNbgn02ZkIsy0whEUEEmL XmDup8ohqes/6Akzn/liPi022+7BlzIdHCUWXUCRzeRggwKBi6PWbjPjyf83FCnvgx Dc3si3XQg1oA+tuExMpV2rN1rYidd/xwAhMFpmLYyiCwPg7+VyWmT4Mwuoi1DjdKKl ugkeoaesoLobvTrG781Mlulr03iYw6i2v6tHW1Wq6YqBhYahXKzQr/8b18DH+h+Rm3 jlsKHW1CajbJj/0Rd7TLRMRQTeJRhi9WHTWvyq6b7+RqspA+YeMLKYxWW4lVNpD4Fy 2kBzkmOC6PLNw== From: Andreas Hindborg Date: Fri, 06 Feb 2026 22:10:47 +0100 Subject: [PATCH v2 01/11] rust: xarray: minor formatting fixes Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260206-xarray-entry-send-v2-1-91c41673fd30@kernel.org> References: <20260206-xarray-entry-send-v2-0-91c41673fd30@kernel.org> In-Reply-To: <20260206-xarray-entry-send-v2-0-91c41673fd30@kernel.org> To: Tamir Duberstein , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Andrew Morton , Christoph Lameter , David Rientjes , Roman Gushchin , Harry Yoo Cc: Daniel Gomez , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=2161; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=IuZIeAbMbsdgaivN59tt1avMvbqBct88rIBvqOT1fR4=; b=owEBbQKS/ZANAwAKAeG4Gj55KGN3AcsmYgBphljiegWf9WDKjbFzObfttbuYLpKDIPlIEsoq3 +CZ3kDLd7iJAjMEAAEKAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaYZY4gAKCRDhuBo+eShj d7ggD/9afE2x+SSzohkJkdplmI+/vDaHHfcxt51QBr6DjxAKEAc4JG2ELyc4yuQ+qLvuvo0T2+L 84en77vsujKqRfv2lgNkQn53NetFo6HwnLLPyUPfSXIMRctQ8L4owf0BR3XzWyl1nV0rGGgBqys 4Q5rHf4RYBK7pA5uPSUvgEuzD74vemtTci7M3aybBCI+B+Emjn2Rqd0wqb3qY3x9WD05TuASXDR rRnoqC/oypLNz+34t8kv4zoH/aCvZkLdCdkz93epIP1opOEJeuRodV/vuuS6dis4NoQ+YEW3z1M /F2vx0aSN8+GLpkW7v5HKffRdifom0swnJt4R+0VxsRn/bSJtkx4557rw7McULZno4EtbaGLDOI Y9JFTLiOiGIxdTnxywukAQVLyUdWR3mJkRvow8A/S+kDpqfPhuy8Psf8YpPdz5fxHviizOXO9+z 7eQv2lfdyG6Y+qcO6m7LvJUVYUV/MzMrwdnjtpYIsbKhdxT1FhVqee5gcmR6EP0HTDETgERT7Fm xdYsKfdwk2LdW7xACOjrM6Qg5K+ZTHlFQyaoKW7Uqd9g8zB8+qvZhRFh3lvCr5uZoO7tk3IA8TU DBcXItr+O/028jur8j1bXS4P5VDLnPu2SVWl3UBicUc4HkQpp8wE82KB/ppYA5KSGngX+lRDa3m M8yx1mttJZac7bw== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Fix formatting in xarray module to comply with kernel coding guidelines: - Update use clauses to use vertical layout with each import on its own line. - Add trailing empty comments to preserve formatting and prevent rustfmt from collapsing imports. - Break long assert_eq! statement in documentation across multiple lines for better readability. Reviewed-by: Gary Guo Reviewed-by: Tamir Duberstein Acked-by: Tamir Duberstein Signed-off-by: Andreas Hindborg --- rust/kernel/xarray.rs | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/rust/kernel/xarray.rs b/rust/kernel/xarray.rs index a49d6db288458..88625c9abf4ef 100644 --- a/rust/kernel/xarray.rs +++ b/rust/kernel/xarray.rs @@ -4,14 +4,33 @@ //! //! C header: [`include/linux/xarray.h`](srctree/include/linux/xarray.h) =20 -use crate::{ - alloc, bindings, build_assert, - error::{Error, Result}, +use core::{ + iter, + marker::PhantomData, + pin::Pin, + ptr::NonNull, // +}; +use kernel::{ + alloc, + bindings, + build_assert, // + error::{ + Error, + Result, // + }, ffi::c_void, - types::{ForeignOwnable, NotThreadSafe, Opaque}, + types::{ + ForeignOwnable, + NotThreadSafe, + Opaque, // + }, +}; +use pin_init::{ + pin_data, + pin_init, + pinned_drop, + PinInit, // }; -use core::{iter, marker::PhantomData, pin::Pin, ptr::NonNull}; -use pin_init::{pin_data, pin_init, pinned_drop, PinInit}; =20 /// An array which efficiently maps sparse integer indices to owned object= s. /// @@ -44,7 +63,10 @@ /// *guard.get_mut(0).unwrap() =3D 0xffff; /// assert_eq!(guard.get(0).copied(), Some(0xffff)); /// -/// assert_eq!(guard.store(0, beef, GFP_KERNEL)?.as_deref().copied(), Some= (0xffff)); +/// assert_eq!( +/// guard.store(0, beef, GFP_KERNEL)?.as_deref().copied(), +/// Some(0xffff) +/// ); /// assert_eq!(guard.get(0).copied(), Some(0xbeef)); /// /// guard.remove(0); --=20 2.51.2 From nobody Sat Feb 7 23:24:09 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A5FC3126C0; Fri, 6 Feb 2026 21:12:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770412337; cv=none; b=a2AfT1AgC5IYWi6KRudKKJdggeESFPQlCpnoAfU0Qi98HqVo/hinJHBzax7D7EKfW33Anb2FMus5Gjp+CVrkQ6SR8yWfpuButxmDiMJ8uB7YAeDbNmyp7D87gWSkhRvDestMwnUdf1FgUFz0jmlU0VvbDE8xlGcIZxiFtYlNUzo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770412337; c=relaxed/simple; bh=QeBXOf7iICj0Ad5qHcIJv0VhQMqjDmVdKEV04oiCJGE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Yl9HdYLvje8k762i3SIMXaUwg4cQ/2MDoyQlCETP0z2OGy6wbR7bQYUUmjIu8TbiPqVhDOtxrYiBFyASJ3kxNSxCOmT4FwnpLq/MvAPTbSRMgWAr3hoMkcAL10Ewg1QbYwru9n+1+Qla/lOuTxDEDfzn3cZ2S5R3NO8DVmdPh1I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nyhNNPYW; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="nyhNNPYW" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7F9C7C19424; Fri, 6 Feb 2026 21:12:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770412337; bh=QeBXOf7iICj0Ad5qHcIJv0VhQMqjDmVdKEV04oiCJGE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=nyhNNPYWM1GLHkWakHwZEPnK0d6uMc9aIrhlMgVy6Ek1MlCQInIw9nQ2+5tDDGXCM UTVMUNDqtE/I3UqqIRibXG+c/0vzFycV+Mo5iuc2gtUq56iR+X6GKZwGLSvYFX9FCM Axg2TnM6TObx7sARpC9tLrKUhFKg5bLXy2fjiPc30k5/FQuze9eG5WZbeK/YwZ4P0D nJoltkXEu14Vt+EuDeRamO1iesLgpWanlwPD3LYFHmoyE1njqo81dGYYzv5K33ChAA Guo42yP+3yBmgdH3PVhGebTiUpM3KBSYFEmVkEIi3arR3/bmLNLT9Nv0sgIOq6Fuf7 Qywznk0Meef2w== From: Andreas Hindborg Date: Fri, 06 Feb 2026 22:10:48 +0100 Subject: [PATCH v2 02/11] rust: xarray: add debug format for `StoreError` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260206-xarray-entry-send-v2-2-91c41673fd30@kernel.org> References: <20260206-xarray-entry-send-v2-0-91c41673fd30@kernel.org> In-Reply-To: <20260206-xarray-entry-send-v2-0-91c41673fd30@kernel.org> To: Tamir Duberstein , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Andrew Morton , Christoph Lameter , David Rientjes , Roman Gushchin , Harry Yoo Cc: Daniel Gomez , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=1047; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=QeBXOf7iICj0Ad5qHcIJv0VhQMqjDmVdKEV04oiCJGE=; b=owEBbQKS/ZANAwAKAeG4Gj55KGN3AcsmYgBphljieWZ/1recsKiHHbZ5uEHZHRV0mZOSAv6KA uaHTggj9BaJAjMEAAEKAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaYZY4gAKCRDhuBo+eShj d+W1D/4qt398Xrr2oiXr1+jJpcY7mmP72uBRCYxORQ81s2Mg+hCY+dAEYH+h9DRbrOawMCh8S3h tz+sMTQFlhUYRHxzdWcgEPxQgBjbjwLytQQskg5tZE/GJFFns3m/EQxCgK0d9HVlp5PmjF1qZvU dExZSDPgvbQ8UjCDfxbwSAUUU5qku5iX3wgHImyOVdv1PUOAcQGfIx3Zb0qnzq8WL2IV9SXEXG0 Mbie9op8xNV2Qu2Nsfph3OXvMjmFDU4LHW7ygdY8i//zLFJsvD89PGqfLW9wPb+ayY2nB5Dbmcy 8goorUjz/oROTOGXemqz0sfwTcxXQ7A+TD85yjdo5C9Mqm0FZtfVgLzk5ln8TUN+IdEdsdy1XHW rDuT+RgGOG7fSX3vAMAFShjyZL4pd1AkDE1yZnweQiVb8yff3mGyfoUxiuAVHP8KWePekmzYZV+ TePzTqSSMpS1VKz6WmCihK/aU1DesDX177pCT/8iLrmmeR6o2YYi5NvVQdjj+oY+Oib0FvpwMRm Ur/l4cHjZ/hZVxNFvlHXaI6xlytrl3P7TeRjcLE2XIJ5c81YdFhKGG/m0J/j0KyQAlfAAvbuAXq CObOyfvdTDBR0T5B06gg7md3kl4q78WAhH157iiR2ox9w+KOOMqBBzU2nVJXz6LR5TrkSVl2l2L lPaBWzFXq9nxaRA== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add a `Debug` implementation for `StoreError` to enable better error reporting and debugging. The implementation only displays the `error` field and omits the `value` field, as `T` may not implement `Debug`. Reviewed-by: Gary Guo Acked-by: Tamir Duberstein Signed-off-by: Andreas Hindborg --- rust/kernel/xarray.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/rust/kernel/xarray.rs b/rust/kernel/xarray.rs index 88625c9abf4ef..d9762c6bef19c 100644 --- a/rust/kernel/xarray.rs +++ b/rust/kernel/xarray.rs @@ -193,6 +193,14 @@ pub struct StoreError { pub value: T, } =20 +impl core::fmt::Debug for StoreError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.debug_struct("StoreError") + .field("error", &self.error) + .finish() + } +} + impl From> for Error { fn from(value: StoreError) -> Self { value.error --=20 2.51.2 From nobody Sat Feb 7 23:24:09 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 282B831352F; Fri, 6 Feb 2026 21:11:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770412296; cv=none; b=LdQBme4gdpNRixI1XskuOqonCFTEU+FnNJ5YS+lXxYtL4VF8AR0B0+x7funzCaCFL7niKZOO/WUMk5adTqaTYWDSz4KhMjlnKw3IHyzNsL9gCYXChpTCm+s1tkr8CaPvRiVkaQfKI0X+4PhFICFk5/eR4IC/wdbGDpl1qNDl6/A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770412296; c=relaxed/simple; bh=PrcApbk7BCNSPsQwnef2I6IdaZ2i07Y2ln8fzlwV9sQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=cV/tsSjP6jauulWv5E8ePYT8o64dqje4gz4ooUO91tRUXhclnwbk1mc6+8uRdCBi9CFlKW5EWld8p4QlSNOE/sDOAIofvUZ9wWGA6EAlSGBWiHugmWA9W52MjPjtTYpeua8IyyswQ0VjSlnfuexnl8pS2i2qU/HGbaSTdkCrTaw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=HQqlC174; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="HQqlC174" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3089EC16AAE; Fri, 6 Feb 2026 21:11:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770412295; bh=PrcApbk7BCNSPsQwnef2I6IdaZ2i07Y2ln8fzlwV9sQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=HQqlC174WwKjwtjLP1BUTMoxieROheLgxdft+OGqysEcip0EvNQ1MMicLZBsg6Ygl 0iHMxOT9CHUgO55F77L5XNmNSzdo81rEEkzGE8xd5XuR6dcrtxvo41iJmz0jZbvVi/ /FmqFiqb4mU+1saT1ogpOGPmK9eg+Wcu12rbxKwb/QuDF6Z0bdlVNhtRICOa91zibI AIIEZ1BL3GbaC1kvTZ2uVx1GtrsSirEUpeT+AWr9yXPjeC/cU67gaWt6VMSh06H69b KgYPNteuFvt6eYQY5iRbMOTdIXUD5GbtjQlWL4kd1XifwIFIYNRcae9LeIdBkEE6kW JUi5yQkVOrlvw== From: Andreas Hindborg Date: Fri, 06 Feb 2026 22:10:49 +0100 Subject: [PATCH v2 03/11] rust: xarray: add `contains_index` method Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260206-xarray-entry-send-v2-3-91c41673fd30@kernel.org> References: <20260206-xarray-entry-send-v2-0-91c41673fd30@kernel.org> In-Reply-To: <20260206-xarray-entry-send-v2-0-91c41673fd30@kernel.org> To: Tamir Duberstein , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Andrew Morton , Christoph Lameter , David Rientjes , Roman Gushchin , Harry Yoo Cc: Daniel Gomez , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=1538; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=PrcApbk7BCNSPsQwnef2I6IdaZ2i07Y2ln8fzlwV9sQ=; b=owEBbQKS/ZANAwAKAeG4Gj55KGN3AcsmYgBphljjynnLtfrU8Iiz9Gm5FPAFGgN+NLu89L3zu FUdyPDQUsCJAjMEAAEKAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaYZY4wAKCRDhuBo+eShj d1WtD/0dSV1caS9hKFf19rq5n4cibJ7yErP+DuvjTNfhG+cx9BGtQf/Ivg9WhmB6dKF1/Ff/ZNE fD7LuuYM708jMZ/Ag3QLJ6bZhYFCBVFMXU7uc1O6puBKeYx/LAuPoocpO5LcFPP/bt1ZEw8WUF0 m7zzmtHnXb+6ujkjd2gl6hBhGgSgOGhvdJyYX3ULUUkVXe8vN5TpwXq/IlUw+Os6dWO0Z8QNI9L 49N/R7i9NuiZ19l2L7xKVsv9nEZmAAA6gJmAdAoeINuz0Q+e9PxyqMi9Ew1aBErPXVPOxMOchq7 4ohQLw/lItGvp12xabHAIbxlKUV9FMDxccovc/mki8TucfkGlQtmlMhLwGS+iQtmMw6havc9ndR Pf5qTpoqj69CNxpXyvsSUWI+xc49m7weMQ8a5zeIO/g2XlBsFrLQwnVtrhsHGKtxqg4Qo02NclV CmO6qLnLmsf4hNzCsYGRhK5mdwew7WujEbd5hlufx+YNqZ6a8kGCXRhAvh0c2OPAgDXd0RCHtnG Qy4kA65FWIqUx5woBVUWuCup53T3cNzSUyNmaXvwWMoXZyaDn7aJaQ+HLJyHHR3bZ7tqvodt018 KGZAdtO2i563HF4hHXqFw6msci9S/h4ojJZJCwvUaxYItpXWU1zcyOWq3rFcLZwLhGoZnQSJbel nenKo4yG1+Wh9Yw== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add a convenience method `contains_index` to check whether an element exists at a given index in the XArray. This method provides a more ergonomic API compared to calling `get` and checking for `Some`. Signed-off-by: Andreas Hindborg --- rust/kernel/xarray.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/rust/kernel/xarray.rs b/rust/kernel/xarray.rs index d9762c6bef19c..ede48b5e1dba3 100644 --- a/rust/kernel/xarray.rs +++ b/rust/kernel/xarray.rs @@ -218,6 +218,27 @@ fn load(&self, index: usize, f: F) -> Option Some(f(ptr)) } =20 + /// Checks if the XArray contains an element at the specified index. + /// + /// # Examples + /// + /// ``` + /// # use kernel::{alloc::{flags::GFP_KERNEL, kbox::KBox}, xarray::{Al= locKind, XArray}}; + /// let xa =3D KBox::pin_init(XArray::new(AllocKind::Alloc), GFP_KERNE= L)?; + /// + /// let mut guard =3D xa.lock(); + /// assert_eq!(guard.contains_index(42), false); + /// + /// guard.store(42, KBox::new(0u32, GFP_KERNEL)?, GFP_KERNEL)?; + /// + /// assert_eq!(guard.contains_index(42), true); + /// + /// # Ok::<(), kernel::error::Error>(()) + /// ``` + pub fn contains_index(&self, index: usize) -> bool { + self.get(index).is_some() + } + /// Provides a reference to the element at the given index. pub fn get(&self, index: usize) -> Option> { self.load(index, |ptr| { --=20 2.51.2 From nobody Sat Feb 7 23:24:09 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5462C30E830; Fri, 6 Feb 2026 21:11:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770412311; cv=none; b=k6C31KaBK+YDTbWJzixwdf1nIX2el7vJYkvGa0ghkV2tKr2wbUMBOjCTtldMc1fdGMCM4LaqVzPbPqjoW/BlIOgcBHqtDzMZYED9UI+FyrU923/449OEk5PnzgdIBPYD8pdWWJXA0WqZiP5X0CYnnhlePxdq/lWekyiwg6YXjBo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770412311; c=relaxed/simple; bh=2aA6TsP5TqOXxamqOrEM+ulf/tg/RPrxh0tZsqKQ8Qk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Mih86/LOBONYRoE5RZ3zbcul7fUDBLmqJgXrh58wPzsnsJ7DzxM06Qkt4DmdXdo0XddQtkfrAQ2WS4ZVTwPO1rF1vyRKWBZ84c8ekum3RAIJysLGJTueHYh45rP9SEXB2S2EXBu7f+RJKHEcQ3Jjbp3V5k8PP85wtokzD0BJxeY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rhBEKlTs; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rhBEKlTs" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7702FC116C6; Fri, 6 Feb 2026 21:11:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770412311; bh=2aA6TsP5TqOXxamqOrEM+ulf/tg/RPrxh0tZsqKQ8Qk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=rhBEKlTsP62RAZDNXL4f6dTWS2K126rDj/IyPLRJ9fZW85Dhc0DG7jYthxD5w0iMs azv0CWMNz2OaTIjSVyKOPoZn6wHpdz8xmO5Tfm0uGZD+9Jkddc2azvX+jFgX0fJ1ap DMaQ5S2sebc4qRDD7FAfwPSIzixaAwphXH27Otk7KqchRQ5LGWaB92agMNmjW2EnRS xJXG7gzfAJdjKjVBiGs8Y9o2LgbJJyuEUN9BrA5aQY9U86ITgX7eTMKW4AfIqFLPEj +EK6tFG44dEQhQPqjuADRRkbk3zMWlPylpJsQrOGn0M9dSzxBA8BrRl0KKh/3o8ypb 1GBrfia7yZKPA== From: Andreas Hindborg Date: Fri, 06 Feb 2026 22:10:50 +0100 Subject: [PATCH v2 04/11] rust: xarray: add `XArrayState` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260206-xarray-entry-send-v2-4-91c41673fd30@kernel.org> References: <20260206-xarray-entry-send-v2-0-91c41673fd30@kernel.org> In-Reply-To: <20260206-xarray-entry-send-v2-0-91c41673fd30@kernel.org> To: Tamir Duberstein , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Andrew Morton , Christoph Lameter , David Rientjes , Roman Gushchin , Harry Yoo Cc: Daniel Gomez , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=3142; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=2aA6TsP5TqOXxamqOrEM+ulf/tg/RPrxh0tZsqKQ8Qk=; b=owEBbQKS/ZANAwAKAeG4Gj55KGN3AcsmYgBphljkR6yFU74Qctxwfi+Hjmm+uCS+oAhwJVObd 03DdYekpUGJAjMEAAEKAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaYZY5AAKCRDhuBo+eShj d7NJEACUN4BBgh5y31PGzMdcKchA+Aw6rT5w2nYg1DwlVMsSRzL7BZOOsYX0BrXCfJtsmRg91dM dLzCLv7Ar7zwcI80iJnrAG8sr+wg5wtepKUfDrTMbSyi6vJp4u+XfwqB4QS8iP36nQTIZ/ostYU 7xwkrXbTLtNr++3c+mo0tU1yUB9cFSYFHcfOR+KU4OI+OaV0agbPhkCur+W+yFgaE+yXzsJfuj+ /faayPfcGoTSrap9G3p78GaKxMKDiYTSVko8qtgbqoBvamPr7ZAokSztOfM/wLhu7lJD2CMAN5H f6FfU4KMs+gZXuHR3iyhDOVBXqpW60wdbvE4+etEFbY7FkY4SAUpXtLdgRb4YwW2M3lOOMUgFzz H5jeeUQhUFkib5dWWmahTU7iCe8BMDctvKP/BaK0M9y0g5FdyW2OVS75nG57V6rO/jsMUeM4b2Y ew/cD84r8n7LV1J5W/A5oiiPHXbSKQ+fHIJNSWxZ/zh+TrdED5Moo1ul2gBXJKJ2kguc1+Yx2+0 xVBGydN2BIb8KXmQbJr5gL1gSGnuuk0iJCKmDKYCW5baQhMbYwxTc4yXO4hNY548Xc+BepwqoH0 +Ujvysi8N7EqQPhESEe2AHlSXKCrLA/HEUHWrtdJlujsGqNVdWiTk+ZSXyZPETCnTzfxXLHjaT2 O0AcmXow/xyhDvA== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add `XArrayState` as internal state for XArray iteration and entry operations. This struct wraps the C `xa_state` structure and holds a reference to a `Guard` to ensure exclusive access to the XArray for the lifetime of the state object. The `XAS_RESTART` constant is also exposed through the bindings helper to properly initialize the `xa_node` field. The struct and its constructor are marked with `#[expect(dead_code)]` as there are no users yet. We will remove this annotation in a later patch. Signed-off-by: Andreas Hindborg --- rust/bindings/bindings_helper.h | 1 + rust/kernel/xarray.rs | 41 +++++++++++++++++++++++++++++++++++++= +++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helpe= r.h index a067038b4b422..58605c32e8102 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -117,6 +117,7 @@ const xa_mark_t RUST_CONST_HELPER_XA_PRESENT =3D XA_PRE= SENT; =20 const gfp_t RUST_CONST_HELPER_XA_FLAGS_ALLOC =3D XA_FLAGS_ALLOC; const gfp_t RUST_CONST_HELPER_XA_FLAGS_ALLOC1 =3D XA_FLAGS_ALLOC1; +const size_t RUST_CONST_HELPER_XAS_RESTART =3D (size_t)XAS_RESTART; =20 const vm_flags_t RUST_CONST_HELPER_VM_MERGEABLE =3D VM_MERGEABLE; const vm_flags_t RUST_CONST_HELPER_VM_READ =3D VM_READ; diff --git a/rust/kernel/xarray.rs b/rust/kernel/xarray.rs index ede48b5e1dba3..d1246ec114898 100644 --- a/rust/kernel/xarray.rs +++ b/rust/kernel/xarray.rs @@ -8,7 +8,10 @@ iter, marker::PhantomData, pin::Pin, - ptr::NonNull, // + ptr::{ + null_mut, + NonNull, // + }, }; use kernel::{ alloc, @@ -319,6 +322,42 @@ pub fn store( } } =20 +/// Internal state for XArray iteration and entry operations. +/// +/// # Invariants +/// +/// - `state` is always a valid `bindings::xa_state`. +#[expect(dead_code)] +pub(crate) struct XArrayState<'a, 'b, T: ForeignOwnable> { + /// Holds a reference to the lock guard to ensure the lock is not drop= ped + /// while `Self` is live. + _access: PhantomData<&'b Guard<'a, T>>, + state: bindings::xa_state, +} + +impl<'a, 'b, T: ForeignOwnable> XArrayState<'a, 'b, T> { + #[expect(dead_code)] + fn new(access: &'b Guard<'a, T>, index: usize) -> Self { + let ptr =3D access.xa.xa.get(); + // INVARIANT: We initialize `self.state` to a valid value below. + Self { + _access: PhantomData, + state: bindings::xa_state { + xa: ptr, + xa_index: index, + xa_shift: 0, + xa_sibs: 0, + xa_offset: 0, + xa_pad: 0, + xa_node: bindings::XAS_RESTART as *mut bindings::xa_node, + xa_alloc: null_mut(), + xa_update: None, + xa_lru: null_mut(), + }, + } + } +} + // SAFETY: `XArray` has no shared mutable state so it is `Send` iff `T`= is `Send`. unsafe impl Send for XArray {} =20 --=20 2.51.2 From nobody Sat Feb 7 23:24:09 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B094B30BB8D; Fri, 6 Feb 2026 21:12:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770412347; cv=none; b=GYbIjmlQljq0t+HkavkOgF+xubZaheukmbKKwikMQPF3mtrbIyq+mOZERKV+XY49Wu2mpthR3Cp0Tq0FxxJGCHbPM4KCn5IfXIYcPPbp/esYL5voJx3lLDRvgv1cLOg0RUd5S0n9srYtQ0iWrJUZBqs/UEbf2fOgm3J3Er7SxxI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770412347; c=relaxed/simple; bh=hPUvMpxg3yU6YJ5+chgN6zUpPTceUM2JTPt9rO+kJpw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=NhtAsUFy/b0m98cAn31CLEwiosYpw5AJMaK1liLqNYYG6ix+gaYsqkUqSsh94ZaI+g56SlUCliPq5C33aO6u+7ht4Zh8Pfj0gKyhykpxgUoa5YwGNK23Ttb0Rgct48O+InedexQ6jPUGLBep59RCAxHkCPHjh5xFdWdG+80z3fc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bE2lwnoT; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="bE2lwnoT" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0B7CFC116C6; Fri, 6 Feb 2026 21:12:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770412347; bh=hPUvMpxg3yU6YJ5+chgN6zUpPTceUM2JTPt9rO+kJpw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=bE2lwnoTbUhX6wRjb9J+cLBcsVr5Jv5E5IbPpP5LHrrkAc3M9EkTMS6jB/oQpYagy YW2PB1LY+7am5q+4DhB0AuNgSvbaxVQwZAGKTGVftSGBt0gHlJLYZyuqzch5Tgxs7M YaLqwJekVNxJi2Ugs/FuuDtJfNi1JizIPESSS39whpoJXwRPo8P/xZpUVdHtnn2FZb Nhog1+pxGIwx7ps3QbBat5j0m+nInoyzc/TCm4Nk3dStvUeuJ/4XHHmdeIRbaQnSWm UtEVJsG+AKVuG5q68oMhic3BrmvqiPXgs4WS8so8WGbFaMmQ6VfT4c+LNdM70YDA8x 3JHAi6obCEqNg== From: Andreas Hindborg Date: Fri, 06 Feb 2026 22:10:51 +0100 Subject: [PATCH v2 05/11] rust: xarray: use `xas_load` instead of `xa_load` in `Guard::load` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260206-xarray-entry-send-v2-5-91c41673fd30@kernel.org> References: <20260206-xarray-entry-send-v2-0-91c41673fd30@kernel.org> In-Reply-To: <20260206-xarray-entry-send-v2-0-91c41673fd30@kernel.org> To: Tamir Duberstein , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Andrew Morton , Christoph Lameter , David Rientjes , Roman Gushchin , Harry Yoo Cc: Daniel Gomez , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=2487; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=hPUvMpxg3yU6YJ5+chgN6zUpPTceUM2JTPt9rO+kJpw=; b=owEBbQKS/ZANAwAKAeG4Gj55KGN3AcsmYgBphljlPuwNO4ue3fT31Waz1i4XhwL+yr2R/wlfG z2Vk0o95fWJAjMEAAEKAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaYZY5QAKCRDhuBo+eShj dwFgEADGYbdE3AJj4qJmyXhExgAWxEZVRa0PBezxw0lLQ3YNuSBS4sxStLzhx/kxcGOxx+p2tYn IYi/BOCeUrZXx2NpOKuSSNE+THWW3FXR1Z95QQumYY/aYmIjLUwpGO8MMxS+rtgMrCaE8nZ2pPI 5AYF8cw8Txef2zi5Cc6ugLnlHlYwSBOUm5uYQm8DcrbxLftoHirfdz4Ku8ar2FnmKCwAx2r/O/C wkNn9Y9N6q2zBzdMXNqLyK96tb1mGO3oX/eiY1eM3+prUfrYMaL/+lT4utWZnxN7ygFlpTY3dj3 OIcAJWPllgGwoORsgGZz+vXaz6um0G7PD8VP04tUP7M4y0r7ujgc9ndcWfEhnV/u4DRKliix4Qf dHbK8iN7aszFO4to8BhY69mvUCf+ewQ6qVdG8TcLI3HeuvV4YPqxZyQrStpMDw1MXz7I2DOMBO7 jDE3s+DtInSr/DbxUAakU+uOjEBw/tJZYFyKN5EEK31lbHTSMsqzBe7SbPP7Ss1S5mskwzvz1uj RzuzsmJALis/JfpgSJDha6AY97Oxmf1vwcZbEGrUAeOQIndg//9njSu/1m7cNZrkhncaW9OTRlP oUWYzmGq+Hz1ZyP7XM6nGGoO0jVKz2PLMRrIgd3PuxtvF6uANoiClQHHaPXFhrEfnhhxCRN3M3p f/4hPVU3lzkg8cA== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Replace the call to `xa_load` with `xas_load` in `Guard::load`. The `xa_load` function takes the RCU lock internally, which we do not need, since the `Guard` already holds an exclusive lock on the `XArray`. The `xas_load` function operates on `xa_state` and assumes the required locks are already held. This change also removes the `#[expect(dead_code)]` annotation from `XArrayState` and its constructor, as they are now in use. Signed-off-by: Andreas Hindborg --- rust/kernel/xarray.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/rust/kernel/xarray.rs b/rust/kernel/xarray.rs index d1246ec114898..eadddafb180ec 100644 --- a/rust/kernel/xarray.rs +++ b/rust/kernel/xarray.rs @@ -215,10 +215,8 @@ fn load(&self, index: usize, f: F) -> Option where F: FnOnce(NonNull) -> U, { - // SAFETY: `self.xa.xa` is always valid by the type invariant. - let ptr =3D unsafe { bindings::xa_load(self.xa.xa.get(), index) }; - let ptr =3D NonNull::new(ptr.cast())?; - Some(f(ptr)) + let mut state =3D XArrayState::new(self, index); + Some(f(state.load()?)) } =20 /// Checks if the XArray contains an element at the specified index. @@ -327,7 +325,6 @@ pub fn store( /// # Invariants /// /// - `state` is always a valid `bindings::xa_state`. -#[expect(dead_code)] pub(crate) struct XArrayState<'a, 'b, T: ForeignOwnable> { /// Holds a reference to the lock guard to ensure the lock is not drop= ped /// while `Self` is live. @@ -336,7 +333,6 @@ pub(crate) struct XArrayState<'a, 'b, T: ForeignOwnable= > { } =20 impl<'a, 'b, T: ForeignOwnable> XArrayState<'a, 'b, T> { - #[expect(dead_code)] fn new(access: &'b Guard<'a, T>, index: usize) -> Self { let ptr =3D access.xa.xa.get(); // INVARIANT: We initialize `self.state` to a valid value below. @@ -356,6 +352,13 @@ fn new(access: &'b Guard<'a, T>, index: usize) -> Self= { }, } } + + fn load(&mut self) -> Option> { + // SAFETY: `state.state` is always valid by the type invariant of + // `XArrayState and we hold the xarray lock`. + let ptr =3D unsafe { bindings::xas_load(&raw mut self.state) }; + NonNull::new(ptr.cast()) + } } =20 // SAFETY: `XArray` has no shared mutable state so it is `Send` iff `T`= is `Send`. --=20 2.51.2 From nobody Sat Feb 7 23:24:09 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3B6B530DED7; Fri, 6 Feb 2026 21:11:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770412306; cv=none; b=dm/P1H64QYNVlz+eVd+xpuno2g9IFGnLZJn6Y+2X52+HOPCbILfedCca8nO08Phmau44mXn0DqgY9lmO0JlOv7rjQqzzeelwfYVLWayIWQ0swXltUVc5Pi5eA/jIzaS9hqpIiXhJPtvAVJnE2iNGgM+eOiXK3Dy8kH5Aum/m9A0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770412306; c=relaxed/simple; bh=SUnEj4f1l9mHuLlzJWz0dNzh+/snH5Ql51wel6NI7LE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=aZJBHizaZXOYEqS0gWijjatcyB+TGirppr2SIjemMQ8lsiI5vO8qS6TpieMyNwXjhASUkC3FzXA3TDy4I4Q5ysab1rWQrQCKUie5OK3xuB+7nkUT5FbqkfJcXUeoZFlv6sAcdPN0spJteKMqy1Tr/KLC8EcY2mgGprCNovpKMOA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=elL/ARi/; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="elL/ARi/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5747BC19423; Fri, 6 Feb 2026 21:11:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770412305; bh=SUnEj4f1l9mHuLlzJWz0dNzh+/snH5Ql51wel6NI7LE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=elL/ARi/Mfy6/8OMFNs8uiRAErlt7a0EsxsJ96Y8ZVMEUAaysUtfZY2sG6o5ng67K 3V1pS2IbXdWBX+Eg9MTVdG0W5b6W4i69faAmQKVTOgvL9Q35sjaD7kLroqd8t3ZGgK 7QJq4sNIooVcA27haeMPsZPq+ORT5o+xPNqHjLLNDhT9vw5Y9bIVLoTjLovOpDK1fd tp8RupLbxnAVDk/51MnYgE8W5OhZDiOr/VTUHPKQCVbj499SnSe4EH9MsPQwguBqaZ CYRmNLiw8z9HcwnNdxOYYTlRUEYx20j9yssvpQEbaDh/Wo++umBNHfF0x9oCHZ9tZg Ru1hGf12XWPtw== From: Andreas Hindborg Date: Fri, 06 Feb 2026 22:10:52 +0100 Subject: [PATCH v2 06/11] rust: xarray: simplify `Guard::load` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260206-xarray-entry-send-v2-6-91c41673fd30@kernel.org> References: <20260206-xarray-entry-send-v2-0-91c41673fd30@kernel.org> In-Reply-To: <20260206-xarray-entry-send-v2-0-91c41673fd30@kernel.org> To: Tamir Duberstein , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Andrew Morton , Christoph Lameter , David Rientjes , Roman Gushchin , Harry Yoo Cc: Daniel Gomez , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=2076; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=SUnEj4f1l9mHuLlzJWz0dNzh+/snH5Ql51wel6NI7LE=; b=owEBbQKS/ZANAwAKAeG4Gj55KGN3AcsmYgBphljmYZEvBGqQzuxdMlWfzFpY+l8tlo4gbcojX j4SxFfq6vqJAjMEAAEKAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaYZY5gAKCRDhuBo+eShj d/jhEACk9AnNcXcMjo9QkvvewgUgKs55e+8UtqRMLkwtnf1JovlHrP0FgBD3dAp7YpEIT2jfiiZ yIINVdEkcxkz6fGc3CigphLMrBcvmc/HMVCD+Tdv/bs48oEJlEzuSWjy192ko7PkXukIr8/YqVU L4aNhXo4z0xRfMMzQpxQVWgS4AeUcGMRF8txn16M1hZPOl0+iZuWfuaRqk9lQcYjFReFcMqbana w6PaI3C1MfNSbtLKRpBelVfKz8oJeysR/JZy9BlNnOBpA8W/iJzHua8iYh81VsB3/6Rt0XTEfJF 2r8kUBsUUOeDwfWn4h5vSxFDqvaKDre+Q8zk3vCCTiaT7snf9JpLnuJ+laCwsPkdPQdC0uCgMis 2wd9F3+rOrcSGbFD2xR/j9HmvZ5PcUKLz9W2cEUwZ/ljm5liiUs5Ef3tjhZ0R+hO10+OJ/3mfKa QTorR+MfYJpVUkk2713dvuD/BaKrSzSS1PebsCShqovjUsGt7zwYWBJdXtFbaqsg8V+Pv+Qof26 jsTwB2Kiy7CbN5xWJaiF+XwU1TM9QmbALEuY+N/3VDU7jR3SmQBgZhFUpHty3554DslLC4TPyGm JJFYMn07uAzgxa0Aujy3IvMOZhTwtycNUtGQNu1OUZsAWoUAuYxwX8DcZ6w1uA62vd7ues2FMOD upjfZA9Vog25OyA== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Simplify the implementation by removing the closure-based API from `Guard::load` in favor of returning `Option>` directly. Signed-off-by: Andreas Hindborg --- rust/kernel/xarray.rs | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/rust/kernel/xarray.rs b/rust/kernel/xarray.rs index eadddafb180ec..e654bf56dc97c 100644 --- a/rust/kernel/xarray.rs +++ b/rust/kernel/xarray.rs @@ -211,12 +211,8 @@ fn from(value: StoreError) -> Self { } =20 impl<'a, T: ForeignOwnable> Guard<'a, T> { - fn load(&self, index: usize, f: F) -> Option - where - F: FnOnce(NonNull) -> U, - { - let mut state =3D XArrayState::new(self, index); - Some(f(state.load()?)) + fn load(&self, index: usize) -> Option> { + XArrayState::new(self, index).load() } =20 /// Checks if the XArray contains an element at the specified index. @@ -242,18 +238,17 @@ pub fn contains_index(&self, index: usize) -> bool { =20 /// Provides a reference to the element at the given index. pub fn get(&self, index: usize) -> Option> { - self.load(index, |ptr| { - // SAFETY: `ptr` came from `T::into_foreign`. - unsafe { T::borrow(ptr.as_ptr()) } - }) + let ptr =3D self.load(index)?; + // SAFETY: `ptr` came from `T::into_foreign`. + Some(unsafe { T::borrow(ptr.as_ptr()) }) } =20 /// Provides a mutable reference to the element at the given index. pub fn get_mut(&mut self, index: usize) -> Option> { - self.load(index, |ptr| { - // SAFETY: `ptr` came from `T::into_foreign`. - unsafe { T::borrow_mut(ptr.as_ptr()) } - }) + let ptr =3D self.load(index)?; + + // SAFETY: `ptr` came from `T::into_foreign`. + Some(unsafe { T::borrow_mut(ptr.as_ptr()) }) } =20 /// Removes and returns the element at the given index. --=20 2.51.2 From nobody Sat Feb 7 23:24:09 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 087BF30DED7; Fri, 6 Feb 2026 21:11:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770412303; cv=none; b=meAH4yFuQSjv/4PM4DFKamzdUeTUm5tZadCeF+PnqDTidWkOPlQmkCYTdm8xu348Yea9yrG0smY9JWbYLzDijdisgPGSCq46Gf44/n5ZxNr0m1aAL4EuhU3Kok2sUjiGJOVm03wgXo+dBfmvvMesQnyFr8D56LJ2xpu0+Xa49JA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770412303; c=relaxed/simple; bh=ziAsziTOC+GzNf139Ut4jKaT94DhutbUW+Q40shocZ0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=bftm63M7i/FRXScfEdBc5fgwOhCHpdhkOk7s6jxNSSQvkQCQVs/30juSaC2Mw84HRTzHU3lzUEkMSNoLhPgXb+Az5/FCuOsSJ/SLLtuOqvu40orpA3DlExKcH7m3x/rm3l9iDiYqvaNv2Yvxraf7+UecB/2lc15ysaEQif9Bzy8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LgnSA+7b; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="LgnSA+7b" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 61DB3C16AAE; Fri, 6 Feb 2026 21:11:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770412300; bh=ziAsziTOC+GzNf139Ut4jKaT94DhutbUW+Q40shocZ0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=LgnSA+7bY0qxDvdMzITJ+FXZElGwsDv68y49w/cMLIlIBvXk+1Tzi5v7YD1gwAjZj I+winp0JvJ2kd6OyT58TLMLbPUrvwC4wYIzvaXFb3IpiwzThADBBzwIKAWGYpRHmdI 3vQanmMlFSsfzLrGSxQnjvsI2n8ic8y1y6CGE03/gGNs5tN8Z1sn4E50azERG+OM5F eVr+3JX4ZL0qo7ngGbxA1WPy1rVJYOVqrW6/donr6nWD4Zw6+GBPdg9iEnyPYUo38v 1lX+h/R5cW5ZzZRp1N5+md3aCSrIAH4/pKBthbX9JDw8VzudKOQxA/nwxAV8+Djqd6 PQZKmchAO5C+w== From: Andreas Hindborg Date: Fri, 06 Feb 2026 22:10:53 +0100 Subject: [PATCH v2 07/11] rust: xarray: add `find_next` and `find_next_mut` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260206-xarray-entry-send-v2-7-91c41673fd30@kernel.org> References: <20260206-xarray-entry-send-v2-0-91c41673fd30@kernel.org> In-Reply-To: <20260206-xarray-entry-send-v2-0-91c41673fd30@kernel.org> To: Tamir Duberstein , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Andrew Morton , Christoph Lameter , David Rientjes , Roman Gushchin , Harry Yoo Cc: Daniel Gomez , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=4023; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=ziAsziTOC+GzNf139Ut4jKaT94DhutbUW+Q40shocZ0=; b=owEBbQKS/ZANAwAKAeG4Gj55KGN3AcsmYgBphljnlh0PwhOjbdwrAoJpOnH1unnfNaF62jnVf OpMfNpflSuJAjMEAAEKAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaYZY5wAKCRDhuBo+eShj d8lxD/96tdOwjEygElTB5W6BwdRyLgvYB/hCTUV/zpbiLgUPQl0pzk+j00jlWUivFwhQBOnlwrE YEgUGsY/y5uzXaAiw5NXnitAZoS6ZGWv2tpbAHvUCwwc/flX1p5Uu6leUtXPY6T8t+ok4lz9DR1 fGSwg7Gb4f1ffmZkq62vlMR9FqECeE+RmhnAT3e9r2m86CSHMgXP5/qHqmzJlmSLxx+3YXqBR6U Yrh1rib85R+ov46tLBhvLSAKzrL6BSdNV8tklv3VD6n6gsCV7q7/JX3FgKFSw24OOX2x6sbc8oo zGsiXe2tpDo6CRpW9wavJsKgtq/VDBtvN7UohRxYDjmJXzRuYW88v4vMnUfPhOz5zgpYbbOEuZC M+vfJ29OIKm2qyjV2440+qdZP3PQFKHf4qOfOowsUnl/l6cBO2mj3GvbRXQ9I/1YcSj/Lt8mBIf hx9fybxfvq/KPIruXbehNTsdyXANogQmXTM5FNNbNLzYWLRarWRxU5cLHl38aMYDJLZwSfYQYlT fP5dvm4HKTSK0uw6wgVIg0dKjwug2PyccQwuqOtW71GIX20wNp89kFn3Dvh16L15xMTuxvxwjMd AltU2qTF9HFwtjnkMPV7R0bERe3P/+XCRDU64hmmiITp7fSgoqnSVu+poZABp+Ue1z5gi/tT4sH gUswhjAe8geVmNg== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add methods to find the next element in an XArray starting from a given index. The methods return a tuple containing the index where the element was found and a reference to the element. The implementation uses the XArray state API via `xas_find` to avoid taking the rcu lock as an exclusive lock is already held by `Guard`. Signed-off-by: Andreas Hindborg --- rust/kernel/xarray.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 68 insertions(+) diff --git a/rust/kernel/xarray.rs b/rust/kernel/xarray.rs index e654bf56dc97c..656ec897a0c41 100644 --- a/rust/kernel/xarray.rs +++ b/rust/kernel/xarray.rs @@ -251,6 +251,67 @@ pub fn get_mut(&mut self, index: usize) -> Option> { Some(unsafe { T::borrow_mut(ptr.as_ptr()) }) } =20 + fn load_next(&self, index: usize) -> Option<(usize, NonNull)> { + XArrayState::new(self, index).load_next() + } + + /// Finds the next element starting from the given index. + /// + /// # Examples + /// + /// ``` + /// # use kernel::{prelude::*, xarray::{AllocKind, XArray}}; + /// let mut xa =3D KBox::pin_init(XArray::>::new(AllocKind::= Alloc), GFP_KERNEL)?; + /// let mut guard =3D xa.lock(); + /// + /// guard.store(10, KBox::new(10u32, GFP_KERNEL)?, GFP_KERNEL)?; + /// guard.store(20, KBox::new(20u32, GFP_KERNEL)?, GFP_KERNEL)?; + /// + /// if let Some((found_index, value)) =3D guard.find_next(11) { + /// assert_eq!(found_index, 20); + /// assert_eq!(*value, 20); + /// } + /// + /// if let Some((found_index, value)) =3D guard.find_next(5) { + /// assert_eq!(found_index, 10); + /// assert_eq!(*value, 10); + /// } + /// + /// # Ok::<(), kernel::error::Error>(()) + /// ``` + pub fn find_next(&self, index: usize) -> Option<(usize, T::Borrowed<'_= >)> { + self.load_next(index) + // SAFETY: `ptr` came from `T::into_foreign`. + .map(|(index, ptr)| (index, unsafe { T::borrow(ptr.as_ptr()) }= )) + } + + /// Finds the next element starting from the given index, returning a = mutable reference. + /// + /// # Examples + /// + /// ``` + /// # use kernel::{prelude::*, xarray::{AllocKind, XArray}}; + /// let mut xa =3D KBox::pin_init(XArray::>::new(AllocKind::= Alloc), GFP_KERNEL)?; + /// let mut guard =3D xa.lock(); + /// + /// guard.store(10, KBox::new(10u32, GFP_KERNEL)?, GFP_KERNEL)?; + /// guard.store(20, KBox::new(20u32, GFP_KERNEL)?, GFP_KERNEL)?; + /// + /// if let Some((found_index, mut_value)) =3D guard.find_next_mut(5) { + /// assert_eq!(found_index, 10); + /// *mut_value =3D 0x99; + /// } + /// + /// assert_eq!(guard.get(10).copied(), Some(0x99)); + /// + /// # Ok::<(), kernel::error::Error>(()) + /// ``` + pub fn find_next_mut(&mut self, index: usize) -> Option<(usize, T::Bor= rowedMut<'_>)> { + self.load_next(index) + // SAFETY: `ptr` came from `T::into_foreign`. + .map(move |(index, ptr)| (index, unsafe { T::borrow_mut(ptr.as= _ptr()) })) + } + /// Removes and returns the element at the given index. pub fn remove(&mut self, index: usize) -> Option { // SAFETY: @@ -354,6 +415,13 @@ fn load(&mut self) -> Option> { let ptr =3D unsafe { bindings::xas_load(&raw mut self.state) }; NonNull::new(ptr.cast()) } + + fn load_next(&mut self) -> Option<(usize, NonNull)> { + // SAFETY: `self.state` is always valid by the type invariant of + // `XArrayState` and the we hold the xarray lock. + let ptr =3D unsafe { bindings::xas_find(&raw mut self.state, usize= ::MAX) }; + NonNull::new(ptr).map(|ptr| (self.state.xa_index, ptr)) + } } =20 // SAFETY: `XArray` has no shared mutable state so it is `Send` iff `T`= is `Send`. --=20 2.51.2 From nobody Sat Feb 7 23:24:09 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F0E8E301001; Fri, 6 Feb 2026 21:11:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770412291; cv=none; b=qas/WC31Naf7xpuISY4RSGJ66KW5zclNBpMprR9u7+/0bHMfLwSkI4alRu5Kek/5C72Ljg5MF2V2IQ4EmJO+J7FYnm6cyc2c6lLgJFF9shog7t5OXpNH5IhA7+obXoGVI3ngBTH1UtAfbd8s1SWC23C8DOJM1yZmCttkVWWOmq4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770412291; c=relaxed/simple; bh=tGpkTbkQCs2yz/lXZr74M2PLP6TLYGnHySlF4MFb55o=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=HME9eslVvlONWXpnC4yrWxZmvNZZWWCFKn/+LyGGf+G9sHSiwxKTLNuLjn4I9uqn2jAoi8wKiItzdr3DlktRU+YSxlJSN85wxBYQGTVjqv3EFmsPnalZPLTwDOPs9l121Am1ClGXXI+fq4hmChRNsSS6E3KKbOfKWpl7MKw0uVE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=QuPDUGdp; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="QuPDUGdp" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 087C7C116C6; Fri, 6 Feb 2026 21:11:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770412290; bh=tGpkTbkQCs2yz/lXZr74M2PLP6TLYGnHySlF4MFb55o=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=QuPDUGdpXG+jRKfCI93WnWBECSxeffdZgPnpEszWgv2T1bPinnDaMUa+lOjEJngmY zjuGhhZTfHXOgl0WUz0nFh7EM4nnmJRk2o5eSyI1ESCu8Dhka8o8mm+mjeYCHbrTwT JUgZ5dWWKI5ShNDHb/LMGXULTzY0OpC+4Bb5uc+tmBE37hqGFuz87LD+XFRrrglgEr 4bAdKCf54TRYUSm5PHEaDoC4PX9V2sZn1w9tWREeV2+4BxsyeY7c/ENq2nRi/Sz2qJ g+lpBVj6c+1R4YRUXbk6GYIG8tKARhbARaaKnpS+dDXGbWZrG8O207eoMumiTfF/lB fS7WMxTZI2ydQ== From: Andreas Hindborg Date: Fri, 06 Feb 2026 22:10:54 +0100 Subject: [PATCH v2 08/11] rust: xarray: add entry API Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260206-xarray-entry-send-v2-8-91c41673fd30@kernel.org> References: <20260206-xarray-entry-send-v2-0-91c41673fd30@kernel.org> In-Reply-To: <20260206-xarray-entry-send-v2-0-91c41673fd30@kernel.org> To: Tamir Duberstein , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Andrew Morton , Christoph Lameter , David Rientjes , Roman Gushchin , Harry Yoo Cc: Daniel Gomez , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=21192; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=tGpkTbkQCs2yz/lXZr74M2PLP6TLYGnHySlF4MFb55o=; b=owEBbQKS/ZANAwAKAeG4Gj55KGN3AcsmYgBphljo+HSkSPWQgXAH0o0Hr5UYAUqzIz990zCWT ShAfYdQhOuJAjMEAAEKAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaYZY6AAKCRDhuBo+eShj d+OPD/91TqFSj+c+q8fj/Fi1NQOKnSpT4wmiEHIgrUtQGTodDgDNAzsDBOJeG4/2kJNOE8FD28Z xOnjYAq4xnCgao/LaKfeDYXtkcaOzEs+pRoTXYV3PvU/XWdo8X2Hjw7SrRrNN00Pn1e1wb74gJ0 zg29yImVFRHsiCkgXZWtSjCD1pgYlMJ7ZKDo8MT2Mq+bnuuUVjOueFe0jwaN+VMTNHOllcfKOe5 Ew14uZZ5MY4P/n6lHxsEK6U/AZIuybzfVSmtVH+xiQ149btU6SFY62V0PqMZEOw8nQRf911zmEl g0GXxajdlmsiQCvdfzSogvSI1T3txaXiPLW4KFQcQDfZ4k39k+EvVJXwrr1Jne19Lra4AYZwaV8 CsiHslLkfXrp8RvuSBOjupDgDPKZjpN95lpgynnGXmBsHmRzDtYj9gWqHnJP2Rfe/js0koJoRVE uhFDJcJlZ80QzZ55Txa+obw3OMzURDuIRiFkBvm5bB7sCAXAKeayVkXtJvcDEZyH6f7mu/8FmM9 fh+xMXFMYuXNwoWWv8VjQZJ7vsKjHVtkpB7F0Wm3mX23u1ry6eBpq51Ibd7Mz6meo7lrtKndGkA kJsi8ltlPa4aPghRInIJbXBrmLQf0ibEsspDUIkLUlsnwr2XQijCaXp4ZqeIvZWcYDl5T9PvUWI TutdKnvBSe3FaHg== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add an Entry API for XArray that provides ergonomic access to array slots that may be vacant or occupied. The API follows the pattern of Rust's standard library HashMap entry API, allowing efficient conditional insertion and modification of entries. The implementation uses the XArray state API (`xas_*` functions) for efficient operations without requiring multiple lookups. Helper functions are added to rust/helpers/xarray.c to wrap C macros that are not directly accessible from Rust. Also update MAINTAINERS to cover the new rust files. Signed-off-by: Andreas Hindborg --- MAINTAINERS | 1 + rust/helpers/xarray.c | 17 ++ rust/kernel/xarray.rs | 123 +++++++++++++++ rust/kernel/xarray/entry.rs | 367 ++++++++++++++++++++++++++++++++++++++++= ++++ 4 files changed, 508 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 0efa8cc6775b7..8202515c6065b 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -28361,6 +28361,7 @@ B: https://github.com/Rust-for-Linux/linux/issues C: https://rust-for-linux.zulipchat.com T: git https://github.com/Rust-for-Linux/linux.git xarray-next F: rust/kernel/xarray.rs +F: rust/kernel/xarray/ =20 XBOX DVD IR REMOTE M: Benjamin Valentin diff --git a/rust/helpers/xarray.c b/rust/helpers/xarray.c index 60b299f11451d..425a6cc494734 100644 --- a/rust/helpers/xarray.c +++ b/rust/helpers/xarray.c @@ -26,3 +26,20 @@ void rust_helper_xa_unlock(struct xarray *xa) { return xa_unlock(xa); } + +void *rust_helper_xas_result(struct xa_state *xas, void *curr) +{ + if (xa_err(xas->xa_node)) + curr =3D xas->xa_node; + return curr; +} + +void *rust_helper_xa_zero_to_null(void *entry) +{ + return xa_is_zero(entry) ? NULL : entry; +} + +int rust_helper_xas_error(const struct xa_state *xas) +{ + return xas_error(xas); +} diff --git a/rust/kernel/xarray.rs b/rust/kernel/xarray.rs index 656ec897a0c41..8c10e8fd76f15 100644 --- a/rust/kernel/xarray.rs +++ b/rust/kernel/xarray.rs @@ -13,11 +13,17 @@ NonNull, // }, }; +pub use entry::{ + Entry, + OccupiedEntry, + VacantEntry, // +}; use kernel::{ alloc, bindings, build_assert, // error::{ + to_result, Error, Result, // }, @@ -251,6 +257,35 @@ pub fn get_mut(&mut self, index: usize) -> Option> { Some(unsafe { T::borrow_mut(ptr.as_ptr()) }) } =20 + /// Gets an entry for the specified index, which can be vacant or occu= pied. + /// + /// # Examples + /// + /// ``` + /// # use kernel::{prelude::*, xarray::{AllocKind, XArray, Entry}}; + /// let mut xa =3D KBox::pin_init(XArray::>::new(AllocKind::= Alloc), GFP_KERNEL)?; + /// let mut guard =3D xa.lock(); + /// + /// assert_eq!(guard.contains_index(42), false); + /// + /// match guard.entry(42) { + /// Entry::Vacant(entry) =3D> { + /// entry.insert(KBox::new(0x1337u32, GFP_KERNEL)?)?; + /// } + /// Entry::Occupied(_) =3D> unreachable!("We did not insert an ent= ry yet"), + /// } + /// + /// assert_eq!(guard.get(42), Some(&0x1337)); + /// + /// # Ok::<(), kernel::error::Error>(()) + /// ``` + pub fn entry<'b>(&'b mut self, index: usize) -> Entry<'a, 'b, T> { + match self.load(index) { + None =3D> Entry::Vacant(VacantEntry::new(self, index)), + Some(ptr) =3D> Entry::Occupied(OccupiedEntry::new(self, index,= ptr)), + } + } + fn load_next(&self, index: usize) -> Option<(usize, NonNull)> { XArrayState::new(self, index).load_next() } @@ -312,6 +347,72 @@ pub fn find_next_mut(&mut self, index: usize) -> Optio= n<(usize, T::BorrowedMut<' .map(move |(index, ptr)| (index, unsafe { T::borrow_mut(ptr.as= _ptr()) })) } =20 + /// Finds the next occupied entry starting from the given index. + /// + /// # Examples + /// + /// ``` + /// # use kernel::{prelude::*, xarray::{AllocKind, XArray}}; + /// let mut xa =3D KBox::pin_init(XArray::>::new(AllocKind::= Alloc), GFP_KERNEL)?; + /// let mut guard =3D xa.lock(); + /// + /// guard.store(10, KBox::new(10u32, GFP_KERNEL)?, GFP_KERNEL)?; + /// guard.store(20, KBox::new(20u32, GFP_KERNEL)?, GFP_KERNEL)?; + /// + /// if let Some(entry) =3D guard.find_next_entry(5) { + /// assert_eq!(entry.index(), 10); + /// let value =3D entry.remove(); + /// assert_eq!(*value, 10); + /// } + /// + /// assert_eq!(guard.get(10), None); + /// + /// # Ok::<(), kernel::error::Error>(()) + /// ``` + pub fn find_next_entry<'b>(&'b mut self, index: usize) -> Option> { + let mut state =3D XArrayState::new(self, index); + let (_, ptr) =3D state.load_next()?; + Some(OccupiedEntry { state, ptr }) + } + + /// Finds the next occupied entry starting at the given index, wrappin= g around. + /// + /// Searches for an entry starting at `index` up to the maximum index.= If no entry + /// is found, wraps around and searches from index 0 up to `index`. + /// + /// # Examples + /// + /// ``` + /// # use kernel::{prelude::*, xarray::{AllocKind, XArray}}; + /// let mut xa =3D KBox::pin_init(XArray::>::new(AllocKind::= Alloc), GFP_KERNEL)?; + /// let mut guard =3D xa.lock(); + /// + /// guard.store(100, KBox::new(42u32, GFP_KERNEL)?, GFP_KERNEL)?; + /// let entry =3D guard.find_next_entry_circular(101); + /// assert_eq!(entry.map(|e| e.index()), Some(100)); + /// + /// # Ok::<(), kernel::error::Error>(()) + /// ``` + pub fn find_next_entry_circular<'b>( + &'b mut self, + index: usize, + ) -> Option> { + let mut state =3D XArrayState::new(self, index); + + // SAFETY: `state.state` is properly initialized by XArrayState::n= ew and the caller holds + // the lock. + let ptr =3D NonNull::new(unsafe { bindings::xas_find(&mut state.st= ate, usize::MAX) }) + .or_else(|| { + state.state.xa_node =3D bindings::XAS_RESTART as *mut bind= ings::xa_node; + state.state.xa_index =3D 0; + // SAFETY: `state.state` is properly initialized and by ty= pe invariant, we hold the + // xarray lock. + NonNull::new(unsafe { bindings::xas_find(&mut state.state,= index) }) + })?; + + Some(OccupiedEntry { state, ptr }) + } + /// Removes and returns the element at the given index. pub fn remove(&mut self, index: usize) -> Option { // SAFETY: @@ -422,8 +523,30 @@ fn load_next(&mut self) -> Option<(usize, NonNull)> { let ptr =3D unsafe { bindings::xas_find(&raw mut self.state, usize= ::MAX) }; NonNull::new(ptr).map(|ptr| (self.state.xa_index, ptr)) } + + fn status(&self) -> Result { + // SAFETY: `self.state` is properly initialized and valid. + to_result(unsafe { bindings::xas_error(&self.state) }) + } + + fn insert(&mut self, value: T) -> Result<*mut c_void, StoreError> { + let new =3D T::into_foreign(value).cast(); + + // SAFETY: `self.state.state` is properly initialized and `new` ca= me from `T::into_foreign`. + // We hold the xarray lock. + unsafe { bindings::xas_store(&mut self.state, new) }; + + self.status().map(|()| new).map_err(|error| { + // SAFETY: `new` came from `T::into_foreign` and `xas_store` d= oes not take ownership of + // the value on error. + let value =3D unsafe { T::from_foreign(new) }; + StoreError { value, error } + }) + } } =20 +mod entry; + // SAFETY: `XArray` has no shared mutable state so it is `Send` iff `T`= is `Send`. unsafe impl Send for XArray {} =20 diff --git a/rust/kernel/xarray/entry.rs b/rust/kernel/xarray/entry.rs new file mode 100644 index 0000000000000..1b1c21bed7022 --- /dev/null +++ b/rust/kernel/xarray/entry.rs @@ -0,0 +1,367 @@ +// SPDX-License-Identifier: GPL-2.0 + +use super::{ + Guard, + StoreError, + XArrayState, // +}; +use core::ptr::NonNull; +use kernel::{ + prelude::*, + types::ForeignOwnable, // +}; + +/// Represents either a vacant or occupied entry in an XArray. +pub enum Entry<'a, 'b, T: ForeignOwnable> { + /// A vacant entry that can have a value inserted. + Vacant(VacantEntry<'a, 'b, T>), + /// An occupied entry containing a value. + Occupied(OccupiedEntry<'a, 'b, T>), +} + +impl Entry<'_, '_, T> { + /// Returns true if this entry is occupied. + /// + /// # Examples + /// + /// ``` + /// # use kernel::{prelude::*, xarray::{AllocKind, XArray, Entry}}; + /// let mut xa =3D KBox::pin_init(XArray::>::new(AllocKind::= Alloc), GFP_KERNEL)?; + /// let mut guard =3D xa.lock(); + /// + /// + /// let entry =3D guard.entry(42); + /// assert_eq!(entry.is_occupied(), false); + /// + /// guard.store(42, KBox::new(0x1337u32, GFP_KERNEL)?, GFP_KERNEL)?; + /// let entry =3D guard.entry(42); + /// assert_eq!(entry.is_occupied(), true); + /// + /// # Ok::<(), kernel::error::Error>(()) + /// ``` + pub fn is_occupied(&self) -> bool { + matches!(self, Entry::Occupied(_)) + } +} + +/// A view into a vacant entry in an XArray. +pub struct VacantEntry<'a, 'b, T: ForeignOwnable> { + state: XArrayState<'a, 'b, T>, +} + +impl<'a, 'b, T> VacantEntry<'a, 'b, T> +where + T: ForeignOwnable, +{ + pub(crate) fn new(guard: &'b mut Guard<'a, T>, index: usize) -> Self { + Self { + state: XArrayState::new(guard, index), + } + } + + /// Inserts a value into this vacant entry. + /// + /// Returns a reference to the newly inserted value. + /// + /// - This method will fail if the nodes on the path to the index + /// represented by this entry are not present in the XArray. + /// - This method will not drop the XArray lock. + /// + /// + /// # Examples + /// + /// ``` + /// # use kernel::{prelude::*, xarray::{AllocKind, XArray, Entry}}; + /// let mut xa =3D KBox::pin_init(XArray::>::new(AllocKind::= Alloc), GFP_KERNEL)?; + /// let mut guard =3D xa.lock(); + /// + /// assert_eq!(guard.get(42), None); + /// + /// if let Entry::Vacant(entry) =3D guard.entry(42) { + /// let value =3D KBox::new(0x1337u32, GFP_KERNEL)?; + /// let borrowed =3D entry.insert(value)?; + /// assert_eq!(*borrowed, 0x1337); + /// } + /// + /// assert_eq!(guard.get(42).copied(), Some(0x1337)); + /// + /// # Ok::<(), kernel::error::Error>(()) + /// ``` + pub fn insert(mut self, value: T) -> Result, StoreE= rror> { + let new =3D self.state.insert(value)?; + + // SAFETY: `new` came from `T::into_foreign`. The entry has exclus= ive + // ownership of `new` as it holds a mutable reference to `Guard`. + Ok(unsafe { T::borrow_mut(new) }) + } + + /// Inserts a value and returns an occupied entry representing the new= ly inserted value. + /// + /// - This method will fail if the nodes on the path to the index + /// represented by this entry are not present in the XArray. + /// - This method will not drop the XArray lock. + /// + /// # Examples + /// + /// ``` + /// # use kernel::{prelude::*, xarray::{AllocKind, XArray, Entry}}; + /// let mut xa =3D KBox::pin_init(XArray::>::new(AllocKind::= Alloc), GFP_KERNEL)?; + /// let mut guard =3D xa.lock(); + /// + /// assert_eq!(guard.get(42), None); + /// + /// if let Entry::Vacant(entry) =3D guard.entry(42) { + /// let value =3D KBox::new(0x1337u32, GFP_KERNEL)?; + /// let occupied =3D entry.insert_entry(value)?; + /// assert_eq!(occupied.index(), 42); + /// } + /// + /// assert_eq!(guard.get(42).copied(), Some(0x1337)); + /// + /// # Ok::<(), kernel::error::Error>(()) + /// ``` + pub fn insert_entry(mut self, value: T) -> Result, StoreError> { + let new =3D self.state.insert(value)?; + + Ok(OccupiedEntry::<'a, 'b, T> { + state: self.state, + // SAFETY: `new` came from `T::into_foreign` and is guaranteed= non-null. + ptr: unsafe { core::ptr::NonNull::new_unchecked(new) }, + }) + } + + /// Returns the index of this vacant entry. + /// + /// # Examples + /// + /// ``` + /// # use kernel::{prelude::*, xarray::{AllocKind, XArray, Entry}}; + /// let mut xa =3D KBox::pin_init(XArray::>::new(AllocKind::= Alloc), GFP_KERNEL)?; + /// let mut guard =3D xa.lock(); + /// + /// assert_eq!(guard.get(42), None); + /// + /// if let Entry::Vacant(entry) =3D guard.entry(42) { + /// assert_eq!(entry.index(), 42); + /// } + /// + /// # Ok::<(), kernel::error::Error>(()) + /// ``` + pub fn index(&self) -> usize { + self.state.state.xa_index + } +} + +/// A view into an occupied entry in an XArray. +pub struct OccupiedEntry<'a, 'b, T: ForeignOwnable> { + pub(crate) state: XArrayState<'a, 'b, T>, + pub(crate) ptr: NonNull, +} + +impl<'a, 'b, T> OccupiedEntry<'a, 'b, T> +where + T: ForeignOwnable, +{ + pub(crate) fn new(guard: &'b mut Guard<'a, T>, index: usize, ptr: NonN= ull) -> Self { + Self { + state: XArrayState::new(guard, index), + ptr, + } + } + + /// Removes the value from this occupied entry and returns it, consumi= ng the entry. + /// + /// # Examples + /// + /// ``` + /// # use kernel::{prelude::*, xarray::{AllocKind, XArray, Entry}}; + /// let mut xa =3D KBox::pin_init(XArray::>::new(AllocKind::= Alloc), GFP_KERNEL)?; + /// let mut guard =3D xa.lock(); + /// + /// guard.store(42, KBox::new(0x1337u32, GFP_KERNEL)?, GFP_KERNEL)?; + /// assert_eq!(guard.get(42).copied(), Some(0x1337)); + /// + /// if let Entry::Occupied(entry) =3D guard.entry(42) { + /// let value =3D entry.remove(); + /// assert_eq!(*value, 0x1337); + /// } + /// + /// assert_eq!(guard.get(42), None); + /// + /// # Ok::<(), kernel::error::Error>(()) + /// ``` + pub fn remove(mut self) -> T { + // SAFETY: `self.state.state` is properly initialized and valid fo= r XAS operations. + let ptr =3D unsafe { + bindings::xas_result( + &mut self.state.state, + bindings::xa_zero_to_null(bindings::xas_store( + &mut self.state.state, + core::ptr::null_mut(), + )), + ) + }; + + // SAFETY: `ptr` is a valid return value from xas_result. + let errno =3D unsafe { bindings::xa_err(ptr) }; + + // NOTE: Storing NULL to an occupied slot never fails. This is by = design + // of the xarray data structure. If a slot is occupied, a store is= a + // simple pointer swap. + debug_assert!(errno =3D=3D 0); + + // SAFETY: + // - `ptr` came from `T::into_foreign`. + // - As this method takes self by value, the lifetimes of any [`T:= :Borrowed`] and + // [`T::BorrowedMut`] we have created must have ended. + unsafe { T::from_foreign(ptr.cast()) } + } + + /// Returns the index of this occupied entry. + /// + /// # Examples + /// + /// ``` + /// # use kernel::{prelude::*, xarray::{AllocKind, XArray, Entry}}; + /// let mut xa =3D KBox::pin_init(XArray::>::new(AllocKind::= Alloc), GFP_KERNEL)?; + /// let mut guard =3D xa.lock(); + /// + /// guard.store(42, KBox::new(0x1337u32, GFP_KERNEL)?, GFP_KERNEL)?; + /// + /// if let Entry::Occupied(entry) =3D guard.entry(42) { + /// assert_eq!(entry.index(), 42); + /// } + /// + /// # Ok::<(), kernel::error::Error>(()) + /// ``` + pub fn index(&self) -> usize { + self.state.state.xa_index + } + + /// Replaces the value in this occupied entry and returns the old valu= e. + /// + /// # Examples + /// + /// ``` + /// # use kernel::{prelude::*, xarray::{AllocKind, XArray, Entry}}; + /// let mut xa =3D KBox::pin_init(XArray::>::new(AllocKind::= Alloc), GFP_KERNEL)?; + /// let mut guard =3D xa.lock(); + /// + /// guard.store(42, KBox::new(0x1337u32, GFP_KERNEL)?, GFP_KERNEL)?; + /// + /// if let Entry::Occupied(mut entry) =3D guard.entry(42) { + /// let new_value =3D KBox::new(0x9999u32, GFP_KERNEL)?; + /// let old_value =3D entry.insert(new_value); + /// assert_eq!(*old_value, 0x1337); + /// } + /// + /// assert_eq!(guard.get(42).copied(), Some(0x9999)); + /// + /// # Ok::<(), kernel::error::Error>(()) + /// ``` + pub fn insert(&mut self, value: T) -> T { + let new =3D T::into_foreign(value).cast(); + // SAFETY: `new` came from `T::into_foreign` and is guaranteed non= -null. + self.ptr =3D unsafe { NonNull::new_unchecked(new) }; + + // SAFETY: `self.state.state` is properly initialized and valid fo= r XAS operations. + let old =3D unsafe { + bindings::xas_result( + &mut self.state.state, + bindings::xa_zero_to_null(bindings::xas_store(&mut self.st= ate.state, new)), + ) + }; + + // SAFETY: `old` is a valid return value from xas_result. + let errno =3D unsafe { bindings::xa_err(old) }; + + // NOTE: Storing NULL to an occupied slot never fails. This is by = design + // of the xarray data structure. If a slot is occupied, a store is= a + // simple pointer swap. + debug_assert!(errno =3D=3D 0); + + // SAFETY: + // - `ptr` came from `T::into_foreign`. + // - As this method takes self by value, the lifetimes of any [`T:= :Borrowed`] and + // [`T::BorrowedMut`] we have created must have ended. + unsafe { T::from_foreign(old) } + } + + /// Converts this occupied entry into a mutable reference to the value= in the slot represented + /// by the entry. + /// + /// # Examples + /// + /// ``` + /// # use kernel::{prelude::*, xarray::{AllocKind, XArray, Entry}}; + /// let mut xa =3D KBox::pin_init(XArray::>::new(AllocKind::= Alloc), GFP_KERNEL)?; + /// let mut guard =3D xa.lock(); + /// + /// guard.store(42, KBox::new(0x1337u32, GFP_KERNEL)?, GFP_KERNEL)?; + /// + /// if let Entry::Occupied(entry) =3D guard.entry(42) { + /// let value_ref =3D entry.into_mut(); + /// *value_ref =3D 0x9999; + /// } + /// + /// assert_eq!(guard.get(42).copied(), Some(0x9999)); + /// + /// # Ok::<(), kernel::error::Error>(()) + /// ``` + pub fn into_mut(self) -> T::BorrowedMut<'b> { + // SAFETY: `ptr` came from `T::into_foreign`. + unsafe { T::borrow_mut(self.ptr.as_ptr()) } + } + + /// Swaps the value in this entry with the provided value. + /// + /// Returns the old value that was in the entry. + /// + /// # Examples + /// + /// ``` + /// # use kernel::{prelude::*, xarray::{AllocKind, XArray, Entry}}; + /// let mut xa =3D KBox::pin_init(XArray::>::new(AllocKind::= Alloc), GFP_KERNEL)?; + /// let mut guard =3D xa.lock(); + /// + /// guard.store(42, KBox::new(100u32, GFP_KERNEL)?, GFP_KERNEL)?; + /// + /// if let Entry::Occupied(mut entry) =3D guard.entry(42) { + /// let mut other =3D 200u32; + /// entry.swap(&mut other); + /// assert_eq!(other, 100); + /// assert_eq!(*entry, 200); + /// } + /// + /// # Ok::<(), kernel::error::Error>(()) + /// ``` + pub fn swap(&mut self, other: &mut U) + where + T: for<'c> ForeignOwnable =3D &'c U, BorrowedMut<'c> = =3D &'c mut U>, + { + use core::ops::DerefMut; + core::mem::swap(self.deref_mut(), other); + } +} + +impl core::ops::Deref for OccupiedEntry<'_, '_, T> +where + T: for<'a> ForeignOwnable =3D &'a U, BorrowedMut<'a> =3D = &'a mut U>, +{ + type Target =3D U; + + fn deref(&self) -> &Self::Target { + // SAFETY: `ptr` came from `T::into_foreign`. + unsafe { T::borrow(self.ptr.as_ptr()) } + } +} + +impl core::ops::DerefMut for OccupiedEntry<'_, '_, T> +where + T: for<'a> ForeignOwnable =3D &'a U, BorrowedMut<'a> =3D = &'a mut U>, +{ + fn deref_mut(&mut self) -> &mut Self::Target { + // SAFETY: `ptr` came from `T::into_foreign`. + unsafe { T::borrow_mut(self.ptr.as_ptr()) } + } +} --=20 2.51.2 From nobody Sat Feb 7 23:24:09 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0990F311979; Fri, 6 Feb 2026 21:12:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770412332; cv=none; b=FFZLckUIpTBbo+CdUqdj2k7u1Wvpp8KsN4bDtvdze+u2DPw2v65tGFXqUOGgdcKXAm41YEIDQkPC69E9XkUQHggfdEJI3FvyuHFd0rFmtTiKCx9Bmipd0nYCHgLLuFRkt/CPD9Ord9rfld+pNLYob62RpifCG+XnSBuCFH6GoEA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770412332; c=relaxed/simple; bh=C8QF46vi4KY242ax7lajp6MFFf081K0dM+QSFTirzLs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ErJG7h3B5YkbUeJ2zb0trc9newDX3O7IMUAfbfsvsME0dnPk9v1nx7OWH+gkgvO0SNMD2dyaWzB+roCf8QOXwUC5fvltktdIrZxwccBBQZEDqN6Hj61pneEwqHgeHuuC1VdQXLQATwRGE7E/thmYrgMhoG+hma2BfqGAi9wa/90= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=unFx6tXI; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="unFx6tXI" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 36E5CC116C6; Fri, 6 Feb 2026 21:12:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770412331; bh=C8QF46vi4KY242ax7lajp6MFFf081K0dM+QSFTirzLs=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=unFx6tXItVCx6MU6Pc1zALMd4icy/GYUrGt67YJCBVNfAeUXJ1WNnJCMzLkmWiIkD z3+hBeXDVjD/szXfSzO1YM7fGhg7zzPy6k+wKdzeb26hyNevvtOUEjs4g6/3G0dTYX 7XXmoYYB0J3lhzi+m0R6PmkDirHoySJf+WFP9x2sjcdYY2JBDBdhF3JpwMnx5Bgrg5 6Kz8TAc0A7WZefR5WZIoDc3+GCg2A5U54RYok6tjAdCLTq8HY9Ofss9QVJb5aVQe5O ymnY/HhIMJ23EHbU2YUGOViMo+FA5Cx0baEU3Ojh0GyirMRkdSm8uAReIQvzRB6MuL s+jXrkT5CRw9w== From: Andreas Hindborg Date: Fri, 06 Feb 2026 22:10:55 +0100 Subject: [PATCH v2 09/11] rust: mm: add abstractions for allocating from a `sheaf` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260206-xarray-entry-send-v2-9-91c41673fd30@kernel.org> References: <20260206-xarray-entry-send-v2-0-91c41673fd30@kernel.org> In-Reply-To: <20260206-xarray-entry-send-v2-0-91c41673fd30@kernel.org> To: Tamir Duberstein , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Andrew Morton , Christoph Lameter , David Rientjes , Roman Gushchin , Harry Yoo Cc: Daniel Gomez , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andreas Hindborg , "Matthew Wilcox (Oracle)" X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=16716; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=C8QF46vi4KY242ax7lajp6MFFf081K0dM+QSFTirzLs=; b=owEBbQKS/ZANAwAKAeG4Gj55KGN3AcsmYgBphljpgmj7e9GgNxEZA7YfgF5ubO1egUZc7vVXT enlR3PK6oeJAjMEAAEKAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaYZY6QAKCRDhuBo+eShj d4aVD/9gtBnAtpFQT1SQMheqqLPa5ehB0PdhSm6TVkjm4XPDi4zqB7nyGGkUHUcnw+7BNdJf7tf mRxYQHOW6ANS5WdQJpn8NsHFo1VLeQG5Fed39IA6odDUDCzijmwwam3r/r5cYieOS0+7EAuvVVJ 5WE3x9q9ARq0eEhKPpAYGQcPt8A+s01kkdkzON3qctzZYGnRcRneykk1HguTLHkBoipazR+bJUh lqgnEGItVv4KlTyX5FJilJ6G84yhTiZLmgn60UawewRuyDLFOm8jZezq8YemnKDJ7QBNno8FLZP lXEyBDF7bQ1I7CaWichspzhG7qIIBRAMwO5ydJmFRoRXCnYV/+1hDaMZRq3LIva07Od7ZvBe/9A 6MECNm0FutiwPKi144DY5A3CjfoHIhLQjIoo3NSO/BQijd2Cm0w4KJRbFRF65BM1BhavevYvG9p P+emzliSQhVi5Trt2PAVCg2FB6aA6IY6e2t6ZsYNutu7Fp8vZccQsVUGw6j+KFNe+t+Cvkr7wxs ua3ka9eAGJadbzRoUtNzlk3RtWOWxKXf3/O9nTNcfM7JffMaHK0Vx0GZvO1CU9aWCIclpEKEd8i xBGQJuqOXAjfABU3MHgbq9JDukMtKE91YbNFUuu7o3z29GOTyWAXi4MgeXreunRAOl5kfM3TY2j qR5wqkF8BuMRDuQ== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add Rust APIs for allocating objects from a `sheaf`. Introduce a reduced abstraction `KMemCacheInit` for `struct kmem_cache` to support management of the `Sheaf`s. Initialize objects using in-place initialization when objects are allocated from a `Sheaf`. This is different from C which tends to do some initialization when the cache is filled. This approach is chosen because there is no destructor/drop capability in `struct kmem_cache` that can be invoked when the cache is dropped. Cc: Vlastimil Babka Cc: "Liam R. Howlett" Cc: "Matthew Wilcox (Oracle)" Cc: Lorenzo Stoakes Cc: linux-mm@kvack.org Signed-off-by: Andreas Hindborg --- rust/kernel/mm.rs | 2 + rust/kernel/mm/sheaf.rs | 406 ++++++++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 408 insertions(+) diff --git a/rust/kernel/mm.rs b/rust/kernel/mm.rs index 4764d7b68f2a7..fcfa5a97ebf0a 100644 --- a/rust/kernel/mm.rs +++ b/rust/kernel/mm.rs @@ -18,6 +18,8 @@ }; use core::{ops::Deref, ptr::NonNull}; =20 +#[cfg(not(any(CONFIG_SLUB_TINY, CONFIG_SLUB_DEBUG)))] +pub mod sheaf; pub mod virt; use virt::VmaRef; =20 diff --git a/rust/kernel/mm/sheaf.rs b/rust/kernel/mm/sheaf.rs new file mode 100644 index 0000000000000..c92750eaf1c4a --- /dev/null +++ b/rust/kernel/mm/sheaf.rs @@ -0,0 +1,406 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Slub allocator sheaf abstraction. +//! +//! Sheaves are percpu array-based caching layers for the slub allocator. +//! They provide a mechanism for pre-allocating objects that can later +//! be retrieved without risking allocation failure, making them useful in +//! contexts where memory allocation must be guaranteed to succeed. +//! +//! The term "sheaf" is the english word for a bundle of straw. In this co= ntext +//! it means a bundle of pre-allocated objects. A per-NUMA-node cache of s= heaves +//! is called a "barn". Because you store your sheafs in barns. +//! +//! # Use cases +//! +//! Sheaves are particularly useful when: +//! +//! - Allocations must be guaranteed to succeed in a restricted context (e= .g., +//! while holding locks or in atomic context). +//! - Multiple allocations need to be performed as a batch operation. +//! - Fast-path allocation performance is critical, as sheaf allocations a= void +//! atomic operations by using local locks with preemption disabled. +//! +//! # Architecture +//! +//! The sheaf system consists of three main components: +//! +//! - [`KMemCache`]: A slab cache configured with sheaf support. +//! - [`Sheaf`]: A pre-filled container of objects from a specific cache. +//! - [`SBox`]: An owned allocation from a sheaf, similar to a `Box`. +//! +//! # Example +//! +//! ``` +//! use kernel::c_str; +//! use kernel::mm::sheaf::{KMemCache, KMemCacheInit, Sheaf, SBox}; +//! use kernel::prelude::*; +//! +//! struct MyObject { +//! value: u32, +//! } +//! +//! impl KMemCacheInit for MyObject { +//! fn init() -> impl Init { +//! init!(MyObject { value: 0 }) +//! } +//! } +//! +//! // Create a cache with sheaf capacity of 16 objects. +//! let cache =3D KMemCache::::new(c_str!("my_cache"), 16)?; +//! +//! // Pre-fill a sheaf with 8 objects. +//! let mut sheaf =3D cache.as_arc_borrow().sheaf(8, GFP_KERNEL)?; +//! +//! // Allocations from the sheaf are guaranteed to succeed until empty. +//! let obj =3D sheaf.alloc().unwrap(); +//! +//! // Return the sheaf when done, attempting to refill it. +//! sheaf.return_refill(GFP_KERNEL); +//! # Ok::<(), Error>(()) +//! ``` +//! +//! # Constraints +//! +//! - Sheaves are disabled when `CONFIG_SLUB_TINY` is enabled. +//! - Sheaves are disabled when slab debugging (`slub_debug`) is active. +//! - The sheaf capacity is fixed at cache creation time. + +use core::{ + convert::Infallible, + marker::PhantomData, + ops::{Deref, DerefMut}, + ptr::NonNull, +}; + +use kernel::prelude::*; + +use crate::sync::{Arc, ArcBorrow}; + +/// A slab cache with sheaf support. +/// +/// This type wraps a kernel `kmem_cache` configured with a sheaf capacity, +/// enabling pre-allocation of objects via [`Sheaf`]. +/// +/// For now, this type only exists for sheaf management. +/// +/// # Type parameter +/// +/// - `T`: The type of objects managed by this cache. Must implement +/// [`KMemCacheInit`] to provide initialization logic for new allocation= s. +/// +/// # Invariants +/// +/// - `cache` is a valid pointer to a `kmem_cache` created with +/// `__kmem_cache_create_args`. +/// - The cache is valid for the lifetime of this struct. +pub struct KMemCache> { + cache: NonNull, + _p: PhantomData, +} + +impl> KMemCache { + /// Creates a new slab cache with sheaf support. + /// + /// Creates a kernel slab cache for objects of type `T` with the speci= fied + /// sheaf capacity. The cache uses the provided `name` for identificat= ion + /// in `/sys/kernel/slab/` and debugging output. + /// + /// # Arguments + /// + /// - `name`: A string identifying the cache. This name appears in sys= fs and + /// debugging output. + /// - `sheaf_capacity`: The maximum number of objects a sheaf from this + /// cache can hold. A capacity of zero disables sheaf support. + /// + /// # Errors + /// + /// Returns an error if: + /// + /// - The cache could not be created due to memory pressure. + /// - The size of `T` cannot be represented as a `c_uint`. + pub fn new(name: &CStr, sheaf_capacity: u32) -> Result> + where + T: KMemCacheInit, + { + let flags =3D 0; + let mut args: bindings::kmem_cache_args =3D pin_init::zeroed(); + args.sheaf_capacity =3D sheaf_capacity; + + // NOTE: We are not initializing at object allocation time, because + // there is no matching teardown function on the C side machinery. + args.ctor =3D None; + + // SAFETY: `name` is a valid C string, `args` is properly initiali= zed, + // and the size of `T` has been validated to fit in a `c_uint`. + let ptr =3D unsafe { + bindings::__kmem_cache_create_args( + name.as_ptr().cast::(), + core::mem::size_of::().try_into()?, + &mut args, + flags, + ) + }; + + // INVARIANT: `ptr` was returned by `__kmem_cache_create_args` and= is + // non-null (checked below). The cache is valid until + // `kmem_cache_destroy` is called in `Drop`. + Ok(Arc::new( + Self { + cache: NonNull::new(ptr).ok_or(ENOMEM)?, + _p: PhantomData, + }, + GFP_KERNEL, + )?) + } + + /// Creates a pre-filled sheaf from this cache. + /// + /// Allocates a sheaf and pre-fills it with `size` objects. Once creat= ed, + /// allocations from the sheaf via [`Sheaf::alloc`] are guaranteed to + /// succeed until the sheaf is depleted. + /// + /// # Arguments + /// + /// - `size`: The number of objects to pre-allocate. Must not exceed t= he + /// cache's `sheaf_capacity`. + /// - `gfp`: Allocation flags controlling how memory is obtained. Use + /// [`GFP_KERNEL`] for normal allocations that may sleep, or + /// [`GFP_NOWAIT`] for non-blocking allocations. + /// + /// # Errors + /// + /// Returns [`ENOMEM`] if the sheaf or its objects could not be alloca= ted. + /// + /// # Warnings + /// + /// The kernel will warn if `size` exceeds `sheaf_capacity`. + pub fn sheaf( + self: ArcBorrow<'_, Self>, + size: usize, + gfp: kernel::alloc::Flags, + ) -> Result> { + // SAFETY: `self.as_raw()` returns a valid cache pointer, and `siz= e` + // has been validated to fit in a `c_uint`. + let ptr =3D unsafe { + bindings::kmem_cache_prefill_sheaf(self.as_raw(), gfp.as_raw()= , size.try_into()?) + }; + + // INVARIANT: `ptr` was returned by `kmem_cache_prefill_sheaf` and= is + // non-null (checked below). `cache` is the cache from which this = sheaf + // was created. `dropped` is false since the sheaf has not been re= turned. + Ok(Sheaf { + sheaf: NonNull::new(ptr).ok_or(ENOMEM)?, + cache: self.into(), + dropped: false, + }) + } + + fn as_raw(&self) -> *mut bindings::kmem_cache { + self.cache.as_ptr() + } +} + +impl> Drop for KMemCache { + fn drop(&mut self) { + // SAFETY: `self.as_raw()` returns a valid cache pointer that was + // created by `__kmem_cache_create_args`. As all objects allocated= from + // this hold a reference on `self`, they must have been dropped fo= r this + // `drop` method to execute. + unsafe { bindings::kmem_cache_destroy(self.as_raw()) }; + } +} + +/// Trait for types that can be initialized in a slab cache. +/// +/// This trait provides the initialization logic for objects allocated fro= m a +/// [`KMemCache`]. When the slab allocator creates new objects, it invokes= the +/// constructor to ensure objects are in a valid initial state. +/// +/// # Implementation +/// +/// Implementors must provide [`init`](KMemCacheInit::init), which returns +/// a in-place initializer for the type. +/// +/// # Example +/// +/// ``` +/// use kernel::mm::sheaf::KMemCacheInit; +/// use kernel::prelude::*; +/// +/// struct MyData { +/// counter: u32, +/// name: [u8; 16], +/// } +/// +/// impl KMemCacheInit for MyData { +/// fn init() -> impl Init { +/// init!(MyData { +/// counter: 0, +/// name: [0; 16], +/// }) +/// } +/// } +/// ``` +pub trait KMemCacheInit { + /// Returns an initializer for creating new objects of type `T`. + /// + /// This method is called by the allocator's constructor to initialize= newly + /// allocated objects. The initializer should set all fields to their + /// default or initial values. + fn init() -> impl Init; +} + +/// A pre-filled container of slab objects. +/// +/// A sheaf holds a set of pre-allocated objects from a [`KMemCache`]. +/// Allocations from a sheaf are guaranteed to succeed until the sheaf is +/// depleted, making sheaves useful in contexts where allocation failure is +/// not acceptable. +/// +/// Sheaves provide faster allocation than direct allocation because they = use +/// local locks with preemption disabled rather than atomic operations. +/// +/// # Lifecycle +/// +/// Sheaves are created via [`KMemCache::sheaf`] and should be returned to= the +/// allocator when no longer needed via [`Sheaf::return_refill`]. If a she= af is +/// simply dropped, it is returned with `GFP_NOWAIT` flags, which may resu= lt in +/// the sheaf being flushed and freed rather than being cached for reuse. +/// +/// # Invariants +/// +/// - `sheaf` is a valid pointer to a `slab_sheaf` obtained from +/// `kmem_cache_prefill_sheaf`. +/// - `cache` is the cache from which this sheaf was created. +/// - `dropped` tracks whether the sheaf has been explicitly returned. +pub struct Sheaf> { + sheaf: NonNull, + cache: Arc>, + dropped: bool, +} + +impl> Sheaf { + fn as_raw(&self) -> *mut bindings::slab_sheaf { + self.sheaf.as_ptr() + } + + /// Return the sheaf and try to refill using `flags`. + /// + /// If the sheaf cannot simply become the percpu spare sheaf, but ther= e's + /// space for a full sheaf in the barn, we try to refill the sheaf bac= k to + /// the cache's sheaf_capacity to avoid handling partially full sheave= s. + /// + /// If the refill fails because gfp is e.g. GFP_NOWAIT, or the barn is= full, + /// the sheaf is instead flushed and freed. + pub fn return_refill(mut self, flags: kernel::alloc::Flags) { + self.dropped =3D true; + // SAFETY: `self.cache.as_raw()` and `self.as_raw()` return valid + // pointers to the cache and sheaf respectively. + unsafe { + bindings::kmem_cache_return_sheaf(self.cache.as_raw(), flags.a= s_raw(), self.as_raw()) + }; + drop(self); + } + + /// Allocates an object from the sheaf. + /// + /// Returns a new [`SBox`] containing an initialized object, or [`None= `] + /// if the sheaf is depleted. Allocations are guaranteed to succeed as + /// long as the sheaf contains pre-allocated objects. + /// + /// The `gfp` flags passed to `kmem_cache_alloc_from_sheaf` are set to= zero, + /// meaning no additional flags like `__GFP_ZERO` or `__GFP_ACCOUNT` a= re + /// applied. + /// + /// The returned `T` is initialized as part of this function. + pub fn alloc(&mut self) -> Option> { + // SAFETY: `self.cache.as_raw()` and `self.as_raw()` return valid + // pointers. The function returns NULL when the sheaf is empty. + let ptr =3D unsafe { + bindings::kmem_cache_alloc_from_sheaf_noprof(self.cache.as_raw= (), 0, self.as_raw()) + }; + + // SAFETY: + // - `ptr` is a valid pointer as it was just returned by the cache. + // - The initializer is infallible, so an error is never returned. + unsafe { T::init().__init(ptr.cast()) }.expect("Initializer is inf= allible"); + + let ptr =3D NonNull::new(ptr.cast::())?; + + // INVARIANT: `ptr` was returned by `kmem_cache_alloc_from_sheaf_n= oprof` + // and initialized above. `cache` is the cache from which this obj= ect + // was allocated. The object remains valid until freed in `Drop`. + Some(SBox { + ptr, + cache: self.cache.clone(), + }) + } +} + +impl> Drop for Sheaf { + fn drop(&mut self) { + if !self.dropped { + // SAFETY: `self.cache.as_raw()` and `self.as_raw()` return va= lid + // pointers. Using `GFP_NOWAIT` because the drop may occur in a + // context where sleeping is not permitted. + unsafe { + bindings::kmem_cache_return_sheaf( + self.cache.as_raw(), + GFP_NOWAIT.as_raw(), + self.as_raw(), + ) + }; + } + } +} + +/// An owned allocation from a cache sheaf. +/// +/// `SBox` is similar to `Box` but is backed by a slab cache allocation ob= tained +/// through a [`Sheaf`]. It provides owned access to an initialized object= and +/// ensures the object is properly freed back to the cache when dropped. +/// +/// The contained `T` is initialized when the `SBox` is returned from allo= c and +/// dropped when the `SBox` is dropped. +/// +/// # Invariants +/// +/// - `ptr` points to a valid, initialized object of type `T`. +/// - `cache` is the cache from which this object was allocated. +/// - The object remains valid for the lifetime of the `SBox`. +pub struct SBox> { + ptr: NonNull, + cache: Arc>, +} + +impl> Deref for SBox { + type Target =3D T; + + fn deref(&self) -> &Self::Target { + // SAFETY: `ptr` is valid and properly aligned per the type invari= ants. + unsafe { self.ptr.as_ref() } + } +} + +impl> DerefMut for SBox { + fn deref_mut(&mut self) -> &mut Self::Target { + // SAFETY: `ptr` is valid and properly aligned per the type invari= ants, + // and we have exclusive access via `&mut self`. + unsafe { self.ptr.as_mut() } + } +} + +impl> Drop for SBox { + fn drop(&mut self) { + // SAFETY: By type invariant, `ptr` points to a valid and initiali= zed + // object. We do not touch `ptr` after returning it to the cache. + unsafe { core::ptr::drop_in_place(self.ptr.as_ptr()) }; + + // SAFETY: `self.ptr` was allocated from `self.cache` via + // `kmem_cache_alloc_from_sheaf_noprof` and is valid. + unsafe { + bindings::kmem_cache_free(self.cache.as_raw(), self.ptr.as_ptr= ().cast()); + } + } +} --=20 2.51.2 From nobody Sat Feb 7 23:24:09 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7B15E3101D8; Fri, 6 Feb 2026 21:11:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770412316; cv=none; b=IYzHnHNzQeCLCeAR+trdLFItH06mOXRD6PyrxY30C+ibHiz7Bg7lYnEWUmyT50uvkJxN9Kmx/JoXr7fhhX/yVJXvAphtGqT0NR8pM0lebkqgXn03hsy9c5c9ckFVi4aHiO+o2MvJSjYS/BfvyxI3fvYlM6xe13OsLS5mojy8D9A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770412316; c=relaxed/simple; bh=yDktjoOvxUj5UzZCRZYJztqCsy8J1BqG4VuAbKzhGVM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=m5TcMQNCXKHyj4UkO3cKqsQBVapgbzbXoFw4h6V4sOVvsE9mOOYnnZ9uuqd88HMwuwBlH9TroU6qcR1xdHq2bvwjyQhr2vZOoWNxNfyEoBSYtZnObbiG0OIpdGB2y/hn0w3noNl9gbGnbcg8X1MCO2V9oujRdU0EXZYqkMxKf38= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=QYuRK1L3; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="QYuRK1L3" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 975CEC116C6; Fri, 6 Feb 2026 21:11:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770412316; bh=yDktjoOvxUj5UzZCRZYJztqCsy8J1BqG4VuAbKzhGVM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=QYuRK1L3VHaNyOyxJ2zyBeowLDBfuXpuxz9w1TFXWWhcRd0T/qinNnrDJUHG2j7XM lv09TXjQIOF15GtPyMSFsHA625gMUdQLXeKPMuqkomkkywNsGoHFsNo3PCYC/g7Fbd 7IhEtmaRqsgUUboVe3TfYEvTbLdvPWbSEWBon3C18RpFQ6t3mQTKupXB004TYLmTX5 VCY36Jc9VVbARvBjR+rgIba8qrPvT9oiJ95Yp6ewPifjsHEc+vmxn4OutNJWW0Eijf VQa2yWubU14qJ0/3qHdmGH2BgwTaqcXASbXTBh/Sr2CEbwS5i+h3PoKr4uRuSkuQc7 XMMJ5V0krH2ug== From: Andreas Hindborg Date: Fri, 06 Feb 2026 22:10:56 +0100 Subject: [PATCH v2 10/11] rust: mm: sheaf: allow use of C initialized static caches Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260206-xarray-entry-send-v2-10-91c41673fd30@kernel.org> References: <20260206-xarray-entry-send-v2-0-91c41673fd30@kernel.org> In-Reply-To: <20260206-xarray-entry-send-v2-0-91c41673fd30@kernel.org> To: Tamir Duberstein , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Andrew Morton , Christoph Lameter , David Rientjes , Roman Gushchin , Harry Yoo Cc: Daniel Gomez , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andreas Hindborg , "Matthew Wilcox (Oracle)" X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=21503; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=yDktjoOvxUj5UzZCRZYJztqCsy8J1BqG4VuAbKzhGVM=; b=owEBbQKS/ZANAwAKAeG4Gj55KGN3AcsmYgBphljqqXLQm4Ui2fm/F6g9svnOtGV2NsNdN5ysM sWS7I1xBnSJAjMEAAEKAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaYZY6gAKCRDhuBo+eShj d+0CD/9025QcoAqR2C6buBG07j9NGla+7qRh9BZ6UcaxA0EoY0EyZpG6S1ZK2O+gsFQtzjtmUN/ 5m83jltkZY6db81CW3loYUGG5ew6T319jWJNRAHBjXsvzLNpvbv/dCcZQxgTcthwlb4KCVaW9yu bG3zCr5yBTo1hfYnn8CkZ6nI4p+CXmcJXgjrPuiOPO4f7+MKAYEfufz7aksZingeXS8E+X6KDRz FHGK7RIzc0IX3x1JLvrYPDZcU4QVGUMvaKJxMKkLYDmKh8tFasMZ3iBTkOYZAsgEGFY8QmCIctT 8AIG0iTL5H5XAOVmMUcHloS4P4lrlo0fL+4GlhlUYbPJrHmi3uSdsr27RIZb87a0hyN4IIBjsPc GiN2fJ4tIISN7WLdcoPQJysZq5SKgqhHeHwPg2eKgZY+0D+RiXA5eK32C6wM3payqUCRHkuaB9X 5BpygRvMnwEquQX6ecAABGyL3faxc6v7Au0CY/vjML3Pa6GrIlKThKNkI/EM98rz01fWVVzlBZN AMbG1gMcKsBfkW8u00ZIChWLUhiQs5zChW7Bw4xy3vmQFF0fwRJgta5hBldAyq9o9G2iCk8UJ7I 5Q19TXoYepiJd+owPGFuMqGaECnwcyaWPKiy+vnFSPnZ5KGcN8D6uZ8n0C8zg69p7yUKSZERsWK vRTmCN8xgrfMyrQ== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Extend the sheaf abstraction to support caches initialized by C at kernel boot time, in addition to dynamically created Rust caches. Introduce `KMemCache` as a transparent wrapper around `kmem_cache` for static caches with `'static` lifetime. Rename the previous `KMemCache` to `KMemCacheHandle` to represent dynamically created, reference-counted caches. Add `Static` and `Dynamic` marker types along with `StaticSheaf` and `DynamicSheaf` type aliases to distinguish sheaves from each cache type. The `Sheaf` type now carries lifetime and allocation mode type parameters. Add `SBox::into_ptr()` and `SBox::static_from_ptr()` methods for passing allocations through C code via raw pointers. Add `KMemCache::from_raw()` for wrapping C-initialized static caches and `Sheaf::refill()` for replenishing a sheaf to a minimum size. Export `kmem_cache_prefill_sheaf`, `kmem_cache_return_sheaf`, `kmem_cache_refill_sheaf`, and `kmem_cache_alloc_from_sheaf_noprof` to allow Rust module code to use the sheaf API. Cc: Vlastimil Babka Cc: "Liam R. Howlett" Cc: "Matthew Wilcox (Oracle)" Cc: Lorenzo Stoakes Cc: linux-mm@kvack.org Signed-off-by: Andreas Hindborg --- mm/slub.c | 4 + rust/kernel/mm/sheaf.rs | 343 +++++++++++++++++++++++++++++++++++++++++++-= ---- 2 files changed, 317 insertions(+), 30 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index f77b7407c51bc..7c6b1d28778d0 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -5428,6 +5428,7 @@ kmem_cache_prefill_sheaf(struct kmem_cache *s, gfp_t = gfp, unsigned int size) =20 return sheaf; } +EXPORT_SYMBOL(kmem_cache_prefill_sheaf); =20 /* * Use this to return a sheaf obtained by kmem_cache_prefill_sheaf() @@ -5483,6 +5484,7 @@ void kmem_cache_return_sheaf(struct kmem_cache *s, gf= p_t gfp, barn_put_full_sheaf(barn, sheaf); stat(s, BARN_PUT); } +EXPORT_SYMBOL(kmem_cache_return_sheaf); =20 /* * refill a sheaf previously returned by kmem_cache_prefill_sheaf to at le= ast @@ -5536,6 +5538,7 @@ int kmem_cache_refill_sheaf(struct kmem_cache *s, gfp= _t gfp, *sheafp =3D sheaf; return 0; } +EXPORT_SYMBOL(kmem_cache_refill_sheaf); =20 /* * Allocate from a sheaf obtained by kmem_cache_prefill_sheaf() @@ -5573,6 +5576,7 @@ kmem_cache_alloc_from_sheaf_noprof(struct kmem_cache = *s, gfp_t gfp, =20 return ret; } +EXPORT_SYMBOL(kmem_cache_alloc_from_sheaf_noprof); =20 unsigned int kmem_cache_sheaf_size(struct slab_sheaf *sheaf) { diff --git a/rust/kernel/mm/sheaf.rs b/rust/kernel/mm/sheaf.rs index c92750eaf1c4a..a604246714f7b 100644 --- a/rust/kernel/mm/sheaf.rs +++ b/rust/kernel/mm/sheaf.rs @@ -23,17 +23,26 @@ //! //! # Architecture //! -//! The sheaf system consists of three main components: +//! The sheaf system supports two modes of operation: +//! +//! - **Static caches**: [`KMemCache`] represents a cache initialized by C= code at +//! kernel boot time. These have `'static` lifetime and produce [`Static= Sheaf`] +//! instances. +//! - **Dynamic caches**: [`KMemCacheHandle`] wraps a cache created at run= time by +//! Rust code. These are reference-counted and produce [`DynamicSheaf`] = instances. +//! +//! Both modes use the same core types: //! -//! - [`KMemCache`]: A slab cache configured with sheaf support. //! - [`Sheaf`]: A pre-filled container of objects from a specific cache. //! - [`SBox`]: An owned allocation from a sheaf, similar to a `Box`. //! //! # Example //! +//! Using a dynamically created cache: +//! //! ``` //! use kernel::c_str; -//! use kernel::mm::sheaf::{KMemCache, KMemCacheInit, Sheaf, SBox}; +//! use kernel::mm::sheaf::{KMemCacheHandle, KMemCacheInit, Sheaf, SBox}; //! use kernel::prelude::*; //! //! struct MyObject { @@ -47,7 +56,7 @@ //! } //! //! // Create a cache with sheaf capacity of 16 objects. -//! let cache =3D KMemCache::::new(c_str!("my_cache"), 16)?; +//! let cache =3D KMemCacheHandle::::new(c_str!("my_cache"), 16)= ?; //! //! // Pre-fill a sheaf with 8 objects. //! let mut sheaf =3D cache.as_arc_borrow().sheaf(8, GFP_KERNEL)?; @@ -75,7 +84,102 @@ =20 use kernel::prelude::*; =20 -use crate::sync::{Arc, ArcBorrow}; +use crate::{ + sync::{Arc, ArcBorrow}, + types::Opaque, +}; + +/// A slab cache with sheaf support. +/// +/// This type is a transparent wrapper around a kernel `kmem_cache`. It ca= n be +/// used with caches created either by C code or via [`KMemCacheHandle`]. +/// +/// When a reference to this type has `'static` lifetime (i.e., `&'static +/// KMemCache`), it typically represents a cache initialized by C at bo= ot +/// time. Such references produce [`StaticSheaf`] instances via [`sheaf`]. +/// +/// [`sheaf`]: KMemCache::sheaf +/// +/// # Type parameter +/// +/// - `T`: The type of objects managed by this cache. Must implement +/// [`KMemCacheInit`] to provide initialization logic for allocations. +#[repr(transparent)] +pub struct KMemCache> { + inner: Opaque, + _p: PhantomData, +} + +impl> KMemCache { + /// Creates a pre-filled sheaf from this cache. + /// + /// Allocates a sheaf and pre-fills it with `size` objects. Once creat= ed, + /// allocations from the sheaf via [`Sheaf::alloc`] are guaranteed to + /// succeed until the sheaf is depleted. + /// + /// # Arguments + /// + /// - `size`: The number of objects to pre-allocate. Must not exceed t= he + /// cache's `sheaf_capacity`. + /// - `gfp`: Allocation flags controlling how memory is obtained. Use + /// [`GFP_KERNEL`] for normal allocations that may sleep, or + /// [`GFP_NOWAIT`] for non-blocking allocations. + /// + /// # Errors + /// + /// Returns [`ENOMEM`] if the sheaf or its objects could not be alloca= ted. + /// + /// # Warnings + /// + /// The kernel will warn if `size` exceeds `sheaf_capacity`. + pub fn sheaf( + &'static self, + size: usize, + gfp: kernel::alloc::Flags, + ) -> Result> { + // SAFETY: `self.as_raw()` returns a valid cache pointer, and `siz= e` + // has been validated to fit in a `c_uint`. + let ptr =3D unsafe { + bindings::kmem_cache_prefill_sheaf(self.inner.get(), gfp.as_ra= w(), size.try_into()?) + }; + + // INVARIANT: `ptr` was returned by `kmem_cache_prefill_sheaf` and= is + // non-null (checked below). `cache` is the cache from which this = sheaf + // was created. `dropped` is false since the sheaf has not been re= turned. + Ok(Sheaf { + sheaf: NonNull::new(ptr).ok_or(ENOMEM)?, + // SAFETY: `self` is a valid reference, so the pointer is non-= null. + cache: CacheRef::Static(unsafe { + NonNull::new_unchecked((&raw const *self).cast_mut()) + }), + dropped: false, + _p: PhantomData, + }) + } + + fn as_raw(&self) -> *mut bindings::kmem_cache { + self.inner.get() + } + + /// Creates a reference to a [`KMemCache`] from a raw pointer. + /// + /// This is useful for wrapping a C-initialized static `kmem_cache`, s= uch as + /// the global `radix_tree_node_cachep` used by XArrays. + /// + /// # Safety + /// + /// - `ptr` must be a valid pointer to a `kmem_cache` that was created= for + /// objects of type `T`. + /// - The cache must remain valid for the lifetime `'a`. + /// - The caller must ensure that the cache was configured appropriate= ly for + /// the type `T`, including proper size and alignment. + pub unsafe fn from_raw<'a>(ptr: *mut bindings::kmem_cache) -> &'a Self= { + // SAFETY: The caller guarantees that `ptr` is a valid pointer to a + // `kmem_cache` created for objects of type `T`, that it remains v= alid + // for lifetime `'a`, and that the cache is properly configured fo= r `T`. + unsafe { &*ptr.cast::() } + } +} =20 /// A slab cache with sheaf support. /// @@ -94,12 +198,12 @@ /// - `cache` is a valid pointer to a `kmem_cache` created with /// `__kmem_cache_create_args`. /// - The cache is valid for the lifetime of this struct. -pub struct KMemCache> { - cache: NonNull, - _p: PhantomData, +#[repr(transparent)] +pub struct KMemCacheHandle> { + cache: NonNull>, } =20 -impl> KMemCache { +impl> KMemCacheHandle { /// Creates a new slab cache with sheaf support. /// /// Creates a kernel slab cache for objects of type `T` with the speci= fied @@ -147,8 +251,7 @@ pub fn new(name: &CStr, sheaf_capacity: u32) -> Result<= Arc> // `kmem_cache_destroy` is called in `Drop`. Ok(Arc::new( Self { - cache: NonNull::new(ptr).ok_or(ENOMEM)?, - _p: PhantomData, + cache: NonNull::new(ptr.cast()).ok_or(ENOMEM)?, }, GFP_KERNEL, )?) @@ -175,11 +278,11 @@ pub fn new(name: &CStr, sheaf_capacity: u32) -> Resul= t> /// # Warnings /// /// The kernel will warn if `size` exceeds `sheaf_capacity`. - pub fn sheaf( - self: ArcBorrow<'_, Self>, + pub fn sheaf<'a>( + self: ArcBorrow<'a, Self>, size: usize, gfp: kernel::alloc::Flags, - ) -> Result> { + ) -> Result> { // SAFETY: `self.as_raw()` returns a valid cache pointer, and `siz= e` // has been validated to fit in a `c_uint`. let ptr =3D unsafe { @@ -191,17 +294,18 @@ pub fn sheaf( // was created. `dropped` is false since the sheaf has not been re= turned. Ok(Sheaf { sheaf: NonNull::new(ptr).ok_or(ENOMEM)?, - cache: self.into(), + cache: CacheRef::Arc(self.into()), dropped: false, + _p: PhantomData, }) } =20 fn as_raw(&self) -> *mut bindings::kmem_cache { - self.cache.as_ptr() + self.cache.as_ptr().cast() } } =20 -impl> Drop for KMemCache { +impl> Drop for KMemCacheHandle { fn drop(&mut self) { // SAFETY: `self.as_raw()` returns a valid cache pointer that was // created by `__kmem_cache_create_args`. As all objects allocated= from @@ -214,13 +318,13 @@ fn drop(&mut self) { /// Trait for types that can be initialized in a slab cache. /// /// This trait provides the initialization logic for objects allocated fro= m a -/// [`KMemCache`]. When the slab allocator creates new objects, it invokes= the -/// constructor to ensure objects are in a valid initial state. +/// [`KMemCache`]. The initializer is called when objects are allocated fr= om a +/// sheaf via [`Sheaf::alloc`]. /// /// # Implementation /// -/// Implementors must provide [`init`](KMemCacheInit::init), which returns -/// a in-place initializer for the type. +/// Implementors must provide [`init`](KMemCacheInit::init), which returns= an +/// infallible initializer for the type. /// /// # Example /// @@ -251,6 +355,28 @@ pub trait KMemCacheInit { fn init() -> impl Init; } =20 +/// Marker type for sheaves from static caches. +/// +/// Used as a type parameter for [`Sheaf`] to indicate the sheaf was creat= ed +/// from a `&'static KMemCache`. +pub enum Static {} + +/// Marker type for sheaves from dynamic caches. +/// +/// Used as a type parameter for [`Sheaf`] to indicate the sheaf was creat= ed +/// from a [`KMemCacheHandle`] via [`ArcBorrow`]. +pub enum Dynamic {} + +/// A sheaf from a static cache. +/// +/// This is a [`Sheaf`] backed by a `&'static KMemCache`. +pub type StaticSheaf<'a, T> =3D Sheaf<'a, T, Static>; + +/// A sheaf from a dynamic cache. +/// +/// This is a [`Sheaf`] backed by a reference-counted [`KMemCacheHandle`]. +pub type DynamicSheaf<'a, T> =3D Sheaf<'a, T, Dynamic>; + /// A pre-filled container of slab objects. /// /// A sheaf holds a set of pre-allocated objects from a [`KMemCache`]. @@ -261,12 +387,23 @@ pub trait KMemCacheInit { /// Sheaves provide faster allocation than direct allocation because they = use /// local locks with preemption disabled rather than atomic operations. /// +/// # Type parameters +/// +/// - `'a`: The lifetime of the cache reference. +/// - `T`: The type of objects in this sheaf. +/// - `A`: Either [`Static`] or [`Dynamic`], indicating whether the backing +/// cache is a static reference or a reference-counted handle. +/// +/// For convenience, [`StaticSheaf`] and [`DynamicSheaf`] type aliases are +/// provided. +/// /// # Lifecycle /// -/// Sheaves are created via [`KMemCache::sheaf`] and should be returned to= the -/// allocator when no longer needed via [`Sheaf::return_refill`]. If a she= af is -/// simply dropped, it is returned with `GFP_NOWAIT` flags, which may resu= lt in -/// the sheaf being flushed and freed rather than being cached for reuse. +/// Sheaves are created via [`KMemCache::sheaf`] or [`KMemCacheHandle::she= af`] +/// and should be returned to the allocator when no longer needed via +/// [`Sheaf::return_refill`]. If a sheaf is simply dropped, it is returned= with +/// `GFP_NOWAIT` flags, which may result in the sheaf being flushed and fr= eed +/// rather than being cached for reuse. /// /// # Invariants /// @@ -274,13 +411,14 @@ pub trait KMemCacheInit { /// `kmem_cache_prefill_sheaf`. /// - `cache` is the cache from which this sheaf was created. /// - `dropped` tracks whether the sheaf has been explicitly returned. -pub struct Sheaf> { +pub struct Sheaf<'a, T: KMemCacheInit, A> { sheaf: NonNull, - cache: Arc>, + cache: CacheRef, dropped: bool, + _p: PhantomData<(&'a KMemCache, A)>, } =20 -impl> Sheaf { +impl<'a, T: KMemCacheInit, A> Sheaf<'a, T, A> { fn as_raw(&self) -> *mut bindings::slab_sheaf { self.sheaf.as_ptr() } @@ -303,6 +441,75 @@ pub fn return_refill(mut self, flags: kernel::alloc::F= lags) { drop(self); } =20 + /// Refills the sheaf to at least the specified size. + /// + /// Replenishes the sheaf by preallocating objects until it contains at + /// least `size` objects. If the sheaf already contains `size` or more + /// objects, this is a no-op. In practice, the sheaf is refilled to its + /// full capacity. + /// + /// # Arguments + /// + /// - `flags`: Allocation flags controlling how memory is obtained. + /// - `size`: The minimum number of objects the sheaf should contain a= fter + /// refilling. If `size` exceeds the cache's `sheaf_capacity`, the s= heaf + /// may be replaced with a larger one. + /// + /// # Errors + /// + /// Returns an error if the objects could not be allocated. If refilli= ng + /// fails, the existing sheaf is left intact. + pub fn refill(&mut self, flags: kernel::alloc::Flags, size: usize) -> = Result { + // SAFETY: `self.cache.as_raw()` returns a valid cache pointer and + // `&raw mut self.sheaf` points to a valid sheaf per the type inva= riants. + kernel::error::to_result(unsafe { + bindings::kmem_cache_refill_sheaf( + self.cache.as_raw(), + flags.as_raw(), + (&raw mut (self.sheaf)).cast(), + size.try_into()?, + ) + }) + } +} + +impl<'a, T: KMemCacheInit> Sheaf<'a, T, Static> { + /// Allocates an object from the sheaf. + /// + /// Returns a new [`SBox`] containing an initialized object, or [`None= `] + /// if the sheaf is depleted. Allocations are guaranteed to succeed as + /// long as the sheaf contains pre-allocated objects. + /// + /// The `gfp` flags passed to `kmem_cache_alloc_from_sheaf` are set to= zero, + /// meaning no additional flags like `__GFP_ZERO` or `__GFP_ACCOUNT` a= re + /// applied. + /// + /// The returned `T` is initialized as part of this function. + pub fn alloc(&mut self) -> Option> { + // SAFETY: `self.cache.as_raw()` and `self.as_raw()` return valid + // pointers. The function returns NULL when the sheaf is empty. + let ptr =3D unsafe { + bindings::kmem_cache_alloc_from_sheaf_noprof(self.cache.as_raw= (), 0, self.as_raw()) + }; + + // SAFETY: + // - `ptr` is a valid pointer as it was just returned by the cache. + // - The initializer is infallible, so an error is never returned. + unsafe { T::init().__init(ptr.cast()) }.expect("Initializer is inf= allible"); + + let ptr =3D NonNull::new(ptr.cast::())?; + + // INVARIANT: `ptr` was returned by `kmem_cache_alloc_from_sheaf_n= oprof` + // and initialized above. `cache` is the cache from which this obj= ect + // was allocated. The object remains valid until freed in `Drop`. + Some(SBox { + ptr, + cache: self.cache.clone(), + }) + } +} + +impl<'a, T: KMemCacheInit> Sheaf<'a, T, Dynamic> { /// Allocates an object from the sheaf. /// /// Returns a new [`SBox`] containing an initialized object, or [`None= `] @@ -338,7 +545,7 @@ pub fn alloc(&mut self) -> Option> { } } =20 -impl> Drop for Sheaf { +impl<'a, T: KMemCacheInit, A> Drop for Sheaf<'a, T, A> { fn drop(&mut self) { if !self.dropped { // SAFETY: `self.cache.as_raw()` and `self.as_raw()` return va= lid @@ -355,6 +562,39 @@ fn drop(&mut self) { } } =20 +/// Internal reference to a cache, either static or reference-counted. +/// +/// # Invariants +/// +/// - For `CacheRef::Static`: the `NonNull` points to a valid `KMemCache` +/// with `'static` lifetime, derived from a `&'static KMemCache` refe= rence. +enum CacheRef> { + /// A reference-counted handle to a dynamically created cache. + Arc(Arc>), + /// A pointer to a static lifetime cache. + Static(NonNull>), +} + +impl> Clone for CacheRef { + fn clone(&self) -> Self { + match self { + Self::Arc(arg0) =3D> Self::Arc(arg0.clone()), + Self::Static(arg0) =3D> Self::Static(*arg0), + } + } +} + +impl> CacheRef { + fn as_raw(&self) -> *mut bindings::kmem_cache { + match self { + CacheRef::Arc(handle) =3D> handle.as_raw(), + // SAFETY: By type invariant, `ptr` points to a valid `KMemCac= he` + // with `'static` lifetime. + CacheRef::Static(ptr) =3D> unsafe { ptr.as_ref() }.as_raw(), + } + } +} + /// An owned allocation from a cache sheaf. /// /// `SBox` is similar to `Box` but is backed by a slab cache allocation ob= tained @@ -371,7 +611,50 @@ fn drop(&mut self) { /// - The object remains valid for the lifetime of the `SBox`. pub struct SBox> { ptr: NonNull, - cache: Arc>, + cache: CacheRef, +} + +impl> SBox { + /// Consumes the `SBox` and returns the raw pointer to the contained v= alue. + /// + /// The caller becomes responsible for freeing the memory. The object = is not + /// dropped and remains initialized. Use [`static_from_ptr`] to recons= truct + /// an `SBox` from the pointer. + /// + /// [`static_from_ptr`]: SBox::static_from_ptr + pub fn into_ptr(self) -> *mut T { + let ptr =3D self.ptr.as_ptr(); + core::mem::forget(self); + ptr + } + + /// Reconstructs an `SBox` from a raw pointer and cache. + /// + /// This is intended for use with objects that were previously convert= ed to + /// raw pointers via [`into_ptr`], typically for passing through C cod= e. + /// + /// [`into_ptr`]: SBox::into_ptr + /// + /// # Safety + /// + /// - `cache` must be a valid pointer to the `kmem_cache` from which `= value` + /// was allocated. + /// - `value` must be a valid pointer to an initialized `T` that was + /// allocated from `cache`. + /// - The caller must ensure that no other `SBox` or reference exists = for + /// `value`. + pub unsafe fn static_from_ptr(cache: *mut bindings::kmem_cache, value:= *mut T) -> Self { + // INVARIANT: The caller guarantees `value` points to a valid, + // initialized `T` allocated from `cache`. + Self { + // SAFETY: By function safety requirements, `value` is not nul= l. + ptr: unsafe { NonNull::new_unchecked(value) }, + cache: CacheRef::Static( + // SAFETY: By function safety requirements, `cache` is not= null. + unsafe { NonNull::new_unchecked(cache.cast()) }, + ), + } + } } =20 impl> Deref for SBox { --=20 2.51.2 From nobody Sat Feb 7 23:24:09 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CB82E3112D2; Fri, 6 Feb 2026 21:12:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770412321; cv=none; b=XoPUoMu2hnPb+rhuXWF5YU64N269SoRuowyF6/1THhH9urz0yVu5zoMn2opnqhc2BG9rXv5Nqb7tGI3szxvs4sB9djswNufNZJpElds1HnaVDAQqjVc4x1YEiBOUGl62dFCxpTK8SrIg/0OFN2+HarKRKBQC5yuO4DBtGAQKvYo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770412321; c=relaxed/simple; bh=AC4NCWQlfHS+3umvfHK9M7bjjDtFlC6otCKn7IFTKu4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=pbovNuCklYMXKk9BLqN9DOm64sCeU0yom93IkOxzkF0WGHYnnndYpthGU+Uttr1KJb/Zr6mGKBahEx0SMUMrpMCp2J+DiunxmgF4F6SX7fL5TjnhxOCtqbQrVKiG+iMfLnLYywJ0Vv3suPf42lQQSvMNOKYQV2YdoewHRc1QoTI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WpmicoAy; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="WpmicoAy" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BC971C116C6; Fri, 6 Feb 2026 21:11:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770412321; bh=AC4NCWQlfHS+3umvfHK9M7bjjDtFlC6otCKn7IFTKu4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=WpmicoAyOeKMjQbPko5tQxJ5PHYWJdWZJSDHlu48rE5FmgmWXT81IpsJEFiBduXjR k47q8v+tUvVKHT5cdzemTzV+S3KVj1b4tF+HLmD2jKGhRzdleSxb9EouITUPIwa7fo 15LtLv4b16pnt9D0xW0CC54xNJQM4zvmwr1Z2jmgbLESlDX2YMygCBDMcsgO59cfd+ icUi1evSQxeUK0y58ugkXLBlj5ouIBtTKB75yHZVkuyxFt8mNA9sRItrHbWlwXNmj8 y32X0YnKfXQdAcRJrr6yYPWCnZ5g2Z4Ji7gR6jUtD9VQ/uQSGCZksim2tZg72idMjd KRBu7O2cG7RwQ== From: Andreas Hindborg Date: Fri, 06 Feb 2026 22:10:57 +0100 Subject: [PATCH v2 11/11] rust: xarray: add preload API Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260206-xarray-entry-send-v2-11-91c41673fd30@kernel.org> References: <20260206-xarray-entry-send-v2-0-91c41673fd30@kernel.org> In-Reply-To: <20260206-xarray-entry-send-v2-0-91c41673fd30@kernel.org> To: Tamir Duberstein , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Andrew Morton , Christoph Lameter , David Rientjes , Roman Gushchin , Harry Yoo Cc: Daniel Gomez , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andreas Hindborg , "Matthew Wilcox (Oracle)" X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=17136; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=AC4NCWQlfHS+3umvfHK9M7bjjDtFlC6otCKn7IFTKu4=; b=owEBbQKS/ZANAwAKAeG4Gj55KGN3AcsmYgBphljqWbK6JVOB8Dx84iCFFrCbId0d2R0MkNmZS 4n6fEjxBVGJAjMEAAEKAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaYZY6gAKCRDhuBo+eShj d9HUD/9sT3Dt5h9Ne4yUU3D2G/PVRBEHLzC3kwVweot4CzkGrdTlPPlxfZfiQGCGgcrtxkIyg6Q 0vqvdlLRsjiCDOE5DljzjyyaI0LdfMODOhKFZRcdowmquevCX+T1+yL2icu2LxsoT0/d6LiUEiD AKu5dRn/05+l9kMtsT6fVqw/tzSV3a/OjGfw64JXDCGX2byE+fnJNw9jrtRB41de4D36DKpVxtX 3g1RUDpdpmEAPQa+6VqZy5Yj8qywIM8kdJjcRe1UEh6ICq0SJhqY406WMYjumk55TBff9Fs37eT Q5yotLmTBv03wRfqN9nc5lzmKijn3LHHTvr1NCOYfQEe8myKlzexkvdSa1V89rnx9ZUoKu7lRIh Is1pSkdLR7UKknSebRu0r8WqyscEABZUt0Y8vAp3wUsjSMUMM2qfJirwGyxKmLAPm7S0QMwG1V6 SXkmIwUXAPtiovVUVLH8/TFHARkXVyBAjqlyHAQj+ygsb/T8wYd14GctMLciUxlwMde0Qd452JU ITvK/JufwLAfBHfbKDO8BihSIf2zwF3KhMfeHSZl+ExSGzKuzKdEP5r/w+3esOe2yhwO1ttxSWH +++hQ5XmwM8CKBvWzU92x9SX31/5DbAog4IVN3Hc6saHTgAn10gN6ritFtxa53nxKeIbDOcrcXM FeLsi3EUXVQdsmQ== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add a preload API that allows preallocating memory for XArray insertions. This enables insertions to proceed without allocation failures in contexts where memory allocation is not desirable, such as in atomic contexts or where reliability is critical. The API includes: - `XArrayPreloadBuffer` for managing a pool of preallocated nodes. - `XArrayPreloadNode` representing a single preallocated node. - Integration with the entry API, allowing `VacantEntry::insert` and `VacantEntry::insert_entry` to accept an optional preload buffer. - A new `Guard::insert_entry` method for inserting with preload support. The implementation uses a circular buffer to efficiently manage preallocated nodes. When an insertion would fail due to ENOMEM, the XArray state API automatically consumes a preallocated node from the buffer if available. Export `radix_tree_node_ctor` from C to enable Rust code to work with the radix tree node cache. Cc: "Liam R. Howlett" Cc: "Matthew Wilcox (Oracle)" Signed-off-by: Andreas Hindborg --- include/linux/radix-tree.h | 3 + lib/radix-tree.c | 5 +- rust/bindings/bindings_helper.h | 3 + rust/kernel/xarray.rs | 172 +++++++++++++++++++++++++++++++++++-= ---- rust/kernel/xarray/entry.rs | 29 ++++--- rust/kernel/xarray/preload.rs | 3 + 6 files changed, 185 insertions(+), 30 deletions(-) diff --git a/include/linux/radix-tree.h b/include/linux/radix-tree.h index eae67015ce51a..c3699f12b070c 100644 --- a/include/linux/radix-tree.h +++ b/include/linux/radix-tree.h @@ -469,4 +469,7 @@ static __always_inline void __rcu **radix_tree_next_slo= t(void __rcu **slot, slot =3D radix_tree_next_slot(slot, iter, \ RADIX_TREE_ITER_TAGGED | tag)) =20 + +void radix_tree_node_ctor(void *arg); + #endif /* _LINUX_RADIX_TREE_H */ diff --git a/lib/radix-tree.c b/lib/radix-tree.c index 976b9bd02a1b5..b642f2775e89c 100644 --- a/lib/radix-tree.c +++ b/lib/radix-tree.c @@ -33,6 +33,7 @@ * Radix tree node cache. */ struct kmem_cache *radix_tree_node_cachep; +EXPORT_SYMBOL(radix_tree_node_cachep); =20 /* * The radix tree is variable-height, so an insert operation not only has @@ -1566,14 +1567,14 @@ void idr_destroy(struct idr *idr) } EXPORT_SYMBOL(idr_destroy); =20 -static void -radix_tree_node_ctor(void *arg) +void radix_tree_node_ctor(void *arg) { struct radix_tree_node *node =3D arg; =20 memset(node, 0, sizeof(*node)); INIT_LIST_HEAD(&node->private_list); } +EXPORT_SYMBOL(radix_tree_node_ctor); =20 static int radix_tree_cpu_dead(unsigned int cpu) { diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helpe= r.h index 58605c32e8102..652f08ad888cd 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -118,6 +118,9 @@ const xa_mark_t RUST_CONST_HELPER_XA_PRESENT =3D XA_PRE= SENT; const gfp_t RUST_CONST_HELPER_XA_FLAGS_ALLOC =3D XA_FLAGS_ALLOC; const gfp_t RUST_CONST_HELPER_XA_FLAGS_ALLOC1 =3D XA_FLAGS_ALLOC1; const size_t RUST_CONST_HELPER_XAS_RESTART =3D (size_t)XAS_RESTART; +const size_t RUST_CONST_HELPER_XA_CHUNK_SHIFT =3D XA_CHUNK_SHIFT; +const size_t RUST_CONST_HELPER_XA_CHUNK_SIZE =3D XA_CHUNK_SIZE; +extern struct kmem_cache *radix_tree_node_cachep; =20 const vm_flags_t RUST_CONST_HELPER_VM_MERGEABLE =3D VM_MERGEABLE; const vm_flags_t RUST_CONST_HELPER_VM_READ =3D VM_READ; diff --git a/rust/kernel/xarray.rs b/rust/kernel/xarray.rs index 8c10e8fd76f15..89bf531308c88 100644 --- a/rust/kernel/xarray.rs +++ b/rust/kernel/xarray.rs @@ -5,6 +5,7 @@ //! C header: [`include/linux/xarray.h`](srctree/include/linux/xarray.h) =20 use core::{ + convert::Infallible, iter, marker::PhantomData, pin::Pin, @@ -23,11 +24,17 @@ bindings, build_assert, // error::{ + code::*, to_result, Error, Result, // }, ffi::c_void, + mm::sheaf::{ + KMemCache, + SBox, + StaticSheaf, // + }, types::{ ForeignOwnable, NotThreadSafe, @@ -35,12 +42,54 @@ }, }; use pin_init::{ + init, pin_data, pin_init, pinned_drop, + Init, PinInit, // }; =20 +/// Sheaf of preallocated [`XArray`] nodes. +pub type XArraySheaf<'a> =3D StaticSheaf<'a, XArrayNode>; + +/// Returns a reference to the global XArray node cache. +/// +/// This provides access to the kernel's `radix_tree_node_cachep`, which i= s the +/// slab cache used for allocating internal XArray nodes. This cache can b= e used +/// to create sheaves for preallocating XArray nodes. +pub fn xarray_kmem_cache() -> &'static KMemCache { + // SAFETY: `radix_tree_node_cachep` is a valid, statically initialized + // kmem_cache that remains valid for the lifetime of the kernel. The c= ache + // is configured for `xa_node` objects which match our `XArrayNode` ty= pe. + unsafe { KMemCache::from_raw(bindings::radix_tree_node_cachep) } +} + +/// An preallocated XArray node. +/// +/// This represents a single preallocated internal node for an XArray. +pub struct XArrayNode { + node: Opaque, +} + +impl kernel::mm::sheaf::KMemCacheInit for XArrayNode { + fn init() -> impl Init { + init!(Self { + // SAFETY: + // - This initialization cannot fail and will never return `Er= r`. + // - The xa_node does not move during initalization. + node <- unsafe { + pin_init::init_from_closure( + |place: *mut Opaque| -> Result<(), = Infallible> { + bindings::radix_tree_node_ctor(place.cast::()); + Ok(()) + }, + ) + } + }) + } +} + /// An array which efficiently maps sparse integer indices to owned object= s. /// /// This is similar to a [`crate::alloc::kvec::Vec>`], but more = efficient when there are @@ -137,15 +186,22 @@ fn iter(&self) -> impl Iterator> + '_ { let mut index =3D 0; =20 // SAFETY: `self.xa` is always valid by the type invariant. - iter::once(unsafe { - bindings::xa_find(self.xa.get(), &mut index, usize::MAX, bindi= ngs::XA_PRESENT) - }) - .chain(iter::from_fn(move || { - // SAFETY: `self.xa` is always valid by the type invariant. - Some(unsafe { - bindings::xa_find_after(self.xa.get(), &mut index, usize::= MAX, bindings::XA_PRESENT) - }) - })) + Iterator::chain( + iter::once(unsafe { + bindings::xa_find(self.xa.get(), &mut index, usize::MAX, b= indings::XA_PRESENT) + }), + iter::from_fn(move || { + // SAFETY: `self.xa` is always valid by the type invariant. + Some(unsafe { + bindings::xa_find_after( + self.xa.get(), + &mut index, + usize::MAX, + bindings::XA_PRESENT, + ) + }) + }), + ) .map_while(|ptr| NonNull::new(ptr.cast())) } =20 @@ -166,7 +222,6 @@ pub fn try_lock(&self) -> Option> { pub fn lock(&self) -> Guard<'_, T> { // SAFETY: `self.xa` is always valid by the type invariant. unsafe { bindings::xa_lock(self.xa.get()) }; - Guard { xa: self, _not_send: NotThreadSafe, @@ -270,7 +325,7 @@ pub fn get_mut(&mut self, index: usize) -> Option> { /// /// match guard.entry(42) { /// Entry::Vacant(entry) =3D> { - /// entry.insert(KBox::new(0x1337u32, GFP_KERNEL)?)?; + /// entry.insert(KBox::new(0x1337u32, GFP_KERNEL)?, None)?; /// } /// Entry::Occupied(_) =3D> unreachable!("We did not insert an ent= ry yet"), /// } @@ -475,6 +530,45 @@ pub fn store( Ok(unsafe { T::try_from_foreign(old) }) } } + + /// Inserts a value and returns an occupied entry for further operatio= ns. + /// + /// If a value is already present, the operation fails. + /// + /// This method will not drop the XArray lock. If memory allocation is + /// required for the operation to succeed, the user should supply memo= ry + /// through the `preload` argument. + /// + /// # Examples + /// + /// ``` + /// # use kernel::{prelude::*, xarray::{AllocKind, XArray}}; + /// let mut xa =3D KBox::pin_init(XArray::>::new(AllocKind::= Alloc), GFP_KERNEL)?; + /// let mut guard =3D xa.lock(); + /// + /// assert_eq!(guard.get(42), None); + /// + /// let value =3D KBox::new(0x1337u32, GFP_KERNEL)?; + /// let entry =3D guard.insert_entry(42, value, None)?; + /// let borrowed =3D entry.into_mut(); + /// assert_eq!(borrowed, &0x1337); + /// + /// # Ok::<(), kernel::error::Error>(()) + /// ``` + pub fn insert_entry<'b>( + &'b mut self, + index: usize, + value: T, + preload: Option<&mut XArraySheaf<'_>>, + ) -> Result, StoreError> { + match self.entry(index) { + Entry::Vacant(entry) =3D> entry.insert_entry(value, preload), + Entry::Occupied(_) =3D> Err(StoreError { + error: EBUSY, + value, + }), + } + } } =20 /// Internal state for XArray iteration and entry operations. @@ -489,6 +583,25 @@ pub(crate) struct XArrayState<'a, 'b, T: ForeignOwnabl= e> { state: bindings::xa_state, } =20 +impl<'a, 'b, T: ForeignOwnable> Drop for XArrayState<'a, 'b, T> { + fn drop(&mut self) { + if !self.state.xa_alloc.is_null() { + // SAFETY: + // - `xa_alloc` is only set via `SBox::into_ptr()` in `insert(= )` where + // the node comes from an `XArraySheaf` backed by `radix_tre= e_node_cachep`. + // - `xa_alloc` points to a valid, initialized `XArrayNode`. + // - `XArrayState` has exclusive ownership of `xa_alloc`, and = no other + // `SBox` or reference exists for this value. + drop(unsafe { + SBox::::static_from_ptr( + bindings::radix_tree_node_cachep, + self.state.xa_alloc.cast(), + ) + }) + } + } +} + impl<'a, 'b, T: ForeignOwnable> XArrayState<'a, 'b, T> { fn new(access: &'b Guard<'a, T>, index: usize) -> Self { let ptr =3D access.xa.xa.get(); @@ -529,16 +642,37 @@ fn status(&self) -> Result { to_result(unsafe { bindings::xas_error(&self.state) }) } =20 - fn insert(&mut self, value: T) -> Result<*mut c_void, StoreError> { + fn insert( + &mut self, + value: T, + mut preload: Option<&mut XArraySheaf<'_>>, + ) -> Result<*mut c_void, StoreError> { let new =3D T::into_foreign(value).cast(); =20 - // SAFETY: `self.state.state` is properly initialized and `new` ca= me from `T::into_foreign`. - // We hold the xarray lock. - unsafe { bindings::xas_store(&mut self.state, new) }; - - self.status().map(|()| new).map_err(|error| { - // SAFETY: `new` came from `T::into_foreign` and `xas_store` d= oes not take ownership of - // the value on error. + loop { + // SAFETY: `self.state` is properly initialized and `new` came= from + // `T::into_foreign`. We hold the xarray lock. + unsafe { bindings::xas_store(&mut self.state, new) }; + + match self.status() { + Ok(()) =3D> break Ok(new), + Err(ENOMEM) =3D> { + debug_assert!(self.state.xa_alloc.is_null()); + let node =3D match preload.as_mut().map(|sheaf| sheaf.= alloc().ok_or(ENOMEM)) { + None =3D> break Err(ENOMEM), + Some(Err(e)) =3D> break Err(e), + Some(Ok(node)) =3D> node, + }; + + self.state.xa_alloc =3D node.into_ptr().cast(); + continue; + } + Err(e) =3D> break Err(e), + } + } + .map_err(|error| { + // SAFETY: `new` came from `T::into_foreign` and `xas_store` d= oes not take + // ownership of the value on error. let value =3D unsafe { T::from_foreign(new) }; StoreError { value, error } }) diff --git a/rust/kernel/xarray/entry.rs b/rust/kernel/xarray/entry.rs index 1b1c21bed7022..ff500be3832b7 100644 --- a/rust/kernel/xarray/entry.rs +++ b/rust/kernel/xarray/entry.rs @@ -3,6 +3,7 @@ use super::{ Guard, StoreError, + XArraySheaf, XArrayState, // }; use core::ptr::NonNull; @@ -29,9 +30,9 @@ impl Entry<'_, '_, T> { /// let mut xa =3D KBox::pin_init(XArray::>::new(AllocKind::= Alloc), GFP_KERNEL)?; /// let mut guard =3D xa.lock(); /// - /// /// let entry =3D guard.entry(42); /// assert_eq!(entry.is_occupied(), false); + /// drop(entry); /// /// guard.store(42, KBox::new(0x1337u32, GFP_KERNEL)?, GFP_KERNEL)?; /// let entry =3D guard.entry(42); @@ -64,7 +65,8 @@ pub(crate) fn new(guard: &'b mut Guard<'a, T>, index: usi= ze) -> Self { /// Returns a reference to the newly inserted value. /// /// - This method will fail if the nodes on the path to the index - /// represented by this entry are not present in the XArray. + /// represented by this entry are not present in the XArray and no m= emory + /// is available via the `preload` argument. /// - This method will not drop the XArray lock. /// /// @@ -79,7 +81,7 @@ pub(crate) fn new(guard: &'b mut Guard<'a, T>, index: usi= ze) -> Self { /// /// if let Entry::Vacant(entry) =3D guard.entry(42) { /// let value =3D KBox::new(0x1337u32, GFP_KERNEL)?; - /// let borrowed =3D entry.insert(value)?; + /// let borrowed =3D entry.insert(value, None)?; /// assert_eq!(*borrowed, 0x1337); /// } /// @@ -87,8 +89,12 @@ pub(crate) fn new(guard: &'b mut Guard<'a, T>, index: us= ize) -> Self { /// /// # Ok::<(), kernel::error::Error>(()) /// ``` - pub fn insert(mut self, value: T) -> Result, StoreE= rror> { - let new =3D self.state.insert(value)?; + pub fn insert( + mut self, + value: T, + preload: Option<&mut XArraySheaf<'_>>, + ) -> Result, StoreError> { + let new =3D self.state.insert(value, preload)?; =20 // SAFETY: `new` came from `T::into_foreign`. The entry has exclus= ive // ownership of `new` as it holds a mutable reference to `Guard`. @@ -98,7 +104,8 @@ pub fn insert(mut self, value: T) -> Result, StoreError> { /// Inserts a value and returns an occupied entry representing the new= ly inserted value. /// /// - This method will fail if the nodes on the path to the index - /// represented by this entry are not present in the XArray. + /// represented by this entry are not present in the XArray and no m= emory + /// is available via the `preload` argument. /// - This method will not drop the XArray lock. /// /// # Examples @@ -112,7 +119,7 @@ pub fn insert(mut self, value: T) -> Result, StoreError> { /// /// if let Entry::Vacant(entry) =3D guard.entry(42) { /// let value =3D KBox::new(0x1337u32, GFP_KERNEL)?; - /// let occupied =3D entry.insert_entry(value)?; + /// let occupied =3D entry.insert_entry(value, None)?; /// assert_eq!(occupied.index(), 42); /// } /// @@ -120,8 +127,12 @@ pub fn insert(mut self, value: T) -> Result, StoreError> { /// /// # Ok::<(), kernel::error::Error>(()) /// ``` - pub fn insert_entry(mut self, value: T) -> Result, StoreError> { - let new =3D self.state.insert(value)?; + pub fn insert_entry( + mut self, + value: T, + preload: Option<&mut XArraySheaf<'_>>, + ) -> Result, StoreError> { + let new =3D self.state.insert(value, preload)?; =20 Ok(OccupiedEntry::<'a, 'b, T> { state: self.state, diff --git a/rust/kernel/xarray/preload.rs b/rust/kernel/xarray/preload.rs new file mode 100644 index 0000000000000..745709579a265 --- /dev/null +++ b/rust/kernel/xarray/preload.rs @@ -0,0 +1,3 @@ +// SPDX-License-Identifier: GPL-2.0 + +use kernel::prelude::*; --=20 2.51.2