hugetlbpage.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  2. * PPC64 (POWER4) Huge TLB Page Support for Kernel.
  3. *
  4. * Copyright (C) 2003 David Gibson, IBM Corporation.
  5. *
  6. * Based on the IA-32 version:
  7. * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
  8. */
  9. #include <linux/init.h>
  10. #include <linux/fs.h>
  11. #include <linux/mm.h>
  12. #include <linux/hugetlb.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/slab.h>
  15. #include <linux/err.h>
  16. #include <linux/sysctl.h>
  17. #include <asm/mman.h>
  18. #include <asm/pgalloc.h>
  19. #include <asm/tlb.h>
  20. #include <asm/tlbflush.h>
  21. #include <asm/mmu_context.h>
  22. #include <asm/machdep.h>
  23. #include <asm/cputable.h>
  24. #include <asm/spu.h>
  25. #define HPAGE_SHIFT_64K 16
  26. #define HPAGE_SHIFT_16M 24
  27. #define NUM_LOW_AREAS (0x100000000UL >> SID_SHIFT)
  28. #define NUM_HIGH_AREAS (PGTABLE_RANGE >> HTLB_AREA_SHIFT)
  29. unsigned int hugepte_shift;
  30. #define PTRS_PER_HUGEPTE (1 << hugepte_shift)
  31. #define HUGEPTE_TABLE_SIZE (sizeof(pte_t) << hugepte_shift)
  32. #define HUGEPD_SHIFT (HPAGE_SHIFT + hugepte_shift)
  33. #define HUGEPD_SIZE (1UL << HUGEPD_SHIFT)
  34. #define HUGEPD_MASK (~(HUGEPD_SIZE-1))
  35. #define huge_pgtable_cache (pgtable_cache[HUGEPTE_CACHE_NUM])
  36. /* Flag to mark huge PD pointers. This means pmd_bad() and pud_bad()
  37. * will choke on pointers to hugepte tables, which is handy for
  38. * catching screwups early. */
  39. #define HUGEPD_OK 0x1
  40. typedef struct { unsigned long pd; } hugepd_t;
  41. #define hugepd_none(hpd) ((hpd).pd == 0)
  42. static inline pte_t *hugepd_page(hugepd_t hpd)
  43. {
  44. BUG_ON(!(hpd.pd & HUGEPD_OK));
  45. return (pte_t *)(hpd.pd & ~HUGEPD_OK);
  46. }
  47. static inline pte_t *hugepte_offset(hugepd_t *hpdp, unsigned long addr)
  48. {
  49. unsigned long idx = ((addr >> HPAGE_SHIFT) & (PTRS_PER_HUGEPTE-1));
  50. pte_t *dir = hugepd_page(*hpdp);
  51. return dir + idx;
  52. }
  53. static int __hugepte_alloc(struct mm_struct *mm, hugepd_t *hpdp,
  54. unsigned long address)
  55. {
  56. pte_t *new = kmem_cache_alloc(huge_pgtable_cache,
  57. GFP_KERNEL|__GFP_REPEAT);
  58. if (! new)
  59. return -ENOMEM;
  60. spin_lock(&mm->page_table_lock);
  61. if (!hugepd_none(*hpdp))
  62. kmem_cache_free(huge_pgtable_cache, new);
  63. else
  64. hpdp->pd = (unsigned long)new | HUGEPD_OK;
  65. spin_unlock(&mm->page_table_lock);
  66. return 0;
  67. }
  68. /* Base page size affects how we walk hugetlb page tables */
  69. #ifdef CONFIG_PPC_64K_PAGES
  70. #define hpmd_offset(pud, addr) pmd_offset(pud, addr)
  71. #define hpmd_alloc(mm, pud, addr) pmd_alloc(mm, pud, addr)
  72. #else
  73. static inline
  74. pmd_t *hpmd_offset(pud_t *pud, unsigned long addr)
  75. {
  76. if (HPAGE_SHIFT == HPAGE_SHIFT_64K)
  77. return pmd_offset(pud, addr);
  78. else
  79. return (pmd_t *) pud;
  80. }
  81. static inline
  82. pmd_t *hpmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long addr)
  83. {
  84. if (HPAGE_SHIFT == HPAGE_SHIFT_64K)
  85. return pmd_alloc(mm, pud, addr);
  86. else
  87. return (pmd_t *) pud;
  88. }
  89. #endif
  90. /* Modelled after find_linux_pte() */
  91. pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
  92. {
  93. pgd_t *pg;
  94. pud_t *pu;
  95. pmd_t *pm;
  96. BUG_ON(get_slice_psize(mm, addr) != mmu_huge_psize);
  97. addr &= HPAGE_MASK;
  98. pg = pgd_offset(mm, addr);
  99. if (!pgd_none(*pg)) {
  100. pu = pud_offset(pg, addr);
  101. if (!pud_none(*pu)) {
  102. pm = hpmd_offset(pu, addr);
  103. if (!pmd_none(*pm))
  104. return hugepte_offset((hugepd_t *)pm, addr);
  105. }
  106. }
  107. return NULL;
  108. }
  109. pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr)
  110. {
  111. pgd_t *pg;
  112. pud_t *pu;
  113. pmd_t *pm;
  114. hugepd_t *hpdp = NULL;
  115. BUG_ON(get_slice_psize(mm, addr) != mmu_huge_psize);
  116. addr &= HPAGE_MASK;
  117. pg = pgd_offset(mm, addr);
  118. pu = pud_alloc(mm, pg, addr);
  119. if (pu) {
  120. pm = hpmd_alloc(mm, pu, addr);
  121. if (pm)
  122. hpdp = (hugepd_t *)pm;
  123. }
  124. if (! hpdp)
  125. return NULL;
  126. if (hugepd_none(*hpdp) && __hugepte_alloc(mm, hpdp, addr))
  127. return NULL;
  128. return hugepte_offset(hpdp, addr);
  129. }
  130. int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
  131. {
  132. return 0;
  133. }
  134. static void free_hugepte_range(struct mmu_gather *tlb, hugepd_t *hpdp)
  135. {
  136. pte_t *hugepte = hugepd_page(*hpdp);
  137. hpdp->pd = 0;
  138. tlb->need_flush = 1;
  139. pgtable_free_tlb(tlb, pgtable_free_cache(hugepte, HUGEPTE_CACHE_NUM,
  140. PGF_CACHENUM_MASK));
  141. }
  142. static void hugetlb_free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
  143. unsigned long addr, unsigned long end,
  144. unsigned long floor, unsigned long ceiling)
  145. {
  146. pmd_t *pmd;
  147. unsigned long next;
  148. unsigned long start;
  149. start = addr;
  150. pmd = pmd_offset(pud, addr);
  151. do {
  152. next = pmd_addr_end(addr, end);
  153. if (pmd_none(*pmd))
  154. continue;
  155. free_hugepte_range(tlb, (hugepd_t *)pmd);
  156. } while (pmd++, addr = next, addr != end);
  157. start &= PUD_MASK;
  158. if (start < floor)
  159. return;
  160. if (ceiling) {
  161. ceiling &= PUD_MASK;
  162. if (!ceiling)
  163. return;
  164. }
  165. if (end - 1 > ceiling - 1)
  166. return;
  167. pmd = pmd_offset(pud, start);
  168. pud_clear(pud);
  169. pmd_free_tlb(tlb, pmd);
  170. }
  171. static void hugetlb_free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
  172. unsigned long addr, unsigned long end,
  173. unsigned long floor, unsigned long ceiling)
  174. {
  175. pud_t *pud;
  176. unsigned long next;
  177. unsigned long start;
  178. start = addr;
  179. pud = pud_offset(pgd, addr);
  180. do {
  181. next = pud_addr_end(addr, end);
  182. #ifdef CONFIG_PPC_64K_PAGES
  183. if (pud_none_or_clear_bad(pud))
  184. continue;
  185. hugetlb_free_pmd_range(tlb, pud, addr, next, floor, ceiling);
  186. #else
  187. if (HPAGE_SHIFT == HPAGE_SHIFT_64K) {
  188. if (pud_none_or_clear_bad(pud))
  189. continue;
  190. hugetlb_free_pmd_range(tlb, pud, addr, next, floor, ceiling);
  191. } else {
  192. if (pud_none(*pud))
  193. continue;
  194. free_hugepte_range(tlb, (hugepd_t *)pud);
  195. }
  196. #endif
  197. } while (pud++, addr = next, addr != end);
  198. start &= PGDIR_MASK;
  199. if (start < floor)
  200. return;
  201. if (ceiling) {
  202. ceiling &= PGDIR_MASK;
  203. if (!ceiling)
  204. return;
  205. }
  206. if (end - 1 > ceiling - 1)
  207. return;
  208. pud = pud_offset(pgd, start);
  209. pgd_clear(pgd);
  210. pud_free_tlb(tlb, pud);
  211. }
  212. /*
  213. * This function frees user-level page tables of a process.
  214. *
  215. * Must be called with pagetable lock held.
  216. */
  217. void hugetlb_free_pgd_range(struct mmu_gather **tlb,
  218. unsigned long addr, unsigned long end,
  219. unsigned long floor, unsigned long ceiling)
  220. {
  221. pgd_t *pgd;
  222. unsigned long next;
  223. unsigned long start;
  224. /*
  225. * Comments below take from the normal free_pgd_range(). They
  226. * apply here too. The tests against HUGEPD_MASK below are
  227. * essential, because we *don't* test for this at the bottom
  228. * level. Without them we'll attempt to free a hugepte table
  229. * when we unmap just part of it, even if there are other
  230. * active mappings using it.
  231. *
  232. * The next few lines have given us lots of grief...
  233. *
  234. * Why are we testing HUGEPD* at this top level? Because
  235. * often there will be no work to do at all, and we'd prefer
  236. * not to go all the way down to the bottom just to discover
  237. * that.
  238. *
  239. * Why all these "- 1"s? Because 0 represents both the bottom
  240. * of the address space and the top of it (using -1 for the
  241. * top wouldn't help much: the masks would do the wrong thing).
  242. * The rule is that addr 0 and floor 0 refer to the bottom of
  243. * the address space, but end 0 and ceiling 0 refer to the top
  244. * Comparisons need to use "end - 1" and "ceiling - 1" (though
  245. * that end 0 case should be mythical).
  246. *
  247. * Wherever addr is brought up or ceiling brought down, we
  248. * must be careful to reject "the opposite 0" before it
  249. * confuses the subsequent tests. But what about where end is
  250. * brought down by HUGEPD_SIZE below? no, end can't go down to
  251. * 0 there.
  252. *
  253. * Whereas we round start (addr) and ceiling down, by different
  254. * masks at different levels, in order to test whether a table
  255. * now has no other vmas using it, so can be freed, we don't
  256. * bother to round floor or end up - the tests don't need that.
  257. */
  258. addr &= HUGEPD_MASK;
  259. if (addr < floor) {
  260. addr += HUGEPD_SIZE;
  261. if (!addr)
  262. return;
  263. }
  264. if (ceiling) {
  265. ceiling &= HUGEPD_MASK;
  266. if (!ceiling)
  267. return;
  268. }
  269. if (end - 1 > ceiling - 1)
  270. end -= HUGEPD_SIZE;
  271. if (addr > end - 1)
  272. return;
  273. start = addr;
  274. pgd = pgd_offset((*tlb)->mm, addr);
  275. do {
  276. BUG_ON(get_slice_psize((*tlb)->mm, addr) != mmu_huge_psize);
  277. next = pgd_addr_end(addr, end);
  278. if (pgd_none_or_clear_bad(pgd))
  279. continue;
  280. hugetlb_free_pud_range(*tlb, pgd, addr, next, floor, ceiling);
  281. } while (pgd++, addr = next, addr != end);
  282. }
  283. void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
  284. pte_t *ptep, pte_t pte)
  285. {
  286. if (pte_present(*ptep)) {
  287. /* We open-code pte_clear because we need to pass the right
  288. * argument to hpte_need_flush (huge / !huge). Might not be
  289. * necessary anymore if we make hpte_need_flush() get the
  290. * page size from the slices
  291. */
  292. pte_update(mm, addr & HPAGE_MASK, ptep, ~0UL, 1);
  293. }
  294. *ptep = __pte(pte_val(pte) & ~_PAGE_HPTEFLAGS);
  295. }
  296. pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
  297. pte_t *ptep)
  298. {
  299. unsigned long old = pte_update(mm, addr, ptep, ~0UL, 1);
  300. return __pte(old);
  301. }
  302. struct page *
  303. follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
  304. {
  305. pte_t *ptep;
  306. struct page *page;
  307. if (get_slice_psize(mm, address) != mmu_huge_psize)
  308. return ERR_PTR(-EINVAL);
  309. ptep = huge_pte_offset(mm, address);
  310. page = pte_page(*ptep);
  311. if (page)
  312. page += (address % HPAGE_SIZE) / PAGE_SIZE;
  313. return page;
  314. }
  315. int pmd_huge(pmd_t pmd)
  316. {
  317. return 0;
  318. }
  319. struct page *
  320. follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  321. pmd_t *pmd, int write)
  322. {
  323. BUG();
  324. return NULL;
  325. }
  326. unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  327. unsigned long len, unsigned long pgoff,
  328. unsigned long flags)
  329. {
  330. return slice_get_unmapped_area(addr, len, flags,
  331. mmu_huge_psize, 1, 0);
  332. }
  333. /*
  334. * Called by asm hashtable.S for doing lazy icache flush
  335. */
  336. static unsigned int hash_huge_page_do_lazy_icache(unsigned long rflags,
  337. pte_t pte, int trap)
  338. {
  339. struct page *page;
  340. int i;
  341. if (!pfn_valid(pte_pfn(pte)))
  342. return rflags;
  343. page = pte_page(pte);
  344. /* page is dirty */
  345. if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
  346. if (trap == 0x400) {
  347. for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++)
  348. __flush_dcache_icache(page_address(page+i));
  349. set_bit(PG_arch_1, &page->flags);
  350. } else {
  351. rflags |= HPTE_R_N;
  352. }
  353. }
  354. return rflags;
  355. }
  356. int hash_huge_page(struct mm_struct *mm, unsigned long access,
  357. unsigned long ea, unsigned long vsid, int local,
  358. unsigned long trap)
  359. {
  360. pte_t *ptep;
  361. unsigned long old_pte, new_pte;
  362. unsigned long va, rflags, pa;
  363. long slot;
  364. int err = 1;
  365. int ssize = user_segment_size(ea);
  366. ptep = huge_pte_offset(mm, ea);
  367. /* Search the Linux page table for a match with va */
  368. va = hpt_va(ea, vsid, ssize);
  369. /*
  370. * If no pte found or not present, send the problem up to
  371. * do_page_fault
  372. */
  373. if (unlikely(!ptep || pte_none(*ptep)))
  374. goto out;
  375. /*
  376. * Check the user's access rights to the page. If access should be
  377. * prevented then send the problem up to do_page_fault.
  378. */
  379. if (unlikely(access & ~pte_val(*ptep)))
  380. goto out;
  381. /*
  382. * At this point, we have a pte (old_pte) which can be used to build
  383. * or update an HPTE. There are 2 cases:
  384. *
  385. * 1. There is a valid (present) pte with no associated HPTE (this is
  386. * the most common case)
  387. * 2. There is a valid (present) pte with an associated HPTE. The
  388. * current values of the pp bits in the HPTE prevent access
  389. * because we are doing software DIRTY bit management and the
  390. * page is currently not DIRTY.
  391. */
  392. do {
  393. old_pte = pte_val(*ptep);
  394. if (old_pte & _PAGE_BUSY)
  395. goto out;
  396. new_pte = old_pte | _PAGE_BUSY |
  397. _PAGE_ACCESSED | _PAGE_HASHPTE;
  398. } while(old_pte != __cmpxchg_u64((unsigned long *)ptep,
  399. old_pte, new_pte));
  400. rflags = 0x2 | (!(new_pte & _PAGE_RW));
  401. /* _PAGE_EXEC -> HW_NO_EXEC since it's inverted */
  402. rflags |= ((new_pte & _PAGE_EXEC) ? 0 : HPTE_R_N);
  403. if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
  404. /* No CPU has hugepages but lacks no execute, so we
  405. * don't need to worry about that case */
  406. rflags = hash_huge_page_do_lazy_icache(rflags, __pte(old_pte),
  407. trap);
  408. /* Check if pte already has an hpte (case 2) */
  409. if (unlikely(old_pte & _PAGE_HASHPTE)) {
  410. /* There MIGHT be an HPTE for this pte */
  411. unsigned long hash, slot;
  412. hash = hpt_hash(va, HPAGE_SHIFT, ssize);
  413. if (old_pte & _PAGE_F_SECOND)
  414. hash = ~hash;
  415. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  416. slot += (old_pte & _PAGE_F_GIX) >> 12;
  417. if (ppc_md.hpte_updatepp(slot, rflags, va, mmu_huge_psize,
  418. ssize, local) == -1)
  419. old_pte &= ~_PAGE_HPTEFLAGS;
  420. }
  421. if (likely(!(old_pte & _PAGE_HASHPTE))) {
  422. unsigned long hash = hpt_hash(va, HPAGE_SHIFT, ssize);
  423. unsigned long hpte_group;
  424. pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
  425. repeat:
  426. hpte_group = ((hash & htab_hash_mask) *
  427. HPTES_PER_GROUP) & ~0x7UL;
  428. /* clear HPTE slot informations in new PTE */
  429. new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | _PAGE_HASHPTE;
  430. /* Add in WIMG bits */
  431. /* XXX We should store these in the pte */
  432. /* --BenH: I think they are ... */
  433. rflags |= _PAGE_COHERENT;
  434. /* Insert into the hash table, primary slot */
  435. slot = ppc_md.hpte_insert(hpte_group, va, pa, rflags, 0,
  436. mmu_huge_psize, ssize);
  437. /* Primary is full, try the secondary */
  438. if (unlikely(slot == -1)) {
  439. hpte_group = ((~hash & htab_hash_mask) *
  440. HPTES_PER_GROUP) & ~0x7UL;
  441. slot = ppc_md.hpte_insert(hpte_group, va, pa, rflags,
  442. HPTE_V_SECONDARY,
  443. mmu_huge_psize, ssize);
  444. if (slot == -1) {
  445. if (mftb() & 0x1)
  446. hpte_group = ((hash & htab_hash_mask) *
  447. HPTES_PER_GROUP)&~0x7UL;
  448. ppc_md.hpte_remove(hpte_group);
  449. goto repeat;
  450. }
  451. }
  452. if (unlikely(slot == -2))
  453. panic("hash_huge_page: pte_insert failed\n");
  454. new_pte |= (slot << 12) & (_PAGE_F_SECOND | _PAGE_F_GIX);
  455. }
  456. /*
  457. * No need to use ldarx/stdcx here
  458. */
  459. *ptep = __pte(new_pte & ~_PAGE_BUSY);
  460. err = 0;
  461. out:
  462. return err;
  463. }
  464. void set_huge_psize(int psize)
  465. {
  466. /* Check that it is a page size supported by the hardware and
  467. * that it fits within pagetable limits. */
  468. if (mmu_psize_defs[psize].shift && mmu_psize_defs[psize].shift < SID_SHIFT &&
  469. (mmu_psize_defs[psize].shift > MIN_HUGEPTE_SHIFT ||
  470. mmu_psize_defs[psize].shift == HPAGE_SHIFT_64K)) {
  471. HPAGE_SHIFT = mmu_psize_defs[psize].shift;
  472. mmu_huge_psize = psize;
  473. #ifdef CONFIG_PPC_64K_PAGES
  474. hugepte_shift = (PMD_SHIFT-HPAGE_SHIFT);
  475. #else
  476. if (HPAGE_SHIFT == HPAGE_SHIFT_64K)
  477. hugepte_shift = (PMD_SHIFT-HPAGE_SHIFT);
  478. else
  479. hugepte_shift = (PUD_SHIFT-HPAGE_SHIFT);
  480. #endif
  481. } else
  482. HPAGE_SHIFT = 0;
  483. }
  484. static int __init hugepage_setup_sz(char *str)
  485. {
  486. unsigned long long size;
  487. int mmu_psize = -1;
  488. int shift;
  489. size = memparse(str, &str);
  490. shift = __ffs(size);
  491. switch (shift) {
  492. #ifndef CONFIG_PPC_64K_PAGES
  493. case HPAGE_SHIFT_64K:
  494. mmu_psize = MMU_PAGE_64K;
  495. break;
  496. #endif
  497. case HPAGE_SHIFT_16M:
  498. mmu_psize = MMU_PAGE_16M;
  499. break;
  500. }
  501. if (mmu_psize >=0 && mmu_psize_defs[mmu_psize].shift)
  502. set_huge_psize(mmu_psize);
  503. else
  504. printk(KERN_WARNING "Invalid huge page size specified(%llu)\n", size);
  505. return 1;
  506. }
  507. __setup("hugepagesz=", hugepage_setup_sz);
  508. static void zero_ctor(struct kmem_cache *cache, void *addr)
  509. {
  510. memset(addr, 0, kmem_cache_size(cache));
  511. }
  512. static int __init hugetlbpage_init(void)
  513. {
  514. if (!cpu_has_feature(CPU_FTR_16M_PAGE))
  515. return -ENODEV;
  516. huge_pgtable_cache = kmem_cache_create("hugepte_cache",
  517. HUGEPTE_TABLE_SIZE,
  518. HUGEPTE_TABLE_SIZE,
  519. 0,
  520. zero_ctor);
  521. if (! huge_pgtable_cache)
  522. panic("hugetlbpage_init(): could not create hugepte cache\n");
  523. return 0;
  524. }
  525. module_init(hugetlbpage_init);