hugetlbpage.c 27 KB

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