162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * mm/pgtable-generic.c 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Generic pgtable methods declared in linux/pgtable.h 662306a36Sopenharmony_ci * 762306a36Sopenharmony_ci * Copyright (C) 2010 Linus Torvalds 862306a36Sopenharmony_ci */ 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci#include <linux/pagemap.h> 1162306a36Sopenharmony_ci#include <linux/hugetlb.h> 1262306a36Sopenharmony_ci#include <linux/pgtable.h> 1362306a36Sopenharmony_ci#include <linux/swap.h> 1462306a36Sopenharmony_ci#include <linux/swapops.h> 1562306a36Sopenharmony_ci#include <linux/mm_inline.h> 1662306a36Sopenharmony_ci#include <asm/pgalloc.h> 1762306a36Sopenharmony_ci#include <asm/tlb.h> 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci/* 2062306a36Sopenharmony_ci * If a p?d_bad entry is found while walking page tables, report 2162306a36Sopenharmony_ci * the error, before resetting entry to p?d_none. Usually (but 2262306a36Sopenharmony_ci * very seldom) called out from the p?d_none_or_clear_bad macros. 2362306a36Sopenharmony_ci */ 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_civoid pgd_clear_bad(pgd_t *pgd) 2662306a36Sopenharmony_ci{ 2762306a36Sopenharmony_ci pgd_ERROR(*pgd); 2862306a36Sopenharmony_ci pgd_clear(pgd); 2962306a36Sopenharmony_ci} 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ci#ifndef __PAGETABLE_P4D_FOLDED 3262306a36Sopenharmony_civoid p4d_clear_bad(p4d_t *p4d) 3362306a36Sopenharmony_ci{ 3462306a36Sopenharmony_ci p4d_ERROR(*p4d); 3562306a36Sopenharmony_ci p4d_clear(p4d); 3662306a36Sopenharmony_ci} 3762306a36Sopenharmony_ci#endif 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_ci#ifndef __PAGETABLE_PUD_FOLDED 4062306a36Sopenharmony_civoid pud_clear_bad(pud_t *pud) 4162306a36Sopenharmony_ci{ 4262306a36Sopenharmony_ci pud_ERROR(*pud); 4362306a36Sopenharmony_ci pud_clear(pud); 4462306a36Sopenharmony_ci} 4562306a36Sopenharmony_ci#endif 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci/* 4862306a36Sopenharmony_ci * Note that the pmd variant below can't be stub'ed out just as for p4d/pud 4962306a36Sopenharmony_ci * above. pmd folding is special and typically pmd_* macros refer to upper 5062306a36Sopenharmony_ci * level even when folded 5162306a36Sopenharmony_ci */ 5262306a36Sopenharmony_civoid pmd_clear_bad(pmd_t *pmd) 5362306a36Sopenharmony_ci{ 5462306a36Sopenharmony_ci pmd_ERROR(*pmd); 5562306a36Sopenharmony_ci pmd_clear(pmd); 5662306a36Sopenharmony_ci} 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ci#ifndef __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS 5962306a36Sopenharmony_ci/* 6062306a36Sopenharmony_ci * Only sets the access flags (dirty, accessed), as well as write 6162306a36Sopenharmony_ci * permission. Furthermore, we know it always gets set to a "more 6262306a36Sopenharmony_ci * permissive" setting, which allows most architectures to optimize 6362306a36Sopenharmony_ci * this. We return whether the PTE actually changed, which in turn 6462306a36Sopenharmony_ci * instructs the caller to do things like update__mmu_cache. This 6562306a36Sopenharmony_ci * used to be done in the caller, but sparc needs minor faults to 6662306a36Sopenharmony_ci * force that call on sun4c so we changed this macro slightly 6762306a36Sopenharmony_ci */ 6862306a36Sopenharmony_ciint ptep_set_access_flags(struct vm_area_struct *vma, 6962306a36Sopenharmony_ci unsigned long address, pte_t *ptep, 7062306a36Sopenharmony_ci pte_t entry, int dirty) 7162306a36Sopenharmony_ci{ 7262306a36Sopenharmony_ci int changed = !pte_same(ptep_get(ptep), entry); 7362306a36Sopenharmony_ci if (changed) { 7462306a36Sopenharmony_ci set_pte_at(vma->vm_mm, address, ptep, entry); 7562306a36Sopenharmony_ci flush_tlb_fix_spurious_fault(vma, address, ptep); 7662306a36Sopenharmony_ci } 7762306a36Sopenharmony_ci return changed; 7862306a36Sopenharmony_ci} 7962306a36Sopenharmony_ci#endif 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ci#ifndef __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH 8262306a36Sopenharmony_ciint ptep_clear_flush_young(struct vm_area_struct *vma, 8362306a36Sopenharmony_ci unsigned long address, pte_t *ptep) 8462306a36Sopenharmony_ci{ 8562306a36Sopenharmony_ci int young; 8662306a36Sopenharmony_ci young = ptep_test_and_clear_young(vma, address, ptep); 8762306a36Sopenharmony_ci if (young) 8862306a36Sopenharmony_ci flush_tlb_page(vma, address); 8962306a36Sopenharmony_ci return young; 9062306a36Sopenharmony_ci} 9162306a36Sopenharmony_ci#endif 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci#ifndef __HAVE_ARCH_PTEP_CLEAR_FLUSH 9462306a36Sopenharmony_cipte_t ptep_clear_flush(struct vm_area_struct *vma, unsigned long address, 9562306a36Sopenharmony_ci pte_t *ptep) 9662306a36Sopenharmony_ci{ 9762306a36Sopenharmony_ci struct mm_struct *mm = (vma)->vm_mm; 9862306a36Sopenharmony_ci pte_t pte; 9962306a36Sopenharmony_ci pte = ptep_get_and_clear(mm, address, ptep); 10062306a36Sopenharmony_ci if (pte_accessible(mm, pte)) 10162306a36Sopenharmony_ci flush_tlb_page(vma, address); 10262306a36Sopenharmony_ci return pte; 10362306a36Sopenharmony_ci} 10462306a36Sopenharmony_ci#endif 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ci#ifdef CONFIG_TRANSPARENT_HUGEPAGE 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ci#ifndef __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS 10962306a36Sopenharmony_ciint pmdp_set_access_flags(struct vm_area_struct *vma, 11062306a36Sopenharmony_ci unsigned long address, pmd_t *pmdp, 11162306a36Sopenharmony_ci pmd_t entry, int dirty) 11262306a36Sopenharmony_ci{ 11362306a36Sopenharmony_ci int changed = !pmd_same(*pmdp, entry); 11462306a36Sopenharmony_ci VM_BUG_ON(address & ~HPAGE_PMD_MASK); 11562306a36Sopenharmony_ci if (changed) { 11662306a36Sopenharmony_ci set_pmd_at(vma->vm_mm, address, pmdp, entry); 11762306a36Sopenharmony_ci flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE); 11862306a36Sopenharmony_ci } 11962306a36Sopenharmony_ci return changed; 12062306a36Sopenharmony_ci} 12162306a36Sopenharmony_ci#endif 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_ci#ifndef __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH 12462306a36Sopenharmony_ciint pmdp_clear_flush_young(struct vm_area_struct *vma, 12562306a36Sopenharmony_ci unsigned long address, pmd_t *pmdp) 12662306a36Sopenharmony_ci{ 12762306a36Sopenharmony_ci int young; 12862306a36Sopenharmony_ci VM_BUG_ON(address & ~HPAGE_PMD_MASK); 12962306a36Sopenharmony_ci young = pmdp_test_and_clear_young(vma, address, pmdp); 13062306a36Sopenharmony_ci if (young) 13162306a36Sopenharmony_ci flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE); 13262306a36Sopenharmony_ci return young; 13362306a36Sopenharmony_ci} 13462306a36Sopenharmony_ci#endif 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_ci#ifndef __HAVE_ARCH_PMDP_HUGE_CLEAR_FLUSH 13762306a36Sopenharmony_cipmd_t pmdp_huge_clear_flush(struct vm_area_struct *vma, unsigned long address, 13862306a36Sopenharmony_ci pmd_t *pmdp) 13962306a36Sopenharmony_ci{ 14062306a36Sopenharmony_ci pmd_t pmd; 14162306a36Sopenharmony_ci VM_BUG_ON(address & ~HPAGE_PMD_MASK); 14262306a36Sopenharmony_ci VM_BUG_ON(pmd_present(*pmdp) && !pmd_trans_huge(*pmdp) && 14362306a36Sopenharmony_ci !pmd_devmap(*pmdp)); 14462306a36Sopenharmony_ci pmd = pmdp_huge_get_and_clear(vma->vm_mm, address, pmdp); 14562306a36Sopenharmony_ci flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE); 14662306a36Sopenharmony_ci return pmd; 14762306a36Sopenharmony_ci} 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD 15062306a36Sopenharmony_cipud_t pudp_huge_clear_flush(struct vm_area_struct *vma, unsigned long address, 15162306a36Sopenharmony_ci pud_t *pudp) 15262306a36Sopenharmony_ci{ 15362306a36Sopenharmony_ci pud_t pud; 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ci VM_BUG_ON(address & ~HPAGE_PUD_MASK); 15662306a36Sopenharmony_ci VM_BUG_ON(!pud_trans_huge(*pudp) && !pud_devmap(*pudp)); 15762306a36Sopenharmony_ci pud = pudp_huge_get_and_clear(vma->vm_mm, address, pudp); 15862306a36Sopenharmony_ci flush_pud_tlb_range(vma, address, address + HPAGE_PUD_SIZE); 15962306a36Sopenharmony_ci return pud; 16062306a36Sopenharmony_ci} 16162306a36Sopenharmony_ci#endif 16262306a36Sopenharmony_ci#endif 16362306a36Sopenharmony_ci 16462306a36Sopenharmony_ci#ifndef __HAVE_ARCH_PGTABLE_DEPOSIT 16562306a36Sopenharmony_civoid pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp, 16662306a36Sopenharmony_ci pgtable_t pgtable) 16762306a36Sopenharmony_ci{ 16862306a36Sopenharmony_ci assert_spin_locked(pmd_lockptr(mm, pmdp)); 16962306a36Sopenharmony_ci 17062306a36Sopenharmony_ci /* FIFO */ 17162306a36Sopenharmony_ci if (!pmd_huge_pte(mm, pmdp)) 17262306a36Sopenharmony_ci INIT_LIST_HEAD(&pgtable->lru); 17362306a36Sopenharmony_ci else 17462306a36Sopenharmony_ci list_add(&pgtable->lru, &pmd_huge_pte(mm, pmdp)->lru); 17562306a36Sopenharmony_ci pmd_huge_pte(mm, pmdp) = pgtable; 17662306a36Sopenharmony_ci} 17762306a36Sopenharmony_ci#endif 17862306a36Sopenharmony_ci 17962306a36Sopenharmony_ci#ifndef __HAVE_ARCH_PGTABLE_WITHDRAW 18062306a36Sopenharmony_ci/* no "address" argument so destroys page coloring of some arch */ 18162306a36Sopenharmony_cipgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp) 18262306a36Sopenharmony_ci{ 18362306a36Sopenharmony_ci pgtable_t pgtable; 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_ci assert_spin_locked(pmd_lockptr(mm, pmdp)); 18662306a36Sopenharmony_ci 18762306a36Sopenharmony_ci /* FIFO */ 18862306a36Sopenharmony_ci pgtable = pmd_huge_pte(mm, pmdp); 18962306a36Sopenharmony_ci pmd_huge_pte(mm, pmdp) = list_first_entry_or_null(&pgtable->lru, 19062306a36Sopenharmony_ci struct page, lru); 19162306a36Sopenharmony_ci if (pmd_huge_pte(mm, pmdp)) 19262306a36Sopenharmony_ci list_del(&pgtable->lru); 19362306a36Sopenharmony_ci return pgtable; 19462306a36Sopenharmony_ci} 19562306a36Sopenharmony_ci#endif 19662306a36Sopenharmony_ci 19762306a36Sopenharmony_ci#ifndef __HAVE_ARCH_PMDP_INVALIDATE 19862306a36Sopenharmony_cipmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address, 19962306a36Sopenharmony_ci pmd_t *pmdp) 20062306a36Sopenharmony_ci{ 20162306a36Sopenharmony_ci pmd_t old = pmdp_establish(vma, address, pmdp, pmd_mkinvalid(*pmdp)); 20262306a36Sopenharmony_ci flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE); 20362306a36Sopenharmony_ci return old; 20462306a36Sopenharmony_ci} 20562306a36Sopenharmony_ci#endif 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_ci#ifndef __HAVE_ARCH_PMDP_INVALIDATE_AD 20862306a36Sopenharmony_cipmd_t pmdp_invalidate_ad(struct vm_area_struct *vma, unsigned long address, 20962306a36Sopenharmony_ci pmd_t *pmdp) 21062306a36Sopenharmony_ci{ 21162306a36Sopenharmony_ci return pmdp_invalidate(vma, address, pmdp); 21262306a36Sopenharmony_ci} 21362306a36Sopenharmony_ci#endif 21462306a36Sopenharmony_ci 21562306a36Sopenharmony_ci#ifndef pmdp_collapse_flush 21662306a36Sopenharmony_cipmd_t pmdp_collapse_flush(struct vm_area_struct *vma, unsigned long address, 21762306a36Sopenharmony_ci pmd_t *pmdp) 21862306a36Sopenharmony_ci{ 21962306a36Sopenharmony_ci /* 22062306a36Sopenharmony_ci * pmd and hugepage pte format are same. So we could 22162306a36Sopenharmony_ci * use the same function. 22262306a36Sopenharmony_ci */ 22362306a36Sopenharmony_ci pmd_t pmd; 22462306a36Sopenharmony_ci 22562306a36Sopenharmony_ci VM_BUG_ON(address & ~HPAGE_PMD_MASK); 22662306a36Sopenharmony_ci VM_BUG_ON(pmd_trans_huge(*pmdp)); 22762306a36Sopenharmony_ci pmd = pmdp_huge_get_and_clear(vma->vm_mm, address, pmdp); 22862306a36Sopenharmony_ci 22962306a36Sopenharmony_ci /* collapse entails shooting down ptes not pmd */ 23062306a36Sopenharmony_ci flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE); 23162306a36Sopenharmony_ci return pmd; 23262306a36Sopenharmony_ci} 23362306a36Sopenharmony_ci#endif 23462306a36Sopenharmony_ci 23562306a36Sopenharmony_ci/* arch define pte_free_defer in asm/pgalloc.h for its own implementation */ 23662306a36Sopenharmony_ci#ifndef pte_free_defer 23762306a36Sopenharmony_cistatic void pte_free_now(struct rcu_head *head) 23862306a36Sopenharmony_ci{ 23962306a36Sopenharmony_ci struct page *page; 24062306a36Sopenharmony_ci 24162306a36Sopenharmony_ci page = container_of(head, struct page, rcu_head); 24262306a36Sopenharmony_ci pte_free(NULL /* mm not passed and not used */, (pgtable_t)page); 24362306a36Sopenharmony_ci} 24462306a36Sopenharmony_ci 24562306a36Sopenharmony_civoid pte_free_defer(struct mm_struct *mm, pgtable_t pgtable) 24662306a36Sopenharmony_ci{ 24762306a36Sopenharmony_ci struct page *page; 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_ci page = pgtable; 25062306a36Sopenharmony_ci call_rcu(&page->rcu_head, pte_free_now); 25162306a36Sopenharmony_ci} 25262306a36Sopenharmony_ci#endif /* pte_free_defer */ 25362306a36Sopenharmony_ci#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 25462306a36Sopenharmony_ci 25562306a36Sopenharmony_ci#if defined(CONFIG_GUP_GET_PXX_LOW_HIGH) && \ 25662306a36Sopenharmony_ci (defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RCU)) 25762306a36Sopenharmony_ci/* 25862306a36Sopenharmony_ci * See the comment above ptep_get_lockless() in include/linux/pgtable.h: 25962306a36Sopenharmony_ci * the barriers in pmdp_get_lockless() cannot guarantee that the value in 26062306a36Sopenharmony_ci * pmd_high actually belongs with the value in pmd_low; but holding interrupts 26162306a36Sopenharmony_ci * off blocks the TLB flush between present updates, which guarantees that a 26262306a36Sopenharmony_ci * successful __pte_offset_map() points to a page from matched halves. 26362306a36Sopenharmony_ci */ 26462306a36Sopenharmony_cistatic unsigned long pmdp_get_lockless_start(void) 26562306a36Sopenharmony_ci{ 26662306a36Sopenharmony_ci unsigned long irqflags; 26762306a36Sopenharmony_ci 26862306a36Sopenharmony_ci local_irq_save(irqflags); 26962306a36Sopenharmony_ci return irqflags; 27062306a36Sopenharmony_ci} 27162306a36Sopenharmony_cistatic void pmdp_get_lockless_end(unsigned long irqflags) 27262306a36Sopenharmony_ci{ 27362306a36Sopenharmony_ci local_irq_restore(irqflags); 27462306a36Sopenharmony_ci} 27562306a36Sopenharmony_ci#else 27662306a36Sopenharmony_cistatic unsigned long pmdp_get_lockless_start(void) { return 0; } 27762306a36Sopenharmony_cistatic void pmdp_get_lockless_end(unsigned long irqflags) { } 27862306a36Sopenharmony_ci#endif 27962306a36Sopenharmony_ci 28062306a36Sopenharmony_cipte_t *__pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp) 28162306a36Sopenharmony_ci{ 28262306a36Sopenharmony_ci unsigned long irqflags; 28362306a36Sopenharmony_ci pmd_t pmdval; 28462306a36Sopenharmony_ci 28562306a36Sopenharmony_ci rcu_read_lock(); 28662306a36Sopenharmony_ci irqflags = pmdp_get_lockless_start(); 28762306a36Sopenharmony_ci pmdval = pmdp_get_lockless(pmd); 28862306a36Sopenharmony_ci pmdp_get_lockless_end(irqflags); 28962306a36Sopenharmony_ci 29062306a36Sopenharmony_ci if (pmdvalp) 29162306a36Sopenharmony_ci *pmdvalp = pmdval; 29262306a36Sopenharmony_ci if (unlikely(pmd_none(pmdval) || is_pmd_migration_entry(pmdval))) 29362306a36Sopenharmony_ci goto nomap; 29462306a36Sopenharmony_ci if (unlikely(pmd_trans_huge(pmdval) || pmd_devmap(pmdval))) 29562306a36Sopenharmony_ci goto nomap; 29662306a36Sopenharmony_ci if (unlikely(pmd_bad(pmdval))) { 29762306a36Sopenharmony_ci pmd_clear_bad(pmd); 29862306a36Sopenharmony_ci goto nomap; 29962306a36Sopenharmony_ci } 30062306a36Sopenharmony_ci return __pte_map(&pmdval, addr); 30162306a36Sopenharmony_cinomap: 30262306a36Sopenharmony_ci rcu_read_unlock(); 30362306a36Sopenharmony_ci return NULL; 30462306a36Sopenharmony_ci} 30562306a36Sopenharmony_ci 30662306a36Sopenharmony_cipte_t *pte_offset_map_nolock(struct mm_struct *mm, pmd_t *pmd, 30762306a36Sopenharmony_ci unsigned long addr, spinlock_t **ptlp) 30862306a36Sopenharmony_ci{ 30962306a36Sopenharmony_ci pmd_t pmdval; 31062306a36Sopenharmony_ci pte_t *pte; 31162306a36Sopenharmony_ci 31262306a36Sopenharmony_ci pte = __pte_offset_map(pmd, addr, &pmdval); 31362306a36Sopenharmony_ci if (likely(pte)) 31462306a36Sopenharmony_ci *ptlp = pte_lockptr(mm, &pmdval); 31562306a36Sopenharmony_ci return pte; 31662306a36Sopenharmony_ci} 31762306a36Sopenharmony_ci 31862306a36Sopenharmony_ci/* 31962306a36Sopenharmony_ci * pte_offset_map_lock(mm, pmd, addr, ptlp), and its internal implementation 32062306a36Sopenharmony_ci * __pte_offset_map_lock() below, is usually called with the pmd pointer for 32162306a36Sopenharmony_ci * addr, reached by walking down the mm's pgd, p4d, pud for addr: either while 32262306a36Sopenharmony_ci * holding mmap_lock or vma lock for read or for write; or in truncate or rmap 32362306a36Sopenharmony_ci * context, while holding file's i_mmap_lock or anon_vma lock for read (or for 32462306a36Sopenharmony_ci * write). In a few cases, it may be used with pmd pointing to a pmd_t already 32562306a36Sopenharmony_ci * copied to or constructed on the stack. 32662306a36Sopenharmony_ci * 32762306a36Sopenharmony_ci * When successful, it returns the pte pointer for addr, with its page table 32862306a36Sopenharmony_ci * kmapped if necessary (when CONFIG_HIGHPTE), and locked against concurrent 32962306a36Sopenharmony_ci * modification by software, with a pointer to that spinlock in ptlp (in some 33062306a36Sopenharmony_ci * configs mm->page_table_lock, in SPLIT_PTLOCK configs a spinlock in table's 33162306a36Sopenharmony_ci * struct page). pte_unmap_unlock(pte, ptl) to unlock and unmap afterwards. 33262306a36Sopenharmony_ci * 33362306a36Sopenharmony_ci * But it is unsuccessful, returning NULL with *ptlp unchanged, if there is no 33462306a36Sopenharmony_ci * page table at *pmd: if, for example, the page table has just been removed, 33562306a36Sopenharmony_ci * or replaced by the huge pmd of a THP. (When successful, *pmd is rechecked 33662306a36Sopenharmony_ci * after acquiring the ptlock, and retried internally if it changed: so that a 33762306a36Sopenharmony_ci * page table can be safely removed or replaced by THP while holding its lock.) 33862306a36Sopenharmony_ci * 33962306a36Sopenharmony_ci * pte_offset_map(pmd, addr), and its internal helper __pte_offset_map() above, 34062306a36Sopenharmony_ci * just returns the pte pointer for addr, its page table kmapped if necessary; 34162306a36Sopenharmony_ci * or NULL if there is no page table at *pmd. It does not attempt to lock the 34262306a36Sopenharmony_ci * page table, so cannot normally be used when the page table is to be updated, 34362306a36Sopenharmony_ci * or when entries read must be stable. But it does take rcu_read_lock(): so 34462306a36Sopenharmony_ci * that even when page table is racily removed, it remains a valid though empty 34562306a36Sopenharmony_ci * and disconnected table. Until pte_unmap(pte) unmaps and rcu_read_unlock()s 34662306a36Sopenharmony_ci * afterwards. 34762306a36Sopenharmony_ci * 34862306a36Sopenharmony_ci * pte_offset_map_nolock(mm, pmd, addr, ptlp), above, is like pte_offset_map(); 34962306a36Sopenharmony_ci * but when successful, it also outputs a pointer to the spinlock in ptlp - as 35062306a36Sopenharmony_ci * pte_offset_map_lock() does, but in this case without locking it. This helps 35162306a36Sopenharmony_ci * the caller to avoid a later pte_lockptr(mm, *pmd), which might by that time 35262306a36Sopenharmony_ci * act on a changed *pmd: pte_offset_map_nolock() provides the correct spinlock 35362306a36Sopenharmony_ci * pointer for the page table that it returns. In principle, the caller should 35462306a36Sopenharmony_ci * recheck *pmd once the lock is taken; in practice, no callsite needs that - 35562306a36Sopenharmony_ci * either the mmap_lock for write, or pte_same() check on contents, is enough. 35662306a36Sopenharmony_ci * 35762306a36Sopenharmony_ci * Note that free_pgtables(), used after unmapping detached vmas, or when 35862306a36Sopenharmony_ci * exiting the whole mm, does not take page table lock before freeing a page 35962306a36Sopenharmony_ci * table, and may not use RCU at all: "outsiders" like khugepaged should avoid 36062306a36Sopenharmony_ci * pte_offset_map() and co once the vma is detached from mm or mm_users is zero. 36162306a36Sopenharmony_ci */ 36262306a36Sopenharmony_cipte_t *__pte_offset_map_lock(struct mm_struct *mm, pmd_t *pmd, 36362306a36Sopenharmony_ci unsigned long addr, spinlock_t **ptlp) 36462306a36Sopenharmony_ci{ 36562306a36Sopenharmony_ci spinlock_t *ptl; 36662306a36Sopenharmony_ci pmd_t pmdval; 36762306a36Sopenharmony_ci pte_t *pte; 36862306a36Sopenharmony_ciagain: 36962306a36Sopenharmony_ci pte = __pte_offset_map(pmd, addr, &pmdval); 37062306a36Sopenharmony_ci if (unlikely(!pte)) 37162306a36Sopenharmony_ci return pte; 37262306a36Sopenharmony_ci ptl = pte_lockptr(mm, &pmdval); 37362306a36Sopenharmony_ci spin_lock(ptl); 37462306a36Sopenharmony_ci if (likely(pmd_same(pmdval, pmdp_get_lockless(pmd)))) { 37562306a36Sopenharmony_ci *ptlp = ptl; 37662306a36Sopenharmony_ci return pte; 37762306a36Sopenharmony_ci } 37862306a36Sopenharmony_ci pte_unmap_unlock(pte, ptl); 37962306a36Sopenharmony_ci goto again; 38062306a36Sopenharmony_ci} 381