From mboxrd@z Thu Jan 1 00:00:00 1970 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 AC179CF884B for ; Fri, 4 Oct 2024 15:42:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 36BB56B0201; Fri, 4 Oct 2024 11:42:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2F2F96B040D; Fri, 4 Oct 2024 11:42:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 11F476B0391; Fri, 4 Oct 2024 11:42:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id DCB246B01FB for ; Fri, 4 Oct 2024 11:42:51 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 59B29121A19 for ; Fri, 4 Oct 2024 15:42:51 +0000 (UTC) X-FDA: 82636337742.08.70F5CD2 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf08.hostedemail.com (Postfix) with ESMTP id BF3E216001B for ; Fri, 4 Oct 2024 15:42:49 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=cSA2K6ze; spf=pass (imf08.hostedemail.com: domain of dakr@kernel.org designates 147.75.193.91 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=1728056503; 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=kEHbOceUJnP8Ubhpcd93+jphAnizfaIlpTbGHAUwJ0E=; b=lYVOdswX0kmZ7KyW+xeJoVIJwLDyN6amv0tboMePIWfr+nLHVIjJ6rZhyq5FAfSCchSDuN mk5Iaa7P7J3n8fz8BOSW6ewwpQsXDSsqM0zL4W93jcInMraDjRh4bh4zpViSP2IVSvfQh0 nww5bdvaisUrfNvUR5pL4nislTw2LV4= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=cSA2K6ze; spf=pass (imf08.hostedemail.com: domain of dakr@kernel.org designates 147.75.193.91 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=1728056503; a=rsa-sha256; cv=none; b=GS9Em6JCgl2RggwHNp7i3pled9VR/LioJ7SRpb05sdv5mCPnPkMxafsWLlWOcplk8Dzn8g 8c9mbWBaypju49FCZM9yS07VXc8l8JTs0tcrY/pjiIJJGae+ydyoirFnyXf2MjROFE/y3m 9nD5iz9ToQtJ3qVrfQR1OdGWPaNJwTw= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id CAF4DA44C5A; Fri, 4 Oct 2024 15:42:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E1B5EC4CECE; Fri, 4 Oct 2024 15:42:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728056568; bh=ivWwJFghp2EePOEDw8b0kSPMmrO/dnpTdtxjr1CLz4w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cSA2K6zeVv53uZz9bvEnG58rmYxYWwi8m+G32sOaGwu7Rcjut8ag2DdcqHYX3Ft6G 1AtbO67RWiG43g1FNUdnf+HuXevV+6dbqHC9FSpn7vgyHsL7w6bYwpz5/a0+dc8nK9 ZwsbLcjfvaDAjSAIHEY3Rj8G3eZmoKFtGM25jukwyFLHVWS5VZvXg6tP8WYPLj1EhO ziQ1MnRkf9Az6ai+7ryVXTuCErMWEuVbtJTMthTpoMjRB/gSl6VKSpxAoCeyZeYbkE q5mIHNIx/0B2PM4AdS81GXikoT4rc4qjb6o+EOkO23+/TEmCWd/aupZx3/hET+9KpY /2UPc0f/gl1oA== 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 v9 08/29] rust: alloc: implement `Vmalloc` allocator Date: Fri, 4 Oct 2024 17:41:12 +0200 Message-ID: <20241004154149.93856-9-dakr@kernel.org> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20241004154149.93856-1-dakr@kernel.org> References: <20241004154149.93856-1-dakr@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: BF3E216001B X-Stat-Signature: o3g3zrnex3wumobttt1jzwu5onob4jhz X-HE-Tag: 1728056569-205109 X-HE-Meta: U2FsdGVkX1+X/Ze/1VY0vZvPKtq8q9NWhkEzEL+wQS96qzQ+Z8KMUN+g52zScSrGtzzd+A7kvvYUktAK/TtFOgJ+v9fKYF/D+gdcWm8Dmg1bTaSTm4MiOJkRkUf8G4NpCm4gF6ZEQgmDDUJxedqr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CZzjGeQCczzs4NuX3WQ1gmcgKueji1dsfKShEA6QfHmM21hL+44BsjfCYzjTm6ZvBlSIUOtv8Ixdy0AQ4N64lUAowJA0jsY5Uj7++6V+Dsfw8agTiP+stkI+ugxtFVn+WSQvuuIkUU/voJie/FRNkiE5qqd 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 Reviewed-by: Gary Guo Signed-off-by: Danilo Krummrich --- 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 b1894c800448..5742ce7d0453 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`]. @@ -168,6 +181,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( -- 2.46.1