pgtable-book3s64.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. * Copyright 2015-2016, Aneesh Kumar K.V, IBM Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/mm_types.h>
  11. #include <linux/memblock.h>
  12. #include <misc/cxl-base.h>
  13. #include <asm/pgalloc.h>
  14. #include <asm/tlb.h>
  15. #include <asm/trace.h>
  16. #include <asm/powernv.h>
  17. #include "mmu_decl.h"
  18. #include <trace/events/thp.h>
  19. unsigned long __pmd_frag_nr;
  20. EXPORT_SYMBOL(__pmd_frag_nr);
  21. unsigned long __pmd_frag_size_shift;
  22. EXPORT_SYMBOL(__pmd_frag_size_shift);
  23. int (*register_process_table)(unsigned long base, unsigned long page_size,
  24. unsigned long tbl_size);
  25. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  26. /*
  27. * This is called when relaxing access to a hugepage. It's also called in the page
  28. * fault path when we don't hit any of the major fault cases, ie, a minor
  29. * update of _PAGE_ACCESSED, _PAGE_DIRTY, etc... The generic code will have
  30. * handled those two for us, we additionally deal with missing execute
  31. * permission here on some processors
  32. */
  33. int pmdp_set_access_flags(struct vm_area_struct *vma, unsigned long address,
  34. pmd_t *pmdp, pmd_t entry, int dirty)
  35. {
  36. int changed;
  37. #ifdef CONFIG_DEBUG_VM
  38. WARN_ON(!pmd_trans_huge(*pmdp) && !pmd_devmap(*pmdp));
  39. assert_spin_locked(pmd_lockptr(vma->vm_mm, pmdp));
  40. #endif
  41. changed = !pmd_same(*(pmdp), entry);
  42. if (changed) {
  43. /*
  44. * We can use MMU_PAGE_2M here, because only radix
  45. * path look at the psize.
  46. */
  47. __ptep_set_access_flags(vma, pmdp_ptep(pmdp),
  48. pmd_pte(entry), address, MMU_PAGE_2M);
  49. }
  50. return changed;
  51. }
  52. int pmdp_test_and_clear_young(struct vm_area_struct *vma,
  53. unsigned long address, pmd_t *pmdp)
  54. {
  55. return __pmdp_test_and_clear_young(vma->vm_mm, address, pmdp);
  56. }
  57. /*
  58. * set a new huge pmd. We should not be called for updating
  59. * an existing pmd entry. That should go via pmd_hugepage_update.
  60. */
  61. void set_pmd_at(struct mm_struct *mm, unsigned long addr,
  62. pmd_t *pmdp, pmd_t pmd)
  63. {
  64. #ifdef CONFIG_DEBUG_VM
  65. /*
  66. * Make sure hardware valid bit is not set. We don't do
  67. * tlb flush for this update.
  68. */
  69. WARN_ON(pte_hw_valid(pmd_pte(*pmdp)) && !pte_protnone(pmd_pte(*pmdp)));
  70. assert_spin_locked(pmd_lockptr(mm, pmdp));
  71. WARN_ON(!(pmd_large(pmd) || pmd_devmap(pmd)));
  72. #endif
  73. trace_hugepage_set_pmd(addr, pmd_val(pmd));
  74. return set_pte_at(mm, addr, pmdp_ptep(pmdp), pmd_pte(pmd));
  75. }
  76. static void do_nothing(void *unused)
  77. {
  78. }
  79. /*
  80. * Serialize against find_current_mm_pte which does lock-less
  81. * lookup in page tables with local interrupts disabled. For huge pages
  82. * it casts pmd_t to pte_t. Since format of pte_t is different from
  83. * pmd_t we want to prevent transit from pmd pointing to page table
  84. * to pmd pointing to huge page (and back) while interrupts are disabled.
  85. * We clear pmd to possibly replace it with page table pointer in
  86. * different code paths. So make sure we wait for the parallel
  87. * find_current_mm_pte to finish.
  88. */
  89. void serialize_against_pte_lookup(struct mm_struct *mm)
  90. {
  91. smp_mb();
  92. smp_call_function_many(mm_cpumask(mm), do_nothing, NULL, 1);
  93. }
  94. /*
  95. * We use this to invalidate a pmdp entry before switching from a
  96. * hugepte to regular pmd entry.
  97. */
  98. pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
  99. pmd_t *pmdp)
  100. {
  101. unsigned long old_pmd;
  102. old_pmd = pmd_hugepage_update(vma->vm_mm, address, pmdp, _PAGE_PRESENT, _PAGE_INVALID);
  103. flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  104. /*
  105. * This ensures that generic code that rely on IRQ disabling
  106. * to prevent a parallel THP split work as expected.
  107. */
  108. serialize_against_pte_lookup(vma->vm_mm);
  109. return __pmd(old_pmd);
  110. }
  111. static pmd_t pmd_set_protbits(pmd_t pmd, pgprot_t pgprot)
  112. {
  113. return __pmd(pmd_val(pmd) | pgprot_val(pgprot));
  114. }
  115. pmd_t pfn_pmd(unsigned long pfn, pgprot_t pgprot)
  116. {
  117. unsigned long pmdv;
  118. pmdv = (pfn << PAGE_SHIFT) & PTE_RPN_MASK;
  119. return pmd_set_protbits(__pmd(pmdv), pgprot);
  120. }
  121. pmd_t mk_pmd(struct page *page, pgprot_t pgprot)
  122. {
  123. return pfn_pmd(page_to_pfn(page), pgprot);
  124. }
  125. pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot)
  126. {
  127. unsigned long pmdv;
  128. pmdv = pmd_val(pmd);
  129. pmdv &= _HPAGE_CHG_MASK;
  130. return pmd_set_protbits(__pmd(pmdv), newprot);
  131. }
  132. /*
  133. * This is called at the end of handling a user page fault, when the
  134. * fault has been handled by updating a HUGE PMD entry in the linux page tables.
  135. * We use it to preload an HPTE into the hash table corresponding to
  136. * the updated linux HUGE PMD entry.
  137. */
  138. void update_mmu_cache_pmd(struct vm_area_struct *vma, unsigned long addr,
  139. pmd_t *pmd)
  140. {
  141. if (radix_enabled())
  142. prefetch((void *)addr);
  143. }
  144. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  145. /* For use by kexec */
  146. void mmu_cleanup_all(void)
  147. {
  148. if (radix_enabled())
  149. radix__mmu_cleanup_all();
  150. else if (mmu_hash_ops.hpte_clear_all)
  151. mmu_hash_ops.hpte_clear_all();
  152. }
  153. #ifdef CONFIG_MEMORY_HOTPLUG
  154. int __meminit create_section_mapping(unsigned long start, unsigned long end, int nid)
  155. {
  156. if (radix_enabled())
  157. return radix__create_section_mapping(start, end, nid);
  158. return hash__create_section_mapping(start, end, nid);
  159. }
  160. int __meminit remove_section_mapping(unsigned long start, unsigned long end)
  161. {
  162. if (radix_enabled())
  163. return radix__remove_section_mapping(start, end);
  164. return hash__remove_section_mapping(start, end);
  165. }
  166. #endif /* CONFIG_MEMORY_HOTPLUG */
  167. void __init mmu_partition_table_init(void)
  168. {
  169. unsigned long patb_size = 1UL << PATB_SIZE_SHIFT;
  170. unsigned long ptcr;
  171. BUILD_BUG_ON_MSG((PATB_SIZE_SHIFT > 36), "Partition table size too large.");
  172. partition_tb = __va(memblock_alloc_base(patb_size, patb_size,
  173. MEMBLOCK_ALLOC_ANYWHERE));
  174. /* Initialize the Partition Table with no entries */
  175. memset((void *)partition_tb, 0, patb_size);
  176. /*
  177. * update partition table control register,
  178. * 64 K size.
  179. */
  180. ptcr = __pa(partition_tb) | (PATB_SIZE_SHIFT - 12);
  181. mtspr(SPRN_PTCR, ptcr);
  182. powernv_set_nmmu_ptcr(ptcr);
  183. }
  184. void mmu_partition_table_set_entry(unsigned int lpid, unsigned long dw0,
  185. unsigned long dw1)
  186. {
  187. unsigned long old = be64_to_cpu(partition_tb[lpid].patb0);
  188. partition_tb[lpid].patb0 = cpu_to_be64(dw0);
  189. partition_tb[lpid].patb1 = cpu_to_be64(dw1);
  190. /*
  191. * Global flush of TLBs and partition table caches for this lpid.
  192. * The type of flush (hash or radix) depends on what the previous
  193. * use of this partition ID was, not the new use.
  194. */
  195. asm volatile("ptesync" : : : "memory");
  196. if (old & PATB_HR) {
  197. asm volatile(PPC_TLBIE_5(%0,%1,2,0,1) : :
  198. "r" (TLBIEL_INVAL_SET_LPID), "r" (lpid));
  199. asm volatile(PPC_TLBIE_5(%0,%1,2,1,1) : :
  200. "r" (TLBIEL_INVAL_SET_LPID), "r" (lpid));
  201. trace_tlbie(lpid, 0, TLBIEL_INVAL_SET_LPID, lpid, 2, 0, 1);
  202. } else {
  203. asm volatile(PPC_TLBIE_5(%0,%1,2,0,0) : :
  204. "r" (TLBIEL_INVAL_SET_LPID), "r" (lpid));
  205. trace_tlbie(lpid, 0, TLBIEL_INVAL_SET_LPID, lpid, 2, 0, 0);
  206. }
  207. /* do we need fixup here ?*/
  208. asm volatile("eieio; tlbsync; ptesync" : : : "memory");
  209. }
  210. EXPORT_SYMBOL_GPL(mmu_partition_table_set_entry);
  211. static pmd_t *get_pmd_from_cache(struct mm_struct *mm)
  212. {
  213. void *pmd_frag, *ret;
  214. spin_lock(&mm->page_table_lock);
  215. ret = mm->context.pmd_frag;
  216. if (ret) {
  217. pmd_frag = ret + PMD_FRAG_SIZE;
  218. /*
  219. * If we have taken up all the fragments mark PTE page NULL
  220. */
  221. if (((unsigned long)pmd_frag & ~PAGE_MASK) == 0)
  222. pmd_frag = NULL;
  223. mm->context.pmd_frag = pmd_frag;
  224. }
  225. spin_unlock(&mm->page_table_lock);
  226. return (pmd_t *)ret;
  227. }
  228. static pmd_t *__alloc_for_pmdcache(struct mm_struct *mm)
  229. {
  230. void *ret = NULL;
  231. struct page *page;
  232. gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO;
  233. if (mm == &init_mm)
  234. gfp &= ~__GFP_ACCOUNT;
  235. page = alloc_page(gfp);
  236. if (!page)
  237. return NULL;
  238. if (!pgtable_pmd_page_ctor(page)) {
  239. __free_pages(page, 0);
  240. return NULL;
  241. }
  242. atomic_set(&page->pt_frag_refcount, 1);
  243. ret = page_address(page);
  244. /*
  245. * if we support only one fragment just return the
  246. * allocated page.
  247. */
  248. if (PMD_FRAG_NR == 1)
  249. return ret;
  250. spin_lock(&mm->page_table_lock);
  251. /*
  252. * If we find pgtable_page set, we return
  253. * the allocated page with single fragement
  254. * count.
  255. */
  256. if (likely(!mm->context.pmd_frag)) {
  257. atomic_set(&page->pt_frag_refcount, PMD_FRAG_NR);
  258. mm->context.pmd_frag = ret + PMD_FRAG_SIZE;
  259. }
  260. spin_unlock(&mm->page_table_lock);
  261. return (pmd_t *)ret;
  262. }
  263. pmd_t *pmd_fragment_alloc(struct mm_struct *mm, unsigned long vmaddr)
  264. {
  265. pmd_t *pmd;
  266. pmd = get_pmd_from_cache(mm);
  267. if (pmd)
  268. return pmd;
  269. return __alloc_for_pmdcache(mm);
  270. }
  271. void pmd_fragment_free(unsigned long *pmd)
  272. {
  273. struct page *page = virt_to_page(pmd);
  274. BUG_ON(atomic_read(&page->pt_frag_refcount) <= 0);
  275. if (atomic_dec_and_test(&page->pt_frag_refcount)) {
  276. pgtable_pmd_page_dtor(page);
  277. __free_page(page);
  278. }
  279. }
  280. static pte_t *get_pte_from_cache(struct mm_struct *mm)
  281. {
  282. void *pte_frag, *ret;
  283. spin_lock(&mm->page_table_lock);
  284. ret = mm->context.pte_frag;
  285. if (ret) {
  286. pte_frag = ret + PTE_FRAG_SIZE;
  287. /*
  288. * If we have taken up all the fragments mark PTE page NULL
  289. */
  290. if (((unsigned long)pte_frag & ~PAGE_MASK) == 0)
  291. pte_frag = NULL;
  292. mm->context.pte_frag = pte_frag;
  293. }
  294. spin_unlock(&mm->page_table_lock);
  295. return (pte_t *)ret;
  296. }
  297. static pte_t *__alloc_for_ptecache(struct mm_struct *mm, int kernel)
  298. {
  299. void *ret = NULL;
  300. struct page *page;
  301. if (!kernel) {
  302. page = alloc_page(PGALLOC_GFP | __GFP_ACCOUNT);
  303. if (!page)
  304. return NULL;
  305. if (!pgtable_page_ctor(page)) {
  306. __free_page(page);
  307. return NULL;
  308. }
  309. } else {
  310. page = alloc_page(PGALLOC_GFP);
  311. if (!page)
  312. return NULL;
  313. }
  314. atomic_set(&page->pt_frag_refcount, 1);
  315. ret = page_address(page);
  316. /*
  317. * if we support only one fragment just return the
  318. * allocated page.
  319. */
  320. if (PTE_FRAG_NR == 1)
  321. return ret;
  322. spin_lock(&mm->page_table_lock);
  323. /*
  324. * If we find pgtable_page set, we return
  325. * the allocated page with single fragement
  326. * count.
  327. */
  328. if (likely(!mm->context.pte_frag)) {
  329. atomic_set(&page->pt_frag_refcount, PTE_FRAG_NR);
  330. mm->context.pte_frag = ret + PTE_FRAG_SIZE;
  331. }
  332. spin_unlock(&mm->page_table_lock);
  333. return (pte_t *)ret;
  334. }
  335. pte_t *pte_fragment_alloc(struct mm_struct *mm, unsigned long vmaddr, int kernel)
  336. {
  337. pte_t *pte;
  338. pte = get_pte_from_cache(mm);
  339. if (pte)
  340. return pte;
  341. return __alloc_for_ptecache(mm, kernel);
  342. }
  343. void pte_fragment_free(unsigned long *table, int kernel)
  344. {
  345. struct page *page = virt_to_page(table);
  346. BUG_ON(atomic_read(&page->pt_frag_refcount) <= 0);
  347. if (atomic_dec_and_test(&page->pt_frag_refcount)) {
  348. if (!kernel)
  349. pgtable_page_dtor(page);
  350. __free_page(page);
  351. }
  352. }
  353. static inline void pgtable_free(void *table, int index)
  354. {
  355. switch (index) {
  356. case PTE_INDEX:
  357. pte_fragment_free(table, 0);
  358. break;
  359. case PMD_INDEX:
  360. pmd_fragment_free(table);
  361. break;
  362. case PUD_INDEX:
  363. kmem_cache_free(PGT_CACHE(PUD_CACHE_INDEX), table);
  364. break;
  365. #if defined(CONFIG_PPC_4K_PAGES) && defined(CONFIG_HUGETLB_PAGE)
  366. /* 16M hugepd directory at pud level */
  367. case HTLB_16M_INDEX:
  368. BUILD_BUG_ON(H_16M_CACHE_INDEX <= 0);
  369. kmem_cache_free(PGT_CACHE(H_16M_CACHE_INDEX), table);
  370. break;
  371. /* 16G hugepd directory at the pgd level */
  372. case HTLB_16G_INDEX:
  373. BUILD_BUG_ON(H_16G_CACHE_INDEX <= 0);
  374. kmem_cache_free(PGT_CACHE(H_16G_CACHE_INDEX), table);
  375. break;
  376. #endif
  377. /* We don't free pgd table via RCU callback */
  378. default:
  379. BUG();
  380. }
  381. }
  382. #ifdef CONFIG_SMP
  383. void pgtable_free_tlb(struct mmu_gather *tlb, void *table, int index)
  384. {
  385. unsigned long pgf = (unsigned long)table;
  386. BUG_ON(index > MAX_PGTABLE_INDEX_SIZE);
  387. pgf |= index;
  388. tlb_remove_table(tlb, (void *)pgf);
  389. }
  390. void __tlb_remove_table(void *_table)
  391. {
  392. void *table = (void *)((unsigned long)_table & ~MAX_PGTABLE_INDEX_SIZE);
  393. unsigned int index = (unsigned long)_table & MAX_PGTABLE_INDEX_SIZE;
  394. return pgtable_free(table, index);
  395. }
  396. #else
  397. void pgtable_free_tlb(struct mmu_gather *tlb, void *table, int index)
  398. {
  399. return pgtable_free(table, index);
  400. }
  401. #endif
  402. #ifdef CONFIG_PROC_FS
  403. atomic_long_t direct_pages_count[MMU_PAGE_COUNT];
  404. void arch_report_meminfo(struct seq_file *m)
  405. {
  406. /*
  407. * Hash maps the memory with one size mmu_linear_psize.
  408. * So don't bother to print these on hash
  409. */
  410. if (!radix_enabled())
  411. return;
  412. seq_printf(m, "DirectMap4k: %8lu kB\n",
  413. atomic_long_read(&direct_pages_count[MMU_PAGE_4K]) << 2);
  414. seq_printf(m, "DirectMap64k: %8lu kB\n",
  415. atomic_long_read(&direct_pages_count[MMU_PAGE_64K]) << 6);
  416. seq_printf(m, "DirectMap2M: %8lu kB\n",
  417. atomic_long_read(&direct_pages_count[MMU_PAGE_2M]) << 11);
  418. seq_printf(m, "DirectMap1G: %8lu kB\n",
  419. atomic_long_read(&direct_pages_count[MMU_PAGE_1G]) << 20);
  420. }
  421. #endif /* CONFIG_PROC_FS */