From patchwork Tue Oct 1 14:59: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: 13818227 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 2692DCEACDC for ; Tue, 1 Oct 2024 15:01:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B622268000B; Tue, 1 Oct 2024 11:01:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B1274280068; Tue, 1 Oct 2024 11:01:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 98DCB68000B; Tue, 1 Oct 2024 11:01:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 774EE280068 for ; Tue, 1 Oct 2024 11:01:03 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 2FBFE8078E for ; Tue, 1 Oct 2024 15:01:03 +0000 (UTC) X-FDA: 82625346006.26.3706C00 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf07.hostedemail.com (Postfix) with ESMTP id EB14140032 for ; Tue, 1 Oct 2024 15:01:00 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=FEC2PB2q; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf07.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=1727794840; a=rsa-sha256; cv=none; b=mzYzQB6m+jS0naEiTyklUoekV4aluoDQllzkIMH74TYJpQGXNTS1NU+x3UXR7gcGk4VIEo A/2kqzma8sEzJOKapixRJKBIRqZ9x9+ECgdatz5xXSLZ9ZkTx5+47eAxrdJUBIv21rXTnz i07r0GkyaJzcEjrg+dLS+YcTgy5fi04= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=FEC2PB2q; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf07.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=1727794840; 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=WtZZ2bG4XnoH3WGdqtUBe6ZQ6u5YKaVoKQCrrwITC/I=; b=dy04nuUenzz4JTkchlcJdv13Za52e2lxi1OzyyhczR+LHJBjJGiYR5sxTUeg7v8IPNBRVf hTWE4Xm8gSqbakiqrZfor1NBJzOsVY0ykQw6n/j3+MHvvUTd0Y02TdiZAvQxPqaicQyOQd mhwlQIrHEmdXEpTaD3PHq+Iluc7rmBY= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id EF4725C02E0; Tue, 1 Oct 2024 15:00:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B9F07C4CEC6; Tue, 1 Oct 2024 15:00:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727794859; bh=HLcg425F90NHvfln7+CkNFLbyp+1IauQlWGwrTXVWkQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FEC2PB2qqWlaxBrCYs0tyLDHvYQfQynYdKreOHCr6nywYxT6IzPfGdvyLLVujp+1c IDSftsjEYeaOIcOKZp5tq+lv+9qQkWTeM9RUsOZeFduP3EpcAUZfRSbC0APhvmic8K 9NlxqiCYYaxMtFvZhXV8SBZCR4GCD07CTRhc52RKjG3gkNmQ4w+7uS4jrzwBd37x3k pf5ch8ng/WAb8VeDeZmI+Vres6Sbx77xHRWw1bvS2A1qHj9EJeWEGkCR+YviUl2Jn8 00cTL1V7LwXT2aG58SmKOypU6fCUb+q+aHxLoIj3SbF97PG3K4613T1gEFvGesC+er PQvO1BPB05kow== 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 v8 08/29] rust: alloc: implement `Vmalloc` allocator Date: Tue, 1 Oct 2024 16:59:43 +0200 Message-ID: <20241001150008.183102-9-dakr@kernel.org> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20241001150008.183102-1-dakr@kernel.org> References: <20241001150008.183102-1-dakr@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: gp51od114aaabrfh939naou7t5s46r6h X-Rspamd-Queue-Id: EB14140032 X-Rspamd-Server: rspam02 X-HE-Tag: 1727794860-290689 X-HE-Meta: U2FsdGVkX1/o49tlZ7F9DbABDOQAC9rJOjz5rhgT4a6y8fYGaeORho0oppRtBvUJEhDCQEU6qhOUT0xl1wNyuyl59l2w96fuWqDhbkTlxFnybjjZY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1c9RKLcp5pAESwwJRuVXAApYlssSUCSk1sLRSbTXGrVFDXIhO1BEHRNcFGsN4hA4KhydHGxjwyzpopA/MGkaYEnzPqf18jUtF/I9uEtPpcycTW06JqiEChrGRJMlks= 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 Reviewed-by: Benno Lossin Signed-off-by: Danilo Krummrich Reviewed-by: Gary Guo --- rust/helpers/helpers.c | 1 + rust/helpers/vmalloc.c | 9 +++++++ rust/kernel/alloc/allocator.rs | 37 +++++++++++++++++++++++++++++ rust/kernel/alloc/allocator_test.rs | 1 + 4 files changed, 48 insertions(+) create mode 100644 rust/helpers/vmalloc.c diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c index 30f40149f3a9..20a0c69d5cc7 100644 --- a/rust/helpers/helpers.c +++ b/rust/helpers/helpers.c @@ -22,5 +22,6 @@ #include "spinlock.c" #include "task.c" #include "uaccess.c" +#include "vmalloc.c" #include "wait.c" #include "workqueue.c" diff --git a/rust/helpers/vmalloc.c b/rust/helpers/vmalloc.c new file mode 100644 index 000000000000..80d34501bbc0 --- /dev/null +++ b/rust/helpers/vmalloc.c @@ -0,0 +1,9 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +void * __must_check __realloc_size(2) +rust_helper_vrealloc(const void *p, size_t size, gfp_t flags) +{ + return vrealloc(p, size, flags); +} diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs index 4f3c3ab7c7a2..f341e9ed5b7c 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`]. @@ -166,6 +179,30 @@ unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { } } +// 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 Vmalloc { + #[inline] + unsafe fn realloc( + ptr: Option>, + layout: Layout, + old_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, old_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(