pgtable-generic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * mm/pgtable-generic.c
  4. *
  5. * Generic pgtable methods declared in linux/pgtable.h
  6. *
  7. * Copyright (C) 2010 Linus Torvalds
  8. */
  9. #include <linux/pagemap.h>
  10. #include <linux/hugetlb.h>
  11. #include <linux/pgtable.h>
  12. #include <linux/swap.h>
  13. #include <linux/swapops.h>
  14. #include <linux/mm_inline.h>
  15. #include <asm/pgalloc.h>
  16. #include <asm/tlb.h>
  17. /*
  18. * If a p?d_bad entry is found while walking page tables, report
  19. * the error, before resetting entry to p?d_none. Usually (but
  20. * very seldom) called out from the p?d_none_or_clear_bad macros.
  21. */
  22. void pgd_clear_bad(pgd_t *pgd)
  23. {
  24. pgd_ERROR(*pgd);
  25. pgd_clear(pgd);
  26. }
  27. #ifndef __PAGETABLE_P4D_FOLDED
  28. void p4d_clear_bad(p4d_t *p4d)
  29. {
  30. p4d_ERROR(*p4d);
  31. p4d_clear(p4d);
  32. }
  33. #endif
  34. #ifndef __PAGETABLE_PUD_FOLDED
  35. void pud_clear_bad(pud_t *pud)
  36. {
  37. pud_ERROR(*pud);
  38. pud_clear(pud);
  39. }
  40. #endif
  41. /*
  42. * Note that the pmd variant below can't be stub'ed out just as for p4d/pud
  43. * above. pmd folding is special and typically pmd_* macros refer to upper
  44. * level even when folded
  45. */
  46. void pmd_clear_bad(pmd_t *pmd)
  47. {
  48. pmd_ERROR(*pmd);
  49. pmd_clear(pmd);
  50. }
  51. #ifndef __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
  52. /*
  53. * Only sets the access flags (dirty, accessed), as well as write
  54. * permission. Furthermore, we know it always gets set to a "more
  55. * permissive" setting, which allows most architectures to optimize
  56. * this. We return whether the PTE actually changed, which in turn
  57. * instructs the caller to do things like update__mmu_cache. This
  58. * used to be done in the caller, but sparc needs minor faults to
  59. * force that call on sun4c so we changed this macro slightly
  60. */
  61. int ptep_set_access_flags(struct vm_area_struct *vma,
  62. unsigned long address, pte_t *ptep,
  63. pte_t entry, int dirty)
  64. {
  65. int changed = !pte_same(ptep_get(ptep), entry);
  66. if (changed) {
  67. set_pte_at(vma->vm_mm, address, ptep, entry);
  68. flush_tlb_fix_spurious_fault(vma, address, ptep);
  69. }
  70. return changed;
  71. }
  72. #endif
  73. #ifndef __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
  74. int ptep_clear_flush_young(struct vm_area_struct *vma,
  75. unsigned long address, pte_t *ptep)
  76. {
  77. int young;
  78. young = ptep_test_and_clear_young(vma, address, ptep);
  79. if (young)
  80. flush_tlb_page(vma, address);
  81. return young;
  82. }
  83. #endif
  84. #ifndef __HAVE_ARCH_PTEP_CLEAR_FLUSH
  85. pte_t ptep_clear_flush(struct vm_area_struct *vma, unsigned long address,
  86. pte_t *ptep)
  87. {
  88. struct mm_struct *mm = (vma)->vm_mm;
  89. pte_t pte;
  90. pte = ptep_get_and_clear(mm, address, ptep);
  91. if (pte_accessible(mm, pte))
  92. flush_tlb_page(vma, address);
  93. return pte;
  94. }
  95. #endif
  96. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  97. #ifndef __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS
  98. int pmdp_set_access_flags(struct vm_area_struct *vma,
  99. unsigned long address, pmd_t *pmdp,
  100. pmd_t entry, int dirty)
  101. {
  102. int changed = !pmd_same(*pmdp, entry);
  103. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  104. if (changed) {
  105. set_pmd_at(vma->vm_mm, address, pmdp, entry);
  106. flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  107. }
  108. return changed;
  109. }
  110. #endif
  111. #ifndef __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH
  112. int pmdp_clear_flush_young(struct vm_area_struct *vma,
  113. unsigned long address, pmd_t *pmdp)
  114. {
  115. int young;
  116. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  117. young = pmdp_test_and_clear_young(vma, address, pmdp);
  118. if (young)
  119. flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  120. return young;
  121. }
  122. #endif
  123. #ifndef __HAVE_ARCH_PMDP_HUGE_CLEAR_FLUSH
  124. pmd_t pmdp_huge_clear_flush(struct vm_area_struct *vma, unsigned long address,
  125. pmd_t *pmdp)
  126. {
  127. pmd_t pmd;
  128. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  129. VM_BUG_ON(pmd_present(*pmdp) && !pmd_trans_huge(*pmdp) &&
  130. !pmd_devmap(*pmdp));
  131. pmd = pmdp_huge_get_and_clear(vma->vm_mm, address, pmdp);
  132. flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  133. return pmd;
  134. }
  135. #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
  136. pud_t pudp_huge_clear_flush(struct vm_area_struct *vma, unsigned long address,
  137. pud_t *pudp)
  138. {
  139. pud_t pud;
  140. VM_BUG_ON(address & ~HPAGE_PUD_MASK);
  141. VM_BUG_ON(!pud_trans_huge(*pudp) && !pud_devmap(*pudp));
  142. pud = pudp_huge_get_and_clear(vma->vm_mm, address, pudp);
  143. flush_pud_tlb_range(vma, address, address + HPAGE_PUD_SIZE);
  144. return pud;
  145. }
  146. #endif
  147. #endif
  148. #ifndef __HAVE_ARCH_PGTABLE_DEPOSIT
  149. void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
  150. pgtable_t pgtable)
  151. {
  152. assert_spin_locked(pmd_lockptr(mm, pmdp));
  153. /* FIFO */
  154. if (!pmd_huge_pte(mm, pmdp))
  155. INIT_LIST_HEAD(&pgtable->lru);
  156. else
  157. list_add(&pgtable->lru, &pmd_huge_pte(mm, pmdp)->lru);
  158. pmd_huge_pte(mm, pmdp) = pgtable;
  159. }
  160. #endif
  161. #ifndef __HAVE_ARCH_PGTABLE_WITHDRAW
  162. /* no "address" argument so destroys page coloring of some arch */
  163. pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
  164. {
  165. pgtable_t pgtable;
  166. assert_spin_locked(pmd_lockptr(mm, pmdp));
  167. /* FIFO */
  168. pgtable = pmd_huge_pte(mm, pmdp);
  169. pmd_huge_pte(mm, pmdp) = list_first_entry_or_null(&pgtable->lru,
  170. struct page, lru);
  171. if (pmd_huge_pte(mm, pmdp))
  172. list_del(&pgtable->lru);
  173. return pgtable;
  174. }
  175. #endif
  176. #ifndef __HAVE_ARCH_PMDP_INVALIDATE
  177. pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
  178. pmd_t *pmdp)
  179. {
  180. VM_WARN_ON_ONCE(!pmd_present(*pmdp));
  181. pmd_t old = pmdp_establish(vma, address, pmdp, pmd_mkinvalid(*pmdp));
  182. flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  183. return old;
  184. }
  185. #endif
  186. #ifndef __HAVE_ARCH_PMDP_INVALIDATE_AD
  187. pmd_t pmdp_invalidate_ad(struct vm_area_struct *vma, unsigned long address,
  188. pmd_t *pmdp)
  189. {
  190. VM_WARN_ON_ONCE(!pmd_present(*pmdp));
  191. return pmdp_invalidate(vma, address, pmdp);
  192. }
  193. #endif
  194. #ifndef pmdp_collapse_flush
  195. pmd_t pmdp_collapse_flush(struct vm_area_struct *vma, unsigned long address,
  196. pmd_t *pmdp)
  197. {
  198. /*
  199. * pmd and hugepage pte format are same. So we could
  200. * use the same function.
  201. */
  202. pmd_t pmd;
  203. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  204. VM_BUG_ON(pmd_trans_huge(*pmdp));
  205. pmd = pmdp_huge_get_and_clear(vma->vm_mm, address, pmdp);
  206. /* collapse entails shooting down ptes not pmd */
  207. flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  208. return pmd;
  209. }
  210. #endif
  211. /* arch define pte_free_defer in asm/pgalloc.h for its own implementation */
  212. #ifndef pte_free_defer
  213. static void pte_free_now(struct rcu_head *head)
  214. {
  215. struct page *page;
  216. page = container_of(head, struct page, rcu_head);
  217. pte_free(NULL /* mm not passed and not used */, (pgtable_t)page);
  218. }
  219. void pte_free_defer(struct mm_struct *mm, pgtable_t pgtable)
  220. {
  221. struct page *page;
  222. page = pgtable;
  223. call_rcu(&page->rcu_head, pte_free_now);
  224. }
  225. #endif /* pte_free_defer */
  226. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  227. #if defined(CONFIG_GUP_GET_PXX_LOW_HIGH) && \
  228. (defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RCU))
  229. /*
  230. * See the comment above ptep_get_lockless() in include/linux/pgtable.h:
  231. * the barriers in pmdp_get_lockless() cannot guarantee that the value in
  232. * pmd_high actually belongs with the value in pmd_low; but holding interrupts
  233. * off blocks the TLB flush between present updates, which guarantees that a
  234. * successful __pte_offset_map() points to a page from matched halves.
  235. */
  236. static unsigned long pmdp_get_lockless_start(void)
  237. {
  238. unsigned long irqflags;
  239. local_irq_save(irqflags);
  240. return irqflags;
  241. }
  242. static void pmdp_get_lockless_end(unsigned long irqflags)
  243. {
  244. local_irq_restore(irqflags);
  245. }
  246. #else
  247. static unsigned long pmdp_get_lockless_start(void) { return 0; }
  248. static void pmdp_get_lockless_end(unsigned long irqflags) { }
  249. #endif
  250. pte_t *__pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp)
  251. {
  252. unsigned long irqflags;
  253. pmd_t pmdval;
  254. rcu_read_lock();
  255. irqflags = pmdp_get_lockless_start();
  256. pmdval = pmdp_get_lockless(pmd);
  257. pmdp_get_lockless_end(irqflags);
  258. if (pmdvalp)
  259. *pmdvalp = pmdval;
  260. if (unlikely(pmd_none(pmdval) || is_pmd_migration_entry(pmdval)))
  261. goto nomap;
  262. if (unlikely(pmd_trans_huge(pmdval) || pmd_devmap(pmdval)))
  263. goto nomap;
  264. if (unlikely(pmd_bad(pmdval))) {
  265. pmd_clear_bad(pmd);
  266. goto nomap;
  267. }
  268. return __pte_map(&pmdval, addr);
  269. nomap:
  270. rcu_read_unlock();
  271. return NULL;
  272. }
  273. pte_t *pte_offset_map_nolock(struct mm_struct *mm, pmd_t *pmd,
  274. unsigned long addr, spinlock_t **ptlp)
  275. {
  276. pmd_t pmdval;
  277. pte_t *pte;
  278. pte = __pte_offset_map(pmd, addr, &pmdval);
  279. if (likely(pte))
  280. *ptlp = pte_lockptr(mm, &pmdval);
  281. return pte;
  282. }
  283. /*
  284. * pte_offset_map_lock(mm, pmd, addr, ptlp), and its internal implementation
  285. * __pte_offset_map_lock() below, is usually called with the pmd pointer for
  286. * addr, reached by walking down the mm's pgd, p4d, pud for addr: either while
  287. * holding mmap_lock or vma lock for read or for write; or in truncate or rmap
  288. * context, while holding file's i_mmap_lock or anon_vma lock for read (or for
  289. * write). In a few cases, it may be used with pmd pointing to a pmd_t already
  290. * copied to or constructed on the stack.
  291. *
  292. * When successful, it returns the pte pointer for addr, with its page table
  293. * kmapped if necessary (when CONFIG_HIGHPTE), and locked against concurrent
  294. * modification by software, with a pointer to that spinlock in ptlp (in some
  295. * configs mm->page_table_lock, in SPLIT_PTLOCK configs a spinlock in table's
  296. * struct page). pte_unmap_unlock(pte, ptl) to unlock and unmap afterwards.
  297. *
  298. * But it is unsuccessful, returning NULL with *ptlp unchanged, if there is no
  299. * page table at *pmd: if, for example, the page table has just been removed,
  300. * or replaced by the huge pmd of a THP. (When successful, *pmd is rechecked
  301. * after acquiring the ptlock, and retried internally if it changed: so that a
  302. * page table can be safely removed or replaced by THP while holding its lock.)
  303. *
  304. * pte_offset_map(pmd, addr), and its internal helper __pte_offset_map() above,
  305. * just returns the pte pointer for addr, its page table kmapped if necessary;
  306. * or NULL if there is no page table at *pmd. It does not attempt to lock the
  307. * page table, so cannot normally be used when the page table is to be updated,
  308. * or when entries read must be stable. But it does take rcu_read_lock(): so
  309. * that even when page table is racily removed, it remains a valid though empty
  310. * and disconnected table. Until pte_unmap(pte) unmaps and rcu_read_unlock()s
  311. * afterwards.
  312. *
  313. * pte_offset_map_nolock(mm, pmd, addr, ptlp), above, is like pte_offset_map();
  314. * but when successful, it also outputs a pointer to the spinlock in ptlp - as
  315. * pte_offset_map_lock() does, but in this case without locking it. This helps
  316. * the caller to avoid a later pte_lockptr(mm, *pmd), which might by that time
  317. * act on a changed *pmd: pte_offset_map_nolock() provides the correct spinlock
  318. * pointer for the page table that it returns. In principle, the caller should
  319. * recheck *pmd once the lock is taken; in practice, no callsite needs that -
  320. * either the mmap_lock for write, or pte_same() check on contents, is enough.
  321. *
  322. * Note that free_pgtables(), used after unmapping detached vmas, or when
  323. * exiting the whole mm, does not take page table lock before freeing a page
  324. * table, and may not use RCU at all: "outsiders" like khugepaged should avoid
  325. * pte_offset_map() and co once the vma is detached from mm or mm_users is zero.
  326. */
  327. pte_t *__pte_offset_map_lock(struct mm_struct *mm, pmd_t *pmd,
  328. unsigned long addr, spinlock_t **ptlp)
  329. {
  330. spinlock_t *ptl;
  331. pmd_t pmdval;
  332. pte_t *pte;
  333. again:
  334. pte = __pte_offset_map(pmd, addr, &pmdval);
  335. if (unlikely(!pte))
  336. return pte;
  337. ptl = pte_lockptr(mm, &pmdval);
  338. spin_lock(ptl);
  339. if (likely(pmd_same(pmdval, pmdp_get_lockless(pmd)))) {
  340. *ptlp = ptl;
  341. return pte;
  342. }
  343. pte_unmap_unlock(pte, ptl);
  344. goto again;
  345. }
  346. EXPORT_SYMBOL(__pte_offset_map_lock);