mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "David Hildenbrand (Arm)" <david@kernel.org>
To: samridh@modal.com, Hugh Dickins <hughd@google.com>,
	Baolin Wang <baolin.wang@linux.alibaba.com>,
	Andrew Morton <akpm@linux-foundation.org>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] mm/shmem: avoid zeroing new folios twice with init_on_alloc=1
Date: Thu, 19 Nov 2026 18:06:03 +0100	[thread overview]
Message-ID: <e4c0fcf1-b70e-4d22-9c4a-5829e1438ce5@kernel.org> (raw)
In-Reply-To: <20261118-samridh-shmem-send-v2-1-f2ea01bfc178@modal.com>

On 11/18/26 21:28, 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.
> 
> Skip the clear for a folio that was just allocated when
> user_alloc_needs_zeroing() says it is not needed, like anonymous memory
> does. Folios that are already in the page cache but not uptodate, such
> as those left by fallocate(), are still cleared, and so is every folio
> on CPUs with cache aliasing.
> 
> 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.625s   0.420s
>   EPYC 9R14       0.216s   0.160s
> 
> With 4K folios the times change by 3% or less, and init_on_alloc=0 is
> not affected. 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. A simple test fills memory
> with data, frees it, and then checks that new memfd pages read as zero,
> with MADV_HUGEPAGE and MADV_NOHUGEPAGE on the fault and fallocate paths.
> It passes on both machines with init_on_alloc=1.
> ---
> Changes in v2:
> - Use user_alloc_needs_zeroing() instead of __GFP_ZERO (Baolin).
> - Link to v1: https://patch.msgid.link/20261117-samridh-shmem-send-v1-1-1b897ff562d9@modal.com
> ---
>  mm/shmem.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/mm/shmem.c b/mm/shmem.c
> index c769d0bc7df9..1a78d15a768e 100644
> --- a/mm/shmem.c
> +++ b/mm/shmem.c
> @@ -2810,8 +2810,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));
> +		/* init_on_alloc already zeroed a new folio. */
> +		if (!alloced || user_alloc_needs_zeroing()) {
> +			for (i = 0; i < n; i++)
> +				clear_highpage(folio_page(folio, i));
> +		}
>  		flush_dcache_folio(folio);

I do wonder whether that could go into the loop, because I faintly remember that
user_alloc_needs_zeroing() is disabled when CPUs have dcache aliasing.

In general LGTM, but I'll wait for more feedback from shmem maintainers.

-- 
Cheers,

David

      reply	other threads:[~2026-11-19 17:06 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 20:28 Samridh via B4 Relay
2026-11-19 17:06 ` David Hildenbrand (Arm) [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=e4c0fcf1-b70e-4d22-9c4a-5829e1438ce5@kernel.org \
    --to=david@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=baolin.wang@linux.alibaba.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®