From nobody Mon Oct 6 03:02:02 2025 Received: from out-183.mta1.migadu.com (out-183.mta1.migadu.com [95.215.58.183]) (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 4DD4A26B2D2 for ; Fri, 25 Jul 2025 07:03:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.183 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753427006; cv=none; b=r2ACWyPu2cCPZZckqDsnvnA0M7AFA5M3dndpJwSojcxRHHHF3OoCigjF4o7TgOEBbDyhiT9nS5JYhBw5zgsnstA2k8Gb65QxGWCqsRSJCE10ciISNaO8F0jjg2ow3mOnXxJsKO8DpKJpNsNaZTxJe0vd43oQBnPlLSHCD4/Ctj8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753427006; c=relaxed/simple; bh=2Gl2cahgaLVZIWDnYUk8nnvaJfE907iYPgS3oeXMn4w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=cdgBpB8tcSBL66ysXDetFymtVIHafHu/DqTY0PDW0mnxB5FD+6gDMWZi5JbwRVJ6p+UMf32Ich4dEzxLTl4H6+WsAO3aYpiM1AO/NO/1F8C7Urc5YO8RK0/9jiZxTebTySEH2Em2OpSVXbpRIEZBElS1X78SpuUYU35a/7BTlpE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=ToEzEuli; arc=none smtp.client-ip=95.215.58.183 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="ToEzEuli" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1753426964; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LOIbM2mlp9tLy2hxdmtCOBP2OarogA8hGYUa+KiCIXA=; b=ToEzEuliIBOYXCUIHJwC9N2o8l+dhi9t9IUeAftEN5wg1Ch8rO3fRFRck7zdxV2QcPQbO0 JnSRrRqvXtFzjAa3uDEmXtSdO3rb2OG84i0DjbLb0UtitzLXKPZTrFA8FZSkZN0W2+bz3V CaRyS4eC9kNpZKnLkoLjpaorcovZINQ= From: Hui Zhu To: Danilo Krummrich , Lorenzo Stoakes , Vlastimil Babka , "Liam R . Howlett" , Uladzislau Rezki , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , bjorn3_gh@protonmail.com, Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Hui Zhu , Geliang Tang Subject: [PATCH v5 1/2] rust: allocator: add KUnit tests for alignment guarantees Date: Fri, 25 Jul 2025 15:02:20 +0800 Message-Id: In-Reply-To: References: 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 X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" From: Hui Zhu Add comprehensive tests to verify correct alignment handling in Rust allocator wrappers. The tests validate: That kmalloc respects both standard (128-byte) and page-size (8192-byte) alignments when allocating structs with explicit alignment attributes. That vmalloc correctly handles standard alignments but intentionally rejects allocations requiring alignments larger than its capabilities. That kvmalloc mirrors vmalloc's constraints, accepting standard alignments but rejecting excessive alignment requirements. The test infrastructure uses specialized aligned structs (Blob and LargeAlignBlob) and a test harness (TestAlign) to validate pointer alignment through different allocation paths. This ensures our Rust allocators correctly propagate kernel allocation constraints. Co-developed-by: Geliang Tang Signed-off-by: Geliang Tang Signed-off-by: Hui Zhu Reviewed-by: Alice Ryhl --- rust/kernel/alloc/allocator.rs | 58 ++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs index aa2dfa9dca4c..bcc916240f11 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -187,3 +187,61 @@ unsafe fn realloc( unsafe { ReallocFunc::KVREALLOC.call(ptr, layout, old_layout, flag= s) } } } + +#[macros::kunit_tests(rust_allocator_kunit)] +mod tests { + use super::*; + use core::mem::MaybeUninit; + use kernel::prelude::*; + + const TEST_SIZE: usize =3D 1024; + const TEST_LARGE_ALIGN_SIZE: usize =3D kernel::page::PAGE_SIZE * 4; + + // These two structs are used to test allocating aligned memory. + // they don't need to be accessed, so they're marked as dead_code. + #[allow(dead_code)] + #[repr(align(128))] + struct Blob([u8; TEST_SIZE]); + #[allow(dead_code)] + #[repr(align(8192))] + struct LargeAlignBlob([u8; TEST_LARGE_ALIGN_SIZE]); + + struct TestAlign(Box, A>); + impl TestAlign { + fn new() -> Result { + Ok(Self(Box::<_, A>::new_uninit(GFP_KERNEL)?)) + } + + fn alignment_valid(&self, align: usize) -> bool { + assert!(align.is_power_of_two()); + + let addr =3D self.0.as_ptr() as usize; + if addr & (align - 1) !=3D 0 { + false + } else { + true + } + } + } + + #[test] + fn test_alignment() -> Result<()> { + let ta =3D TestAlign::::new()?; + assert!(ta.alignment_valid(128)); + + let ta =3D TestAlign::::new()?; + assert!(ta.alignment_valid(8192)); + + let ta =3D TestAlign::::new()?; + assert!(ta.alignment_valid(128)); + + assert!(TestAlign::::new().is_err()); + + let ta =3D TestAlign::::new()?; + assert!(ta.alignment_valid(128)); + + assert!(TestAlign::::new().is_err()); + + Ok(()) + } +} --=20 2.43.0 From nobody Mon Oct 6 03:02:02 2025 Received: from out-171.mta1.migadu.com (out-171.mta1.migadu.com [95.215.58.171]) (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 396F623D280 for ; Fri, 25 Jul 2025 07:02:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753426971; cv=none; b=JAXHlZlI6HTVi159o2t9Omx6RGXMnmmDnautwEMVsm3YSAeozGw9lWaAFEhjHuIZw+R45vxMFbRobOjVJ9YurkSAR2RVm94106m1YeGo3nHQMVYUEjvXtfHS2Wj7rVh39b6hGnF1GpDZpZhorZKG84hQpXNcWDaCy25iIh5ZNcQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753426971; c=relaxed/simple; bh=sVKJXmIHL8iu8lKvPknablaS4BAt4Ho9tkTmzj5mLSE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=VLPqMSIfX4YLf8h/sI6kbvjojzR89aBvA2TEI8n4FEAfR2lG7UPOpkJAIwulAK0NHkfkQCbiCBpjjpZ8GaiwU8o6n+yvSSJifd/t45zEfRNNc6nQU+9R+TyQHWdl3aQHE3+LzS+78NlleHy0PQAoch+4pnguntqXsyizXlC6Ndw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=fQQ8rI6/; arc=none smtp.client-ip=95.215.58.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="fQQ8rI6/" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1753426968; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pg6Pw/+iwYd83cgdfN4Bh0EwknHj0usqoSKEWVkMPoY=; b=fQQ8rI6/h3DIxATplBwlhcGSjV9hSBeW1rVy6wThi8I/APmugVzUapX0hFwP06RiuGLvDP NskkSYjbkEO9F2p9890wgqj8oVytKEQUQRBEGEwMuYZXWoqZvLXGb8TgUkvjSzAmw/zwjd nXm8z81AFLW4N932xMKhfSXJduR+gX0= From: Hui Zhu To: Danilo Krummrich , Lorenzo Stoakes , Vlastimil Babka , "Liam R . Howlett" , Uladzislau Rezki , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , bjorn3_gh@protonmail.com, Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Hui Zhu , Geliang Tang Subject: [PATCH v5 2/2] rust: alloc: kvec: add doc example for as_slice method Date: Fri, 25 Jul 2025 15:02:21 +0800 Message-Id: <7d5600d4913d62a011d23c917786ce947644b50b.1753423953.git.zhuhui@kylinos.cn> In-Reply-To: References: 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 X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" From: Hui Zhu Add a practical usage example to the documentation of KVec::as_slice() showing how to: Create a new KVec. Push elements into it. Convert to a slice via as_slice(). Co-developed-by: Geliang Tang Signed-off-by: Geliang Tang Signed-off-by: Hui Zhu Reviewed-by: Alice Ryhl --- rust/kernel/alloc/kvec.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs index 3c72e0bdddb8..fa04cc0987d6 100644 --- a/rust/kernel/alloc/kvec.rs +++ b/rust/kernel/alloc/kvec.rs @@ -224,6 +224,16 @@ unsafe fn dec_len(&mut self, count: usize) -> &mut [T]= { } =20 /// Returns a slice of the entire vector. + /// + /// # Examples + /// + /// ``` + /// let mut v =3D KVec::new(); + /// v.push(1, GFP_KERNEL)?; + /// v.push(2, GFP_KERNEL)?; + /// assert_eq!(v.as_slice(), &[1, 2]); + /// # Ok::<(), Error>(()) + /// ``` #[inline] pub fn as_slice(&self) -> &[T] { self --=20 2.43.0