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; 6+ 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] 6+ messages in thread

* Re: [PATCH] mm/vmalloc: skip clearing page array in vfree()
  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
  1 sibling, 1 reply; 6+ messages in thread
From: Dev Jain @ 2026-12-03 10:26 UTC (permalink / raw)
  To: hu.shengming, akpm, urezki; +Cc: linux-mm, baoquan.he, zhang.run



On 03/12/26 2:25 pm, hu.shengming@zte.com.cn wrote:
> 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.
> 
You could run test_vmalloc.c and check whether you get an
improvement. I expect you get it because that benchmark is
very sensitive.


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

* Re: [PATCH] mm/vmalloc: skip clearing page array in vfree()
  2026-12-03 10:26 ` Dev Jain
@ 2026-12-03 13:05   ` hu.shengming
  0 siblings, 0 replies; 6+ messages in thread
From: hu.shengming @ 2026-12-03 13:05 UTC (permalink / raw)
  To: dev.jain; +Cc: akpm, urezki, linux-mm, baoquan.he, zhang.run

> On 03/12/26 2:25 pm, hu.shengming@zte.com.cn wrote:
> > 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.
> > 
> You could run test_vmalloc.c and check whether you get an
> improvement. I expect you get it because that benchmark is
> very sensitive.

Thanks for the suggestion. I ran test_vmalloc.c.

The vfree time is reduced by about 2%, corresponding to
roughly 1.5-1.7% of the entire fix_size_alloc_test loop.

Test setup: x86-64 KVM guest, 8 vCPUs, 4 GiB RAM,
running next-20261123. The test used a 1 GiB allocation
with 50 loops and 10 repetitions.

A two-boot A/B comparison was too noisy (3-5% variation).
Instead, I used a temporary debugfs knob (not part of the
patch) to switch clearing at runtime and ran five ABAB
pairs within a single boot, after one warm-up run.

The following numbers are averages over five runs for
each setting. Each run consisted of 10 repeats × 50 loops.

                             clear=1     clear=0     delta
vfree, per call              7.394 ms    7.240 ms    -2.1%
vmalloc, per call            3.444 ms    3.417 ms    -0.8%
per repeat (avg)             541.9 ms    532.8 ms    -1.7%

All five clear=1 runs were slower than all five clear=0
runs, confirming a consistent improvement.

--
With Best Regards,
Shengming


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

* Re: [PATCH] mm/vmalloc: skip clearing page array in vfree()
  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 14:37 ` Baoquan He
  2026-12-03 15:37   ` hu.shengming
  1 sibling, 1 reply; 6+ messages in thread
From: Baoquan He @ 2026-12-03 14:37 UTC (permalink / raw)
  To: hu.shengming; +Cc: akpm, urezki, linux-mm, zhang.run

On 12/03/26 at 04:55pm, hu.shengming@zte.com.cn wrote:
> 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.

That's interesting, how did you find it? It's a very tiny gap, I guess
it's reported by AI.

> 
> 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	[flat|nested] 6+ messages in thread

* Re: [PATCH] mm/vmalloc: skip clearing page array in vfree()
  2026-12-03 14:37 ` Baoquan He
@ 2026-12-03 15:37   ` hu.shengming
  2026-12-03 16:32     ` Baoquan He
  0 siblings, 1 reply; 6+ messages in thread
From: hu.shengming @ 2026-12-03 15:37 UTC (permalink / raw)
  To: baoquan.he; +Cc: akpm, urezki, linux-mm, zhang.run

> On 12/03/26 at 04:55pm, hu.shengming@zte.com.cn wrote:
> > 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.

> That's interesting, how did you find it? It's a very tiny gap, I guess
> it's reported by AI.

Yeah, we need more tokens! I spotted it while using AI
to help trace the vfree() path.

--
With Best Regards,
Shengming

> > 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	[flat|nested] 6+ messages in thread

* Re: [PATCH] mm/vmalloc: skip clearing page array in vfree()
  2026-12-03 15:37   ` hu.shengming
@ 2026-12-03 16:32     ` Baoquan He
  0 siblings, 0 replies; 6+ messages in thread
From: Baoquan He @ 2026-12-03 16:32 UTC (permalink / raw)
  To: hu.shengming; +Cc: akpm, urezki, linux-mm, zhang.run

On 12/03/26 at 11:37pm, hu.shengming@zte.com.cn wrote:
> > On 12/03/26 at 04:55pm, hu.shengming@zte.com.cn wrote:
> > > 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.
> 
> > That's interesting, how did you find it? It's a very tiny gap, I guess
> > it's reported by AI.
> 
> Yeah, we need more tokens! I spotted it while using AI
> to help trace the vfree() path.

Thanks for telling, I personally would not change it because the cost is
the function interface change even though it's an internal function, and
the gain is about 2% only for vfree(). Anyway, leave it to maintainer
Uladzislau to decide.

Thanks
Baoquan

> > > 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	[flat|nested] 6+ messages in thread

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

Thread overview: 6+ 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
2026-12-03 15:37   ` hu.shengming
2026-12-03 16:32     ` Baoquan He

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