summaryrefslogtreecommitdiff
path: root/rust/kernel
diff options
context:
space:
mode:
authorBoqun Feng <boqun.feng@gmail.com>2023-07-29 18:29:03 -0700
committerMiguel Ojeda <ojeda@kernel.org>2023-08-07 11:33:33 +0200
commitf39a97d0d8a765268b8de17f2e34393e7cb2cd2d (patch)
tree55c5d2e12293e2bb2f1ab45a13a5bd0762c2ddbb /rust/kernel
parent917b2e00b90f7a0f21ab3f28e7cf165825a7aba7 (diff)
rust: allocator: Use krealloc_aligned() in KernelAllocator::alloc
This fixes the potential issue that when KernelAllocator is used, the allocation may be mis-aligned due to SLAB's alignment guarantee. Signed-off-by: Boqun Feng <boqun.feng@gmail.com> Reviewed-by: Andreas Hindborg <a.hindborg@samsung.com> Link: https://lore.kernel.org/r/20230730012905.643822-3-boqun.feng@gmail.com Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Diffstat (limited to 'rust/kernel')
-rw-r--r--rust/kernel/allocator.rs6
1 files changed, 3 insertions, 3 deletions
diff --git a/rust/kernel/allocator.rs b/rust/kernel/allocator.rs
index 9363b527be66..d4a3670ef405 100644
--- a/rust/kernel/allocator.rs
+++ b/rust/kernel/allocator.rs
@@ -41,9 +41,9 @@ unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: bindings::gf
unsafe impl GlobalAlloc for KernelAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
- // `krealloc()` is used instead of `kmalloc()` because the latter is
- // an inline function and cannot be bound to as a result.
- unsafe { bindings::krealloc(ptr::null(), layout.size(), bindings::GFP_KERNEL) as *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) }
}
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {