pgtable.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * This file contains common routines for dealing with free of page tables
  4. * Along with common page table handling code
  5. *
  6. * Derived from arch/powerpc/mm/tlb_64.c:
  7. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  8. *
  9. * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
  10. * and Cort Dougan (PReP) (cort@cs.nmt.edu)
  11. * Copyright (C) 1996 Paul Mackerras
  12. *
  13. * Derived from "arch/i386/mm/init.c"
  14. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  15. *
  16. * Dave Engebretsen <engebret@us.ibm.com>
  17. * Rework for PPC64 port.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/gfp.h>
  21. #include <linux/mm.h>
  22. #include <linux/percpu.h>
  23. #include <linux/hardirq.h>
  24. #include <linux/page_table_check.h>
  25. #include <linux/hugetlb.h>
  26. #include <asm/tlbflush.h>
  27. #include <asm/tlb.h>
  28. #include <asm/hugetlb.h>
  29. #include <asm/pte-walk.h>
  30. #ifdef CONFIG_PPC64
  31. #define PGD_ALIGN (sizeof(pgd_t) * MAX_PTRS_PER_PGD)
  32. #else
  33. #define PGD_ALIGN PAGE_SIZE
  34. #endif
  35. pgd_t swapper_pg_dir[MAX_PTRS_PER_PGD] __section(".bss..page_aligned") __aligned(PGD_ALIGN);
  36. static inline int is_exec_fault(void)
  37. {
  38. return current->thread.regs && TRAP(current->thread.regs) == 0x400;
  39. }
  40. /* We only try to do i/d cache coherency on stuff that looks like
  41. * reasonably "normal" PTEs. We currently require a PTE to be present
  42. * and we avoid _PAGE_SPECIAL and cache inhibited pte. We also only do that
  43. * on userspace PTEs
  44. */
  45. static inline int pte_looks_normal(pte_t pte, unsigned long addr)
  46. {
  47. if (pte_present(pte) && !pte_special(pte)) {
  48. if (pte_ci(pte))
  49. return 0;
  50. if (!is_kernel_addr(addr))
  51. return 1;
  52. }
  53. return 0;
  54. }
  55. static struct folio *maybe_pte_to_folio(pte_t pte)
  56. {
  57. unsigned long pfn = pte_pfn(pte);
  58. struct page *page;
  59. if (unlikely(!pfn_valid(pfn)))
  60. return NULL;
  61. page = pfn_to_page(pfn);
  62. if (PageReserved(page))
  63. return NULL;
  64. return page_folio(page);
  65. }
  66. #ifdef CONFIG_PPC_BOOK3S
  67. /* Server-style MMU handles coherency when hashing if HW exec permission
  68. * is supposed per page (currently 64-bit only). If not, then, we always
  69. * flush the cache for valid PTEs in set_pte. Embedded CPU without HW exec
  70. * support falls into the same category.
  71. */
  72. static pte_t set_pte_filter_hash(pte_t pte, unsigned long addr)
  73. {
  74. pte = __pte(pte_val(pte) & ~_PAGE_HPTEFLAGS);
  75. if (pte_looks_normal(pte, addr) && !(cpu_has_feature(CPU_FTR_COHERENT_ICACHE) ||
  76. cpu_has_feature(CPU_FTR_NOEXECUTE))) {
  77. struct folio *folio = maybe_pte_to_folio(pte);
  78. if (!folio)
  79. return pte;
  80. if (!test_bit(PG_dcache_clean, &folio->flags.f)) {
  81. flush_dcache_icache_folio(folio);
  82. set_bit(PG_dcache_clean, &folio->flags.f);
  83. }
  84. }
  85. return pte;
  86. }
  87. #else /* CONFIG_PPC_BOOK3S */
  88. static pte_t set_pte_filter_hash(pte_t pte, unsigned long addr) { return pte; }
  89. #endif /* CONFIG_PPC_BOOK3S */
  90. /* Embedded type MMU with HW exec support. This is a bit more complicated
  91. * as we don't have two bits to spare for _PAGE_EXEC and _PAGE_HWEXEC so
  92. * instead we "filter out" the exec permission for non clean pages.
  93. *
  94. * This is also called once for the folio. So only work with folio->flags here.
  95. */
  96. static inline pte_t set_pte_filter(pte_t pte, unsigned long addr)
  97. {
  98. struct folio *folio;
  99. if (radix_enabled())
  100. return pte;
  101. if (mmu_has_feature(MMU_FTR_HPTE_TABLE))
  102. return set_pte_filter_hash(pte, addr);
  103. /* No exec permission in the first place, move on */
  104. if (!pte_exec(pte) || !pte_looks_normal(pte, addr))
  105. return pte;
  106. /* If you set _PAGE_EXEC on weird pages you're on your own */
  107. folio = maybe_pte_to_folio(pte);
  108. if (unlikely(!folio))
  109. return pte;
  110. /* If the page clean, we move on */
  111. if (test_bit(PG_dcache_clean, &folio->flags.f))
  112. return pte;
  113. /* If it's an exec fault, we flush the cache and make it clean */
  114. if (is_exec_fault()) {
  115. flush_dcache_icache_folio(folio);
  116. set_bit(PG_dcache_clean, &folio->flags.f);
  117. return pte;
  118. }
  119. /* Else, we filter out _PAGE_EXEC */
  120. return pte_exprotect(pte);
  121. }
  122. static pte_t set_access_flags_filter(pte_t pte, struct vm_area_struct *vma,
  123. int dirty)
  124. {
  125. struct folio *folio;
  126. if (IS_ENABLED(CONFIG_PPC_BOOK3S_64))
  127. return pte;
  128. if (mmu_has_feature(MMU_FTR_HPTE_TABLE))
  129. return pte;
  130. /* So here, we only care about exec faults, as we use them
  131. * to recover lost _PAGE_EXEC and perform I$/D$ coherency
  132. * if necessary. Also if _PAGE_EXEC is already set, same deal,
  133. * we just bail out
  134. */
  135. if (dirty || pte_exec(pte) || !is_exec_fault())
  136. return pte;
  137. #ifdef CONFIG_DEBUG_VM
  138. /* So this is an exec fault, _PAGE_EXEC is not set. If it was
  139. * an error we would have bailed out earlier in do_page_fault()
  140. * but let's make sure of it
  141. */
  142. if (WARN_ON(!(vma->vm_flags & VM_EXEC)))
  143. return pte;
  144. #endif /* CONFIG_DEBUG_VM */
  145. /* If you set _PAGE_EXEC on weird pages you're on your own */
  146. folio = maybe_pte_to_folio(pte);
  147. if (unlikely(!folio))
  148. goto bail;
  149. /* If the page is already clean, we move on */
  150. if (test_bit(PG_dcache_clean, &folio->flags.f))
  151. goto bail;
  152. /* Clean the page and set PG_dcache_clean */
  153. flush_dcache_icache_folio(folio);
  154. set_bit(PG_dcache_clean, &folio->flags.f);
  155. bail:
  156. return pte_mkexec(pte);
  157. }
  158. /*
  159. * set_pte stores a linux PTE into the linux page table.
  160. */
  161. void set_ptes(struct mm_struct *mm, unsigned long addr, pte_t *ptep,
  162. pte_t pte, unsigned int nr)
  163. {
  164. /* Note: mm->context.id might not yet have been assigned as
  165. * this context might not have been activated yet when this
  166. * is called. Filter the pte value and use the filtered value
  167. * to setup all the ptes in the range.
  168. */
  169. pte = set_pte_filter(pte, addr);
  170. /*
  171. * We don't need to call arch_enter/leave_lazy_mmu_mode()
  172. * because we expect set_ptes to be only be used on not present
  173. * and not hw_valid ptes. Hence there is no translation cache flush
  174. * involved that need to be batched.
  175. */
  176. page_table_check_ptes_set(mm, addr, ptep, pte, nr);
  177. for (;;) {
  178. /*
  179. * Make sure hardware valid bit is not set. We don't do
  180. * tlb flush for this update.
  181. */
  182. VM_WARN_ON(pte_hw_valid(*ptep) && !pte_protnone(*ptep));
  183. /* Perform the setting of the PTE */
  184. __set_pte_at(mm, addr, ptep, pte, 0);
  185. if (--nr == 0)
  186. break;
  187. ptep++;
  188. addr += PAGE_SIZE;
  189. pte = pte_next_pfn(pte);
  190. }
  191. }
  192. void set_pte_at_unchecked(struct mm_struct *mm, unsigned long addr,
  193. pte_t *ptep, pte_t pte)
  194. {
  195. VM_WARN_ON(pte_hw_valid(*ptep) && !pte_protnone(*ptep));
  196. pte = set_pte_filter(pte, addr);
  197. __set_pte_at(mm, addr, ptep, pte, 0);
  198. }
  199. void unmap_kernel_page(unsigned long va)
  200. {
  201. pmd_t *pmdp = pmd_off_k(va);
  202. pte_t *ptep = pte_offset_kernel(pmdp, va);
  203. pte_clear(&init_mm, va, ptep);
  204. flush_tlb_kernel_range(va, va + PAGE_SIZE);
  205. }
  206. /*
  207. * This is called when relaxing access to a PTE. It's also called in the page
  208. * fault path when we don't hit any of the major fault cases, ie, a minor
  209. * update of _PAGE_ACCESSED, _PAGE_DIRTY, etc... The generic code will have
  210. * handled those two for us, we additionally deal with missing execute
  211. * permission here on some processors
  212. */
  213. int ptep_set_access_flags(struct vm_area_struct *vma, unsigned long address,
  214. pte_t *ptep, pte_t entry, int dirty)
  215. {
  216. int changed;
  217. entry = set_access_flags_filter(entry, vma, dirty);
  218. changed = !pte_same(*(ptep), entry);
  219. if (changed) {
  220. assert_pte_locked(vma->vm_mm, address);
  221. __ptep_set_access_flags(vma, ptep, entry,
  222. address, mmu_virtual_psize);
  223. }
  224. return changed;
  225. }
  226. #ifdef CONFIG_HUGETLB_PAGE
  227. int huge_ptep_set_access_flags(struct vm_area_struct *vma,
  228. unsigned long addr, pte_t *ptep,
  229. pte_t pte, int dirty)
  230. {
  231. #ifdef HUGETLB_NEED_PRELOAD
  232. /*
  233. * The "return 1" forces a call of update_mmu_cache, which will write a
  234. * TLB entry. Without this, platforms that don't do a write of the TLB
  235. * entry in the TLB miss handler asm will fault ad infinitum.
  236. */
  237. ptep_set_access_flags(vma, addr, ptep, pte, dirty);
  238. return 1;
  239. #else
  240. int changed, psize;
  241. pte = set_access_flags_filter(pte, vma, dirty);
  242. changed = !pte_same(*(ptep), pte);
  243. if (changed) {
  244. #ifdef CONFIG_PPC_BOOK3S_64
  245. struct hstate *h = hstate_vma(vma);
  246. psize = hstate_get_psize(h);
  247. #ifdef CONFIG_DEBUG_VM
  248. assert_spin_locked(huge_pte_lockptr(h, vma->vm_mm, ptep));
  249. #endif
  250. #else
  251. /*
  252. * Not used on non book3s64 platforms.
  253. * 8xx compares it with mmu_virtual_psize to
  254. * know if it is a huge page or not.
  255. */
  256. psize = MMU_PAGE_COUNT;
  257. #endif
  258. __ptep_set_access_flags(vma, ptep, pte, addr, psize);
  259. }
  260. return changed;
  261. #endif
  262. }
  263. #if defined(CONFIG_PPC_8xx)
  264. #if defined(CONFIG_SPLIT_PTE_PTLOCKS) || defined(CONFIG_SPLIT_PMD_PTLOCKS)
  265. /* We need the same lock to protect the PMD table and the two PTE tables. */
  266. #error "8M hugetlb folios are incompatible with split page table locks"
  267. #endif
  268. static void __set_huge_pte_at(pmd_t *pmd, pte_t *ptep, pte_basic_t val)
  269. {
  270. pte_basic_t *entry = (pte_basic_t *)ptep;
  271. int num, i;
  272. /*
  273. * Make sure hardware valid bit is not set. We don't do
  274. * tlb flush for this update.
  275. */
  276. VM_WARN_ON(pte_hw_valid(*ptep) && !pte_protnone(*ptep));
  277. num = number_of_cells_per_pte(pmd, val, 1);
  278. for (i = 0; i < num; i++, entry++, val += SZ_4K)
  279. *entry = val;
  280. }
  281. void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep,
  282. pte_t pte, unsigned long sz)
  283. {
  284. pmd_t *pmdp = pmd_off(mm, addr);
  285. pte = set_pte_filter(pte, addr);
  286. if (sz == SZ_8M) { /* Flag both PMD entries as 8M and fill both page tables */
  287. *pmdp = __pmd(pmd_val(*pmdp) | _PMD_PAGE_8M);
  288. *(pmdp + 1) = __pmd(pmd_val(*(pmdp + 1)) | _PMD_PAGE_8M);
  289. __set_huge_pte_at(pmdp, pte_offset_kernel(pmdp, 0), pte_val(pte));
  290. __set_huge_pte_at(pmdp, pte_offset_kernel(pmdp + 1, 0), pte_val(pte) + SZ_4M);
  291. } else {
  292. __set_huge_pte_at(pmdp, ptep, pte_val(pte));
  293. }
  294. }
  295. #else
  296. void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep,
  297. pte_t pte, unsigned long sz)
  298. {
  299. unsigned long pdsize;
  300. int i;
  301. pte = set_pte_filter(pte, addr);
  302. /*
  303. * Make sure hardware valid bit is not set. We don't do
  304. * tlb flush for this update.
  305. */
  306. VM_WARN_ON(pte_hw_valid(*ptep) && !pte_protnone(*ptep));
  307. if (sz < PMD_SIZE)
  308. pdsize = PAGE_SIZE;
  309. else if (sz < PUD_SIZE)
  310. pdsize = PMD_SIZE;
  311. else if (sz < P4D_SIZE)
  312. pdsize = PUD_SIZE;
  313. else if (sz < PGDIR_SIZE)
  314. pdsize = P4D_SIZE;
  315. else
  316. pdsize = PGDIR_SIZE;
  317. for (i = 0; i < sz / pdsize; i++, ptep++, addr += pdsize) {
  318. __set_pte_at(mm, addr, ptep, pte, 0);
  319. pte = __pte(pte_val(pte) + ((unsigned long long)pdsize / PAGE_SIZE << PFN_PTE_SHIFT));
  320. }
  321. }
  322. #endif
  323. #endif /* CONFIG_HUGETLB_PAGE */
  324. #ifdef CONFIG_DEBUG_VM
  325. void assert_pte_locked(struct mm_struct *mm, unsigned long addr)
  326. {
  327. pgd_t *pgd;
  328. p4d_t *p4d;
  329. pud_t *pud;
  330. pmd_t *pmd;
  331. pte_t *pte;
  332. spinlock_t *ptl;
  333. if (mm == &init_mm)
  334. return;
  335. pgd = mm->pgd + pgd_index(addr);
  336. BUG_ON(pgd_none(*pgd));
  337. p4d = p4d_offset(pgd, addr);
  338. BUG_ON(p4d_none(*p4d));
  339. pud = pud_offset(p4d, addr);
  340. BUG_ON(pud_none(*pud));
  341. pmd = pmd_offset(pud, addr);
  342. /*
  343. * khugepaged to collapse normal pages to hugepage, first set
  344. * pmd to none to force page fault/gup to take mmap_lock. After
  345. * pmd is set to none, we do a pte_clear which does this assertion
  346. * so if we find pmd none, return.
  347. */
  348. if (pmd_none(*pmd))
  349. return;
  350. pte = pte_offset_map_ro_nolock(mm, pmd, addr, &ptl);
  351. BUG_ON(!pte);
  352. assert_spin_locked(ptl);
  353. pte_unmap(pte);
  354. }
  355. #endif /* CONFIG_DEBUG_VM */
  356. unsigned long vmalloc_to_phys(void *va)
  357. {
  358. unsigned long pfn = vmalloc_to_pfn(va);
  359. BUG_ON(!pfn);
  360. return __pa(pfn_to_kaddr(pfn)) + offset_in_page(va);
  361. }
  362. EXPORT_SYMBOL_GPL(vmalloc_to_phys);
  363. /*
  364. * We have 3 cases for pgds and pmds:
  365. * (1) invalid (all zeroes)
  366. * (2) pointer to next table, as normal; bottom 6 bits == 0
  367. * (3) leaf pte for huge page _PAGE_PTE set
  368. *
  369. * So long as we atomically load page table pointers we are safe against teardown,
  370. * we can follow the address down to the page and take a ref on it.
  371. * This function need to be called with interrupts disabled. We use this variant
  372. * when we have MSR[EE] = 0 but the paca->irq_soft_mask = IRQS_ENABLED
  373. */
  374. pte_t *__find_linux_pte(pgd_t *pgdir, unsigned long ea,
  375. bool *is_thp, unsigned *hpage_shift)
  376. {
  377. pgd_t *pgdp;
  378. #ifdef CONFIG_PPC64
  379. p4d_t p4d, *p4dp;
  380. pud_t pud, *pudp;
  381. #endif
  382. pmd_t pmd, *pmdp;
  383. pte_t *ret_pte;
  384. unsigned pdshift;
  385. if (hpage_shift)
  386. *hpage_shift = 0;
  387. if (is_thp)
  388. *is_thp = false;
  389. /*
  390. * Always operate on the local stack value. This make sure the
  391. * value don't get updated by a parallel THP split/collapse,
  392. * page fault or a page unmap. The return pte_t * is still not
  393. * stable. So should be checked there for above conditions.
  394. * Top level is an exception because it is folded into p4d.
  395. *
  396. * On PPC32, P4D/PUD/PMD are folded into PGD so go straight to
  397. * PMD level.
  398. */
  399. pgdp = pgdir + pgd_index(ea);
  400. #ifdef CONFIG_PPC64
  401. p4dp = p4d_offset(pgdp, ea);
  402. p4d = READ_ONCE(*p4dp);
  403. pdshift = P4D_SHIFT;
  404. if (p4d_none(p4d))
  405. return NULL;
  406. if (p4d_leaf(p4d)) {
  407. ret_pte = (pte_t *)p4dp;
  408. goto out;
  409. }
  410. /*
  411. * Even if we end up with an unmap, the pgtable will not
  412. * be freed, because we do an rcu free and here we are
  413. * irq disabled
  414. */
  415. pdshift = PUD_SHIFT;
  416. pudp = pud_offset(&p4d, ea);
  417. pud = READ_ONCE(*pudp);
  418. if (pud_none(pud))
  419. return NULL;
  420. if (pud_leaf(pud)) {
  421. ret_pte = (pte_t *)pudp;
  422. goto out;
  423. }
  424. pmdp = pmd_offset(&pud, ea);
  425. #else
  426. pmdp = pmd_offset(pud_offset(p4d_offset(pgdp, ea), ea), ea);
  427. #endif
  428. pdshift = PMD_SHIFT;
  429. pmd = READ_ONCE(*pmdp);
  430. /*
  431. * A hugepage collapse is captured by this condition, see
  432. * pmdp_collapse_flush.
  433. */
  434. if (pmd_none(pmd))
  435. return NULL;
  436. #ifdef CONFIG_PPC_BOOK3S_64
  437. /*
  438. * A hugepage split is captured by this condition, see
  439. * pmdp_invalidate.
  440. *
  441. * Huge page modification can be caught here too.
  442. */
  443. if (pmd_is_serializing(pmd))
  444. return NULL;
  445. #endif
  446. if (pmd_trans_huge(pmd)) {
  447. if (is_thp)
  448. *is_thp = true;
  449. ret_pte = (pte_t *)pmdp;
  450. goto out;
  451. }
  452. if (pmd_leaf(pmd)) {
  453. ret_pte = (pte_t *)pmdp;
  454. goto out;
  455. }
  456. return pte_offset_kernel(&pmd, ea);
  457. out:
  458. if (hpage_shift)
  459. *hpage_shift = pdshift;
  460. return ret_pte;
  461. }
  462. EXPORT_SYMBOL_GPL(__find_linux_pte);
  463. /* Note due to the way vm flags are laid out, the bits are XWR */
  464. const pgprot_t protection_map[16] = {
  465. [VM_NONE] = PAGE_NONE,
  466. [VM_READ] = PAGE_READONLY,
  467. [VM_WRITE] = PAGE_COPY,
  468. [VM_WRITE | VM_READ] = PAGE_COPY,
  469. [VM_EXEC] = PAGE_EXECONLY_X,
  470. [VM_EXEC | VM_READ] = PAGE_READONLY_X,
  471. [VM_EXEC | VM_WRITE] = PAGE_COPY_X,
  472. [VM_EXEC | VM_WRITE | VM_READ] = PAGE_COPY_X,
  473. [VM_SHARED] = PAGE_NONE,
  474. [VM_SHARED | VM_READ] = PAGE_READONLY,
  475. [VM_SHARED | VM_WRITE] = PAGE_SHARED,
  476. [VM_SHARED | VM_WRITE | VM_READ] = PAGE_SHARED,
  477. [VM_SHARED | VM_EXEC] = PAGE_EXECONLY_X,
  478. [VM_SHARED | VM_EXEC | VM_READ] = PAGE_READONLY_X,
  479. [VM_SHARED | VM_EXEC | VM_WRITE] = PAGE_SHARED_X,
  480. [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = PAGE_SHARED_X
  481. };
  482. #ifndef CONFIG_PPC_BOOK3S_64
  483. DECLARE_VM_GET_PAGE_PROT
  484. #endif