From patchwork Mon Aug 12 18:22:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13760924 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 27A2EC52D7C for ; Mon, 12 Aug 2024 18:24:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BA2086B00AD; Mon, 12 Aug 2024 14:24:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B789A6B00AE; Mon, 12 Aug 2024 14:24:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A40A16B00AF; Mon, 12 Aug 2024 14:24:52 -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 86FE06B00AD for ; Mon, 12 Aug 2024 14:24:52 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 4D11880339 for ; Mon, 12 Aug 2024 18:24:52 +0000 (UTC) X-FDA: 82444419624.20.0758A6B Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf09.hostedemail.com (Postfix) with ESMTP id 005F6140017 for ; Mon, 12 Aug 2024 18:24:49 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=R2PRrRzm; spf=pass (imf09.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723487055; a=rsa-sha256; cv=none; b=F1MoUyY5flQcOxwfaL/1amrjlko3DHAZEE3D8NGmYe6vKGlWKb60nUY1Q1CG8txtKV/1K+ mN2scEeO8x1Xb8oPVZJdyIQ7yvG4KlwbpGXzIH9MJYKOpXfBoUFDwxlDua+eO27OsuG6bR 3AKNwdVFbXw4pRCNJIegj6eGJxVufRo= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=R2PRrRzm; spf=pass (imf09.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=1723487055; 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:in-reply-to:references:references:dkim-signature; bh=wm78gxg5JM+3D7Rf9YN1GVNjw0uLawwkICw7qG6K2GU=; b=HCgumA0ReFAGDSIF97lL4z3phGPvBbTITBjYiV4bttzT4qZpDL9OTvRtQnK7WC1jrgpQbg vxiFKchshrdpdkDPVo96r8+xvbByrKhaKVVlk0plBMIF9IKHRYGIPeYAwnubmOUP2WP3rx QbOf1e503OBj6SSdefw/SPXgRAfj2es= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id DD181CE1037; Mon, 12 Aug 2024 18:24:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 02FB5C4AF0C; Mon, 12 Aug 2024 18:24:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723487085; bh=ITHOVb+qmodDUzbraOhMnm/IPfqxkdSYWt9L0/6UUHg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=R2PRrRzm+sxi6h0mbRIbj3sVO7A7/Rs9qf3CP6nSz1dh8f0SNA64zrjrpVVLruHlQ 3BFVUU3jvJcUVpqzGx4jNLWpDl+hSQupHUBTsfko5zSpxspOjFOgtOUUF6QLp4Eu2R P2Jwf3jgK2PvwPhtagm2rdrcySOoF9SrhGJL6HMf4A9loJ4iyl2lfIaFtfpx7L7asW tBaeenOsbRAjdWleNZtLtRAzjqOFlzIb6Hq34YSFJ6Ol265wgDMcjQOv+/pj/DQ+nK RVxXOrl8/btgq+is4+yhBy3Pohr5grqiaqeVr6zRrI98GAQel/Rdo/n6yQDvwAgyEi vs0Zb3VCnxxqQ== 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 v5 07/26] rust: alloc: implement `KVmalloc` allocator Date: Mon, 12 Aug 2024 20:22:53 +0200 Message-ID: <20240812182355.11641-8-dakr@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240812182355.11641-1-dakr@kernel.org> References: <20240812182355.11641-1-dakr@kernel.org> MIME-Version: 1.0 X-Stat-Signature: dtzfgtz339ngmfgows1hk13xrrndbwtg X-Rspamd-Queue-Id: 005F6140017 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1723487089-397678 X-HE-Meta: U2FsdGVkX18Aw5DyVx8NKSsEBIw8eUy2SCR6fZ/+h/yYU4U4LLKxahd16Rn0pG/E1c3iWd5Pu7nujv6eMUhOtXsgSELSX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A/WOYgWwlwKebvPrQcQsxh5w5hmtUmHiJ2tPIu2jYw4TjkbdCCrhNc+sMzZNS4SBf1GA2OWHaEw1Uq0mMd8CjJOW4krkOqEG64uzpJiokFLIYA6qRnelM+PL2q/R9kJjhk7kyN2aeTAGPnu7/w5uVe+hF0= 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: Implement `Allocator` for `KVmalloc`, an `Allocator` that tries to allocate memory wth `kmalloc` first and, on failure, falls back to `vmalloc`. All memory allocations made with `KVmalloc` end up in `kvrealloc_noprof()`; all frees in `kvfree()`. Reviewed-by: Alice Ryhl Signed-off-by: Danilo Krummrich --- rust/helpers.c | 6 ++++++ rust/kernel/alloc/allocator.rs | 27 +++++++++++++++++++++++++++ rust/kernel/alloc/allocator_test.rs | 1 + 3 files changed, 34 insertions(+) diff --git a/rust/helpers.c b/rust/helpers.c index 7406943f887d..3285a85765ff 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -206,6 +206,12 @@ void *rust_helper_vrealloc(const void *p, size_t size, gfp_t flags) } EXPORT_SYMBOL_GPL(rust_helper_vrealloc); +void *rust_helper_kvrealloc(const void *p, size_t size, gfp_t flags) +{ + return kvrealloc(p, size, flags); +} +EXPORT_SYMBOL_GPL(rust_helper_kvrealloc); + /* * `bindgen` binds the C `size_t` type as the Rust `usize` type, so we can * use it in contexts where Rust expects a `usize` like slice (array) indices. diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs index fdda22c6983f..243d8c49a57b 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -23,6 +23,12 @@ /// contiguous kernel virtual space. pub struct Vmalloc; +/// The kvmalloc kernel allocator. +/// +/// Attempt to allocate physically contiguous memory, but upon failure, fall back to non-contiguous +/// (vmalloc) allocation. +pub struct KVmalloc; + /// Returns a proper size to alloc a new object aligned to `new_layout`'s alignment. fn aligned_size(new_layout: Layout) -> usize { // Customized layouts from `Layout::from_size_align()` can have size < align, so pad first. @@ -65,6 +71,9 @@ impl ReallocFunc { // INVARIANT: `vrealloc` satisfies the type invariants. const VREALLOC: Self = Self(bindings::vrealloc); + // INVARIANT: `kvrealloc` satisfies the type invariants. + const KVREALLOC: Self = Self(bindings::kvrealloc); + /// # Safety /// /// This method has the same safety requirements as `Allocator::realloc`. @@ -160,6 +169,24 @@ unsafe fn realloc( } } +unsafe impl Allocator for KVmalloc { + unsafe fn realloc( + ptr: Option>, + layout: Layout, + flags: Flags, + ) -> Result, AllocError> { + // TODO: Support alignments larger than PAGE_SIZE. + if layout.align() > bindings::PAGE_SIZE { + pr_warn!("KVmalloc does not support alignments larger than PAGE_SIZE yet.\n"); + return Err(AllocError); + } + + // SAFETY: If not `None`, `ptr` is guaranteed to point to valid memory, which was previously + // allocated with this `Allocator`. + unsafe { ReallocFunc::KVREALLOC.call(ptr, layout, flags) } + } +} + #[global_allocator] static ALLOCATOR: Kmalloc = Kmalloc; diff --git a/rust/kernel/alloc/allocator_test.rs b/rust/kernel/alloc/allocator_test.rs index e7bf2982f68f..1b2642c547ec 100644 --- a/rust/kernel/alloc/allocator_test.rs +++ b/rust/kernel/alloc/allocator_test.rs @@ -8,6 +8,7 @@ pub struct Kmalloc; pub type Vmalloc = Kmalloc; +pub type KVmalloc = Kmalloc; unsafe impl Allocator for Kmalloc { unsafe fn realloc(