From: Danilo Krummrich <dakr@kernel.org>
To: ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com,
boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com,
benno.lossin@proton.me, a.hindborg@samsung.com,
aliceryhl@google.com, akpm@linux-foundation.org
Cc: daniel.almeida@collabora.com, faith.ekstrand@collabora.com,
boris.brezillon@collabora.com, lina@asahilina.net,
mcanal@igalia.com, zhiw@nvidia.com, cjia@nvidia.com,
jhubbard@nvidia.com, airlied@redhat.com, ajanulgu@redhat.com,
lyude@redhat.com, linux-kernel@vger.kernel.org,
rust-for-linux@vger.kernel.org, linux-mm@kvack.org,
Danilo Krummrich <dakr@kernel.org>
Subject: [PATCH v7 00/26] Generic `Allocator` support for Rust
Date: Thu, 12 Sep 2024 00:52:36 +0200 [thread overview]
Message-ID: <20240911225449.152928-1-dakr@kernel.org> (raw)
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-stable, 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 v7:
- rebase onto rust-next resolving recent conflicts (93dc3be19450447a3a7090bd1dfb9f3daac3e8d2)
- documentation / safety comment changes suggested by Benno and Boqun
- rename `ptr` to `current` in `IntoIter::next` (Alice)
- remove unnecessary braces in `kvec!` (Benno)
- add `debug_assert!` in `Vec::set_len` (Benno)
- remove unused args in `impl_slice_eq!` (Benno)
- simplify `if` statement in `Cmalloc::realloc` (Benno)
Changes in v6:
- rebase onto rust-dev
- keep compiler annotations for {k,v,kv}realloc()
- documentation changes suggested by Alice, Benno
- remove `Box::into_pin`
- fix typo in `Send` and `Sync` for `Box` and `Vec`
- `kvec!` changes suggested by Alice
- free `src` after copy in `Cmalloc`
- handle `n == 0` in `Vec::extend_with`
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
- 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
Changes in v4:
- (safety) comment fixes suggested by Alice and Boqun
- remove `Box::from_raw_alloc` and `Box::into_raw_alloc`, we don't need them
- in `Box::drop` call `size_of_val` before `drop_in_place`
- implement ForeignOwnable for Pin<Box<T>> as suggested by Alice
- in `Vec::extend_with`, iterate over `n` instead of `spare.len()`
- for `Vmalloc` and `KVmalloc` fail allocation for alignments larger than
PAGE_SIZE for now (will add support for larger alignments in a separate
series)
- implement `Cmalloc` in `allocator_test` and type alias all kernel allocator
types to it, such that we can use the kernel's `Box` and `Vec` types in
userspace tests (rusttest)
- this makes patch "rust: str: test: replace `alloc::format`" rather trivial
Changes in v3:
- Box:
- minor documentation fixes
- removed unnecessary imports in doc tests
- dropeed `self` argument from some remaining `Box` methods
- implement `InPlaceInit` for Box<T, A> rather than specifically for `KBox<T>`
- Vec:
- minor documentation fixes
- removed useless `Vec::allocator` method
- in `Vec::extend_with` use `Vec::spare_capacity_mut` instead of raw pointer operations
- added a few missing safety comments
- pass GFP flags to `Vec::collect`
- fixed a rustdoc warning in alloc.rs
- fixed the allocator_test module to implement the `Allocator` trait correctly
- rebased to rust-next
Changes in v2:
- preserve `impl GlobalAlloc for Kmalloc` and remove it at the end (Benno)
- remove `&self` parameter from all `Allocator` functions (Benno)
- various documentation fixes for `Allocator` (Benno)
- use `NonNull<u8>` for `Allocator::free` and `Option<NonNull<u8>>` for
`Allocator::realloc` (Benno)
- fix leak of `IntoIter` in `Vec::collect` (Boqun)
- always realloc (try to shrink) in `Vec::collect`, it's up the the
`Allocator` to provide a heuristic whether it makes sense to actually shrink
- rename `KBox<T, A>` -> `Box<T, A>` and `KVec<T, A>` -> `Vec<T, A>` and
provide type aliases `KBox<T>`, `VBox<T>`, `KVBox<T>`, etc.
- This allows for much cleaner code and, in combination with removing
`&self` parameters from `Allocator`s, gets us rid of the need for
`Box::new` and `Box::new_alloc` and all other "_alloc" postfixed
functions.
- Before: `KBox::new_alloc(foo, Vmalloc)?`
- After: `VBox::new(foo)?`, which resolves to
`Box::<Foo, Vmalloc>::new(foo)?;
Danilo Krummrich (26):
rust: alloc: add `Allocator` trait
rust: alloc: separate `aligned_size` from `krealloc_aligned`
rust: alloc: rename `KernelAllocator` to `Kmalloc`
rust: alloc: implement `Allocator` for `Kmalloc`
rust: alloc: add module `allocator_test`
rust: alloc: implement `Vmalloc` allocator
rust: alloc: implement `KVmalloc` allocator
rust: alloc: add __GFP_NOWARN to `Flags`
rust: alloc: implement kernel `Box`
rust: treewide: switch to our kernel `Box` type
rust: alloc: remove extension of std's `Box`
rust: alloc: add `Box` to prelude
rust: alloc: implement kernel `Vec` type
rust: alloc: implement `IntoIterator` for `Vec`
rust: alloc: implement `collect` for `IntoIter`
rust: treewide: switch to the kernel `Vec` type
rust: alloc: remove `VecExt` extension
rust: alloc: add `Vec` to prelude
rust: error: use `core::alloc::LayoutError`
rust: error: check for config `test` in `Error::name`
rust: alloc: implement `contains` for `Flags`
rust: alloc: implement `Cmalloc` in module allocator_test
rust: str: test: replace `alloc::format`
rust: alloc: update module comment of alloc.rs
kbuild: rust: remove the `alloc` crate and `GlobalAlloc`
MAINTAINERS: add entry for the Rust `alloc` module
MAINTAINERS | 7 +
drivers/block/rnull.rs | 4 +-
rust/Makefile | 43 +-
rust/bindings/bindings_helper.h | 1 +
rust/exports.c | 1 -
rust/helpers/helpers.c | 1 +
rust/helpers/slab.c | 6 +
rust/helpers/vmalloc.c | 9 +
rust/kernel/alloc.rs | 152 ++++-
rust/kernel/alloc/allocator.rs | 198 ++++--
rust/kernel/alloc/allocator_test.rs | 200 ++++++
rust/kernel/alloc/box_ext.rs | 87 ---
rust/kernel/alloc/kbox.rs | 465 ++++++++++++++
rust/kernel/alloc/kvec.rs | 905 ++++++++++++++++++++++++++++
rust/kernel/alloc/vec_ext.rs | 185 ------
rust/kernel/error.rs | 6 +-
rust/kernel/init.rs | 95 +--
rust/kernel/init/__internal.rs | 2 +-
rust/kernel/lib.rs | 1 -
rust/kernel/prelude.rs | 5 +-
rust/kernel/rbtree.rs | 49 +-
rust/kernel/str.rs | 35 +-
rust/kernel/sync/arc.rs | 17 +-
rust/kernel/sync/condvar.rs | 4 +-
rust/kernel/sync/lock/mutex.rs | 2 +-
rust/kernel/sync/lock/spinlock.rs | 2 +-
rust/kernel/sync/locked_by.rs | 2 +-
rust/kernel/types.rs | 52 +-
rust/kernel/uaccess.rs | 17 +-
rust/kernel/workqueue.rs | 20 +-
rust/macros/lib.rs | 12 +-
samples/rust/rust_minimal.rs | 4 +-
scripts/Makefile.build | 7 +-
scripts/generate_rust_analyzer.py | 11 +-
34 files changed, 2029 insertions(+), 578 deletions(-)
create mode 100644 rust/helpers/vmalloc.c
create mode 100644 rust/kernel/alloc/allocator_test.rs
delete mode 100644 rust/kernel/alloc/box_ext.rs
create mode 100644 rust/kernel/alloc/kbox.rs
create mode 100644 rust/kernel/alloc/kvec.rs
delete mode 100644 rust/kernel/alloc/vec_ext.rs
base-commit: db8c7e4c95acc68f1e1cd51e0a7f1532c69a23c9
--
2.46.0
next reply other threads:[~2024-09-11 22:55 UTC|newest]
Thread overview: 74+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-11 22:52 Danilo Krummrich [this message]
2024-09-11 22:52 ` [PATCH v7 01/26] rust: alloc: add `Allocator` trait Danilo Krummrich
2024-09-15 15:28 ` Gary Guo
2024-09-15 17:02 ` Danilo Krummrich
2024-09-15 19:22 ` Gary Guo
2024-09-15 20:08 ` Gary Guo
2024-09-15 21:39 ` Danilo Krummrich
2024-09-15 21:37 ` Danilo Krummrich
2024-09-21 15:32 ` [RFC PATCH] rust: alloc: pass `old_layout` to `Allocator` Danilo Krummrich
[not found] ` <CAH5fLgievsJOe7QET+Wh2c0upygv-nhSnOuTN8K_QkruLwOPgA@mail.gmail.com>
2024-09-23 15:20 ` Benno Lossin
2024-09-23 16:13 ` Gary Guo
2024-09-24 13:31 ` Danilo Krummrich
2024-09-24 13:34 ` Danilo Krummrich
2024-09-24 19:58 ` Gary Guo
2024-09-11 22:52 ` [PATCH v7 02/26] rust: alloc: separate `aligned_size` from `krealloc_aligned` Danilo Krummrich
2024-09-11 22:52 ` [PATCH v7 03/26] rust: alloc: rename `KernelAllocator` to `Kmalloc` Danilo Krummrich
2024-09-11 22:52 ` [PATCH v7 04/26] rust: alloc: implement `Allocator` for `Kmalloc` Danilo Krummrich
2024-09-26 13:00 ` Benno Lossin
2024-09-26 13:24 ` Danilo Krummrich
2024-09-26 14:00 ` Benno Lossin
2024-09-11 22:52 ` [PATCH v7 05/26] rust: alloc: add module `allocator_test` Danilo Krummrich
2024-09-11 22:52 ` [PATCH v7 06/26] rust: alloc: implement `Vmalloc` allocator Danilo Krummrich
2024-09-26 13:06 ` Benno Lossin
2024-09-11 22:52 ` [PATCH v7 07/26] rust: alloc: implement `KVmalloc` allocator Danilo Krummrich
2024-09-26 13:07 ` Benno Lossin
2024-09-11 22:52 ` [PATCH v7 08/26] rust: alloc: add __GFP_NOWARN to `Flags` Danilo Krummrich
2024-09-28 18:55 ` Gary Guo
2024-09-11 22:52 ` [PATCH v7 09/26] rust: alloc: implement kernel `Box` Danilo Krummrich
2024-09-26 13:23 ` Benno Lossin
2024-09-28 18:54 ` Gary Guo
2024-09-11 22:52 ` [PATCH v7 10/26] rust: treewide: switch to our kernel `Box` type Danilo Krummrich
2024-09-28 18:59 ` Gary Guo
2024-09-29 14:52 ` Danilo Krummrich
2024-09-11 22:52 ` [PATCH v7 11/26] rust: alloc: remove extension of std's `Box` Danilo Krummrich
2024-09-28 19:00 ` Gary Guo
2024-09-11 22:52 ` [PATCH v7 12/26] rust: alloc: add `Box` to prelude Danilo Krummrich
2024-09-28 19:00 ` Gary Guo
2024-09-11 22:52 ` [PATCH v7 13/26] rust: alloc: implement kernel `Vec` type Danilo Krummrich
2024-09-26 13:47 ` Benno Lossin
2024-09-28 12:43 ` Danilo Krummrich
2024-09-28 13:20 ` Benno Lossin
2024-09-28 19:14 ` Gary Guo
2024-09-11 22:52 ` [PATCH v7 14/26] rust: alloc: implement `IntoIterator` for `Vec` Danilo Krummrich
2024-09-26 13:53 ` Benno Lossin
2024-09-28 19:20 ` Gary Guo
2024-09-11 22:52 ` [PATCH v7 15/26] rust: alloc: implement `collect` for `IntoIter` Danilo Krummrich
2024-09-26 13:57 ` Benno Lossin
2024-09-28 19:27 ` Gary Guo
2024-09-29 15:12 ` Danilo Krummrich
2024-09-11 22:52 ` [PATCH v7 16/26] rust: treewide: switch to the kernel `Vec` type Danilo Krummrich
2024-09-28 19:28 ` Gary Guo
2024-09-11 22:52 ` [PATCH v7 17/26] rust: alloc: remove `VecExt` extension Danilo Krummrich
2024-09-28 19:29 ` Gary Guo
2024-09-11 22:52 ` [PATCH v7 18/26] rust: alloc: add `Vec` to prelude Danilo Krummrich
2024-09-28 19:29 ` Gary Guo
2024-09-11 22:52 ` [PATCH v7 19/26] rust: error: use `core::alloc::LayoutError` Danilo Krummrich
2024-09-28 19:30 ` Gary Guo
2024-09-11 22:52 ` [PATCH v7 20/26] rust: error: check for config `test` in `Error::name` Danilo Krummrich
2024-09-28 19:30 ` Gary Guo
2024-09-11 22:52 ` [PATCH v7 21/26] rust: alloc: implement `contains` for `Flags` Danilo Krummrich
2024-09-28 19:31 ` Gary Guo
2024-09-11 22:52 ` [PATCH v7 22/26] rust: alloc: implement `Cmalloc` in module allocator_test Danilo Krummrich
2024-09-28 19:35 ` Gary Guo
2024-09-29 15:14 ` Danilo Krummrich
2024-09-11 22:52 ` [PATCH v7 23/26] rust: str: test: replace `alloc::format` Danilo Krummrich
2024-09-28 19:37 ` Gary Guo
2024-09-11 22:53 ` [PATCH v7 24/26] rust: alloc: update module comment of alloc.rs Danilo Krummrich
2024-09-28 19:38 ` Gary Guo
2024-09-11 22:53 ` [PATCH v7 25/26] kbuild: rust: remove the `alloc` crate and `GlobalAlloc` Danilo Krummrich
2024-09-28 19:43 ` Gary Guo
2024-09-29 15:17 ` Danilo Krummrich
2024-10-01 13:27 ` Danilo Krummrich
[not found] ` <CANiq72nuZ41eDXkybGBbGRMgJzUOe1rRZioS-amJfH7UV-9cMQ@mail.gmail.com>
2024-10-03 21:53 ` Danilo Krummrich
2024-09-11 22:53 ` [PATCH v7 26/26] MAINTAINERS: add entry for the Rust `alloc` module Danilo Krummrich
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240911225449.152928-1-dakr@kernel.org \
--to=dakr@kernel.org \
--cc=a.hindborg@samsung.com \
--cc=airlied@redhat.com \
--cc=ajanulgu@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=boris.brezillon@collabora.com \
--cc=cjia@nvidia.com \
--cc=daniel.almeida@collabora.com \
--cc=faith.ekstrand@collabora.com \
--cc=gary@garyguo.net \
--cc=jhubbard@nvidia.com \
--cc=lina@asahilina.net \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lyude@redhat.com \
--cc=mcanal@igalia.com \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=wedsonaf@gmail.com \
--cc=zhiw@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox