hugetlbpage.c 22 KB

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