Fixes for hugetlb and vmalloc on arm64
Subsystems (15)
v2 cover Fixes for hugetlb and vmalloc on arm64
Hi All,
This series contains some fixes for hugetlb on arm64, and is split out from v1 of a wider series at [1]. While the last patch is technically targetting core-mm and is not directly related to arm64, I'd like to to go via the arm64 tree so that the wider performance improvement series (v2 to be posted shortly) that depends on this series doesn't have to be robust to the fix not being present.
I've included maintainers/reviewers for all the arches that are (trivially) touched due to the API changes, hoping for some ACKs.
Changes since v1 [1]
====================
- Added Rb from Anshuman - Thanks!
- Added "#ifndef __PAGETABLE_PMD_FOLDED" around PUD_SIZE in flush_hugetlb_tlb_range()
I've marked all of these as candidates for backport to stable.
Applies on top of v6.14-rc3. All mm selftests run and pass.
[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/
Thanks,
Ryan
Ryan Roberts (4):
mm: hugetlb: Add huge page size param to huge_ptep_get_and_clear()
arm64: hugetlb: Fix huge_ptep_get_and_clear() for non-present ptes
arm64: hugetlb: Fix flush_hugetlb_tlb_range() invalidation level
mm: Don't skip arch_sync_kernel_mappings() in error paths
arch/arm64/include/asm/hugetlb.h | 26 ++++++++++----- arch/arm64/mm/hugetlbpage.c | 48 +++++++++++++--------------- arch/loongarch/include/asm/hugetlb.h | 6 ++-- arch/mips/include/asm/hugetlb.h | 6 ++-- arch/parisc/include/asm/hugetlb.h | 2 +- arch/parisc/mm/hugetlbpage.c | 2 +- arch/powerpc/include/asm/hugetlb.h | 6 ++-- arch/riscv/include/asm/hugetlb.h | 3 +- arch/riscv/mm/hugetlbpage.c | 2 +- arch/s390/include/asm/hugetlb.h | 12 ++++--- arch/s390/mm/hugetlbpage.c | 10 ++++-- arch/sparc/include/asm/hugetlb.h | 2 +- arch/sparc/mm/hugetlbpage.c | 2 +- include/asm-generic/hugetlb.h | 2 +- include/linux/hugetlb.h | 4 ++- mm/hugetlb.c | 4 +-- mm/memory.c | 6 ++-- mm/vmalloc.c | 4 +-- 18 files changed, 87 insertions(+), 60 deletions(-)
--
2.43.0
PATCH v2 1/4 mm: hugetlb: Add huge page size param to huge_ptep_get_and_clear()
In order to fix a bug, arm64 needs to be told the size of the huge page for which the huge_pte is being set in huge_ptep_get_and_clear(). Provide for this by adding an unsigned long sz parameter to the function. This follows the same pattern as huge_pte_clear() and set_huge_pte_at().
This commit makes the required interface modifications to the core mm as well as all arches that implement this function (arm64, loongarch, mips, parisc, powerpc, riscv, s390, sparc). The actual arm64 bug will be fixed in a separate commit.
--- arch/arm64/include/asm/hugetlb.h | 4 ++-- arch/arm64/mm/hugetlbpage.c | 8 +++++--- arch/loongarch/include/asm/hugetlb.h | 6 ++++-- arch/mips/include/asm/hugetlb.h | 6 ++++-- arch/parisc/include/asm/hugetlb.h | 2 +- arch/parisc/mm/hugetlbpage.c | 2 +- arch/powerpc/include/asm/hugetlb.h | 6 ++++-- arch/riscv/include/asm/hugetlb.h | 3 ++- arch/riscv/mm/hugetlbpage.c | 2 +- arch/s390/include/asm/hugetlb.h | 12 ++++++++---- arch/s390/mm/hugetlbpage.c | 10 ++++++++-- arch/sparc/include/asm/hugetlb.h | 2 +- arch/sparc/mm/hugetlbpage.c | 2 +- include/asm-generic/hugetlb.h | 2 +- include/linux/hugetlb.h | 4 +++- mm/hugetlb.c | 4 ++-- 16 files changed, 48 insertions(+), 27 deletions(-)
arch/arm64/include/asm/hugetlb.h +2 -2
@@ -42,8 +42,8 @@ extern int huge_ptep_set_access_flags(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, pte_t pte, int dirty);#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEAR -extern pte_t huge_ptep_get_and_clear (struct mm_struct *mm, -unsigned long addr,pte_t *ptep); +extern pte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr, +pte_t *ptep,unsigned long sz);#define __HAVE_ARCH_HUGE_PTEP_SET_WRPROTECTextern void huge_ptep_set_wrprotect (struct mm_struct *mm,unsigned long addr,pte_t *ptep);
arch/arm64/mm/hugetlbpage.c +5 -3
@@ -396,8 +396,8 @@ void huge_pte_clear(struct mm_struct *mm, unsigned long addr,__pte_clear (mm, addr, ptep); } -pte_t huge_ptep_get_and_clear (struct mm_struct *mm, -unsigned long addr,pte_t *ptep) +pte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr, +pte_t *ptep,unsigned long sz) {int ncontig;size_t pgsize;
@@ -549,6 +549,8 @@ bool __init arch_hugetlb_valid_size(unsigned long size)pte_t huge_ptep_modify_prot_start (struct vm_area_struct *vma,unsigned long addr,pte_t *ptep) { +unsigned long psize =huge_page_size (hstate_vma (vma)); +if (alternative_has_cap_unlikely (ARM64_WORKAROUND_2645198 )) {/* * Break-before-make (BBM) is required for all user space mappings
@@ -558,7 +560,7 @@ pte_t huge_ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addrif (pte_user_exec (__ptep_get (ptep)))return huge_ptep_clear_flush (vma, addr, ptep); } -return huge_ptep_get_and_clear (vma->vm_mm, addr, ptep); +return huge_ptep_get_and_clear (vma->vm_mm, addr, ptep, psize); } void huge_ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep,
arch/loongarch/include/asm/hugetlb.h +4 -2
@@ -36,7 +36,8 @@ static inline void huge_pte_clear(struct mm_struct *mm, unsigned long addr,#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEARstatic inline pte_t huge_ptep_get_and_clear (struct mm_struct *mm, -unsigned long addr,pte_t *ptep) +unsigned long addr,pte_t *ptep, +unsigned long sz) {pte_t clear;pte_t pte =ptep_get (ptep);
@@ -51,8 +52,9 @@ static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep) { pte_t pte; +unsigned long sz =huge_page_size (hstate_vma (vma)); - pte =huge_ptep_get_and_clear (vma->vm_mm, addr, ptep); + pte =huge_ptep_get_and_clear (vma->vm_mm, addr, ptep, sz);flush_tlb_page (vma, addr);return pte; }
arch/mips/include/asm/hugetlb.h +4 -2
@@ -27,7 +27,8 @@ static inline int prepare_hugepage_range(struct file *file,#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEARstatic inline pte_t huge_ptep_get_and_clear (struct mm_struct *mm, -unsigned long addr,pte_t *ptep) +unsigned long addr,pte_t *ptep, +unsigned long sz) {pte_t clear;pte_t pte = *ptep;
@@ -42,13 +43,14 @@ static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep) { pte_t pte; +unsigned long sz =huge_page_size (hstate_vma (vma));/* * clear the huge pte entry firstly, so that the other smp threads will * not get old pte entry after finishing flush_tlb_page and before * setting new huge pte entry */ - pte =huge_ptep_get_and_clear (vma->vm_mm, addr, ptep); + pte =huge_ptep_get_and_clear (vma->vm_mm, addr, ptep, sz);flush_tlb_page (vma, addr);return pte; }
arch/parisc/include/asm/hugetlb.h +1 -1
@@ -10,7 +10,7 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEARpte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr, -pte_t *ptep); +pte_t *ptep,unsigned long sz);#define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
arch/parisc/mm/hugetlbpage.c +1 -1
@@ -126,7 +126,7 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,pte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr, -pte_t *ptep) +pte_t *ptep,unsigned long sz) {pte_t entry;
arch/powerpc/include/asm/hugetlb.h +4 -2
@@ -45,7 +45,8 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep,#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEARstatic inline pte_t huge_ptep_get_and_clear (struct mm_struct *mm, -unsigned long addr,pte_t *ptep) +unsigned long addr,pte_t *ptep, +unsigned long sz) {return __pte (pte_update (mm, addr, ptep, ~0UL ,0 ,1 )); }
@@ -55,8 +56,9 @@ static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep) { pte_t pte; +unsigned long sz =huge_page_size (hstate_vma (vma)); - pte =huge_ptep_get_and_clear (vma->vm_mm, addr, ptep); + pte =huge_ptep_get_and_clear (vma->vm_mm, addr, ptep, sz);flush_hugetlb_page (vma, addr);return pte; }
arch/riscv/include/asm/hugetlb.h +2 -1
@@ -28,7 +28,8 @@ void set_huge_pte_at(struct mm_struct *mm,#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEARpte_t huge_ptep_get_and_clear (struct mm_struct *mm, -unsigned long addr,pte_t *ptep); +unsigned long addr,pte_t *ptep, +unsigned long sz);#define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
arch/riscv/mm/hugetlbpage.c +1 -1
@@ -293,7 +293,7 @@ int huge_ptep_set_access_flags(struct vm_area_struct *vma,pte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr, -pte_t *ptep) +pte_t *ptep,unsigned long sz) {pte_t orig_pte =ptep_get (ptep);int pte_num;
arch/s390/include/asm/hugetlb.h +8 -4
@@ -26,7 +26,11 @@ void __set_huge_pte_at(struct mm_struct *mm, unsigned long addr,pte_t huge_ptep_get (struct mm_struct *mm,unsigned long addr,pte_t *ptep);#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEAR -pte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr,pte_t *ptep); +pte_t huge_ptep_get_and_clear (struct mm_struct *mm, +unsigned long addr,pte_t *ptep, +unsigned long sz); +pte_t __huge_ptep_get_and_clear (struct mm_struct *mm, +unsigned long addr,pte_t *ptep);static inline void arch_clear_hugetlb_flags (struct folio *folio) {
@@ -48,7 +52,7 @@ static inline void huge_pte_clear(struct mm_struct *mm, unsigned long addr,static inline pte_t huge_ptep_clear_flush (struct vm_area_struct *vma,unsigned long address,pte_t *ptep) { -return huge_ptep_get_and_clear (vma->vm_mm, address, ptep); +return __huge_ptep_get_and_clear (vma->vm_mm, address, ptep); }#define __HAVE_ARCH_HUGE_PTEP_SET_ACCESS_FLAGS
@@ -59,7 +63,7 @@ static inline int huge_ptep_set_access_flags(struct vm_area_struct *vma,int changed = !pte_same (huge_ptep_get (vma->vm_mm, addr, ptep), pte);if (changed) { -huge_ptep_get_and_clear (vma->vm_mm, addr, ptep); +__huge_ptep_get_and_clear (vma->vm_mm, addr, ptep);__set_huge_pte_at (vma->vm_mm, addr, ptep, pte); }return changed;
@@ -69,7 +73,7 @@ static inline int huge_ptep_set_access_flags(struct vm_area_struct *vma,static inline void huge_ptep_set_wrprotect (struct mm_struct *mm,unsigned long addr,pte_t *ptep) { -pte_t pte =huge_ptep_get_and_clear (mm, addr, ptep); +pte_t pte =__huge_ptep_get_and_clear (mm, addr, ptep);__set_huge_pte_at (mm, addr, ptep,pte_wrprotect (pte)); }
arch/s390/mm/hugetlbpage.c +8 -2
@@ -188,8 +188,8 @@ pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)return __rste_to_pte (pte_val (*ptep)); } -pte_t huge_ptep_get_and_clear (struct mm_struct *mm, -unsigned long addr,pte_t *ptep) +pte_t __huge_ptep_get_and_clear (struct mm_struct *mm, +unsigned long addr,pte_t *ptep) {pte_t pte =huge_ptep_get (mm, addr, ptep);pmd_t *pmdp = (pmd_t *) ptep;
@@ -202,6 +202,12 @@ pte_t huge_ptep_get_and_clear(struct mm_struct *mm,return pte; } +pte_t huge_ptep_get_and_clear (struct mm_struct *mm, +unsigned long addr,pte_t *ptep,unsigned long sz) +{ +return __huge_ptep_get_and_clear (mm, addr, ptep); +} +pte_t *huge_pte_alloc (struct mm_struct *mm,struct vm_area_struct *vma,unsigned long addr,unsigned long sz) {
arch/sparc/include/asm/hugetlb.h +1 -1
@@ -20,7 +20,7 @@ void __set_huge_pte_at(struct mm_struct *mm, unsigned long addr,#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEARpte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr, -pte_t *ptep); +pte_t *ptep,unsigned long sz);#define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
arch/sparc/mm/hugetlbpage.c +1 -1
@@ -260,7 +260,7 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, }pte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr, -pte_t *ptep) +pte_t *ptep,unsigned long sz) {unsigned int i, nptes, orig_shift, shift;unsigned long size;
include/asm-generic/hugetlb.h +1 -1
@@ -90,7 +90,7 @@ static inline void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,#ifndef __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEARstatic inline pte_t huge_ptep_get_and_clear (struct mm_struct *mm, -unsigned long addr,pte_t *ptep) +unsigned long addr,pte_t *ptep,unsigned long sz) {return ptep_get_and_clear (mm, addr, ptep); }
include/linux/hugetlb.h +3 -1
@@ -1004,7 +1004,9 @@ static inline void hugetlb_count_sub(long l, struct mm_struct *mm)static inline pte_t huge_ptep_modify_prot_start (struct vm_area_struct *vma,unsigned long addr,pte_t *ptep) { -return huge_ptep_get_and_clear (vma->vm_mm, addr, ptep); +unsigned long psize =huge_page_size (hstate_vma (vma)); + +return huge_ptep_get_and_clear (vma->vm_mm, addr, ptep, psize); }#endif
mm/hugetlb.c +2 -2
@@ -5447,7 +5447,7 @@ static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr,if (src_ptl != dst_ptl)spin_lock_nested (src_ptl,SINGLE_DEPTH_NESTING ); - pte =huge_ptep_get_and_clear (mm, old_addr, src_pte); + pte =huge_ptep_get_and_clear (mm, old_addr, src_pte, sz);if (need_clear_uffd_wp &&pte_marker_uffd_wp (pte))huge_pte_clear (mm, new_addr, dst_pte, sz);
@@ -5622,7 +5622,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,set_vma_resv_flags (vma,HPAGE_RESV_UNMAPPED ); } - pte =huge_ptep_get_and_clear (mm, address, ptep); + pte =huge_ptep_get_and_clear (mm, address, ptep, sz);tlb_remove_huge_tlb_entry (h, tlb, ptep, address);if (huge_pte_dirty (pte))set_page_dirty (page);
Signature · 1 line
2.43.0
PATCH v2 2/4 arm64: hugetlb: Fix huge_ptep_get_and_clear() for non-present ptes
In reply to Ryan Robertsarm64 supports multiple huge_pte sizes. Some of the sizes are covered by a single pte entry at a particular level (PMD_SIZE, PUD_SIZE), and some are covered by multiple ptes at a particular level (CONT_PTE_SIZE, CONT_PMD_SIZE). So the function has to figure out the size from the huge_pte pointer. This was previously done by walking the pgtable to determine the level and by using the PTE_CONT bit to determine the number of ptes at the level.
But the PTE_CONT bit is only valid when the pte is present. For non-present pte values (e.g. markers, migration entries), the previous implementation was therefore erroniously determining the size. There is at least one known caller in core-mm, move_huge_pte(), which may call huge_ptep_get_and_clear() for a non-present pte. So we must be robust to this case. Additionally the "regular" ptep_get_and_clear() is robust to being called for non-present ptes so it makes sense to follow the behaviour.
Fix this by using the new sz parameter which is now provided to the function. Additionally when clearing each pte in a contig range, don't gather the access and dirty bits if the pte is not present.
An alternative approach that would not require API changes would be to store the PTE_CONT bit in a spare bit in the swap entry pte for the non-present case. But it felt cleaner to follow other APIs' lead and just pass in the size.
As an aside, PTE_CONT is bit 52, which corresponds to bit 40 in the swap entry offset field (layout of non-present pte). Since hugetlb is never swapped to disk, this field will only be populated for markers, which always set this bit to 0 and hwpoison swap entries, which set the offset field to a PFN; So it would only ever be 1 for a 52-bit PVA system where memory in that high half was poisoned (I think!). So in practice, this bit would almost always be zero for non-present ptes and we would only clear the first entry if it was actually a contiguous block. That's probably a less severe symptom than if it was always interpretted as 1 and cleared out potentially-present neighboring PTEs.
--- arch/arm64/mm/hugetlbpage.c | 40 ++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 23 deletions(-)
arch/arm64/mm/hugetlbpage.c +17 -23
@@ -163,24 +163,23 @@ static pte_t get_clear_contig(struct mm_struct *mm, unsigned long pgsize, unsigned long ncontig) { -pte_t orig_pte =__ptep_get (ptep); -unsigned long i; - -for (i =0 ; i < ncontig; i++, addr += pgsize, ptep++) { -pte_t pte =__ptep_get_and_clear (mm, addr, ptep); - -/* -* If HW_AFDBM is enabled, then the HW could turn on -* the dirty or accessed bit for any page in the set, -* so check them all. -*/ -if (pte_dirty (pte)) - orig_pte =pte_mkdirty (orig_pte); - -if (pte_young (pte)) - orig_pte =pte_mkyoung (orig_pte); +pte_t pte, tmp_pte; +bool present; + + pte =__ptep_get_and_clear (mm, addr, ptep); + present =pte_present (pte); +while (--ncontig) { + ptep++; + addr += pgsize; + tmp_pte =__ptep_get_and_clear (mm, addr, ptep); +if (present) { +if (pte_dirty (tmp_pte)) + pte =pte_mkdirty (pte); +if (pte_young (tmp_pte)) + pte =pte_mkyoung (pte); + } } -return orig_pte; +return pte; } static pte_t get_clear_contig_flush(struct mm_struct *mm,
@@ -401,13 +400,8 @@ pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, {int ncontig;size_t pgsize; -pte_t orig_pte =__ptep_get (ptep); - -if (!pte_cont (orig_pte)) -return __ptep_get_and_clear (mm, addr, ptep); - - ncontig =find_num_contig (mm, addr, ptep, &pgsize); + ncontig =num_contig_ptes (sz, &pgsize);return get_clear_contig (mm, addr, ptep, pgsize, ncontig); }
Signature · 1 line
2.43.0
PATCH v2 3/4 arm64: hugetlb: Fix flush_hugetlb_tlb_range() invalidation level
In reply to Ryan Robertscommit c910f2b65518 ("arm64/mm: Update tlb invalidation routines for FEAT_LPA2") changed the "invalidation level unknown" hint from 0 to TLBI_TTL_UNKNOWN (INT_MAX). But the fallback "unknown level" path in flush_hugetlb_tlb_range() was not updated. So as it stands, when trying to invalidate CONT_PMD_SIZE or CONT_PTE_SIZE hugetlb mappings, we will spuriously try to invalidate at level 0 on LPA2-enabled systems.
Fix this so that the fallback passes TLBI_TTL_UNKNOWN, and while we are at it, explicitly use the correct stride and level for CONT_PMD_SIZE and CONT_PTE_SIZE, which should provide a minor optimization.
--- arch/arm64/include/asm/hugetlb.h | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)
arch/arm64/include/asm/hugetlb.h +16 -6
@@ -76,12 +76,22 @@ static inline void flush_hugetlb_tlb_range(struct vm_area_struct *vma, {unsigned long stride =huge_page_size (hstate_vma (vma)); -if (stride ==PMD_SIZE ) -__flush_tlb_range (vma, start, end, stride, false,2 ); -else if (stride ==PUD_SIZE ) -__flush_tlb_range (vma, start, end, stride, false,1 ); -else -__flush_tlb_range (vma, start, end,PAGE_SIZE , false,0 ); +switch (stride) { +#ifndef __PAGETABLE_PMD_FOLDED +case PUD_SIZE : +__flush_tlb_range (vma, start, end,PUD_SIZE , false,1 ); +break ; +#endif +case CONT_PMD_SIZE : +case PMD_SIZE : +__flush_tlb_range (vma, start, end,PMD_SIZE , false,2 ); +break ; +case CONT_PTE_SIZE : +__flush_tlb_range (vma, start, end,PAGE_SIZE , false,3 ); +break ; +default : +__flush_tlb_range (vma, start, end,PAGE_SIZE , false,TLBI_TTL_UNKNOWN ); + } }#endif /* __ASM_HUGETLB_H */
Signature · 1 line
2.43.0
PATCH v2 4/4 mm: Don't skip arch_sync_kernel_mappings() in error paths
In reply to Ryan RobertsFix callers that previously skipped calling arch_sync_kernel_mappings() if an error occurred during a pgtable update. The call is still required to sync any pgtable updates that may have occurred prior to hitting the error condition.
These are theoretical bugs discovered during code review.
--- mm/memory.c | 6 ++++-- mm/vmalloc.c | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-)
mm/memory.c +4 -2
@@ -3040,8 +3040,10 @@ static int __apply_to_page_range(struct mm_struct *mm, unsigned long addr, next =pgd_addr_end (addr, end);if (pgd_none (*pgd) && !create)continue ; -if (WARN_ON_ONCE (pgd_leaf (*pgd))) -return -EINVAL ; +if (WARN_ON_ONCE (pgd_leaf (*pgd))) { + err = -EINVAL ; +break ; + }if (!pgd_none (*pgd) &&WARN_ON_ONCE (pgd_bad (*pgd))) {if (!create)continue ;
mm/vmalloc.c +2 -2
@@ -586,13 +586,13 @@ static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end, mask |=PGTBL_PGD_MODIFIED ; err =vmap_pages_p4d_range (pgd, addr, next, prot, pages, &nr, &mask);if (err) -return err; +break ; }while (pgd++, addr = next, addr != end);if (mask &ARCH_PAGE_TABLE_SYNC_MASK )arch_sync_kernel_mappings (start, end); -return 0 ; +return err; }/*
Signature · 1 line
2.43.0
Re: [PATCH v2 1/4] mm: hugetlb: Add huge page size param to huge_ptep_get_and_clear()
In reply to Ryan RobertsQuoted 6 lines · Ryan Roberts
In order to fix a bug, arm64 needs to be told the size of the huge page for which the
huge_pteis being set inhuge_ptep_get_and_clear(). Provide for this by adding anunsigned long szparameter to the function. This follows the same pattern ashuge_pte_clear()andset_huge_pte_at().
Quoted 8 lines
This commit makes the required interface modifications to the core mm as well as all arches that implement this function (arm64, loongarch, mips, parisc, powerpc, riscv, s390, sparc). The actual arm64 bug will be fixed in a separate commit.
Fixes 66b3923a1a0f ("arm64: hugetlb: add support for PTE contiguous bit")Signed-off-by Ryan Roberts <[email protected]>
LGTM
Quoted 348 lines
--- arch/arm64/include/asm/hugetlb.h | 4 ++-- arch/arm64/mm/hugetlbpage.c | 8 +++++--- arch/loongarch/include/asm/hugetlb.h | 6 ++++-- arch/mips/include/asm/hugetlb.h | 6 ++++-- arch/parisc/include/asm/hugetlb.h | 2 +- arch/parisc/mm/hugetlbpage.c | 2 +- arch/powerpc/include/asm/hugetlb.h | 6 ++++-- arch/riscv/include/asm/hugetlb.h | 3 ++- arch/riscv/mm/hugetlbpage.c | 2 +- arch/s390/include/asm/hugetlb.h | 12 ++++++++---- arch/s390/mm/hugetlbpage.c | 10 ++++++++-- arch/sparc/include/asm/hugetlb.h | 2 +- arch/sparc/mm/hugetlbpage.c | 2 +- include/asm-generic/hugetlb.h | 2 +- include/linux/hugetlb.h | 4 +++- mm/hugetlb.c | 4 ++-- 16 files changed, 48 insertions(+), 27 deletions(-)arch/arm64/include/asm/hugetlb.h +2
-2@@ -42,8 +42,8 @@ extern int huge_ptep_set_access_flags(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, pte_t pte, int dirty);#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEAR -extern pte_t huge_ptep_get_and_clear (struct mm_struct *mm, -unsigned long addr,pte_t *ptep); +extern pte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr, +pte_t *ptep,unsigned long sz);#define __HAVE_ARCH_HUGE_PTEP_SET_WRPROTECTextern void huge_ptep_set_wrprotect (struct mm_struct *mm,unsigned long addr,pte_t *ptep);arch/arm64/mm/hugetlbpage.c +5
-3@@ -396,8 +396,8 @@ void huge_pte_clear(struct mm_struct *mm, unsigned long addr,__pte_clear (mm, addr, ptep); } -pte_t huge_ptep_get_and_clear (struct mm_struct *mm, -unsigned long addr,pte_t *ptep) +pte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr, +pte_t *ptep,unsigned long sz) {int ncontig;size_t pgsize;@@ -549,6 +549,8 @@ bool __init arch_hugetlb_valid_size(unsigned long size)pte_t huge_ptep_modify_prot_start (struct vm_area_struct *vma,unsigned long addr,pte_t *ptep) { +unsigned long psize =huge_page_size (hstate_vma (vma)); +if (alternative_has_cap_unlikely (ARM64_WORKAROUND_2645198 )) {/* * Break-before-make (BBM) is required for all user space mappings @@ -558,7 +560,7 @@ pte_t huge_ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addrif (pte_user_exec (__ptep_get (ptep)))return huge_ptep_clear_flush (vma, addr, ptep); } -return huge_ptep_get_and_clear (vma->vm_mm, addr, ptep); +return huge_ptep_get_and_clear (vma->vm_mm, addr, ptep, psize); } void huge_ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep,arch/loongarch/include/asm/hugetlb.h +4
-2@@ -36,7 +36,8 @@ static inline void huge_pte_clear(struct mm_struct *mm, unsigned long addr,#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEARstatic inline pte_t huge_ptep_get_and_clear (struct mm_struct *mm, -unsigned long addr,pte_t *ptep) +unsigned long addr,pte_t *ptep, +unsigned long sz) {pte_t clear;pte_t pte =ptep_get (ptep);@@ -51,8 +52,9 @@ static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep) { pte_t pte; +unsigned long sz =huge_page_size (hstate_vma (vma)); - pte =huge_ptep_get_and_clear (vma->vm_mm, addr, ptep); + pte =huge_ptep_get_and_clear (vma->vm_mm, addr, ptep, sz);flush_tlb_page (vma, addr);return pte; }arch/mips/include/asm/hugetlb.h +4
-2@@ -27,7 +27,8 @@ static inline int prepare_hugepage_range(struct file *file,#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEARstatic inline pte_t huge_ptep_get_and_clear (struct mm_struct *mm, -unsigned long addr,pte_t *ptep) +unsigned long addr,pte_t *ptep, +unsigned long sz) {pte_t clear;pte_t pte = *ptep;@@ -42,13 +43,14 @@ static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep) { pte_t pte; +unsigned long sz =huge_page_size (hstate_vma (vma));/* * clear the huge pte entry firstly, so that the other smp threads will * not get old pte entry after finishing flush_tlb_page and before * setting new huge pte entry */ - pte =huge_ptep_get_and_clear (vma->vm_mm, addr, ptep); + pte =huge_ptep_get_and_clear (vma->vm_mm, addr, ptep, sz);flush_tlb_page (vma, addr);return pte; }arch/parisc/include/asm/hugetlb.h +1
-1@@ -10,7 +10,7 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEARpte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr, -pte_t *ptep); +pte_t *ptep,unsigned long sz);#define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,arch/parisc/mm/hugetlbpage.c +1
-1@@ -126,7 +126,7 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,pte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr, -pte_t *ptep) +pte_t *ptep,unsigned long sz) {pte_t entry;arch/powerpc/include/asm/hugetlb.h +4
-2@@ -45,7 +45,8 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep,#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEARstatic inline pte_t huge_ptep_get_and_clear (struct mm_struct *mm, -unsigned long addr,pte_t *ptep) +unsigned long addr,pte_t *ptep, +unsigned long sz) {return __pte (pte_update (mm, addr, ptep, ~0UL ,0 ,1 )); }@@ -55,8 +56,9 @@ static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep) { pte_t pte; +unsigned long sz =huge_page_size (hstate_vma (vma)); - pte =huge_ptep_get_and_clear (vma->vm_mm, addr, ptep); + pte =huge_ptep_get_and_clear (vma->vm_mm, addr, ptep, sz);flush_hugetlb_page (vma, addr);return pte; }arch/riscv/include/asm/hugetlb.h +2
-1@@ -28,7 +28,8 @@ void set_huge_pte_at(struct mm_struct *mm,#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEARpte_t huge_ptep_get_and_clear (struct mm_struct *mm, -unsigned long addr,pte_t *ptep); +unsigned long addr,pte_t *ptep, +unsigned long sz);#define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,arch/riscv/mm/hugetlbpage.c +1
-1@@ -293,7 +293,7 @@ int huge_ptep_set_access_flags(struct vm_area_struct *vma,pte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr, -pte_t *ptep) +pte_t *ptep,unsigned long sz) {pte_t orig_pte =ptep_get (ptep);int pte_num;arch/s390/include/asm/hugetlb.h +8
-4@@ -26,7 +26,11 @@ void __set_huge_pte_at(struct mm_struct *mm, unsigned long addr,pte_t huge_ptep_get (struct mm_struct *mm,unsigned long addr,pte_t *ptep);#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEAR -pte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr,pte_t *ptep); +pte_t huge_ptep_get_and_clear (struct mm_struct *mm, +unsigned long addr,pte_t *ptep, +unsigned long sz); +pte_t __huge_ptep_get_and_clear (struct mm_struct *mm, +unsigned long addr,pte_t *ptep);static inline void arch_clear_hugetlb_flags (struct folio *folio) {@@ -48,7 +52,7 @@ static inline void huge_pte_clear(struct mm_struct *mm, unsigned long addr,static inline pte_t huge_ptep_clear_flush (struct vm_area_struct *vma,unsigned long address,pte_t *ptep) { -return huge_ptep_get_and_clear (vma->vm_mm, address, ptep); +return __huge_ptep_get_and_clear (vma->vm_mm, address, ptep); }#define __HAVE_ARCH_HUGE_PTEP_SET_ACCESS_FLAGS@@ -59,7 +63,7 @@ static inline int huge_ptep_set_access_flags(struct vm_area_struct *vma,int changed = !pte_same (huge_ptep_get (vma->vm_mm, addr, ptep), pte);if (changed) { -huge_ptep_get_and_clear (vma->vm_mm, addr, ptep); +__huge_ptep_get_and_clear (vma->vm_mm, addr, ptep);__set_huge_pte_at (vma->vm_mm, addr, ptep, pte); }return changed;@@ -69,7 +73,7 @@ static inline int huge_ptep_set_access_flags(struct vm_area_struct *vma,static inline void huge_ptep_set_wrprotect (struct mm_struct *mm,unsigned long addr,pte_t *ptep) { -pte_t pte =huge_ptep_get_and_clear (mm, addr, ptep); +pte_t pte =__huge_ptep_get_and_clear (mm, addr, ptep);__set_huge_pte_at (mm, addr, ptep,pte_wrprotect (pte)); }arch/s390/mm/hugetlbpage.c +8
-2@@ -188,8 +188,8 @@ pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)return __rste_to_pte (pte_val (*ptep)); } -pte_t huge_ptep_get_and_clear (struct mm_struct *mm, -unsigned long addr,pte_t *ptep) +pte_t __huge_ptep_get_and_clear (struct mm_struct *mm, +unsigned long addr,pte_t *ptep) {pte_t pte =huge_ptep_get (mm, addr, ptep);pmd_t *pmdp = (pmd_t *) ptep;@@ -202,6 +202,12 @@ pte_t huge_ptep_get_and_clear(struct mm_struct *mm,return pte; } +pte_t huge_ptep_get_and_clear (struct mm_struct *mm, +unsigned long addr,pte_t *ptep,unsigned long sz) +{ +return __huge_ptep_get_and_clear (mm, addr, ptep); +} +pte_t *huge_pte_alloc (struct mm_struct *mm,struct vm_area_struct *vma,unsigned long addr,unsigned long sz) {arch/sparc/include/asm/hugetlb.h +1
-1@@ -20,7 +20,7 @@ void __set_huge_pte_at(struct mm_struct *mm, unsigned long addr,#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEARpte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr, -pte_t *ptep); +pte_t *ptep,unsigned long sz);#define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,arch/sparc/mm/hugetlbpage.c +1
-1@@ -260,7 +260,7 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, }pte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr, -pte_t *ptep) +pte_t *ptep,unsigned long sz) {unsigned int i, nptes, orig_shift, shift;unsigned long size;include/asm-generic/hugetlb.h +1
-1@@ -90,7 +90,7 @@ static inline void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,#ifndef __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEARstatic inline pte_t huge_ptep_get_and_clear (struct mm_struct *mm, -unsigned long addr,pte_t *ptep) +unsigned long addr,pte_t *ptep,unsigned long sz) {return ptep_get_and_clear (mm, addr, ptep); }include/linux/hugetlb.h +3
-1@@ -1004,7 +1004,9 @@ static inline void hugetlb_count_sub(long l, struct mm_struct *mm)static inline pte_t huge_ptep_modify_prot_start (struct vm_area_struct *vma,unsigned long addr,pte_t *ptep) { -return huge_ptep_get_and_clear (vma->vm_mm, addr, ptep); +unsigned long psize =huge_page_size (hstate_vma (vma)); + +return huge_ptep_get_and_clear (vma->vm_mm, addr, ptep, psize); }#endif mm/hugetlb.c +2
-2@@ -5447,7 +5447,7 @@ static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr,if (src_ptl != dst_ptl)spin_lock_nested (src_ptl,SINGLE_DEPTH_NESTING ); - pte =huge_ptep_get_and_clear (mm, old_addr, src_pte); + pte =huge_ptep_get_and_clear (mm, old_addr, src_pte, sz);if (need_clear_uffd_wp &&pte_marker_uffd_wp (pte))huge_pte_clear (mm, new_addr, dst_pte, sz);@@ -5622,7 +5622,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,set_vma_resv_flags (vma,HPAGE_RESV_UNMAPPED ); } - pte =huge_ptep_get_and_clear (mm, address, ptep); + pte =huge_ptep_get_and_clear (mm, address, ptep, sz);tlb_remove_huge_tlb_entry (h, tlb, ptep, address);if (huge_pte_dirty (pte))set_page_dirty (page);
Re: [PATCH v2 2/4] arm64: hugetlb: Fix huge_ptep_get_and_clear() for non-present ptes
In reply to Ryan RobertsQuoted 63 lines · Ryan Roberts
arm64 supports multiple
huge_ptesizes. Some of the sizes are covered by a single pte entry at a particular level (PMD_SIZE,PUD_SIZE), and some are covered by multiple ptes at a particular level (CONT_PTE_SIZE,CONT_PMD_SIZE). So the function has to figure out the size from thehuge_ptepointer. This was previously done by walking the pgtable to determine the level and by using thePTE_CONTbit to determine the number of ptes at the level.But the
PTE_CONTbit is only valid when the pte is present. For non-present pte values (e.g. markers, migration entries), the previous implementation was therefore erroniously determining the size. There is at least one known caller in core-mm,move_huge_pte(), which may callhuge_ptep_get_and_clear()for a non-present pte. So we must be robust to this case. Additionally the "regular"ptep_get_and_clear()is robust to being called for non-present ptes so it makes sense to follow the behaviour.Fix this by using the new sz parameter which is now provided to the function. Additionally when clearing each pte in a contig range, don't gather the access and dirty bits if the pte is not present.
An alternative approach that would not require API changes would be to store the
PTE_CONTbit in a spare bit in the swap entry pte for the non-present case. But it felt cleaner to follow other APIs' lead and just pass in the size.As an aside,
PTE_CONTis bit 52, which corresponds to bit 40 in the swap entry offset field (layout of non-present pte). Since hugetlb is never swapped to disk, this field will only be populated for markers, which always set this bit to 0 and hwpoison swap entries, which set the offset field to a PFN; So it would only ever be 1 for a 52-bit PVA system where memory in that high half was poisoned (I think!). So in practice, this bit would almost always be zero for non-present ptes and we would only clear the first entry if it was actually a contiguous block. That's probably a less severe symptom than if it was always interpretted as 1 and cleared out potentially-present neighboring PTEs.Fixes 66b3923a1a0f ("arm64: hugetlb: add support for PTE contiguous bit")Signed-off-by Ryan Roberts <[email protected]>--- arch/arm64/mm/hugetlbpage.c | 40 ++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 23 deletions(-)arch/arm64/mm/hugetlbpage.c +0
-11@@ -163,24 +163,23 @@ static pte_t get_clear_contig(struct mm_struct *mm,unsigned long pgsize, unsigned long ncontig) { -pte_t orig_pte =__ptep_get (ptep); -unsigned long i; - -for (i =0 ; i < ncontig; i++, addr += pgsize, ptep++) { -pte_t pte =__ptep_get_and_clear (mm, addr, ptep); - -/* -* If HW_AFDBM is enabled, then the HW could turn on -* the dirty or accessed bit for any page in the set, -* so check them all. -*/
arch/arm64/mm/hugetlbpage.c static pte_t get_clear_contig(struct mm_struct *mm, · line 170
Quoted 10 lines
arch/arm64/mm/hugetlbpage.c +5
-5-if (pte_dirty (pte)) - orig_pte =pte_mkdirty (orig_pte); - -if (pte_young (pte)) - orig_pte =pte_mkyoung (orig_pte); +pte_t pte, tmp_pte; +bool present; + + pte =__ptep_get_and_clear (mm, addr, ptep); + present =pte_present (pte);
pte_present() may not be evaluated for standard huge pages at [PMD|PUD]_SIZE e.g when ncontig = 1 in the argument.
arch/arm64/mm/hugetlbpage.c static pte_t get_clear_contig(struct mm_struct *mm, · line 171
Quoted 1 line
arch/arm64/mm/hugetlbpage.c +1
-0+while (--ncontig) {
Should this be converted into a for loop instead just to be in sync with other similar iterators in this file.
for (i = 1; i < ncontig; i++, addr += pgsize, ptep++)
{
tmp_pte = __ptep_get_and_clear(mm, addr, ptep);
if (present) {
if (pte_dirty(tmp_pte))
pte = pte_mkdirty(pte);
if (pte_young(tmp_pte))
pte = pte_mkyoung(pte);
}
}Quoted 30 lines
arch/arm64/mm/hugetlbpage.c +11
-7+ ptep++; + addr += pgsize; + tmp_pte =__ptep_get_and_clear (mm, addr, ptep); +if (present) { +if (pte_dirty (tmp_pte)) + pte =pte_mkdirty (pte); +if (pte_young (tmp_pte)) + pte =pte_mkyoung (pte); + } } -return orig_pte; +return pte; } static pte_t get_clear_contig_flush(struct mm_struct *mm,@@ -401,13 +400,8 @@ pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, {int ncontig;size_t pgsize; -pte_t orig_pte =__ptep_get (ptep); - -if (!pte_cont (orig_pte)) -return __ptep_get_and_clear (mm, addr, ptep); - - ncontig =find_num_contig (mm, addr, ptep, &pgsize); + ncontig =num_contig_ptes (sz, &pgsize);return get_clear_contig (mm, addr, ptep, pgsize, ncontig); }
Re: [PATCH v2 3/4] arm64: hugetlb: Fix flush_hugetlb_tlb_range() invalidation level
In reply to Ryan RobertsQuoted 7 lines · Ryan Roberts
commit
c910f2b65518("arm64/mm: Update tlb invalidation routines forFEAT_LPA2") changed the "invalidation level unknown" hint from 0 toTLBI_TTL_UNKNOWN(INT_MAX). But the fallback "unknown level" path inflush_hugetlb_tlb_range()was not updated. So as it stands, when trying to invalidateCONT_PMD_SIZEorCONT_PTE_SIZEhugetlb mappings, we will spuriously try to invalidate at level 0 on LPA2-enabled systems.
Quoted 7 lines
Fix this so that the fallback passes
TLBI_TTL_UNKNOWN, and while we are at it, explicitly use the correct stride and level forCONT_PMD_SIZEandCONT_PTE_SIZE, which should provide a minor optimization.Fixes c910f2b65518 ("arm64/mm: Update tlb invalidation routines for FEAT_LPA2")Signed-off-by Ryan Roberts <[email protected]>
LGTM
Quoted 37 lines
--- arch/arm64/include/asm/hugetlb.h | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)arch/arm64/include/asm/hugetlb.h +16
-6@@ -76,12 +76,22 @@ static inline void flush_hugetlb_tlb_range(struct vm_area_struct *vma, {unsigned long stride =huge_page_size (hstate_vma (vma)); -if (stride ==PMD_SIZE ) -__flush_tlb_range (vma, start, end, stride, false,2 ); -else if (stride ==PUD_SIZE ) -__flush_tlb_range (vma, start, end, stride, false,1 ); -else -__flush_tlb_range (vma, start, end,PAGE_SIZE , false,0 ); +switch (stride) { +#ifndef __PAGETABLE_PMD_FOLDED +case PUD_SIZE : +__flush_tlb_range (vma, start, end,PUD_SIZE , false,1 ); +break ; +#endif +case CONT_PMD_SIZE : +case PMD_SIZE : +__flush_tlb_range (vma, start, end,PMD_SIZE , false,2 ); +break ; +case CONT_PTE_SIZE : +__flush_tlb_range (vma, start, end,PAGE_SIZE , false,3 ); +break ; +default : +__flush_tlb_range (vma, start, end,PAGE_SIZE , false,TLBI_TTL_UNKNOWN ); + } }#endif /* __ASM_HUGETLB_H */
Re: [PATCH v2 2/4] arm64: hugetlb: Fix huge_ptep_get_and_clear() for non-present ptes
In reply to Anshuman KhandualQuoted 71 lines · Anshuman Khandual
Quoted 68 lines · Ryan Roberts
arm64 supports multiple
huge_ptesizes. Some of the sizes are covered by a single pte entry at a particular level (PMD_SIZE,PUD_SIZE), and some are covered by multiple ptes at a particular level (CONT_PTE_SIZE,CONT_PMD_SIZE). So the function has to figure out the size from thehuge_ptepointer. This was previously done by walking the pgtable to determine the level and by using thePTE_CONTbit to determine the number of ptes at the level.But the
PTE_CONTbit is only valid when the pte is present. For non-present pte values (e.g. markers, migration entries), the previous implementation was therefore erroniously determining the size. There is at least one known caller in core-mm,move_huge_pte(), which may callhuge_ptep_get_and_clear()for a non-present pte. So we must be robust to this case. Additionally the "regular"ptep_get_and_clear()is robust to being called for non-present ptes so it makes sense to follow the behaviour.Fix this by using the new sz parameter which is now provided to the function. Additionally when clearing each pte in a contig range, don't gather the access and dirty bits if the pte is not present.
An alternative approach that would not require API changes would be to store the
PTE_CONTbit in a spare bit in the swap entry pte for the non-present case. But it felt cleaner to follow other APIs' lead and just pass in the size.As an aside,
PTE_CONTis bit 52, which corresponds to bit 40 in the swap entry offset field (layout of non-present pte). Since hugetlb is never swapped to disk, this field will only be populated for markers, which always set this bit to 0 and hwpoison swap entries, which set the offset field to a PFN; So it would only ever be 1 for a 52-bit PVA system where memory in that high half was poisoned (I think!). So in practice, this bit would almost always be zero for non-present ptes and we would only clear the first entry if it was actually a contiguous block. That's probably a less severe symptom than if it was always interpretted as 1 and cleared out potentially-present neighboring PTEs.Fixes 66b3923a1a0f ("arm64: hugetlb: add support for PTE contiguous bit")Signed-off-by Ryan Roberts <[email protected]>--- arch/arm64/mm/hugetlbpage.c | 40 ++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 23 deletions(-)arch/arm64/mm/hugetlbpage.c +0
-16@@ -163,24 +163,23 @@ static pte_t get_clear_contig(struct mm_struct *mm,unsigned long pgsize, unsigned long ncontig) { -pte_t orig_pte =__ptep_get (ptep); -unsigned long i; - -for (i =0 ; i < ncontig; i++, addr += pgsize, ptep++) { -pte_t pte =__ptep_get_and_clear (mm, addr, ptep); - -/* -* If HW_AFDBM is enabled, then the HW could turn on -* the dirty or accessed bit for any page in the set, -* so check them all. -*/ -if (pte_dirty (pte)) - orig_pte =pte_mkdirty (orig_pte); - -if (pte_young (pte)) - orig_pte =pte_mkyoung (orig_pte);
Quoted 8 lines
arch/arm64/mm/hugetlbpage.c static pte_t get_clear_contig(struct mm_struct *mm, · line 170
Quoted 5 lines
arch/arm64/mm/hugetlbpage.c +5
-0+pte_t pte, tmp_pte; +bool present; + + pte =__ptep_get_and_clear (mm, addr, ptep); + present =pte_present (pte);
pte_present()may not be evaluated for standard huge pages at [PMD|PUD]_SIZE e.g when ncontig = 1 in the argument.
Sorry I'm not quite sure what you're suggesting here? Are you proposing that pte_present() should be moved into the loop so that we only actually call it when we are going to consume it? I'm happy to do that if it's the preference, but I thought it was neater to hoist it out of the loop.
Quoted 7 lines
arch/arm64/mm/hugetlbpage.c static pte_t get_clear_contig(struct mm_struct *mm, · line 171
Quoted 2 lines
arch/arm64/mm/hugetlbpage.c +1
-0+while (--ncontig) {Should this be converted into a for loop instead just to be in sync with other similar iterators in this file.
for (i = 1; i < ncontig; i++, addr += pgsize, ptep++)
Quoted 9 lines
{
tmp_pte = __ptep_get_and_clear(mm, addr, ptep);
if (present) {
if (pte_dirty(tmp_pte))
pte = pte_mkdirty(pte);
if (pte_young(tmp_pte))
pte = pte_mkyoung(pte);
}
}I think the way you have written this it's incorrect. Let's say we have 16 ptes in the block. We want to iterate over the last 15 of them (we have already read pte 0). But you're iterating over the first 15 because you don't increment addr and ptep until after you've been around the loop the first time. So we would need to explicitly increment those 2 before entering the loop. But that is only neccessary if ncontig > 1. Personally I think my approach is neater...
Quoted 31 lines
Quoted 31 lines
arch/arm64/mm/hugetlbpage.c +11
-7+ ptep++; + addr += pgsize; + tmp_pte =__ptep_get_and_clear (mm, addr, ptep); +if (present) { +if (pte_dirty (tmp_pte)) + pte =pte_mkdirty (pte); +if (pte_young (tmp_pte)) + pte =pte_mkyoung (pte); + } } -return orig_pte; +return pte; } static pte_t get_clear_contig_flush(struct mm_struct *mm,@@ -401,13 +400,8 @@ pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, {int ncontig;size_t pgsize; -pte_t orig_pte =__ptep_get (ptep); - -if (!pte_cont (orig_pte)) -return __ptep_get_and_clear (mm, addr, ptep); - - ncontig =find_num_contig (mm, addr, ptep, &pgsize); + ncontig =num_contig_ptes (sz, &pgsize);return get_clear_contig (mm, addr, ptep, pgsize, ncontig); }
Re: [PATCH v2 1/4] mm: hugetlb: Add huge page size param to huge_ptep_get_and_clear()
In reply to Ryan RobertsQuoted 6 lines · Ryan Roberts
In order to fix a bug, arm64 needs to be told the size of the huge page for which the
huge_pteis being set inhuge_ptep_get_and_clear(). Provide for this by adding anunsigned long szparameter to the function. This follows the same pattern ashuge_pte_clear()andset_huge_pte_at().
Quoted 8 lines
This commit makes the required interface modifications to the core mm as well as all arches that implement this function (arm64, loongarch, mips, parisc, powerpc, riscv, s390, sparc). The actual arm64 bug will be fixed in a separate commit.
Fixes 66b3923a1a0f ("arm64: hugetlb: add support for PTE contiguous bit")Signed-off-by Ryan Roberts <[email protected]>
Re: [PATCH v2 2/4] arm64: hugetlb: Fix huge_ptep_get_and_clear() for non-present ptes
In reply to Ryan RobertsQuoted 20 lines · Ryan Roberts
arch/arm64/mm/hugetlbpage.c +0
-12@@ -163,24 +163,23 @@ static pte_t get_clear_contig(struct mm_struct *mm,unsigned long pgsize, unsigned long ncontig) { -pte_t orig_pte =__ptep_get (ptep); -unsigned long i; - -for (i =0 ; i < ncontig; i++, addr += pgsize, ptep++) { -pte_t pte =__ptep_get_and_clear (mm, addr, ptep); - -/* -* If HW_AFDBM is enabled, then the HW could turn on -* the dirty or accessed bit for any page in the set, -* so check them all. -*/ -if (pte_dirty (pte))
arch/arm64/mm/hugetlbpage.c static pte_t get_clear_contig(struct mm_struct *mm, · line 171
Quoted 10 lines
arch/arm64/mm/hugetlbpage.c +6
-4- orig_pte =pte_mkdirty (orig_pte); - -if (pte_young (pte)) - orig_pte =pte_mkyoung (orig_pte); +pte_t pte, tmp_pte; +bool present; + + pte =__ptep_get_and_clear (mm, addr, ptep); + present =pte_present (pte); +while (--ncontig) {
A 'for' loop may be more consistent with the rest of the file but I really don't mind the 'while' loop.
Re: [PATCH v2 3/4] arm64: hugetlb: Fix flush_hugetlb_tlb_range() invalidation level
In reply to Ryan RobertsQuoted 7 lines · Ryan Roberts
commit
c910f2b65518("arm64/mm: Update tlb invalidation routines forFEAT_LPA2") changed the "invalidation level unknown" hint from 0 toTLBI_TTL_UNKNOWN(INT_MAX). But the fallback "unknown level" path inflush_hugetlb_tlb_range()was not updated. So as it stands, when trying to invalidateCONT_PMD_SIZEorCONT_PTE_SIZEhugetlb mappings, we will spuriously try to invalidate at level 0 on LPA2-enabled systems.
Quoted 7 lines
Fix this so that the fallback passes
TLBI_TTL_UNKNOWN, and while we are at it, explicitly use the correct stride and level forCONT_PMD_SIZEandCONT_PTE_SIZE, which should provide a minor optimization.Fixes c910f2b65518 ("arm64/mm: Update tlb invalidation routines for FEAT_LPA2")Signed-off-by Ryan Roberts <[email protected]>
Re: [PATCH v2 4/4] mm: Don't skip arch_sync_kernel_mappings() in error paths
In reply to Ryan RobertsQuoted 5 lines · Ryan Roberts
Fix callers that previously skipped calling
arch_sync_kernel_mappings()if an error occurred during a pgtable update. The call is still required to sync any pgtable updates that may have occurred prior to hitting the error condition.
Quoted 7 lines
These are theoretical bugs discovered during code review.
Fixes 2ba3e6947aed ("mm/vmalloc: track which page-table levels were modified")Fixes 0c95cba49255 ("mm: apply_to_pte_range warn and fail if a large pte is encountered")Reviewed-by Anshuman Khandual <[email protected]>Signed-off-by Ryan Roberts <[email protected]>
Catalin Marinas
maintainerWill, Ryan,
Quoted 8 lines · Ryan Roberts
This series contains some fixes for hugetlb on arm64, and is split out from v1 of a wider series at [1]. While the last patch is technically targetting core-mm and is not directly related to arm64, I'd like to to go via the arm64 tree so that the wider performance improvement series (v2 to be posted shortly) that depends on this series doesn't have to be robust to the fix not being present.
I've included maintainers/reviewers for all the arches that are (trivially) touched due to the API changes, hoping for some ACKs.
These fixes look fine to me and I think we should get them in for 6.14. I think Andrew was ok with them going in via the arm64 tree:
https://lore.kernel.org/linux-arm-kernel/[email protected]/
Signature · 1 line
Catalin
Re: [PATCH v2 2/4] arm64: hugetlb: Fix huge_ptep_get_and_clear() for non-present ptes
In reply to Ryan RobertsQuoted 12 lines · Ryan Roberts
Quoted 11 lines · Anshuman Khandual
Quoted 8 lines · Ryan Roberts
arm64 supports multiple
huge_ptesizes. Some of the sizes are covered by a single pte entry at a particular level (PMD_SIZE,PUD_SIZE), and some are covered by multiple ptes at a particular level (CONT_PTE_SIZE,CONT_PMD_SIZE). So the function has to figure out the size from thehuge_ptepointer. This was previously done by walking the pgtable to determine the level and by using thePTE_CONTbit to determine the number of ptes at the level.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
But the
PTE_CONTbit is only valid when the pte is present. For non-present pte values (e.g. markers, migration entries), the previous implementation was therefore erroniously determining the size. There is
typo - s/erroniously/erroneously ^^^^^^
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
at least one known caller in core-mm,
move_huge_pte(), which may callhuge_ptep_get_and_clear()for a non-present pte. So we must be robust to this case. Additionally the "regular"ptep_get_and_clear()is robust to being called for non-present ptes so it makes sense to follow the behaviour.Fix this by using the new sz parameter which is now provided to the function. Additionally when clearing each pte in a contig range, don't gather the access and dirty bits if the pte is not present.
An alternative approach that would not require API changes would be to store the
PTE_CONTbit in a spare bit in the swap entry pte for the non-present case. But it felt cleaner to follow other APIs' lead and just pass in the size.
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
As an aside,
PTE_CONTis bit 52, which corresponds to bit 40 in the swap entry offset field (layout of non-present pte). Since hugetlb is never swapped to disk, this field will only be populated for markers, which always set this bit to 0 and hwpoison swap entries, which set the offset field to a PFN; So it would only ever be 1 for a 52-bit PVA system where memory in that high half was poisoned (I think!). So in practice, this bit would almost always be zero for non-present ptes and we would only clear the first entry if it was actually a contiguous block. That's probably a less severe symptom than if it was always interpretted as 1
typo - s/interpretted/interpreted ^^^^^^
Quoted 42 lines
Quoted 33 lines
Quoted 33 lines
and cleared out potentially-present neighboring PTEs.
Fixes 66b3923a1a0f ("arm64: hugetlb: add support for PTE contiguous bit")Signed-off-by Ryan Roberts <[email protected]>--- arch/arm64/mm/hugetlbpage.c | 40 ++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 23 deletions(-)arch/arm64/mm/hugetlbpage.c +0
-16@@ -163,24 +163,23 @@ static pte_t get_clear_contig(struct mm_struct *mm,unsigned long pgsize, unsigned long ncontig) { -pte_t orig_pte =__ptep_get (ptep); -unsigned long i; - -for (i =0 ; i < ncontig; i++, addr += pgsize, ptep++) { -pte_t pte =__ptep_get_and_clear (mm, addr, ptep); - -/* -* If HW_AFDBM is enabled, then the HW could turn on -* the dirty or accessed bit for any page in the set, -* so check them all. -*/ -if (pte_dirty (pte)) - orig_pte =pte_mkdirty (orig_pte); - -if (pte_young (pte)) - orig_pte =pte_mkyoung (orig_pte);Quoted 8 lines
arch/arm64/mm/hugetlbpage.c static pte_t get_clear_contig(struct mm_struct *mm, · line 170
Quoted 5 lines
arch/arm64/mm/hugetlbpage.c +5
-0+pte_t pte, tmp_pte; +bool present; + + pte =__ptep_get_and_clear (mm, addr, ptep); + present =pte_present (pte);
pte_present()may not be evaluated for standard huge pages at [PMD|PUD]_SIZE e.g when ncontig = 1 in the argument.
Quoted 3 lines
Sorry I'm not quite sure what you're suggesting here? Are you proposing that
pte_present()should be moved into the loop so that we only actually call it when we are going to consume it? I'm happy to do that if it's the preference,
Right, pte_present() is only required for the cont huge pages but not for the normal huge pages.
Quoted 1 line
but I thought it was neater to hoist it out of the loop.
Agreed, but when possible pte_present() cost should be avoided for the normal huge pages where it is not required.
Quoted 18 lines
Quoted 8 lines
arch/arm64/mm/hugetlbpage.c static pte_t get_clear_contig(struct mm_struct *mm, · line 171
Quoted 3 lines
arch/arm64/mm/hugetlbpage.c +1
-0+while (--ncontig) {Should this be converted into a for loop instead just to be in sync with other similar iterators in this file.
for (i = 1; i < ncontig; i++, addr += pgsize, ptep++)
Quoted 9 lines
{ tmp_pte = __ptep_get_and_clear(mm, addr, ptep); if (present) { if (pte_dirty(tmp_pte)) pte = pte_mkdirty(pte); if (pte_young(tmp_pte)) pte = pte_mkyoung(pte); } }
Quoted 6 lines
I think the way you have written this it's incorrect. Let's say we have 16 ptes in the block. We want to iterate over the last 15 of them (we have already read pte 0). But you're iterating over the first 15 because you don't increment addr and ptep until after you've been around the loop the first time. So we would need to explicitly increment those 2 before entering the loop. But that is only neccessary if ncontig > 1. Personally I think my approach is neater...
Thinking about this again. Just wondering should not a pte_present() check on each entries being cleared along with (ncontig > 1) in this existing loop before transferring over the dirty and accessed bits - also work as intended with less code churn ?
static pte_t get_clear_contig(struct mm_struct *mm,
unsigned long addr, pte_t *ptep, unsigned long pgsize, unsigned long ncontig)
{
pte_t orig_pte = __ptep_get(ptep);
unsigned long i;
for (i = 0; i < ncontig; i++, addr += pgsize, ptep++) {
pte_t pte = __ptep_get_and_clear(mm, addr, ptep);
if (ncontig > 1 && !pte_present(pte))
continue;
/*
* If HW_AFDBM is enabled, then the HW could turn on
* the dirty or accessed bit for any page in the set,
* so check them all.
*/
if (pte_dirty(pte))
orig_pte = pte_mkdirty(orig_pte);
if (pte_young(pte))
orig_pte = pte_mkyoung(orig_pte);
}
return orig_pte;
}* Normal huge pages
- enters the for loop just once
- clears the single entry
- always transfers dirty and access bits
- pte_present() does not matter as ncontig = 1
* Contig huge pages
- enters the for loop ncontig times - for each sub page
- clears all sub page entries
- transfers dirty and access bits only when pte_present()
- pte_present() is relevant as ncontig > 1
Quoted 33 lines
Quoted 32 lines
Quoted 32 lines
arch/arm64/mm/hugetlbpage.c +11
-7+ ptep++; + addr += pgsize; + tmp_pte =__ptep_get_and_clear (mm, addr, ptep); +if (present) { +if (pte_dirty (tmp_pte)) + pte =pte_mkdirty (pte); +if (pte_young (tmp_pte)) + pte =pte_mkyoung (pte); + } } -return orig_pte; +return pte; } static pte_t get_clear_contig_flush(struct mm_struct *mm,@@ -401,13 +400,8 @@ pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, {int ncontig;size_t pgsize; -pte_t orig_pte =__ptep_get (ptep); - -if (!pte_cont (orig_pte)) -return __ptep_get_and_clear (mm, addr, ptep); - - ncontig =find_num_contig (mm, addr, ptep, &pgsize); + ncontig =num_contig_ptes (sz, &pgsize);return get_clear_contig (mm, addr, ptep, pgsize, ncontig); }
Re: [PATCH v2 1/4] mm: hugetlb: Add huge page size param to huge_ptep_get_and_clear()
In reply to Ryan RobertsQuoted 2 lines · Ryan Roberts
In order to fix a bug, arm64 needs to be told the size of the huge page for which the
huge_pteis being set inhuge_ptep_get_and_clear().
s/set/cleared/ ?
Quoted 9 lines
Provide for this by adding an
unsigned long szparameter to the function. This follows the same pattern ashuge_pte_clear()andset_huge_pte_at().This commit makes the required interface modifications to the core mm as well as all arches that implement this function (arm64, loongarch, mips, parisc, powerpc, riscv, s390, sparc). The actual arm64 bug will be fixed in a separate commit.
Quoted 4 lines
Fixes 66b3923a1a0f ("arm64: hugetlb: add support for PTE contiguous bit")Signed-off-by Ryan Roberts <[email protected]>---
Signature · 3 lines
Cheers, David / dhildenb
Catalin Marinas
maintainerRe: [PATCH v2 2/4] arm64: hugetlb: Fix huge_ptep_get_and_clear() for non-present ptes
In reply to Anshuman KhandualQuoted 26 lines · Anshuman Khandual
Quoted 18 lines · Ryan Roberts
Quoted 7 lines · Anshuman Khandual
arch/arm64/mm/hugetlbpage.c static pte_t get_clear_contig(struct mm_struct *mm, · line 171
Quoted 1 line · Ryan Roberts
arch/arm64/mm/hugetlbpage.c +1
-0+while (--ncontig) {Should this be converted into a for loop instead just to be in sync with other similar iterators in this file.
for (i = 1; i < ncontig; i++, addr += pgsize, ptep++)
Quoted 9 lines
{ tmp_pte = __ptep_get_and_clear(mm, addr, ptep); if (present) { if (pte_dirty(tmp_pte)) pte = pte_mkdirty(pte); if (pte_young(tmp_pte)) pte = pte_mkyoung(pte); } }Quoted 6 lines
I think the way you have written this it's incorrect. Let's say we have 16 ptes in the block. We want to iterate over the last 15 of them (we have already read pte 0). But you're iterating over the first 15 because you don't increment addr and ptep until after you've been around the loop the first time. So we would need to explicitly increment those 2 before entering the loop. But that is only neccessary if ncontig > 1. Personally I think my approach is neater...
Quoted 4 lines
Thinking about this again. Just wondering should not a
pte_present()check on each entries being cleared along with (ncontig > 1) in this existing loop before transferring over the dirty and accessed bits - also work as intended with less code churn ?
Shouldn't all the ptes in a contig block be either all present or all not-present? Is there any point in checking each individually?
Signature · 1 line
Catalin
Re: [PATCH v2 2/4] arm64: hugetlb: Fix huge_ptep_get_and_clear() for non-present ptes
Quoted 27 lines · Catalin Marinas
Quoted 26 lines · Anshuman Khandual
Quoted 24 lines · Ryan Roberts
Quoted 7 lines · Anshuman Khandual
arch/arm64/mm/hugetlbpage.c static pte_t get_clear_contig(struct mm_struct *mm, · line 171
Quoted 1 line · Ryan Roberts
arch/arm64/mm/hugetlbpage.c +1
-0+while (--ncontig) {Should this be converted into a for loop instead just to be in sync with other similar iterators in this file.
for (i = 1; i < ncontig; i++, addr += pgsize, ptep++)
Quoted 9 lines
{ tmp_pte = __ptep_get_and_clear(mm, addr, ptep); if (present) { if (pte_dirty(tmp_pte)) pte = pte_mkdirty(pte); if (pte_young(tmp_pte)) pte = pte_mkyoung(pte); } }I think the way you have written this it's incorrect. Let's say we have 16 ptes in the block. We want to iterate over the last 15 of them (we have already read pte 0). But you're iterating over the first 15 because you don't increment addr and ptep until after you've been around the loop the first time. So we would need to explicitly increment those 2 before entering the loop. But that is only neccessary if ncontig > 1. Personally I think my approach is neater...
Quoted 7 lines
Quoted 4 lines
Thinking about this again. Just wondering should not a
pte_present()check on each entries being cleared along with (ncontig > 1) in this existing loop before transferring over the dirty and accessed bits - also work as intended with less code churn ?Shouldn't all the ptes in a contig block be either all present or all not-present? Is there any point in checking each individually?
I agree, that's why I was just testing it once outside the loop. I'm confident my code and Anshuman's alternative are both correct. So it's just a stylistic choice really. I'd prefer to leave it as is, but will change if there is consensus. I'm not sure I'm hearing that though.
Catalin, do you want me to respin the series to fix up the typos that Anshuman highlighted, or will you handle that?
Thanks,
Ryan
Will Deacon
maintainerRe: [PATCH v2 2/4] arm64: hugetlb: Fix huge_ptep_get_and_clear() for non-present ptes
In reply to Ryan RobertsQuoted 74 lines · Ryan Roberts
arm64 supports multiple
huge_ptesizes. Some of the sizes are covered by a single pte entry at a particular level (PMD_SIZE,PUD_SIZE), and some are covered by multiple ptes at a particular level (CONT_PTE_SIZE,CONT_PMD_SIZE). So the function has to figure out the size from thehuge_ptepointer. This was previously done by walking the pgtable to determine the level and by using thePTE_CONTbit to determine the number of ptes at the level.But the
PTE_CONTbit is only valid when the pte is present. For non-present pte values (e.g. markers, migration entries), the previous implementation was therefore erroniously determining the size. There is at least one known caller in core-mm,move_huge_pte(), which may callhuge_ptep_get_and_clear()for a non-present pte. So we must be robust to this case. Additionally the "regular"ptep_get_and_clear()is robust to being called for non-present ptes so it makes sense to follow the behaviour.Fix this by using the new sz parameter which is now provided to the function. Additionally when clearing each pte in a contig range, don't gather the access and dirty bits if the pte is not present.
An alternative approach that would not require API changes would be to store the
PTE_CONTbit in a spare bit in the swap entry pte for the non-present case. But it felt cleaner to follow other APIs' lead and just pass in the size.As an aside,
PTE_CONTis bit 52, which corresponds to bit 40 in the swap entry offset field (layout of non-present pte). Since hugetlb is never swapped to disk, this field will only be populated for markers, which always set this bit to 0 and hwpoison swap entries, which set the offset field to a PFN; So it would only ever be 1 for a 52-bit PVA system where memory in that high half was poisoned (I think!). So in practice, this bit would almost always be zero for non-present ptes and we would only clear the first entry if it was actually a contiguous block. That's probably a less severe symptom than if it was always interpretted as 1 and cleared out potentially-present neighboring PTEs.Fixes 66b3923a1a0f ("arm64: hugetlb: add support for PTE contiguous bit")Signed-off-by Ryan Roberts <[email protected]>--- arch/arm64/mm/hugetlbpage.c | 40 ++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 23 deletions(-)arch/arm64/mm/hugetlbpage.c +6
-16@@ -163,24 +163,23 @@ static pte_t get_clear_contig(struct mm_struct *mm, unsigned long pgsize, unsigned long ncontig) { -pte_t orig_pte =__ptep_get (ptep); -unsigned long i; - -for (i =0 ; i < ncontig; i++, addr += pgsize, ptep++) { -pte_t pte =__ptep_get_and_clear (mm, addr, ptep); - -/* -* If HW_AFDBM is enabled, then the HW could turn on -* the dirty or accessed bit for any page in the set, -* so check them all. -*/ -if (pte_dirty (pte)) - orig_pte =pte_mkdirty (orig_pte); - -if (pte_young (pte)) - orig_pte =pte_mkyoung (orig_pte); +pte_t pte, tmp_pte; +bool present; + + pte =__ptep_get_and_clear (mm, addr, ptep); + present =pte_present (pte); +while (--ncontig) {
arch/arm64/mm/hugetlbpage.c static pte_t get_clear_contig(struct mm_struct *mm, · line 181
Quoted 10 lines
arch/arm64/mm/hugetlbpage.c +9
-0+ ptep++; + addr += pgsize; + tmp_pte =__ptep_get_and_clear (mm, addr, ptep); +if (present) { +if (pte_dirty (tmp_pte)) + pte =pte_mkdirty (pte); +if (pte_young (tmp_pte)) + pte =pte_mkyoung (pte); + } }
nit: With the loop now structured like this, we really can't handle num_contig_ptes() returning 0 if it gets an unknown size. Granted, that really shouldn't happen, but perhaps it would be better to add a 'default' case with a WARN() to num_contig_ptes() and then add an early return here?
Will
Will Deacon
maintainerQuoted 9 lines · Catalin Marinas
Will, Ryan,
Quoted 6 lines · Ryan Roberts
This series contains some fixes for hugetlb on arm64, and is split out from v1 of a wider series at [1]. While the last patch is technically targetting core-mm and is not directly related to arm64, I'd like to to go via the arm64 tree so that the wider performance improvement series (v2 to be posted shortly) that depends on this series doesn't have to be robust to the fix not being present.
Quoted 7 lines
Quoted 2 lines
I've included maintainers/reviewers for all the arches that are (trivially) touched due to the API changes, hoping for some ACKs.
These fixes look fine to me and I think we should get them in for 6.14. I think Andrew was ok with them going in via the arm64 tree:
I think the diffstat looks worse than it really is, as the arch changes are reasonably mechanical. I'd like to let it sit in next for a few days though, so I'll pick this up once we've resolved my comment on patch #2.
Ryan -- did you find all of these issues by inspection, or are you aware of anybody hitting them in practice?
Will
Alexandre Ghiti
Reviewed-byRe: [PATCH v2 1/4] mm: hugetlb: Add huge page size param to huge_ptep_get_and_clear()
In reply to David HildenbrandHi Ryan,
Quoted 19 lines · David Hildenbrand
Quoted 2 lines · Ryan Roberts
In order to fix a bug, arm64 needs to be told the size of the huge page for which the
huge_pteis being set inhuge_ptep_get_and_clear().s/set/cleared/ ?
Quoted 12 lines
Provide for this by adding an
unsigned long szparameter to the function. This follows the same pattern ashuge_pte_clear()andset_huge_pte_at().This commit makes the required interface modifications to the core mm as well as all arches that implement this function (arm64, loongarch, mips, parisc, powerpc, riscv, s390, sparc). The actual arm64 bug will be fixed in a separate commit.
Fixes 66b3923a1a0f ("arm64: hugetlb: add support for PTE contiguous bit")Signed-off-by Ryan Roberts <[email protected]>
Thanks,
Alex
Quoted 9 lines
Quoted 1 line
---
--
Cheers,David / dhildenb
Re: [PATCH v2 2/4] arm64: hugetlb: Fix huge_ptep_get_and_clear() for non-present ptes
In reply to Ryan RobertsHi Ryan,
Quoted 106 lines · Ryan Roberts
arm64 supports multiple
huge_ptesizes. Some of the sizes are covered by a single pte entry at a particular level (PMD_SIZE,PUD_SIZE), and some are covered by multiple ptes at a particular level (CONT_PTE_SIZE,CONT_PMD_SIZE). So the function has to figure out the size from thehuge_ptepointer. This was previously done by walking the pgtable to determine the level and by using thePTE_CONTbit to determine the number of ptes at the level.But the
PTE_CONTbit is only valid when the pte is present. For non-present pte values (e.g. markers, migration entries), the previous implementation was therefore erroniously determining the size. There is at least one known caller in core-mm,move_huge_pte(), which may callhuge_ptep_get_and_clear()for a non-present pte. So we must be robust to this case. Additionally the "regular"ptep_get_and_clear()is robust to being called for non-present ptes so it makes sense to follow the behaviour.Fix this by using the new sz parameter which is now provided to the function. Additionally when clearing each pte in a contig range, don't gather the access and dirty bits if the pte is not present.
An alternative approach that would not require API changes would be to store the
PTE_CONTbit in a spare bit in the swap entry pte for the non-present case. But it felt cleaner to follow other APIs' lead and just pass in the size.As an aside,
PTE_CONTis bit 52, which corresponds to bit 40 in the swap entry offset field (layout of non-present pte). Since hugetlb is never swapped to disk, this field will only be populated for markers, which always set this bit to 0 and hwpoison swap entries, which set the offset field to a PFN; So it would only ever be 1 for a 52-bit PVA system where memory in that high half was poisoned (I think!). So in practice, this bit would almost always be zero for non-present ptes and we would only clear the first entry if it was actually a contiguous block. That's probably a less severe symptom than if it was always interpretted as 1 and cleared out potentially-present neighboring PTEs.Fixes 66b3923a1a0f ("arm64: hugetlb: add support for PTE contiguous bit")Signed-off-by Ryan Roberts <[email protected]>--- arch/arm64/mm/hugetlbpage.c | 40 ++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 23 deletions(-)arch/arm64/mm/hugetlbpage.c +17
-24@@ -163,24 +163,23 @@ static pte_t get_clear_contig(struct mm_struct *mm, unsigned long pgsize, unsigned long ncontig) { -pte_t orig_pte =__ptep_get (ptep); -unsigned long i; - -for (i =0 ; i < ncontig; i++, addr += pgsize, ptep++) { -pte_t pte =__ptep_get_and_clear (mm, addr, ptep); - -/* -* If HW_AFDBM is enabled, then the HW could turn on -* the dirty or accessed bit for any page in the set, -* so check them all. -*/ -if (pte_dirty (pte)) - orig_pte =pte_mkdirty (orig_pte); - -if (pte_young (pte)) - orig_pte =pte_mkyoung (orig_pte); +pte_t pte, tmp_pte; +bool present; + + pte =__ptep_get_and_clear (mm, addr, ptep); + present =pte_present (pte); +while (--ncontig) { + ptep++; + addr += pgsize; + tmp_pte =__ptep_get_and_clear (mm, addr, ptep); +if (present) { +if (pte_dirty (tmp_pte)) + pte =pte_mkdirty (pte); +if (pte_young (tmp_pte)) + pte =pte_mkyoung (pte); + } } -return orig_pte; +return pte; } static pte_t get_clear_contig_flush(struct mm_struct *mm,@@ -401,13 +400,8 @@ pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, {int ncontig;size_t pgsize; -pte_t orig_pte =__ptep_get (ptep); - -if (!pte_cont (orig_pte)) -return __ptep_get_and_clear (mm, addr, ptep); - - ncontig =find_num_contig (mm, addr, ptep, &pgsize); + ncontig =num_contig_ptes (sz, &pgsize);return get_clear_contig (mm, addr, ptep, pgsize, ncontig); } --
Quoted 2 lines
2.43.0
Thanks for ccing me on this fix, we have the same issue (in huge_pte_clear() too) in riscv. I'll come up with a fix and wait for you guys to merge this.
That only comforts my idea to merge both riscv and arm64 implementations :)
Thanks again,
Alex
Re: [PATCH v2 2/4] arm64: hugetlb: Fix huge_ptep_get_and_clear() for non-present ptes
In reply to Will DeaconQuoted 82 lines · Will Deacon
Quoted 81 lines · Ryan Roberts
arm64 supports multiple
huge_ptesizes. Some of the sizes are covered by a single pte entry at a particular level (PMD_SIZE,PUD_SIZE), and some are covered by multiple ptes at a particular level (CONT_PTE_SIZE,CONT_PMD_SIZE). So the function has to figure out the size from thehuge_ptepointer. This was previously done by walking the pgtable to determine the level and by using thePTE_CONTbit to determine the number of ptes at the level.But the
PTE_CONTbit is only valid when the pte is present. For non-present pte values (e.g. markers, migration entries), the previous implementation was therefore erroniously determining the size. There is at least one known caller in core-mm,move_huge_pte(), which may callhuge_ptep_get_and_clear()for a non-present pte. So we must be robust to this case. Additionally the "regular"ptep_get_and_clear()is robust to being called for non-present ptes so it makes sense to follow the behaviour.Fix this by using the new sz parameter which is now provided to the function. Additionally when clearing each pte in a contig range, don't gather the access and dirty bits if the pte is not present.
An alternative approach that would not require API changes would be to store the
PTE_CONTbit in a spare bit in the swap entry pte for the non-present case. But it felt cleaner to follow other APIs' lead and just pass in the size.As an aside,
PTE_CONTis bit 52, which corresponds to bit 40 in the swap entry offset field (layout of non-present pte). Since hugetlb is never swapped to disk, this field will only be populated for markers, which always set this bit to 0 and hwpoison swap entries, which set the offset field to a PFN; So it would only ever be 1 for a 52-bit PVA system where memory in that high half was poisoned (I think!). So in practice, this bit would almost always be zero for non-present ptes and we would only clear the first entry if it was actually a contiguous block. That's probably a less severe symptom than if it was always interpretted as 1 and cleared out potentially-present neighboring PTEs.Fixes 66b3923a1a0f ("arm64: hugetlb: add support for PTE contiguous bit")Signed-off-by Ryan Roberts <[email protected]>--- arch/arm64/mm/hugetlbpage.c | 40 ++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 23 deletions(-)arch/arm64/mm/hugetlbpage.c +13
-16@@ -163,24 +163,23 @@ static pte_t get_clear_contig(struct mm_struct *mm, unsigned long pgsize, unsigned long ncontig) { -pte_t orig_pte =__ptep_get (ptep); -unsigned long i; - -for (i =0 ; i < ncontig; i++, addr += pgsize, ptep++) { -pte_t pte =__ptep_get_and_clear (mm, addr, ptep); - -/* -* If HW_AFDBM is enabled, then the HW could turn on -* the dirty or accessed bit for any page in the set, -* so check them all. -*/ -if (pte_dirty (pte)) - orig_pte =pte_mkdirty (orig_pte); - -if (pte_young (pte)) - orig_pte =pte_mkyoung (orig_pte); +pte_t pte, tmp_pte; +bool present; + + pte =__ptep_get_and_clear (mm, addr, ptep); + present =pte_present (pte); +while (--ncontig) { + ptep++; + addr += pgsize; + tmp_pte =__ptep_get_and_clear (mm, addr, ptep); +if (present) { +if (pte_dirty (tmp_pte)) + pte =pte_mkdirty (pte); +if (pte_young (tmp_pte))
Quoted 8 lines
arch/arm64/mm/hugetlbpage.c static pte_t get_clear_contig(struct mm_struct *mm, · line 181
Quoted 3 lines
arch/arm64/mm/hugetlbpage.c +2
-0+ pte =pte_mkyoung (pte); + } }nit: With the loop now structured like this, we really can't handle
num_contig_ptes()returning 0 if it gets an unknown size. Granted, that really shouldn't happen, but perhaps it would be better to add a 'default' case with aWARN()tonum_contig_ptes()and then add an early return here?
Looking at other users of num_contig_ptes() it looks like huge_ptep_get() already assumes at least 1 pte (it calls __ptep_get() before calling num_contig_ptes()) and set_huge_pte_at() assumes 1 pte for the "present and non-contig" case. So num_contig_ptes() returning 0 is already not really consumed consistently.
How about we change the default num_contig_ptes() return value to 1 and add a warning if size is invalid:
---8<---
arch/arm64/mm/hugetlbpage.c +0 -0
@@ -100,20 +100,11 @@ static int find_num_contig(struct mm_struct *mm, unsigned
long addr,
static inline int num_contig_ptes(unsigned long size, size_t *pgsize) {
- int contig_ptes = 0;
+ int contig_ptes = 1;
*pgsize = size;
switch (size) {-#ifndef __PAGETABLE_PMD_FOLDED
- case PUD_SIZE:
- if (pud_sect_supported())
- contig_ptes = 1;
- break;
-#endif
- case PMD_SIZE:
- contig_ptes = 1;
- break;
case CONT_PMD_SIZE:
*pgsize = PMD_SIZE;
contig_ptes = CONT_PMDS;
@@ -122,6 +113,8 @@ static inline int num_contig_ptes(unsigned long size, size_t
*pgsize)
*pgsize = PAGE_SIZE;
contig_ptes = CONT_PTES;
break;
+ default:
+ WARN_ON(!__hugetlb_valid_size(size));
}
return contig_ptes;---8<---
Quoted 2 lines
Will
Quoted 13 lines · Will Deacon
Quoted 12 lines · Catalin Marinas
Will, Ryan,
Quoted 8 lines · Ryan Roberts
This series contains some fixes for hugetlb on arm64, and is split out from v1 of a wider series at [1]. While the last patch is technically targetting core-mm and is not directly related to arm64, I'd like to to go via the arm64 tree so that the wider performance improvement series (v2 to be posted shortly) that depends on this series doesn't have to be robust to the fix not being present.
I've included maintainers/reviewers for all the arches that are (trivially) touched due to the API changes, hoping for some ACKs.
Quoted 8 lines
Quoted 4 lines
These fixes look fine to me and I think we should get them in for 6.14. I think Andrew was ok with them going in via the arm64 tree:
I think the diffstat looks worse than it really is, as the arch changes are reasonably mechanical. I'd like to let it sit in next for a few days though, so I'll pick this up once we've resolved my comment on patch #2.
Sounds good to me; if you're happy with my proposal in the patch 2 thread, then I'll respin.
Quoted 3 lines
Ryan -- did you find all of these issues by inspection, or are you aware of anybody hitting them in practice?
All by inspection. So I guess they are not urgent to fix from that perspective.
Quoted 2 lines
Will
Alexander Gordeev
maintainerRe: [PATCH v2 1/4] mm: hugetlb: Add huge page size param to huge_ptep_get_and_clear()
In reply to Ryan RobertsOn Mon, Feb 17, 2025 at 02:04:14PM +0000, Ryan Roberts wrote:
Hi Ryan,
Quoted 15 lines
In order to fix a bug, arm64 needs to be told the size of the huge page for which the
huge_pteis being set inhuge_ptep_get_and_clear(). Provide for this by adding anunsigned long szparameter to the function. This follows the same pattern ashuge_pte_clear()andset_huge_pte_at().This commit makes the required interface modifications to the core mm as well as all arches that implement this function (arm64, loongarch, mips, parisc, powerpc, riscv, s390, sparc). The actual arm64 bug will be fixed in a separate commit.
Fixes 66b3923a1a0f ("arm64: hugetlb: add support for PTE contiguous bit")Signed-off-by Ryan Roberts <[email protected]>---
...
arch/s390/include/asm/hugetlb.h void __set_huge_pte_at(struct mm_struct *mm, unsigned long addr, · line 31
Quoted 12 lines
arch/s390/include/asm/hugetlb.h +3
-1@@ -26,7 +26,11 @@ void __set_huge_pte_at(struct mm_struct *mm, unsigned long addr,pte_t huge_ptep_get (struct mm_struct *mm,unsigned long addr,pte_t *ptep);#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEAR -pte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr,pte_t *ptep); +pte_t huge_ptep_get_and_clear (struct mm_struct *mm, +unsigned long addr,pte_t *ptep, +unsigned long sz);
Please, format parameters similarily to set_huge_pte_at() few lines above.
arch/s390/include/asm/hugetlb.h void __set_huge_pte_at(struct mm_struct *mm, unsigned long addr, · line 33
Quoted 2 lines
arch/s390/include/asm/hugetlb.h +2
-0+pte_t __huge_ptep_get_and_clear (struct mm_struct *mm, +unsigned long addr,pte_t *ptep);
The formatting is broken, but please see below.
Quoted 45 lines
arch/s390/include/asm/hugetlb.h +3
-3static inline void arch_clear_hugetlb_flags (struct folio *folio) {@@ -48,7 +52,7 @@ static inline void huge_pte_clear(struct mm_struct *mm, unsigned long addr,static inline pte_t huge_ptep_clear_flush (struct vm_area_struct *vma,unsigned long address,pte_t *ptep) { -return huge_ptep_get_and_clear (vma->vm_mm, address, ptep); +return __huge_ptep_get_and_clear (vma->vm_mm, address, ptep); }#define __HAVE_ARCH_HUGE_PTEP_SET_ACCESS_FLAGS@@ -59,7 +63,7 @@ static inline int huge_ptep_set_access_flags(struct vm_area_struct *vma,int changed = !pte_same (huge_ptep_get (vma->vm_mm, addr, ptep), pte);if (changed) { -huge_ptep_get_and_clear (vma->vm_mm, addr, ptep); +__huge_ptep_get_and_clear (vma->vm_mm, addr, ptep);__set_huge_pte_at (vma->vm_mm, addr, ptep, pte); }return changed;@@ -69,7 +73,7 @@ static inline int huge_ptep_set_access_flags(struct vm_area_struct *vma,static inline void huge_ptep_set_wrprotect (struct mm_struct *mm,unsigned long addr,pte_t *ptep) { -pte_t pte =huge_ptep_get_and_clear (mm, addr, ptep); +pte_t pte =__huge_ptep_get_and_clear (mm, addr, ptep);__set_huge_pte_at (mm, addr, ptep,pte_wrprotect (pte)); }arch/s390/mm/hugetlbpage.c +2
-2@@ -188,8 +188,8 @@ pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)return __rste_to_pte (pte_val (*ptep)); } -pte_t huge_ptep_get_and_clear (struct mm_struct *mm, -unsigned long addr,pte_t *ptep) +pte_t __huge_ptep_get_and_clear (struct mm_struct *mm, +unsigned long addr,pte_t *ptep) {pte_t pte =huge_ptep_get (mm, addr, ptep);pmd_t *pmdp = (pmd_t *) ptep;
arch/s390/mm/hugetlbpage.c pte_t huge_ptep_get_and_clear(struct mm_struct *mm, · line 209
Quoted 8 lines
arch/s390/mm/hugetlbpage.c +5
-0@@ -202,6 +202,12 @@ pte_t huge_ptep_get_and_clear(struct mm_struct *mm,return pte; } +pte_t huge_ptep_get_and_clear (struct mm_struct *mm, +unsigned long addr,pte_t *ptep,unsigned long sz) +{ +return __huge_ptep_get_and_clear (mm, addr, ptep); +}
Is there a reason why this is not a header inline, as other callers of __huge_ptep_get_and_clear()?
arch/s390/mm/hugetlbpage.c pte_t huge_ptep_get_and_clear(struct mm_struct *mm, · line 213
Quoted 3 lines
arch/s390/mm/hugetlbpage.c +0
-0pte_t *huge_pte_alloc (struct mm_struct *mm,struct vm_area_struct *vma,unsigned long addr,unsigned long sz) {
...
Thanks!
Re: [PATCH v2 1/4] mm: hugetlb: Add huge page size param to huge_ptep_get_and_clear()
Quoted 27 lines · Alexander Gordeev
On Mon, Feb 17, 2025 at 02:04:14PM +0000, Ryan Roberts wrote:
Hi Ryan,
Quoted 15 lines
In order to fix a bug, arm64 needs to be told the size of the huge page for which the
huge_pteis being set inhuge_ptep_get_and_clear(). Provide for this by adding anunsigned long szparameter to the function. This follows the same pattern ashuge_pte_clear()andset_huge_pte_at().This commit makes the required interface modifications to the core mm as well as all arches that implement this function (arm64, loongarch, mips, parisc, powerpc, riscv, s390, sparc). The actual arm64 bug will be fixed in a separate commit.
Fixes 66b3923a1a0f ("arm64: hugetlb: add support for PTE contiguous bit")Signed-off-by Ryan Roberts <[email protected]>---
...
Quoted 7 lines
arch/s390/include/asm/hugetlb.h +0
-0@@ -26,7 +26,11 @@ void __set_huge_pte_at(struct mm_struct *mm, unsigned long addr,pte_t huge_ptep_get (struct mm_struct *mm,unsigned long addr,pte_t *ptep);
Quoted 7 lines
arch/s390/include/asm/hugetlb.h void __set_huge_pte_at(struct mm_struct *mm, unsigned long addr, · line 31
Quoted 5 lines
arch/s390/include/asm/hugetlb.h +3
-1#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEAR -pte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr,pte_t *ptep); +pte_t huge_ptep_get_and_clear (struct mm_struct *mm, +unsigned long addr,pte_t *ptep, +unsigned long sz);Please, format parameters similarily to
set_huge_pte_at()few lines above.
Appologies. I've fixed this for the next version.
Quoted 5 lines
arch/s390/include/asm/hugetlb.h void __set_huge_pte_at(struct mm_struct *mm, unsigned long addr, · line 33
Quoted 3 lines
arch/s390/include/asm/hugetlb.h +2
-0+pte_t __huge_ptep_get_and_clear (struct mm_struct *mm, +unsigned long addr,pte_t *ptep);The formatting is broken, but please see below.
Formatting fixed here too.
Quoted 49 lines
Quoted 49 lines
arch/s390/include/asm/hugetlb.h +3
-3static inline void arch_clear_hugetlb_flags (struct folio *folio) {@@ -48,7 +52,7 @@ static inline void huge_pte_clear(struct mm_struct *mm, unsigned long addr,static inline pte_t huge_ptep_clear_flush (struct vm_area_struct *vma,unsigned long address,pte_t *ptep) { -return huge_ptep_get_and_clear (vma->vm_mm, address, ptep); +return __huge_ptep_get_and_clear (vma->vm_mm, address, ptep); }#define __HAVE_ARCH_HUGE_PTEP_SET_ACCESS_FLAGS@@ -59,7 +63,7 @@ static inline int huge_ptep_set_access_flags(struct vm_area_struct *vma,int changed = !pte_same (huge_ptep_get (vma->vm_mm, addr, ptep), pte);if (changed) { -huge_ptep_get_and_clear (vma->vm_mm, addr, ptep); +__huge_ptep_get_and_clear (vma->vm_mm, addr, ptep);__set_huge_pte_at (vma->vm_mm, addr, ptep, pte); }return changed;@@ -69,7 +73,7 @@ static inline int huge_ptep_set_access_flags(struct vm_area_struct *vma,static inline void huge_ptep_set_wrprotect (struct mm_struct *mm,unsigned long addr,pte_t *ptep) { -pte_t pte =huge_ptep_get_and_clear (mm, addr, ptep); +pte_t pte =__huge_ptep_get_and_clear (mm, addr, ptep);__set_huge_pte_at (mm, addr, ptep,pte_wrprotect (pte)); }arch/s390/mm/hugetlbpage.c +2
-2@@ -188,8 +188,8 @@ pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)return __rste_to_pte (pte_val (*ptep)); } -pte_t huge_ptep_get_and_clear (struct mm_struct *mm, -unsigned long addr,pte_t *ptep) +pte_t __huge_ptep_get_and_clear (struct mm_struct *mm, +unsigned long addr,pte_t *ptep) {pte_t pte =huge_ptep_get (mm, addr, ptep);pmd_t *pmdp = (pmd_t *) ptep;@@ -202,6 +202,12 @@ pte_t huge_ptep_get_and_clear(struct mm_struct *mm,return pte; }
Quoted 8 lines
arch/s390/mm/hugetlbpage.c pte_t huge_ptep_get_and_clear(struct mm_struct *mm, · line 209
Quoted 5 lines
arch/s390/mm/hugetlbpage.c +5
-0+pte_t huge_ptep_get_and_clear (struct mm_struct *mm, +unsigned long addr,pte_t *ptep,unsigned long sz) +{ +return __huge_ptep_get_and_clear (mm, addr, ptep); +}Is there a reason why this is not a header inline, as other callers of
__huge_ptep_get_and_clear()?
I was trying to make the change as uninvasive as possible, so didn't want to change the linkage in case I accidentally broke something. Happy to make this an inline in the header though, if you prefer?
Thanks,
Ryan
Quoted 7 lines
arch/s390/mm/hugetlbpage.c pte_t huge_ptep_get_and_clear(struct mm_struct *mm, · line 213
Quoted 4 lines
arch/s390/mm/hugetlbpage.c +0
-0pte_t *huge_pte_alloc (struct mm_struct *mm,struct vm_area_struct *vma,unsigned long addr,unsigned long sz) {...
Thanks!
Will Deacon
maintainerRe: [PATCH v2 2/4] arm64: hugetlb: Fix huge_ptep_get_and_clear() for non-present ptes
In reply to Ryan RobertsQuoted 21 lines · Ryan Roberts
Quoted 11 lines · Will Deacon
Quoted 10 lines · Ryan Roberts
(quoted diff) +10
-0+ pte = __ptep_get_and_clear(mm, addr, ptep); + present = pte_present(pte); + while (--ncontig) { + ptep++; + addr += pgsize; + tmp_pte = __ptep_get_and_clear(mm, addr, ptep); + if (present) { + if (pte_dirty(tmp_pte)) + pte = pte_mkdirty(pte); + if (pte_young(tmp_pte))Quoted 8 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+ pte = pte_mkyoung(pte); + } }nit: With the loop now structured like this, we really can't handle
num_contig_ptes()returning 0 if it gets an unknown size. Granted, that really shouldn't happen, but perhaps it would be better to add a 'default' case with aWARN()tonum_contig_ptes()and then add an early return here?
Quoted 8 lines
Looking at other users of
num_contig_ptes()it looks likehuge_ptep_get()already assumes at least 1 pte (it calls__ptep_get()before callingnum_contig_ptes()) andset_huge_pte_at()assumes 1 pte for the "present and non-contig" case. Sonum_contig_ptes()returning 0 is already not really consumed consistently.How about we change the default
num_contig_ptes()return value to 1 and add a warning if size is invalid:
Fine by me!
I assume you'll fold that in and send a new version, along with the typo fixes?
Cheers,
Will
Alexander Gordeev
maintainerRe: [PATCH v2 1/4] mm: hugetlb: Add huge page size param to huge_ptep_get_and_clear()
In reply to Ryan RobertsQuoted 9 lines · Ryan Roberts
Quoted 8 lines
arch/s390/mm/hugetlbpage.c pte_t huge_ptep_get_and_clear(struct mm_struct *mm, · line 209
Quoted 5 lines
arch/s390/mm/hugetlbpage.c +5
-0+pte_t huge_ptep_get_and_clear (struct mm_struct *mm, +unsigned long addr,pte_t *ptep,unsigned long sz) +{ +return __huge_ptep_get_and_clear (mm, addr, ptep); +}Is there a reason why this is not a header inline, as other callers of
__huge_ptep_get_and_clear()?
Quoted 3 lines
I was trying to make the change as uninvasive as possible, so didn't want to change the linkage in case I accidentally broke something. Happy to make this an inline in the header though, if you prefer?
Yes, please.
Quoted 2 lines
Thanks,
Ryan
Thanks!
Re: [PATCH v2 1/4] mm: hugetlb: Add huge page size param to huge_ptep_get_and_clear()
In reply to Ryan RobertsLe 17/02/2025 à 15:04, Ryan Roberts a écrit :
Quoted 6 lines
In order to fix a bug, arm64 needs to be told the size of the huge page for which the
huge_pteis being set inhuge_ptep_get_and_clear(). Provide for this by adding anunsigned long szparameter to the function. This follows the same pattern ashuge_pte_clear()andset_huge_pte_at().
Quoted 8 lines
This commit makes the required interface modifications to the core mm as well as all arches that implement this function (arm64, loongarch, mips, parisc, powerpc, riscv, s390, sparc). The actual arm64 bug will be fixed in a separate commit.
Fixes 66b3923a1a0f ("arm64: hugetlb: add support for PTE contiguous bit")Signed-off-by Ryan Roberts <[email protected]>
I like the idea of adding the page size.
Once page size is added to both huge_ptep_get_and_clear() and huge_ptep_set_wrprotect(), it will be possible to remove the pagetable walk in powerpc 8xx pte_update() that is required at the moment to know if ptep is a 8M cont-PMD entry or a cont-PTE entry for a 512k or 16k page.
Quoted 348 lines
--- arch/arm64/include/asm/hugetlb.h | 4 ++-- arch/arm64/mm/hugetlbpage.c | 8 +++++--- arch/loongarch/include/asm/hugetlb.h | 6 ++++-- arch/mips/include/asm/hugetlb.h | 6 ++++-- arch/parisc/include/asm/hugetlb.h | 2 +- arch/parisc/mm/hugetlbpage.c | 2 +- arch/powerpc/include/asm/hugetlb.h | 6 ++++-- arch/riscv/include/asm/hugetlb.h | 3 ++- arch/riscv/mm/hugetlbpage.c | 2 +- arch/s390/include/asm/hugetlb.h | 12 ++++++++---- arch/s390/mm/hugetlbpage.c | 10 ++++++++-- arch/sparc/include/asm/hugetlb.h | 2 +- arch/sparc/mm/hugetlbpage.c | 2 +- include/asm-generic/hugetlb.h | 2 +- include/linux/hugetlb.h | 4 +++- mm/hugetlb.c | 4 ++-- 16 files changed, 48 insertions(+), 27 deletions(-)arch/arm64/include/asm/hugetlb.h +2
-2@@ -42,8 +42,8 @@ extern int huge_ptep_set_access_flags(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, pte_t pte, int dirty);#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEAR -extern pte_t huge_ptep_get_and_clear (struct mm_struct *mm, -unsigned long addr,pte_t *ptep); +extern pte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr, +pte_t *ptep,unsigned long sz);#define __HAVE_ARCH_HUGE_PTEP_SET_WRPROTECTextern void huge_ptep_set_wrprotect (struct mm_struct *mm,unsigned long addr,pte_t *ptep);arch/arm64/mm/hugetlbpage.c +5
-3@@ -396,8 +396,8 @@ void huge_pte_clear(struct mm_struct *mm, unsigned long addr,__pte_clear (mm, addr, ptep); } -pte_t huge_ptep_get_and_clear (struct mm_struct *mm, -unsigned long addr,pte_t *ptep) +pte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr, +pte_t *ptep,unsigned long sz) {int ncontig;size_t pgsize;@@ -549,6 +549,8 @@ bool __init arch_hugetlb_valid_size(unsigned long size)pte_t huge_ptep_modify_prot_start (struct vm_area_struct *vma,unsigned long addr,pte_t *ptep) { +unsigned long psize =huge_page_size (hstate_vma (vma)); +if (alternative_has_cap_unlikely (ARM64_WORKAROUND_2645198 )) {/* * Break-before-make (BBM) is required for all user space mappings @@ -558,7 +560,7 @@ pte_t huge_ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addrif (pte_user_exec (__ptep_get (ptep)))return huge_ptep_clear_flush (vma, addr, ptep); } -return huge_ptep_get_and_clear (vma->vm_mm, addr, ptep); +return huge_ptep_get_and_clear (vma->vm_mm, addr, ptep, psize); } void huge_ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep,arch/loongarch/include/asm/hugetlb.h +4
-2@@ -36,7 +36,8 @@ static inline void huge_pte_clear(struct mm_struct *mm, unsigned long addr,#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEARstatic inline pte_t huge_ptep_get_and_clear (struct mm_struct *mm, -unsigned long addr,pte_t *ptep) +unsigned long addr,pte_t *ptep, +unsigned long sz) {pte_t clear;pte_t pte =ptep_get (ptep);@@ -51,8 +52,9 @@ static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep) { pte_t pte; +unsigned long sz =huge_page_size (hstate_vma (vma)); - pte =huge_ptep_get_and_clear (vma->vm_mm, addr, ptep); + pte =huge_ptep_get_and_clear (vma->vm_mm, addr, ptep, sz);flush_tlb_page (vma, addr);return pte; }arch/mips/include/asm/hugetlb.h +4
-2@@ -27,7 +27,8 @@ static inline int prepare_hugepage_range(struct file *file,#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEARstatic inline pte_t huge_ptep_get_and_clear (struct mm_struct *mm, -unsigned long addr,pte_t *ptep) +unsigned long addr,pte_t *ptep, +unsigned long sz) {pte_t clear;pte_t pte = *ptep;@@ -42,13 +43,14 @@ static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep) { pte_t pte; +unsigned long sz =huge_page_size (hstate_vma (vma));/* * clear the huge pte entry firstly, so that the other smp threads will * not get old pte entry after finishing flush_tlb_page and before * setting new huge pte entry */ - pte =huge_ptep_get_and_clear (vma->vm_mm, addr, ptep); + pte =huge_ptep_get_and_clear (vma->vm_mm, addr, ptep, sz);flush_tlb_page (vma, addr);return pte; }arch/parisc/include/asm/hugetlb.h +1
-1@@ -10,7 +10,7 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEARpte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr, -pte_t *ptep); +pte_t *ptep,unsigned long sz);#define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,arch/parisc/mm/hugetlbpage.c +1
-1@@ -126,7 +126,7 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,pte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr, -pte_t *ptep) +pte_t *ptep,unsigned long sz) {pte_t entry;arch/powerpc/include/asm/hugetlb.h +4
-2@@ -45,7 +45,8 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep,#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEARstatic inline pte_t huge_ptep_get_and_clear (struct mm_struct *mm, -unsigned long addr,pte_t *ptep) +unsigned long addr,pte_t *ptep, +unsigned long sz) {return __pte (pte_update (mm, addr, ptep, ~0UL ,0 ,1 )); }@@ -55,8 +56,9 @@ static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep) { pte_t pte; +unsigned long sz =huge_page_size (hstate_vma (vma)); - pte =huge_ptep_get_and_clear (vma->vm_mm, addr, ptep); + pte =huge_ptep_get_and_clear (vma->vm_mm, addr, ptep, sz);flush_hugetlb_page (vma, addr);return pte; }arch/riscv/include/asm/hugetlb.h +2
-1@@ -28,7 +28,8 @@ void set_huge_pte_at(struct mm_struct *mm,#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEARpte_t huge_ptep_get_and_clear (struct mm_struct *mm, -unsigned long addr,pte_t *ptep); +unsigned long addr,pte_t *ptep, +unsigned long sz);#define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,arch/riscv/mm/hugetlbpage.c +1
-1@@ -293,7 +293,7 @@ int huge_ptep_set_access_flags(struct vm_area_struct *vma,pte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr, -pte_t *ptep) +pte_t *ptep,unsigned long sz) {pte_t orig_pte =ptep_get (ptep);int pte_num;arch/s390/include/asm/hugetlb.h +8
-4@@ -26,7 +26,11 @@ void __set_huge_pte_at(struct mm_struct *mm, unsigned long addr,pte_t huge_ptep_get (struct mm_struct *mm,unsigned long addr,pte_t *ptep);#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEAR -pte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr,pte_t *ptep); +pte_t huge_ptep_get_and_clear (struct mm_struct *mm, +unsigned long addr,pte_t *ptep, +unsigned long sz); +pte_t __huge_ptep_get_and_clear (struct mm_struct *mm, +unsigned long addr,pte_t *ptep);static inline void arch_clear_hugetlb_flags (struct folio *folio) {@@ -48,7 +52,7 @@ static inline void huge_pte_clear(struct mm_struct *mm, unsigned long addr,static inline pte_t huge_ptep_clear_flush (struct vm_area_struct *vma,unsigned long address,pte_t *ptep) { -return huge_ptep_get_and_clear (vma->vm_mm, address, ptep); +return __huge_ptep_get_and_clear (vma->vm_mm, address, ptep); }#define __HAVE_ARCH_HUGE_PTEP_SET_ACCESS_FLAGS@@ -59,7 +63,7 @@ static inline int huge_ptep_set_access_flags(struct vm_area_struct *vma,int changed = !pte_same (huge_ptep_get (vma->vm_mm, addr, ptep), pte);if (changed) { -huge_ptep_get_and_clear (vma->vm_mm, addr, ptep); +__huge_ptep_get_and_clear (vma->vm_mm, addr, ptep);__set_huge_pte_at (vma->vm_mm, addr, ptep, pte); }return changed;@@ -69,7 +73,7 @@ static inline int huge_ptep_set_access_flags(struct vm_area_struct *vma,static inline void huge_ptep_set_wrprotect (struct mm_struct *mm,unsigned long addr,pte_t *ptep) { -pte_t pte =huge_ptep_get_and_clear (mm, addr, ptep); +pte_t pte =__huge_ptep_get_and_clear (mm, addr, ptep);__set_huge_pte_at (mm, addr, ptep,pte_wrprotect (pte)); }arch/s390/mm/hugetlbpage.c +8
-2@@ -188,8 +188,8 @@ pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)return __rste_to_pte (pte_val (*ptep)); } -pte_t huge_ptep_get_and_clear (struct mm_struct *mm, -unsigned long addr,pte_t *ptep) +pte_t __huge_ptep_get_and_clear (struct mm_struct *mm, +unsigned long addr,pte_t *ptep) {pte_t pte =huge_ptep_get (mm, addr, ptep);pmd_t *pmdp = (pmd_t *) ptep;@@ -202,6 +202,12 @@ pte_t huge_ptep_get_and_clear(struct mm_struct *mm,return pte; } +pte_t huge_ptep_get_and_clear (struct mm_struct *mm, +unsigned long addr,pte_t *ptep,unsigned long sz) +{ +return __huge_ptep_get_and_clear (mm, addr, ptep); +} +pte_t *huge_pte_alloc (struct mm_struct *mm,struct vm_area_struct *vma,unsigned long addr,unsigned long sz) {arch/sparc/include/asm/hugetlb.h +1
-1@@ -20,7 +20,7 @@ void __set_huge_pte_at(struct mm_struct *mm, unsigned long addr,#define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEARpte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr, -pte_t *ptep); +pte_t *ptep,unsigned long sz);#define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,arch/sparc/mm/hugetlbpage.c +1
-1@@ -260,7 +260,7 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, }pte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr, -pte_t *ptep) +pte_t *ptep,unsigned long sz) {unsigned int i, nptes, orig_shift, shift;unsigned long size;include/asm-generic/hugetlb.h +1
-1@@ -90,7 +90,7 @@ static inline void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,#ifndef __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEARstatic inline pte_t huge_ptep_get_and_clear (struct mm_struct *mm, -unsigned long addr,pte_t *ptep) +unsigned long addr,pte_t *ptep,unsigned long sz) {return ptep_get_and_clear (mm, addr, ptep); }include/linux/hugetlb.h +3
-1@@ -1004,7 +1004,9 @@ static inline void hugetlb_count_sub(long l, struct mm_struct *mm)static inline pte_t huge_ptep_modify_prot_start (struct vm_area_struct *vma,unsigned long addr,pte_t *ptep) { -return huge_ptep_get_and_clear (vma->vm_mm, addr, ptep); +unsigned long psize =huge_page_size (hstate_vma (vma)); + +return huge_ptep_get_and_clear (vma->vm_mm, addr, ptep, psize); }#endif mm/hugetlb.c +2
-2@@ -5447,7 +5447,7 @@ static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr,if (src_ptl != dst_ptl)spin_lock_nested (src_ptl,SINGLE_DEPTH_NESTING ); - pte =huge_ptep_get_and_clear (mm, old_addr, src_pte); + pte =huge_ptep_get_and_clear (mm, old_addr, src_pte, sz);if (need_clear_uffd_wp &&pte_marker_uffd_wp (pte))huge_pte_clear (mm, new_addr, dst_pte, sz);@@ -5622,7 +5622,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,set_vma_resv_flags (vma,HPAGE_RESV_UNMAPPED ); } - pte =huge_ptep_get_and_clear (mm, address, ptep); + pte =huge_ptep_get_and_clear (mm, address, ptep, sz);tlb_remove_huge_tlb_entry (h, tlb, ptep, address);if (huge_pte_dirty (pte))set_page_dirty (page);
Re: [PATCH v2 2/4] arm64: hugetlb: Fix huge_ptep_get_and_clear() for non-present ptes
In reply to Will DeaconQuoted 28 lines · Will Deacon
Quoted 27 lines · Ryan Roberts
Quoted 11 lines · Will Deacon
Quoted 10 lines · Ryan Roberts
(quoted diff) +10
-0+ pte = __ptep_get_and_clear(mm, addr, ptep); + present = pte_present(pte); + while (--ncontig) { + ptep++; + addr += pgsize; + tmp_pte = __ptep_get_and_clear(mm, addr, ptep); + if (present) { + if (pte_dirty(tmp_pte)) + pte = pte_mkdirty(pte); + if (pte_young(tmp_pte))Quoted 8 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+ pte = pte_mkyoung(pte); + } }nit: With the loop now structured like this, we really can't handle
num_contig_ptes()returning 0 if it gets an unknown size. Granted, that really shouldn't happen, but perhaps it would be better to add a 'default' case with aWARN()tonum_contig_ptes()and then add an early return here?Looking at other users of
num_contig_ptes()it looks likehuge_ptep_get()already assumes at least 1 pte (it calls__ptep_get()before callingnum_contig_ptes()) andset_huge_pte_at()assumes 1 pte for the "present and non-contig" case. Sonum_contig_ptes()returning 0 is already not really consumed consistently.
Quoted 7 lines
Quoted 2 lines
How about we change the default
num_contig_ptes()return value to 1 and add a warning if size is invalid:Fine by me!
I assume you'll fold that in and send a new version, along with the typo fixes?
Yep, I'll aim to post this today. I have a few review comments for s390 to add in too.
Quoted 4 lines
Cheers,
Will