[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <87y1iwpc3j.fsf@metaspace.dk>
Date: Mon, 31 Jul 2023 13:12:17 +0200
From: "Andreas Hindborg (Samsung)" <nmi@...aspace.dk>
To: Boqun Feng <boqun.feng@...il.com>
Cc: rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org,
linux-mm@...ck.org, Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>,
Wedson Almeida Filho <wedsonaf@...il.com>,
Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <benno.lossin@...ton.me>,
Martin Rodriguez Reboredo <yakoyoku@...il.com>,
Alice Ryhl <aliceryhl@...gle.com>,
Dariusz Sosnowski <dsosnowski@...snowski.pl>,
Geoffrey Thomas <geofft@...reload.com>,
Fox Chen <foxhlchen@...il.com>,
John Baublitz <john.m.baublitz@...il.com>,
Christoph Lameter <cl@...ux.com>,
Pekka Enberg <penberg@...nel.org>,
David Rientjes <rientjes@...gle.com>,
Joonsoo Kim <iamjoonsoo.kim@....com>,
Andrew Morton <akpm@...ux-foundation.org>,
Vlastimil Babka <vbabka@...e.cz>,
Roman Gushchin <roman.gushchin@...ux.dev>,
Hyeonggon Yoo <42.hyeyoo@...il.com>,
Kees Cook <keescook@...omium.org>, stable@...r.kernel.org
Subject: Re: [PATCH 3/3] rust: alloc: Add realloc and alloc_zeroed to the
GlobalAlloc impl
Boqun Feng <boqun.feng@...il.com> writes:
> From: Björn Roy Baron <bjorn3_gh@...tonmail.com>
>
> While there are default impls for these methods, using the respective C
> api's is faster. Currently neither the existing nor these new
> GlobalAlloc method implementations are actually called. Instead the
> __rust_* function defined below the GlobalAlloc impl are used. With
> rustc 1.71 these functions will be gone and all allocation calls will go
> through the GlobalAlloc implementation.
>
> Link: https://github.com/Rust-for-Linux/linux/issues/68
> Signed-off-by: Björn Roy Baron <bjorn3_gh@...tonmail.com>
> [boqun: add size adjustment for alignment requirement]
> Signed-off-by: Boqun Feng <boqun.feng@...il.com>
> ---
Reviewed-by: Andreas Hindborg <a.hindborg@...sung.com>
> rust/kernel/allocator.rs | 27 +++++++++++++++++++++++++++
> 1 file changed, 27 insertions(+)
>
> diff --git a/rust/kernel/allocator.rs b/rust/kernel/allocator.rs
> index 1aec688cf0e0..6f1f50465ab3 100644
> --- a/rust/kernel/allocator.rs
> +++ b/rust/kernel/allocator.rs
> @@ -51,6 +51,33 @@ unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
> bindings::kfree(ptr as *const core::ffi::c_void);
> }
> }
> +
> + unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
> + // SAFETY:
> + // - `new_size`, when rounded up to the nearest multiple of `layout.align()`, will not
> + // overflow `isize` by the function safety requirement.
> + // - `layout.align()` is a proper alignment (i.e. not zero and must be a power of two).
> + let layout = unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) };
> +
> + // SAFETY:
> + // - `ptr` is either null or a pointer allocated by this allocator by the function safety
> + // requirement.
> + // - the size of `layout` is not zero because `new_size` is not zero by the function safety
> + // requirement.
> + unsafe { krealloc_aligned(ptr, layout, bindings::GFP_KERNEL) }
> + }
> +
> + unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
> + // SAFETY: `ptr::null_mut()` is null and `layout` has a non-zero size by the function safety
> + // requirement.
> + unsafe {
> + krealloc_aligned(
> + ptr::null_mut(),
> + layout,
> + bindings::GFP_KERNEL | bindings::__GFP_ZERO,
> + )
> + }
> + }
> }
>
> #[global_allocator]
Powered by blists - more mailing lists