arm64: mm: Use READ_ONCE/WRITE_ONCE when accessing page tables
PATCH 1/1 arm64: mm: Use READ_ONCE/WRITE_ONCE when accessing page tables
In many cases, page tables can be accessed concurrently by either another CPU (due to things like fast gup) or by the hardware page table walker itself, which may set access/dirty bits. In such cases, it is important to use READ_ONCE/WRITE_ONCE when accessing page table entries so that entries cannot be torn, merged or subject to apparent loss of coherence due to compiler transformations.
Whilst there are some scenarios where this cannot happen (e.g. pinned kernel mappings for the linear region), the overhead of using READ_ONCE /WRITE_ONCE everywhere is minimal and makes the code an awful lot easier to reason about. This patch consistently uses these macros in the arch code, as well as explicitly namespacing pointers to page table entries from the entries themselves by using adopting a 'p' suffix for the former (as is sometimes used elsewhere in the kernel source).
--- arch/arm64/include/asm/hugetlb.h | 2 +- arch/arm64/include/asm/kvm_mmu.h | 26 ++-- arch/arm64/include/asm/mmu_context.h | 4 +- arch/arm64/include/asm/pgalloc.h | 44 +++--- arch/arm64/include/asm/pgtable.h | 23 +-- arch/arm64/kernel/efi.c | 2 +- arch/arm64/kernel/hibernate.c | 148 +++++++++--------- arch/arm64/mm/dump.c | 54 ++++--- arch/arm64/mm/fault.c | 44 +++--- arch/arm64/mm/hugetlbpage.c | 94 ++++++------ arch/arm64/mm/kasan_init.c | 70 ++++----- arch/arm64/mm/mmu.c | 282 ++++++++++++++++++----------------- arch/arm64/mm/pageattr.c | 32 ++-- 13 files changed, 426 insertions(+), 399 deletions(-)
arch/arm64/include/asm/hugetlb.h +1 -1
@@ -22,7 +22,7 @@static inline pte_t huge_ptep_get (pte_t *ptep) { -return *ptep; +return READ_ONCE (*ptep); }
arch/arm64/include/asm/kvm_mmu.h +13 -13
@@ -185,42 +185,42 @@ static inline pmd_t kvm_s2pmd_mkexec(pmd_t pmd)return pmd; } -static inline void kvm_set_s2pte_readonly (pte_t *pte) +static inline void kvm_set_s2pte_readonly (pte_t *ptep) {pteval_t old_pteval, pteval; - pteval =READ_ONCE (pte_val (*pte)); + pteval =READ_ONCE (pte_val (*ptep));do { old_pteval = pteval; pteval &= ~PTE_S2_RDWR ; pteval |=PTE_S2_RDONLY ; - pteval =cmpxchg_relaxed (&pte_val (*pte), old_pteval, pteval); + pteval =cmpxchg_relaxed (&pte_val (*ptep), old_pteval, pteval); }while (pteval != old_pteval); } -static inline bool kvm_s2pte_readonly (pte_t *pte) +static inline bool kvm_s2pte_readonly (pte_t *ptep) { -return (pte_val (*pte) &PTE_S2_RDWR ) ==PTE_S2_RDONLY ; +return (READ_ONCE (pte_val (*ptep)) &PTE_S2_RDWR ) ==PTE_S2_RDONLY ; } -static inline bool kvm_s2pte_exec (pte_t *pte) +static inline bool kvm_s2pte_exec (pte_t *ptep) { -return !(pte_val (*pte) &PTE_S2_XN ); +return !(READ_ONCE (pte_val (*ptep)) &PTE_S2_XN ); } -static inline void kvm_set_s2pmd_readonly (pmd_t *pmd) +static inline void kvm_set_s2pmd_readonly (pmd_t *pmdp) { -kvm_set_s2pte_readonly ((pte_t *)pmd); +kvm_set_s2pte_readonly ((pte_t *)pmdp); } -static inline bool kvm_s2pmd_readonly (pmd_t *pmd) +static inline bool kvm_s2pmd_readonly (pmd_t *pmdp) { -return kvm_s2pte_readonly ((pte_t *)pmd); +return kvm_s2pte_readonly ((pte_t *)pmdp); } -static inline bool kvm_s2pmd_exec (pmd_t *pmd) +static inline bool kvm_s2pmd_exec (pmd_t *pmdp) { -return !(pmd_val (*pmd) &PMD_S2_XN ); +return !(READ_ONCE (pmd_val (*pmdp)) &PMD_S2_XN ); }static inline bool kvm_page_empty (void *ptr)
arch/arm64/include/asm/mmu_context.h +2 -2
@@ -141,13 +141,13 @@ static inline void cpu_install_idmap(void)* Atomically replaces the active TTBR1_EL1 PGD with a new VA-compatible PGD, * avoiding the possibility of conflicting TLB entries being allocated. */ -static inline void cpu_replace_ttbr1 (pgd_t *pgd) +static inline void cpu_replace_ttbr1 (pgd_t *pgdp) {typedef void (ttbr_replace_func )(phys_addr_t );extern ttbr_replace_func idmap_cpu_replace_ttbr1;ttbr_replace_func *replace_phys; -phys_addr_t pgd_phys =virt_to_phys (pgd); +phys_addr_t pgd_phys =virt_to_phys (pgdp); replace_phys = (void *)__pa_symbol (idmap_cpu_replace_ttbr1);
arch/arm64/include/asm/pgalloc.h +22 -22
95 lines not on this page · raw message
arch/arm64/include/asm/pgtable.h +13 -10
@@ -218,7 +218,7 @@ static inline pmd_t pmd_mkcont(pmd_t pmd)static inline void set_pte (pte_t *ptep,pte_t pte) { - *ptep = pte; +WRITE_ONCE (*ptep, pte);/* * Only if the new pte is valid and kernel, otherwise TLB maintenance
@@ -250,6 +250,8 @@ extern void __sync_icache_dcache(pte_t pteval, unsigned long addr);static inline void set_pte_at (struct mm_struct *mm,unsigned long addr,pte_t *ptep,pte_t pte) { +pte_t old_pte; +if (pte_present (pte) &&pte_user_exec (pte) && !pte_special (pte))__sync_icache_dcache (pte, addr);
@@ -258,14 +260,15 @@ static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,* hardware updates of the pte (ptep_set_access_flags safely changes * valid ptes without going through an invalid entry). */ -if (IS_ENABLED (CONFIG_DEBUG_VM ) &&pte_valid (*ptep) &&pte_valid (pte) && + old_pte =READ_ONCE (*ptep); +if (IS_ENABLED (CONFIG_DEBUG_VM ) &&pte_valid (old_pte) &&pte_valid (pte) && (mm == current->active_mm ||atomic_read (&mm->mm_users) >1 )) {VM_WARN_ONCE (!pte_young (pte),"%s: racy access flag clearing: 0x%016llx -> 0x%016llx" , - __func__,pte_val (*ptep),pte_val (pte)); -VM_WARN_ONCE (pte_write (*ptep) && !pte_dirty (pte), + __func__,pte_val (old_pte),pte_val (pte)); +VM_WARN_ONCE (pte_write (old_pte) && !pte_dirty (pte),"%s: racy dirty state clearing: 0x%016llx -> 0x%016llx" , - __func__,pte_val (*ptep),pte_val (pte)); + __func__,pte_val (old_pte),pte_val (pte)); }set_pte (ptep, pte);
@@ -431,7 +434,7 @@ extern pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,static inline void set_pmd (pmd_t *pmdp,pmd_t pmd) { - *pmdp = pmd; +WRITE_ONCE (*pmdp, pmd);dsb (ishst);isb (); }
@@ -482,7 +485,7 @@ static inline phys_addr_t pmd_page_paddr(pmd_t pmd)static inline void set_pud (pud_t *pudp,pud_t pud) { - *pudp = pud; +WRITE_ONCE (*pudp, pud);dsb (ishst);isb (); }
@@ -500,7 +503,7 @@ static inline phys_addr_t pud_page_paddr(pud_t pud)/* Find an entry in the second-level page table. */ #define pmd_index (addr) (((addr) >> PMD_SHIFT) & (PTRS_PER_PMD - 1)) -#define pmd_offset_phys (dir, addr) (pud_page_paddr(*(dir)) + pmd_index(addr) * sizeof(pmd_t)) +#define pmd_offset_phys (dir, addr) (pud_page_paddr(READ_ONCE(*(dir))) + pmd_index(addr) * sizeof(pmd_t))#define pmd_offset (dir, addr) ((pmd_t *)__va(pmd_offset_phys((dir), (addr))))#define pmd_set_fixmap (addr) ((pmd_t *)set_fixmap_offset(FIX_PMD, addr))
@@ -535,7 +538,7 @@ static inline phys_addr_t pud_page_paddr(pud_t pud)static inline void set_pgd (pgd_t *pgdp,pgd_t pgd) { - *pgdp = pgd; +WRITE_ONCE (*pgdp, pgd);dsb (ishst); }
@@ -552,7 +555,7 @@ static inline phys_addr_t pgd_page_paddr(pgd_t pgd)/* Find an entry in the frst-level page table. */ #define pud_index (addr) (((addr) >> PUD_SHIFT) & (PTRS_PER_PUD - 1)) -#define pud_offset_phys (dir, addr) (pgd_page_paddr(*(dir)) + pud_index(addr) * sizeof(pud_t)) +#define pud_offset_phys (dir, addr) (pgd_page_paddr(READ_ONCE(*(dir))) + pud_index(addr) * sizeof(pud_t))#define pud_offset (dir, addr) ((pud_t *)__va(pud_offset_phys((dir), (addr))))#define pud_set_fixmap (addr) ((pud_t *)set_fixmap_offset(FIX_PUD, addr))
arch/arm64/kernel/efi.c +1 -1
@@ -90,7 +90,7 @@ static int __init set_permissions(pte_t *ptep, pgtable_t token, unsigned long addr, void *data) { efi_memory_desc_t *md = data; -pte_t pte = *ptep; +pte_t pte =READ_ONCE (*ptep);if (md->attribute &EFI_MEMORY_RO ) pte =set_pte_bit (pte,__pgprot (PTE_RDONLY ));
arch/arm64/kernel/hibernate.c +76 -72
259 lines not on this page · raw message
arch/arm64/mm/dump.c +30 -24
95 lines not on this page · raw message
arch/arm64/mm/fault.c +25 -19
80 lines not on this page · raw message
arch/arm64/mm/hugetlbpage.c +43 -51
162 lines not on this page · raw message
arch/arm64/mm/kasan_init.c +35 -35
141 lines not on this page · raw message
arch/arm64/mm/mmu.c +148 -134
628 lines not on this page · raw message
arch/arm64/mm/pageattr.c +17 -15
@@ -29,7 +29,7 @@ static int change_page_range(pte_t *ptep, pgtable_t token, unsigned long addr, void *data) {struct page_change_data *cdata = data; -pte_t pte = *ptep; +pte_t pte =READ_ONCE (*ptep); pte =clear_pte_bit (pte, cdata->clear_mask); pte =set_pte_bit (pte, cdata->set_mask);
@@ -156,30 +156,32 @@ void __kernel_map_pages(struct page *page, int numpages, int enable)*/ bool kernel_page_present (struct page *page) { -pgd_t *pgd; -pud_t *pud; -pmd_t *pmd; -pte_t *pte; +pgd_t *pgdp; +pud_t *pudp, pud; +pmd_t *pmdp, pmd; +pte_t *ptep;unsigned long addr = (unsigned long )page_address (page); - pgd =pgd_offset_k (addr); -if (pgd_none (*pgd)) + pgdp =pgd_offset_k (addr); +if (pgd_none (READ_ONCE (*pgdp)))return false; - pud =pud_offset (pgd, addr); -if (pud_none (*pud)) + pudp =pud_offset (pgdp, addr); + pud =READ_ONCE (*pudp); +if (pud_none (pud))return false; -if (pud_sect (*pud)) +if (pud_sect (pud))return true; - pmd =pmd_offset (pud, addr); -if (pmd_none (*pmd)) + pmdp =pmd_offset (pudp, addr); + pmd =READ_ONCE (*pmdp); +if (pmd_none (pmd))return false; -if (pmd_sect (*pmd)) +if (pmd_sect (pmd))return true; - pte =pte_offset_kernel (pmd, addr); -return pte_valid (*pte); + ptep =pte_offset_kernel (pmdp, addr); +return pte_valid (READ_ONCE (*ptep)); }#endif /* CONFIG_HIBERNATION */ #endif /* CONFIG_DEBUG_PAGEALLOC */
Signature · 1 line
2.1.4
PATCH 1/1 arm64: mm: Use READ_ONCE/WRITE_ONCE when accessing page tables
Quoted 15 lines · Will Deacon
In many cases, page tables can be accessed concurrently by either another CPU (due to things like fast gup) or by the hardware page table walker itself, which may set access/dirty bits. In such cases, it is important to use READ_ONCE/WRITE_ONCE when accessing page table entries so that entries cannot be torn, merged or subject to apparent loss of coherence due to compiler transformations.
Whilst there are some scenarios where this cannot happen (e.g. pinned kernel mappings for the linear region), the overhead of using
READ_ONCE/WRITE_ONCE everywhere is minimal and makes the code an awful lot easier to reason about. This patch consistently uses these macros in the arch code, as well as explicitly namespacing pointers to page table entries from the entries themselves by using adopting a 'p' suffix for the former (as is sometimes used elsewhere in the kernel source).
Quoted 4 lines
Cc Marc Zyngier <[email protected]>Tested-by Yury Norov <[email protected]>Tested-by Richard Ruigrok <[email protected]>Signed-off-by Will Deacon <[email protected]>
M.
Signature · 1 line
Jazz is not dead. It just smells funny...