From patchwork Mon Aug 12 18:22:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13760923 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 4804BC52D7C for ; Mon, 12 Aug 2024 18:24:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CDC6D6B0095; Mon, 12 Aug 2024 14:24:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C8A336B00AD; Mon, 12 Aug 2024 14:24:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B042D6B00AE; Mon, 12 Aug 2024 14:24:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 907BB6B0095 for ; Mon, 12 Aug 2024 14:24:43 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 57A991C1BD1 for ; Mon, 12 Aug 2024 18:24:43 +0000 (UTC) X-FDA: 82444419246.10.C945D44 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf26.hostedemail.com (Postfix) with ESMTP id A770A140012 for ; Mon, 12 Aug 2024 18:24:41 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=NFvp3rUV; 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=1723487025; a=rsa-sha256; cv=none; b=z8qZQKDpT/puKSvw5LsuealndvD6akvy9mIoDkIM0zOdcsNNMWh8yqm9szDK22qzDqtkMr f0dPX87DC5sPxh4RBOW0a/H703fXd9iYcJbdTh2pk3+phlA2/joAx2Q1UxWuheyMM5JP6l W1rUkfNEJFjPLf7vaci2dEM3obsJZ24= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=NFvp3rUV; 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=1723487025; 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=+PZFzOtqSsDrw+fHKr6d50Q2ufzJqro0ehZaXJdYeWo=; b=lW6XKoXwvm8jQ6KCxC+mnhOupNByhm6BifQh0B+gVbI0D9SZVjcCSLHrKcUtPJ/48StzQE ZlxH2ce+ONUu/kRyFimIctQ/1gRRDT6LTxmC2xJ0976Ndg+czyXae3Bs6JjzP2YwusZYw8 1I/s+ygIJkI2FyzWITKPkBsSQy9jOWo= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id D3E296146C; Mon, 12 Aug 2024 18:24:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 975EFC4AF0F; Mon, 12 Aug 2024 18:24:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723487080; bh=h5pfzEktHmh1N0L77l+cZ8kzDdEbRgd13sQHut0w7XY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NFvp3rUVlsJ+UIxsUGlMKE8VTsD2Aq8HQxJ6wXype4vyAHL2JtkK3mOcnqC2DvnGf RmFkZ/Axmc1yYu1S60+J355btuuhvCl8k6va48DkEb1AE+kv7+2ACKRPMVx/RA1Grm 8ju+i4zFpn3C6CpUsSopqqg9VC2Kra0gRKGL1W4dTZsSFnQcsgfnAGX3XZd6bP7Y1Q XOhWkTqlAKYoyI90KBpS3koeohXNoO4UW7OLvdIrC0VU1eFvFuJRTaJnyUP/w1XMaZ +Zcszs4mggLiSYZhQUaYYRApDVKuKQNFVkwynM6kBwM8NIb1v+WthEPX/Jcg0D/T4v rZ4s5Ccnx/0jw== 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 06/26] rust: alloc: implement `Vmalloc` allocator Date: Mon, 12 Aug 2024 20:22:52 +0200 Message-ID: <20240812182355.11641-7-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-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: A770A140012 X-Stat-Signature: 667cef14tjz4sj6gemgdnezps1zg9p15 X-Rspam-User: X-HE-Tag: 1723487081-453765 X-HE-Meta: U2FsdGVkX19h+hHs+scpWw9PlVX2rFhu3iIwHPgiIWpkdbxdHy9D+TajVnKb7XOKJPs4+CchPWpLvv89lDOV8sANW5gQ5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g+oMGnpLE/HD5KhBmMd6mnwnr2b1ZycfWeYzBHPP/sB0mvYf5QwwYMbFhMbZX52siB/BeGwXxBp0Y3Aqo3Gt1hrqfBg2CladyBeY79THmlThAh2XG33bjjy566kH7scIejCvoNJ/68LT6wNHfam1VsBd4PO31CALEe4= 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 `Vmalloc`, the kernel's virtually contiguous allocator, typically used for larger objects, (much) larger than page size. All memory allocations made with `Vmalloc` end up in `vrealloc()`. Reviewed-by: Alice Ryhl Signed-off-by: Danilo Krummrich --- rust/helpers.c | 7 +++++++ rust/kernel/alloc/allocator.rs | 28 ++++++++++++++++++++++++++++ rust/kernel/alloc/allocator_test.rs | 1 + 3 files changed, 36 insertions(+) diff --git a/rust/helpers.c b/rust/helpers.c index 9f7275493365..7406943f887d 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -199,6 +200,12 @@ void *rust_helper_krealloc(const void *objp, size_t new_size, gfp_t flags) } EXPORT_SYMBOL_GPL(rust_helper_krealloc); +void *rust_helper_vrealloc(const void *p, size_t size, gfp_t flags) +{ + return vrealloc(p, size, flags); +} +EXPORT_SYMBOL_GPL(rust_helper_vrealloc); + /* * `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 b46883d87715..fdda22c6983f 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -9,6 +9,7 @@ use crate::alloc::{AllocError, Allocator}; use crate::bindings; +use crate::pr_warn; /// The contiguous kernel allocator. /// @@ -16,6 +17,12 @@ /// `bindings::krealloc`. pub struct Kmalloc; +/// The virtually contiguous kernel allocator. +/// +/// The vmalloc allocator allocates pages from the page level allocator and maps them into the +/// contiguous kernel virtual space. +pub struct Vmalloc; + /// 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. @@ -55,6 +62,9 @@ impl ReallocFunc { // INVARIANT: `krealloc` satisfies the type invariants. const KREALLOC: Self = Self(bindings::krealloc); + // INVARIANT: `vrealloc` satisfies the type invariants. + const VREALLOC: Self = Self(bindings::vrealloc); + /// # Safety /// /// This method has the same safety requirements as `Allocator::realloc`. @@ -132,6 +142,24 @@ unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { } } +unsafe impl Allocator for Vmalloc { + 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!("Vmalloc 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::VREALLOC.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 4785efc474a7..e7bf2982f68f 100644 --- a/rust/kernel/alloc/allocator_test.rs +++ b/rust/kernel/alloc/allocator_test.rs @@ -7,6 +7,7 @@ use core::ptr::NonNull; pub struct Kmalloc; +pub type Vmalloc = Kmalloc; unsafe impl Allocator for Kmalloc { unsafe fn realloc(