hugetlbpage.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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 (!is_hugepd(pmd)) {
  514. /*
  515. * if it is not hugepd pointer, we should already find
  516. * it cleared.
  517. */
  518. WARN_ON(!pmd_none_or_clear_bad(pmd));
  519. continue;
  520. }
  521. #ifdef CONFIG_PPC_FSL_BOOK3E
  522. /*
  523. * Increment next by the size of the huge mapping since
  524. * there may be more than one entry at this level for a
  525. * single hugepage, but all of them point to
  526. * the same kmem cache that holds the hugepte.
  527. */
  528. next = addr + (1 << hugepd_shift(*(hugepd_t *)pmd));
  529. #endif
  530. free_hugepd_range(tlb, (hugepd_t *)pmd, PMD_SHIFT,
  531. addr, next, floor, ceiling);
  532. } while (addr = next, addr != end);
  533. start &= PUD_MASK;
  534. if (start < floor)
  535. return;
  536. if (ceiling) {
  537. ceiling &= PUD_MASK;
  538. if (!ceiling)
  539. return;
  540. }
  541. if (end - 1 > ceiling - 1)
  542. return;
  543. pmd = pmd_offset(pud, start);
  544. pud_clear(pud);
  545. pmd_free_tlb(tlb, pmd, start);
  546. }
  547. static void hugetlb_free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
  548. unsigned long addr, unsigned long end,
  549. unsigned long floor, unsigned long ceiling)
  550. {
  551. pud_t *pud;
  552. unsigned long next;
  553. unsigned long start;
  554. start = addr;
  555. do {
  556. pud = pud_offset(pgd, addr);
  557. next = pud_addr_end(addr, end);
  558. if (!is_hugepd(pud)) {
  559. if (pud_none_or_clear_bad(pud))
  560. continue;
  561. hugetlb_free_pmd_range(tlb, pud, addr, next, floor,
  562. ceiling);
  563. } else {
  564. #ifdef CONFIG_PPC_FSL_BOOK3E
  565. /*
  566. * Increment next by the size of the huge mapping since
  567. * there may be more than one entry at this level for a
  568. * single hugepage, but all of them point to
  569. * the same kmem cache that holds the hugepte.
  570. */
  571. next = addr + (1 << hugepd_shift(*(hugepd_t *)pud));
  572. #endif
  573. free_hugepd_range(tlb, (hugepd_t *)pud, PUD_SHIFT,
  574. addr, next, floor, ceiling);
  575. }
  576. } while (addr = next, addr != end);
  577. start &= PGDIR_MASK;
  578. if (start < floor)
  579. return;
  580. if (ceiling) {
  581. ceiling &= PGDIR_MASK;
  582. if (!ceiling)
  583. return;
  584. }
  585. if (end - 1 > ceiling - 1)
  586. return;
  587. pud = pud_offset(pgd, start);
  588. pgd_clear(pgd);
  589. pud_free_tlb(tlb, pud, start);
  590. }
  591. /*
  592. * This function frees user-level page tables of a process.
  593. *
  594. * Must be called with pagetable lock held.
  595. */
  596. void hugetlb_free_pgd_range(struct mmu_gather *tlb,
  597. unsigned long addr, unsigned long end,
  598. unsigned long floor, unsigned long ceiling)
  599. {
  600. pgd_t *pgd;
  601. unsigned long next;
  602. /*
  603. * Because there are a number of different possible pagetable
  604. * layouts for hugepage ranges, we limit knowledge of how
  605. * things should be laid out to the allocation path
  606. * (huge_pte_alloc(), above). Everything else works out the
  607. * structure as it goes from information in the hugepd
  608. * pointers. That means that we can't here use the
  609. * optimization used in the normal page free_pgd_range(), of
  610. * checking whether we're actually covering a large enough
  611. * range to have to do anything at the top level of the walk
  612. * instead of at the bottom.
  613. *
  614. * To make sense of this, you should probably go read the big
  615. * block comment at the top of the normal free_pgd_range(),
  616. * too.
  617. */
  618. do {
  619. next = pgd_addr_end(addr, end);
  620. pgd = pgd_offset(tlb->mm, addr);
  621. if (!is_hugepd(pgd)) {
  622. if (pgd_none_or_clear_bad(pgd))
  623. continue;
  624. hugetlb_free_pud_range(tlb, pgd, addr, next, floor, ceiling);
  625. } else {
  626. #ifdef CONFIG_PPC_FSL_BOOK3E
  627. /*
  628. * Increment next by the size of the huge mapping since
  629. * there may be more than one entry at the pgd level
  630. * for a single hugepage, but all of them point to the
  631. * same kmem cache that holds the hugepte.
  632. */
  633. next = addr + (1 << hugepd_shift(*(hugepd_t *)pgd));
  634. #endif
  635. free_hugepd_range(tlb, (hugepd_t *)pgd, PGDIR_SHIFT,
  636. addr, next, floor, ceiling);
  637. }
  638. } while (addr = next, addr != end);
  639. }
  640. struct page *
  641. follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
  642. {
  643. pte_t *ptep;
  644. struct page *page;
  645. unsigned shift;
  646. unsigned long mask;
  647. ptep = find_linux_pte_or_hugepte(mm->pgd, address, &shift);
  648. /* Verify it is a huge page else bail. */
  649. if (!ptep || !shift)
  650. return ERR_PTR(-EINVAL);
  651. mask = (1UL << shift) - 1;
  652. page = pte_page(*ptep);
  653. if (page)
  654. page += (address & mask) / PAGE_SIZE;
  655. return page;
  656. }
  657. struct page *
  658. follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  659. pmd_t *pmd, int write)
  660. {
  661. BUG();
  662. return NULL;
  663. }
  664. int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
  665. unsigned long end, int write, struct page **pages, int *nr)
  666. {
  667. unsigned long mask;
  668. unsigned long pte_end;
  669. struct page *head, *page, *tail;
  670. pte_t pte;
  671. int refs;
  672. pte_end = (addr + sz) & ~(sz-1);
  673. if (pte_end < end)
  674. end = pte_end;
  675. pte = *ptep;
  676. mask = _PAGE_PRESENT | _PAGE_USER;
  677. if (write)
  678. mask |= _PAGE_RW;
  679. if ((pte_val(pte) & mask) != mask)
  680. return 0;
  681. /* hugepages are never "special" */
  682. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  683. refs = 0;
  684. head = pte_page(pte);
  685. page = head + ((addr & (sz-1)) >> PAGE_SHIFT);
  686. tail = page;
  687. do {
  688. VM_BUG_ON(compound_head(page) != head);
  689. pages[*nr] = page;
  690. (*nr)++;
  691. page++;
  692. refs++;
  693. } while (addr += PAGE_SIZE, addr != end);
  694. if (!page_cache_add_speculative(head, refs)) {
  695. *nr -= refs;
  696. return 0;
  697. }
  698. if (unlikely(pte_val(pte) != pte_val(*ptep))) {
  699. /* Could be optimized better */
  700. *nr -= refs;
  701. while (refs--)
  702. put_page(head);
  703. return 0;
  704. }
  705. /*
  706. * Any tail page need their mapcount reference taken before we
  707. * return.
  708. */
  709. while (refs--) {
  710. if (PageTail(tail))
  711. get_huge_page_tail(tail);
  712. tail++;
  713. }
  714. return 1;
  715. }
  716. static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end,
  717. unsigned long sz)
  718. {
  719. unsigned long __boundary = (addr + sz) & ~(sz-1);
  720. return (__boundary - 1 < end - 1) ? __boundary : end;
  721. }
  722. int gup_hugepd(hugepd_t *hugepd, unsigned pdshift,
  723. unsigned long addr, unsigned long end,
  724. int write, struct page **pages, int *nr)
  725. {
  726. pte_t *ptep;
  727. unsigned long sz = 1UL << hugepd_shift(*hugepd);
  728. unsigned long next;
  729. ptep = hugepte_offset(hugepd, addr, pdshift);
  730. do {
  731. next = hugepte_addr_end(addr, end, sz);
  732. if (!gup_hugepte(ptep, sz, addr, end, write, pages, nr))
  733. return 0;
  734. } while (ptep++, addr = next, addr != end);
  735. return 1;
  736. }
  737. #ifdef CONFIG_PPC_MM_SLICES
  738. unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  739. unsigned long len, unsigned long pgoff,
  740. unsigned long flags)
  741. {
  742. struct hstate *hstate = hstate_file(file);
  743. int mmu_psize = shift_to_mmu_psize(huge_page_shift(hstate));
  744. return slice_get_unmapped_area(addr, len, flags, mmu_psize, 1);
  745. }
  746. #endif
  747. unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
  748. {
  749. #ifdef CONFIG_PPC_MM_SLICES
  750. unsigned int psize = get_slice_psize(vma->vm_mm, vma->vm_start);
  751. return 1UL << mmu_psize_to_shift(psize);
  752. #else
  753. if (!is_vm_hugetlb_page(vma))
  754. return PAGE_SIZE;
  755. return huge_page_size(hstate_vma(vma));
  756. #endif
  757. }
  758. static inline bool is_power_of_4(unsigned long x)
  759. {
  760. if (is_power_of_2(x))
  761. return (__ilog2(x) % 2) ? false : true;
  762. return false;
  763. }
  764. static int __init add_huge_page_size(unsigned long long size)
  765. {
  766. int shift = __ffs(size);
  767. int mmu_psize;
  768. /* Check that it is a page size supported by the hardware and
  769. * that it fits within pagetable and slice limits. */
  770. #ifdef CONFIG_PPC_FSL_BOOK3E
  771. if ((size < PAGE_SIZE) || !is_power_of_4(size))
  772. return -EINVAL;
  773. #else
  774. if (!is_power_of_2(size)
  775. || (shift > SLICE_HIGH_SHIFT) || (shift <= PAGE_SHIFT))
  776. return -EINVAL;
  777. #endif
  778. if ((mmu_psize = shift_to_mmu_psize(shift)) < 0)
  779. return -EINVAL;
  780. #ifdef CONFIG_SPU_FS_64K_LS
  781. /* Disable support for 64K huge pages when 64K SPU local store
  782. * support is enabled as the current implementation conflicts.
  783. */
  784. if (shift == PAGE_SHIFT_64K)
  785. return -EINVAL;
  786. #endif /* CONFIG_SPU_FS_64K_LS */
  787. BUG_ON(mmu_psize_defs[mmu_psize].shift != shift);
  788. /* Return if huge page size has already been setup */
  789. if (size_to_hstate(size))
  790. return 0;
  791. hugetlb_add_hstate(shift - PAGE_SHIFT);
  792. return 0;
  793. }
  794. static int __init hugepage_setup_sz(char *str)
  795. {
  796. unsigned long long size;
  797. size = memparse(str, &str);
  798. if (add_huge_page_size(size) != 0)
  799. printk(KERN_WARNING "Invalid huge page size specified(%llu)\n", size);
  800. return 1;
  801. }
  802. __setup("hugepagesz=", hugepage_setup_sz);
  803. #ifdef CONFIG_PPC_FSL_BOOK3E
  804. struct kmem_cache *hugepte_cache;
  805. static int __init hugetlbpage_init(void)
  806. {
  807. int psize;
  808. for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
  809. unsigned shift;
  810. if (!mmu_psize_defs[psize].shift)
  811. continue;
  812. shift = mmu_psize_to_shift(psize);
  813. /* Don't treat normal page sizes as huge... */
  814. if (shift != PAGE_SHIFT)
  815. if (add_huge_page_size(1ULL << shift) < 0)
  816. continue;
  817. }
  818. /*
  819. * Create a kmem cache for hugeptes. The bottom bits in the pte have
  820. * size information encoded in them, so align them to allow this
  821. */
  822. hugepte_cache = kmem_cache_create("hugepte-cache", sizeof(pte_t),
  823. HUGEPD_SHIFT_MASK + 1, 0, NULL);
  824. if (hugepte_cache == NULL)
  825. panic("%s: Unable to create kmem cache for hugeptes\n",
  826. __func__);
  827. /* Default hpage size = 4M */
  828. if (mmu_psize_defs[MMU_PAGE_4M].shift)
  829. HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_4M].shift;
  830. else
  831. panic("%s: Unable to set default huge page size\n", __func__);
  832. return 0;
  833. }
  834. #else
  835. static int __init hugetlbpage_init(void)
  836. {
  837. int psize;
  838. if (!mmu_has_feature(MMU_FTR_16M_PAGE))
  839. return -ENODEV;
  840. for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
  841. unsigned shift;
  842. unsigned pdshift;
  843. if (!mmu_psize_defs[psize].shift)
  844. continue;
  845. shift = mmu_psize_to_shift(psize);
  846. if (add_huge_page_size(1ULL << shift) < 0)
  847. continue;
  848. if (shift < PMD_SHIFT)
  849. pdshift = PMD_SHIFT;
  850. else if (shift < PUD_SHIFT)
  851. pdshift = PUD_SHIFT;
  852. else
  853. pdshift = PGDIR_SHIFT;
  854. /*
  855. * if we have pdshift and shift value same, we don't
  856. * use pgt cache for hugepd.
  857. */
  858. if (pdshift != shift) {
  859. pgtable_cache_add(pdshift - shift, NULL);
  860. if (!PGT_CACHE(pdshift - shift))
  861. panic("hugetlbpage_init(): could not create "
  862. "pgtable cache for %d bit pagesize\n", shift);
  863. }
  864. }
  865. /* Set default large page size. Currently, we pick 16M or 1M
  866. * depending on what is available
  867. */
  868. if (mmu_psize_defs[MMU_PAGE_16M].shift)
  869. HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_16M].shift;
  870. else if (mmu_psize_defs[MMU_PAGE_1M].shift)
  871. HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_1M].shift;
  872. return 0;
  873. }
  874. #endif
  875. module_init(hugetlbpage_init);
  876. void flush_dcache_icache_hugepage(struct page *page)
  877. {
  878. int i;
  879. void *start;
  880. BUG_ON(!PageCompound(page));
  881. for (i = 0; i < (1UL << compound_order(page)); i++) {
  882. if (!PageHighMem(page)) {
  883. __flush_dcache_icache(page_address(page+i));
  884. } else {
  885. start = kmap_atomic(page+i);
  886. __flush_dcache_icache(start);
  887. kunmap_atomic(start);
  888. }
  889. }
  890. }