From patchwork Tue Oct 1 14:59:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13818225 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 94E9FCEACD5 for ; Tue, 1 Oct 2024 15:00:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2D6C2680009; Tue, 1 Oct 2024 11:00:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 28609280068; Tue, 1 Oct 2024 11:00:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 14DCB680009; Tue, 1 Oct 2024 11:00:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id EB96F280068 for ; Tue, 1 Oct 2024 11:00:52 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id AA435141463 for ; Tue, 1 Oct 2024 15:00:52 +0000 (UTC) X-FDA: 82625345544.10.1DB71E8 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf03.hostedemail.com (Postfix) with ESMTP id A0F912001A for ; Tue, 1 Oct 2024 15:00:50 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=oKjAMs00; spf=pass (imf03.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727794723; 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=SyowH/p4NUPulUsdmcadFC0xwJkaVb+cMVVdMicS9JM=; b=r57AFeMYXlEwCMD/xpSKfk4vn0tXHiFuvifoeCNpk6NYk7XTjFDwSd/6Ij9ELJE1Ry6M2E 1MBnsFofnHM7uj+taDGSsTtcgkpnZAf6FJbEZU7leW33NgvEQ0kDg4PnbJvL72baX+XS7I c+I7pBrcZDwKzIoLy5OjfmoRcmiEXQI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727794723; a=rsa-sha256; cv=none; b=wFcHhpi+GKN5mNruqANJZUVDKVmj4Z+srZBh/tzNiFwORbN5bSWj/5lA6jUsoyA/34bZ3J fCzEthbwHzzxe2Y79iyU46DcA2Go/2vsZ+Z47nxAgIyjclxspGpMSQQqnvy1MS3gqWpk7/ m8DF9gtpsBEc2uzeE0Fu0AFJhsBCX4w= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=oKjAMs00; spf=pass (imf03.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 381505C044F; Tue, 1 Oct 2024 15:00:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0B144C4CED2; Tue, 1 Oct 2024 15:00:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727794848; bh=LpkjNNodsESQX/WH+zGgYq9+ft7IaGarSymkz3GKdgI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oKjAMs00KvN55r37SyGM4QVoo7q+wPKTB0KrHiVJx+r0UGrHFtGwN1Yajw5GL9Qbs BB47x+26Q5zDuMCc7Z6iS1by4ZQVrjAmcqG5WB354Wn5wdD/YQrCoiOBfmRDoWEzgS wPOWrfw9e+HqxQKjTBJWU4mR7fyMMhwWxV4oLWhgX3zKTR7PFyoFiX5dLkDZZ1kmVd DF2uzhaxykw0AXrgL49xD283mOwBcGZDn9OGv9NyLWUldBgin+l3Bw9HkmlmoL/7OM idDCJ/8oiY1FKg+1EJh1sgupAr6nTB7eXXWRRiJN+YkiGDwTuwhg39iyw2G/UwJel2 cvqdQJxR99h6A== 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 06/29] rust: alloc: implement `Allocator` for `Kmalloc` Date: Tue, 1 Oct 2024 16:59:41 +0200 Message-ID: <20241001150008.183102-7-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-Stat-Signature: qyf6yge3bojco6sik3ogz6bzn95rim3n X-Rspamd-Queue-Id: A0F912001A X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1727794850-278674 X-HE-Meta: U2FsdGVkX1+RicfEHSMS5XK5vuss6bio2mYM/q6VRgBehz3M4XOLFwcX48me7i3izYEe9YXmMQ4yUabsCUrtNhau4mSl5za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Y6NWVq2EUt5fH1a7HaR04eAfq03cSjtcoCEEbZV+YLpz4DmEHxAia0KoXW5h++BsgQZtfkupiEerM4Cn3I/J9cQ8BiT/FwEXnMD6SN3yIGs7qHzr4d9NFUEe5nY3jKdfmg== 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 Reviewed-by: Benno Lossin Reviewed-by: Gary Guo --- 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 1f28b004b447..4f3c3ab7c7a2 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) } + } +} + 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