Skip to contentSkip to conversation

mm: convert to walk_page_range_vma() to eliminate find_vma()

PATCH In review Kefeng Wang 4 participants 18 messages Jun 18 – Jun 27

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover mm: convert to walk_page_range_vma() to eliminate find_vma()

walk_page_range() performs a find_vma() lookup on each page table walk. For callers that already hold a valid VMA and operate on a known single-VMA range, this lookup is redundant. Replace walk_page_range() with walk_page_range_vma() where the caller guarantees single-VMA semantics.

v2:
- Address comments from Zi and David
- per-vma optimization is separated out
- Remove unneeded prot_none_test()
- Fix some spells and collect ACK/RB

Kefeng Wang (4):
mm: mincore: use walk_page_range_vma() in do_mincore()
mm: mprotect: use walk_page_range_vma() in mprotect_fixup()
mm: mlock: use walk_page_range_vma() in mlock_vma_pages_range()
mm: migrate_device: use walk_page_range_vma() in migrate_vma_collect()

 mm/migrate_device.c |  2 +-
 mm/mincore.c        | 16 +++++++++++++++-
 mm/mlock.c          |  2 +-
 mm/mprotect.c       |  9 +--------
 4 files changed, 18 insertions(+), 11 deletions(-)
Signature · 1 line
2.27.0
#1 lore raw

PATCH v2 1/4 mm: mincore: use walk_page_range_vma() in do_mincore()

The do_mincore() uses walk_page_range() to walk the page table. Fortunately, the caller always passes start/end that falls within a single VMA, so it's safe to use the walk_page_range_vma() in do_mincore() to eliminate an unnecessary find_vma() lookup.

Unlike walk_page_range(), walk_page_range_vma() does not call walk_page_test(), which handles VM_PFNMAP by invoking ->pte_hole() to skip the page table walk. Without this check, PFNMAP PTEs would be treated as present by mincore_pte_range(), changing the returned residency status. Handle VM_PFNMAP explicitly in do_mincore() to preserve the original behavior.

---
 mm/mincore.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
mm/mincore.c +15 -1
index 296f2e3922b5..0c6731ae6c4d 100644
@@ -259,7 +259,21 @@ static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *v
 		memset(vec, 1, pages);
 		return pages;
 	}
-	err = walk_page_range(vma->vm_mm, addr, end, &mincore_walk_ops, vec);
+
+	/*
+	 * walk_page_range_vma() does not call walk_page_test(), which
+	 * handles VM_PFNMAP VMA by invoking ->pte_hole() to skip the
+	 * page table walk. Without this check, PFNMAP PTEs would be
+	 * treated as present by mincore_pte_range(), changing the returned
+	 * residency status from the historical "not resident" to "resident".
+	 * Handle VM_PFNMAP explicitly to preserve the original behavior.
+	 */
+	if (vma->vm_flags & VM_PFNMAP) {
+		__mincore_unmapped_range(addr, end, vma, vec);
+		return (end - addr) >> PAGE_SHIFT;
+	}
+
+	err = walk_page_range_vma(vma, addr, end, &mincore_walk_ops, vec);
 	if (err < 0)
 		return err;
 	return (end - addr) >> PAGE_SHIFT;
Signature · 1 line
2.27.0
reviewer
#2 lore raw

Re: [PATCH v2 1/4] mm: mincore: use walk_page_range_vma() in do_mincore()

Please CC reviewers properly!

Quoted 7 lines · Kefeng Wang

The do_mincore() uses walk_page_range() to walk the page table. Fortunately, the caller always passes start/end that falls within a single VMA, so it's safe to use the walk_page_range_vma() in do_mincore() to eliminate an unnecessary find_vma() lookup.

Unlike walk_page_range(), walk_page_range_vma() does not call walk_page_test(), which handles VM_PFNMAP by invoking ->pte_hole()

Why not? Can we fix that instead? I really don't like having this open coded in callers. Are there callers of walk_page_range_vma() that expect to look at PFNMAP mappings as well? From what I can see, the callers all seem to operate on folios (and/or anonymous memory).

Quoted 19 lines

to skip the page table walk. Without this check, PFNMAP PTEs would be treated as present by mincore_pte_range(), changing the returned residency status. Handle VM_PFNMAP explicitly in do_mincore() to preserve the original behavior.

---
 mm/mincore.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
mm/mincore.c +0 -0
index 296f2e3922b5..0c6731ae6c4d 100644
@@ -259,7 +259,21 @@ static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *v
 		memset(vec, 1, pages);
 		return pages;
 	}

mm/mincore.c static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *v · line 270

Quoted 10 lines
mm/mincore.c +9 -1
-	err = walk_page_range(vma->vm_mm, addr, end, &mincore_walk_ops, vec);
+
+	/*
+	 * walk_page_range_vma() does not call walk_page_test(), which
+	 * handles VM_PFNMAP VMA by invoking ->pte_hole() to skip the
+	 * page table walk. Without this check, PFNMAP PTEs would be
+	 * treated as present by mincore_pte_range(), changing the returned
+	 * residency status from the historical "not resident" to "resident".
+	 * Handle VM_PFNMAP explicitly to preserve the original behavior.
+	 */

This whole comment looks poised to rot very very quickly.

Quoted 14 lines
mm/mincore.c +6 -0
+	if (vma->vm_flags & VM_PFNMAP) {
+		__mincore_unmapped_range(addr, end, vma, vec);
+		return (end - addr) >> PAGE_SHIFT;
+	}
+
+	err = walk_page_range_vma(vma, addr, end, &mincore_walk_ops, vec);
 	if (err < 0)
 		return err;
 	return (end - addr) >> PAGE_SHIFT;
Signature · 1 line
2.27.0
Signature · 1 line
Pedro
#3 lore raw

Re: [PATCH v2 1/4] mm: mincore: use walk_page_range_vma() in do_mincore()

Quoted 8 lines · Pedro Falcato

Please CC reviewers properly!

Quoted 5 lines · Kefeng Wang

The do_mincore() uses walk_page_range() to walk the page table. Fortunately, the caller always passes start/end that falls within a single VMA, so it's safe to use the walk_page_range_vma() in do_mincore() to eliminate an unnecessary find_vma() lookup.

Quoted 7 lines
Quoted 2 lines

Unlike walk_page_range(), walk_page_range_vma() does not call walk_page_test(), which handles VM_PFNMAP by invoking ->pte_hole()

Why not? Can we fix that instead? I really don't like having this open coded in callers. Are there callers of walk_page_range_vma() that expect to look at PFNMAP mappings as well? From what I can see, the callers all seem to operate on folios (and/or anonymous memory).

I'd rather not.

commit c31783eeae7b22dc3f6edde7339de6112959225d

Author: David Hildenbrand <[email protected]>
Date:   Fri Oct 21 12:11:38 2022 +0200

mm/pagewalk: don't trigger test_walk() in walk_page_vma()

As Peter points out, the caller passes a single VMA and can just do that check itself.

And in fact, no existing users rely on test_walk() getting called. So let's just remove it and make the implementation slightly more efficient.

Signature · 3 lines
Cheers,

David
#4 lore raw

Re: [PATCH v2 1/4] mm: mincore: use walk_page_range_vma() in do_mincore()

In reply to Pedro Falcato
Quoted 2 lines · Pedro Falcato

Please CC reviewers properly!

Oh, I will put more reviewes to cc list.

Quoted 6 lines · Kefeng Wang
Quoted 5 lines

The do_mincore() uses walk_page_range() to walk the page table. Fortunately, the caller always passes start/end that falls within a single VMA, so it's safe to use the walk_page_range_vma() in do_mincore() to eliminate an unnecessary find_vma() lookup.

Quoted 7 lines
Quoted 2 lines

Unlike walk_page_range(), walk_page_range_vma() does not call walk_page_test(), which handles VM_PFNMAP by invoking ->pte_hole()

Why not? Can we fix that instead? I really don't like having this open coded in callers. Are there callers of walk_page_range_vma() that expect to look at PFNMAP mappings as well? From what I can see, the callers all seem to operate on folios (and/or anonymous memory).

As you said, all the other callers don't operate VM_PFNMAP, so we don't
want to add walk_page_test() into walk_page_range_vma(). This hack is to
preserve the original behavior, but as David said[1], we could add a follow-up patch to remove the special handling to see if anyone screams, and this indeed changed some behaviors, so it's better to handle it with
another patch.

[1] https://lore.kernel.org/linux-mm/[email protected]/

Quoted 48 lines
Quoted 20 lines

to skip the page table walk. Without this check, PFNMAP PTEs would be treated as present by mincore_pte_range(), changing the returned residency status. Handle VM_PFNMAP explicitly in do_mincore() to preserve the original behavior.

---
  mm/mincore.c | 16 +++++++++++++++-
  1 file changed, 15 insertions(+), 1 deletion(-)
mm/mincore.c +0 -0
index 296f2e3922b5..0c6731ae6c4d 100644
@@ -259,7 +259,21 @@ static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *v
  		memset(vec, 1, pages);
  		return pages;
  	}

mm/mincore.c static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *v · line 270

Quoted 10 lines
mm/mincore.c +9 -1
-	err = walk_page_range(vma->vm_mm, addr, end, &mincore_walk_ops, vec);
+
+	/*
+	 * walk_page_range_vma() does not call walk_page_test(), which
+	 * handles VM_PFNMAP VMA by invoking ->pte_hole() to skip the
+	 * page table walk. Without this check, PFNMAP PTEs would be
+	 * treated as present by mincore_pte_range(), changing the returned
+	 * residency status from the historical "not resident" to "resident".
+	 * Handle VM_PFNMAP explicitly to preserve the original behavior.
+	 */

This whole comment looks poised to rot very very quickly.

Quoted 14 lines
mm/mincore.c +6 -0
+	if (vma->vm_flags & VM_PFNMAP) {
+		__mincore_unmapped_range(addr, end, vma, vec);
+		return (end - addr) >> PAGE_SHIFT;
+	}
+
+	err = walk_page_range_vma(vma, addr, end, &mincore_walk_ops, vec);
  	if (err < 0)
  		return err;
  	return (end - addr) >> PAGE_SHIFT;
Signature · 1 line
2.27.0
reviewer Reviewed-by
#5 lore raw

Re: [PATCH v2 1/4] mm: mincore: use walk_page_range_vma() in do_mincore()

Quoted 22 lines · Kefeng Wang
Quoted 2 lines · Pedro Falcato

Please CC reviewers properly!

Oh, I will put more reviewes to cc list.

Quoted 6 lines · Kefeng Wang
Quoted 5 lines

The do_mincore() uses walk_page_range() to walk the page table. Fortunately, the caller always passes start/end that falls within a single VMA, so it's safe to use the walk_page_range_vma() in do_mincore() to eliminate an unnecessary find_vma() lookup.

Quoted 7 lines
Quoted 2 lines

Unlike walk_page_range(), walk_page_range_vma() does not call walk_page_test(), which handles VM_PFNMAP by invoking ->pte_hole()

Why not? Can we fix that instead? I really don't like having this open coded in callers. Are there callers of walk_page_range_vma() that expect to look at PFNMAP mappings as well? From what I can see, the callers all seem to operate on folios (and/or anonymous memory).

Quoted 6 lines

As you said, all the other callers don't operate VM_PFNMAP, so we don't want to add walk_page_test() into walk_page_range_vma(). This hack is to preserve the original behavior, but as David said[1], we could add a follow-up patch to remove the special handling to see if anyone screams, and this indeed changed some behaviors, so it's better to handle it with another patch.

Yes, I agree, I'm 95% sure no one is invoking mincore() on PFNMAP mappings.

So, if we're keeping this check for this patch:

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
mm/mincore.c +1 -1
index 296f2e3922b5..0c6731ae6c4d 100644
@@ -259,7 +259,21 @@ static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *v
  		memset(vec, 1, pages);
  		return pages;
  	}
-	err = walk_page_range(vma->vm_mm, addr, end, &mincore_walk_ops, vec);
+
Quoted 8 lines
Quoted 8 lines

mm/mincore.c static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *v · line 270

Quoted 8 lines
mm/mincore.c +8 -0
+	/*
+	 * walk_page_range_vma() does not call walk_page_test(), which
+	 * handles VM_PFNMAP VMA by invoking ->pte_hole() to skip the
+	 * page table walk. Without this check, PFNMAP PTEs would be
+	 * treated as present by mincore_pte_range(), changing the returned
+	 * residency status from the historical "not resident" to "resident".
+	 * Handle VM_PFNMAP explicitly to preserve the original behavior.
+	 */

I would rather we amend this comment to something like:

	/* mincore (historically) reports PFNMAP mappings as non-resident. */

because we don't need to explain internal differences in walk_page_range functions in a random comment in mincore. And perhaps attempt a separate PFNMAP check removal patch as part of the series, or as a follow up (so if it does matter, we can simply revert that patch instead of this conversion).

In any case,

Signature · 1 line
Pedro
#6 lore raw

Re: [PATCH v2 1/4] mm: mincore: use walk_page_range_vma() in do_mincore()

Quoted 56 lines · Pedro Falcato
Quoted 22 lines · Kefeng Wang
Quoted 2 lines · Pedro Falcato

Please CC reviewers properly!

Oh, I will put more reviewes to cc list.

Quoted 6 lines · Kefeng Wang
Quoted 5 lines

The do_mincore() uses walk_page_range() to walk the page table. Fortunately, the caller always passes start/end that falls within a single VMA, so it's safe to use the walk_page_range_vma() in do_mincore() to eliminate an unnecessary find_vma() lookup.

Quoted 7 lines
Quoted 2 lines

Unlike walk_page_range(), walk_page_range_vma() does not call walk_page_test(), which handles VM_PFNMAP by invoking ->pte_hole()

Why not? Can we fix that instead? I really don't like having this open coded in callers. Are there callers of walk_page_range_vma() that expect to look at PFNMAP mappings as well? From what I can see, the callers all seem to operate on folios (and/or anonymous memory).

Quoted 6 lines

As you said, all the other callers don't operate VM_PFNMAP, so we don't want to add walk_page_test() into walk_page_range_vma(). This hack is to preserve the original behavior, but as David said[1], we could add a follow-up patch to remove the special handling to see if anyone screams, and this indeed changed some behaviors, so it's better to handle it with another patch.

Yes, I agree, I'm 95% sure no one is invoking mincore() on PFNMAP mappings.

So, if we're keeping this check for this patch:

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
mm/mincore.c +1 -1
index 296f2e3922b5..0c6731ae6c4d 100644
@@ -259,7 +259,21 @@ static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *v
   		memset(vec, 1, pages);
   		return pages;
   	}
-	err = walk_page_range(vma->vm_mm, addr, end, &mincore_walk_ops, vec);
+
Quoted 8 lines
Quoted 8 lines

mm/mincore.c static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *v · line 270

Quoted 8 lines
mm/mincore.c +8 -0
+	/*
+	 * walk_page_range_vma() does not call walk_page_test(), which
+	 * handles VM_PFNMAP VMA by invoking ->pte_hole() to skip the
+	 * page table walk. Without this check, PFNMAP PTEs would be
+	 * treated as present by mincore_pte_range(), changing the returned
+	 * residency status from the historical "not resident" to "resident".
+	 * Handle VM_PFNMAP explicitly to preserve the original behavior.
+	 */

I would rather we amend this comment to something like:

Quoted 4 lines
	/* mincore (historically) reports PFNMAP mappings as non-resident. */

because we don't need to explain internal differences in walk_page_range functions in a random comment in mincore. And perhaps attempt a separate

Hope Andrew can fix the comments when pickup patches.

Quoted 3 lines

PFNMAP check removal patch as part of the series, or as a follow up (so if it does matter, we can simply revert that patch instead of this conversion).

I will send it separately, along with other mincore optimizations.

Quoted 4 lines

In any case,

Thanks.

maintainer
#7 lore raw

Re: [PATCH v2 1/4] mm: mincore: use walk_page_range_vma() in do_mincore()

Quoted 13 lines · On Mon, 22 Jun 2026 11:23:11 +0800 Kefeng Wang <[email protected]>…
Quoted 13 lines
Quoted 10 lines
Quoted 10 lines

Review

Quoted 10 lines
(quoted diff) +9 -1
-	err = walk_page_range(vma->vm_mm, addr, end, &mincore_walk_ops, vec);
+
+	/*
+	 * walk_page_range_vma() does not call walk_page_test(), which
+	 * handles VM_PFNMAP VMA by invoking ->pte_hole() to skip the
+	 * page table walk. Without this check, PFNMAP PTEs would be
+	 * treated as present by mincore_pte_range(), changing the returned
+	 * residency status from the historical "not resident" to "resident".
+	 * Handle VM_PFNMAP explicitly to preserve the original behavior.
+	 */

I would rather we amend this comment to something like:

Quoted 6 lines
Quoted 4 lines
	/* mincore (historically) reports PFNMAP mappings as non-resident. */

because we don't need to explain internal differences in walk_page_range functions in a random comment in mincore. And perhaps attempt a separate

Hope Andrew can fix the comments when pickup patches.

I added this:

mm/mincore.c renamed from mm/mincore.c~mm-mincore-use-walk_page_range_vma-in-do_mincore-fix +1 -6
@@ -261,12 +261,7 @@ static long do_mincore(unsigned long add
 	}
 
 	/*
-	 * walk_page_range_vma() does not call walk_page_test(), which
-	 * handles VM_PFNMAP VMA by invoking ->pte_hole() to skip the
-	 * page table walk. Without this check, PFNMAP PTEs would be
-	 * treated as present by mincore_pte_range(), changing the returned
-	 * residency status from the historical "not resident" to "resident".
-	 * Handle VM_PFNMAP explicitly to preserve the original behavior.
+	 * mincore historically reports PFNMAP mappings as non-resident.
 	 */
 	if (vma->vm_flags & VM_PFNMAP) {
 		__mincore_unmapped_range(addr, end, vma, vec);

_

Sashiko is OK with your patchset, but it might have found four(!) pre-existing issues:

	https://sashiko.dev/#/patchset/[email protected]
#8 lore raw

Re: [PATCH v2 1/4] mm: mincore: use walk_page_range_vma() in do_mincore()

Quoted 40 lines · Andrew Morton
Quoted 13 lines · On Mon, 22 Jun 2026 11:23:11 +0800 Kefeng Wang <[email protected]>…
Quoted 13 lines
Quoted 10 lines
Quoted 10 lines

Review

Quoted 10 lines
(quoted diff) +9 -1
-	err = walk_page_range(vma->vm_mm, addr, end, &mincore_walk_ops, vec);
+
+	/*
+	 * walk_page_range_vma() does not call walk_page_test(), which
+	 * handles VM_PFNMAP VMA by invoking ->pte_hole() to skip the
+	 * page table walk. Without this check, PFNMAP PTEs would be
+	 * treated as present by mincore_pte_range(), changing the returned
+	 * residency status from the historical "not resident" to "resident".
+	 * Handle VM_PFNMAP explicitly to preserve the original behavior.
+	 */

I would rather we amend this comment to something like:

Quoted 6 lines
Quoted 4 lines
	/* mincore (historically) reports PFNMAP mappings as non-resident. */

because we don't need to explain internal differences in walk_page_range functions in a random comment in mincore. And perhaps attempt a separate

Hope Andrew can fix the comments when pickup patches.

I added this:

mm/mincore.c renamed from mm/mincore.c~mm-mincore-use-walk_page_range_vma-in-do_mincore-fix +1 -6
@@ -261,12 +261,7 @@ static long do_mincore(unsigned long add
  	}
 
  	/*
-	 * walk_page_range_vma() does not call walk_page_test(), which
-	 * handles VM_PFNMAP VMA by invoking ->pte_hole() to skip the
-	 * page table walk. Without this check, PFNMAP PTEs would be
-	 * treated as present by mincore_pte_range(), changing the returned
-	 * residency status from the historical "not resident" to "resident".
-	 * Handle VM_PFNMAP explicitly to preserve the original behavior.
+	 * mincore historically reports PFNMAP mappings as non-resident.
  	 */
  	if (vma->vm_flags & VM_PFNMAP) {
  		__mincore_unmapped_range(addr, end, vma, vec);
Quoted 2 lines

_

Thanks.

Quoted 6 lines

Sashiko is OK with your patchset, but it might have found four(!) pre-existing issues:

	https://sashiko.dev/#/patchset/[email protected]

Sorry for the late, this is pre-existing issue, I am not familiar with zone devices, so just focus on my changes and the subsequent changes related to mincore.. After a quick review, most of them are not issues based on my understanding.

+ CC Alistair and Gregroy for migrate_device.c, also Zi could you help to check it?

1. mlock.c, ACTION_AGAIN
2. migrate_device.c, goto again array overrun / double counting
3. migrate_device.c, softleaf_to_folio VM_WARN
4. migrate_device.c, pte_pfn / pte_dirty on non-present PTE

I think issue 4 is real issue, a quick fix is

mm/migrate_device.c +2 -1
index 554754eb26ff..3ccc4fc1015c 100644
@@ -401,7 +401,8 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
                        bool anon_exclusive;
                        pte_t swp_pte;
 
-                       flush_cache_page(vma, addr, pte_pfn(pte));
+                       if (pte_present(pte))
+                               flush_cache_page(vma, addr, pte_pfn(pte));
                        anon_exclusive = folio_test_anon(folio) &&
                                          PageAnonExclusive(page);
                        if (anon_exclusive) {
@@ -422,8 +423,12 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
                        migrate->cpages++;
 
                        /* Set the dirty flag on the folio now the pte 

is gone. */
- if (pte_dirty(pte))
+ if (pte_present(pte)) {
+ if (pte_dirty(pte))
+ folio_mark_dirty(folio);

+                       } else if (softleaf_is_device_private_write(entry)) {
                                folio_mark_dirty(folio);

+ }

/* Setup special migration page table entry */ if (mpfn & MIGRATE_PFN_WRITE)

But as I said, I am not familiar with it, just a thought.

#9 lore raw

Re: [PATCH v2 1/4] mm: mincore: use walk_page_range_vma() in do_mincore()

Quoted 65 lines · Kefeng Wang
Quoted 23 lines · Andrew Morton
Quoted 2 lines · On Mon, 22 Jun 2026 11:23:11 +0800 Kefeng Wang <[email protected]>…

Hope Andrew can fix the comments when pickup patches.

I added this:

mm/mincore.c renamed from mm/mincore.c~mm-mincore-use-walk_page_range_vma-in-do_mincore-fix +1 -6
@@ -261,12 +261,7 @@ static long do_mincore(unsigned long add
       }
         /*
-     * walk_page_range_vma() does not call walk_page_test(), which
-     * handles VM_PFNMAP VMA by invoking ->pte_hole() to skip the
-     * page table walk. Without this check, PFNMAP PTEs would be
-     * treated as present by mincore_pte_range(), changing the returned
-     * residency status from the historical "not resident" to "resident".
-     * Handle VM_PFNMAP explicitly to preserve the original behavior.
+     * mincore historically reports PFNMAP mappings as non-resident.
        */
       if (vma->vm_flags & VM_PFNMAP) {
           __mincore_unmapped_range(addr, end, vma, vec);
Quoted 2 lines

_

Thanks.

Quoted 7 lines

Sashiko is OK with your patchset, but it might have found four(!) pre-existing issues:

    https://sashiko.dev/#/patchset/20260618092845.3905740-1-
[email protected]

Sorry for the late, this is pre-existing issue, I am not familiar with zone devices, so just focus on my changes and the subsequent changes related to mincore.. After a quick review, most of them are not issues based on my understanding.

+ CC Alistair and Gregroy for migrate_device.c, also Zi could you help to check it?

1. mlock.c, ACTION_AGAIN
2. migrate_device.c, goto again array overrun / double counting
3. migrate_device.c, softleaf_to_folio VM_WARN
4. migrate_device.c, pte_pfn / pte_dirty on non-present PTE

I think issue 4 is real issue, a quick fix is

mm/migrate_device.c +2 -1
index 554754eb26ff..3ccc4fc1015c 100644
@@ -401,7 +401,8 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
                         bool anon_exclusive;
                         pte_t swp_pte;
 
-                       flush_cache_page(vma, addr, pte_pfn(pte));
+                       if (pte_present(pte))
+                               flush_cache_page(vma, addr, pte_pfn(pte));
Quoted 2 lines

                        anon_exclusive = folio_test_anon(folio) && PageAnonExclusive(page);

That looks reasonable.

mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 431

Quoted 12 lines

                        if (anon_exclusive) {

mm/migrate_device.c +5 -1
@@ -422,8 +423,12 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
                         migrate->cpages++;
 
                         /* Set the dirty flag on the folio now the pte is gone. */
-                       if (pte_dirty(pte))
+                       if (pte_present(pte)) {
+                               if (pte_dirty(pte))
+                                       folio_mark_dirty(folio);
+                       } else if (softleaf_is_device_private_write(entry)) {
                                 folio_mark_dirty(folio);
+                       }

The pte_presnt() check looks reasonable. But dirty handling is really responsibility of the device side user.

So likely just

if (pte_present(pte) && pte_dirty(pte))

	folio_mark_dirty(folio);
Signature · 3 lines
Cheers,

David
#10 lore raw

Re: [PATCH v2 1/4] mm: mincore: use walk_page_range_vma() in do_mincore()

Quoted 87 lines · David Hildenbrand (Arm)
Quoted 59 lines · Kefeng Wang
Quoted 23 lines · Andrew Morton
Quoted 2 lines · On Mon, 22 Jun 2026 11:23:11 +0800 Kefeng Wang <[email protected]>…

Hope Andrew can fix the comments when pickup patches.

I added this:

mm/mincore.c renamed from mm/mincore.c~mm-mincore-use-walk_page_range_vma-in-do_mincore-fix +1 -6
@@ -261,12 +261,7 @@ static long do_mincore(unsigned long add
       }
         /*
-     * walk_page_range_vma() does not call walk_page_test(), which
-     * handles VM_PFNMAP VMA by invoking ->pte_hole() to skip the
-     * page table walk. Without this check, PFNMAP PTEs would be
-     * treated as present by mincore_pte_range(), changing the returned
-     * residency status from the historical "not resident" to "resident".
-     * Handle VM_PFNMAP explicitly to preserve the original behavior.
+     * mincore historically reports PFNMAP mappings as non-resident.
        */
       if (vma->vm_flags & VM_PFNMAP) {
           __mincore_unmapped_range(addr, end, vma, vec);
Quoted 2 lines

_

Thanks.

Quoted 7 lines

Sashiko is OK with your patchset, but it might have found four(!) pre-existing issues:

    https://sashiko.dev/#/patchset/20260618092845.3905740-1-
[email protected]

Sorry for the late, this is pre-existing issue, I am not familiar with zone devices, so just focus on my changes and the subsequent changes related to mincore.. After a quick review, most of them are not issues based on my understanding.

+ CC Alistair and Gregroy for migrate_device.c, also Zi could you help to check it?

1. mlock.c, ACTION_AGAIN
2. migrate_device.c, goto again array overrun / double counting
3. migrate_device.c, softleaf_to_folio VM_WARN
4. migrate_device.c, pte_pfn / pte_dirty on non-present PTE

I think issue 4 is real issue, a quick fix is

mm/migrate_device.c +0 -0
index 554754eb26ff..3ccc4fc1015c 100644

mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 407

Quoted 8 lines
mm/migrate_device.c +2 -1
@@ -401,7 +401,8 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
                         bool anon_exclusive;
                         pte_t swp_pte;
 
-                       flush_cache_page(vma, addr, pte_pfn(pte));
+                       if (pte_present(pte))
+                               flush_cache_page(vma, addr, pte_pfn(pte));
                         anon_exclusive = folio_test_anon(folio) &&
                                           PageAnonExclusive(page);

That looks reasonable.

mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 431

Quoted 12 lines
mm/migrate_device.c +5 -1
                         if (anon_exclusive) {
@@ -422,8 +423,12 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
                         migrate->cpages++;
 
                         /* Set the dirty flag on the folio now the pte is gone. */
-                       if (pte_dirty(pte))
+                       if (pte_present(pte)) {
+                               if (pte_dirty(pte))
+                                       folio_mark_dirty(folio);
+                       } else if (softleaf_is_device_private_write(entry)) {
                                 folio_mark_dirty(folio);
+                       }

The pte_presnt() check looks reasonable. But dirty handling is really responsibility of the device side user.

Quoted 5 lines

So likely just

if (pte_present(pte) && pte_dirty(pte))

	folio_mark_dirty(folio);

Got it, I will send a separate fix if no more comments, thanks.

maintainer Acked-by on v2 1/4
#11 lore raw

Re: [PATCH v2 1/4] mm: mincore: use walk_page_range_vma() in do_mincore()

In reply to Kefeng Wang
Quoted 30 lines · Kefeng Wang

The do_mincore() uses walk_page_range() to walk the page table. Fortunately, the caller always passes start/end that falls within a single VMA, so it's safe to use the walk_page_range_vma() in do_mincore() to eliminate an unnecessary find_vma() lookup.

Unlike walk_page_range(), walk_page_range_vma() does not call walk_page_test(), which handles VM_PFNMAP by invoking ->pte_hole() to skip the page table walk. Without this check, PFNMAP PTEs would be treated as present by mincore_pte_range(), changing the returned residency status. Handle VM_PFNMAP explicitly in do_mincore() to preserve the original behavior.

---
 mm/mincore.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
mm/mincore.c +3 -1
index 296f2e3922b5..0c6731ae6c4d 100644
@@ -259,7 +259,21 @@ static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *v
 		memset(vec, 1, pages);
 		return pages;
 	}
-	err = walk_page_range(vma->vm_mm, addr, end, &mincore_walk_ops, vec);
+
+	/*
+	 * walk_page_range_vma() does not call walk_page_test(), which

mm/mincore.c static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *v · line 274

Quoted 10 lines
mm/mincore.c +10 -0
+	 * handles VM_PFNMAP VMA by invoking ->pte_hole() to skip the
+	 * page table walk. Without this check, PFNMAP PTEs would be
+	 * treated as present by mincore_pte_range(), changing the returned
+	 * residency status from the historical "not resident" to "resident".
+	 * Handle VM_PFNMAP explicitly to preserve the original behavior.
+	 */
+	if (vma->vm_flags & VM_PFNMAP) {
+		__mincore_unmapped_range(addr, end, vma, vec);
+		return (end - addr) >> PAGE_SHIFT;
+	}

This is fine to leave behavior unchanged for now.

We could consider removing the special handling in a separate patch, though. Would just do the right thing IMHO, and it's hard to believe that someone depends on pages in VM_PFNMAP to *not* be present.

(could even contain anonymous memory!)

So I would suggest to ahve a follow-up patch where we remove that special handling and see if anyone screams (nobody will).

Signature · 3 lines
Cheers,

David
#12 lore raw

PATCH v2 2/4 mm: mprotect: use walk_page_range_vma() in mprotect_fixup()

In reply to Kefeng Wang

In mprotect_fixup(), the PROT_NONE PFN permission check uses walk_page_range() to walk the page table. Fortunately, the caller always passes start/end that falls within a single VMA, the do_mprotect_pkey() iterates per-VMA via for_each_vma_range(), and setup_arg_pages() passes the whole VMA.

Note, walk_page_test() isn't called in walk_page_range_vma(), however, prot_none_test() in prot_none_walk_ops always return 0, so it's safe to replace walk_page_range() with walk_page_range_vma() to eliminate an unnecessary find_vma() lookup, also remove unneeded prot_none_test() too.

---
 mm/mprotect.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
mm/mprotect.c +1 -8
index 9cbf932b028c..b1595450e241 100644
@@ -708,16 +708,9 @@ static int prot_none_hugetlb_entry(pte_t *pte, unsigned long hmask,
 		0 : -EACCES;
 }
 
-static int prot_none_test(unsigned long addr, unsigned long next,
-			  struct mm_walk *walk)
-{
-	return 0;
-}
-
 static const struct mm_walk_ops prot_none_walk_ops = {
 	.pte_entry		= prot_none_pte_entry,
 	.hugetlb_entry		= prot_none_hugetlb_entry,
-	.test_walk		= prot_none_test,
 	.walk_lock		= PGWALK_WRLOCK,
 };
 
@@ -753,7 +746,7 @@ mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb,
 	    !vma_flags_test_any_mask(&new_vma_flags, VMA_ACCESS_FLAGS)) {
 		pgprot_t new_pgprot = vm_get_page_prot(newflags);
 
-		error = walk_page_range(current->mm, start, end,
+		error = walk_page_range_vma(vma, start, end,
 				&prot_none_walk_ops, &new_pgprot);
 		if (error)
 			return error;
Signature · 1 line
2.27.0
reviewer Reviewed-by on v2 2/4
#13 lore raw

Re: [PATCH v2 2/4] mm: mprotect: use walk_page_range_vma() in mprotect_fixup()

Quoted 11 lines · Kefeng Wang

In mprotect_fixup(), the PROT_NONE PFN permission check uses walk_page_range() to walk the page table. Fortunately, the caller always passes start/end that falls within a single VMA, the do_mprotect_pkey() iterates per-VMA via for_each_vma_range(), and setup_arg_pages() passes the whole VMA.

Note, walk_page_test() isn't called in walk_page_range_vma(), however, prot_none_test() in prot_none_walk_ops always return 0, so it's safe to replace walk_page_range() with walk_page_range_vma() to eliminate an unnecessary find_vma() lookup, also remove unneeded prot_none_test() too.

Again, I strongly prefer walk_page_range_vma() to be consistent with walk_page_range() and others. But the change itself (apart from that) is fairly uncontroversial, LGTM.

Quoted 41 lines
---
 mm/mprotect.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
mm/mprotect.c +1 -8
index 9cbf932b028c..b1595450e241 100644
@@ -708,16 +708,9 @@ static int prot_none_hugetlb_entry(pte_t *pte, unsigned long hmask,
 		0 : -EACCES;
 }
 
-static int prot_none_test(unsigned long addr, unsigned long next,
-			  struct mm_walk *walk)
-{
-	return 0;
-}
-
 static const struct mm_walk_ops prot_none_walk_ops = {
 	.pte_entry		= prot_none_pte_entry,
 	.hugetlb_entry		= prot_none_hugetlb_entry,
-	.test_walk		= prot_none_test,
 	.walk_lock		= PGWALK_WRLOCK,
 };
 
@@ -753,7 +746,7 @@ mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb,
 	    !vma_flags_test_any_mask(&new_vma_flags, VMA_ACCESS_FLAGS)) {
 		pgprot_t new_pgprot = vm_get_page_prot(newflags);
 
-		error = walk_page_range(current->mm, start, end,
+		error = walk_page_range_vma(vma, start, end,
 				&prot_none_walk_ops, &new_pgprot);
 		if (error)
 			return error;
Signature · 1 line
2.27.0
Signature · 1 line
Pedro
#14 lore raw

PATCH v2 3/4 mm: mlock: use walk_page_range_vma() in mlock_vma_pages_range()

In reply to Kefeng Wang

The mlock_vma_pages_range() uses walk_page_range() to walk the page table. Fortunately, the caller always passes start/end that falls within a single VMA, apply_vma_lock_flags() iterates per-VMA, and apply_mlockall_flags() passes the whole VMA.

Since there is no .test_walk in mlock_walk_ops and VM_PFNMAP was filtered by vma_supports_mlock(), it's safe to replace walk_page_range() with walk_page_range_vma() to eliminate an unnecessary find_vma() lookup.

---
 mm/mlock.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/mlock.c +1 -1
index 8c227fefa2df..97e49038d8d3 100644
@@ -446,7 +446,7 @@ static void mlock_vma_pages_range(struct vm_area_struct *vma,
 	vma_flags_reset_once(vma, new_vma_flags);
 
 	lru_add_drain();
-	walk_page_range(vma->vm_mm, start, end, &mlock_walk_ops, NULL);
+	walk_page_range_vma(vma, start, end, &mlock_walk_ops, NULL);
 	lru_add_drain();
 
 	if (vma_flags_test(new_vma_flags, VMA_IO_BIT)) {
Signature · 1 line
2.27.0
reviewer Reviewed-by on v2 3/4
#15 lore raw

Re: [PATCH v2 3/4] mm: mlock: use walk_page_range_vma() in mlock_vma_pages_range()

Quoted 5 lines · Kefeng Wang

The mlock_vma_pages_range() uses walk_page_range() to walk the page table. Fortunately, the caller always passes start/end that falls within a single VMA, apply_vma_lock_flags() iterates per-VMA, and apply_mlockall_flags() passes the whole VMA.

Quoted 8 lines

Since there is no .test_walk in mlock_walk_ops and VM_PFNMAP was filtered by vma_supports_mlock(), it's safe to replace walk_page_range() with walk_page_range_vma() to eliminate an unnecessary find_vma() lookup.

Signature · 1 line
Pedro
#16 lore raw

PATCH v2 4/4 mm: migrate_device: use walk_page_range_vma() in migrate_vma_collect()

In reply to Kefeng Wang

The migrate_vma_collect() uses walk_page_range() to walk the page table. Fortunately, migrate_vma_setup() already validates that the entire range falls within a single VMA.

Since there is no .test_walk in migrate_vma_walk_ops and VM_PFNMAP was filtered by migrate_vma_setup(), it's safe to replace walk_page_range() with walk_page_range_vma() to eliminate an unnecessary find_vma() lookup.

---
 mm/migrate_device.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/migrate_device.c +1 -1
index 554754eb26ff..ae39173d6a0e 100644
@@ -513,7 +513,7 @@ static void migrate_vma_collect(struct migrate_vma *migrate)
 		migrate->pgmap_owner);
 	mmu_notifier_invalidate_range_start(&range);
 
-	walk_page_range(migrate->vma->vm_mm, migrate->start, migrate->end,
+	walk_page_range_vma(migrate->vma, migrate->start, migrate->end,
 			&migrate_vma_walk_ops, migrate);
 
 	mmu_notifier_invalidate_range_end(&range);
Signature · 1 line
2.27.0
reviewer Acked-by on v2 4/4
#17 lore raw

Re: [PATCH v2 4/4] mm: migrate_device: use walk_page_range_vma() in migrate_vma_collect()

Quoted 4 lines · Kefeng Wang

The migrate_vma_collect() uses walk_page_range() to walk the page table. Fortunately, migrate_vma_setup() already validates that the entire range falls within a single VMA.

Quoted 8 lines

Since there is no .test_walk in migrate_vma_walk_ops and VM_PFNMAP was filtered by migrate_vma_setup(), it's safe to replace walk_page_range() with walk_page_range_vma() to eliminate an unnecessary find_vma() lookup.

Signature · 1 line
Pedro