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 C0071C83F1D for ; Tue, 15 Jul 2025 10:00:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 634838D0003; Tue, 15 Jul 2025 06:00:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5BFCE8D0001; Tue, 15 Jul 2025 06:00:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 485B28D0003; Tue, 15 Jul 2025 06:00:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 2F39A8D0001 for ; Tue, 15 Jul 2025 06:00:28 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id D1F255866F for ; Tue, 15 Jul 2025 10:00:27 +0000 (UTC) X-FDA: 83666054094.15.D368B38 Received: from out-180.mta1.migadu.com (out-180.mta1.migadu.com [95.215.58.180]) by imf14.hostedemail.com (Postfix) with ESMTP id 03B0C10000F for ; Tue, 15 Jul 2025 10:00:25 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="sbo/b7yt"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf14.hostedemail.com: domain of hui.zhu@linux.dev designates 95.215.58.180 as permitted sender) smtp.mailfrom=hui.zhu@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1752573626; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=sPnzVrWVUWuZIVWV/G/AQlEdfek6Y/tQOREMS8qveAQ=; b=wshEkz7pqTUc8Dn7G8LnPOwyRO6Q+1N0pt84Cb9RgBeVVxuC4X55u/uM1i4RNABGc6SFCn 6bxP/8xoqZFG/Kg755BNn6Xq3XiG2kmhHg3FNnVhI7zFrJ8N5BertFkJ78j/tgahnorwNz p4UCPqP4KUDXf2jxq3+tE0AT0UdPMW0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1752573626; a=rsa-sha256; cv=none; b=LSARKywj+ElaCsdNfXYjNQnrE8bdHHPYxPBqM3eML0/wxkwGc1kfWXgUamMxzRZA+MQ4Tk JNRPxGUc30kMWMgJyvwXdWBeEGbVs2vBkXtjTwz00Xd3XpNAcvNv6fBwYekmW7w2SwnHPq jh4aZX8JKq7AIZlPOILSiUtRqiMbWJs= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="sbo/b7yt"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf14.hostedemail.com: domain of hui.zhu@linux.dev designates 95.215.58.180 as permitted sender) smtp.mailfrom=hui.zhu@linux.dev X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1752573623; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sPnzVrWVUWuZIVWV/G/AQlEdfek6Y/tQOREMS8qveAQ=; b=sbo/b7yt/cBXHqBAnLzx1hfGwCbgVqPZpBFzq3KrZgKDKB6pjM4YwdAVIA0blBesWLMLRL ZHgdL9OZLdmYJFzHTZrj7g34pn3Y4NeBXkb/4wGBcx7neU0ffqTkNTSNdHcMGTNUtB2uve pGxfRNSOMreSemw3HuEW2+ql5wzXBdA= From: Hui Zhu To: Andrew Morton , Uladzislau Rezki , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , bjorn3_gh@protonmail.com, Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Geliang Tang , Hui Zhu , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org Subject: [PATCH 2/3] rust: allocator: Vmalloc: Support alignments larger than PAGE_SIZE Date: Tue, 15 Jul 2025 17:59:47 +0800 Message-Id: <5e78317d87c42b548929ca386670837ad617fc9d.1752573305.git.zhuhui@kylinos.cn> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Stat-Signature: 4ng4fceacafcyspr3rgs97nmsijnwztj X-Rspamd-Queue-Id: 03B0C10000F X-Rspamd-Server: rspam10 X-Rspam-User: X-HE-Tag: 1752573625-640095 X-HE-Meta: U2FsdGVkX1/URsrdMdVyxGSq2f6Jxxa+CHsoUKVYOwcGAeollQseJDURad3FhROqUjs4Ko9FwEBksJdFYFQktB/nbaxLVUjW9/iC9X+0Indj5rHSkulY6O0isqZITHNHJ+6C7rYPZzmk2qYoZhdJEGdDfwd5NZ0oKJMfDZyrZhzH4UXKWI0rzP0n5choeY6nr8UcLX5XLf0NKdN26f2yq6HI1wzM9vD8uGHUYCURSx2S4R+X2wGdMYBi4LjDgriIGiDTtyADkJ7DWMbjk0fPIOuIXbH9EOtCg9kfH3bkHWPOhRCuFWJ1lRN3gvoQsNMNutNHAjufBP6UNWsRgTaq4D/Hs1f5LeOpZLqppqY/Mvb4q0PilV1FQmSX0Tpu8m9rW6Wg5etAJ/udmT/P9nvhv4WxEdl77CVhJbUEnUDu0qY3z5tulToMmtUOUPU4xpWODAYWGhsP4KUsHfHoW+c+Qw5A+EEkEotv3gSMkzRGq0d8OZ+mboxROoxjh87BMDKgtbwttWzG3VmvWe5DBSxSJGEHYb3COg8HTa6DReGENITPsLW9KLYa6RdOSxlmIuDQW+/zSz0DKXILjni74MrVMA66wfMBqIKLopng9W5Ss0o0mOFJ6hB2G98TxjwufXshka0848WJ2U9EoJG7i/jwlRatrKFtHHPKf397/47FzSQUXZkvH1iXUBoit2p9XR4ksvlYX/Dhvdwy8plxxJl3ZoWueUdALe/Df7Y7IQcR5LrhwqMStMIGKP7D07NH1nMc1XQgcREYkX/zaUfRsOmiMHtmKP+Y3Z6gE4IkrKn8lwHBA2a5RNRYvfbCcLpbmlFO7YYWaZMWi7c37AIipemMhNVPDwRYU6/TQjaNq3Cv6Lv4nY4oA+CSHP75Ga96vZvwDAPR2UdogkTvChf63jmSxafYGUwnyiflpMsQLZV+J9YHizLDwDUUq7Nt+fZnTqhVGOdrkw1M9Q01x3fqUPR jYR90KcO JzQwXd+hTZ/1VXqSbqd01lxbLpakUaw3Vt4kMxdTzXjFLA7+cuQcjvI88ZdgRSX4qGkcI81fbMDpEdtsDb8Wj2JMYch/gOp0fRy8ptBHnr3xy1GCL22aq31Yb6aTtep31QAID88TgQZlxCRmbPRpWc5D1MPUfpUlbdvA9hy/E7b0mgca+5s5UsMhV4kJ2hHLIhfkgxYShlYY/Is0/AFWxHUynWh8WNcWwz6lOFkYIo1urtiFNIm6XjdK60a5BNEMKDHucTi7IDmxaNdqXOXABfhvm736KKF9DQv5GFEc3idYXscetCeQ4ycYmng== 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: From: Hui Zhu This commit add code to make rust alloc Vmalloc support alignments larger than PAGE_SIZE. It adds a new option element to ReallocFunc. When an object supports aligned reallocation, it can register its alignment-specific realloc function here. During VREALLOC initialization, it sets bindings::vrealloc_align to this element. When ReallocFunc::call executes, if the object supports aligned reallocation and the alignment exceeds PAGE_SIZE, the aligned realloc function is used to support alignment capabilities. Co-developed-by: Geliang Tang Signed-off-by: Geliang Tang Signed-off-by: Hui Zhu --- rust/helpers/vmalloc.c | 7 +++++++ rust/kernel/alloc/allocator.rs | 32 ++++++++++++++++++++------------ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/rust/helpers/vmalloc.c b/rust/helpers/vmalloc.c index 80d34501bbc0..3290c4c4c42f 100644 --- a/rust/helpers/vmalloc.c +++ b/rust/helpers/vmalloc.c @@ -7,3 +7,10 @@ rust_helper_vrealloc(const void *p, size_t size, gfp_t flags) { return vrealloc(p, size, flags); } + +void * __must_check __realloc_size(2) +rust_helper_vrealloc_align(const void *p, size_t size, size_t align, + gfp_t flags) +{ + return vrealloc_align(p, size, align, flags); +} diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs index aa2dfa9dca4c..197222e15c26 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -59,17 +59,25 @@ 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) -> *mut crate::ffi::c_void, + Option< + unsafe extern "C" fn( + *const crate::ffi::c_void, + usize, + usize, + u32, + ) -> *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, None); // INVARIANT: `vrealloc` satisfies the type invariants. - const VREALLOC: Self = Self(bindings::vrealloc); + const VREALLOC: Self = Self(bindings::vrealloc, Some(bindings::vrealloc_align)); // INVARIANT: `kvrealloc` satisfies the type invariants. - const KVREALLOC: Self = Self(bindings::kvrealloc); + const KVREALLOC: Self = Self(bindings::kvrealloc, None); /// # Safety /// @@ -108,9 +116,15 @@ unsafe fn call( // GUARANTEE: // - `self.0` is one of `krealloc`, `vrealloc`, `kvrealloc`. // - 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).cast() + // If `size == 0` and `ptr != NULL` the memory behind the pointer is freed. + let raw_ptr = if let Some(f) = self.1 { + if layout.align() > bindings::PAGE_SIZE { + unsafe { f(ptr.cast(), size, layout.align(), flags.0).cast() } + } else { + unsafe { self.0(ptr.cast(), size, flags.0).cast() } + } + } else { + unsafe { self.0(ptr.cast(), size, flags.0).cast() } }; let ptr = if size == 0 { @@ -152,12 +166,6 @@ unsafe fn realloc( old_layout: Layout, flags: Flags, ) -> 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) } -- 2.43.0