From patchwork Fri Aug 16 00:10:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13765287 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 A3682C52D7D for ; Fri, 16 Aug 2024 00:13:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3982A8D0016; Thu, 15 Aug 2024 20:13:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 348278D0002; Thu, 15 Aug 2024 20:13:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1EA068D0016; Thu, 15 Aug 2024 20:13:00 -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 F3D498D0002 for ; Thu, 15 Aug 2024 20:12:59 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 94273C18B4 for ; Fri, 16 Aug 2024 00:12:59 +0000 (UTC) X-FDA: 82456183278.10.671C177 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf15.hostedemail.com (Postfix) with ESMTP id DF3A1A0013 for ; Fri, 16 Aug 2024 00:12:57 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ivbEzzrL; spf=pass (imf15.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 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=1723767141; a=rsa-sha256; cv=none; b=1sgiKJIWeK+EJyDdZ3SEory/jsVsPRSzBGz3kVWlKZq3ZgOYydVYJ77iXMFCniHe8dn87D 7ADNslu/xjyWA1QDB2xSKzA/eltNK/ZKx79YuNiGZ6YXPuQwI+8Zpah8sho+4Ly0Be6UY9 +r9JjL664nMnDguypjJJaGIuvvMMIS0= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ivbEzzrL; spf=pass (imf15.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 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=1723767141; 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=NAdps/r0odAShDVfs7UjImCjLgMSC6fLOI+bzykHcRg=; b=k3I4R4lkYd63Mle5iMGJChUJQ5stZjO2/ZxuzLA9I8C652uX/07c3ulfZOZrHVuswVXN5t scM9Zsm1esK+ALuHlMIHzRCFQaVVSI4gMwB7lMDHbA2sCGFjmWQujyvOmN6P8YDrrU6dIW WNS1rwh4yPTidn5C84CuSgagJn/7AGU= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 26E6E62064; Fri, 16 Aug 2024 00:12:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F230AC32786; Fri, 16 Aug 2024 00:12:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723767176; bh=hX4ulOq19dU7ZAikDhvlf8QJavajKpr4Y1F7LrLQguk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ivbEzzrL5ZD6557M5uq0jSgcjAt5wWYtmCbIQ9K7oSjxLS807foNstdYN4j+jMGpP lSwcGuFd6VKoIx0D3B/gSvm3oc582Z6pZRPv+S80DVcnUvzIlmM4YKWx39BMgSzj6D dMOLVSiWZJBF3w2cu5cgWBghkp8xwFSWkr0HKatrCBRK+S20NXI8Y6ovt29z/BYZhx /ZgS/fANOeqkwB8g+kl175jYA+jQf8rGp5C0G1/4w3805JTs39UyDfPmK2t28nPuFV UVutHfCih2dJpohwcLfc/JOYLznPNMIJvo7XMk60wrDDhIsPGogxBLJ3n8D0wpaXTA s4ye2rlW5fzYw== 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 06/26] rust: alloc: implement `Vmalloc` allocator Date: Fri, 16 Aug 2024 02:10:48 +0200 Message-ID: <20240816001216.26575-7-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-Stat-Signature: ppu585ifuequxo13au69ytoec8gdtmiu X-Rspamd-Queue-Id: DF3A1A0013 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1723767177-550032 X-HE-Meta: U2FsdGVkX18laI46UKqNC+g1ibLTd+O4qMj/eN+wXJOmBQHRd5+rj8nUncJ0P/PZu2g38xUzSrzEMR99M2beTKJbvNo2BCq/m9nftPmkjDuPD1aqx8F/Iq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ITI68T82FzBWuPo++JOYM/B5rFg4QX5uPid72ENuEiGQ9xCBG9wESe4knPx6ktTUnRnGx5YWuzwYrgV2oowT/onRr0kIyg7g9jMf/WYnMW5iFWYb3/0uk8KyhtxOI= 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 | 8 ++++++++ rust/kernel/alloc/allocator.rs | 32 +++++++++++++++++++++++++++++ rust/kernel/alloc/allocator_test.rs | 1 + 3 files changed, 41 insertions(+) diff --git a/rust/helpers.c b/rust/helpers.c index 41df5b1a6de6..96f8b37a668b 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include @@ -248,6 +249,13 @@ rust_helper_krealloc(const void *objp, size_t new_size, gfp_t flags) } EXPORT_SYMBOL_GPL(rust_helper_krealloc); +void * __must_check __realloc_size(2) +rust_helper_vrealloc(const void *p, size_t size, gfp_t flags) +{ + return vrealloc(p, size, flags); +} +EXPORT_SYMBOL_GPL(rust_helper_vrealloc); + 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 78e7d5488843..09a322e9168d 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -15,6 +15,7 @@ use crate::alloc::{AllocError, Allocator}; use crate::bindings; +use crate::pr_warn; /// The contiguous kernel allocator. /// @@ -24,6 +25,15 @@ /// For more details see [self]. pub struct Kmalloc; +/// The virtually contiguous kernel allocator. +/// +/// `Vmalloc` allocates pages from the page level allocator and maps them into the contiguous kernel +/// virtual space. It is typically used for large allocations. The memory allocated with this +/// allocator is not physically contiguous. +/// +/// For more details see [self]. +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. @@ -63,6 +73,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`]. @@ -141,6 +154,25 @@ unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { } } +unsafe impl Allocator for Vmalloc { + #[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!("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(