hugetlbpage.c 25 KB

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