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 969DAC8302F for ; Mon, 30 Jun 2025 22:17:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3B06A6B00AB; Mon, 30 Jun 2025 18:17:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 387AA6B00AC; Mon, 30 Jun 2025 18:17:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2799F6B00AD; Mon, 30 Jun 2025 18:17:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 139D86B00AB for ; Mon, 30 Jun 2025 18:17:00 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B2D1DC0194 for ; Mon, 30 Jun 2025 22:16:59 +0000 (UTC) X-FDA: 83613478158.01.0FFC4FC Received: from mailrelay4-3.pub.mailoutpod3-cph3.one.com (mailrelay4-3.pub.mailoutpod3-cph3.one.com [46.30.212.3]) by imf12.hostedemail.com (Postfix) with ESMTP id D362340002 for ; Mon, 30 Jun 2025 22:16:57 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=konsulko.se header.s=rsa1 header.b=gzIw72ke; dkim=pass header.d=konsulko.se header.s=ed1 header.b=Z63KqWek ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1751321818; a=rsa-sha256; cv=none; b=TrNeKFLPcaGSK7R7vZdrd8obCyeg7TbCmyvQEgcLpDdbSwagxSxpfvgobR0oRKImnLHjMO wznNhy2sKMSiWXoK8wS/vp0Rwmbc5q8LjAukS9AIIynNMxAhZSY0F+pcNtBQBmS4YjbWG2 atCBHTm0V7ldn2HsPCq5k4Jkvlg4ZGQ= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=konsulko.se header.s=rsa1 header.b=gzIw72ke; dkim=pass header.d=konsulko.se header.s=ed1 header.b=Z63KqWek; dmarc=none; spf=none (imf12.hostedemail.com: domain of vitaly.wool@konsulko.se has no SPF policy when checking 46.30.212.3) smtp.mailfrom=vitaly.wool@konsulko.se ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1751321818; 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=xlhdGuz/Z+dkJo2xYiSj4xZE1JqckJSK1NBWLMJ0VOE=; b=IChdn+i62Mj1DbF/8w3JzHF//uUdfxjzR2KeQYdtYNAaXKBSIe+FJeBSfqMPrgGSk1wcN5 TfY60QqqpdB5baf5tOmLXar3VsIuykJF8FpeGKUEvo//62VwXdVsw5k+Q8doo524PkPQYi eJi36FHL8buhJBQeI332x3oCVK/4/bo= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1751321816; x=1751926616; d=konsulko.se; s=rsa1; h=content-transfer-encoding:mime-version:references:in-reply-to:message-id:date: subject:cc:to:from:from; bh=xlhdGuz/Z+dkJo2xYiSj4xZE1JqckJSK1NBWLMJ0VOE=; b=gzIw72ke3MazTuhdxrRztK6R2Yw4oEdiz6vndPzAc1MDC19knX725alEn3Rwk5sbS3EKkjjGPoGHm 2JtLwf40LsHXhRVKzguoYH+WdK/Ac4/kH56JkhezyU7eC5C3zme+cwxXyV6dTyrZv9W/XpEhEA3I3A NspzpHuURJhb0wFbePLoFS2VPAdJ3GeD/ngYEx2s/J+iy0lW8AW6fTg30u+qTvYp95PHMyOBN4D1QF JlSE5kjTVkeTdVzQO+D2rapf4Q6tEjsq7hDeUsK8hlvuqKFHX80cgsFXHhy6dZvEIVAUWf0VF04Whn YnRJ30jEhzkL9TrRNFs+h49rdKWHz8Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1751321816; x=1751926616; d=konsulko.se; s=ed1; h=content-transfer-encoding:mime-version:references:in-reply-to:message-id:date: subject:cc:to:from:from; bh=xlhdGuz/Z+dkJo2xYiSj4xZE1JqckJSK1NBWLMJ0VOE=; b=Z63KqWekIbMKVQhsvGJ9ejEtf0+nL8ZL0Xjg4Tpe+N3tMohLbta5EIkomnd9NXzxrkWtZaAUCqkj9 EWirzn0Bg== X-HalOne-ID: ee37d59e-55ff-11f0-b560-29b2d794c87d Received: from slottsdator.home (host-90-238-19-233.mobileonline.telia.com [90.238.19.233]) by mailrelay4.pub.mailoutpod3-cph3.one.com (Halon) with ESMTPSA id ee37d59e-55ff-11f0-b560-29b2d794c87d; Mon, 30 Jun 2025 22:16: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 large alignments in allocations Date: Tue, 1 Jul 2025 00:16:53 +0200 Message-Id: <20250630221653.3325329-1-vitaly.wool@konsulko.se> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250630221511.3325123-1-vitaly.wool@konsulko.se> References: <20250630221511.3325123-1-vitaly.wool@konsulko.se> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: D362340002 X-Stat-Signature: 3obbr1yzz4fw4ykzsxssxf788fhr6seg X-HE-Tag: 1751321817-379890 X-HE-Meta: U2FsdGVkX19KdTs1sEMEtYu8UeKZwq3my9qCDRVUOcJbSxjvBH4G9DTG3NmkZTfOVB2jkMMbfm2yt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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. All the preparations on the C side are already done, we just need to add bindings for _node_align() functions and start using those. Signed-off-by: Vitaly Wool --- rust/helpers/slab.c | 10 ++++++---- rust/helpers/vmalloc.c | 5 +++-- rust/kernel/alloc/allocator.rs | 28 ++++++++-------------------- 3 files changed, 17 insertions(+), 26 deletions(-) diff --git a/rust/helpers/slab.c b/rust/helpers/slab.c index 8472370a4338..d729be798f31 100644 --- a/rust/helpers/slab.c +++ b/rust/helpers/slab.c @@ -3,13 +3,15 @@ #include void * __must_check __realloc_size(2) -rust_helper_krealloc_node(const void *objp, size_t new_size, gfp_t flags, int node) +rust_helper_krealloc_node_align(const void *objp, size_t new_size, unsigned long align, + gfp_t flags, int node) { - return krealloc_node(objp, new_size, flags, node); + return krealloc_node_align(objp, new_size, align, flags, node); } void * __must_check __realloc_size(2) -rust_helper_kvrealloc_node(const void *p, size_t size, gfp_t flags, int node) +rust_helper_kvrealloc_node_align(const void *p, size_t size, unsigned long align, + gfp_t flags, int node) { - return kvrealloc_node(p, size, flags, node); + return kvrealloc_node_align(p, size, align, flags, node); } diff --git a/rust/helpers/vmalloc.c b/rust/helpers/vmalloc.c index 62d30db9a1a6..7d7f7336b3d2 100644 --- a/rust/helpers/vmalloc.c +++ b/rust/helpers/vmalloc.c @@ -3,7 +3,8 @@ #include void * __must_check __realloc_size(2) -rust_helper_vrealloc_node(const void *p, size_t size, gfp_t flags, int node) +rust_helper_vrealloc_node_align(const void *p, size_t size, unsigned long align, + gfp_t flags, int node) { - return vrealloc_node(p, size, flags, node); + return vrealloc_node_align(p, size, align, flags, node); } diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs index 2e86e9839a1b..58e5bf78c159 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -59,19 +59,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, u32, crate::ffi::c_int, + *const crate::ffi::c_void, usize, crate::ffi::c_ulong, u32, crate::ffi::c_int, ) -> *mut crate::ffi::c_void, ); impl ReallocFunc { - // INVARIANT: `krealloc_node` satisfies the type invariants. - const KREALLOC: Self = Self(bindings::krealloc_node); + // INVARIANT: `krealloc_node_align` satisfies the type invariants. + const KREALLOC: Self = Self(bindings::krealloc_node_align); - // INVARIANT: `vrealloc_node` satisfies the type invariants. - const VREALLOC: Self = Self(bindings::vrealloc_node); + // INVARIANT: `vrealloc_node_align` satisfies the type invariants. + const VREALLOC: Self = Self(bindings::vrealloc_node_align); - // INVARIANT: `kvrealloc_node` satisfies the type invariants. - const KVREALLOC: Self = Self(bindings::kvrealloc_node); + // INVARIANT: `kvrealloc_node_align` satisfies the type invariants. + const KVREALLOC: Self = Self(bindings::kvrealloc_node_align); /// # Safety /// @@ -113,7 +113,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, nid.0).cast() + self.0(ptr.cast(), size, layout.align(), flags.0, nid.0).cast() }; let ptr = if size == 0 { @@ -157,12 +157,6 @@ unsafe fn realloc_node( flags: Flags, nid: NumaNode, ) -> 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, nid) } @@ -182,12 +176,6 @@ unsafe fn realloc_node( flags: Flags, nid: NumaNode, ) -> 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); - } - // 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, nid) } -- 2.39.2