pgtable-hash64.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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 <linux/mm.h>
  14. #include <asm/pgalloc.h>
  15. #include <asm/pgtable.h>
  16. #include <asm/sections.h>
  17. #include <asm/mmu.h>
  18. #include <asm/tlb.h>
  19. #include "mmu_decl.h"
  20. #define CREATE_TRACE_POINTS
  21. #include <trace/events/thp.h>
  22. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  23. /*
  24. * vmemmap is the starting address of the virtual address space where
  25. * struct pages are allocated for all possible PFNs present on the system
  26. * including holes and bad memory (hence sparse). These virtual struct
  27. * pages are stored in sequence in this virtual address space irrespective
  28. * of the fact whether the corresponding PFN is valid or not. This achieves
  29. * constant relationship between address of struct page and its PFN.
  30. *
  31. * During boot or memory hotplug operation when a new memory section is
  32. * added, physical memory allocation (including hash table bolting) will
  33. * be performed for the set of struct pages which are part of the memory
  34. * section. This saves memory by not allocating struct pages for PFNs
  35. * which are not valid.
  36. *
  37. * ----------------------------------------------
  38. * | PHYSICAL ALLOCATION OF VIRTUAL STRUCT PAGES|
  39. * ----------------------------------------------
  40. *
  41. * f000000000000000 c000000000000000
  42. * vmemmap +--------------+ +--------------+
  43. * + | page struct | +--------------> | page struct |
  44. * | +--------------+ +--------------+
  45. * | | page struct | +--------------> | page struct |
  46. * | +--------------+ | +--------------+
  47. * | | page struct | + +------> | page struct |
  48. * | +--------------+ | +--------------+
  49. * | | page struct | | +--> | page struct |
  50. * | +--------------+ | | +--------------+
  51. * | | page struct | | |
  52. * | +--------------+ | |
  53. * | | page struct | | |
  54. * | +--------------+ | |
  55. * | | page struct | | |
  56. * | +--------------+ | |
  57. * | | page struct | | |
  58. * | +--------------+ | |
  59. * | | page struct | +-------+ |
  60. * | +--------------+ |
  61. * | | page struct | +-----------+
  62. * | +--------------+
  63. * | | page struct | No mapping
  64. * | +--------------+
  65. * | | page struct | No mapping
  66. * v +--------------+
  67. *
  68. * -----------------------------------------
  69. * | RELATION BETWEEN STRUCT PAGES AND PFNS|
  70. * -----------------------------------------
  71. *
  72. * vmemmap +--------------+ +---------------+
  73. * + | page struct | +-------------> | PFN |
  74. * | +--------------+ +---------------+
  75. * | | page struct | +-------------> | PFN |
  76. * | +--------------+ +---------------+
  77. * | | page struct | +-------------> | PFN |
  78. * | +--------------+ +---------------+
  79. * | | page struct | +-------------> | PFN |
  80. * | +--------------+ +---------------+
  81. * | | |
  82. * | +--------------+
  83. * | | |
  84. * | +--------------+
  85. * | | |
  86. * | +--------------+ +---------------+
  87. * | | page struct | +-------------> | PFN |
  88. * | +--------------+ +---------------+
  89. * | | |
  90. * | +--------------+
  91. * | | |
  92. * | +--------------+ +---------------+
  93. * | | page struct | +-------------> | PFN |
  94. * | +--------------+ +---------------+
  95. * | | page struct | +-------------> | PFN |
  96. * v +--------------+ +---------------+
  97. */
  98. /*
  99. * On hash-based CPUs, the vmemmap is bolted in the hash table.
  100. *
  101. */
  102. int __meminit hash__vmemmap_create_mapping(unsigned long start,
  103. unsigned long page_size,
  104. unsigned long phys)
  105. {
  106. int rc = htab_bolt_mapping(start, start + page_size, phys,
  107. pgprot_val(PAGE_KERNEL),
  108. mmu_vmemmap_psize, mmu_kernel_ssize);
  109. if (rc < 0) {
  110. int rc2 = htab_remove_mapping(start, start + page_size,
  111. mmu_vmemmap_psize,
  112. mmu_kernel_ssize);
  113. BUG_ON(rc2 && (rc2 != -ENOENT));
  114. }
  115. return rc;
  116. }
  117. #ifdef CONFIG_MEMORY_HOTPLUG
  118. void hash__vmemmap_remove_mapping(unsigned long start,
  119. unsigned long page_size)
  120. {
  121. int rc = htab_remove_mapping(start, start + page_size,
  122. mmu_vmemmap_psize,
  123. mmu_kernel_ssize);
  124. BUG_ON((rc < 0) && (rc != -ENOENT));
  125. WARN_ON(rc == -ENOENT);
  126. }
  127. #endif
  128. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  129. /*
  130. * map_kernel_page currently only called by __ioremap
  131. * map_kernel_page adds an entry to the ioremap page table
  132. * and adds an entry to the HPT, possibly bolting it
  133. */
  134. int hash__map_kernel_page(unsigned long ea, unsigned long pa, unsigned long flags)
  135. {
  136. pgd_t *pgdp;
  137. pud_t *pudp;
  138. pmd_t *pmdp;
  139. pte_t *ptep;
  140. BUILD_BUG_ON(TASK_SIZE_USER64 > H_PGTABLE_RANGE);
  141. if (slab_is_available()) {
  142. pgdp = pgd_offset_k(ea);
  143. pudp = pud_alloc(&init_mm, pgdp, ea);
  144. if (!pudp)
  145. return -ENOMEM;
  146. pmdp = pmd_alloc(&init_mm, pudp, ea);
  147. if (!pmdp)
  148. return -ENOMEM;
  149. ptep = pte_alloc_kernel(pmdp, ea);
  150. if (!ptep)
  151. return -ENOMEM;
  152. set_pte_at(&init_mm, ea, ptep, pfn_pte(pa >> PAGE_SHIFT,
  153. __pgprot(flags)));
  154. } else {
  155. /*
  156. * If the mm subsystem is not fully up, we cannot create a
  157. * linux page table entry for this mapping. Simply bolt an
  158. * entry in the hardware page table.
  159. *
  160. */
  161. if (htab_bolt_mapping(ea, ea + PAGE_SIZE, pa, flags,
  162. mmu_io_psize, mmu_kernel_ssize)) {
  163. printk(KERN_ERR "Failed to do bolted mapping IO "
  164. "memory at %016lx !\n", pa);
  165. return -ENOMEM;
  166. }
  167. }
  168. smp_wmb();
  169. return 0;
  170. }
  171. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  172. unsigned long hash__pmd_hugepage_update(struct mm_struct *mm, unsigned long addr,
  173. pmd_t *pmdp, unsigned long clr,
  174. unsigned long set)
  175. {
  176. __be64 old_be, tmp;
  177. unsigned long old;
  178. #ifdef CONFIG_DEBUG_VM
  179. WARN_ON(!hash__pmd_trans_huge(*pmdp) && !pmd_devmap(*pmdp));
  180. assert_spin_locked(&mm->page_table_lock);
  181. #endif
  182. __asm__ __volatile__(
  183. "1: ldarx %0,0,%3\n\
  184. and. %1,%0,%6\n\
  185. bne- 1b \n\
  186. andc %1,%0,%4 \n\
  187. or %1,%1,%7\n\
  188. stdcx. %1,0,%3 \n\
  189. bne- 1b"
  190. : "=&r" (old_be), "=&r" (tmp), "=m" (*pmdp)
  191. : "r" (pmdp), "r" (cpu_to_be64(clr)), "m" (*pmdp),
  192. "r" (cpu_to_be64(H_PAGE_BUSY)), "r" (cpu_to_be64(set))
  193. : "cc" );
  194. old = be64_to_cpu(old_be);
  195. trace_hugepage_update(addr, old, clr, set);
  196. if (old & H_PAGE_HASHPTE)
  197. hpte_do_hugepage_flush(mm, addr, pmdp, old);
  198. return old;
  199. }
  200. pmd_t hash__pmdp_collapse_flush(struct vm_area_struct *vma, unsigned long address,
  201. pmd_t *pmdp)
  202. {
  203. pmd_t pmd;
  204. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  205. VM_BUG_ON(pmd_trans_huge(*pmdp));
  206. VM_BUG_ON(pmd_devmap(*pmdp));
  207. pmd = *pmdp;
  208. pmd_clear(pmdp);
  209. /*
  210. * Wait for all pending hash_page to finish. This is needed
  211. * in case of subpage collapse. When we collapse normal pages
  212. * to hugepage, we first clear the pmd, then invalidate all
  213. * the PTE entries. The assumption here is that any low level
  214. * page fault will see a none pmd and take the slow path that
  215. * will wait on mmap_sem. But we could very well be in a
  216. * hash_page with local ptep pointer value. Such a hash page
  217. * can result in adding new HPTE entries for normal subpages.
  218. * That means we could be modifying the page content as we
  219. * copy them to a huge page. So wait for parallel hash_page
  220. * to finish before invalidating HPTE entries. We can do this
  221. * by sending an IPI to all the cpus and executing a dummy
  222. * function there.
  223. */
  224. serialize_against_pte_lookup(vma->vm_mm);
  225. /*
  226. * Now invalidate the hpte entries in the range
  227. * covered by pmd. This make sure we take a
  228. * fault and will find the pmd as none, which will
  229. * result in a major fault which takes mmap_sem and
  230. * hence wait for collapse to complete. Without this
  231. * the __collapse_huge_page_copy can result in copying
  232. * the old content.
  233. */
  234. flush_tlb_pmd_range(vma->vm_mm, &pmd, address);
  235. return pmd;
  236. }
  237. /*
  238. * We want to put the pgtable in pmd and use pgtable for tracking
  239. * the base page size hptes
  240. */
  241. void hash__pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
  242. pgtable_t pgtable)
  243. {
  244. pgtable_t *pgtable_slot;
  245. assert_spin_locked(&mm->page_table_lock);
  246. /*
  247. * we store the pgtable in the second half of PMD
  248. */
  249. pgtable_slot = (pgtable_t *)pmdp + PTRS_PER_PMD;
  250. *pgtable_slot = pgtable;
  251. /*
  252. * expose the deposited pgtable to other cpus.
  253. * before we set the hugepage PTE at pmd level
  254. * hash fault code looks at the deposted pgtable
  255. * to store hash index values.
  256. */
  257. smp_wmb();
  258. }
  259. pgtable_t hash__pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
  260. {
  261. pgtable_t pgtable;
  262. pgtable_t *pgtable_slot;
  263. assert_spin_locked(&mm->page_table_lock);
  264. pgtable_slot = (pgtable_t *)pmdp + PTRS_PER_PMD;
  265. pgtable = *pgtable_slot;
  266. /*
  267. * Once we withdraw, mark the entry NULL.
  268. */
  269. *pgtable_slot = NULL;
  270. /*
  271. * We store HPTE information in the deposited PTE fragment.
  272. * zero out the content on withdraw.
  273. */
  274. memset(pgtable, 0, PTE_FRAG_SIZE);
  275. return pgtable;
  276. }
  277. /*
  278. * A linux hugepage PMD was changed and the corresponding hash table entries
  279. * neesd to be flushed.
  280. */
  281. void hpte_do_hugepage_flush(struct mm_struct *mm, unsigned long addr,
  282. pmd_t *pmdp, unsigned long old_pmd)
  283. {
  284. int ssize;
  285. unsigned int psize;
  286. unsigned long vsid;
  287. unsigned long flags = 0;
  288. /* get the base page size,vsid and segment size */
  289. #ifdef CONFIG_DEBUG_VM
  290. psize = get_slice_psize(mm, addr);
  291. BUG_ON(psize == MMU_PAGE_16M);
  292. #endif
  293. if (old_pmd & H_PAGE_COMBO)
  294. psize = MMU_PAGE_4K;
  295. else
  296. psize = MMU_PAGE_64K;
  297. if (!is_kernel_addr(addr)) {
  298. ssize = user_segment_size(addr);
  299. vsid = get_user_vsid(&mm->context, addr, ssize);
  300. WARN_ON(vsid == 0);
  301. } else {
  302. vsid = get_kernel_vsid(addr, mmu_kernel_ssize);
  303. ssize = mmu_kernel_ssize;
  304. }
  305. if (mm_is_thread_local(mm))
  306. flags |= HPTE_LOCAL_UPDATE;
  307. return flush_hash_hugepage(vsid, addr, pmdp, psize, ssize, flags);
  308. }
  309. pmd_t hash__pmdp_huge_get_and_clear(struct mm_struct *mm,
  310. unsigned long addr, pmd_t *pmdp)
  311. {
  312. pmd_t old_pmd;
  313. pgtable_t pgtable;
  314. unsigned long old;
  315. pgtable_t *pgtable_slot;
  316. old = pmd_hugepage_update(mm, addr, pmdp, ~0UL, 0);
  317. old_pmd = __pmd(old);
  318. /*
  319. * We have pmd == none and we are holding page_table_lock.
  320. * So we can safely go and clear the pgtable hash
  321. * index info.
  322. */
  323. pgtable_slot = (pgtable_t *)pmdp + PTRS_PER_PMD;
  324. pgtable = *pgtable_slot;
  325. /*
  326. * Let's zero out old valid and hash index details
  327. * hash fault look at them.
  328. */
  329. memset(pgtable, 0, PTE_FRAG_SIZE);
  330. /*
  331. * Serialize against find_current_mm_pte variants which does lock-less
  332. * lookup in page tables with local interrupts disabled. For huge pages
  333. * it casts pmd_t to pte_t. Since format of pte_t is different from
  334. * pmd_t we want to prevent transit from pmd pointing to page table
  335. * to pmd pointing to huge page (and back) while interrupts are disabled.
  336. * We clear pmd to possibly replace it with page table pointer in
  337. * different code paths. So make sure we wait for the parallel
  338. * find_curren_mm_pte to finish.
  339. */
  340. serialize_against_pte_lookup(mm);
  341. return old_pmd;
  342. }
  343. int hash__has_transparent_hugepage(void)
  344. {
  345. if (!mmu_has_feature(MMU_FTR_16M_PAGE))
  346. return 0;
  347. /*
  348. * We support THP only if PMD_SIZE is 16MB.
  349. */
  350. if (mmu_psize_defs[MMU_PAGE_16M].shift != PMD_SHIFT)
  351. return 0;
  352. /*
  353. * We need to make sure that we support 16MB hugepage in a segement
  354. * with base page size 64K or 4K. We only enable THP with a PAGE_SIZE
  355. * of 64K.
  356. */
  357. /*
  358. * If we have 64K HPTE, we will be using that by default
  359. */
  360. if (mmu_psize_defs[MMU_PAGE_64K].shift &&
  361. (mmu_psize_defs[MMU_PAGE_64K].penc[MMU_PAGE_16M] == -1))
  362. return 0;
  363. /*
  364. * Ok we only have 4K HPTE
  365. */
  366. if (mmu_psize_defs[MMU_PAGE_4K].penc[MMU_PAGE_16M] == -1)
  367. return 0;
  368. return 1;
  369. }
  370. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  371. #ifdef CONFIG_STRICT_KERNEL_RWX
  372. static bool hash__change_memory_range(unsigned long start, unsigned long end,
  373. unsigned long newpp)
  374. {
  375. unsigned long idx;
  376. unsigned int step, shift;
  377. shift = mmu_psize_defs[mmu_linear_psize].shift;
  378. step = 1 << shift;
  379. start = ALIGN_DOWN(start, step);
  380. end = ALIGN(end, step); // aligns up
  381. if (start >= end)
  382. return false;
  383. pr_debug("Changing page protection on range 0x%lx-0x%lx, to 0x%lx, step 0x%x\n",
  384. start, end, newpp, step);
  385. for (idx = start; idx < end; idx += step)
  386. /* Not sure if we can do much with the return value */
  387. mmu_hash_ops.hpte_updateboltedpp(newpp, idx, mmu_linear_psize,
  388. mmu_kernel_ssize);
  389. return true;
  390. }
  391. void hash__mark_rodata_ro(void)
  392. {
  393. unsigned long start, end;
  394. start = (unsigned long)_stext;
  395. end = (unsigned long)__init_begin;
  396. WARN_ON(!hash__change_memory_range(start, end, PP_RXXX));
  397. }
  398. void hash__mark_initmem_nx(void)
  399. {
  400. unsigned long start, end, pp;
  401. start = (unsigned long)__init_begin;
  402. end = (unsigned long)__init_end;
  403. pp = htab_convert_pte_flags(pgprot_val(PAGE_KERNEL));
  404. WARN_ON(!hash__change_memory_range(start, end, pp));
  405. }
  406. #endif