rust: allocator: Prevent mis-aligned allocation
authorBoqun Feng <boqun.feng@gmail.com>
Sun, 30 Jul 2023 01:29:02 +0000 (18:29 -0700)
committerMiguel Ojeda <ojeda@kernel.org>
Fri, 4 Aug 2023 15:10:31 +0000 (17:10 +0200)
commitb3d8aa84bbfe9b58ccc5332cacf8ea17200af310
tree896f78de27e1336c7c2b5eb3ed6bbc413e6ef2bf
parent6eaae198076080886b9e7d57f4ae06fa782f90ef
rust: allocator: Prevent mis-aligned allocation

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@suse.cz>
Co-developed-by: "Andreas Hindborg (Samsung)" <nmi@metaspace.dk>
Signed-off-by: "Andreas Hindborg (Samsung)" <nmi@metaspace.dk>
Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
Cc: stable@vger.kernel.org # v6.1+
Acked-by: Vlastimil Babka <vbabka@suse.cz>
Fixes: 247b365dc8dc ("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: Miguel Ojeda <ojeda@kernel.org>
rust/bindings/bindings_helper.h
rust/kernel/allocator.rs