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 E9164C7115B for ; Thu, 26 Jun 2025 08:37:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 787F16B00AB; Thu, 26 Jun 2025 04:37:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 737106B00AC; Thu, 26 Jun 2025 04:37:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 626DC6B00AD; Thu, 26 Jun 2025 04:37:00 -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 4B30A6B00AB for ; Thu, 26 Jun 2025 04:37:00 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 0233D80977 for ; Thu, 26 Jun 2025 08:36:59 +0000 (UTC) X-FDA: 83596896600.22.6948350 Received: from mailrelay3-3.pub.mailoutpod3-cph3.one.com (mailrelay3-3.pub.mailoutpod3-cph3.one.com [46.30.212.2]) by imf15.hostedemail.com (Postfix) with ESMTP id E1CFFA000B for ; Thu, 26 Jun 2025 08:36:57 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=konsulko.se header.s=rsa1 header.b=QKEutYsi; dkim=pass header.d=konsulko.se header.s=ed1 header.b=5NJ99EEw; spf=none (imf15.hostedemail.com: domain of vitaly.wool@konsulko.se has no SPF policy when checking 46.30.212.2) 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=1750927018; 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=eY2PvRpyBIA97uJAt2Gqql9RLK3sKrcnmWF3Jybxl34=; b=kharETqOKY/vnq2TzDIDzJ5iy1ZJp+rL2zGRqmOuY+z3attDOQg073LiKTMwo4xZPxF9DX UuarxIq7URaQqITOuRfrgNNi3wHEyp1ycyZS9gvXJxsI6XRRI+AuIR7znhLeeZK5DV/jd7 tjAWqlvp+JUAekjf5Ydw5lnO3vIFoAI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1750927018; a=rsa-sha256; cv=none; b=lnUtGu+7KnMySBsCero+uOt3G28VBnTEF9nt3H/lt7yNFNwkc6V8a0jGw950bkcpb6ubGF R0cvhAu7BW7kApui4N3KkVi6KqaT9JJ2ojhz08IUzskMFdcXQugSW0IMVCWRe8FbLyhI/w 6WRF2NsjSbdnsch/+murn7UCo0fIFVM= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=konsulko.se header.s=rsa1 header.b=QKEutYsi; dkim=pass header.d=konsulko.se header.s=ed1 header.b=5NJ99EEw; spf=none (imf15.hostedemail.com: domain of vitaly.wool@konsulko.se has no SPF policy when checking 46.30.212.2) smtp.mailfrom=vitaly.wool@konsulko.se; dmarc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1750927016; x=1751531816; d=konsulko.se; s=rsa1; h=content-transfer-encoding:mime-version:references:in-reply-to:message-id:date: subject:cc:to:from:from; bh=eY2PvRpyBIA97uJAt2Gqql9RLK3sKrcnmWF3Jybxl34=; b=QKEutYsiXmFvFBsUfSQmw8LXHu7Nfuo+e68Hc5tXHU+S8ex6ZXK5lU22xkT8Z9q/PxmgQRoBf1Q3Z yfzr1unCGpB3MWu1WkCWaX4sM94zAj/wpQwEhZTNn6jm2oJACWGfMoeUqxX+fXeC4plwTJIjUgqX4J GvmTXchwdJjJR8icpEZq0mudHnj3O9fIjl7F6Xe4Xl6bETePbwBSWza+YQW0US1XbIROwREZC8AoRx SgRJdlju7amQvQ0vHxj+p8woDi/t3HeA9a/K+xyYhClkKrO5I3U7YPlI0jKdkLyg20E0ywLALeqKB9 E/JsFF5lpKig8M/POPwKDPmjyJsi0tg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1750927016; x=1751531816; d=konsulko.se; s=ed1; h=content-transfer-encoding:mime-version:references:in-reply-to:message-id:date: subject:cc:to:from:from; bh=eY2PvRpyBIA97uJAt2Gqql9RLK3sKrcnmWF3Jybxl34=; b=5NJ99EEwK9PPy829s2VDLjcc6yLi+kA/M0eIYzUp5WovVkAJUNMRG8oqGMiqPBUy+1RdKmwJNpL1l CeU/xojCg== X-HalOne-ID: b680223f-5268-11f0-86c4-2b8368a4d5c5 Received: from localhost.localdomain (host-90-238-19-233.mobileonline.telia.com [90.238.19.233]) by mailrelay3.pub.mailoutpod3-cph3.one.com (Halon) with ESMTPSA id b680223f-5268-11f0-86c4-2b8368a4d5c5; Thu, 26 Jun 2025 08:36:55 +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 v4 4/4] rust: support NUMA ids in allocations Date: Thu, 26 Jun 2025 10:36:53 +0200 Message-Id: <20250626083653.3596424-1-vitaly.wool@konsulko.se> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250626083516.3596197-1-vitaly.wool@konsulko.se> References: <20250626083516.3596197-1-vitaly.wool@konsulko.se> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: E1CFFA000B X-Stat-Signature: t1fa9muu6kquhgmuiujptncaij4z119q X-Rspam-User: X-HE-Tag: 1750927017-93281 X-HE-Meta: U2FsdG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zT9cXcHHNkmOImPNUQkeP1WFFLXKt7o9NDLgMrrNWD0RjMV/uFq7l1nqIW5nqmWsDFkhq/dceYR18+VNyzHUhwk7NvhOD+yXSq1CvGOPmtXX/K6vbW9IH0kvB0hnmJD9mZsqx0O3tSc= 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 specifying NUMA ids in Rust allocators as an Option (i. e. providing `None` as nid corresponds to NUMA_NO_NODE). This will allow to specify node to use for allocation of e. g. {KV}Box. Signed-off-by: Vitaly Wool --- rust/helpers/slab.c | 9 +++++---- rust/helpers/vmalloc.c | 4 ++-- rust/kernel/alloc.rs | 28 ++++++++++++++++++++++++++-- rust/kernel/alloc/allocator.rs | 26 ++++++++++++++++++-------- rust/kernel/alloc/kvec.rs | 3 ++- 5 files changed, 53 insertions(+), 17 deletions(-) diff --git a/rust/helpers/slab.c b/rust/helpers/slab.c index 5e9e8dd2bba0..ab1cf72f8353 100644 --- a/rust/helpers/slab.c +++ b/rust/helpers/slab.c @@ -3,13 +3,14 @@ #include void * __must_check __realloc_size(2) -rust_helper_krealloc(const void *objp, size_t new_size, unsigned long align, gfp_t flags) +rust_helper_krealloc_node(const void *objp, size_t new_size, unsigned long align, gfp_t flags, + int nid) { - return krealloc(objp, new_size, flags); + return krealloc_node(objp, new_size, flags, nid); } void * __must_check __realloc_size(2) -rust_helper_kvrealloc(const void *p, size_t size, unsigned long align, gfp_t flags) +rust_helper_kvrealloc_node(const void *p, size_t size, unsigned long align, gfp_t flags, int nid) { - return kvrealloc(p, size, flags); + return kvrealloc_node(p, size, flags, nid); } diff --git a/rust/helpers/vmalloc.c b/rust/helpers/vmalloc.c index 4618c0b79283..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, unsigned long align, gfp_t flags) +rust_helper_vrealloc_node(const void *p, size_t size, unsigned long align, gfp_t flags, int node) { - return vrealloc_node(p, size, align, flags, NUMA_NO_NODE); + return vrealloc_node(p, size, align, flags, node); } diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs index a2c49e5494d3..1e26c2a7f47c 100644 --- a/rust/kernel/alloc.rs +++ b/rust/kernel/alloc.rs @@ -156,7 +156,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`. @@ -196,6 +219,7 @@ unsafe fn realloc( layout: Layout, old_layout: Layout, flags: Flags, + nid: Option, ) -> Result, AllocError>; /// Free an existing memory allocation. @@ -211,7 +235,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 a0d78c497974..5a0d0a57dfe5 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -58,18 +58,19 @@ 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, 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 { // INVARIANT: `krealloc` satisfies the type invariants. - const KREALLOC: Self = Self(bindings::krealloc); + const KREALLOC: Self = Self(bindings::krealloc_node); // 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); + const KVREALLOC: Self = Self(bindings::kvrealloc_node); /// # Safety /// @@ -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, layout.align(), 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, nid) } } } @@ -151,10 +159,11 @@ unsafe fn realloc( layout: Layout, old_layout: Layout, flags: Flags, + nid: Option, ) -> Result, 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) } } } @@ -169,9 +178,10 @@ unsafe fn realloc( layout: Layout, old_layout: Layout, flags: Flags, + nid: Option ) -> Result, AllocError> { // 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 { ReallocFunc::KVREALLOC.call(ptr, layout, old_layout, flags, nid) } } } 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