From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Yeoreum Yun <yeoreum.yun@arm.com>,
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>,
Rik van Riel <riel@surriel.com>,
Yuan-Hao Hsu <aa9736195201@gmail.com>
Subject: Re: [PATCH v2 1/4] mm: gup: introduce gup_unshared_pages()
Date: Mon, 2 Nov 2026 13:18:05 +0100 [thread overview]
Message-ID: <5feb82ef-a9bc-4ef3-b056-9732563e6225@kernel.org> (raw)
In-Reply-To: <20261102-refactor_gup_fast-v2-1-bc1ac018c834@arm.com>
On 11/2/26 12:30, Yeoreum Yun wrote:
> 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 | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++------
> mm/internal.h | 49 ++++++------------------------------------
> 2 files changed, 70 insertions(+), 48 deletions(-)
>
> diff --git a/mm/gup.c b/mm/gup.c
> index 7d43ce717b246..49681f63950ec 100644
> --- a/mm/gup.c
> +++ b/mm/gup.c
> @@ -633,6 +633,59 @@ static struct page *no_page_table(struct vm_area_struct *vma,
> return NULL;
> }
>
> +unsigned long gup_unshared_pages(struct vm_area_struct *vma, unsigned int flags,
> + struct folio *folio, struct page *page,
> + unsigned long nr_pages)
Two tab indent.
This function should be static.
As a prerequisite patch you should move gup_unshared_pages() into this file.
$ git grep gup_must_unshare
include/linux/rmap.h: * gup_must_unshare().
include/linux/rmap.h: * gup_must_unshare().
mm/gup.c: if (!pud_write(pud) && gup_must_unshare(vma, flags, page))
mm/gup.c: if (!pmd_write(pmdval) && gup_must_unshare(vma, flags, page))
mm/gup.c: if (!pte_write(pte) && gup_must_unshare(vma, flags, page)) {
mm/gup.c: if (!pte_write(pte) && gup_must_unshare(NULL, flags, page)) {
mm/gup.c: if (!pmd_write(orig) && gup_must_unshare(NULL, flags, &folio->page)) {
mm/gup.c: if (!pud_write(orig) && gup_must_unshare(NULL, flags, &folio->page)) {
mm/internal.h:static inline bool gup_must_unshare(struct vm_area_struct *vma,
I'm not 100% happy about the function name due to the !folio_test_anon()+!FOLL_LONGTERM
path, but cannot think of something better.
> +{
> + 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 nr_pages;
> + /*
> + * Note: PageAnon(page) is stable until the page is actually getting
You are not calling PageAnon() anymore.
/* folio_test_anon(folio) is stable until folio gets freed. */
> + * 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 nr_pages;
> +
> + /* We really need the vma ... */
> + 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 nr_pages;
> + }
> +
> + /* 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.
> + */
> + for (i = 0; i < nr_pages; i++) {
> + if (!PageAnonExclusive(page + i))
> + return i;
> + }
> +
> + return nr_pages;
> +}
> +
> #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,
> @@ -668,7 +721,8 @@ static struct page *follow_huge_pud(struct vm_area_struct *vma,
> pfn += (addr & ~PUD_MASK) >> PAGE_SHIFT;
> page = pfn_to_page(pfn);
>
> - if (!pud_write(pud) && gup_must_unshare(vma, flags, page))
> + if (!pud_write(pud) &&
> + gup_must_unshare(vma, flags, page_folio(page), page))
> return ERR_PTR(-EMLINK);
There is already another instance of page_folio(page) int his file. Use a local
variable and have one folio lookup. (that's the whole reason for passing a folio)
>
> ret = try_grab_folio(page_folio(page), 1, flags);
> @@ -722,7 +776,8 @@ 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, page_folio(page), page))
> return ERR_PTR(-EMLINK);
Dito.
>
> VM_WARN_ON_ONCE_PAGE((flags & FOLL_PIN) && PageAnon(page) &&
> @@ -846,7 +901,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;
> }
> @@ -2892,7 +2947,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;
> }
> @@ -2969,7 +3024,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, &folio->page)) {
This only works because we don't have PMD mappings of things that are larger than PMDs.
While at it, pass the proper page.
> gup_put_folio(folio, nr_pages, flags);
> return 0;
> }
> @@ -3011,7 +3067,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, &folio->page)) {
Dito.
> gup_put_folio(folio, nr_pages, flags);
> return 0;
> }
> diff --git a/mm/internal.h b/mm/internal.h
> index c53c369bf8b5d..35016b1afc8bf 100644
> --- a/mm/internal.h
> +++ b/mm/internal.h
> @@ -1282,6 +1282,10 @@ int migrate_device_coherent_folio(struct folio *folio);
> int __must_check try_grab_folio(struct folio *folio, int refs,
> unsigned int flags);
>
> +unsigned long gup_unshared_pages(struct vm_area_struct *vma, unsigned int flags,
> + struct folio *folio, struct page *page,
> + unsigned long nr_pages);
> +
> /*
> * mm/huge_memory.c
> */
> @@ -1354,51 +1358,12 @@ enum {
> * 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)
> + unsigned int flags,
> + struct folio *folio, struct page *page)
Whatever you touch, please also convert to two-tab indent.
--
Cheers,
David
next prev parent reply other threads:[~2026-11-02 12:18 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-02 11:30 [PATCH v2 0/4] mm: gup: batch PTE-mapped large folios in gup_fast_pte_range() Yeoreum Yun
2026-11-02 11:30 ` [PATCH v2 1/4] mm: gup: introduce gup_unshared_pages() Yeoreum Yun
2026-11-02 12:18 ` David Hildenbrand (Arm) [this message]
2026-11-02 13:45 ` Yeoreum Yun
2026-11-02 11:30 ` [PATCH v2 2/4] mm: internal: fix inconsistent in folio_pte_batch_flags() for shadow stack Yeoreum Yun
2026-11-02 12:10 ` David Hildenbrand (Arm)
2026-11-02 12:58 ` Yeoreum Yun
2026-11-02 14:01 ` David Hildenbrand (Arm)
2026-11-02 14:07 ` Yeoreum Yun
2026-11-02 14:08 ` David Hildenbrand (Arm)
2026-11-02 11:30 ` [PATCH v2 3/4] mm: internal: introduce FPB_LOCKLESS for folio_pte_batch_flags() Yeoreum Yun
2026-11-02 12:23 ` David Hildenbrand (Arm)
2026-11-02 11:30 ` [PATCH v2 4/4] 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=5feb82ef-a9bc-4ef3-b056-9732563e6225@kernel.org \
--to=david@kernel.org \
--cc=aa9736195201@gmail.com \
--cc=akpm@linux-foundation.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 \
--cc=yeoreum.yun@arm.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
all inboxes | Powered by JetHome®