pgtable-generic.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 <linux/iommu.h>
  16. #include <linux/pgalloc.h>
  17. #include <asm/tlb.h>
  18. /*
  19. * If a p?d_bad entry is found while walking page tables, report
  20. * the error, before resetting entry to p?d_none. Usually (but
  21. * very seldom) called out from the p?d_none_or_clear_bad macros.
  22. */
  23. void pgd_clear_bad(pgd_t *pgd)
  24. {
  25. pgd_ERROR(*pgd);
  26. pgd_clear(pgd);
  27. }
  28. #ifndef __PAGETABLE_P4D_FOLDED
  29. void p4d_clear_bad(p4d_t *p4d)
  30. {
  31. p4d_ERROR(*p4d);
  32. p4d_clear(p4d);
  33. }
  34. #endif
  35. #ifndef __PAGETABLE_PUD_FOLDED
  36. void pud_clear_bad(pud_t *pud)
  37. {
  38. pud_ERROR(*pud);
  39. pud_clear(pud);
  40. }
  41. #endif
  42. /*
  43. * Note that the pmd variant below can't be stub'ed out just as for p4d/pud
  44. * above. pmd folding is special and typically pmd_* macros refer to upper
  45. * level even when folded
  46. */
  47. void pmd_clear_bad(pmd_t *pmd)
  48. {
  49. pmd_ERROR(*pmd);
  50. pmd_clear(pmd);
  51. }
  52. #ifndef __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
  53. /*
  54. * Only sets the access flags (dirty, accessed), as well as write
  55. * permission. Furthermore, we know it always gets set to a "more
  56. * permissive" setting, which allows most architectures to optimize
  57. * this. We return whether the PTE actually changed, which in turn
  58. * instructs the caller to do things like update__mmu_cache. This
  59. * used to be done in the caller, but sparc needs minor faults to
  60. * force that call on sun4c so we changed this macro slightly
  61. */
  62. int ptep_set_access_flags(struct vm_area_struct *vma,
  63. unsigned long address, pte_t *ptep,
  64. pte_t entry, int dirty)
  65. {
  66. int changed = !pte_same(ptep_get(ptep), entry);
  67. if (changed) {
  68. set_pte_at(vma->vm_mm, address, ptep, entry);
  69. flush_tlb_fix_spurious_fault(vma, address, ptep);
  70. }
  71. return changed;
  72. }
  73. #endif
  74. #ifndef __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
  75. int ptep_clear_flush_young(struct vm_area_struct *vma,
  76. unsigned long address, pte_t *ptep)
  77. {
  78. int young;
  79. young = ptep_test_and_clear_young(vma, address, ptep);
  80. if (young)
  81. flush_tlb_page(vma, address);
  82. return young;
  83. }
  84. #endif
  85. #ifndef __HAVE_ARCH_PTEP_CLEAR_FLUSH
  86. pte_t ptep_clear_flush(struct vm_area_struct *vma, unsigned long address,
  87. pte_t *ptep)
  88. {
  89. struct mm_struct *mm = (vma)->vm_mm;
  90. pte_t pte;
  91. pte = ptep_get_and_clear(mm, address, ptep);
  92. if (pte_accessible(mm, pte))
  93. flush_tlb_page(vma, address);
  94. return pte;
  95. }
  96. #endif
  97. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  98. #ifndef __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS
  99. int pmdp_set_access_flags(struct vm_area_struct *vma,
  100. unsigned long address, pmd_t *pmdp,
  101. pmd_t entry, int dirty)
  102. {
  103. int changed = !pmd_same(*pmdp, entry);
  104. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  105. if (changed) {
  106. set_pmd_at(vma->vm_mm, address, pmdp, entry);
  107. flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  108. }
  109. return changed;
  110. }
  111. #endif
  112. #ifndef __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH
  113. int pmdp_clear_flush_young(struct vm_area_struct *vma,
  114. unsigned long address, pmd_t *pmdp)
  115. {
  116. int young;
  117. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  118. young = pmdp_test_and_clear_young(vma, address, pmdp);
  119. if (young)
  120. flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  121. return young;
  122. }
  123. #endif
  124. #ifndef __HAVE_ARCH_PMDP_HUGE_CLEAR_FLUSH
  125. pmd_t pmdp_huge_clear_flush(struct vm_area_struct *vma, unsigned long address,
  126. pmd_t *pmdp)
  127. {
  128. pmd_t pmd;
  129. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  130. VM_BUG_ON(pmd_present(*pmdp) && !pmd_trans_huge(*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));
  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) || !pmd_present(pmdval)))
  261. goto nomap;
  262. if (unlikely(pmd_trans_huge(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_ro_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. pte_t *pte_offset_map_rw_nolock(struct mm_struct *mm, pmd_t *pmd,
  284. unsigned long addr, pmd_t *pmdvalp,
  285. spinlock_t **ptlp)
  286. {
  287. pte_t *pte;
  288. VM_WARN_ON_ONCE(!pmdvalp);
  289. pte = __pte_offset_map(pmd, addr, pmdvalp);
  290. if (likely(pte))
  291. *ptlp = pte_lockptr(mm, pmdvalp);
  292. return pte;
  293. }
  294. /*
  295. * pte_offset_map_lock(mm, pmd, addr, ptlp) is usually called with the pmd
  296. * pointer for addr, reached by walking down the mm's pgd, p4d, pud for addr:
  297. * either while holding mmap_lock or vma lock for read or for write; or in
  298. * truncate or rmap context, while holding file's i_mmap_lock or anon_vma lock
  299. * for read (or for write). In a few cases, it may be used with pmd pointing to
  300. * a pmd_t already copied to or constructed on the stack.
  301. *
  302. * When successful, it returns the pte pointer for addr, with its page table
  303. * kmapped if necessary (when CONFIG_HIGHPTE), and locked against concurrent
  304. * modification by software, with a pointer to that spinlock in ptlp (in some
  305. * configs mm->page_table_lock, in SPLIT_PTLOCK configs a spinlock in table's
  306. * struct page). pte_unmap_unlock(pte, ptl) to unlock and unmap afterwards.
  307. *
  308. * But it is unsuccessful, returning NULL with *ptlp unchanged, if there is no
  309. * page table at *pmd: if, for example, the page table has just been removed,
  310. * or replaced by the huge pmd of a THP. (When successful, *pmd is rechecked
  311. * after acquiring the ptlock, and retried internally if it changed: so that a
  312. * page table can be safely removed or replaced by THP while holding its lock.)
  313. *
  314. * pte_offset_map(pmd, addr), and its internal helper __pte_offset_map() above,
  315. * just returns the pte pointer for addr, its page table kmapped if necessary;
  316. * or NULL if there is no page table at *pmd. It does not attempt to lock the
  317. * page table, so cannot normally be used when the page table is to be updated,
  318. * or when entries read must be stable. But it does take rcu_read_lock(): so
  319. * that even when page table is racily removed, it remains a valid though empty
  320. * and disconnected table. Until pte_unmap(pte) unmaps and rcu_read_unlock()s
  321. * afterwards.
  322. *
  323. * pte_offset_map_ro_nolock(mm, pmd, addr, ptlp), above, is like pte_offset_map();
  324. * but when successful, it also outputs a pointer to the spinlock in ptlp - as
  325. * pte_offset_map_lock() does, but in this case without locking it. This helps
  326. * the caller to avoid a later pte_lockptr(mm, *pmd), which might by that time
  327. * act on a changed *pmd: pte_offset_map_ro_nolock() provides the correct spinlock
  328. * pointer for the page table that it returns. Even after grabbing the spinlock,
  329. * we might be looking either at a page table that is still mapped or one that
  330. * was unmapped and is about to get freed. But for R/O access this is sufficient.
  331. * So it is only applicable for read-only cases where any modification operations
  332. * to the page table are not allowed even if the corresponding spinlock is held
  333. * afterwards.
  334. *
  335. * pte_offset_map_rw_nolock(mm, pmd, addr, pmdvalp, ptlp), above, is like
  336. * pte_offset_map_ro_nolock(); but when successful, it also outputs the pdmval.
  337. * It is applicable for may-write cases where any modification operations to the
  338. * page table may happen after the corresponding spinlock is held afterwards.
  339. * But the users should make sure the page table is stable like checking pte_same()
  340. * or checking pmd_same() by using the output pmdval before performing the write
  341. * operations.
  342. *
  343. * Note: "RO" / "RW" expresses the intended semantics, not that the *kmap* will
  344. * be read-only/read-write protected.
  345. *
  346. * Note that free_pgtables(), used after unmapping detached vmas, or when
  347. * exiting the whole mm, does not take page table lock before freeing a page
  348. * table, and may not use RCU at all: "outsiders" like khugepaged should avoid
  349. * pte_offset_map() and co once the vma is detached from mm or mm_users is zero.
  350. */
  351. pte_t *pte_offset_map_lock(struct mm_struct *mm, pmd_t *pmd,
  352. unsigned long addr, spinlock_t **ptlp)
  353. {
  354. spinlock_t *ptl;
  355. pmd_t pmdval;
  356. pte_t *pte;
  357. again:
  358. pte = __pte_offset_map(pmd, addr, &pmdval);
  359. if (unlikely(!pte))
  360. return pte;
  361. ptl = pte_lockptr(mm, &pmdval);
  362. spin_lock(ptl);
  363. if (likely(pmd_same(pmdval, pmdp_get_lockless(pmd)))) {
  364. *ptlp = ptl;
  365. return pte;
  366. }
  367. pte_unmap_unlock(pte, ptl);
  368. goto again;
  369. }
  370. #ifdef CONFIG_ASYNC_KERNEL_PGTABLE_FREE
  371. static void kernel_pgtable_work_func(struct work_struct *work);
  372. static struct {
  373. struct list_head list;
  374. /* protect above ptdesc lists */
  375. spinlock_t lock;
  376. struct work_struct work;
  377. } kernel_pgtable_work = {
  378. .list = LIST_HEAD_INIT(kernel_pgtable_work.list),
  379. .lock = __SPIN_LOCK_UNLOCKED(kernel_pgtable_work.lock),
  380. .work = __WORK_INITIALIZER(kernel_pgtable_work.work, kernel_pgtable_work_func),
  381. };
  382. static void kernel_pgtable_work_func(struct work_struct *work)
  383. {
  384. struct ptdesc *pt, *next;
  385. LIST_HEAD(page_list);
  386. spin_lock(&kernel_pgtable_work.lock);
  387. list_splice_tail_init(&kernel_pgtable_work.list, &page_list);
  388. spin_unlock(&kernel_pgtable_work.lock);
  389. iommu_sva_invalidate_kva_range(PAGE_OFFSET, TLB_FLUSH_ALL);
  390. list_for_each_entry_safe(pt, next, &page_list, pt_list)
  391. __pagetable_free(pt);
  392. }
  393. void pagetable_free_kernel(struct ptdesc *pt)
  394. {
  395. spin_lock(&kernel_pgtable_work.lock);
  396. list_add(&pt->pt_list, &kernel_pgtable_work.list);
  397. spin_unlock(&kernel_pgtable_work.lock);
  398. schedule_work(&kernel_pgtable_work.work);
  399. }
  400. #endif