From: Jill Ravaliya <jillravaliya@gmail.com>
To: akpm@linux-foundation.org, urezki@gmail.com
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
Jill Ravaliya <jillravaliya@gmail.com>
Subject: [PATCH 1/2] mm/vmalloc: free unused pages when shrinking vrealloc() allocation
Date: Thu, 7 May 2026 17:18:53 +0530 [thread overview]
Message-ID: <20260507114854.41117-1-jillravaliya@gmail.com> (raw)
vrealloc() shrink path zeros unused memory and updates
vm->requested_size, but never frees the physical pages,
removes page table mappings, or flushes the TLB for the
unused range.
When a caller shrinks a vmalloc allocation, physical pages
backing the unused portion remain allocated until vfree()
is eventually called, wasting real RAM.
Fix this by unmapping the unused virtual range using
vunmap_range() which also flushes the TLB, freeing each
unused physical page back to the buddy allocator, and
updating vm->nr_pages to reflect the new page count.
Signed-off-by: Jill Ravaliya <jillravaliya@gmail.com>
---
mm/vmalloc.c | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index aa08651ec..a8cedfc5d 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -4336,6 +4336,27 @@ void *vrealloc_node_align_noprof(const void *p, size_t size, unsigned long align
memset((void *)p + size, 0, old_size - size);
vm->requested_size = size;
kasan_vrealloc(p, old_size, size);
+
+ /* Shrink the vm_area: unmap and free unused pages. */
+ if (size < alloced_size) {
+ unsigned long new_nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
+ unsigned long i;
+
+ /* Unmap unused virtual range and flush TLB. */
+ vunmap_range((unsigned long)p + PAGE_ALIGN(size),
+ (unsigned long)p + alloced_size);
+
+ /* Free unused physical pages back to buddy allocator. */
+ for (i = new_nr_pages; i < vm->nr_pages; i++) {
+ mod_lruvec_page_state(vm->pages[i],
+ NR_VMALLOC, -1);
+ __free_page(vm->pages[i]);
+ vm->pages[i] = NULL;
+ }
+
+ vm->nr_pages = new_nr_pages;
+ }
+
return (void *)p;
}
--
2.43.0
next reply other threads:[~2026-05-07 11:49 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-07 11:48 Jill Ravaliya [this message]
2026-05-07 11:48 ` [PATCH 2/2] selftests/mm: add test for vrealloc() shrink page freeing Jill Ravaliya
2026-05-07 17:17 ` [PATCH 1/2] mm/vmalloc: free unused pages when shrinking vrealloc() allocation Uladzislau Rezki
2026-05-07 20:26 ` [syzbot ci] " syzbot ci
-- strict thread matches above, loose matches on Subject: below --
2026-05-09 4:11 [PATCH 1/2] " Jill Ravaliya
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260507114854.41117-1-jillravaliya@gmail.com \
--to=jillravaliya@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=urezki@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox