mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Garg, Shivank" <shivankg@amd.com>
To: "david@kernel.org" <david@kernel.org>,
	"nilayvaish@google.com" <nilayvaish@google.com>,
	"akpm@linux-foundation.org" <akpm@linux-foundation.org>
Cc: "luizcap@redhat.com" <luizcap@redhat.com>,
	"mhocko@suse.com" <mhocko@suse.com>,
	"linux-mm@kvack.org" <linux-mm@kvack.org>,
	"surenb@google.com" <surenb@google.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"rppt@kernel.org" <rppt@kernel.org>,
	"vbabka@kernel.org" <vbabka@kernel.org>,
	"liam@infradead.org" <liam@infradead.org>
Subject: Re: [PATCH] mm/util: fix off-by-one in snapshot_page() retry check
Date: Mon, 23 Nov 2026 04:44:51 +0000	[thread overview]
Message-ID: <00417754d72202c9032b793d40af7486d00a9979.camel@amd.com> (raw)
In-Reply-To: <20261123041200.3239570-1-nilayvaish@google.com>

On Sun, 2026-11-22 at 20:12 -0800, Nilay Vaish wrote:
> [You don't often get email from nilayvaish@google.com. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
> 
> snapshot_page() computes ps->idx, the index of @page within the folio
> it appeared to belong to, and nr_pages, the size of that folio as read
> from a copy of its head page. Because the folio can be split
> concurrently, the two can disagree, in which case the snapshot is
> retried and finally marked as unfaithful.
> 
> Valid indices are 0 to nr_pages - 1, but the check only catches
> ps->idx > nr_pages. If the folio is split such that ps->idx equals
> nr_pages, @page is no longer part of the folio that was copied, yet the
> snapshot is returned as faithful with an out-of-range index.
> 
> Use >= so that this case is retried as well.
> 
> The race can be hit from userspace. On a kernel with a debug-only check
> for this condition, one thread repeatedly faulted in a PMD-sized THP and
> split it to order 0 through the split_huge_pages debugfs file, while
> three threads read the /proc/kpageflags entry of the THP's second page.
> In 90 seconds (about 181,000 splits), 14 snapshots of that page were
> returned as faithful with idx 1 and nr_pages 1. With this change, a
> second 90-second run (about 138,000 splits) hit the idx == nr_pages case
> 41 times; each one was retried, and no snapshot was returned with an
> out-of-range index.

Hi Nilay,

This one is already posted [1] and applied in mm tree.

> 
> This issue was found by an AI coding assistant (Gemini) while reviewing
> snapshot_page(). The fix and the reproducer were written with its help.
> 

As a tip for AI-assisted reviews: semcode [2] has an MCP server with
lore search, which makes it easy to check whether a fix has already
been posted before sending. It has saved me from duplicates a few
times.

[1] https://lore.kernel.org/all/20261116-snapshot_page_fix-v1-1-8e425984e8d1@kernel.org
[2] https://github.com/facebookexperimental/semcode

Thanks,
Shivank


> Fixes: d863a12108f2 ("mm/util: introduce snapshot_page()")
> Signed-off-by: Nilay Vaish <nilayvaish@google.com>
> ---
>  mm/util.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/mm/util.c b/mm/util.c
> index 80f6c714844f..f2c23e4551a8 100644
> --- a/mm/util.c
> +++ b/mm/util.c
> @@ -1444,7 +1444,7 @@ void snapshot_page(struct page_snapshot *ps, const struct page *page)
>                 set_ps_flags(ps, foliop, page);
>         }
> 
> -       if (ps->idx > nr_pages) {
> +       if (ps->idx >= nr_pages) {
>                 if (loops-- > 0)
>                         goto again;
>                 clear_compound_head(&ps->page_snapshot);
> --
> 2.56.1.750.gf70dc682cf-goog



      reply	other threads:[~2026-11-23  4:44 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23  4:12 Nilay Vaish
2026-11-23  4:44 ` Garg, Shivank [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=00417754d72202c9032b793d40af7486d00a9979.camel@amd.com \
    --to=shivankg@amd.com \
    --cc=akpm@linux-foundation.org \
    --cc=david@kernel.org \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=luizcap@redhat.com \
    --cc=mhocko@suse.com \
    --cc=nilayvaish@google.com \
    --cc=rppt@kernel.org \
    --cc=surenb@google.com \
    --cc=vbabka@kernel.org \
    /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®