Skip to contentSkip to conversation

mm/migrate: look up consecutive pages together in do_pages_stat_array()

PATCH Superseded Qiliang Yuan 2 participants 3 messages Oct 1 – Oct 2

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/migrate: look up consecutive pages together in do_pages_stat_array()

move_pages() with a NULL node list reports the node of each page. RDMA and KV-cache transfer engines use it to find where large registered buffers live, querying every 4K page of buffers that span hundreds of gigabytes.

do_pages_stat_array() looks up the VMA and walks the page tables from the top for every address, taking and dropping the PTE lock each time. That costs about 105 ns per page, so a 16 GiB buffer takes 440 ms.

Callers almost always pass consecutive addresses. Reuse the VMA while the address stays inside it. After folio_walk_start() locks a PTE table or a PMD-mapped folio, answer the following addresses under the same PMD from that table or folio before releasing the lock. Other addresses still take the existing path.

On 7.3-rc5 in a 16-vCPU VM, querying every page of a populated buffer:

                       before       after
  4K pages, per page   105 ns       22.7 ns
  THP, per page        92 ns        13.1 ns
  16 GiB, 4K pages     439 ms       95 ms
---
 mm/migrate.c | 83 ++++++++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 61 insertions(+), 22 deletions(-)
mm/migrate.c +61 -22
index 15b45832bcfa7..5f470dc7dc746 100644
@@ -2451,44 +2451,83 @@ static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
 	return err;
 }
 
+static int folio_stat(struct folio *folio)
+{
+	if (is_zero_folio(folio) || is_huge_zero_folio(folio))
+		return -EFAULT;
+	if (folio_is_zone_device(folio))
+		return -ENOENT;
+	return folio_nid(folio);
+}
+
+/* Look up a PTE in a locked page table the way folio_walk_start() does. */
+static int pte_stat(struct vm_area_struct *vma, unsigned long addr,
+		    pte_t *ptep)
+{
+	pte_t pte = ptep_get(ptep);
+	struct page *page;
+
+	if (!pte_present(pte))
+		return -ENOENT;
+	page = vm_normal_page(vma, addr, pte);
+	if (page)
+		return folio_stat(page_folio(page));
+	if (is_zero_pfn(pte_pfn(pte)))
+		return -EFAULT;
+	return -ENOENT;
+}
+
 /*
  * Determine the nodes of an array of pages and store it in an array of status.
  */
 static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
 				const void __user **pages, int *status)
 {
-	unsigned long i;
+	struct vm_area_struct *vma = NULL;
+	unsigned long i = 0;
 
 	mmap_read_lock(mm);
 
-	for (i = 0; i < nr_pages; i++) {
-		unsigned long addr = (unsigned long)(*pages);
-		struct vm_area_struct *vma;
+	while (i < nr_pages) {
+		unsigned long addr = (unsigned long)pages[i];
+		unsigned long next, end;
 		struct folio_walk fw;
 		struct folio *folio;
-		int err = -EFAULT;
+		pte_t *ptep;
 
-		vma = vma_lookup(mm, addr);
-		if (!vma)
-			goto set_status;
+		if (!vma || addr < vma->vm_start || addr >= vma->vm_end)
+			vma = vma_lookup(mm, addr);
+		if (!vma) {
+			status[i++] = -EFAULT;
+			continue;
+		}
 
 		folio = folio_walk_start(&fw, vma, addr, FW_ZEROPAGE);
-		if (folio) {
-			if (is_zero_folio(folio) || is_huge_zero_folio(folio))
-				err = -EFAULT;
-			else if (folio_is_zone_device(folio))
-				err = -ENOENT;
-			else
-				err = folio_nid(folio);
-			folio_walk_end(&fw, vma);
-		} else {
-			err = -ENOENT;
+		if (!folio) {
+			status[i++] = -ENOENT;
+			continue;
 		}
-set_status:
-		*status = err;
+		status[i++] = folio_stat(folio);
 
-		pages++;
-		status++;
+		/*
+		 * Callers usually pass consecutive pages. Answer those that
+		 * fall under the entry or page table we already hold locked
+		 * instead of walking the page tables again for each of them.
+		 */
+		end = pmd_addr_end(addr, vma->vm_end);
+		ptep = fw.ptep;
+		for (next = addr + PAGE_SIZE; i < nr_pages && next < end;
+		     next += PAGE_SIZE, i++) {
+			if ((unsigned long)pages[i] != next)
+				break;
+			if (fw.level == FW_LEVEL_PMD)
+				status[i] = status[i - 1];
+			else if (fw.level == FW_LEVEL_PTE)
+				status[i] = pte_stat(vma, next, ++ptep);
+			else
+				break;
+		}
+		folio_walk_end(&fw, vma);
 	}
 
 	mmap_read_unlock(mm);

---
base-commit: 551c722f40809618230001baccf219193e22fc5a
change-id: 20261001-bug-mm-move-pages-stat-batch-f62a29ea5867

Best regards,

Signature · 1 line
Qiliang Yuan <[email protected]>
reviewer
#1 lore raw
Quoted 9 lines · Qiliang Yuan

move_pages() with a NULL node list reports the node of each page. RDMA and KV-cache transfer engines use it to find where large registered buffers live, querying every 4K page of buffers that span hundreds of gigabytes.

do_pages_stat_array() looks up the VMA and walks the page tables from the top for every address, taking and dropping the PTE lock each time. That costs about 105 ns per page, so a 16 GiB buffer takes 440 ms.

Quoted 5 lines

Callers almost always pass consecutive addresses. Reuse the VMA while the address stays inside it. After folio_walk_start() locks a PTE table or a PMD-mapped folio, answer the following addresses under the same PMD from that table or folio before releasing the lock. Other addresses still take the existing path.

Instead of adding customized batched walk code, it might be better to use walk_page_range() + pmd_entry callback. See mincore_pte_range() for reference.

BTW, DO_PAGES_STAT_CHUNK_NR is limited at 16, so the batching benefit comes from 16 pages instead of 512 pages in one PMD? I wonder if that limit could be raised and how much more benefit we will see.

Quoted 128 lines

On 7.3-rc5 in a 16-vCPU VM, querying every page of a populated buffer:

                       before       after
  4K pages, per page   105 ns       22.7 ns
  THP, per page        92 ns        13.1 ns
  16 GiB, 4K pages     439 ms       95 ms
---
 mm/migrate.c | 83 ++++++++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 61 insertions(+), 22 deletions(-)
mm/migrate.c +61 -23
index 15b45832bcfa7..5f470dc7dc746 100644
@@ -2451,44 +2451,83 @@ static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
 	return err;
 }
 
+static int folio_stat(struct folio *folio)
+{
+	if (is_zero_folio(folio) || is_huge_zero_folio(folio))
+		return -EFAULT;
+	if (folio_is_zone_device(folio))
+		return -ENOENT;
+	return folio_nid(folio);
+}
+
+/* Look up a PTE in a locked page table the way folio_walk_start() does. */
+static int pte_stat(struct vm_area_struct *vma, unsigned long addr,
+		    pte_t *ptep)
+{
+	pte_t pte = ptep_get(ptep);
+	struct page *page;
+
+	if (!pte_present(pte))
+		return -ENOENT;
+	page = vm_normal_page(vma, addr, pte);
+	if (page)
+		return folio_stat(page_folio(page));
+	if (is_zero_pfn(pte_pfn(pte)))
+		return -EFAULT;
+	return -ENOENT;
+}
+
 /*
  * Determine the nodes of an array of pages and store it in an array of status.
  */
 static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
 				const void __user **pages, int *status)
 {
-	unsigned long i;
+	struct vm_area_struct *vma = NULL;
+	unsigned long i = 0;
 
 	mmap_read_lock(mm);
 
-	for (i = 0; i < nr_pages; i++) {
-		unsigned long addr = (unsigned long)(*pages);
-		struct vm_area_struct *vma;
+	while (i < nr_pages) {
+		unsigned long addr = (unsigned long)pages[i];
+		unsigned long next, end;
 		struct folio_walk fw;
 		struct folio *folio;
-		int err = -EFAULT;
+		pte_t *ptep;
 
-		vma = vma_lookup(mm, addr);
-		if (!vma)
-			goto set_status;
+		if (!vma || addr < vma->vm_start || addr >= vma->vm_end)
+			vma = vma_lookup(mm, addr);
+		if (!vma) {
+			status[i++] = -EFAULT;
+			continue;
+		}
 
 		folio = folio_walk_start(&fw, vma, addr, FW_ZEROPAGE);
-		if (folio) {
-			if (is_zero_folio(folio) || is_huge_zero_folio(folio))
-				err = -EFAULT;
-			else if (folio_is_zone_device(folio))
-				err = -ENOENT;
-			else
-				err = folio_nid(folio);
-			folio_walk_end(&fw, vma);
-		} else {
-			err = -ENOENT;
+		if (!folio) {
+			status[i++] = -ENOENT;
+			continue;
 		}
-set_status:
-		*status = err;
+		status[i++] = folio_stat(folio);
 
-		pages++;
-		status++;
+		/*
+		 * Callers usually pass consecutive pages. Answer those that
+		 * fall under the entry or page table we already hold locked
+		 * instead of walking the page tables again for each of them.
+		 */
+		end = pmd_addr_end(addr, vma->vm_end);
+		ptep = fw.ptep;
+		for (next = addr + PAGE_SIZE; i < nr_pages && next < end;
+		     next += PAGE_SIZE, i++) {
+			if ((unsigned long)pages[i] != next)
+				break;
+			if (fw.level == FW_LEVEL_PMD)
+				status[i] = status[i - 1];
+			else if (fw.level == FW_LEVEL_PTE)
+				status[i] = pte_stat(vma, next, ++ptep);
+			else
+				break;
+		}
+		folio_walk_end(&fw, vma);
 	}
 
 	mmap_read_unlock(mm);
 
---

base-commit: 551c722f40809618230001baccf219193e22fc5a
change-id: 20261001-bug-mm-move-pages-stat-batch-f62a29ea5867

Quoted 3 lines

Best regards,

Signature · 1 line
Qiliang Yuan <[email protected]>

Best Regards,
Yan, Zi

#2 lore raw
Quoted 3 lines · Zi Yan

Instead of adding customized batched walk code, it might be better to use walk_page_range() + pmd_entry callback. See mincore_pte_range() for reference.

Thanks, v2 does that, with pud_entry and hugetlb_entry callbacks so the walk doesn't split PUD or hugetlb leaves.

Quoted 3 lines

BTW, DO_PAGES_STAT_CHUNK_NR is limited at 16, so the batching benefit comes from 16 pages instead of 512 pages in one PMD? I wonder if that limit could be raised and how much more benefit we will see.

Yes. With runs walked in one go, the walk setup paid every 16 pages dominates. Per page, querying a populated 4 GiB buffer:

                16        64        256       512       1024
  4K pages      31.9 ns   15.0 ns   11.8 ns   11.2 ns   10.9 ns
  THP           23.1 ns   6.6 ns    2.5 ns    1.8 ns    1.6 ns

v2 raises the chunk to 512 in a separate patch, which also allocates the arrays instead of keeping 6 KiB on the stack:

https://lore.kernel.org/r/20261002-bug-mm-move-pages-stat-batch-v2-0-f73b5d20519f@gmail.com

Qiliang