From patchwork Tue Oct 1 14:59:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13818223 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 BDCA9CEACD5 for ; Tue, 1 Oct 2024 15:00:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4D1BC680007; Tue, 1 Oct 2024 11:00:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4AB03280068; Tue, 1 Oct 2024 11:00:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 34AEF680007; Tue, 1 Oct 2024 11:00:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 1412D280068 for ; Tue, 1 Oct 2024 11:00:42 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 5AEAAC15FE for ; Tue, 1 Oct 2024 15:00:41 +0000 (UTC) X-FDA: 82625345082.05.ECC1738 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf08.hostedemail.com (Postfix) with ESMTP id 4287E160019 for ; Tue, 1 Oct 2024 15:00:39 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=rcjoRaVU; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf08.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=1727794818; a=rsa-sha256; cv=none; b=rc4HnOQzyPqZP5EerBLA4XgROlldW6xpevIxJpRVuS0/AcPpudCIWIBdR8VbY9HSL1DKAm nmOXr/qSlpaX+YkanqDyxhUcV9SUXQKo34aMXXH/Ijsn0PCUvCMRpqUlM6j/AwDZnfZ9/4 qL/Azl9jRJRCvU/lNPZuCuQ+o/sUGFA= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=rcjoRaVU; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf08.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=1727794818; 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=qFrmP5157eCfDsowG9v7lcPreOrVZv3LnPoMdyp9W3k=; b=dXwqhHXokIJ7e9KYy6HdLBeNJCIIm+gtqBhhkKQyNy3XHB2Um3wHZVrHD7Y9Rxok4Z0uOY 1QUqD9vhqf8GZDX0qovKDDRDS4q/46ZDmcKq2piF9KJMMXLUyYv4KvcRkWbKY9g7IQNLnf zLPanIcslwTR/1LDcZn1ngjuBHnLckg= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 3A889A4027C; Tue, 1 Oct 2024 15:00:30 +0000 (UTC) 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> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: qpphfbtispwnkr4ssedeawg1fsejigzd X-Rspamd-Queue-Id: 4287E160019 X-Rspamd-Server: rspam02 X-HE-Tag: 1727794839-177950 X-HE-Meta: U2FsdGVkX19+IfIqt+qdJLjirklM/06e2YC8Z2aExX+Y+D7pfEk8fsck/i7Lw0dloAYRQrr3mXu58NyFJTMfZ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A0fesm2qQE+nXNbUcCSzNzDdkXrMzlHNgPaMx8gYLqc5ze/B335tDu7B/+HSF6qv8SsfgN40l7XS/YqmzGE7QuQEgiKhBihlmktNJ4quK2s8YO2Z0eDr3M3Tw2mjn0z9KsUKd5w9CBskg4XlHDRoC+U= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000005, 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. 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 _ = 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 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 //! 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)) + } +} + 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 size by the function safety