pgtable-hash64.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright 2005, Paul Mackerras, IBM Corporation.
  3. * Copyright 2009, Benjamin Herrenschmidt, IBM Corporation.
  4. * Copyright 2015-2016, Aneesh Kumar K.V, IBM Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/sched.h>
  12. #include <asm/pgalloc.h>
  13. #include <asm/tlb.h>
  14. #include "mmu_decl.h"
  15. #define CREATE_TRACE_POINTS
  16. #include <trace/events/thp.h>
  17. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  18. /*
  19. * On hash-based CPUs, the vmemmap is bolted in the hash table.
  20. *
  21. */
  22. int __meminit hash__vmemmap_create_mapping(unsigned long start,
  23. unsigned long page_size,
  24. unsigned long phys)
  25. {
  26. int rc = htab_bolt_mapping(start, start + page_size, phys,
  27. pgprot_val(PAGE_KERNEL),
  28. mmu_vmemmap_psize, mmu_kernel_ssize);
  29. if (rc < 0) {
  30. int rc2 = htab_remove_mapping(start, start + page_size,
  31. mmu_vmemmap_psize,
  32. mmu_kernel_ssize);
  33. BUG_ON(rc2 && (rc2 != -ENOENT));
  34. }
  35. return rc;
  36. }
  37. #ifdef CONFIG_MEMORY_HOTPLUG
  38. void hash__vmemmap_remove_mapping(unsigned long start,
  39. unsigned long page_size)
  40. {
  41. int rc = htab_remove_mapping(start, start + page_size,
  42. mmu_vmemmap_psize,
  43. mmu_kernel_ssize);
  44. BUG_ON((rc < 0) && (rc != -ENOENT));
  45. WARN_ON(rc == -ENOENT);
  46. }
  47. #endif
  48. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  49. /*
  50. * map_kernel_page currently only called by __ioremap
  51. * map_kernel_page adds an entry to the ioremap page table
  52. * and adds an entry to the HPT, possibly bolting it
  53. */
  54. int hash__map_kernel_page(unsigned long ea, unsigned long pa, unsigned long flags)
  55. {
  56. pgd_t *pgdp;
  57. pud_t *pudp;
  58. pmd_t *pmdp;
  59. pte_t *ptep;
  60. BUILD_BUG_ON(TASK_SIZE_USER64 > H_PGTABLE_RANGE);
  61. if (slab_is_available()) {
  62. pgdp = pgd_offset_k(ea);
  63. pudp = pud_alloc(&init_mm, pgdp, ea);
  64. if (!pudp)
  65. return -ENOMEM;
  66. pmdp = pmd_alloc(&init_mm, pudp, ea);
  67. if (!pmdp)
  68. return -ENOMEM;
  69. ptep = pte_alloc_kernel(pmdp, ea);
  70. if (!ptep)
  71. return -ENOMEM;
  72. set_pte_at(&init_mm, ea, ptep, pfn_pte(pa >> PAGE_SHIFT,
  73. __pgprot(flags)));
  74. } else {
  75. /*
  76. * If the mm subsystem is not fully up, we cannot create a
  77. * linux page table entry for this mapping. Simply bolt an
  78. * entry in the hardware page table.
  79. *
  80. */
  81. if (htab_bolt_mapping(ea, ea + PAGE_SIZE, pa, flags,
  82. mmu_io_psize, mmu_kernel_ssize)) {
  83. printk(KERN_ERR "Failed to do bolted mapping IO "
  84. "memory at %016lx !\n", pa);
  85. return -ENOMEM;
  86. }
  87. }
  88. smp_wmb();
  89. return 0;
  90. }
  91. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  92. unsigned long hash__pmd_hugepage_update(struct mm_struct *mm, unsigned long addr,
  93. pmd_t *pmdp, unsigned long clr,
  94. unsigned long set)
  95. {
  96. __be64 old_be, tmp;
  97. unsigned long old;
  98. #ifdef CONFIG_DEBUG_VM
  99. WARN_ON(!pmd_trans_huge(*pmdp));
  100. assert_spin_locked(&mm->page_table_lock);
  101. #endif
  102. __asm__ __volatile__(
  103. "1: ldarx %0,0,%3\n\
  104. and. %1,%0,%6\n\
  105. bne- 1b \n\
  106. andc %1,%0,%4 \n\
  107. or %1,%1,%7\n\
  108. stdcx. %1,0,%3 \n\
  109. bne- 1b"
  110. : "=&r" (old_be), "=&r" (tmp), "=m" (*pmdp)
  111. : "r" (pmdp), "r" (cpu_to_be64(clr)), "m" (*pmdp),
  112. "r" (cpu_to_be64(H_PAGE_BUSY)), "r" (cpu_to_be64(set))
  113. : "cc" );
  114. old = be64_to_cpu(old_be);
  115. trace_hugepage_update(addr, old, clr, set);
  116. if (old & H_PAGE_HASHPTE)
  117. hpte_do_hugepage_flush(mm, addr, pmdp, old);
  118. return old;
  119. }
  120. pmd_t hash__pmdp_collapse_flush(struct vm_area_struct *vma, unsigned long address,
  121. pmd_t *pmdp)
  122. {
  123. pmd_t pmd;
  124. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  125. VM_BUG_ON(pmd_trans_huge(*pmdp));
  126. pmd = *pmdp;
  127. pmd_clear(pmdp);
  128. /*
  129. * Wait for all pending hash_page to finish. This is needed
  130. * in case of subpage collapse. When we collapse normal pages
  131. * to hugepage, we first clear the pmd, then invalidate all
  132. * the PTE entries. The assumption here is that any low level
  133. * page fault will see a none pmd and take the slow path that
  134. * will wait on mmap_sem. But we could very well be in a
  135. * hash_page with local ptep pointer value. Such a hash page
  136. * can result in adding new HPTE entries for normal subpages.
  137. * That means we could be modifying the page content as we
  138. * copy them to a huge page. So wait for parallel hash_page
  139. * to finish before invalidating HPTE entries. We can do this
  140. * by sending an IPI to all the cpus and executing a dummy
  141. * function there.
  142. */
  143. kick_all_cpus_sync();
  144. /*
  145. * Now invalidate the hpte entries in the range
  146. * covered by pmd. This make sure we take a
  147. * fault and will find the pmd as none, which will
  148. * result in a major fault which takes mmap_sem and
  149. * hence wait for collapse to complete. Without this
  150. * the __collapse_huge_page_copy can result in copying
  151. * the old content.
  152. */
  153. flush_tlb_pmd_range(vma->vm_mm, &pmd, address);
  154. return pmd;
  155. }
  156. /*
  157. * We want to put the pgtable in pmd and use pgtable for tracking
  158. * the base page size hptes
  159. */
  160. void hash__pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
  161. pgtable_t pgtable)
  162. {
  163. pgtable_t *pgtable_slot;
  164. assert_spin_locked(&mm->page_table_lock);
  165. /*
  166. * we store the pgtable in the second half of PMD
  167. */
  168. pgtable_slot = (pgtable_t *)pmdp + PTRS_PER_PMD;
  169. *pgtable_slot = pgtable;
  170. /*
  171. * expose the deposited pgtable to other cpus.
  172. * before we set the hugepage PTE at pmd level
  173. * hash fault code looks at the deposted pgtable
  174. * to store hash index values.
  175. */
  176. smp_wmb();
  177. }
  178. pgtable_t hash__pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
  179. {
  180. pgtable_t pgtable;
  181. pgtable_t *pgtable_slot;
  182. assert_spin_locked(&mm->page_table_lock);
  183. pgtable_slot = (pgtable_t *)pmdp + PTRS_PER_PMD;
  184. pgtable = *pgtable_slot;
  185. /*
  186. * Once we withdraw, mark the entry NULL.
  187. */
  188. *pgtable_slot = NULL;
  189. /*
  190. * We store HPTE information in the deposited PTE fragment.
  191. * zero out the content on withdraw.
  192. */
  193. memset(pgtable, 0, PTE_FRAG_SIZE);
  194. return pgtable;
  195. }
  196. void hash__pmdp_huge_split_prepare(struct vm_area_struct *vma,
  197. unsigned long address, pmd_t *pmdp)
  198. {
  199. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  200. VM_BUG_ON(REGION_ID(address) != USER_REGION_ID);
  201. /*
  202. * We can't mark the pmd none here, because that will cause a race
  203. * against exit_mmap. We need to continue mark pmd TRANS HUGE, while
  204. * we spilt, but at the same time we wan't rest of the ppc64 code
  205. * not to insert hash pte on this, because we will be modifying
  206. * the deposited pgtable in the caller of this function. Hence
  207. * clear the _PAGE_USER so that we move the fault handling to
  208. * higher level function and that will serialize against ptl.
  209. * We need to flush existing hash pte entries here even though,
  210. * the translation is still valid, because we will withdraw
  211. * pgtable_t after this.
  212. */
  213. pmd_hugepage_update(vma->vm_mm, address, pmdp, 0, _PAGE_PRIVILEGED);
  214. }
  215. /*
  216. * A linux hugepage PMD was changed and the corresponding hash table entries
  217. * neesd to be flushed.
  218. */
  219. void hpte_do_hugepage_flush(struct mm_struct *mm, unsigned long addr,
  220. pmd_t *pmdp, unsigned long old_pmd)
  221. {
  222. int ssize;
  223. unsigned int psize;
  224. unsigned long vsid;
  225. unsigned long flags = 0;
  226. const struct cpumask *tmp;
  227. /* get the base page size,vsid and segment size */
  228. #ifdef CONFIG_DEBUG_VM
  229. psize = get_slice_psize(mm, addr);
  230. BUG_ON(psize == MMU_PAGE_16M);
  231. #endif
  232. if (old_pmd & H_PAGE_COMBO)
  233. psize = MMU_PAGE_4K;
  234. else
  235. psize = MMU_PAGE_64K;
  236. if (!is_kernel_addr(addr)) {
  237. ssize = user_segment_size(addr);
  238. vsid = get_vsid(mm->context.id, addr, ssize);
  239. WARN_ON(vsid == 0);
  240. } else {
  241. vsid = get_kernel_vsid(addr, mmu_kernel_ssize);
  242. ssize = mmu_kernel_ssize;
  243. }
  244. tmp = cpumask_of(smp_processor_id());
  245. if (cpumask_equal(mm_cpumask(mm), tmp))
  246. flags |= HPTE_LOCAL_UPDATE;
  247. return flush_hash_hugepage(vsid, addr, pmdp, psize, ssize, flags);
  248. }
  249. pmd_t hash__pmdp_huge_get_and_clear(struct mm_struct *mm,
  250. unsigned long addr, pmd_t *pmdp)
  251. {
  252. pmd_t old_pmd;
  253. pgtable_t pgtable;
  254. unsigned long old;
  255. pgtable_t *pgtable_slot;
  256. old = pmd_hugepage_update(mm, addr, pmdp, ~0UL, 0);
  257. old_pmd = __pmd(old);
  258. /*
  259. * We have pmd == none and we are holding page_table_lock.
  260. * So we can safely go and clear the pgtable hash
  261. * index info.
  262. */
  263. pgtable_slot = (pgtable_t *)pmdp + PTRS_PER_PMD;
  264. pgtable = *pgtable_slot;
  265. /*
  266. * Let's zero out old valid and hash index details
  267. * hash fault look at them.
  268. */
  269. memset(pgtable, 0, PTE_FRAG_SIZE);
  270. /*
  271. * Serialize against find_linux_pte_or_hugepte which does lock-less
  272. * lookup in page tables with local interrupts disabled. For huge pages
  273. * it casts pmd_t to pte_t. Since format of pte_t is different from
  274. * pmd_t we want to prevent transit from pmd pointing to page table
  275. * to pmd pointing to huge page (and back) while interrupts are disabled.
  276. * We clear pmd to possibly replace it with page table pointer in
  277. * different code paths. So make sure we wait for the parallel
  278. * find_linux_pte_or_hugepage to finish.
  279. */
  280. kick_all_cpus_sync();
  281. return old_pmd;
  282. }
  283. int hash__has_transparent_hugepage(void)
  284. {
  285. if (!mmu_has_feature(MMU_FTR_16M_PAGE))
  286. return 0;
  287. /*
  288. * We support THP only if PMD_SIZE is 16MB.
  289. */
  290. if (mmu_psize_defs[MMU_PAGE_16M].shift != PMD_SHIFT)
  291. return 0;
  292. /*
  293. * We need to make sure that we support 16MB hugepage in a segement
  294. * with base page size 64K or 4K. We only enable THP with a PAGE_SIZE
  295. * of 64K.
  296. */
  297. /*
  298. * If we have 64K HPTE, we will be using that by default
  299. */
  300. if (mmu_psize_defs[MMU_PAGE_64K].shift &&
  301. (mmu_psize_defs[MMU_PAGE_64K].penc[MMU_PAGE_16M] == -1))
  302. return 0;
  303. /*
  304. * Ok we only have 4K HPTE
  305. */
  306. if (mmu_psize_defs[MMU_PAGE_4K].penc[MMU_PAGE_16M] == -1)
  307. return 0;
  308. return 1;
  309. }
  310. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */