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 2F085C7115C for ; Wed, 25 Jun 2025 06:30:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C8B6E6B00B0; Wed, 25 Jun 2025 02:30:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C14C06B00B5; Wed, 25 Jun 2025 02:30:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AB5A76B00B6; Wed, 25 Jun 2025 02:30:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 953266B00B0 for ; Wed, 25 Jun 2025 02:30:39 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 205471232F9 for ; Wed, 25 Jun 2025 06:30:39 +0000 (UTC) X-FDA: 83592949398.03.67AF489 Received: from mailrelay5-3.pub.mailoutpod3-cph3.one.com (mailrelay5-3.pub.mailoutpod3-cph3.one.com [46.30.212.10]) by imf29.hostedemail.com (Postfix) with ESMTP id 5AB85120004 for ; Wed, 25 Jun 2025 06:30:37 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=konsulko.se header.s=rsa1 header.b=OA76rY39; dkim=pass header.d=konsulko.se header.s=ed1 header.b=5cnH5iKX; spf=none (imf29.hostedemail.com: domain of vitaly.wool@konsulko.se has no SPF policy when checking 46.30.212.10) smtp.mailfrom=vitaly.wool@konsulko.se; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1750833037; 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=fSXvnx+fgvE5GXly6XPTqjm0PPBXVKwM6V3gQyXQXoE=; b=50oNMNo/XNWXGYQMg6QR9lwAW6wcFpxOO/C6HEnMLy/aTiSE81Uxh/lKGS0YDcupoNiAmg 9/9F/Qiro3xIZ5E/g1n0eCCfljJf5/MPhlp+cNl7dF9UtHkKkMIbDPkpX53gz+nU8T5dYm BxhNbwU4FdyNDz9W+oH+2ORreDr2cso= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1750833037; a=rsa-sha256; cv=none; b=w0YOuEi1A9XImKdJxo/Bj9KyNoKk7mvhopiuECMsrAo9uaxAj3Ovz3K0ziiEhfkbn4fPYM ScU1cj4EDM1rrbRUgd5sGswjvb+ugY+pP4tPcQnGF69BWj5eCqAv2pPC74ljd9kA1wZgmL 6kG6c6m3jg12Gp6+1+DWkMU2yjEnu50= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=konsulko.se header.s=rsa1 header.b=OA76rY39; dkim=pass header.d=konsulko.se header.s=ed1 header.b=5cnH5iKX; spf=none (imf29.hostedemail.com: domain of vitaly.wool@konsulko.se has no SPF policy when checking 46.30.212.10) smtp.mailfrom=vitaly.wool@konsulko.se; dmarc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1750833036; x=1751437836; d=konsulko.se; s=rsa1; h=content-transfer-encoding:mime-version:references:in-reply-to:message-id:date: subject:cc:to:from:from; bh=fSXvnx+fgvE5GXly6XPTqjm0PPBXVKwM6V3gQyXQXoE=; b=OA76rY39MWM22xsVJ7hjXljB1U16EOfI9O2OKw1kJEMLHNWpXNAgZQtS729VvpJkNjC6P+eeC3lzk c7/0/Q49KS8JL18LrHlcR99GInfun+HCs9uAtMi2UNhVDLot70cwryaXoyKk9qa0ORGHQyL3iEQU5A 0hl/j+XSJuxaQOad70p2vvSdatpluNbV8pwEIsb0Z3msrgc/fGWE+CPu0t9cUKt0Tr98O5SDSOeo/q PPAHx5C59dkmjTljVQCEzY9TZafPrgNHAL0FzauEdKJNVVa0nUDL/uNMZsW5xhrBF7AdFavVos44p8 B3ZYX0MFEduRh6j3ThTboU4AUZLx2YA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1750833036; x=1751437836; d=konsulko.se; s=ed1; h=content-transfer-encoding:mime-version:references:in-reply-to:message-id:date: subject:cc:to:from:from; bh=fSXvnx+fgvE5GXly6XPTqjm0PPBXVKwM6V3gQyXQXoE=; b=5cnH5iKXg5YmwXPrzVkNy8nYsX5vAWWPC5gI3yCxuRNWu586PmJyafz7HXDLMa/iqALpNMCI8A3bh U/QejuRCA== X-HalOne-ID: e5c569ab-518d-11f0-b995-d7c209f8bd06 Received: from localhost.localdomain (host-90-238-19-233.mobileonline.telia.com [90.238.19.233]) by mailrelay5.pub.mailoutpod3-cph3.one.com (Halon) with ESMTPSA id e5c569ab-518d-11f0-b995-d7c209f8bd06; Wed, 25 Jun 2025 06:30:35 +0000 (UTC) From: Vitaly Wool To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, linux-kernel@vger.kernel.org, Uladzislau Rezki , Danilo Krummrich , Alice Ryhl , rust-for-linux@vger.kernel.org, Vitaly Wool Subject: [PATCH v3 2/2] rust: support align and NUMA id in allocations Date: Wed, 25 Jun 2025 08:30:26 +0200 Message-Id: <20250625063026.3379921-1-vitaly.wool@konsulko.se> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250625062917.3379804-1-vitaly.wool@konsulko.se> References: <20250625062917.3379804-1-vitaly.wool@konsulko.se> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 5AB85120004 X-Stat-Signature: muq1hsnr7m4ioxueeh69owfhtig3wgsi X-Rspam-User: X-HE-Tag: 1750833037-876617 X-HE-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F6iEou3pe19seQlqhl5oYdxVosPaW9XbiE1zcNS/CEFntWyunGHSrTOYa62OzRtg26kDn/WrK3iX0aRSJCea/BvE3zvAGNLhmcha2Nk/uWsnHZywyyTcdoTFyXOLqlbJd23ruEt+UdRFMvlUXnJE= 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: Add support for large (> PAGE_SIZE) alignments in Rust allocators (Kmalloc support for large alignments is limited to the requested size, which is a reasonable limitation anyway). Besides, add support for NUMA id to Vmalloc. Signed-off-by: Vitaly Wool --- rust/helpers/slab.c | 8 +++++-- rust/helpers/vmalloc.c | 4 ++-- rust/kernel/alloc.rs | 28 ++++++++++++++++++++++-- rust/kernel/alloc/allocator.rs | 40 +++++++++++++++++++--------------- rust/kernel/alloc/kvec.rs | 3 ++- 5 files changed, 59 insertions(+), 24 deletions(-) diff --git a/rust/helpers/slab.c b/rust/helpers/slab.c index a842bfbddcba..221c517f57a1 100644 --- a/rust/helpers/slab.c +++ b/rust/helpers/slab.c @@ -3,13 +3,17 @@ #include void * __must_check __realloc_size(2) -rust_helper_krealloc(const void *objp, size_t new_size, gfp_t flags) +rust_helper_krealloc(const void *objp, size_t new_size, unsigned long align, gfp_t flags, int nid) { + if (WARN_ON(new_size & (align - 1))) + return NULL; return krealloc(objp, new_size, flags); } void * __must_check __realloc_size(2) -rust_helper_kvrealloc(const void *p, size_t size, gfp_t flags) +rust_helper_kvrealloc(const void *p, size_t size, unsigned long align, gfp_t flags, int nid) { + if (WARN_ON(size & (align - 1))) + return NULL; return kvrealloc(p, size, flags); } diff --git a/rust/helpers/vmalloc.c b/rust/helpers/vmalloc.c index 80d34501bbc0..9131279222fa 100644 --- a/rust/helpers/vmalloc.c +++ b/rust/helpers/vmalloc.c @@ -3,7 +3,7 @@ #include void * __must_check __realloc_size(2) -rust_helper_vrealloc(const void *p, size_t size, gfp_t flags) +rust_helper_vrealloc_node(const void *p, size_t size, unsigned long align, gfp_t flags, int node) { - return vrealloc(p, size, flags); + return vrealloc_node(p, size, align, flags, node); } diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs index 2e377c52fa07..12a723bf6092 100644 --- a/rust/kernel/alloc.rs +++ b/rust/kernel/alloc.rs @@ -161,7 +161,30 @@ pub unsafe trait Allocator { fn alloc(layout: Layout, flags: Flags) -> Result, AllocError> { // SAFETY: Passing `None` to `realloc` is valid by its safety requirements and asks for a // new memory allocation. - unsafe { Self::realloc(None, layout, Layout::new::<()>(), flags) } + unsafe { Self::realloc(None, layout, Layout::new::<()>(), flags, None) } + } + + /// Allocate memory based on `layout`, `flags` and `nid`. + /// + /// On success, returns a buffer represented as `NonNull<[u8]>` that satisfies the layout + /// constraints (i.e. minimum size and alignment as specified by `layout`). + /// + /// This function is equivalent to `realloc` when called with `None`. + /// + /// # Guarantees + /// + /// When the return value is `Ok(ptr)`, then `ptr` is + /// - valid for reads and writes for `layout.size()` bytes, until it is passed to + /// [`Allocator::free`] or [`Allocator::realloc`], + /// - aligned to `layout.align()`, + /// + /// Additionally, `Flags` are honored as documented in + /// . + fn alloc_node(layout: Layout, flags: Flags, nid: Option) + -> Result, AllocError> { + // SAFETY: Passing `None` to `realloc` is valid by its safety requirements and asks for a + // new memory allocation. + unsafe { Self::realloc(None, layout, Layout::new::<()>(), flags, nid) } } /// Re-allocate an existing memory allocation to satisfy the requested `layout`. @@ -201,6 +224,7 @@ unsafe fn realloc( layout: Layout, old_layout: Layout, flags: Flags, + nid: Option, ) -> Result, AllocError>; /// Free an existing memory allocation. @@ -216,7 +240,7 @@ unsafe fn free(ptr: NonNull, layout: Layout) { // SAFETY: The caller guarantees that `ptr` points at a valid allocation created by this // allocator. We are passing a `Layout` with the smallest possible alignment, so it is // smaller than or equal to the alignment previously used with this allocation. - let _ = unsafe { Self::realloc(Some(ptr), Layout::new::<()>(), layout, Flags(0)) }; + let _ = unsafe { Self::realloc(Some(ptr), Layout::new::<()>(), layout, Flags(0), None) }; } } diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs index aa2dfa9dca4c..91b36e128b92 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -58,7 +58,8 @@ fn aligned_size(new_layout: Layout) -> usize { /// /// One of the following: `krealloc`, `vrealloc`, `kvrealloc`. struct ReallocFunc( - unsafe extern "C" fn(*const crate::ffi::c_void, usize, u32) -> *mut crate::ffi::c_void, + unsafe extern "C" fn(*const crate::ffi::c_void, usize, usize, u32, i32) + -> *mut crate::ffi::c_void, ); impl ReallocFunc { @@ -66,7 +67,7 @@ impl ReallocFunc { const KREALLOC: Self = Self(bindings::krealloc); // INVARIANT: `vrealloc` satisfies the type invariants. - const VREALLOC: Self = Self(bindings::vrealloc); + const VREALLOC: Self = Self(bindings::vrealloc_node); // INVARIANT: `kvrealloc` satisfies the type invariants. const KVREALLOC: Self = Self(bindings::kvrealloc); @@ -87,6 +88,7 @@ unsafe fn call( layout: Layout, old_layout: Layout, flags: Flags, + nid: Option, ) -> Result, AllocError> { let size = aligned_size(layout); let ptr = match ptr { @@ -100,6 +102,11 @@ unsafe fn call( None => ptr::null(), }; + let c_nid = match nid { + None => bindings::NUMA_NO_NODE, + Some(n) => n, + }; + // SAFETY: // - `self.0` is one of `krealloc`, `vrealloc`, `kvrealloc` and thus only requires that // `ptr` is NULL or valid. @@ -110,7 +117,7 @@ unsafe fn call( // - Those functions provide the guarantees 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() + self.0(ptr.cast(), size, layout.align(), flags.0, c_nid).cast() }; let ptr = if size == 0 { @@ -134,9 +141,10 @@ unsafe fn realloc( layout: Layout, old_layout: Layout, flags: Flags, + _nid: Option, ) -> Result, AllocError> { // SAFETY: `ReallocFunc::call` has the same safety requirements as `Allocator::realloc`. - unsafe { ReallocFunc::KREALLOC.call(ptr, layout, old_layout, flags) } + unsafe { ReallocFunc::KREALLOC.call(ptr, layout, old_layout, flags, None) } } } @@ -151,16 +159,11 @@ unsafe fn realloc( layout: Layout, old_layout: Layout, flags: Flags, + nid: Option, ) -> Result, AllocError> { - // 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 valid memory, which was previously // allocated with this `Allocator`. - unsafe { ReallocFunc::VREALLOC.call(ptr, layout, old_layout, flags) } + unsafe { ReallocFunc::VREALLOC.call(ptr, layout, old_layout, flags, nid) } } } @@ -175,15 +178,18 @@ unsafe fn realloc( layout: Layout, old_layout: Layout, flags: Flags, + _nid: Option, ) -> Result, AllocError> { - // TODO: Support alignments larger than PAGE_SIZE. - if layout.align() > bindings::PAGE_SIZE { - pr_warn!("KVmalloc does not support alignments larger than PAGE_SIZE yet.\n"); - return Err(AllocError); - } + // if the caller wants to have alignment bigger than PAGE_SIZE + // it's only vmalloc we can offer to satisfy that request + let alloc_func = if layout.align() > bindings::PAGE_SIZE { + ReallocFunc::VREALLOC + } else { + ReallocFunc::KVREALLOC + }; // SAFETY: If not `None`, `ptr` is guaranteed to point to valid memory, which was previously // allocated with this `Allocator`. - unsafe { ReallocFunc::KVREALLOC.call(ptr, layout, old_layout, flags) } + unsafe { alloc_func.call(ptr, layout, old_layout, flags, None) } } } diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs index 1a0dd852a468..ef4f977ba012 100644 --- a/rust/kernel/alloc/kvec.rs +++ b/rust/kernel/alloc/kvec.rs @@ -633,6 +633,7 @@ pub fn reserve(&mut self, additional: usize, flags: Flags) -> Result<(), AllocEr layout.into(), self.layout.into(), flags, + None, )? }; @@ -1058,7 +1059,7 @@ pub fn collect(self, flags: Flags) -> Vec { // the type invariant to be smaller than `cap`. Depending on `realloc` this operation // may shrink the buffer or leave it as it is. ptr = match unsafe { - A::realloc(Some(buf.cast()), layout.into(), old_layout.into(), flags) + A::realloc(Some(buf.cast()), layout.into(), old_layout.into(), flags, None) } { // If we fail to shrink, which likely can't even happen, continue with the existing // buffer. -- 2.39.2