From patchwork Mon Aug 12 18:22:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13760917 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 9139BC3DA7F for ; Mon, 12 Aug 2024 18:24:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E3A916B0089; Mon, 12 Aug 2024 14:24:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DE9866B008C; Mon, 12 Aug 2024 14:24:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CB0DB6B009F; Mon, 12 Aug 2024 14:24:16 -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 AEC596B0089 for ; Mon, 12 Aug 2024 14:24:16 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 59D57C0377 for ; Mon, 12 Aug 2024 18:24:16 +0000 (UTC) X-FDA: 82444418112.02.F8EC201 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf01.hostedemail.com (Postfix) with ESMTP id A07E540014 for ; Mon, 12 Aug 2024 18:24:14 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Y0S8qU3y; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf01.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=1723486976; 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=2FD9Vbn5RGxp6wUveMNEi+OKaBxsoUkU9q3bQNxEhik=; b=Hx7aZcxCgGTrQi72FRNF/4tW8AUYBBp5WqkpRbOgc7rkSoyggxvBCLq4PM/Ls48LSFDwUe 9JDb+TRP2q4e/fulsca5mNpI2fTNXsG6RUGz0T/mqZXgNVD5QQbFVqV52nMrFiD7kxCBGj QQJGxnlc9ZmJ2ITK1bQpVGJ1VJxdxAo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723486976; a=rsa-sha256; cv=none; b=QZ5S3doI22TlwuIt5vT8rcpYuIdTK3HdDYFtIGGNfuu56Hqb9E5Y+bD6o+jDae4uuXNbTv aqLcYUsm6kOLPDOc5gt5pdbqgGJYFAkj1wqA4V9drPaM87twNYDN+AvgWrZ+DyuvyiVWEu wflXqYG+N9DwRALfmY+EBgldDtVLLBo= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Y0S8qU3y; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf01.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=dakr@kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id AFB1B61369; Mon, 12 Aug 2024 18:24:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8F796C4AF0C; Mon, 12 Aug 2024 18:24:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723487053; bh=M1Wwvc1PMQiy2tSYLyMdNK6lKzi7oyHYDv/dPN7vzzs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Y0S8qU3yW9xyw0FV2WFxe5tJ6xfV6A4RAetYAlao35rcuYSGc3kxSqYYOSIyvSGlg K8gPFxuu8eGEvuDuHp3ZV3P0n7swgSZjhvDt9zUqOVkjrhDqRVAi1HStM8lFhN5MNr nNuR0gbj94xcaN4MVpSpjulAxr4U5pGB7IsCuEUSxTatJDviLEojsaGJv6I80fmkEM wkossGl3of7NM3pu7JsEEQ7ht8amVK9+dr2vIUWHKOBt8uGBn/HHv21Z4IbjnTqNGD +AEzlU/2/bQ5SghHXgTa+OqtHrdSQbt2bzs4J9GWPWPGrO5IvTd9rPEFopHNBoBKWX 5l+1AxcAqop7Q== 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 v5 01/26] rust: alloc: add `Allocator` trait Date: Mon, 12 Aug 2024 20:22:47 +0200 Message-ID: <20240812182355.11641-2-dakr@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240812182355.11641-1-dakr@kernel.org> References: <20240812182355.11641-1-dakr@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: A07E540014 X-Stat-Signature: 6dfuadcy7tjizh79zbqg1je7tqgy3r9y X-Rspam-User: X-HE-Tag: 1723487054-209528 X-HE-Meta: U2FsdGVkX19fafhnwdYtbbhS/TA5rTFeGPUQdvzHOf8wCXJQfsGcuqOyVcYMq2u/recZbrMdLRcOYnr0U7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2G7GfTmX5m3CnSO0jwscQ+okOIsraCf9eWZ+P3/vNiNDuLVQmrq4IIqyHFLBawrcVfDqqe8X5G3A9VOpIPzjV2fP4c3jyvfM8op9lhaPRYBz+x0dRyVVAgtuzInTw9KfqnuSZvxfvbmhY/iKdvYmtrXJgWEqDMHbrd+IWTIsdqNOo9/t3hTo= 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`. Reviewed-by: Alice Ryhl Signed-off-by: Danilo Krummrich --- rust/kernel/alloc.rs | 81 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs index 1966bd407017..194745498a75 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,83 @@ 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. +/// +/// If `realloc` is called with: +/// - a size of zero, the given memory allocation, if any, must be freed +/// - `None`, 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 `None`. + fn alloc(layout: Layout, flags: Flags) -> Result, AllocError> { + // SAFETY: Passing `None` 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 + /// + /// If `ptr = Some(p)`, then `p` must point to an existing and valid memory allocation created + /// by this allocator. 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` and + /// must not be a dangling pointer. + /// + /// The memory allocation at `ptr` must never again be read from or written to. + unsafe fn free(ptr: NonNull) { + // SAFETY: The caller guarantees that `ptr` points at a valid allocation created by this + // allocator. We are passing a `Layout` with the smallest possible alignment, so it is + // smaller than or equal to the alignment previously used with this allocation. + let _ = unsafe { Self::realloc(Some(ptr), Layout::new::<()>(), Flags(0)) }; + } +}