From patchwork Fri Aug 16 00:10:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13765281 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 14968C52D7D for ; Fri, 16 Aug 2024 00:12:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8A1FC6B027A; Thu, 15 Aug 2024 20:12:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 877A66B027B; Thu, 15 Aug 2024 20:12:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 718876B027C; Thu, 15 Aug 2024 20:12:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 545326B027A for ; Thu, 15 Aug 2024 20:12:33 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id D6878A90A1 for ; Fri, 16 Aug 2024 00:12:32 +0000 (UTC) X-FDA: 82456182144.11.C1ADB0C Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf24.hostedemail.com (Postfix) with ESMTP id AAC3A180014 for ; Fri, 16 Aug 2024 00:12:29 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=sgsYE1Mm; spf=pass (imf24.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723767076; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=KTFA+UZ4xpcqqR3j+4PAXf7rxzYhYz+8GIcnq86uUNk=; b=3e0xQsT5q5PCBM2PsAJConjZlg2xIUMFI2Q1+ddEoDXJDrC6dJaztPB4d1hD/RUizvSWmz f5j9PDES+KqKiua+P+UfC01c0MRQ+5/FM/b6bTGhGTdYdIjNVzfQRon3t3Nd5KsNRDnmlY sH1f7OBpd+8KK8mBxuPlRpxpRf7KjM0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723767076; a=rsa-sha256; cv=none; b=G40v0deuxQAghIfDHXjcljTPHqfdGTnzI54HUhHpDb3pgPHvac+LBV2+nrcKed9u0+YgP0 p2XQKImt/TH2AH8S4gg5Nesx2e64PKfIoBkSz+2sTvrKg/mfyUpfFsWji9U95/qmV5svSd 04vpMw+kiX7Pi/eOFEj8Qy/i7FWpfwM= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=sgsYE1Mm; spf=pass (imf24.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id CAEA7CE1E63; Fri, 16 Aug 2024 00:12:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 21F35C32786; Fri, 16 Aug 2024 00:12:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723767145; bh=GvB4H22h4QOyHHefP0mnM9E2EBFfbvQezXUvW6AQ6SA=; h=From:To:Cc:Subject:Date:From; b=sgsYE1Mmtxil7/a6m3cG2btLqmK6ozsLre3AI7CyeIiHtX9LBN44IKFRbbVzuesFM m91KQjAnMURXZHVKLcJd5TAuc56RLjMB6AYtnLgNTd8S39gtk2qcf4JH41EXoony4i KJIYCrAHG/so0u3tM/DTzlKwc8sLq6pYIlD6ReygtL88wPLqV17g7/pc1iQucBvc2w R7Sctawot6rvBA0e9bF3floY/7x69IZcXXFgh6TfaVvTK48aCpR/VfH563Xfur4NJI OzZ2IgoLxZBM72horxdefwdBcnPpdJYBd7NVBxN8nKew0NJHSz0jkrPLg460NnI5Ex XGfyXeGFi55eg== From: Danilo Krummrich 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 Subject: [PATCH v6 00/26] Generic `Allocator` support for Rust Date: Fri, 16 Aug 2024 02:10:42 +0200 Message-ID: <20240816001216.26575-1-dakr@kernel.org> X-Mailer: git-send-email 2.46.0 MIME-Version: 1.0 X-Stat-Signature: shwpzkyg88jzpgpekio6csfsxx1g41sp X-Rspamd-Queue-Id: AAC3A180014 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1723767149-671589 X-HE-Meta: U2FsdGVkX1/jCNvRJJ6Vu56sW/H+Ivs+Gys6jMv5k5DMFTdEG6uXZyDyUx156EMS7tNMCebz0lWWFxYfntOH5SOiAeYw6vpsF6fKwBHMyuOGtGcOk4BnPiBIy4vMfqQJ5josT7ErmmeZTa6ZcwXz9REfjtli9ovR94Oekihj4RMkpzKslKul5kSZ+V9mf9tqMt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0Fa09xtHm7b37xokq47l0tFQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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` and `Vec` types. Additionally, this series adds the following type aliases: ``` pub type KBox = Box; pub type VBox = Box; pub type KVBox = Box; pub type KVec = Vec; pub type VVec = Vec; pub type KVVec = Vec; ``` 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 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` 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> 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 rather than specifically for `KBox` - 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` for `Allocator::free` and `Option>` 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` -> `Box` and `KVec` -> `Vec` and provide type aliases `KBox`, `VBox`, `KVBox`, 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::::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 `BoxExt` extension 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 | 44 +- rust/bindings/bindings_helper.h | 1 + rust/exports.c | 1 - rust/helpers.c | 15 + rust/kernel/alloc.rs | 142 ++++- rust/kernel/alloc/allocator.rs | 173 ++++-- rust/kernel/alloc/allocator_test.rs | 185 ++++++ rust/kernel/alloc/box_ext.rs | 80 --- rust/kernel/alloc/kbox.rs | 480 +++++++++++++++ rust/kernel/alloc/kvec.rs | 891 ++++++++++++++++++++++++++++ rust/kernel/alloc/vec_ext.rs | 185 ------ rust/kernel/error.rs | 6 +- rust/kernel/init.rs | 93 +-- rust/kernel/init/__internal.rs | 2 +- rust/kernel/lib.rs | 1 - rust/kernel/prelude.rs | 5 +- rust/kernel/rbtree.rs | 34 +- 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 | 30 +- 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 +- 32 files changed, 1973 insertions(+), 539 deletions(-) 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: f005c686d6c1a2e66f2f9d21179d6b6bd45b20e2