Skip to content

Commit a9bff7e

Browse files
ojedagregkh
authored andcommitted
rust: alloc: fix rusttest by providing Cmalloc::aligned_layout too
[ Upstream commit 0f580d5 ] Commit fde578c ("rust: alloc: replace aligned_size() with Kmalloc::aligned_layout()") provides a public `aligned_layout` function in `Kamlloc`, but not in `Cmalloc`, and thus uses of it will trigger an error in `rusttest`. Such a user appeared in the following commit 22ab064 ("rust: drm: ensure kmalloc() compatible Layout"): error[E0599]: no function or associated item named `aligned_layout` found for struct `alloc::allocator_test::Cmalloc` in the current scope --> rust/kernel/drm/device.rs:100:31 | 100 | let layout = Kmalloc::aligned_layout(Layout::new::<Self>()); | ^^^^^^^^^^^^^^ function or associated item not found in `Cmalloc` | ::: rust/kernel/alloc/allocator_test.rs:19:1 | 19 | pub struct Cmalloc; | ------------------ function or associated item `aligned_layout` not found for this struct Thus add an equivalent one for `Cmalloc`. Fixes: fde578c ("rust: alloc: replace aligned_size() with Kmalloc::aligned_layout()") Signed-off-by: Miguel Ojeda <ojeda@kernel.org> Link: https://lore.kernel.org/r/20250816204215.2719559-1-ojeda@kernel.org Signed-off-by: Danilo Krummrich <dakr@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 21b38f3 commit a9bff7e

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

rust/kernel/alloc/allocator_test.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ pub type Kmalloc = Cmalloc;
2222
pub type Vmalloc = Kmalloc;
2323
pub type KVmalloc = Kmalloc;
2424

25+
impl Cmalloc {
26+
/// Returns a [`Layout`] that makes [`Kmalloc`] fulfill the requested size and alignment of
27+
/// `layout`.
28+
pub fn aligned_layout(layout: Layout) -> Layout {
29+
// Note that `layout.size()` (after padding) is guaranteed to be a multiple of
30+
// `layout.align()` which together with the slab guarantees means that `Kmalloc` will return
31+
// a properly aligned object (see comments in `kmalloc()` for more information).
32+
layout.pad_to_align()
33+
}
34+
}
35+
2536
extern "C" {
2637
#[link_name = "aligned_alloc"]
2738
fn libc_aligned_alloc(align: usize, size: usize) -> *mut crate::ffi::c_void;

0 commit comments

Comments
 (0)