hugetlbpage.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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/of_fdt.h>
  15. #include <linux/memblock.h>
  16. #include <linux/bootmem.h>
  17. #include <asm/pgtable.h>
  18. #include <asm/pgalloc.h>
  19. #include <asm/tlb.h>
  20. #include <asm/setup.h>
  21. #define PAGE_SHIFT_64K 16
  22. #define PAGE_SHIFT_16M 24
  23. #define PAGE_SHIFT_16G 34
  24. unsigned int HPAGE_SHIFT;
  25. /*
  26. * Tracks gpages after the device tree is scanned and before the
  27. * huge_boot_pages list is ready. On 64-bit implementations, this is
  28. * just used to track 16G pages and so is a single array. 32-bit
  29. * implementations may have more than one gpage size due to limitations
  30. * of the memory allocators, so we need multiple arrays
  31. */
  32. #ifdef CONFIG_PPC64
  33. #define MAX_NUMBER_GPAGES 1024
  34. static u64 gpage_freearray[MAX_NUMBER_GPAGES];
  35. static unsigned nr_gpages;
  36. #else
  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. #endif
  44. static inline int shift_to_mmu_psize(unsigned int shift)
  45. {
  46. int psize;
  47. for (psize = 0; psize < MMU_PAGE_COUNT; ++psize)
  48. if (mmu_psize_defs[psize].shift == shift)
  49. return psize;
  50. return -1;
  51. }
  52. static inline unsigned int mmu_psize_to_shift(unsigned int mmu_psize)
  53. {
  54. if (mmu_psize_defs[mmu_psize].shift)
  55. return mmu_psize_defs[mmu_psize].shift;
  56. BUG();
  57. }
  58. #define hugepd_none(hpd) ((hpd).pd == 0)
  59. pte_t *find_linux_pte_or_hugepte(pgd_t *pgdir, unsigned long ea, unsigned *shift)
  60. {
  61. pgd_t *pg;
  62. pud_t *pu;
  63. pmd_t *pm;
  64. hugepd_t *hpdp = NULL;
  65. unsigned pdshift = PGDIR_SHIFT;
  66. if (shift)
  67. *shift = 0;
  68. pg = pgdir + pgd_index(ea);
  69. if (is_hugepd(pg)) {
  70. hpdp = (hugepd_t *)pg;
  71. } else if (!pgd_none(*pg)) {
  72. pdshift = PUD_SHIFT;
  73. pu = pud_offset(pg, ea);
  74. if (is_hugepd(pu))
  75. hpdp = (hugepd_t *)pu;
  76. else if (!pud_none(*pu)) {
  77. pdshift = PMD_SHIFT;
  78. pm = pmd_offset(pu, ea);
  79. if (is_hugepd(pm))
  80. hpdp = (hugepd_t *)pm;
  81. else if (!pmd_none(*pm)) {
  82. return pte_offset_kernel(pm, ea);
  83. }
  84. }
  85. }
  86. if (!hpdp)
  87. return NULL;
  88. if (shift)
  89. *shift = hugepd_shift(*hpdp);
  90. return hugepte_offset(hpdp, ea, pdshift);
  91. }
  92. pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
  93. {
  94. return find_linux_pte_or_hugepte(mm->pgd, addr, NULL);
  95. }
  96. static int __hugepte_alloc(struct mm_struct *mm, hugepd_t *hpdp,
  97. unsigned long address, unsigned pdshift, unsigned pshift)
  98. {
  99. struct kmem_cache *cachep;
  100. pte_t *new;
  101. #ifdef CONFIG_PPC64
  102. cachep = PGT_CACHE(pdshift - pshift);
  103. #else
  104. int i;
  105. int num_hugepd = 1 << (pshift - pdshift);
  106. cachep = hugepte_cache;
  107. #endif
  108. new = kmem_cache_zalloc(cachep, GFP_KERNEL|__GFP_REPEAT);
  109. BUG_ON(pshift > HUGEPD_SHIFT_MASK);
  110. BUG_ON((unsigned long)new & HUGEPD_SHIFT_MASK);
  111. if (! new)
  112. return -ENOMEM;
  113. spin_lock(&mm->page_table_lock);
  114. #ifdef CONFIG_PPC64
  115. if (!hugepd_none(*hpdp))
  116. kmem_cache_free(cachep, new);
  117. else
  118. hpdp->pd = ((unsigned long)new & ~PD_HUGE) | pshift;
  119. #else
  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. hpdp->pd = ((unsigned long)new & ~PD_HUGE) | pshift;
  131. }
  132. /* If we bailed from the for loop early, an error occurred, clean up */
  133. if (i < num_hugepd) {
  134. for (i = i - 1 ; i >= 0; i--, hpdp--)
  135. hpdp->pd = 0;
  136. kmem_cache_free(cachep, new);
  137. }
  138. #endif
  139. spin_unlock(&mm->page_table_lock);
  140. return 0;
  141. }
  142. pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr, unsigned long sz)
  143. {
  144. pgd_t *pg;
  145. pud_t *pu;
  146. pmd_t *pm;
  147. hugepd_t *hpdp = NULL;
  148. unsigned pshift = __ffs(sz);
  149. unsigned pdshift = PGDIR_SHIFT;
  150. addr &= ~(sz-1);
  151. pg = pgd_offset(mm, addr);
  152. if (pshift >= PUD_SHIFT) {
  153. hpdp = (hugepd_t *)pg;
  154. } else {
  155. pdshift = PUD_SHIFT;
  156. pu = pud_alloc(mm, pg, addr);
  157. if (pshift >= PMD_SHIFT) {
  158. hpdp = (hugepd_t *)pu;
  159. } else {
  160. pdshift = PMD_SHIFT;
  161. pm = pmd_alloc(mm, pu, addr);
  162. hpdp = (hugepd_t *)pm;
  163. }
  164. }
  165. if (!hpdp)
  166. return NULL;
  167. BUG_ON(!hugepd_none(*hpdp) && !hugepd_ok(*hpdp));
  168. if (hugepd_none(*hpdp) && __hugepte_alloc(mm, hpdp, addr, pdshift, pshift))
  169. return NULL;
  170. return hugepte_offset(hpdp, addr, pdshift);
  171. }
  172. #ifdef CONFIG_PPC32
  173. /* Build list of addresses of gigantic pages. This function is used in early
  174. * boot before the buddy or bootmem allocator is setup.
  175. */
  176. void add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages)
  177. {
  178. unsigned int idx = shift_to_mmu_psize(__ffs(page_size));
  179. int i;
  180. if (addr == 0)
  181. return;
  182. gpage_freearray[idx].nr_gpages = number_of_pages;
  183. for (i = 0; i < number_of_pages; i++) {
  184. gpage_freearray[idx].gpage_list[i] = addr;
  185. addr += page_size;
  186. }
  187. }
  188. /*
  189. * Moves the gigantic page addresses from the temporary list to the
  190. * huge_boot_pages list.
  191. */
  192. int alloc_bootmem_huge_page(struct hstate *hstate)
  193. {
  194. struct huge_bootmem_page *m;
  195. int idx = shift_to_mmu_psize(hstate->order + PAGE_SHIFT);
  196. int nr_gpages = gpage_freearray[idx].nr_gpages;
  197. if (nr_gpages == 0)
  198. return 0;
  199. #ifdef CONFIG_HIGHMEM
  200. /*
  201. * If gpages can be in highmem we can't use the trick of storing the
  202. * data structure in the page; allocate space for this
  203. */
  204. m = alloc_bootmem(sizeof(struct huge_bootmem_page));
  205. m->phys = gpage_freearray[idx].gpage_list[--nr_gpages];
  206. #else
  207. m = phys_to_virt(gpage_freearray[idx].gpage_list[--nr_gpages]);
  208. #endif
  209. list_add(&m->list, &huge_boot_pages);
  210. gpage_freearray[idx].nr_gpages = nr_gpages;
  211. gpage_freearray[idx].gpage_list[nr_gpages] = 0;
  212. m->hstate = hstate;
  213. return 1;
  214. }
  215. /*
  216. * Scan the command line hugepagesz= options for gigantic pages; store those in
  217. * a list that we use to allocate the memory once all options are parsed.
  218. */
  219. unsigned long gpage_npages[MMU_PAGE_COUNT];
  220. static int __init do_gpage_early_setup(char *param, char *val)
  221. {
  222. static phys_addr_t size;
  223. unsigned long npages;
  224. /*
  225. * The hugepagesz and hugepages cmdline options are interleaved. We
  226. * use the size variable to keep track of whether or not this was done
  227. * properly and skip over instances where it is incorrect. Other
  228. * command-line parsing code will issue warnings, so we don't need to.
  229. *
  230. */
  231. if ((strcmp(param, "default_hugepagesz") == 0) ||
  232. (strcmp(param, "hugepagesz") == 0)) {
  233. size = memparse(val, NULL);
  234. } else if (strcmp(param, "hugepages") == 0) {
  235. if (size != 0) {
  236. if (sscanf(val, "%lu", &npages) <= 0)
  237. npages = 0;
  238. gpage_npages[shift_to_mmu_psize(__ffs(size))] = npages;
  239. size = 0;
  240. }
  241. }
  242. return 0;
  243. }
  244. /*
  245. * This function allocates physical space for pages that are larger than the
  246. * buddy allocator can handle. We want to allocate these in highmem because
  247. * the amount of lowmem is limited. This means that this function MUST be
  248. * called before lowmem_end_addr is set up in MMU_init() in order for the lmb
  249. * allocate to grab highmem.
  250. */
  251. void __init reserve_hugetlb_gpages(void)
  252. {
  253. static __initdata char cmdline[COMMAND_LINE_SIZE];
  254. phys_addr_t size, base;
  255. int i;
  256. strlcpy(cmdline, boot_command_line, COMMAND_LINE_SIZE);
  257. parse_args("hugetlb gpages", cmdline, NULL, 0, &do_gpage_early_setup);
  258. /*
  259. * Walk gpage list in reverse, allocating larger page sizes first.
  260. * Skip over unsupported sizes, or sizes that have 0 gpages allocated.
  261. * When we reach the point in the list where pages are no longer
  262. * considered gpages, we're done.
  263. */
  264. for (i = MMU_PAGE_COUNT-1; i >= 0; i--) {
  265. if (mmu_psize_defs[i].shift == 0 || gpage_npages[i] == 0)
  266. continue;
  267. else if (mmu_psize_to_shift(i) < (MAX_ORDER + PAGE_SHIFT))
  268. break;
  269. size = (phys_addr_t)(1ULL << mmu_psize_to_shift(i));
  270. base = memblock_alloc_base(size * gpage_npages[i], size,
  271. MEMBLOCK_ALLOC_ANYWHERE);
  272. add_gpage(base, size, gpage_npages[i]);
  273. }
  274. }
  275. #else /* PPC64 */
  276. /* Build list of addresses of gigantic pages. This function is used in early
  277. * boot before the buddy or bootmem allocator is setup.
  278. */
  279. void add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages)
  280. {
  281. if (!addr)
  282. return;
  283. while (number_of_pages > 0) {
  284. gpage_freearray[nr_gpages] = addr;
  285. nr_gpages++;
  286. number_of_pages--;
  287. addr += page_size;
  288. }
  289. }
  290. /* Moves the gigantic page addresses from the temporary list to the
  291. * huge_boot_pages list.
  292. */
  293. int alloc_bootmem_huge_page(struct hstate *hstate)
  294. {
  295. struct huge_bootmem_page *m;
  296. if (nr_gpages == 0)
  297. return 0;
  298. m = phys_to_virt(gpage_freearray[--nr_gpages]);
  299. gpage_freearray[nr_gpages] = 0;
  300. list_add(&m->list, &huge_boot_pages);
  301. m->hstate = hstate;
  302. return 1;
  303. }
  304. #endif
  305. int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
  306. {
  307. return 0;
  308. }
  309. #ifdef CONFIG_PPC32
  310. #define HUGEPD_FREELIST_SIZE \
  311. ((PAGE_SIZE - sizeof(struct hugepd_freelist)) / sizeof(pte_t))
  312. struct hugepd_freelist {
  313. struct rcu_head rcu;
  314. unsigned int index;
  315. void *ptes[0];
  316. };
  317. static DEFINE_PER_CPU(struct hugepd_freelist *, hugepd_freelist_cur);
  318. static void hugepd_free_rcu_callback(struct rcu_head *head)
  319. {
  320. struct hugepd_freelist *batch =
  321. container_of(head, struct hugepd_freelist, rcu);
  322. unsigned int i;
  323. for (i = 0; i < batch->index; i++)
  324. kmem_cache_free(hugepte_cache, batch->ptes[i]);
  325. free_page((unsigned long)batch);
  326. }
  327. static void hugepd_free(struct mmu_gather *tlb, void *hugepte)
  328. {
  329. struct hugepd_freelist **batchp;
  330. batchp = &__get_cpu_var(hugepd_freelist_cur);
  331. if (atomic_read(&tlb->mm->mm_users) < 2 ||
  332. cpumask_equal(mm_cpumask(tlb->mm),
  333. cpumask_of(smp_processor_id()))) {
  334. kmem_cache_free(hugepte_cache, hugepte);
  335. return;
  336. }
  337. if (*batchp == NULL) {
  338. *batchp = (struct hugepd_freelist *)__get_free_page(GFP_ATOMIC);
  339. (*batchp)->index = 0;
  340. }
  341. (*batchp)->ptes[(*batchp)->index++] = hugepte;
  342. if ((*batchp)->index == HUGEPD_FREELIST_SIZE) {
  343. call_rcu_sched(&(*batchp)->rcu, hugepd_free_rcu_callback);
  344. *batchp = NULL;
  345. }
  346. }
  347. #endif
  348. static void free_hugepd_range(struct mmu_gather *tlb, hugepd_t *hpdp, int pdshift,
  349. unsigned long start, unsigned long end,
  350. unsigned long floor, unsigned long ceiling)
  351. {
  352. pte_t *hugepte = hugepd_page(*hpdp);
  353. int i;
  354. unsigned long pdmask = ~((1UL << pdshift) - 1);
  355. unsigned int num_hugepd = 1;
  356. #ifdef CONFIG_PPC64
  357. unsigned int shift = hugepd_shift(*hpdp);
  358. #else
  359. /* Note: On 32-bit the hpdp may be the first of several */
  360. num_hugepd = (1 << (hugepd_shift(*hpdp) - pdshift));
  361. #endif
  362. start &= pdmask;
  363. if (start < floor)
  364. return;
  365. if (ceiling) {
  366. ceiling &= pdmask;
  367. if (! ceiling)
  368. return;
  369. }
  370. if (end - 1 > ceiling - 1)
  371. return;
  372. for (i = 0; i < num_hugepd; i++, hpdp++)
  373. hpdp->pd = 0;
  374. tlb->need_flush = 1;
  375. #ifdef CONFIG_PPC64
  376. pgtable_free_tlb(tlb, hugepte, pdshift - shift);
  377. #else
  378. hugepd_free(tlb, hugepte);
  379. #endif
  380. }
  381. static void hugetlb_free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
  382. unsigned long addr, unsigned long end,
  383. unsigned long floor, unsigned long ceiling)
  384. {
  385. pmd_t *pmd;
  386. unsigned long next;
  387. unsigned long start;
  388. start = addr;
  389. pmd = pmd_offset(pud, addr);
  390. do {
  391. next = pmd_addr_end(addr, end);
  392. if (pmd_none(*pmd))
  393. continue;
  394. free_hugepd_range(tlb, (hugepd_t *)pmd, PMD_SHIFT,
  395. addr, next, floor, ceiling);
  396. } while (pmd++, addr = next, addr != end);
  397. start &= PUD_MASK;
  398. if (start < floor)
  399. return;
  400. if (ceiling) {
  401. ceiling &= PUD_MASK;
  402. if (!ceiling)
  403. return;
  404. }
  405. if (end - 1 > ceiling - 1)
  406. return;
  407. pmd = pmd_offset(pud, start);
  408. pud_clear(pud);
  409. pmd_free_tlb(tlb, pmd, start);
  410. }
  411. static void hugetlb_free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
  412. unsigned long addr, unsigned long end,
  413. unsigned long floor, unsigned long ceiling)
  414. {
  415. pud_t *pud;
  416. unsigned long next;
  417. unsigned long start;
  418. start = addr;
  419. pud = pud_offset(pgd, addr);
  420. do {
  421. next = pud_addr_end(addr, end);
  422. if (!is_hugepd(pud)) {
  423. if (pud_none_or_clear_bad(pud))
  424. continue;
  425. hugetlb_free_pmd_range(tlb, pud, addr, next, floor,
  426. ceiling);
  427. } else {
  428. free_hugepd_range(tlb, (hugepd_t *)pud, PUD_SHIFT,
  429. addr, next, floor, ceiling);
  430. }
  431. } while (pud++, addr = next, addr != end);
  432. start &= PGDIR_MASK;
  433. if (start < floor)
  434. return;
  435. if (ceiling) {
  436. ceiling &= PGDIR_MASK;
  437. if (!ceiling)
  438. return;
  439. }
  440. if (end - 1 > ceiling - 1)
  441. return;
  442. pud = pud_offset(pgd, start);
  443. pgd_clear(pgd);
  444. pud_free_tlb(tlb, pud, start);
  445. }
  446. /*
  447. * This function frees user-level page tables of a process.
  448. *
  449. * Must be called with pagetable lock held.
  450. */
  451. void hugetlb_free_pgd_range(struct mmu_gather *tlb,
  452. unsigned long addr, unsigned long end,
  453. unsigned long floor, unsigned long ceiling)
  454. {
  455. pgd_t *pgd;
  456. unsigned long next;
  457. /*
  458. * Because there are a number of different possible pagetable
  459. * layouts for hugepage ranges, we limit knowledge of how
  460. * things should be laid out to the allocation path
  461. * (huge_pte_alloc(), above). Everything else works out the
  462. * structure as it goes from information in the hugepd
  463. * pointers. That means that we can't here use the
  464. * optimization used in the normal page free_pgd_range(), of
  465. * checking whether we're actually covering a large enough
  466. * range to have to do anything at the top level of the walk
  467. * instead of at the bottom.
  468. *
  469. * To make sense of this, you should probably go read the big
  470. * block comment at the top of the normal free_pgd_range(),
  471. * too.
  472. */
  473. do {
  474. next = pgd_addr_end(addr, end);
  475. pgd = pgd_offset(tlb->mm, addr);
  476. if (!is_hugepd(pgd)) {
  477. if (pgd_none_or_clear_bad(pgd))
  478. continue;
  479. hugetlb_free_pud_range(tlb, pgd, addr, next, floor, ceiling);
  480. } else {
  481. #ifdef CONFIG_PPC32
  482. /*
  483. * Increment next by the size of the huge mapping since
  484. * on 32-bit there may be more than one entry at the pgd
  485. * level for a single hugepage, but all of them point to
  486. * the same kmem cache that holds the hugepte.
  487. */
  488. next = addr + (1 << hugepd_shift(*(hugepd_t *)pgd));
  489. #endif
  490. free_hugepd_range(tlb, (hugepd_t *)pgd, PGDIR_SHIFT,
  491. addr, next, floor, ceiling);
  492. }
  493. } while (addr = next, addr != end);
  494. }
  495. struct page *
  496. follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
  497. {
  498. pte_t *ptep;
  499. struct page *page;
  500. unsigned shift;
  501. unsigned long mask;
  502. ptep = find_linux_pte_or_hugepte(mm->pgd, address, &shift);
  503. /* Verify it is a huge page else bail. */
  504. if (!ptep || !shift)
  505. return ERR_PTR(-EINVAL);
  506. mask = (1UL << shift) - 1;
  507. page = pte_page(*ptep);
  508. if (page)
  509. page += (address & mask) / PAGE_SIZE;
  510. return page;
  511. }
  512. int pmd_huge(pmd_t pmd)
  513. {
  514. return 0;
  515. }
  516. int pud_huge(pud_t pud)
  517. {
  518. return 0;
  519. }
  520. struct page *
  521. follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  522. pmd_t *pmd, int write)
  523. {
  524. BUG();
  525. return NULL;
  526. }
  527. static noinline int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
  528. unsigned long end, int write, struct page **pages, int *nr)
  529. {
  530. unsigned long mask;
  531. unsigned long pte_end;
  532. struct page *head, *page, *tail;
  533. pte_t pte;
  534. int refs;
  535. pte_end = (addr + sz) & ~(sz-1);
  536. if (pte_end < end)
  537. end = pte_end;
  538. pte = *ptep;
  539. mask = _PAGE_PRESENT | _PAGE_USER;
  540. if (write)
  541. mask |= _PAGE_RW;
  542. if ((pte_val(pte) & mask) != mask)
  543. return 0;
  544. /* hugepages are never "special" */
  545. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  546. refs = 0;
  547. head = pte_page(pte);
  548. page = head + ((addr & (sz-1)) >> PAGE_SHIFT);
  549. tail = page;
  550. do {
  551. VM_BUG_ON(compound_head(page) != head);
  552. pages[*nr] = page;
  553. (*nr)++;
  554. page++;
  555. refs++;
  556. } while (addr += PAGE_SIZE, addr != end);
  557. if (!page_cache_add_speculative(head, refs)) {
  558. *nr -= refs;
  559. return 0;
  560. }
  561. if (unlikely(pte_val(pte) != pte_val(*ptep))) {
  562. /* Could be optimized better */
  563. *nr -= refs;
  564. while (refs--)
  565. put_page(head);
  566. return 0;
  567. }
  568. /*
  569. * Any tail page need their mapcount reference taken before we
  570. * return.
  571. */
  572. while (refs--) {
  573. if (PageTail(tail))
  574. get_huge_page_tail(tail);
  575. tail++;
  576. }
  577. return 1;
  578. }
  579. static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end,
  580. unsigned long sz)
  581. {
  582. unsigned long __boundary = (addr + sz) & ~(sz-1);
  583. return (__boundary - 1 < end - 1) ? __boundary : end;
  584. }
  585. int gup_hugepd(hugepd_t *hugepd, unsigned pdshift,
  586. unsigned long addr, unsigned long end,
  587. int write, struct page **pages, int *nr)
  588. {
  589. pte_t *ptep;
  590. unsigned long sz = 1UL << hugepd_shift(*hugepd);
  591. unsigned long next;
  592. ptep = hugepte_offset(hugepd, addr, pdshift);
  593. do {
  594. next = hugepte_addr_end(addr, end, sz);
  595. if (!gup_hugepte(ptep, sz, addr, end, write, pages, nr))
  596. return 0;
  597. } while (ptep++, addr = next, addr != end);
  598. return 1;
  599. }
  600. unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  601. unsigned long len, unsigned long pgoff,
  602. unsigned long flags)
  603. {
  604. #ifdef CONFIG_PPC_MM_SLICES
  605. struct hstate *hstate = hstate_file(file);
  606. int mmu_psize = shift_to_mmu_psize(huge_page_shift(hstate));
  607. return slice_get_unmapped_area(addr, len, flags, mmu_psize, 1, 0);
  608. #else
  609. return get_unmapped_area(file, addr, len, pgoff, flags);
  610. #endif
  611. }
  612. unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
  613. {
  614. #ifdef CONFIG_PPC_MM_SLICES
  615. unsigned int psize = get_slice_psize(vma->vm_mm, vma->vm_start);
  616. return 1UL << mmu_psize_to_shift(psize);
  617. #else
  618. if (!is_vm_hugetlb_page(vma))
  619. return PAGE_SIZE;
  620. return huge_page_size(hstate_vma(vma));
  621. #endif
  622. }
  623. static inline bool is_power_of_4(unsigned long x)
  624. {
  625. if (is_power_of_2(x))
  626. return (__ilog2(x) % 2) ? false : true;
  627. return false;
  628. }
  629. static int __init add_huge_page_size(unsigned long long size)
  630. {
  631. int shift = __ffs(size);
  632. int mmu_psize;
  633. /* Check that it is a page size supported by the hardware and
  634. * that it fits within pagetable and slice limits. */
  635. #ifdef CONFIG_PPC_FSL_BOOK3E
  636. if ((size < PAGE_SIZE) || !is_power_of_4(size))
  637. return -EINVAL;
  638. #else
  639. if (!is_power_of_2(size)
  640. || (shift > SLICE_HIGH_SHIFT) || (shift <= PAGE_SHIFT))
  641. return -EINVAL;
  642. #endif
  643. if ((mmu_psize = shift_to_mmu_psize(shift)) < 0)
  644. return -EINVAL;
  645. #ifdef CONFIG_SPU_FS_64K_LS
  646. /* Disable support for 64K huge pages when 64K SPU local store
  647. * support is enabled as the current implementation conflicts.
  648. */
  649. if (shift == PAGE_SHIFT_64K)
  650. return -EINVAL;
  651. #endif /* CONFIG_SPU_FS_64K_LS */
  652. BUG_ON(mmu_psize_defs[mmu_psize].shift != shift);
  653. /* Return if huge page size has already been setup */
  654. if (size_to_hstate(size))
  655. return 0;
  656. hugetlb_add_hstate(shift - PAGE_SHIFT);
  657. return 0;
  658. }
  659. static int __init hugepage_setup_sz(char *str)
  660. {
  661. unsigned long long size;
  662. size = memparse(str, &str);
  663. if (add_huge_page_size(size) != 0)
  664. printk(KERN_WARNING "Invalid huge page size specified(%llu)\n", size);
  665. return 1;
  666. }
  667. __setup("hugepagesz=", hugepage_setup_sz);
  668. #ifdef CONFIG_FSL_BOOKE
  669. struct kmem_cache *hugepte_cache;
  670. static int __init hugetlbpage_init(void)
  671. {
  672. int psize;
  673. for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
  674. unsigned shift;
  675. if (!mmu_psize_defs[psize].shift)
  676. continue;
  677. shift = mmu_psize_to_shift(psize);
  678. /* Don't treat normal page sizes as huge... */
  679. if (shift != PAGE_SHIFT)
  680. if (add_huge_page_size(1ULL << shift) < 0)
  681. continue;
  682. }
  683. /*
  684. * Create a kmem cache for hugeptes. The bottom bits in the pte have
  685. * size information encoded in them, so align them to allow this
  686. */
  687. hugepte_cache = kmem_cache_create("hugepte-cache", sizeof(pte_t),
  688. HUGEPD_SHIFT_MASK + 1, 0, NULL);
  689. if (hugepte_cache == NULL)
  690. panic("%s: Unable to create kmem cache for hugeptes\n",
  691. __func__);
  692. /* Default hpage size = 4M */
  693. if (mmu_psize_defs[MMU_PAGE_4M].shift)
  694. HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_4M].shift;
  695. else
  696. panic("%s: Unable to set default huge page size\n", __func__);
  697. return 0;
  698. }
  699. #else
  700. static int __init hugetlbpage_init(void)
  701. {
  702. int psize;
  703. if (!mmu_has_feature(MMU_FTR_16M_PAGE))
  704. return -ENODEV;
  705. for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
  706. unsigned shift;
  707. unsigned pdshift;
  708. if (!mmu_psize_defs[psize].shift)
  709. continue;
  710. shift = mmu_psize_to_shift(psize);
  711. if (add_huge_page_size(1ULL << shift) < 0)
  712. continue;
  713. if (shift < PMD_SHIFT)
  714. pdshift = PMD_SHIFT;
  715. else if (shift < PUD_SHIFT)
  716. pdshift = PUD_SHIFT;
  717. else
  718. pdshift = PGDIR_SHIFT;
  719. pgtable_cache_add(pdshift - shift, NULL);
  720. if (!PGT_CACHE(pdshift - shift))
  721. panic("hugetlbpage_init(): could not create "
  722. "pgtable cache for %d bit pagesize\n", shift);
  723. }
  724. /* Set default large page size. Currently, we pick 16M or 1M
  725. * depending on what is available
  726. */
  727. if (mmu_psize_defs[MMU_PAGE_16M].shift)
  728. HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_16M].shift;
  729. else if (mmu_psize_defs[MMU_PAGE_1M].shift)
  730. HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_1M].shift;
  731. return 0;
  732. }
  733. #endif
  734. module_init(hugetlbpage_init);
  735. void flush_dcache_icache_hugepage(struct page *page)
  736. {
  737. int i;
  738. void *start;
  739. BUG_ON(!PageCompound(page));
  740. for (i = 0; i < (1UL << compound_order(page)); i++) {
  741. if (!PageHighMem(page)) {
  742. __flush_dcache_icache(page_address(page+i));
  743. } else {
  744. start = kmap_atomic(page+i, KM_PPC_SYNC_ICACHE);
  745. __flush_dcache_icache(start);
  746. kunmap_atomic(start, KM_PPC_SYNC_ICACHE);
  747. }
  748. }
  749. }