lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID:
 <CH3PR12MB90797F5DFD93753FFDA0312D9724A@CH3PR12MB9079.namprd12.prod.outlook.com>
Date: Wed, 30 Jul 2025 16:26:00 +0800
From: Kunwu Chan <kunwu.chan@...mail.com>
To: Hui Zhu <hui.zhu@...ux.dev>, Danilo Krummrich <dakr@...nel.org>,
 Lorenzo Stoakes <lorenzo.stoakes@...cle.com>,
 Vlastimil Babka <vbabka@...e.cz>, "Liam R . Howlett"
 <Liam.Howlett@...cle.com>, Uladzislau Rezki <urezki@...il.com>,
 Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>,
 Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
 bjorn3_gh@...tonmail.com, Benno Lossin <lossin@...nel.org>,
 Andreas Hindborg <a.hindborg@...nel.org>, Alice Ryhl <aliceryhl@...gle.com>,
 Trevor Gross <tmgross@...ch.edu>, rust-for-linux@...r.kernel.org,
 linux-kernel@...r.kernel.org, akpm@...ux-foundation.org,
 vitaly.wool@...sulko.se
Cc: Hui Zhu <zhuhui@...inos.cn>, Geliang Tang <geliang@...nel.org>
Subject: Re: [PATCH v6 1/2] rust: allocator: add KUnit tests for alignment
 guarantees

On 2025/7/30 11:35, Hui Zhu wrote:
> From: Hui Zhu <zhuhui@...inos.cn>
>
> 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 <geliang@...nel.org>
> Signed-off-by: Geliang Tang <geliang@...nel.org>
> Signed-off-by: Hui Zhu <zhuhui@...inos.cn>
> ---
>   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, flags, 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 = 1024;
> +        const TEST_LARGE_ALIGN_SIZE: usize = 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<T, A: Allocator>(Box<MaybeUninit<T>, A>);
> +        impl<T, A: Allocator> TestAlign<T, A> {
> +            fn new() -> Result<Self> {
> +                Ok(Self(Box::<_, A>::new_uninit(GFP_KERNEL)?))
> +            }
> +
> +            fn alignment_valid(&self, align: usize) -> bool {
> +                assert!(align.is_power_of_two());
> +
> +                let addr = self.0.as_ptr() as usize;
> +                addr & (align - 1) == 0
> +            }
> +        }
> +
> +        let ta = TestAlign::<Blob, Kmalloc>::new()?;
> +        assert!(ta.alignment_valid(128));
> +
> +        let ta = TestAlign::<LargeAlignBlob, Kmalloc>::new()?;
> +        assert!(ta.alignment_valid(8192));
> +
> +        let ta = TestAlign::<Blob, Vmalloc>::new()?;
> +        assert!(ta.alignment_valid(128));
> +
> +        let ta = TestAlign::<LargeAlignBlob, Vmalloc>::new()?;
> +        assert!(ta.alignment_valid(8192));
> +
> +        let ta = TestAlign::<Blob, KVmalloc>::new()?;
> +        assert!(ta.alignment_valid(128));
> +
> +        let ta = TestAlign::<LargeAlignBlob, KVmalloc>::new()?;
> +        assert!(ta.alignment_valid(8192));
> +
> +        Ok(())
> +    }
> +}

Reviewed-by: Kunwu Chan <chentao@...inos.cn>

-- 
Thanks,
   Kunwu.Chan(Tao.Chan)


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ