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 CB54EC3DA7F for ; Sun, 4 Aug 2024 17:40:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F17B56B007B; Sun, 4 Aug 2024 13:40:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EC7A36B0082; Sun, 4 Aug 2024 13:40:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D8E756B0085; Sun, 4 Aug 2024 13:40:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id BC5036B007B for ; Sun, 4 Aug 2024 13:40:09 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 42FAD1A1ADF for ; Sun, 4 Aug 2024 17:40:09 +0000 (UTC) X-FDA: 82415276538.17.04D77DF Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf09.hostedemail.com (Postfix) with ESMTP id E2BCB14000A for ; Sun, 4 Aug 2024 17:40:06 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="E8/iNeTN"; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf09.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=dakr@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722793162; a=rsa-sha256; cv=none; b=q1+/QmUS5k/BZw2ylKHa9GVapi1nM2Fq8BRI7R7D0Wj/dcKTRahM2vczOgNdPaZFFgYHrT o6cN7BFPokOPdHU+qo02cvV3uk3GSy9aZQQR3kSoKKwHBCzpPxjF+Yeva6kaU4XrFROLJH XLH6lL1CZV0tVVmks3Kmi/mKPy0U2tw= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="E8/iNeTN"; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf09.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 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=1722793162; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=NZ0CDWwMfqx/PB2HPvtRjbU5fAGu2fteKcBFvdl0b9U=; b=50uU9SbLrwYUq1Gceddkgk965jj4pZVzmMkeZSLw+kjtCzVs/jdPPzvpZ3AMnkpEkxVGYE 9In4vKPjtOVn3qKr2ZX2NqmnQqS3F29xlMjII4alIbS1xnJralkAjlHvmi6zi6pLPL6Nno NlGHR9fjmzdV8gfBsvgqNxoJwUwMMUY= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 6DC95CE0954; Sun, 4 Aug 2024 17:40:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 712EBC32786; Sun, 4 Aug 2024 17:39:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722793201; bh=qHPGbFeoOcxpSvq6fb4QCfXh/TEYTmXf/c3Zo+EfqVA=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=E8/iNeTNFtEc5Ho6m3a9NKbc4UJkfGsw9/bkX1HBG2HWEU3dJYDXneuOPzTsqlMjH Q+Fun1MGN41rl8a0EevmWpDL5zMBCm9V1Wf6QkuYswrgZlveaa7jhwH+NGaRmiIzjf 22gtNRT64jyDNtu33nZioa2qlExWN28W+CZNDg7/u38gTg2Z4xkD90267QT5tD11S+ fg5wvL5O9DHO52NcOA/W0N7R8pseNtjP5shHblhbiVzxEBCSxfztOk+OrpLe8Ln782 B+s76XMSoeY3wTfIXEESeeVRE9x9LHkZOJRMTKaL5tVNWTsTvHRgD4DaEtbX/KgS4d bxioQjumgVtBA== Date: Sun, 4 Aug 2024 19:39:52 +0200 From: Danilo Krummrich To: Boqun Feng Cc: ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, 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, Vlastimil Babka Subject: Re: [PATCH v3 06/25] rust: alloc: implement `Vmalloc` allocator Message-ID: References: <20240801000641.1882-1-dakr@kernel.org> <20240801000641.1882-7-dakr@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Queue-Id: E2BCB14000A X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: ek3djj78xynxro8irs4caei9u68p5nnf X-HE-Tag: 1722793206-588877 X-HE-Meta: U2FsdGVkX19nSpejbKAbG62DseJV56h+krsLNX6Uw88qDnAXDdwNhJwd9LFvx/nXSQ+I1/ZCDPuX2ZbWfcePsVwVy0HQ0GEM/JOtxkkeX3PE8ppcnPjHups1OsAi5nntyJeaCjF+u0YAFFxmD65UEjM9kBJe2xmL1610oUL99JfhRmCBMvzZoul3MAHX6Vb+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/u5aQDQ8vcRrRjUs2J1/giWWoBab1LYmS1Tjxu/XZMcRhEC0GOSoIngxLUArPT5SeORexgXPzTcXmTtOk8PyWA 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: On Sun, Aug 04, 2024 at 05:16:49PM +0200, Danilo Krummrich wrote: > On Sun, Aug 04, 2024 at 02:41:26PM +0200, Danilo Krummrich wrote: > > On Sat, Aug 03, 2024 at 11:44:58PM -0700, Boqun Feng wrote: > > > On Thu, Aug 01, 2024 at 02:02:05AM +0200, 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()`. > > > > > > > > Signed-off-by: Danilo Krummrich > > > > --- > > > > rust/helpers.c | 8 ++++++++ > > > > rust/kernel/alloc/allocator.rs | 24 ++++++++++++++++++++++++ > > > > rust/kernel/alloc/allocator_test.rs | 1 + > > > > 3 files changed, 33 insertions(+) > > > > > > > > diff --git a/rust/helpers.c b/rust/helpers.c > > > > index 92d3c03ae1bd..4c628986f0c9 100644 > > > > --- a/rust/helpers.c > > > > +++ b/rust/helpers.c > > > > @@ -33,6 +33,7 @@ > > > > #include > > > > #include > > > > #include > > > > +#include > > > > #include > > > > #include > > > > > > > > @@ -200,6 +201,13 @@ rust_helper_krealloc(const void *objp, size_t new_size, gfp_t flags) > > > > } > > > > EXPORT_SYMBOL_GPL(rust_helper_krealloc); > > > > > > > > +void * __must_check __realloc_size(2) > > > > +rust_helper_vrealloc(const void *p, size_t size, gfp_t flags) > > > > +{ > > > > + return 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/allocator.rs > > > > index 397ae5bcc043..e9a3d0694f41 100644 > > > > --- a/rust/kernel/alloc/allocator.rs > > > > +++ b/rust/kernel/alloc/allocator.rs > > > > @@ -16,6 +16,12 @@ > > > > /// `bindings::krealloc`. > > > > pub struct Kmalloc; > > > > > > > > +/// The virtually contiguous kernel allocator. > > > > +/// > > > > +/// The vmalloc allocator allocates pages from the page level allocator and maps them into the > > > > +/// contiguous kernel virtual space. > > > > +pub struct Vmalloc; > > > > + > > > > /// 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. > > > > @@ -58,6 +64,10 @@ fn krealloc() -> Self { > > > > Self(bindings::krealloc) > > > > } > > > > > > > > + fn vrealloc() -> Self { > > > > + Self(bindings::vrealloc) > > > > + } > > > > + > > > > // SAFETY: `call` has the exact same safety requirements as `Allocator::realloc`. > > > > unsafe fn call( > > > > &self, > > > > @@ -136,6 +146,20 @@ 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 = ReallocFunc::vrealloc(); > > > > + > > > > > > IIUC, vrealloc() calls __vmalloc_noprof() in allocation case, that is > > > calling __vmalloc_node_noprof() with align=1. In such a case, how would > > > vmalloc() guarantee the allocated memory is aligned to layout.align()? > > > > True, good catch. I thought of this a while ago and then forgot to fix it. > > Just for clarification, we're always PAGE_SIZE aligned (guaranteed by > __alloc_vmap_area()), which probably would always be sufficient. That's why I > didn't gave it too much attention in the first place and then forgot about it. > > However, we indeed do not honor layout.align() if it's larger than PAGE_SIZE. Another note on that: My plan for this series was to just fail allocation for alignment requests larger than PAGE_SIZE. And, if required, address larger alignments in a later series, since this one is probably big enough already. However, for `Vmalloc` we could support it right away, since it's trivial. For `KVmalloc` though it requires a bit more effort. For consistancy it would probably be better to support alignments larger than PAGE_SIZE either for `Vmalloc` and `KVmalloc` or neither of those though. My personal tendency goes a bit more into the direction of picking consistancy. Any other opinions? > > > > > > > > > [Cc Vlastimil] > > > > > > Regards, > > > Boqun > > > > > > > + // 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) } > > > > + } > > > > +} > > > > + > > > > #[global_allocator] > > > > static ALLOCATOR: Kmalloc = Kmalloc; > > > > > > > > diff --git a/rust/kernel/alloc/allocator_test.rs b/rust/kernel/alloc/allocator_test.rs > > > > index 4785efc474a7..e7bf2982f68f 100644 > > > > --- a/rust/kernel/alloc/allocator_test.rs > > > > +++ b/rust/kernel/alloc/allocator_test.rs > > > > @@ -7,6 +7,7 @@ > > > > use core::ptr::NonNull; > > > > > > > > pub struct Kmalloc; > > > > +pub type Vmalloc = Kmalloc; > > > > > > > > unsafe impl Allocator for Kmalloc { > > > > unsafe fn realloc( > > > > -- > > > > 2.45.2 > > > > > > >