hugetlbpage.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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. #ifdef CONFIG_PPC_BOOK3S_64
  672. /*
  673. * We need to make sure that for different page sizes reported by
  674. * firmware we only add hugetlb support for page sizes that can be
  675. * supported by linux page table layout.
  676. * For now we have
  677. * Radix: 2M
  678. * Hash: 16M and 16G
  679. */
  680. if (radix_enabled()) {
  681. if (mmu_psize != MMU_PAGE_2M)
  682. return -EINVAL;
  683. } else {
  684. if (mmu_psize != MMU_PAGE_16M && mmu_psize != MMU_PAGE_16G)
  685. return -EINVAL;
  686. }
  687. #endif
  688. BUG_ON(mmu_psize_defs[mmu_psize].shift != shift);
  689. /* Return if huge page size has already been setup */
  690. if (size_to_hstate(size))
  691. return 0;
  692. hugetlb_add_hstate(shift - PAGE_SHIFT);
  693. return 0;
  694. }
  695. static int __init hugepage_setup_sz(char *str)
  696. {
  697. unsigned long long size;
  698. size = memparse(str, &str);
  699. if (add_huge_page_size(size) != 0) {
  700. hugetlb_bad_size();
  701. pr_err("Invalid huge page size specified(%llu)\n", size);
  702. }
  703. return 1;
  704. }
  705. __setup("hugepagesz=", hugepage_setup_sz);
  706. #ifdef CONFIG_PPC_FSL_BOOK3E
  707. struct kmem_cache *hugepte_cache;
  708. static int __init hugetlbpage_init(void)
  709. {
  710. int psize;
  711. for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
  712. unsigned shift;
  713. if (!mmu_psize_defs[psize].shift)
  714. continue;
  715. shift = mmu_psize_to_shift(psize);
  716. /* Don't treat normal page sizes as huge... */
  717. if (shift != PAGE_SHIFT)
  718. if (add_huge_page_size(1ULL << shift) < 0)
  719. continue;
  720. }
  721. /*
  722. * Create a kmem cache for hugeptes. The bottom bits in the pte have
  723. * size information encoded in them, so align them to allow this
  724. */
  725. hugepte_cache = kmem_cache_create("hugepte-cache", sizeof(pte_t),
  726. HUGEPD_SHIFT_MASK + 1, 0, NULL);
  727. if (hugepte_cache == NULL)
  728. panic("%s: Unable to create kmem cache for hugeptes\n",
  729. __func__);
  730. /* Default hpage size = 4M */
  731. if (mmu_psize_defs[MMU_PAGE_4M].shift)
  732. HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_4M].shift;
  733. else
  734. panic("%s: Unable to set default huge page size\n", __func__);
  735. return 0;
  736. }
  737. #else
  738. static int __init hugetlbpage_init(void)
  739. {
  740. int psize;
  741. if (!radix_enabled() && !mmu_has_feature(MMU_FTR_16M_PAGE))
  742. return -ENODEV;
  743. for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
  744. unsigned shift;
  745. unsigned pdshift;
  746. if (!mmu_psize_defs[psize].shift)
  747. continue;
  748. shift = mmu_psize_to_shift(psize);
  749. if (add_huge_page_size(1ULL << shift) < 0)
  750. continue;
  751. if (shift < PMD_SHIFT)
  752. pdshift = PMD_SHIFT;
  753. else if (shift < PUD_SHIFT)
  754. pdshift = PUD_SHIFT;
  755. else
  756. pdshift = PGDIR_SHIFT;
  757. /*
  758. * if we have pdshift and shift value same, we don't
  759. * use pgt cache for hugepd.
  760. */
  761. if (pdshift != shift) {
  762. pgtable_cache_add(pdshift - shift, NULL);
  763. if (!PGT_CACHE(pdshift - shift))
  764. panic("hugetlbpage_init(): could not create "
  765. "pgtable cache for %d bit pagesize\n", shift);
  766. }
  767. }
  768. /* Set default large page size. Currently, we pick 16M or 1M
  769. * depending on what is available
  770. */
  771. if (mmu_psize_defs[MMU_PAGE_16M].shift)
  772. HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_16M].shift;
  773. else if (mmu_psize_defs[MMU_PAGE_1M].shift)
  774. HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_1M].shift;
  775. else if (mmu_psize_defs[MMU_PAGE_2M].shift)
  776. HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_2M].shift;
  777. return 0;
  778. }
  779. #endif
  780. arch_initcall(hugetlbpage_init);
  781. void flush_dcache_icache_hugepage(struct page *page)
  782. {
  783. int i;
  784. void *start;
  785. BUG_ON(!PageCompound(page));
  786. for (i = 0; i < (1UL << compound_order(page)); i++) {
  787. if (!PageHighMem(page)) {
  788. __flush_dcache_icache(page_address(page+i));
  789. } else {
  790. start = kmap_atomic(page+i);
  791. __flush_dcache_icache(start);
  792. kunmap_atomic(start);
  793. }
  794. }
  795. }
  796. #endif /* CONFIG_HUGETLB_PAGE */
  797. /*
  798. * We have 4 cases for pgds and pmds:
  799. * (1) invalid (all zeroes)
  800. * (2) pointer to next table, as normal; bottom 6 bits == 0
  801. * (3) leaf pte for huge page _PAGE_PTE set
  802. * (4) hugepd pointer, _PAGE_PTE = 0 and bits [2..6] indicate size of table
  803. *
  804. * So long as we atomically load page table pointers we are safe against teardown,
  805. * we can follow the address down to the the page and take a ref on it.
  806. * This function need to be called with interrupts disabled. We use this variant
  807. * when we have MSR[EE] = 0 but the paca->soft_enabled = 1
  808. */
  809. pte_t *__find_linux_pte_or_hugepte(pgd_t *pgdir, unsigned long ea,
  810. bool *is_thp, unsigned *shift)
  811. {
  812. pgd_t pgd, *pgdp;
  813. pud_t pud, *pudp;
  814. pmd_t pmd, *pmdp;
  815. pte_t *ret_pte;
  816. hugepd_t *hpdp = NULL;
  817. unsigned pdshift = PGDIR_SHIFT;
  818. if (shift)
  819. *shift = 0;
  820. if (is_thp)
  821. *is_thp = false;
  822. pgdp = pgdir + pgd_index(ea);
  823. pgd = READ_ONCE(*pgdp);
  824. /*
  825. * Always operate on the local stack value. This make sure the
  826. * value don't get updated by a parallel THP split/collapse,
  827. * page fault or a page unmap. The return pte_t * is still not
  828. * stable. So should be checked there for above conditions.
  829. */
  830. if (pgd_none(pgd))
  831. return NULL;
  832. else if (pgd_huge(pgd)) {
  833. ret_pte = (pte_t *) pgdp;
  834. goto out;
  835. } else if (is_hugepd(__hugepd(pgd_val(pgd))))
  836. hpdp = (hugepd_t *)&pgd;
  837. else {
  838. /*
  839. * Even if we end up with an unmap, the pgtable will not
  840. * be freed, because we do an rcu free and here we are
  841. * irq disabled
  842. */
  843. pdshift = PUD_SHIFT;
  844. pudp = pud_offset(&pgd, ea);
  845. pud = READ_ONCE(*pudp);
  846. if (pud_none(pud))
  847. return NULL;
  848. else if (pud_huge(pud)) {
  849. ret_pte = (pte_t *) pudp;
  850. goto out;
  851. } else if (is_hugepd(__hugepd(pud_val(pud))))
  852. hpdp = (hugepd_t *)&pud;
  853. else {
  854. pdshift = PMD_SHIFT;
  855. pmdp = pmd_offset(&pud, ea);
  856. pmd = READ_ONCE(*pmdp);
  857. /*
  858. * A hugepage collapse is captured by pmd_none, because
  859. * it mark the pmd none and do a hpte invalidate.
  860. */
  861. if (pmd_none(pmd))
  862. return NULL;
  863. if (pmd_trans_huge(pmd)) {
  864. if (is_thp)
  865. *is_thp = true;
  866. ret_pte = (pte_t *) pmdp;
  867. goto out;
  868. }
  869. if (pmd_huge(pmd)) {
  870. ret_pte = (pte_t *) pmdp;
  871. goto out;
  872. } else if (is_hugepd(__hugepd(pmd_val(pmd))))
  873. hpdp = (hugepd_t *)&pmd;
  874. else
  875. return pte_offset_kernel(&pmd, ea);
  876. }
  877. }
  878. if (!hpdp)
  879. return NULL;
  880. ret_pte = hugepte_offset(*hpdp, ea, pdshift);
  881. pdshift = hugepd_shift(*hpdp);
  882. out:
  883. if (shift)
  884. *shift = pdshift;
  885. return ret_pte;
  886. }
  887. EXPORT_SYMBOL_GPL(__find_linux_pte_or_hugepte);
  888. int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
  889. unsigned long end, int write, struct page **pages, int *nr)
  890. {
  891. unsigned long mask;
  892. unsigned long pte_end;
  893. struct page *head, *page;
  894. pte_t pte;
  895. int refs;
  896. pte_end = (addr + sz) & ~(sz-1);
  897. if (pte_end < end)
  898. end = pte_end;
  899. pte = READ_ONCE(*ptep);
  900. mask = _PAGE_PRESENT | _PAGE_READ;
  901. /*
  902. * On some CPUs like the 8xx, _PAGE_RW hence _PAGE_WRITE is defined
  903. * as 0 and _PAGE_RO has to be set when a page is not writable
  904. */
  905. if (write)
  906. mask |= _PAGE_WRITE;
  907. else
  908. mask |= _PAGE_RO;
  909. if ((pte_val(pte) & mask) != mask)
  910. return 0;
  911. /* hugepages are never "special" */
  912. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  913. refs = 0;
  914. head = pte_page(pte);
  915. page = head + ((addr & (sz-1)) >> PAGE_SHIFT);
  916. do {
  917. VM_BUG_ON(compound_head(page) != head);
  918. pages[*nr] = page;
  919. (*nr)++;
  920. page++;
  921. refs++;
  922. } while (addr += PAGE_SIZE, addr != end);
  923. if (!page_cache_add_speculative(head, refs)) {
  924. *nr -= refs;
  925. return 0;
  926. }
  927. if (unlikely(pte_val(pte) != pte_val(*ptep))) {
  928. /* Could be optimized better */
  929. *nr -= refs;
  930. while (refs--)
  931. put_page(head);
  932. return 0;
  933. }
  934. return 1;
  935. }