From nobody Sun Oct 5 20:17:11 2025 Received: from out-179.mta1.migadu.com (out-179.mta1.migadu.com [95.215.58.179]) (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 2D2EA18DB27 for ; Wed, 30 Jul 2025 03:36:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753846618; cv=none; b=rcbDpO1ME7susmG81D1cInZT1qC2Sm/YlqlzpmSmCsxELKpO3ogUqbtXzbc898Tf3NHg2mxOekqMQswkbv7wCK/VEnXC+Iw0u82Ax/b6OYFfVvK+od5pwGKCOtwZ/gL4qA6kivPJCfWioMx1YFVg9/uDu7iLrmXLEPwrKfNa3Ag= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753846618; c=relaxed/simple; bh=HYywDqKiv34kOX7gRKWSI8RGs7zQkfd0xeZpQZkImRA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iC5cBmcUfoG/TZmERATa7WgCXm21dut2OkA5bBLp9JZ0QMrxKQeWJE9jSyW/mP5Dt2XxzIA77ddmdAv1YTi320LP1kzIzGnJr2UxGcoSOYDRIy3yTgQvO0HH7dcXj5/GnUjsVyC08KfXLWWdl6Un5Rw12XAud0uA+opQvkt0EBw= 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=YgsBfiaB; arc=none smtp.client-ip=95.215.58.179 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="YgsBfiaB" 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=1753846615; 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=TemiC/peMt9AXLXu/qa2eGX0iZwfkiigeEqsbhADwKA=; b=YgsBfiaBKgpkE99wNjaWSXsRrv1aVx4bmw+eksKCziJw61zY1Ic6TJHS1kvNw3MpActYeW 6xLkqyYpHHxe8fQNh8fp/1gpAL/hYs6qL/0FoYlxPRlwmGJEPSW9hIRXZAqfc1mPLJk1Gf Z3HS8YcH5U5QraBgrRTK14p06w5Ic8E= 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, akpm@linux-foundation.org, vitaly.wool@konsulko.se Cc: Hui Zhu , Geliang Tang Subject: [PATCH v6 1/2] rust: allocator: add KUnit tests for alignment guarantees Date: Wed, 30 Jul 2025 11:35:21 +0800 Message-ID: <3ad14f15b6d2639d6e998ecd03158313414b69dd.1753841900.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 test module to verify memory alignment guarantees for Rust kernel allocators. The tests cover `Kmalloc`, `Vmalloc` and `KVmalloc` allocators with both standard and large page-aligned allocations. Key features of the tests: 1. Creates alignment-constrained types: - 128-byte aligned `Blob` - 8192-byte (4-page) aligned `LargeAlignBlob` 2. Validates allocators using `TestAlign` helper which: - Checks address alignment masks - Supports uninitialized allocations 3. Tests all three allocators with both alignment requirements: - Kmalloc with 128B and 8192B - Vmalloc with 128B and 8192B - KVmalloc with 128B and 8192B Co-developed-by: Geliang Tang Signed-off-by: Geliang Tang Signed-off-by: Hui Zhu Reviewed-by: Alice Ryhl --- rust/kernel/alloc/allocator.rs | 56 ++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs index 63f271624428..1f173038cec9 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -184,3 +184,59 @@ unsafe fn realloc( unsafe { ReallocFunc::KVREALLOC.call(ptr, layout, old_layout, flag= s, nid) } } } + +#[macros::kunit_tests(rust_allocator_kunit)] +mod tests { + use super::*; + use core::mem::MaybeUninit; + use kernel::prelude::*; + + #[test] + fn test_alignment() -> Result<()> { + 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. + #[expect(dead_code)] + #[repr(align(128))] + struct Blob([u8; TEST_SIZE]); + #[expect(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; + addr & (align - 1) =3D=3D 0 + } + } + + 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)); + + let ta =3D TestAlign::::new()?; + assert!(ta.alignment_valid(8192)); + + let ta =3D TestAlign::::new()?; + assert!(ta.alignment_valid(128)); + + let ta =3D TestAlign::::new()?; + assert!(ta.alignment_valid(8192)); + + Ok(()) + } +} --=20 2.43.0 From nobody Sun Oct 5 20:17:11 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 D281F1DACA7 for ; Wed, 30 Jul 2025 03:37:01 +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=1753846623; cv=none; b=NG58MwKIB6VGjPsdF9hG0cv8iLPdLBkfqnP7BbPff85Wr7RrvJQ+D17NosP9pmMe0YBj1a+NZPsl2Fjq73bHMFDDZuwDiw3UOjBc2bJgVkroL9AXDWaz5jFlY2HG/VuCq8mGjZLLQzN+lvAAK7R5lt/kNPgeFfP6D8R6Hb2DjFk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753846623; c=relaxed/simple; bh=pvUw3HOxO3kubIOU4lbfo6vjGrJWhVYRfVOKaI+9+8Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nS7qOAQeEEBrtYW5qSw/AIi0w5/k3+cwcfTManTGRnAbtewB1SkKiupocRav2EmS60OAKoVOfAniUJQ5CheRFaVPKwNTS9gFbuTz5MXRIm4ym/WFuz7AUUJxgO2o9uK/VTPLJslmrQVCguDQrT++nGIm15uuCAxVof8doxda1gg= 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=ZWQS7OOk; 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="ZWQS7OOk" 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=1753846620; 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=NL2NutqI3C7y42/84zE/Wv89v2yejsQI2VaLqtRerjo=; b=ZWQS7OOklMFC9W+ul+VI4zzg8VSkwyMOMMCucZcuPFggFQbbPgvcAMKkmYU0n1hcfhf1HF Lo+6JUnF/JU5C1ezmtOeFazDHaoSXitcs5UfnA2FKhIq/8VmH3YmBkjZ3tJwV0C0GlkmLB w1NM+2lc/T3ebLela9Xh/M8NHQU2KjM= 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, akpm@linux-foundation.org, vitaly.wool@konsulko.se Cc: Hui Zhu , Geliang Tang Subject: [PATCH v6 2/2] rust: alloc: kvec: add doc example for as_slice method Date: Wed, 30 Jul 2025 11:35:22 +0800 Message-ID: <786e88abea2557080bde65fa9892df3b956731f2.1753841900.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 92d0ed3f302e..f57e08c64929 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