Fix incorrect access of hugetlb pte entries
Subsystems (9)
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
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.
--- include/linux/hugetlb.h | 3 +++ mm/rmap.c | 16 ++++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-)
include/linux/hugetlb.h +3 -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); +static inline pte_t huge_ptep_clear_flush (struct vm_area_struct *vma,unsigned long addr,pte_t *ptep) {
mm/rmap.c +10 -6
@@ -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
PATCH 2/5 mm/rmap: use huge_ptep_get() in try_to_migrate_one()
In reply to Dev Jaintry_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.
--- mm/rmap.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
mm/rmap.c +10 -6
@@ -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
PATCH 3/5 mm/migrate: use huge_ptep_get() in remove_migration_pte()
In reply to Dev Jainremove_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.
--- mm/migrate.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
mm/migrate.c +5 -1
@@ -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
PATCH 4/5 mm/page_vma_mapped: use huge_ptep_get() for hugetlb
In reply to Dev Jaincheck_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.
--- mm/page_vma_mapped.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
mm/page_vma_mapped.c +7 -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); +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
PATCH 5/5 mm/mprotect: use huge_ptep_get() for hugetlb
In reply to Dev Jainprot_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.
--- mm/mprotect.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
mm/mprotect.c +7 -1
@@ -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
Zi Yan
reviewerQuoted 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: usehuge_ptep_get()intry_to_unmap_one()mm/rmap: usehuge_ptep_get()intry_to_migrate_one()mm/migrate: usehuge_ptep_get()inremove_migration_pte()mm/page_vma_mapped: usehuge_ptep_get()for hugetlb mm/mprotect: usehuge_ptep_get()for hugetlbinclude/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
Lance Yang
reviewerRe: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb
In reply to Dev JainQuoted 25 lines · Dev Jain
check_pte()is the final validation step inpage_vma_mapped_walk(). It readspvmw->ptewithptep_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 viaptep_get()would causepte_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()")Signed-off-by Dev Jain <[email protected]>---
mm/page_vma_mapped.c| 8 +++++++-1 file changed, 7 insertions(+), 1 deletion(-)mm/page_vma_mapped.c +0
-0
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
Muchun Song
maintainerRe: [PATCH 1/5] mm/rmap: use huge_ptep_get() in try_to_unmap_one()
In reply to Dev JainQuoted 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 casepage_vma_mapped_walk()returns the pte pointer to the hugetlb folio in pvmw.pte, but the code reads it withptep_get().On arches which provide their own
huge_ptep_get()to dereference a huge pte pointer, accessing viaptep_get()would causepte_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()")Signed-off-by Dev Jain <[email protected]>--- include/linux/hugetlb.h | 3 +++ mm/rmap.c | 16 ++++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-)include/linux/hugetlb.h +0
-0
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@@ -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);
Re: [PATCH 2/5] mm/rmap: use huge_ptep_get() in try_to_migrate_one()
In reply to Dev JainQuoted 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 withptep_get().On arches which provide their own
huge_ptep_get()to dereference a huge pte pointer, accessing viaptep_get()would causepte_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
a98a2f0c8ce1copied the bug fromtry_to_unmap_oneintotry_to_migrate_one.Fixes a98a2f0c8ce1 ("mm/rmap: split migration into its own function")Signed-off-by Dev Jain <[email protected]>
Thanks.
Re: [PATCH 3/5] mm/migrate: use huge_ptep_get() in remove_migration_pte()
In reply to Dev JainQuoted 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 withptep_get().
Quoted 5 lines
On arches which provide their own
huge_ptep_get()to dereference a huge pte pointer, accessing viaptep_get()would causepte_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")Signed-off-by Dev Jain <[email protected]>
Thanks
Muchun Song
maintainerRe: [PATCH 5/5] mm/mprotect: use huge_ptep_get() for hugetlb
In reply to Dev JainQuoted 32 lines · Dev Jain
prot_none_hugetlb_entry()is the hugetlb callback for the earlymprotect(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 needhuge_ptep_get(), reading that entry withptep_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 simpleptep_get()for x86, and theprot_nonewalk occurs only for x86. But use the correct helper anyways.Fixes 42e4089c7890 ("x86/speculation/l1tf: Disallow non privileged high MMIO PROT_NONE mappings")Signed-off-by Dev Jain <[email protected]>--- mm/mprotect.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)mm/mprotect.c +1
-0@@ -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
@@ -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)
Re: [PATCH 1/5] mm/rmap: use huge_ptep_get() in try_to_unmap_one()
In reply to Muchun SongQuoted 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 casepage_vma_mapped_walk()returns the pte pointer to the hugetlb folio in pvmw.pte, but the code reads it withptep_get().On arches which provide their own
huge_ptep_get()to dereference a huge pte pointer, accessing viaptep_get()would causepte_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()")Signed-off-by Dev Jain <[email protected]>---
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@@ -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,
MuchunQuoted 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@@ -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);
Re: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb
In reply to Lance YangQuoted 31 lines · Lance Yang
Quoted 25 lines · Dev Jain
check_pte()is the final validation step inpage_vma_mapped_walk(). It readspvmw->ptewithptep_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 viaptep_get()would causepte_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()")Signed-off-by Dev Jain <[email protected]>---
mm/page_vma_mapped.c| 8 +++++++-1 file changed, 7 insertions(+), 1 deletion(-)mm/page_vma_mapped.c +0
-0mm/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()afterpage_vma_mapped_walk()succeeds. But before this patch, hugetlb sill goes throughcheck_pte()(still usingptep_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
Re: [PATCH 5/5] mm/mprotect: use huge_ptep_get() for hugetlb
In reply to Muchun SongQuoted 38 lines · Muchun Song
Quoted 35 lines · Dev Jain
prot_none_hugetlb_entry()is the hugetlb callback for the earlymprotect(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 needhuge_ptep_get(), reading that entry withptep_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 simpleptep_get()for x86, and theprot_nonewalk occurs only for x86. But use the correct helper anyways.Fixes 42e4089c7890 ("x86/speculation/l1tf: Disallow non privileged high MMIO PROT_NONE mappings")Signed-off-by Dev Jain <[email protected]>---
mm/mprotect.c| 8 +++++++-1 file changed, 7 insertions(+), 1 deletion(-)mm/mprotect.c +0
-0@@ -699,14 +699,20 @@ static int prot_none_pte_entry(pte_t *pte, unsigned long addr, 0 : -EACCES; }+#ifdef
CONFIG_HUGETLB_PAGE
static intprot_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_tentry =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 NULLThis 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@@ -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,
MuchunQuoted 3 lines
+#endif
static intprot_none_test(unsigned long addr, unsigned long next, struct mm_walk *walk)
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 handlingI 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: usehuge_ptep_get()intry_to_unmap_one()mm/rmap: usehuge_ptep_get()intry_to_migrate_one()mm/migrate: usehuge_ptep_get()inremove_migration_pte()mm/page_vma_mapped: usehuge_ptep_get()for hugetlb mm/mprotect: usehuge_ptep_get()for hugetlbinclude/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(-)
Muchun Song
maintainerRe: [PATCH 1/5] mm/rmap: use huge_ptep_get() in try_to_unmap_one()
In reply to Dev JainQuoted 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 casepage_vma_mapped_walk()returns the pte pointer to the hugetlb folio in pvmw.pte, but the code reads it withptep_get().On arches which provide their own
huge_ptep_get()to dereference a huge pte pointer, accessing viaptep_get()would causepte_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()")Signed-off-by Dev Jain <[email protected]>--- 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@@ -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,
MuchunQuoted 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@@ -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);
Muchun Song
maintainerRe: [PATCH 5/5] mm/mprotect: use huge_ptep_get() for hugetlb
In reply to Dev JainQuoted 54 lines · Dev Jain
Quoted 38 lines · Muchun Song
Quoted 35 lines · Dev Jain
prot_none_hugetlb_entry()is the hugetlb callback for the earlymprotect(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 needhuge_ptep_get(), reading that entry withptep_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 simpleptep_get()for x86, and theprot_nonewalk occurs only for x86. But use the correct helper anyways.Fixes 42e4089c7890 ("x86/speculation/l1tf: Disallow non privileged high MMIO PROT_NONE mappings")Signed-off-by Dev Jain <[email protected]>--- mm/mprotect.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)mm/mprotect.c +2
-1@@ -699,14 +699,20 @@ static int prot_none_pte_entry(pte_t *pte, unsigned long addr, 0 : -EACCES; } +#ifdef CONFIG_HUGETLB_PAGEstatic 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 NULLThis 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@@ -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,
MuchunQuoted 3 lines
mm/mprotect.c +1
-0+#endif static int prot_none_test (unsigned long addr,unsigned long next,struct mm_walk *walk)
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 earlymprotect(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 needhuge_ptep_get(), reading that entry withptep_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 simpleptep_get()for x86, and theprot_nonewalk occurs only for x86. But use the correct helper anyways.Fixes 42e4089c7890 ("x86/speculation/l1tf: Disallow non privileged high MMIO PROT_NONE mappings")Signed-off-by Dev Jain <[email protected]>--- mm/mprotect.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)mm/mprotect.c +0
-0@@ -699,14 +699,20 @@ static int prot_none_pte_entry(pte_t *pte, unsigned long addr, 0 : -EACCES; } +#ifdef CONFIG_HUGETLB_PAGEstatic 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 NULLThis 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@@ -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,
MuchunQuoted 3 lines
mm/mprotect.c +1
-0+#endif static int prot_none_test (unsigned long addr,unsigned long next,struct mm_walk *walk)
Lance Yang
reviewerRe: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb
In reply to Dev JainQuoted 25 lines · Dev Jain
check_pte()is the final validation step inpage_vma_mapped_walk(). It readspvmw->ptewithptep_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 viaptep_get()would causepte_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()")Signed-off-by Dev Jain <[email protected]>---
mm/page_vma_mapped.c| 8 +++++++-1 file changed, 7 insertions(+), 1 deletion(-)mm/page_vma_mapped.c +0
-0
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
@@ -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
Lance Yang
reviewerRe: [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 inpage_vma_mapped_walk(). It readspvmw->ptewithptep_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 viaptep_get()would causepte_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()")Signed-off-by Dev Jain <[email protected]>---
mm/page_vma_mapped.c| 8 +++++++-1 file changed, 7 insertions(+), 1 deletion(-)mm/page_vma_mapped.c +0
-0mm/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 tohuge_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->addressneed not be hugepage-aligned.@Miaohe
For arm64,
CONT_PMD_SIZEis one supported HugeTLB size. With such a VMA,page_vma_mapped_walk()passes that size tohugetlb_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 arm64huge_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(structmm_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->pteis calculated from the aligned addr, not necessarily from the originalpvmw->address. Butcheck_pte()passes the original address together withpvmw->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_thuge_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, sofind_num_contig()can returnCONT_PTESfor aCONT_PMDHugeTLB mapping.On 16K arm64, that changes ncontig from 32 to 128. So
huge_ptep_get()can walk past theCONT_PMDentries, and possibly past the PMD table.Should
check_pte()pass the address matchingpvmw->pte, sth like:---8<---
mm/page_vma_mapped.c +7
-5@@ -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->addressunchanged forpage_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
Re: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb
In reply to Lance YangQuoted 22 lines · Lance Yang
Quoted 20 lines · Dev Jain
check_pte()is the final validation step inpage_vma_mapped_walk(). It readspvmw->ptewithptep_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 viaptep_get()would causepte_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()")Signed-off-by Dev Jain <[email protected]>---
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
-0mm/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 tohuge_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->addressneed not be hugepage-aligned.@Miaohe
For arm64,
CONT_PMD_SIZEis one supported HugeTLB size. With such a VMA,page_vma_mapped_walk()passes that size tohugetlb_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 arm64huge_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(structmm_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->pteis calculated from the aligned addr, not necessarily from the originalpvmw->address. Butcheck_pte()passes the original address together withpvmw->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_thuge_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, sofind_num_contig()can returnCONT_PTESfor aCONT_PMDHugeTLB mapping.On 16K arm64, that changes ncontig from 32 to 128. So
huge_ptep_get()can walk past theCONT_PMDentries, and possibly past the PMD table.Should
check_pte()pass the address matchingpvmw->pte, sth like:---8<---
mm/page_vma_mapped.c +7
-5@@ -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->addressunchanged forpage_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
Lance Yang
reviewerRe: [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 inpage_vma_mapped_walk(). It readspvmw->ptewithptep_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 viaptep_get()would causepte_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()")Signed-off-by Dev Jain <[email protected]>---
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
-0mm/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 tohuge_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->addressneed 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_SIZEis one supported HugeTLB size. With such a VMA,page_vma_mapped_walk()passes that size tohugetlb_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 arm64huge_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(structmm_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->pteis calculated from the aligned addr, not necessarily from the originalpvmw->address. Butcheck_pte()passes the original address together withpvmw->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_thuge_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, sofind_num_contig()can returnCONT_PTESfor aCONT_PMDHugeTLB mapping.On 16K arm64, that changes ncontig from 32 to 128. So
huge_ptep_get()can walk past theCONT_PMDentries, and possibly past the PMD table.Should
check_pte()pass the address matchingpvmw->pte, sth like:---8<---
mm/page_vma_mapped.c +7
-5@@ -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->addressunchanged forpage_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
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 inpage_vma_mapped_walk(). It readspvmw->ptewithptep_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 viaptep_get()would causepte_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()")Signed-off-by Dev Jain <[email protected]>---
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
-0mm/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 tohuge_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->addressneed 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,
structlist_head*tokill, intforce_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, structlist_head*to_kill, intforce_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,
structvm_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_tpage_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->addresscan be offset from the start of the hugetlb mapping by
folio_page_idx(folio, page)<<PAGE_SHIFTShould
check_pte()pass the hugepage-aligned address tohuge_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_SIZEis one supported HugeTLB size. With such a VMA,page_vma_mapped_walk()passes that size tohugetlb_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 arm64huge_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(structmm_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->pteis calculated from the aligned addr, not necessarily from the originalpvmw->address. Butcheck_pte()passes the original address together withpvmw->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_thuge_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, sofind_num_contig()can returnCONT_PTESfor aCONT_PMDHugeTLB mapping.On 16K arm64, that changes ncontig from 32 to 128. So
huge_ptep_get()can walk past theCONT_PMDentries, and possibly past the PMD table.Should
check_pte()pass the address matchingpvmw->pte, sth like:---8<---
mm/page_vma_mapped.c +7
-5@@ -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->addressunchanged forpage_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
Lance Yang
reviewerRe: [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 inpage_vma_mapped_walk(). It readspvmw->ptewithptep_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 viaptep_get()would causepte_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()")Signed-off-by Dev Jain <[email protected]>---
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
-0mm/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 tohuge_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->addressneed 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,
structlist_head*tokill, intforce_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, structlist_head*to_kill, intforce_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,
structvm_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_tpage_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->addresscan be offset from the start of the hugetlb mapping by
folio_page_idx(folio, page)<<PAGE_SHIFTShould
check_pte()pass the hugepage-aligned address tohuge_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_SIZEis one supported HugeTLB size. With such a VMA,page_vma_mapped_walk()passes that size tohugetlb_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 arm64huge_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(structmm_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->pteis calculated from the aligned addr, not necessarily from the originalpvmw->address. Butcheck_pte()passes the original address together withpvmw->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_thuge_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, sofind_num_contig()can returnCONT_PTESfor aCONT_PMDHugeTLB mapping.On 16K arm64, that changes ncontig from 32 to 128. So
huge_ptep_get()can walk past theCONT_PMDentries, and possibly past the PMD table.Should
check_pte()pass the address matchingpvmw->pte, sth like:---8<---
mm/page_vma_mapped.c +7
-5@@ -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->addressunchanged forpage_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
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 inpage_vma_mapped_walk(). It readspvmw->ptewithptep_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 viaptep_get()would causepte_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()")Signed-off-by Dev Jain <[email protected]>---
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
-0mm/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 tohuge_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->addressneed 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); structpage_vma_mapped_walkpvmw = {
.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_tpage_pgoff(const struct folio *folio, const struct page *page)
{
returnfolio->index+folio_page_idx(folio, page); }So if the poisoned PFN points to a tail page,
pvmw->addresscan 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_SHIFTShould
check_pte()pass the hugepage-aligned address tohuge_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_SIZEis one supported HugeTLB size. With such a VMA,page_vma_mapped_walk()passes that size tohugetlb_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 arm64huge_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(structmm_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->pteis calculated from the aligned addr, not necessarily from the originalpvmw->address. Butcheck_pte()passes the original address together withpvmw->pte:+ ptent =
huge_ptep_get(pvmw->vma->vm_mm, pvmw->address, + pvmw->pte);arm64 then uses that addr again to choose ncontig:
pte_thuge_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++) {
...
}
returnorig_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;
returnCONT_PMDS;
}
returnCONT_PTES;
}With a tail address, pmdp may no longer point at
pvmw->pte, sofind_num_contig()can returnCONT_PTESfor aCONT_PMDHugeTLB mapping.On 16K arm64, that changes ncontig from 32 to 128. So
huge_ptep_get()can walk past theCONT_PMDentries, and possibly past the PMD table.Should
check_pte()pass the address matchingpvmw->pte, sth like:---8<---
mm/page_vma_mapped.c +7
-4@@ -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, LanceQuoted 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
-0if (pvmw->flags & PVMW_MIGRATION) { const softleaf_t entry = softleaf_from_pte(ptent);--
2.43.0.
Re: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb
In reply to Lance YangQuoted 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 inpage_vma_mapped_walk(). It readspvmw->ptewithptep_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 viaptep_get()would causepte_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()")Signed-off-by Dev Jain <[email protected]>---
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
-0mm/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 tohuge_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->addressneed 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); structpage_vma_mapped_walkpvmw = {
.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_tpage_pgoff(const struct folio *folio, const struct page *page)
{
returnfolio->index+folio_page_idx(folio, page); }Quoted 7 lines
So if the poisoned PFN points to a tail page,
pvmw->addresscan be offset from the start of the hugetlb mapping by
folio_page_idx(folio, page)<<PAGE_SHIFTShould
check_pte()pass the hugepage-aligned address tohuge_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
@@ -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_rangestatic inline int is_hugepage_only_range (struct mm_struct *mm,unsigned long addr,unsigned long len)
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);
Quoted 129 lines
Thanks, Lance
Quoted 124 lines
Quoted 123 lines
Cheers, Lance
Quoted 118 lines
Quoted 116 lines
For arm64,
CONT_PMD_SIZEis one supported HugeTLB size. With such a VMA,page_vma_mapped_walk()passes that size tohugetlb_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 arm64huge_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(structmm_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->pteis calculated from the aligned addr, not necessarily from the originalpvmw->address. Butcheck_pte()passes the original address together withpvmw->pte:+ ptent =
huge_ptep_get(pvmw->vma->vm_mm, pvmw->address, + pvmw->pte);arm64 then uses that addr again to choose ncontig:
pte_thuge_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++) {
...
}
returnorig_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;
returnCONT_PMDS;
}
returnCONT_PTES;
}With a tail address, pmdp may no longer point at
pvmw->pte, sofind_num_contig()can returnCONT_PTESfor aCONT_PMDHugeTLB mapping.On 16K arm64, that changes ncontig from 32 to 128. So
huge_ptep_get()can walk past theCONT_PMDentries, and possibly past the PMD table.Should
check_pte()pass the address matchingpvmw->pte, sth like:---8<---
mm/page_vma_mapped.c +7
-4@@ -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, LanceQuoted 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
Lance Yang
reviewerRe: [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@@ -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_rangestatic inline int is_hugepage_only_range (struct mm_struct *mm,unsigned long addr,unsigned long len)mm/page_vma_mapped.c +0
-0
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
David Hildenbrand (Arm)
maintainerRe: [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@@ -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
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@@ -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
Lance Yang
reviewerRe: [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@@ -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
David Hildenbrand (Arm)
maintainerRe: [PATCH 4/5] mm/page_vma_mapped: use huge_ptep_get() for hugetlb
In reply to Dev JainQuoted 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
@@ -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
Lance Yang
reviewerRe: [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@@ -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_thuge_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 fromhugetlb.cwhere we should take better care of that. (e.g., partially zapping a hugetlb page is not possible)Signature · 3 lines
Cheers, David
David Hildenbrand (Arm)
maintainerRe: [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@@ -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_thuge_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
@@ -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
Lance Yang
reviewerRe: [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@@ -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_thuge_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@@ -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 innum_contig_ptes()entirely and possibly just remove it.That would probably be cleanest.
Agreed!
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@@ -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_thuge_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@@ -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 innum_contig_ptes()entirely and possibly just remove it.That would probably be cleanest.
David Hildenbrand (Arm)
maintainerRe: [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
@@ -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
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@@ -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
@@ -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
David Hildenbrand (Arm)
maintainerRe: [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
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