From patchwork Tue Oct 1 14:59:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13818228 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 446DDCEACDC for ; Tue, 1 Oct 2024 15:01:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C73A568000C; Tue, 1 Oct 2024 11:01:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C24A4280068; Tue, 1 Oct 2024 11:01:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AA00068000C; Tue, 1 Oct 2024 11:01:08 -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 83C33280068 for ; Tue, 1 Oct 2024 11:01:08 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id E9260413B2 for ; Tue, 1 Oct 2024 15:01:07 +0000 (UTC) X-FDA: 82625346174.25.E141D36 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf08.hostedemail.com (Postfix) with ESMTP id 308EB160036 for ; Tue, 1 Oct 2024 15:01:05 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="Jb+L/974"; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf08.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=1727794763; a=rsa-sha256; cv=none; b=AdH6dKJ+084bv8m8pI4a7+FFv3+AUT7XcQ1Y6MkpPl6KxnMNSsW0vzKKxqHC82SBlsL85J K6VVUvoFV8TfPLtMKFf88M44Kzh+i6Tkqu3crgZJsy9t4X/MqXFr7Vr5Jlxxp58bA1vBzV uzNPe61Bu7s3GfvWmWC87tIXDtuhMIM= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="Jb+L/974"; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf08.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=1727794763; 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=mTVTvWKBANHMJZHeH6BJATxo69A42yUtQyl4pBz6v5Q=; b=GQDWAsExw7WWdxwxg7KayoB4LYjDExSokbAxHeGxJVjy0iuH/Q0kmWMBy4zRDmwQpNUz9y oSpWChf7GV3Z7QcFO/xzYwlGReq725/QFmc1E1ZJfI34uuzWkNpdBEFajWr48oI3CVtsCJ B4zsDOpLt0tzuxG5OV1iVNzSxEl8RiI= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 51C995C05AF; Tue, 1 Oct 2024 15:01:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 242ECC4CECD; Tue, 1 Oct 2024 15:00:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727794865; bh=25/QaXEy+FB9oK5Q0dGNxbiEnTbfCEBUqqsWLlj6o/M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Jb+L/974KTCo1VTgtjLQzagR16hrKSSSSmPbdgDVllc+ydBuPYpR38zx/QFIVGmEE 0V6iGjMj6CbPKi25I33ZhHZri+vbXNmsL5rnZ2I1TRzwD6hhyF9JcNeh2O4nEJu5Gr HMg6pAUP6lGE89yX3/8NqWZXOgSFTqk+RBq1Gzq4XsiJHvRPCSed0rJcZhyG0IBCZC r7Yo6hocN2eiC0fiaURlaXaI1q7EIDdU1FDF+mFFdO9Yyg2YaS9wOTSwmUQUR8KZj1 3L7fQPAcnOw3zFQ6iaY8nqyWNWITQP9YI3Z2I7g8eVu9NAi4E4C/8uxf1OGFPJY1Ff WxoyilgZnMdFg== 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 09/29] rust: alloc: implement `KVmalloc` allocator Date: Tue, 1 Oct 2024 16:59:44 +0200 Message-ID: <20241001150008.183102-10-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-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 308EB160036 X-Stat-Signature: qhyxbsocwe1b4uigjazttqd5qdibjrfg X-Rspam-User: X-HE-Tag: 1727794865-792854 X-HE-Meta: U2FsdGVkX19pllndnAcW7Pv3fpzoQhPwtHJPX6yotPi31PiG4u/rqws1BSVErVDpHmW8XrcUFydDtsvX1zx5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v0ue8l0dq7rUeq6/jmcNXuy11Cc50JmiUeKK2cE/CSyczfkjVxV9O664IKqhpIuIvER6OU4pU5aEEgOCnjexY8qedPzGTy6bd0kL3oR55532qRYKrlzrbVjbfC/tzVuK2mTW2e/lwMW4m1mbyLyvRiP7+fZquTTT9cLfYE= 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()`. Reviewed-by: Alice Ryhl Reviewed-by: Benno Lossin Signed-off-by: Danilo Krummrich Reviewed-by: Gary Guo --- rust/helpers/slab.c | 6 +++++ rust/kernel/alloc/allocator.rs | 36 +++++++++++++++++++++++++++++ rust/kernel/alloc/allocator_test.rs | 1 + 3 files changed, 43 insertions(+) diff --git a/rust/helpers/slab.c b/rust/helpers/slab.c index f043e087f9d6..a842bfbddcba 100644 --- a/rust/helpers/slab.c +++ b/rust/helpers/slab.c @@ -7,3 +7,9 @@ rust_helper_krealloc(const void *objp, size_t new_size, gfp_t flags) { return krealloc(objp, new_size, flags); } + +void * __must_check __realloc_size(2) +rust_helper_kvrealloc(const void *p, size_t size, gfp_t flags) +{ + return kvrealloc(p, size, flags); +} diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs index f341e9ed5b7c..d0a8819c6741 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -34,6 +34,15 @@ /// For more details see [self]. pub struct Vmalloc; +/// The kvmalloc kernel allocator. +/// +/// `KVmalloc` attempts to allocate memory with `Kmalloc` first, but falls back to `Vmalloc` upon +/// failure. This allocator is typically used when the size for the requested allocation is not +/// known and may exceed the capabilities of `Kmalloc`. +/// +/// For more details see [self]. +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. @@ -76,6 +85,9 @@ impl ReallocFunc { // INVARIANT: `vrealloc` satisfies the type invariants. const VREALLOC: Self = Self(bindings::vrealloc); + // INVARIANT: `kvrealloc` satisfies the type invariants. + const KVREALLOC: Self = Self(bindings::kvrealloc); + /// # Safety /// /// This method has the same safety requirements as [`Allocator::realloc`]. @@ -203,6 +215,30 @@ unsafe fn realloc( } } +// 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 KVmalloc { + #[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!("KVmalloc 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::KVREALLOC.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 e7bf2982f68f..1b2642c547ec 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(