linux-stable/rust
Boqun Feng 0c8515842b rust: allocator: Prevent mis-aligned allocation
commit b3d8aa84bb 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: 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: 247b365dc8 ("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>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2023-08-11 12:14:18 +02:00
..
alloc rust: alloc: vec: Add some try_* methods we need 2023-04-10 23:55:43 +02:00
bindings rust: allocator: Prevent mis-aligned allocation 2023-08-11 12:14:18 +02:00
kernel rust: allocator: Prevent mis-aligned allocation 2023-08-11 12:14:18 +02:00
macros rust: init: add PinnedDrop trait and macros 2023-04-12 18:41:05 +02:00
uapi rust: uapi: Add UAPI crate 2023-04-22 01:46:45 +02:00
.gitignore rust: uapi: Add UAPI crate 2023-04-22 01:46:45 +02:00
bindgen_parameters rust: bindgen: Add alt_instr as opaque type 2023-03-02 22:44:15 +01:00
build_error.rs rust: add build_error crate 2022-12-04 01:59:16 +01:00
compiler_builtins.rs rust: compiler_builtins: make stubs non-global 2023-01-16 21:04:34 +01:00
exports.c rust: add build_error crate 2022-12-04 01:59:16 +01:00
helpers.c rust: sync: introduce CondVar 2023-04-22 01:46:45 +02:00
Makefile Rust changes for v6.4 2023-04-30 11:20:22 -07:00