From patchwork Tue Jul 23 18:09:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13740245 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 A89B2C3DA63 for ; Tue, 23 Jul 2024 18:10:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 245886B0083; Tue, 23 Jul 2024 14:10:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1F6236B0085; Tue, 23 Jul 2024 14:10:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 096776B0088; Tue, 23 Jul 2024 14:10:57 -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 DC5396B0083 for ; Tue, 23 Jul 2024 14:10:56 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 6C734C033B for ; Tue, 23 Jul 2024 18:10:56 +0000 (UTC) X-FDA: 82371808512.12.74B1883 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf03.hostedemail.com (Postfix) with ESMTP id B8DAE20028 for ; Tue, 23 Jul 2024 18:10:54 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=mgmy56LJ; spf=pass (imf03.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 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=1721758207; 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=2qQRrka3UryTbYAuX/ijxslDaW3kT3bq8Ah1937YcTQ=; b=CClGY668VEcIjczyTBmYbpjDtm/Rtn2BOTMTQ3sf80RZB/BPhvIWR5pCHdabcJGtSgXnH0 oWwZDB6NWpfSrOqDlWm+nl0wsuXSAOb5L2aZfwJhA1s0KCoiYgwIGyEG3banFq8HbkF95t fuen3F3mMgY3zvlbcRmsF+1ULqDj6X0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721758207; a=rsa-sha256; cv=none; b=Pb4Jr6XchY59Ulk78OAtrCqTTzvHe6kMQ/H8sfeOAjH7f3kWlmHLYet2XUPC409T1F8b7d BrWpCUUBTgWH6X7Y/arstf4DEJpxZWEQLhEKLYtZbd763DICJLJnTiPKbs2Yts3JLD8ffO C85o+xjrMuXGnJOzRWW2ZraHvXkPdRo= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=mgmy56LJ; spf=pass (imf03.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 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 dfw.source.kernel.org (Postfix) with ESMTP id 82B3E60DFD; Tue, 23 Jul 2024 18:10:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 13310C4AF09; Tue, 23 Jul 2024 18:10:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721758253; bh=ki5xi3wh309ewAEmvFrHMp8ZsNfUwPpdMts9frlaya8=; h=From:To:Cc:Subject:Date:From; b=mgmy56LJp97CUmhcB329aE9XUHwKBkmDtwij09R2LbuKlj+gBKG9zIFzHZkdsoZXL JUcsnCBgpliWD6Hj9VoQJ/+yJiZylAmIykvtzIXbVvjt9XET5mdGBibKSMAqns783F LnpxzQFZTzZBqgjwp4maGofG0exKc/6gn+P9Il2kIVpy4DBOl5fkwgSZ87jd2c3Bxu OkVvoyNVQflbtb9F3jfLC3Dn6Wub+l9TWALhoV1AskwZH0TXohxSarBNnLcSO8s1on 6yt+g/C7txLZD0BKH5V6FDkTP4Kzn+p4UUAvlXP/+jT2eQKuXEEYcIT+A5nJQCDNyt lhZu9ySAcoCbw== 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 v2 00/23] Generic `Allocator` support for Rust Date: Tue, 23 Jul 2024 20:09:49 +0200 Message-ID: <20240723181024.21168-1-dakr@kernel.org> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 X-Stat-Signature: 51sjap9okzku8fo6x1asacixsh6abkj6 X-Rspamd-Queue-Id: B8DAE20028 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1721758254-164782 X-HE-Meta: U2FsdGVkX1/14VMIrjlLyfMOLaG2X0oQFnkIRdaAL6BrQGGt0dQv7YKaSJCSg8R90RlKIL+M3Jb2Tn2lTb2GR5n/A18OuhwT1ktpyjDKWITns6aLnrAvXPSex3y08F+HJErhw59Q4NlOQuX4XTLXxJ3TmvW8MXbP+RgJy/Zuxre4yW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3wVXJMDV2DrXjHuXjSKFLBr7+bhpnkA9e3fFZbJLXiQ9S3Tw== 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 in [1], which just 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 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 (23): 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: 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` kbuild: rust: remove the `alloc` crate rust/Makefile | 44 +- rust/exports.c | 1 - rust/helpers.c | 15 + rust/kernel/alloc.rs | 99 +++- rust/kernel/alloc/allocator.rs | 147 +++-- rust/kernel/alloc/allocator_test.rs | 23 + rust/kernel/alloc/box_ext.rs | 56 -- rust/kernel/alloc/kbox.rs | 344 ++++++++++++ rust/kernel/alloc/kvec.rs | 831 ++++++++++++++++++++++++++++ rust/kernel/alloc/vec_ext.rs | 185 ------- rust/kernel/error.rs | 2 +- rust/kernel/init.rs | 49 +- 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/workqueue.rs | 20 +- samples/rust/rust_minimal.rs | 4 +- scripts/Makefile.build | 7 +- 25 files changed, 1713 insertions(+), 419 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: d270beaca6818349b2aed7e6034b800a777087cc