From patchwork Tue Jul 23 18:09:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13740250 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 75853C3DA63 for ; Tue, 23 Jul 2024 18:11:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 071D46B0098; Tue, 23 Jul 2024 14:11:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 021736B0099; Tue, 23 Jul 2024 14:11:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E04C26B009A; Tue, 23 Jul 2024 14:11:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id C1A736B0098 for ; Tue, 23 Jul 2024 14:11:33 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 44F7DA1F9A for ; Tue, 23 Jul 2024 18:11:33 +0000 (UTC) X-FDA: 82371810066.11.1D0DDE2 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf01.hostedemail.com (Postfix) with ESMTP id D6D1740007 for ; Tue, 23 Jul 2024 18:11:30 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=baOAej1A; spf=pass (imf01.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 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=1721758255; 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=a1Z170eel8JgQ9M7Yxbm+eSyWn0zSiRoP53h7G2mg/Q=; b=LMGz7onK3R3rPArWHYVYpAwu1WpmK+al+xT1kzHllK4NjKDclxetrpujGbUjyQh+SgIM9O 1kT01DXUGgkwkcO/l/8RjhtQiamaes1QnbcUvYsAw4ULWljqBbJ4ewQe7guxS08XbdTimM TYSvauaxjC1vEz5Kwk0T0jH5z3IR1Eo= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=baOAej1A; spf=pass (imf01.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 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=1721758255; a=rsa-sha256; cv=none; b=ElG4P8ijvjw6SZLawIHJmHrdEISElg/aQT0Gfr5YF2RNCHys9JYwinlgiEIO8KuQPCUcCC SKJ7rRzq9sU3oyDL3PqbWF9KvOtoAA3oPL6FbvY+sWccXCb0zraX3qlSOSKckID/59Wo9o MIVCRJahwBlXBh53hk6rPhoANdr9D4c= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id DA21ACE0EAC; Tue, 23 Jul 2024 18:11:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DBEB5C4AF11; Tue, 23 Jul 2024 18:11:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721758287; bh=YaemE3GyNUw6k5SxqCko0qV/GMMCyuitkbWwSJz9u6E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=baOAej1Au7/hHblUtzSZcJOHCZz7qZr12SRUNNlEDYgjYE8epT7uQ6Ol6uKD0vVle 7KTQvroSucEVwHrGu82BmmATVO34yILswc2nfJqeTGChptNBIA2IaOeaev9rzZjGsg 5meeIrkW+uA5ZeBIitR9pR/a4Y8ngqdWbvCYEcf4QU18vwY3kKTPbK8bMyNMke3u0Z 4h8SOfuWi3fpJrF8AO46vqQI0DbAHLPLmlZdTIpdjq4+hDc6KtS4WMcxrWRSzvknT3 S8FSd0onOAXFkJCFP9n4LqJ67ES+ojOTTatk9WwKYHOAzbWDV9DJzwkMfmXPBErcuD b2mfW9M/rKnEQ== 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, acurrid@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 v2 06/23] rust: alloc: implement `Vmalloc` allocator Date: Tue, 23 Jul 2024 20:09:55 +0200 Message-ID: <20240723181024.21168-7-dakr@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240723181024.21168-1-dakr@kernel.org> References: <20240723181024.21168-1-dakr@kernel.org> MIME-Version: 1.0 X-Stat-Signature: bjbkprd58apz7px754zcwg6mfxthka97 X-Rspam-User: X-Rspamd-Queue-Id: D6D1740007 X-Rspamd-Server: rspam02 X-HE-Tag: 1721758290-646410 X-HE-Meta: U2FsdGVkX19Az8ssZrqOtq4HGVsQtqHzVvqXNxUfWchS0679dc2kAEPIImtf3LD8aIwUntPXNbF1xXLuA+VHgyLOcUEJZuVOCfK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MRcafeNhau7a4M1kAUkLdNQN0ETeiJaLSN2BB4HRhnEQ5KufLoB18NSc88GYskJTuDoX+Ip5Ji2ZbXOJHDOlkUzSOnoZ8vWi+t2vqJDjIleuGkbsuEwq84Cm426eZwn1vz2BlA== 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()`. Signed-off-by: Danilo Krummrich --- rust/helpers.c | 8 ++++++++ rust/kernel/alloc/allocator.rs | 24 ++++++++++++++++++++++++ rust/kernel/alloc/allocator_test.rs | 1 + 3 files changed, 33 insertions(+) diff --git a/rust/helpers.c b/rust/helpers.c index 3df5217fb2ff..5ca7e4872ee8 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -165,6 +166,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); + /* * `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 397ae5bcc043..e9a3d0694f41 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -16,6 +16,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. @@ -58,6 +64,10 @@ fn krealloc() -> Self { Self(bindings::krealloc) } + fn vrealloc() -> Self { + Self(bindings::vrealloc) + } + // SAFETY: `call` has the exact same safety requirements as `Allocator::realloc`. unsafe fn call( &self, @@ -136,6 +146,20 @@ 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> { + let realloc = ReallocFunc::vrealloc(); + + // SAFETY: If not `None`, `ptr` is guaranteed to point to valid memory, which was previously + // allocated with this `Allocator`. + unsafe { realloc.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 3a0abe65491d..b2d7db492ba6 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(