From patchwork Mon Aug 12 18:22:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13760921 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 85730C52D7C for ; Mon, 12 Aug 2024 18:24:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1B7D06B00A9; Mon, 12 Aug 2024 14:24:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1676E6B00AA; Mon, 12 Aug 2024 14:24:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 02E456B00AB; Mon, 12 Aug 2024 14:24:32 -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 D89266B00A9 for ; Mon, 12 Aug 2024 14:24:32 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 9992B40379 for ; Mon, 12 Aug 2024 18:24:32 +0000 (UTC) X-FDA: 82444418784.28.5893A32 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf19.hostedemail.com (Postfix) with ESMTP id DB8C91A0011 for ; Mon, 12 Aug 2024 18:24:30 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ax0ovfo+; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf19.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=dakr@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723487014; a=rsa-sha256; cv=none; b=vs7IUZ92zoyr2qwbKnY91nBI+gQZsi64sCf/bMwhpKpzpDJ4TLHIl+e766NWKMp1Kmhj3N uLMSolWj8iJ90lXxJpxAAhVgLx2c8MiZJ5sL6wqi3QuGjURBnJ1PCFOKiI68btyKViRmgx XwF8qTzhOFltVC6J+xMGPwrpqC86dEQ= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ax0ovfo+; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf19.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=1723487014; 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=BmPCGgJl9uu6+jXwwzElyK3f+SpfOcwge/5P9pLn8xQ=; b=YC+PWNw44edcR1+3JFOReToNers4MwSDnXSwmGZBbn2TfSYvaY77Z7GCOK5jZzW5c3tIz+ TRHftU2tWJr24eY0dQeG/43eYSB33377Ec+eOxYramORcq6FKFaIT8eoOgt34OeSSPKeCa De/eRaCPdhUcMxFJf48leV44o9zHvXc= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 1404B61467; Mon, 12 Aug 2024 18:24:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C61ADC4AF09; Mon, 12 Aug 2024 18:24:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723487069; bh=gb9gRmfntU6sLvTnzwOy0nZzExJzcMuzdJXh+XZXxrI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ax0ovfo+JPuw2LDf+OJPpzMSsXQE9YksuOJXuCo+WmnrW8NlGUwys0w3f+ygfqrio 28tnbwcxAKYNJidKiK73kPcgYOc6OCZK1i6y2SxKcgsiDpu8N1EYsTOhK4wawo2hLx 7GCa3TWWOFDAZVPaF+qPrr9g3pcy8XO77lCAchTLje0bB8y2vjHl8+wlBQ7ev3qVsU 5+46tl5L6he6oy/hfmoF53lwjEIN5lkT7hJHA0CUNtw/edL+OwQ7p+0rhTSM9Io9Av v+Fr10KAWwevX78GGFCLCpub/wgqkPU0baB6gUAY0fL3TZH2iMpz0J+I3zivupq8N1 OdkMYTzQx2l7Q== 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 04/26] rust: alloc: implement `Allocator` for `Kmalloc` Date: Mon, 12 Aug 2024 20:22:50 +0200 Message-ID: <20240812182355.11641-5-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: rspam12 X-Rspamd-Queue-Id: DB8C91A0011 X-Stat-Signature: xpjj3r3wosekkbw5r7xabzzmiwt31b4k X-Rspam-User: X-HE-Tag: 1723487070-45705 X-HE-Meta: U2FsdGVkX19Qpc/lO+nM6dPRu6mQc4qRO/uXOOjFPg/46fnAd+FzGA5zI8bM4PH8Rma3KqjyGu6JtRu5BVl+Uay2CLoKH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vRajw00WQAl7EDpVPctuNESnbmpzwv/yxBVUn5VvKpWNwqcAEx88vZu8dW1WMhQa72RMI68j4Q1coIZw6fIdmed9/+j3tM2tD655jxF6ZAPunLRnW2yOyZc8Au9FHjoC906Q== 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: Implement `Allocator` for `Kmalloc`, the kernel's default allocator, typically used for objects smaller than page size. All memory allocations made with `Kmalloc` end up in `krealloc()`. It serves as allocator for the subsequently introduced types `KBox` and `KVec`. Signed-off-by: Danilo Krummrich --- rust/helpers.c | 3 +- rust/kernel/alloc.rs | 2 +- rust/kernel/alloc/allocator.rs | 63 +++++++++++++++++++++++++++++++++- 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/rust/helpers.c b/rust/helpers.c index 92d3c03ae1bd..9f7275493365 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -193,8 +193,7 @@ void rust_helper_init_work_with_key(struct work_struct *work, work_func_t func, } EXPORT_SYMBOL_GPL(rust_helper_init_work_with_key); -void * __must_check __realloc_size(2) -rust_helper_krealloc(const void *objp, size_t new_size, gfp_t flags) +void *rust_helper_krealloc(const void *objp, size_t new_size, gfp_t flags) { return krealloc(objp, new_size, flags); } diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs index 194745498a75..b732720cfb95 100644 --- a/rust/kernel/alloc.rs +++ b/rust/kernel/alloc.rs @@ -4,7 +4,7 @@ #[cfg(not(test))] #[cfg(not(testlib))] -mod allocator; +pub mod allocator; pub mod box_ext; pub mod vec_ext; diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs index e32182f91167..b46883d87715 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -5,8 +5,16 @@ use super::{flags::*, Flags}; use core::alloc::{GlobalAlloc, Layout}; use core::ptr; +use core::ptr::NonNull; -struct Kmalloc; +use crate::alloc::{AllocError, Allocator}; +use crate::bindings; + +/// The contiguous kernel allocator. +/// +/// The contiguous kernel allocator only ever allocates physically contiguous memory through +/// `bindings::krealloc`. +pub struct Kmalloc; /// Returns a proper size to alloc a new object aligned to `new_layout`'s alignment. fn aligned_size(new_layout: Layout) -> usize { @@ -36,6 +44,59 @@ 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, +); + +impl ReallocFunc { + // INVARIANT: `krealloc` satisfies the type invariants. + const KREALLOC: Self = Self(bindings::krealloc); + + /// # Safety + /// + /// This method has the same safety requirements as `Allocator::realloc`. + unsafe fn call( + &self, + ptr: Option>, + layout: Layout, + flags: Flags, + ) -> Result, AllocError> { + let size = aligned_size(layout); + let ptr = match ptr { + Some(ptr) => ptr.as_ptr(), + None => ptr::null(), + }; + + // SAFETY: `ptr` is either NULL or valid by the safety requirements 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 { + NonNull::dangling() + } else { + NonNull::new(raw_ptr).ok_or(AllocError)? + }; + + Ok(NonNull::slice_from_raw_parts(ptr, size)) + } +} + +unsafe impl Allocator for Kmalloc { + unsafe fn realloc( + ptr: Option>, + layout: Layout, + flags: Flags, + ) -> Result, AllocError> { + // SAFETY: `ReallocFunc::call` has the same safety requirements as `Allocator::realloc`. + unsafe { ReallocFunc::KREALLOC.call(ptr, layout, flags) } + } +} + 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