Re: [PATCH 3/9] mm/gup: convert follow_page_mask() to return a long

From: David Hildenbrand (Arm)

Date: Thu Nov 26 2026 - 09:04:47 EST


On 11/23/26 20:56, Rik van Riel wrote:
> follow_page_mask() and its helpers return a struct page pointer: NULL,
> ERR_PTR(), or the page found. Change the return type to long instead:
> 0, a negative errno, or 1 with the page stored in a new pages[0] slot.
>
> This lets the return type carry a page count rather than a single
> struct page pointer.

You can say here that we now make the interface match gup-fast.

>
> follow_huge_pud(), follow_huge_pmd() and follow_page_pte() now store
> the found page in a pages[0] slot. __get_user_pages() reads that page
> back from pages[i], fills the rest of a large folio's run itself, and
> flushes the whole run's caches with gup_flush_pages().

You can say here that we will rework that next, to collect all folio pages from
inside these functions directly.

> The follow path
> itself never flushes: the caches are always flushed from
> __get_user_pages(), without any page table lock held.
>
> The vsyscall gate area bypasses follow_page_mask() and fills and flushes
> its own slot the same way. The gate handles a single page, so it
> initializes the count to 1.
>
> *page_mask and __get_user_pages()'s handling of a large folio's
> remaining subpages are untouched, and mm/gup_test.c

As willy keeps saying, there is no such thing as subpages.

"a large folio's remainin pages"

> (PIN_LONGTERM_BENCHMARK) shows no measurable difference for 4 kB,
> 64 kB mTHP, or 2 MB THP.
>
> No functional changes intended.
>
> Suggested-by: David Hildenbrand <david@xxxxxxxxxx>
> Signed-off-by: Rik van Riel <riel@xxxxxxxxxxx>
> ---


[...]>
> /* first iteration or cross vma bound */
> if (!vma || start >= vma->vm_end) {
> @@ -1523,8 +1536,11 @@ static long __get_user_pages(struct mm_struct *mm,
> pages ? &page : NULL);
> if (ret)
> goto out;
> - page_mask = 0;
> - goto next_page;
> + if (pages)
> + pages[i] = page;
> + nr = 1;

Why not use gup_fill_pages() here?

But why do we goto fill_flush where we do another gup_fill_pages?

> + page_increm = 1;
> + goto fill_flush;
> }
>
> if (!vma) {
> @@ -1546,10 +1562,11 @@ static long __get_user_pages(struct mm_struct *mm,
> }
> cond_resched();
>
> - page = follow_page_mask(vma, start, gup_flags, &page_mask);
> - if (!page || PTR_ERR(page) == -EMLINK) {
> + nr = follow_page_mask(vma, start, gup_flags, &page_mask,
> + pages ? &pages[i] : NULL);
> + if (!nr || nr == -EMLINK) {
> ret = faultin_page(vma, start, gup_flags,
> - PTR_ERR(page) == -EMLINK, locked);
> + nr == -EMLINK, locked);
> switch (ret) {
> case 0:
> goto retry;
> @@ -1563,7 +1580,7 @@ static long __get_user_pages(struct mm_struct *mm,
> goto out;
> }
> BUG();
> - } else if (PTR_ERR(page) == -EEXIST) {
> + } else if (nr == -EEXIST) {
> /*
> * Proper page table entry exists, but no corresponding
> * struct page. If the caller expects **pages to be
> @@ -1571,49 +1588,56 @@ static long __get_user_pages(struct mm_struct *mm,
> * for this page.
> */
> if (pages) {
> - ret = PTR_ERR(page);
> + ret = nr;
> goto out;
> }
> - } else if (IS_ERR(page)) {
> - ret = PTR_ERR(page);
> + } else if (nr < 0) {
> + ret = nr;
> goto out;
> }
> -next_page:
> +
> page_increm = 1 + (~(start >> PAGE_SHIFT) & page_mask);
> if (page_increm > nr_pages)
> page_increm = nr_pages;
>
> - if (pages) {
> + /*
> + * This must be a large folio (and doesn't need to
> + * be the whole folio; it can be part of it), do
> + * the refcount work for the remaining pages.
> + *
> + * NOTE: here the page may not be the head page
> + * e.g. when start addr is not thp-size aligned.
> + * try_grab_folio() should have taken care of tail
> + * pages.
> + */
> + if (pages && page_increm > 1) {
> + struct folio *folio = page_folio(pages[i]);
> +
> /*
> - * This must be a large folio (and doesn't need to
> - * be the whole folio; it can be part of it), do
> - * the refcount work for all the subpages too.
> - *
> - * NOTE: here the page may not be the head page
> - * e.g. when start addr is not thp-size aligned.
> - * try_grab_folio() should have taken care of tail
> - * pages.
> + * Since we already hold refcount on the
> + * large folio, this should never fail.

This code recently changed slightly:

commit 2d9bcd380585abc436f055fbb618b34f98882004
Author: David Hildenbrand (Arm) <david@xxxxxxxxxx>
Date: Thu Nov 12 23:53:24 2026 +0100

mm/gup: limit pagecache folio refcount when grabbing references

So once you rebase on top of mm-core/for-next, there will be some smaller
conflcits but nothign earth shattering.


There is a lot happening in this patch. I wonder if there would be some way to
convert some of the functions before converting follow_page_mask().

--
Cheers,

David