pgtable-book3s64.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. ret = page_address(page);
  239. /*
  240. * if we support only one fragment just return the
  241. * allocated page.
  242. */
  243. if (PMD_FRAG_NR == 1)
  244. return ret;
  245. spin_lock(&mm->page_table_lock);
  246. /*
  247. * If we find pgtable_page set, we return
  248. * the allocated page with single fragement
  249. * count.
  250. */
  251. if (likely(!mm->context.pmd_frag)) {
  252. set_page_count(page, PMD_FRAG_NR);
  253. mm->context.pmd_frag = ret + PMD_FRAG_SIZE;
  254. }
  255. spin_unlock(&mm->page_table_lock);
  256. return (pmd_t *)ret;
  257. }
  258. pmd_t *pmd_fragment_alloc(struct mm_struct *mm, unsigned long vmaddr)
  259. {
  260. pmd_t *pmd;
  261. pmd = get_pmd_from_cache(mm);
  262. if (pmd)
  263. return pmd;
  264. return __alloc_for_pmdcache(mm);
  265. }
  266. void pmd_fragment_free(unsigned long *pmd)
  267. {
  268. struct page *page = virt_to_page(pmd);
  269. if (put_page_testzero(page)) {
  270. pgtable_pmd_page_dtor(page);
  271. free_unref_page(page);
  272. }
  273. }
  274. static pte_t *get_pte_from_cache(struct mm_struct *mm)
  275. {
  276. void *pte_frag, *ret;
  277. spin_lock(&mm->page_table_lock);
  278. ret = mm->context.pte_frag;
  279. if (ret) {
  280. pte_frag = ret + PTE_FRAG_SIZE;
  281. /*
  282. * If we have taken up all the fragments mark PTE page NULL
  283. */
  284. if (((unsigned long)pte_frag & ~PAGE_MASK) == 0)
  285. pte_frag = NULL;
  286. mm->context.pte_frag = pte_frag;
  287. }
  288. spin_unlock(&mm->page_table_lock);
  289. return (pte_t *)ret;
  290. }
  291. static pte_t *__alloc_for_ptecache(struct mm_struct *mm, int kernel)
  292. {
  293. void *ret = NULL;
  294. struct page *page;
  295. if (!kernel) {
  296. page = alloc_page(PGALLOC_GFP | __GFP_ACCOUNT);
  297. if (!page)
  298. return NULL;
  299. if (!pgtable_page_ctor(page)) {
  300. __free_page(page);
  301. return NULL;
  302. }
  303. } else {
  304. page = alloc_page(PGALLOC_GFP);
  305. if (!page)
  306. return NULL;
  307. }
  308. ret = page_address(page);
  309. /*
  310. * if we support only one fragment just return the
  311. * allocated page.
  312. */
  313. if (PTE_FRAG_NR == 1)
  314. return ret;
  315. spin_lock(&mm->page_table_lock);
  316. /*
  317. * If we find pgtable_page set, we return
  318. * the allocated page with single fragement
  319. * count.
  320. */
  321. if (likely(!mm->context.pte_frag)) {
  322. set_page_count(page, PTE_FRAG_NR);
  323. mm->context.pte_frag = ret + PTE_FRAG_SIZE;
  324. }
  325. spin_unlock(&mm->page_table_lock);
  326. return (pte_t *)ret;
  327. }
  328. pte_t *pte_fragment_alloc(struct mm_struct *mm, unsigned long vmaddr, int kernel)
  329. {
  330. pte_t *pte;
  331. pte = get_pte_from_cache(mm);
  332. if (pte)
  333. return pte;
  334. return __alloc_for_ptecache(mm, kernel);
  335. }
  336. void pte_fragment_free(unsigned long *table, int kernel)
  337. {
  338. struct page *page = virt_to_page(table);
  339. if (put_page_testzero(page)) {
  340. if (!kernel)
  341. pgtable_page_dtor(page);
  342. free_unref_page(page);
  343. }
  344. }
  345. static inline void pgtable_free(void *table, int index)
  346. {
  347. switch (index) {
  348. case PTE_INDEX:
  349. pte_fragment_free(table, 0);
  350. break;
  351. case PMD_INDEX:
  352. pmd_fragment_free(table);
  353. break;
  354. case PUD_INDEX:
  355. kmem_cache_free(PGT_CACHE(PUD_CACHE_INDEX), table);
  356. break;
  357. /* We don't free pgd table via RCU callback */
  358. default:
  359. BUG();
  360. }
  361. }
  362. #ifdef CONFIG_SMP
  363. void pgtable_free_tlb(struct mmu_gather *tlb, void *table, int index)
  364. {
  365. unsigned long pgf = (unsigned long)table;
  366. BUG_ON(index > MAX_PGTABLE_INDEX_SIZE);
  367. pgf |= index;
  368. tlb_remove_table(tlb, (void *)pgf);
  369. }
  370. void __tlb_remove_table(void *_table)
  371. {
  372. void *table = (void *)((unsigned long)_table & ~MAX_PGTABLE_INDEX_SIZE);
  373. unsigned int index = (unsigned long)_table & MAX_PGTABLE_INDEX_SIZE;
  374. return pgtable_free(table, index);
  375. }
  376. #else
  377. void pgtable_free_tlb(struct mmu_gather *tlb, void *table, int index)
  378. {
  379. return pgtable_free(table, index);
  380. }
  381. #endif