pgtable-hash64.c 9.5 KB

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