From patchwork Mon Aug 5 15:19:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13753713 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 8BB29C3DA4A for ; Mon, 5 Aug 2024 15:20:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 15CF46B0093; Mon, 5 Aug 2024 11:20:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 10D7B6B0095; Mon, 5 Aug 2024 11:20:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F3E236B0096; Mon, 5 Aug 2024 11:20:41 -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 D7AA66B0093 for ; Mon, 5 Aug 2024 11:20:41 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 9F0261A0268 for ; Mon, 5 Aug 2024 15:20:41 +0000 (UTC) X-FDA: 82418553882.23.61F3B4F Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf01.hostedemail.com (Postfix) with ESMTP id 50B8240009 for ; Mon, 5 Aug 2024 15:20:38 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=EMxxVbqZ; spf=pass (imf01.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722871178; 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=oIfNG5+EAdgpQkSpCBi98MFR4NgYWnX2/c6hwf14RYo=; b=mNltc4gbX/6cURniUSptEcNq+Qg8ndRzKmierKBG1FiNlCvCK13CnhMVz29bbMpOZl432l W4084V0CZ0ZmC8qDxzvf2X7NQSJrGrE51dyQ0J1T+XjvpkROjfULRahx23x+PmfWo9Bddj sAuewc4SER2o+tbGjsKwe4df34naFSg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722871178; a=rsa-sha256; cv=none; b=06AU4uYcFijwZ7u40yo+tYBaZrEGUuD78114InTI0T4LVTAATfn3BwxulgUR9tKOVkk0KV I9NAQ8cIgFa+7huk2rYN4ejAbS9HhRI3m/ZN9X3cgiJcVDqhtAqc0OCjXvnI5SqRNAr6PN DWxha6EouSgxgr/f3S+0whLGffRIUSE= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=EMxxVbqZ; spf=pass (imf01.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 2DFB5CE0B2D; Mon, 5 Aug 2024 15:20:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 697CAC32782; Mon, 5 Aug 2024 15:20:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722871235; bh=KUN3egAY7JyTXEWIOlIzv3zwwD5pg50NL+IK/lAWQ2g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EMxxVbqZsYS6nje0qCRhq8ZjYYic641hDJRMsjp6Pa5Ba7wKckgxEzzaVkWT4c7E4 tY9hyufAZRqbEWsqp+jP1c0nJdb4JIGFs6/3mKu6g0HwA3prX2eDxZWIHeieyQ7zkw msTYgDTMoxrHx9Kfdysi8sOAVZmA8GbTMIvk/Gatky1A38ZHuV4WLGPyEQIChmxeM/ sdjY4dYgVjDkpONE5ArmejxyhKkv7F8zgYza4kcdEJpyQzUSNLaPCoiPAFpq2MqJK4 s5qsHODvwWTrJn3dJcGxBsVJWuLDQ9HO9Ln6kp/S5ofY9gnIuFimqfJCsRSx9VFpSO V4zwpKrFBpwrA== 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, acurrid@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 v4 04/28] rust: alloc: implement `Allocator` for `Kmalloc` Date: Mon, 5 Aug 2024 17:19:23 +0200 Message-ID: <20240805152004.5039-5-dakr@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240805152004.5039-1-dakr@kernel.org> References: <20240805152004.5039-1-dakr@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 50B8240009 X-Stat-Signature: racf8pgbxmxmnq1pham681naccaf9mbo X-HE-Tag: 1722871238-325011 X-HE-Meta: U2FsdGVkX1/Ph36oVe/yca6ER0UhkSVzddx7Fd7BCr9f0Ne6pzvB+u6ecl4rB3p7F7odqgn5FATkhgy/fy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gQw8yfR8/Dbz8IAy2pbWBfERIB9kFZauJRDksUUf9nAewZm8Auvb5fHNOMoI7Co6oC8XCni53wQY1oCAbUB4CAVOXdWso2PrQreUBQQDfzUZacWNi4LF8YUrgcAlvuUhbhsQGBjqg== 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 | 68 +++++++++++++++++++++++++++++++++- 3 files changed, 69 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 8a71a589469d..bc01a17df5e0 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 2c1eae25da84..c6ad1dd59dd0 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 { @@ -40,6 +48,64 @@ pub(crate) unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: F } } +/// # 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. + fn krealloc() -> Self { + Self(bindings::krealloc) + } + + /// # Safety + /// + /// This method has the exact 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 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> { + let realloc = ReallocFunc::krealloc(); + + // SAFETY: If not `None`, `ptr` is guaranteed to point to valid memory, which was previously + // allocated with this `Allocator`. + unsafe { realloc.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