mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yuan-Hao Hsu <aa9736195201@gmail.com>
To: Rik van Riel <riel@surriel.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	David Hildenbrand <david@kernel.org>,
	Jason Gunthorpe <jgg@ziepe.ca>,
	John Hubbard <jhubbard@nvidia.com>, Peter Xu <peterx@redhat.com>,
	Lorenzo Stoakes <ljs@kernel.org>,
	liam@infradead.org, Vlastimil Babka <vbabka@kernel.org>,
	Mike Rapoport <rppt@kernel.org>,
	Suren Baghdasaryan <surenb@google.com>,
	Michal Hocko <mhocko@suse.com>, Aristeu Rozanski <aris@ruivo.org>,
	Ryan Roberts <ryan.roberts@arm.com>, Dev Jain <dev.jain@arm.com>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] mm/gup: batch PTE-mapped large folios in gup_fast_pte_range()
Date: Sun, 11 Oct 2026 03:52:02 +0800	[thread overview]
Message-ID: <20261010195202.689-1-aa9736195201@gmail.com> (raw)
In-Reply-To: <7dd4ec994502233cdc09fe5c4369adc89168d738.camel@surriel.com>

On Wed, 07 Oct 2026 06:47:06 -0400, Rik van Riel wrote:
> Since this is lockless, the PTEs could change under
> us between when we walk them in folio_pte_batch_flags()
> and the batched try_grab_folio().
>
> Is that race condition safe, or do we need to re-check
> afterward that:
> 1) the ptes still point at the same folio, and/or
> 2) the folio still spans the entire range, and was
>    not split before we grabbed the refcount?

It is safe, because the batch only runs after the first page of the
run has gone through the unchanged per-page path: the folio reference
is already held (and for FOLL_PIN the _pincount already raised, with
the smp_mb__after_atomic() in try_grab_folio_fast()) before any of
the other ptes is read.

2) With that reference held the folio cannot be split: both
__folio_split() and folio_split_unmapped() have to folio_ref_freeze()
the folio to the expected count and back off with -EAGAIN otherwise.
So folio_nr_pages() and page + i are stable for the scan.

1) The per-page path re-checks the pte because it has to pin first
and look at the pte second ("(1) pin, then (2) check pte" in the
comment above gup_fast_pte_range()).  For the rest of the run that is
already the order: each pte is read while the folio is held, so
seeing it map page + i is the same state the per-page path is in
right after its re-check.  A pte changing after that is the race
GUP-fast accepts anyway: the other side clears the pte and then
checks for a pin or an extra reference, both of which are per folio,
so it sees ours.  try_grab_folio() only adds references to a folio we
already hold and does not look at the ptes, so re-reading them after
it would not tell us anything new.

The pmd re-check after the grab is the batch's counterpart of the
per-page pmd check for THP collapse (pmdp_collapse_flush() detaching
the page table while it stays readable with interrupts off), same as
in gup_fast_pmd_leaf(): if the pmd changed, the extra references are
dropped and the next iteration bails out to the slow path.  The other
early returns in gup_fast_pte_batch() happen before any extra
reference is taken, so every bail-out leaves only the head page's
reference.

fork (write_protect_seq around the whole walk) and the read-only
FOLL_PIN check (gup_must_unshare() per page, after the pte read) are
unchanged from the per-page path.

Yuan-Hao Hsu

      reply	other threads:[~2026-10-10 19:52 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-17  8:37 Yuan-Hao Hsu
2026-09-17  8:42 ` David Hildenbrand (Arm)
2026-09-17  8:43   ` David Hildenbrand (Arm)
2026-09-17  9:01     ` Yuan-Hao Hsu
2026-10-07 10:07   ` Rik van Riel
2026-10-07 10:14     ` David Hildenbrand (Arm)
2026-10-07 10:47 ` Rik van Riel
2026-10-10 19:52   ` Yuan-Hao Hsu [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=20261010195202.689-1-aa9736195201@gmail.com \
    --to=aa9736195201@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=aris@ruivo.org \
    --cc=david@kernel.org \
    --cc=dev.jain@arm.com \
    --cc=jgg@ziepe.ca \
    --cc=jhubbard@nvidia.com \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=mhocko@suse.com \
    --cc=peterx@redhat.com \
    --cc=riel@surriel.com \
    --cc=rppt@kernel.org \
    --cc=ryan.roberts@arm.com \
    --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®