hugetlbpage.c 22 KB

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