From patchwork Mon Aug 5 15:19:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13753710 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 2FC92C3DA7F for ; Mon, 5 Aug 2024 15:20:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B48F46B0085; Mon, 5 Aug 2024 11:20:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AF8936B0088; Mon, 5 Aug 2024 11:20:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9994C6B0089; Mon, 5 Aug 2024 11:20:26 -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 792326B0085 for ; Mon, 5 Aug 2024 11:20:26 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 01AD11A0306 for ; Mon, 5 Aug 2024 15:20:25 +0000 (UTC) X-FDA: 82418553252.29.53BE8A5 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf23.hostedemail.com (Postfix) with ESMTP id AD7E3140010 for ; Mon, 5 Aug 2024 15:20:22 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=VAqcutSL; spf=pass (imf23.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=1722871193; 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=AxYpsWo2EnhIOTGm3LdMoQCGO6y1NpWF1vV9URIPTKk=; b=CHF1QuuipbVDopNnBkD7o9asq/SpPHXsOfctnjHU0AleBiA3sKha3uvYGlgy5mN4ix3JtU RzwrfuyAs9iKtYRYJp5gOGup1dLdMZ8bjJBE1tkHM289EAk7/hTD65/qGKeyt/54gvsi4r wP1zjnjvU3nGji3ampfD+pOXK+oHqG0= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=VAqcutSL; spf=pass (imf23.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=1722871193; a=rsa-sha256; cv=none; b=Y731YKFO/XMfTLgs3uA1OuvbyJ/4cfZTyKx0bA7E/ZUAA6eiSguvwNeG6cwLjJFYCReDPA 2ofFMrfwrGU6RNaVsHEmltQdurllgLtRDLZib8C9LoCbBsVQLZworebswSopm1n4Yxx+l3 5sYezmlRsUdIzd3EW2MpLjgc9JzU19k= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 88875CE0B2C; Mon, 5 Aug 2024 15:20:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C6339C4AF0B; Mon, 5 Aug 2024 15:20:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722871218; bh=bj2UZpRhWMoc+XVgxuPd+wiqHp6BzB0ewgAdFDBiTYg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VAqcutSL+jtXhIoRiTSkMmZPCI/bBw3pmkzBP8y2/RKxoqqrgTmBCuv07L8Iry7FG NT2oRfpAarApvL7iwz1AChRLNislMw1SRumZx0OJN2RUOInTDpwna/9AudOBqeXaas NtiHLp1V7RyLar9j3jOLHHQY1wCFnYmRj7XndwW+QkisRNcEYP2hodWSAE4U4lkJ/k QF/m40DR9nXme8xAsCTSNfMtGHmYASh0XxAyI+bL9DFk9aqL1aVsr7+7xGl/GL/qN/ mTObrcHiOO3uxo9FZ5CEIXnOXpJLv4ANJ4VS8ESiTb/M3LMiILxa7UllD0tlUkS/Pc 577mQVpjXuo9w== 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 v4 01/28] rust: alloc: add `Allocator` trait Date: Mon, 5 Aug 2024 17:19:20 +0200 Message-ID: <20240805152004.5039-2-dakr@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240805152004.5039-1-dakr@kernel.org> References: <20240805152004.5039-1-dakr@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: AD7E3140010 X-Stat-Signature: xa5istskqd8htcnyhag81h9dye59sz1o X-HE-Tag: 1722871222-719163 X-HE-Meta: U2FsdGVkX19+4SIJOr+pNTogmEG+sa482QEwkskigHrCaj4q6mlBE524lqdIqYtbFeeTA8moOLhFBvkbtLUqVoTTWSxIJcRqBJV9mii2J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C6JQy9zNJPzdxPU6nBraaCNrvSwxy9VIGPb3sMC1yUp4RzjHx8Z+AjcWX4HlOy+rgVpWOSB52q73oLMk9Gl0ouSt7QHhWENXmSVvtO5EeqfQEazqtoYxJA7kfgNaBZjrCLX7XJRpGmrA== 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 Reviewed-by: Alice Ryhl --- rust/kernel/alloc.rs | 79 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs index 1966bd407017..8a71a589469d 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,81 @@ 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. +/// +/// In order to be able to support `#[derive(SmartPointer)]` later on, we need to avoid a design +/// that requires an `Allocator` to be instantiated, hence its functions must not contain any kind +/// of `self` parameter. +/// +/// # 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 of the same type. 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 + /// + /// `Some(ptr)` must point to an existing and valid memory allocation created by this allocator + /// instance. The alignment encoded in `layout` must be smaller than or equal to the alignment + /// requested in the previous `alloc` or `realloc` call of the same allocation. + /// + /// Additionally, `ptr` is allowed to be `None`; 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)) }; + } +}