From patchwork Fri Oct 4 15:41:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13822617 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 2A9BFCF8849 for ; Fri, 4 Oct 2024 15:42:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B33C06B0395; Fri, 4 Oct 2024 11:42:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A6BC76B038F; Fri, 4 Oct 2024 11:42:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8C2ED6B0395; Fri, 4 Oct 2024 11:42:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 666626B017F for ; Fri, 4 Oct 2024 11:42:41 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 1E839161975 for ; Fri, 4 Oct 2024 15:42:41 +0000 (UTC) X-FDA: 82636337322.18.73964A8 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf27.hostedemail.com (Postfix) with ESMTP id 7AEF74001C for ; Fri, 4 Oct 2024 15:42:39 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=qgIP+fTy; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf27.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=1728056518; a=rsa-sha256; cv=none; b=qQSokQLjsAtipvCMJqNh6DI9zRpc5QoFNwAIHCUfi7BCTfFRzdLQIwSA2W5NGBgLBXSPDG /5pcgPG0Ga3dEdq5aZK6MXwXmbV727bnagSyi0FyP5gCb4Vyhjx3bF/iBtz41dbkJqF82S RQbA/fW6GxRvj4JmpqlBUZgqCGGRiHU= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=qgIP+fTy; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf27.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=1728056518; 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=OXbb9vuIJZW1uNrX76L0wqPxeeMp2MxaDSrDlseTL58=; b=rH0tVByMrlFlm68V7NVhsz5pgt/+GRPcZHWHNm9cZQyNEZm4d+oPPAykP3W9eH3mvV0iJY 3TB4Wq6WExaT8MlAxNvkWME4iWWiBUcmEsmYESqjiZ3hihMDpQTw3JKkQKxKY8WNOEfDmn MRAeHwRUkosdmoY2vKfnJ5URZMJsfvE= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 339085C5A2A; Fri, 4 Oct 2024 15:42:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1D46EC4CEC6; Fri, 4 Oct 2024 15:42:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728056558; bh=WMfOs4LG4FcoFF7HvER0kneu0SsNLoPkbGbTFCvgLjw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qgIP+fTyX7lwjVlnpmUyGjOJZynK9iiYrJ1weIGyEyn4HIs0F5TN1pp1cwK2jj/oJ 41Rd3fxoq7eW1KKElQYZRH6QXoHEfGl8QtcapXpTVKm4+FaSiP5QIg2njJwHir2YsH jfTO1eTd6sZUn+4rporGAsUPmMVD75txon7Bk5OBZ5mTQDBCx9jfdeQ7pR/a5ED45e Q3GTVUw0R/LGjBm/hUASbRIBRsypT3vGThTkUmrnuwfbPMU5p+qV/hAc496btDURlO i5zN3rdjaPsfxoOGzu0yjs7POkDsibjNx9eySUuTBaPSC2bWajwdVUKDyosIU+4Wov ci7cEGqNZPRDA== 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 06/29] rust: alloc: implement `Allocator` for `Kmalloc` Date: Fri, 4 Oct 2024 17:41:10 +0200 Message-ID: <20241004154149.93856-7-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: 7AEF74001C X-Rspamd-Server: rspam01 X-Stat-Signature: o7cc4th7uqsmeix81ououocngfn5e3dj X-HE-Tag: 1728056559-916173 X-HE-Meta: U2FsdGVkX194t1iXuIDBQRj7t6myM/lZl1T3nYF9+IgTtnR79jGltWSBgw4LvPBxMr92kJqSePIu3DFMIzt03wh81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CayI4g8EFOMei/ex6TtV0bXvR+eAJ+4EO9Krj2X3uzxmAFFnOzz4RkPhFR4Z+xg6QiLhKDAlbyQbKJdSBSqOS8JiiYuWmkn2dsf1aS0Z9EL3scQLDgKlzp+yA//e0Mpa7/DdCNb7QUSxSW7OPnFEc8PNP/c8b8AXS0GD99tbI2jmhPWRjAVB7xvkLE6AAuISQT7MHk= 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`. Reviewed-by: Benno Lossin Reviewed-by: Gary Guo Signed-off-by: Danilo Krummrich --- rust/kernel/alloc/allocator.rs | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs index e5bac90046a4..b1894c800448 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -13,10 +13,16 @@ use core::ptr; use core::ptr::NonNull; -use crate::alloc::AllocError; +use crate::alloc::{AllocError, Allocator}; use crate::bindings; -struct Kmalloc; +/// The contiguous kernel allocator. +/// +/// `Kmalloc` is typically used for physically contiguous allocations up to page size, but also +/// supports larger allocations up to `bindings::KMALLOC_MAX_SIZE`, which is hardware specific. +/// +/// For more details see [self]. +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 { @@ -53,8 +59,10 @@ struct ReallocFunc( unsafe extern "C" fn(*const core::ffi::c_void, usize, u32) -> *mut core::ffi::c_void, ); -#[expect(dead_code)] 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`]. @@ -106,6 +114,23 @@ unsafe fn call( } } +// SAFETY: `realloc` delegates to `ReallocFunc::call`, which guarantees that +// - memory remains valid until it is explicitly freed, +// - passing a pointer to a valid memory allocation is OK, +// - `realloc` satisfies the guarantees, since `ReallocFunc::call` has the same. +unsafe impl Allocator for Kmalloc { + #[inline] + unsafe fn realloc( + ptr: Option>, + layout: Layout, + old_layout: Layout, + flags: Flags, + ) -> Result, AllocError> { + // SAFETY: `ReallocFunc::call` has the same safety requirements as `Allocator::realloc`. + unsafe { ReallocFunc::KREALLOC.call(ptr, layout, old_layout, flags) } + } +} + // SAFETY: TODO. unsafe impl GlobalAlloc for Kmalloc { unsafe fn alloc(&self, layout: Layout) -> *mut u8 {