From patchwork Tue Jul 23 18:09:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13740248 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 67E6AC3DA63 for ; Tue, 23 Jul 2024 18:11:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EEFF06B0092; Tue, 23 Jul 2024 14:11:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E7A6A6B0093; Tue, 23 Jul 2024 14:11:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D199F6B0095; Tue, 23 Jul 2024 14:11:21 -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 AEB636B0092 for ; Tue, 23 Jul 2024 14:11:21 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 670211C05DE for ; Tue, 23 Jul 2024 18:11:21 +0000 (UTC) X-FDA: 82371809562.28.A4B0906 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf04.hostedemail.com (Postfix) with ESMTP id 25D0140005 for ; Tue, 23 Jul 2024 18:11:18 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=RDOuFQ8+; spf=pass (imf04.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=1721758257; 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=XJQFnSigBs64AK6qJbsb/6K4YewhmH5hGvCy+TIFET0=; b=MG0s94y+eKRClIK4ZcSwMjyFB6vapVCePQEVEs1YJ6H0ZJED1KKq0ZITwqJqKsu1rYyXq5 RN2/g367rZJGw7yF5ueX0P0eyXCw3Mi6QkiY5o54++SKReWoaxqVELpnz/bVtraWgfQsmb tidHuIpOzEWMgZFH7AI+ZJezxU9hd8Y= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=RDOuFQ8+; spf=pass (imf04.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721758257; a=rsa-sha256; cv=none; b=D6OIViFFdixhUYt006lbxxcVY9cW1DWutV0v6LIjJFJCHcF7Ngczxjd8SDu4cMF5zrhWry GHyV7RW/pz0esPtFVzsoOymtcu47GQMlXE+p8NZO9k4YT0xhFp9Xkz+5X0p8CqwIKMCo3s MJelniJLVAp95qUawHNxkXQ5XKRwG68= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 85368CE0E8B; Tue, 23 Jul 2024 18:11:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8F4CEC4AF13; Tue, 23 Jul 2024 18:11:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721758275; bh=LIUBeLsNsDjs5xTYqpQj2wzu9Kf9qHfPrPDSatbqKYw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RDOuFQ8+8bKHj3kQ5nWdYX5MCjfcmldiMQGhEIFnZxm/KBri2pLNVpBER8FMKqVci ZT92rVWWPBz+aDCzyZdSSlXazYnHOxRhSrZAF5QtXKwt6Yx/roa7uuwiT5e3gl2AS4 ntdGyFz4xLeHT1Odjc3gkiTPeQmqzUy5JSfBib2nOqbOmIBArs/cI3wHr+/Tcngfhh VY34vof/Bv2HOLTCI3+ufH6FwSU5SErRmZtq+aKhDsI7/BuNAW3/1MYF+7slArtDav LDp3Q+xY6tp5G2K70K4w2N47olFreJnlgve7+Lg6ah1OJeGemb78FuuGuCsBvXn0d4 T4QqZnGPBIRQg== 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 v2 04/23] rust: alloc: implement `Allocator` for `Kmalloc` Date: Tue, 23 Jul 2024 20:09:53 +0200 Message-ID: <20240723181024.21168-5-dakr@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240723181024.21168-1-dakr@kernel.org> References: <20240723181024.21168-1-dakr@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 25D0140005 X-Stat-Signature: s5ec9wzyoofp9bf79s9ps53hurr3jre6 X-HE-Tag: 1721758278-34996 X-HE-Meta: U2FsdGVkX18hCJqpaqyelU83/98tLVJcHmZpBOsGQwgfnoAgVwMtk/ORl1iSP5T0rg/PTM90aXZkuROizP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WFHNrhcjeKrYNUAuWP5JOJtGmWNvEPEr9BvR1Xt269Te37jfCDkySoy3xR95KxDkNaMSZgc/Pr1hltcYgLb+1xFxG03INnW6sIFnJMnqYhVXg8mqj6+IayZbQGNV7iNQ== 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/kernel/alloc.rs | 2 +- rust/kernel/alloc/allocator.rs | 66 ++++++++++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs index 11d12264c194..bf739ce65387 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 10774c51ae26..397ae5bcc043 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -5,9 +5,18 @@ 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 { // Customized layouts from `Layout::from_size_align()` can have size < align, so pad first. let layout = new_layout.pad_to_align(); @@ -18,7 +27,7 @@ fn aligned_size(new_layout: Layout) -> usize { layout.size() } -/// Calls `krealloc` with a proper size to alloc a new object aligned to `new_layout`'s alignment. +/// Calls `krealloc` with a proper size to alloc a new object. /// /// # Safety /// @@ -39,6 +48,59 @@ pub(crate) unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: F } } +struct ReallocFunc( + // INVARIANT: One of the following `krealloc`, `vrealloc`, `kvrealloc`. + unsafe extern "C" fn(*const core::ffi::c_void, usize, u32) -> *mut core::ffi::c_void, +); + +impl ReallocFunc { + fn krealloc() -> Self { + Self(bindings::krealloc) + } + + // SAFETY: `call` 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