* [PATCH v3 0/6] mm: gup: batch PTE-mapped large folios in gup_fast_pte_range()
@ 2026-11-09 12:32 Yeoreum Yun
2026-11-09 12:32 ` [PATCH v3 1/6] mm: khugepaged: ignore MADV_HUGEPAGE for shadow stacks Yeoreum Yun
` (5 more replies)
0 siblings, 6 replies; 8+ messages in thread
From: Yeoreum Yun @ 2026-11-09 12:32 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Jason Gunthorpe, John Hubbard, Peter Xu, Lorenzo Stoakes,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, Andrew Morton,
David Hildenbrand, Rik van Riel, Yuan-Hao Hsu, Yeoreum Yun,
stable
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 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, 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 collapsed, freed nor split underneath the scan.
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
---
Changes in v3:
- move gup_must_unshare() and INTERNAL_GUP_FLAGS from internal.h to gup.c
- drop the folio_pte_batch_flags() patch for shadow-stack.
- ignore VM_HUGEPAGE for shadow-stack via madvise().
- add r-b / a-b tags.
- Link to v2: https://lore.kernel.org/r/20261102-refactor_gup_fast-v2-0-bc1ac018c834@arm.com
Changes in v2:
- fix exceptional case for shstk on x86.
- remove redendant check for pmd change in each pte barch.
- Link to v1: https://lore.kernel.org/all/20260917083747.786-1-aa9736195201@gmail.com/
---
Yeoreum Yun (5):
mm: khugepaged: ignore MADV_HUGEPAGE for shadow stacks
mm: gup: clarify a reason to check pte/pmd in gup_fast_pte_range()
mm: gup: move gup_must_unshare() and INTERNAL_GUP_FLAGS to gup.c
mm: gup: introduce gup_unshared_pages()
mm: gup: batch PTE-mapped large folios in gup_fast_pte_range()
Yuan-Hao Hsu (1):
mm: internal: introduce FPB_LOCKLESS for folio_pte_batch_flags()
mm/gup.c | 185 +++++++++++++++++++++++++++++++++++++++++++++-----------
mm/internal.h | 78 +++---------------------
mm/khugepaged.c | 3 +
3 files changed, 159 insertions(+), 107 deletions(-)
---
base-commit: b3710a1d0f145a4f6f1d44fdaff56a6af8cffe89
change-id: 20261015-refactor_gup_fast-b55e3f593dc2
Best regards,
--
Sincerely,
Yeoreum Yun
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v3 1/6] mm: khugepaged: ignore MADV_HUGEPAGE for shadow stacks
2026-11-09 12:32 [PATCH v3 0/6] mm: gup: batch PTE-mapped large folios in gup_fast_pte_range() Yeoreum Yun
@ 2026-11-09 12:32 ` Yeoreum Yun
2026-11-09 14:22 ` David Hildenbrand (Arm)
2026-11-09 12:32 ` [PATCH v3 2/6] mm: gup: clarify a reason to check pte/pmd in gup_fast_pte_range() Yeoreum Yun
` (4 subsequent siblings)
5 siblings, 1 reply; 8+ messages in thread
From: Yeoreum Yun @ 2026-11-09 12:32 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Jason Gunthorpe, John Hubbard, Peter Xu, Lorenzo Stoakes,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, Andrew Morton,
David Hildenbrand, Rik van Riel, Yuan-Hao Hsu, Yeoreum Yun,
stable
commit 3efb9800557b ("mm: do not map the shadow stack as THP") makes
shadow stack mappings default to VM_NOHUGEPAGE. However, userspace can
still clear this flag and set VM_HUGEPAGE through madvise().
Allowing large folios in x86 shadow stacks exposes a limitation in
folio_pte_batch_flags(). For example:
1. create a shadow stack mapping.
2. apply MADV_HUGEPAGE and populate the mapping with a PTE-mapped
large folio.
3. apply MADV_DONTFORK to the first base page of that folio.
4. call fork().
In the parent, the first page remains writable and anonymous-exclusive,
while the remaining pages shared with the child become write-protected
and lose PageAnonExclusive(). These pages can still belong to the same
large folio.
On x86, writable shadow-stack PTEs have _PAGE_RW clear and _PAGE_DIRTY
set. Write-protected PTEs have both bits clear, with the dirty state
preserved in _PAGE_SAVED_DIRTY.
When FPB_RESPECT_WRITE is set without FPB_RESPECT_DIRTY,
folio_pte_batch_flags() normalizes PTEs using pte_mkclean(), which clears
both dirty bits. This loses the distinction between writable
shadow-stack PTEs and write-protected PTEs, allowing them to be grouped
into the same batch. GUP-fast can encounter such a batch when walking
across the VMA boundary created by MADV_DONTFORK.
Since there is no practical use-case shadow stack with large folio,
ignore MADV_HUGEPAGE for shadow stack mappings, preserving their
VM_NOHUGEPAGE setting and preventing large-folio allocation.
Fixes: 3efb9800557b ("mm: do not map the shadow stack as THP")
Suggested-by: David Hildenbrand (Arm) <david@kernel.org>
Signed-off-by: Yeoreum Yun <yeoreum.yun@arm.com>
Cc: <stable@vger.kernel.org>
---
mm/khugepaged.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 40fad28c52f6f..51ec043fa26c3 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -367,6 +367,9 @@ int hugepage_madvise(struct vm_area_struct *vma,
{
switch (advice) {
case MADV_HUGEPAGE:
+ /* PTE batching doesn't work reliably. */
+ if (vma_test_single_mask(vma, VMA_SHADOW_STACK))
+ break;
*vm_flags &= ~VM_NOHUGEPAGE;
*vm_flags |= VM_HUGEPAGE;
break;
--
2.43.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v3 2/6] mm: gup: clarify a reason to check pte/pmd in gup_fast_pte_range()
2026-11-09 12:32 [PATCH v3 0/6] mm: gup: batch PTE-mapped large folios in gup_fast_pte_range() Yeoreum Yun
2026-11-09 12:32 ` [PATCH v3 1/6] mm: khugepaged: ignore MADV_HUGEPAGE for shadow stacks Yeoreum Yun
@ 2026-11-09 12:32 ` Yeoreum Yun
2026-11-09 12:32 ` [PATCH v3 3/6] mm: gup: move gup_must_unshare() and INTERNAL_GUP_FLAGS to gup.c Yeoreum Yun
` (3 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: Yeoreum Yun @ 2026-11-09 12:32 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Jason Gunthorpe, John Hubbard, Peter Xu, Lorenzo Stoakes,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, Andrew Morton,
David Hildenbrand, Rik van Riel, Yuan-Hao Hsu, Yeoreum Yun
Clarify a reason to check pte/pmd in gup_fast_pte_range().
Signed-off-by: Yeoreum Yun <yeoreum.yun@arm.com>
---
mm/gup.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/mm/gup.c b/mm/gup.c
index 7d43ce717b246..7f9348c633d58 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -2881,6 +2881,14 @@ static unsigned long gup_fast_pte_range(pmd_t pmd, pmd_t *pmdp,
if (!folio)
goto pte_unmap;
+ /*
+ * Recheck the PTE after grabbing the folio to detect concurrent
+ * unmapping or protection changes such as by KSM.
+ *
+ * Also, recheck the PMD: THP collpase can detach the PTE table
+ * while its entries remain unchaged. Checking the PTE alone
+ * would not dtect that we are walking a detached page table.
+ */
if (unlikely(pmd_val(pmd) != pmd_val(pmdp_get_lockless(pmdp))) ||
unlikely(pte_val(pte) != pte_val(ptep_get_lockless(ptep)))) {
gup_put_folio(folio, 1, flags);
--
2.43.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v3 3/6] mm: gup: move gup_must_unshare() and INTERNAL_GUP_FLAGS to gup.c
2026-11-09 12:32 [PATCH v3 0/6] mm: gup: batch PTE-mapped large folios in gup_fast_pte_range() Yeoreum Yun
2026-11-09 12:32 ` [PATCH v3 1/6] mm: khugepaged: ignore MADV_HUGEPAGE for shadow stacks Yeoreum Yun
2026-11-09 12:32 ` [PATCH v3 2/6] mm: gup: clarify a reason to check pte/pmd in gup_fast_pte_range() Yeoreum Yun
@ 2026-11-09 12:32 ` Yeoreum Yun
2026-11-09 12:32 ` [PATCH v3 4/6] mm: internal: introduce FPB_LOCKLESS for folio_pte_batch_flags() Yeoreum Yun
` (2 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: Yeoreum Yun @ 2026-11-09 12:32 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Jason Gunthorpe, John Hubbard, Peter Xu, Lorenzo Stoakes,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, Andrew Morton,
David Hildenbrand, Rik van Riel, Yuan-Hao Hsu, Yeoreum Yun
gup_must_unshare() and INTERNAL_GUP_FLAGS are used in gup.c only.
Move gup_must_unshare() and INTERNAL_GUP_FLAGS from internal.h to gup.c
Signed-off-by: Yeoreum Yun <yeoreum.yun@arm.com>
---
mm/gup.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
mm/internal.h | 70 -----------------------------------------------------------
2 files changed, 69 insertions(+), 70 deletions(-)
diff --git a/mm/gup.c b/mm/gup.c
index 7f9348c633d58..bfe83ce1411d5 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -633,6 +633,71 @@ static struct page *no_page_table(struct vm_area_struct *vma,
return NULL;
}
+/*
+ * Indicates for which pages that are write-protected in the page table,
+ * whether GUP has to trigger unsharing via FAULT_FLAG_UNSHARE such that the
+ * GUP pin will remain consistent with the pages mapped into the page tables
+ * of the MM.
+ *
+ * Temporary unmapping of PageAnonExclusive() pages or clearing of
+ * PageAnonExclusive() has to protect against concurrent GUP:
+ * * Ordinary GUP: Using the PT lock
+ * * GUP-fast and fork(): mm->write_protect_seq
+ * * GUP-fast and KSM or temporary unmapping (swap, migration): see
+ * folio_try_share_anon_rmap_*()
+ *
+ * Must be called with the (sub)page that's actually referenced via the
+ * page table entry, which might not necessarily be the head page for a
+ * PTE-mapped THP.
+ *
+ * If the vma is NULL, we're coming from the GUP-fast path and might have
+ * to fallback to the slow path just to lookup the vma.
+ */
+static inline bool gup_must_unshare(struct vm_area_struct *vma,
+ unsigned int flags, struct page *page)
+{
+ /*
+ * 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.
+ */
+ if ((flags & (FOLL_WRITE | FOLL_PIN)) != FOLL_PIN)
+ return false;
+ /*
+ * Note: PageAnon(page) is stable until the page is actually getting
+ * freed.
+ */
+ if (!PageAnon(page)) {
+ /*
+ * We only care about R/O long-term pining: R/O short-term
+ * pinning does not have the semantics to observe successive
+ * changes through the process page tables.
+ */
+ if (!(flags & FOLL_LONGTERM))
+ return false;
+
+ /* We really need the vma ... */
+ if (!vma)
+ return true;
+
+ /*
+ * ... because we only care about writable private ("COW")
+ * mappings where we have to break COW early.
+ */
+ return vma_is_cow_mapping(vma);
+ }
+
+ /* Paired with a memory barrier in folio_try_share_anon_rmap_*(). */
+ if (IS_ENABLED(CONFIG_HAVE_GUP_FAST))
+ smp_rmb();
+
+ /*
+ * Note that KSM pages cannot be exclusive, and consequently,
+ * cannot get pinned.
+ */
+ return !PageAnonExclusive(page);
+}
+
#ifdef CONFIG_PGTABLE_HAS_HUGE_LEAVES
/* FOLL_FORCE can write to even unwritable PUDs in COW mappings. */
static inline bool can_follow_write_pud(pud_t pud, struct page *page,
@@ -2503,6 +2568,10 @@ static long __gup_longterm_locked(struct mm_struct *mm,
return rc ? rc : nr_pinned_pages;
}
+#define INTERNAL_GUP_FLAGS (FOLL_TOUCH | FOLL_TRIED | FOLL_REMOTE | FOLL_PIN | \
+ FOLL_FAST_ONLY | FOLL_UNLOCKABLE | \
+ FOLL_MADV_POPULATE)
+
/*
* Check that the given flags are valid for the exported gup/pup interface, and
* update them with the required flags that the caller must have set.
diff --git a/mm/internal.h b/mm/internal.h
index 9b8864a8540aa..7a315c0a93f49 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -1330,76 +1330,6 @@ enum {
FOLL_MADV_POPULATE = 1 << 22,
};
-#define INTERNAL_GUP_FLAGS (FOLL_TOUCH | FOLL_TRIED | FOLL_REMOTE | FOLL_PIN | \
- FOLL_FAST_ONLY | FOLL_UNLOCKABLE | \
- FOLL_MADV_POPULATE)
-
-/*
- * Indicates for which pages that are write-protected in the page table,
- * whether GUP has to trigger unsharing via FAULT_FLAG_UNSHARE such that the
- * GUP pin will remain consistent with the pages mapped into the page tables
- * of the MM.
- *
- * Temporary unmapping of PageAnonExclusive() pages or clearing of
- * PageAnonExclusive() has to protect against concurrent GUP:
- * * Ordinary GUP: Using the PT lock
- * * GUP-fast and fork(): mm->write_protect_seq
- * * GUP-fast and KSM or temporary unmapping (swap, migration): see
- * folio_try_share_anon_rmap_*()
- *
- * Must be called with the (sub)page that's actually referenced via the
- * page table entry, which might not necessarily be the head page for a
- * PTE-mapped THP.
- *
- * If the vma is NULL, we're coming from the GUP-fast path and might have
- * to fallback to the slow path just to lookup the vma.
- */
-static inline bool gup_must_unshare(struct vm_area_struct *vma,
- unsigned int flags, struct page *page)
-{
- /*
- * 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.
- */
- if ((flags & (FOLL_WRITE | FOLL_PIN)) != FOLL_PIN)
- return false;
- /*
- * Note: PageAnon(page) is stable until the page is actually getting
- * freed.
- */
- if (!PageAnon(page)) {
- /*
- * We only care about R/O long-term pining: R/O short-term
- * pinning does not have the semantics to observe successive
- * changes through the process page tables.
- */
- if (!(flags & FOLL_LONGTERM))
- return false;
-
- /* We really need the vma ... */
- if (!vma)
- return true;
-
- /*
- * ... because we only care about writable private ("COW")
- * mappings where we have to break COW early.
- */
- return vma_is_cow_mapping(vma);
- }
-
- /* Paired with a memory barrier in folio_try_share_anon_rmap_*(). */
- if (IS_ENABLED(CONFIG_HAVE_GUP_FAST))
- smp_rmb();
-
- /*
- * Note that KSM pages cannot be exclusive, and consequently,
- * cannot get pinned.
- */
- return !PageAnonExclusive(page);
-}
-
-
static inline bool vma_soft_dirty_enabled(struct vm_area_struct *vma)
{
/*
--
2.43.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v3 4/6] mm: internal: introduce FPB_LOCKLESS for folio_pte_batch_flags()
2026-11-09 12:32 [PATCH v3 0/6] mm: gup: batch PTE-mapped large folios in gup_fast_pte_range() Yeoreum Yun
` (2 preceding siblings ...)
2026-11-09 12:32 ` [PATCH v3 3/6] mm: gup: move gup_must_unshare() and INTERNAL_GUP_FLAGS to gup.c Yeoreum Yun
@ 2026-11-09 12:32 ` Yeoreum Yun
2026-11-09 12:32 ` [PATCH v3 5/6] mm: gup: introduce gup_unshared_pages() Yeoreum Yun
2026-11-09 12:32 ` [PATCH v3 6/6] mm: gup: batch PTE-mapped large folios in gup_fast_pte_range() Yeoreum Yun
5 siblings, 0 replies; 8+ messages in thread
From: Yeoreum Yun @ 2026-11-09 12:32 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Jason Gunthorpe, John Hubbard, Peter Xu, Lorenzo Stoakes,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, Andrew Morton,
David Hildenbrand, Rik van Riel, Yuan-Hao Hsu, Yeoreum Yun
From: Yuan-Hao Hsu <aa9736195201@gmail.com>
Add support for calling folio_pte_batch_flags() in GUP-fast context when we are
not holding the PT lock. Instead of reading PTEs through ptep_get(), we have to
use ptep_get_lockless().
Specify the new behavior through a new flag FPB_LOCKLESS. We'll make use of this
functionality for batching PTEs in gup_fast_pte_range() next.
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
Signed-off-by: Yuan-Hao Hsu <aa9736195201@gmail.com>
Signed-off-by: Yeoreum Yun <yeoreum.yun@arm.com>
---
mm/internal.h | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/mm/internal.h b/mm/internal.h
index 7a315c0a93f49..e4992ca0dbd6a 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -373,6 +373,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 (e.g, 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))
@@ -441,7 +447,7 @@ static inline unsigned int folio_pte_batch_flags(struct folio *folio,
ptep = ptep + nr;
while (nr < max_nr) {
- pte = ptep_get(ptep);
+ pte = (flags & FPB_LOCKLESS) ? ptep_get_lockless(ptep) : ptep_get(ptep);
if (!pte_same(__pte_batch_clear_ignored(pte, flags), expected_pte))
break;
--
2.43.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v3 5/6] mm: gup: introduce gup_unshared_pages()
2026-11-09 12:32 [PATCH v3 0/6] mm: gup: batch PTE-mapped large folios in gup_fast_pte_range() Yeoreum Yun
` (3 preceding siblings ...)
2026-11-09 12:32 ` [PATCH v3 4/6] mm: internal: introduce FPB_LOCKLESS for folio_pte_batch_flags() Yeoreum Yun
@ 2026-11-09 12:32 ` Yeoreum Yun
2026-11-09 12:32 ` [PATCH v3 6/6] mm: gup: batch PTE-mapped large folios in gup_fast_pte_range() Yeoreum Yun
5 siblings, 0 replies; 8+ messages in thread
From: Yeoreum Yun @ 2026-11-09 12:32 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Jason Gunthorpe, John Hubbard, Peter Xu, Lorenzo Stoakes,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, Andrew Morton,
David Hildenbrand, Rik van Riel, Yuan-Hao Hsu, Yeoreum Yun
This is preparatory patch to make gup_fast_pte_range() batchable.
Introduce gup_unshared_pages() to count consecutive pages within a folio
that do not require unsharing, and use it to implement gup_must_unshare().
Also add a folio argument to gup_must_unshare() so callers can reuse an
existing folio and avoid the compound_head() call implicit in PageAnon().
There is no functional change.
Signed-off-by: Yeoreum Yun <yeoreum.yun@arm.com>
---
mm/gup.c | 111 +++++++++++++++++++++++++++++++++++++--------------------------
1 file changed, 66 insertions(+), 45 deletions(-)
diff --git a/mm/gup.c b/mm/gup.c
index bfe83ce1411d5..c863bae33e2f1 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -633,58 +633,39 @@ static struct page *no_page_table(struct vm_area_struct *vma,
return NULL;
}
-/*
- * Indicates for which pages that are write-protected in the page table,
- * whether GUP has to trigger unsharing via FAULT_FLAG_UNSHARE such that the
- * GUP pin will remain consistent with the pages mapped into the page tables
- * of the MM.
- *
- * Temporary unmapping of PageAnonExclusive() pages or clearing of
- * PageAnonExclusive() has to protect against concurrent GUP:
- * * Ordinary GUP: Using the PT lock
- * * GUP-fast and fork(): mm->write_protect_seq
- * * GUP-fast and KSM or temporary unmapping (swap, migration): see
- * folio_try_share_anon_rmap_*()
- *
- * Must be called with the (sub)page that's actually referenced via the
- * page table entry, which might not necessarily be the head page for a
- * PTE-mapped THP.
- *
- * If the vma is NULL, we're coming from the GUP-fast path and might have
- * to fallback to the slow path just to lookup the vma.
- */
-static inline bool gup_must_unshare(struct vm_area_struct *vma,
- unsigned int flags, struct page *page)
+static unsigned long gup_unshared_pages(struct vm_area_struct *vma,
+ unsigned int flags, struct folio *folio,
+ struct page *page, unsigned long nr_pages)
{
+ 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.
*/
if ((flags & (FOLL_WRITE | FOLL_PIN)) != FOLL_PIN)
- return false;
- /*
- * Note: PageAnon(page) is stable until the page is actually getting
- * freed.
- */
- if (!PageAnon(page)) {
+ return nr_pages;
+
+ /* folio_test_anon(folio) is stable until folio gets freed. */
+ if (!folio_test_anon(folio)) {
/*
* We only care about R/O long-term pining: R/O short-term
* pinning does not have the semantics to observe successive
* changes through the process page tables.
*/
if (!(flags & FOLL_LONGTERM))
- return false;
+ return nr_pages;
/* We really need the vma ... */
- if (!vma)
- return true;
+ if (!vma || vma_is_cow_mapping(vma))
+ return 0;
/*
* ... because we only care about writable private ("COW")
* mappings where we have to break COW early.
*/
- return vma_is_cow_mapping(vma);
+ return nr_pages;
}
/* Paired with a memory barrier in folio_try_share_anon_rmap_*(). */
@@ -695,7 +676,38 @@ static inline bool gup_must_unshare(struct vm_area_struct *vma,
* Note that KSM pages cannot be exclusive, and consequently,
* cannot get pinned.
*/
- return !PageAnonExclusive(page);
+ for (i = 0; i < nr_pages; i++) {
+ if (!PageAnonExclusive(page + i))
+ return i;
+ }
+
+ return nr_pages;
+}
+
+/*
+ * Indicates for which pages that are write-protected in the page table,
+ * whether GUP has to trigger unsharing via FAULT_FLAG_UNSHARE such that the
+ * GUP pin will remain consistent with the pages mapped into the page tables
+ * of the MM.
+ *
+ * Temporary unmapping of PageAnonExclusive() pages or clearing of
+ * PageAnonExclusive() has to protect against concurrent GUP:
+ * * Ordinary GUP: Using the PT lock
+ * * GUP-fast and fork(): mm->write_protect_seq
+ * * GUP-fast and KSM or temporary unmapping (swap, migration): see
+ * folio_try_share_anon_rmap_*()
+ *
+ * Must be called with the (sub)page that's actually referenced via the
+ * page table entry, which might not necessarily be the head page for a
+ * PTE-mapped THP.
+ *
+ * If the vma is NULL, we're coming from the GUP-fast path and might have
+ * to fallback to the slow path just to lookup the vma.
+ */
+static inline bool gup_must_unshare(struct vm_area_struct *vma,
+ unsigned int flags, struct folio *folio, struct page *page)
+{
+ return gup_unshared_pages(vma, flags, folio, page, 1) != 1;
}
#ifdef CONFIG_PGTABLE_HAS_HUGE_LEAVES
@@ -717,6 +729,7 @@ static struct page *follow_huge_pud(struct vm_area_struct *vma,
{
struct mm_struct *mm = vma->vm_mm;
struct page *page;
+ struct folio *folio;
pud_t pud = *pudp;
unsigned long pfn = pud_pfn(pud);
int ret;
@@ -732,11 +745,12 @@ static struct page *follow_huge_pud(struct vm_area_struct *vma,
pfn += (addr & ~PUD_MASK) >> PAGE_SHIFT;
page = pfn_to_page(pfn);
+ folio = page_folio(page);
- if (!pud_write(pud) && gup_must_unshare(vma, flags, page))
+ if (!pud_write(pud) && gup_must_unshare(vma, flags, folio, page))
return ERR_PTR(-EMLINK);
- ret = try_grab_folio(page_folio(page), 1, flags);
+ ret = try_grab_folio(folio, 1, flags);
if (ret)
page = ERR_PTR(ret);
else
@@ -771,11 +785,14 @@ static struct page *follow_huge_pmd(struct vm_area_struct *vma,
struct mm_struct *mm = vma->vm_mm;
pmd_t pmdval = *pmd;
struct page *page;
+ struct folio *folio;
int ret;
assert_spin_locked(pmd_lockptr(mm, pmd));
page = pmd_page(pmdval);
+ folio = page_folio(page);
+
if ((flags & FOLL_WRITE) &&
!can_follow_write_pmd(pmdval, page, vma, flags))
return NULL;
@@ -787,13 +804,13 @@ static struct page *follow_huge_pmd(struct vm_area_struct *vma,
if (pmd_protnone(*pmd) && !gup_can_follow_protnone(vma, flags))
return NULL;
- if (!pmd_write(pmdval) && gup_must_unshare(vma, flags, page))
+ if (!pmd_write(pmdval) && gup_must_unshare(vma, flags, folio, page))
return ERR_PTR(-EMLINK);
VM_WARN_ON_ONCE_PAGE((flags & FOLL_PIN) && PageAnon(page) &&
!PageAnonExclusive(page), page);
- ret = try_grab_folio(page_folio(page), 1, flags);
+ ret = try_grab_folio(folio, 1, flags);
if (ret)
return ERR_PTR(ret);
@@ -911,7 +928,7 @@ static struct page *follow_page_pte(struct vm_area_struct *vma,
}
folio = page_folio(page);
- if (!pte_write(pte) && gup_must_unshare(vma, flags, page)) {
+ if (!pte_write(pte) && gup_must_unshare(vma, flags, folio, page)) {
page = ERR_PTR(-EMLINK);
goto out;
}
@@ -2969,7 +2986,7 @@ static unsigned long gup_fast_pte_range(pmd_t pmd, pmd_t *pmdp,
goto pte_unmap;
}
- if (!pte_write(pte) && gup_must_unshare(NULL, flags, page)) {
+ if (!pte_write(pte) && gup_must_unshare(NULL, flags, folio, page)) {
gup_put_folio(folio, 1, flags);
goto pte_unmap;
}
@@ -3016,7 +3033,7 @@ static unsigned long gup_fast_pmd_leaf(pmd_t orig, pmd_t *pmdp,
unsigned long addr, unsigned long end,
unsigned int flags, struct page **pages)
{
- struct page *page;
+ struct page *mapped_page, *page;
struct folio *folio;
unsigned long nr_pages, i;
@@ -3031,7 +3048,8 @@ static unsigned long gup_fast_pmd_leaf(pmd_t orig, pmd_t *pmdp,
return 0;
nr_pages = (end - addr) >> PAGE_SHIFT;
- page = pmd_page(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
+ mapped_page = pmd_page(orig);
+ page = mapped_page + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
folio = try_grab_folio_fast(page, nr_pages, flags);
if (!folio)
@@ -3046,7 +3064,8 @@ static unsigned long gup_fast_pmd_leaf(pmd_t orig, pmd_t *pmdp,
gup_put_folio(folio, nr_pages, flags);
return 0;
}
- if (!pmd_write(orig) && gup_must_unshare(NULL, flags, &folio->page)) {
+ if (!pmd_write(orig) &&
+ gup_must_unshare(NULL, flags, folio, mapped_page)) {
gup_put_folio(folio, nr_pages, flags);
return 0;
}
@@ -3061,7 +3080,7 @@ static unsigned long gup_fast_pud_leaf(pud_t orig, pud_t *pudp,
unsigned long addr, unsigned long end,
unsigned int flags, struct page **pages)
{
- struct page *page;
+ struct page *mapped_page, *page;
struct folio *folio;
unsigned long nr_pages, i;
@@ -3072,7 +3091,8 @@ static unsigned long gup_fast_pud_leaf(pud_t orig, pud_t *pudp,
return 0;
nr_pages = (end - addr) >> PAGE_SHIFT;
- page = pud_page(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
+ mapped_page = pud_page(orig);
+ page = mapped_page + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
folio = try_grab_folio_fast(page, nr_pages, flags);
if (!folio)
@@ -3088,7 +3108,8 @@ static unsigned long gup_fast_pud_leaf(pud_t orig, pud_t *pudp,
return 0;
}
- if (!pud_write(orig) && gup_must_unshare(NULL, flags, &folio->page)) {
+ if (!pud_write(orig) &&
+ gup_must_unshare(NULL, flags, folio, mapped_page)) {
gup_put_folio(folio, nr_pages, flags);
return 0;
}
--
2.43.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v3 6/6] mm: gup: batch PTE-mapped large folios in gup_fast_pte_range()
2026-11-09 12:32 [PATCH v3 0/6] mm: gup: batch PTE-mapped large folios in gup_fast_pte_range() Yeoreum Yun
` (4 preceding siblings ...)
2026-11-09 12:32 ` [PATCH v3 5/6] mm: gup: introduce gup_unshared_pages() Yeoreum Yun
@ 2026-11-09 12:32 ` Yeoreum Yun
5 siblings, 0 replies; 8+ messages in thread
From: Yeoreum Yun @ 2026-11-09 12:32 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Jason Gunthorpe, John Hubbard, Peter Xu, Lorenzo Stoakes,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, Andrew Morton,
David Hildenbrand, Rik van Riel, Yuan-Hao Hsu, Yeoreum Yun
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 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 collapsed, freed nor split underneath the scan.
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
Signed-off-by: Yuan-Hao Hsu <aa9736195201@gmail.com>
Signed-off-by: Yeoreum Yun <yeoreum.yun@arm.com>
---
mm/gup.c | 65 +++++++++++++++++++++++++++++++++++++++-------------------------
1 file changed, 40 insertions(+), 25 deletions(-)
diff --git a/mm/gup.c b/mm/gup.c
index c863bae33e2f1..abb09f73f8ee0 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -2932,7 +2932,8 @@ static unsigned long gup_fast_pte_range(pmd_t pmd, pmd_t *pmdp,
unsigned long addr, unsigned long end,
unsigned int flags, struct page **pages)
{
- unsigned long nr_pages = 0;
+ unsigned long nr_pages = 0, nr_batch;
+ struct folio *folio = NULL;
pte_t *ptep, *ptem;
ptem = ptep = pte_offset_map(&pmd, addr);
@@ -2941,7 +2942,9 @@ static unsigned long gup_fast_pte_range(pmd_t pmd, pmd_t *pmdp,
do {
pte_t pte = ptep_get_lockless(ptep);
struct page *page;
- struct folio *folio;
+ unsigned long i;
+
+ nr_batch = 1;
/*
* Always fallback to ordinary GUP on PROT_NONE-mapped pages:
@@ -2951,13 +2954,13 @@ static unsigned long gup_fast_pte_range(pmd_t pmd, pmd_t *pmdp,
* permissions.
*/
if (pte_protnone(pte))
- goto pte_unmap;
+ goto out;
if (!pte_access_permitted(pte, flags & FOLL_WRITE))
- goto pte_unmap;
+ goto out;
if (pte_special(pte))
- goto pte_unmap;
+ goto out;
/* If it's not marked as special it must have a valid memmap. */
VM_WARN_ON_ONCE(!pfn_valid(pte_pfn(pte)));
@@ -2965,7 +2968,7 @@ static unsigned long gup_fast_pte_range(pmd_t pmd, pmd_t *pmdp,
folio = try_grab_folio_fast(page, 1, flags);
if (!folio)
- goto pte_unmap;
+ goto out;
/*
* Recheck the PTE after grabbing the folio to detect concurrent
@@ -2976,19 +2979,25 @@ static unsigned long gup_fast_pte_range(pmd_t pmd, pmd_t *pmdp,
* would not dtect that we are walking a detached page table.
*/
if (unlikely(pmd_val(pmd) != pmd_val(pmdp_get_lockless(pmdp))) ||
- unlikely(pte_val(pte) != pte_val(ptep_get_lockless(ptep)))) {
- gup_put_folio(folio, 1, flags);
- goto pte_unmap;
- }
+ unlikely(pte_val(pte) != pte_val(ptep_get_lockless(ptep))))
+ goto out;
- if (!gup_fast_folio_allowed(folio, flags)) {
- gup_put_folio(folio, 1, flags);
- goto pte_unmap;
- }
+ if (!gup_fast_folio_allowed(folio, flags))
+ goto out;
- if (!pte_write(pte) && gup_must_unshare(NULL, flags, folio, page)) {
- gup_put_folio(folio, 1, flags);
- goto pte_unmap;
+ 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_unshared_pages(NULL, flags, folio, page, nr_batch);
+ if (!nr_batch)
+ goto out;
}
/*
@@ -2997,16 +3006,22 @@ static unsigned long gup_fast_pte_range(pmd_t pmd, pmd_t *pmdp,
* see Documentation/core-api/pin_user_pages.rst for
* details.
*/
- if ((flags & FOLL_PIN) && arch_make_folio_accessible(folio)) {
- gup_put_folio(folio, 1, flags);
- goto pte_unmap;
- }
+ if ((flags & FOLL_PIN) && arch_make_folio_accessible(folio))
+ goto out;
+
+ if (nr_batch > 1 && try_grab_folio(folio, nr_batch - 1, flags))
+ nr_batch = 1;
+
folio_set_referenced(folio);
- pages[nr_pages] = page;
- nr_pages++;
- } while (ptep++, addr += PAGE_SIZE, addr != end);
+ for (i = 0; i < nr_batch; i++)
+ pages[nr_pages + i] = page + i;
+ nr_pages += nr_batch;
+ folio = NULL;
+ } while (ptep += nr_batch, addr += nr_batch * PAGE_SIZE, addr != end);
-pte_unmap:
+out:
+ if (folio)
+ gup_put_folio(folio, 1, flags);
pte_unmap(ptem);
return nr_pages;
}
--
2.43.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v3 1/6] mm: khugepaged: ignore MADV_HUGEPAGE for shadow stacks
2026-11-09 12:32 ` [PATCH v3 1/6] mm: khugepaged: ignore MADV_HUGEPAGE for shadow stacks Yeoreum Yun
@ 2026-11-09 14:22 ` David Hildenbrand (Arm)
0 siblings, 0 replies; 8+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-09 14:22 UTC (permalink / raw)
To: Yeoreum Yun, linux-mm, linux-kernel
Cc: Jason Gunthorpe, John Hubbard, Peter Xu, Lorenzo Stoakes,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, Andrew Morton, Rik van Riel,
Yuan-Hao Hsu, stable
On 11/9/26 13:32, Yeoreum Yun wrote:
> commit 3efb9800557b ("mm: do not map the shadow stack as THP") makes
> shadow stack mappings default to VM_NOHUGEPAGE. However, userspace can
> still clear this flag and set VM_HUGEPAGE through madvise().
>
> Allowing large folios in x86 shadow stacks exposes a limitation in
> folio_pte_batch_flags(). For example:
>
> 1. create a shadow stack mapping.
> 2. apply MADV_HUGEPAGE and populate the mapping with a PTE-mapped
> large folio.
> 3. apply MADV_DONTFORK to the first base page of that folio.
> 4. call fork().
>
> In the parent, the first page remains writable and anonymous-exclusive,
> while the remaining pages shared with the child become write-protected
> and lose PageAnonExclusive(). These pages can still belong to the same
> large folio.
>
> On x86, writable shadow-stack PTEs have _PAGE_RW clear and _PAGE_DIRTY
> set. Write-protected PTEs have both bits clear, with the dirty state
> preserved in _PAGE_SAVED_DIRTY.
>
> When FPB_RESPECT_WRITE is set without FPB_RESPECT_DIRTY,
> folio_pte_batch_flags() normalizes PTEs using pte_mkclean(), which clears
> both dirty bits. This loses the distinction between writable
> shadow-stack PTEs and write-protected PTEs, allowing them to be grouped
> into the same batch. GUP-fast can encounter such a batch when walking
> across the VMA boundary created by MADV_DONTFORK.
>
> Since there is no practical use-case shadow stack with large folio,
> ignore MADV_HUGEPAGE for shadow stack mappings, preserving their
> VM_NOHUGEPAGE setting and preventing large-folio allocation.
>
> Fixes: 3efb9800557b ("mm: do not map the shadow stack as THP")
> Suggested-by: David Hildenbrand (Arm) <david@kernel.org>
> Signed-off-by: Yeoreum Yun <yeoreum.yun@arm.com>
> Cc: <stable@vger.kernel.org>
> ---
> mm/khugepaged.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> index 40fad28c52f6f..51ec043fa26c3 100644
> --- a/mm/khugepaged.c
> +++ b/mm/khugepaged.c
> @@ -367,6 +367,9 @@ int hugepage_madvise(struct vm_area_struct *vma,
> {
> switch (advice) {
> case MADV_HUGEPAGE:
> + /* PTE batching doesn't work reliably. */
> + if (vma_test_single_mask(vma, VMA_SHADOW_STACK))
> + break;
> *vm_flags &= ~VM_NOHUGEPAGE;
> *vm_flags |= VM_HUGEPAGE;
> break;
>
Ack, I prefer this solution for now until we know how to make pte_mkclean() on
x86 shadow stack PTEs not result in an implicit pte_wrprotect().
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
--
Cheers,
David
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-11-09 14:22 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-09 12:32 [PATCH v3 0/6] mm: gup: batch PTE-mapped large folios in gup_fast_pte_range() Yeoreum Yun
2026-11-09 12:32 ` [PATCH v3 1/6] mm: khugepaged: ignore MADV_HUGEPAGE for shadow stacks Yeoreum Yun
2026-11-09 14:22 ` David Hildenbrand (Arm)
2026-11-09 12:32 ` [PATCH v3 2/6] mm: gup: clarify a reason to check pte/pmd in gup_fast_pte_range() Yeoreum Yun
2026-11-09 12:32 ` [PATCH v3 3/6] mm: gup: move gup_must_unshare() and INTERNAL_GUP_FLAGS to gup.c Yeoreum Yun
2026-11-09 12:32 ` [PATCH v3 4/6] mm: internal: introduce FPB_LOCKLESS for folio_pte_batch_flags() Yeoreum Yun
2026-11-09 12:32 ` [PATCH v3 5/6] mm: gup: introduce gup_unshared_pages() Yeoreum Yun
2026-11-09 12:32 ` [PATCH v3 6/6] mm: gup: batch PTE-mapped large folios in gup_fast_pte_range() Yeoreum Yun
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®