Skip to content
Commit 0c851584 authored by Boqun Feng's avatar Boqun Feng Committed by Greg Kroah-Hartman
Browse files

rust: allocator: Prevent mis-aligned allocation



commit b3d8aa84 upstream.

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: default avatarVlastimil Babka <vbabka@suse.cz>
Co-developed-by: default avatar"Andreas Hindborg (Samsung)" <nmi@metaspace.dk>
Signed-off-by: default avatar"Andreas Hindborg (Samsung)" <nmi@metaspace.dk>
Signed-off-by: default avatarBoqun Feng <boqun.feng@gmail.com>
Cc: stable@vger.kernel.org # v6.1+
Acked-by: default avatarVlastimil Babka <vbabka@suse.cz>
Fixes: 247b365d ("rust: add `kernel` crate")
Link: https://github.com/Rust-for-Linux/linux/issues/974
Link: https://lore.kernel.org/r/20230730012905.643822-2-boqun.feng@gmail.com


[ Applied rewording of comment as discussed in the mailing list. ]
Signed-off-by: default avatarMiguel Ojeda <ojeda@kernel.org>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 3a2543be
Loading
Loading
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment