pgtable-book3s64.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. WARN_ON(pte_present(pmd_pte(*pmdp)) && !pte_protnone(pmd_pte(*pmdp)));
  66. assert_spin_locked(pmd_lockptr(mm, pmdp));
  67. WARN_ON(!(pmd_trans_huge(pmd) || pmd_devmap(pmd)));
  68. #endif
  69. trace_hugepage_set_pmd(addr, pmd_val(pmd));
  70. return set_pte_at(mm, addr, pmdp_ptep(pmdp), pmd_pte(pmd));
  71. }
  72. static void do_nothing(void *unused)
  73. {
  74. }
  75. /*
  76. * Serialize against find_current_mm_pte which does lock-less
  77. * lookup in page tables with local interrupts disabled. For huge pages
  78. * it casts pmd_t to pte_t. Since format of pte_t is different from
  79. * pmd_t we want to prevent transit from pmd pointing to page table
  80. * to pmd pointing to huge page (and back) while interrupts are disabled.
  81. * We clear pmd to possibly replace it with page table pointer in
  82. * different code paths. So make sure we wait for the parallel
  83. * find_current_mm_pte to finish.
  84. */
  85. void serialize_against_pte_lookup(struct mm_struct *mm)
  86. {
  87. smp_mb();
  88. smp_call_function_many(mm_cpumask(mm), do_nothing, NULL, 1);
  89. }
  90. /*
  91. * We use this to invalidate a pmdp entry before switching from a
  92. * hugepte to regular pmd entry.
  93. */
  94. pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
  95. pmd_t *pmdp)
  96. {
  97. unsigned long old_pmd;
  98. old_pmd = pmd_hugepage_update(vma->vm_mm, address, pmdp, _PAGE_PRESENT, 0);
  99. flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  100. /*
  101. * This ensures that generic code that rely on IRQ disabling
  102. * to prevent a parallel THP split work as expected.
  103. */
  104. serialize_against_pte_lookup(vma->vm_mm);
  105. return __pmd(old_pmd);
  106. }
  107. static pmd_t pmd_set_protbits(pmd_t pmd, pgprot_t pgprot)
  108. {
  109. return __pmd(pmd_val(pmd) | pgprot_val(pgprot));
  110. }
  111. pmd_t pfn_pmd(unsigned long pfn, pgprot_t pgprot)
  112. {
  113. unsigned long pmdv;
  114. pmdv = (pfn << PAGE_SHIFT) & PTE_RPN_MASK;
  115. return pmd_set_protbits(__pmd(pmdv), pgprot);
  116. }
  117. pmd_t mk_pmd(struct page *page, pgprot_t pgprot)
  118. {
  119. return pfn_pmd(page_to_pfn(page), pgprot);
  120. }
  121. pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot)
  122. {
  123. unsigned long pmdv;
  124. pmdv = pmd_val(pmd);
  125. pmdv &= _HPAGE_CHG_MASK;
  126. return pmd_set_protbits(__pmd(pmdv), newprot);
  127. }
  128. /*
  129. * This is called at the end of handling a user page fault, when the
  130. * fault has been handled by updating a HUGE PMD entry in the linux page tables.
  131. * We use it to preload an HPTE into the hash table corresponding to
  132. * the updated linux HUGE PMD entry.
  133. */
  134. void update_mmu_cache_pmd(struct vm_area_struct *vma, unsigned long addr,
  135. pmd_t *pmd)
  136. {
  137. if (radix_enabled())
  138. prefetch((void *)addr);
  139. }
  140. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  141. /* For use by kexec */
  142. void mmu_cleanup_all(void)
  143. {
  144. if (radix_enabled())
  145. radix__mmu_cleanup_all();
  146. else if (mmu_hash_ops.hpte_clear_all)
  147. mmu_hash_ops.hpte_clear_all();
  148. }
  149. #ifdef CONFIG_MEMORY_HOTPLUG
  150. int __meminit create_section_mapping(unsigned long start, unsigned long end, int nid)
  151. {
  152. if (radix_enabled())
  153. return radix__create_section_mapping(start, end, nid);
  154. return hash__create_section_mapping(start, end, nid);
  155. }
  156. int __meminit remove_section_mapping(unsigned long start, unsigned long end)
  157. {
  158. if (radix_enabled())
  159. return radix__remove_section_mapping(start, end);
  160. return hash__remove_section_mapping(start, end);
  161. }
  162. #endif /* CONFIG_MEMORY_HOTPLUG */
  163. void __init mmu_partition_table_init(void)
  164. {
  165. unsigned long patb_size = 1UL << PATB_SIZE_SHIFT;
  166. unsigned long ptcr;
  167. BUILD_BUG_ON_MSG((PATB_SIZE_SHIFT > 36), "Partition table size too large.");
  168. partition_tb = __va(memblock_alloc_base(patb_size, patb_size,
  169. MEMBLOCK_ALLOC_ANYWHERE));
  170. /* Initialize the Partition Table with no entries */
  171. memset((void *)partition_tb, 0, patb_size);
  172. /*
  173. * update partition table control register,
  174. * 64 K size.
  175. */
  176. ptcr = __pa(partition_tb) | (PATB_SIZE_SHIFT - 12);
  177. mtspr(SPRN_PTCR, ptcr);
  178. powernv_set_nmmu_ptcr(ptcr);
  179. }
  180. void mmu_partition_table_set_entry(unsigned int lpid, unsigned long dw0,
  181. unsigned long dw1)
  182. {
  183. unsigned long old = be64_to_cpu(partition_tb[lpid].patb0);
  184. partition_tb[lpid].patb0 = cpu_to_be64(dw0);
  185. partition_tb[lpid].patb1 = cpu_to_be64(dw1);
  186. /*
  187. * Global flush of TLBs and partition table caches for this lpid.
  188. * The type of flush (hash or radix) depends on what the previous
  189. * use of this partition ID was, not the new use.
  190. */
  191. asm volatile("ptesync" : : : "memory");
  192. if (old & PATB_HR) {
  193. asm volatile(PPC_TLBIE_5(%0,%1,2,0,1) : :
  194. "r" (TLBIEL_INVAL_SET_LPID), "r" (lpid));
  195. asm volatile(PPC_TLBIE_5(%0,%1,2,1,1) : :
  196. "r" (TLBIEL_INVAL_SET_LPID), "r" (lpid));
  197. trace_tlbie(lpid, 0, TLBIEL_INVAL_SET_LPID, lpid, 2, 0, 1);
  198. } else {
  199. asm volatile(PPC_TLBIE_5(%0,%1,2,0,0) : :
  200. "r" (TLBIEL_INVAL_SET_LPID), "r" (lpid));
  201. trace_tlbie(lpid, 0, TLBIEL_INVAL_SET_LPID, lpid, 2, 0, 0);
  202. }
  203. /* do we need fixup here ?*/
  204. asm volatile("eieio; tlbsync; ptesync" : : : "memory");
  205. }
  206. EXPORT_SYMBOL_GPL(mmu_partition_table_set_entry);
  207. static pmd_t *get_pmd_from_cache(struct mm_struct *mm)
  208. {
  209. void *pmd_frag, *ret;
  210. spin_lock(&mm->page_table_lock);
  211. ret = mm->context.pmd_frag;
  212. if (ret) {
  213. pmd_frag = ret + PMD_FRAG_SIZE;
  214. /*
  215. * If we have taken up all the fragments mark PTE page NULL
  216. */
  217. if (((unsigned long)pmd_frag & ~PAGE_MASK) == 0)
  218. pmd_frag = NULL;
  219. mm->context.pmd_frag = pmd_frag;
  220. }
  221. spin_unlock(&mm->page_table_lock);
  222. return (pmd_t *)ret;
  223. }
  224. static pmd_t *__alloc_for_pmdcache(struct mm_struct *mm)
  225. {
  226. void *ret = NULL;
  227. struct page *page;
  228. gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO;
  229. if (mm == &init_mm)
  230. gfp &= ~__GFP_ACCOUNT;
  231. page = alloc_page(gfp);
  232. if (!page)
  233. return NULL;
  234. if (!pgtable_pmd_page_ctor(page)) {
  235. __free_pages(page, 0);
  236. return NULL;
  237. }
  238. atomic_set(&page->pt_frag_refcount, 1);
  239. ret = page_address(page);
  240. /*
  241. * if we support only one fragment just return the
  242. * allocated page.
  243. */
  244. if (PMD_FRAG_NR == 1)
  245. return ret;
  246. spin_lock(&mm->page_table_lock);
  247. /*
  248. * If we find pgtable_page set, we return
  249. * the allocated page with single fragement
  250. * count.
  251. */
  252. if (likely(!mm->context.pmd_frag)) {
  253. atomic_set(&page->pt_frag_refcount, PMD_FRAG_NR);
  254. mm->context.pmd_frag = ret + PMD_FRAG_SIZE;
  255. }
  256. spin_unlock(&mm->page_table_lock);
  257. return (pmd_t *)ret;
  258. }
  259. pmd_t *pmd_fragment_alloc(struct mm_struct *mm, unsigned long vmaddr)
  260. {
  261. pmd_t *pmd;
  262. pmd = get_pmd_from_cache(mm);
  263. if (pmd)
  264. return pmd;
  265. return __alloc_for_pmdcache(mm);
  266. }
  267. void pmd_fragment_free(unsigned long *pmd)
  268. {
  269. struct page *page = virt_to_page(pmd);
  270. BUG_ON(atomic_read(&page->pt_frag_refcount) <= 0);
  271. if (atomic_dec_and_test(&page->pt_frag_refcount)) {
  272. pgtable_pmd_page_dtor(page);
  273. __free_page(page);
  274. }
  275. }
  276. static pte_t *get_pte_from_cache(struct mm_struct *mm)
  277. {
  278. void *pte_frag, *ret;
  279. spin_lock(&mm->page_table_lock);
  280. ret = mm->context.pte_frag;
  281. if (ret) {
  282. pte_frag = ret + PTE_FRAG_SIZE;
  283. /*
  284. * If we have taken up all the fragments mark PTE page NULL
  285. */
  286. if (((unsigned long)pte_frag & ~PAGE_MASK) == 0)
  287. pte_frag = NULL;
  288. mm->context.pte_frag = pte_frag;
  289. }
  290. spin_unlock(&mm->page_table_lock);
  291. return (pte_t *)ret;
  292. }
  293. static pte_t *__alloc_for_ptecache(struct mm_struct *mm, int kernel)
  294. {
  295. void *ret = NULL;
  296. struct page *page;
  297. if (!kernel) {
  298. page = alloc_page(PGALLOC_GFP | __GFP_ACCOUNT);
  299. if (!page)
  300. return NULL;
  301. if (!pgtable_page_ctor(page)) {
  302. __free_page(page);
  303. return NULL;
  304. }
  305. } else {
  306. page = alloc_page(PGALLOC_GFP);
  307. if (!page)
  308. return NULL;
  309. }
  310. atomic_set(&page->pt_frag_refcount, 1);
  311. ret = page_address(page);
  312. /*
  313. * if we support only one fragment just return the
  314. * allocated page.
  315. */
  316. if (PTE_FRAG_NR == 1)
  317. return ret;
  318. spin_lock(&mm->page_table_lock);
  319. /*
  320. * If we find pgtable_page set, we return
  321. * the allocated page with single fragement
  322. * count.
  323. */
  324. if (likely(!mm->context.pte_frag)) {
  325. atomic_set(&page->pt_frag_refcount, PTE_FRAG_NR);
  326. mm->context.pte_frag = ret + PTE_FRAG_SIZE;
  327. }
  328. spin_unlock(&mm->page_table_lock);
  329. return (pte_t *)ret;
  330. }
  331. pte_t *pte_fragment_alloc(struct mm_struct *mm, unsigned long vmaddr, int kernel)
  332. {
  333. pte_t *pte;
  334. pte = get_pte_from_cache(mm);
  335. if (pte)
  336. return pte;
  337. return __alloc_for_ptecache(mm, kernel);
  338. }
  339. void pte_fragment_free(unsigned long *table, int kernel)
  340. {
  341. struct page *page = virt_to_page(table);
  342. BUG_ON(atomic_read(&page->pt_frag_refcount) <= 0);
  343. if (atomic_dec_and_test(&page->pt_frag_refcount)) {
  344. if (!kernel)
  345. pgtable_page_dtor(page);
  346. __free_page(page);
  347. }
  348. }
  349. static inline void pgtable_free(void *table, int index)
  350. {
  351. switch (index) {
  352. case PTE_INDEX:
  353. pte_fragment_free(table, 0);
  354. break;
  355. case PMD_INDEX:
  356. pmd_fragment_free(table);
  357. break;
  358. case PUD_INDEX:
  359. kmem_cache_free(PGT_CACHE(PUD_CACHE_INDEX), table);
  360. break;
  361. #if defined(CONFIG_PPC_4K_PAGES) && defined(CONFIG_HUGETLB_PAGE)
  362. /* 16M hugepd directory at pud level */
  363. case HTLB_16M_INDEX:
  364. BUILD_BUG_ON(H_16M_CACHE_INDEX <= 0);
  365. kmem_cache_free(PGT_CACHE(H_16M_CACHE_INDEX), table);
  366. break;
  367. /* 16G hugepd directory at the pgd level */
  368. case HTLB_16G_INDEX:
  369. BUILD_BUG_ON(H_16G_CACHE_INDEX <= 0);
  370. kmem_cache_free(PGT_CACHE(H_16G_CACHE_INDEX), table);
  371. break;
  372. #endif
  373. /* We don't free pgd table via RCU callback */
  374. default:
  375. BUG();
  376. }
  377. }
  378. #ifdef CONFIG_SMP
  379. void pgtable_free_tlb(struct mmu_gather *tlb, void *table, int index)
  380. {
  381. unsigned long pgf = (unsigned long)table;
  382. BUG_ON(index > MAX_PGTABLE_INDEX_SIZE);
  383. pgf |= index;
  384. tlb_remove_table(tlb, (void *)pgf);
  385. }
  386. void __tlb_remove_table(void *_table)
  387. {
  388. void *table = (void *)((unsigned long)_table & ~MAX_PGTABLE_INDEX_SIZE);
  389. unsigned int index = (unsigned long)_table & MAX_PGTABLE_INDEX_SIZE;
  390. return pgtable_free(table, index);
  391. }
  392. #else
  393. void pgtable_free_tlb(struct mmu_gather *tlb, void *table, int index)
  394. {
  395. return pgtable_free(table, index);
  396. }
  397. #endif
  398. #ifdef CONFIG_PROC_FS
  399. atomic_long_t direct_pages_count[MMU_PAGE_COUNT];
  400. void arch_report_meminfo(struct seq_file *m)
  401. {
  402. /*
  403. * Hash maps the memory with one size mmu_linear_psize.
  404. * So don't bother to print these on hash
  405. */
  406. if (!radix_enabled())
  407. return;
  408. seq_printf(m, "DirectMap4k: %8lu kB\n",
  409. atomic_long_read(&direct_pages_count[MMU_PAGE_4K]) << 2);
  410. seq_printf(m, "DirectMap64k: %8lu kB\n",
  411. atomic_long_read(&direct_pages_count[MMU_PAGE_64K]) << 6);
  412. seq_printf(m, "DirectMap2M: %8lu kB\n",
  413. atomic_long_read(&direct_pages_count[MMU_PAGE_2M]) << 11);
  414. seq_printf(m, "DirectMap1G: %8lu kB\n",
  415. atomic_long_read(&direct_pages_count[MMU_PAGE_1G]) << 20);
  416. }
  417. #endif /* CONFIG_PROC_FS */
  418. /*
  419. * For hash translation mode, we use the deposited table to store hash slot
  420. * information and they are stored at PTRS_PER_PMD offset from related pmd
  421. * location. Hence a pmd move requires deposit and withdraw.
  422. *
  423. * For radix translation with split pmd ptl, we store the deposited table in the
  424. * pmd page. Hence if we have different pmd page we need to withdraw during pmd
  425. * move.
  426. *
  427. * With hash we use deposited table always irrespective of anon or not.
  428. * With radix we use deposited table only for anonymous mapping.
  429. */
  430. int pmd_move_must_withdraw(struct spinlock *new_pmd_ptl,
  431. struct spinlock *old_pmd_ptl,
  432. struct vm_area_struct *vma)
  433. {
  434. if (radix_enabled())
  435. return (new_pmd_ptl != old_pmd_ptl) && vma_is_anonymous(vma);
  436. return true;
  437. }