From nobody Mon Apr 13 21:41:26 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 9CC2D3B3BE6 for ; Wed, 4 Mar 2026 14:53:29 +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=1772636009; cv=none; b=Q1M9DMGuq15r2vx9Ks7xIJgwMeefWSSb4bFl1Az06j1e+HExq1JT2u18tcP17hN+bI8UsDHXDxeahpjal70HYB4kWDYHkDUVPga1fq52N0PPHbKmPlRkVeK9bvt3KaXjQUXxROa/Y9SSIfJgbh8g8rOVjBu4RMCe48wvQDmaiNg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772636009; c=relaxed/simple; bh=Wa4JBE1IYU7JKwjDkn6HXyP6M9ZwBaG9oz9xsBPZiG8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=qJk8b1eVUXEsvBSCC0oDjvyh/Mq6NADcGeNoEHfbIljMuwTbPjvUzDATtR3tH6py3vnjAhz6Fn2zM0VxAr2UnF5Ytd7FZuSt9GD5kSMr4ZF0Fyfkzpy9d5LSxxIoHaBu/iaKvKUd3e2FfnORqJXEk6aNkKYKvniEBMGLn0KZmhk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=L2f1NtDH; 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="L2f1NtDH" Received: by smtp.kernel.org (Postfix) with ESMTPS id 62D1FC2BC87; Wed, 4 Mar 2026 14:53:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1772636009; bh=Wa4JBE1IYU7JKwjDkn6HXyP6M9ZwBaG9oz9xsBPZiG8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=L2f1NtDHhUjG3fDL1nzaEkzX7Gn1pIidsT4YvlLNlDf/p/aVluQUS0kpY+okMw9ly 1MBMyjbOyJU3JfYBa6AL+MPGXeZUbIc21Rbnfpr1MakG8uDszXERGm+vSSZdKeCVHK Q9Hjm+zHSuF11VmoAkQ/0Uw9sNhHHWpmBXTjJ8TW95clSv2gN+0r9XWsi74osJDkfX Yr/0W68yeey2k8O0koD4tCsu5w2HHa1ZX3o/55OqWlTulVJUsa9/lDeCxJm9jhkfu+ dpQ0DhA8VrwISyKfuZCqUwU1QkBWy3aK4Lv4qgqQBVnGK0i7V8y8BnYYXCjaDG9puM aRAqI0agxs1SQ== 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 4F25CEF8FEE; Wed, 4 Mar 2026 14:53:29 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Wed, 04 Mar 2026 20:23:15 +0530 Subject: [PATCH v2 1/2] mm/vmalloc: extract vmalloc_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: <20260304-vmalloc-shrink-v2-1-28c291d60100@zohomail.in> References: <20260304-vmalloc-shrink-v2-0-28c291d60100@zohomail.in> In-Reply-To: <20260304-vmalloc-shrink-v2-0-28c291d60100@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=1772636006; l=2379; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=ylDdrWHdpgAIPnTMbUkuZf3YssBZgAmCXof5K8bWIJQ=; b=Cc1LRtSDOXj7IWYWNC3yabvfhCMK2bqJRbdVi4kWl/10umyvVSpnr2BPzyCG79sYsIj4j9yzJ cdwhX88SsHnDL55LPD8VYSLaL5a3QqWyOmKCG0ky4d8TOq5mXOyK8k6 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 vmalloc_free_pages() helper. The helper operates on a range [start, end) of pages from a vm_struct, making it suitable for both full free (vfree) and partial free (upcoming vrealloc shrink). No functional change. Signed-off-by: Shivam Kalra --- mm/vmalloc.c | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index c607307c657a..e2aef0a79f2e 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -3416,6 +3416,32 @@ void vfree_atomic(const void *addr) schedule_work(&p->wq); } =20 +/* + * vmalloc_free_pages - free a range of pages from a vmalloc allocation + * @vm: the vm_struct containing the pages + * @start: first page index to free (inclusive) + * @end: last page index to free (exclusive) + * + * Free pages [start, end) updating NR_VMALLOC stat accounting. + * Caller is responsible for unmapping (vunmap_range) and KASAN + * poisoning before calling this. + */ +static void vmalloc_free_pages(struct vm_struct *vm, unsigned int start, + unsigned int end) +{ + unsigned int i; + + for (i =3D start; i < end; i++) { + struct page *page =3D vm->pages[i]; + + BUG_ON(!page); + if (!(vm->flags & VM_MAP_PUT_PAGES)) + mod_lruvec_page_state(page, NR_VMALLOC, -1); + __free_page(page); + cond_resched(); + } +} + /** * 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); @@ -3459,19 +3484,8 @@ 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(); - } + if (vm->nr_pages) + vmalloc_free_pages(vm, 0, vm->nr_pages); kvfree(vm->pages); kfree(vm); } --=20 2.43.0 From nobody Mon Apr 13 21:41:26 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 A8A8C3B3C00 for ; Wed, 4 Mar 2026 14:53:29 +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=1772636009; cv=none; b=S8a86jXFk60DrinZuQKtNkolfyNmVKLigH8+ZK50TGULgK2xfed6zYwcNY3WXmnZXQAQsauji7Ko8UYxZI9tnl2MWe5GJ5fl8lEGn4YXpilDsko+9TBFi//7muVgwpB87nfVId9geOkUqWftCMFydcqS5rhnVEJuMp5S3sibCSY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772636009; c=relaxed/simple; bh=obzS9P6ZNeJAxn5nItDpGm+ktrhNRXvYN4TD1fyB2FU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=DL28KzlBiZS7mkQAHZqqYPTP32ArUmIMbMD9w61V55ZfLVUN6vRTD9PaV7y2pQryMO9P6J96XIdDalsE9vM4Tq9krcxDrYkb60oMQPnbK82trnD14j7gZ1mLti1Qqavj3CaOYrIoHz+MIHTBUUSKkMZFCQ7oC1AI+8+o6SsCzdA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Im4hCNPI; 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="Im4hCNPI" Received: by smtp.kernel.org (Postfix) with ESMTPS id 88F79C2BC9E; Wed, 4 Mar 2026 14:53:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1772636009; bh=obzS9P6ZNeJAxn5nItDpGm+ktrhNRXvYN4TD1fyB2FU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=Im4hCNPIosrXVQBGxE8Ipsea7tdG4rpM+qk85retlt2KiOKccGzRdu3+pAw9dMYXm +ZW9ZBRcatAzi6hTwksmnvsHx8RrvWW8fMqgBrPOrW7r6N/WBOUyrbog5b3rN6BcjS QOrq1XW8T1SnvDo9AjlMve2hFwnoJSgsF0UwzXs1YjYb9TShUuiApiQUL0Gm7H7H8v O4Ep4XwnCVaBkBy0fQQDEZnocobohWIFwvZSN/SRdGY9+1ea03FdC3uIk/6r81ASh7 Nrkps7DkZVSrZYwL7V8jRaGwRcd3/BOW/IO1acL0IqaTJBY5F0MhddYz26GFTz+8/e 63SxP1kXEDN8Q== 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 7CA5CEF8FF3; Wed, 4 Mar 2026 14:53:29 +0000 (UTC) From: Shivam Kalra via B4 Relay Date: Wed, 04 Mar 2026 20:23:16 +0530 Subject: [PATCH v2 2/2] 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: <20260304-vmalloc-shrink-v2-2-28c291d60100@zohomail.in> References: <20260304-vmalloc-shrink-v2-0-28c291d60100@zohomail.in> In-Reply-To: <20260304-vmalloc-shrink-v2-0-28c291d60100@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=1772636006; l=2543; i=shivamkalra98@zohomail.in; s=20260212; h=from:subject:message-id; bh=OKQH3IWxL4ounbCE+6VgaXMjPoT4ux/8tD8AGU1gTJo=; b=XNA3+LeXRJL7O5ATYcsa+KLREiMcLDVQS5J9AI3I7lUZGevOoDDdscBLtWr+7Zg0rAx2tNSCN h50igQWgwQOD+lDRnPxcaWkq3mCXBLTeE2qV6mz8HTdO/zdrWXnwTgZ 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. The virtual address reservation (vm->size / vmap_area) is intentionally kept unchanged, preserving the address for potential future grow-in-place support. Fix the grow-in-place check to compare against vm->nr_pages rather than get_vm_area_size(), since the latter reflects the virtual reservation which does not shrink. Without this fix, a grow after shrink would access freed pages. Signed-off-by: Shivam Kalra --- mm/vmalloc.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index e2aef0a79f2e..1a59afb94ba4 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -4340,14 +4340,23 @@ 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. */ + if (new_nr_pages < vm->nr_pages && !vm_area_page_order(vm)) { + unsigned long addr =3D (unsigned long)p; + + vunmap_range(addr + (new_nr_pages << PAGE_SHIFT), + addr + (vm->nr_pages << PAGE_SHIFT)); + + vmalloc_free_pages(vm, new_nr_pages, vm->nr_pages); + vm->nr_pages =3D new_nr_pages; + } vm->requested_size =3D size; kasan_vrealloc(p, old_size, size); return (void *)p; @@ -4356,7 +4365,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