hugetlbpage.c 26 KB

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