All of lore.kernel.org
 help / color / mirror / Atom feed
From: Baolin Wang <baolin.wang@linux.alibaba.com>
To: samridh@modal.com, Hugh Dickins <hughd@google.com>,
	David Hildenbrand <david@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] mm/shmem: avoid zeroing new folios twice with init_on_alloc=1
Date: Wed, 18 Nov 2026 11:29:41 +0800	[thread overview]
Message-ID: <8d90ee6b-ed72-4ace-b667-42e35c5baa5a@linux.alibaba.com> (raw)
In-Reply-To: <20261117-samridh-shmem-send-v1-1-1b897ff562d9@modal.com>



On 11/18/26 3:32 AM, Samridh via B4 Relay wrote:
> From: Samridh <samridh@modal.com>
> 
> shmem_get_folio_gfp() clears a new folio before marking it uptodate.
> With init_on_alloc=1 the page allocator has already zeroed it, so the
> folio ends up being zeroed twice.
> 
> For SGP_CACHE, the only mode that clears a new folio, ask the page
> allocator for a zeroed folio with __GFP_ZERO. Folios that are already in
> the page cache but not uptodate, such as those left by fallocate(), are
> still cleared. With init_on_alloc=0 the zeroing moves into the
> allocator, so a folio is zeroed for nothing when the memcg charge, the
> page cache insert or the block accounting fails.
> 
> Time to write one byte to each page of a new 4 GiB memfd mapping, with
> shmem_enabled=always and init_on_alloc=1 (median of 24 runs on 6.18.55,
> x86_64 VMs):
> 
>                    before   after
>    Xeon 6975P-C    0.620s   0.416s
>    EPYC 9R14       0.216s   0.158s
> 
> With 4K folios, or with init_on_alloc=0, the times change by 3% or
> less. We found this while running gVisor, which keeps sandbox memory in
> a memfd.
> 
> I wrote this patch with help from an AI coding assistant (Claude Code),
> then reviewed and tested it.
> 
> Signed-off-by: Samridh <samridh@modal.com>
> ---
> Tested on 6.18.55 with this change backported, on Intel and AMD x86_64
> VMs, and build-tested on mm/core for-next. I also wrote a simple test.
> It fills memory with data, frees it, and then checks that new memfd
> pages read as zero. It covers MADV_HUGEPAGE and MADV_NOHUGEPAGE on the
> fault and fallocate paths. It passes on both machines with
> init_on_alloc/init_on_free at 1/0, 0/0 and 1/1.
> 
> The idea came from David's comment on the __GFP_PREZEROED RFC [1].
> One thing I'm not sure about is whether flush_dcache_folio() after the
> allocator has zeroed the folio is enough on architectures with cache
> aliasing, so a second look there would be welcome.
> 
> [1] https://lore.kernel.org/all/b258160c-5287-43dd-b56e-268c403b85ea@kernel.org/
> ---
>   mm/shmem.c | 13 +++++++++----
>   1 file changed, 9 insertions(+), 4 deletions(-)
> 
> diff --git a/mm/shmem.c b/mm/shmem.c
> index 5cb628cc9c6b..d2c6781e86a5 100644
> --- a/mm/shmem.c
> +++ b/mm/shmem.c
> @@ -2692,6 +2692,7 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index,
>   	int error;
>   	bool alloced;
>   	unsigned long orders = 0;
> +	gfp_t zero_gfp = sgp == SGP_CACHE ? __GFP_ZERO : 0;
>   
>   	if (WARN_ON_ONCE(!shmem_mapping(inode->i_mapping)))
>   		return -EINVAL;
> @@ -2770,7 +2771,7 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index,
>   
>   		huge_gfp = vma_thp_gfp_mask(vma);
>   		huge_gfp = thp_shmem_limit_gfp_mask(huge_gfp, gfp);
> -		folio = shmem_alloc_and_add_folio(vmf, huge_gfp,
> +		folio = shmem_alloc_and_add_folio(vmf, huge_gfp | zero_gfp,
>   				inode, index, fault_mm, orders);
>   		if (!IS_ERR(folio)) {
>   			if (folio_test_pmd_mappable(folio))
> @@ -2782,7 +2783,8 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index,
>   			goto repeat;
>   	}
>   
> -	folio = shmem_alloc_and_add_folio(vmf, gfp, inode, index, fault_mm, 0);
> +	folio = shmem_alloc_and_add_folio(vmf, gfp | zero_gfp, inode, index,
> +					  fault_mm, 0);
>   	if (IS_ERR(folio)) {
>   		error = PTR_ERR(folio);
>   		if (error == -EEXIST)
> @@ -2810,8 +2812,11 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index,
>   	if (sgp != SGP_WRITE && !folio_test_uptodate(folio)) {
>   		long i, n = folio_nr_pages(folio);
>   
> -		for (i = 0; i < n; i++)
> -			clear_highpage(folio_page(folio, i));
> +		/* A folio allocated with __GFP_ZERO is already zeroed. */
> +		if (!alloced || !zero_gfp) {

I don't like the GFP flag hack. Why not use user_alloc_needs_zeroing() 
to decide whether we need to zero folios, like anonymous memory does?


  reply	other threads:[~2026-11-18  3:29 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 19:32 [PATCH] mm/shmem: avoid zeroing new folios twice with init_on_alloc=1 Samridh
2026-11-17 19:32 ` Samridh via B4 Relay
2026-11-18  3:29 ` Baolin Wang [this message]
2026-11-18  4:59   ` Fnu Samridh

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=8d90ee6b-ed72-4ace-b667-42e35c5baa5a@linux.alibaba.com \
    --to=baolin.wang@linux.alibaba.com \
    --cc=akpm@linux-foundation.org \
    --cc=david@kernel.org \
    --cc=hughd@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=samridh@modal.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.