From nobody Thu Nov 28 14:37:43 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A0B0F84047; Tue, 1 Oct 2024 15:00:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727794838; cv=none; b=Z0QFz0a2n35wJFFn2RicGgFFnk07Vxf21IhSidmn9YN24TDn/XgxvRDDNXTaThZ6dRSgaZ/r5AtJ7nstfaOqmZ91XEir5aD+4U2c+rCmFIZHwAjn1ffyfPDeLaiPb6xUorWh7wgsDcXEvD6+wSAhigE+fKXpjlgmyr/r7kIre0w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727794838; c=relaxed/simple; bh=84o4HmTQOhnlx31XPrRNPs5XQKdYhsElQs3aIrFeaTI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YPZ5SWglAoYnS+LOf2Q374JIROaj8meHHizdOetG0boYuHRQ3EPsHfrZv6FrMzuTxu1mePf7sqiasZ5otBdE2g/4CfrVW4O2Ffj/9tV+oJxyGjoYQ7nE1SzhM6AhwGuLhUlSfbYsS4fO7P+pmYVo/2ZVpu/r48wigGB3m6dXm9o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rcjoRaVU; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rcjoRaVU" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 48AA0C4CEC7; Tue, 1 Oct 2024 15:00:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727794838; bh=84o4HmTQOhnlx31XPrRNPs5XQKdYhsElQs3aIrFeaTI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rcjoRaVUcimcVWFSIYbTey2Zty6FitSgX28zPXXGv3J4oTA5yd/hfYFdzB/n+l75o qnSvFkhWtKNtmz45G4qZF64m36g2H6/ZDxKdo+2SmCx7Wwp7UuiJ0S/g/iI3i9tqyy A46Mn9wREIFPNL+jg+tsrhNCa+TLeN+1RFfuNheG9k9wISBVu6E3+e3FMk0B5c8dov VZTc/yvDh8Pw+pNY3ImItb0cvMxQUystEcFWtjiybDx2q9il/MYsdDmoE3dkJHF3EY PWIICYZxmnEzpP6lx3n9B7qdpmS6fYBEoixfEDiNLbqvq+cNFbgHX6sa9KrHSmYtMp Lioa9e+QOJhKQ== 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 04/29] rust: alloc: implement `ReallocFunc` Date: Tue, 1 Oct 2024 16:59:39 +0200 Message-ID: <20241001150008.183102-5-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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" `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. Signed-off-by: Danilo Krummrich Reviewed-by: Benno Lossin Reviewed-by: Gary Guo --- 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 _ =3D unsafe { Self::realloc(Some(ptr), Layout::new::<()>(), l= ayout, Flags(0)) }; } } + +/// Returns a properly aligned dangling pointer from the given `layout`. +pub(crate) fn dangling_from_layout(layout: Layout) -> NonNull { + let ptr =3D 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 e32182f91167..1f28b004b447 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -1,10 +1,20 @@ // SPDX-License-Identifier: GPL-2.0 =20 //! Allocator support. +//! +//! Documentation for the kernel's memory allocators can found in the "Mem= ory Allocation Guide" +//! linked below. For instance, this includes the concept of "get free pag= e" (GFP) flags and the +//! typical application of the different kernel allocators. +//! +//! Reference: =20 use super::{flags::*, Flags}; use core::alloc::{GlobalAlloc, Layout}; use core::ptr; +use core::ptr::NonNull; + +use crate::alloc::AllocError; +use crate::bindings; =20 struct Kmalloc; =20 @@ -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, fla= gs.0) as *mut u8 } } =20 +/// # Invariants +/// +/// One of the following `krealloc`, `vrealloc`, `kvrealloc`. +struct ReallocFunc( + unsafe extern "C" fn(*const core::ffi::c_void, usize, u32) -> *mut cor= e::ffi::c_void, +); + +#[expect(dead_code)] +impl ReallocFunc { + /// # Safety + /// + /// This method has the same safety requirements as [`Allocator::reall= oc`]. + /// + /// # Guarantees + /// + /// This method has the same guarantees as `Allocator::realloc`. Addit= ionally + /// - it accepts any pointer to a valid memory allocation allocated by= this function. + /// - memory allocated by this function remains valid until it is pass= ed to this function. + unsafe fn call( + &self, + ptr: Option>, + layout: Layout, + old_layout: Layout, + flags: Flags, + ) -> Result, AllocError> { + let size =3D aligned_size(layout); + let ptr =3D match ptr { + Some(ptr) =3D> { + if old_layout.size() =3D=3D 0 { + ptr::null() + } else { + ptr.as_ptr() + } + } + None =3D> ptr::null(), + }; + + // SAFETY: + // - `self.0` is one of `krealloc`, `vrealloc`, `kvrealloc` and th= us only requires that + // `ptr` is NULL or valid. + // - `ptr` is either NULL or valid by the safety requirements of t= his function. + // + // GUARANTEE: + // - `self.0` is one of `krealloc`, `vrealloc`, `kvrealloc`. + // - Those functions provide the guarantees of this function. + let raw_ptr =3D unsafe { + // If `size =3D=3D 0` and `ptr !=3D NULL` the memory behind th= e pointer is freed. + self.0(ptr.cast(), size, flags.0).cast() + }; + + let ptr =3D if size =3D=3D 0 { + crate::alloc::dangling_from_layout(layout) + } else { + NonNull::new(raw_ptr).ok_or(AllocError)? + }; + + Ok(NonNull::slice_from_raw_parts(ptr, size)) + } +} + unsafe impl GlobalAlloc for Kmalloc { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { // SAFETY: `ptr::null_mut()` is null and `layout` has a non-zero s= ize by the function safety --=20 2.46.1