From patchwork Fri Oct 4 15:41:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13822615 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 6F0C8CF884C for ; Fri, 4 Oct 2024 15:42:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0834F6B0404; Fri, 4 Oct 2024 11:42:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 00B406B0405; Fri, 4 Oct 2024 11:42:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E15886B0407; Fri, 4 Oct 2024 11:42:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id C3B426B0404 for ; Fri, 4 Oct 2024 11:42:30 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 430F481824 for ; Fri, 4 Oct 2024 15:42:30 +0000 (UTC) X-FDA: 82636336860.10.C846BD2 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf25.hostedemail.com (Postfix) with ESMTP id 9C9B1A001A for ; Fri, 4 Oct 2024 15:42:28 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="QVUR/08O"; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf25.hostedemail.com: domain of dakr@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=dakr@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728056507; a=rsa-sha256; cv=none; b=Dc0Ey03+82xn3CL6Q1oGialu8EKNc52ELEkyd4Ng4d7Hlhkyv9XpagUQo1utYbbZB5toJC 8Wmx9WkhbuBMcjSyTN4r82KvHcRehLRsoZd/BBPwvI3nDzZ/euPpO0+I86OjYPNq0Heo1x YgMS5UNhnIfzeR9NpshlDZrfxrYAyYE= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="QVUR/08O"; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf25.hostedemail.com: domain of dakr@kernel.org designates 147.75.193.91 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=1728056507; 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=Y8afrj5fwsE8kPj55OEkbECCJFcjKz8tGCPXOPqYD/A=; b=WCBMT93cvjL5dM+uKcOheZmK9m9oe6gMtrRGn5yPwMfPZFLZSAPuQ38STa7nawszRe3ez8 crMSroSF+/sALQsaT7JleH/1ZOKCQ1N17E0ySzYqeFNQwGFtpRcOf8GcrWYMbZQpFQF/U1 aKYeZhgEa90W8Z0oBOa1Im79QwmhigI= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 57371A44C48; Fri, 4 Oct 2024 15:42:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 41F86C4CEC7; Fri, 4 Oct 2024 15:42:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728056547; bh=FjNpQ7WplEuJvZRXB9OKv92SNKAtpq7ActsyiT3/uto=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QVUR/08O9+E1CNbbgPERgJT5Kwjss1sEY4YsIYtAHtUWpluZl24F8r65XdahkgBbQ KqsJ+pcUshMVHutDmTwYlUonLQLcUfNl0AiCnazXFGf5qr5JpfCGDIwiJ6KsyGztRX iQ3m3d/gWQT517HOJkSJcM4RfkInPGO+pW6mf3AjXK49jOp5beuavcWBoODmsUgU34 0PGUzUgE+v+RuLOz6Fk71tEy5IFx3IW1U+B6bVzWR6AjgLdjB9HMIuHFI+1N1mTxYn LQgA3F8cbXRDpAqi9uZyXziN5ChX3v8E2XeIQS2IbA78gm/hlC+0097YdthT8KcV56 rpe/FpWijamSA== 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 v9 04/29] rust: alloc: implement `ReallocFunc` Date: Fri, 4 Oct 2024 17:41:08 +0200 Message-ID: <20241004154149.93856-5-dakr@kernel.org> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20241004154149.93856-1-dakr@kernel.org> References: <20241004154149.93856-1-dakr@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 9C9B1A001A X-Rspamd-Server: rspam01 X-Stat-Signature: o4k7i1pqzq1wxft8npyhjt4pgqpu5y5u X-HE-Tag: 1728056548-40352 X-HE-Meta: U2FsdGVkX18k5Hf5L+osykvvYdL+KF0ynfGmg7jjVtAN2B/c/s4jHahjX/VHIDZq4AWiB50VrsxCKo0u6oKxxU8TBPE8yMy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HLMW1JxoWCj9zJKF44n4cPnPFjfnT761PNG7bIChSAGjUXu6xNmaYAZa/3ZzplnG6jnlQ1sv5y/aHkxmuOL99x82QOuDEfdLbfWnCGXAyjV5aputyAQvMsQCZTzpPzRN68irQb73tcdAAA1HU0Al3kFn14181p5yt7mxi/b3rLfpNWv00Jeuw3SQxFv9etkeVwece8YjsJIBNUE3C5 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: `ReallocFunc` is an abstraction for the kernel's realloc derivates, such as `krealloc`, `vrealloc` and `kvrealloc`. All of the named functions share the same function signature and implement the same semantics. The `ReallocFunc` abstractions provides a generalized wrapper around those, to trivialize the implementation of `Kmalloc`, `Vmalloc` and `KVmalloc` in subsequent patches. Reviewed-by: Benno Lossin Reviewed-by: Gary Guo Signed-off-by: Danilo Krummrich --- rust/kernel/alloc.rs | 8 ++++ rust/kernel/alloc/allocator.rs | 70 ++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs index 4deeea3488be..da49c5306431 100644 --- a/rust/kernel/alloc.rs +++ b/rust/kernel/alloc.rs @@ -187,3 +187,11 @@ unsafe fn free(ptr: NonNull, layout: Layout) { let _ = unsafe { Self::realloc(Some(ptr), Layout::new::<()>(), layout, Flags(0)) }; } } + +/// Returns a properly aligned dangling pointer from the given `layout`. +pub(crate) fn dangling_from_layout(layout: Layout) -> NonNull { + let ptr = layout.align() as *mut u8; + + // SAFETY: `layout.align()` (and hence `ptr`) is guaranteed to be non-zero. + unsafe { NonNull::new_unchecked(ptr) } +} diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs index 3b1c735ba409..e5bac90046a4 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -1,10 +1,20 @@ // SPDX-License-Identifier: GPL-2.0 //! Allocator support. +//! +//! Documentation for the kernel's memory allocators can found in the "Memory Allocation Guide" +//! linked below. For instance, this includes the concept of "get free page" (GFP) flags and the +//! typical application of the different kernel allocators. +//! +//! Reference: use super::{flags::*, Flags}; use core::alloc::{GlobalAlloc, Layout}; use core::ptr; +use core::ptr::NonNull; + +use crate::alloc::AllocError; +use crate::bindings; struct Kmalloc; @@ -36,6 +46,66 @@ pub(crate) unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: F unsafe { bindings::krealloc(ptr as *const core::ffi::c_void, size, flags.0) as *mut u8 } } +/// # Invariants +/// +/// One of the following `krealloc`, `vrealloc`, `kvrealloc`. +struct ReallocFunc( + unsafe extern "C" fn(*const core::ffi::c_void, usize, u32) -> *mut core::ffi::c_void, +); + +#[expect(dead_code)] +impl ReallocFunc { + /// # Safety + /// + /// This method has the same safety requirements as [`Allocator::realloc`]. + /// + /// # Guarantees + /// + /// This method has the same guarantees as `Allocator::realloc`. Additionally + /// - it accepts any pointer to a valid memory allocation allocated by this function. + /// - memory allocated by this function remains valid until it is passed to this function. + unsafe fn call( + &self, + ptr: Option>, + layout: Layout, + old_layout: Layout, + flags: Flags, + ) -> Result, AllocError> { + let size = aligned_size(layout); + let ptr = match ptr { + Some(ptr) => { + if old_layout.size() == 0 { + ptr::null() + } else { + ptr.as_ptr() + } + } + None => ptr::null(), + }; + + // SAFETY: + // - `self.0` is one of `krealloc`, `vrealloc`, `kvrealloc` and thus only requires that + // `ptr` is NULL or valid. + // - `ptr` is either NULL or valid by the safety requirements of this function. + // + // GUARANTEE: + // - `self.0` is one of `krealloc`, `vrealloc`, `kvrealloc`. + // - Those functions provide the guarantees of this function. + let raw_ptr = unsafe { + // If `size == 0` and `ptr != NULL` the memory behind the pointer is freed. + self.0(ptr.cast(), size, flags.0).cast() + }; + + let ptr = if size == 0 { + crate::alloc::dangling_from_layout(layout) + } else { + NonNull::new(raw_ptr).ok_or(AllocError)? + }; + + Ok(NonNull::slice_from_raw_parts(ptr, size)) + } +} + // SAFETY: TODO. unsafe impl GlobalAlloc for Kmalloc { unsafe fn alloc(&self, layout: Layout) -> *mut u8 {