From mboxrd@z Thu Jan 1 00:00:00 1970 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 DC48BC8303E for ; Thu, 29 Aug 2024 18:32:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 73E586B0082; Thu, 29 Aug 2024 14:32:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6EE9D6B0083; Thu, 29 Aug 2024 14:32:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 603D46B00AF; Thu, 29 Aug 2024 14:32:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 3BD826B0082 for ; Thu, 29 Aug 2024 14:32:51 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id DFB46141194 for ; Thu, 29 Aug 2024 18:32:50 +0000 (UTC) X-FDA: 82506129300.12.5ADFD49 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by imf25.hostedemail.com (Postfix) with ESMTP id 32F55A000A for ; Thu, 29 Aug 2024 18:32:47 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=proton.me header.s=protonmail header.b=Ftcn2N2e; spf=pass (imf25.hostedemail.com: domain of benno.lossin@proton.me designates 185.70.43.16 as permitted sender) smtp.mailfrom=benno.lossin@proton.me; dmarc=pass (policy=quarantine) header.from=proton.me ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724956279; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=YOfxQtw40ZXlYY9ClmFMSo+YqxH/dt5Klo4ck81NB/A=; b=jzT25DMIgAL4eGq1/Ept+l0hhyJ1M7NERtyLA0mAcboMwSGACHUpEb7E8SSBWz4pxO6wI4 iBvolbpGKsf3LbfCjkWFH8/VQxj/lxTHblG7ll6T0h7V6pK3yTIGlQRmFBDCvCLn9tZX99 OAaxND6D5KzmhcZi1pHMKMeO4D1iLmY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724956279; a=rsa-sha256; cv=none; b=PT9GZbEtyOm/bmhm2Ybqyk31QBp63ggVSSjb6Dj6t/cquuUiT4Zvi5eTy9qxXpTTwPJYCP tBtB0/c86O0a5tkO3hTsxPjP7/p6Vhb9X4dnYkf48H+gC7hKIFBzQcGQov6NiDVgid/TYF KWMC0BIm7YqYftl5BOaJi2Dkaax9JUw= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=proton.me header.s=protonmail header.b=Ftcn2N2e; spf=pass (imf25.hostedemail.com: domain of benno.lossin@proton.me designates 185.70.43.16 as permitted sender) smtp.mailfrom=benno.lossin@proton.me; dmarc=pass (policy=quarantine) header.from=proton.me DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1724956366; x=1725215566; bh=YOfxQtw40ZXlYY9ClmFMSo+YqxH/dt5Klo4ck81NB/A=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=Ftcn2N2eOuSuiG7u+g7CIp+3zJMQ2q7LYMg+6er7KOtGiO3B+XOy07ntPSylMjvfd NzNi6QgtNPVwtvm64/ROgFqUtonPV6cMdkpAEqAzKiwVey+Ip4xqkBiW24nokrunAu 2EUG9eb+iq4iBusYzszjJq4NaoZcaVh7Ktwx5DZyVtIKvL/XcrndPSZaxD5QVIGXCm CwAirNhHIymW+wIi68sQuNxUfMk191Ugh80bk4VvpZZUKB8CUmC2AU+Ot0HtXI8+nu BhG9U3FGqWt/lroWdN1mKMilS4lUq9jZWs+dF6nz5MRNZ27/LkJznJ+XZzzd5KR7Wr xSUSEduTqoZ6Q== Date: Thu, 29 Aug 2024 18:32:42 +0000 To: Danilo Krummrich , ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, a.hindborg@samsung.com, aliceryhl@google.com, akpm@linux-foundation.org From: Benno Lossin 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 Subject: Re: [PATCH v6 04/26] rust: alloc: implement `Allocator` for `Kmalloc` Message-ID: <2dd02834-b2b6-4ff6-9e29-43c9d77b69e2@proton.me> In-Reply-To: <20240816001216.26575-5-dakr@kernel.org> References: <20240816001216.26575-1-dakr@kernel.org> <20240816001216.26575-5-dakr@kernel.org> Feedback-ID: 71780778:user:proton X-Pm-Message-ID: 5d22a818e5a4787a27c7c22d2758781a7315a81e MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 32F55A000A X-Stat-Signature: t4uncgfff3ofssgozizh6aexk9ggsh98 X-HE-Tag: 1724956367-539607 X-HE-Meta: U2FsdGVkX19h1N+Me51rP1TN18aJaT9+R9heUhWTTqzdTL6E2opaZjH2dWlXOOTM5+4pxQBW+5k2wzEqfBSKf1gmywefW2xBS+S2cB3i5iE5+1frHlr7OFFeXkeCKNRIq92zzuinvPDwE59yCGkcGgsfoh6Ky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hwTL/tJBX1MkCFkUHcW+6bV7HVXQbBVlf3J8E5o3jG2hyYt0PmxYV2VQq+3eaaHFZrO1w31aqu2Dfzk2dY0QRTM6O5og+XeQmd9LF1DC/kIQd+7RLGLmunHfMRFsYv4Hf0b8fU9jltg12FYLtRR91msgmjTdQ1IEacLTkbKhs/4inMg8ghy3+l2Vw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On 16.08.24 02:10, Danilo Krummrich wrote: > Implement `Allocator` for `Kmalloc`, the kernel's default allocator, > typically used for objects smaller than page size. >=20 > All memory allocations made with `Kmalloc` end up in `krealloc()`. >=20 > It serves as allocator for the subsequently introduced types `KBox` and > `KVec`. >=20 > Signed-off-by: Danilo Krummrich > --- > rust/kernel/alloc.rs | 2 +- > rust/kernel/alloc/allocator.rs | 72 +++++++++++++++++++++++++++++++++- > 2 files changed, 72 insertions(+), 2 deletions(-) >=20 > diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs > index 9932f21b0539..477dbe3c5a2f 100644 > --- a/rust/kernel/alloc.rs > +++ b/rust/kernel/alloc.rs > @@ -4,7 +4,7 @@ >=20 > #[cfg(not(test))] > #[cfg(not(testlib))] > -mod allocator; > +pub mod allocator; > pub mod box_ext; > pub mod vec_ext; >=20 > diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator= .rs > index e32182f91167..78e7d5488843 100644 > --- a/rust/kernel/alloc/allocator.rs > +++ b/rust/kernel/alloc/allocator.rs > @@ -1,12 +1,28 @@ > // SPDX-License-Identifier: GPL-2.0 >=20 > //! Allocator support. > +//! > +//! Documentation for the kernel's memory allocators can found in the "M= emory Allocation Guide" > +//! linked below. For instance, this includes the concept of "get free p= age" (GFP) flags and the > +//! typical application of the different kernel allocators. > +//! > +//! Reference: Thanks, this nice. >=20 > use super::{flags::*, Flags}; > use core::alloc::{GlobalAlloc, Layout}; > use core::ptr; > +use core::ptr::NonNull; >=20 > -struct Kmalloc; > +use crate::alloc::{AllocError, Allocator}; > +use crate::bindings; > + > +/// 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`, whic= h is hardware specific. Does putting a link here work? (I guess we don't yet export the bindings documentation, so it will probably fail... When we decide to enable it, we should create an issue to add missing links) > +/// > +/// For more details see [self]. > +pub struct Kmalloc; >=20 > /// Returns a proper size to alloc a new object aligned to `new_layout`'= s alignment. > fn aligned_size(new_layout: Layout) -> usize { > @@ -36,6 +52,60 @@ pub(crate) unsafe fn krealloc_aligned(ptr: *mut u8, ne= w_layout: Layout, flags: F > unsafe { bindings::krealloc(ptr as *const core::ffi::c_void, size, f= lags.0) as *mut u8 } > } >=20 > +/// # Invariants > +/// > +/// One of the following `krealloc`, `vrealloc`, `kvrealloc`. > +struct ReallocFunc( > + unsafe extern "C" fn(*const core::ffi::c_void, usize, u32) -> *mut c= ore::ffi::c_void, > +); > + > +impl ReallocFunc { > + // INVARIANT: `krealloc` satisfies the type invariants. > + const KREALLOC: Self =3D Self(bindings::krealloc); > + > + /// # Safety > + /// > + /// This method has the same safety requirements as [`Allocator::rea= lloc`]. > + unsafe fn call( > + &self, > + ptr: Option>, > + layout: Layout, > + flags: Flags, > + ) -> Result, AllocError> { > + let size =3D aligned_size(layout); > + let ptr =3D match ptr { > + Some(ptr) =3D> ptr.as_ptr(), > + None =3D> ptr::null(), > + }; > + > + // SAFETY: `ptr` is either NULL or valid by the safety requireme= nts of this function. You need some justification as to why calling the three allowed functions here. > + let raw_ptr =3D unsafe { > + // If `size =3D=3D 0` and `ptr !=3D NULL` the memory behind = the pointer is freed. > + self.0(ptr.cast(), size, flags.0).cast() > + }; > + > + let ptr =3D if size =3D=3D 0 { > + NonNull::dangling() > + } else { > + NonNull::new(raw_ptr).ok_or(AllocError)? > + }; > + > + Ok(NonNull::slice_from_raw_parts(ptr, size)) > + } > +} > + > +unsafe impl Allocator for Kmalloc { Missing SAFETY comment. --- Cheers, Benno > + #[inline] > + 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 > -- > 2.46.0 >=20