From patchwork Wed Sep 11 22:52:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13801180 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 EF971EE57DC for ; Wed, 11 Sep 2024 22:55:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8AD016B009A; Wed, 11 Sep 2024 18:55:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 836586B009B; Wed, 11 Sep 2024 18:55:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6D69A6B009C; Wed, 11 Sep 2024 18:55:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 4E7526B009A for ; Wed, 11 Sep 2024 18:55:41 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 04A7A1C5C8D for ; Wed, 11 Sep 2024 22:55:40 +0000 (UTC) X-FDA: 82553966082.16.628BF9D Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf26.hostedemail.com (Postfix) with ESMTP id 5CC79140013 for ; Wed, 11 Sep 2024 22:55:39 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="sh382Oa/"; spf=pass (imf26.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726095311; 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=xpCyIIItvhaSWCjWFHqWOk5Js5blwh8hFjcI+6KQiYA=; b=gCwOhgf7TZFsgMbb5ZnRNM9X8bPXOnTVqvhB+cQD6ER8Zw42ZIUlyWhRG8ajjf7tLFaTPH zD8tfeCEnE2RnWQ6m/KgACB6Jh2xwzteQgp6ycXyahg5E7G0UFW9bmyw+xvuh7uzsNk7Zk goGP7hbPylJTIU9w8znrlkx1MVlpdUw= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="sh382Oa/"; spf=pass (imf26.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726095311; a=rsa-sha256; cv=none; b=VfCabHHDp8yeb3G9T7CQp+Jr7jkajkjdCw/3F51Gje0KMbLgzrfwkbhK3XH6XD5IAD7WbA xCmbKrLAtZqz1Env5lGEeiJeldRLqbEnLtLzCronaP36nBZcXYvSZKmicyG8QBt1JAXjma cY7CrWM3kRBUyao1bpmupPe+CXZxF4Y= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id E9B395C0532; Wed, 11 Sep 2024 22:55:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5A1C7C4CEC5; Wed, 11 Sep 2024 22:55:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1726095338; bh=7ud9iFBT5EcUDGEvKd041Y+gvEKEu7RxRlS2eA0UCRA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sh382Oa/l5uE2UTx21wKubJ27EmXt/WljM5TDLCvic0zWfZUvcq0zujcO0gwtq8UN frSY8H4qkNT32280yR8hCKXi+tJGkeGY5mHW8l5aOYU7V8gfphjJJ6SzFh3O7D/j4d UEgK+aCBsDu+hr6gEL8aTYlWF8nBFRgQaAsgPEnw2kJaHBgWUB+fyqREO+OOM0I+jY mV50Hr2ANdJESRoToUkBIfUIggcAVBckovKxkAdC4hTFOLLzCn75rA54hGe/eF7wxp H6JsuRM16TZWPBhgK0nnGbrkA087+fbnAGT9hNcDPE2UU1L7Y3IY/+sfiWnwIIUTdx 86+gU0pe6L42g== 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 v7 07/26] rust: alloc: implement `KVmalloc` allocator Date: Thu, 12 Sep 2024 00:52:43 +0200 Message-ID: <20240911225449.152928-8-dakr@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240911225449.152928-1-dakr@kernel.org> References: <20240911225449.152928-1-dakr@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: i884dwrw59epqb5tg56p18cpg6q7rbmx X-Rspamd-Queue-Id: 5CC79140013 X-Rspamd-Server: rspam11 X-HE-Tag: 1726095339-899807 X-HE-Meta: U2FsdGVkX1+9I+AoY/blMGmDzzVRjyDsYa91vwVbXNPif5009GcpdL5AdUB0TD3RNrUiu4HeeFffJI3oCVipqswvtT7Xu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/PG6T/hBRA8USB5vSnI/yt9OyW4B2ozyYRSdvCvDgwVoesX04Gc7l7gj2wwbbxGODwKFCEyAL5Mh93JEQCGQbYSfCUjQQ3jXI9oT4iL+YxaGGdVxY+XUwPzJbbxfuAero03bVwphHieWzRyZYQZHFNM= 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 Reviewed-by: Benno Lossin --- rust/helpers/slab.c | 6 +++++ rust/kernel/alloc/allocator.rs | 35 +++++++++++++++++++++++++++++ rust/kernel/alloc/allocator_test.rs | 1 + 3 files changed, 42 insertions(+) diff --git a/rust/helpers/slab.c b/rust/helpers/slab.c index f043e087f9d6..a842bfbddcba 100644 --- a/rust/helpers/slab.c +++ b/rust/helpers/slab.c @@ -7,3 +7,9 @@ rust_helper_krealloc(const void *objp, size_t new_size, gfp_t flags) { return krealloc(objp, new_size, flags); } + +void * __must_check __realloc_size(2) +rust_helper_kvrealloc(const void *p, size_t size, gfp_t flags) +{ + return kvrealloc(p, size, flags); +} diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs index 0f2bc702e8e4..a5d7e66a68db 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 requested 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`]. @@ -194,6 +206,29 @@ unsafe fn realloc( } } +// SAFETY: `realloc` delegates to `ReallocFunc::call`, which guarantees that +// - memory remains valid until it is explicitly freed, +// - passing a pointer to a valid memory allocation is OK, +// - `realloc` satisfies the guarantees, since `ReallocFunc::call` has the same. +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(