hugetlbpage.c 26 KB

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