From mboxrd@z Thu Jan 1 00:00:00 1970 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 21274CF884B for ; Fri, 4 Oct 2024 15:42:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8BFC26B032C; Fri, 4 Oct 2024 11:42:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 86FDA6B03FB; Fri, 4 Oct 2024 11:42:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 737426B03FC; Fri, 4 Oct 2024 11:42:10 -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 560E76B032C for ; Fri, 4 Oct 2024 11:42:10 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 00C68ACB9B for ; Fri, 4 Oct 2024 15:42:09 +0000 (UTC) X-FDA: 82636336020.25.A016BEE Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf06.hostedemail.com (Postfix) with ESMTP id 1EAF9180009 for ; Fri, 4 Oct 2024 15:42:06 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=AOjdlJW2; spf=pass (imf06.hostedemail.com: domain of dakr@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728056461; 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=xhjbsQGjBlahDdxIT7MeJGpmaeRPpNzW/ebao7z9mfY=; b=HN+3ERF3dYl3ikovXFdwXDj1GZU99lCFrueRPILaqfn5gN2S7W5g7m2IP4TT+P3a4/CrEc 3YOSaeGy4+v0K7AacKPtCLnJRLI1YroOGpYSDl1OYlKej6iRwMz2FR6MJ2t77ywbaFetIC 4m5dJK157YV4RNhqSl1Le0OIzR9s22o= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=AOjdlJW2; spf=pass (imf06.hostedemail.com: domain of dakr@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728056461; a=rsa-sha256; cv=none; b=tsmuA7lBOwwOPrZI7JEkVGHRuvZ/qcdvyjxRqdi95h2nnvL9R6e1lH/6ECp7wttNJZLUJV DghdSeavUlFDXUC++iug2lP0cMT0hBeWl3rigFQQZPf95KgF4oMrQyf8qglUtrYnd2vDU9 MwZpxhflzrrMnNRGTfgUW8pD+S7KiAs= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 6FB09A44BB9; Fri, 4 Oct 2024 15:41:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C2230C4CEC6; Fri, 4 Oct 2024 15:41:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728056525; bh=H08Fx0zYKY9vR6Epfn5M7n7Q/crcjJYGKssXgYFRPm4=; h=From:To:Cc:Subject:Date:From; b=AOjdlJW2ed7kC+61hDlfBf6bLy63eW1g0WvPPX6+ldkYUxdsL7sRfBrlz7DJJm40g WiDBtKOwdhV9yA18QVUhiysy3nzzvp0DUq4lFVJYd05jRwt3SXXoeOz3eWvi/mLkFb TRZwjZQoM08YA93F7P0ksW+LEy9hH9K+u1CTHJXKAB77UZd0kZblE8ofYLPH4d8mpQ FeUxRswPWClUc8S4C5xO/KHPCZeTc7OWU4W4CE3fPcd+vm3+02OAMgrwOT8p3Hobu6 KIMVaJMtQDYNLGc99Sz16KD1ekT8MAFTNCrFVf9UPXTZvq/4KDQPzYk6QKxtDKZsbk M/4uArwaGCNBw== 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 v9 00/29] Generic `Allocator` support for Rust Date: Fri, 4 Oct 2024 17:41:04 +0200 Message-ID: <20241004154149.93856-1-dakr@kernel.org> X-Mailer: git-send-email 2.46.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 1EAF9180009 X-Stat-Signature: ssriguxe6i8wkx4tsxn5gsj7cwzn85rw X-HE-Tag: 1728056526-961488 X-HE-Meta: U2FsdGVkX1++PlHJxvT3UgvZDuCwfKkSF7r/BvqT6KciOUHxTmBGL62xEnPxOR8FJW7GVrSy7wL56RN7kHikrOcKvMPvQdZ+QEOP7OKT6xVNNNZd6wXHDStaZg5/hvENlHN96Gmnk3NU0z4vkNMjEt89wVWJ/E6UMbi21+syLEC+oFMYTwQ6OB2uiADO5vbomKiWQ5lwgZfGOnlzf6voCA5m/cZhHDMGb0Zk8vTZ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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-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 v9: - rebase onto rust-next (lint series) - `Vec`: - add missing invariant comments - fix missing `#![feature(inline_const)]` - add note for `kvec!` that GFP_KERNEL is used for allocation - `ArrayLayout`: fix missing import for `size_of::` - `Box`: fix missing safety comment Changes in v8: - rebase onto rust-next (v6.12-rc1), including the -mm dependencies from [1] - introduce `ArrayLayout` and rework the `Allocator` trait to take an `old_layout` argument - split up "rust: alloc: implement `Allocator` for `Kmalloc`" in three separate patches (introduce `ReallocFunc`, make allocator module public and implement `Kmalloc`) - wrap `impl_slice_eq!` pattern in `$()*` - couple of style changes suggested by Benno and Gary 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` 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)?; Benno Lossin (1): rust: alloc: introduce `ArrayLayout` Danilo Krummrich (28): rust: alloc: add `Allocator` trait rust: alloc: separate `aligned_size` from `krealloc_aligned` rust: alloc: rename `KernelAllocator` to `Kmalloc` rust: alloc: implement `ReallocFunc` rust: alloc: make `allocator` module public 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 | 150 ++++- rust/kernel/alloc/allocator.rs | 210 +++++-- rust/kernel/alloc/allocator_test.rs | 95 +++ rust/kernel/alloc/box_ext.rs | 89 --- rust/kernel/alloc/kbox.rs | 456 ++++++++++++++ rust/kernel/alloc/kvec.rs | 914 ++++++++++++++++++++++++++++ rust/kernel/alloc/layout.rs | 91 +++ 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 | 2 +- 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 | 4 +- scripts/generate_rust_analyzer.py | 11 +- 35 files changed, 2025 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 create mode 100644 rust/kernel/alloc/layout.rs delete mode 100644 rust/kernel/alloc/vec_ext.rs base-commit: 7f4a2c6cacb0efad2bd9ae5062ab1b75e13c6c1f -- 2.46.1