hugetlbpage.c 13 KB

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