Not all huge_pte helper APIs explicitly provide the size of the
huge_pte. So the helpers have to depend on various methods to determine
the size of the huge_pte. Some of these methods are dubious.
Let's clean up the code to use preferred methods and retire the dubious
ones. The options in order of preference:
- If size is provided as parameter, use it together with
num_contig_ptes(). This is explicit and works for both present and
non-present ptes.
- If vma is provided as a parameter, retrieve size via
huge_page_size(hstate_vma(vma)) and use it together with
num_contig_ptes(). This is explicit and works for both present and
non-present ptes.
- If the pte is present and contiguous, use find_num_contig() to walk
the pgtable to find the level and infer the number of ptes from
level. Only works for *present* ptes.
- If the pte is present and not contiguous and you can infer from this
that only 1 pte needs to be operated on. This is ok if you don't care
about the absolute size, and just want to know the number of ptes.
- NEVER rely on resolving the PFN of a present pte to a folio and
getting the folio's size. This is fragile at best, because there is
nothing to stop the core-mm from allocating a folio twice as big as
the huge_pte then mapping it across 2 consecutive huge_ptes. Or just
partially mapping it.
Where we require that the pte is present, add warnings if not-present.
Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
---
arch/arm64/mm/hugetlbpage.c | 20 +++++++++++++++-----
1 file changed, 15 insertions(+), 5 deletions(-)
diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c
index b3a7fafe8892..6a2af9fb2566 100644
--- a/arch/arm64/mm/hugetlbpage.c
+++ b/arch/arm64/mm/hugetlbpage.c
@@ -129,7 +129,7 @@ pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
if (!pte_present(orig_pte) || !pte_cont(orig_pte))
return orig_pte;
- ncontig = num_contig_ptes(page_size(pte_page(orig_pte)), &pgsize);
+ ncontig = find_num_contig(mm, addr, ptep, &pgsize);
for (i = 0; i < ncontig; i++, ptep++) {
pte_t pte = __ptep_get(ptep);
@@ -438,16 +438,19 @@ int huge_ptep_set_access_flags(struct vm_area_struct *vma,
pgprot_t hugeprot;
pte_t orig_pte;
+ VM_WARN_ON(!pte_present(pte));
+
if (!pte_cont(pte))
return __ptep_set_access_flags(vma, addr, ptep, pte, dirty);
- ncontig = find_num_contig(mm, addr, ptep, &pgsize);
+ ncontig = num_contig_ptes(huge_page_size(hstate_vma(vma)), &pgsize);
dpfn = pgsize >> PAGE_SHIFT;
if (!__cont_access_flags_changed(ptep, pte, ncontig))
return 0;
orig_pte = get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig);
+ VM_WARN_ON(!pte_present(orig_pte));
/* Make sure we don't lose the dirty or young state */
if (pte_dirty(orig_pte))
@@ -472,7 +475,10 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm,
size_t pgsize;
pte_t pte;
- if (!pte_cont(__ptep_get(ptep))) {
+ pte = __ptep_get(ptep);
+ VM_WARN_ON(!pte_present(pte));
+
+ if (!pte_cont(pte)) {
__ptep_set_wrprotect(mm, addr, ptep);
return;
}
@@ -496,11 +502,15 @@ pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
struct mm_struct *mm = vma->vm_mm;
size_t pgsize;
int ncontig;
+ pte_t pte;
+
+ pte = __ptep_get(ptep);
+ VM_WARN_ON(!pte_present(pte));
- if (!pte_cont(__ptep_get(ptep)))
+ if (!pte_cont(pte))
return ptep_clear_flush(vma, addr, ptep);
- ncontig = find_num_contig(mm, addr, ptep, &pgsize);
+ ncontig = num_contig_ptes(huge_page_size(hstate_vma(vma)), &pgsize);
return get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig);
}
--
2.43.0
On 3/4/25 20:34, Ryan Roberts wrote:
> Not all huge_pte helper APIs explicitly provide the size of the
> huge_pte. So the helpers have to depend on various methods to determine
> the size of the huge_pte. Some of these methods are dubious.
>
> Let's clean up the code to use preferred methods and retire the dubious
> ones. The options in order of preference:
>
> - If size is provided as parameter, use it together with
> num_contig_ptes(). This is explicit and works for both present and
> non-present ptes.
>
> - If vma is provided as a parameter, retrieve size via
> huge_page_size(hstate_vma(vma)) and use it together with
> num_contig_ptes(). This is explicit and works for both present and
> non-present ptes.
>
> - If the pte is present and contiguous, use find_num_contig() to walk
> the pgtable to find the level and infer the number of ptes from
> level. Only works for *present* ptes.
>
> - If the pte is present and not contiguous and you can infer from this
> that only 1 pte needs to be operated on. This is ok if you don't care
> about the absolute size, and just want to know the number of ptes.
>
> - NEVER rely on resolving the PFN of a present pte to a folio and
> getting the folio's size. This is fragile at best, because there is
> nothing to stop the core-mm from allocating a folio twice as big as
> the huge_pte then mapping it across 2 consecutive huge_ptes. Or just
> partially mapping it.
>
> Where we require that the pte is present, add warnings if not-present.
>
> Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
> ---
> arch/arm64/mm/hugetlbpage.c | 20 +++++++++++++++-----
> 1 file changed, 15 insertions(+), 5 deletions(-)
>
> diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c
> index b3a7fafe8892..6a2af9fb2566 100644
> --- a/arch/arm64/mm/hugetlbpage.c
> +++ b/arch/arm64/mm/hugetlbpage.c
> @@ -129,7 +129,7 @@ pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
> if (!pte_present(orig_pte) || !pte_cont(orig_pte))
> return orig_pte;
>
> - ncontig = num_contig_ptes(page_size(pte_page(orig_pte)), &pgsize);
> + ncontig = find_num_contig(mm, addr, ptep, &pgsize);
> for (i = 0; i < ncontig; i++, ptep++) {
> pte_t pte = __ptep_get(ptep);
>
> @@ -438,16 +438,19 @@ int huge_ptep_set_access_flags(struct vm_area_struct *vma,
> pgprot_t hugeprot;
> pte_t orig_pte;
>
> + VM_WARN_ON(!pte_present(pte));
> +
> if (!pte_cont(pte))
> return __ptep_set_access_flags(vma, addr, ptep, pte, dirty);
>
> - ncontig = find_num_contig(mm, addr, ptep, &pgsize);
> + ncontig = num_contig_ptes(huge_page_size(hstate_vma(vma)), &pgsize);
> dpfn = pgsize >> PAGE_SHIFT;
>
> if (!__cont_access_flags_changed(ptep, pte, ncontig))
> return 0;
>
> orig_pte = get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig);
> + VM_WARN_ON(!pte_present(orig_pte));
>
> /* Make sure we don't lose the dirty or young state */
> if (pte_dirty(orig_pte))
> @@ -472,7 +475,10 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm,
> size_t pgsize;
> pte_t pte;
>
> - if (!pte_cont(__ptep_get(ptep))) {
> + pte = __ptep_get(ptep);
> + VM_WARN_ON(!pte_present(pte));
> +
> + if (!pte_cont(pte)) {
> __ptep_set_wrprotect(mm, addr, ptep);
> return;
> }
> @@ -496,11 +502,15 @@ pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
> struct mm_struct *mm = vma->vm_mm;
> size_t pgsize;
> int ncontig;
> + pte_t pte;
> +
> + pte = __ptep_get(ptep);
> + VM_WARN_ON(!pte_present(pte));
>
> - if (!pte_cont(__ptep_get(ptep)))
> + if (!pte_cont(pte))
> return ptep_clear_flush(vma, addr, ptep);
>
> - ncontig = find_num_contig(mm, addr, ptep, &pgsize);
> + ncontig = num_contig_ptes(huge_page_size(hstate_vma(vma)), &pgsize);
> return get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig);
> }
>
Should a comment be added before all the VM_WARN_ON() explaining the rationale
about why the page table entries need to be present, before checking for their
contiguous attribute before subsequently calling into find_num_contig() ?
Regardless, LGTM.
Reviewed-by: Anshuman Khandual <anshuman.khandual@arm.com>
On Tue, Mar 04, 2025 at 03:04:31PM +0000, Ryan Roberts wrote: > Not all huge_pte helper APIs explicitly provide the size of the > huge_pte. So the helpers have to depend on various methods to determine > the size of the huge_pte. Some of these methods are dubious. > > Let's clean up the code to use preferred methods and retire the dubious > ones. The options in order of preference: > > - If size is provided as parameter, use it together with > num_contig_ptes(). This is explicit and works for both present and > non-present ptes. > > - If vma is provided as a parameter, retrieve size via > huge_page_size(hstate_vma(vma)) and use it together with > num_contig_ptes(). This is explicit and works for both present and > non-present ptes. > > - If the pte is present and contiguous, use find_num_contig() to walk > the pgtable to find the level and infer the number of ptes from > level. Only works for *present* ptes. > > - If the pte is present and not contiguous and you can infer from this > that only 1 pte needs to be operated on. This is ok if you don't care > about the absolute size, and just want to know the number of ptes. > > - NEVER rely on resolving the PFN of a present pte to a folio and > getting the folio's size. This is fragile at best, because there is > nothing to stop the core-mm from allocating a folio twice as big as > the huge_pte then mapping it across 2 consecutive huge_ptes. Or just > partially mapping it. > > Where we require that the pte is present, add warnings if not-present. > > Signed-off-by: Ryan Roberts <ryan.roberts@arm.com> Reviewed-by: Catalin Marinas <catalin.marinas@arm.com>
© 2016 - 2026 Red Hat, Inc.