Re: [PATCH v9 07/29] rust: alloc: add module `allocator_test`

From: Danilo Krummrich
Date: Tue Oct 08 2024 - 10:31:47 EST


On Tue, Oct 08, 2024 at 06:10:39AM -0700, Boqun Feng wrote:
> On Fri, Oct 04, 2024 at 05:41:11PM +0200, Danilo Krummrich wrote:
> > `Allocator`s, such as `Kmalloc`, will be used by e.g. `Box` and `Vec` in
> > subsequent patches, and hence this dependency propagates throughout the
> > whole kernel.
> >
> > Add the `allocator_test` module that provides an empty implementation
> > for all `Allocator`s in the kernel, such that we don't break the
> > `rusttest` make target in subsequent patches.
> >
> > Reviewed-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
> > Reviewed-by: Benno Lossin <benno.lossin@xxxxxxxxx>
> > Reviewed-by: Gary Guo <gary@xxxxxxxxxxx>
> > Signed-off-by: Danilo Krummrich <dakr@xxxxxxxxxx>
> > ---
> > rust/kernel/alloc.rs | 9 +++++++--
> > rust/kernel/alloc/allocator_test.rs | 19 +++++++++++++++++++
> > 2 files changed, 26 insertions(+), 2 deletions(-)
> > create mode 100644 rust/kernel/alloc/allocator_test.rs
> >
> > diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs
> > index 91b2a26faf3d..f8ddc8d96da9 100644
> > --- a/rust/kernel/alloc.rs
> > +++ b/rust/kernel/alloc.rs
> > @@ -2,12 +2,17 @@
> >
> > //! Extensions to the [`alloc`] crate.
> >
> > -#[cfg(not(test))]
> > -#[cfg(not(testlib))]
> > +#[cfg(not(any(test, testlib)))]
> > pub mod allocator;
> > pub mod box_ext;
> > pub mod vec_ext;
> >
> > +#[cfg(any(test, testlib))]
> > +pub mod allocator_test;
> > +
> > +#[cfg(any(test, testlib))]
> > +pub use self::allocator_test as allocator;
> > +
> > /// Indicates an allocation error.
> > #[derive(Copy, Clone, PartialEq, Eq, Debug)]
> > pub struct AllocError;
> > diff --git a/rust/kernel/alloc/allocator_test.rs b/rust/kernel/alloc/allocator_test.rs
> > new file mode 100644
> > index 000000000000..4785efc474a7
> > --- /dev/null
> > +++ b/rust/kernel/alloc/allocator_test.rs
> > @@ -0,0 +1,19 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +
> > +#![allow(missing_docs)]
> > +
> > +use super::{AllocError, Allocator, Flags};
> > +use core::alloc::Layout;
> > +use core::ptr::NonNull;
> > +
> > +pub struct Kmalloc;
> > +
> > +unsafe impl Allocator for Kmalloc {
> > + unsafe fn realloc(
> > + _ptr: Option<NonNull<u8>>,
> > + _layout: Layout,
>
> The `old_layout` parameter is missing here.

Thanks, good catch.

diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs
index f8ddc8d96da9..28281d600492 100644
--- a/rust/kernel/alloc.rs
+++ b/rust/kernel/alloc.rs
@@ -193,6 +193,7 @@ unsafe fn free(ptr: NonNull<u8>, layout: Layout) {
}
}

+#[allow(dead_code)]
/// Returns a properly aligned dangling pointer from the given `layout`.
pub(crate) fn dangling_from_layout(layout: Layout) -> NonNull<u8> {
let ptr = layout.align() as *mut u8;
diff --git a/rust/kernel/alloc/allocator_test.rs b/rust/kernel/alloc/allocator_test.rs
index 4785efc474a7..c5d325506f0c 100644
--- a/rust/kernel/alloc/allocator_test.rs
+++ b/rust/kernel/alloc/allocator_test.rs
@@ -12,6 +12,7 @@ unsafe impl Allocator for Kmalloc {
unsafe fn realloc(
_ptr: Option<NonNull<u8>>,
_layout: Layout,
+ _old_layout: Layout,
_flags: Flags,
) -> Result<NonNull<[u8]>, AllocError> {
panic!();

We also have to fix an intermediate dead code warning for `dangling_from_layout`
in the `rusttest` target, until we start using it when we implement `Cmalloc`
in this module.