From: Rik van Riel <riel@surriel.com>
To: linux-kernel@vger.kernel.org
Cc: linux-mm@kvack.org, David Hildenbrand <david@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
Suren Baghdasaryan <surenb@google.com>,
Lorenzo Stoakes <ljs@kernel.org>,
Christoph Hellwig <hch@infradead.org>,
Yuan-Hao Hsu <aa9736195201@gmail.com>,
John Hubbard <jhubbard@nvidia.com>,
Jason Gunthorpe <jgg@ziepe.ca>, Peter Xu <peterx@redhat.com>,
Rik van Riel <riel@surriel.com>
Subject: [PATCH 9/9] mm/gup: unshare-check every page of a pte batch
Date: Mon, 23 Nov 2026 14:56:09 -0500 [thread overview]
Message-ID: <20261123195609.4115244-10-riel@surriel.com> (raw)
In-Reply-To: <20261123195609.4115244-1-riel@surriel.com>
A slow GUP write pin over a PTE batch can hand back pages that a later
write fault replaces under the pin holder: the process writes to
the copy, the holder keeps the old page:
pin holder process (mm owner)
pin all 16 for write
batch passes unchecked
write to a page
fault copies it elsewhere
read pinned page -> stale
With GUP-fast, FOLL_WRITE | FOLL_PIN can never reach
gup_pages_not_requiring_unshare, but slow GUP can reach it
with a read-only head PTE.
Only pure reads skip the per-page check now; anything else is
vetted page by page, so GUP pins the pages the process owns
exclusively and faults in the rest one by one.
Without this fix, the pin holder can see stale page content,
with it the pin and program address map the same page.
Tested by faulting a 64K mTHP, shaping it (fork+exit, head
write, mprotect read-only), then pinning all 16 with
FOLL_FORCE|FOLL_WRITE: before, the pin takes the whole batch
with no faults but the program's writes can COW the pages
later; with the fix, the pin unshares every page first
and the writes land in place.
---
mm/gup.c | 18 ++++++++++--------
1 file changed, 10 insertions(+), 8 deletions(-)
diff --git a/mm/gup.c b/mm/gup.c
index 886a413b8233..ec39e489e131 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -640,11 +640,12 @@ static unsigned long gup_pages_not_requiring_unshare(
unsigned int i;
/*
- * FOLL_WRITE is implicitly handled correctly as the page table entry
- * has to be writable -- and if it references (part of) an anonymous
- * folio, that part is required to be marked exclusive.
+ * A read-only PTE can map a shared anonymous page. Pinning
+ * one without unsharing it first leaves the pin holder behind on
+ * the old page once a later write fault copies it elsewhere.
+ * Only short term read accesses can skip the per-page check below.
*/
- if ((flags & (FOLL_WRITE | FOLL_PIN)) != FOLL_PIN)
+ if (!(flags & (FOLL_WRITE | FOLL_PIN)))
return nr_pages;
/* folio_test_anon(folio) is stable until folio gets freed. */
@@ -1015,10 +1016,11 @@ static long gup_lookup_pte(struct vm_area_struct *vma, unsigned long address,
* Return how many PTEs map consecutive pages of the same folio and can be
* committed as one run. Always at least 1.
*
- * The write-fault and unshare checks in gup_lookup_pte() are per PTE, but a
- * writable run needs no repeat: a writable anon page is exclusive. A
- * read-only run under FOLL_WRITE or FOLL_PIN does need the per-page check,
- * so it stays one page at a time.
+ * The scan covers PTE bits only; a read-only run is then trimmed to the
+ * leading pages that need no unsharing, so a mixed-exclusivity batch
+ * commits its exclusive prefix and the fault path unshares each
+ * remaining follower in turn. A zero return means even the head page
+ * needs unsharing, and the caller must fault it in instead.
*
* @fpb_flags selects the scan: slow GUP merges young/dirty so
* folio_mark_dirty() sees any dirty PTE in the run; fast GUP scans
--
2.56.0
prev parent reply other threads:[~2026-11-23 19:56 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 19:56 [PATCH v6 0/9] mm/gup: walk and batch whole PTE tables in slow GUP Rik van Riel
2026-11-23 19:56 ` [PATCH 1/9] mm/gup: break out gup_fill_pages() helper Rik van Riel
2026-11-23 19:56 ` [PATCH 2/9] mm/gup: break out gup_flush_pages() helper Rik van Riel
2026-11-23 19:56 ` [PATCH 3/9] mm/gup: convert follow_page_mask() to return a long Rik van Riel
2026-11-23 19:56 ` [PATCH 4/9] mm/gup: split out folio grab-and-store helper Rik van Riel
2026-11-23 19:56 ` [PATCH 5/9] mm/gup: break out gup_lookup_pte() helper Rik van Riel
2026-11-23 19:56 ` [PATCH 6/9] mm/gup: return a huge page's full count from follow_page_mask() Rik van Riel
2026-11-23 19:56 ` [PATCH 7/9] mm/gup: walk multiple PTEs per follow_page_pte() call Rik van Riel
2026-11-23 19:56 ` [PATCH 8/9] mm/gup: batch contiguous same-folio PTEs into one refcount grab Rik van Riel
2026-11-23 19:56 ` Rik van Riel [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=20261123195609.4115244-10-riel@surriel.com \
--to=riel@surriel.com \
--cc=aa9736195201@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=david@kernel.org \
--cc=hch@infradead.org \
--cc=jgg@ziepe.ca \
--cc=jhubbard@nvidia.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=peterx@redhat.com \
--cc=surenb@google.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®