From nobody Sun Oct 5 20:03:51 2025 Received: from out-182.mta1.migadu.com (out-182.mta1.migadu.com [95.215.58.182]) (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 7A7948F5E for ; Thu, 31 Jul 2025 02:51:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753930296; cv=none; b=k/Oxyz9lidbUtES93QXxljkrk8MgBp4RxoIoiXunoo/DhcDCTHrm7zVrAbq3xXSPFF7sxYW9DNqwCYk7wr5WFqxABkbeHupIgrc0I/R8vtrlsGammCyGQJNrDUtzHYgJ4pnXsJR15g06pWS/vnDA+BMGVrfZ0lIt2vzpndC4vY8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753930296; c=relaxed/simple; bh=tuB64WSJSxd3SoYzAXwpJOXZrEA5aGaZSyxobv6TQ6U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=L3nZZx1q8i/QL1n37LXEw4E5KQfVWzP8xKew2KkFdgBcETXFB1UsraUmJnV4/6pEG4mpia1HgZjl3/0hkWeasTh7bnvLy6RdKlq7YBgFm7Bl6NfUtmcuZPA2C5LPDofG+cQw7WcHt8P9MQR3JMtXTgKtbI30kzlPmbfQfYz1rkM= 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=jWjMllAb; arc=none smtp.client-ip=95.215.58.182 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="jWjMllAb" 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=1753930292; 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=LyliLTKWJv/QG6ipWMrGFKIDAc0QyAuX9yHSs/YpZrg=; b=jWjMllAbk4GmKmIGYPgVBkP7grbepAVPB1mfAC1n0KOKwsG4KkYw96Yw+cubebJJVoopSy oar97MhYxl2dhLdPWwKHthAkt/ZqTRJqNWw/Igi0v9Siqrndv+NQhiZDGXj1DOOFb8hW5k LlzXdgUrIKoJ48lFUjMzW5lqTwVKg68= 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 , Kunwu Chan Subject: [PATCH v7 1/3] rust: allocator: add KUnit tests for alignment guarantees Date: Thu, 31 Jul 2025 10:50:05 +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 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: Kunwu Chan Acked-by: Danilo Krummrich --- 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..a20111b40529 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)] +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 is_aligned_to(&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.is_aligned_to(128)); + + let ta =3D TestAlign::::new()?; + assert!(ta.is_aligned_to(8192)); + + let ta =3D TestAlign::::new()?; + assert!(ta.is_aligned_to(128)); + + let ta =3D TestAlign::::new()?; + assert!(ta.is_aligned_to(8192)); + + let ta =3D TestAlign::::new()?; + assert!(ta.is_aligned_to(128)); + + let ta =3D TestAlign::::new()?; + assert!(ta.is_aligned_to(8192)); + + Ok(()) + } +} --=20 2.43.0