From patchwork Thu Aug 1 00:02:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13749346 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 AC15EC3DA7F for ; Thu, 1 Aug 2024 00:07:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3AEC36B009F; Wed, 31 Jul 2024 20:07:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 353026B009E; Wed, 31 Jul 2024 20:07:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1F3E96B009F; Wed, 31 Jul 2024 20:07:37 -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 0122E6B009D for ; Wed, 31 Jul 2024 20:07:36 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id B0DB5160816 for ; Thu, 1 Aug 2024 00:07:36 +0000 (UTC) X-FDA: 82401737712.14.1685D2D Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf20.hostedemail.com (Postfix) with ESMTP id 4672E1C002B for ; Thu, 1 Aug 2024 00:07:33 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=o21YFXJi; spf=pass (imf20.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=1722470798; 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=MGpbhRuzRd/0lKmk2mt7wYVrOta4jOEI7JFbV9c47Eo=; b=K+IvtlPrKjQ1Aq6YERxp4k/DrAcixGFSjmMsyzwbd3QDrZNYnfFF6P2cvlBIn0dqjHLLxv DGv5BDoqRWPFtrwgTAnEN/T3pVMNNmrI/jYaUOIrosoLdSmTabLE6scwiFRV1gyOi2wGbU qst8XQnujgUNVSfr1yF9P+2x9n3J0TQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722470798; a=rsa-sha256; cv=none; b=zr11vRWajiGTtePFUgmSKzDr+fStwjtnd2uFXGdVYqb8kOHG5j5L02F7Cv/BO7D6RwnO/c ZVjCfayYpggwxawMl3Eoir3osC1LO3TWce9yXn7Y2N7Rd4spJo+WXwMA9DjUYa+zZa4hLO dqEpX0102nOLLyU/VJCruPP3Rlye/FM= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=o21YFXJi; spf=pass (imf20.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 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 9B4ABCE186A; Thu, 1 Aug 2024 00:07:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BA3D3C4AF0C; Thu, 1 Aug 2024 00:07:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722470850; bh=vpuQqVGdruui4OBsLFoLlveSHdy3R+nqUtK2TdTuy/s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=o21YFXJiH68ZpPo4iNUPdphpPPYfsKEMZCBCPzYfh/vAR0GDVtj+LZXIeMANyxArZ DYmzdHQYBRD/8fWd0BDotHVm7vBfvinT+cAleKi4RuY1WeTjiypprJp8xXL3X7hvC1 wYKQbfVhD0UBCMKzN63FlFziuaYEFp0jtmFStkbopTbpLaMJ5wym9m8dp2rErXd9B8 RWelDLz8llXjECN6gmrbJyaIlQYkO7UPZt4XOMvFuzbu6IZsoyuzfq2yBO7rcbQ7US m2Nd22NIm9TLiDaj+/MGiESojJ1k2/+xGn77288VwrVUKIYRdcS9Yqo1070D8C5Ymt 5RgT0hTUW7mDA== 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 07/25] rust: alloc: implement `KVmalloc` allocator Date: Thu, 1 Aug 2024 02:02:06 +0200 Message-ID: <20240801000641.1882-8-dakr@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240801000641.1882-1-dakr@kernel.org> References: <20240801000641.1882-1-dakr@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 4672E1C002B X-Stat-Signature: r88cpr38r1cbczubr9k5sgs135xzrokc X-HE-Tag: 1722470853-804076 X-HE-Meta: U2FsdGVkX1+c/i7mRMLJ9aZ3fSlRqFsMzpewfPFvKeKC0iVAW9ems7fC8u9Xtly71xwQZMBopouZ4FdDslAjKr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lrWDAkA7VogzyAEVo4Hkaxer6sYuWcHsGzM+S+wDOWL/+NCDEZMxIp2VPVq43xRWrR6KI+bNwygC7yP5bsNLKNTUQgUzly1nab+JqnTNA== 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()`. Signed-off-by: Danilo Krummrich Reviewed-by: Alice Ryhl --- rust/helpers.c | 7 +++++++ rust/kernel/alloc/allocator.rs | 24 ++++++++++++++++++++++++ rust/kernel/alloc/allocator_test.rs | 1 + 3 files changed, 32 insertions(+) diff --git a/rust/helpers.c b/rust/helpers.c index 4c628986f0c9..4ad7dc347523 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -208,6 +208,13 @@ rust_helper_vrealloc(const void *p, size_t size, gfp_t flags) } EXPORT_SYMBOL_GPL(rust_helper_vrealloc); +void * __must_check __realloc_size(2) +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 e9a3d0694f41..1e53f149db96 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -22,6 +22,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. @@ -68,6 +74,10 @@ fn vrealloc() -> Self { Self(bindings::vrealloc) } + fn kvrealloc() -> Self { + Self(bindings::kvrealloc) + } + // SAFETY: `call` has the exact same safety requirements as `Allocator::realloc`. unsafe fn call( &self, @@ -160,6 +170,20 @@ unsafe fn realloc( } } +unsafe impl Allocator for KVmalloc { + unsafe fn realloc( + ptr: Option>, + layout: Layout, + flags: Flags, + ) -> Result, AllocError> { + let realloc = ReallocFunc::kvrealloc(); + + // SAFETY: If not `None`, `ptr` is guaranteed to point to valid memory, which was previously + // allocated with this `Allocator`. + unsafe { realloc.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(