Message ID | 20230730012905.643822-3-boqun.feng@gmail.com (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | Fix alignment issue and prepare for rust 1.71 | expand |
Boqun Feng <boqun.feng@gmail.com> writes: > 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> > --- > rust/kernel/allocator.rs | 6 +++--- > 1 file changed, 3 insertions(+), 3 deletions(-) > > diff --git a/rust/kernel/allocator.rs b/rust/kernel/allocator.rs > index fae11d1fdba7..1aec688cf0e0 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) { Reviewed-by: Andreas Hindborg <a.hindborg@samsung.com>
diff --git a/rust/kernel/allocator.rs b/rust/kernel/allocator.rs index fae11d1fdba7..1aec688cf0e0 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) {
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> --- rust/kernel/allocator.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)