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: <DBCKAOSOMXHB.3IEHVGIH7ZANN@kernel.org>
Date: Tue, 15 Jul 2025 12:37:52 +0200
From: "Danilo Krummrich" <dakr@...nel.org>
To: "Hui Zhu" <hui.zhu@...ux.dev>
Cc: "Andrew Morton" <akpm@...ux-foundation.org>, "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>, "Geliang
 Tang" <geliang@...nel.org>, "Hui Zhu" <zhuhui@...inos.cn>,
 <linux-kernel@...r.kernel.org>, <linux-mm@...ck.org>,
 <rust-for-linux@...r.kernel.org>
Subject: Re: [PATCH 3/3] rust: add a sample allocator usage

On Tue Jul 15, 2025 at 11:59 AM CEST, Hui Zhu wrote:
> +impl kernel::Module for RustAllocator {
> +    fn init(_module: &'static ThisModule) -> Result<Self> {
> +        pr_info!("Rust allocator sample (init)\n");
> +
> +        let mut vmalloc_vec = KVec::new();
> +        for (size, align) in VMALLOC_ARG {
> +            let (ptr, layout) = vmalloc_align(size, align)?;

Ok, I think I get the idea, you want to demonstrate how to use the Allocator
trait for raw memory allocations.

However, doing so is discouraged unless there's really no other way. One obvious
example are Rust's own memory allocation primitives, such as Box and Vec.

So, instead of this raw allocation, you can just use VBox::new() or
VBox::new_uninit() in the following way.

	[repr(align(ALIGN))]
	struct Blob([u8; SIZE]);

	// Creates a vmalloc allocation of size `SIZE` with an alignment of
	// `ALIGN`. The allocation is freed once `b` is dropped.
	let b = VBox::<Blob>::new_uninit(GFP_KERNEL)?;

This way you don't have to handle the layout and the Allocator type yourself and
you also don't have to care about explicitly calling vfree(), VBox does all this
for you.

> +
> +            let (addr, is_ok) = check_ptr(ptr, size, align);
> +            if !is_ok {
> +                clear_vmalloc_vec(&vmalloc_vec);
> +                return Err(EINVAL);
> +            }
> +
> +            vmalloc_vec.push((addr, layout), GFP_KERNEL)?;
> +        }
> +
> +        Ok(RustAllocator { vmalloc_vec })
> +    }
> +}

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ