From patchwork Fri Aug 16 00:10: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: 13765288 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 D377AC52D7F for ; Fri, 16 Aug 2024 00:13:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6F36A8D0017; Thu, 15 Aug 2024 20:13:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6A45E8D0002; Thu, 15 Aug 2024 20:13:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 56C638D0017; Thu, 15 Aug 2024 20:13:05 -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 38CBB8D0002 for ; Thu, 15 Aug 2024 20:13:05 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id EE6D741926 for ; Fri, 16 Aug 2024 00:13:04 +0000 (UTC) X-FDA: 82456183488.14.E48869D Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf26.hostedemail.com (Postfix) with ESMTP id 482C6140012 for ; Fri, 16 Aug 2024 00:13:03 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=SQZzPoe9; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf26.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=1723767123; a=rsa-sha256; cv=none; b=YxFdiOAGkpT6GegCsRIcH4W6PZ3zLdGKbfV2KouLlL8YyTs/R0Ps0YA9NzRB5k/GkxrXEg gsT8tzmwr5BZxkGXoIpW313OAOdneKbtRNujOPQU3hdx4QG4VPBNrhokWOYKOKH/BYIQRz 3kqb4WagmKFJx9KFg3zAXBtNDW1DDmk= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=SQZzPoe9; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf26.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=1723767123; 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=yzoXiTotH678Qkh+dnu8Yqkf00zerUhFlQXr98AF0V8=; b=RQ8gE5BM37yTSw4vQeNHGzF2Z6rMASE0YjSnq3/CtNYvq76Obo8uhkVmUcqB4pCpPx66yy 8M6G10i7QqzNLAfLo7aOALKK1G0YB4Sca4b+LMlE+7haOxk0/4LQE1e482+UXQ7/A7kdfc 1dUMq1uC2KYOVA8xUDS37nz6YGaNrhA= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 7777F62066; Fri, 16 Aug 2024 00:13:02 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 48C41C32786; Fri, 16 Aug 2024 00:12:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723767182; bh=aTI9CEsgYyrzlstHqemFJKjddMy0vpbtXP30W2anE+I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SQZzPoe97D+j/WBU1u+3YngiwpWqgLo/aAIXWVe+iB8QGj1vygnofidHkGK6NnMD7 BVS4b9/lkbFii1pXNCEfa8Z2jbz2iaRVZvV1KOdGF2uhpj906RfBaFhIoF2IV14pPP tE2k4VDoEtGSx4xwS5O+8O7H7ScuoGirsRIyNoVp7mqmymOEOQ2TCA244+StOBxnau lkbE7M70hQtDF/cOKYr3eVKg+5KAa1lSq9yRbRQMO0kIItZHqYJ8eEMfxmw9FONO42 4xIdX1eEodyTFZ/kkx9/L/EExQ19aqDlS+3hqOFnUB2C73KrT/RUqrxLLWGunTmEGi utML9JoiTM+dQ== 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 v6 07/26] rust: alloc: implement `KVmalloc` allocator Date: Fri, 16 Aug 2024 02:10:49 +0200 Message-ID: <20240816001216.26575-8-dakr@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240816001216.26575-1-dakr@kernel.org> References: <20240816001216.26575-1-dakr@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 482C6140012 X-Stat-Signature: 7bh8m38ypyhokoxppoe8neeh7yp7f6qe X-Rspam-User: X-HE-Tag: 1723767183-275317 X-HE-Meta: U2FsdGVkX18yNALrVO4d/+Ga/WcrBRtKGYQ4UWZZ+5mfLi0d8j0Cv2tGG41zRVUutSV+RfjgPuNRQ8NpFhOCF91lxVuluHFlzO8t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DZpJ/8ZO/7gaNcQikwhxvJ3KHcB25cdgAkmEGol8J2znRmdSDvWqJqfdvEJyP6mCx2dqd2znbC3U9zVKysJUFJi8xtt4q++Y4Xe/SBFK8VkRn560fRXGisYI4CwROe+8Ivk8cGEVZge8QQNg= 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 | 7 +++++++ rust/kernel/alloc/allocator.rs | 31 +++++++++++++++++++++++++++++ rust/kernel/alloc/allocator_test.rs | 1 + 3 files changed, 39 insertions(+) diff --git a/rust/helpers.c b/rust/helpers.c index 96f8b37a668b..9a7604e3a749 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -256,6 +256,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); + void rust_helper_rb_link_node(struct rb_node *node, struct rb_node *parent, struct rb_node **rb_link) { diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs index 09a322e9168d..0183fbac1ada 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -34,6 +34,15 @@ /// For more details see [self]. pub struct Vmalloc; +/// The kvmalloc kernel allocator. +/// +/// `KVmalloc` attempts to allocate memory with `Kmalloc` first, but falls back to `Vmalloc` upon +/// failure. This allocator is typically used when the size for the requsted allocation is not +/// known and may exceed the capabilities of `Kmalloc`. +/// +/// For more details see [self]. +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. @@ -76,6 +85,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`]. @@ -173,6 +185,25 @@ unsafe fn realloc( } } +unsafe impl Allocator for KVmalloc { + #[inline] + 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(