hugetlbpage.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. /*
  2. * PPC Huge TLB Page Support for Kernel.
  3. *
  4. * Copyright (C) 2003 David Gibson, IBM Corporation.
  5. * Copyright (C) 2011 Becky Bruce, Freescale Semiconductor
  6. *
  7. * Based on the IA-32 version:
  8. * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
  9. */
  10. #include <linux/mm.h>
  11. #include <linux/io.h>
  12. #include <linux/slab.h>
  13. #include <linux/hugetlb.h>
  14. #include <linux/export.h>
  15. #include <linux/of_fdt.h>
  16. #include <linux/memblock.h>
  17. #include <linux/bootmem.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/swap.h>
  20. #include <linux/swapops.h>
  21. #include <asm/pgtable.h>
  22. #include <asm/pgalloc.h>
  23. #include <asm/tlb.h>
  24. #include <asm/setup.h>
  25. #include <asm/hugetlb.h>
  26. #include <asm/pte-walk.h>
  27. #ifdef CONFIG_HUGETLB_PAGE
  28. #define PAGE_SHIFT_64K 16
  29. #define PAGE_SHIFT_512K 19
  30. #define PAGE_SHIFT_8M 23
  31. #define PAGE_SHIFT_16M 24
  32. #define PAGE_SHIFT_16G 34
  33. unsigned int HPAGE_SHIFT;
  34. EXPORT_SYMBOL(HPAGE_SHIFT);
  35. #define hugepd_none(hpd) (hpd_val(hpd) == 0)
  36. pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr, unsigned long sz)
  37. {
  38. /*
  39. * Only called for hugetlbfs pages, hence can ignore THP and the
  40. * irq disabled walk.
  41. */
  42. return __find_linux_pte(mm->pgd, addr, NULL, NULL);
  43. }
  44. static int __hugepte_alloc(struct mm_struct *mm, hugepd_t *hpdp,
  45. unsigned long address, unsigned pdshift, unsigned pshift)
  46. {
  47. struct kmem_cache *cachep;
  48. pte_t *new;
  49. int i;
  50. int num_hugepd;
  51. if (pshift >= pdshift) {
  52. cachep = hugepte_cache;
  53. num_hugepd = 1 << (pshift - pdshift);
  54. } else {
  55. cachep = PGT_CACHE(pdshift - pshift);
  56. num_hugepd = 1;
  57. }
  58. new = kmem_cache_zalloc(cachep, pgtable_gfp_flags(mm, GFP_KERNEL));
  59. BUG_ON(pshift > HUGEPD_SHIFT_MASK);
  60. BUG_ON((unsigned long)new & HUGEPD_SHIFT_MASK);
  61. if (! new)
  62. return -ENOMEM;
  63. /*
  64. * Make sure other cpus find the hugepd set only after a
  65. * properly initialized page table is visible to them.
  66. * For more details look for comment in __pte_alloc().
  67. */
  68. smp_wmb();
  69. spin_lock(&mm->page_table_lock);
  70. /*
  71. * We have multiple higher-level entries that point to the same
  72. * actual pte location. Fill in each as we go and backtrack on error.
  73. * We need all of these so the DTLB pgtable walk code can find the
  74. * right higher-level entry without knowing if it's a hugepage or not.
  75. */
  76. for (i = 0; i < num_hugepd; i++, hpdp++) {
  77. if (unlikely(!hugepd_none(*hpdp)))
  78. break;
  79. else {
  80. #ifdef CONFIG_PPC_BOOK3S_64
  81. *hpdp = __hugepd(__pa(new) |
  82. (shift_to_mmu_psize(pshift) << 2));
  83. #elif defined(CONFIG_PPC_8xx)
  84. *hpdp = __hugepd(__pa(new) |
  85. (pshift == PAGE_SHIFT_8M ? _PMD_PAGE_8M :
  86. _PMD_PAGE_512K) | _PMD_PRESENT);
  87. #else
  88. /* We use the old format for PPC_FSL_BOOK3E */
  89. *hpdp = __hugepd(((unsigned long)new & ~PD_HUGE) | pshift);
  90. #endif
  91. }
  92. }
  93. /* If we bailed from the for loop early, an error occurred, clean up */
  94. if (i < num_hugepd) {
  95. for (i = i - 1 ; i >= 0; i--, hpdp--)
  96. *hpdp = __hugepd(0);
  97. kmem_cache_free(cachep, new);
  98. }
  99. spin_unlock(&mm->page_table_lock);
  100. return 0;
  101. }
  102. /*
  103. * These macros define how to determine which level of the page table holds
  104. * the hpdp.
  105. */
  106. #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx)
  107. #define HUGEPD_PGD_SHIFT PGDIR_SHIFT
  108. #define HUGEPD_PUD_SHIFT PUD_SHIFT
  109. #else
  110. #define HUGEPD_PGD_SHIFT PUD_SHIFT
  111. #define HUGEPD_PUD_SHIFT PMD_SHIFT
  112. #endif
  113. /*
  114. * At this point we do the placement change only for BOOK3S 64. This would
  115. * possibly work on other subarchs.
  116. */
  117. pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr, unsigned long sz)
  118. {
  119. pgd_t *pg;
  120. pud_t *pu;
  121. pmd_t *pm;
  122. hugepd_t *hpdp = NULL;
  123. unsigned pshift = __ffs(sz);
  124. unsigned pdshift = PGDIR_SHIFT;
  125. addr &= ~(sz-1);
  126. pg = pgd_offset(mm, addr);
  127. #ifdef CONFIG_PPC_BOOK3S_64
  128. if (pshift == PGDIR_SHIFT)
  129. /* 16GB huge page */
  130. return (pte_t *) pg;
  131. else if (pshift > PUD_SHIFT)
  132. /*
  133. * We need to use hugepd table
  134. */
  135. hpdp = (hugepd_t *)pg;
  136. else {
  137. pdshift = PUD_SHIFT;
  138. pu = pud_alloc(mm, pg, addr);
  139. if (pshift == PUD_SHIFT)
  140. return (pte_t *)pu;
  141. else if (pshift > PMD_SHIFT)
  142. hpdp = (hugepd_t *)pu;
  143. else {
  144. pdshift = PMD_SHIFT;
  145. pm = pmd_alloc(mm, pu, addr);
  146. if (pshift == PMD_SHIFT)
  147. /* 16MB hugepage */
  148. return (pte_t *)pm;
  149. else
  150. hpdp = (hugepd_t *)pm;
  151. }
  152. }
  153. #else
  154. if (pshift >= HUGEPD_PGD_SHIFT) {
  155. hpdp = (hugepd_t *)pg;
  156. } else {
  157. pdshift = PUD_SHIFT;
  158. pu = pud_alloc(mm, pg, addr);
  159. if (pshift >= HUGEPD_PUD_SHIFT) {
  160. hpdp = (hugepd_t *)pu;
  161. } else {
  162. pdshift = PMD_SHIFT;
  163. pm = pmd_alloc(mm, pu, addr);
  164. hpdp = (hugepd_t *)pm;
  165. }
  166. }
  167. #endif
  168. if (!hpdp)
  169. return NULL;
  170. BUG_ON(!hugepd_none(*hpdp) && !hugepd_ok(*hpdp));
  171. if (hugepd_none(*hpdp) && __hugepte_alloc(mm, hpdp, addr, pdshift, pshift))
  172. return NULL;
  173. return hugepte_offset(*hpdp, addr, pdshift);
  174. }
  175. #ifdef CONFIG_PPC_BOOK3S_64
  176. /*
  177. * Tracks gpages after the device tree is scanned and before the
  178. * huge_boot_pages list is ready on pseries.
  179. */
  180. #define MAX_NUMBER_GPAGES 1024
  181. __initdata static u64 gpage_freearray[MAX_NUMBER_GPAGES];
  182. __initdata static unsigned nr_gpages;
  183. /*
  184. * Build list of addresses of gigantic pages. This function is used in early
  185. * boot before the buddy allocator is setup.
  186. */
  187. void __init pseries_add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages)
  188. {
  189. if (!addr)
  190. return;
  191. while (number_of_pages > 0) {
  192. gpage_freearray[nr_gpages] = addr;
  193. nr_gpages++;
  194. number_of_pages--;
  195. addr += page_size;
  196. }
  197. }
  198. int __init pseries_alloc_bootmem_huge_page(struct hstate *hstate)
  199. {
  200. struct huge_bootmem_page *m;
  201. if (nr_gpages == 0)
  202. return 0;
  203. m = phys_to_virt(gpage_freearray[--nr_gpages]);
  204. gpage_freearray[nr_gpages] = 0;
  205. list_add(&m->list, &huge_boot_pages);
  206. m->hstate = hstate;
  207. return 1;
  208. }
  209. #endif
  210. int __init alloc_bootmem_huge_page(struct hstate *h)
  211. {
  212. #ifdef CONFIG_PPC_BOOK3S_64
  213. if (firmware_has_feature(FW_FEATURE_LPAR) && !radix_enabled())
  214. return pseries_alloc_bootmem_huge_page(h);
  215. #endif
  216. return __alloc_bootmem_huge_page(h);
  217. }
  218. #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx)
  219. #define HUGEPD_FREELIST_SIZE \
  220. ((PAGE_SIZE - sizeof(struct hugepd_freelist)) / sizeof(pte_t))
  221. struct hugepd_freelist {
  222. struct rcu_head rcu;
  223. unsigned int index;
  224. void *ptes[0];
  225. };
  226. static DEFINE_PER_CPU(struct hugepd_freelist *, hugepd_freelist_cur);
  227. static void hugepd_free_rcu_callback(struct rcu_head *head)
  228. {
  229. struct hugepd_freelist *batch =
  230. container_of(head, struct hugepd_freelist, rcu);
  231. unsigned int i;
  232. for (i = 0; i < batch->index; i++)
  233. kmem_cache_free(hugepte_cache, batch->ptes[i]);
  234. free_page((unsigned long)batch);
  235. }
  236. static void hugepd_free(struct mmu_gather *tlb, void *hugepte)
  237. {
  238. struct hugepd_freelist **batchp;
  239. batchp = &get_cpu_var(hugepd_freelist_cur);
  240. if (atomic_read(&tlb->mm->mm_users) < 2 ||
  241. mm_is_thread_local(tlb->mm)) {
  242. kmem_cache_free(hugepte_cache, hugepte);
  243. put_cpu_var(hugepd_freelist_cur);
  244. return;
  245. }
  246. if (*batchp == NULL) {
  247. *batchp = (struct hugepd_freelist *)__get_free_page(GFP_ATOMIC);
  248. (*batchp)->index = 0;
  249. }
  250. (*batchp)->ptes[(*batchp)->index++] = hugepte;
  251. if ((*batchp)->index == HUGEPD_FREELIST_SIZE) {
  252. call_rcu_sched(&(*batchp)->rcu, hugepd_free_rcu_callback);
  253. *batchp = NULL;
  254. }
  255. put_cpu_var(hugepd_freelist_cur);
  256. }
  257. #else
  258. static inline void hugepd_free(struct mmu_gather *tlb, void *hugepte) {}
  259. #endif
  260. static void free_hugepd_range(struct mmu_gather *tlb, hugepd_t *hpdp, int pdshift,
  261. unsigned long start, unsigned long end,
  262. unsigned long floor, unsigned long ceiling)
  263. {
  264. pte_t *hugepte = hugepd_page(*hpdp);
  265. int i;
  266. unsigned long pdmask = ~((1UL << pdshift) - 1);
  267. unsigned int num_hugepd = 1;
  268. unsigned int shift = hugepd_shift(*hpdp);
  269. /* Note: On fsl the hpdp may be the first of several */
  270. if (shift > pdshift)
  271. num_hugepd = 1 << (shift - pdshift);
  272. start &= pdmask;
  273. if (start < floor)
  274. return;
  275. if (ceiling) {
  276. ceiling &= pdmask;
  277. if (! ceiling)
  278. return;
  279. }
  280. if (end - 1 > ceiling - 1)
  281. return;
  282. for (i = 0; i < num_hugepd; i++, hpdp++)
  283. *hpdp = __hugepd(0);
  284. if (shift >= pdshift)
  285. hugepd_free(tlb, hugepte);
  286. else
  287. pgtable_free_tlb(tlb, hugepte, pdshift - shift);
  288. }
  289. static void hugetlb_free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
  290. unsigned long addr, unsigned long end,
  291. unsigned long floor, unsigned long ceiling)
  292. {
  293. pmd_t *pmd;
  294. unsigned long next;
  295. unsigned long start;
  296. start = addr;
  297. do {
  298. unsigned long more;
  299. pmd = pmd_offset(pud, addr);
  300. next = pmd_addr_end(addr, end);
  301. if (!is_hugepd(__hugepd(pmd_val(*pmd)))) {
  302. /*
  303. * if it is not hugepd pointer, we should already find
  304. * it cleared.
  305. */
  306. WARN_ON(!pmd_none_or_clear_bad(pmd));
  307. continue;
  308. }
  309. /*
  310. * Increment next by the size of the huge mapping since
  311. * there may be more than one entry at this level for a
  312. * single hugepage, but all of them point to
  313. * the same kmem cache that holds the hugepte.
  314. */
  315. more = addr + (1 << hugepd_shift(*(hugepd_t *)pmd));
  316. if (more > next)
  317. next = more;
  318. free_hugepd_range(tlb, (hugepd_t *)pmd, PMD_SHIFT,
  319. addr, next, floor, ceiling);
  320. } while (addr = next, addr != end);
  321. start &= PUD_MASK;
  322. if (start < floor)
  323. return;
  324. if (ceiling) {
  325. ceiling &= PUD_MASK;
  326. if (!ceiling)
  327. return;
  328. }
  329. if (end - 1 > ceiling - 1)
  330. return;
  331. pmd = pmd_offset(pud, start);
  332. pud_clear(pud);
  333. pmd_free_tlb(tlb, pmd, start);
  334. mm_dec_nr_pmds(tlb->mm);
  335. }
  336. static void hugetlb_free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
  337. unsigned long addr, unsigned long end,
  338. unsigned long floor, unsigned long ceiling)
  339. {
  340. pud_t *pud;
  341. unsigned long next;
  342. unsigned long start;
  343. start = addr;
  344. do {
  345. pud = pud_offset(pgd, addr);
  346. next = pud_addr_end(addr, end);
  347. if (!is_hugepd(__hugepd(pud_val(*pud)))) {
  348. if (pud_none_or_clear_bad(pud))
  349. continue;
  350. hugetlb_free_pmd_range(tlb, pud, addr, next, floor,
  351. ceiling);
  352. } else {
  353. unsigned long more;
  354. /*
  355. * Increment next by the size of the huge mapping since
  356. * there may be more than one entry at this level for a
  357. * single hugepage, but all of them point to
  358. * the same kmem cache that holds the hugepte.
  359. */
  360. more = addr + (1 << hugepd_shift(*(hugepd_t *)pud));
  361. if (more > next)
  362. next = more;
  363. free_hugepd_range(tlb, (hugepd_t *)pud, PUD_SHIFT,
  364. addr, next, floor, ceiling);
  365. }
  366. } while (addr = next, addr != end);
  367. start &= PGDIR_MASK;
  368. if (start < floor)
  369. return;
  370. if (ceiling) {
  371. ceiling &= PGDIR_MASK;
  372. if (!ceiling)
  373. return;
  374. }
  375. if (end - 1 > ceiling - 1)
  376. return;
  377. pud = pud_offset(pgd, start);
  378. pgd_clear(pgd);
  379. pud_free_tlb(tlb, pud, start);
  380. mm_dec_nr_puds(tlb->mm);
  381. }
  382. /*
  383. * This function frees user-level page tables of a process.
  384. */
  385. void hugetlb_free_pgd_range(struct mmu_gather *tlb,
  386. unsigned long addr, unsigned long end,
  387. unsigned long floor, unsigned long ceiling)
  388. {
  389. pgd_t *pgd;
  390. unsigned long next;
  391. /*
  392. * Because there are a number of different possible pagetable
  393. * layouts for hugepage ranges, we limit knowledge of how
  394. * things should be laid out to the allocation path
  395. * (huge_pte_alloc(), above). Everything else works out the
  396. * structure as it goes from information in the hugepd
  397. * pointers. That means that we can't here use the
  398. * optimization used in the normal page free_pgd_range(), of
  399. * checking whether we're actually covering a large enough
  400. * range to have to do anything at the top level of the walk
  401. * instead of at the bottom.
  402. *
  403. * To make sense of this, you should probably go read the big
  404. * block comment at the top of the normal free_pgd_range(),
  405. * too.
  406. */
  407. do {
  408. next = pgd_addr_end(addr, end);
  409. pgd = pgd_offset(tlb->mm, addr);
  410. if (!is_hugepd(__hugepd(pgd_val(*pgd)))) {
  411. if (pgd_none_or_clear_bad(pgd))
  412. continue;
  413. hugetlb_free_pud_range(tlb, pgd, addr, next, floor, ceiling);
  414. } else {
  415. unsigned long more;
  416. /*
  417. * Increment next by the size of the huge mapping since
  418. * there may be more than one entry at the pgd level
  419. * for a single hugepage, but all of them point to the
  420. * same kmem cache that holds the hugepte.
  421. */
  422. more = addr + (1 << hugepd_shift(*(hugepd_t *)pgd));
  423. if (more > next)
  424. next = more;
  425. free_hugepd_range(tlb, (hugepd_t *)pgd, PGDIR_SHIFT,
  426. addr, next, floor, ceiling);
  427. }
  428. } while (addr = next, addr != end);
  429. }
  430. struct page *follow_huge_pd(struct vm_area_struct *vma,
  431. unsigned long address, hugepd_t hpd,
  432. int flags, int pdshift)
  433. {
  434. pte_t *ptep;
  435. spinlock_t *ptl;
  436. struct page *page = NULL;
  437. unsigned long mask;
  438. int shift = hugepd_shift(hpd);
  439. struct mm_struct *mm = vma->vm_mm;
  440. retry:
  441. ptl = &mm->page_table_lock;
  442. spin_lock(ptl);
  443. ptep = hugepte_offset(hpd, address, pdshift);
  444. if (pte_present(*ptep)) {
  445. mask = (1UL << shift) - 1;
  446. page = pte_page(*ptep);
  447. page += ((address & mask) >> PAGE_SHIFT);
  448. if (flags & FOLL_GET)
  449. get_page(page);
  450. } else {
  451. if (is_hugetlb_entry_migration(*ptep)) {
  452. spin_unlock(ptl);
  453. __migration_entry_wait(mm, ptep, ptl);
  454. goto retry;
  455. }
  456. }
  457. spin_unlock(ptl);
  458. return page;
  459. }
  460. static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end,
  461. unsigned long sz)
  462. {
  463. unsigned long __boundary = (addr + sz) & ~(sz-1);
  464. return (__boundary - 1 < end - 1) ? __boundary : end;
  465. }
  466. int gup_huge_pd(hugepd_t hugepd, unsigned long addr, unsigned pdshift,
  467. unsigned long end, int write, struct page **pages, int *nr)
  468. {
  469. pte_t *ptep;
  470. unsigned long sz = 1UL << hugepd_shift(hugepd);
  471. unsigned long next;
  472. ptep = hugepte_offset(hugepd, addr, pdshift);
  473. do {
  474. next = hugepte_addr_end(addr, end, sz);
  475. if (!gup_hugepte(ptep, sz, addr, end, write, pages, nr))
  476. return 0;
  477. } while (ptep++, addr = next, addr != end);
  478. return 1;
  479. }
  480. #ifdef CONFIG_PPC_MM_SLICES
  481. unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  482. unsigned long len, unsigned long pgoff,
  483. unsigned long flags)
  484. {
  485. struct hstate *hstate = hstate_file(file);
  486. int mmu_psize = shift_to_mmu_psize(huge_page_shift(hstate));
  487. if (radix_enabled())
  488. return radix__hugetlb_get_unmapped_area(file, addr, len,
  489. pgoff, flags);
  490. return slice_get_unmapped_area(addr, len, flags, mmu_psize, 1);
  491. }
  492. #endif
  493. unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
  494. {
  495. #ifdef CONFIG_PPC_MM_SLICES
  496. unsigned int psize = get_slice_psize(vma->vm_mm, vma->vm_start);
  497. /* With radix we don't use slice, so derive it from vma*/
  498. if (!radix_enabled())
  499. return 1UL << mmu_psize_to_shift(psize);
  500. #endif
  501. if (!is_vm_hugetlb_page(vma))
  502. return PAGE_SIZE;
  503. return huge_page_size(hstate_vma(vma));
  504. }
  505. static inline bool is_power_of_4(unsigned long x)
  506. {
  507. if (is_power_of_2(x))
  508. return (__ilog2(x) % 2) ? false : true;
  509. return false;
  510. }
  511. static int __init add_huge_page_size(unsigned long long size)
  512. {
  513. int shift = __ffs(size);
  514. int mmu_psize;
  515. /* Check that it is a page size supported by the hardware and
  516. * that it fits within pagetable and slice limits. */
  517. if (size <= PAGE_SIZE)
  518. return -EINVAL;
  519. #if defined(CONFIG_PPC_FSL_BOOK3E)
  520. if (!is_power_of_4(size))
  521. return -EINVAL;
  522. #elif !defined(CONFIG_PPC_8xx)
  523. if (!is_power_of_2(size) || (shift > SLICE_HIGH_SHIFT))
  524. return -EINVAL;
  525. #endif
  526. if ((mmu_psize = shift_to_mmu_psize(shift)) < 0)
  527. return -EINVAL;
  528. #ifdef CONFIG_PPC_BOOK3S_64
  529. /*
  530. * We need to make sure that for different page sizes reported by
  531. * firmware we only add hugetlb support for page sizes that can be
  532. * supported by linux page table layout.
  533. * For now we have
  534. * Radix: 2M
  535. * Hash: 16M and 16G
  536. */
  537. if (radix_enabled()) {
  538. if (mmu_psize != MMU_PAGE_2M) {
  539. if (cpu_has_feature(CPU_FTR_POWER9_DD1) ||
  540. (mmu_psize != MMU_PAGE_1G))
  541. return -EINVAL;
  542. }
  543. } else {
  544. if (mmu_psize != MMU_PAGE_16M && mmu_psize != MMU_PAGE_16G)
  545. return -EINVAL;
  546. }
  547. #endif
  548. BUG_ON(mmu_psize_defs[mmu_psize].shift != shift);
  549. /* Return if huge page size has already been setup */
  550. if (size_to_hstate(size))
  551. return 0;
  552. hugetlb_add_hstate(shift - PAGE_SHIFT);
  553. return 0;
  554. }
  555. static int __init hugepage_setup_sz(char *str)
  556. {
  557. unsigned long long size;
  558. size = memparse(str, &str);
  559. if (add_huge_page_size(size) != 0) {
  560. hugetlb_bad_size();
  561. pr_err("Invalid huge page size specified(%llu)\n", size);
  562. }
  563. return 1;
  564. }
  565. __setup("hugepagesz=", hugepage_setup_sz);
  566. struct kmem_cache *hugepte_cache;
  567. static int __init hugetlbpage_init(void)
  568. {
  569. int psize;
  570. #if !defined(CONFIG_PPC_FSL_BOOK3E) && !defined(CONFIG_PPC_8xx)
  571. if (!radix_enabled() && !mmu_has_feature(MMU_FTR_16M_PAGE))
  572. return -ENODEV;
  573. #endif
  574. for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
  575. unsigned shift;
  576. unsigned pdshift;
  577. if (!mmu_psize_defs[psize].shift)
  578. continue;
  579. shift = mmu_psize_to_shift(psize);
  580. if (add_huge_page_size(1ULL << shift) < 0)
  581. continue;
  582. if (shift < HUGEPD_PUD_SHIFT)
  583. pdshift = PMD_SHIFT;
  584. else if (shift < HUGEPD_PGD_SHIFT)
  585. pdshift = PUD_SHIFT;
  586. else
  587. pdshift = PGDIR_SHIFT;
  588. /*
  589. * if we have pdshift and shift value same, we don't
  590. * use pgt cache for hugepd.
  591. */
  592. if (pdshift > shift)
  593. pgtable_cache_add(pdshift - shift, NULL);
  594. #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx)
  595. else if (!hugepte_cache) {
  596. /*
  597. * Create a kmem cache for hugeptes. The bottom bits in
  598. * the pte have size information encoded in them, so
  599. * align them to allow this
  600. */
  601. hugepte_cache = kmem_cache_create("hugepte-cache",
  602. sizeof(pte_t),
  603. HUGEPD_SHIFT_MASK + 1,
  604. 0, NULL);
  605. if (hugepte_cache == NULL)
  606. panic("%s: Unable to create kmem cache "
  607. "for hugeptes\n", __func__);
  608. }
  609. #endif
  610. }
  611. #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx)
  612. /* Default hpage size = 4M on FSL_BOOK3E and 512k on 8xx */
  613. if (mmu_psize_defs[MMU_PAGE_4M].shift)
  614. HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_4M].shift;
  615. else if (mmu_psize_defs[MMU_PAGE_512K].shift)
  616. HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_512K].shift;
  617. #else
  618. /* Set default large page size. Currently, we pick 16M or 1M
  619. * depending on what is available
  620. */
  621. if (mmu_psize_defs[MMU_PAGE_16M].shift)
  622. HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_16M].shift;
  623. else if (mmu_psize_defs[MMU_PAGE_1M].shift)
  624. HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_1M].shift;
  625. else if (mmu_psize_defs[MMU_PAGE_2M].shift)
  626. HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_2M].shift;
  627. #endif
  628. return 0;
  629. }
  630. arch_initcall(hugetlbpage_init);
  631. void flush_dcache_icache_hugepage(struct page *page)
  632. {
  633. int i;
  634. void *start;
  635. BUG_ON(!PageCompound(page));
  636. for (i = 0; i < (1UL << compound_order(page)); i++) {
  637. if (!PageHighMem(page)) {
  638. __flush_dcache_icache(page_address(page+i));
  639. } else {
  640. start = kmap_atomic(page+i);
  641. __flush_dcache_icache(start);
  642. kunmap_atomic(start);
  643. }
  644. }
  645. }
  646. #endif /* CONFIG_HUGETLB_PAGE */
  647. /*
  648. * We have 4 cases for pgds and pmds:
  649. * (1) invalid (all zeroes)
  650. * (2) pointer to next table, as normal; bottom 6 bits == 0
  651. * (3) leaf pte for huge page _PAGE_PTE set
  652. * (4) hugepd pointer, _PAGE_PTE = 0 and bits [2..6] indicate size of table
  653. *
  654. * So long as we atomically load page table pointers we are safe against teardown,
  655. * we can follow the address down to the the page and take a ref on it.
  656. * This function need to be called with interrupts disabled. We use this variant
  657. * when we have MSR[EE] = 0 but the paca->soft_enabled = 1
  658. */
  659. pte_t *__find_linux_pte(pgd_t *pgdir, unsigned long ea,
  660. bool *is_thp, unsigned *hpage_shift)
  661. {
  662. pgd_t pgd, *pgdp;
  663. pud_t pud, *pudp;
  664. pmd_t pmd, *pmdp;
  665. pte_t *ret_pte;
  666. hugepd_t *hpdp = NULL;
  667. unsigned pdshift = PGDIR_SHIFT;
  668. if (hpage_shift)
  669. *hpage_shift = 0;
  670. if (is_thp)
  671. *is_thp = false;
  672. pgdp = pgdir + pgd_index(ea);
  673. pgd = READ_ONCE(*pgdp);
  674. /*
  675. * Always operate on the local stack value. This make sure the
  676. * value don't get updated by a parallel THP split/collapse,
  677. * page fault or a page unmap. The return pte_t * is still not
  678. * stable. So should be checked there for above conditions.
  679. */
  680. if (pgd_none(pgd))
  681. return NULL;
  682. else if (pgd_huge(pgd)) {
  683. ret_pte = (pte_t *) pgdp;
  684. goto out;
  685. } else if (is_hugepd(__hugepd(pgd_val(pgd))))
  686. hpdp = (hugepd_t *)&pgd;
  687. else {
  688. /*
  689. * Even if we end up with an unmap, the pgtable will not
  690. * be freed, because we do an rcu free and here we are
  691. * irq disabled
  692. */
  693. pdshift = PUD_SHIFT;
  694. pudp = pud_offset(&pgd, ea);
  695. pud = READ_ONCE(*pudp);
  696. if (pud_none(pud))
  697. return NULL;
  698. else if (pud_huge(pud)) {
  699. ret_pte = (pte_t *) pudp;
  700. goto out;
  701. } else if (is_hugepd(__hugepd(pud_val(pud))))
  702. hpdp = (hugepd_t *)&pud;
  703. else {
  704. pdshift = PMD_SHIFT;
  705. pmdp = pmd_offset(&pud, ea);
  706. pmd = READ_ONCE(*pmdp);
  707. /*
  708. * A hugepage collapse is captured by pmd_none, because
  709. * it mark the pmd none and do a hpte invalidate.
  710. */
  711. if (pmd_none(pmd))
  712. return NULL;
  713. if (pmd_trans_huge(pmd) || pmd_devmap(pmd)) {
  714. if (is_thp)
  715. *is_thp = true;
  716. ret_pte = (pte_t *) pmdp;
  717. goto out;
  718. }
  719. if (pmd_huge(pmd)) {
  720. ret_pte = (pte_t *) pmdp;
  721. goto out;
  722. } else if (is_hugepd(__hugepd(pmd_val(pmd))))
  723. hpdp = (hugepd_t *)&pmd;
  724. else
  725. return pte_offset_kernel(&pmd, ea);
  726. }
  727. }
  728. if (!hpdp)
  729. return NULL;
  730. ret_pte = hugepte_offset(*hpdp, ea, pdshift);
  731. pdshift = hugepd_shift(*hpdp);
  732. out:
  733. if (hpage_shift)
  734. *hpage_shift = pdshift;
  735. return ret_pte;
  736. }
  737. EXPORT_SYMBOL_GPL(__find_linux_pte);
  738. int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
  739. unsigned long end, int write, struct page **pages, int *nr)
  740. {
  741. unsigned long pte_end;
  742. struct page *head, *page;
  743. pte_t pte;
  744. int refs;
  745. pte_end = (addr + sz) & ~(sz-1);
  746. if (pte_end < end)
  747. end = pte_end;
  748. pte = READ_ONCE(*ptep);
  749. if (!pte_present(pte) || !pte_read(pte))
  750. return 0;
  751. if (write && !pte_write(pte))
  752. return 0;
  753. /* hugepages are never "special" */
  754. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  755. refs = 0;
  756. head = pte_page(pte);
  757. page = head + ((addr & (sz-1)) >> PAGE_SHIFT);
  758. do {
  759. VM_BUG_ON(compound_head(page) != head);
  760. pages[*nr] = page;
  761. (*nr)++;
  762. page++;
  763. refs++;
  764. } while (addr += PAGE_SIZE, addr != end);
  765. if (!page_cache_add_speculative(head, refs)) {
  766. *nr -= refs;
  767. return 0;
  768. }
  769. if (unlikely(pte_val(pte) != pte_val(*ptep))) {
  770. /* Could be optimized better */
  771. *nr -= refs;
  772. while (refs--)
  773. put_page(head);
  774. return 0;
  775. }
  776. return 1;
  777. }