From patchwork Thu Aug 1 00:02:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13749340 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 D07E0C3DA64 for ; Thu, 1 Aug 2024 00:07:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6AF556B0083; Wed, 31 Jul 2024 20:07:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 638E36B0088; Wed, 31 Jul 2024 20:07:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 500596B0089; Wed, 31 Jul 2024 20:07:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 30B736B0083 for ; Wed, 31 Jul 2024 20:07:06 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id AB2E11C2C8E for ; Thu, 1 Aug 2024 00:07:05 +0000 (UTC) X-FDA: 82401736410.28.5759C09 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf01.hostedemail.com (Postfix) with ESMTP id 690FC4001A for ; Thu, 1 Aug 2024 00:07:03 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=EZyPia1n; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf01.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 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=1722470761; 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=BAEAXc4XtzeJtIivSCOhHPkcB/x9zV7EStIejOmGIM0=; b=vZk6G4O6tYiqtyhTKUb9weOPul23bFTSaejD0cNStEeCGWKJPzVYLiKF3+BBpdvvvBPrx5 wmyWQS5Rm81acNd/4rY/Oq9GlwhrRjsev3SKAM4f5zClwWBCariYhJ2q1MX0FZLzrms8AX oBhk2Ve1KPM26oK8BpF5vKiY36nnjk8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722470761; a=rsa-sha256; cv=none; b=qKkr/JZBk+Hdy95w0abbVI0F7x/Lup9hJ6F8x3vxeUZxAyk2avFWGAm3oZ8mXyZdjP/Bqs KkCcy0SIaawg7uS9LPi+6B1g3hGy/ueNXeMd6MwwtUM5nTA8uJB0x+agiiAkEKT83tnU9H XacMSP7GWvBKPblH3WcehUXuCIpuB6g= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=EZyPia1n; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf01.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=dakr@kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 810FCCE1811; Thu, 1 Aug 2024 00:06:58 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C6C43C4AF0E; Thu, 1 Aug 2024 00:06:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722470817; bh=kRuEhdnhz6Oh4MuGdrkk5vMdvHwyKzjCm+Q5vf6XVyw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EZyPia1n+C0W5qz4kOxNx9KtE86e+aYpLFiHeEsfmLE8REX244dH5Fld8vnG8OWiZ I94s406MrWq+XMAJ5CvqLyEZ6/oG7aG3/Yn5PO0+VRj+dr1OS12yfrs/2t+4d7ZLss z3HvFFA1Luj4pnTMr/Zt3ZNMxItVXCH0i8tO24PlKAabfOrX3RqrGhxClSvFmhr6bI W7i63bjwg+UbUJyliCuE1n6qme78rFHJCXcUdP/nQTwb90pZc307evxXka/xaZAL2S sHNQvp2tGtawND8QO0AXNQvYv7RzXPrtlQLkRF0+ESzwD0h3Saa+NkJnaazUfsg2My gjjaI1s6MpLuw== 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 v3 01/25] rust: alloc: add `Allocator` trait Date: Thu, 1 Aug 2024 02:02:00 +0200 Message-ID: <20240801000641.1882-2-dakr@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240801000641.1882-1-dakr@kernel.org> References: <20240801000641.1882-1-dakr@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 690FC4001A X-Stat-Signature: qj4dwb7nz7qgku7nfaz1pfdwomkrzeug X-Rspam-User: X-HE-Tag: 1722470823-49291 X-HE-Meta: U2FsdGVkX1/Ztt0eFYhZCsYFyl+SpmR1QNIolum5NwLFMmBEoi+9viylVvCNHMXkJScCANzuLNf9uhg7owkOXntoKO6wFrhd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ZoJewE5yC2d+XJifohtysgQxf15w1XyQFyl26fFyVGV7HOUwveEJ8RXYbTXI/uOJ21mKEVKKiBYLLn2kNOgUGa9RkrT/jV7X3G2HqcQ2vijQ9hCWEQdCEHYR12p9pmpC/1yeaHUKQ== 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: Add a kernel specific `Allocator` trait, that in contrast to the one in Rust's core library doesn't require unstable features and supports GFP flags. Subsequent patches add the following trait implementors: `Kmalloc`, `Vmalloc` and `KVmalloc`. Signed-off-by: Danilo Krummrich --- rust/kernel/alloc.rs | 73 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs index 1966bd407017..b79dd2c49277 100644 --- a/rust/kernel/alloc.rs +++ b/rust/kernel/alloc.rs @@ -11,6 +11,7 @@ /// Indicates an allocation error. #[derive(Copy, Clone, PartialEq, Eq, Debug)] pub struct AllocError; +use core::{alloc::Layout, ptr::NonNull}; /// Flags to be used when allocating memory. /// @@ -86,3 +87,75 @@ pub mod flags { /// small allocations. pub const GFP_NOWAIT: Flags = Flags(bindings::GFP_NOWAIT); } + +/// The kernel's [`Allocator`] trait. +/// +/// An implementation of [`Allocator`] can allocate, re-allocate and free memory buffer described +/// via [`Layout`]. +/// +/// [`Allocator`] is designed to be implemented as a ZST; [`Allocator`] functions do not operate on +/// an object instance. +/// +/// # Safety +/// +/// Memory returned from an allocator must point to a valid memory buffer and remain valid until +/// it is explicitly freed. +/// +/// Any pointer to a memory buffer which is currently allocated must be valid to be passed to any +/// other [`Allocator`] function. The same applies for a NULL pointer. +/// +/// If `realloc` is called with: +/// - a size of zero, the given memory allocation, if any, must be freed +/// - a NULL pointer, a new memory allocation must be created +pub unsafe trait Allocator { + /// Allocate memory based on `layout` and `flags`. + /// + /// On success, returns a buffer represented as `NonNull<[u8]>` that satisfies the layout + /// constraints (i.e. minimum size and alignment as specified by `layout`). + /// + /// This function is equivalent to `realloc` when called with a NULL pointer. + fn alloc(layout: Layout, flags: Flags) -> Result, AllocError> { + // SAFETY: Passing a NULL pointer to `realloc` is valid by it's safety requirements and asks + // for a new memory allocation. + unsafe { Self::realloc(None, layout, flags) } + } + + /// Re-allocate an existing memory allocation to satisfy the requested `layout`. If the + /// requested size is zero, `realloc` behaves equivalent to `free`. + /// + /// If the requested size is larger than the size of the existing allocation, a successful call + /// to `realloc` guarantees that the new or grown buffer has at least `Layout::size` bytes, but + /// may also be larger. + /// + /// If the requested size is smaller than the size of the existing allocation, `realloc` may or + /// may not shrink the buffer; this is implementation specific to the allocator. + /// + /// On allocation failure, the existing buffer, if any, remains valid. + /// + /// The buffer is represented as `NonNull<[u8]>`. + /// + /// # Safety + /// + /// `ptr` must point to an existing and valid memory allocation created by this allocator + /// instance. + /// + /// Additionally, `ptr` is allowed to be a NULL pointer; in this case a new memory allocation is + /// created. + unsafe fn realloc( + ptr: Option>, + layout: Layout, + flags: Flags, + ) -> Result, AllocError>; + + /// Free an existing memory allocation. + /// + /// # Safety + /// + /// `ptr` must point to an existing and valid memory allocation created by this `Allocator` + /// instance. + unsafe fn free(ptr: NonNull) { + // SAFETY: `ptr` is guaranteed to be previously allocated with this `Allocator` or NULL. + // Calling `realloc` with a buffer size of zero, frees the buffer `ptr` points to. + let _ = unsafe { Self::realloc(Some(ptr), Layout::new::<()>(), Flags(0)) }; + } +}