hugetlbpage.c 24 KB

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