Skip to content

mm/vmalloc: free unused pages on vrealloc() shrink

Shivam Kalra b4-sent linux-mm lkml 14 versions – Applied

Status from this message: [merged mm-stable] lib-test_vmalloc-add-vrealloc-test-case.patch removed from -mm tree

Get series with b4
b4 am [email protected] change-id 20260302-vmalloc-shrink-04b2fa688a14

Versions newest first

  1. v14 Latest5 patches 3 repliesApplied 5/5
  2. v135 patches 3 repliesSuperseded
  3. v125 patches 10 repliesSuperseded
Earlier versions · 11
VersionPostedPatchesRepliesStatus
v115 patches2 repliesSuperseded
v104 patches8 repliesSuperseded
v94 patches5 repliesSuperseded
v86 patches10 repliesSuperseded
v76 patches3 repliesSuperseded
v66 patches2 repliesSuperseded
v53 patches13 repliesSuperseded
v43 patches2 repliesSuperseded
v32 patches4 repliesSuperseded
v22 patches3 repliesSuperseded
v12 patches3 repliesSuperseded

Changes in v14 from the cover letter

- Scope nr_pages usage in vread_iter() to VM_ALLOC areas only;
vmap() areas don't initialize nr_pages. (Sashiko, Uladzislau Rezki)
- Link to v13: https://patch.msgid.link/[email protected]

Patches in v14 All 5 applied

#Subject and filesSizeRepliesApplied
1/5mm/vmalloc: extract vm_area_free_pages() helper from vfree()mm/vmalloc.c+27 -70 repliesApplied
2/5mm/vmalloc: use physical page count for vrealloc() grow-in-place checkmm/vmalloc.c+7 -10 repliesApplied
3/5mm/vmalloc: use physical page count in vread_iter() for VM_ALLOC areasmm/vmalloc.c+12 -10 repliesApplied
4/5mm/vmalloc: free unused pages on vrealloc() shrinkmm/vmalloc.c+52 -40 repliesApplied
5/5lib/test_vmalloc: add vrealloc test caselib/test_vmalloc.c+62 -00 repliesApplied

Reviews trailers per patch

All 5 patches have a Reviewed-by or Acked-by.

PatchUladzislau Rezki (Sony)
1/5R-b
2/5R-b
3/5R-b
4/5R-b
5/5R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v14 thread

Cover letter [PATCH v14 0/5] mm/vmalloc: free unused pages on vrealloc() shrink

This series implements the TODO in vrealloc() to unmap and free unused pages when shrinking across a page boundary.

Problem:
When vrealloc() shrinks an allocation, it updates bookkeeping (requested_size, KASAN shadow) but does not free the underlying physical pages. This wastes memory for the lifetime of the allocation.

Solution:
- Patch 1: Extracts a vm_area_free_pages(vm, start_idx, end_idx) helper from vfree() that frees a range of pages with memcg and nr_vmalloc_pages accounting. Freed page pointers are set to NULL to prevent stale references.
- Patch 2: 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 sizes. This is a prerequisite for shrinking.
- Patch 3: For VM_ALLOC areas in vread_iter(), derive the vm area size from vm->nr_pages rather than get_vm_area_size(), which would overestimate the mapped range after a shrink. Other mapping types (vmap, ioremap) don't set nr_pages and keep using get_vm_area_size().
- Patch 4: Uses the helper to free tail pages when vrealloc() shrinks across a page boundary.
- Patch 5: Adds a vrealloc test case to lib/test_vmalloc that exercises grow-realloc, shrink-across-boundary, shrink-within-page, and grow-in-place paths.

The virtual address reservation is kept intact to preserve the range for potential future grow-in-place support.
A concrete user is the Rust binder driver's KVVec::shrink_to [1], which performs explicit vrealloc() shrinks for memory reclamation.

Tested:
- KASAN KUnit (vmalloc_oob passes)
- lib/test_vmalloc stress tests (3/3, 1M iterations each)
- checkpatch, sparse, W=1, allmodconfig, coccicheck clean

[1] https://lore.kernel.org/all/[email protected]/

---
Changes in v14:
- Scope nr_pages usage in vread_iter() to VM_ALLOC areas only;
vmap() areas don't initialize nr_pages. (Sashiko, Uladzislau Rezki)
- Link to v13: https://patch.msgid.link/[email protected]

Changes in v13:
- Collect all r-b tags
- Resubmit for Review.
- Link to v12: https://lore.kernel.org/r/[email protected]

Changes in v12:
- Rewrite vm_area_free_pages() to use free_pages_bulk()
following the upstream vfree() refactoring.(Andrew)
- Drop Reviewed-by tags from Patch 1 due to the rewrite
- Rebase to latest mm-new
- Link to v11: https://lore.kernel.org/r/[email protected]

Changes in v11:
- Prepare vread_iter() to use vm->nr_pages instead of
get_vm_area_size() (Uladzislau Rezki, Sashiko)
- Drop (size_t) cast from nr_pages << PAGE_SHIFT (Uladzislau Rezki)
- Link to v10: https://lore.kernel.org/r/[email protected]

Changes in v10:
- Reorder vm->nr_pages to the beginning (Alice Ryhl)
- Link to v9: https://lore.kernel.org/r/[email protected]

Changes in v9:
- Remove READ_ONCE, WRITE_ONCE and drop commit
about show_numa_info. (Uladzislau Rezki)
- Update the commit message in Patch 2. (Alice Ryhl)
- Remove zero newly exposed memory commit.
- Link to v8: https://lore.kernel.org/r/[email protected]

Changes in v8:
- Strip the KASAN tag from the pointer before addr_to_node()
to avoid acquiring the wrong node lock (Sashiko).
- Rebase to latest mm-new.
- Link to v7: https://lore.kernel.org/r/[email protected]

Changes in v7:
- Fix NULL pointer dereference in shrink path (Sashiko)
- Acquire vn->busy.lock when updating vm->nr_pages to synchronize
with concurrent readers (Uladzislau Rezki)
- Use READ_ONCE in vmalloc_dump_obj (Sashiko)
- Skip shrink path on GFP_NIO or GFP_NOFS. (Sashiko)
- Fix Overflow issue for large allocations. (Sashiko)
- Use vrealloc instead of vmalloc in vrealloc test.
- Link to v6: https://lore.kernel.org/r/[email protected]

Changes in v6:
- Fix VM_USERMAP crash by explicitly bypassing early in the shrink path if the flag is set.(Sashiko)
- Fix Kmemleak scanner panic by calling kmemleak_free_part() to update tracking on shrink.(Sashiko)
- Fix /proc/vmallocinfo race condition by protecting vm->nr_pages access with READ_ONCE()/WRITE_ONCE() for concurrent readers.(Sashiko)
- Fix stale data leak on grow-after-shrink by enforcing mandatory zeroing of the newly exposed memory.(Sashiko)
- Fix memory leaks in vrealloc_test() by using a temporary pointer to preserve and
free the original allocation upon failure.(Sashiko)
- Rename vmalloc_free_pages parameters from start/end to start_idx/end_idx for better clarity.(Uladzislau Rezki)
- Link to v5: https://lore.kernel.org/r/[email protected]
- Link to Sashiko: https://sashiko.dev/#/patchset/20260317-vmalloc-shrink-v5-0-bbfbf54c5265%40zohomail.in

Changes in v5:
- Skip vrealloc shrink for VM_FLUSH_RESET_PERMS (Uladzislau Rezki)
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Rename vmalloc_free_pages() to vm_area_free_pages() to align with vm_area_alloc_pages() (Uladzislau Rezki)
- NULL out freed vm->pages[] entries to prevent stale pointers (Alice Ryhl)
- Remove redundant if (vm->nr_pages) guard in vfree() (Uladzislau Rezki)
- Add vrealloc test case to lib/test_vmalloc (new patch 3/3)
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Restore the comment.
- Rebase to the latest mm-new
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Updated the base-commit to mm-new
- Fix conflicts after rebase
- Ran clang-format on the changes made
- Use a single kasan_vrealloc (Alice Ryhl)
- Link to v1: https://lore.kernel.org/r/[email protected]

To: Andrew Morton <[email protected]>
To: Uladzislau Rezki <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
Shivam Kalra (5):
mm/vmalloc: extract vm_area_free_pages() helper from vfree() mm/vmalloc: use physical page count for vrealloc() grow-in-place check mm/vmalloc: use physical page count in vread_iter() for VM_ALLOC areas mm/vmalloc: free unused pages on vrealloc() shrink lib/test_vmalloc: add vrealloc test case

 lib/test_vmalloc.c |  62 ++++++++++++++++++++++++++++++
 mm/vmalloc.c       | 111 ++++++++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 160 insertions(+), 13 deletions(-)

---
base-commit: a277a07d19bffcad38dea57bc234d0810a328657
change-id: 20260302-vmalloc-shrink-04b2fa688a14

Best regards,

Signature · 1 line
Shivam Kalra <[email protected]>