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 58F42C77B7F for ; Fri, 27 Jun 2025 21:26:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EB54D6B00B7; Fri, 27 Jun 2025 17:26:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E3EC06B00BB; Fri, 27 Jun 2025 17:26:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D2E6E6B00BC; Fri, 27 Jun 2025 17:26:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id BC4EC6B00B7 for ; Fri, 27 Jun 2025 17:26:25 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 847F610439E for ; Fri, 27 Jun 2025 21:26:25 +0000 (UTC) X-FDA: 83602464330.24.6D3A3AC Received: from mailrelay-egress12.pub.mailoutpod2-cph3.one.com (mailrelay-egress12.pub.mailoutpod2-cph3.one.com [46.30.211.187]) by imf05.hostedemail.com (Postfix) with ESMTP id C0D99100010 for ; Fri, 27 Jun 2025 21:26:23 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=konsulko.se header.s=rsa1 header.b=Gz5A6VFe; dkim=pass header.d=konsulko.se header.s=ed1 header.b="///1GzXm" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1751059583; a=rsa-sha256; cv=none; b=2jQmnaKbIxK9wjRarbAxcpBky8/muG2j+/IOlUsxiI6zTRFObUtdlmMWpzhmQUcMryy/2e nJ0C3cyvu6huaBmnSFGWZvhnlrIquIoP1gWXNEOHCsa4B2ReJyIyTaszAwEvwgg2doNDiW WXhKR8qR8/2sgGtr/riO9jYGYYl2Zz0= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=konsulko.se header.s=rsa1 header.b=Gz5A6VFe; dkim=pass header.d=konsulko.se header.s=ed1 header.b="///1GzXm"; dmarc=none; spf=none (imf05.hostedemail.com: domain of vitaly.wool@konsulko.se has no SPF policy when checking 46.30.211.187) smtp.mailfrom=vitaly.wool@konsulko.se ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1751059583; 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=anTwVwyLd1QLV7blMpi25BlhA5diqP2cKXl07/Bz8gyF1S9bIFbwn+hAqJNyIpoj+jl11U 0VNrEQswn45V2ZXXXW71mSEK9MrjYmjZH0jdfHj6ABceXCksjeCo//OaZQFxn+hZk2a3EC traC5NWKoiiXvfXLdiMj2BLMepkGhqs= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1751059582; x=1751664382; 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=Gz5A6VFeSO753UAcSLu/qYqVAig1R6dM2/XcUCoD6L7WUPlYX9zrcDp4wTARcTYG4A61wIQSfSZ0T s6HI7yqGgtshRTuJ/QwJ5eDZ5dajrOVa4GsXCnpOKqI4AFXkSroPV6fy8DEBmHbFOz9v2MEIas5LNA KZyknULVvsBvhH9Syut0G3WSadCj3qW9j8yMvvPiLuxDM+XZjmZ9sGz3f1M1FFDpa+ql+oklwtgmML LVqpI6jDm5OUlebAYJ1W2F1JTLUBvQiGy7q2TKMO0r/WCYJwKVecCuGlC8cAcyuc+1eOdanWYHvtfM 0Q/W6PqIE7ijmM9+YK7uG300G47YHHQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1751059582; x=1751664382; 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=///1GzXmu36VL+aGYgMpb3bktZ4JyJXqKjBUbfcv+HYUehQMGuoKEPwE51RGuDmtj2ViR09N+hFmO qYgPADsCg== X-HalOne-ID: 5ec8b48c-539d-11f0-bb6d-4f541c8bf1cc Received: from localhost.localdomain (host-90-238-19-233.mobileonline.telia.com [90.238.19.233]) by mailrelay3.pub.mailoutpod2-cph3.one.com (Halon) with ESMTPSA id 5ec8b48c-539d-11f0-bb6d-4f541c8bf1cc; Fri, 27 Jun 2025 21:26:22 +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 v7 4/4] rust: support large alignments in allocations Date: Fri, 27 Jun 2025 23:26:17 +0200 Message-Id: <20250627212617.2181779-1-vitaly.wool@konsulko.se> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250627212451.2181627-1-vitaly.wool@konsulko.se> References: <20250627212451.2181627-1-vitaly.wool@konsulko.se> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Stat-Signature: 3gxgy3ogz1nbxqixxgoeetdcukuh7snm X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: C0D99100010 X-Rspam-User: X-HE-Tag: 1751059583-35965 X-HE-Meta: U2FsdGVkX1+6eibeZVzQoxVjDYVhdsaYC8TogTzPWRsVQyUEcl20EYho7CZ7iD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=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. 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