From nobody Wed Apr 1 20:55:13 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 EF3BC37F00D for ; Wed, 1 Apr 2026 17:16:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775063810; cv=none; b=lfpV4zAKhoABF+Sp2bzCu5NyF0aiQ+xV+2rlJ42MwdcZpao+ADrFhPKKz99/EMEmwArogH11i68FE89738EzvM2zQsO68TKCzjHjO/5X2MNtycQJkl0KFPnntB0TVKVjUPu/acZJ9vkzbsljb6H6E58ugvie2NHzKGvBWqq/70c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775063810; c=relaxed/simple; bh=g2XGgGei5/9K901lcE8j/TYckn3BeFqUsM0mB2rJlX8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=iW4IK74uQhiPIFrD/1XaDivCneUGE7L2O6OUS9BSIkE9FLhW2YzNtPKe1q/f/l7P98o5qiPFzQuZbPK9MMCohpnjbb3FoRFcA0INQib/vDMRePHx9fHvi+sYSEKDUxQiJ3ihW9rWnc2yO7J0KrwH9/XCiGXZl74gPf4gVX+DpSc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=r3TyOWoa; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="r3TyOWoa" Received: by smtp.kernel.org (Postfix) with ESMTPS id C404AC19423; Wed, 1 Apr 2026 17:16:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775063809; bh=g2XGgGei5/9K901lcE8j/TYckn3BeFqUsM0mB2rJlX8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=r3TyOWoa+zluNcik/LoELjg8BZT6qRfY/Qfkj0+XovMfqSHjlBg3baQmWN9p/wZgC EEPaJFGJiWIM9RXpacgHSWzC8MxBgoght9QG4wyukbjMbXcnRQ7U3I0UhrPQ/R6a5H 3fy/XpMn6fQeO4oHVVrSsy1dM3p+fBHvHw7XGNOe7xvXjHPOAe5ffXfcYFJUECGMOa NicP2aP+0RGF76diAOE9wKMCIeRddU/sHzVmzUT8Zqe0EOUUv1Xpnh/u/BZmuT89MZ Aoj8P81XLDsR9WircFZneW4Dqpn3Jo8gKKT+3BxKgVOsKG0Hzs92Sq6vLWPx/DTfs7 udMA4FWNeY3Jw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id AFEF61076386; Wed, 1 Apr 2026 17:16:49 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Wed, 01 Apr 2026 22:46:33 +0530 Subject: [PATCH v9 1/4] mm/vmalloc: extract vm_area_free_pages() helper from vfree() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260401-vmalloc-shrink-v9-1-bf58dfb997d8@zohomail.in> References: <20260401-vmalloc-shrink-v9-0-bf58dfb997d8@zohomail.in> In-Reply-To: <20260401-vmalloc-shrink-v9-0-bf58dfb997d8@zohomail.in> To: Andrew Morton , Uladzislau Rezki Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Alice Ryhl , Danilo Krummrich , Shivam Kalra X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1775063806; l=2828; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=8O2oBTeVmP5owdpoE0RYwkP6QcFE0K1KL1uyRgBELyY=; b=6jid7SK6D0wuU3u+YB/vxMxjWUUdmWgQlWnWXgP2sZYsn6O9DV3Pny1JVzjbhgPQYpWaz2/h7 SjtzPGetQTGDRuV/FuPOpmtF5AQQvhO3F4fDvJKo4sTpMbKK60oyFMF X-Developer-Key: i=shivamkalra98@zohomail.in; a=ed25519; pk=9Q+S1LD/xjbjL7bEaLIlwRADBwU/6LJq7lYm8LFrkQE= X-Endpoint-Received: by B4 Relay for shivamkalra98@zohomail.in/20260212 with auth_id=633 X-Original-From: Shivam Kalra Reply-To: shivamkalra98@zohomail.in From: Shivam Kalra Extract the page-freeing loop and NR_VMALLOC stat accounting from vfree() into a reusable vm_area_free_pages() helper. The helper operates on a range [start_idx, end_idx) of pages from a vm_struct, making it suitable for both full free (vfree) and partial free (upcoming vrealloc shrink). Freed page pointers in vm->pages[] are set to NULL to prevent stale references when the vm_struct outlives the free (as in vrealloc shrink). Reviewed-by: Alice Ryhl Reviewed-by: Uladzislau Rezki (Sony) Signed-off-by: Shivam Kalra Suggested-by: Danilo Krummrich --- mm/vmalloc.c | 47 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 57eae99d9909..fe8700270139 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -3424,6 +3424,38 @@ void vfree_atomic(const void *addr) schedule_work(&p->wq); } =20 +/* + * vm_area_free_pages - free a range of pages from a vmalloc allocation + * @vm: the vm_struct containing the pages + * @start_idx: first page index to free (inclusive) + * @end_idx: last page index to free (exclusive) + * + * Free pages [start_idx, end_idx) updating NR_VMALLOC stat accounting. + * Freed vm->pages[] entries are set to NULL. + * Caller is responsible for unmapping (vunmap_range) and KASAN + * poisoning before calling this. + */ +static void vm_area_free_pages(struct vm_struct *vm, unsigned int start_id= x, + unsigned int end_idx) +{ + unsigned int i; + + for (i =3D start_idx; i < end_idx; i++) { + struct page *page =3D vm->pages[i]; + + BUG_ON(!page); + /* + * High-order allocs for huge vmallocs are split, so + * can be freed as an array of order-0 allocations + */ + if (!(vm->flags & VM_MAP_PUT_PAGES)) + mod_lruvec_page_state(page, NR_VMALLOC, -1); + __free_page(page); + vm->pages[i] =3D NULL; + cond_resched(); + } +} + /** * vfree - Release memory allocated by vmalloc() * @addr: Memory base address @@ -3444,7 +3476,6 @@ void vfree_atomic(const void *addr) void vfree(const void *addr) { struct vm_struct *vm; - int i; =20 if (unlikely(in_interrupt())) { vfree_atomic(addr); @@ -3467,19 +3498,7 @@ void vfree(const void *addr) =20 if (unlikely(vm->flags & VM_FLUSH_RESET_PERMS)) vm_reset_perms(vm); - for (i =3D 0; i < vm->nr_pages; i++) { - struct page *page =3D vm->pages[i]; - - BUG_ON(!page); - /* - * High-order allocs for huge vmallocs are split, so - * can be freed as an array of order-0 allocations - */ - if (!(vm->flags & VM_MAP_PUT_PAGES)) - mod_lruvec_page_state(page, NR_VMALLOC, -1); - __free_page(page); - cond_resched(); - } + vm_area_free_pages(vm, 0, vm->nr_pages); kvfree(vm->pages); kfree(vm); } --=20 2.43.0 From nobody Wed Apr 1 20:55:13 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 193F347CC80 for ; Wed, 1 Apr 2026 17:16:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775063810; cv=none; b=Mx5tF67fFJLrH8qRhlxIVhzkMPNWPzax01+OWfkUJAObx0LDLU++qWmJEEFfgez2bvNsRKLM37MblZ5hlu6tCjsT7T+TWTPEJBme9WH5GIgujxVyy+BhS1l/Bo6Hn/igxqcPSc1fbJztZ096+eqTy8TX1acug4ehx9BZIIW64OY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775063810; c=relaxed/simple; bh=GrrsWD02UPmYCvEAvfamqo/z+5NYjFR4VRPRWEY5O04=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=lxGPbnLs+LwWPXzfwkBSNUTiRlJ1G1YVe4r6Won468nzqda2zA0AficS04QztpFND5UCtv6PxFGy3sdcpIdA1tk1/Mk77Wu4GSYiWdjA7vyJs8cFeUnXv7uDuDYVwn1C4bS+fK0iN4ipCAXsrgtzggUvVviedrxG9GLaPQWu5ws= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=E1aIlH2k; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="E1aIlH2k" Received: by smtp.kernel.org (Postfix) with ESMTPS id CFE87C2BCB0; Wed, 1 Apr 2026 17:16:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775063809; bh=GrrsWD02UPmYCvEAvfamqo/z+5NYjFR4VRPRWEY5O04=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=E1aIlH2kYe2i9+lJwfjPQSrN5fsrlDGqenwzf9L6nbwZFFvrepe17+wQgkus3TKKt P1imQbvVhuX/95M4hKQoqXmVbv1oqAXk6L89Q/MAkQ/ga79yVJyBKxosnYAPc44lyz dNcBjt9I+ZuuSYJ7cmSGdo25AL1KQTRG/KCyEd9tVopN1svAZb8iwNfxuUK8DbGb+U RXplXw9Yx9xdNnM3WKJKC2y+FH/qqRwLBEvWG7ALYkKdUIdi076sraDxJCH617v/55 Vu8MMx3P2Akqkcy0x7rY8i5v00zjE6LQx5FbVZQmE5ew5vM9XVowPwiFsi0NfGwdDa rnEHChoFnR/sw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id C071A1076389; Wed, 1 Apr 2026 17:16:49 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Wed, 01 Apr 2026 22:46:34 +0530 Subject: [PATCH v9 2/4] mm/vmalloc: use physical page count for vrealloc() grow-in-place check Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260401-vmalloc-shrink-v9-2-bf58dfb997d8@zohomail.in> References: <20260401-vmalloc-shrink-v9-0-bf58dfb997d8@zohomail.in> In-Reply-To: <20260401-vmalloc-shrink-v9-0-bf58dfb997d8@zohomail.in> To: Andrew Morton , Uladzislau Rezki Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Alice Ryhl , Danilo Krummrich , Shivam Kalra X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1775063806; l=1719; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=fkV50pSgKJcZXtbHtu5lgbydbnDdREie8wZ4ZapTlb4=; b=07524EnqAQ3AQ7TUxkiDJWQW8JwTQO/aXl40dY1cG4TPB+DhNnC4jyalYGNX251n728AQwiKI h4eXxb6A1k6DP/F2SFwNfxWSFWtWLf8wwg0f88CirLI4EX8Tx3rR3M6 X-Developer-Key: i=shivamkalra98@zohomail.in; a=ed25519; pk=9Q+S1LD/xjbjL7bEaLIlwRADBwU/6LJq7lYm8LFrkQE= X-Endpoint-Received: by B4 Relay for shivamkalra98@zohomail.in/20260212 with auth_id=633 X-Original-From: Shivam Kalra Reply-To: shivamkalra98@zohomail.in From: Shivam Kalra Update the grow-in-place check in vrealloc() to compare the requested size against the actual physical page count (vm->nr_pages) rather than the virtual area size (alloced_size, derived from get_vm_area_size()). Currently both values are equivalent, but the upcoming vrealloc() shrink functionality will free pages without reducing the virtual reservation size. After such a shrink, the old alloced_size-based comparison would incorrectly allow a grow-in-place operation to succeed and attempt to access freed pages. Switch to vm->nr_pages now so the check remains correct once shrink support is added. Signed-off-by: Shivam Kalra Suggested-by: Danilo Krummrich --- mm/vmalloc.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index fe8700270139..1c6d747220ce 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -4351,6 +4351,12 @@ void *vrealloc_node_align_noprof(const void *p, size= _t size, unsigned long align if (unlikely(flags & __GFP_THISNODE) && nid !=3D NUMA_NO_NODE && nid !=3D page_to_nid(vmalloc_to_page(p))) goto need_realloc; + } else { + /* + * If p is NULL, vrealloc behaves exactly like vmalloc. + * Skip the shrink and in-place grow paths. + */ + goto need_realloc; } =20 /* @@ -4369,7 +4375,7 @@ void *vrealloc_node_align_noprof(const void *p, size_= t size, unsigned long align /* * We already have the bytes available in the allocation; use them. */ - if (size <=3D alloced_size) { + if (size <=3D (size_t)vm->nr_pages << PAGE_SHIFT) { /* * No need to zero memory here, as unused memory will have * already been zeroed at initial allocation time or during --=20 2.43.0 From nobody Wed Apr 1 20:55:13 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 182CF1D432D for ; Wed, 1 Apr 2026 17:16:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775063810; cv=none; b=hvcibD4JhDFMkrjONui4NVjBzKTppzyC3LRgVTKKvRI4zUYAFJUGWH2GacTl6bPrnLaNor354Al0nsUQ73vMOcvEDMZSRDIp9wTH74whvBE95vfgmcR4/N1HvNSjc2V6DwQhX8P0LrAr7/5b/PxviJGJi2zQ++Pr2afqq+1rn3A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775063810; c=relaxed/simple; bh=YIqk+GI3VlI39w15Sj/+hEGyGQSwUg0HivbIRY5c4l8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=rCqLH/EPegSxLxz6BOM48aazDgeg5SxmiscKeMmaSU5KTY9kuMZDpprxAx28jQv5WnseMGllFyMrPcsSDz9sydjONRnD2YEpC7qfiqPsoQ43gZK32ijpUERsIBJhfVW5TLWOhr9Mkx+eoENDn/KF5RdCl1q4rKtXN4Tw7JV+ozc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=JQfDdp7E; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="JQfDdp7E" Received: by smtp.kernel.org (Postfix) with ESMTPS id DC5CEC2BCB2; Wed, 1 Apr 2026 17:16:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775063809; bh=YIqk+GI3VlI39w15Sj/+hEGyGQSwUg0HivbIRY5c4l8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=JQfDdp7ElxJsM6m31GkWVQXQnvD4o8S/nHjUCZGiNVe6OA+Dv8zgystD8j+OSG5T/ tHcC1WWFaWL6bacbxSNXYkG49tbafopteHPROR4ZgbNuA5231cpa52Fz/8N/EmuokN wkO1z92rj1/WKaNh715KqyBHqcJpTbu4V0/QootmAik4rDBP4lhnhGDuzg7yeVOdgF OHMDe3XXpL4CZ3fFBmLBmCq46j+ObHk1I/0xsGrclZvL5dSSbdN42SQxUoqh3bap+k +3c9uaHKsc9LkHu/M00CQJT29OqEr4FpXjBWpX7RDfPKRauTkmtt5m76QZAYRBgv3G SVQTwHVu2u38A== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id D05F810F6FDC; Wed, 1 Apr 2026 17:16:49 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Wed, 01 Apr 2026 22:46:35 +0530 Subject: [PATCH v9 3/4] mm/vmalloc: free unused pages on vrealloc() shrink Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260401-vmalloc-shrink-v9-3-bf58dfb997d8@zohomail.in> References: <20260401-vmalloc-shrink-v9-0-bf58dfb997d8@zohomail.in> In-Reply-To: <20260401-vmalloc-shrink-v9-0-bf58dfb997d8@zohomail.in> To: Andrew Morton , Uladzislau Rezki Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Alice Ryhl , Danilo Krummrich , Shivam Kalra X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1775063806; l=4129; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=3nMDP0UoKiSAVmLTEwwBC/svH7X4LQVEHStunnKNDUk=; b=jLa7+jV9wUcG1BnhTYe4qHrTaNnG3xfduH29EuCmCUKWjLhrU55aSyND7UWjYsRf/R4JNRiUh cuvBprtt72sDVTCcDtCv4hJWvg+8WPJuN2Uoyq3mpXsbOsXSMyUaJE8 X-Developer-Key: i=shivamkalra98@zohomail.in; a=ed25519; pk=9Q+S1LD/xjbjL7bEaLIlwRADBwU/6LJq7lYm8LFrkQE= X-Endpoint-Received: by B4 Relay for shivamkalra98@zohomail.in/20260212 with auth_id=633 X-Original-From: Shivam Kalra Reply-To: shivamkalra98@zohomail.in From: Shivam Kalra When vrealloc() shrinks an allocation and the new size crosses a page boundary, unmap and free the tail pages that are no longer needed. This reclaims physical memory that was previously wasted for the lifetime of the allocation. The heuristic is simple: always free when at least one full page becomes unused. Huge page allocations (page_order > 0) are skipped, as partial freeing would require splitting. Allocations with VM_FLUSH_RESET_PERMS are also skipped, as their direct-map permissions must be reset before pages are returned to the page allocator, which is handled by vm_reset_perms() during vfree(). Additionally, allocations with VM_USERMAP are skipped because remap_vmalloc_range_partial() validates mapping requests against the unchanged vm->size; freeing tail pages would cause vmalloc_to_page() to return NULL for the unmapped range. To protect concurrent readers, the shrink path uses Node lock to synchronize before freeing the pages. Finally, we notify kmemleak of the reduced allocation size using kmemleak_free_part() to prevent the kmemleak scanner from faulting on the newly unmapped virtual addresses. The virtual address reservation (vm->size / vmap_area) is intentionally kept unchanged, preserving the address for potential future grow-in-place support. Suggested-by: Danilo Krummrich Signed-off-by: Shivam Kalra --- mm/vmalloc.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 1c6d747220ce..a7731e54560b 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -4359,14 +4359,62 @@ void *vrealloc_node_align_noprof(const void *p, siz= e_t size, unsigned long align goto need_realloc; } =20 - /* - * TODO: Shrink the vm_area, i.e. unmap and free unused pages. What - * would be a good heuristic for when to shrink the vm_area? - */ if (size <=3D old_size) { + unsigned int new_nr_pages =3D PAGE_ALIGN(size) >> PAGE_SHIFT; + /* Zero out "freed" memory, potentially for future realloc. */ if (want_init_on_free() || want_init_on_alloc(flags)) memset((void *)p + size, 0, old_size - size); + + /* + * Free tail pages when shrink crosses a page boundary. + * + * Skip huge page allocations (page_order > 0) as partial + * freeing would require splitting. + * + * Skip VM_FLUSH_RESET_PERMS, as direct-map permissions must + * be reset before pages are returned to the allocator. + * + * Skip VM_USERMAP, as remap_vmalloc_range_partial() validates + * mapping requests against the unchanged vm->size; freeing + * tail pages would cause vmalloc_to_page() to return NULL for + * the unmapped range. + * + * Skip if either GFP_NOFS or GFP_NOIO are used. + * kmemleak_free_part() internally allocates with + * GFP_KERNEL, which could trigger a recursive deadlock + * if we are under filesystem or I/O reclaim. + */ + if (new_nr_pages < vm->nr_pages && !vm_area_page_order(vm) && + !(vm->flags & (VM_FLUSH_RESET_PERMS | VM_USERMAP)) && + gfp_has_io_fs(flags)) { + unsigned long addr =3D (unsigned long)kasan_reset_tag(p); + unsigned int old_nr_pages =3D vm->nr_pages; + + /* Notify kmemleak of the reduced allocation size before unmapping. */ + kmemleak_free_part( + (void *)addr + ((unsigned long)new_nr_pages + << PAGE_SHIFT), + (unsigned long)(old_nr_pages - new_nr_pages) + << PAGE_SHIFT); + + vunmap_range(addr + ((unsigned long)new_nr_pages + << PAGE_SHIFT), + addr + ((unsigned long)old_nr_pages + << PAGE_SHIFT)); + + /* + * Use the node lock to synchronize with concurrent + * readers (vmalloc_info_show). + */ + struct vmap_node *vn =3D addr_to_node(addr); + + spin_lock(&vn->busy.lock); + vm->nr_pages =3D new_nr_pages; + spin_unlock(&vn->busy.lock); + + vm_area_free_pages(vm, new_nr_pages, old_nr_pages); + } vm->requested_size =3D size; kasan_vrealloc(p, old_size, size); return (void *)p; --=20 2.43.0 From nobody Wed Apr 1 20:55:13 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 2977D47CC87 for ; Wed, 1 Apr 2026 17:16:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775063810; cv=none; b=nxLZ4hlrlhbnYhq4U+NJ5ZlZTV6/V6BwNNCWO9Nf8Ci1z8pu07u9/5qvRoGuPFY3bSGFUX6piXwc+4PwJBr10eHDrXbRjeOgL6s3eopnY9uu1EpGOZ3nxWLR5QzehgBa9k81T9jLIZMTlo+rLE/tw5/lNhdL+i4c9nre5wCC6jE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775063810; c=relaxed/simple; bh=NgcWajqEHrBjrpmlHzE3kqeLHOp/AqP67j8VknlwGaM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ksOvjMF5prq/8cYlz2rzGLCmirSzln0aHbn5fyCeqeKgI6C6t1EYSQURRq15Ej08FPS49kv4sxtf8c8luEeZPbUm/gRsCkvZGNW3k1r/3ufGxWOuFsHzUKXkEJ0PGfuNpqsL/dA9rV7MwH6p99QZrnOC2+3ipGhbjH+XZuxUnco= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Iya8vXGd; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Iya8vXGd" Received: by smtp.kernel.org (Postfix) with ESMTPS id ED531C2BCAF; Wed, 1 Apr 2026 17:16:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775063810; bh=NgcWajqEHrBjrpmlHzE3kqeLHOp/AqP67j8VknlwGaM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=Iya8vXGd+fmwXV88td2Z0XWbPF2NdANMRqUGbEVJIjsCR4oH0KSGqKZsrcigIIxNc ZgnJTwsuQM/lF4seFd8RvToVpXMAcMRrGC63kUuCd2RNNSezWEzU/V2rOcIdgYHFZj F/mfQHJ0ZhwXmYKiNI8tE+ZCfYfPm+3NBipYEu7IP5ezYVhfie8UBTHjM1ZC16sf6V tQ5vO1BozWfkeJRB2OM4fkDOiTtnOPCZ4/4xZ2Igr9EuWikAtqbnR342i/g5poJ9o7 wGWu6ztUuoA4zWgE2oXm2UgBopfxM9AkRjaPFQkmA5RAI5f0bivl8ZSR9oMFv/2hV8 9JOi8m2jU2KRw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id DF81C1076381; Wed, 1 Apr 2026 17:16:49 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Wed, 01 Apr 2026 22:46:36 +0530 Subject: [PATCH v9 4/4] lib/test_vmalloc: add vrealloc test case Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260401-vmalloc-shrink-v9-4-bf58dfb997d8@zohomail.in> References: <20260401-vmalloc-shrink-v9-0-bf58dfb997d8@zohomail.in> In-Reply-To: <20260401-vmalloc-shrink-v9-0-bf58dfb997d8@zohomail.in> To: Andrew Morton , Uladzislau Rezki Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Alice Ryhl , Danilo Krummrich , Shivam Kalra X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1775063806; l=2905; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=yP1QXayDidKnNU2S8I7BqJprRzQI2mQyl0fytHsaqPM=; b=K+rvqijkH6LgioddShavqfdDzGqGhFPpOudpCsY2f5P28WR6Xscdt+/cPBZMV4+aWr6Zijp2d Y3sYegmUeZYBuDOhdMAZGuZmXpwWjr78v8g053MOKdoIlCWSUOYYW2B X-Developer-Key: i=shivamkalra98@zohomail.in; a=ed25519; pk=9Q+S1LD/xjbjL7bEaLIlwRADBwU/6LJq7lYm8LFrkQE= X-Endpoint-Received: by B4 Relay for shivamkalra98@zohomail.in/20260212 with auth_id=633 X-Original-From: Shivam Kalra Reply-To: shivamkalra98@zohomail.in From: Shivam Kalra Introduce a new test case "vrealloc_test" that exercises the vrealloc() shrink and in-place grow paths: - Grow beyond allocated pages (triggers full reallocation). - Shrink crossing a page boundary (frees tail pages). - Shrink within the same page (no page freeing). - Grow within the already allocated page count (in-place). Data integrity is validated after each realloc step by checking that the first byte of the original allocation is preserved. The test is gated behind run_test_mask bit 12 (id 4096). Reviewed-by: Uladzislau Rezki (Sony) Signed-off-by: Shivam Kalra Suggested-by: Danilo Krummrich --- lib/test_vmalloc.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 62 insertions(+) diff --git a/lib/test_vmalloc.c b/lib/test_vmalloc.c index 876c72c18a0c..b23f85e8f8ca 100644 --- a/lib/test_vmalloc.c +++ b/lib/test_vmalloc.c @@ -55,6 +55,7 @@ __param(int, run_test_mask, 7, "\t\tid: 512, name: kvfree_rcu_2_arg_vmalloc_test\n" "\t\tid: 1024, name: vm_map_ram_test\n" "\t\tid: 2048, name: no_block_alloc_test\n" + "\t\tid: 4096, name: vrealloc_test\n" /* Add a new test case description here. */ ); =20 @@ -421,6 +422,66 @@ vm_map_ram_test(void) return nr_allocated !=3D map_nr_pages; } =20 +static int vrealloc_test(void) +{ + void *ptr, *tmp; + int i; + + for (i =3D 0; i < test_loop_count; i++) { + int err =3D -1; + + ptr =3D vrealloc(NULL, PAGE_SIZE, GFP_KERNEL); + if (!ptr) + return -1; + + *((__u8 *)ptr) =3D 'a'; + + /* Grow: beyond allocated pages, triggers full realloc. */ + tmp =3D vrealloc(ptr, 4 * PAGE_SIZE, GFP_KERNEL); + if (!tmp) + goto error; + ptr =3D tmp; + + if (*((__u8 *)ptr) !=3D 'a') + goto error; + + /* Shrink: crosses page boundary, frees tail pages. */ + tmp =3D vrealloc(ptr, PAGE_SIZE, GFP_KERNEL); + if (!tmp) + goto error; + ptr =3D tmp; + + if (*((__u8 *)ptr) !=3D 'a') + goto error; + + /* Shrink: within same page, no page freeing. */ + tmp =3D vrealloc(ptr, PAGE_SIZE / 2, GFP_KERNEL); + if (!tmp) + goto error; + ptr =3D tmp; + + if (*((__u8 *)ptr) !=3D 'a') + goto error; + + /* Grow: within allocated page, in-place, no realloc. */ + tmp =3D vrealloc(ptr, PAGE_SIZE, GFP_KERNEL); + if (!tmp) + goto error; + ptr =3D tmp; + + if (*((__u8 *)ptr) !=3D 'a') + goto error; + + err =3D 0; +error: + vfree(ptr); + if (err) + return err; + } + + return 0; +} + struct test_case_desc { const char *test_name; int (*test_func)(void); @@ -440,6 +501,7 @@ static struct test_case_desc test_case_array[] =3D { { "kvfree_rcu_2_arg_vmalloc_test", kvfree_rcu_2_arg_vmalloc_test, }, { "vm_map_ram_test", vm_map_ram_test, }, { "no_block_alloc_test", no_block_alloc_test, true }, + { "vrealloc_test", vrealloc_test, }, /* Add a new test case here. */ }; =20 --=20 2.43.0