[PATCH 1/5] mm/gup: add struct gup_out output descriptor
From: Swarna Prabhu
Date: Wed Nov 18 2026 - 16:36:59 EST
From: Swarna Prabhu <s.prabhu@xxxxxxxxxxx>
The GUP page table walkers hand the pages they grabbed back through a
pages[] pointer that every level bumps on its own, which ties them to
reporting memory as an array of pages.
Add 'struct gup_out', a descriptor the walkers can append to instead,
along with 'gup_out_add_range()' to record consecutive pages of a single
folio. The descriptor keeps count of the pages recorded so far, so a
walker no longer has to care where in the array it is, no matter how
often it is called for a single GUP request.
The @folio argument of 'gup_out_add_range()' is not used yet, it is
there for describing memory by its folio later in the series.
No functional change.
Signed-off-by: Swarna Prabhu <s.prabhu@xxxxxxxxxxx>
---
mm/gup.c | 39 +++++++++++++++++++++++++++++++++++++++
1 file changed, 39 insertions(+)
diff --git a/mm/gup.c b/mm/gup.c
index 3b3888578a7e..0433579ed618 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -482,6 +482,45 @@ static inline void mm_set_has_pinned_flag(struct mm_struct *mm)
mm_flags_set(MMF_HAS_PINNED, mm);
}
+/*
+ * Output descriptor of the GUP page table walkers.
+ *
+ * GUP hands the memory it grabbed back to the caller as an array of pages
+ * (@pages).
+ *
+ * @nr_pages is the number of pages recorded so far: the walkers simply keep
+ * appending to the descriptor, no matter how often they are called for a
+ * single GUP request.
+ */
+struct gup_out {
+ struct page **pages;
+ unsigned long nr_pages;
+};
+
+#define GUP_OUT_PAGES(_pages) ((struct gup_out){ .pages = (_pages) })
+
+/**
+ * gup_out_add_range() - record consecutive pages of a single folio
+ * @out: the output descriptor
+ * @folio: the folio @page belongs to
+ * @page: the first page to record
+ * @nr_pages: the number of consecutive pages to record
+ *
+ * The caller must hold a reference (FOLL_GET) or pin (FOLL_PIN) for each of
+ * the @nr_pages pages.
+ */
+static inline void gup_out_add_range(struct gup_out *out, struct folio *folio,
+ struct page *page, unsigned long nr_pages)
+{
+ struct page **pages = out->pages + out->nr_pages;
+ unsigned long i;
+
+ for (i = 0; i < nr_pages; i++)
+ pages[i] = page + i;
+
+ out->nr_pages += nr_pages;
+}
+
#ifdef CONFIG_MMU
#ifdef CONFIG_HAVE_GUP_FAST
--
2.53.0