Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: Samridh <samridh@modal.com>, "Hugh Dickins" <hughd@google.com>,
	"David Hildenbrand" <david@kernel.org>,
	"Baolin Wang" <baolin.wang@linux.alibaba.com>,
	"Andrew Morton" <akpm@linux-foundation.org>
Cc: linux-mm@kvack.org
Subject: Re: [PATCH] mm/shmem: avoid zeroing new folios twice with init_on_alloc=1
Date: Wed, 18 Nov 2026 19:32:38 +0000	[thread overview]
Message-ID: <sashiko-outbox-221869@kernel.org> (raw)
In-Reply-To: <20261117-samridh-shmem-send-v1-1-1b897ff562d9@modal.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] mm/shmem: single-word Signed-off-by identity in commit message
- [High] mm/shmem: cross-CPU dcache flush after __GFP_ZERO in
  shmem_get_folio_gfp()
--

commit acaf023dbfc00405b46eac6010b09d293eda6ffd
Author: Samridh <samridh@modal.com>

> mm/shmem: avoid zeroing new folios twice with init_on_alloc=1
>
> 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>

[Severity: High]
Documentation/process/submitting-patches.rst requires a full real name in
the Author header and Signed-off-by: tag rather than a single-word name or
handle.

> diff --git a/mm/shmem.c b/mm/shmem.c
> index 5cb628cc9c6bb..d2c6781e86a55 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) {
> +			for (i = 0; i < n; i++)
> +				clear_highpage(folio_page(folio, i));
> +		}
>  		flush_dcache_folio(folio);
>  		folio_mark_uptodate(folio);
>  	}

[Severity: High]
Can skipping clear_highpage() here cause flush_dcache_folio(folio) to run
on a different CPU from the one that zeroed the folio?

flush_dcache_folio() requires running on the CPU that performed the kernel
stores into the folio.  On sparc64 and on 32-bit SMP ARM (when
cache_ops_need_broadcast() is true), flush_dcache_folio() only flushes or
records the local CPU's D-cache:

arch/sparc/mm/init_64.c:flush_dcache_folio() {
	...
	this_cpu = get_cpu();

	mapping = folio_flush_mapping(folio);
	if (mapping && !mapping_mapped(mapping)) {
		bool dirty = test_bit(PG_dcache_dirty, &folio->flags.f);
		if (dirty) {
			int dirty_cpu = dcache_dirty_cpu(folio);

			if (dirty_cpu == this_cpu)
				goto out;
			smp_flush_dcache_folio_impl(folio, dirty_cpu);
		}
		set_dcache_dirty(folio, this_cpu);
	} else {
		flush_dcache_folio_impl(folio);
	}
	...
}

With __GFP_ZERO, the zeroing stores can happen on another CPU before
flush_dcache_folio(folio) is reached:

CPU0                              CPU1
shmem_get_folio_gfp()
  shmem_alloc_and_add_folio()
    shmem_alloc_folio(..., __GFP_ZERO)
      post_alloc_hook()
        kernel_init_pages()
    mem_cgroup_charge() (sleeps, task migrates)
                                  ...
                                  flush_dcache_folio(folio)
                                    /* flushes/records CPU1, leaving
                                     * zeroed lines dirty in CPU0's L1 */

In addition, when init_on_free=1, post_alloc_hook() skips zeroing on
allocation because the page was zeroed when freed in
__free_pages_prepare(), which may have happened on another CPU without a
D-cache flush.  In either case, a userspace mapping at an aliasing virtual
address can read stale non-zero data from memory instead of the zeroed
L1 cache lines.

Anonymous folio allocations avoid this by checking
user_alloc_needs_zeroing(), which keeps explicit post-allocation zeroing
when cpu_dcache_is_aliasing() || cpu_icache_is_aliasing() is true.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-samridh-shmem-send-v1-1-1b897ff562d9@modal.com?part=1


      parent reply	other threads:[~2026-11-18 19:32 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 via B4 Relay
2026-11-18  3:29 ` Baolin Wang
2026-11-18  4:59   ` Fnu Samridh
2026-11-18 19:32 ` sashiko-bot [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=sashiko-outbox-221869@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=david@kernel.org \
    --cc=hughd@google.com \
    --cc=linux-mm@kvack.org \
    --cc=samridh@modal.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /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