On Mon, Feb 17, 2025 at 02:07:59PM +0000, Ryan Roberts wrote: > __set_pte_complete(), set_pmd(), set_pud(), set_p4d() and set_pgd() are > 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. The assumption probably was that set_pmd/pud() are called a lot less often than set_pte() as they cover larger address ranges (whether table or leaf). > set_p4d()/set_pgd() continue to emit the barriers unconditionally. We probably missed them, should have been the same as set_pmd(). > 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). Also see commit d0b7a302d58a ("Revert "arm64: Remove unnecessary ISBs from set_{pte,pmd,pud}"") why we added back the ISBs to the pmd/pud accessors and the last paragraph on why we are ok with the spurious faults for PTEs. For user mappings, the translation fault is routed through the usual path that can handle mapping new entries, so I think we are fine. But it's worth double-checking Will's comment (unless he only referred to kernel table entries). > 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. Setting to invalid is fine indeed, handled by the TLB flushing code, hence the pmd_valid() checks. > 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. That's not entirely true for change_protection() for example or the fork() path when we make the entries read-only from writeable without BBM. We could improve these cases as well, I haven't looked in detail. ptep_modify_prot_commit() via change_pte_range() can defer the barriers to tlb_end_vma(). Something similar on the copy_present_ptes() path. > 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. It's worth clarifying Will's comment in the commit I mentioned above. > 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 > 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 > entries is an extra fault. > > I observe roughly the same number of page faults (107M) with and without > this change when compiling the kernel on Apple M2. That's microarch specific, highly dependent on timing, so you may never see a difference. > +static inline bool pmd_valid_not_user(pmd_t pmd) > +{ > + /* > + * User-space table entries always have (PXN && !UXN). All other > + * combinations indicate it's a table entry for kernel space. > + * Valid-not-user leaf entries follow the same rules as > + * pte_valid_not_user(). > + */ > + if (pmd_table(pmd)) > + return !((pmd_val(pmd) & (PMD_TABLE_PXN | PMD_TABLE_UXN)) == PMD_TABLE_PXN); > + return pte_valid_not_user(pmd_pte(pmd)); > +} With the 128-bit format I think we lost the PXN/UXNTable bits, though we have software bits if we need to. I just wonder whether it's worth the hassle of skipping some barriers for user non-leaf entries. Did you see any improvement in practice? -- Catalin