From nobody Wed Jun 10 08:23:11 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 73F824D2EC8 for ; Tue, 19 May 2026 12:12:31 +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=1779192751; cv=none; b=M7kZmaiRVIlMf6dgBYaaD/iKf90MblUP+Ig64QN7EMKJdQysLFXB2JPHkMMPaWvufYDwcf2JDWHSlu2qcaxPbHBlH3BfEtioML8xSL511LWScwODwif1Z41jgNP8d1KODQEbfp7lnhNEdghQdF6oCk/BA2MLaZJyilcZgbONeYk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779192751; c=relaxed/simple; bh=JjaSFuYe7b6lS86dH1GkDUShlw+lnlW94v+/M0gOE9Q=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=YIM2VvqXXr9grNMtA+nVZADc1KzaIfZoa/b8dB4HmWX387wiPDsGbZPZlOmdLT6M25IEPWASlPt7WGqpTXYey3449cRZ5PlDzOjbGi1PD2kpCjfJ9U+KBwcOFT8so7H2U9dMTJtkJ9RcduDUU+Fx8Bdt1V0Gi+pKCJmQlbd5LRY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Ovz1yWUW; 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="Ovz1yWUW" Received: by smtp.kernel.org (Postfix) with ESMTPS id 30406C2BCC6; Tue, 19 May 2026 12:12:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1779192751; bh=JjaSFuYe7b6lS86dH1GkDUShlw+lnlW94v+/M0gOE9Q=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=Ovz1yWUW9o31ZcoZahosrguXg/SML9syMt9vNwgc5+xX21XeINHAeA2uD1KEr3jud egn8EdxnlvSjlA/bpGhg0Zfiz6hc5FkQZguM9Z4CJ51S57h7FE75K/y6ipzgVP4xPP e+/H1XZkN80jWLatRtxOFu4SGTE9RM/8Eskx+cG2UnPVxCHwFGD+hgdtu5QPCV0Nwr MkjXyidq1ICtEoyfeMTvXKUE1MT5jKYw/3qx5ujGX6vD+uLdmp3KvT8L8NYZTo0+H9 CTxDQ1BitQI2cn4i8lXWvhvspsvKoBV98VPH4+oFGTc/n+Ose4Z0wDAMehPcgtfDhG MszCcoz3vAp+Q== 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 20C6BCD5BA4; Tue, 19 May 2026 12:12:31 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Tue, 19 May 2026 17:42:14 +0530 Subject: [PATCH v14 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: <20260519-vmalloc-shrink-v14-1-70b96ee3e9c9@zohomail.in> References: <20260519-vmalloc-shrink-v14-0-70b96ee3e9c9@zohomail.in> In-Reply-To: <20260519-vmalloc-shrink-v14-0-70b96ee3e9c9@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.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1779192749; l=2470; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=bztRoZL7KG5/gkbYV0tpM0YC0DnlnM8KHP5WxzHQxJY=; b=LK61kZLrhgYetzYF6yc9h/2X8RSsJN1fh4jgcEDRwFmB+VRZ+bGS2RFhvjoQgx/h2KAwBmidK A9nE6+VpMvJA6bGQMEDUe0wu11CyThjN7CW2KJmu2JT0Cak/3RP60Hu 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). Reviewed-by: Uladzislau Rezki (Sony) Signed-off-by: Shivam Kalra 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 eabb86b13b7e..5555601b9529 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 10 08:23:11 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 73E4C4CA287 for ; Tue, 19 May 2026 12:12:31 +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=1779192751; cv=none; b=NIkhw/diTfiK6+fvW2B+2jbdmXnC62w8NJn8QDFSI2nUJ+CDyUGETXrHSlfNbt/k1TEUhzI2AcEdBVndwksMTjHP5LZ24EUwWFxkefwGomiDc1Py1xQmJSLLxdfWvuHFpBrOiqfuqBP5Jb4NHcdC/O3rBuNWGRyAGAmym50hRHo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779192751; c=relaxed/simple; bh=Vr0IHoxwEI2Zc2s6Bahmf4XnaDVxjgNzlt5KNphDUvE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=gkpRDBLBif5tkOap7JNooG0rBH5SzoqW+hqVByF/Zpfk6A0i8XDZzS7f/wHRYU64PrO9Rrw1sCfjfjmXD7FOX5dG1ZkhD/Ab45t1lsJtWeVjewRgSiZBzRAXkGI5BPP1zaad3c7mhrnNYH3WvcYeAeC5K9N+rgfPw8iU9KFLp20= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qXE1gqS8; 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="qXE1gqS8" Received: by smtp.kernel.org (Postfix) with ESMTPS id 38B54C2BCB8; Tue, 19 May 2026 12:12:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1779192751; bh=Vr0IHoxwEI2Zc2s6Bahmf4XnaDVxjgNzlt5KNphDUvE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=qXE1gqS8T+m5qnUOmak7CkzQKTSFu+yEjAMA+9dnsCwFn4Ltd+sXN1QRr4DprT0Tw P8Rwz2pvH1W+l9N82abUA79KQ+IB927aKSLGSUowkTGb7n0XdKkLBSligRgLo9k/2W oCqeMnwLVsSLkam2w8i9bayozBMSd/TNPY8Xf8tzE90+YRJJ0IyM/HK+HAj4ZTiMEN 3JOGaJbz3uJ7QWZSH5SJfk7UbdS4l/ZzfzaC4NRCv4QowsydZb4V3w6GBTeqJYamvo 1YcrU+wwGUrtwJOwfGZ2wpZgHXr8xQt1fhaf5D2eCNpteXtZ3kODNNvxzLbrZmyOWL YoR3FgRVls8oA== 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 2F441CD5BA6; Tue, 19 May 2026 12:12:31 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Tue, 19 May 2026 17:42:15 +0530 Subject: [PATCH v14 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: <20260519-vmalloc-shrink-v14-2-70b96ee3e9c9@zohomail.in> References: <20260519-vmalloc-shrink-v14-0-70b96ee3e9c9@zohomail.in> In-Reply-To: <20260519-vmalloc-shrink-v14-0-70b96ee3e9c9@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.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1779192749; l=1768; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=PlRn+4ky3xwmAe2MvESYvdRi8E+mwHTHUGmONHypEP0=; b=g7jXo2qEkXEzlGxmL3RUHs8WUq5wxnSpq8dRxDH/mQxZsNKZZ5CmZoXNomzQByJsSW7j2bPS5 Ugb1mu+cfOEBXXUAtEfpvE4X+5IKZuGn0sqGH4hdeRPKgRmRCUxDJZM 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. Reviewed-by: Uladzislau Rezki (Sony) 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 5555601b9529..3e159b74cfab 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -4343,6 +4343,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 /* @@ -4361,7 +4367,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 10 08:23:11 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 73EE44CA292 for ; Tue, 19 May 2026 12:12:31 +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=1779192751; cv=none; b=LDD4Sn5rXlRzrVcswX9ag++jHQr887li2/Y2ShIFj3Yg7pLiqTl2pmCIp4r+kH5H8qIA4jFnvqluZezpJ2uLKp5y8Ob5nb09d2Favpn+Im9xKCoH93V8DFG1bbhA+kIVUWwDhkwm2P28RLQiH746sEazTvSTOXpTRnAZBVa8zp4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779192751; c=relaxed/simple; bh=DoyPRnUD10ZYPnUyihoSOX0F7D142OjSjSI8+OzltR4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=O/uA5WuzRP7mxwdfA4SebQYQozYtevVS5m04Ivf198LY8WAMyo6O5xvkB9JWvX3tsOAurjPg2B2b7AeyJV0q5ueHtnGgifocBqsU6d9jlbhNuJdqj3uyZR/1FgIHLis/7Jj218piQjGvx72FpgCitWvJt7xGlqm1HvD0tSyg/DA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=H9/vs7xV; 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="H9/vs7xV" Received: by smtp.kernel.org (Postfix) with ESMTPS id 4611EC2BCF5; Tue, 19 May 2026 12:12:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1779192751; bh=DoyPRnUD10ZYPnUyihoSOX0F7D142OjSjSI8+OzltR4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=H9/vs7xVUDwl/4cYlaH9pVVBv6paPsmJTXoNK3frswbc7a9zAh1x2iOAVw8DgJ59I LrCGvARR/hJpQd6miBDrlgNcKtOosFidJqra+mMoZRzQFDhu0PuzYRcljg0qYsVn2f t0Jo8qskQLXLUQC1g5dJubhoHarjKDWFe0b30wRsLiTQujvoarpLHBbO2RmnWH0XBd bUYU8twVVZTmGZ/GaQALXajBIg9zOrczH1bW9r332cFvw64XNWL299pzE6Z4WX9388 WSRuA2NuhMuq/XH+P6YAeJk/unOpOKNlRuJ9lHLaRalT2RjrSU3FJzHollYN70cBdO RexQrfPrJ/13Q== 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 3F7DDCD4851; Tue, 19 May 2026 12:12:31 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Tue, 19 May 2026 17:42:16 +0530 Subject: [PATCH v14 3/5] mm/vmalloc: use physical page count in vread_iter() for VM_ALLOC areas 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: <20260519-vmalloc-shrink-v14-3-70b96ee3e9c9@zohomail.in> References: <20260519-vmalloc-shrink-v14-0-70b96ee3e9c9@zohomail.in> In-Reply-To: <20260519-vmalloc-shrink-v14-0-70b96ee3e9c9@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.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1779192749; l=1455; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=17YglxgG2ppUqTmOumv09d5uhEbrPDSaz3Teo6shawQ=; b=15zXYsWKqHJrAve7nV5Lj3j6NhR1QbpkCVzs/TxDWjJT14PEhptP1gVhj2ucTu11Cb1lwn2Qx 5SiREPfwJ02C/Z3uoyWrH9NoY6YBtHeGjLyWDQJn182X/2d4P8Kfa3G 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 For VM_ALLOC areas in vread_iter(), derive the vm area size from vm->nr_pages rather than get_vm_area_size(). Only VM_ALLOC areas are subject to vrealloc() shrinking, which frees pages without reducing the virtual reservation size. Switch to using vm->nr_pages for VM_ALLOC areas so the reader remains correct once shrink support is added. Other mapping types (vmap, ioremap) do not initialize nr_pages and will continue using get_vm_area_size(). Reviewed-by: Uladzislau Rezki (Sony) Signed-off-by: Shivam Kalra Suggested-by: Danilo Krummrich --- mm/vmalloc.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 3e159b74cfab..2f606cfade9b 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -4666,7 +4666,18 @@ 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) + /* + * For VM_ALLOC areas, use nr_pages rather than + * get_vm_area_size() because vrealloc() may shrink + * the mapping without updating area->size. Other + * mapping types (vmap, ioremap) don't set nr_pages. + */ + size =3D (vm->flags & VM_ALLOC) ? + (vm->nr_pages << PAGE_SHIFT) : + get_vm_area_size(vm); + else + size =3D va_size(va); =20 if (addr >=3D vaddr + size) goto next_va; --=20 2.43.0 From nobody Wed Jun 10 08:23:11 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 7E40E4CA273 for ; Tue, 19 May 2026 12:12:31 +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=1779192751; cv=none; b=YP0iOvBZY55Ms0rll58/yvkHVblnSW29W805tK+HP1toBS7ItL8IVbhvQyi/3xDH6yHV+FDJtn1qKhPX65Tt16lgIsRwXT6kd3UmAF69A6Ba+Cvbb6rv5zO0tvqKdSvxVZRY3Y6KgbO4k7aIFQHCiV+x/06N659mM/9yghmaAyg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779192751; c=relaxed/simple; bh=kzxOELvI03StZULzOSrGFtYm7t4tK35U/3bSVguwEnk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=unF0DcfV6P8rVPIwhIK1dRWZbd482AoPrmuBX7kMLbPi0LTsO8lIrFe5C8oZ6Zw4PE77g87jy0r6B0/r8v8q5DA9vHOPA7xvNEzH5uqjCPAmoZsiipduI85JfX7AGqvljqTidcKrdP77dbYsDmm5rpu//NAMMhyA4lPV0VTeNSA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hcTfvcUc; 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="hcTfvcUc" Received: by smtp.kernel.org (Postfix) with ESMTPS id 58571C2BCF6; Tue, 19 May 2026 12:12:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1779192751; bh=kzxOELvI03StZULzOSrGFtYm7t4tK35U/3bSVguwEnk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=hcTfvcUc3fYIat2GsWZ1uo3Ku8INsqF7cLlm5zEb+MXleqnh9mMpllvrjJ37Q0D81 PJmYoAPIjx3t7SyPkqWCEwaHf987tyds4DaKO3fM4ClP9R1m9Ktuu+n3yenzYXo6pU cFcK88m21uczd8b9qz50GOl3d5EsYrz1lnVrJEneFsKNx9gfe2S792D/stwxN1la0F qxzfOJeGOr8XnUlfH0HEziVjImzx6i41in7yAvsfqHtaOHa9A3ihvTDA/Qz5yZtZyi S1v2aVPqH7tZ6gqKgxfOWLMNvm/gTf+cbhkouRHA3RCpnDxTGMX1gsrtWyqvtmSJ63 gWCTJNQLqchfQ== 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 4F9E5CD5BA4; Tue, 19 May 2026 12:12:31 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Tue, 19 May 2026 17:42:17 +0530 Subject: [PATCH v14 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: <20260519-vmalloc-shrink-v14-4-70b96ee3e9c9@zohomail.in> References: <20260519-vmalloc-shrink-v14-0-70b96ee3e9c9@zohomail.in> In-Reply-To: <20260519-vmalloc-shrink-v14-0-70b96ee3e9c9@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.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1779192749; l=4186; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=JWwYna1z+im5GzJcD/lBquWI944uUkw7bLFPCe6kW04=; b=i6e63l0ACVS36Cc+z2Ha9I3XK+hOcrdvYWstKumceo/6PSgfVjaeLgOm2oVqdru5vl/VH2NPO Yzc3aJw8kYNCIF0ejwgbEVWmq4FK97XWCwH1aJ3qytSqXrs+hoxWWxu 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 Reviewed-by: Uladzislau Rezki (Sony) 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 2f606cfade9b..12b750bb6d70 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -4351,14 +4351,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 10 08:23:11 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 9A1A84D8D8E for ; Tue, 19 May 2026 12:12:31 +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=1779192751; cv=none; b=lCJNtaTKwUZGbNoKWzqG95fPXxmJ1rDXuJ70TpVYOVfPFN6OaCImUpbLwGIzTfZP0lbtbIGslRJjogU3LdXiwxPmohhyOFWAK5HlrODuvRYfN3TYEJZvb2D3lC0UyZIvFJdiN6E2G5YKe4lLUgSdMjtYSNXLjEyVz2ZoL8x9/L4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779192751; c=relaxed/simple; bh=NgcWajqEHrBjrpmlHzE3kqeLHOp/AqP67j8VknlwGaM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=O/5BOAc4N9IOPMP5/Hq/B3zWtdOAvXakz2H2a76esBbrWbK/n5yU8FL5/1U69rjO0HMn5bTmm2jtxdenmmr27eaPKVbiZj7CWSAnifW9ugZezC9HuKncqNVy3JjRdJno5la9xM6VYXNIcoC0Azac31irQp0OAdbeRaJS2kzBZ5k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ihzdS6SP; 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="ihzdS6SP" Received: by smtp.kernel.org (Postfix) with ESMTPS id 65E24C2BCFB; Tue, 19 May 2026 12:12:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1779192751; bh=NgcWajqEHrBjrpmlHzE3kqeLHOp/AqP67j8VknlwGaM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=ihzdS6SPoeYv+Tp3wB5p5u9s7CnS/6rsZyVon0lZHxFW6ZIRqTWR4gA1C/NLWZSpa PeJKsvSTaV4VmpAlCNBdOzuzgq0SZuHqNKXrf0RtxEBNJSoZe/V6DXTs9zWrpLU8uB XegrIzL5LYQpJCmkrTzreSWHnzpU7+vRJ1hN83I641mRY3VC7y5n0mm4ohpsFx7Kv0 SQE6TDlXGMcRy1OFJo2qhfAfLNgIGjD1k2qqQNizSHTbzyB2pubeLosxaQZkWr9nJz nyiT5qwXbS4hcbHO3yi+2WBotZFo0wWkD83BOM9BlKmKH0/Ic/Nhi6eg16QEAEWlpS fiSPkNBCi3Amg== 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 5F30ACD4F5B; Tue, 19 May 2026 12:12:31 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Tue, 19 May 2026 17:42:18 +0530 Subject: [PATCH v14 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: <20260519-vmalloc-shrink-v14-5-70b96ee3e9c9@zohomail.in> References: <20260519-vmalloc-shrink-v14-0-70b96ee3e9c9@zohomail.in> In-Reply-To: <20260519-vmalloc-shrink-v14-0-70b96ee3e9c9@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.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1779192749; l=2905; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=yP1QXayDidKnNU2S8I7BqJprRzQI2mQyl0fytHsaqPM=; b=qRwd2vwgvAIzFKzG34KR4AENXRAoMWlQWk9KjRHrtik8HlO1YbhPgnKGodIcAnWABPsjDf3lE 5O5VZg2CZVBA2SAi/4XKrKRLkb+1syhjHClgrYqcMSjJMvi8TJiBoQ9 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