From patchwork Tue Jul 23 18:09:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13740251 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 7D771C3DA63 for ; Tue, 23 Jul 2024 18:11:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 10E556B009A; Tue, 23 Jul 2024 14:11:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0BDEE6B009B; Tue, 23 Jul 2024 14:11:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E7B276B009C; Tue, 23 Jul 2024 14:11:38 -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 C7F2B6B009A for ; Tue, 23 Jul 2024 14:11:38 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 7D30B14032D for ; Tue, 23 Jul 2024 18:11:38 +0000 (UTC) X-FDA: 82371810276.26.6569BA3 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf30.hostedemail.com (Postfix) with ESMTP id 3707F80034 for ; Tue, 23 Jul 2024 18:11:35 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=P0fy4oHg; spf=pass (imf30.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=1721758273; 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=TDB1iNccaH+8aHo8IMoKY9aDpMHOdcr7EYA8sLrNcLI=; b=U2FTZMFoMJt3t40ACJRxttoNUXH8EL8Cpm3YNeGi8wwQOapWH3M/UfDm45PAg4CgbufPkZ cdO6B5x/1ygJZdFHSOEf1+CEc2vGLg9RgehMMpIocJYEdvMlc/PQtCyRJG8t0eJWJSOt/l 4Kjv5vsdDnQSSp9thA2aQ6NOSI7M6fU= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=P0fy4oHg; spf=pass (imf30.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=1721758273; a=rsa-sha256; cv=none; b=O0YDZa+0+K5r2y8HdMNMl2DJV29Dwl442x0cgEifigsFRxinvwMDQc15ddUy7LWorZYTxH wQaQovucXtI1wX+6pjFOly9cN+mU0s4ffoWWdR32XP6QBOJGjI6/2uRzLypByCgcEyVM7E okDLgCrq7JnS5S8qojYFOVkOVyEBuPs= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 6D36ECE0E93; Tue, 23 Jul 2024 18:11:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7E755C4AF09; Tue, 23 Jul 2024 18:11:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721758292; bh=VoSgcZBnVOwLP7rZy/xS0BTIj35SmIvHWDRctFLWgt8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=P0fy4oHgiIxm8fq1X97zoBznf3/NVarogrH+h0ecVwl2+FDYFNOUrCCy0ZNOG3Jm0 adQ50MkRsjoHOEd+Oc5uFHJbeibMZYhLLVzoVHjQD25QlzgVmqhoZkDozm+er2h1Pf 7q8u9sl1Bkhpo/f2FPlzT0E6H1kdmmhQQN9/yEevF+Z6pu97nY9WgN4drWO/N8wkRH Owu/UB9CgoG4HYoAfjgEMhUAl8uEkBtoBGlPL9NZwcBJ1gZsAqASGeeFsNxkoyk/KV 8cR5UTpSHU1s+WRfjTz6fAp3R5Pf/bE8F7tvuYMOu6vKAFuoLWE83As6FHoGyF8fus oo9UN0/r3MqtA== 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 07/23] rust: alloc: implement `KVmalloc` allocator Date: Tue, 23 Jul 2024 20:09:56 +0200 Message-ID: <20240723181024.21168-8-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-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 3707F80034 X-Stat-Signature: o3455fwt6aiczt3cwrc8u77xzo1fr93k X-HE-Tag: 1721758295-750930 X-HE-Meta: U2FsdGVkX1+1ZxoE+I1+P7xWRi/0Sx922xluUHHUnU22ghcKE9JOkTIJaFDuuK2eQPMorzteGb1io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nYcKF3aNbnQHV/B4ZFsxJYydLQSMsHa48AFTFPGJgq2KRItn+v4C9YCEmituo583nMLBs1ZE9DBaGT3Ff3dwcQMGLeGFwQV+emGsuBXSXt9Qpq68u66QLsfDwH3bFbcG2Bg0tG+yYxQ== 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 `KVmalloc`, an `Allocator` that tries to allocate memory wth `kmalloc` first and, on failure, falls back to `vmalloc`. All memory allocations made with `KVmalloc` end up in `kvrealloc_noprof()`; all frees in `kvfree()`. Signed-off-by: Danilo Krummrich --- rust/helpers.c | 7 +++++++ rust/kernel/alloc/allocator.rs | 24 ++++++++++++++++++++++++ rust/kernel/alloc/allocator_test.rs | 1 + 3 files changed, 32 insertions(+) diff --git a/rust/helpers.c b/rust/helpers.c index 5ca7e4872ee8..19a5e4a0c7ee 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -173,6 +173,13 @@ rust_helper_vrealloc(const void *p, size_t size, gfp_t flags) } EXPORT_SYMBOL_GPL(rust_helper_vrealloc); +void * __must_check __realloc_size(2) +rust_helper_kvrealloc(const void *p, size_t size, gfp_t flags) +{ + return kvrealloc(p, size, flags); +} +EXPORT_SYMBOL_GPL(rust_helper_kvrealloc); + /* * `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 e9a3d0694f41..1e53f149db96 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -22,6 +22,12 @@ /// contiguous kernel virtual space. pub struct Vmalloc; +/// The kvmalloc kernel allocator. +/// +/// Attempt to allocate physically contiguous memory, but upon failure, fall back to non-contiguous +/// (vmalloc) allocation. +pub struct KVmalloc; + /// 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. @@ -68,6 +74,10 @@ fn vrealloc() -> Self { Self(bindings::vrealloc) } + fn kvrealloc() -> Self { + Self(bindings::kvrealloc) + } + // SAFETY: `call` has the exact same safety requirements as `Allocator::realloc`. unsafe fn call( &self, @@ -160,6 +170,20 @@ unsafe fn realloc( } } +unsafe impl Allocator for KVmalloc { + unsafe fn realloc( + ptr: Option>, + layout: Layout, + flags: Flags, + ) -> Result, AllocError> { + let realloc = ReallocFunc::kvrealloc(); + + // 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 b2d7db492ba6..f0e96016b196 100644 --- a/rust/kernel/alloc/allocator_test.rs +++ b/rust/kernel/alloc/allocator_test.rs @@ -8,6 +8,7 @@ pub struct Kmalloc; pub type Vmalloc = Kmalloc; +pub type KVmalloc = Kmalloc; unsafe impl Allocator for Kmalloc { unsafe fn realloc(