hugetlbpage.c 25 KB

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