Re: [PATCH 1/3] rust: allocator: Prevent mis-aligned allocation

From: Vlastimil Babka
Date: Mon Jul 31 2023 - 03:56:27 EST


On 7/30/23 03:29, Boqun Feng wrote:
> Currently the rust allocator 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.
>
> Fix 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 the 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+

Acked-by: Vlastimil Babka <vbabka@xxxxxxx>