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 8/9] mm/gup: batch contiguous same-folio PTEs into one refcount grab
Date: Mon, 23 Nov 2026 14:56:08 -0500 [thread overview]
Message-ID: <20261123195609.4115244-9-riel@surriel.com> (raw)
In-Reply-To: <20261123195609.4115244-1-riel@surriel.com>
follow_page_pte() now walks a whole page table in one call, but still
resolves and commits each PTE on its own, so a PTE-mapped large folio
(mTHP) pays one try_grab_folio() per subpage.
Add gup_pte_batch_len() and give gup_grab_folio_pages() a run
length. Each run starts with a full per-PTE resolve, then
gup_pte_batch_len() finds how many more PTEs extend it with a pte_same()
scan rather than a re-derivation of each page, and the run is committed
with one refcount grab. Runs stop at folio boundaries, so plain base
pages are unaffected.
Gather the dirty bits from all the PTEs in a batch, in order to mark
the folio dirty for FOLL_WRITE.
Same benchmark as the previous change, with before again taken at the
base of the series:
gup_test -L -m 256 -n 65536 -r 16 -t
before after
64 kB mTHP 2946 us 231 us (12.8x)
4 kB base (control) 2753 us 1198 us (2.3x)
2 MB THP (control) 72 us 71 us
The 4 kB column is the previous change's 2.3x, unchanged: base pages
share no folio, so there is nothing to batch. 64 kB mTHP goes from that
change's 2.2x to 12.8x, a further 5.9x from committing a run with one
refcount grab instead of one per subpage.
With FOLL_TOUCH, folio_mark_accessed() runs once per run instead of
once per page: 512 calls before and 32 after over 2 MB of 64 kB
folios, leaving them referenced but inactive after the first call.
That matches the huge-page and fast paths, which mark once
per folio. (Measured by Yuan-Hao Hsu.)
Suggested-by: David Hildenbrand <david@kernel.org>
Signed-off-by: Rik van Riel <riel@surriel.com>
---
mm/gup.c | 95 ++++++++++++++++++++++++++++++++++++++++++++------------
1 file changed, 76 insertions(+), 19 deletions(-)
diff --git a/mm/gup.c b/mm/gup.c
index b5747f2c2ca3..886a413b8233 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -1011,6 +1011,46 @@ static long gup_lookup_pte(struct vm_area_struct *vma, unsigned long address,
return 0;
}
+/*
+ * 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.
+ *
+ * @fpb_flags selects the scan: slow GUP merges young/dirty so
+ * folio_mark_dirty() sees any dirty PTE in the run; fast GUP scans
+ * locklessly and revalidates the run after grabbing it. @vma is only
+ * needed for FPB_MERGE_WRITE and may be NULL otherwise.
+ */
+static unsigned long gup_pte_batch_len(struct vm_area_struct *vma,
+ unsigned long address, unsigned long walk_end,
+ struct folio *folio, pte_t *ptep, pte_t *batch_pte,
+ unsigned int flags, fpb_t fpb_flags)
+{
+ unsigned long max, nr = 1;
+
+ if (folio_test_large(folio)) {
+ max = (walk_end - address) >> PAGE_SHIFT;
+ if (max > 1)
+ nr = folio_pte_batch_flags(folio, vma, ptep,
+ batch_pte, max, fpb_flags);
+ }
+
+ /*
+ * Read-only runs are trimmed to the leading pages that need no
+ * unsharing; a zero return means even the head page needs
+ * unsharing, and the caller must fault it in instead. Writable
+ * runs need no repeat: a writable anon page is exclusive.
+ */
+ if (!pte_write(*batch_pte))
+ nr = gup_pages_not_requiring_unshare(vma, flags, folio,
+ pte_page(*batch_pte), nr);
+ return nr;
+}
+
/*
* Walk the PTEs from the start address to the end of this page table or VMA,
* whichever comes first, and commit every page found.
@@ -1046,7 +1086,10 @@ static long follow_page_pte(struct vm_area_struct *vma,
for (; address < walk_end; address += PAGE_SIZE, ptep++) {
pte_t pte = ptep_get(ptep);
+ struct folio *folio;
struct page *page;
+ unsigned long batch;
+ pte_t batch_pte;
/*
* no_page_table() may access the page cache for a
@@ -1061,12 +1104,33 @@ static long follow_page_pte(struct vm_area_struct *vma,
ret = gup_lookup_pte(vma, address, ptep, pte, flags, &page);
if (ret || !page)
break;
- ret = gup_grab_fill_folio_pages(page_folio(page), page, 1,
- pte_dirty(pte), flags,
+ folio = page_folio(page);
+ batch_pte = pte;
+
+ batch = gup_pte_batch_len(vma, address, walk_end, folio,
+ ptep, &batch_pte, flags,
+ FPB_RESPECT_WRITE |
+ FPB_MERGE_YOUNG_DIRTY);
+ /*
+ * Break out if the helper says the pages are not
+ * usable as-is.
+ */
+ if (!batch) {
+ ret = 0;
+ break;
+ }
+ ret = gup_grab_fill_folio_pages(folio, page, batch,
+ pte_dirty(batch_pte), flags,
pages ? pages + nr : NULL);
if (ret)
break;
- nr++;
+ nr += batch;
+ /*
+ * The loop's own increment covers one PTE; skip
+ * the rest of the batch.
+ */
+ ptep += batch - 1;
+ address += (batch - 1) * PAGE_SIZE;
}
pte_unmap_unlock(orig_ptep, ptl);
@@ -3021,8 +3085,6 @@ static unsigned long gup_fast_pte_range(pmd_t pmd, pmd_t *pmdp,
pte_t pte = ptep_get_lockless(ptep);
struct page *page;
- nr_batch = 1;
-
/*
* Always fallback to ordinary GUP on PROT_NONE-mapped pages:
* pte_access_permitted() better should reject these pages
@@ -3062,20 +3124,15 @@ static unsigned long gup_fast_pte_range(pmd_t pmd, pmd_t *pmdp,
if (!gup_fast_folio_allowed(folio, flags))
goto out;
- if (folio_test_large(folio))
- /*
- * The reference acquired above keeps the folio stable
- * for the following checks.
- */
- nr_batch = folio_pte_batch_flags(folio, NULL, ptep, &pte,
- (end - addr) >> PAGE_SHIFT,
- FPB_LOCKLESS | FPB_RESPECT_WRITE);
-
- if (!pte_write(pte)) {
- nr_batch = gup_pages_not_requiring_unshare(NULL, flags, folio, page, nr_batch);
- if (!nr_batch)
- goto out;
- }
+ /*
+ * The reference acquired above keeps the folio stable
+ * for the following checks.
+ */
+ nr_batch = gup_pte_batch_len(NULL, addr, end, folio,
+ ptep, &pte, flags,
+ FPB_LOCKLESS | FPB_RESPECT_WRITE);
+ if (!nr_batch)
+ goto out;
/*
* We need to make the page accessible if and only if we are
--
2.56.0
next 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 ` Rik van Riel [this message]
2026-11-23 19:56 ` [PATCH 9/9] mm/gup: unshare-check every page of a pte batch Rik van Riel
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-9-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