Re: [PATCH 1/9] mm/gup: break out gup_fill_pages() helper
From: David Hildenbrand (Arm)
Date: Thu Nov 26 2026 - 08:40:51 EST
On 11/23/26 20:56, Rik van Riel wrote:
> __get_user_pages() fills pages[] in an open-coded loop, and the
> GUP-fast PTE, PMD and PUD leaf paths each have their own copy of
> the same loop.
>
> Move the store into a gup_fill_pages() helper, shared by the slow
> and the fast paths. The helper keeps a pages==NULL check, so the
> fault-in users that pass no pages array (faultin_page_range(),
> populate_vma_page_range()) need no conditionals at their call
> sites. Cache flushing stays open-coded for now; it moves into its
> own helper in the next patch.
>
> No functional changes intended.
>
> Suggested-by: David Hildenbrand <david@xxxxxxxxxx>
> Signed-off-by: Rik van Riel <riel@xxxxxxxxxxx>
> ---
> mm/gup.c | 35 +++++++++++++++++++++++++----------
> 1 file changed, 25 insertions(+), 10 deletions(-)
>
> diff --git a/mm/gup.c b/mm/gup.c
> index b0a0019de80a..e0698d2075b4 100644
> --- a/mm/gup.c
> +++ b/mm/gup.c
> @@ -710,6 +710,25 @@ static inline bool gup_must_unshare(struct vm_area_struct *vma,
> return gup_pages_not_requiring_unshare(vma, flags, folio, page, 1) != 1;
> }
>
> +/*
> + * Store @nr_pages consecutive pages starting at @page into @pages.
> + * The caller must hold at least one reference on the folio, so it
> + * cannot go away, or get split while the array is filled.
> + *
> + * Some GUP users only fault pages in and pass no pages array.
> + */
Drop all these comments, they don't add a lot of value within this file.
Any implementation details should be commented in-line, but also that's not
required here.
> +static void gup_fill_pages(struct page **pages, struct page *page,
> + unsigned long nr_pages)
> +{
> + unsigned long i;
> +
> + if (!pages)
> + return;
> +
> + for (i = 0; i < nr_pages; i++)
> + pages[i] = page + i;
> +}
> +
> #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,
> @@ -1579,9 +1598,9 @@ static long __get_user_pages(struct mm_struct *mm,
> }
> }
>
> + gup_fill_pages(pages + i, page, page_increm);
> for (j = 0; j < page_increm; j++) {
> subpage = page + j;
> - pages[i + j] = subpage;
> flush_anon_page(vma, subpage, start + j * PAGE_SIZE);
> flush_dcache_page(subpage);
Ok, two loops. I assume on architectures we care about that would be optimized
out completely either way.
Reviewed-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
--
Cheers,
David