hugetlbpage.c 27 KB

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