Re: [PATCH] rust: allocator: Prevents mis-aligned allocation

From: Martin Rodriguez Reboredo
Date: Wed Jun 14 2023 - 12:35:41 EST


On 6/13/23 13:42, Boqun Feng wrote:
Currently the KernelAllocator simply passes the size of the type Layout
to krealloc(), and in theory the alignment requirement from the type
Layout may be larger than the guarantee provided by SLAB, which means
the allocated object is mis-aligned.

Fixes this by adjusting the allocation size to the nearest power of two,
which SLAB always guarantees a size-aligned allocation. And because Rust
guarantees that original size must be a multiple of alignment and the
alignment must be a power of two, then the alignment requirement is
satisfied.

Suggested-by: Vlastimil Babka <vbabka@xxxxxxx>
Co-developed-by: Andreas Hindborg (Samsung) <nmi@xxxxxxxxxxxx>
Signed-off-by: Andreas Hindborg (Samsung) <nmi@xxxxxxxxxxxx>
Signed-off-by: Boqun Feng <boqun.feng@xxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx # v6.1+
---
[...]

Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@xxxxxxxxx>