hugetlbpage.c 26 KB

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