pgtable.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2015-2016, Aneesh Kumar K.V, IBM Corporation.
  4. */
  5. #include <linux/sched.h>
  6. #include <linux/mm_types.h>
  7. #include <linux/memblock.h>
  8. #include <linux/memremap.h>
  9. #include <linux/pkeys.h>
  10. #include <linux/debugfs.h>
  11. #include <misc/cxl-base.h>
  12. #include <asm/pgalloc.h>
  13. #include <asm/tlb.h>
  14. #include <asm/trace.h>
  15. #include <asm/powernv.h>
  16. #include <asm/firmware.h>
  17. #include <asm/ultravisor.h>
  18. #include <asm/kexec.h>
  19. #include <mm/mmu_decl.h>
  20. #include <trace/events/thp.h>
  21. #include "internal.h"
  22. struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT];
  23. EXPORT_SYMBOL_GPL(mmu_psize_defs);
  24. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  25. int mmu_vmemmap_psize = MMU_PAGE_4K;
  26. #endif
  27. unsigned long __pmd_frag_nr;
  28. EXPORT_SYMBOL(__pmd_frag_nr);
  29. unsigned long __pmd_frag_size_shift;
  30. EXPORT_SYMBOL(__pmd_frag_size_shift);
  31. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  32. /*
  33. * This is called when relaxing access to a hugepage. It's also called in the page
  34. * fault path when we don't hit any of the major fault cases, ie, a minor
  35. * update of _PAGE_ACCESSED, _PAGE_DIRTY, etc... The generic code will have
  36. * handled those two for us, we additionally deal with missing execute
  37. * permission here on some processors
  38. */
  39. int pmdp_set_access_flags(struct vm_area_struct *vma, unsigned long address,
  40. pmd_t *pmdp, pmd_t entry, int dirty)
  41. {
  42. int changed;
  43. #ifdef CONFIG_DEBUG_VM
  44. WARN_ON(!pmd_trans_huge(*pmdp) && !pmd_devmap(*pmdp));
  45. assert_spin_locked(pmd_lockptr(vma->vm_mm, pmdp));
  46. #endif
  47. changed = !pmd_same(*(pmdp), entry);
  48. if (changed) {
  49. /*
  50. * We can use MMU_PAGE_2M here, because only radix
  51. * path look at the psize.
  52. */
  53. __ptep_set_access_flags(vma, pmdp_ptep(pmdp),
  54. pmd_pte(entry), address, MMU_PAGE_2M);
  55. }
  56. return changed;
  57. }
  58. int pmdp_test_and_clear_young(struct vm_area_struct *vma,
  59. unsigned long address, pmd_t *pmdp)
  60. {
  61. return __pmdp_test_and_clear_young(vma->vm_mm, address, pmdp);
  62. }
  63. /*
  64. * set a new huge pmd. We should not be called for updating
  65. * an existing pmd entry. That should go via pmd_hugepage_update.
  66. */
  67. void set_pmd_at(struct mm_struct *mm, unsigned long addr,
  68. pmd_t *pmdp, pmd_t pmd)
  69. {
  70. #ifdef CONFIG_DEBUG_VM
  71. /*
  72. * Make sure hardware valid bit is not set. We don't do
  73. * tlb flush for this update.
  74. */
  75. WARN_ON(pte_hw_valid(pmd_pte(*pmdp)) && !pte_protnone(pmd_pte(*pmdp)));
  76. assert_spin_locked(pmd_lockptr(mm, pmdp));
  77. WARN_ON(!(pmd_large(pmd)));
  78. #endif
  79. trace_hugepage_set_pmd(addr, pmd_val(pmd));
  80. return set_pte_at(mm, addr, pmdp_ptep(pmdp), pmd_pte(pmd));
  81. }
  82. static void do_serialize(void *arg)
  83. {
  84. /* We've taken the IPI, so try to trim the mask while here */
  85. if (radix_enabled()) {
  86. struct mm_struct *mm = arg;
  87. exit_lazy_flush_tlb(mm, false);
  88. }
  89. }
  90. /*
  91. * Serialize against find_current_mm_pte which does lock-less
  92. * lookup in page tables with local interrupts disabled. For huge pages
  93. * it casts pmd_t to pte_t. Since format of pte_t is different from
  94. * pmd_t we want to prevent transit from pmd pointing to page table
  95. * to pmd pointing to huge page (and back) while interrupts are disabled.
  96. * We clear pmd to possibly replace it with page table pointer in
  97. * different code paths. So make sure we wait for the parallel
  98. * find_current_mm_pte to finish.
  99. */
  100. void serialize_against_pte_lookup(struct mm_struct *mm)
  101. {
  102. smp_mb();
  103. smp_call_function_many(mm_cpumask(mm), do_serialize, mm, 1);
  104. }
  105. /*
  106. * We use this to invalidate a pmdp entry before switching from a
  107. * hugepte to regular pmd entry.
  108. */
  109. pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
  110. pmd_t *pmdp)
  111. {
  112. unsigned long old_pmd;
  113. old_pmd = pmd_hugepage_update(vma->vm_mm, address, pmdp, _PAGE_PRESENT, _PAGE_INVALID);
  114. flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  115. return __pmd(old_pmd);
  116. }
  117. pmd_t pmdp_huge_get_and_clear_full(struct vm_area_struct *vma,
  118. unsigned long addr, pmd_t *pmdp, int full)
  119. {
  120. pmd_t pmd;
  121. VM_BUG_ON(addr & ~HPAGE_PMD_MASK);
  122. VM_BUG_ON((pmd_present(*pmdp) && !pmd_trans_huge(*pmdp) &&
  123. !pmd_devmap(*pmdp)) || !pmd_present(*pmdp));
  124. pmd = pmdp_huge_get_and_clear(vma->vm_mm, addr, pmdp);
  125. /*
  126. * if it not a fullmm flush, then we can possibly end up converting
  127. * this PMD pte entry to a regular level 0 PTE by a parallel page fault.
  128. * Make sure we flush the tlb in this case.
  129. */
  130. if (!full)
  131. flush_pmd_tlb_range(vma, addr, addr + HPAGE_PMD_SIZE);
  132. return pmd;
  133. }
  134. static pmd_t pmd_set_protbits(pmd_t pmd, pgprot_t pgprot)
  135. {
  136. return __pmd(pmd_val(pmd) | pgprot_val(pgprot));
  137. }
  138. /*
  139. * At some point we should be able to get rid of
  140. * pmd_mkhuge() and mk_huge_pmd() when we update all the
  141. * other archs to mark the pmd huge in pfn_pmd()
  142. */
  143. pmd_t pfn_pmd(unsigned long pfn, pgprot_t pgprot)
  144. {
  145. unsigned long pmdv;
  146. pmdv = (pfn << PAGE_SHIFT) & PTE_RPN_MASK;
  147. return __pmd_mkhuge(pmd_set_protbits(__pmd(pmdv), pgprot));
  148. }
  149. pmd_t mk_pmd(struct page *page, pgprot_t pgprot)
  150. {
  151. return pfn_pmd(page_to_pfn(page), pgprot);
  152. }
  153. pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot)
  154. {
  155. unsigned long pmdv;
  156. pmdv = pmd_val(pmd);
  157. pmdv &= _HPAGE_CHG_MASK;
  158. return pmd_set_protbits(__pmd(pmdv), newprot);
  159. }
  160. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  161. /* For use by kexec, called with MMU off */
  162. notrace void mmu_cleanup_all(void)
  163. {
  164. if (radix_enabled())
  165. radix__mmu_cleanup_all();
  166. else if (mmu_hash_ops.hpte_clear_all)
  167. mmu_hash_ops.hpte_clear_all();
  168. reset_sprs();
  169. }
  170. #ifdef CONFIG_MEMORY_HOTPLUG
  171. int __meminit create_section_mapping(unsigned long start, unsigned long end,
  172. int nid, pgprot_t prot)
  173. {
  174. if (radix_enabled())
  175. return radix__create_section_mapping(start, end, nid, prot);
  176. return hash__create_section_mapping(start, end, nid, prot);
  177. }
  178. int __meminit remove_section_mapping(unsigned long start, unsigned long end)
  179. {
  180. if (radix_enabled())
  181. return radix__remove_section_mapping(start, end);
  182. return hash__remove_section_mapping(start, end);
  183. }
  184. #endif /* CONFIG_MEMORY_HOTPLUG */
  185. void __init mmu_partition_table_init(void)
  186. {
  187. unsigned long patb_size = 1UL << PATB_SIZE_SHIFT;
  188. unsigned long ptcr;
  189. /* Initialize the Partition Table with no entries */
  190. partition_tb = memblock_alloc(patb_size, patb_size);
  191. if (!partition_tb)
  192. panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
  193. __func__, patb_size, patb_size);
  194. ptcr = __pa(partition_tb) | (PATB_SIZE_SHIFT - 12);
  195. set_ptcr_when_no_uv(ptcr);
  196. powernv_set_nmmu_ptcr(ptcr);
  197. }
  198. static void flush_partition(unsigned int lpid, bool radix)
  199. {
  200. if (radix) {
  201. radix__flush_all_lpid(lpid);
  202. radix__flush_all_lpid_guest(lpid);
  203. } else {
  204. asm volatile("ptesync" : : : "memory");
  205. asm volatile(PPC_TLBIE_5(%0,%1,2,0,0) : :
  206. "r" (TLBIEL_INVAL_SET_LPID), "r" (lpid));
  207. /* do we need fixup here ?*/
  208. asm volatile("eieio; tlbsync; ptesync" : : : "memory");
  209. trace_tlbie(lpid, 0, TLBIEL_INVAL_SET_LPID, lpid, 2, 0, 0);
  210. }
  211. }
  212. void mmu_partition_table_set_entry(unsigned int lpid, unsigned long dw0,
  213. unsigned long dw1, bool flush)
  214. {
  215. unsigned long old = be64_to_cpu(partition_tb[lpid].patb0);
  216. /*
  217. * When ultravisor is enabled, the partition table is stored in secure
  218. * memory and can only be accessed doing an ultravisor call. However, we
  219. * maintain a copy of the partition table in normal memory to allow Nest
  220. * MMU translations to occur (for normal VMs).
  221. *
  222. * Therefore, here we always update partition_tb, regardless of whether
  223. * we are running under an ultravisor or not.
  224. */
  225. partition_tb[lpid].patb0 = cpu_to_be64(dw0);
  226. partition_tb[lpid].patb1 = cpu_to_be64(dw1);
  227. /*
  228. * If ultravisor is enabled, we do an ultravisor call to register the
  229. * partition table entry (PATE), which also do a global flush of TLBs
  230. * and partition table caches for the lpid. Otherwise, just do the
  231. * flush. The type of flush (hash or radix) depends on what the previous
  232. * use of the partition ID was, not the new use.
  233. */
  234. if (firmware_has_feature(FW_FEATURE_ULTRAVISOR)) {
  235. uv_register_pate(lpid, dw0, dw1);
  236. pr_info("PATE registered by ultravisor: dw0 = 0x%lx, dw1 = 0x%lx\n",
  237. dw0, dw1);
  238. } else if (flush) {
  239. /*
  240. * Boot does not need to flush, because MMU is off and each
  241. * CPU does a tlbiel_all() before switching them on, which
  242. * flushes everything.
  243. */
  244. flush_partition(lpid, (old & PATB_HR));
  245. }
  246. }
  247. EXPORT_SYMBOL_GPL(mmu_partition_table_set_entry);
  248. static pmd_t *get_pmd_from_cache(struct mm_struct *mm)
  249. {
  250. void *pmd_frag, *ret;
  251. if (PMD_FRAG_NR == 1)
  252. return NULL;
  253. spin_lock(&mm->page_table_lock);
  254. ret = mm->context.pmd_frag;
  255. if (ret) {
  256. pmd_frag = ret + PMD_FRAG_SIZE;
  257. /*
  258. * If we have taken up all the fragments mark PTE page NULL
  259. */
  260. if (((unsigned long)pmd_frag & ~PAGE_MASK) == 0)
  261. pmd_frag = NULL;
  262. mm->context.pmd_frag = pmd_frag;
  263. }
  264. spin_unlock(&mm->page_table_lock);
  265. return (pmd_t *)ret;
  266. }
  267. static pmd_t *__alloc_for_pmdcache(struct mm_struct *mm)
  268. {
  269. void *ret = NULL;
  270. struct page *page;
  271. gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO;
  272. if (mm == &init_mm)
  273. gfp &= ~__GFP_ACCOUNT;
  274. page = alloc_page(gfp);
  275. if (!page)
  276. return NULL;
  277. if (!pgtable_pmd_page_ctor(page)) {
  278. __free_pages(page, 0);
  279. return NULL;
  280. }
  281. atomic_set(&page->pt_frag_refcount, 1);
  282. ret = page_address(page);
  283. /*
  284. * if we support only one fragment just return the
  285. * allocated page.
  286. */
  287. if (PMD_FRAG_NR == 1)
  288. return ret;
  289. spin_lock(&mm->page_table_lock);
  290. /*
  291. * If we find pgtable_page set, we return
  292. * the allocated page with single fragment
  293. * count.
  294. */
  295. if (likely(!mm->context.pmd_frag)) {
  296. atomic_set(&page->pt_frag_refcount, PMD_FRAG_NR);
  297. mm->context.pmd_frag = ret + PMD_FRAG_SIZE;
  298. }
  299. spin_unlock(&mm->page_table_lock);
  300. return (pmd_t *)ret;
  301. }
  302. pmd_t *pmd_fragment_alloc(struct mm_struct *mm, unsigned long vmaddr)
  303. {
  304. pmd_t *pmd;
  305. pmd = get_pmd_from_cache(mm);
  306. if (pmd)
  307. return pmd;
  308. return __alloc_for_pmdcache(mm);
  309. }
  310. void pmd_fragment_free(unsigned long *pmd)
  311. {
  312. struct page *page = virt_to_page(pmd);
  313. if (PageReserved(page))
  314. return free_reserved_page(page);
  315. BUG_ON(atomic_read(&page->pt_frag_refcount) <= 0);
  316. if (atomic_dec_and_test(&page->pt_frag_refcount)) {
  317. pgtable_pmd_page_dtor(page);
  318. __free_page(page);
  319. }
  320. }
  321. static inline void pgtable_free(void *table, int index)
  322. {
  323. switch (index) {
  324. case PTE_INDEX:
  325. pte_fragment_free(table, 0);
  326. break;
  327. case PMD_INDEX:
  328. pmd_fragment_free(table);
  329. break;
  330. case PUD_INDEX:
  331. __pud_free(table);
  332. break;
  333. #if defined(CONFIG_PPC_4K_PAGES) && defined(CONFIG_HUGETLB_PAGE)
  334. /* 16M hugepd directory at pud level */
  335. case HTLB_16M_INDEX:
  336. BUILD_BUG_ON(H_16M_CACHE_INDEX <= 0);
  337. kmem_cache_free(PGT_CACHE(H_16M_CACHE_INDEX), table);
  338. break;
  339. /* 16G hugepd directory at the pgd level */
  340. case HTLB_16G_INDEX:
  341. BUILD_BUG_ON(H_16G_CACHE_INDEX <= 0);
  342. kmem_cache_free(PGT_CACHE(H_16G_CACHE_INDEX), table);
  343. break;
  344. #endif
  345. /* We don't free pgd table via RCU callback */
  346. default:
  347. BUG();
  348. }
  349. }
  350. void pgtable_free_tlb(struct mmu_gather *tlb, void *table, int index)
  351. {
  352. unsigned long pgf = (unsigned long)table;
  353. BUG_ON(index > MAX_PGTABLE_INDEX_SIZE);
  354. pgf |= index;
  355. tlb_remove_table(tlb, (void *)pgf);
  356. }
  357. void __tlb_remove_table(void *_table)
  358. {
  359. void *table = (void *)((unsigned long)_table & ~MAX_PGTABLE_INDEX_SIZE);
  360. unsigned int index = (unsigned long)_table & MAX_PGTABLE_INDEX_SIZE;
  361. return pgtable_free(table, index);
  362. }
  363. #ifdef CONFIG_PROC_FS
  364. atomic_long_t direct_pages_count[MMU_PAGE_COUNT];
  365. void arch_report_meminfo(struct seq_file *m)
  366. {
  367. /*
  368. * Hash maps the memory with one size mmu_linear_psize.
  369. * So don't bother to print these on hash
  370. */
  371. if (!radix_enabled())
  372. return;
  373. seq_printf(m, "DirectMap4k: %8lu kB\n",
  374. atomic_long_read(&direct_pages_count[MMU_PAGE_4K]) << 2);
  375. seq_printf(m, "DirectMap64k: %8lu kB\n",
  376. atomic_long_read(&direct_pages_count[MMU_PAGE_64K]) << 6);
  377. seq_printf(m, "DirectMap2M: %8lu kB\n",
  378. atomic_long_read(&direct_pages_count[MMU_PAGE_2M]) << 11);
  379. seq_printf(m, "DirectMap1G: %8lu kB\n",
  380. atomic_long_read(&direct_pages_count[MMU_PAGE_1G]) << 20);
  381. }
  382. #endif /* CONFIG_PROC_FS */
  383. pte_t ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addr,
  384. pte_t *ptep)
  385. {
  386. unsigned long pte_val;
  387. /*
  388. * Clear the _PAGE_PRESENT so that no hardware parallel update is
  389. * possible. Also keep the pte_present true so that we don't take
  390. * wrong fault.
  391. */
  392. pte_val = pte_update(vma->vm_mm, addr, ptep, _PAGE_PRESENT, _PAGE_INVALID, 0);
  393. return __pte(pte_val);
  394. }
  395. void ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr,
  396. pte_t *ptep, pte_t old_pte, pte_t pte)
  397. {
  398. if (radix_enabled())
  399. return radix__ptep_modify_prot_commit(vma, addr,
  400. ptep, old_pte, pte);
  401. set_pte_at(vma->vm_mm, addr, ptep, pte);
  402. }
  403. /*
  404. * For hash translation mode, we use the deposited table to store hash slot
  405. * information and they are stored at PTRS_PER_PMD offset from related pmd
  406. * location. Hence a pmd move requires deposit and withdraw.
  407. *
  408. * For radix translation with split pmd ptl, we store the deposited table in the
  409. * pmd page. Hence if we have different pmd page we need to withdraw during pmd
  410. * move.
  411. *
  412. * With hash we use deposited table always irrespective of anon or not.
  413. * With radix we use deposited table only for anonymous mapping.
  414. */
  415. int pmd_move_must_withdraw(struct spinlock *new_pmd_ptl,
  416. struct spinlock *old_pmd_ptl,
  417. struct vm_area_struct *vma)
  418. {
  419. if (radix_enabled())
  420. return (new_pmd_ptl != old_pmd_ptl) && vma_is_anonymous(vma);
  421. return true;
  422. }
  423. /*
  424. * Does the CPU support tlbie?
  425. */
  426. bool tlbie_capable __read_mostly = true;
  427. EXPORT_SYMBOL(tlbie_capable);
  428. /*
  429. * Should tlbie be used for management of CPU TLBs, for kernel and process
  430. * address spaces? tlbie may still be used for nMMU accelerators, and for KVM
  431. * guest address spaces.
  432. */
  433. bool tlbie_enabled __read_mostly = true;
  434. static int __init setup_disable_tlbie(char *str)
  435. {
  436. if (!radix_enabled()) {
  437. pr_err("disable_tlbie: Unable to disable TLBIE with Hash MMU.\n");
  438. return 1;
  439. }
  440. tlbie_capable = false;
  441. tlbie_enabled = false;
  442. return 1;
  443. }
  444. __setup("disable_tlbie", setup_disable_tlbie);
  445. static int __init pgtable_debugfs_setup(void)
  446. {
  447. if (!tlbie_capable)
  448. return 0;
  449. /*
  450. * There is no locking vs tlb flushing when changing this value.
  451. * The tlb flushers will see one value or another, and use either
  452. * tlbie or tlbiel with IPIs. In both cases the TLBs will be
  453. * invalidated as expected.
  454. */
  455. debugfs_create_bool("tlbie_enabled", 0600,
  456. arch_debugfs_dir,
  457. &tlbie_enabled);
  458. return 0;
  459. }
  460. arch_initcall(pgtable_debugfs_setup);
  461. #if defined(CONFIG_ZONE_DEVICE) && defined(CONFIG_ARCH_HAS_MEMREMAP_COMPAT_ALIGN)
  462. /*
  463. * Override the generic version in mm/memremap.c.
  464. *
  465. * With hash translation, the direct-map range is mapped with just one
  466. * page size selected by htab_init_page_sizes(). Consult
  467. * mmu_psize_defs[] to determine the minimum page size alignment.
  468. */
  469. unsigned long memremap_compat_align(void)
  470. {
  471. if (!radix_enabled()) {
  472. unsigned int shift = mmu_psize_defs[mmu_linear_psize].shift;
  473. return max(SUBSECTION_SIZE, 1UL << shift);
  474. }
  475. return SUBSECTION_SIZE;
  476. }
  477. EXPORT_SYMBOL_GPL(memremap_compat_align);
  478. #endif
  479. pgprot_t vm_get_page_prot(unsigned long vm_flags)
  480. {
  481. unsigned long prot;
  482. /* Radix supports execute-only, but protection_map maps X -> RX */
  483. if (radix_enabled() && ((vm_flags & VM_ACCESS_FLAGS) == VM_EXEC)) {
  484. prot = pgprot_val(PAGE_EXECONLY);
  485. } else {
  486. prot = pgprot_val(protection_map[vm_flags &
  487. (VM_ACCESS_FLAGS | VM_SHARED)]);
  488. }
  489. if (vm_flags & VM_SAO)
  490. prot |= _PAGE_SAO;
  491. #ifdef CONFIG_PPC_MEM_KEYS
  492. prot |= vmflag_to_pte_pkey_bits(vm_flags);
  493. #endif
  494. return __pgprot(prot);
  495. }
  496. EXPORT_SYMBOL(vm_get_page_prot);