Rust for Linux List
 help / color / mirror / Atom feed
* [PATCH] rust: alloc: fix `rusttest` by providing `Cmalloc::aligned_layout` too
@ 2025-08-16 20:42 Miguel Ojeda
  2025-08-16 23:37 ` Danilo Krummrich
  0 siblings, 1 reply; 2+ messages in thread
From: Miguel Ojeda @ 2025-08-16 20:42 UTC (permalink / raw)
  To: Danilo Krummrich, Miguel Ojeda, Alex Gaynor, David Airlie,
	Simona Vetter
  Cc: Lorenzo Stoakes, Vlastimil Babka, Liam R . Howlett,
	Uladzislau Rezki, rust-for-linux, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, dri-devel, linux-kernel, patches

Commit fde578c86281 ("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 22ab0641b939 ("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: fde578c86281 ("rust: alloc: replace aligned_size() with Kmalloc::aligned_layout()")
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
Danilo, as usual, please feel free to rebase if that is better.

Here I just copied the function, including comments, as-is, which maybe we want
to tweak. However, we may want to remove `allocator_test` soon anyway (this adds
to the justification for doing that, so that is good news :).

I am sending that one in parallel -- hopefully we can land that removal too in
this cycle, but meanwhile, we can put something like this in your branch.

 rust/kernel/alloc/allocator_test.rs | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/rust/kernel/alloc/allocator_test.rs b/rust/kernel/alloc/allocator_test.rs
index a3074480bd8d..90dd987d40e4 100644
--- a/rust/kernel/alloc/allocator_test.rs
+++ b/rust/kernel/alloc/allocator_test.rs
@@ -22,6 +22,17 @@
 pub type Vmalloc = Kmalloc;
 pub type KVmalloc = Kmalloc;

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

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] rust: alloc: fix `rusttest` by providing `Cmalloc::aligned_layout` too
  2025-08-16 20:42 [PATCH] rust: alloc: fix `rusttest` by providing `Cmalloc::aligned_layout` too Miguel Ojeda
@ 2025-08-16 23:37 ` Danilo Krummrich
  0 siblings, 0 replies; 2+ messages in thread
From: Danilo Krummrich @ 2025-08-16 23:37 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Alex Gaynor, David Airlie, Simona Vetter, Lorenzo Stoakes,
	Vlastimil Babka, Liam R . Howlett, Uladzislau Rezki,
	rust-for-linux, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	dri-devel, linux-kernel, patches

On Sat Aug 16, 2025 at 10:42 PM CEST, Miguel Ojeda wrote:
> Commit fde578c86281 ("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`.

Good catch, thanks! -- Applied to drm-misc-fixes.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-08-16 23:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-16 20:42 [PATCH] rust: alloc: fix `rusttest` by providing `Cmalloc::aligned_layout` too Miguel Ojeda
2025-08-16 23:37 ` Danilo Krummrich

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox