From nobody Sun Oct 5 20:14:58 2025 Received: from mailrelay-egress16.pub.mailoutpod3-cph3.one.com (mailrelay-egress16.pub.mailoutpod3-cph3.one.com [46.30.212.3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AF76A2E11C6 for ; Wed, 30 Jul 2025 19:21:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=46.30.212.3 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753903269; cv=none; b=UgdZBySEIkQc6Fj/iwCWunqerBPInm4FJTWWwPzE7d8jAmmo/5WmbmOfHbLjQ8+cF1URuqTuWBtI7wyLD48mOa4Ex4B4GfJ+kdRA9BN9Qtp3NVKFZR03RBealrnX6n8ckEfAeJtBPuUBFHpw+/nwx9+lDJ9qQ6zhVkmDQFuI4IE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753903269; c=relaxed/simple; bh=BaNJPPvriKriFwTdNwSv8plkAbfiQoFdhYDxCuWeGqo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ml1tg1pAJsoy6Vf+O4Xfstw+mzhiHkdqWcPyG6YhFQ5/XEDM6CHKYbHzJiWMyzaEvQaqQe/4VDVDXugx4KeN8p3H79Nxwa6lg5o7B7ONX6gGh4tpWP4pIMT+9Bi9wGXd1D2g8OXbQ4GhPfCnTWf4ucbz1gKqVb1ciNKueESlAvs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=konsulko.se; spf=none smtp.mailfrom=konsulko.se; dkim=pass (2048-bit key) header.d=konsulko.se header.i=@konsulko.se header.b=kqVcEhhF; dkim=permerror (0-bit key) header.d=konsulko.se header.i=@konsulko.se header.b=E+hJluxp; arc=none smtp.client-ip=46.30.212.3 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=konsulko.se Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=konsulko.se Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=konsulko.se header.i=@konsulko.se header.b="kqVcEhhF"; dkim=permerror (0-bit key) header.d=konsulko.se header.i=@konsulko.se header.b="E+hJluxp" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1753903266; x=1754508066; d=konsulko.se; s=rsa1; h=content-transfer-encoding:mime-version:references:in-reply-to:message-id:date: subject:cc:to:from:from; bh=9/m4MO6Q9UDpoalx8uuY2cF/KzW9kmn77IOh1iOY9IU=; b=kqVcEhhFl9ILbR6XiefEURkPV15FlDqWFQHAxZgJgJskJ/UEr053u4VgUE0OBQWsMMKJh2NgrMg0n Qneh9HDPF1Y+O6EZ8wTxCSOaCb5Q8iznVT9AlDV87Kuk7xMK4GRLr6rYDgFSt2giX/Er/l6jB89uZY Ti30jk5Qaq9Y2SraIP20geOxuDvyZrgIPcbilurbpB48EAt5O60rkwjqOG9MrkRyp97bLHyA8J11yX AKeqqMr4UYmbCWDDNAruZa6ioZyhnQQdpeGk+jyIV6isrdM8QpjU+Wo7py8LOgAd8bu8yviqqE7Kcy cF5KSm21QYq2+UYVVlfKc9z08pNdiOA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1753903266; x=1754508066; d=konsulko.se; s=ed1; h=content-transfer-encoding:mime-version:references:in-reply-to:message-id:date: subject:cc:to:from:from; bh=9/m4MO6Q9UDpoalx8uuY2cF/KzW9kmn77IOh1iOY9IU=; b=E+hJluxptc5Hgx5gUKqIPEjPaZzcg1BzkmL2XB9nxmR4zKsa24kT/kiDauNf+Yw8ZsXH1dD1GUojI j03oxD9CQ== X-HalOne-ID: 55256178-6d7a-11f0-9726-fb5fec76084d Received: from slottsdator.home (host-90-238-19-233.mobileonline.telia.com [90.238.19.233]) by mailrelay3.pub.mailoutpod3-cph3.one.com (Halon) with ESMTPSA id 55256178-6d7a-11f0-9726-fb5fec76084d; Wed, 30 Jul 2025 19:21:05 +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 , Vlastimil Babka , rust-for-linux@vger.kernel.org, Lorenzo Stoakes , "Liam R . Howlett" , Kent Overstreet , linux-bcachefs@vger.kernel.org, bpf@vger.kernel.org, Herbert Xu , Jann Horn , Pedro Falcato , Vitaly Wool Subject: [PATCH v14 4/4] rust: support large alignments in allocations Date: Wed, 30 Jul 2025 21:21:01 +0200 Message-Id: <20250730192101.358943-1-vitaly.wool@konsulko.se> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250730191921.352591-1-vitaly.wool@konsulko.se> References: <20250730191921.352591-1-vitaly.wool@konsulko.se> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Acked-by: Danilo Krummrich Acked-by: Alice Ryhl --- rust/helpers/slab.c | 10 ++++++---- rust/helpers/vmalloc.c | 5 +++-- rust/kernel/alloc/allocator.rs | 30 +++++++++-------------------- rust/kernel/alloc/allocator_test.rs | 1 + 4 files changed, 19 insertions(+), 27 deletions(-) diff --git a/rust/helpers/slab.c b/rust/helpers/slab.c index 8472370a4338..7fac958907b0 100644 --- a/rust/helpers/slab.c +++ b/rust/helpers/slab.c @@ -3,13 +3,15 @@ #include =20 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, unsigne= d 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); } =20 void * __must_check __realloc_size(2) -rust_helper_kvrealloc_node(const void *p, size_t size, gfp_t flags, int no= de) +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 =20 void * __must_check __realloc_size(2) -rust_helper_vrealloc_node(const void *p, size_t size, gfp_t flags, int nod= e) +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 8af7e04e3cc6..63f271624428 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -15,7 +15,6 @@ =20 use crate::alloc::{AllocError, Allocator, NumaNode}; use crate::bindings; -use crate::pr_warn; =20 /// The contiguous kernel allocator. /// @@ -56,25 +55,26 @@ fn aligned_size(new_layout: Layout) -> usize { =20 /// # Invariants /// -/// One of the following: `krealloc_node`, `vrealloc_node`, `kvrealloc_nod= e`. +/// One of the following: `krealloc_node_align`, `vrealloc_node_align`, `k= vrealloc_node_align`. struct ReallocFunc( unsafe extern "C" fn( *const crate::ffi::c_void, usize, + crate::ffi::c_ulong, u32, crate::ffi::c_int, ) -> *mut crate::ffi::c_void, ); =20 impl ReallocFunc { - // INVARIANT: `krealloc_node` satisfies the type invariants. - const KREALLOC: Self =3D Self(bindings::krealloc_node); + // INVARIANT: `krealloc_node_align` satisfies the type invariants. + const KREALLOC: Self =3D Self(bindings::krealloc_node_align); =20 - // INVARIANT: `vrealloc_node` satisfies the type invariants. - const VREALLOC: Self =3D Self(bindings::vrealloc_node); + // INVARIANT: `vrealloc_node_align` satisfies the type invariants. + const VREALLOC: Self =3D Self(bindings::vrealloc_node_align); =20 - // INVARIANT: `kvrealloc_node` satisfies the type invariants. - const KVREALLOC: Self =3D Self(bindings::kvrealloc_node); + // INVARIANT: `kvrealloc_node_align` satisfies the type invariants. + const KVREALLOC: Self =3D Self(bindings::kvrealloc_node_align); =20 /// # Safety /// @@ -116,7 +116,7 @@ unsafe fn call( // - Those functions provide the guarantees of this function. let raw_ptr =3D unsafe { // If `size =3D=3D 0` and `ptr !=3D NULL` the memory behind th= e 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() }; =20 let ptr =3D if size =3D=3D 0 { @@ -160,12 +160,6 @@ unsafe fn realloc( 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 me= mory, which was previously // allocated with this `Allocator`. unsafe { ReallocFunc::VREALLOC.call(ptr, layout, old_layout, flags= , nid) } @@ -185,12 +179,6 @@ unsafe fn realloc( 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 PAG= E_SIZE yet.\n"); - return Err(AllocError); - } - // SAFETY: If not `None`, `ptr` is guaranteed to point to valid me= mory, which was previously // allocated with this `Allocator`. unsafe { ReallocFunc::KVREALLOC.call(ptr, layout, old_layout, flag= s, nid) } diff --git a/rust/kernel/alloc/allocator_test.rs b/rust/kernel/alloc/alloca= tor_test.rs index d19c06ef0498..17b27c6e9e37 100644 --- a/rust/kernel/alloc/allocator_test.rs +++ b/rust/kernel/alloc/allocator_test.rs @@ -40,6 +40,7 @@ unsafe fn realloc( layout: Layout, old_layout: Layout, flags: Flags, + nid: NumaNode, ) -> Result, AllocError> { let src =3D match ptr { Some(src) =3D> { --=20 2.39.2