Skip to contentSkip to conversation

mm/gup: batch PTE-mapped large folios in gup_fast_pte_range()

PATCH In review Yuan-Hao Hsu 4 participants 11 messages Sep 17 – Oct 27

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/gup: batch PTE-mapped large folios in gup_fast_pte_range()

GUP-fast grabs a PTE-mapped large folio one page at a time. Every PTE costs a try_grab_folio_fast() (a refcount cmpxchg, plus the pincount atomic, a full barrier and a node stat update for FOLL_PIN), a gup_fast_folio_allowed() and a folio_set_referenced(), so a 64 kB mTHP pays sixteen of each and a PTE-mapped 2 MB THP pays 512. The PMD and PUD leaf paths already take the whole range with one try_grab_folio_fast() call, and the slow path is getting the same treatment for PTEs in Rik's follow_page_mask() series.

The users are the hot ones: iov_iter_extract_user_pages() pins every O_DIRECT buffer through pin_user_pages_fast(), io_uring registers its buffers through it, and so does RDMA memory registration. With mTHP those buffers are PTE-mapped large folios.

After the head page has been grabbed and verified as before, extend the run to the following PTEs that map the next pages of the same folio with the same protection bits, take the remaining references for the run with try_grab_folio(), and fill pages[] from the run. The PTEs of the run are read while the reference on the folio is held, so the folio can neither be freed nor split underneath the scan; a THP collapse can still detach the page table, so the pmd is checked again after the run is grabbed and the run is dropped if it changed, leaving the next iteration to hit the existing pmd check and bail out. A read-only run of an anonymous folio stops at the first page that gup_must_unshare() rejects, so the per-page exclusivity guarantee for FOLL_PIN is kept.

folio_pte_batch_flags() does the scan. It reads the entries with ptep_get(), which is not safe without the page table lock on CONFIG_GUP_GET_PXX_LOW_HIGH (x86 PAE, mips32, sh) or with arm64 contpte, and GUP-fast reads every PTE with ptep_get_lockless() for that reason. Add FPB_LOCKLESS to make the helper do the same; it is inlined per call site, so the other callers do not change.

The order-0 path keeps its instruction sequence. The folio flags are tested before folio_set_referenced() on purpose: a load of the flags right after that locked instruction cost about 25% on order-0 pages in the gup_test benchmark below, and reading them first brings that path back to the baseline.

gup_test ioctl over a 256 MB anonymous region, FOLL_WRITE, 65536 pages per call, median of 15 runs, two baseline boots / one patched boot, on an x86-64 VM:

                       get_user_pages_fast()   pin_user_pages_fast()
                       before       after      before       after
  4 kB pages           849/789 us   783 us     1174/1138 us 1094 us
  64 kB mTHP           812/811 us   126 us     1215/1113 us  188 us
  1 MB mTHP            799/774 us    59 us     1185/1077 us   64 us
  2 MB THP, PTE-mapped 803/786 us    57 us     1159/1120 us   58 us

2 MB THP, PMD-mapped 42/40 us 40 us 46/41 us 42 us

fio O_DIRECT randread of a null_blk device (CPU bound) with the I/O buffer in 64 kB mTHP, median of 15 five-second runs:

  bs=1M psync                 126.2/125.6 GB/s  ->  220.6 GB/s
  bs=1M io_uring, iodepth 16   71.2/71.6 GB/s   ->  112.1 GB/s
  bs=64k io_uring, iodepth 16  50.2/50.7 GB/s   ->   60.1 GB/s

IORING_REGISTER_BUFFERS of 1 GB, median of 15:

  64 kB mTHP           6179/5998 us  ->  1599 us
  1 MB mTHP            5790/5801 us  ->   893 us
  4 kB pages           7173/6741 us  ->  6763 us
---
 mm/gup.c      | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++
 mm/internal.h | 15 +++++++++-
 2 files changed, 90 insertions(+), 1 deletion(-)
mm/gup.c +76 -0
index eb898ea1ee22..1b4bbb525609 100644
@@ -2807,6 +2807,64 @@ static bool gup_fast_folio_allowed(struct folio *folio, unsigned int flags)
 }
 
 #ifdef CONFIG_ARCH_HAS_PTE_SPECIAL
+/*
+ * Extend the run of pages grabbed by gup_fast_pte_range() from @page, mapped
+ * by the PTE at @ptep, to the following PTEs that map the next pages of
+ * @folio with the same protection.  The caller holds one reference for @page
+ * and has verified @pte against the page table; this takes the references
+ * for the rest of the run and stores its pages in @pages.  Returns the
+ * number of pages in the run, including @page.
+ */
+static unsigned int gup_fast_pte_batch(struct folio *folio,
+		struct page *page, pmd_t pmd, pmd_t *pmdp, pte_t *ptep,
+		pte_t pte, unsigned int flags, unsigned int max_nr,
+		struct page **pages)
+{
+	unsigned int nr, i;
+
+	if (max_nr == 1)
+		return 1;
+
+	/*
+	 * The reference on @folio keeps folio_nr_pages() stable.  The PTEs
+	 * are read without the PTL, hence FPB_LOCKLESS.
+	 */
+	nr = folio_pte_batch_flags(folio, NULL, ptep, &pte, max_nr,
+				   FPB_RESPECT_WRITE | FPB_LOCKLESS);
+
+	/*
+	 * gup_must_unshare() is per page: a read-only run of an anonymous
+	 * folio ends at the first page that is not exclusive.
+	 */
+	if (!pte_write(pte)) {
+		for (i = 1; i < nr; i++) {
+			if (gup_must_unshare(NULL, flags, page + i))
+				break;
+		}
+		nr = i;
+	}
+	if (nr == 1)
+		return 1;
+
+	if (try_grab_folio(folio, nr - 1, flags))
+		return 1;
+
+	/*
+	 * The PTEs were read after the reference on @folio was taken, so the
+	 * pages cannot have been freed, but the page table could have been
+	 * detached by a THP collapse meanwhile, leaving stale PTEs.  Drop the
+	 * run and let the next iteration hit the pmd check and bail out.
+	 */
+	if (unlikely(pmd_val(pmd) != pmd_val(pmdp_get_lockless(pmdp)))) {
+		gup_put_folio(folio, nr - 1, flags);
+		return 1;
+	}
+
+	for (i = 1; i < nr; i++)
+		*pages++ = page + i;
+	return nr;
+}
+
 /*
  * GUP-fast relies on pte change detection to avoid concurrent pgtable
  * operations.
@@ -2830,6 +2888,8 @@ static int gup_fast_pte_range(pmd_t pmd, pmd_t *pmdp, unsigned long addr,
 		unsigned long end, unsigned int flags, struct page **pages,
 		int *nr)
 {
+	unsigned int nr_batch;
+	bool large;
 	int ret = 0;
 	pte_t *ptep, *ptem;
 
@@ -2891,9 +2951,25 @@ static int gup_fast_pte_range(pmd_t pmd, pmd_t *pmdp, unsigned long addr,
 			gup_put_folio(folio, 1, flags);
 			goto pte_unmap;
 		}
+		/*
+		 * Read the folio flags before the atomic in
+		 * folio_set_referenced(); a load right after it has to wait for
+		 * it, which is measurable on the order-0 path.
+		 */
+		large = folio_test_large(folio);
 		folio_set_referenced(folio);
 		pages[*nr] = page;
 		(*nr)++;
+
+		if (likely(!large))
+			continue;
+
+		nr_batch = gup_fast_pte_batch(folio, page, pmd, pmdp, ptep, pte,
+					      flags, (end - addr) >> PAGE_SHIFT,
+					      pages + *nr);
+		*nr += nr_batch - 1;
+		ptep += nr_batch - 1;
+		addr += (nr_batch - 1) * PAGE_SIZE;
 	} while (ptep++, addr += PAGE_SIZE, addr != end);
 
 	ret = 1;
mm/internal.h +14 -1
index 38b1165212c9..783754a81960 100644
@@ -368,6 +368,12 @@ typedef int __bitwise fpb_t;
  */
 #define FPB_MERGE_YOUNG_DIRTY		((__force fpb_t)BIT(4))
 
+/*
+ * Read the page table entries with ptep_get_lockless(): the caller does not
+ * hold the page table lock (GUP-fast).
+ */
+#define FPB_LOCKLESS			((__force fpb_t)BIT(5))
+
 static inline pte_t __pte_batch_clear_ignored(pte_t pte, fpb_t flags)
 {
 	if (!(flags & FPB_RESPECT_DIRTY))
@@ -400,6 +406,10 @@ static inline pte_t __pte_batch_clear_ignored(pte_t pte, fpb_t flags)
  * must be limited by the caller so scanning cannot exceed a single VMA and
  * a single page table.
  *
+ * The caller must hold the page table lock, unless FPB_LOCKLESS is set: then
+ * the entries are read with ptep_get_lockless() and the caller has to make
+ * sure the folio cannot be freed or split, as GUP-fast does.
+ *
  * Depending on the FPB_MERGE_* flags, the pte stored at @ptentp will
  * be updated: it's crucial that a pointer to a COPY of the first
  * page table entry, obtained through ptep_get(), is provided as @ptentp.
@@ -436,7 +446,10 @@ static inline unsigned int folio_pte_batch_flags(struct folio *folio,
 	ptep = ptep + nr;
 
 	while (nr < max_nr) {
-		pte = ptep_get(ptep);
+		if (flags & FPB_LOCKLESS)
+			pte = ptep_get_lockless(ptep);
+		else
+			pte = ptep_get(ptep);
 
 		if (!pte_same(__pte_batch_clear_ignored(pte, flags), expected_pte))
 			break;

base-commit: 9b87fdc9af2fbfcdb5c24a64139685ef80f6573f

Signature · 1 line
2.43.0
#1 lore raw
Quoted 58 lines · Yuan-Hao Hsu

GUP-fast grabs a PTE-mapped large folio one page at a time. Every PTE costs a try_grab_folio_fast() (a refcount cmpxchg, plus the pincount atomic, a full barrier and a node stat update for FOLL_PIN), a gup_fast_folio_allowed() and a folio_set_referenced(), so a 64 kB mTHP pays sixteen of each and a PTE-mapped 2 MB THP pays 512. The PMD and PUD leaf paths already take the whole range with one try_grab_folio_fast() call, and the slow path is getting the same treatment for PTEs in Rik's follow_page_mask() series.

The users are the hot ones: iov_iter_extract_user_pages() pins every O_DIRECT buffer through pin_user_pages_fast(), io_uring registers its buffers through it, and so does RDMA memory registration. With mTHP those buffers are PTE-mapped large folios.

After the head page has been grabbed and verified as before, extend the run to the following PTEs that map the next pages of the same folio with the same protection bits, take the remaining references for the run with try_grab_folio(), and fill pages[] from the run. The PTEs of the run are read while the reference on the folio is held, so the folio can neither be freed nor split underneath the scan; a THP collapse can still detach the page table, so the pmd is checked again after the run is grabbed and the run is dropped if it changed, leaving the next iteration to hit the existing pmd check and bail out. A read-only run of an anonymous folio stops at the first page that gup_must_unshare() rejects, so the per-page exclusivity guarantee for FOLL_PIN is kept.

folio_pte_batch_flags() does the scan. It reads the entries with ptep_get(), which is not safe without the page table lock on CONFIG_GUP_GET_PXX_LOW_HIGH (x86 PAE, mips32, sh) or with arm64 contpte, and GUP-fast reads every PTE with ptep_get_lockless() for that reason. Add FPB_LOCKLESS to make the helper do the same; it is inlined per call site, so the other callers do not change.

The order-0 path keeps its instruction sequence. The folio flags are tested before folio_set_referenced() on purpose: a load of the flags right after that locked instruction cost about 25% on order-0 pages in the gup_test benchmark below, and reading them first brings that path back to the baseline.

gup_test ioctl over a 256 MB anonymous region, FOLL_WRITE, 65536 pages per call, median of 15 runs, two baseline boots / one patched boot, on an x86-64 VM:

                       get_user_pages_fast()   pin_user_pages_fast()
                       before       after      before       after
  4 kB pages           849/789 us   783 us     1174/1138 us 1094 us
  64 kB mTHP           812/811 us   126 us     1215/1113 us  188 us
  1 MB mTHP            799/774 us    59 us     1185/1077 us   64 us
  2 MB THP, PTE-mapped 803/786 us    57 us     1159/1120 us   58 us

2 MB THP, PMD-mapped 42/40 us 40 us 46/41 us 42 us

fio O_DIRECT randread of a null_blk device (CPU bound) with the I/O buffer in 64 kB mTHP, median of 15 five-second runs:

  bs=1M psync                 126.2/125.6 GB/s  ->  220.6 GB/s
  bs=1M io_uring, iodepth 16   71.2/71.6 GB/s   ->  112.1 GB/s
  bs=64k io_uring, iodepth 16  50.2/50.7 GB/s   ->   60.1 GB/s
Quoted 6 lines

IORING_REGISTER_BUFFERS of 1 GB, median of 15:

  64 kB mTHP           6179/5998 us  ->  1599 us
  1 MB mTHP            5790/5801 us  ->   893 us
  4 kB pages           7173/6741 us  ->  6763 us

I think Rik was already working on this and sent some patches. Anyhow, there is quite some GUP review backlog I have t go through, so this will have to wait.

Signature · 3 lines
Cheers,

David
#2 lore raw
Quoted 61 lines · David Hildenbrand (Arm)
Quoted 60 lines · Yuan-Hao Hsu

GUP-fast grabs a PTE-mapped large folio one page at a time. Every PTE costs a try_grab_folio_fast() (a refcount cmpxchg, plus the pincount atomic, a full barrier and a node stat update for FOLL_PIN), a gup_fast_folio_allowed() and a folio_set_referenced(), so a 64 kB mTHP pays sixteen of each and a PTE-mapped 2 MB THP pays 512. The PMD and PUD leaf paths already take the whole range with one try_grab_folio_fast() call, and the slow path is getting the same treatment for PTEs in Rik's follow_page_mask() series.

The users are the hot ones: iov_iter_extract_user_pages() pins every O_DIRECT buffer through pin_user_pages_fast(), io_uring registers its buffers through it, and so does RDMA memory registration. With mTHP those buffers are PTE-mapped large folios.

After the head page has been grabbed and verified as before, extend the run to the following PTEs that map the next pages of the same folio with the same protection bits, take the remaining references for the run with try_grab_folio(), and fill pages[] from the run. The PTEs of the run are read while the reference on the folio is held, so the folio can neither be freed nor split underneath the scan; a THP collapse can still detach the page table, so the pmd is checked again after the run is grabbed and the run is dropped if it changed, leaving the next iteration to hit the existing pmd check and bail out. A read-only run of an anonymous folio stops at the first page that gup_must_unshare() rejects, so the per-page exclusivity guarantee for FOLL_PIN is kept.

folio_pte_batch_flags() does the scan. It reads the entries with ptep_get(), which is not safe without the page table lock on CONFIG_GUP_GET_PXX_LOW_HIGH (x86 PAE, mips32, sh) or with arm64 contpte, and GUP-fast reads every PTE with ptep_get_lockless() for that reason. Add FPB_LOCKLESS to make the helper do the same; it is inlined per call site, so the other callers do not change.

The order-0 path keeps its instruction sequence. The folio flags are tested before folio_set_referenced() on purpose: a load of the flags right after that locked instruction cost about 25% on order-0 pages in the gup_test benchmark below, and reading them first brings that path back to the baseline.

gup_test ioctl over a 256 MB anonymous region, FOLL_WRITE, 65536 pages per call, median of 15 runs, two baseline boots / one patched boot, on an x86-64 VM:

                       get_user_pages_fast()   pin_user_pages_fast()
                       before       after      before       after
  4 kB pages           849/789 us   783 us     1174/1138 us 1094 us
  64 kB mTHP           812/811 us   126 us     1215/1113 us  188 us
  1 MB mTHP            799/774 us    59 us     1185/1077 us   64 us
  2 MB THP, PTE-mapped 803/786 us    57 us     1159/1120 us   58 us

2 MB THP, PMD-mapped 42/40 us 40 us 46/41 us 42 us

fio O_DIRECT randread of a null_blk device (CPU bound) with the I/O buffer in 64 kB mTHP, median of 15 five-second runs:

  bs=1M psync                 126.2/125.6 GB/s  ->  220.6 GB/s
  bs=1M io_uring, iodepth 16   71.2/71.6 GB/s   ->  112.1 GB/s
  bs=64k io_uring, iodepth 16  50.2/50.7 GB/s   ->   60.1 GB/s

IORING_REGISTER_BUFFERS of 1 GB, median of 15:

Quoted 8 lines
Quoted 4 lines
  64 kB mTHP           6179/5998 us  ->  1599 us
  1 MB mTHP            5790/5801 us  ->   893 us
  4 kB pages           7173/6741 us  ->  6763 us

I think Rik was already working on this and sent some patches. Anyhow, there is quite some GUP review backlog I have t go through, so this will have to wait.

Ah, no, Rik looked at GUP not GUP-fast. So I'll have a look but it will take a while.

Signature · 3 lines
Cheers,

David
#3 lore raw
Quoted 2 lines · David Hildenbrand (Arm)

Ah, no, Rik looked at GUP not GUP-fast. So I'll have a look but it will take a while.

Thanks.

One note for whenever you get to it: the patch is against mainline, and the last hunk has a two-line context conflict with Aristeu's gup_fast_*() cleanup in mm-everything (pages[nr_pages++] there). I have the same change on top of mm-everything-2026-09-15-07-39 if that base is preferable, and can post FPB_LOCKLESS as a separate first patch if a two-patch series is easier to review.

Yuan-Hao Hsu

#4 lore raw
In reply to David Hildenbrand (Arm)
Quoted 21 lines · David Hildenbrand (Arm)
Quoted 13 lines · Yuan-Hao Hsu

GUP-fast grabs a PTE-mapped large folio one page at a time.  Every PTE
costs a try_grab_folio_fast() (a refcount cmpxchg, plus the pincount
atomic, a full barrier and a node stat update for FOLL_PIN), a gup_fast_folio_allowed() and a folio_set_referenced(), so a 64 kB mTHP
pays sixteen of each and a PTE-mapped 2 MB THP pays 512.  The PMD and
PUD leaf paths already take the whole range with one try_grab_folio_fast() call, and the slow path is getting the same treatment for PTEs in Rik's follow_page_mask() series.

Quoted 6 lines

IORING_REGISTER_BUFFERS of 1 GB, median of 15:

  64 kB mTHP           6179/5998 us  ->  1599 us
  1 MB mTHP            5790/5801 us  ->   893 us
  4 kB pages           7173/6741 us  ->  6763 us

Quoted 4 lines

I think Rik was already working on this and sent some patches. Anyhow, there is
quite some GUP review backlog I have t go through, so this will have to wait.

My patches are for the regular gup path, in follow_pages_mask()

This is for the gup_fast path.

Unfortunately there is some nearly duplicated code between them. Last I looked I did not find any clear ways to merge them, but I can take another look if you want.

Signature · 1 line
All Rights Reversed.
#5 lore raw
Quoted 30 lines · Rik van Riel
Quoted 21 lines · David Hildenbrand (Arm)
Quoted 13 lines · Yuan-Hao Hsu

GUP-fast grabs a PTE-mapped large folio one page at a time.  Every PTE
costs a try_grab_folio_fast() (a refcount cmpxchg, plus the pincount
atomic, a full barrier and a node stat update for FOLL_PIN), a gup_fast_folio_allowed() and a folio_set_referenced(), so a 64 kB mTHP
pays sixteen of each and a PTE-mapped 2 MB THP pays 512.  The PMD and
PUD leaf paths already take the whole range with one try_grab_folio_fast() call, and the slow path is getting the same treatment for PTEs in Rik's follow_page_mask() series.

Quoted 6 lines

IORING_REGISTER_BUFFERS of 1 GB, median of 15:

  64 kB mTHP           6179/5998 us  ->  1599 us
  1 MB mTHP            5790/5801 us  ->   893 us
  4 kB pages           7173/6741 us  ->  6763 us

Quoted 4 lines

I think Rik was already working on this and sent some patches. Anyhow, there is
quite some GUP review backlog I have t go through, so this will have to wait.

My patches are for the regular gup path, in follow_pages_mask()

Quoted 6 lines

This is for the gup_fast path.

Unfortunately there is some nearly duplicated code between them. Last I looked I did not find any clear ways to merge them, but I can take another look if you want.

We'll likely have to keep it separate.

I looked at the patch here and it needs quite some work do do it cleanly. Too much for me to comment on this, so I'll likely end up doing it myself or delegate it to someone internally.

Signature · 3 lines
Cheers,

David
#6 lore raw
In reply to Yuan-Hao Hsu
Quoted 12 lines · Yuan-Hao Hsu

GUP-fast grabs a PTE-mapped large folio one page at a time.  Every PTE
costs a try_grab_folio_fast() (a refcount cmpxchg, plus the pincount atomic, a full barrier and a node stat update for FOLL_PIN), a gup_fast_folio_allowed() and a folio_set_referenced(), so a 64 kB mTHP
pays sixteen of each and a PTE-mapped 2 MB THP pays 512.  The PMD and PUD leaf paths already take the whole range with one try_grab_folio_fast() call, and the slow path is getting the same treatment for PTEs in Rik's follow_page_mask() series.

Quoted 7 lines

fio O_DIRECT randread of a null_blk device (CPU bound) with the I/O buffer in 64 kB mTHP, median of 15 five-second runs:

  bs=1M psync                 126.2/125.6 GB/s  ->  220.6 GB/s bs=1M io_uring, iodepth 16   71.2/71.6 GB/s   ->  112.1 GB/s bs=64k io_uring, iodepth 16  50.2/50.7 GB/s   ->   60.1 GB/s

That's huge! Nice.

Quoted 21 lines
(quoted diff) +2 -0
+static unsigned int gup_fast_pte_batch(struct folio *folio,
+		struct page *page, pmd_t pmd, pmd_t *pmdp, pte_t

*ptep,

mm/gup.c +10 -0
+		pte_t pte, unsigned int flags, unsigned int max_nr,
+		struct page **pages)
+{
+	unsigned int nr, i;
+
+	if (max_nr == 1)
+		return 1;
+
+	/*
+	 * The reference on @folio keeps folio_nr_pages() stable. 

The PTEs

mm/gup.c +4 -0
+	 * are read without the PTL, hence FPB_LOCKLESS.
+	 */
+	nr = folio_pte_batch_flags(folio, NULL, ptep, &pte, max_nr,
+				   FPB_RESPECT_WRITE |

FPB_LOCKLESS);

...

Review

Quoted 3 lines
(quoted diff) +2 -0
+	if (try_grab_folio(folio, nr - 1, flags))
+		return 1;

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?

The equivalent changes to GUP do not need that check, because that all happens under the pte_lock:

https://lore.kernel.org/all/[email protected]/

Signature · 1 line
All Rights Reversed.
#7 lore raw
Quoted 9 lines · Rik van Riel

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

Reviewed-by
#8 lore raw
Quoted 11 lines · Yuan-Hao Hsu
Quoted 9 lines · Rik van Riel

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?

Quoted 7 lines

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:

You are right. The page tables can still be remapped to point at something else after gup_fast_pte_range() grabs a reference to the folio, but that can happen no matter what precautions we take inside gup_fast, because it can happen after gup_fast has returned.

I have no other concerns, so feel free to add my:

Could I convince you to take a look through my earlier series to make regular GUP go faster?

If there are any issues left in that, I would like to fix them before the maintainers get around to reviewing that series:

https://lore.kernel.org/r/[email protected]/

Signature · 1 line
All Rights Reversed.
#9 lore raw
Quoted 26 lines · Rik van Riel
Quoted 11 lines · Yuan-Hao Hsu
Quoted 9 lines · Rik van Riel

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?

Quoted 7 lines

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:

You are right. The page tables can still be remapped to point at something else after gup_fast_pte_range() grabs a reference to the folio, but that can happen no matter what precautions we take inside gup_fast, because it can happen after gup_fast has returned.

Quoted 3 lines

I have no other concerns, so feel free to add my:

There are a bunch of things I don't like about this, including suspiciously LLM -sloppy looking stuff like

+ if (!pte_write(pte)) {
+ for (i = 1; i < nr; i++) {
+ if (gup_must_unshare(NULL, flags, page + i))
+ break;
+ }
+ nr = i;
+ }

I'll find someone to polish this up.

Signature · 3 lines
Cheers,

David
#10 lore raw
Quoted 42 lines · David Hildenbrand (Arm)
Quoted 26 lines · Rik van Riel
Quoted 11 lines · Yuan-Hao Hsu
Quoted 9 lines · Rik van Riel

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?

Quoted 7 lines

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:

You are right. The page tables can still be remapped to point at something else after gup_fast_pte_range() grabs a reference to the folio, but that can happen no matter what precautions we take inside gup_fast, because it can happen after gup_fast has returned.

Quoted 3 lines

I have no other concerns, so feel free to add my:

There are a bunch of things I don't like about this, including suspiciously LLM

mm/gup.c +7 -1
-sloppy looking stuff like
 
+	if (!pte_write(pte)) {
+		for (i = 1; i < nr; i++) {
+			if (gup_must_unshare(NULL, flags, page + i))
+				break;
+		}
+		nr = i;
+	}
Quoted 1 line

I'll find someone to polish this up.

Thanks David, I'll post with a little bit fix in next rc-period.

Signature · 2 lines
Sincerely,
Yeoreum Yun