Skip to content

mm/gup: walk and batch whole PTE tables in slow GUP

Rik van Riel linux-mm lkml 4 versions – New

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v6 Latest9 patches 0 repliesNew
  2. v5 posting 2/28 patches 6 repliesSuperseded
  3. v5 RFC posting 1/28 patches 11 repliesSuperseded
Earlier versions · 1
VersionPostedPatchesRepliesStatus
v3 RFC8 patches28 repliesSuperseded

Changes in v6 from the cover letter

- Moved some dead-in-patch-5 code to patch 7 (Lorenzo)
- Clean up some comments and variable names (Lorenzo)
- A few more comment cleanups elsewhere in the series
- Changes gup_pages_not_requiring_unshare() so slow GUP FOLL_WRITE writes to shared anonymous pages don't get lost.
- Clamp the follow_page_pte() walk to the page table end without using pmd_addr_end(), which is a noop with 2 level page tables.

Patches in v6 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/9mm/gup: break out gup_fill_pages() helpermm/gup.c+25 -100 replies
2/9mm/gup: break out gup_flush_pages() helpermm/gup.c+19 -80 replies
3/9mm/gup: convert follow_page_mask() to return a longmm/gup.c+141 -1170 replies
4/9mm/gup: split out folio grab-and-store helpermm/gup.c+47 -290 replies
5/9mm/gup: break out gup_lookup_pte() helpermm/gup.c+58 -440 replies
6/9mm/gup: return a huge page's full count from follow_page_mask()mm/gup.c+57 -960 replies
7/9mm/gup: walk multiple PTEs per follow_page_pte() callmm/gup.c+61 -340 replies
8/9mm/gup: batch contiguous same-folio PTEs into one refcount grabmm/gup.c+76 -190 replies
9/9mm/gup: unshare-check every page of a pte batchmm/gup.c+10 -80 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v6 yet.

Read the v6 thread

Cover letter [PATCH v6 0/9] mm/gup: walk and batch whole PTE tables in slow GUP

Slow GUP resolves and commits one PTE per follow_page_pte() call, so __get_user_pages() restarts the pgd/p4d/pud/pmd descent and retakes the PTE lock for every page, and a PTE-mapped large folio (mTHP) pays one try_grab_folio() per subpage. The fast path already batches PTE-mapped large folios; the slow path does not.

This series brings the slow path to the same shape in reviewable
steps: three no-functional-change refactors (store, flush, return type), two splits of follow_page_pte(), huge-page count reporting, the multi-PTE walk, the same-folio batch commit, and the unshare check every batched write run needs. The
batch-length helper is shared with GUP-fast through an fpb_flags parameter selecting the scan.

Patches:
1. break out gup_fill_pages() helper
2. break out gup_flush_pages() helper
3. convert follow_page_mask() to return a long
4. split out folio grab-and-store helper
5. break out gup_lookup_pte() helper
6. return a huge page's full count from follow_page_mask()
7. walk multiple PTEs per follow_page_pte() call
8. batch contiguous same-folio PTEs into one refcount grab
9. unshare-check every page of a pte batch

Measured with tools/mm/gup_bench.c (PIN_LONGTERM_BENCHMARK), median get time over 16 iterations on a 256 MB MADV_HUGEPAGE region in a 4 CPU VM, folio sizes confirmed through the anon_fault_alloc counters (4100 folios at 64 kB, 128 at 2 MB):

gup_bench -L -m 256 -n 65536 -r 16 -t
before after
4 kB base pages 2753 us 1178 us (2.3x, patch 7)
64 kB mTHP 2946 us 231 us (12.8x, patch 8)
2 MB THP (control) 72 us 71 us

Patch 7's gain is the page-table descent that no longer restarts per page; patch 8 adds a further 5.9x on mTHP by committing each run with one refcount grab. Base pages share no folio and are unaffected by patch 8; 2 MB THP never reaches follow_page_pte(). With FOLL_TOUCH, folio_mark_accessed() drops from 512 calls to 32 over 2 MB of 64 kB folios (measured by Yuan-Hao Hsu), matching the huge-page and fast paths, which mark once per folio.

This is based on 102d6c3d5892 (merge of "mm: gup: batch PTE-mapped large folios in gup_fast_pte_range()" from the gup-7.5-ptebatch branch of mm-core.git)

 gup.c |  589 ++++++++++++++++++++++++++++++++++++++++--------------------------
 1 file changed, 359 insertions(+), 230 deletions(-)

Changelog
---------
v5 -> v6:
- Moved some dead-in-patch-5 code to patch 7 (Lorenzo)
- Clean up some comments and variable names (Lorenzo)
- A few more comment cleanups elsewhere in the series
- Changes gup_pages_not_requiring_unshare() so slow GUP FOLL_WRITE writes to shared anonymous pages don't get lost.
- Clamp the follow_page_pte() walk to the page table end without using pmd_addr_end(), which is a noop with 2 level page tables.
v4 -> v5:
- Rebased onto 102d6c3d5892 (landed GUP-fast batching and the folio-based unshare helpers); adapted to the store-only gup_fill_pages() and gup_must_unshare(vma, flags, folio, page).
- Split the store/flush extraction in two: new patch 2 breaks out gup_flush_pages()
- Dropped the "fill the pages array outside the pud/pmd lock" patch. Separating of the fill and flush helpers made it possible to just keep the (fast) fills inline, while keeping the (slow) flush out of the loop, at the tail end of __get_user_pages()
- Rewrote patch 8 around a shared gup_pte_batch_len() with an fpb_flags scan selector; GUP-fast uses the same helper instead of its inline scan and trim. (David Hildenbrand)
- Read-only runs are trimmed to the pages needing no unsharing instead of refused; the trim also covers runs of one page, which the fast path relies on as its per-page unshare gate.
- Renamed gup_grab_folio_pages() to gup_grab_fill_folio_pages().
- Re-measured the benchmarks.