From: Yeoreum Yun <yeoreum.yun@arm.com>
To: linux-mm@kvack.org, linux-kernel@vger.kernel.org
Cc: Jason Gunthorpe <jgg@ziepe.ca>,
John Hubbard <jhubbard@nvidia.com>, Peter Xu <peterx@redhat.com>,
Lorenzo Stoakes <ljs@kernel.org>,
"Liam R. Howlett" <liam@infradead.org>,
Vlastimil Babka <vbabka@kernel.org>,
Mike Rapoport <rppt@kernel.org>,
Suren Baghdasaryan <surenb@google.com>,
Michal Hocko <mhocko@suse.com>,
Andrew Morton <akpm@linux-foundation.org>,
David Hildenbrand <david@kernel.org>,
Rik van Riel <riel@surriel.com>,
Yuan-Hao Hsu <aa9736195201@gmail.com>,
Yeoreum Yun <yeoreum.yun@arm.com>
Subject: [PATCH v3 5/6] mm: gup: introduce gup_unshared_pages()
Date: Mon, 09 Nov 2026 12:32:29 +0000 [thread overview]
Message-ID: <20261109-refactor_gup_fast-v3-5-2aaef02df0e1@arm.com> (raw)
In-Reply-To: <20261109-refactor_gup_fast-v3-0-2aaef02df0e1@arm.com>
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
next prev parent reply other threads:[~2026-11-09 12:32 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` [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 ` Yeoreum Yun [this message]
2026-11-09 12:32 ` [PATCH v3 6/6] mm: gup: batch PTE-mapped large folios in gup_fast_pte_range() Yeoreum Yun
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=20261109-refactor_gup_fast-v3-5-2aaef02df0e1@arm.com \
--to=yeoreum.yun@arm.com \
--cc=aa9736195201@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=david@kernel.org \
--cc=jgg@ziepe.ca \
--cc=jhubbard@nvidia.com \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=mhocko@suse.com \
--cc=peterx@redhat.com \
--cc=riel@surriel.com \
--cc=rppt@kernel.org \
--cc=surenb@google.com \
--cc=vbabka@kernel.org \
/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®