From: Uladzislau Rezki <urezki@gmail.com>
To: Jill Ravaliya <jillravaliya@gmail.com>
Cc: akpm@linux-foundation.org, urezki@gmail.com, linux-mm@kvack.org,
linux-kernel@vger.kernel.org,
Shivam Kalra <shivamkalra98@zohomail.in>
Subject: Re: [PATCH 1/2] mm/vmalloc: free unused pages when shrinking vrealloc() allocation
Date: Thu, 7 May 2026 19:17:54 +0200 [thread overview]
Message-ID: <afzJQlfTYbFAN8Wl@milan> (raw)
In-Reply-To: <20260507114854.41117-1-jillravaliya@gmail.com>
On Thu, May 07, 2026 at 05:18:53PM +0530, Jill Ravaliya wrote:
> 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
>
There is already work to address this: https://lore.kernel.org/all/20260428-vmalloc-shrink-v12-0-3c18c9172eb1@zohomail.in/
--
Uladzislau Rezki
next prev parent reply other threads:[~2026-05-07 17:17 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-07 11:48 [PATCH 1/2] mm/vmalloc: free unused pages when shrinking vrealloc() allocation Jill Ravaliya
2026-05-07 11:48 ` [PATCH 2/2] selftests/mm: add test for vrealloc() shrink page freeing Jill Ravaliya
2026-05-07 17:17 ` Uladzislau Rezki [this message]
2026-05-07 20:26 ` [syzbot ci] Re: mm/vmalloc: free unused pages when shrinking vrealloc() allocation 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=afzJQlfTYbFAN8Wl@milan \
--to=urezki@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=jillravaliya@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=shivamkalra98@zohomail.in \
/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