Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Baoquan He <baoquan.he@linux.dev>
To: hu.shengming@zte.com.cn
Cc: akpm@linux-foundation.org, urezki@gmail.com, linux-mm@kvack.org,
	zhang.run@zte.com.cn
Subject: Re: [PATCH] mm/vmalloc: skip clearing page array in vfree()
Date: Thu, 3 Dec 2026 22:37:54 +0800	[thread overview]
Message-ID: <axF-wkau6VGK81Qh@fedora> (raw)
In-Reply-To: <20261203165513646NPjqNhu4nc7m3b7zg9wRc@zte.com.cn>

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


      parent reply	other threads:[~2026-12-03 14:38 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

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=axF-wkau6VGK81Qh@fedora \
    --to=baoquan.he@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=hu.shengming@zte.com.cn \
    --cc=linux-mm@kvack.org \
    --cc=urezki@gmail.com \
    --cc=zhang.run@zte.com.cn \
    /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