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 8266DC7EE2A for ; Fri, 27 Jun 2025 18:16:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2300F6B00A8; Fri, 27 Jun 2025 14:16:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1B9426B00A9; Fri, 27 Jun 2025 14:16:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 059876B00AA; Fri, 27 Jun 2025 14:16:56 -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 E3DF06B00A8 for ; Fri, 27 Jun 2025 14:16:56 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id B42B61D39DE for ; Fri, 27 Jun 2025 18:16:56 +0000 (UTC) X-FDA: 83601986832.05.469FDFE Received: from mailrelay2-3.pub.mailoutpod3-cph3.one.com (mailrelay2-3.pub.mailoutpod3-cph3.one.com [46.30.212.1]) by imf30.hostedemail.com (Postfix) with ESMTP id C679C8000C for ; Fri, 27 Jun 2025 18:16:54 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=konsulko.se header.s=rsa1 header.b="q2T6/3Zy"; dkim=pass header.d=konsulko.se header.s=ed1 header.b=rnigHV4Q; dmarc=none; spf=none (imf30.hostedemail.com: domain of vitaly.wool@konsulko.se has no SPF policy when checking 46.30.212.1) smtp.mailfrom=vitaly.wool@konsulko.se ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1751048215; a=rsa-sha256; cv=none; b=N4lYRmN62yCAPj6qThVMi0SyPbCUvWEWFklgxf3tmIMN4/NTt4QCEg1PTLoTvG2sN9b3BP +U1s+4TTClgJOtg8/oOPGCffVIpla5JH7lJ/TfrhT/+puLiH2XvwwLHNyiz+P+053Od7dS TrH6oaSYCS9OLwst25sfpXvFvtw3dAs= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=konsulko.se header.s=rsa1 header.b="q2T6/3Zy"; dkim=pass header.d=konsulko.se header.s=ed1 header.b=rnigHV4Q; dmarc=none; spf=none (imf30.hostedemail.com: domain of vitaly.wool@konsulko.se has no SPF policy when checking 46.30.212.1) smtp.mailfrom=vitaly.wool@konsulko.se ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1751048215; 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=nGDXplcE1kynSxniFbixzug4U6mDvYyhwrjb8WHIhtg=; b=58vYt2nqtejIhi62gz791obfX5bKxT3dY1XZOTymMLKTEXIZVwz3gZj6r9qQ9xWliid+wR cBt2qtDo9XkdC9KLzYvcN5Zwl9lBcLMf14Qnb4oarOBhQOfet1OITK4Rs0CsYsb9mx53jC uj+28NKZpyetlf2aHe1VneuELFcSolY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1751048212; x=1751653012; d=konsulko.se; s=rsa1; h=content-transfer-encoding:mime-version:references:in-reply-to:message-id:date: subject:cc:to:from:from; bh=nGDXplcE1kynSxniFbixzug4U6mDvYyhwrjb8WHIhtg=; b=q2T6/3ZyOqMhuPtNThP7x9F/JyEqdmaZ+scvIPY2wYJU26q8X6AW3IsSE3ScjEt9n9DCVuIaEQi16 fdZD9dPLAJcAgjdCQlkq946utdeNwp8fyYxzbirr+zeVz40td08NVa1jQb7/Nc2rhNbAibtK+XWjfe kql6nMooVFo0fdHCReSRp/lQ7ooSJFiccQ7JzqkqN86M+uCzo8rgKBcfJK+2lPenKeiIuxU+9wpRi/ cqqRR8kkGSzGptQ+hdNEQSi17ySAPPNmQALMk/OkMGKKYILDkfUHL9+5Z59H1hh3YRz6HUprQrKxF0 /ORRCu8eCCCJ3SiuQhZcouJDRrplM/g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1751048212; x=1751653012; d=konsulko.se; s=ed1; h=content-transfer-encoding:mime-version:references:in-reply-to:message-id:date: subject:cc:to:from:from; bh=nGDXplcE1kynSxniFbixzug4U6mDvYyhwrjb8WHIhtg=; b=rnigHV4QpoNKhsBxThOlStYW8z3A/ltqFBVXlnO69ooDSHvDLnc++oaCBb0CY5gdhScDWhSmnlTxy +ku7ObGCg== X-HalOne-ID: e597f1cd-5382-11f0-b0a1-b37c246f863f Received: from slottsdator.home (host-90-238-19-233.mobileonline.telia.com [90.238.19.233]) by mailrelay2.pub.mailoutpod3-cph3.one.com (Halon) with ESMTPSA id e597f1cd-5382-11f0-b0a1-b37c246f863f; Fri, 27 Jun 2025 18:16:52 +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 v6 4/4] rust: support large alignments in allocations Date: Fri, 27 Jun 2025 20:16:50 +0200 Message-Id: <20250627181650.2081123-1-vitaly.wool@konsulko.se> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250627181505.2080916-1-vitaly.wool@konsulko.se> References: <20250627181505.2080916-1-vitaly.wool@konsulko.se> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Queue-Id: C679C8000C X-Rspamd-Server: rspam10 X-Stat-Signature: 3dxzyhw9ako6o4yoz38qrwq1pru3m1y5 X-HE-Tag: 1751048214-68521 X-HE-Meta: U2FsdGVkX1/q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u8Snw5SxQEMOamd1BQlNXcfWBI+vQl8Ni4rZwBSd5pAdVKs0ux6UWNvR9qPYNaE2DkhQZcboVtHf/2nQ2cUNkw== 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. 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 | 14 ++++++++++++++ rust/helpers/vmalloc.c | 7 +++++++ rust/kernel/alloc/allocator.rs | 34 +++++++++++----------------------- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/rust/helpers/slab.c b/rust/helpers/slab.c index 5abd552ccbd4..c4bb451bcf4e 100644 --- a/rust/helpers/slab.c +++ b/rust/helpers/slab.c @@ -25,3 +25,17 @@ rust_helper_kvrealloc_node(const void *p, size_t size, gfp_t flags, int node) { return kvrealloc_node(p, size, flags, node); } + +void * __must_check __realloc_size(2) +rust_helper_krealloc_node_align(const void *objp, size_t new_size, unsigned long align, + gfp_t flags, int node) +{ + return krealloc_node_align(objp, new_size, align, flags, node); +} + +void * __must_check __realloc_size(2) +rust_helper_kvrealloc_node_align(const void *p, size_t size, unsigned long align, + gfp_t flags, int node) +{ + return kvrealloc_node_align(p, size, align, flags, node); +} diff --git a/rust/helpers/vmalloc.c b/rust/helpers/vmalloc.c index e6c796c65ee1..09aee472340f 100644 --- a/rust/helpers/vmalloc.c +++ b/rust/helpers/vmalloc.c @@ -13,3 +13,10 @@ rust_helper_vrealloc_node(const void *p, size_t size, gfp_t flags, int node) { return vrealloc_node(p, size, flags, node); } + +void * __must_check __realloc_size(2) +rust_helper_vrealloc_node_align(const void *p, size_t size, unsigned long align, + gfp_t flags, int 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 47d9668c3599..7f986c974150 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_NODE: Self = Self(bindings::krealloc_node); + // INVARIANT: `krealloc_node_align` satisfies the type invariants. + const KREALLOC_NODE_ALIGN: Self = Self(bindings::krealloc_node_align); - // INVARIANT: `vrealloc_node` satisfies the type invariants. - const VREALLOC_NODE: Self = Self(bindings::vrealloc_node); + // INVARIANT: `vrealloc_node_align` satisfies the type invariants. + const VREALLOC_NODE_ALIGN: Self = Self(bindings::vrealloc_node_align); - // INVARIANT: `kvrealloc_node` satisfies the type invariants. - const KVREALLOC_NODE: Self = Self(bindings::kvrealloc_node); + // INVARIANT: `kvrealloc_node_align` satisfies the type invariants. + const KVREALLOC_NODE_ALIGN: Self = Self(bindings::kvrealloc_node_align); /// # Safety /// @@ -118,7 +118,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, c_nid).cast() + self.0(ptr.cast(), size, layout.align(), flags.0, c_nid).cast() }; let ptr = if size == 0 { @@ -145,7 +145,7 @@ unsafe fn realloc( nid: Option, ) -> Result, AllocError> { // SAFETY: `ReallocFunc::call` has the same safety requirements as `Allocator::realloc`. - unsafe { ReallocFunc::KREALLOC_NODE.call(ptr, layout, old_layout, flags, nid) } + unsafe { ReallocFunc::KREALLOC_NODE_ALIGN.call(ptr, layout, old_layout, flags, nid) } } } @@ -162,15 +162,9 @@ unsafe fn realloc( 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_NODE.call(ptr, layout, old_layout, flags, nid) } + unsafe { ReallocFunc::VREALLOC_NODE_ALIGN.call(ptr, layout, old_layout, flags, nid) } } } @@ -187,14 +181,8 @@ unsafe fn realloc( 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); - } - // SAFETY: If not `None`, `ptr` is guaranteed to point to valid memory, which was previously // allocated with this `Allocator`. - unsafe { ReallocFunc::KVREALLOC_NODE.call(ptr, layout, old_layout, flags, nid) } + unsafe { ReallocFunc::KVREALLOC_NODE_ALIGN.call(ptr, layout, old_layout, flags, nid) } } } -- 2.39.2