Skip to content

Commit 46cc59b

Browse files
fbqherrnst
authored andcommitted
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 <[email protected]> Reviewed-by: Andreas Hindborg <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 68ba60d commit 46cc59b

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

rust/kernel/allocator.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: bindings::gf
4141

4242
unsafe impl GlobalAlloc for KernelAllocator {
4343
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
44-
// `krealloc()` is used instead of `kmalloc()` because the latter is
45-
// an inline function and cannot be bound to as a result.
46-
unsafe { bindings::krealloc(ptr::null(), layout.size(), bindings::GFP_KERNEL) as *mut u8 }
44+
// SAFETY: `ptr::null_mut()` is null and `layout` has a non-zero size by the function safety
45+
// requirement.
46+
unsafe { krealloc_aligned(ptr::null_mut(), layout, bindings::GFP_KERNEL) }
4747
}
4848

4949
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {

0 commit comments

Comments
 (0)