hugetlbpage.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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 <asm/pgtable.h>
  20. #include <asm/pgalloc.h>
  21. #include <asm/tlb.h>
  22. #include <asm/setup.h>
  23. #include <asm/hugetlb.h>
  24. #ifdef CONFIG_HUGETLB_PAGE
  25. #define PAGE_SHIFT_64K 16
  26. #define PAGE_SHIFT_16M 24
  27. #define PAGE_SHIFT_16G 34
  28. unsigned int HPAGE_SHIFT;
  29. /*
  30. * Tracks gpages after the device tree is scanned and before the
  31. * huge_boot_pages list is ready. On non-Freescale implementations, this is
  32. * just used to track 16G pages and so is a single array. FSL-based
  33. * implementations may have more than one gpage size, so we need multiple
  34. * arrays
  35. */
  36. #ifdef CONFIG_PPC_FSL_BOOK3E
  37. #define MAX_NUMBER_GPAGES 128
  38. struct psize_gpages {
  39. u64 gpage_list[MAX_NUMBER_GPAGES];
  40. unsigned int nr_gpages;
  41. };
  42. static struct psize_gpages gpage_freearray[MMU_PAGE_COUNT];
  43. #else
  44. #define MAX_NUMBER_GPAGES 1024
  45. static u64 gpage_freearray[MAX_NUMBER_GPAGES];
  46. static unsigned nr_gpages;
  47. #endif
  48. #define hugepd_none(hpd) ((hpd).pd == 0)
  49. #ifdef CONFIG_PPC_BOOK3S_64
  50. /*
  51. * At this point we do the placement change only for BOOK3S 64. This would
  52. * possibly work on other subarchs.
  53. */
  54. /*
  55. * We have PGD_INDEX_SIZ = 12 and PTE_INDEX_SIZE = 8, so that we can have
  56. * 16GB hugepage pte in PGD and 16MB hugepage pte at PMD;
  57. *
  58. * Defined in such a way that we can optimize away code block at build time
  59. * if CONFIG_HUGETLB_PAGE=n.
  60. */
  61. int pmd_huge(pmd_t pmd)
  62. {
  63. /*
  64. * leaf pte for huge page, bottom two bits != 00
  65. */
  66. return ((pmd_val(pmd) & 0x3) != 0x0);
  67. }
  68. int pud_huge(pud_t pud)
  69. {
  70. /*
  71. * leaf pte for huge page, bottom two bits != 00
  72. */
  73. return ((pud_val(pud) & 0x3) != 0x0);
  74. }
  75. int pgd_huge(pgd_t pgd)
  76. {
  77. /*
  78. * leaf pte for huge page, bottom two bits != 00
  79. */
  80. return ((pgd_val(pgd) & 0x3) != 0x0);
  81. }
  82. #else
  83. int pmd_huge(pmd_t pmd)
  84. {
  85. return 0;
  86. }
  87. int pud_huge(pud_t pud)
  88. {
  89. return 0;
  90. }
  91. int pgd_huge(pgd_t pgd)
  92. {
  93. return 0;
  94. }
  95. #endif
  96. pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
  97. {
  98. /* Only called for hugetlbfs pages, hence can ignore THP */
  99. return __find_linux_pte_or_hugepte(mm->pgd, addr, NULL);
  100. }
  101. static int __hugepte_alloc(struct mm_struct *mm, hugepd_t *hpdp,
  102. unsigned long address, unsigned pdshift, unsigned pshift)
  103. {
  104. struct kmem_cache *cachep;
  105. pte_t *new;
  106. #ifdef CONFIG_PPC_FSL_BOOK3E
  107. int i;
  108. int num_hugepd = 1 << (pshift - pdshift);
  109. cachep = hugepte_cache;
  110. #else
  111. cachep = PGT_CACHE(pdshift - pshift);
  112. #endif
  113. new = kmem_cache_zalloc(cachep, GFP_KERNEL|__GFP_REPEAT);
  114. BUG_ON(pshift > HUGEPD_SHIFT_MASK);
  115. BUG_ON((unsigned long)new & HUGEPD_SHIFT_MASK);
  116. if (! new)
  117. return -ENOMEM;
  118. spin_lock(&mm->page_table_lock);
  119. #ifdef CONFIG_PPC_FSL_BOOK3E
  120. /*
  121. * We have multiple higher-level entries that point to the same
  122. * actual pte location. Fill in each as we go and backtrack on error.
  123. * We need all of these so the DTLB pgtable walk code can find the
  124. * right higher-level entry without knowing if it's a hugepage or not.
  125. */
  126. for (i = 0; i < num_hugepd; i++, hpdp++) {
  127. if (unlikely(!hugepd_none(*hpdp)))
  128. break;
  129. else
  130. /* We use the old format for PPC_FSL_BOOK3E */
  131. hpdp->pd = ((unsigned long)new & ~PD_HUGE) | pshift;
  132. }
  133. /* If we bailed from the for loop early, an error occurred, clean up */
  134. if (i < num_hugepd) {
  135. for (i = i - 1 ; i >= 0; i--, hpdp--)
  136. hpdp->pd = 0;
  137. kmem_cache_free(cachep, new);
  138. }
  139. #else
  140. if (!hugepd_none(*hpdp))
  141. kmem_cache_free(cachep, new);
  142. else {
  143. #ifdef CONFIG_PPC_BOOK3S_64
  144. hpdp->pd = (unsigned long)new |
  145. (shift_to_mmu_psize(pshift) << 2);
  146. #else
  147. hpdp->pd = ((unsigned long)new & ~PD_HUGE) | pshift;
  148. #endif
  149. }
  150. #endif
  151. spin_unlock(&mm->page_table_lock);
  152. return 0;
  153. }
  154. /*
  155. * These macros define how to determine which level of the page table holds
  156. * the hpdp.
  157. */
  158. #ifdef CONFIG_PPC_FSL_BOOK3E
  159. #define HUGEPD_PGD_SHIFT PGDIR_SHIFT
  160. #define HUGEPD_PUD_SHIFT PUD_SHIFT
  161. #else
  162. #define HUGEPD_PGD_SHIFT PUD_SHIFT
  163. #define HUGEPD_PUD_SHIFT PMD_SHIFT
  164. #endif
  165. #ifdef CONFIG_PPC_BOOK3S_64
  166. /*
  167. * At this point we do the placement change only for BOOK3S 64. This would
  168. * possibly work on other subarchs.
  169. */
  170. pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr, unsigned long sz)
  171. {
  172. pgd_t *pg;
  173. pud_t *pu;
  174. pmd_t *pm;
  175. hugepd_t *hpdp = NULL;
  176. unsigned pshift = __ffs(sz);
  177. unsigned pdshift = PGDIR_SHIFT;
  178. addr &= ~(sz-1);
  179. pg = pgd_offset(mm, addr);
  180. if (pshift == PGDIR_SHIFT)
  181. /* 16GB huge page */
  182. return (pte_t *) pg;
  183. else if (pshift > PUD_SHIFT)
  184. /*
  185. * We need to use hugepd table
  186. */
  187. hpdp = (hugepd_t *)pg;
  188. else {
  189. pdshift = PUD_SHIFT;
  190. pu = pud_alloc(mm, pg, addr);
  191. if (pshift == PUD_SHIFT)
  192. return (pte_t *)pu;
  193. else if (pshift > PMD_SHIFT)
  194. hpdp = (hugepd_t *)pu;
  195. else {
  196. pdshift = PMD_SHIFT;
  197. pm = pmd_alloc(mm, pu, addr);
  198. if (pshift == PMD_SHIFT)
  199. /* 16MB hugepage */
  200. return (pte_t *)pm;
  201. else
  202. hpdp = (hugepd_t *)pm;
  203. }
  204. }
  205. if (!hpdp)
  206. return NULL;
  207. BUG_ON(!hugepd_none(*hpdp) && !hugepd_ok(*hpdp));
  208. if (hugepd_none(*hpdp) && __hugepte_alloc(mm, hpdp, addr, pdshift, pshift))
  209. return NULL;
  210. return hugepte_offset(*hpdp, addr, pdshift);
  211. }
  212. #else
  213. pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr, unsigned long sz)
  214. {
  215. pgd_t *pg;
  216. pud_t *pu;
  217. pmd_t *pm;
  218. hugepd_t *hpdp = NULL;
  219. unsigned pshift = __ffs(sz);
  220. unsigned pdshift = PGDIR_SHIFT;
  221. addr &= ~(sz-1);
  222. pg = pgd_offset(mm, addr);
  223. if (pshift >= HUGEPD_PGD_SHIFT) {
  224. hpdp = (hugepd_t *)pg;
  225. } else {
  226. pdshift = PUD_SHIFT;
  227. pu = pud_alloc(mm, pg, addr);
  228. if (pshift >= HUGEPD_PUD_SHIFT) {
  229. hpdp = (hugepd_t *)pu;
  230. } else {
  231. pdshift = PMD_SHIFT;
  232. pm = pmd_alloc(mm, pu, addr);
  233. hpdp = (hugepd_t *)pm;
  234. }
  235. }
  236. if (!hpdp)
  237. return NULL;
  238. BUG_ON(!hugepd_none(*hpdp) && !hugepd_ok(*hpdp));
  239. if (hugepd_none(*hpdp) && __hugepte_alloc(mm, hpdp, addr, pdshift, pshift))
  240. return NULL;
  241. return hugepte_offset(*hpdp, addr, pdshift);
  242. }
  243. #endif
  244. #ifdef CONFIG_PPC_FSL_BOOK3E
  245. /* Build list of addresses of gigantic pages. This function is used in early
  246. * boot before the buddy allocator is setup.
  247. */
  248. void add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages)
  249. {
  250. unsigned int idx = shift_to_mmu_psize(__ffs(page_size));
  251. int i;
  252. if (addr == 0)
  253. return;
  254. gpage_freearray[idx].nr_gpages = number_of_pages;
  255. for (i = 0; i < number_of_pages; i++) {
  256. gpage_freearray[idx].gpage_list[i] = addr;
  257. addr += page_size;
  258. }
  259. }
  260. /*
  261. * Moves the gigantic page addresses from the temporary list to the
  262. * huge_boot_pages list.
  263. */
  264. int alloc_bootmem_huge_page(struct hstate *hstate)
  265. {
  266. struct huge_bootmem_page *m;
  267. int idx = shift_to_mmu_psize(huge_page_shift(hstate));
  268. int nr_gpages = gpage_freearray[idx].nr_gpages;
  269. if (nr_gpages == 0)
  270. return 0;
  271. #ifdef CONFIG_HIGHMEM
  272. /*
  273. * If gpages can be in highmem we can't use the trick of storing the
  274. * data structure in the page; allocate space for this
  275. */
  276. m = memblock_virt_alloc(sizeof(struct huge_bootmem_page), 0);
  277. m->phys = gpage_freearray[idx].gpage_list[--nr_gpages];
  278. #else
  279. m = phys_to_virt(gpage_freearray[idx].gpage_list[--nr_gpages]);
  280. #endif
  281. list_add(&m->list, &huge_boot_pages);
  282. gpage_freearray[idx].nr_gpages = nr_gpages;
  283. gpage_freearray[idx].gpage_list[nr_gpages] = 0;
  284. m->hstate = hstate;
  285. return 1;
  286. }
  287. /*
  288. * Scan the command line hugepagesz= options for gigantic pages; store those in
  289. * a list that we use to allocate the memory once all options are parsed.
  290. */
  291. unsigned long gpage_npages[MMU_PAGE_COUNT];
  292. static int __init do_gpage_early_setup(char *param, char *val,
  293. const char *unused)
  294. {
  295. static phys_addr_t size;
  296. unsigned long npages;
  297. /*
  298. * The hugepagesz and hugepages cmdline options are interleaved. We
  299. * use the size variable to keep track of whether or not this was done
  300. * properly and skip over instances where it is incorrect. Other
  301. * command-line parsing code will issue warnings, so we don't need to.
  302. *
  303. */
  304. if ((strcmp(param, "default_hugepagesz") == 0) ||
  305. (strcmp(param, "hugepagesz") == 0)) {
  306. size = memparse(val, NULL);
  307. } else if (strcmp(param, "hugepages") == 0) {
  308. if (size != 0) {
  309. if (sscanf(val, "%lu", &npages) <= 0)
  310. npages = 0;
  311. if (npages > MAX_NUMBER_GPAGES) {
  312. pr_warn("MMU: %lu pages requested for page "
  313. "size %llu KB, limiting to "
  314. __stringify(MAX_NUMBER_GPAGES) "\n",
  315. npages, size / 1024);
  316. npages = MAX_NUMBER_GPAGES;
  317. }
  318. gpage_npages[shift_to_mmu_psize(__ffs(size))] = npages;
  319. size = 0;
  320. }
  321. }
  322. return 0;
  323. }
  324. /*
  325. * This function allocates physical space for pages that are larger than the
  326. * buddy allocator can handle. We want to allocate these in highmem because
  327. * the amount of lowmem is limited. This means that this function MUST be
  328. * called before lowmem_end_addr is set up in MMU_init() in order for the lmb
  329. * allocate to grab highmem.
  330. */
  331. void __init reserve_hugetlb_gpages(void)
  332. {
  333. static __initdata char cmdline[COMMAND_LINE_SIZE];
  334. phys_addr_t size, base;
  335. int i;
  336. strlcpy(cmdline, boot_command_line, COMMAND_LINE_SIZE);
  337. parse_args("hugetlb gpages", cmdline, NULL, 0, 0, 0,
  338. &do_gpage_early_setup);
  339. /*
  340. * Walk gpage list in reverse, allocating larger page sizes first.
  341. * Skip over unsupported sizes, or sizes that have 0 gpages allocated.
  342. * When we reach the point in the list where pages are no longer
  343. * considered gpages, we're done.
  344. */
  345. for (i = MMU_PAGE_COUNT-1; i >= 0; i--) {
  346. if (mmu_psize_defs[i].shift == 0 || gpage_npages[i] == 0)
  347. continue;
  348. else if (mmu_psize_to_shift(i) < (MAX_ORDER + PAGE_SHIFT))
  349. break;
  350. size = (phys_addr_t)(1ULL << mmu_psize_to_shift(i));
  351. base = memblock_alloc_base(size * gpage_npages[i], size,
  352. MEMBLOCK_ALLOC_ANYWHERE);
  353. add_gpage(base, size, gpage_npages[i]);
  354. }
  355. }
  356. #else /* !PPC_FSL_BOOK3E */
  357. /* Build list of addresses of gigantic pages. This function is used in early
  358. * boot before the buddy allocator is setup.
  359. */
  360. void add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages)
  361. {
  362. if (!addr)
  363. return;
  364. while (number_of_pages > 0) {
  365. gpage_freearray[nr_gpages] = addr;
  366. nr_gpages++;
  367. number_of_pages--;
  368. addr += page_size;
  369. }
  370. }
  371. /* Moves the gigantic page addresses from the temporary list to the
  372. * huge_boot_pages list.
  373. */
  374. int alloc_bootmem_huge_page(struct hstate *hstate)
  375. {
  376. struct huge_bootmem_page *m;
  377. if (nr_gpages == 0)
  378. return 0;
  379. m = phys_to_virt(gpage_freearray[--nr_gpages]);
  380. gpage_freearray[nr_gpages] = 0;
  381. list_add(&m->list, &huge_boot_pages);
  382. m->hstate = hstate;
  383. return 1;
  384. }
  385. #endif
  386. #ifdef CONFIG_PPC_FSL_BOOK3E
  387. #define HUGEPD_FREELIST_SIZE \
  388. ((PAGE_SIZE - sizeof(struct hugepd_freelist)) / sizeof(pte_t))
  389. struct hugepd_freelist {
  390. struct rcu_head rcu;
  391. unsigned int index;
  392. void *ptes[0];
  393. };
  394. static DEFINE_PER_CPU(struct hugepd_freelist *, hugepd_freelist_cur);
  395. static void hugepd_free_rcu_callback(struct rcu_head *head)
  396. {
  397. struct hugepd_freelist *batch =
  398. container_of(head, struct hugepd_freelist, rcu);
  399. unsigned int i;
  400. for (i = 0; i < batch->index; i++)
  401. kmem_cache_free(hugepte_cache, batch->ptes[i]);
  402. free_page((unsigned long)batch);
  403. }
  404. static void hugepd_free(struct mmu_gather *tlb, void *hugepte)
  405. {
  406. struct hugepd_freelist **batchp;
  407. batchp = this_cpu_ptr(&hugepd_freelist_cur);
  408. if (atomic_read(&tlb->mm->mm_users) < 2 ||
  409. cpumask_equal(mm_cpumask(tlb->mm),
  410. cpumask_of(smp_processor_id()))) {
  411. kmem_cache_free(hugepte_cache, hugepte);
  412. put_cpu_var(hugepd_freelist_cur);
  413. return;
  414. }
  415. if (*batchp == NULL) {
  416. *batchp = (struct hugepd_freelist *)__get_free_page(GFP_ATOMIC);
  417. (*batchp)->index = 0;
  418. }
  419. (*batchp)->ptes[(*batchp)->index++] = hugepte;
  420. if ((*batchp)->index == HUGEPD_FREELIST_SIZE) {
  421. call_rcu_sched(&(*batchp)->rcu, hugepd_free_rcu_callback);
  422. *batchp = NULL;
  423. }
  424. put_cpu_var(hugepd_freelist_cur);
  425. }
  426. #endif
  427. static void free_hugepd_range(struct mmu_gather *tlb, hugepd_t *hpdp, int pdshift,
  428. unsigned long start, unsigned long end,
  429. unsigned long floor, unsigned long ceiling)
  430. {
  431. pte_t *hugepte = hugepd_page(*hpdp);
  432. int i;
  433. unsigned long pdmask = ~((1UL << pdshift) - 1);
  434. unsigned int num_hugepd = 1;
  435. #ifdef CONFIG_PPC_FSL_BOOK3E
  436. /* Note: On fsl the hpdp may be the first of several */
  437. num_hugepd = (1 << (hugepd_shift(*hpdp) - pdshift));
  438. #else
  439. unsigned int shift = hugepd_shift(*hpdp);
  440. #endif
  441. start &= pdmask;
  442. if (start < floor)
  443. return;
  444. if (ceiling) {
  445. ceiling &= pdmask;
  446. if (! ceiling)
  447. return;
  448. }
  449. if (end - 1 > ceiling - 1)
  450. return;
  451. for (i = 0; i < num_hugepd; i++, hpdp++)
  452. hpdp->pd = 0;
  453. #ifdef CONFIG_PPC_FSL_BOOK3E
  454. hugepd_free(tlb, hugepte);
  455. #else
  456. pgtable_free_tlb(tlb, hugepte, pdshift - shift);
  457. #endif
  458. }
  459. static void hugetlb_free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
  460. unsigned long addr, unsigned long end,
  461. unsigned long floor, unsigned long ceiling)
  462. {
  463. pmd_t *pmd;
  464. unsigned long next;
  465. unsigned long start;
  466. start = addr;
  467. do {
  468. pmd = pmd_offset(pud, addr);
  469. next = pmd_addr_end(addr, end);
  470. if (!is_hugepd(__hugepd(pmd_val(*pmd)))) {
  471. /*
  472. * if it is not hugepd pointer, we should already find
  473. * it cleared.
  474. */
  475. WARN_ON(!pmd_none_or_clear_bad(pmd));
  476. continue;
  477. }
  478. #ifdef CONFIG_PPC_FSL_BOOK3E
  479. /*
  480. * Increment next by the size of the huge mapping since
  481. * there may be more than one entry at this level for a
  482. * single hugepage, but all of them point to
  483. * the same kmem cache that holds the hugepte.
  484. */
  485. next = addr + (1 << hugepd_shift(*(hugepd_t *)pmd));
  486. #endif
  487. free_hugepd_range(tlb, (hugepd_t *)pmd, PMD_SHIFT,
  488. addr, next, floor, ceiling);
  489. } while (addr = next, addr != end);
  490. start &= PUD_MASK;
  491. if (start < floor)
  492. return;
  493. if (ceiling) {
  494. ceiling &= PUD_MASK;
  495. if (!ceiling)
  496. return;
  497. }
  498. if (end - 1 > ceiling - 1)
  499. return;
  500. pmd = pmd_offset(pud, start);
  501. pud_clear(pud);
  502. pmd_free_tlb(tlb, pmd, start);
  503. mm_dec_nr_pmds(tlb->mm);
  504. }
  505. static void hugetlb_free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
  506. unsigned long addr, unsigned long end,
  507. unsigned long floor, unsigned long ceiling)
  508. {
  509. pud_t *pud;
  510. unsigned long next;
  511. unsigned long start;
  512. start = addr;
  513. do {
  514. pud = pud_offset(pgd, addr);
  515. next = pud_addr_end(addr, end);
  516. if (!is_hugepd(__hugepd(pud_val(*pud)))) {
  517. if (pud_none_or_clear_bad(pud))
  518. continue;
  519. hugetlb_free_pmd_range(tlb, pud, addr, next, floor,
  520. ceiling);
  521. } else {
  522. #ifdef CONFIG_PPC_FSL_BOOK3E
  523. /*
  524. * Increment next by the size of the huge mapping since
  525. * there may be more than one entry at this level for a
  526. * single hugepage, but all of them point to
  527. * the same kmem cache that holds the hugepte.
  528. */
  529. next = addr + (1 << hugepd_shift(*(hugepd_t *)pud));
  530. #endif
  531. free_hugepd_range(tlb, (hugepd_t *)pud, PUD_SHIFT,
  532. addr, next, floor, ceiling);
  533. }
  534. } while (addr = next, addr != end);
  535. start &= PGDIR_MASK;
  536. if (start < floor)
  537. return;
  538. if (ceiling) {
  539. ceiling &= PGDIR_MASK;
  540. if (!ceiling)
  541. return;
  542. }
  543. if (end - 1 > ceiling - 1)
  544. return;
  545. pud = pud_offset(pgd, start);
  546. pgd_clear(pgd);
  547. pud_free_tlb(tlb, pud, start);
  548. }
  549. /*
  550. * This function frees user-level page tables of a process.
  551. */
  552. void hugetlb_free_pgd_range(struct mmu_gather *tlb,
  553. unsigned long addr, unsigned long end,
  554. unsigned long floor, unsigned long ceiling)
  555. {
  556. pgd_t *pgd;
  557. unsigned long next;
  558. /*
  559. * Because there are a number of different possible pagetable
  560. * layouts for hugepage ranges, we limit knowledge of how
  561. * things should be laid out to the allocation path
  562. * (huge_pte_alloc(), above). Everything else works out the
  563. * structure as it goes from information in the hugepd
  564. * pointers. That means that we can't here use the
  565. * optimization used in the normal page free_pgd_range(), of
  566. * checking whether we're actually covering a large enough
  567. * range to have to do anything at the top level of the walk
  568. * instead of at the bottom.
  569. *
  570. * To make sense of this, you should probably go read the big
  571. * block comment at the top of the normal free_pgd_range(),
  572. * too.
  573. */
  574. do {
  575. next = pgd_addr_end(addr, end);
  576. pgd = pgd_offset(tlb->mm, addr);
  577. if (!is_hugepd(__hugepd(pgd_val(*pgd)))) {
  578. if (pgd_none_or_clear_bad(pgd))
  579. continue;
  580. hugetlb_free_pud_range(tlb, pgd, addr, next, floor, ceiling);
  581. } else {
  582. #ifdef CONFIG_PPC_FSL_BOOK3E
  583. /*
  584. * Increment next by the size of the huge mapping since
  585. * there may be more than one entry at the pgd level
  586. * for a single hugepage, but all of them point to the
  587. * same kmem cache that holds the hugepte.
  588. */
  589. next = addr + (1 << hugepd_shift(*(hugepd_t *)pgd));
  590. #endif
  591. free_hugepd_range(tlb, (hugepd_t *)pgd, PGDIR_SHIFT,
  592. addr, next, floor, ceiling);
  593. }
  594. } while (addr = next, addr != end);
  595. }
  596. /*
  597. * We are holding mmap_sem, so a parallel huge page collapse cannot run.
  598. * To prevent hugepage split, disable irq.
  599. */
  600. struct page *
  601. follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
  602. {
  603. pte_t *ptep, pte;
  604. unsigned shift;
  605. unsigned long mask, flags;
  606. struct page *page = ERR_PTR(-EINVAL);
  607. local_irq_save(flags);
  608. ptep = find_linux_pte_or_hugepte(mm->pgd, address, &shift);
  609. if (!ptep)
  610. goto no_page;
  611. pte = READ_ONCE(*ptep);
  612. /*
  613. * Verify it is a huge page else bail.
  614. * Transparent hugepages are handled by generic code. We can skip them
  615. * here.
  616. */
  617. if (!shift || pmd_trans_huge(__pmd(pte_val(pte))))
  618. goto no_page;
  619. if (!pte_present(pte)) {
  620. page = NULL;
  621. goto no_page;
  622. }
  623. mask = (1UL << shift) - 1;
  624. page = pte_page(pte);
  625. if (page)
  626. page += (address & mask) / PAGE_SIZE;
  627. no_page:
  628. local_irq_restore(flags);
  629. return page;
  630. }
  631. struct page *
  632. follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  633. pmd_t *pmd, int write)
  634. {
  635. BUG();
  636. return NULL;
  637. }
  638. struct page *
  639. follow_huge_pud(struct mm_struct *mm, unsigned long address,
  640. pud_t *pud, int write)
  641. {
  642. BUG();
  643. return NULL;
  644. }
  645. static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end,
  646. unsigned long sz)
  647. {
  648. unsigned long __boundary = (addr + sz) & ~(sz-1);
  649. return (__boundary - 1 < end - 1) ? __boundary : end;
  650. }
  651. int gup_huge_pd(hugepd_t hugepd, unsigned long addr, unsigned pdshift,
  652. unsigned long end, int write, struct page **pages, int *nr)
  653. {
  654. pte_t *ptep;
  655. unsigned long sz = 1UL << hugepd_shift(hugepd);
  656. unsigned long next;
  657. ptep = hugepte_offset(hugepd, addr, pdshift);
  658. do {
  659. next = hugepte_addr_end(addr, end, sz);
  660. if (!gup_hugepte(ptep, sz, addr, end, write, pages, nr))
  661. return 0;
  662. } while (ptep++, addr = next, addr != end);
  663. return 1;
  664. }
  665. #ifdef CONFIG_PPC_MM_SLICES
  666. unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  667. unsigned long len, unsigned long pgoff,
  668. unsigned long flags)
  669. {
  670. struct hstate *hstate = hstate_file(file);
  671. int mmu_psize = shift_to_mmu_psize(huge_page_shift(hstate));
  672. return slice_get_unmapped_area(addr, len, flags, mmu_psize, 1);
  673. }
  674. #endif
  675. unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
  676. {
  677. #ifdef CONFIG_PPC_MM_SLICES
  678. unsigned int psize = get_slice_psize(vma->vm_mm, vma->vm_start);
  679. return 1UL << mmu_psize_to_shift(psize);
  680. #else
  681. if (!is_vm_hugetlb_page(vma))
  682. return PAGE_SIZE;
  683. return huge_page_size(hstate_vma(vma));
  684. #endif
  685. }
  686. static inline bool is_power_of_4(unsigned long x)
  687. {
  688. if (is_power_of_2(x))
  689. return (__ilog2(x) % 2) ? false : true;
  690. return false;
  691. }
  692. static int __init add_huge_page_size(unsigned long long size)
  693. {
  694. int shift = __ffs(size);
  695. int mmu_psize;
  696. /* Check that it is a page size supported by the hardware and
  697. * that it fits within pagetable and slice limits. */
  698. #ifdef CONFIG_PPC_FSL_BOOK3E
  699. if ((size < PAGE_SIZE) || !is_power_of_4(size))
  700. return -EINVAL;
  701. #else
  702. if (!is_power_of_2(size)
  703. || (shift > SLICE_HIGH_SHIFT) || (shift <= PAGE_SHIFT))
  704. return -EINVAL;
  705. #endif
  706. if ((mmu_psize = shift_to_mmu_psize(shift)) < 0)
  707. return -EINVAL;
  708. #ifdef CONFIG_SPU_FS_64K_LS
  709. /* Disable support for 64K huge pages when 64K SPU local store
  710. * support is enabled as the current implementation conflicts.
  711. */
  712. if (shift == PAGE_SHIFT_64K)
  713. return -EINVAL;
  714. #endif /* CONFIG_SPU_FS_64K_LS */
  715. BUG_ON(mmu_psize_defs[mmu_psize].shift != shift);
  716. /* Return if huge page size has already been setup */
  717. if (size_to_hstate(size))
  718. return 0;
  719. hugetlb_add_hstate(shift - PAGE_SHIFT);
  720. return 0;
  721. }
  722. static int __init hugepage_setup_sz(char *str)
  723. {
  724. unsigned long long size;
  725. size = memparse(str, &str);
  726. if (add_huge_page_size(size) != 0)
  727. printk(KERN_WARNING "Invalid huge page size specified(%llu)\n", size);
  728. return 1;
  729. }
  730. __setup("hugepagesz=", hugepage_setup_sz);
  731. #ifdef CONFIG_PPC_FSL_BOOK3E
  732. struct kmem_cache *hugepte_cache;
  733. static int __init hugetlbpage_init(void)
  734. {
  735. int psize;
  736. for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
  737. unsigned shift;
  738. if (!mmu_psize_defs[psize].shift)
  739. continue;
  740. shift = mmu_psize_to_shift(psize);
  741. /* Don't treat normal page sizes as huge... */
  742. if (shift != PAGE_SHIFT)
  743. if (add_huge_page_size(1ULL << shift) < 0)
  744. continue;
  745. }
  746. /*
  747. * Create a kmem cache for hugeptes. The bottom bits in the pte have
  748. * size information encoded in them, so align them to allow this
  749. */
  750. hugepte_cache = kmem_cache_create("hugepte-cache", sizeof(pte_t),
  751. HUGEPD_SHIFT_MASK + 1, 0, NULL);
  752. if (hugepte_cache == NULL)
  753. panic("%s: Unable to create kmem cache for hugeptes\n",
  754. __func__);
  755. /* Default hpage size = 4M */
  756. if (mmu_psize_defs[MMU_PAGE_4M].shift)
  757. HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_4M].shift;
  758. else
  759. panic("%s: Unable to set default huge page size\n", __func__);
  760. return 0;
  761. }
  762. #else
  763. static int __init hugetlbpage_init(void)
  764. {
  765. int psize;
  766. if (!mmu_has_feature(MMU_FTR_16M_PAGE))
  767. return -ENODEV;
  768. for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
  769. unsigned shift;
  770. unsigned pdshift;
  771. if (!mmu_psize_defs[psize].shift)
  772. continue;
  773. shift = mmu_psize_to_shift(psize);
  774. if (add_huge_page_size(1ULL << shift) < 0)
  775. continue;
  776. if (shift < PMD_SHIFT)
  777. pdshift = PMD_SHIFT;
  778. else if (shift < PUD_SHIFT)
  779. pdshift = PUD_SHIFT;
  780. else
  781. pdshift = PGDIR_SHIFT;
  782. /*
  783. * if we have pdshift and shift value same, we don't
  784. * use pgt cache for hugepd.
  785. */
  786. if (pdshift != shift) {
  787. pgtable_cache_add(pdshift - shift, NULL);
  788. if (!PGT_CACHE(pdshift - shift))
  789. panic("hugetlbpage_init(): could not create "
  790. "pgtable cache for %d bit pagesize\n", shift);
  791. }
  792. }
  793. /* Set default large page size. Currently, we pick 16M or 1M
  794. * depending on what is available
  795. */
  796. if (mmu_psize_defs[MMU_PAGE_16M].shift)
  797. HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_16M].shift;
  798. else if (mmu_psize_defs[MMU_PAGE_1M].shift)
  799. HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_1M].shift;
  800. return 0;
  801. }
  802. #endif
  803. module_init(hugetlbpage_init);
  804. void flush_dcache_icache_hugepage(struct page *page)
  805. {
  806. int i;
  807. void *start;
  808. BUG_ON(!PageCompound(page));
  809. for (i = 0; i < (1UL << compound_order(page)); i++) {
  810. if (!PageHighMem(page)) {
  811. __flush_dcache_icache(page_address(page+i));
  812. } else {
  813. start = kmap_atomic(page+i);
  814. __flush_dcache_icache(start);
  815. kunmap_atomic(start);
  816. }
  817. }
  818. }
  819. #endif /* CONFIG_HUGETLB_PAGE */
  820. /*
  821. * We have 4 cases for pgds and pmds:
  822. * (1) invalid (all zeroes)
  823. * (2) pointer to next table, as normal; bottom 6 bits == 0
  824. * (3) leaf pte for huge page, bottom two bits != 00
  825. * (4) hugepd pointer, bottom two bits == 00, next 4 bits indicate size of table
  826. *
  827. * So long as we atomically load page table pointers we are safe against teardown,
  828. * we can follow the address down to the the page and take a ref on it.
  829. * This function need to be called with interrupts disabled. We use this variant
  830. * when we have MSR[EE] = 0 but the paca->soft_enabled = 1
  831. */
  832. pte_t *__find_linux_pte_or_hugepte(pgd_t *pgdir, unsigned long ea,
  833. unsigned *shift)
  834. {
  835. pgd_t pgd, *pgdp;
  836. pud_t pud, *pudp;
  837. pmd_t pmd, *pmdp;
  838. pte_t *ret_pte;
  839. hugepd_t *hpdp = NULL;
  840. unsigned pdshift = PGDIR_SHIFT;
  841. if (shift)
  842. *shift = 0;
  843. pgdp = pgdir + pgd_index(ea);
  844. pgd = READ_ONCE(*pgdp);
  845. /*
  846. * Always operate on the local stack value. This make sure the
  847. * value don't get updated by a parallel THP split/collapse,
  848. * page fault or a page unmap. The return pte_t * is still not
  849. * stable. So should be checked there for above conditions.
  850. */
  851. if (pgd_none(pgd))
  852. return NULL;
  853. else if (pgd_huge(pgd)) {
  854. ret_pte = (pte_t *) pgdp;
  855. goto out;
  856. } else if (is_hugepd(__hugepd(pgd_val(pgd))))
  857. hpdp = (hugepd_t *)&pgd;
  858. else {
  859. /*
  860. * Even if we end up with an unmap, the pgtable will not
  861. * be freed, because we do an rcu free and here we are
  862. * irq disabled
  863. */
  864. pdshift = PUD_SHIFT;
  865. pudp = pud_offset(&pgd, ea);
  866. pud = READ_ONCE(*pudp);
  867. if (pud_none(pud))
  868. return NULL;
  869. else if (pud_huge(pud)) {
  870. ret_pte = (pte_t *) pudp;
  871. goto out;
  872. } else if (is_hugepd(__hugepd(pud_val(pud))))
  873. hpdp = (hugepd_t *)&pud;
  874. else {
  875. pdshift = PMD_SHIFT;
  876. pmdp = pmd_offset(&pud, ea);
  877. pmd = READ_ONCE(*pmdp);
  878. /*
  879. * A hugepage collapse is captured by pmd_none, because
  880. * it mark the pmd none and do a hpte invalidate.
  881. *
  882. * We don't worry about pmd_trans_splitting here, The
  883. * caller if it needs to handle the splitting case
  884. * should check for that.
  885. */
  886. if (pmd_none(pmd))
  887. return NULL;
  888. if (pmd_huge(pmd) || pmd_large(pmd)) {
  889. ret_pte = (pte_t *) pmdp;
  890. goto out;
  891. } else if (is_hugepd(__hugepd(pmd_val(pmd))))
  892. hpdp = (hugepd_t *)&pmd;
  893. else
  894. return pte_offset_kernel(&pmd, ea);
  895. }
  896. }
  897. if (!hpdp)
  898. return NULL;
  899. ret_pte = hugepte_offset(*hpdp, ea, pdshift);
  900. pdshift = hugepd_shift(*hpdp);
  901. out:
  902. if (shift)
  903. *shift = pdshift;
  904. return ret_pte;
  905. }
  906. EXPORT_SYMBOL_GPL(__find_linux_pte_or_hugepte);
  907. int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
  908. unsigned long end, int write, struct page **pages, int *nr)
  909. {
  910. unsigned long mask;
  911. unsigned long pte_end;
  912. struct page *head, *page, *tail;
  913. pte_t pte;
  914. int refs;
  915. pte_end = (addr + sz) & ~(sz-1);
  916. if (pte_end < end)
  917. end = pte_end;
  918. pte = READ_ONCE(*ptep);
  919. mask = _PAGE_PRESENT | _PAGE_USER;
  920. if (write)
  921. mask |= _PAGE_RW;
  922. if ((pte_val(pte) & mask) != mask)
  923. return 0;
  924. /* hugepages are never "special" */
  925. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  926. refs = 0;
  927. head = pte_page(pte);
  928. page = head + ((addr & (sz-1)) >> PAGE_SHIFT);
  929. tail = page;
  930. do {
  931. VM_BUG_ON(compound_head(page) != head);
  932. pages[*nr] = page;
  933. (*nr)++;
  934. page++;
  935. refs++;
  936. } while (addr += PAGE_SIZE, addr != end);
  937. if (!page_cache_add_speculative(head, refs)) {
  938. *nr -= refs;
  939. return 0;
  940. }
  941. if (unlikely(pte_val(pte) != pte_val(*ptep))) {
  942. /* Could be optimized better */
  943. *nr -= refs;
  944. while (refs--)
  945. put_page(head);
  946. return 0;
  947. }
  948. /*
  949. * Any tail page need their mapcount reference taken before we
  950. * return.
  951. */
  952. while (refs--) {
  953. if (PageTail(tail))
  954. get_huge_page_tail(tail);
  955. tail++;
  956. }
  957. return 1;
  958. }