arm64: contpte: fix set_access_flags() no-op check for SMMU/ATS faults
Subsystems (3)
PATCH v2 1/1 arm64: contpte: fix set_access_flags() no-op check for SMMU/ATS faults
contpte_ptep_set_access_flags() compared the gathered ptep_get() value against the requested entry to detect no-ops. ptep_get() ORs AF/dirty from all sub-PTEs in the CONT block, so a dirty sibling can make the target appear already-dirty. When the gathered value matches entry, the function returns 0 even though the target sub-PTE still has PTE_RDONLY set in hardware.
For a CPU with FEAT_HAFDBS this gathered view is fine, since hardware may set AF/dirty on any sub-PTE and CPU TLB behavior is effectively gathered across the CONT range. But page-table walkers that evaluate each descriptor individually (e.g. a CPU without DBM support, or an SMMU without HTTU, or with HA/HD disabled in CD.TCR) can keep faulting on the unchanged target sub-PTE, causing an infinite fault loop.
Gathering can therefore cause false no-ops when only a sibling has been updated:
- write faults: target still has PTE_RDONLY (needs PTE_RDONLY cleared)
- read faults: target still lacks PTE_AF
Fix by checking each sub-PTE against the requested AF/dirty/write state (the same bits consumed by __ptep_set_access_flags()), using raw per-PTE values rather than the gathered ptep_get() view, before returning no-op. Keep using the raw target PTE for the write-bit unfold decision.
Per Arm ARM (DDI 0487) D8.7.1 ("The Contiguous bit"), any sub-PTE in a CONT range may become the effective cached translation and software must maintain consistent attributes across the range.
---
Changes in v2:
- Clarify commit message/comments: issue affects per-descriptor walkers (CPU without DBM support, or SMMU without HTTU / with HA/HD disabled).
- Clarify sub-PTE comparison semantics: use raw per-PTE values and match bits consumed by __ptep_set_access_flags() (AF, DIRTY, write permission).
- Add Reviewed-by/Tested-by trailers from the v1 thread.
--- arch/arm64/mm/contpte.c | 53 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 4 deletions(-)
arch/arm64/mm/contpte.c +49 -4
@@ -599,6 +599,27 @@ void contpte_clear_young_dirty_ptes(struct vm_area_struct *vma, }EXPORT_SYMBOL_GPL (contpte_clear_young_dirty_ptes); +static bool contpte_all_subptes_match_access_flags (pte_t *ptep,pte_t entry) +{ +pte_t *cont_ptep =contpte_align_down (ptep); +/* +* PFNs differ per sub-PTE. Match only bits consumed by +* __ptep_set_access_flags(): AF, DIRTY and write permission. +*/ +const pteval_t cmp_mask =PTE_RDONLY |PTE_AF |PTE_WRITE |PTE_DIRTY ; +pteval_t entry_cmp =pte_val (entry) & cmp_mask; +int i; + +for (i =0 ; i <CONT_PTES ; i++) { +pteval_t pte_cmp =pte_val (__ptep_get (cont_ptep + i)) & cmp_mask; + +if (pte_cmp != entry_cmp) +return false; + } + +return true; +} +int contpte_ptep_set_access_flags (struct vm_area_struct *vma,unsigned long addr,pte_t *ptep,pte_t entry,int dirty)
@@ -608,13 +629,37 @@ int contpte_ptep_set_access_flags(struct vm_area_struct *vma,int i;/* -* Gather the access/dirty bits for the contiguous range. If nothing has -* changed, its a noop. +* Check whether all sub-PTEs in the CONT block already match the +* requested access flags/write permission, using raw per-PTE values +* rather than the gathered ptep_get() view. +* +* __ptep_set_access_flags() can update AF, dirty and write +* permission, but only to make the mapping more permissive. +* +* ptep_get() gathers AF/dirty state across the whole CONT block, +* which is correct for a CPU with FEAT_HAFDBS. But page-table +* walkers that evaluate each descriptor individually (e.g. a CPU +* without DBM support, or an SMMU without HTTU, or with HA/HD +* disabled in CD.TCR) can keep faulting on the target sub-PTE if +* only a sibling has been updated. Gathering can therefore cause +* false no-ops when only a sibling has been updated: +* - write faults: target still has PTE_RDONLY (needs PTE_RDONLY cleared) +* - read faults: target still lacks PTE_AF +* +* Per Arm ARM (DDI 0487) D8.7.1, any sub-PTE in a CONT range may +* become the effective cached translation, so all entries must have +* consistent attributes. Check the full CONT block before returning +* no-op, and when any sub-PTE mismatches, proceed to update the whole +* range. */ - orig_pte =pte_mknoncont (ptep_get (ptep)); -if (pte_val (orig_pte) ==pte_val (entry)) +if (contpte_all_subptes_match_access_flags (ptep, entry))return 0 ; +/* +* Use raw target pte (not gathered) for write-bit unfold decision. +*/ + orig_pte =pte_mknoncont (__ptep_get (ptep)); +/* * We can fix up access/dirty bits without having to unfold the contig * range. But if the write bit is changing, we must unfold.
---
base-commit: c107785c7e8dbabd1c18301a1c362544b5786282
change-id: 20260305-contpte-fault-loop-76ed911b01c0
Best regards,
Signature · 1 line
Piotr Jaroszynski <[email protected]>
Quoted 120 lines · Piotr Jaroszynski
contpte_ptep_set_access_flags()compared the gatheredptep_get()value against the requested entry to detect no-ops.ptep_get()ORs AF/dirty from all sub-PTEs in the CONT block, so a dirty sibling can make the target appear already-dirty. When the gathered value matches entry, the function returns 0 even though the target sub-PTE still hasPTE_RDONLYset in hardware.For a CPU with
FEAT_HAFDBSthis gathered view is fine, since hardware may set AF/dirty on any sub-PTE and CPU TLB behavior is effectively gathered across the CONT range. But page-table walkers that evaluate each descriptor individually (e.g. a CPU without DBM support, or an SMMU without HTTU, or with HA/HD disabled in CD.TCR) can keep faulting on the unchanged target sub-PTE, causing an infinite fault loop.Gathering can therefore cause false no-ops when only a sibling has been updated:
- write faults: target still hasPTE_RDONLY(needsPTE_RDONLYcleared)
- read faults: target still lacksPTE_AFFix by checking each sub-PTE against the requested AF/dirty/write state (the same bits consumed by
__ptep_set_access_flags()), using raw per-PTE values rather than the gatheredptep_get()view, before returning no-op. Keep using the raw target PTE for the write-bit unfold decision.Per Arm ARM (DDI 0487) D8.7.1 ("The Contiguous bit"), any sub-PTE in a CONT range may become the effective cached translation and software must maintain consistent attributes across the range.
Fixes 4602e5757bcc ("arm64/mm: wire up PTE_CONT for user mappings")Cc Ryan Roberts <[email protected]>Cc Catalin Marinas <[email protected]>Cc Will Deacon <[email protected]>Cc Jason Gunthorpe <[email protected]>Cc John Hubbard <[email protected]>Cc Zi Yan <[email protected]>Cc Breno Leitao <[email protected]>Reviewed-by Alistair Popple <[email protected]>Reviewed-by James Houghton <[email protected]>Reviewed-by Ryan Roberts <[email protected]>Reviewed-by Catalin Marinas <[email protected]>Tested-by Breno Leitao <[email protected]>Signed-off-by Piotr Jaroszynski <[email protected]>---
Changes in v2:
- Clarify commit message/comments: issue affects per-descriptor walkers (CPU without DBM support, or SMMU without HTTU / with HA/HD disabled).
- Clarify sub-PTE comparison semantics: use raw per-PTE values and match bits consumed by__ptep_set_access_flags()(AF, DIRTY, write permission).
- Add Reviewed-by/Tested-by trailers from the v1 thread.--- arch/arm64/mm/contpte.c | 53 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 4 deletions(-)arch/arm64/mm/contpte.c +44
-4@@ -599,6 +599,27 @@ void contpte_clear_young_dirty_ptes(struct vm_area_struct *vma, }EXPORT_SYMBOL_GPL (contpte_clear_young_dirty_ptes); +static bool contpte_all_subptes_match_access_flags (pte_t *ptep,pte_t entry) +{ +pte_t *cont_ptep =contpte_align_down (ptep); +/* +* PFNs differ per sub-PTE. Match only bits consumed by +* __ptep_set_access_flags(): AF, DIRTY and write permission. +*/ +const pteval_t cmp_mask =PTE_RDONLY |PTE_AF |PTE_WRITE |PTE_DIRTY ; +pteval_t entry_cmp =pte_val (entry) & cmp_mask; +int i; + +for (i =0 ; i <CONT_PTES ; i++) { +pteval_t pte_cmp =pte_val (__ptep_get (cont_ptep + i)) & cmp_mask; + +if (pte_cmp != entry_cmp) +return false; + } + +return true; +} +int contpte_ptep_set_access_flags (struct vm_area_struct *vma,unsigned long addr,pte_t *ptep,pte_t entry,int dirty)@@ -608,13 +629,37 @@ int contpte_ptep_set_access_flags(struct vm_area_struct *vma,int i;/* -* Gather the access/dirty bits for the contiguous range. If nothing has -* changed, its a noop. +* Check whether all sub-PTEs in the CONT block already match the +* requested access flags/write permission, using raw per-PTE values +* rather than the gathered ptep_get() view. +* +* __ptep_set_access_flags() can update AF, dirty and write +* permission, but only to make the mapping more permissive. +* +* ptep_get() gathers AF/dirty state across the whole CONT block, +* which is correct for a CPU with FEAT_HAFDBS. But page-table +* walkers that evaluate each descriptor individually (e.g. a CPU +* without DBM support, or an SMMU without HTTU, or with HA/HD +* disabled in CD.TCR) can keep faulting on the target sub-PTE if +* only a sibling has been updated. Gathering can therefore cause +* false no-ops when only a sibling has been updated: +* - write faults: target still has PTE_RDONLY (needs PTE_RDONLY cleared) +* - read faults: target still lacks PTE_AF +* +* Per Arm ARM (DDI 0487) D8.7.1, any sub-PTE in a CONT range may +* become the effective cached translation, so all entries must have +* consistent attributes. Check the full CONT block before returning +* no-op, and when any sub-PTE mismatches, proceed to update the whole +* range. */ - orig_pte =pte_mknoncont (ptep_get (ptep)); -if (pte_val (orig_pte) ==pte_val (entry)) +if (contpte_all_subptes_match_access_flags (ptep, entry))
arch/arm64/mm/contpte.c int contpte_ptep_set_access_flags(struct vm_area_struct *vma, · line 665
Quoted 10 lines
arch/arm64/mm/contpte.c +5
-0return 0 ; +/* +* Use raw target pte (not gathered) for write-bit unfold decision. +*/ + orig_pte =pte_mknoncont (__ptep_get (ptep)); +/* * We can fix up access/dirty bits without having to unfold the contig * range. But if the write bit is changing, we must unfold.
Looks good
Balbir
Quoted 8 lines · Piotr Jaroszynski
contpte_ptep_set_access_flags()compared the gatheredptep_get()value against the requested entry to detect no-ops.ptep_get()ORs AF/dirty from all sub-PTEs in the CONT block, so a dirty sibling can make the target appear already-dirty. When the gathered value matches entry, the function returns 0 even though the target sub-PTE still hasPTE_RDONLYset in hardware.[...]
Applied to arm64 (for-next/fixes), thanks!
[1/1] arm64: contpte: fix set_access_flags() no-op check for SMMU/ATS faults
https://git.kernel.org/arm64/c/97c5550b7631
Cheers,
Signature · 5 lines
Will https://fixes.arm64.dev https://next.arm64.dev https://will.arm64.dev