linux-stable/rust/kernel
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
..
init rust: init: add `stack_pin_init!` macro 2023-04-12 18:41:05 +02:00
sync rust: sync: introduce `CondVar` 2023-04-22 01:46:45 +02:00
allocator.rs rust: allocator: Prevent mis-aligned allocation 2023-08-11 12:14:18 +02:00
build_assert.rs rust: build_assert: add `build_{error,assert}!` macros 2022-12-04 01:59:16 +01:00
error.rs rust: error: Add from_result() helper 2023-04-12 18:41:04 +02:00
init.rs rust: init: broaden the blanket impl of `Init` 2023-04-21 00:35:26 +02:00
ioctl.rs rust: ioctl: Add ioctl number manipulation functions 2023-04-22 01:46:45 +02:00
lib.rs rust: ioctl: Add ioctl number manipulation functions 2023-04-22 01:46:45 +02:00
prelude.rs rust: introduce `current` 2023-04-22 00:20:00 +02:00
print.rs rust: kernel: Mark rust_fmt_argument as extern "C" 2023-04-06 23:11:04 +02:00
static_assert.rs rust: static_assert: add `static_assert!` macro 2022-12-04 01:59:16 +01:00
std_vendor.rs rust: std_vendor: add `dbg!` macro based on `std`'s one 2022-12-04 01:59:16 +01:00
str.rs rust: str: fix requierments->requirements typo 2023-04-06 21:55:12 +02:00
sync.rs rust: sync: introduce `CondVar` 2023-04-22 01:46:45 +02:00
task.rs rust: introduce `current` 2023-04-22 00:20:00 +02:00
types.rs rust: introduce `ARef` 2023-04-22 00:20:00 +02:00