[PATCH v5 2/8] mm/gup: break out gup_flush_pages() helper

From: Rik van Riel

Date: Mon Nov 16 2026 - 16:01:55 EST


__get_user_pages() flushes each page's caches in an open-coded loop
right after filling the array.

Move it into a gup_flush_pages() helper.

No functional changes intended.

Suggested-by: David Hildenbrand <david@xxxxxxxxxx>
Signed-off-by: Rik van Riel <riel@xxxxxxxxxxx>
---
mm/gup.c | 27 +++++++++++++++++++--------
1 file changed, 19 insertions(+), 8 deletions(-)

diff --git a/mm/gup.c b/mm/gup.c
index e0698d2075b4..d35fedbce7e1 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -729,6 +729,24 @@ static void gup_fill_pages(struct page **pages, struct page *page,
pages[i] = page + i;
}

+/*
+ * Flush the caches for @nr_pages pages stored in @pages. The caller
+ * already holds folio references on the pages.
+ */
+static void gup_flush_pages(struct vm_area_struct *vma, unsigned long address,
+ struct page **pages, unsigned long nr_pages)
+{
+ unsigned long i;
+
+ if (!pages)
+ return;
+
+ for (i = 0; i < nr_pages; i++) {
+ flush_anon_page(vma, pages[i], address + i * PAGE_SIZE);
+ flush_dcache_page(pages[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,
@@ -1566,9 +1584,6 @@ static long __get_user_pages(struct mm_struct *mm,
page_increm = nr_pages;

if (pages) {
- struct page *subpage;
- unsigned int j;
-
/*
* This must be a large folio (and doesn't need to
* be the whole folio; it can be part of it), do
@@ -1599,11 +1614,7 @@ 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;
- flush_anon_page(vma, subpage, start + j * PAGE_SIZE);
- flush_dcache_page(subpage);
- }
+ gup_flush_pages(vma, start, pages + i, page_increm);
}

i += page_increm;
--
2.56.0