Skip to contentSkip to conversation

Fix incorrect access of hugetlb pte entries

PATCH Superseded Dev Jain 6 participants 39 messages Jun 25 – Jun 30

Subsystems (9)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Dev Jain

author
#0 lore raw

cover Fix incorrect access of hugetlb pte entries

There are various places which use ptep_get() to get the pte entry corresponding to a hugetlb folio. Some arches have special handling to compute the pteval, so they provide huge_ptep_get(). Use this helper consistently.

Dev Jain (5):
mm/rmap: use huge_ptep_get() in try_to_unmap_one() mm/rmap: use huge_ptep_get() in try_to_migrate_one() mm/migrate: use huge_ptep_get() in remove_migration_pte() mm/page_vma_mapped: use huge_ptep_get() for hugetlb mm/mprotect: use huge_ptep_get() for hugetlb

 include/linux/hugetlb.h |  3 +++
 mm/migrate.c            |  6 +++++-
 mm/mprotect.c           |  8 +++++++-
 mm/page_vma_mapped.c    |  8 +++++++-
 mm/rmap.c               | 32 ++++++++++++++++++++------------
 5 files changed, 42 insertions(+), 15 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 1/5 mm/rmap: use huge_ptep_get() in try_to_unmap_one()

try_to_unmap_one() handles hugetlb folios when memory failure needs to replace a poisoned hugetlb mapping with a hwpoison entry. In that case page_vma_mapped_walk() returns the pte pointer to the hugetlb folio in pvmw.pte, but the code reads it with ptep_get().

On arches which provide their own huge_ptep_get() to dereference a huge pte pointer, accessing via ptep_get() would cause pte_pfn(), pte_present() etc to misbehave.

It is not clear whether this has a trivially visible effect to userspace.

Just use huge_ptep_get() for dereferencing a huge pte pointer.

Fixes c7ab0d2fdc84 ("mm: convert try_to_unmap_one() to use page_vma_mapped_walk()")
---
 include/linux/hugetlb.h |  3 +++
 mm/rmap.c               | 16 ++++++++++------
 2 files changed, 13 insertions(+), 6 deletions(-)
include/linux/hugetlb.h +3 -0
index 2abaf99321e90..fdb7bdf7645c5 100644
@@ -1261,6 +1261,9 @@ static inline void hugetlb_count_sub(long l, struct mm_struct *mm)
 {
 }
 
+pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr,
+		    pte_t *ptep);
+
 static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
 					  unsigned long addr, pte_t *ptep)
 {
mm/rmap.c +10 -6
index 1c77d5dc06e9f..aa8a254efaecc 100644
@@ -2095,11 +2095,16 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 		/* Unexpected PMD-mapped THP? */
 		VM_BUG_ON_FOLIO(!pvmw.pte, folio);
 
-		/*
-		 * Handle PFN swap PTEs, such as device-exclusive ones, that
-		 * actually map pages.
-		 */
-		pteval = ptep_get(pvmw.pte);
+		address = pvmw.address;
+		if (folio_test_hugetlb(folio)) {
+			pteval = huge_ptep_get(mm, address, pvmw.pte);
+		} else {
+			/*
+			 * Handle PFN swap PTEs, such as device-exclusive ones,
+			 * that actually map pages.
+			 */
+			pteval = ptep_get(pvmw.pte);
+		}
 		if (likely(pte_present(pteval))) {
 			pfn = pte_pfn(pteval);
 		} else {
@@ -2110,7 +2115,6 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 		}
 
 		subpage = folio_page(folio, pfn - folio_pfn(folio));
-		address = pvmw.address;
 		anon_exclusive = folio_test_anon(folio) &&
 				 PageAnonExclusive(subpage);
 
Signature · 1 line
2.43.0
author
#5 lore raw

PATCH 2/5 mm/rmap: use huge_ptep_get() in try_to_migrate_one()

In reply to Dev Jain

try_to_migrate_one() is used by folio migration to replace a present mapping with a migration entry. For hugetlb folios, page_vma_mapped_walk() returns the pte pointer to the hugetlb folio in pvmw.pte, but the code reads the huge pte entry with ptep_get().

On arches which provide their own huge_ptep_get() to dereference a huge pte pointer, accessing via ptep_get() would cause pte_pfn(), pte_present() etc to misbehave.

It is not clear whether this has a trivially visible effect to userspace.

Use huge_ptep_get() to dereference a huge pte pointer.

Commit a98a2f0c8ce1 copied the bug from try_to_unmap_one into try_to_migrate_one.

Fixes a98a2f0c8ce1 ("mm/rmap: split migration into its own function")
---
 mm/rmap.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
mm/rmap.c +10 -6
index aa8a254efaecc..abc3a44baaa3d 100644
@@ -2505,11 +2505,16 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,
 		/* Unexpected PMD-mapped THP? */
 		VM_BUG_ON_FOLIO(!pvmw.pte, folio);
 
-		/*
-		 * Handle PFN swap PTEs, such as device-exclusive ones, that
-		 * actually map pages.
-		 */
-		pteval = ptep_get(pvmw.pte);
+		address = pvmw.address;
+		if (folio_test_hugetlb(folio)) {
+			pteval = huge_ptep_get(mm, address, pvmw.pte);
+		} else {
+			/*
+			 * Handle PFN swap PTEs, such as device-exclusive ones,
+			 * that actually map pages.
+			 */
+			pteval = ptep_get(pvmw.pte);
+		}
 		if (likely(pte_present(pteval))) {
 			pfn = pte_pfn(pteval);
 		} else {
@@ -2520,7 +2525,6 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,
 		}
 
 		subpage = folio_page(folio, pfn - folio_pfn(folio));
-		address = pvmw.address;
 		anon_exclusive = folio_test_anon(folio) &&
 				 PageAnonExclusive(subpage);
 
Signature · 1 line
2.43.0
author
#7 lore raw

PATCH 3/5 mm/migrate: use huge_ptep_get() in remove_migration_pte()

In reply to Dev Jain

remove_migration_pte() converts migration entries back to present PTEs after folio migration completes. For hugetlb folios, page_vma_mapped_walk() returns the pte pointer to the hugetlb folio in pvmw.pte, but the code reads it with ptep_get().

On arches which provide their own huge_ptep_get() to dereference a huge pte pointer, accessing via ptep_get() would cause pte_pfn(), pte_present() etc to misbehave.

It is not clear whether this has a trivially visible effect to userspace.

Use huge_ptep_get() to dereference a huge pte pointer.

Fixes 290408d4a250 ("hugetlb: hugepage migration core")
---
 mm/migrate.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
mm/migrate.c +5 -1
index d9b23909d716c..c65f0f43df7eb 100644
@@ -371,7 +371,11 @@ static bool remove_migration_pte(struct folio *folio,
 			continue;
 		}
 #endif
-		old_pte = ptep_get(pvmw.pte);
+		if (folio_test_hugetlb(folio))
+			old_pte = huge_ptep_get(vma->vm_mm, pvmw.address,
+						pvmw.pte);
+		else
+			old_pte = ptep_get(pvmw.pte);
 		if (rmap_walk_arg->map_unused_to_zeropage &&
 		    try_to_map_unused_to_zeropage(&pvmw, folio, old_pte, idx))
 			continue;
Signature · 1 line
2.43.0
author
#9 lore raw

PATCH 4/5 mm/page_vma_mapped: use huge_ptep_get() for hugetlb

In reply to Dev Jain

check_pte() is the final validation step in page_vma_mapped_walk(). It reads pvmw->pte with ptep_get() to decide whether the entry maps the PFN range being walked. For hugetlb VMAs, that pointer refers to a hugetlb entry.

On arches which provide their own huge_ptep_get() to dereference a huge pte pointer, accessing via ptep_get() would cause pte_pfn(), pte_present() etc to misbehave.

It is not clear whether this has a trivially visible effect to userspace.

Use huge_ptep_get() to dereference a huge pte pointer.

Fixes ace71a19cec5 ("mm: introduce page_vma_mapped_walk()")
---
 mm/page_vma_mapped.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
mm/page_vma_mapped.c +7 -1
index 2ccbabfb2cc17..18e1d341f463c 100644
@@ -107,7 +107,13 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp,
 static bool check_pte(struct page_vma_mapped_walk *pvmw, unsigned long pte_nr)
 {
 	unsigned long pfn;
-	pte_t ptent = ptep_get(pvmw->pte);
+	pte_t ptent;
+
+	if (is_vm_hugetlb_page(pvmw->vma))
+		ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
+				      pvmw->pte);
+	else
+		ptent = ptep_get(pvmw->pte);
 
 	if (pvmw->flags & PVMW_MIGRATION) {
 		const softleaf_t entry = softleaf_from_pte(ptent);
Signature · 1 line
2.43.0
author
#32 lore raw

PATCH 5/5 mm/mprotect: use huge_ptep_get() for hugetlb

In reply to Dev Jain

prot_none_hugetlb_entry() is the hugetlb callback for the early mprotect(PROT_NONE) PFN permission walk on x86.

The callback passes the decoded PFN to pfn_modify_allowed(). For a hugetlb callback, the pte pointer refers to a hugetlb entry. On architectures where hugetlb entries need huge_ptep_get(), reading that entry with ptep_get() can make the permission check use the wrong PFN.

Use huge_ptep_get() before decoding the hugetlb PFN.

Currently there is no path which can trigger a bug: huge_ptep_get() is a simple ptep_get() for x86, and the prot_none walk occurs only for x86. But use the correct helper anyways.

Fixes 42e4089c7890 ("x86/speculation/l1tf: Disallow non privileged high MMIO PROT_NONE mappings")
---
 mm/mprotect.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
mm/mprotect.c +7 -1
index 9cbf932b028cf..23779632d18bf 100644
@@ -699,14 +699,20 @@ static int prot_none_pte_entry(pte_t *pte, unsigned long addr,
 		0 : -EACCES;
 }
 
+#ifdef CONFIG_HUGETLB_PAGE
 static int prot_none_hugetlb_entry(pte_t *pte, unsigned long hmask,
 				   unsigned long addr, unsigned long next,
 				   struct mm_walk *walk)
 {
-	return pfn_modify_allowed(pte_pfn(ptep_get(pte)),
+	pte_t entry = huge_ptep_get(walk->mm, addr, pte);
+
+	return pfn_modify_allowed(pte_pfn(entry),
 				  *(pgprot_t *)(walk->private)) ?
 		0 : -EACCES;
 }
+#else
+#define prot_none_hugetlb_entry	NULL
+#endif
 
 static int prot_none_test(unsigned long addr, unsigned long next,
 			  struct mm_walk *walk)
Signature · 1 line
2.43.0
reviewer
#37 lore raw
In reply to Dev Jain
Quoted 2 lines · Dev Jain

There are various places which use ptep_get() to get the pte entry corresponding to a hugetlb folio. Some arches have special handling

I think it is better to mention s390 as a concrete example.

Quoted 16 lines

to compute the pteval, so they provide huge_ptep_get(). Use this helper consistently.

Dev Jain (5):
mm/rmap: use huge_ptep_get() in try_to_unmap_one() mm/rmap: use huge_ptep_get() in try_to_migrate_one() mm/migrate: use huge_ptep_get() in remove_migration_pte() mm/page_vma_mapped: use huge_ptep_get() for hugetlb mm/mprotect: use huge_ptep_get() for hugetlb

 include/linux/hugetlb.h |  3 +++
 mm/migrate.c            |  6 +++++-
 mm/mprotect.c           |  8 +++++++-
 mm/page_vma_mapped.c    |  8 +++++++-
 mm/rmap.c               | 32 ++++++++++++++++++++------------
 5 files changed, 42 insertions(+), 15 deletions(-)
Signature · 2 lines
Best Regards,
Yan, Zi
reviewer
#10 lore raw

Re: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb

In reply to Dev Jain
Quoted 25 lines · Dev Jain

check_pte() is the final validation step in page_vma_mapped_walk(). It reads pvmw->pte with ptep_get() to decide whether the entry maps the PFN range being walked. For hugetlb VMAs, that pointer refers to a hugetlb entry.

On arches which provide their own huge_ptep_get() to dereference a huge pte pointer, accessing via ptep_get() would cause pte_pfn(), pte_present() etc to misbehave.

It is not clear whether this has a trivially visible effect to userspace.

Use huge_ptep_get() to dereference a huge pte pointer.

Fixes ace71a19cec5 ("mm: introduce page_vma_mapped_walk()")

---
mm/page_vma_mapped.c | 8 +++++++-

1 file changed, 7 insertions(+), 1 deletion(-)
mm/page_vma_mapped.c +0 -0
index 2ccbabfb2cc17..18e1d341f463c 100644

mm/page_vma_mapped.c static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp, · line 107

Quoted 1 line
mm/page_vma_mapped.c +0 -0
@@ -107,7 +107,13 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp,
 static bool check_pte(struct page_vma_mapped_walk *pvmw, unsigned long pte_nr)

Just one ordering thing: should this patch come first?

Patches #01-#03 only reach the new huge_ptep_get() after page_vma_mapped_walk() succeeds. But before this patch, hugetlb sill goes through check_pte() (still using ptep_get()).

Quoted 17 lines
mm/page_vma_mapped.c +7 -1
 {
 	unsigned long pfn;
-	pte_t ptent = ptep_get(pvmw->pte);
+	pte_t ptent;
+
+	if (is_vm_hugetlb_page(pvmw->vma))
+		ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
+				      pvmw->pte);
+	else
+		ptent = ptep_get(pvmw->pte);
 
 	if (pvmw->flags & PVMW_MIGRATION) {
 		const softleaf_t entry = softleaf_from_pte(ptent);
Signature · 1 line
2.43.0
maintainer
#2 lore raw

Re: [PATCH 1/5] mm/rmap: use huge_ptep_get() in try_to_unmap_one()

In reply to Dev Jain
Quoted 26 lines · Dev Jain

try_to_unmap_one() handles hugetlb folios when memory failure needs to replace a poisoned hugetlb mapping with a hwpoison entry. In that case page_vma_mapped_walk() returns the pte pointer to the hugetlb folio in pvmw.pte, but the code reads it with ptep_get().

On arches which provide their own huge_ptep_get() to dereference a huge pte pointer, accessing via ptep_get() would cause pte_pfn(), pte_present() etc to misbehave.

It is not clear whether this has a trivially visible effect to userspace.

Just use huge_ptep_get() for dereferencing a huge pte pointer.

Fixes c7ab0d2fdc84 ("mm: convert try_to_unmap_one() to use page_vma_mapped_walk()")
---
  include/linux/hugetlb.h |  3 +++
  mm/rmap.c               | 16 ++++++++++------
  2 files changed, 13 insertions(+), 6 deletions(-)
include/linux/hugetlb.h +0 -0
index 2abaf99321e90..fdb7bdf7645c5 100644

include/linux/hugetlb.h static inline void hugetlb_count_sub(long l, struct mm_struct *mm) · line 1265

Quoted 5 lines
include/linux/hugetlb.h +2 -0
@@ -1261,6 +1261,9 @@ static inline void hugetlb_count_sub(long l, struct mm_struct *mm)
  {
  }
 
+pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr,
+		    pte_t *ptep);

Thanks so much for the fix! I'm curious, though: why do we need to add a separate declaration for this function here?

Thanks,
Muchun

Quoted 38 lines
include/linux/hugetlb.h +1 -0
+
  static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
  					  unsigned long addr, pte_t *ptep)
  {
mm/rmap.c +10 -6
index 1c77d5dc06e9f..aa8a254efaecc 100644
@@ -2095,11 +2095,16 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
  		/* Unexpected PMD-mapped THP? */
  		VM_BUG_ON_FOLIO(!pvmw.pte, folio);
 
-		/*
-		 * Handle PFN swap PTEs, such as device-exclusive ones, that
-		 * actually map pages.
-		 */
-		pteval = ptep_get(pvmw.pte);
+		address = pvmw.address;
+		if (folio_test_hugetlb(folio)) {
+			pteval = huge_ptep_get(mm, address, pvmw.pte);
+		} else {
+			/*
+			 * Handle PFN swap PTEs, such as device-exclusive ones,
+			 * that actually map pages.
+			 */
+			pteval = ptep_get(pvmw.pte);
+		}
  		if (likely(pte_present(pteval))) {
  			pfn = pte_pfn(pteval);
  		} else {
@@ -2110,7 +2115,6 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
  		}
 
  		subpage = folio_page(folio, pfn - folio_pfn(folio));
-		address = pvmw.address;
  		anon_exclusive = folio_test_anon(folio) &&
  				 PageAnonExclusive(subpage);
 
maintainer Acked-by on v1 2/5
#6 lore raw

Re: [PATCH 2/5] mm/rmap: use huge_ptep_get() in try_to_migrate_one()

In reply to Dev Jain
Quoted 15 lines · Dev Jain

try_to_migrate_one() is used by folio migration to replace a present mapping with a migration entry. For hugetlb folios, page_vma_mapped_walk() returns the pte pointer to the hugetlb folio in pvmw.pte, but the code reads the huge pte entry with ptep_get().

On arches which provide their own huge_ptep_get() to dereference a huge pte pointer, accessing via ptep_get() would cause pte_pfn(), pte_present() etc to misbehave.

It is not clear whether this has a trivially visible effect to userspace.

Quoted 8 lines

Use huge_ptep_get() to dereference a huge pte pointer.

Commit a98a2f0c8ce1 copied the bug from try_to_unmap_one into try_to_migrate_one.

Fixes a98a2f0c8ce1 ("mm/rmap: split migration into its own function")

Thanks.

maintainer Acked-by on v1 3/5
#8 lore raw

Re: [PATCH 3/5] mm/migrate: use huge_ptep_get() in remove_migration_pte()

In reply to Dev Jain
Quoted 9 lines · Dev Jain

remove_migration_pte() converts migration entries back to present PTEs after folio migration completes. For hugetlb folios, page_vma_mapped_walk() returns the pte pointer to the hugetlb folio in pvmw.pte, but the code reads it with ptep_get().

Quoted 5 lines

On arches which provide their own huge_ptep_get() to dereference a huge pte pointer, accessing via ptep_get() would cause pte_pfn(), pte_present() etc to misbehave.

It is not clear whether this has a trivially visible effect to userspace.

We are dealing with migration entries here, so the issue mentioned shouldn't be a problem with any of the architectures. Semantically speaking, we definitely should fix this.

Quoted 6 lines

Use huge_ptep_get() to dereference a huge pte pointer.

Fixes 290408d4a250 ("hugetlb: hugepage migration core")

Thanks

maintainer
#33 lore raw

Re: [PATCH 5/5] mm/mprotect: use huge_ptep_get() for hugetlb

In reply to Dev Jain
Quoted 32 lines · Dev Jain

prot_none_hugetlb_entry() is the hugetlb callback for the early mprotect(PROT_NONE) PFN permission walk on x86.

The callback passes the decoded PFN to pfn_modify_allowed(). For a hugetlb callback, the pte pointer refers to a hugetlb entry. On architectures where hugetlb entries need huge_ptep_get(), reading that entry with ptep_get() can make the permission check use the wrong PFN.

Use huge_ptep_get() before decoding the hugetlb PFN.

Currently there is no path which can trigger a bug: huge_ptep_get() is a simple ptep_get() for x86, and the prot_none walk occurs only for x86. But use the correct helper anyways.

Fixes 42e4089c7890 ("x86/speculation/l1tf: Disallow non privileged high MMIO PROT_NONE mappings")
---
  mm/mprotect.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
mm/mprotect.c +1 -0
index 9cbf932b028cf..23779632d18bf 100644
@@ -699,14 +699,20 @@ static int prot_none_pte_entry(pte_t *pte, unsigned long addr,
  		0 : -EACCES;
  }
 
+#ifdef CONFIG_HUGETLB_PAGE
  static int prot_none_hugetlb_entry(pte_t *pte, unsigned long hmask,
  				   unsigned long addr, unsigned long next,
  				   struct mm_walk *walk)

mm/mprotect.c static int prot_none_pte_entry(pte_t *pte, unsigned long addr, · line 714

Quoted 10 lines
mm/mprotect.c +5 -1
  {
-	return pfn_modify_allowed(pte_pfn(ptep_get(pte)),
+	pte_t entry = huge_ptep_get(walk->mm, addr, pte);
+
+	return pfn_modify_allowed(pte_pfn(entry),
  				  *(pgprot_t *)(walk->private)) ?
  		0 : -EACCES;
  }
+#else
+#define prot_none_hugetlb_entry	NULL

This is very strange, because we defined a stub as NULL for a helper function. How about  the following diff?

mm/mprotect.c +0 -0
index 9cbf932b028c..4d8c1551fbce 100644
@@ -716,7 +716,9 @@ static int prot_none_test(unsigned long addr, unsigned long next,
 

 static const struct mm_walk_ops prot_none_walk_ops = { .pte_entry              = prot_none_pte_entry, +#ifdef CONFIG_HUGETLB_PAGE
        .hugetlb_entry          = prot_none_hugetlb_entry, +#endif
        .test_walk              = prot_none_test, .walk_lock              = PGWALK_WRLOCK, };

Thanks,
Muchun

Quoted 4 lines
mm/mprotect.c +1 -0
+#endif
 
  static int prot_none_test(unsigned long addr, unsigned long next,
  			  struct mm_walk *walk)
author
#3 lore raw

Re: [PATCH 1/5] mm/rmap: use huge_ptep_get() in try_to_unmap_one()

In reply to Muchun Song
Quoted 24 lines · Muchun Song
Quoted 21 lines · Dev Jain

try_to_unmap_one() handles hugetlb folios when memory failure needs to replace a poisoned hugetlb mapping with a hwpoison entry. In that case page_vma_mapped_walk() returns the pte pointer to the hugetlb folio in pvmw.pte, but the code reads it with ptep_get().

On arches which provide their own huge_ptep_get() to dereference a huge pte pointer, accessing via ptep_get() would cause pte_pfn(), pte_present() etc to misbehave.

It is not clear whether this has a trivially visible effect to userspace.

Just use huge_ptep_get() for dereferencing a huge pte pointer.

Fixes c7ab0d2fdc84 ("mm: convert try_to_unmap_one() to use page_vma_mapped_walk()")

---
  include/linux/hugetlb.h |  3 +++
  mm/rmap.c               | 16 ++++++++++------

  2 files changed, 13 insertions(+), 6 deletions(-)
Quoted 12 lines

include/linux/hugetlb.h static inline void hugetlb_count_sub(long l, struct mm_struct *mm) · line 1264

Quoted 9 lines
include/linux/hugetlb.h +1 -0
index 2abaf99321e90..fdb7bdf7645c5 100644
@@ -1261,6 +1261,9 @@ static inline void hugetlb_count_sub(long l, struct mm_struct *mm)
   {
   }
   +pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr,
+            pte_t *ptep);

Thanks so much for the fix! I'm curious, though: why do we need to add a separate declaration for this function here?

For !CONFIG_HUGETLB_PAGE, compiler complains that there is no huge_ptep_get. So this is to make compiler happy.

Quoted 41 lines

Thanks,
Muchun

Quoted 36 lines
include/linux/hugetlb.h +1 -0
+
   static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
                         unsigned long addr, pte_t *ptep)
   {
mm/rmap.c +0 -0
index 1c77d5dc06e9f..aa8a254efaecc 100644
@@ -2095,11 +2095,16 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
           /* Unexpected PMD-mapped THP? */
           VM_BUG_ON_FOLIO(!pvmw.pte, folio);

  -        /*
-         * Handle PFN swap PTEs, such as device-exclusive ones, that -         * actually map pages.
-         */
-        pteval = ptep_get(pvmw.pte);
+        address = pvmw.address;
+        if (folio_test_hugetlb(folio)) {
+            pteval = huge_ptep_get(mm, address, pvmw.pte); +        } else {
+            /*
+             * Handle PFN swap PTEs, such as device-exclusive ones, +             * that actually map pages.
+             */
+            pteval = ptep_get(pvmw.pte);
+        }
          if (likely(pte_present(pteval))) {
              pfn = pte_pfn(pteval);
          } else {

mm/rmap.c +0 -1
@@ -2110,7 +2115,6 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
           }
             subpage = folio_page(folio, pfn - folio_pfn(folio));
-        address = pvmw.address;

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

author
#11 lore raw

Re: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb

In reply to Lance Yang
Quoted 31 lines · Lance Yang
Quoted 25 lines · Dev Jain

check_pte() is the final validation step in page_vma_mapped_walk(). It reads pvmw->pte with ptep_get() to decide whether the entry maps the PFN range being walked. For hugetlb VMAs, that pointer refers to a hugetlb entry.

On arches which provide their own huge_ptep_get() to dereference a huge pte pointer, accessing via ptep_get() would cause pte_pfn(), pte_present() etc to misbehave.

It is not clear whether this has a trivially visible effect to userspace.

Use huge_ptep_get() to dereference a huge pte pointer.

Fixes ace71a19cec5 ("mm: introduce page_vma_mapped_walk()")

---
mm/page_vma_mapped.c | 8 +++++++-

1 file changed, 7 insertions(+), 1 deletion(-)
mm/page_vma_mapped.c +0 -0
index 2ccbabfb2cc17..18e1d341f463c 100644

mm/page_vma_mapped.c static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp, · line 107

Quoted 1 line
mm/page_vma_mapped.c +0 -0
@@ -107,7 +107,13 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp,
 static bool check_pte(struct page_vma_mapped_walk *pvmw, unsigned long pte_nr)

Just one ordering thing: should this patch come first?

Quoted 3 lines

Patches #01-#03 only reach the new huge_ptep_get() after page_vma_mapped_walk() succeeds. But before this patch, hugetlb sill goes through check_pte() (still using ptep_get()).

You are right, but do we care? This is not a series meant for adding functionality. I just sent it as a series because they are similar fixes - the patches are to be applied individually with no dependency.

Quoted 19 lines
Quoted 18 lines
mm/page_vma_mapped.c +7 -1
 {
 	unsigned long pfn;
-	pte_t ptent = ptep_get(pvmw->pte);
+	pte_t ptent;
+
+	if (is_vm_hugetlb_page(pvmw->vma))
+		ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
+				      pvmw->pte);
+	else
+		ptent = ptep_get(pvmw->pte);
 
 	if (pvmw->flags & PVMW_MIGRATION) {
 		const softleaf_t entry = softleaf_from_pte(ptent);
Signature · 1 line
2.43.0
author
#34 lore raw

Re: [PATCH 5/5] mm/mprotect: use huge_ptep_get() for hugetlb

In reply to Muchun Song
Quoted 38 lines · Muchun Song
Quoted 35 lines · Dev Jain

prot_none_hugetlb_entry() is the hugetlb callback for the early mprotect(PROT_NONE) PFN permission walk on x86.

The callback passes the decoded PFN to pfn_modify_allowed(). For a hugetlb callback, the pte pointer refers to a hugetlb entry. On architectures where hugetlb entries need huge_ptep_get(), reading that entry with ptep_get() can make the permission check use the wrong PFN.

Use huge_ptep_get() before decoding the hugetlb PFN.

Currently there is no path which can trigger a bug: huge_ptep_get() is a simple ptep_get() for x86, and the prot_none walk occurs only for x86. But use the correct helper anyways.

Fixes 42e4089c7890 ("x86/speculation/l1tf: Disallow non privileged high MMIO PROT_NONE mappings")

---
  mm/mprotect.c | 8 +++++++-

  1 file changed, 7 insertions(+), 1 deletion(-)
mm/mprotect.c +0 -0
index 9cbf932b028cf..23779632d18bf 100644
@@ -699,14 +699,20 @@ static int prot_none_pte_entry(pte_t *pte, unsigned long addr,
           0 : -EACCES;
   }

  +#ifdef CONFIG_HUGETLB_PAGE
  static int prot_none_hugetlb_entry(pte_t *pte, unsigned long hmask, unsigned long addr, unsigned long next, struct mm_walk *walk)
  {
-    return pfn_modify_allowed(pte_pfn(ptep_get(pte)),
+    pte_t entry = huge_ptep_get(walk->mm, addr, pte);
+

Quoted 8 lines

mm/mprotect.c

Quoted 6 lines

+    return pfn_modify_allowed(pte_pfn(entry), *(pgprot_t *)(walk->private)) ? 0 : -EACCES;
  }

mm/mprotect.c +2 -0
+#else
+#define prot_none_hugetlb_entry    NULL

This is very strange, because we defined a stub as NULL for a helper

I was following pattern elsewhere, search for ".hugetlb_entry" in the codebase and you will find others doing the same.

Quoted 24 lines

function. How about  the following diff?

mm/mprotect.c +2 -0
index 9cbf932b028c..4d8c1551fbce 100644
@@ -716,7 +716,9 @@ static int prot_none_test(unsigned long addr, unsigned long next,
 
  static const struct mm_walk_ops prot_none_walk_ops = {
         .pte_entry              = prot_none_pte_entry,
+#ifdef CONFIG_HUGETLB_PAGE
         .hugetlb_entry          = prot_none_hugetlb_entry,
+#endif
         .test_walk              = prot_none_test,
         .walk_lock              = PGWALK_WRLOCK,
  };

Thanks,
Muchun

Quoted 3 lines

+#endif
    static int prot_none_test(unsigned long addr, unsigned long next, struct mm_walk *walk)

author
#38 lore raw
In reply to Zi Yan
Quoted 5 lines · Zi Yan
Quoted 2 lines · Dev Jain

There are various places which use ptep_get() to get the pte entry corresponding to a hugetlb folio. Some arches have special handling

I think it is better to mention s390 as a concrete example.

Sure. In case there is no v2, requesting Andrew to change "Some arches have special handling" to "Some arches like s390 have special handling".

Quoted 21 lines
Quoted 17 lines

to compute the pteval, so they provide huge_ptep_get(). Use this helper consistently.

Dev Jain (5):
mm/rmap: use huge_ptep_get() in try_to_unmap_one() mm/rmap: use huge_ptep_get() in try_to_migrate_one() mm/migrate: use huge_ptep_get() in remove_migration_pte() mm/page_vma_mapped: use huge_ptep_get() for hugetlb mm/mprotect: use huge_ptep_get() for hugetlb

 include/linux/hugetlb.h |  3 +++
 mm/migrate.c            |  6 +++++-
 mm/mprotect.c           |  8 +++++++-
 mm/page_vma_mapped.c    |  8 +++++++-
 mm/rmap.c               | 32 ++++++++++++++++++++------------
 5 files changed, 42 insertions(+), 15 deletions(-)
maintainer
#4 lore raw

Re: [PATCH 1/5] mm/rmap: use huge_ptep_get() in try_to_unmap_one()

In reply to Dev Jain
Quoted 44 lines · Dev Jain
Quoted 24 lines · Muchun Song
Quoted 21 lines · Dev Jain

try_to_unmap_one() handles hugetlb folios when memory failure needs to replace a poisoned hugetlb mapping with a hwpoison entry. In that case page_vma_mapped_walk() returns the pte pointer to the hugetlb folio in pvmw.pte, but the code reads it with ptep_get().

On arches which provide their own huge_ptep_get() to dereference a huge pte pointer, accessing via ptep_get() would cause pte_pfn(), pte_present() etc to misbehave.

It is not clear whether this has a trivially visible effect to userspace.

Just use huge_ptep_get() for dereferencing a huge pte pointer.

Fixes c7ab0d2fdc84 ("mm: convert try_to_unmap_one() to use page_vma_mapped_walk()")
---
  include/linux/hugetlb.h |  3 +++
  mm/rmap.c               | 16 ++++++++++------
  2 files changed, 13 insertions(+), 6 deletions(-)
Quoted 12 lines

include/linux/hugetlb.h static inline void hugetlb_count_sub(long l, struct mm_struct *mm) · line 1264

Quoted 9 lines
include/linux/hugetlb.h +1 -0
index 2abaf99321e90..fdb7bdf7645c5 100644
@@ -1261,6 +1261,9 @@ static inline void hugetlb_count_sub(long l, struct mm_struct *mm)
  {
  }
  +pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr,
+            pte_t *ptep);

Thanks so much for the fix! I'm curious, though: why do we need to add a separate declaration for this function here?

Quoted 2 lines

For !CONFIG_HUGETLB_PAGE, compiler complains that there is no huge_ptep_get. So this is to make compiler happy.

Got it. We can refer to 5d4af6195c87c6b162b7963e0ad00a214b80d764 to fix this warning.

Muchun,
Thanks.

Quoted 40 lines
Quoted 40 lines

Thanks,
Muchun

Quoted 35 lines
include/linux/hugetlb.h +1 -0
+
  static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
                        unsigned long addr, pte_t *ptep)
  {
mm/rmap.c +10 -5
index 1c77d5dc06e9f..aa8a254efaecc 100644
@@ -2095,11 +2095,16 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
          /* Unexpected PMD-mapped THP? */
          VM_BUG_ON_FOLIO(!pvmw.pte, folio);
  -        /*
-         * Handle PFN swap PTEs, such as device-exclusive ones, that
-         * actually map pages.
-         */
-        pteval = ptep_get(pvmw.pte);
+        address = pvmw.address;
+        if (folio_test_hugetlb(folio)) {
+            pteval = huge_ptep_get(mm, address, pvmw.pte);
+        } else {
+            /*
+             * Handle PFN swap PTEs, such as device-exclusive ones,
+             * that actually map pages.
+             */
+            pteval = ptep_get(pvmw.pte);
+        }
          if (likely(pte_present(pteval))) {
              pfn = pte_pfn(pteval);
          } else {
@@ -2110,7 +2115,6 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
          }
            subpage = folio_page(folio, pfn - folio_pfn(folio));
-        address = pvmw.address;
          anon_exclusive = folio_test_anon(folio) &&
                   PageAnonExclusive(subpage);
maintainer
#35 lore raw

Re: [PATCH 5/5] mm/mprotect: use huge_ptep_get() for hugetlb

In reply to Dev Jain
Quoted 54 lines · Dev Jain
Quoted 38 lines · Muchun Song
Quoted 35 lines · Dev Jain

prot_none_hugetlb_entry() is the hugetlb callback for the early mprotect(PROT_NONE) PFN permission walk on x86.

The callback passes the decoded PFN to pfn_modify_allowed(). For a hugetlb callback, the pte pointer refers to a hugetlb entry. On architectures where hugetlb entries need huge_ptep_get(), reading that entry with ptep_get() can make the permission check use the wrong PFN.

Use huge_ptep_get() before decoding the hugetlb PFN.

Currently there is no path which can trigger a bug: huge_ptep_get() is a simple ptep_get() for x86, and the prot_none walk occurs only for x86. But use the correct helper anyways.

Fixes 42e4089c7890 ("x86/speculation/l1tf: Disallow non privileged high MMIO PROT_NONE mappings")
---
  mm/mprotect.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
mm/mprotect.c +2 -1
index 9cbf932b028cf..23779632d18bf 100644
@@ -699,14 +699,20 @@ static int prot_none_pte_entry(pte_t *pte, unsigned long addr,
          0 : -EACCES;
  }
  +#ifdef CONFIG_HUGETLB_PAGE
  static int prot_none_hugetlb_entry(pte_t *pte, unsigned long hmask,
                     unsigned long addr, unsigned long next,
                     struct mm_walk *walk)
  {
-    return pfn_modify_allowed(pte_pfn(ptep_get(pte)),
+    pte_t entry = huge_ptep_get(walk->mm, addr, pte);
+
Quoted 8 lines

mm/mprotect.c static int prot_none_pte_entry(pte_t *pte, unsigned long addr, · line 713

Quoted 6 lines
mm/mprotect.c +3 -0
+    return pfn_modify_allowed(pte_pfn(entry),
                    *(pgprot_t *)(walk->private)) ?
          0 : -EACCES;
  }
+#else
+#define prot_none_hugetlb_entry    NULL

This is very strange, because we defined a stub as NULL for a helper

Quoted 2 lines

I was following pattern elsewhere, search for ".hugetlb_entry" in the codebase and you will find others doing the same.

Okay, I understand why you want to do it that way, but I would still recommend not following that format.

Thanks.

Quoted 25 lines
Quoted 25 lines

function. How about the following diff?

mm/mprotect.c +2 -0
index 9cbf932b028c..4d8c1551fbce 100644
@@ -716,7 +716,9 @@ static int prot_none_test(unsigned long addr, unsigned long next,
 
 static const struct mm_walk_ops prot_none_walk_ops = {
        .pte_entry              = prot_none_pte_entry,
+#ifdef CONFIG_HUGETLB_PAGE
        .hugetlb_entry          = prot_none_hugetlb_entry,
+#endif
        .test_walk              = prot_none_test,
        .walk_lock              = PGWALK_WRLOCK,
 };

Thanks,
Muchun

Quoted 3 lines
mm/mprotect.c +1 -0
+#endif
    static int prot_none_test(unsigned long addr, unsigned long next,
                struct mm_walk *walk)
author
#36 lore raw

Re: [PATCH 5/5] mm/mprotect: use huge_ptep_get() for hugetlb

Quoted 54 lines · Muchun Song
Quoted 54 lines · Dev Jain
Quoted 46 lines · Muchun Song
Quoted 31 lines · Dev Jain

prot_none_hugetlb_entry() is the hugetlb callback for the early mprotect(PROT_NONE) PFN permission walk on x86.

The callback passes the decoded PFN to pfn_modify_allowed(). For a hugetlb callback, the pte pointer refers to a hugetlb entry. On architectures where hugetlb entries need huge_ptep_get(), reading that entry with ptep_get() can make the permission check use the wrong PFN.

Use huge_ptep_get() before decoding the hugetlb PFN.

Currently there is no path which can trigger a bug: huge_ptep_get() is a simple ptep_get() for x86, and the prot_none walk occurs only for x86. But use the correct helper anyways.

Fixes 42e4089c7890 ("x86/speculation/l1tf: Disallow non privileged high MMIO PROT_NONE mappings")
---
  mm/mprotect.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
mm/mprotect.c +0 -0
index 9cbf932b028cf..23779632d18bf 100644
@@ -699,14 +699,20 @@ static int prot_none_pte_entry(pte_t *pte, unsigned long addr,
          0 : -EACCES;
  }
  +#ifdef CONFIG_HUGETLB_PAGE
  static int prot_none_hugetlb_entry(pte_t *pte, unsigned long hmask,
                     unsigned long addr, unsigned long next,
                     struct mm_walk *walk)

mm/mprotect.c static int prot_none_pte_entry(pte_t *pte, unsigned long addr, · line 713

Quoted 10 lines
mm/mprotect.c +5 -1
  {
-    return pfn_modify_allowed(pte_pfn(ptep_get(pte)),
+    pte_t entry = huge_ptep_get(walk->mm, addr, pte);
+
+    return pfn_modify_allowed(pte_pfn(entry),
                    *(pgprot_t *)(walk->private)) ?
          0 : -EACCES;
  }
+#else
+#define prot_none_hugetlb_entry    NULL

This is very strange, because we defined a stub as NULL for a helper

Quoted 5 lines
Quoted 2 lines

I was following pattern elsewhere, search for ".hugetlb_entry" in the codebase and you will find others doing the same.

Okay, I understand why you want to do it that way, but I would still recommend not following that format.

Okay then I'll update v2 with the below diff.

Quoted 31 lines

Thanks.

Quoted 25 lines
Quoted 25 lines

function. How about the following diff?

mm/mprotect.c +2 -0
index 9cbf932b028c..4d8c1551fbce 100644
@@ -716,7 +716,9 @@ static int prot_none_test(unsigned long addr, unsigned long next,
 
 static const struct mm_walk_ops prot_none_walk_ops = {
        .pte_entry              = prot_none_pte_entry,
+#ifdef CONFIG_HUGETLB_PAGE
        .hugetlb_entry          = prot_none_hugetlb_entry,
+#endif
        .test_walk              = prot_none_test,
        .walk_lock              = PGWALK_WRLOCK,
 };

Thanks,
Muchun

Quoted 3 lines
mm/mprotect.c +1 -0
+#endif
    static int prot_none_test(unsigned long addr, unsigned long next,
                struct mm_walk *walk)
reviewer
#12 lore raw

Re: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb

In reply to Dev Jain
Quoted 25 lines · Dev Jain

check_pte() is the final validation step in page_vma_mapped_walk(). It reads pvmw->pte with ptep_get() to decide whether the entry maps the PFN range being walked. For hugetlb VMAs, that pointer refers to a hugetlb entry.

On arches which provide their own huge_ptep_get() to dereference a huge pte pointer, accessing via ptep_get() would cause pte_pfn(), pte_present() etc to misbehave.

It is not clear whether this has a trivially visible effect to userspace.

Use huge_ptep_get() to dereference a huge pte pointer.

Fixes ace71a19cec5 ("mm: introduce page_vma_mapped_walk()")

---
mm/page_vma_mapped.c | 8 +++++++-

1 file changed, 7 insertions(+), 1 deletion(-)
mm/page_vma_mapped.c +0 -0
index 2ccbabfb2cc17..18e1d341f463c 100644

mm/page_vma_mapped.c static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp, · line 114

Quoted 9 lines
mm/page_vma_mapped.c +5 -1
@@ -107,7 +107,13 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp,
 static bool check_pte(struct page_vma_mapped_walk *pvmw, unsigned long pte_nr)
 {
 	unsigned long pfn;
-	pte_t ptent = ptep_get(pvmw->pte);
+	pte_t ptent;
+
+	if (is_vm_hugetlb_page(pvmw->vma))
+		ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
+				      pvmw->pte);

I think check_pte() can pass a wrong address to huge_ptep_get() ...

Not sure that is wrong in the first place. For memory failure, page_mapped_in_vma() can be called with a poisoned tail page of a hugetlb folio. In that case, pvmw->address need not be hugepage-aligned.

@Miaohe

For arm64, CONT_PMD_SIZE is one supported HugeTLB size. With such a VMA, page_vma_mapped_walk() passes that size to hugetlb_walk():

bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)

{
	...
	if (unlikely(is_vm_hugetlb_page(vma))) {
		...
		pvmw->pte = hugetlb_walk(vma, pvmw->address, size);
		...
	}
	...
}

hugetlb_walk() then calls arm64 huge_pte_offset(mm, addr, sz). For sz == CONT_PMD_SIZE, huge_pte_offset() aligns its local addr before calculating pmdp:

pte_t *huge_pte_offset(struct mm_struct *mm,
unsigned long addr, unsigned long sz)

{
	...
	if (sz == CONT_PMD_SIZE)
		addr &= CONT_PMD_MASK;

	pmdp = pmd_offset(pudp, addr);
	pmd = READ_ONCE(*pmdp);
	...
}

So for that case, pvmw->pte is calculated from the aligned addr, not necessarily from the original pvmw->address. But check_pte() passes the original address together with pvmw->pte:

+ ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
+ pvmw->pte);

arm64 then uses that addr again to choose ncontig:

pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)

{
	...
	ncontig = find_num_contig(mm, addr, ptep, &pgsize);
	for (i = 0; i < ncontig; i++, ptep++) {
		...
	}
	return orig_pte;
}

static int find_num_contig(struct mm_struct *mm, unsigned long addr,
pte_t *ptep, size_t *pgsize)

{
	pgd_t *pgdp = pgd_offset(mm, addr);
	p4d_t *p4dp;
	pud_t *pudp;
	pmd_t *pmdp;

	*pgsize = PAGE_SIZE;
	p4dp = p4d_offset(pgdp, addr);
	pudp = pud_offset(p4dp, addr);
	pmdp = pmd_offset(pudp, addr);
	if ((pte_t *)pmdp == ptep) {
		*pgsize = PMD_SIZE;
		return CONT_PMDS;
	}
	return CONT_PTES;
}

With a tail address, pmdp may no longer point at pvmw->pte, so find_num_contig() can return CONT_PTES for a CONT_PMD HugeTLB mapping.

On 16K arm64, that changes ncontig from 32 to 128. So huge_ptep_get() can walk past the CONT_PMD entries, and possibly past the PMD table.

Should check_pte() pass the address matching pvmw->pte, sth like:

---8<---

mm/page_vma_mapped.c +7 -4
index 406fd50bbd8f..58463493bd3d 100644
@@ -109,11 +109,14 @@ static bool check_pte(struct page_vma_mapped_walk *pvmw, unsigned long pte_nr)
 	unsigned long pfn;
 	pte_t ptent;
 
-	if (is_vm_hugetlb_page(pvmw->vma))
-		ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
-				      pvmw->pte);
-	else
+	if (is_vm_hugetlb_page(pvmw->vma)) {
+		struct hstate *hstate = hstate_vma(pvmw->vma);
+		unsigned long haddr = pvmw->address & huge_page_mask(hstate);
+
+		ptent = huge_ptep_get(pvmw->vma->vm_mm, haddr, pvmw->pte);
+	} else {
 		ptent = ptep_get(pvmw->pte);
+	}
 
 	if (pvmw->flags & PVMW_MIGRATION) {
 		const softleaf_t entry = softleaf_from_pte(ptent);

--

while leaving pvmw->address unchanged for page_mapped_in_vma()?

Cheers, Lance

Quoted 9 lines
mm/page_vma_mapped.c +2 -0
+	else
+		ptent = ptep_get(pvmw->pte);
 
 	if (pvmw->flags & PVMW_MIGRATION) {
 		const softleaf_t entry = softleaf_from_pte(ptent);
Signature · 1 line
2.43.0
reviewer
#13 lore raw

Re: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb

Quoted 79 lines · Lance Yang
Quoted 25 lines · Dev Jain

check_pte() is the final validation step in page_vma_mapped_walk(). It reads pvmw->pte with ptep_get() to decide whether the entry maps the PFN range being walked. For hugetlb VMAs, that pointer refers to a hugetlb entry.

On arches which provide their own huge_ptep_get() to dereference a huge pte pointer, accessing via ptep_get() would cause pte_pfn(), pte_present() etc to misbehave.

It is not clear whether this has a trivially visible effect to userspace.

Use huge_ptep_get() to dereference a huge pte pointer.

Fixes ace71a19cec5 ("mm: introduce page_vma_mapped_walk()")

---
mm/page_vma_mapped.c | 8 +++++++-

1 file changed, 7 insertions(+), 1 deletion(-)
mm/page_vma_mapped.c +0 -0
index 2ccbabfb2cc17..18e1d341f463c 100644

mm/page_vma_mapped.c static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp, · line 114

Quoted 9 lines
mm/page_vma_mapped.c +5 -1
@@ -107,7 +107,13 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp,
 static bool check_pte(struct page_vma_mapped_walk *pvmw, unsigned long pte_nr)
 {
 	unsigned long pfn;
-	pte_t ptent = ptep_get(pvmw->pte);
+	pte_t ptent;
+
+	if (is_vm_hugetlb_page(pvmw->vma))
+		ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
+				      pvmw->pte);

I think check_pte() can pass a wrong address to huge_ptep_get() ...

Not sure that is wrong in the first place. For memory failure, page_mapped_in_vma() can be called with a poisoned tail page of a hugetlb folio. In that case, pvmw->address need not be hugepage-aligned.

@Miaohe

For arm64, CONT_PMD_SIZE is one supported HugeTLB size. With such a VMA, page_vma_mapped_walk() passes that size to hugetlb_walk():

bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)

{
	...
	if (unlikely(is_vm_hugetlb_page(vma))) {
		...
		pvmw->pte = hugetlb_walk(vma, pvmw->address, size);
		...
	}
	...
}

hugetlb_walk() then calls arm64 huge_pte_offset(mm, addr, sz). For sz == CONT_PMD_SIZE, huge_pte_offset() aligns its local addr before calculating pmdp:

pte_t *huge_pte_offset(struct mm_struct *mm,
unsigned long addr, unsigned long sz)

{
	...
	if (sz == CONT_PMD_SIZE)
		addr &= CONT_PMD_MASK;

	pmdp = pmd_offset(pudp, addr);
	pmd = READ_ONCE(*pmdp);
	...
}

So for that case, pvmw->pte is calculated from the aligned addr, not necessarily from the original pvmw->address. But check_pte() passes the original address together with pvmw->pte:

Review

Quoted 2 lines
(quoted diff) +2 -0
+		ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
+				      pvmw->pte);

In addition:

Went through all arch code that has its own huge_ptep_get(); only arm64 and powerpc actually use addr, and there addr has to match the ptep, IIUC.

So I am wondering whether all huge_ptep_get() callers satisfy that requirement.

Cheers, Lance

Quoted 79 lines

arm64 then uses that addr again to choose ncontig:

pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)

{
	...
	ncontig = find_num_contig(mm, addr, ptep, &pgsize);
	for (i = 0; i < ncontig; i++, ptep++) {
		...
	}
	return orig_pte;
}

static int find_num_contig(struct mm_struct *mm, unsigned long addr,
pte_t *ptep, size_t *pgsize)

{
	pgd_t *pgdp = pgd_offset(mm, addr);
	p4d_t *p4dp;
	pud_t *pudp;
	pmd_t *pmdp;

	*pgsize = PAGE_SIZE;
	p4dp = p4d_offset(pgdp, addr);
	pudp = pud_offset(p4dp, addr);
	pmdp = pmd_offset(pudp, addr);
	if ((pte_t *)pmdp == ptep) {
		*pgsize = PMD_SIZE;
		return CONT_PMDS;
	}
	return CONT_PTES;
}

With a tail address, pmdp may no longer point at pvmw->pte, so find_num_contig() can return CONT_PTES for a CONT_PMD HugeTLB mapping.

On 16K arm64, that changes ncontig from 32 to 128. So huge_ptep_get() can walk past the CONT_PMD entries, and possibly past the PMD table.

Should check_pte() pass the address matching pvmw->pte, sth like:

---8<---

mm/page_vma_mapped.c +7 -5
index 406fd50bbd8f..58463493bd3d 100644
@@ -109,11 +109,14 @@ static bool check_pte(struct page_vma_mapped_walk *pvmw, unsigned long pte_nr)
  	unsigned long pfn;
  	pte_t ptent;
 
-	if (is_vm_hugetlb_page(pvmw->vma))
-		ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
-				      pvmw->pte);
-	else
+	if (is_vm_hugetlb_page(pvmw->vma)) {
+		struct hstate *hstate = hstate_vma(pvmw->vma);
+		unsigned long haddr = pvmw->address & huge_page_mask(hstate);
+
+		ptent = huge_ptep_get(pvmw->vma->vm_mm, haddr, pvmw->pte);
+	} else {
  		ptent = ptep_get(pvmw->pte);
+	}
 
  	if (pvmw->flags & PVMW_MIGRATION) {
  		const softleaf_t entry = softleaf_from_pte(ptent);
--

while leaving pvmw->address unchanged for page_mapped_in_vma()?

Cheers, Lance

Quoted 9 lines
mm/page_vma_mapped.c +2 -0
+	else
+		ptent = ptep_get(pvmw->pte);
 
 	if (pvmw->flags & PVMW_MIGRATION) {
 		const softleaf_t entry = softleaf_from_pte(ptent);
Signature · 1 line
2.43.0
author
#14 lore raw

Re: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb

In reply to Lance Yang
Quoted 22 lines · Lance Yang
Quoted 20 lines · Dev Jain

check_pte() is the final validation step in page_vma_mapped_walk(). It reads pvmw->pte with ptep_get() to decide whether the entry maps the PFN range being walked. For hugetlb VMAs, that pointer refers to a hugetlb entry.

On arches which provide their own huge_ptep_get() to dereference a huge pte pointer, accessing via ptep_get() would cause pte_pfn(), pte_present() etc to misbehave.

It is not clear whether this has a trivially visible effect to userspace.

Use huge_ptep_get() to dereference a huge pte pointer.

Fixes ace71a19cec5 ("mm: introduce page_vma_mapped_walk()")

---
mm/page_vma_mapped.c | 8 +++++++-

1 file changed, 7 insertions(+), 1 deletion(-)
Quoted 16 lines
Quoted 5 lines
mm/page_vma_mapped.c +0 -0
index 2ccbabfb2cc17..18e1d341f463c 100644

mm/page_vma_mapped.c static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp, · line 114

Quoted 9 lines
mm/page_vma_mapped.c +5 -1
@@ -107,7 +107,13 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp,
 static bool check_pte(struct page_vma_mapped_walk *pvmw, unsigned long pte_nr)
 {
 	unsigned long pfn;
-	pte_t ptent = ptep_get(pvmw->pte);
+	pte_t ptent;
+
+	if (is_vm_hugetlb_page(pvmw->vma))
+		ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
+				      pvmw->pte);

I think check_pte() can pass a wrong address to huge_ptep_get() ...

Won't this be handled by rmap_walk_anon/rmap_walk_file - they are the ones performing the rmap traversal and passing address to try_to_unmap_one/folio_referenced_one etc ...

Quoted 122 lines

Not sure that is wrong in the first place. For memory failure, page_mapped_in_vma() can be called with a poisoned tail page of a hugetlb folio. In that case, pvmw->address need not be hugepage-aligned.

@Miaohe

For arm64, CONT_PMD_SIZE is one supported HugeTLB size. With such a VMA, page_vma_mapped_walk() passes that size to hugetlb_walk():

bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)

{
	...
	if (unlikely(is_vm_hugetlb_page(vma))) {
		...
		pvmw->pte = hugetlb_walk(vma, pvmw->address, size);
		...
	}
	...
}

hugetlb_walk() then calls arm64 huge_pte_offset(mm, addr, sz). For sz == CONT_PMD_SIZE, huge_pte_offset() aligns its local addr before calculating pmdp:

pte_t *huge_pte_offset(struct mm_struct *mm,
unsigned long addr, unsigned long sz)

{
	...
	if (sz == CONT_PMD_SIZE)
		addr &= CONT_PMD_MASK;

	pmdp = pmd_offset(pudp, addr);
	pmd = READ_ONCE(*pmdp);
	...
}

So for that case, pvmw->pte is calculated from the aligned addr, not necessarily from the original pvmw->address. But check_pte() passes the original address together with pvmw->pte:

(quoted diff) +2 -0
+		ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
+				      pvmw->pte);

arm64 then uses that addr again to choose ncontig:

pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)

{
	...
	ncontig = find_num_contig(mm, addr, ptep, &pgsize);
	for (i = 0; i < ncontig; i++, ptep++) {
		...
	}
	return orig_pte;
}

static int find_num_contig(struct mm_struct *mm, unsigned long addr,
pte_t *ptep, size_t *pgsize)

{
	pgd_t *pgdp = pgd_offset(mm, addr);
	p4d_t *p4dp;
	pud_t *pudp;
	pmd_t *pmdp;

	*pgsize = PAGE_SIZE;
	p4dp = p4d_offset(pgdp, addr);
	pudp = pud_offset(p4dp, addr);
	pmdp = pmd_offset(pudp, addr);
	if ((pte_t *)pmdp == ptep) {
		*pgsize = PMD_SIZE;
		return CONT_PMDS;
	}
	return CONT_PTES;
}

With a tail address, pmdp may no longer point at pvmw->pte, so find_num_contig() can return CONT_PTES for a CONT_PMD HugeTLB mapping.

On 16K arm64, that changes ncontig from 32 to 128. So huge_ptep_get() can walk past the CONT_PMD entries, and possibly past the PMD table.

Should check_pte() pass the address matching pvmw->pte, sth like:

---8<---

mm/page_vma_mapped.c +7 -5
index 406fd50bbd8f..58463493bd3d 100644
@@ -109,11 +109,14 @@ static bool check_pte(struct page_vma_mapped_walk *pvmw, unsigned long pte_nr)
 	unsigned long pfn;
 	pte_t ptent;
 
-	if (is_vm_hugetlb_page(pvmw->vma))
-		ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
-				      pvmw->pte);
-	else
+	if (is_vm_hugetlb_page(pvmw->vma)) {
+		struct hstate *hstate = hstate_vma(pvmw->vma);
+		unsigned long haddr = pvmw->address & huge_page_mask(hstate);
+
+		ptent = huge_ptep_get(pvmw->vma->vm_mm, haddr, pvmw->pte);
+	} else {
 		ptent = ptep_get(pvmw->pte);
+	}
 
 	if (pvmw->flags & PVMW_MIGRATION) {
 		const softleaf_t entry = softleaf_from_pte(ptent);
--

while leaving pvmw->address unchanged for page_mapped_in_vma()?

Cheers, Lance

Quoted 9 lines
mm/page_vma_mapped.c +2 -0
+	else
+		ptent = ptep_get(pvmw->pte);
 
 	if (pvmw->flags & PVMW_MIGRATION) {
 		const softleaf_t entry = softleaf_from_pte(ptent);
Signature · 1 line
2.43.0
reviewer
#15 lore raw

Re: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb

Quoted 42 lines · Dev Jain
Quoted 22 lines · Lance Yang
Quoted 20 lines · Dev Jain

check_pte() is the final validation step in page_vma_mapped_walk(). It reads pvmw->pte with ptep_get() to decide whether the entry maps the PFN range being walked. For hugetlb VMAs, that pointer refers to a hugetlb entry.

On arches which provide their own huge_ptep_get() to dereference a huge pte pointer, accessing via ptep_get() would cause pte_pfn(), pte_present() etc to misbehave.

It is not clear whether this has a trivially visible effect to userspace.

Use huge_ptep_get() to dereference a huge pte pointer.

Fixes ace71a19cec5 ("mm: introduce page_vma_mapped_walk()")

---
mm/page_vma_mapped.c | 8 +++++++-

1 file changed, 7 insertions(+), 1 deletion(-)
Quoted 16 lines
Quoted 5 lines
mm/page_vma_mapped.c +0 -0
index 2ccbabfb2cc17..18e1d341f463c 100644

mm/page_vma_mapped.c static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp, · line 114

Quoted 9 lines
mm/page_vma_mapped.c +5 -1
@@ -107,7 +107,13 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp,
 static bool check_pte(struct page_vma_mapped_walk *pvmw, unsigned long pte_nr)
 {
 	unsigned long pfn;
-	pte_t ptent = ptep_get(pvmw->pte);
+	pte_t ptent;
+
+	if (is_vm_hugetlb_page(pvmw->vma))
+		ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
+				      pvmw->pte);

I think check_pte() can pass a wrong address to huge_ptep_get() ...

Quoted 3 lines

Won't this be handled by rmap_walk_anon/rmap_walk_file - they are the ones performing the rmap traversal and passing address to try_to_unmap_one/folio_referenced_one etc ...

Right, that should cover the rmap callbacks. The bit I was worried about is page_mapped_in_vma() though.

Quoted 6 lines
Quoted 6 lines

Not sure that is wrong in the first place. For memory failure, page_mapped_in_vma() can be called with a poisoned tail page of a hugetlb folio. In that case, pvmw->address need not be hugepage-aligned.

@Miaohe

For hugetlb memory failure we start with the poisoned PFN:

static int try_memory_failure_hugetlb(unsigned long pfn, int flags)

{
	...
	struct page *p = pfn_to_page(pfn);
	struct folio *folio;
	...

	folio = page_folio(p);

	...

	if (!hwpoison_user_mappings(folio, p, pfn, flags)) {
		...
	}

	...
}

and pass the same p down:

static bool hwpoison_user_mappings(struct folio *folio, struct page *p,
unsigned long pfn, int flags)

{
	...

	collect_procs(folio, p, &tokill, flags & MF_ACTION_REQUIRED);

	...
}

static void collect_procs(const struct folio *folio, const struct page *page,
struct list_head *tokill, int force_early)

{
	...

	if (unlikely(folio_test_ksm(folio)))
		...
	else if (folio_test_anon(folio))
		collect_procs_anon(folio, page, tokill, force_early);
	else
		...
}

So collect_procs_anon() still gets the poisoned page, not &folio->page:

static void collect_procs_anon(const struct folio *folio,
const struct page *page, struct list_head *to_kill, int force_early)

{
	...

	pgoff = page_pgoff(folio, page);
	rcu_read_lock();
	for_each_process(tsk) {
		...
		
		anon_vma_interval_tree_foreach(vmac, &av->rb_root,
					       pgoff, pgoff) {
			...
			addr = page_mapped_in_vma(page, vma);
			...
		}
	}
	rcu_read_unlock();
	anon_vma_unlock_read(av);
}

page_mapped_in_vma() then builds pvmw for that page:

unsigned long page_mapped_in_vma(const struct page *page,
struct vm_area_struct *vma)

{
	const struct folio *folio = page_folio(page);
	struct page_vma_mapped_walk pvmw = {
		.pfn = page_to_pfn(page),
		.nr_pages = 1,
		.vma = vma,
		.flags = PVMW_SYNC,
	};

	pvmw.address = vma_address(vma, page_pgoff(folio, page), 1);
	...
}

and page_pgoff() includes the subpage index:

static inline pgoff_t page_pgoff(const struct folio *folio,
const struct page *page)

{
	return folio->index + folio_page_idx(folio, page);
}

So if the poisoned PFN points to a tail page, pvmw->address can be offset from the start of the hugetlb mapping by

folio_page_idx(folio, page) << PAGE_SHIFT

Should check_pte() pass the hugepage-aligned address to huge_ptep_get() for that case?

Cheers, Lance

Quoted 118 lines
Quoted 116 lines

For arm64, CONT_PMD_SIZE is one supported HugeTLB size. With such a VMA, page_vma_mapped_walk() passes that size to hugetlb_walk():

bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)

{
	...
	if (unlikely(is_vm_hugetlb_page(vma))) {
		...
		pvmw->pte = hugetlb_walk(vma, pvmw->address, size);
		...
	}
	...
}

hugetlb_walk() then calls arm64 huge_pte_offset(mm, addr, sz). For sz == CONT_PMD_SIZE, huge_pte_offset() aligns its local addr before calculating pmdp:

pte_t *huge_pte_offset(struct mm_struct *mm,
unsigned long addr, unsigned long sz)

{
	...
	if (sz == CONT_PMD_SIZE)
		addr &= CONT_PMD_MASK;

	pmdp = pmd_offset(pudp, addr);
	pmd = READ_ONCE(*pmdp);
	...
}

So for that case, pvmw->pte is calculated from the aligned addr, not necessarily from the original pvmw->address. But check_pte() passes the original address together with pvmw->pte:

(quoted diff) +2 -0
+		ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
+				      pvmw->pte);

arm64 then uses that addr again to choose ncontig:

pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)

{
	...
	ncontig = find_num_contig(mm, addr, ptep, &pgsize);
	for (i = 0; i < ncontig; i++, ptep++) {
		...
	}
	return orig_pte;
}

static int find_num_contig(struct mm_struct *mm, unsigned long addr,
pte_t *ptep, size_t *pgsize)

{
	pgd_t *pgdp = pgd_offset(mm, addr);
	p4d_t *p4dp;
	pud_t *pudp;
	pmd_t *pmdp;

	*pgsize = PAGE_SIZE;
	p4dp = p4d_offset(pgdp, addr);
	pudp = pud_offset(p4dp, addr);
	pmdp = pmd_offset(pudp, addr);
	if ((pte_t *)pmdp == ptep) {
		*pgsize = PMD_SIZE;
		return CONT_PMDS;
	}
	return CONT_PTES;
}

With a tail address, pmdp may no longer point at pvmw->pte, so find_num_contig() can return CONT_PTES for a CONT_PMD HugeTLB mapping.

On 16K arm64, that changes ncontig from 32 to 128. So huge_ptep_get() can walk past the CONT_PMD entries, and possibly past the PMD table.

Should check_pte() pass the address matching pvmw->pte, sth like:

---8<---

mm/page_vma_mapped.c +7 -5
index 406fd50bbd8f..58463493bd3d 100644
@@ -109,11 +109,14 @@ static bool check_pte(struct page_vma_mapped_walk *pvmw, unsigned long pte_nr)
 	unsigned long pfn;
 	pte_t ptent;
 
-	if (is_vm_hugetlb_page(pvmw->vma))
-		ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
-				      pvmw->pte);
-	else
+	if (is_vm_hugetlb_page(pvmw->vma)) {
+		struct hstate *hstate = hstate_vma(pvmw->vma);
+		unsigned long haddr = pvmw->address & huge_page_mask(hstate);
+
+		ptent = huge_ptep_get(pvmw->vma->vm_mm, haddr, pvmw->pte);
+	} else {
 		ptent = ptep_get(pvmw->pte);
+	}
 
 	if (pvmw->flags & PVMW_MIGRATION) {
 		const softleaf_t entry = softleaf_from_pte(ptent);
--

while leaving pvmw->address unchanged for page_mapped_in_vma()?

Cheers, Lance

Quoted 9 lines
mm/page_vma_mapped.c +2 -0
+	else
+		ptent = ptep_get(pvmw->pte);
 
 	if (pvmw->flags & PVMW_MIGRATION) {
 		const softleaf_t entry = softleaf_from_pte(ptent);
Signature · 1 line
2.43.0
author
#16 lore raw

Re: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb

Quoted 152 lines · Lance Yang
Quoted 42 lines · Dev Jain
Quoted 22 lines · Lance Yang
Quoted 20 lines · Dev Jain

check_pte() is the final validation step in page_vma_mapped_walk(). It reads pvmw->pte with ptep_get() to decide whether the entry maps the PFN range being walked. For hugetlb VMAs, that pointer refers to a hugetlb entry.

On arches which provide their own huge_ptep_get() to dereference a huge pte pointer, accessing via ptep_get() would cause pte_pfn(), pte_present() etc to misbehave.

It is not clear whether this has a trivially visible effect to userspace.

Use huge_ptep_get() to dereference a huge pte pointer.

Fixes ace71a19cec5 ("mm: introduce page_vma_mapped_walk()")

---
mm/page_vma_mapped.c | 8 +++++++-

1 file changed, 7 insertions(+), 1 deletion(-)
Quoted 16 lines
Quoted 5 lines
mm/page_vma_mapped.c +0 -0
index 2ccbabfb2cc17..18e1d341f463c 100644

mm/page_vma_mapped.c static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp, · line 114

Quoted 9 lines
mm/page_vma_mapped.c +5 -1
@@ -107,7 +107,13 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp,
 static bool check_pte(struct page_vma_mapped_walk *pvmw, unsigned long pte_nr)
 {
 	unsigned long pfn;
-	pte_t ptent = ptep_get(pvmw->pte);
+	pte_t ptent;
+
+	if (is_vm_hugetlb_page(pvmw->vma))
+		ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
+				      pvmw->pte);

I think check_pte() can pass a wrong address to huge_ptep_get() ...

Quoted 3 lines

Won't this be handled by rmap_walk_anon/rmap_walk_file - they are the ones performing the rmap traversal and passing address to try_to_unmap_one/folio_referenced_one etc ...

Right, that should cover the rmap callbacks. The bit I was worried about is page_mapped_in_vma() though.

Quoted 6 lines
Quoted 6 lines

Not sure that is wrong in the first place. For memory failure, page_mapped_in_vma() can be called with a poisoned tail page of a hugetlb folio. In that case, pvmw->address need not be hugepage-aligned.

@Miaohe

For hugetlb memory failure we start with the poisoned PFN:

static int try_memory_failure_hugetlb(unsigned long pfn, int flags)

{
	...
	struct page *p = pfn_to_page(pfn);
	struct folio *folio;
	...

	folio = page_folio(p);

	...

	if (!hwpoison_user_mappings(folio, p, pfn, flags)) {
		...
	}

	...
}

and pass the same p down:

static bool hwpoison_user_mappings(struct folio *folio, struct page *p,
unsigned long pfn, int flags)

{
	...

	collect_procs(folio, p, &tokill, flags & MF_ACTION_REQUIRED);

	...
}

static void collect_procs(const struct folio *folio, const struct page *page,
struct list_head *tokill, int force_early)

{
	...

	if (unlikely(folio_test_ksm(folio)))
		...
	else if (folio_test_anon(folio))
		collect_procs_anon(folio, page, tokill, force_early);
	else
		...
}

So collect_procs_anon() still gets the poisoned page, not &folio->page:

static void collect_procs_anon(const struct folio *folio,
const struct page *page, struct list_head *to_kill, int force_early)

{
	...

	pgoff = page_pgoff(folio, page);
	rcu_read_lock();
	for_each_process(tsk) {
		...
		
		anon_vma_interval_tree_foreach(vmac, &av->rb_root,
					       pgoff, pgoff) {
			...
			addr = page_mapped_in_vma(page, vma);
			...
		}
	}
	rcu_read_unlock();
	anon_vma_unlock_read(av);
}

page_mapped_in_vma() then builds pvmw for that page:

unsigned long page_mapped_in_vma(const struct page *page,
struct vm_area_struct *vma)

{
	const struct folio *folio = page_folio(page);
	struct page_vma_mapped_walk pvmw = {
		.pfn = page_to_pfn(page),
		.nr_pages = 1,
		.vma = vma,
		.flags = PVMW_SYNC,
	};

	pvmw.address = vma_address(vma, page_pgoff(folio, page), 1);
	...
}

and page_pgoff() includes the subpage index:

static inline pgoff_t page_pgoff(const struct folio *folio,
const struct page *page)

{
	return folio->index + folio_page_idx(folio, page);
}
Quoted 7 lines

So if the poisoned PFN points to a tail page, pvmw->address can be offset from the start of the hugetlb mapping by

folio_page_idx(folio, page) << PAGE_SHIFT

Should check_pte() pass the hugepage-aligned address to huge_ptep_get() for that case?

Thanks! This looks correct.

I can indeed fix this up in check_pte. But in the memory-failure path it has always been confusing to me for hugetlb folios why we are bothering with the tail page. I am sure that area can also be simplified. But for now I'll just do a simple fix here itself.

Quoted 122 lines

Cheers, Lance

Quoted 118 lines
Quoted 116 lines

For arm64, CONT_PMD_SIZE is one supported HugeTLB size. With such a VMA, page_vma_mapped_walk() passes that size to hugetlb_walk():

bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)

{
	...
	if (unlikely(is_vm_hugetlb_page(vma))) {
		...
		pvmw->pte = hugetlb_walk(vma, pvmw->address, size);
		...
	}
	...
}

hugetlb_walk() then calls arm64 huge_pte_offset(mm, addr, sz). For sz == CONT_PMD_SIZE, huge_pte_offset() aligns its local addr before calculating pmdp:

pte_t *huge_pte_offset(struct mm_struct *mm,
unsigned long addr, unsigned long sz)

{
	...
	if (sz == CONT_PMD_SIZE)
		addr &= CONT_PMD_MASK;

	pmdp = pmd_offset(pudp, addr);
	pmd = READ_ONCE(*pmdp);
	...
}

So for that case, pvmw->pte is calculated from the aligned addr, not necessarily from the original pvmw->address. But check_pte() passes the original address together with pvmw->pte:

(quoted diff) +2 -0
+		ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
+				      pvmw->pte);

arm64 then uses that addr again to choose ncontig:

pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)

{
	...
	ncontig = find_num_contig(mm, addr, ptep, &pgsize);
	for (i = 0; i < ncontig; i++, ptep++) {
		...
	}
	return orig_pte;
}

static int find_num_contig(struct mm_struct *mm, unsigned long addr,
pte_t *ptep, size_t *pgsize)

{
	pgd_t *pgdp = pgd_offset(mm, addr);
	p4d_t *p4dp;
	pud_t *pudp;
	pmd_t *pmdp;

	*pgsize = PAGE_SIZE;
	p4dp = p4d_offset(pgdp, addr);
	pudp = pud_offset(p4dp, addr);
	pmdp = pmd_offset(pudp, addr);
	if ((pte_t *)pmdp == ptep) {
		*pgsize = PMD_SIZE;
		return CONT_PMDS;
	}
	return CONT_PTES;
}

With a tail address, pmdp may no longer point at pvmw->pte, so find_num_contig() can return CONT_PTES for a CONT_PMD HugeTLB mapping.

On 16K arm64, that changes ncontig from 32 to 128. So huge_ptep_get() can walk past the CONT_PMD entries, and possibly past the PMD table.

Should check_pte() pass the address matching pvmw->pte, sth like:

---8<---

mm/page_vma_mapped.c +7 -5
index 406fd50bbd8f..58463493bd3d 100644
@@ -109,11 +109,14 @@ static bool check_pte(struct page_vma_mapped_walk *pvmw, unsigned long pte_nr)
 	unsigned long pfn;
 	pte_t ptent;
 
-	if (is_vm_hugetlb_page(pvmw->vma))
-		ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
-				      pvmw->pte);
-	else
+	if (is_vm_hugetlb_page(pvmw->vma)) {
+		struct hstate *hstate = hstate_vma(pvmw->vma);
+		unsigned long haddr = pvmw->address & huge_page_mask(hstate);
+
+		ptent = huge_ptep_get(pvmw->vma->vm_mm, haddr, pvmw->pte);
+	} else {
 		ptent = ptep_get(pvmw->pte);
+	}
 
 	if (pvmw->flags & PVMW_MIGRATION) {
 		const softleaf_t entry = softleaf_from_pte(ptent);
--

while leaving pvmw->address unchanged for page_mapped_in_vma()?

Cheers, Lance

Quoted 9 lines
mm/page_vma_mapped.c +2 -0
+	else
+		ptent = ptep_get(pvmw->pte);
 
 	if (pvmw->flags & PVMW_MIGRATION) {
 		const softleaf_t entry = softleaf_from_pte(ptent);
Signature · 1 line
2.43.0
reviewer
#17 lore raw

Re: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb

Quoted 163 lines · Dev Jain
Quoted 152 lines · Lance Yang
Quoted 42 lines · Dev Jain
Quoted 22 lines · Lance Yang
Quoted 20 lines · Dev Jain

check_pte() is the final validation step in page_vma_mapped_walk(). It reads pvmw->pte with ptep_get() to decide whether the entry maps the PFN range being walked. For hugetlb VMAs, that pointer refers to a hugetlb entry.

On arches which provide their own huge_ptep_get() to dereference a huge pte pointer, accessing via ptep_get() would cause pte_pfn(), pte_present() etc to misbehave.

It is not clear whether this has a trivially visible effect to userspace.

Use huge_ptep_get() to dereference a huge pte pointer.

Fixes ace71a19cec5 ("mm: introduce page_vma_mapped_walk()")

---
mm/page_vma_mapped.c | 8 +++++++-

1 file changed, 7 insertions(+), 1 deletion(-)
Quoted 16 lines
Quoted 5 lines
mm/page_vma_mapped.c +0 -0
index 2ccbabfb2cc17..18e1d341f463c 100644

mm/page_vma_mapped.c static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp, · line 114

Quoted 9 lines
mm/page_vma_mapped.c +5 -1
@@ -107,7 +107,13 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp,
 static bool check_pte(struct page_vma_mapped_walk *pvmw, unsigned long pte_nr)
 {
 	unsigned long pfn;
-	pte_t ptent = ptep_get(pvmw->pte);
+	pte_t ptent;
+
+	if (is_vm_hugetlb_page(pvmw->vma))
+		ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
+				      pvmw->pte);

I think check_pte() can pass a wrong address to huge_ptep_get() ...

Quoted 3 lines

Won't this be handled by rmap_walk_anon/rmap_walk_file - they are the ones performing the rmap traversal and passing address to try_to_unmap_one/folio_referenced_one etc ...

Right, that should cover the rmap callbacks. The bit I was worried about is page_mapped_in_vma() though.

Quoted 6 lines
Quoted 6 lines

Not sure that is wrong in the first place. For memory failure, page_mapped_in_vma() can be called with a poisoned tail page of a hugetlb folio. In that case, pvmw->address need not be hugepage-aligned.

@Miaohe

For hugetlb memory failure we start with the poisoned PFN:

static int try_memory_failure_hugetlb(unsigned long pfn, int flags)

{
	...
	struct page *p = pfn_to_page(pfn);
	struct folio *folio;
	...

	folio = page_folio(p);

	...

	if (!hwpoison_user_mappings(folio, p, pfn, flags)) {
		...
	}

	...
}

and pass the same p down:

static bool hwpoison_user_mappings(struct folio *folio, struct page *p,
unsigned long pfn, int flags)

{
	...

	collect_procs(folio, p, &tokill, flags & MF_ACTION_REQUIRED);

	...
}

static void collect_procs(const struct folio *folio, const struct page *page,
struct list_head *tokill, int force_early)

{
	...

	if (unlikely(folio_test_ksm(folio)))
		...
	else if (folio_test_anon(folio))
		collect_procs_anon(folio, page, tokill, force_early);
	else
		...
}

So collect_procs_anon() still gets the poisoned page, not &folio->page:

static void collect_procs_anon(const struct folio *folio,
const struct page *page, struct list_head *to_kill, int force_early)

{
	...

	pgoff = page_pgoff(folio, page);
	rcu_read_lock();
	for_each_process(tsk) {
		...
		
		anon_vma_interval_tree_foreach(vmac, &av->rb_root,
					       pgoff, pgoff) {
			...
			addr = page_mapped_in_vma(page, vma);
			...
		}
	}
	rcu_read_unlock();
	anon_vma_unlock_read(av);
}

page_mapped_in_vma() then builds pvmw for that page:

unsigned long page_mapped_in_vma(const struct page *page,
struct vm_area_struct *vma)

{
	const struct folio *folio = page_folio(page);
	struct page_vma_mapped_walk pvmw = {
		.pfn = page_to_pfn(page),
		.nr_pages = 1,
		.vma = vma,
		.flags = PVMW_SYNC,
	};

	pvmw.address = vma_address(vma, page_pgoff(folio, page), 1);
	...
}

and page_pgoff() includes the subpage index:

static inline pgoff_t page_pgoff(const struct folio *folio,
const struct page *page)

{
	return folio->index + folio_page_idx(folio, page);
}
Quoted 7 lines

So if the poisoned PFN points to a tail page, pvmw->address can be offset from the start of the hugetlb mapping by

folio_page_idx(folio, page) << PAGE_SHIFT

Should check_pte() pass the hugepage-aligned address to huge_ptep_get() for that case?

Quoted 6 lines

Thanks! This looks correct.

I can indeed fix this up in check_pte. But in the memory-failure path it has always been confusing to me for hugetlb folios why we are bothering with the tail page. I am sure that area can also be simplified. But for now I'll just do a simple fix here itself.

Just thinking out loud: given that huge_ptep_get() already assumes that addr matches the huge pte, at least on arm64, would it make sense to have a small hugetlb wrapper around it that takes hstate and aligns the address before calling the arch helper?

Might make the rule clearer, and a bit harder to get wrong again :)

Thanks, Lance

Quoted 124 lines
Quoted 123 lines

Cheers, Lance

Quoted 118 lines
Quoted 116 lines

For arm64, CONT_PMD_SIZE is one supported HugeTLB size. With such a VMA, page_vma_mapped_walk() passes that size to hugetlb_walk():

bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)

{
	...
	if (unlikely(is_vm_hugetlb_page(vma))) {
		...
		pvmw->pte = hugetlb_walk(vma, pvmw->address, size);
		...
	}
	...
}

hugetlb_walk() then calls arm64 huge_pte_offset(mm, addr, sz). For sz == CONT_PMD_SIZE, huge_pte_offset() aligns its local addr before calculating pmdp:

pte_t *huge_pte_offset(struct mm_struct *mm,
unsigned long addr, unsigned long sz)

{
	...
	if (sz == CONT_PMD_SIZE)
		addr &= CONT_PMD_MASK;

	pmdp = pmd_offset(pudp, addr);
	pmd = READ_ONCE(*pmdp);
	...
}

So for that case, pvmw->pte is calculated from the aligned addr, not necessarily from the original pvmw->address. But check_pte() passes the original address together with pvmw->pte:

(quoted diff) +2 -0
+		ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
+				      pvmw->pte);

arm64 then uses that addr again to choose ncontig:

pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)

{
	...
	ncontig = find_num_contig(mm, addr, ptep, &pgsize);
	for (i = 0; i < ncontig; i++, ptep++) {
		...
	}
	return orig_pte;
}

static int find_num_contig(struct mm_struct *mm, unsigned long addr,
pte_t *ptep, size_t *pgsize)

{
	pgd_t *pgdp = pgd_offset(mm, addr);
	p4d_t *p4dp;
	pud_t *pudp;
	pmd_t *pmdp;

	*pgsize = PAGE_SIZE;
	p4dp = p4d_offset(pgdp, addr);
	pudp = pud_offset(p4dp, addr);
	pmdp = pmd_offset(pudp, addr);
	if ((pte_t *)pmdp == ptep) {
		*pgsize = PMD_SIZE;
		return CONT_PMDS;
	}
	return CONT_PTES;
}

With a tail address, pmdp may no longer point at pvmw->pte, so find_num_contig() can return CONT_PTES for a CONT_PMD HugeTLB mapping.

On 16K arm64, that changes ncontig from 32 to 128. So huge_ptep_get() can walk past the CONT_PMD entries, and possibly past the PMD table.

Should check_pte() pass the address matching pvmw->pte, sth like:

---8<---

mm/page_vma_mapped.c +7 -5
index 406fd50bbd8f..58463493bd3d 100644
@@ -109,11 +109,14 @@ static bool check_pte(struct page_vma_mapped_walk *pvmw, unsigned long pte_nr)
  	unsigned long pfn;
  	pte_t ptent;
 
-	if (is_vm_hugetlb_page(pvmw->vma))
-		ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
-				      pvmw->pte);
-	else
+	if (is_vm_hugetlb_page(pvmw->vma)) {
+		struct hstate *hstate = hstate_vma(pvmw->vma);
+		unsigned long haddr = pvmw->address & huge_page_mask(hstate);
+
+		ptent = huge_ptep_get(pvmw->vma->vm_mm, haddr, pvmw->pte);
+	} else {
  		ptent = ptep_get(pvmw->pte);
+	}
 
  	if (pvmw->flags & PVMW_MIGRATION) {
  		const softleaf_t entry = softleaf_from_pte(ptent);
--

while leaving pvmw->address unchanged for page_mapped_in_vma()?

Cheers, Lance

Quoted 9 lines
mm/page_vma_mapped.c +2 -0
+	else
+		ptent = ptep_get(pvmw->pte);
 
 	if (pvmw->flags & PVMW_MIGRATION) {
 		const softleaf_t entry = softleaf_from_pte(ptent);
Signature · 1 line
2.43.0
#18 lore raw

Re: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb

Quoted 161 lines · Lance Yang
Quoted 158 lines · Dev Jain
Quoted 155 lines · Lance Yang
Quoted 42 lines · Dev Jain
Quoted 22 lines · Lance Yang
Quoted 20 lines · Dev Jain

check_pte() is the final validation step in page_vma_mapped_walk(). It reads pvmw->pte with ptep_get() to decide whether the entry maps the PFN range being walked. For hugetlb VMAs, that pointer refers to a hugetlb entry.

On arches which provide their own huge_ptep_get() to dereference a huge pte pointer, accessing via ptep_get() would cause pte_pfn(), pte_present() etc to misbehave.

It is not clear whether this has a trivially visible effect to userspace.

Use huge_ptep_get() to dereference a huge pte pointer.

Fixes ace71a19cec5 ("mm: introduce page_vma_mapped_walk()")

---
mm/page_vma_mapped.c | 8 +++++++-

1 file changed, 7 insertions(+), 1 deletion(-)
Quoted 16 lines
Quoted 5 lines
mm/page_vma_mapped.c +0 -0
index 2ccbabfb2cc17..18e1d341f463c 100644

mm/page_vma_mapped.c static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp, · line 114

Quoted 9 lines
mm/page_vma_mapped.c +5 -1
@@ -107,7 +107,13 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp,
 static bool check_pte(struct page_vma_mapped_walk *pvmw, unsigned long pte_nr)
 {
     unsigned long pfn;
-    pte_t ptent = ptep_get(pvmw->pte);
+    pte_t ptent;
+
+    if (is_vm_hugetlb_page(pvmw->vma))
+        ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
+                      pvmw->pte);

I think check_pte() can pass a wrong address to huge_ptep_get() ...

Quoted 3 lines

Won't this be handled by rmap_walk_anon/rmap_walk_file - they are the ones performing the rmap traversal and passing address to try_to_unmap_one/folio_referenced_one etc ...

Right, that should cover the rmap callbacks. The bit I was worried about is page_mapped_in_vma() though.

Quoted 6 lines
Quoted 6 lines

Not sure that is wrong in the first place. For memory failure, page_mapped_in_vma() can be called with a poisoned tail page of a hugetlb folio. In that case, pvmw->address need not be hugepage-aligned.

@Miaohe

For hugetlb memory failure we start with the poisoned PFN:

static int try_memory_failure_hugetlb(unsigned long pfn, int flags) {
    ...
    struct page *p = pfn_to_page(pfn);
    struct folio *folio;
    ...

    folio = page_folio(p);

    ...

    if (!hwpoison_user_mappings(folio, p, pfn, flags)) {
        ...
    }

    ...
}

and pass the same p down:

static bool hwpoison_user_mappings(struct folio *folio, struct page *p, unsigned long pfn, int flags)
{
    ...

    collect_procs(folio, p, &tokill, flags & MF_ACTION_REQUIRED);

    ...
}

static void collect_procs(const struct folio *folio, const struct page *page, struct list_head *tokill, int force_early)
{
    ...

    if (unlikely(folio_test_ksm(folio)))
        ...
    else if (folio_test_anon(folio)) collect_procs_anon(folio, page, tokill, force_early); else
        ...
}

So collect_procs_anon() still gets the poisoned page, not &folio->page:

static void collect_procs_anon(const struct folio *folio, const struct page *page, struct list_head *to_kill, int force_early)
{
    ...

    pgoff = page_pgoff(folio, page); rcu_read_lock();
    for_each_process(tsk) {
        ...

        anon_vma_interval_tree_foreach(vmac, &av->rb_root, pgoff, pgoff) {
            ...
            addr = page_mapped_in_vma(page, vma);
            ...
        }
    }
    rcu_read_unlock();
    anon_vma_unlock_read(av);
}

page_mapped_in_vma() then builds pvmw for that page:

unsigned long page_mapped_in_vma(const struct page *page, struct vm_area_struct *vma)
{
    const struct folio *folio = page_folio(page); struct page_vma_mapped_walk pvmw = {
        .pfn = page_to_pfn(page),
        .nr_pages = 1,
        .vma = vma,
        .flags = PVMW_SYNC,
    };

    pvmw.address = vma_address(vma, page_pgoff(folio, page), 1);
    ...
}

and page_pgoff() includes the subpage index:

static inline pgoff_t page_pgoff(const struct folio *folio, const struct page *page)
{
    return folio->index + folio_page_idx(folio, page); }

So if the poisoned PFN points to a tail page, pvmw->address can be offset from the start of the hugetlb mapping by

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

folio_page_idx(folio, page) << PAGE_SHIFT

Should check_pte() pass the hugepage-aligned address to huge_ptep_get() for that case?

Thanks! This looks correct.

Thanks both.

Quoted 2 lines
Quoted 2 lines

I can indeed fix this up in check_pte. But in the memory-failure path it has always been confusing to me for hugetlb folios why we are bothering

IIUC, the hugetlb tail page is used to calculate the specified hwpoisoned address and send it to userspace through SIGBUS.

Quoted 7 lines
Quoted 2 lines

with the tail page. I am sure that area can also be simplified. But for now I'll just do a simple fix here itself.

Just thinking out loud: given that huge_ptep_get() already assumes that addr matches the huge pte, at least on arm64, would it make sense to have a small hugetlb wrapper around it that takes hstate and aligns the address before calling the arch helper?

This proposal looks good to me given the assumption.

Thanks.
.

Quoted 131 lines

Might make the rule clearer, and a bit harder to get wrong again :)

Thanks, Lance

Quoted 114 lines
Quoted 114 lines

Cheers, Lance

Quoted 110 lines
Quoted 110 lines

For arm64, CONT_PMD_SIZE is one supported HugeTLB size. With such a VMA, page_vma_mapped_walk() passes that size to hugetlb_walk():

bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) {
    ...
    if (unlikely(is_vm_hugetlb_page(vma))) {
        ...
        pvmw->pte = hugetlb_walk(vma, pvmw->address, size);
        ...
    }
    ...
}

hugetlb_walk() then calls arm64 huge_pte_offset(mm, addr, sz). For sz == CONT_PMD_SIZE, huge_pte_offset() aligns its local addr before calculating pmdp:

pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr, unsigned long sz) {
    ...
    if (sz == CONT_PMD_SIZE)
        addr &= CONT_PMD_MASK;

    pmdp = pmd_offset(pudp, addr);
    pmd = READ_ONCE(*pmdp);
    ...
}

So for that case, pvmw->pte is calculated from the aligned addr, not necessarily from the original pvmw->address. But check_pte() passes the original address together with pvmw->pte:

+        ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address, +                      pvmw->pte);

arm64 then uses that addr again to choose ncontig:

pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep) {
    ...
    ncontig = find_num_contig(mm, addr, ptep, &pgsize);
    for (i = 0; i < ncontig; i++, ptep++) {
        ...
    }
    return orig_pte;
}

static int find_num_contig(struct mm_struct *mm, unsigned long addr, pte_t *ptep, size_t *pgsize)
{
    pgd_t *pgdp = pgd_offset(mm, addr);
    p4d_t *p4dp;
    pud_t *pudp;
    pmd_t *pmdp;

    *pgsize = PAGE_SIZE;
    p4dp = p4d_offset(pgdp, addr);
    pudp = pud_offset(p4dp, addr);
    pmdp = pmd_offset(pudp, addr);
    if ((pte_t *)pmdp == ptep) {
        *pgsize = PMD_SIZE;
        return CONT_PMDS;
    }
    return CONT_PTES;
}

With a tail address, pmdp may no longer point at pvmw->pte, so find_num_contig() can return CONT_PTES for a CONT_PMD HugeTLB mapping.

On 16K arm64, that changes ncontig from 32 to 128. So huge_ptep_get() can walk past the CONT_PMD entries, and possibly past the PMD table.

Should check_pte() pass the address matching pvmw->pte, sth like:

---8<---

mm/page_vma_mapped.c +7 -4
index 406fd50bbd8f..58463493bd3d 100644
@@ -109,11 +109,14 @@ static bool check_pte(struct page_vma_mapped_walk *pvmw, unsigned long pte_nr)
       unsigned long pfn;
       pte_t ptent;
 
-    if (is_vm_hugetlb_page(pvmw->vma))
-        ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
-                      pvmw->pte);
-    else
+    if (is_vm_hugetlb_page(pvmw->vma)) {
+        struct hstate *hstate = hstate_vma(pvmw->vma);
+        unsigned long haddr = pvmw->address & huge_page_mask(hstate);
+
+        ptent = huge_ptep_get(pvmw->vma->vm_mm, haddr, pvmw->pte);
+    } else {
           ptent = ptep_get(pvmw->pte);
+    }
 
       if (pvmw->flags & PVMW_MIGRATION) {
           const softleaf_t entry = softleaf_from_pte(ptent);
Signature · 3 lines
while leaving pvmw->address unchanged for page_mapped_in_vma()?

Cheers, Lance
Quoted 3 lines
mm/page_vma_mapped.c +2 -0
+    else
+        ptent = ptep_get(pvmw->pte);
 
Quoted 10 lines
Quoted 9 lines
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
mm/page_vma_mapped.c +0 -0
     if (pvmw->flags & PVMW_MIGRATION) {
         const softleaf_t entry = softleaf_from_pte(ptent);

--
2.43.0

.

author
#19 lore raw

Re: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb

In reply to Lance Yang
Quoted 173 lines · Lance Yang
Quoted 163 lines · Dev Jain
Quoted 152 lines · Lance Yang
Quoted 42 lines · Dev Jain
Quoted 22 lines · Lance Yang
Quoted 20 lines · Dev Jain

check_pte() is the final validation step in page_vma_mapped_walk(). It reads pvmw->pte with ptep_get() to decide whether the entry maps the PFN range being walked. For hugetlb VMAs, that pointer refers to a hugetlb entry.

On arches which provide their own huge_ptep_get() to dereference a huge pte pointer, accessing via ptep_get() would cause pte_pfn(), pte_present() etc to misbehave.

It is not clear whether this has a trivially visible effect to userspace.

Use huge_ptep_get() to dereference a huge pte pointer.

Fixes ace71a19cec5 ("mm: introduce page_vma_mapped_walk()")

---
mm/page_vma_mapped.c | 8 +++++++-

1 file changed, 7 insertions(+), 1 deletion(-)
Quoted 16 lines
Quoted 5 lines
mm/page_vma_mapped.c +0 -0
index 2ccbabfb2cc17..18e1d341f463c 100644

mm/page_vma_mapped.c static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp, · line 114

Quoted 9 lines
mm/page_vma_mapped.c +5 -1
@@ -107,7 +107,13 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp,
 static bool check_pte(struct page_vma_mapped_walk *pvmw, unsigned long pte_nr)
 {
     unsigned long pfn;
-    pte_t ptent = ptep_get(pvmw->pte);
+    pte_t ptent;
+
+    if (is_vm_hugetlb_page(pvmw->vma))
+        ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
+                      pvmw->pte);

I think check_pte() can pass a wrong address to huge_ptep_get() ...

Quoted 3 lines

Won't this be handled by rmap_walk_anon/rmap_walk_file - they are the ones performing the rmap traversal and passing address to try_to_unmap_one/folio_referenced_one etc ...

Right, that should cover the rmap callbacks. The bit I was worried about is page_mapped_in_vma() though.

Quoted 6 lines
Quoted 6 lines

Not sure that is wrong in the first place. For memory failure, page_mapped_in_vma() can be called with a poisoned tail page of a hugetlb folio. In that case, pvmw->address need not be hugepage-aligned.

@Miaohe

For hugetlb memory failure we start with the poisoned PFN:

static int try_memory_failure_hugetlb(unsigned long pfn, int flags) {
    ...
    struct page *p = pfn_to_page(pfn);
    struct folio *folio;
    ...

    folio = page_folio(p);

    ...

    if (!hwpoison_user_mappings(folio, p, pfn, flags)) {
        ...
    }

    ...
}

and pass the same p down:

static bool hwpoison_user_mappings(struct folio *folio, struct page *p, unsigned long pfn, int flags)
{
    ...

    collect_procs(folio, p, &tokill, flags & MF_ACTION_REQUIRED);

    ...
}

static void collect_procs(const struct folio *folio, const struct page *page, struct list_head *tokill, int force_early)
{
    ...

    if (unlikely(folio_test_ksm(folio)))
        ...
    else if (folio_test_anon(folio)) collect_procs_anon(folio, page, tokill, force_early); else
        ...
}

So collect_procs_anon() still gets the poisoned page, not &folio->page:

static void collect_procs_anon(const struct folio *folio, const struct page *page, struct list_head *to_kill, int force_early)
{
    ...

    pgoff = page_pgoff(folio, page); rcu_read_lock();
    for_each_process(tsk) {
        ...

        anon_vma_interval_tree_foreach(vmac, &av->rb_root, pgoff, pgoff) {
            ...
            addr = page_mapped_in_vma(page, vma);
            ...
        }
    }
    rcu_read_unlock();
    anon_vma_unlock_read(av);
}

page_mapped_in_vma() then builds pvmw for that page:

unsigned long page_mapped_in_vma(const struct page *page, struct vm_area_struct *vma)
{
    const struct folio *folio = page_folio(page); struct page_vma_mapped_walk pvmw = {
        .pfn = page_to_pfn(page),
        .nr_pages = 1,
        .vma = vma,
        .flags = PVMW_SYNC,
    };

    pvmw.address = vma_address(vma, page_pgoff(folio, page), 1);
    ...
}

and page_pgoff() includes the subpage index:

static inline pgoff_t page_pgoff(const struct folio *folio, const struct page *page)
{
    return folio->index + folio_page_idx(folio, page); }

Quoted 7 lines

So if the poisoned PFN points to a tail page, pvmw->address can be offset from the start of the hugetlb mapping by

folio_page_idx(folio, page) << PAGE_SHIFT

Should check_pte() pass the hugepage-aligned address to huge_ptep_get() for that case?

Quoted 6 lines

Thanks! This looks correct.

I can indeed fix this up in check_pte. But in the memory-failure path it has always been confusing to me for hugetlb folios why we are bothering with the tail page. I am sure that area can also be simplified. But for now I'll just do a simple fix here itself.

Quoted 6 lines

Just thinking out loud: given that huge_ptep_get() already assumes that addr matches the huge pte, at least on arm64, would it make sense to have a small hugetlb wrapper around it that takes hstate and aligns the address before calling the arch helper?

Might make the rule clearer, and a bit harder to get wrong again :)

Are you suggesting something like:

include/linux/hugetlb.h +8 -0
index fdb7bdf7645c..xxxxxxxxxxxx 100644
@@ -825,6 +825,15 @@ static inline struct folio *filemap_lock_hugetlb_folio(struct hstate *h,
 
 #include <asm/hugetlb.h>
 
+static inline pte_t hugetlb_ptep_get(struct vm_area_struct *vma,
+				     unsigned long addr, pte_t *ptep)
+{
+	struct hstate *h = hstate_vma(vma);
+
+	return huge_ptep_get(vma->vm_mm, addr & huge_page_mask(h), ptep);
+}
+
 #ifndef is_hugepage_only_range
 static inline int is_hugepage_only_range(struct mm_struct *mm,
 					unsigned long addr, unsigned long len)
mm/page_vma_mapped.c +1 -2
index 18e1d341f463..xxxxxxxxxxxx 100644
@@ -110,8 +110,7 @@ static bool check_pte(struct page_vma_mapped_walk *pvmw, unsigned long pte_nr)
 	pte_t ptent;
 
 	if (is_vm_hugetlb_page(pvmw->vma))
-		ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
-				      pvmw->pte);
+		ptent = hugetlb_ptep_get(pvmw->vma, pvmw->address, pvmw->pte);
 	else
 		ptent = ptep_get(pvmw->pte);
 
Quoted 129 lines

Thanks, Lance

Quoted 124 lines
Quoted 123 lines

Cheers, Lance

Quoted 118 lines
Quoted 116 lines

For arm64, CONT_PMD_SIZE is one supported HugeTLB size. With such a VMA, page_vma_mapped_walk() passes that size to hugetlb_walk():

bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) {
    ...
    if (unlikely(is_vm_hugetlb_page(vma))) {
        ...
        pvmw->pte = hugetlb_walk(vma, pvmw->address, size);
        ...
    }
    ...
}

hugetlb_walk() then calls arm64 huge_pte_offset(mm, addr, sz). For sz == CONT_PMD_SIZE, huge_pte_offset() aligns its local addr before calculating pmdp:

pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr, unsigned long sz) {
    ...
    if (sz == CONT_PMD_SIZE)
        addr &= CONT_PMD_MASK;

    pmdp = pmd_offset(pudp, addr);
    pmd = READ_ONCE(*pmdp);
    ...
}

So for that case, pvmw->pte is calculated from the aligned addr, not necessarily from the original pvmw->address. But check_pte() passes the original address together with pvmw->pte:

+        ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address, +                      pvmw->pte);

arm64 then uses that addr again to choose ncontig:

pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep) {
    ...
    ncontig = find_num_contig(mm, addr, ptep, &pgsize);
    for (i = 0; i < ncontig; i++, ptep++) {
        ...
    }
    return orig_pte;
}

static int find_num_contig(struct mm_struct *mm, unsigned long addr, pte_t *ptep, size_t *pgsize)
{
    pgd_t *pgdp = pgd_offset(mm, addr);
    p4d_t *p4dp;
    pud_t *pudp;
    pmd_t *pmdp;

    *pgsize = PAGE_SIZE;
    p4dp = p4d_offset(pgdp, addr);
    pudp = pud_offset(p4dp, addr);
    pmdp = pmd_offset(pudp, addr);
    if ((pte_t *)pmdp == ptep) {
        *pgsize = PMD_SIZE;
        return CONT_PMDS;
    }
    return CONT_PTES;
}

With a tail address, pmdp may no longer point at pvmw->pte, so find_num_contig() can return CONT_PTES for a CONT_PMD HugeTLB mapping.

On 16K arm64, that changes ncontig from 32 to 128. So huge_ptep_get() can walk past the CONT_PMD entries, and possibly past the PMD table.

Should check_pte() pass the address matching pvmw->pte, sth like:

---8<---

mm/page_vma_mapped.c +7 -4
index 406fd50bbd8f..58463493bd3d 100644
@@ -109,11 +109,14 @@ static bool check_pte(struct page_vma_mapped_walk *pvmw, unsigned long pte_nr)
       unsigned long pfn;
       pte_t ptent;
 
-    if (is_vm_hugetlb_page(pvmw->vma))
-        ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
-                      pvmw->pte);
-    else
+    if (is_vm_hugetlb_page(pvmw->vma)) {
+        struct hstate *hstate = hstate_vma(pvmw->vma);
+        unsigned long haddr = pvmw->address & huge_page_mask(hstate);
+
+        ptent = huge_ptep_get(pvmw->vma->vm_mm, haddr, pvmw->pte);
+    } else {
           ptent = ptep_get(pvmw->pte);
+    }
 
       if (pvmw->flags & PVMW_MIGRATION) {
           const softleaf_t entry = softleaf_from_pte(ptent);
Signature · 3 lines
while leaving pvmw->address unchanged for page_mapped_in_vma()?

Cheers, Lance
Quoted 9 lines
mm/page_vma_mapped.c +2 -0
+    else
+        ptent = ptep_get(pvmw->pte);
 
     if (pvmw->flags & PVMW_MIGRATION) {
         const softleaf_t entry = softleaf_from_pte(ptent);

--
2.43.0

reviewer
#20 lore raw

Re: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb

Quoted 3 lines · Dev Jain

On 26/06/26 10:16 pm, Lance Yang wrote:

[...]

Quoted 9 lines
Quoted 7 lines

Just thinking out loud: given that huge_ptep_get() already assumes that addr matches the huge pte, at least on arm64, would it make sense to have a small hugetlb wrapper around it that takes hstate and aligns the address before calling the arch helper?

Might make the rule clearer, and a bit harder to get wrong again :)

Are you suggesting something like:

Yes, that's what I had in mind :) thanks!

include/linux/hugetlb.h static inline struct folio *filemap_lock_hugetlb_folio(struct hstate *h, · line 826

Quoted 7 lines
include/linux/hugetlb.h +0 -0
index fdb7bdf7645c..xxxxxxxxxxxx 100644
@@ -825,6 +825,15 @@ static inline struct folio *filemap_lock_hugetlb_folio(struct hstate *h,
 
 #include <asm/hugetlb.h>

Maybe worth spelling out the rule as well:

For arch helpers that use addr, huge_ptep_get() assumes addr is the address for the hugetlb entry ptep points to. arm64 already makes that assumption.

Callers where addr may not be hugepage-aligned should use hugetlb_ptep_get() instead.

Quoted 16 lines
include/linux/hugetlb.h +8 -0
+static inline pte_t hugetlb_ptep_get(struct vm_area_struct *vma,
+				     unsigned long addr, pte_t *ptep)
+{
+	struct hstate *h = hstate_vma(vma);
+
+	return huge_ptep_get(vma->vm_mm, addr & huge_page_mask(h), ptep);
+}
+
 #ifndef is_hugepage_only_range
 static inline int is_hugepage_only_range(struct mm_struct *mm,
 					unsigned long addr, unsigned long len)
mm/page_vma_mapped.c +0 -0
index 18e1d341f463..xxxxxxxxxxxx 100644

mm/page_vma_mapped.c static bool check_pte(struct page_vma_mapped_walk *pvmw, unsigned long pte_nr) · line 115

Quoted 8 lines
mm/page_vma_mapped.c +1 -2
@@ -110,8 +110,7 @@ static bool check_pte(struct page_vma_mapped_walk *pvmw, unsigned long pte_nr)
 	pte_t ptent;
 
 	if (is_vm_hugetlb_page(pvmw->vma))
-		ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
-				      pvmw->pte);
+		ptent = hugetlb_ptep_get(pvmw->vma, pvmw->address, pvmw->pte);
 	else
 		ptent = ptep_get(pvmw->pte);

[...]

Cheers, Lance

#21 lore raw

Re: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb

Quoted 28 lines · Lance Yang
Quoted 3 lines · Dev Jain

On 26/06/26 10:16 pm, Lance Yang wrote:

[...]

Quoted 9 lines
Quoted 7 lines

Just thinking out loud: given that huge_ptep_get() already assumes that addr matches the huge pte, at least on arm64, would it make sense to have a small hugetlb wrapper around it that takes hstate and aligns the address before calling the arch helper?

Might make the rule clearer, and a bit harder to get wrong again :)

Are you suggesting something like:

Yes, that's what I had in mind :) thanks!

include/linux/hugetlb.h static inline struct folio *filemap_lock_hugetlb_folio(struct hstate *h, · line 826

Quoted 7 lines
include/linux/hugetlb.h +0 -0
index fdb7bdf7645c..xxxxxxxxxxxx 100644
@@ -825,6 +825,15 @@ static inline struct folio *filemap_lock_hugetlb_folio(struct hstate *h,
 
 #include <asm/hugetlb.h>

Maybe worth spelling out the rule as well:

Quoted 6 lines

For arch helpers that use addr, huge_ptep_get() assumes addr is the address for the hugetlb entry ptep points to. arm64 already makes that assumption.

Callers where addr may not be hugepage-aligned should use hugetlb_ptep_get() instead.

Do we have any examples where code would do that? I would think that all code must properly align addr ahead of times.

Signature · 3 lines
Cheers,

David
author
#22 lore raw

Re: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb

Quoted 33 lines · David Hildenbrand (Arm)
Quoted 32 lines · Lance Yang
Quoted 3 lines · Dev Jain

On 26/06/26 10:16 pm, Lance Yang wrote:

[...]

Quoted 9 lines
Quoted 7 lines

Just thinking out loud: given that huge_ptep_get() already assumes that addr matches the huge pte, at least on arm64, would it make sense to have a small hugetlb wrapper around it that takes hstate and aligns the address before calling the arch helper?

Might make the rule clearer, and a bit harder to get wrong again :)

Are you suggesting something like:

Yes, that's what I had in mind :) thanks!

include/linux/hugetlb.h static inline struct folio *filemap_lock_hugetlb_folio(struct hstate *h, · line 826

Quoted 7 lines
include/linux/hugetlb.h +0 -0
index fdb7bdf7645c..xxxxxxxxxxxx 100644
@@ -825,6 +825,15 @@ static inline struct folio *filemap_lock_hugetlb_folio(struct hstate *h,
 
 #include <asm/hugetlb.h>

Maybe worth spelling out the rule as well:

For arch helpers that use addr, huge_ptep_get() assumes addr is the address for the hugetlb entry ptep points to. arm64 already makes that assumption.

Quoted 5 lines
Quoted 2 lines

Callers where addr may not be hugepage-aligned should use hugetlb_ptep_get() instead.

Do we have any examples where code would do that? I would think that all code must properly align addr ahead of times.

Sashiko notes other places:

https://sashiko.dev/#/patchset/20260625112955.3254283-1-dev.jain%40arm.com

Quoted 1 line
reviewer
#31 lore raw

Re: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb

In reply to David Hildenbrand (Arm)
Quoted 33 lines · David Hildenbrand (Arm)
Quoted 32 lines · Lance Yang
Quoted 3 lines · Dev Jain

On 26/06/26 10:16 pm, Lance Yang wrote:

[...]

Quoted 9 lines
Quoted 7 lines

Just thinking out loud: given that huge_ptep_get() already assumes that addr matches the huge pte, at least on arm64, would it make sense to have a small hugetlb wrapper around it that takes hstate and aligns the address before calling the arch helper?

Might make the rule clearer, and a bit harder to get wrong again :)

Are you suggesting something like:

Yes, that's what I had in mind :) thanks!

include/linux/hugetlb.h static inline struct folio *filemap_lock_hugetlb_folio(struct hstate *h, · line 826

Quoted 7 lines
include/linux/hugetlb.h +0 -0
index fdb7bdf7645c..xxxxxxxxxxxx 100644
@@ -825,6 +825,15 @@ static inline struct folio *filemap_lock_hugetlb_folio(struct hstate *h,
 
 #include <asm/hugetlb.h>

Maybe worth spelling out the rule as well:

For arch helpers that use addr, huge_ptep_get() assumes addr is the address for the hugetlb entry ptep points to. arm64 already makes that assumption.

Quoted 5 lines
Quoted 2 lines

Callers where addr may not be hugepage-aligned should use hugetlb_ptep_get() instead.

Do we have any examples where code would do that? I would think that all code must properly align addr ahead of times.

I was thinking of the memory-failure case from earlier:

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

There, page_mapped_in_vma() can be called with the poisoned tail page, so pvmw.address comes from page_pgoff(folio, page) and need not be hugepage-aligned.

Cheers, Lance

#23 lore raw

Re: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb

In reply to Dev Jain
Quoted 19 lines · Dev Jain
Quoted 16 lines · David Hildenbrand (Arm)
Quoted 14 lines · Lance Yang

[...]

Yes, that's what I had in mind :) thanks!

Maybe worth spelling out the rule as well:

For arch helpers that use addr, huge_ptep_get() assumes addr is the address for the hugetlb entry ptep points to. arm64 already makes that assumption.

Callers where addr may not be hugepage-aligned should use hugetlb_ptep_get() instead.

Quoted 6 lines
Quoted 2 lines

Do we have any examples where code would do that? I would think that all code must properly align addr ahead of times.

Sashiko notes other places:

https://sashiko.dev/#/patchset/20260625112955.3254283-1-dev.jain%40arm.com

Yeah, that looks shaky. We do seem to have a bunch of these cases, primarily from pagewalk code (where some users like pagemap need the actual address).

I think we have two options

1) To prevent any (further) issues, make huge_ptep_get() always consume the hstate, and let the arch code deal with aligning it. Invasive.

2) Make the arch code handle aligning without the hstate.

arch/arm64/mm/hugetlbpage.c +3 -0
index 30772a909aea3..303a1b74796c9 100644
@@ -126,6 +126,9 @@ pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
                return orig_pte;
 
        ncontig = find_num_contig(mm, addr, ptep, &pgsize);
+       ptep = PTR_ALIGN_DOWN(ptep, sizeof(*ptep) * ncontig);
+       orig_pte = __ptep_get(ptep);
+
        for (i = 0; i < ncontig; i++, ptep++) {
                pte_t pte = __ptep_get(ptep);
 

(nshift/order instead of ncontig might avoid a multiplication, but not sure if that matters in practice)

IIUC, that's similar to what huge_ptep_get() does on ppc.

static inline pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)

{
	if (ptep_is_8m_pmdp(mm, addr, ptep))
		ptep = pte_offset_kernel((pmd_t *)ptep, ALIGN_DOWN(addr, SZ_8M));
	return ptep_get(ptep);
}

I'd assume we could do the same on riscv. Besides that, I don't think any arch has cont entries.

Interestingly, huge_pte_clear() / huge_ptep_get_and_clear() and friends would be all wrong when the wrong address is passed. But that code really is called from hugetlb.c where we should take better care of that. (e.g., partially zapping a hugetlb page is not possible)

Signature · 3 lines
Cheers,

David
reviewer
#24 lore raw

Re: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb

Quoted 23 lines · David Hildenbrand (Arm)
Quoted 22 lines · Dev Jain
Quoted 18 lines · David Hildenbrand (Arm)
Quoted 6 lines · Lance Yang

[...]

Yes, that's what I had in mind :) thanks!

Quoted 8 lines

Maybe worth spelling out the rule as well:

For arch helpers that use addr, huge_ptep_get() assumes addr is the address for the hugetlb entry ptep points to. arm64 already makes that assumption.

Callers where addr may not be hugepage-aligned should use hugetlb_ptep_get() instead.

Do we have any examples where code would do that? I would think that all code must properly align addr ahead of times.

Quoted 6 lines
Quoted 3 lines

Sashiko notes other places:

https://sashiko.dev/#/patchset/20260625112955.3254283-1-dev.jain%40arm.com

Yeah, that looks shaky. We do seem to have a bunch of these cases, primarily from pagewalk code (where some users like pagemap need the actual address).

Indeed ...

Quoted 4 lines

I think we have two options

1) To prevent any (further) issues, make huge_ptep_get() always consume the hstate, and let the arch code deal with aligning it. Invasive.

Kinda lean toward option 1, even if it's more invasive. If we pass the hstate down, each arch can figure out the right addr from there.

Quoted 22 lines

2) Make the arch code handle aligning without the hstate.

arch/arm64/mm/hugetlbpage.c +3 -0
index 30772a909aea3..303a1b74796c9 100644
@@ -126,6 +126,9 @@ pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
               return orig_pte;
 
       ncontig = find_num_contig(mm, addr, ptep, &pgsize);
+       ptep = PTR_ALIGN_DOWN(ptep, sizeof(*ptep) * ncontig);
+       orig_pte = __ptep_get(ptep);
+
       for (i = 0; i < ncontig; i++, ptep++) {
               pte_t pte = __ptep_get(ptep);
 

(nshift/order instead of ncontig might avoid a multiplication, but not sure if that matters in practice)

IIUC, that's similar to what huge_ptep_get() does on ppc.

static inline pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)

Quoted 8 lines
{
	if (ptep_is_8m_pmdp(mm, addr, ptep))
		ptep = pte_offset_kernel((pmd_t *)ptep, ALIGN_DOWN(addr, SZ_8M));
	return ptep_get(ptep);
}

I'd assume we could do the same on riscv. Besides that, I don't think any arch has cont entries.

AFAICT, for huge_ptep_get() the addr users are arm64 and powerpc, riscv doesn't really care about addr there. Looks mostly arm64-specific ...

Quoted 12 lines

Interestingly, huge_pte_clear() / huge_ptep_get_and_clear() and friends would be all wrong when the wrong address is passed. But that code really is called from hugetlb.c where we should take better care of that. (e.g., partially zapping a hugetlb page is not possible)

Signature · 3 lines
Cheers,

David
#25 lore raw

Re: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb

Quoted 51 lines · Lance Yang
Quoted 10 lines · David Hildenbrand (Arm)
Quoted 6 lines · Dev Jain

Sashiko notes other places:

https://sashiko.dev/#/patchset/20260625112955.3254283-1-dev.jain%40arm.com

Yeah, that looks shaky. We do seem to have a bunch of these cases, primarily from pagewalk code (where some users like pagemap need the actual address).

Indeed ...

Quoted 4 lines

I think we have two options

1) To prevent any (further) issues, make huge_ptep_get() always consume the hstate, and let the arch code deal with aligning it. Invasive.

Kinda lean toward option 1, even if it's more invasive. If we pass the hstate down, each arch can figure out the right addr from there.

Quoted 28 lines

2) Make the arch code handle aligning without the hstate.

arch/arm64/mm/hugetlbpage.c +3 -0
index 30772a909aea3..303a1b74796c9 100644
@@ -126,6 +126,9 @@ pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
               return orig_pte;
 
       ncontig = find_num_contig(mm, addr, ptep, &pgsize);
+       ptep = PTR_ALIGN_DOWN(ptep, sizeof(*ptep) * ncontig);
+       orig_pte = __ptep_get(ptep);
+
       for (i = 0; i < ncontig; i++, ptep++) {
               pte_t pte = __ptep_get(ptep);
 

(nshift/order instead of ncontig might avoid a multiplication, but not sure if that matters in practice)

IIUC, that's similar to what huge_ptep_get() does on ppc.

static inline pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)

{
	if (ptep_is_8m_pmdp(mm, addr, ptep))
		ptep = pte_offset_kernel((pmd_t *)ptep, ALIGN_DOWN(addr, SZ_8M));
	return ptep_get(ptep);
}
Quoted 5 lines
Quoted 2 lines

I'd assume we could do the same on riscv. Besides that, I don't think any arch has cont entries.

AFAICT, for huge_ptep_get() the addr users are arm64 and powerpc, riscv doesn't really care about addr there. Looks mostly arm64-specific ...

powerpc handles it correctly in the weird "span two PMD entries" case by aligning the PMD down.

Risc-v copied from arm64, but can simply derive the #entries from the PTE value. it doesn't have to re-walk the table using the address.

But I think the following is required to fix, no?

arch/riscv/mm/hugetlbpage.c +1 -1
index a6d217112cf46..7e25cc13b3dba 100644
@@ -5,6 +5,7 @@
 #ifdef CONFIG_RISCV_ISA_SVNAPOT
 pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
 {
-       unsigned long pte_num;
+       unsigned long pte_num, pte_order;
        int i;
        pte_t orig_pte = ptep_get(ptep);
@@ -12,7 +13,11 @@ pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr,

pte_t *ptep)

        if (!pte_present(orig_pte) || !pte_napot(orig_pte))
                return orig_pte;
-       pte_num = napot_pte_num(napot_cont_order(orig_pte));
+       pte_order = napot_cont_order(orig_pte);
+       pte_num = napot_pte_num(pte_order);

+
+ ptep = PTR_ALIGN_DOWN(ptep, sizeof(*ptep) << pte_order);
+ orig_pte = ptep_get(ptep);

        for (i = 0; i < pte_num; i++, ptep++) {
                pte_t pte = ptep_get(ptep);

I'd prefer (2) as a simple stable fix first.

If we do (1) on top, huge_ptep_get() on arm64 could stop walking the page table another time.

If we pass the hstate (or vma) to set_huge_pte_at(), huge_pte_clear(), huge_ptep_get_and_clear(), we could likely get rid of the re-walk in num_contig_ptes() entirely and possibly just remove it.

That would probably be cleanest.

Signature · 3 lines
Cheers,

David
reviewer
#26 lore raw

Re: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb

Quoted 57 lines · David Hildenbrand (Arm)
Quoted 51 lines · Lance Yang
Quoted 10 lines · David Hildenbrand (Arm)
Quoted 6 lines · Dev Jain

Sashiko notes other places:

https://sashiko.dev/#/patchset/20260625112955.3254283-1-dev.jain%40arm.com

Yeah, that looks shaky. We do seem to have a bunch of these cases, primarily from pagewalk code (where some users like pagemap need the actual address).

Indeed ...

Quoted 4 lines

I think we have two options

1) To prevent any (further) issues, make huge_ptep_get() always consume the hstate, and let the arch code deal with aligning it. Invasive.

Kinda lean toward option 1, even if it's more invasive. If we pass the hstate down, each arch can figure out the right addr from there.

Quoted 28 lines

2) Make the arch code handle aligning without the hstate.

arch/arm64/mm/hugetlbpage.c +3 -0
index 30772a909aea3..303a1b74796c9 100644
@@ -126,6 +126,9 @@ pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
                return orig_pte;
 
        ncontig = find_num_contig(mm, addr, ptep, &pgsize);
+       ptep = PTR_ALIGN_DOWN(ptep, sizeof(*ptep) * ncontig);
+       orig_pte = __ptep_get(ptep);
+
        for (i = 0; i < ncontig; i++, ptep++) {
                pte_t pte = __ptep_get(ptep);
 

(nshift/order instead of ncontig might avoid a multiplication, but not sure if that matters in practice)

IIUC, that's similar to what huge_ptep_get() does on ppc.

static inline pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)

{
	if (ptep_is_8m_pmdp(mm, addr, ptep))
		ptep = pte_offset_kernel((pmd_t *)ptep, ALIGN_DOWN(addr, SZ_8M));
	return ptep_get(ptep);
}
Quoted 5 lines
Quoted 2 lines

I'd assume we could do the same on riscv. Besides that, I don't think any arch has cont entries.

AFAICT, for huge_ptep_get() the addr users are arm64 and powerpc, riscv doesn't really care about addr there. Looks mostly arm64-specific ...

Quoted 5 lines

powerpc handles it correctly in the weird "span two PMD entries" case by aligning the PMD down.

Risc-v copied from arm64, but can simply derive the #entries from the PTE value. it doesn't have to re-walk the table using the address.

Yeah, fair enough, thanks for spelling that out!

Quoted 31 lines

But I think the following is required to fix, no?

arch/riscv/mm/hugetlbpage.c +6 -2
index a6d217112cf46..7e25cc13b3dba 100644
@@ -5,6 +5,7 @@
  #ifdef CONFIG_RISCV_ISA_SVNAPOT
  pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  {
-       unsigned long pte_num;
+       unsigned long pte_num, pte_order;
         int i;
         pte_t orig_pte = ptep_get(ptep);
@@ -12,7 +13,11 @@ pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr,
 pte_t *ptep)
         if (!pte_present(orig_pte) || !pte_napot(orig_pte))
                 return orig_pte;
 
-       pte_num = napot_pte_num(napot_cont_order(orig_pte));
+       pte_order = napot_cont_order(orig_pte);
+       pte_num = napot_pte_num(pte_order);
+
+       ptep = PTR_ALIGN_DOWN(ptep, sizeof(*ptep) << pte_order);
+       orig_pte = ptep_get(ptep);
 
         for (i = 0; i < pte_num; i++, ptep++) {
                 pte_t pte = ptep_get(ptep);
Quoted 1 line

I'd prefer (2) as a simple stable fix first.

Right. I'm good with (2) as the stable fix first :)

Still pretty new to arch code, but happy to stare at it some more.

Quoted 8 lines

If we do (1) on top, huge_ptep_get() on arm64 could stop walking the page table another time.

If we pass the hstate (or vma) to set_huge_pte_at(), huge_pte_clear(), huge_ptep_get_and_clear(), we could likely get rid of the re-walk in num_contig_ptes() entirely and possibly just remove it.

That would probably be cleanest.

Agreed!

author
#27 lore raw

Re: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb

In reply to David Hildenbrand (Arm)
Quoted 57 lines · David Hildenbrand (Arm)
Quoted 51 lines · Lance Yang
Quoted 10 lines · David Hildenbrand (Arm)
Quoted 6 lines · Dev Jain

Sashiko notes other places:

https://sashiko.dev/#/patchset/20260625112955.3254283-1-dev.jain%40arm.com

Yeah, that looks shaky. We do seem to have a bunch of these cases, primarily from pagewalk code (where some users like pagemap need the actual address).

Indeed ...

Quoted 4 lines

I think we have two options

1) To prevent any (further) issues, make huge_ptep_get() always consume the hstate, and let the arch code deal with aligning it. Invasive.

Kinda lean toward option 1, even if it's more invasive. If we pass the hstate down, each arch can figure out the right addr from there.

Quoted 28 lines

2) Make the arch code handle aligning without the hstate.

arch/arm64/mm/hugetlbpage.c +3 -0
index 30772a909aea3..303a1b74796c9 100644
@@ -126,6 +126,9 @@ pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
               return orig_pte;
 
       ncontig = find_num_contig(mm, addr, ptep, &pgsize);
+       ptep = PTR_ALIGN_DOWN(ptep, sizeof(*ptep) * ncontig);
+       orig_pte = __ptep_get(ptep);
+
       for (i = 0; i < ncontig; i++, ptep++) {
               pte_t pte = __ptep_get(ptep);
 

(nshift/order instead of ncontig might avoid a multiplication, but not sure if that matters in practice)

IIUC, that's similar to what huge_ptep_get() does on ppc.

static inline pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)

{
	if (ptep_is_8m_pmdp(mm, addr, ptep))
		ptep = pte_offset_kernel((pmd_t *)ptep, ALIGN_DOWN(addr, SZ_8M));
	return ptep_get(ptep);
}
Quoted 5 lines
Quoted 2 lines

I'd assume we could do the same on riscv. Besides that, I don't think any arch has cont entries.

AFAICT, for huge_ptep_get() the addr users are arm64 and powerpc, riscv doesn't really care about addr there. Looks mostly arm64-specific ...

Quoted 7 lines

powerpc handles it correctly in the weird "span two PMD entries" case by aligning the PMD down.

Risc-v copied from arm64, but can simply derive the #entries from the PTE value. it doesn't have to re-walk the table using the address.

But I think the following is required to fix, no?

We don't receive an unaligned ptep in huge_ptep_get, and riscv derives the number of cont ptes from the pte itself, so why is the below required?

Quoted 41 lines
arch/riscv/mm/hugetlbpage.c +6 -2
index a6d217112cf46..7e25cc13b3dba 100644
@@ -5,6 +5,7 @@
 #ifdef CONFIG_RISCV_ISA_SVNAPOT
 pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
 {
-       unsigned long pte_num;
+       unsigned long pte_num, pte_order;
        int i;
        pte_t orig_pte = ptep_get(ptep);
@@ -12,7 +13,11 @@ pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr,
 pte_t *ptep)
        if (!pte_present(orig_pte) || !pte_napot(orig_pte))
                return orig_pte;
 
-       pte_num = napot_pte_num(napot_cont_order(orig_pte));
+       pte_order = napot_cont_order(orig_pte);
+       pte_num = napot_pte_num(pte_order);
+
+       ptep = PTR_ALIGN_DOWN(ptep, sizeof(*ptep) << pte_order);
+       orig_pte = ptep_get(ptep);
 
        for (i = 0; i < pte_num; i++, ptep++) {
                pte_t pte = ptep_get(ptep);

I'd prefer (2) as a simple stable fix first.

If we do (1) on top, huge_ptep_get() on arm64 could stop walking the page table another time.

If we pass the hstate (or vma) to set_huge_pte_at(), huge_pte_clear(), huge_ptep_get_and_clear(), we could likely get rid of the re-walk in num_contig_ptes() entirely and possibly just remove it.

That would probably be cleanest.

#28 lore raw

Re: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb

Quoted 19 lines · Dev Jain
Quoted 16 lines · David Hildenbrand (Arm)
Quoted 6 lines · Lance Yang
Quoted 2 lines

from pagewalk code (where some users like pagemap need the actual address).

Indeed ...

Quoted 6 lines

Kinda lean toward option 1, even if it's more invasive. If we pass the hstate down, each arch can figure out the right addr from there.

AFAICT, for huge_ptep_get() the addr users are arm64 and powerpc, riscv doesn't really care about addr there. Looks mostly arm64-specific ...

powerpc handles it correctly in the weird "span two PMD entries" case by aligning the PMD down.

Quoted 7 lines
Quoted 4 lines

Risc-v copied from arm64, but can simply derive the #entries from the PTE value. it doesn't have to re-walk the table using the address.

But I think the following is required to fix, no?

We don't receive an unaligned ptep in huge_ptep_get, and riscv derives the number of cont ptes from the pte itself, so why is the below required?

Let me look at the actual report once more ...

I thought for a second that the problem would be having the ptep not point at the start of the hugetlb page mapping. But that should always be the case. So yes, riscv does not have any problems.

And IIUC, arm64 only has a problem when CONT_PTES != CONT_PMDS (16 kernel?).

Yeah, aligning the ptep down doesn't solve anything, it's already properly aligned.

To fix it inside arm64 code, we'd have to teach find_num_contig() to ignore the ptep and instead look for the cont bit, maybe?

But I'm sure I messed this up as I am working on 10 things at the same time :D

arch/arm64/mm/hugetlbpage.c +4 -4
index d477a9dd1b472..d1d03795c135e 100644
@@ -76,7 +76,7 @@ bool arch_hugetlb_migration_supported(struct hstate *h)
 #endif
 
 static int find_num_contig(struct mm_struct *mm, unsigned long addr,
-                          pte_t *ptep, size_t *pgsize)
+                          size_t *pgsize)
 {
        pgd_t *pgdp = pgd_offset(mm, addr);
        p4d_t *p4dp;
@@ -87,7 +87,7 @@ static int find_num_contig(struct mm_struct *mm, unsigned long addr,
        p4dp = p4d_offset(pgdp, addr);
        pudp = pud_offset(p4dp, addr);
        pmdp = pmd_offset(pudp, addr);
-       if ((pte_t *)pmdp == ptep) {
+       if (pmd_cont(*pmdp)) {
                *pgsize = PMD_SIZE;
                return CONT_PMDS;
        }
@@ -131,7 +131,7 @@ pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
        if (!pte_present(orig_pte) || !pte_cont(orig_pte))
                return orig_pte;
 
-       ncontig = find_num_contig(mm, addr, ptep, &pgsize);
+       ncontig = find_num_contig(mm, addr, &pgsize);
        for (i = 0; i < ncontig; i++, ptep++) {
                pte_t pte = __ptep_get(ptep);
 
@@ -475,7 +475,7 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm,
                return;
        }
 
-       ncontig = find_num_contig(mm, addr, ptep, &pgsize);
+       ncontig = find_num_contig(mm, addr, &pgsize);
 
        pte = get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig);
        pte = pte_wrprotect(pte);
mm/memory.c +0 -0
Signature · 3 lines
Cheers,

David
author
#29 lore raw

Re: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb

Quoted 58 lines · David Hildenbrand (Arm)
Quoted 19 lines · Dev Jain
Quoted 16 lines · David Hildenbrand (Arm)
Quoted 6 lines · Lance Yang
Quoted 2 lines

from pagewalk code (where some users like pagemap need the actual address).

Indeed ...

Quoted 6 lines

Kinda lean toward option 1, even if it's more invasive. If we pass the hstate down, each arch can figure out the right addr from there.

AFAICT, for huge_ptep_get() the addr users are arm64 and powerpc, riscv doesn't really care about addr there. Looks mostly arm64-specific ...

powerpc handles it correctly in the weird "span two PMD entries" case by aligning the PMD down.

Quoted 7 lines
Quoted 4 lines

Risc-v copied from arm64, but can simply derive the #entries from the PTE value. it doesn't have to re-walk the table using the address.

But I think the following is required to fix, no?

We don't receive an unaligned ptep in huge_ptep_get, and riscv derives the number of cont ptes from the pte itself, so why is the below required?

Let me look at the actual report once more ...

I thought for a second that the problem would be having the ptep not point at the start of the hugetlb page mapping. But that should always be the case. So yes, riscv does not have any problems.

And IIUC, arm64 only has a problem when CONT_PTES != CONT_PMDS (16 kernel?).

Yeah, aligning the ptep down doesn't solve anything, it's already properly aligned.

To fix it inside arm64 code, we'd have to teach find_num_contig() to ignore the ptep and instead look for the cont bit, maybe?

But I'm sure I messed this up as I am working on 10 things at the same time :D

arch/arm64/mm/hugetlbpage.c +1 -1
index d477a9dd1b472..d1d03795c135e 100644
@@ -76,7 +76,7 @@ bool arch_hugetlb_migration_supported(struct hstate *h)
 #endif
 
 static int find_num_contig(struct mm_struct *mm, unsigned long addr,
-                          pte_t *ptep, size_t *pgsize)
+                          size_t *pgsize)
 {
        pgd_t *pgdp = pgd_offset(mm, addr);
        p4d_t *p4dp;

arch/arm64/mm/hugetlbpage.c static int find_num_contig(struct mm_struct *mm, unsigned long addr, · line 90

Quoted 5 lines
arch/arm64/mm/hugetlbpage.c +1 -1
@@ -87,7 +87,7 @@ static int find_num_contig(struct mm_struct *mm, unsigned long addr,
        p4dp = p4d_offset(pgdp, addr);
        pudp = pud_offset(p4dp, addr);
        pmdp = pmd_offset(pudp, addr);
-       if ((pte_t *)pmdp == ptep) {
+       if (pmd_cont(*pmdp)) {

We can simply do this right:

arch/arm64/mm/hugetlbpage.c +1 -1
index b8432886085af..a35fa373263dc 100644
@@ -87,7 +87,7 @@ static int find_num_contig(struct mm_struct *mm, unsigned long addr,
 	p4dp = p4d_offset(pgdp, addr);
 	pudp = pud_offset(p4dp, addr);
 	pmdp = pmd_offset(pudp, addr);
-	if ((pte_t *)pmdp == ptep) {
+	if ((pte_t *)PTR_ALIGN_DOWN(pmdp, sizeof(*pmdp) * CONT_PMDS) == ptep) {
 		*pgsize = PMD_SIZE;
 		return CONT_PMDS;
 	}
Quoted 24 lines
arch/arm64/mm/hugetlbpage.c +2 -2
                *pgsize = PMD_SIZE;
                return CONT_PMDS;
        }
@@ -131,7 +131,7 @@ pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
        if (!pte_present(orig_pte) || !pte_cont(orig_pte))
                return orig_pte;
 
-       ncontig = find_num_contig(mm, addr, ptep, &pgsize);
+       ncontig = find_num_contig(mm, addr, &pgsize);
        for (i = 0; i < ncontig; i++, ptep++) {
                pte_t pte = __ptep_get(ptep);
 
@@ -475,7 +475,7 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm,
                return;
        }
 
-       ncontig = find_num_contig(mm, addr, ptep, &pgsize);
+       ncontig = find_num_contig(mm, addr, &pgsize);
 
        pte = get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig);
        pte = pte_wrprotect(pte);
mm/memory.c +0 -0
#30 lore raw

Re: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb

Quoted 8 lines

We can simply do this right:

arch/arm64/mm/hugetlbpage.c +0 -0
index b8432886085af..a35fa373263dc 100644

arch/arm64/mm/hugetlbpage.c static int find_num_contig(struct mm_struct *mm, unsigned long addr, · line 92

Quoted 7 lines
arch/arm64/mm/hugetlbpage.c +1 -1
@@ -87,7 +87,7 @@ static int find_num_contig(struct mm_struct *mm, unsigned long addr,
 	p4dp = p4d_offset(pgdp, addr);
 	pudp = pud_offset(p4dp, addr);
 	pmdp = pmd_offset(pudp, addr);
-	if ((pte_t *)pmdp == ptep) {
+	if ((pte_t *)PTR_ALIGN_DOWN(pmdp, sizeof(*pmdp) * CONT_PMDS) == ptep) {
 		*pgsize = PMD_SIZE;
 		return CONT_PMDS;

That should also work I think.

Signature · 3 lines
Cheers,

David