arm64: contpte: fix set_access_flags() no-op check for SMMU/ATS faults
Subsystems (3)
PATCH 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 CPU page-table walks this is benign: with FEAT_HAFDBS the hardware may set AF/dirty on any sub-PTE and the CPU TLB treats the gathered result as authoritative for the entire range. But an SMMU without HTTU (or with HA/HD disabled in CD.TCR) evaluates each descriptor individually and will keep raising F_PERMISSION 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 all sub-PTEs' access flags individually (not via the gathered view) before returning no-op, and use the raw target PTE for the write-bit unfold decision. The access-flag mask matches the one used by __ptep_set_access_flags().
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.
--- arch/arm64/mm/contpte.c | 47 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-)
arch/arm64/mm/contpte.c +43 -4
@@ -390,6 +390,23 @@ 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); +const pteval_t access_mask =PTE_RDONLY |PTE_AF |PTE_WRITE |PTE_DIRTY ; +pteval_t entry_access =pte_val (entry) & access_mask; +int i; + +for (i =0 ; i <CONT_PTES ; i++) { +pteval_t pte_access =pte_val (__ptep_get (cont_ptep + i)) & access_mask; + +if (pte_access != entry_access) +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)
@@ -399,13 +416,35 @@ 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 have the +* requested access flags, using raw per-PTE values rather than the +* gathered ptep_get() view. +* +* ptep_get() gathers AF/dirty state across the whole CONT block, +* which is correct for CPU TLB semantics: with FEAT_HAFDBS the +* hardware may set AF/dirty on any sub-PTE and the CPU TLB treats +* the gathered result as authoritative for the entire range. But an +* SMMU without HTTU (or with HA/HD disabled in CD.TCR) evaluates +* each descriptor individually and will keep faulting on the target +* sub-PTE if its flags haven't actually 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.
Signature · 1 line
2.22.1.7.gac84d6e93c.dirty
Quoted 31 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 CPU page-table walks this is benign: with
FEAT_HAFDBSthe hardware may set AF/dirty on any sub-PTE and the CPU TLB treats the gathered result as authoritative for the entire range. But an SMMU without HTTU (or with HA/HD disabled in CD.TCR) evaluates each descriptor individually and will keep raisingF_PERMISSIONon 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 all sub-PTEs' access flags individually (not via the gathered view) before returning no-op, and use the raw target PTE for the write-bit unfold decision. The access-flag mask matches the one used by
__ptep_set_access_flags().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")
Quoted 10 lines
Reviewed-by Alistair Popple <[email protected]>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]>Signed-off-by Piotr Jaroszynski <[email protected]>
Thanks for the fix!
This is similar (sort of) to a HugeTLB page fault loop I stumbled upon a while ago[1]. (I wonder if there have been more cases like this.)
Feel free to add:
Quoted 2 lines · James Houghton
This is similar (sort of) to a HugeTLB page fault loop I stumbled upon a while ago[1]. (I wonder if there have been more cases like this.)
We spent some time here looking at what the ARM spec requires, and what kinds of HW implementations are possible..
I think the consensus is that SW must never write individual CONT PTEs that are different from each other.
For example, a possible HW implementation could fetch a cache line of PTEs, see the CONT bit at the VA's PTE, and then always select the first PTE in the cache line to load the TLB.
Alternatively HW could just always load the PTE from the VA that is faulting.
Since ARM ARM doesn't restrict this, SW must assume both possibilities and that means it must synchronize all PTEs since it cannot predict which PTE will be loaded by HW.
DBM will only write to a single PTE, which means HW can create inconsistent PTEs. However if SW is fixing a fault, especially for different walker IP (SMMU vs CPU), then it must restore the synchronization since the PTE selection of one walker may not be compatible with the HW update of the other.
Thus, I think any place where SW is creating inconsistent PTEs, or leaving PTEs inconsistent after a fault is a bug.. We found a Rule statement that seemed to require this as well.
Jason
Quoted 44 lines · James Houghton
Quoted 31 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 CPU page-table walks this is benign: with
FEAT_HAFDBSthe hardware may set AF/dirty on any sub-PTE and the CPU TLB treats the gathered result as authoritative for the entire range. But an SMMU without HTTU (or with HA/HD disabled in CD.TCR) evaluates each descriptor individually and will keep raisingF_PERMISSIONon 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 all sub-PTEs' access flags individually (not via the gathered view) before returning no-op, and use the raw target PTE for the write-bit unfold decision. The access-flag mask matches the one used by
__ptep_set_access_flags().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")Quoted 10 lines
Reviewed-by Alistair Popple <[email protected]>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]>Signed-off-by Piotr Jaroszynski <[email protected]>
Quoted 4 lines
Thanks for the fix!
This is similar (sort of) to a HugeTLB page fault loop I stumbled upon a while ago[1]. (I wonder if there have been more cases like this.)
I see that your commit 3c0696076aad ("arm64: mm: Always make sw-dirty PTEs hw-dirty in pte_modify") from that discussion was picked up and it's very relevant for the hugetlb exposure question. With your patch, do we have a guarantee that sw-dirty implies hw-dirty in all cases? If yes, then there should be no exposure for that path. But it still makes sense to make it more explicit.
Quoted 4 lines
Feel free to add:
Reviewed-by James Houghton <[email protected]>
Thanks!
Quoted 2 lines
Quoted 11 lines · Piotr Jaroszynski
Quoted 2 lines · James Houghton
This is similar (sort of) to a HugeTLB page fault loop I stumbled upon a while ago[1]. (I wonder if there have been more cases like this.)
I see that your commit
3c0696076aad("arm64: mm: Always make sw-dirty PTEs hw-dirty inpte_modify") from that discussion was picked up and it's very relevant for the hugetlb exposure question. With your patch, do we have a guarantee that sw-dirty implies hw-dirty in all cases? If yes, then there should be no exposure for that path. But it still makes sense to make it more explicit.
At the time, I was unsure that the patch made it *impossible* for Linux to ever write sw-dirty hw-clean PTEs, but, after that patch, such a write would have to somehow avoid pte_modify(). That seems unlikely; that was satisfactory for me at the time.
I thought (and still think) that explicitly fixing the HugeTLB path would be a good idea. I think that patch wasn't taken because it wasn't necessary to fix the bug I was staring at.
So if you want to take the patch to the HugeTLB path[2] or improve it in a v2 here, please do!
Thanks!
[2] https://lore.kernel.org/all/[email protected]/
Quoted 1 line
Quoted 1 line
Quoted 7 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.
Quoted 6 lines
For CPU page-table walks this is benign: with
FEAT_HAFDBSthe hardware may set AF/dirty on any sub-PTE and the CPU TLB treats the gathered result as authoritative for the entire range. But an SMMU without HTTU (or with HA/HD disabled in CD.TCR) evaluates each descriptor individually and will keep raisingF_PERMISSIONon the unchanged target sub-PTE, causing an infinite fault loop.
Ouch; thanks for the fix!
Quoted 11 lines
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 all sub-PTEs' access flags individually (not via the gathered view) before returning no-op, and use the raw target PTE for the write-bit unfold decision. The access-flag mask matches the one used by
__ptep_set_access_flags().
Quoted 6 lines
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")
nit: there shouldn't be whitespace here.
Quoted 10 lines
Reviewed-by Alistair Popple <[email protected]>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]>Signed-off-by Piotr Jaroszynski <[email protected]>
This fix looks good to me:
Quoted 15 lines
--- arch/arm64/mm/contpte.c | 47 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-)arch/arm64/mm/contpte.c +3
-0@@ -390,6 +390,23 @@ 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);
arch/arm64/mm/contpte.c void contpte_clear_young_dirty_ptes(struct vm_area_struct *vma, · line 405
Quoted 10 lines
arch/arm64/mm/contpte.c +10
-0+const pteval_t access_mask =PTE_RDONLY |PTE_AF |PTE_WRITE |PTE_DIRTY ; +pteval_t entry_access =pte_val (entry) & access_mask; +int i; + +for (i =0 ; i <CONT_PTES ; i++) { +pteval_t pte_access =pte_val (__ptep_get (cont_ptep + i)) & access_mask; + +if (pte_access != entry_access) +return false; + }
There are 2 forms of "dirty"; HW and SW. Here you are testing that all ptes in the contpte block have the same form of dirty, which I think is the correct thing to do. You could relax to just test that every pte has one of the forms of dirty, But in that case, if a pte is sw-dirty but not hw-dirty, then the PTE_RDONLY bit remains set and the SMMU will fault, I think?
If my reasoning is correct, then I think arm64 hugetlb has a similar bug; See __cont_access_flags_changed(), which just checks for any form of dirty. So I guess hugetlb is buggy in the same way and should be fixed to use this more stringent approach?
Thanks,
Ryan
Quoted 47 lines
arch/arm64/mm/contpte.c +30
-4+ +return true; +} +int contpte_ptep_set_access_flags (struct vm_area_struct *vma,unsigned long addr,pte_t *ptep,pte_t entry,int dirty)@@ -399,13 +416,35 @@ 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 have the +* requested access flags, using raw per-PTE values rather than the +* gathered ptep_get() view. +* +* ptep_get() gathers AF/dirty state across the whole CONT block, +* which is correct for CPU TLB semantics: with FEAT_HAFDBS the +* hardware may set AF/dirty on any sub-PTE and the CPU TLB treats +* the gathered result as authoritative for the entire range. But an +* SMMU without HTTU (or with HA/HD disabled in CD.TCR) evaluates +* each descriptor individually and will keep faulting on the target +* sub-PTE if its flags haven't actually 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.
Quoted 48 lines · Ryan Roberts
Quoted 7 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.Quoted 6 lines
For CPU page-table walks this is benign: with
FEAT_HAFDBSthe hardware may set AF/dirty on any sub-PTE and the CPU TLB treats the gathered result as authoritative for the entire range. But an SMMU without HTTU (or with HA/HD disabled in CD.TCR) evaluates each descriptor individually and will keep raisingF_PERMISSIONon the unchanged target sub-PTE, causing an infinite fault loop.Ouch; thanks for the fix!
Quoted 11 lines
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 all sub-PTEs' access flags individually (not via the gathered view) before returning no-op, and use the raw target PTE for the write-bit unfold decision. The access-flag mask matches the one used by
__ptep_set_access_flags().Quoted 6 lines
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")nit: there shouldn't be whitespace here.
Quoted 10 lines
Reviewed-by Alistair Popple <[email protected]>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]>Signed-off-by Piotr Jaroszynski <[email protected]>
Quoted 3 lines
This fix looks good to me:
Reviewed-by Ryan Roberts <[email protected]>
Thanks!
Quoted 25 lines
Quoted 25 lines
--- arch/arm64/mm/contpte.c | 47 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-)arch/arm64/mm/contpte.c +11
-0@@ -390,6 +390,23 @@ 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); +const pteval_t access_mask =PTE_RDONLY |PTE_AF |PTE_WRITE |PTE_DIRTY ; +pteval_t entry_access =pte_val (entry) & access_mask; +int i; + +for (i =0 ; i <CONT_PTES ; i++) { +pteval_t pte_access =pte_val (__ptep_get (cont_ptep + i)) & access_mask; + +if (pte_access != entry_access)
Quoted 8 lines
arch/arm64/mm/contpte.c void contpte_clear_young_dirty_ptes(struct vm_area_struct *vma, · line 405
Quoted 2 lines
arch/arm64/mm/contpte.c +2
-0+return false; + }There are 2 forms of "dirty"; HW and SW. Here you are testing that all ptes in the contpte block have the same form of dirty, which I think is the correct thing to do. You could relax to just test that every pte has one of the forms of dirty, But in that case, if a pte is sw-dirty but not hw-dirty, then the
PTE_RDONLYbit remains set and the SMMU will fault, I think?
Yes, for SMMU we need to make sure the HW-dirty state is correct or it will keep faulting. And while we are doing it, it makes sense to just update all the bits to be consistent.
Quoted 5 lines
If my reasoning is correct, then I think arm64 hugetlb has a similar bug; See
__cont_access_flags_changed(), which just checks for any form of dirty. So I guess hugetlb is buggy in the same way and should be fixed to use this more stringent approach?
Given sw-dirty is managed by sw, is it correct for sw to ever create a PTE that's sw-dirty but not hw-dirty? If not, then I think it will still work fine for the SMMU case as sw-dirty implies hw-dirty, and if it's missing then we will set both. But for thoroughness it could make sense to be stricter and add some comments there as it does feel a little fragile. I'm very new to this area though so probably best for others to comment and tackle this.
Thanks,
Piotr
Quoted 53 lines
Thanks,
RyanQuoted 47 lines
arch/arm64/mm/contpte.c +30
-4+ +return true; +} +int contpte_ptep_set_access_flags (struct vm_area_struct *vma,unsigned long addr,pte_t *ptep,pte_t entry,int dirty)@@ -399,13 +416,35 @@ 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 have the +* requested access flags, using raw per-PTE values rather than the +* gathered ptep_get() view. +* +* ptep_get() gathers AF/dirty state across the whole CONT block, +* which is correct for CPU TLB semantics: with FEAT_HAFDBS the +* hardware may set AF/dirty on any sub-PTE and the CPU TLB treats +* the gathered result as authoritative for the entire range. But an +* SMMU without HTTU (or with HA/HD disabled in CD.TCR) evaluates +* each descriptor individually and will keep faulting on the target +* sub-PTE if its flags haven't actually 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.
Quoted 5 lines · Piotr Jaroszynski
Quoted 4 lines
If my reasoning is correct, then I think arm64 hugetlb has a similar bug; See
__cont_access_flags_changed(), which just checks for any form of dirty. So I guess hugetlb is buggy in the same way and should be fixed to use this more stringent approach?
Quoted 7 lines
Given sw-dirty is managed by sw, is it correct for sw to ever create a PTE that's sw-dirty but not hw-dirty? If not, then I think it will still work fine for the SMMU case as sw-dirty implies hw-dirty, and if it's missing then we will set both. But for thoroughness it could make sense to be stricter and add some comments there as it does feel a little fragile. I'm very new to this area though so probably best for others to comment and tackle this.
I also am not so familiar with SW dirty, but the big thing to be sensitive to here is that the CPU can get by with setting something as RDONLY | DBM and the CPU will flip off RDONLY without issue, so you might not notice it.
But if handle_mm_fault() is called on something RDONLY|DBM it *MUST* clear the RDONLY for all CONT entries, or fail the fault. Otherwise the SMMU SVA explodes.
However, I agree the __cont_access_flags_changed() looks concerning.
I spent a few AI $$ to cross check this and it echo's Ryan's concern but did not find a specific counter-example where PTE_WRITE | PTE_DIRTY | PTE_RDONLY can happen for hugetlb. I don't really trust this negative result, so I'm inclined to agree it should be made more robust. I like the look of the patch (below) it proposed too.
AI ramblings:
PROMPT:
Review [email protected] and focus on __cont_access_flags_changed(), huge_ptep_set_access_flags(), hugetlb_fault(). Does it have the same issue as this patch is fixing, considering the following additional note:
A big thing to be sensitive to here is that the CPU can get by with setting something as RDONLY | DBM and the CPU will flip off RDONLY without issue, so you might not notice it.
But if handle_mm_fault() is called on something RDONLY|DBM it *MUST* clear the RDONLY for all CONT entries, or fail the fault.
====
Analysis: Does hugetlb __cont_access_flags_changed() have the same bug as the contpte patch?
The contpte bug (what the patch fixes)
The old contpte_ptep_set_access_flags() (contpte.c:614) used ptep_get() which gathers AF/dirty from ALL sub-PTEs via OR, then compared the gathered result against entry:
// OLD (buggy):
orig_pte = pte_mknoncont(ptep_get(ptep)); // gathers across all sub-PTEs!
if (pte_val(orig_pte) == pte_val(entry))
return 0;A dirty sibling makes the gathered PTE look already-dirty, even though the target sub-PTE still has PTE_RDONLY in hardware. False no-op → SMMU infinite fault loop.
The fix replaces this with contpte_all_subptes_match_access_flags() which checks each sub-PTE’s raw bit values
(PTE_RDONLY | PTE_AF | PTE_WRITE | PTE_DIRTY).
The hugetlb code
__cont_access_flags_changed() (hugetlbpage.c:400-418) does NOT use gathering for the no-op check – it reads each sub-PTE individually via __ptep_get():
for (i = 0; i < ncontig; i++) {
pte_t orig_pte = __ptep_get(ptep + i);
if (pte_dirty(pte) != pte_dirty(orig_pte))
return 1;
if (pte_young(pte) != pte_young(orig_pte))
return 1;
}So it does NOT have the exact same gathering-based false no-op bug.
But it does have a weaker form of the same conceptual problem
The check uses pte_dirty() which is:
#define pte_hw_dirty(pte) (pte_write(pte) && !pte_rdonly(pte))
#define pte_sw_dirty(pte) (!!(pte_val(pte) & PTE_DIRTY))
#define pte_dirty(pte) (pte_sw_dirty(pte) || pte_hw_dirty(pte))It abstracts over HW-dirty and SW-dirty. It does NOT check PTE_RDONLY directly.
If a sub-PTE were in the state PTE_WRITE | PTE_DIRTY | PTE_RDONLY (SW-dirty but HW-readonly), then pte_dirty() returns true (via sw_dirty), and the check would say “no change needed” – but PTE_RDONLY is still set in hardware, which the SMMU would fault on.
The contpte fix’s contpte_all_subptes_match_access_flags() would catch this because it compares the raw PTE_RDONLY bit directly.
Does PTE_WRITE | PTE_DIRTY | PTE_RDONLY actually occur?
=======================================================
Looking at all the arm64 PTE manipulation functions:
- pte_mkdirty(): sets PTE_DIRTY AND clears PTE_RDONLY (if writable) - pte_mkwrite_novma(): sets PTE_WRITE, clears PTE_RDONLY if PTE_DIRTY - pte_wrprotect(): saves HW dirty to SW dirty, sets PTE_RDONLY, clears
PTE_WRITE
- pte_mkclean(): clears PTE_DIRTY, sets PTE_RDONLY
- HAFDBS: can only CLEAR PTE_RDONLY, never set it
None of these create PTE_DIRTY | PTE_WRITE | PTE_RDONLY. The invariant
holds: for writable pages, PTE_DIRTY => !PTE_RDONLY.
Tracing the RDONLY|DBM scenario through hugetlb_fault()
For the specific scenario: all sub-PTEs are PTE_WRITE | PTE_RDONLY (RDONLY|DBM, clean writable), and handle_mm_fault() is called:
1. huge_ptep_get() gathers – no dirty siblings, so vmf.orig_pte = PTE_WRITE | PTE_RDONLY | PTE_AF
2. huge_pte_mkdirty() sets PTE_DIRTY, clears PTE_RDONLY 3. entry = PTE_WRITE | PTE_DIRTY | !PTE_RDONLY | PTE_AF 4. __cont_access_flags_changed():
- All sub-PTEs: PTE_WRITE | PTE_RDONLY | PTE_AF
- pte_dirty(entry) = true, pte_dirty(sub) = false (RDONLY set, no
PTE_DIRTY)
- Mismatch detected -> returns 1 -> BBM proceeds
5. BBM rewrites ALL PTEs with
PTE_WRITE | PTE_DIRTY | !PTE_RDONLY | PTE_AF
6. RDONLY cleared for all entries. Correct.
With some siblings HW-dirtied by CPU (e.g., PTE #3 has !PTE_RDONLY):
1. huge_ptep_get() gathers: sees HW-dirty sibling, calls pte_mkdirty() on gathered
2. entry = PTE_WRITE | PTE_DIRTY | !PTE_RDONLY | PTE_AF
3. __cont_access_flags_changed():
- PTE #3 (!PTE_RDONLY): pte_dirty() = true -> match
- PTE #0 (PTE_RDONLY): pte_dirty() = false -> mismatch -> returns
1
4. BBM proceeds. All PTEs rewritten. Correct.
With ALL siblings HW-dirtied by CPU:
1. All sub-PTEs: PTE_WRITE | !PTE_RDONLY | PTE_AF (all HW dirty)
2. entry: PTE_WRITE | PTE_DIRTY | !PTE_RDONLY | PTE_AF
3. __cont_access_flags_changed(): all pte_dirty() match -> returns 0
(no-op)4. But RDONLY is already clear on all PTEs, so SMMU won’t fault. Correct.
Conclusion
==========
The hugetlb code does NOT have the same bug in the sense that it doesn’t produce false no-ops with current code. The key difference is __cont_access_flags_changed() checks each sub-PTE individually, not a gathered view.
However, Ryan Roberts’ observation is valid:__cont_access_flags_changed() has a latent weakness. It relies on the invariant PTE_DIRTY => !PTE_RDONLY (for writable pages) rather than checking PTE_RDONLY directly. If this invariant were ever violated (e.g., by a future code change), the no-op check could falsely pass, leaving RDONLY set in hardware and causing SMMU fault loops. The contpte fix’s approach of checking raw bit values is more robust and should be adopted by the hugetlb code as a defense-in-depth measure.
The recommended fix for hugetlb: replace the pte_dirty()/pte_young() checks in __cont_access_flags_changed() with a raw-bitmask comparison similar to contpte_all_subptes_match_access_flags(), using the mask PTE_RDONLY | PTE_AF | PTE_WRITE | PTE_DIRTY.
arch/arm64/mm/hugetlbpage.c +10 -14
@@ -389,28 +389,24 @@ pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, }/* -* huge_ptep_set_access_flags will update access flags (dirty, accesssed) +* huge_ptep_set_access_flags will update access flags (dirty, accessed) * and write permission. * -* For a contiguous huge pte range we need to check whether or not write -* permission has to change only on the first pte in the set. Then for -* all the contiguous ptes we need to check whether or not there is a -* discrepancy between dirty or young. +* Check all sub-PTEs' raw access flag bits rather than using the abstracted +* pte_dirty()/pte_young() helpers which conflate HW-dirty and SW-dirty. +* This ensures PTE_RDONLY is checked directly: a sub-PTE that is SW-dirty +* (PTE_DIRTY set) but still has PTE_RDONLY would be missed by pte_dirty() +* but will cause an SMMU without HTTU to keep faulting. The access flag +* mask matches the one used by __ptep_set_access_flags(). */ static int __cont_access_flags_changed (pte_t *ptep,pte_t pte,int ncontig) { +const pteval_t access_mask =PTE_RDONLY |PTE_AF |PTE_WRITE |PTE_DIRTY ; +pteval_t pte_access =pte_val (pte) & access_mask;int i; -if (pte_write (pte) !=pte_write (__ptep_get (ptep))) -return 1 ; -for (i =0 ; i < ncontig; i++) { -pte_t orig_pte =__ptep_get (ptep + i); - -if (pte_dirty (pte) !=pte_dirty (orig_pte)) -return 1 ; - -if (pte_young (pte) !=pte_young (orig_pte)) +if ((pte_val (__ptep_get (ptep + i)) & access_mask) != pte_access)return 1 ; }
Jason
Quoted 9 lines · Jason Gunthorpe
Quoted 7 lines · Piotr Jaroszynski
Quoted 4 lines
If my reasoning is correct, then I think arm64 hugetlb has a similar bug; See
__cont_access_flags_changed(), which just checks for any form of dirty. So I guess hugetlb is buggy in the same way and should be fixed to use this more stringent approach?Given sw-dirty is managed by sw, is it correct for sw to ever create a PTE that's sw-dirty but not hw-dirty?
It's possible to have a dirty pte that you subsequently make read-only by calling pte_wrprotect().
These are the valid states for the bits:
* Dirty Writable | PTE_RDONLY PTE_WRITE PTE_DIRTY (sw) * 0 0 | 1 0 0 * 0 1 | 1 1 0 * 1 0 | 1 0 1 * 1 1 | 0 1 x
But I guess only PTE_RDONLY|PTE_WRITE|PTE_DIRTY causes a problem, which can't happen.
Quoted 7 lines
Quoted 6 lines
If not, then I think it will still
work fine for the SMMU case as sw-dirty implies hw-dirty, and if it's missing then we will set both. But for thoroughness it could make sense to be stricter and add some comments there as it does feel a little fragile. I'm very new to this area though so probably best for others to comment and tackle this.
Quoted 8 lines
I also am not so familiar with SW dirty, but the big thing to be sensitive to here is that the CPU can get by with setting something as RDONLY | DBM and the CPU will flip off RDONLY without issue, so you might not notice it.
But if
handle_mm_fault()is called on something RDONLY|DBM it *MUST* clear the RDONLY for all CONT entries, or fail the fault. Otherwise the SMMU SVA explodes.
Got it.
Quoted 6 lines
However, I agree the
__cont_access_flags_changed()looks concerning.I spent a few AI $$ to cross check this and it echo's Ryan's concern but did not find a specific counter-example where
PTE_WRITE|PTE_DIRTY|PTE_RDONLYcan happen for hugetlb.
The table I shared above (from pgtable.h) agrees that PTE_WRITE|PTE_DIRTY|PTE_RDONLY can never happen.
Quoted 182 lines
I don't really trust
this negative result, so I'm inclined to agree it should be made more robust. I like the look of the patch (below) it proposed too.AI ramblings:
PROMPT:
Review [email protected] and focus on__cont_access_flags_changed(),huge_ptep_set_access_flags(),hugetlb_fault(). Does it have the same issue as this patch is fixing, considering the following additional note:A big thing to be sensitive to here is that the CPU can get by with setting something as RDONLY | DBM and the CPU will flip off RDONLY without issue, so you might not notice it.
But if
handle_mm_fault()is called on something RDONLY|DBM it *MUST* clear the RDONLY for all CONT entries, or fail the fault.
====Analysis: Does hugetlb
__cont_access_flags_changed()have the same bug as the contpte patch?The contpte bug (what the patch fixes)
The old
contpte_ptep_set_access_flags()(contpte.c:614) usedptep_get()which gathers AF/dirty from ALL sub-PTEs via OR, then compared the gathered result against entry:// OLD (buggy): orig_pte = pte_mknoncont(ptep_get(ptep)); // gathers across all sub-PTEs! if (pte_val(orig_pte) == pte_val(entry)) return 0;A dirty sibling makes the gathered PTE look already-dirty, even though the target sub-PTE still has
PTE_RDONLYin hardware. False no-op → SMMU infinite fault loop.The fix replaces this with
contpte_all_subptes_match_access_flags()which checks each sub-PTE’s raw bit values
(PTE_RDONLY|PTE_AF|PTE_WRITE|PTE_DIRTY).The hugetlb code
__cont_access_flags_changed()(hugetlbpage.c:400-418) does NOT use gathering for the no-op check – it reads each sub-PTE individually via__ptep_get():for (i = 0; i < ncontig; i++) { pte_t orig_pte = __ptep_get(ptep + i); if (pte_dirty(pte) != pte_dirty(orig_pte)) return 1; if (pte_young(pte) != pte_young(orig_pte)) return 1; }So it does NOT have the exact same gathering-based false no-op bug.
But it does have a weaker form of the same conceptual problem
The check uses
pte_dirty()which is:#define pte_hw_dirty(pte) (pte_write(pte) && !pte_rdonly(pte)) #define pte_sw_dirty(pte) (!!(pte_val(pte) & PTE_DIRTY)) #define pte_dirty(pte) (pte_sw_dirty(pte) || pte_hw_dirty(pte))It abstracts over HW-dirty and SW-dirty. It does NOT check
PTE_RDONLYdirectly.If a sub-PTE were in the state
PTE_WRITE|PTE_DIRTY|PTE_RDONLY(SW-dirty but HW-readonly), thenpte_dirty()returns true (viasw_dirty), and the check would say “no change needed” – butPTE_RDONLYis still set in hardware, which the SMMU would fault on.The contpte fix’s
contpte_all_subptes_match_access_flags()would catch this because it compares the rawPTE_RDONLYbit directly.Does
PTE_WRITE|PTE_DIRTY|PTE_RDONLYactually occur?
=======================================================Looking at all the arm64 PTE manipulation functions:
(quoted diff) +0
-5- pte_mkdirty(): sets PTE_DIRTY AND clears PTE_RDONLY (if writable) - pte_mkwrite_novma(): sets PTE_WRITE, clears PTE_RDONLY if PTE_DIRTY - pte_wrprotect(): saves HW dirty to SW dirty, sets PTE_RDONLY, clears PTE_WRITE - pte_mkclean(): clears PTE_DIRTY, sets PTE_RDONLY - HAFDBS: can only CLEAR PTE_RDONLY, never set itNone of these create
PTE_DIRTY|PTE_WRITE|PTE_RDONLY. The invariant
holds: for writable pages,PTE_DIRTY=> !PTE_RDONLY.Tracing the RDONLY|DBM scenario through
hugetlb_fault()For the specific scenario: all sub-PTEs are
PTE_WRITE|PTE_RDONLY(RDONLY|DBM, clean writable), andhandle_mm_fault()is called:1.
huge_ptep_get()gathers – no dirty siblings, so vmf.orig_pte =PTE_WRITE|PTE_RDONLY|PTE_AF2. huge_pte_mkdirty() sets PTE_DIRTY, clears PTE_RDONLY 3. entry = PTE_WRITE | PTE_DIRTY | !PTE_RDONLY | PTE_AF 4. __cont_access_flags_changed():- All sub-PTEs:
PTE_WRITE|PTE_RDONLY|PTE_AF
-pte_dirty(entry)= true,pte_dirty(sub)= false (RDONLY set, no
PTE_DIRTY)
- Mismatch detected -> returns 1 -> BBM proceeds
5. BBM rewrites ALL PTEs with
PTE_WRITE|PTE_DIRTY| !PTE_RDONLY |PTE_AF
6. RDONLY cleared for all entries. Correct.With some siblings HW-dirtied by CPU (e.g., PTE #3 has !PTE_RDONLY):
1.
huge_ptep_get()gathers: sees HW-dirty sibling, callspte_mkdirty()on gathered
2. entry =PTE_WRITE|PTE_DIRTY| !PTE_RDONLY |PTE_AF
3.__cont_access_flags_changed():
- PTE #3 (!PTE_RDONLY):pte_dirty()= true -> match
- PTE #0 (PTE_RDONLY):pte_dirty()= false -> mismatch -> returns
1
4. BBM proceeds. All PTEs rewritten. Correct.With ALL siblings HW-dirtied by CPU:
1. All sub-PTEs: PTE_WRITE | !PTE_RDONLY | PTE_AF (all HW dirty) 2. entry: PTE_WRITE | PTE_DIRTY | !PTE_RDONLY | PTE_AF 3. __cont_access_flags_changed(): all pte_dirty() match -> returns 0 (no-op)4. But RDONLY is already clear on all PTEs, so SMMU won’t fault. Correct.
Conclusion
==========The hugetlb code does NOT have the same bug in the sense that it doesn’t produce false no-ops with current code. The key difference is
__cont_access_flags_changed()checks each sub-PTE individually, not a gathered view.However, Ryan Roberts’ observation is valid:
__cont_access_flags_changed()has a latent weakness. It relies on the invariantPTE_DIRTY=> !PTE_RDONLY (for writable pages) rather than checkingPTE_RDONLYdirectly. If this invariant were ever violated (e.g., by a future code change), the no-op check could falsely pass, leaving RDONLY set in hardware and causing SMMU fault loops. The contpte fix’s approach of checking raw bit values is more robust and should be adopted by the hugetlb code as a defense-in-depth measure.The recommended fix for hugetlb: replace the pte_dirty()/pte_young() checks in
__cont_access_flags_changed()with a raw-bitmask comparison similar tocontpte_all_subptes_match_access_flags(), using the maskPTE_RDONLY|PTE_AF|PTE_WRITE|PTE_DIRTY.arch/arm64/mm/hugetlbpage.c +9
-8@@ -389,28 +389,24 @@ pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, }/* -* huge_ptep_set_access_flags will update access flags (dirty, accesssed) +* huge_ptep_set_access_flags will update access flags (dirty, accessed) * and write permission. * -* For a contiguous huge pte range we need to check whether or not write -* permission has to change only on the first pte in the set. Then for -* all the contiguous ptes we need to check whether or not there is a -* discrepancy between dirty or young. +* Check all sub-PTEs' raw access flag bits rather than using the abstracted +* pte_dirty()/pte_young() helpers which conflate HW-dirty and SW-dirty. +* This ensures PTE_RDONLY is checked directly: a sub-PTE that is SW-dirty +* (PTE_DIRTY set) but still has PTE_RDONLY would be missed by pte_dirty() +* but will cause an SMMU without HTTU to keep faulting. The access flag +* mask matches the one used by __ptep_set_access_flags(). */ static int __cont_access_flags_changed (pte_t *ptep,pte_t pte,int ncontig) { +const pteval_t access_mask =PTE_RDONLY |PTE_AF |PTE_WRITE |PTE_DIRTY ; +pteval_t pte_access =pte_val (pte) & access_mask;int i; -if (pte_write (pte) !=pte_write (__ptep_get (ptep))) -return 1 ; -
arch/arm64/mm/hugetlbpage.c pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, · line 411
Quoted 10 lines
arch/arm64/mm/hugetlbpage.c +1
-6for (i =0 ; i < ncontig; i++) { -pte_t orig_pte =__ptep_get (ptep + i); - -if (pte_dirty (pte) !=pte_dirty (orig_pte)) -return 1 ; - -if (pte_young (pte) !=pte_young (orig_pte)) +if ((pte_val (__ptep_get (ptep + i)) & access_mask) != pte_access)return 1 ; }
I think, based on all the above, the current version is actually not buggy. But I'm only willing to go to 95% confidence :)
The change looks reasonable though, if you want to be safe.
Thanks,
Ryan
Quoted 3 lines
Jason
Quoted 6 lines · Ryan Roberts
Quoted 6 lines
static int
__cont_access_flags_changed(pte_t *ptep, pte_t pte, int ncontig){arch/arm64/mm/hugetlbpage.c +2
-0+const pteval_t access_mask =PTE_RDONLY |PTE_AF |PTE_WRITE |PTE_DIRTY ; +pteval_t pte_access =pte_val (pte) & access_mask;int i;
Quoted 16 lines
arch/arm64/mm/hugetlbpage.c pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, · line 411
Quoted 13 lines
arch/arm64/mm/hugetlbpage.c +1
-9-if (pte_write (pte) !=pte_write (__ptep_get (ptep))) -return 1 ; -for (i =0 ; i < ncontig; i++) { -pte_t orig_pte =__ptep_get (ptep + i); - -if (pte_dirty (pte) !=pte_dirty (orig_pte)) -return 1 ; - -if (pte_young (pte) !=pte_young (orig_pte)) +if ((pte_val (__ptep_get (ptep + i)) & access_mask) != pte_access)return 1 ; }I think, based on all the above, the current version is actually not buggy. But I'm only willing to go to 95% confidence :)
If I understand it right James already found and fixed a case that violated the invariant right? Maybe it is wise to be robust since things lock up if it goes wrong.
Jason
Catalin Marinas
maintainerQuoted 7 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.
Quoted 6 lines
For CPU page-table walks this is benign: with
FEAT_HAFDBSthe hardware may set AF/dirty on any sub-PTE and the CPU TLB treats the gathered result as authoritative for the entire range. But an SMMU without HTTU (or with HA/HD disabled in CD.TCR) evaluates each descriptor individually and will keep raisingF_PERMISSIONon the unchanged target sub-PTE, causing an infinite fault loop.
This can also happen if not all CPUs support the hardware updates of the AF/dirty bits.
Quoted 49 lines
arch/arm64/mm/contpte.c +32
-2@@ -390,6 +390,23 @@ 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); +const pteval_t access_mask =PTE_RDONLY |PTE_AF |PTE_WRITE |PTE_DIRTY ; +pteval_t entry_access =pte_val (entry) & access_mask; +int i; + +for (i =0 ; i <CONT_PTES ; i++) { +pteval_t pte_access =pte_val (__ptep_get (cont_ptep + i)) & access_mask; + +if (pte_access != entry_access) +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)@@ -399,13 +416,35 @@ 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 have the +* requested access flags, using raw per-PTE values rather than the +* gathered ptep_get() view. +* +* ptep_get() gathers AF/dirty state across the whole CONT block, +* which is correct for CPU TLB semantics: with FEAT_HAFDBS the +* hardware may set AF/dirty on any sub-PTE and the CPU TLB treats +* the gathered result as authoritative for the entire range. But an +* SMMU without HTTU (or with HA/HD disabled in CD.TCR) evaluates +* each descriptor individually and will keep faulting on the target +* sub-PTE if its flags haven't actually 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 +*
arch/arm64/mm/contpte.c int contpte_ptep_set_access_flags(struct vm_area_struct *vma, · line 441
Quoted 10 lines
arch/arm64/mm/contpte.c +6
-2+* 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 ;
Actually, do we need to loop over all the ptes? I think it sufficient to only check the one at ptep since it is the one that triggered the fault. Instead of ptep_get(ptep), use __ptep_get(ptep). The rest of the function sets the flags correctly for all the ptes in the contig range.
Signature · 1 line
Catalin
Quoted 27 lines · Catalin Marinas
Quoted 27 lines
arch/arm64/mm/contpte.c +20
-2@@ -399,13 +416,35 @@ 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 have the +* requested access flags, using raw per-PTE values rather than the +* gathered ptep_get() view. +* +* ptep_get() gathers AF/dirty state across the whole CONT block, +* which is correct for CPU TLB semantics: with FEAT_HAFDBS the +* hardware may set AF/dirty on any sub-PTE and the CPU TLB treats +* the gathered result as authoritative for the entire range. But an +* SMMU without HTTU (or with HA/HD disabled in CD.TCR) evaluates +* each descriptor individually and will keep faulting on the target +* sub-PTE if its flags haven't actually 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. */
Quoted 8 lines
arch/arm64/mm/contpte.c int contpte_ptep_set_access_flags(struct vm_area_struct *vma, · line 441
Quoted 4 lines
arch/arm64/mm/contpte.c +1
-2- 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 ;Actually, do we need to loop over all the ptes? I think it sufficient to only check the one at ptep since it is the one that triggered the fault.
With CONT we should not be thinking "the one that triggered the fault".
The PTE that triggered the fault is the PTE that the HW happened to load into the TLB, we cannot assume it is the sub PTE we are faulting at. For instance it could be a sub PTE for a completely unrelated access at a different VA that got cached.
Again, the requirement here is that a fault on a CONT PTE must fix all the access flags to be consistent or fail. It cannot resume the fault and leave the sub PTEs inconsistent as the HW is always allowed to load the RDONLY one for any access to the CONT.
Jason
Catalin Marinas
maintainerQuoted 37 lines · Jason Gunthorpe
Quoted 27 lines · Catalin Marinas
Quoted 27 lines
arch/arm64/mm/contpte.c +20
-2@@ -399,13 +416,35 @@ 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 have the +* requested access flags, using raw per-PTE values rather than the +* gathered ptep_get() view. +* +* ptep_get() gathers AF/dirty state across the whole CONT block, +* which is correct for CPU TLB semantics: with FEAT_HAFDBS the +* hardware may set AF/dirty on any sub-PTE and the CPU TLB treats +* the gathered result as authoritative for the entire range. But an +* SMMU without HTTU (or with HA/HD disabled in CD.TCR) evaluates +* each descriptor individually and will keep faulting on the target +* sub-PTE if its flags haven't actually 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. */ Quoted 8 lines
arch/arm64/mm/contpte.c int contpte_ptep_set_access_flags(struct vm_area_struct *vma, · line 441
Quoted 4 lines
arch/arm64/mm/contpte.c +1
-2- 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 ;Actually, do we need to loop over all the ptes? I think it sufficient to only check the one at ptep since it is the one that triggered the fault.
Quoted 7 lines
With CONT we should not be thinking "the one that triggered the fault".
The PTE that triggered the fault is the PTE that the HW happened to load into the TLB, we cannot assume it is the sub PTE we are faulting at. For instance it could be a sub PTE for a completely unrelated access at a different VA that got cached.
Good point. For the AF bit, the hardware is not allowed to cache it in the TLB, so we can't get an AF fault for an unrelated VA nearby. We can, however, for the dirty bit since PTE_RDONLY is allowed to be cached in the TLB.
Quoted 4 lines
Again, the requirement here is that a fault on a CONT PTE must fix all the access flags to be consistent or fail. It cannot resume the fault and leave the sub PTEs inconsistent as the HW is always allowed to load the RDONLY one for any access to the CONT.
It should fix all of them to be consistent if it got a fault. I was wondering whether we can simplify this with a single pte read (but still setting all in the range). It only works for the AF bit, not dirty. We could add a check if it makes things slightly faster on this path.
Now I also wonder if the pte_write(orig_pte) == pte_write(entry) check to elide BBM is still valid if we have hardware that does not support DBM. I need to dig some more into the Arm ARM.
Signature · 1 line
Catalin
Quoted 2 lines · Catalin Marinas
Good point. For the AF bit, the hardware is not allowed to cache it in the TLB, so we can't get an AF fault for an unrelated VA nearby.
The way we have read the spec is there is no restriction on what PTE the HW accesses when it encounters a CONT group.
To be concrete, the spec seems to say it is legal to make HW that fetches the PTE at the VA, sees the CONT bit, and then always fetches the 0th PTE from the group and only uses that for permission checks.
Therefore SW should never assume that HW will read any particular sub-PTE under any scenario.
It seems current cores don't do this, and it is a bit silly to do, but I can imagine an optimizion where the core does a cache line fetch to read the PTE so it can freely snap to the PTE at the start of the cache line for permission checks. Consolidating permission storage to fewer PTEs would reduce atomic memory traffic if the TLB is thrashing.
Jason
Quoted 11 lines · Jason Gunthorpe
Quoted 2 lines · Catalin Marinas
Good point. For the AF bit, the hardware is not allowed to cache it in the TLB, so we can't get an AF fault for an unrelated VA nearby.
The way we have read the spec is there is no restriction on what PTE the HW accesses when it encounters a CONT group.
To be concrete, the spec seems to say it is legal to make HW that fetches the PTE at the VA, sees the CONT bit, and then always fetches the 0th PTE from the group and only uses that for permission checks.
Quoted 8 lines
Therefore SW should never assume that HW will read any particular sub-PTE under any scenario.
It seems current cores don't do this, and it is a bit silly to do, but I can imagine an optimizion where the core does a cache line fetch to read the PTE so it can freely snap to the PTE at the start of the cache line for permission checks. Consolidating permission storage to fewer PTEs would reduce atomic memory traffic if the TLB is thrashing.
"The Contiguous bit" section I quoted in the change says this:
The entry is permitted to be cached in a TLB as though it is one of a number of adjacent translation table entries that point to a contiguous OA range with consistent attributes and permissions.
Software is required to ensure that all of the adjacent translation table entries for the contiguous region point to a contiguous OA range with consistent attributes and permissions.
I think your example is valid as any of the sub-PTEs can be cached. Another valid example is that first you access addr A and PTE for A gets cached as the value for the whole 2MB region. Then you access a different address B within the region and fault based on the cached attributes. In this case the SMMU never had to read the PTE for B as it already cached it when accessing A. If the faulting code only read the PTE for B it can show that e.g. RDONLY was already cleared and hit the problem again.
In summary, I don't see a way to skip reading and fixing all the sub-PTEs. And the previous code is already reading all of them so the fix is not adding any new overhead.
Quoted 2 lines
Jason
Catalin Marinas
maintainerQuoted 6 lines · Jason Gunthorpe
Quoted 2 lines · Catalin Marinas
Good point. For the AF bit, the hardware is not allowed to cache it in the TLB, so we can't get an AF fault for an unrelated VA nearby.
The way we have read the spec is there is no restriction on what PTE the HW accesses when it encounters a CONT group.
Trying to find some rule in the Arm ARM, it only says that hardware AF/DBM only happens to a single entry but it is not specific about which in a contiguous range.
So yeah, it's better not to assume anything. If it helps software, we could tighten the architecture but I think the benefit is marginal.
Signature · 1 line
Catalin
Quoted 30 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 CPU page-table walks this is benign: with
FEAT_HAFDBSthe hardware may set AF/dirty on any sub-PTE and the CPU TLB treats the gathered result as authoritative for the entire range. But an SMMU without HTTU (or with HA/HD disabled in CD.TCR) evaluates each descriptor individually and will keep raisingF_PERMISSIONon 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 all sub-PTEs' access flags individually (not via the gathered view) before returning no-op, and use the raw target PTE for the write-bit unfold decision. The access-flag mask matches the one used by
__ptep_set_access_flags().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")
Quoted 10 lines
Reviewed-by Alistair Popple <[email protected]>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]>Signed-off-by Piotr Jaroszynski <[email protected]>
Looking at the patch again, some more comments.
arch/arm64/mm/contpte.c void contpte_clear_young_dirty_ptes(struct vm_area_struct *vma, · line 393
Quoted 9 lines · Piotr Jaroszynski
arch/arm64/mm/contpte.c +1
-0@@ -390,6 +390,23 @@ 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)
More of a nitpick: since this checks both the flags and write permission, I'd rename to something else. Maybe contpte_ptep_same() to somewhat resemble pte_same() used by __ptep_set_access_flags().
arch/arm64/mm/contpte.c void contpte_clear_young_dirty_ptes(struct vm_area_struct *vma, · line 396
Quoted 3 lines
arch/arm64/mm/contpte.c +3
-0+{ +pte_t *cont_ptep =contpte_align_down (ptep); +const pteval_t access_mask =PTE_RDONLY |PTE_AF |PTE_WRITE |PTE_DIRTY ;
We can drop the PTE_DIRTY from the mask as it's not relevant to the hardware permission. It probably doesn't matter in practice.
Quoted 17 lines
arch/arm64/mm/contpte.c +13
-0+pteval_t entry_access =pte_val (entry) & access_mask; +int i; + +for (i =0 ; i <CONT_PTES ; i++) { +pteval_t pte_access =pte_val (__ptep_get (cont_ptep + i)) & access_mask; + +if (pte_access != entry_access) +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)
arch/arm64/mm/contpte.c int contpte_ptep_set_access_flags(struct vm_area_struct *vma, · line 421
Quoted 8 lines
arch/arm64/mm/contpte.c +3
-2@@ -399,13 +416,35 @@ 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 have the +* requested access flags, using raw per-PTE values rather than the +* gathered ptep_get() view.
It's not just about the access flag but AF, dirty and write permission, all can be changed by this function (and only to a more permissive setting).
arch/arm64/mm/contpte.c int contpte_ptep_set_access_flags(struct vm_area_struct *vma, · line 427
Quoted 6 lines
arch/arm64/mm/contpte.c +6
-0+* +* ptep_get() gathers AF/dirty state across the whole CONT block, +* which is correct for CPU TLB semantics: with FEAT_HAFDBS the +* hardware may set AF/dirty on any sub-PTE and the CPU TLB treats +* the gathered result as authoritative for the entire range. But an +* SMMU without HTTU (or with HA/HD disabled in CD.TCR) evaluates
Or CPU equally, we don't force all CPUs in a system to support DBM.
Quoted 11 lines
arch/arm64/mm/contpte.c +11
-0+* each descriptor individually and will keep faulting on the target +* sub-PTE if its flags haven't actually 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.
arch/arm64/mm/contpte.c int contpte_ptep_set_access_flags(struct vm_area_struct *vma, · line 446
Quoted 10 lines
arch/arm64/mm/contpte.c +5
-2*/ - 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));
This is fine since all should have the same PTE_WRITE bit.
Anyway, nothing major, so:
Quoted 11 lines · Catalin Marinas
Looking at the patch again, some more comments.
Quoted 8 lines · Piotr Jaroszynski
arch/arm64/mm/contpte.c +0
-0@@ -390,6 +390,23 @@ void contpte_clear_young_dirty_ptes(struct vm_area_struct *vma, }EXPORT_SYMBOL_GPL (contpte_clear_young_dirty_ptes);
Quoted 5 lines
arch/arm64/mm/contpte.c void contpte_clear_young_dirty_ptes(struct vm_area_struct *vma, · line 393
Quoted 1 line
arch/arm64/mm/contpte.c +1
-0+static bool contpte_all_subptes_match_access_flags (pte_t *ptep,pte_t entry)More of a nitpick: since this checks both the flags and write permission, I'd rename to something else. Maybe
contpte_ptep_same()to somewhat resemblepte_same()used by__ptep_set_access_flags().
pte_same() also compares the PFN though. I picked the _access_flags suffix to match the ptep_set_access_flags() naming. I do agree the aliasing of AF and "access flags" is unfortunate, but it seems preexisting. I am updating the comments to be clearer in v2, let me know if that works.
Quoted 7 lines
arch/arm64/mm/contpte.c void contpte_clear_young_dirty_ptes(struct vm_area_struct *vma, · line 396
Quoted 4 lines
arch/arm64/mm/contpte.c +3
-0+{ +pte_t *cont_ptep =contpte_align_down (ptep); +const pteval_t access_mask =PTE_RDONLY |PTE_AF |PTE_WRITE |PTE_DIRTY ;We can drop the
PTE_DIRTYfrom the mask as it's not relevant to the hardware permission. It probably doesn't matter in practice.
I think it's good to be consistent and just update everything while we are doing it such that the sub-PTEs are in sync.
Quoted 20 lines
Quoted 20 lines
arch/arm64/mm/contpte.c +13
-0+pteval_t entry_access =pte_val (entry) & access_mask; +int i; + +for (i =0 ; i <CONT_PTES ; i++) { +pteval_t pte_access =pte_val (__ptep_get (cont_ptep + i)) & access_mask; + +if (pte_access != entry_access) +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)@@ -399,13 +416,35 @@ int contpte_ptep_set_access_flags(struct vm_area_struct *vma,int i;
Quoted 10 lines
arch/arm64/mm/contpte.c int contpte_ptep_set_access_flags(struct vm_area_struct *vma, · line 421
Quoted 6 lines
arch/arm64/mm/contpte.c +3
-2/* -* 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 have the +* requested access flags, using raw per-PTE values rather than the +* gathered ptep_get() view. It's not just about the access flag but AF, dirty and write permission, all can be changed by this function (and only to a more permissive setting).
Thanks, updating the wording in v2.
Quoted 9 lines
arch/arm64/mm/contpte.c int contpte_ptep_set_access_flags(struct vm_area_struct *vma, · line 427
Quoted 7 lines
arch/arm64/mm/contpte.c +6
-0+* +* ptep_get() gathers AF/dirty state across the whole CONT block, +* which is correct for CPU TLB semantics: with FEAT_HAFDBS the +* hardware may set AF/dirty on any sub-PTE and the CPU TLB treats +* the gathered result as authoritative for the entire range. But an +* SMMU without HTTU (or with HA/HD disabled in CD.TCR) evaluates Or CPU equally, we don't force all CPUs in a system to support DBM.
Thanks, updating the wording in v2.
Quoted 25 lines
Quoted 12 lines
arch/arm64/mm/contpte.c +11
-0+* each descriptor individually and will keep faulting on the target +* sub-PTE if its flags haven't actually 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. arch/arm64/mm/contpte.c int contpte_ptep_set_access_flags(struct vm_area_struct *vma, · line 446
Quoted 10 lines
arch/arm64/mm/contpte.c +5
-2*/ - 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));This is fine since all should have the same
PTE_WRITEbit.
Quoted 3 lines
Anyway, nothing major, so:
Reviewed-by Catalin Marinas <[email protected]>
Thanks!