From patchwork Mon Aug 5 15:19:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13753709 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 1E6CAC3DA7F for ; Mon, 5 Aug 2024 15:20:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 442936B007B; Mon, 5 Aug 2024 11:20:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3F2F16B0082; Mon, 5 Aug 2024 11:20:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2BA5A6B0085; Mon, 5 Aug 2024 11:20:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 0DEC76B007B for ; Mon, 5 Aug 2024 11:20:19 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 0D0A8A1BC6 for ; Mon, 5 Aug 2024 15:20:18 +0000 (UTC) X-FDA: 82418552916.22.9F3F661 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf13.hostedemail.com (Postfix) with ESMTP id B4B342002D for ; Mon, 5 Aug 2024 15:20:14 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=rjRANWf8; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf13.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=1722871146; 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-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=dMguz2rdyOL8lrzXMry8RBUKmljU6H9zCxKj5S2H+dU=; b=YFe3NdnmFzIJee2UoBKisIZBBfOTY41Z/8A+gxJLlElQpCJ1/2vAaYrOJR0J1q58P8WeSd T8y6SJ0KybGWwnfomkeE1JrrS91A3W8WYtK1cKZmDzO/1dDy9cww3Gla84bhPaxmijCBnM GNJzB1S/zrI8oP60H/47vNR4Hai/XrA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722871146; a=rsa-sha256; cv=none; b=XjJW7nbUhPCUiTnCMXuAQvYFKJNnGfe/CMfW7SgksFtFQnFMto2d46PAcWxP391bgl+K0L naosibKgSJgqDXuSaFNGTCL8ACRfC1BCSrdkwJ2UDE7Pa4KHO94mQFG3cdLT0o74Qxvuym KewrjwhZP/tXeIf4/hf10Vy9+Kkh+Uo= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=rjRANWf8; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf13.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=dakr@kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 9FB8260CEE; Mon, 5 Aug 2024 15:20:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 42629C32782; Mon, 5 Aug 2024 15:20:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722871213; bh=QrXtEg0wmyvs1JTx22APAXm/GrJ0/eFkNJxtPGyB9rw=; h=From:To:Cc:Subject:Date:From; b=rjRANWf85K84MD9P6KZ6wg7jZbtGjIEWGXaYh2NPAL45yGGOpuq+MvSKiu9Hyhjkp 1edEeqMGSxzeT8b2PQrsiriADeKECUPaQdCvZ4+jwgw68MoViaBAx2YpGGnFKu0MTK cdAs+PrFkK/FFiVX1wZk1+V8rfnp+yWdQ0L2BS6n6hNB418+wEI31VJ8PNOwwQUxLK F4D6Y2wnMeP6MkG1aQnDVF2O+QsuYjXEuEi7jY6Ka12SksUf0fbZR+i+xWNIcMBUTO vmcM2ACHEpI6/XGqDoRoTOENv/mPPjiD47aA0v46Mpkr3f8EL6uLsFnPd91J+z6I/e TWXe2H5+V4jfA== 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 v4 00/28] Generic `Allocator` support for Rust Date: Mon, 5 Aug 2024 17:19:19 +0200 Message-ID: <20240805152004.5039-1-dakr@kernel.org> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: B4B342002D X-Stat-Signature: gybz6phuduk7kkcoshigg4bnirq65x7z X-Rspam-User: X-HE-Tag: 1722871214-633313 X-HE-Meta: U2FsdGVkX1/YBvRRC9+1vmip9Z4tOYanKSTYLw5AX9av/b8Vj7oqnkvEUrQLf9RfvkKm9SNQ38e9ITyW5KpZWiA7zWbJhJNusUZ7DN0QdpWHKiIjfdwO+W+vnNZscLdiEoEVJ8Pv/W9C47LNlChsTi0Brq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t/03jiQ4m+YVXx6U4vtxHS1PtRDRmHUgMV+CnR27N4OWLJ7SEMrye/nLHzB1DdmAgrTEdxmwlq/VGoK/SRTN8KFFg 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 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 (28): 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: 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 rust/Makefile | 44 +- rust/exports.c | 1 - rust/helpers.c | 16 +- rust/kernel/alloc.rs | 114 +++- rust/kernel/alloc/allocator.rs | 167 ++++-- rust/kernel/alloc/allocator_test.rs | 98 ++++ rust/kernel/alloc/box_ext.rs | 56 -- rust/kernel/alloc/kbox.rs | 344 +++++++++++ rust/kernel/alloc/kvec.rs | 855 ++++++++++++++++++++++++++++ rust/kernel/alloc/vec_ext.rs | 185 ------ rust/kernel/error.rs | 6 +- 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 | 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 | 222 +++++++- rust/kernel/uaccess.rs | 15 +- rust/kernel/workqueue.rs | 20 +- samples/rust/rust_minimal.rs | 4 +- scripts/Makefile.build | 7 +- 26 files changed, 1863 insertions(+), 417 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