From nobody Wed Jun 17 05:15:23 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 327F4366072 for ; Mon, 27 Apr 2026 20:24:26 +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=1777321466; cv=none; b=SecZxtuyfkrQkyLXaPYydhDuBHZb1s3j3GzYXmovdYdnj23Thhv2lflClNYw62Fu8blSSoxY9P0uyv/th8N4DXwurP/PglOulkmSLJHHdmVd0m0ec+a24x5sAQa2m+VQVHohjaeoIWwxhyjlNUgfgIEV3ip5nprDr76+n86fZzE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777321466; c=relaxed/simple; bh=8VhyCSBWNt3i7bjVVa9tmZQb09bkp/mb65vyqxOzAO4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Pzoh7I+krKYwTOt251bO2l9Ed9z+BYjAV/JLjjnk8Sj73jgBOLxRRmEHoXlVveEVkyNORJip/eRc0/Z8WwMFefqfxB4vrhdXr5mb/ERDV7MAIcpNQ7NjoEiJa0AjpmQxZF/eFRPUM20VElVLSUlwwpWG23GaRcnu93xR8dMvWaQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=m8Jc8bTb; 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="m8Jc8bTb" Received: by smtp.kernel.org (Postfix) with ESMTPS id E2140C2BCB5; Mon, 27 Apr 2026 20:24:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1777321465; bh=8VhyCSBWNt3i7bjVVa9tmZQb09bkp/mb65vyqxOzAO4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=m8Jc8bTbKj43oqaFWYInpU2kAnpvpNC1S66mrquw1uRWdS73FTTzbjmGtCJynLqlq FgtxLlQ3njNvac7Hq3PIA+I/U2MXq4Uu1XS1U7LuQYQ72OC2+5eE24Or+ndulHcHLW wq6xfMNxRsR7LOpoEOsiNrWG3Gy2lduUPArBs7991G+31z5Xy2XILxc9UX/Xzu+BDX Bmkq5tT8W3QGF1gZxQzcUGQ4mWI5rg8whLt6826FFs48a96cqTGXh/GCEN/qM4KGTG qob1Zhquc7ITnDNplwy/2Okta/LfW1R9sxnpbLYjNShytxtmYChi6H1ZuBQCu07XQx AClqUX/L3lTqw== 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 D00BAFF8867; Mon, 27 Apr 2026 20:24:25 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Tue, 28 Apr 2026 01:54:16 +0530 Subject: [PATCH v12 1/5] 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: <20260428-vmalloc-shrink-v12-1-3c18c9172eb1@zohomail.in> References: <20260428-vmalloc-shrink-v12-0-3c18c9172eb1@zohomail.in> In-Reply-To: <20260428-vmalloc-shrink-v12-0-3c18c9172eb1@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=1777321463; l=2413; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=p0TbLs1fXk9LoPaeNtoH0gYLWzCHadOSjVu2hcSyOWs=; b=2DQ3j8HZ9+xH7S202jUyFt7bDNj1UAxPd873RQEa5Z03BRcnL/ZSyntKmpnYUrytZGaVc3Apo eULk1FOnUdzAWS3jc5R2vXRsjce8Qyjn9sIOXaXuHMqO6hMYBF231NZ 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 page freeing 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). Signed-off-by: Shivam Kalra Reviewed-by: Uladzislau Rezki (Sony) Suggested-by: Danilo Krummrich --- mm/vmalloc.c | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 878c5b7bf837..1073abb6094e 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -3416,6 +3416,32 @@ 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; + + if (!(vm->flags & VM_MAP_PUT_PAGES)) { + for (i =3D start_idx; i < end_idx; i++) + mod_lruvec_page_state(vm->pages[i], NR_VMALLOC, -1); + } + free_pages_bulk(vm->pages + start_idx, end_idx - start_idx); + + for (i =3D start_idx; i < end_idx; i++) + vm->pages[i] =3D NULL; +} + /** * vfree - Release memory allocated by vmalloc() * @addr: Memory base address @@ -3436,7 +3462,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); @@ -3460,12 +3485,7 @@ void vfree(const void *addr) if (unlikely(vm->flags & VM_FLUSH_RESET_PERMS)) vm_reset_perms(vm); =20 - if (!(vm->flags & VM_MAP_PUT_PAGES)) { - for (i =3D 0; i < vm->nr_pages; i++) - mod_lruvec_page_state(vm->pages[i], NR_VMALLOC, -1); - } - free_pages_bulk(vm->pages, vm->nr_pages); - + vm_area_free_pages(vm, 0, vm->nr_pages); kvfree(vm->pages); kfree(vm); } --=20 2.43.0 From nobody Wed Jun 17 05:15:23 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 3288536AB5E for ; Mon, 27 Apr 2026 20:24:26 +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=1777321466; cv=none; b=CJwCHyC6eDO8lT/K1xjX9k7qpHvjo5x7lUMOyNHX3s+jIjv0Zc0GbQRNu6Ny2UnQHajocq8P5UTR3gpcUnNcruwYHLwIaij7JASWCcUzIRn+LVDHkHuZo6iO7DF0eutXOxZpbgvjW2V5yoTAV/kDWMvcksum+IHUMtuXcDnBsSg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777321466; c=relaxed/simple; bh=MxKAswVy5gspAeS/niaj4sA5dDUAOJ+IREaEamGnxyM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=DYLaHs/L8M7tLepbRs8hN7/o9P3tGu4Dx7CzKeqytluscoo/1CLE1afsUaf08kc9FtB2vBvgk6Ob1gYRqo3X5lthzxFWUPFTNKM2/knU8T59KRn/90kj9cDzF2XGigjjbv4JfHrwaw4KRDp91s8h6oNyv0/L+jYpiz4lDbW8oHQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZoTFI2cn; 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="ZoTFI2cn" Received: by smtp.kernel.org (Postfix) with ESMTPS id ECE58C2BCB8; Mon, 27 Apr 2026 20:24:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1777321466; bh=MxKAswVy5gspAeS/niaj4sA5dDUAOJ+IREaEamGnxyM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=ZoTFI2cnoxpqcTJfUrUa6i07Ikr0rK913P/ArjT77PDU33uMqzaG46/Eu96ZD5OnI jfBMitByDLL752leJOmxSohF+mew2H8KEeIUWRIvzm9gB2zuDwiN+vE2Zc3DCMM4hG 5fIKv2SpZrtn4+7BC5wIr28vqXaXge4U6ykql1sP1/BA2w66CS/YdBWs90IVI+fUqA AeYdbJGpd8yNO39qWQo80X0zfraEBCbhxT8DpVS8MVPdQkDFS2JGBCXTSYpppbcYfw iAr+9jgM2bAecfHTBxyF0dXRDql6wK+k/+UtG1iHU5Q7wy+UNgfUxm/pYf3Q6WptL/ Z4CuWSKfOnXeQ== 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 DD89DFF886D; Mon, 27 Apr 2026 20:24:25 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Tue, 28 Apr 2026 01:54:17 +0530 Subject: [PATCH v12 2/5] 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: <20260428-vmalloc-shrink-v12-2-3c18c9172eb1@zohomail.in> References: <20260428-vmalloc-shrink-v12-0-3c18c9172eb1@zohomail.in> In-Reply-To: <20260428-vmalloc-shrink-v12-0-3c18c9172eb1@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=1777321463; l=1711; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=3zaMDF3eqVJSjOkW52oDkHrDPus2VHUR0DHtrB4u00o=; b=9xDmV4QtPHHxQIrLU6FWBWFk5O11upRJ6kVq6BdfEg+o3OqmT9PIwsBDRRVjoqRHmvya8jdBG SRE0nLYjyARBGXOwMeVysPpN0kqJGi+kck6apHCR7qUZjDCWkzTwGDv 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 Reviewed-by: Uladzislau Rezki (Sony) 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 1073abb6094e..9cb3e287a1e8 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -4338,6 +4338,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 /* @@ -4356,7 +4362,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 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 Jun 17 05:15:23 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 3292A36AB76 for ; Mon, 27 Apr 2026 20:24:26 +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=1777321466; cv=none; b=oYQ4fL0fQM9FRb02agkOvXo1OjPXdapgALIAxd/v2lpz91B8Ak+SBBrk67YMrzKPqUSehfyzjSnx1eTJ+IVYgR5zF94FUs42r5CvBg6JD4Wm6xZO8a6ItuGoMMFbcJ/2B1E8dRFWt3A/YenSemC8Djfer9vTEVfKtfgtHPrmUFU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777321466; c=relaxed/simple; bh=SDc+T3e7+LwO88oci/ESlFXaNZSdeI8dvMAEpN6Sk9s=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=UlEc43vEHuKzOUac2XLiLCR5tjgLPOEoiEHvzVEdH/LdDpYrIfIxohuhX+SqS9lGEYxvmknBVlEQpqb9H0fOnPcf6N6tZfox9eDFXP2ZMyr9espnIarQEowNHShBS3842ccXEwDGDuJ3szKc6zbHOaTayD4s5gCYdzrWJEmolwk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=U0wwv4qM; 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="U0wwv4qM" Received: by smtp.kernel.org (Postfix) with ESMTPS id 041EAC2BCB9; Mon, 27 Apr 2026 20:24:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1777321466; bh=SDc+T3e7+LwO88oci/ESlFXaNZSdeI8dvMAEpN6Sk9s=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=U0wwv4qMKz1JQWG0UWgHt72ZRwiuJHbxTG2KBNO/DSUyKYu+HvTuC6nkvu+WRB3FE 2W5v7fUwGoqu4TBoYNvxalB3puAt6AF+sPst/Od/e6PIAOr6L4cTNkFSPA/Fqi/HfC WhJAp4OaMb7KsuUg0bJkudBG1uZjyXnh4bLMrV6FsgyN/X1DwIfJZtE7TgV+tpDBZ4 2ELnjaReXF6AgotpgydG1nZ/V9t92cyRhmm/JhLM76EX/Qn1i3lI6WWIHe67mvn3XB s+9lG4232d/Nx7x2D3dJpj3h8tosqpYBneqPhgpSGAdReWDkfARrn2M9jf7QQhoH4/ 6mPxHlXOfzEDw== 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 ECA21FF8872; Mon, 27 Apr 2026 20:24:25 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Tue, 28 Apr 2026 01:54:18 +0530 Subject: [PATCH v12 3/5] mm/vmalloc: use physical page count in vread_iter() 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: <20260428-vmalloc-shrink-v12-3-3c18c9172eb1@zohomail.in> References: <20260428-vmalloc-shrink-v12-0-3c18c9172eb1@zohomail.in> In-Reply-To: <20260428-vmalloc-shrink-v12-0-3c18c9172eb1@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=1777321463; l=1268; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=IzrpuvbBn3WlkxnhP3YgMF2kBytQxCzQDInbnNPSh4s=; b=7okzB/Cxu/mR4PDwNKA/wjJi+ud8fSilxvYtTPmIBHwgQcROTwxb+qvGA2qDyliVp7ZkuFDAG RegZNLcWwF8AwQM9LeqZlGIJDhqt2qCGxdYJTi9QcfN3eckCxdVshlG 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 vread_iter() to derive the vm area size from vm->nr_pages rather than 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 get_vm_area_size() based calculation would overestimate the mapped range, causing vread_iter() to attempt reading from unmapped addresses. Switch to vm->nr_pages now so the reader remains correct once shrink support is added. Signed-off-by: Shivam Kalra Reviewed-by: Uladzislau Rezki (Sony) Suggested-by: Danilo Krummrich --- mm/vmalloc.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 9cb3e287a1e8..65e0a23efb3b 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -4661,7 +4661,14 @@ long vread_iter(struct iov_iter *iter, const char *a= ddr, size_t count) smp_rmb(); =20 vaddr =3D (char *) va->va_start; - size =3D vm ? get_vm_area_size(vm) : va_size(va); + if (vm) + /* + * Cannot use get_vm_area_size() because realloc() + * may shrink the mapping and area->size may be outdated. + */ + size =3D vm->nr_pages << PAGE_SHIFT; + else + size =3D va_size(va); =20 if (addr >=3D vaddr + size) goto next_va; --=20 2.43.0 From nobody Wed Jun 17 05:15:23 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 36E9536D500 for ; Mon, 27 Apr 2026 20:24:26 +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=1777321466; cv=none; b=FxgoEfCHWAFLHwncbzsESsaQn0RNtbj6SXRkltPsef3fcX5CVyNzISdEq3uA7+GhFgeCq5ATRxjqWfEQFgG/IHduBM0TP19y58MeyUAvRbgthYArmy4DIkxAIAwhfIWwvJmY4QZX8+51WlCwBzOfm3ZToFlTr2VabE5iowc85XQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777321466; c=relaxed/simple; bh=/F8v5Oh0txeIev8z2jZ0Qvbb6TLvh7Meq8C3Y2ZodlI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=NSkM5XzyYDm700n02RZKaMe8TIBebsZmklM00MalfYbJkYGQiSFWSSJoU1v5sAF2hcPbyLxW4cY14rRCehZnLXXtE9kYl1F0511N22GlBDwazf6gRAWYjrkXmxbTllSj1ul1uExivZn506mMk0clVRehj40D+6YsFwIauM3jfug= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=XZ1Cu5ct; 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="XZ1Cu5ct" Received: by smtp.kernel.org (Postfix) with ESMTPS id 0ED5EC2BCC4; Mon, 27 Apr 2026 20:24:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1777321466; bh=/F8v5Oh0txeIev8z2jZ0Qvbb6TLvh7Meq8C3Y2ZodlI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=XZ1Cu5ct8bjSpN57GpUzhxspGWdwT/LBzZfThyR+Uf4ZGvk1zMrvMraXZ5jcAhLGr +vOw5rhNKV2vLEHVmOYa4ZZrslTRt3AuS7yk/vsZ0JaFBVLLOBACdGU6dCaP/STPDK hoQMm1MoRIuihogKJb+wtgKTrbWfHmiJH1PcVlLNOvrbeVkjf1uCJnmyfYFLFf6fkJ 1pEU+bYB2maPiAORLtJuVeZkCcfUbfMeAOsYVxBZPvv/+lio9OXv00Bj6vCYBDAAbt fGNhXVtuv6hIoctl/5KhCJ60zYd9v4ZxMX4uvbkldnFr+c0ys4eKd4KT8p6wkSloLV lZkxyILwtpelg== 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 06480FF8874; Mon, 27 Apr 2026 20:24:26 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Tue, 28 Apr 2026 01:54:19 +0530 Subject: [PATCH v12 4/5] 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: <20260428-vmalloc-shrink-v12-4-3c18c9172eb1@zohomail.in> References: <20260428-vmalloc-shrink-v12-0-3c18c9172eb1@zohomail.in> In-Reply-To: <20260428-vmalloc-shrink-v12-0-3c18c9172eb1@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=1777321463; l=4129; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=5NJsZJhr4gxLdNkrGrcBoAv03CvvDNrlYCw3Jjj+FFo=; b=MyOLz6qbpCthFvegsA2v+ZrsVHbTI7LcbjCvhYHTrQiWSnDquffPxjeOpeRjoWlNN33niXLX3 DPYYLQbB9RhChU6zUQXVVXuNJixa87jFnfg44ArFOtDTxE5naseWcGJ 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 Reviewed-by: Uladzislau Rezki (Sony) --- mm/vmalloc.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 65e0a23efb3b..9f810d306db9 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -4346,14 +4346,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; + + /* + * 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); + + /* 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)); + + 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 Jun 17 05:15:23 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 61D2836DA1C for ; Mon, 27 Apr 2026 20:24:26 +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=1777321466; cv=none; b=I0+2lH2NPya6rq+PPmVzd6PH45OJ5E26t23lEl3aJjGKQHjS5EYgHHJLh/4LNGxLcOA6h8kx0GqJSfCjZg2hZ8v36bOEFgWYDD8KCSTKvqvtONz8nEs5uKoQAXwIyfsdyxxQICHkRQ5W9v+gxiiIpCxC7tNyzaxWW8g+FX1+RqE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777321466; c=relaxed/simple; bh=NgcWajqEHrBjrpmlHzE3kqeLHOp/AqP67j8VknlwGaM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=pn9HLrX8aFzyz02blwXnuNFPyQ7FaU3vH5LxBQWOyIR64FrjhtXuW2VVILToRwejsIwAY6g2AmUA7SDV9pti3dcaoeQ+8rpVNT0FF4CqyL/X7zd/FpmxvnK6FQ89ELgh/Nt55Dtl6J0CXDxADK6ckk/TvlIfrcwPwPm526hqC30= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rOZqEKNg; 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="rOZqEKNg" Received: by smtp.kernel.org (Postfix) with ESMTPS id 1D1EAC2BCC6; Mon, 27 Apr 2026 20:24:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1777321466; bh=NgcWajqEHrBjrpmlHzE3kqeLHOp/AqP67j8VknlwGaM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=rOZqEKNg1fCw1sLp6FyO+gvB8d/3BYUxdw9Ks654MOfq8fwmLpaJu3bWOlqOAdjRR oyJJ3s4IMH43/Sa8wI5IlB7AQLKJ4G3h3i10WDGM/OeldtSSD8yT5xkuoB2AnhJ5hm Ksj98Rdnliblg7+HIzqrnvNY8of48UZZyKk871U1xEoXEjQmcmhd4lQbVqn5DL8Uv7 91rKAQla7dIR0ai9S1C6VOy3ip8l07CGx8QjsoSfDoJLJ3aurG/m639ad4nCNQTrl2 2i1JvuXpBSK7dOtGGYTD7al1E9t/zaeJBw2NKOTZcqbqO4T1SkvZO4TlgCXGWZ+41L obT/ESnt6vVRA== 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 148B8FF8870; Mon, 27 Apr 2026 20:24:26 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Tue, 28 Apr 2026 01:54:20 +0530 Subject: [PATCH v12 5/5] 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: <20260428-vmalloc-shrink-v12-5-3c18c9172eb1@zohomail.in> References: <20260428-vmalloc-shrink-v12-0-3c18c9172eb1@zohomail.in> In-Reply-To: <20260428-vmalloc-shrink-v12-0-3c18c9172eb1@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=1777321463; l=2905; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=yP1QXayDidKnNU2S8I7BqJprRzQI2mQyl0fytHsaqPM=; b=i8Z3ANl5DezVl1EY2eeSaDzeTmV1WTIADSFw6u2DzPbrVpqLqS6cK9lt8QLacgjK4BLOJVLXB J32q5iaBFqqB+yk1MmiZT3ZRr34vFpbA0+ttS+zQoW7zWEU9VTUdExq 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