Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mm/vmalloc: skip clearing page array in vfree()
@ 2026-12-03  8:55 hu.shengming
  2026-12-03 10:26 ` Dev Jain
  2026-12-03 14:37 ` Baoquan He
  0 siblings, 2 replies; 4+ messages in thread
From: hu.shengming @ 2026-12-03  8:55 UTC (permalink / raw)
  To: akpm, urezki; +Cc: linux-mm, baoquan.he, zhang.run

From: Shengming Hu <hu.shengming@zte.com.cn>

vm_area_free_pages() clears freed entries in vm->pages[].
This is needed by vrealloc() shrink, which retains the array,
but not by vfree(), which immediately releases it with kvfree().

Add a clear_pages parameter to skip clearing when the page
array will be freed. Pass false from vfree() and true
from vrealloc() shrink.

On a 64-bit system with 4 KiB pages, this avoids 2 MiB of
unnecessary writes when freeing a 1 GiB vmalloc allocation.

Note that Baoquan He's ongoing VM_SPARSE series adds new
callers of this helper. These callers should pass true
to preserve the existing clearing behavior.

Link: https://lore-kernel.gnuweeb.org/linux-mm/20261129150755.191662-1-baoquan.he@linux.dev/

Signed-off-by: Shengming Hu <hu.shengming@zte.com.cn>
---
 mm/vmalloc.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 320e981188c9..fa402bd38879 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -3541,14 +3541,15 @@ void vfree_atomic(const void *addr)
  * @vm: the vm_struct containing the pages
  * @start_idx: first page index to free (inclusive)
  * @end_idx: last page index to free (exclusive)
+ * @clear_pages: whether to clear freed vm->pages[] entries
  *
  * Free pages [start_idx, end_idx) updating NR_VMALLOC stat accounting.
- * Freed vm->pages[] entries are set to NULL.
+ * If @clear_pages is true, 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 long start_idx,
-			       unsigned long end_idx)
+			       unsigned long end_idx, bool clear_pages)
 {
 	unsigned long i;

@@ -3558,8 +3559,10 @@ static void vm_area_free_pages(struct vm_struct *vm, unsigned long start_idx,
 	}
 	free_pages_bulk(vm->pages + start_idx, end_idx - start_idx);

-	for (i = start_idx; i < end_idx; i++)
-		vm->pages[i] = NULL;
+	if (clear_pages) {
+		for (i = start_idx; i < end_idx; i++)
+			vm->pages[i] = NULL;
+	}
 }

 /**
@@ -3605,7 +3608,7 @@ void vfree(const void *addr)
 	if (unlikely(vm->flags & VM_FLUSH_RESET_PERMS))
 		vm_reset_perms(vm);

-	vm_area_free_pages(vm, 0, vm->nr_pages);
+	vm_area_free_pages(vm, 0, vm->nr_pages, false);
 	kvfree(vm->pages);
 	kfree(vm);
 }
@@ -4643,7 +4646,7 @@ void *vrealloc_node_align_noprof(const void *p, size_t size, unsigned long align
 			vunmap_range(addr + (new_nr_pages << PAGE_SHIFT),
 				     addr + (old_nr_pages << PAGE_SHIFT));

-			vm_area_free_pages(vm, new_nr_pages, old_nr_pages);
+			vm_area_free_pages(vm, new_nr_pages, old_nr_pages, true);
 		}
 		vm->requested_size = size;
 		kasan_vrealloc(p, old_size, size);
-- 
2.43.5


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-03 14:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03  8:55 [PATCH] mm/vmalloc: skip clearing page array in vfree() hu.shengming
2026-12-03 10:26 ` Dev Jain
2026-12-03 13:05   ` hu.shengming
2026-12-03 14:37 ` Baoquan He

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox