[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <808973f5-2c3b-4373-a41b-29e54f3ca4fa@proton.me>
Date: Sat, 31 Aug 2024 05:21:25 +0000
From: Benno Lossin <benno.lossin@...ton.me>
To: Danilo Krummrich <dakr@...nel.org>, ojeda@...nel.org, alex.gaynor@...il.com, wedsonaf@...il.com, boqun.feng@...il.com, gary@...yguo.net, bjorn3_gh@...tonmail.com, a.hindborg@...sung.com, aliceryhl@...gle.com, akpm@...ux-foundation.org
Cc: daniel.almeida@...labora.com, faith.ekstrand@...labora.com, boris.brezillon@...labora.com, lina@...hilina.net, mcanal@...lia.com, zhiw@...dia.com, cjia@...dia.com, jhubbard@...dia.com, airlied@...hat.com, ajanulgu@...hat.com, lyude@...hat.com, linux-kernel@...r.kernel.org, rust-for-linux@...r.kernel.org, linux-mm@...ck.org
Subject: Re: [PATCH v6 06/26] rust: alloc: implement `Vmalloc` allocator
On 16.08.24 02:10, Danilo Krummrich wrote:
> @@ -63,6 +73,9 @@ impl ReallocFunc {
> // INVARIANT: `krealloc` satisfies the type invariants.
> const KREALLOC: Self = Self(bindings::krealloc);
>
> + // INVARIANT: `vrealloc` satisfies the type invariants.
> + const VREALLOC: Self = Self(bindings::vrealloc);
> +
> /// # Safety
> ///
> /// This method has the same safety requirements as [`Allocator::realloc`].
> @@ -141,6 +154,25 @@ unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
> }
> }
>
> +unsafe impl Allocator for Vmalloc {
The same safety comment as with `Kmalloc` should fit here (and with
`KVmalloc` too).
---
Cheers,
Benno
> + #[inline]
> + unsafe fn realloc(
> + ptr: Option<NonNull<u8>>,
> + layout: Layout,
> + flags: Flags,
> + ) -> Result<NonNull<[u8]>, AllocError> {
> + // TODO: Support alignments larger than PAGE_SIZE.
> + if layout.align() > bindings::PAGE_SIZE {
> + pr_warn!("Vmalloc does not support alignments larger than PAGE_SIZE yet.\n");
> + return Err(AllocError);
> + }
> +
> + // SAFETY: If not `None`, `ptr` is guaranteed to point to valid memory, which was previously
> + // allocated with this `Allocator`.
> + unsafe { ReallocFunc::VREALLOC.call(ptr, layout, flags) }
> + }
> +}
> +
> #[global_allocator]
> static ALLOCATOR: Kmalloc = Kmalloc;
>
> diff --git a/rust/kernel/alloc/allocator_test.rs b/rust/kernel/alloc/allocator_test.rs
> index 4785efc474a7..e7bf2982f68f 100644
> --- a/rust/kernel/alloc/allocator_test.rs
> +++ b/rust/kernel/alloc/allocator_test.rs
> @@ -7,6 +7,7 @@
> use core::ptr::NonNull;
>
> pub struct Kmalloc;
> +pub type Vmalloc = Kmalloc;
>
> unsafe impl Allocator for Kmalloc {
> unsafe fn realloc(
> --
> 2.46.0
>
Powered by blists - more mailing lists