On 20/02/2025 16:54, Kevin Brodsky wrote: > On 17/02/2025 15:07, Ryan Roberts wrote: >> __set_pte_complete(), set_pmd(), set_pud(), set_p4d() and set_pgd() are > > Nit: it would be more accurate to say __set_pte() instead of > __set_pte_complete(), as it is the former that actually writes the PTE > (and then issues barriers). Yeah, fair enough. Will fix in the next version. > >> used to write entries into pgtables. And they issue barriers (currently >> dsb and isb) to ensure that the written values are observed by the table >> walker prior to any program-order-future memory access to the mapped >> location. >> >> Over the years some of these functions have received optimizations: In >> particular, commit 7f0b1bf04511 ("arm64: Fix barriers used for page >> table modifications") made it so that the barriers were only emitted for >> valid-kernel mappings for set_pte() (now __set_pte_complete()). And >> commit 0795edaf3f1f ("arm64: pgtable: Implement p[mu]d_valid() and check >> in set_p[mu]d()") made it so that set_pmd()/set_pud() only emitted the >> barriers for valid mappings. set_p4d()/set_pgd() continue to emit the >> barriers unconditionally. >> >> This is all very confusing to the casual observer; surely the rules >> should be invariant to the level? Let's change this so that every level >> consistently emits the barriers only when setting valid, non-user >> entries (both table and leaf). >> >> It seems obvious that if it is ok to elide barriers all but valid kernel >> mappings at pte level, it must also be ok to do this for leaf entries at >> other levels: If setting an entry to invalid, a tlb maintenance >> operation must surely follow to synchronise the TLB and this contains >> the required barriers. If setting a valid user mapping, the previous >> mapping must have been invalid and there must have been a TLB >> maintenance operation (complete with barriers) to honour >> break-before-make. So the worst that can happen is we take an extra >> fault (which will imply the DSB + ISB) and conclude that there is >> nothing to do. These are the arguments for doing this optimization at >> pte level and they also apply to leaf mappings at other levels. >> >> For table entries, the same arguments hold: If unsetting a table entry, >> a TLB is required and this will emit the required barriers. If setting a > > s/TLB/TLB maintenance/ > >> table entry, the previous value must have been invalid and the table >> walker must already be able to observe that. Additionally the contents >> of the pgtable being pointed to in the newly set entry must be visible >> before the entry is written and this is enforced via smp_wmb() (dmb) in >> the pgtable allocation functions and in __split_huge_pmd_locked(). But >> this last part could never have been enforced by the barriers in >> set_pXd() because they occur after updating the entry. So ultimately, >> the wost that can happen by eliding these barriers for user table > > s/wost/worst/ > > - Kevin > >> entries is an extra fault. >> >> [...] >