Thread (71 messages) 71 messages, 10 authors, 2025-10-09

Re: [PATCH v2 19/37] mm/gup: remove record_subpages()

From: Jens Axboe <axboe@kernel.dk>
Date: 2025-09-05 11:26:56
Also in: dri-devel, intel-gfx, io-uring, kvm, linux-arm-kernel, linux-crypto, linux-ide, linux-iommu, linux-kselftest, linux-mips, linux-mm, linux-mmc, linux-riscv, linux-s390, linux-scsi, lkml, virtualization

On 9/5/25 12:41 AM, David Hildenbrand wrote:
quoted hunk ↗ jump to hunk
On 01.09.25 17:03, David Hildenbrand wrote:
quoted
We can just cleanup the code by calculating the #refs earlier,
so we can just inline what remains of record_subpages().

Calculate the number of references/pages ahead of times, and record them
only once all our tests passed.

Signed-off-by: David Hildenbrand <redacted>
---
  mm/gup.c | 25 ++++++++-----------------
  1 file changed, 8 insertions(+), 17 deletions(-)
diff --git a/mm/gup.c b/mm/gup.c
index c10cd969c1a3b..f0f4d1a68e094 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -484,19 +484,6 @@ static inline void mm_set_has_pinned_flag(struct mm_struct *mm)
  #ifdef CONFIG_MMU
    #ifdef CONFIG_HAVE_GUP_FAST
-static int record_subpages(struct page *page, unsigned long sz,
-               unsigned long addr, unsigned long end,
-               struct page **pages)
-{
-    int nr;
-
-    page += (addr & (sz - 1)) >> PAGE_SHIFT;
-    for (nr = 0; addr != end; nr++, addr += PAGE_SIZE)
-        pages[nr] = page++;
-
-    return nr;
-}
-
  /**
   * try_grab_folio_fast() - Attempt to get or pin a folio in fast path.
   * @page:  pointer to page to be grabbed
@@ -2967,8 +2954,8 @@ static int gup_fast_pmd_leaf(pmd_t orig, pmd_t *pmdp, unsigned long addr,
      if (pmd_special(orig))
          return 0;
  -    page = pmd_page(orig);
-    refs = record_subpages(page, PMD_SIZE, addr, end, pages + *nr);
+    refs = (end - addr) >> PAGE_SHIFT;
+    page = pmd_page(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
        folio = try_grab_folio_fast(page, refs, flags);
      if (!folio)
@@ -2989,6 +2976,8 @@ static int gup_fast_pmd_leaf(pmd_t orig, pmd_t *pmdp, unsigned long addr,
      }
        *nr += refs;
+    for (; refs; refs--)
+        *(pages++) = page++;
      folio_set_referenced(folio);
      return 1;
  }
@@ -3007,8 +2996,8 @@ static int gup_fast_pud_leaf(pud_t orig, pud_t *pudp, unsigned long addr,
      if (pud_special(orig))
          return 0;
  -    page = pud_page(orig);
-    refs = record_subpages(page, PUD_SIZE, addr, end, pages + *nr);
+    refs = (end - addr) >> PAGE_SHIFT;
+    page = pud_page(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
        folio = try_grab_folio_fast(page, refs, flags);
      if (!folio)
@@ -3030,6 +3019,8 @@ static int gup_fast_pud_leaf(pud_t orig, pud_t *pudp, unsigned long addr,
      }
        *nr += refs;
+    for (; refs; refs--)
+        *(pages++) = page++;
      folio_set_referenced(folio);
      return 1;
  }
Okay, this code is nasty. We should rework this code to just return the nr and receive a the proper
pages pointer, getting rid of the "*nr" parameter.

For the time being, the following should do the trick:

commit bfd07c995814354f6b66c5b6a72e96a7aa9fb73b (HEAD -> nth_page)
Author: David Hildenbrand [off-list ref]
Date:   Fri Sep 5 08:38:43 2025 +0200

    fixup: mm/gup: remove record_subpages()
        pages is not adjusted by the caller, but idnexed by existing *nr.
        Signed-off-by: David Hildenbrand [off-list ref]
diff --git a/mm/gup.c b/mm/gup.c
index 010fe56f6e132..22420f2069ee1 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -2981,6 +2981,7 @@ static int gup_fast_pmd_leaf(pmd_t orig, pmd_t *pmdp, unsigned long addr,
                return 0;
        }
 
+       pages += *nr;
        *nr += refs;
        for (; refs; refs--)
                *(pages++) = page++;
@@ -3024,6 +3025,7 @@ static int gup_fast_pud_leaf(pud_t orig, pud_t *pudp, unsigned long addr,
                return 0;
        }
 
+       pages += *nr;
        *nr += refs;
        for (; refs; refs--)
                *(pages++) = page++;
Tested as fixing the issue for me, thanks.

-- 
Jens Axboe
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help