From patchwork Thu Aug 1 00:01:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13749339 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 752ADC3DA7F for ; Thu, 1 Aug 2024 00:06:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 047E56B007B; Wed, 31 Jul 2024 20:06:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F138F6B0082; Wed, 31 Jul 2024 20:06:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB3326B0083; Wed, 31 Jul 2024 20:06:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id C0D566B007B for ; Wed, 31 Jul 2024 20:06:55 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 5239A4086A for ; Thu, 1 Aug 2024 00:06:55 +0000 (UTC) X-FDA: 82401735990.20.F56DC02 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf24.hostedemail.com (Postfix) with ESMTP id B12E518000F for ; Thu, 1 Aug 2024 00:06:53 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=TXUJelO7; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf24.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=dakr@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722470768; a=rsa-sha256; cv=none; b=8iXHffxGrEyFVM6thfhShDWKCw0O0REsZFPWI2rdjPMGI7w5ZpC3uw45+zssYzo4wv39bX c5sdR9+GTLsFuKQaV00X2AXL6hxho26EyQOJo301ujf80sADSlGUWbXpumZQcYKPl6c4Pn KHE4y6qZ/+FiyWeWQmSVr1cSHdRePAM= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=TXUJelO7; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf24.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=dakr@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722470768; 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=qWo78EhI3rceOh9HW4wFdlXFTx6EAKp0c/qtu/H8QUs=; b=aydKCx6MtosC5/ltRvntIAfStHFEN+XfqKJGylRUBGdGbkoErvIiMPFv50bZ6cDqdxDMeE cOOe3Os1AWfAsnwlWM3ICfzErhybxHd00Hlj8ueMkI7t2L6uEQFpeGUGo/exa+ipDewgpv bG+VX0UFj2VLkVI4k21Wcm7CQ5a9Okk= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id A16ED62569; Thu, 1 Aug 2024 00:06:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4CCDBC116B1; Thu, 1 Aug 2024 00:06:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722470812; bh=IRxbzLwh8BnLAQZ/qRxPBDb50SXvHiJg+HVmjPu4N+0=; h=From:To:Cc:Subject:Date:From; b=TXUJelO7b8OtoI20SuAqbmQV/LqtOEo8lq2/2feGq7apkml4s2IHHa5ENEPIqkfdz e5twvtWtJRVKJyFn2mRKfMXD4Vl5y2H0BwSDRprZTjRAMeq8+Hkh6I1JO1I+Vv5gVN NP+byJIReArdl38kN6/RXgw7GimUynSKtBRNHTN/rphFZZcmlutcINQ82lYPzrPyQi G0HUfv9Uwm251cjDejh8plkETYDeAcJA7u4Y1/74l/FYlOoqCa1tpQIloKto5uWt+e wrLJWpThG4j2JVkTudldKKtKIM2YBcabVthknRvOmxeROLu2597KpQ6giJZYusqwob ku30/KQ8AQUHA== 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, acurrid@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 v3 00/25] Generic `Allocator` support for Rust Date: Thu, 1 Aug 2024 02:01:59 +0200 Message-ID: <20240801000641.1882-1-dakr@kernel.org> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: B12E518000F X-Stat-Signature: k6pumz6eqd6neogd8r5rakyxu5o3fmku X-Rspam-User: X-HE-Tag: 1722470813-406001 X-HE-Meta: U2FsdGVkX1/GM6Xqjs+gmQzsbx8qFKCH0p5/Jzi+VZLfNDergOYVPuzlnTksrViBu00PCjZR5Vbhm4dJ/NEgUJeV+/CxdWNgG7vT4A9gLsEtkIwkAaiQWESSR5vn+Ws+9Ah/oZILGOn173R+frFwm9xIDF3srxJaOX91I5MGAKY9JQK2agIBvcSHJTI2NhwmTzb3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qdo8FHp2elPOcJmA2mdo5FvtkxDNw== 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 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 (25): 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: types: implement `Unique` 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: import kernel `Box` type in types.rs rust: alloc: import kernel `Box` type in init.rs 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: alloc: remove `GlobalAlloc` and `krealloc_aligned` rust: error: use `core::alloc::LayoutError` rust: str: test: replace `alloc::format` rust: alloc: update module comment of alloc.rs kbuild: rust: remove the `alloc` crate rust/Makefile | 44 +- rust/exports.c | 1 - rust/helpers.c | 15 + rust/kernel/alloc.rs | 101 +++- rust/kernel/alloc/allocator.rs | 147 +++-- rust/kernel/alloc/allocator_test.rs | 21 + rust/kernel/alloc/box_ext.rs | 56 -- rust/kernel/alloc/kbox.rs | 340 +++++++++++ rust/kernel/alloc/kvec.rs | 847 ++++++++++++++++++++++++++++ rust/kernel/alloc/vec_ext.rs | 185 ------ rust/kernel/error.rs | 2 +- rust/kernel/init.rs | 56 +- rust/kernel/init/__internal.rs | 2 +- rust/kernel/lib.rs | 1 - rust/kernel/prelude.rs | 5 +- rust/kernel/str.rs | 78 ++- 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 | 192 ++++++- rust/kernel/uaccess.rs | 15 +- rust/kernel/workqueue.rs | 20 +- samples/rust/rust_minimal.rs | 4 +- scripts/Makefile.build | 7 +- 26 files changed, 1735 insertions(+), 431 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: c0d669a6bbe161a3afa26dea3d8491863e352e59