hugetlbpage.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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/smp_lock.h>
  15. #include <linux/slab.h>
  16. #include <linux/err.h>
  17. #include <linux/sysctl.h>
  18. #include <asm/mman.h>
  19. #include <asm/pgalloc.h>
  20. #include <asm/tlb.h>
  21. #include <asm/tlbflush.h>
  22. #include <asm/mmu_context.h>
  23. #include <asm/machdep.h>
  24. #include <asm/cputable.h>
  25. #include <asm/tlb.h>
  26. #include <linux/sysctl.h>
  27. /* Modelled after find_linux_pte() */
  28. pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
  29. {
  30. pgd_t *pg;
  31. pud_t *pu;
  32. pmd_t *pm;
  33. pte_t *pt;
  34. BUG_ON(! in_hugepage_area(mm->context, addr));
  35. addr &= HPAGE_MASK;
  36. pg = pgd_offset(mm, addr);
  37. if (!pgd_none(*pg)) {
  38. pu = pud_offset(pg, addr);
  39. if (!pud_none(*pu)) {
  40. pm = pmd_offset(pu, addr);
  41. pt = (pte_t *)pm;
  42. BUG_ON(!pmd_none(*pm)
  43. && !(pte_present(*pt) && pte_huge(*pt)));
  44. return pt;
  45. }
  46. }
  47. return NULL;
  48. }
  49. pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr)
  50. {
  51. pgd_t *pg;
  52. pud_t *pu;
  53. pmd_t *pm;
  54. pte_t *pt;
  55. BUG_ON(! in_hugepage_area(mm->context, addr));
  56. addr &= HPAGE_MASK;
  57. pg = pgd_offset(mm, addr);
  58. pu = pud_alloc(mm, pg, addr);
  59. if (pu) {
  60. pm = pmd_alloc(mm, pu, addr);
  61. if (pm) {
  62. pt = (pte_t *)pm;
  63. BUG_ON(!pmd_none(*pm)
  64. && !(pte_present(*pt) && pte_huge(*pt)));
  65. return pt;
  66. }
  67. }
  68. return NULL;
  69. }
  70. #define HUGEPTE_BATCH_SIZE (HPAGE_SIZE / PMD_SIZE)
  71. void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
  72. pte_t *ptep, pte_t pte)
  73. {
  74. int i;
  75. if (pte_present(*ptep)) {
  76. pte_clear(mm, addr, ptep);
  77. flush_tlb_pending();
  78. }
  79. for (i = 0; i < HUGEPTE_BATCH_SIZE; i++) {
  80. *ptep = __pte(pte_val(pte) & ~_PAGE_HPTEFLAGS);
  81. ptep++;
  82. }
  83. }
  84. pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
  85. pte_t *ptep)
  86. {
  87. unsigned long old = pte_update(ptep, ~0UL);
  88. int i;
  89. if (old & _PAGE_HASHPTE)
  90. hpte_update(mm, addr, old, 0);
  91. for (i = 1; i < HUGEPTE_BATCH_SIZE; i++)
  92. ptep[i] = __pte(0);
  93. return __pte(old);
  94. }
  95. /*
  96. * This function checks for proper alignment of input addr and len parameters.
  97. */
  98. int is_aligned_hugepage_range(unsigned long addr, unsigned long len)
  99. {
  100. if (len & ~HPAGE_MASK)
  101. return -EINVAL;
  102. if (addr & ~HPAGE_MASK)
  103. return -EINVAL;
  104. if (! (within_hugepage_low_range(addr, len)
  105. || within_hugepage_high_range(addr, len)) )
  106. return -EINVAL;
  107. return 0;
  108. }
  109. static void flush_segments(void *parm)
  110. {
  111. u16 segs = (unsigned long) parm;
  112. unsigned long i;
  113. asm volatile("isync" : : : "memory");
  114. for (i = 0; i < 16; i++) {
  115. if (! (segs & (1U << i)))
  116. continue;
  117. asm volatile("slbie %0" : : "r" (i << SID_SHIFT));
  118. }
  119. asm volatile("isync" : : : "memory");
  120. }
  121. static int prepare_low_seg_for_htlb(struct mm_struct *mm, unsigned long seg)
  122. {
  123. unsigned long start = seg << SID_SHIFT;
  124. unsigned long end = (seg+1) << SID_SHIFT;
  125. struct vm_area_struct *vma;
  126. BUG_ON(seg >= 16);
  127. /* Check no VMAs are in the region */
  128. vma = find_vma(mm, start);
  129. if (vma && (vma->vm_start < end))
  130. return -EBUSY;
  131. return 0;
  132. }
  133. static int open_low_hpage_segs(struct mm_struct *mm, u16 newsegs)
  134. {
  135. unsigned long i;
  136. newsegs &= ~(mm->context.htlb_segs);
  137. if (! newsegs)
  138. return 0; /* The segments we want are already open */
  139. for (i = 0; i < 16; i++)
  140. if ((1 << i) & newsegs)
  141. if (prepare_low_seg_for_htlb(mm, i) != 0)
  142. return -EBUSY;
  143. mm->context.htlb_segs |= newsegs;
  144. /* update the paca copy of the context struct */
  145. get_paca()->context = mm->context;
  146. /* the context change must make it to memory before the flush,
  147. * so that further SLB misses do the right thing. */
  148. mb();
  149. on_each_cpu(flush_segments, (void *)(unsigned long)newsegs, 0, 1);
  150. return 0;
  151. }
  152. int prepare_hugepage_range(unsigned long addr, unsigned long len)
  153. {
  154. if (within_hugepage_high_range(addr, len))
  155. return 0;
  156. else if ((addr < 0x100000000UL) && ((addr+len) < 0x100000000UL)) {
  157. int err;
  158. /* Yes, we need both tests, in case addr+len overflows
  159. * 64-bit arithmetic */
  160. err = open_low_hpage_segs(current->mm,
  161. LOW_ESID_MASK(addr, len));
  162. if (err)
  163. printk(KERN_DEBUG "prepare_hugepage_range(%lx, %lx)"
  164. " failed (segs: 0x%04hx)\n", addr, len,
  165. LOW_ESID_MASK(addr, len));
  166. return err;
  167. }
  168. return -EINVAL;
  169. }
  170. struct page *
  171. follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
  172. {
  173. pte_t *ptep;
  174. struct page *page;
  175. if (! in_hugepage_area(mm->context, address))
  176. return ERR_PTR(-EINVAL);
  177. ptep = huge_pte_offset(mm, address);
  178. page = pte_page(*ptep);
  179. if (page)
  180. page += (address % HPAGE_SIZE) / PAGE_SIZE;
  181. return page;
  182. }
  183. int pmd_huge(pmd_t pmd)
  184. {
  185. return 0;
  186. }
  187. struct page *
  188. follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  189. pmd_t *pmd, int write)
  190. {
  191. BUG();
  192. return NULL;
  193. }
  194. /* Because we have an exclusive hugepage region which lies within the
  195. * normal user address space, we have to take special measures to make
  196. * non-huge mmap()s evade the hugepage reserved regions. */
  197. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
  198. unsigned long len, unsigned long pgoff,
  199. unsigned long flags)
  200. {
  201. struct mm_struct *mm = current->mm;
  202. struct vm_area_struct *vma;
  203. unsigned long start_addr;
  204. if (len > TASK_SIZE)
  205. return -ENOMEM;
  206. if (addr) {
  207. addr = PAGE_ALIGN(addr);
  208. vma = find_vma(mm, addr);
  209. if (((TASK_SIZE - len) >= addr)
  210. && (!vma || (addr+len) <= vma->vm_start)
  211. && !is_hugepage_only_range(mm, addr,len))
  212. return addr;
  213. }
  214. if (len > mm->cached_hole_size) {
  215. start_addr = addr = mm->free_area_cache;
  216. } else {
  217. start_addr = addr = TASK_UNMAPPED_BASE;
  218. mm->cached_hole_size = 0;
  219. }
  220. full_search:
  221. vma = find_vma(mm, addr);
  222. while (TASK_SIZE - len >= addr) {
  223. BUG_ON(vma && (addr >= vma->vm_end));
  224. if (touches_hugepage_low_range(mm, addr, len)) {
  225. addr = ALIGN(addr+1, 1<<SID_SHIFT);
  226. vma = find_vma(mm, addr);
  227. continue;
  228. }
  229. if (touches_hugepage_high_range(addr, len)) {
  230. addr = TASK_HPAGE_END;
  231. vma = find_vma(mm, addr);
  232. continue;
  233. }
  234. if (!vma || addr + len <= vma->vm_start) {
  235. /*
  236. * Remember the place where we stopped the search:
  237. */
  238. mm->free_area_cache = addr + len;
  239. return addr;
  240. }
  241. if (addr + mm->cached_hole_size < vma->vm_start)
  242. mm->cached_hole_size = vma->vm_start - addr;
  243. addr = vma->vm_end;
  244. vma = vma->vm_next;
  245. }
  246. /* Make sure we didn't miss any holes */
  247. if (start_addr != TASK_UNMAPPED_BASE) {
  248. start_addr = addr = TASK_UNMAPPED_BASE;
  249. mm->cached_hole_size = 0;
  250. goto full_search;
  251. }
  252. return -ENOMEM;
  253. }
  254. /*
  255. * This mmap-allocator allocates new areas top-down from below the
  256. * stack's low limit (the base):
  257. *
  258. * Because we have an exclusive hugepage region which lies within the
  259. * normal user address space, we have to take special measures to make
  260. * non-huge mmap()s evade the hugepage reserved regions.
  261. */
  262. unsigned long
  263. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  264. const unsigned long len, const unsigned long pgoff,
  265. const unsigned long flags)
  266. {
  267. struct vm_area_struct *vma, *prev_vma;
  268. struct mm_struct *mm = current->mm;
  269. unsigned long base = mm->mmap_base, addr = addr0;
  270. unsigned long largest_hole = mm->cached_hole_size;
  271. int first_time = 1;
  272. /* requested length too big for entire address space */
  273. if (len > TASK_SIZE)
  274. return -ENOMEM;
  275. /* dont allow allocations above current base */
  276. if (mm->free_area_cache > base)
  277. mm->free_area_cache = base;
  278. /* requesting a specific address */
  279. if (addr) {
  280. addr = PAGE_ALIGN(addr);
  281. vma = find_vma(mm, addr);
  282. if (TASK_SIZE - len >= addr &&
  283. (!vma || addr + len <= vma->vm_start)
  284. && !is_hugepage_only_range(mm, addr,len))
  285. return addr;
  286. }
  287. if (len <= largest_hole) {
  288. largest_hole = 0;
  289. mm->free_area_cache = base;
  290. }
  291. try_again:
  292. /* make sure it can fit in the remaining address space */
  293. if (mm->free_area_cache < len)
  294. goto fail;
  295. /* either no address requested or cant fit in requested address hole */
  296. addr = (mm->free_area_cache - len) & PAGE_MASK;
  297. do {
  298. hugepage_recheck:
  299. if (touches_hugepage_low_range(mm, addr, len)) {
  300. addr = (addr & ((~0) << SID_SHIFT)) - len;
  301. goto hugepage_recheck;
  302. } else if (touches_hugepage_high_range(addr, len)) {
  303. addr = TASK_HPAGE_BASE - len;
  304. }
  305. /*
  306. * Lookup failure means no vma is above this address,
  307. * i.e. return with success:
  308. */
  309. if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
  310. return addr;
  311. /*
  312. * new region fits between prev_vma->vm_end and
  313. * vma->vm_start, use it:
  314. */
  315. if (addr+len <= vma->vm_start &&
  316. (!prev_vma || (addr >= prev_vma->vm_end))) {
  317. /* remember the address as a hint for next time */
  318. mm->cached_hole_size = largest_hole;
  319. return (mm->free_area_cache = addr);
  320. } else {
  321. /* pull free_area_cache down to the first hole */
  322. if (mm->free_area_cache == vma->vm_end) {
  323. mm->free_area_cache = vma->vm_start;
  324. mm->cached_hole_size = largest_hole;
  325. }
  326. }
  327. /* remember the largest hole we saw so far */
  328. if (addr + largest_hole < vma->vm_start)
  329. largest_hole = vma->vm_start - addr;
  330. /* try just below the current vma->vm_start */
  331. addr = vma->vm_start-len;
  332. } while (len <= vma->vm_start);
  333. fail:
  334. /*
  335. * if hint left us with no space for the requested
  336. * mapping then try again:
  337. */
  338. if (first_time) {
  339. mm->free_area_cache = base;
  340. largest_hole = 0;
  341. first_time = 0;
  342. goto try_again;
  343. }
  344. /*
  345. * A failed mmap() very likely causes application failure,
  346. * so fall back to the bottom-up function here. This scenario
  347. * can happen with large stack limits and large mmap()
  348. * allocations.
  349. */
  350. mm->free_area_cache = TASK_UNMAPPED_BASE;
  351. mm->cached_hole_size = ~0UL;
  352. addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
  353. /*
  354. * Restore the topdown base:
  355. */
  356. mm->free_area_cache = base;
  357. mm->cached_hole_size = ~0UL;
  358. return addr;
  359. }
  360. static unsigned long htlb_get_low_area(unsigned long len, u16 segmask)
  361. {
  362. unsigned long addr = 0;
  363. struct vm_area_struct *vma;
  364. vma = find_vma(current->mm, addr);
  365. while (addr + len <= 0x100000000UL) {
  366. BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
  367. if (! __within_hugepage_low_range(addr, len, segmask)) {
  368. addr = ALIGN(addr+1, 1<<SID_SHIFT);
  369. vma = find_vma(current->mm, addr);
  370. continue;
  371. }
  372. if (!vma || (addr + len) <= vma->vm_start)
  373. return addr;
  374. addr = ALIGN(vma->vm_end, HPAGE_SIZE);
  375. /* Depending on segmask this might not be a confirmed
  376. * hugepage region, so the ALIGN could have skipped
  377. * some VMAs */
  378. vma = find_vma(current->mm, addr);
  379. }
  380. return -ENOMEM;
  381. }
  382. static unsigned long htlb_get_high_area(unsigned long len)
  383. {
  384. unsigned long addr = TASK_HPAGE_BASE;
  385. struct vm_area_struct *vma;
  386. vma = find_vma(current->mm, addr);
  387. for (vma = find_vma(current->mm, addr);
  388. addr + len <= TASK_HPAGE_END;
  389. vma = vma->vm_next) {
  390. BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
  391. BUG_ON(! within_hugepage_high_range(addr, len));
  392. if (!vma || (addr + len) <= vma->vm_start)
  393. return addr;
  394. addr = ALIGN(vma->vm_end, HPAGE_SIZE);
  395. /* Because we're in a hugepage region, this alignment
  396. * should not skip us over any VMAs */
  397. }
  398. return -ENOMEM;
  399. }
  400. unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  401. unsigned long len, unsigned long pgoff,
  402. unsigned long flags)
  403. {
  404. if (len & ~HPAGE_MASK)
  405. return -EINVAL;
  406. if (!cpu_has_feature(CPU_FTR_16M_PAGE))
  407. return -EINVAL;
  408. if (test_thread_flag(TIF_32BIT)) {
  409. int lastshift = 0;
  410. u16 segmask, cursegs = current->mm->context.htlb_segs;
  411. /* First see if we can do the mapping in the existing
  412. * low hpage segments */
  413. addr = htlb_get_low_area(len, cursegs);
  414. if (addr != -ENOMEM)
  415. return addr;
  416. for (segmask = LOW_ESID_MASK(0x100000000UL-len, len);
  417. ! lastshift; segmask >>=1) {
  418. if (segmask & 1)
  419. lastshift = 1;
  420. addr = htlb_get_low_area(len, cursegs | segmask);
  421. if ((addr != -ENOMEM)
  422. && open_low_hpage_segs(current->mm, segmask) == 0)
  423. return addr;
  424. }
  425. printk(KERN_DEBUG "hugetlb_get_unmapped_area() unable to open"
  426. " enough segments\n");
  427. return -ENOMEM;
  428. } else {
  429. return htlb_get_high_area(len);
  430. }
  431. }
  432. int hash_huge_page(struct mm_struct *mm, unsigned long access,
  433. unsigned long ea, unsigned long vsid, int local)
  434. {
  435. pte_t *ptep;
  436. unsigned long va, vpn;
  437. pte_t old_pte, new_pte;
  438. unsigned long rflags, prpn;
  439. long slot;
  440. int err = 1;
  441. spin_lock(&mm->page_table_lock);
  442. ptep = huge_pte_offset(mm, ea);
  443. /* Search the Linux page table for a match with va */
  444. va = (vsid << 28) | (ea & 0x0fffffff);
  445. vpn = va >> HPAGE_SHIFT;
  446. /*
  447. * If no pte found or not present, send the problem up to
  448. * do_page_fault
  449. */
  450. if (unlikely(!ptep || pte_none(*ptep)))
  451. goto out;
  452. /* BUG_ON(pte_bad(*ptep)); */
  453. /*
  454. * Check the user's access rights to the page. If access should be
  455. * prevented then send the problem up to do_page_fault.
  456. */
  457. if (unlikely(access & ~pte_val(*ptep)))
  458. goto out;
  459. /*
  460. * At this point, we have a pte (old_pte) which can be used to build
  461. * or update an HPTE. There are 2 cases:
  462. *
  463. * 1. There is a valid (present) pte with no associated HPTE (this is
  464. * the most common case)
  465. * 2. There is a valid (present) pte with an associated HPTE. The
  466. * current values of the pp bits in the HPTE prevent access
  467. * because we are doing software DIRTY bit management and the
  468. * page is currently not DIRTY.
  469. */
  470. old_pte = *ptep;
  471. new_pte = old_pte;
  472. rflags = 0x2 | (! (pte_val(new_pte) & _PAGE_RW));
  473. /* _PAGE_EXEC -> HW_NO_EXEC since it's inverted */
  474. rflags |= ((pte_val(new_pte) & _PAGE_EXEC) ? 0 : HW_NO_EXEC);
  475. /* Check if pte already has an hpte (case 2) */
  476. if (unlikely(pte_val(old_pte) & _PAGE_HASHPTE)) {
  477. /* There MIGHT be an HPTE for this pte */
  478. unsigned long hash, slot;
  479. hash = hpt_hash(vpn, 1);
  480. if (pte_val(old_pte) & _PAGE_SECONDARY)
  481. hash = ~hash;
  482. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  483. slot += (pte_val(old_pte) & _PAGE_GROUP_IX) >> 12;
  484. if (ppc_md.hpte_updatepp(slot, rflags, va, 1, local) == -1)
  485. pte_val(old_pte) &= ~_PAGE_HPTEFLAGS;
  486. }
  487. if (likely(!(pte_val(old_pte) & _PAGE_HASHPTE))) {
  488. unsigned long hash = hpt_hash(vpn, 1);
  489. unsigned long hpte_group;
  490. prpn = pte_pfn(old_pte);
  491. repeat:
  492. hpte_group = ((hash & htab_hash_mask) *
  493. HPTES_PER_GROUP) & ~0x7UL;
  494. /* Update the linux pte with the HPTE slot */
  495. pte_val(new_pte) &= ~_PAGE_HPTEFLAGS;
  496. pte_val(new_pte) |= _PAGE_HASHPTE;
  497. /* Add in WIMG bits */
  498. /* XXX We should store these in the pte */
  499. rflags |= _PAGE_COHERENT;
  500. slot = ppc_md.hpte_insert(hpte_group, va, prpn,
  501. HPTE_V_LARGE, rflags);
  502. /* Primary is full, try the secondary */
  503. if (unlikely(slot == -1)) {
  504. pte_val(new_pte) |= _PAGE_SECONDARY;
  505. hpte_group = ((~hash & htab_hash_mask) *
  506. HPTES_PER_GROUP) & ~0x7UL;
  507. slot = ppc_md.hpte_insert(hpte_group, va, prpn,
  508. HPTE_V_LARGE, rflags);
  509. if (slot == -1) {
  510. if (mftb() & 0x1)
  511. hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
  512. ppc_md.hpte_remove(hpte_group);
  513. goto repeat;
  514. }
  515. }
  516. if (unlikely(slot == -2))
  517. panic("hash_huge_page: pte_insert failed\n");
  518. pte_val(new_pte) |= (slot<<12) & _PAGE_GROUP_IX;
  519. /*
  520. * No need to use ldarx/stdcx here because all who
  521. * might be updating the pte will hold the
  522. * page_table_lock
  523. */
  524. *ptep = new_pte;
  525. }
  526. err = 0;
  527. out:
  528. spin_unlock(&mm->page_table_lock);
  529. return err;
  530. }