hugetlbpage.c 26 KB

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