From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-40134.protonmail.ch (mail-40134.protonmail.ch [185.70.40.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 46CAE1C688E for ; Wed, 7 Aug 2024 07:23:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.40.134 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723015434; cv=none; b=UxTOOw8zJGv5AfnQzzE+2eL1zYOrBdB3nu6vaTAgeCDW4yHrLzYAXEniJx8+yTB1ldCejkS7lrkf4zQsgG8lmeG/BgVTiGFD2Lpx75gNXGXczCV3Ms1X9j2D2DYOYBLQFsjoOl8ePONRA1VWbici4Oo5XvxPAbtOY73nSxONF+Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723015434; c=relaxed/simple; bh=xdGo4xSoahzawkf/UMWpDpfNeuykWh4zVfbIV78aXr4=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=iEamj2SNCDB2ih8c/rowi01QDCO1Xua4z2qnS8Ez3VnZl+6Wm7UR84anzaoPn9KzNeuG6a01+BqRFtumTJoQwgdC9Isvrx8bSK4VzXNoftlZY2EqyJpzkdN2TWO+bgGXWpau5yE5++GKgqrYK3HjqQxKLSSriv3oJCUSnKGubwo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me; spf=pass smtp.mailfrom=proton.me; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b=UiREsEBy; arc=none smtp.client-ip=185.70.40.134 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=proton.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="UiREsEBy" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1723015431; x=1723274631; bh=xxaFpg0Ze3aE/zz773LrPtPLPWXfOgXdS9SauyJpIPY=; 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=UiREsEByLc3Xkbi86UfWyGqYHWtGWlWuGPEoKO/FEImNOI4LXISIq8TJ7C10IfSLK Hx2e/NwfHQkSsN81/mPngQKx6QHZ+Ljw0xcvTDcjI0i8G295Yzt3tMfcM/nLToIVkT 6+S3y+exEc4ee4IbR0aFPIdM6fYFuY2mkcAtWZutvk8VdhCmWGveaFP1LlMfGwQ8b8 vGR/jDi40MxOPhC2li6hKwllQjP0GgEM6RK9ncMaQA9FJ+i82PhZFJie8wb8LtliZB 1hQ28VaKnAB0lKED1vfq4qSo2k0xWABixZ11g7DZLRL++tVVNimrxtptR6w3Mav8ey ZlSR0PK7Xl9Dg== Date: Wed, 07 Aug 2024 07:23:48 +0000 To: Danilo Krummrich From: Benno Lossin Cc: 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, 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 Subject: Re: [PATCH v4 06/28] rust: alloc: implement `Vmalloc` allocator Message-ID: <64498fd9-b3f1-46f4-aebe-e5ff65634a86@proton.me> In-Reply-To: References: <20240805152004.5039-1-dakr@kernel.org> <20240805152004.5039-7-dakr@kernel.org> <9c144953-819d-44fa-9bb5-af6fa93a5042@proton.me> Feedback-ID: 71780778:user:proton X-Pm-Message-ID: 1280b02a83101c11cf2e540fcb2f068fe473cd92 Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 06.08.24 21:01, Danilo Krummrich wrote: > On Tue, Aug 06, 2024 at 05:00:24PM +0000, Benno Lossin wrote: >> On 05.08.24 17:19, Danilo Krummrich wrote: >>> Implement `Allocator` for `Vmalloc`, the kernel's virtually contiguous >>> allocator, typically used for larger objects, (much) larger than page >>> size. >>> >>> All memory allocations made with `Vmalloc` end up in `vrealloc()`. >>> >>> Reviewed-by: Alice Ryhl >>> Signed-off-by: Danilo Krummrich >>> --- >>> rust/helpers.c | 7 +++++++ >>> rust/kernel/alloc/allocator.rs | 32 +++++++++++++++++++++++++++++ >>> rust/kernel/alloc/allocator_test.rs | 1 + >>> 3 files changed, 40 insertions(+) >>> >>> diff --git a/rust/helpers.c b/rust/helpers.c >>> index 9f7275493365..7406943f887d 100644 >>> --- a/rust/helpers.c >>> +++ b/rust/helpers.c >>> @@ -33,6 +33,7 @@ >>> #include >>> #include >>> #include >>> +#include >>> #include >>> #include >>> >>> @@ -199,6 +200,12 @@ void *rust_helper_krealloc(const void *objp, size_= t new_size, gfp_t flags) >>> } >>> EXPORT_SYMBOL_GPL(rust_helper_krealloc); >>> >>> +void *rust_helper_vrealloc(const void *p, size_t size, gfp_t flags) >>> +{ >>> +=09return vrealloc(p, size, flags); >>> +} >>> +EXPORT_SYMBOL_GPL(rust_helper_vrealloc); >>> + >>> /* >>> * `bindgen` binds the C `size_t` type as the Rust `usize` type, so we= can >>> * use it in contexts where Rust expects a `usize` like slice (array) = indices. >>> diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocat= or.rs >>> index c6ad1dd59dd0..bb55895cbd03 100644 >>> --- a/rust/kernel/alloc/allocator.rs >>> +++ b/rust/kernel/alloc/allocator.rs >>> @@ -9,6 +9,7 @@ >>> >>> use crate::alloc::{AllocError, Allocator}; >>> use crate::bindings; >>> +use crate::pr_warn; >>> >>> /// The contiguous kernel allocator. >>> /// >>> @@ -16,6 +17,12 @@ >>> /// `bindings::krealloc`. >>> pub struct Kmalloc; >>> >>> +/// The virtually contiguous kernel allocator. >>> +/// >>> +/// The vmalloc allocator allocates pages from the page level allocato= r and maps them into the >>> +/// contiguous kernel virtual space. >>> +pub struct Vmalloc; >> >> One thing that I should also have mentioned for `Kmalloc`, do we want >> these types to also have values? I don't think that we need them to be, >> so we could declare them as `pub enum Vmalloc {}`. >=20 > What the difference? Would `pub enum Vmalloc {}` be better for some reaso= n? It doesn't make a huge difference, it doesn't allow you to create a value of type `Vmalloc` (as there are no values of that type). So you can't accidentally use the type where it shouldn't be used. If we use `pub struct Vmalloc;`, then you can do this: let a =3D Vmalloc; you can't really do anything with it (as there are no methods on that type), but it might be confusing for people. >>> @@ -141,6 +153,26 @@ unsafe fn alloc_zeroed(&self, layout: Layout) -> *= mut u8 { >>> } >>> } >>> >>> +unsafe impl Allocator for Vmalloc { >>> + unsafe fn realloc( >>> + ptr: Option>, >>> + layout: Layout, >>> + flags: Flags, >>> + ) -> Result, AllocError> { >>> + let realloc =3D ReallocFunc::vrealloc(); >>> + >>> + // TODO: Support alignments larger than PAGE_SIZE. >>> + if layout.align() > bindings::PAGE_SIZE { >>> + pr_warn!("Vmalloc does not support alignments larger than = PAGE_SIZE yet.\n"); >>> + return Err(AllocError); >>> + } >>> + >>> + // SAFETY: If not `None`, `ptr` is guaranteed to point to vali= d memory, which was previously >>> + // allocated with this `Allocator`. >>> + unsafe { realloc.call(ptr, layout, flags) } >> >> I am a bit confused, for `Kmalloc`, you manually returned >> `NonNull::dangling` when allocating a zero-sized allocation, but here >> you don't? >> >=20 > I do, it's the exact same implementation for krealloc(), vrealloc() and > kvrealloc(). That why I added the `ReallocFunc` abstraction. Oh yeah, my bad. --- Cheers Benno