hugetlbpage.c 26 KB

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