hugetlbpage.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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. }
  381. /*
  382. * This function frees user-level page tables of a process.
  383. */
  384. void hugetlb_free_pgd_range(struct mmu_gather *tlb,
  385. unsigned long addr, unsigned long end,
  386. unsigned long floor, unsigned long ceiling)
  387. {
  388. pgd_t *pgd;
  389. unsigned long next;
  390. /*
  391. * Because there are a number of different possible pagetable
  392. * layouts for hugepage ranges, we limit knowledge of how
  393. * things should be laid out to the allocation path
  394. * (huge_pte_alloc(), above). Everything else works out the
  395. * structure as it goes from information in the hugepd
  396. * pointers. That means that we can't here use the
  397. * optimization used in the normal page free_pgd_range(), of
  398. * checking whether we're actually covering a large enough
  399. * range to have to do anything at the top level of the walk
  400. * instead of at the bottom.
  401. *
  402. * To make sense of this, you should probably go read the big
  403. * block comment at the top of the normal free_pgd_range(),
  404. * too.
  405. */
  406. do {
  407. next = pgd_addr_end(addr, end);
  408. pgd = pgd_offset(tlb->mm, addr);
  409. if (!is_hugepd(__hugepd(pgd_val(*pgd)))) {
  410. if (pgd_none_or_clear_bad(pgd))
  411. continue;
  412. hugetlb_free_pud_range(tlb, pgd, addr, next, floor, ceiling);
  413. } else {
  414. unsigned long more;
  415. /*
  416. * Increment next by the size of the huge mapping since
  417. * there may be more than one entry at the pgd level
  418. * for a single hugepage, but all of them point to the
  419. * same kmem cache that holds the hugepte.
  420. */
  421. more = addr + (1 << hugepd_shift(*(hugepd_t *)pgd));
  422. if (more > next)
  423. next = more;
  424. free_hugepd_range(tlb, (hugepd_t *)pgd, PGDIR_SHIFT,
  425. addr, next, floor, ceiling);
  426. }
  427. } while (addr = next, addr != end);
  428. }
  429. struct page *follow_huge_pd(struct vm_area_struct *vma,
  430. unsigned long address, hugepd_t hpd,
  431. int flags, int pdshift)
  432. {
  433. pte_t *ptep;
  434. spinlock_t *ptl;
  435. struct page *page = NULL;
  436. unsigned long mask;
  437. int shift = hugepd_shift(hpd);
  438. struct mm_struct *mm = vma->vm_mm;
  439. retry:
  440. ptl = &mm->page_table_lock;
  441. spin_lock(ptl);
  442. ptep = hugepte_offset(hpd, address, pdshift);
  443. if (pte_present(*ptep)) {
  444. mask = (1UL << shift) - 1;
  445. page = pte_page(*ptep);
  446. page += ((address & mask) >> PAGE_SHIFT);
  447. if (flags & FOLL_GET)
  448. get_page(page);
  449. } else {
  450. if (is_hugetlb_entry_migration(*ptep)) {
  451. spin_unlock(ptl);
  452. __migration_entry_wait(mm, ptep, ptl);
  453. goto retry;
  454. }
  455. }
  456. spin_unlock(ptl);
  457. return page;
  458. }
  459. static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end,
  460. unsigned long sz)
  461. {
  462. unsigned long __boundary = (addr + sz) & ~(sz-1);
  463. return (__boundary - 1 < end - 1) ? __boundary : end;
  464. }
  465. int gup_huge_pd(hugepd_t hugepd, unsigned long addr, unsigned pdshift,
  466. unsigned long end, int write, struct page **pages, int *nr)
  467. {
  468. pte_t *ptep;
  469. unsigned long sz = 1UL << hugepd_shift(hugepd);
  470. unsigned long next;
  471. ptep = hugepte_offset(hugepd, addr, pdshift);
  472. do {
  473. next = hugepte_addr_end(addr, end, sz);
  474. if (!gup_hugepte(ptep, sz, addr, end, write, pages, nr))
  475. return 0;
  476. } while (ptep++, addr = next, addr != end);
  477. return 1;
  478. }
  479. #ifdef CONFIG_PPC_MM_SLICES
  480. unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  481. unsigned long len, unsigned long pgoff,
  482. unsigned long flags)
  483. {
  484. struct hstate *hstate = hstate_file(file);
  485. int mmu_psize = shift_to_mmu_psize(huge_page_shift(hstate));
  486. if (radix_enabled())
  487. return radix__hugetlb_get_unmapped_area(file, addr, len,
  488. pgoff, flags);
  489. return slice_get_unmapped_area(addr, len, flags, mmu_psize, 1);
  490. }
  491. #endif
  492. unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
  493. {
  494. #ifdef CONFIG_PPC_MM_SLICES
  495. unsigned int psize = get_slice_psize(vma->vm_mm, vma->vm_start);
  496. /* With radix we don't use slice, so derive it from vma*/
  497. if (!radix_enabled())
  498. return 1UL << mmu_psize_to_shift(psize);
  499. #endif
  500. if (!is_vm_hugetlb_page(vma))
  501. return PAGE_SIZE;
  502. return huge_page_size(hstate_vma(vma));
  503. }
  504. static inline bool is_power_of_4(unsigned long x)
  505. {
  506. if (is_power_of_2(x))
  507. return (__ilog2(x) % 2) ? false : true;
  508. return false;
  509. }
  510. static int __init add_huge_page_size(unsigned long long size)
  511. {
  512. int shift = __ffs(size);
  513. int mmu_psize;
  514. /* Check that it is a page size supported by the hardware and
  515. * that it fits within pagetable and slice limits. */
  516. if (size <= PAGE_SIZE)
  517. return -EINVAL;
  518. #if defined(CONFIG_PPC_FSL_BOOK3E)
  519. if (!is_power_of_4(size))
  520. return -EINVAL;
  521. #elif !defined(CONFIG_PPC_8xx)
  522. if (!is_power_of_2(size) || (shift > SLICE_HIGH_SHIFT))
  523. return -EINVAL;
  524. #endif
  525. if ((mmu_psize = shift_to_mmu_psize(shift)) < 0)
  526. return -EINVAL;
  527. #ifdef CONFIG_PPC_BOOK3S_64
  528. /*
  529. * We need to make sure that for different page sizes reported by
  530. * firmware we only add hugetlb support for page sizes that can be
  531. * supported by linux page table layout.
  532. * For now we have
  533. * Radix: 2M
  534. * Hash: 16M and 16G
  535. */
  536. if (radix_enabled()) {
  537. if (mmu_psize != MMU_PAGE_2M) {
  538. if (cpu_has_feature(CPU_FTR_POWER9_DD1) ||
  539. (mmu_psize != MMU_PAGE_1G))
  540. return -EINVAL;
  541. }
  542. } else {
  543. if (mmu_psize != MMU_PAGE_16M && mmu_psize != MMU_PAGE_16G)
  544. return -EINVAL;
  545. }
  546. #endif
  547. BUG_ON(mmu_psize_defs[mmu_psize].shift != shift);
  548. /* Return if huge page size has already been setup */
  549. if (size_to_hstate(size))
  550. return 0;
  551. hugetlb_add_hstate(shift - PAGE_SHIFT);
  552. return 0;
  553. }
  554. static int __init hugepage_setup_sz(char *str)
  555. {
  556. unsigned long long size;
  557. size = memparse(str, &str);
  558. if (add_huge_page_size(size) != 0) {
  559. hugetlb_bad_size();
  560. pr_err("Invalid huge page size specified(%llu)\n", size);
  561. }
  562. return 1;
  563. }
  564. __setup("hugepagesz=", hugepage_setup_sz);
  565. struct kmem_cache *hugepte_cache;
  566. static int __init hugetlbpage_init(void)
  567. {
  568. int psize;
  569. #if !defined(CONFIG_PPC_FSL_BOOK3E) && !defined(CONFIG_PPC_8xx)
  570. if (!radix_enabled() && !mmu_has_feature(MMU_FTR_16M_PAGE))
  571. return -ENODEV;
  572. #endif
  573. for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
  574. unsigned shift;
  575. unsigned pdshift;
  576. if (!mmu_psize_defs[psize].shift)
  577. continue;
  578. shift = mmu_psize_to_shift(psize);
  579. if (add_huge_page_size(1ULL << shift) < 0)
  580. continue;
  581. if (shift < HUGEPD_PUD_SHIFT)
  582. pdshift = PMD_SHIFT;
  583. else if (shift < HUGEPD_PGD_SHIFT)
  584. pdshift = PUD_SHIFT;
  585. else
  586. pdshift = PGDIR_SHIFT;
  587. /*
  588. * if we have pdshift and shift value same, we don't
  589. * use pgt cache for hugepd.
  590. */
  591. if (pdshift > shift)
  592. pgtable_cache_add(pdshift - shift, NULL);
  593. #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx)
  594. else if (!hugepte_cache) {
  595. /*
  596. * Create a kmem cache for hugeptes. The bottom bits in
  597. * the pte have size information encoded in them, so
  598. * align them to allow this
  599. */
  600. hugepte_cache = kmem_cache_create("hugepte-cache",
  601. sizeof(pte_t),
  602. HUGEPD_SHIFT_MASK + 1,
  603. 0, NULL);
  604. if (hugepte_cache == NULL)
  605. panic("%s: Unable to create kmem cache "
  606. "for hugeptes\n", __func__);
  607. }
  608. #endif
  609. }
  610. #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx)
  611. /* Default hpage size = 4M on FSL_BOOK3E and 512k on 8xx */
  612. if (mmu_psize_defs[MMU_PAGE_4M].shift)
  613. HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_4M].shift;
  614. else if (mmu_psize_defs[MMU_PAGE_512K].shift)
  615. HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_512K].shift;
  616. #else
  617. /* Set default large page size. Currently, we pick 16M or 1M
  618. * depending on what is available
  619. */
  620. if (mmu_psize_defs[MMU_PAGE_16M].shift)
  621. HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_16M].shift;
  622. else if (mmu_psize_defs[MMU_PAGE_1M].shift)
  623. HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_1M].shift;
  624. else if (mmu_psize_defs[MMU_PAGE_2M].shift)
  625. HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_2M].shift;
  626. #endif
  627. return 0;
  628. }
  629. arch_initcall(hugetlbpage_init);
  630. void flush_dcache_icache_hugepage(struct page *page)
  631. {
  632. int i;
  633. void *start;
  634. BUG_ON(!PageCompound(page));
  635. for (i = 0; i < (1UL << compound_order(page)); i++) {
  636. if (!PageHighMem(page)) {
  637. __flush_dcache_icache(page_address(page+i));
  638. } else {
  639. start = kmap_atomic(page+i);
  640. __flush_dcache_icache(start);
  641. kunmap_atomic(start);
  642. }
  643. }
  644. }
  645. #endif /* CONFIG_HUGETLB_PAGE */
  646. /*
  647. * We have 4 cases for pgds and pmds:
  648. * (1) invalid (all zeroes)
  649. * (2) pointer to next table, as normal; bottom 6 bits == 0
  650. * (3) leaf pte for huge page _PAGE_PTE set
  651. * (4) hugepd pointer, _PAGE_PTE = 0 and bits [2..6] indicate size of table
  652. *
  653. * So long as we atomically load page table pointers we are safe against teardown,
  654. * we can follow the address down to the the page and take a ref on it.
  655. * This function need to be called with interrupts disabled. We use this variant
  656. * when we have MSR[EE] = 0 but the paca->soft_enabled = 1
  657. */
  658. pte_t *__find_linux_pte(pgd_t *pgdir, unsigned long ea,
  659. bool *is_thp, unsigned *hpage_shift)
  660. {
  661. pgd_t pgd, *pgdp;
  662. pud_t pud, *pudp;
  663. pmd_t pmd, *pmdp;
  664. pte_t *ret_pte;
  665. hugepd_t *hpdp = NULL;
  666. unsigned pdshift = PGDIR_SHIFT;
  667. if (hpage_shift)
  668. *hpage_shift = 0;
  669. if (is_thp)
  670. *is_thp = false;
  671. pgdp = pgdir + pgd_index(ea);
  672. pgd = READ_ONCE(*pgdp);
  673. /*
  674. * Always operate on the local stack value. This make sure the
  675. * value don't get updated by a parallel THP split/collapse,
  676. * page fault or a page unmap. The return pte_t * is still not
  677. * stable. So should be checked there for above conditions.
  678. */
  679. if (pgd_none(pgd))
  680. return NULL;
  681. else if (pgd_huge(pgd)) {
  682. ret_pte = (pte_t *) pgdp;
  683. goto out;
  684. } else if (is_hugepd(__hugepd(pgd_val(pgd))))
  685. hpdp = (hugepd_t *)&pgd;
  686. else {
  687. /*
  688. * Even if we end up with an unmap, the pgtable will not
  689. * be freed, because we do an rcu free and here we are
  690. * irq disabled
  691. */
  692. pdshift = PUD_SHIFT;
  693. pudp = pud_offset(&pgd, ea);
  694. pud = READ_ONCE(*pudp);
  695. if (pud_none(pud))
  696. return NULL;
  697. else if (pud_huge(pud)) {
  698. ret_pte = (pte_t *) pudp;
  699. goto out;
  700. } else if (is_hugepd(__hugepd(pud_val(pud))))
  701. hpdp = (hugepd_t *)&pud;
  702. else {
  703. pdshift = PMD_SHIFT;
  704. pmdp = pmd_offset(&pud, ea);
  705. pmd = READ_ONCE(*pmdp);
  706. /*
  707. * A hugepage collapse is captured by pmd_none, because
  708. * it mark the pmd none and do a hpte invalidate.
  709. */
  710. if (pmd_none(pmd))
  711. return NULL;
  712. if (pmd_trans_huge(pmd) || pmd_devmap(pmd)) {
  713. if (is_thp)
  714. *is_thp = true;
  715. ret_pte = (pte_t *) pmdp;
  716. goto out;
  717. }
  718. if (pmd_huge(pmd)) {
  719. ret_pte = (pte_t *) pmdp;
  720. goto out;
  721. } else if (is_hugepd(__hugepd(pmd_val(pmd))))
  722. hpdp = (hugepd_t *)&pmd;
  723. else
  724. return pte_offset_kernel(&pmd, ea);
  725. }
  726. }
  727. if (!hpdp)
  728. return NULL;
  729. ret_pte = hugepte_offset(*hpdp, ea, pdshift);
  730. pdshift = hugepd_shift(*hpdp);
  731. out:
  732. if (hpage_shift)
  733. *hpage_shift = pdshift;
  734. return ret_pte;
  735. }
  736. EXPORT_SYMBOL_GPL(__find_linux_pte);
  737. int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
  738. unsigned long end, int write, struct page **pages, int *nr)
  739. {
  740. unsigned long pte_end;
  741. struct page *head, *page;
  742. pte_t pte;
  743. int refs;
  744. pte_end = (addr + sz) & ~(sz-1);
  745. if (pte_end < end)
  746. end = pte_end;
  747. pte = READ_ONCE(*ptep);
  748. if (!pte_present(pte) || !pte_read(pte))
  749. return 0;
  750. if (write && !pte_write(pte))
  751. return 0;
  752. /* hugepages are never "special" */
  753. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  754. refs = 0;
  755. head = pte_page(pte);
  756. page = head + ((addr & (sz-1)) >> PAGE_SHIFT);
  757. do {
  758. VM_BUG_ON(compound_head(page) != head);
  759. pages[*nr] = page;
  760. (*nr)++;
  761. page++;
  762. refs++;
  763. } while (addr += PAGE_SIZE, addr != end);
  764. if (!page_cache_add_speculative(head, refs)) {
  765. *nr -= refs;
  766. return 0;
  767. }
  768. if (unlikely(pte_val(pte) != pte_val(*ptep))) {
  769. /* Could be optimized better */
  770. *nr -= refs;
  771. while (refs--)
  772. put_page(head);
  773. return 0;
  774. }
  775. return 1;
  776. }