From nobody Sun Oct 5 07:24:33 2025 Received: from mailrelay-egress16.pub.mailoutpod3-cph3.one.com (mailrelay-egress16.pub.mailoutpod3-cph3.one.com [46.30.212.3]) (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 6E9EB239E7F for ; Thu, 7 Aug 2025 12:10:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=46.30.212.3 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754568626; cv=none; b=IvVhhK6Or1Yb3371DFoNsou4Onr4/PJYDDRwyxM9OV2ObNVIkZb8YJqImwHMNgc4Dq9OT5q+Gmft2BrABGJyqVm267LrH0ZjTP1olTd4dpn7M+4ruzQqpLSlzB2ZkU3GI9HE3psZ8TxztmIb8Td9aRz0bqRjaDby9Fw1eg2dnto= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754568626; c=relaxed/simple; bh=bO8dcPP/dxchhlZwTJ+ksGxVRxEV4n3Jm2FmNev4q2E=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=kYer48kU1q1gwJmvTg7h8bHco9FdLB4FciBA1hXjml79BTqN45d8I8KESzuP5NQwCEZJMHoal5a4fQABCpDtyw/4ChjeEnwSX48YbH2UEb3ln9ne4FywoyxTLefSv3wmdljnJoyV8pYUeX550j4BATX6nuHCLvGQjHVDzDEf94k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=konsulko.se; spf=none smtp.mailfrom=konsulko.se; dkim=pass (2048-bit key) header.d=konsulko.se header.i=@konsulko.se header.b=EFIaPnUu; dkim=permerror (0-bit key) header.d=konsulko.se header.i=@konsulko.se header.b=5eg+fR6M; arc=none smtp.client-ip=46.30.212.3 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=konsulko.se Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=konsulko.se Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=konsulko.se header.i=@konsulko.se header.b="EFIaPnUu"; dkim=permerror (0-bit key) header.d=konsulko.se header.i=@konsulko.se header.b="5eg+fR6M" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1754568621; x=1755173421; d=konsulko.se; s=rsa1; h=content-transfer-encoding:mime-version:message-id:date:subject:cc:to:from: from; bh=Q52bbfTzkfCRNzr97Nue/VaOOC2qoR/9F6VBSjElX9Y=; b=EFIaPnUuUmgLgR7m5p098Z8JsSBM3NAh8fuj/ZKhsTrnly0XTjXO1sTvIlxpB1d0YjnTfx20jEU0u oBdHAtQyV4We2iaGDR4vAF41hNSTifC11zbyKJpT4utxZ6sF4G1EefTHE8bVwR9wUy/1yKpNLMfywa 8Y3kqgw7WOZnwlHn4kGDCZ9PeLJNYB4CkXY9gBC5tcviEkooJDtx2s2rTn6Z7K+AjJ3djR81odCLPR YoO1ijZtf5M7aOCNviAUkZ0KO6vk/PzTyd9Y/tlbww8jQmLcnBNmvqLbL3gN1BAM3Dm9M4D2szLOeA mxokO6DGTsh2arm1BeEbHrGJzNvoqUw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1754568621; x=1755173421; d=konsulko.se; s=ed1; h=content-transfer-encoding:mime-version:message-id:date:subject:cc:to:from: from; bh=Q52bbfTzkfCRNzr97Nue/VaOOC2qoR/9F6VBSjElX9Y=; b=5eg+fR6MTuRxb8YPyHtiF65WbTnnLGJR6G5WKX32WRmCaZfbM3wi/krwvJ3X6nClGPonaOwsY+EK+ NOYXjpODA== X-HalOne-ID: 7ce91011-7387-11f0-b24c-d510462faafc Received: from slottsdator.home (host-95-203-16-218.mobileonline.telia.com [95.203.16.218]) by mailrelay4.pub.mailoutpod3-cph3.one.com (Halon) with ESMTPSA id 7ce91011-7387-11f0-b24c-d510462faafc; Thu, 07 Aug 2025 12:10:20 +0000 (UTC) From: Vitaly Wool To: rust-for-linux@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Uladzislau Rezki , Danilo Krummrich , Alice Ryhl , Vlastimil Babka , Lorenzo Stoakes , "Liam R . Howlett" , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , Bjorn Roy Baron , Benno Lossin , Andreas Hindborg , Trevor Gross , Vitaly Wool Subject: [PATCH] rust: extend kbox with a new constructor Date: Thu, 7 Aug 2025 14:10:11 +0200 Message-Id: <20250807121011.2317762-1-vitaly.wool@konsulko.se> X-Mailer: git-send-email 2.39.2 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Alice Ryhl Add a new constructor to KBox to facilitate KBox creation from a pinned slice of elements. This allows to efficiently allocate memory for e.g. arrays of structrures containing spinlocks or mutexes. Signed-off-by: Alice Ryhl Signed-off-by: Vitaly Wool --- rust/kernel/alloc/kbox.rs | 51 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/rust/kernel/alloc/kbox.rs b/rust/kernel/alloc/kbox.rs index 1fef9beb57c8..74877afab0a3 100644 --- a/rust/kernel/alloc/kbox.rs +++ b/rust/kernel/alloc/kbox.rs @@ -290,6 +290,57 @@ pub fn pin(x: T, flags: Flags) -> Result= >, AllocError> Ok(Self::new(x, flags)?.into()) } =20 + /// Construct a pinned slice of elements `Pin>`. This is a= convenient means for + /// creation of e.g. arrays of structrures containing spinlocks or mut= exes. + /// + /// # Examples + /// + /// ``` + /// #[pin_data] + /// struct Example { + /// c: u32, + /// #[pin] + /// d: SpinLock, + /// } + /// + /// impl Example { + /// fn new() -> impl PinInit { + /// pin_init!(Self { + /// c: 10, + /// d <- new_spinlock!(Inner { a: 20, b: 30 }), + /// }) + /// } + /// } + /// // Allocate a boxed slice of 10 `Example`s. + /// let s =3D KBox::pin_slice( + /// | _i | Example::new(), + /// 10, + /// GFP_KERNEL + /// )?; + /// assert_eq!(s[5].c, 10); + /// assert_eq!(s[3].d.lock().a, 20), + /// ``` + pub fn pin_slice(mut init: F, len: usize, flags: Flags) -> Re= sult>, E> + where + F: FnMut(usize) -> I, + I: PinInit, + E: From, + { + let mut buffer =3D super::Vec::::with_capacity(len, flags)?; + for i in 0..len { + let ptr =3D buffer.spare_capacity_mut().as_mut_ptr().cast(); + // SAFETY: This address is available to be initialized, and it= will either be dropped + // on a future error or returned as a pinned location. + unsafe { init(i).__pinned_init(ptr)? }; + // SAFETY: We initialized one more value. + unsafe { buffer.inc_len(1) }; + } + let (ptr, _, _) =3D buffer.into_raw_parts(); + let slice =3D core::ptr::slice_from_raw_parts_mut(ptr, len); + // SAFETY: This memory holds a valid [T] allocated with the right = allocator. + Ok(Pin::from(unsafe { Box::from_raw(slice) })) + } + /// Convert a [`Box`] to a [`Pin>`]. If `T` does not imp= lement /// [`Unpin`], then `x` will be pinned in memory and can't be moved. pub fn into_pin(this: Self) -> Pin { --=20 2.39.2