Re: [PATCH v5 00/26] Generic `Allocator` support for Rust

Boqun Feng
2024-08-14T19:41:19Z

Hi Danilo,

On Mon, Aug 12, 2024 at 08:22:46PM +0200, Danilo Krummrich wrote:
> Hi,
>
> This patch series adds generic kernel allocator support for Rust, which so far
> is limited to `kmalloc` allocations.
>
> In order to abstain from (re-)adding unstable Rust features to the kernel, this
> patch series does not extend the `Allocator` trait from Rust's `alloc` crate,
> nor does it extend the `BoxExt` and `VecExt` extensions.
>
> Instead, this series introduces a kernel specific `Allocator` trait, which is
> implemented by the `Kmalloc`, `Vmalloc` and `KVmalloc` allocators, also
> implemented in the context of this series.
>
> As a consequence we need our own kernel `Box<T, A>` and `Vec<T, A>` types.
> Additionally, this series adds the following type aliases:
>
> ```
> pub type KBox<T> = Box<T, Kmalloc>;
> pub type VBox<T> = Box<T, Vmalloc>;
> pub type KVBox<T> = Box<T, KVmalloc>;
>
>
> pub type KVec<T> = Vec<T, Kmalloc>;
> pub type VVec<T> = Vec<T, Vmalloc>;
> pub type KVVec<T> = Vec<T, KVmalloc>;
> ```
>
> With that, we can start using the kernel `Box` and `Vec` types throughout the
> tree and remove the now obolete extensions `BoxExt` and `VecExt`.
>
> For a final cleanup, this series removes the last minor dependencies to Rust's
> `alloc` crate and removes it from the entire kernel build.
>
> The series ensures not to break the `rusttest` make target by implementing the
> `allocator_test` module providing a stub implementation for all kernel
> `Allocator`s.
>
> This patch series passes all KUnit tests, including the ones added by this
> series. Additionally, the tests were run with `kmemleak` and `KASAN` enabled,
> without any issues.
>
> This series is based on [1], which hit -mm/mm-unstable, and is also available
> in [2].
>
> [1] https://git.kernel.org/pub/scm/linux/kernel/git/dakr/linux.git/log/?h=mm/krealloc
> [2] https://git.kernel.org/pub/scm/linux/kernel/git/dakr/linux.git/log/?h=rust/mm
>
> Changes in v5:
> - (safety) comment / documentation fixes suggested by Alice, Benno and Gary
> - remove `Unique<T>` and implement `Send` and `Sync` for `Box` and `Vec`
> - use `KMALLOC_MAX_SIZE` for `KVmalloc` test and add a `Kmalloc` test that
> expects to fail for `KMALLOC_MAX_SIZE`
> - create use constants `KREALLOC`, `VREALLOC` and `KVREALLOC` for
> `ReallocFuncs`
> - drop `Box::drop_contents` for now, will add it again, once I actually rebase
> on the original patch that introduces it

I'm trying to put your series on rust-dev, but I hit a few conflicts due
to the conflict with `Box::drop_contents`, which has been in rust-dev
for a while. And the conflict is not that trivial for me to resolve.
So just a head-up, that's a requirement for me to put it on rust-dev for
more tests from my end ;-)

Regards,
Boqun

> - improve usage of `size_of_val` in `Box`
> - move `InPlaceInit` and `ForeignOwnable` impls into kbox.rs
> - fix missing `Box` conversions in rnull.rs
> - reworked `Cmalloc` to keep track of the size of memory allocations itself
> - remove `GlobalAlloc` together with the `alloc` crate to avoid a linker error
> - remove `alloc` from scripts/generate_rust_analyzer.py
> …