hugetlbpage.c 13 KB

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