hugetlbpage.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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. #define HUGEPGDIR_SHIFT (HPAGE_SHIFT + PAGE_SHIFT - 3)
  28. #define HUGEPGDIR_SIZE (1UL << HUGEPGDIR_SHIFT)
  29. #define HUGEPGDIR_MASK (~(HUGEPGDIR_SIZE-1))
  30. #define HUGEPTE_INDEX_SIZE 9
  31. #define HUGEPGD_INDEX_SIZE 10
  32. #define PTRS_PER_HUGEPTE (1 << HUGEPTE_INDEX_SIZE)
  33. #define PTRS_PER_HUGEPGD (1 << HUGEPGD_INDEX_SIZE)
  34. static inline int hugepgd_index(unsigned long addr)
  35. {
  36. return (addr & ~REGION_MASK) >> HUGEPGDIR_SHIFT;
  37. }
  38. static pud_t *hugepgd_offset(struct mm_struct *mm, unsigned long addr)
  39. {
  40. int index;
  41. if (! mm->context.huge_pgdir)
  42. return NULL;
  43. index = hugepgd_index(addr);
  44. BUG_ON(index >= PTRS_PER_HUGEPGD);
  45. return (pud_t *)(mm->context.huge_pgdir + index);
  46. }
  47. static inline pte_t *hugepte_offset(pud_t *dir, unsigned long addr)
  48. {
  49. int index;
  50. if (pud_none(*dir))
  51. return NULL;
  52. index = (addr >> HPAGE_SHIFT) % PTRS_PER_HUGEPTE;
  53. return (pte_t *)pud_page(*dir) + index;
  54. }
  55. static pud_t *hugepgd_alloc(struct mm_struct *mm, unsigned long addr)
  56. {
  57. BUG_ON(! in_hugepage_area(mm->context, addr));
  58. if (! mm->context.huge_pgdir) {
  59. pgd_t *new;
  60. spin_unlock(&mm->page_table_lock);
  61. /* Don't use pgd_alloc(), because we want __GFP_REPEAT */
  62. new = kmem_cache_alloc(zero_cache, GFP_KERNEL | __GFP_REPEAT);
  63. BUG_ON(memcmp(new, empty_zero_page, PAGE_SIZE));
  64. spin_lock(&mm->page_table_lock);
  65. /*
  66. * Because we dropped the lock, we should re-check the
  67. * entry, as somebody else could have populated it..
  68. */
  69. if (mm->context.huge_pgdir)
  70. pgd_free(new);
  71. else
  72. mm->context.huge_pgdir = new;
  73. }
  74. return hugepgd_offset(mm, addr);
  75. }
  76. static pte_t *hugepte_alloc(struct mm_struct *mm, pud_t *dir, unsigned long addr)
  77. {
  78. if (! pud_present(*dir)) {
  79. pte_t *new;
  80. spin_unlock(&mm->page_table_lock);
  81. new = kmem_cache_alloc(zero_cache, GFP_KERNEL | __GFP_REPEAT);
  82. BUG_ON(memcmp(new, empty_zero_page, PAGE_SIZE));
  83. spin_lock(&mm->page_table_lock);
  84. /*
  85. * Because we dropped the lock, we should re-check the
  86. * entry, as somebody else could have populated it..
  87. */
  88. if (pud_present(*dir)) {
  89. if (new)
  90. kmem_cache_free(zero_cache, new);
  91. } else {
  92. struct page *ptepage;
  93. if (! new)
  94. return NULL;
  95. ptepage = virt_to_page(new);
  96. ptepage->mapping = (void *) mm;
  97. ptepage->index = addr & HUGEPGDIR_MASK;
  98. pud_populate(mm, dir, new);
  99. }
  100. }
  101. return hugepte_offset(dir, addr);
  102. }
  103. static pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
  104. {
  105. pud_t *pud;
  106. BUG_ON(! in_hugepage_area(mm->context, addr));
  107. pud = hugepgd_offset(mm, addr);
  108. if (! pud)
  109. return NULL;
  110. return hugepte_offset(pud, addr);
  111. }
  112. static pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr)
  113. {
  114. pud_t *pud;
  115. BUG_ON(! in_hugepage_area(mm->context, addr));
  116. pud = hugepgd_alloc(mm, addr);
  117. if (! pud)
  118. return NULL;
  119. return hugepte_alloc(mm, pud, addr);
  120. }
  121. static void set_huge_pte(struct mm_struct *mm, struct vm_area_struct *vma,
  122. unsigned long addr, struct page *page,
  123. pte_t *ptep, int write_access)
  124. {
  125. pte_t entry;
  126. add_mm_counter(mm, rss, HPAGE_SIZE / PAGE_SIZE);
  127. if (write_access) {
  128. entry =
  129. pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
  130. } else {
  131. entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot));
  132. }
  133. entry = pte_mkyoung(entry);
  134. entry = pte_mkhuge(entry);
  135. set_pte_at(mm, addr, ptep, entry);
  136. }
  137. /*
  138. * This function checks for proper alignment of input addr and len parameters.
  139. */
  140. int is_aligned_hugepage_range(unsigned long addr, unsigned long len)
  141. {
  142. if (len & ~HPAGE_MASK)
  143. return -EINVAL;
  144. if (addr & ~HPAGE_MASK)
  145. return -EINVAL;
  146. if (! (within_hugepage_low_range(addr, len)
  147. || within_hugepage_high_range(addr, len)) )
  148. return -EINVAL;
  149. return 0;
  150. }
  151. static void flush_segments(void *parm)
  152. {
  153. u16 segs = (unsigned long) parm;
  154. unsigned long i;
  155. asm volatile("isync" : : : "memory");
  156. for (i = 0; i < 16; i++) {
  157. if (! (segs & (1U << i)))
  158. continue;
  159. asm volatile("slbie %0" : : "r" (i << SID_SHIFT));
  160. }
  161. asm volatile("isync" : : : "memory");
  162. }
  163. static int prepare_low_seg_for_htlb(struct mm_struct *mm, unsigned long seg)
  164. {
  165. unsigned long start = seg << SID_SHIFT;
  166. unsigned long end = (seg+1) << SID_SHIFT;
  167. struct vm_area_struct *vma;
  168. BUG_ON(seg >= 16);
  169. /* Check no VMAs are in the region */
  170. vma = find_vma(mm, start);
  171. if (vma && (vma->vm_start < end))
  172. return -EBUSY;
  173. return 0;
  174. }
  175. static int open_low_hpage_segs(struct mm_struct *mm, u16 newsegs)
  176. {
  177. unsigned long i;
  178. newsegs &= ~(mm->context.htlb_segs);
  179. if (! newsegs)
  180. return 0; /* The segments we want are already open */
  181. for (i = 0; i < 16; i++)
  182. if ((1 << i) & newsegs)
  183. if (prepare_low_seg_for_htlb(mm, i) != 0)
  184. return -EBUSY;
  185. mm->context.htlb_segs |= newsegs;
  186. /* update the paca copy of the context struct */
  187. get_paca()->context = mm->context;
  188. /* the context change must make it to memory before the flush,
  189. * so that further SLB misses do the right thing. */
  190. mb();
  191. on_each_cpu(flush_segments, (void *)(unsigned long)newsegs, 0, 1);
  192. return 0;
  193. }
  194. int prepare_hugepage_range(unsigned long addr, unsigned long len)
  195. {
  196. if (within_hugepage_high_range(addr, len))
  197. return 0;
  198. else if ((addr < 0x100000000UL) && ((addr+len) < 0x100000000UL)) {
  199. int err;
  200. /* Yes, we need both tests, in case addr+len overflows
  201. * 64-bit arithmetic */
  202. err = open_low_hpage_segs(current->mm,
  203. LOW_ESID_MASK(addr, len));
  204. if (err)
  205. printk(KERN_DEBUG "prepare_hugepage_range(%lx, %lx)"
  206. " failed (segs: 0x%04hx)\n", addr, len,
  207. LOW_ESID_MASK(addr, len));
  208. return err;
  209. }
  210. return -EINVAL;
  211. }
  212. int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
  213. struct vm_area_struct *vma)
  214. {
  215. pte_t *src_pte, *dst_pte, entry;
  216. struct page *ptepage;
  217. unsigned long addr = vma->vm_start;
  218. unsigned long end = vma->vm_end;
  219. int err = -ENOMEM;
  220. while (addr < end) {
  221. dst_pte = huge_pte_alloc(dst, addr);
  222. if (!dst_pte)
  223. goto out;
  224. src_pte = huge_pte_offset(src, addr);
  225. entry = *src_pte;
  226. ptepage = pte_page(entry);
  227. get_page(ptepage);
  228. add_mm_counter(dst, rss, HPAGE_SIZE / PAGE_SIZE);
  229. set_pte_at(dst, addr, dst_pte, entry);
  230. addr += HPAGE_SIZE;
  231. }
  232. err = 0;
  233. out:
  234. return err;
  235. }
  236. int
  237. follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
  238. struct page **pages, struct vm_area_struct **vmas,
  239. unsigned long *position, int *length, int i)
  240. {
  241. unsigned long vpfn, vaddr = *position;
  242. int remainder = *length;
  243. WARN_ON(!is_vm_hugetlb_page(vma));
  244. vpfn = vaddr/PAGE_SIZE;
  245. while (vaddr < vma->vm_end && remainder) {
  246. if (pages) {
  247. pte_t *pte;
  248. struct page *page;
  249. pte = huge_pte_offset(mm, vaddr);
  250. /* hugetlb should be locked, and hence, prefaulted */
  251. WARN_ON(!pte || pte_none(*pte));
  252. page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
  253. WARN_ON(!PageCompound(page));
  254. get_page(page);
  255. pages[i] = page;
  256. }
  257. if (vmas)
  258. vmas[i] = vma;
  259. vaddr += PAGE_SIZE;
  260. ++vpfn;
  261. --remainder;
  262. ++i;
  263. }
  264. *length = remainder;
  265. *position = vaddr;
  266. return i;
  267. }
  268. struct page *
  269. follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
  270. {
  271. pte_t *ptep;
  272. struct page *page;
  273. if (! in_hugepage_area(mm->context, address))
  274. return ERR_PTR(-EINVAL);
  275. ptep = huge_pte_offset(mm, address);
  276. page = pte_page(*ptep);
  277. if (page)
  278. page += (address % HPAGE_SIZE) / PAGE_SIZE;
  279. return page;
  280. }
  281. int pmd_huge(pmd_t pmd)
  282. {
  283. return 0;
  284. }
  285. struct page *
  286. follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  287. pmd_t *pmd, int write)
  288. {
  289. BUG();
  290. return NULL;
  291. }
  292. void unmap_hugepage_range(struct vm_area_struct *vma,
  293. unsigned long start, unsigned long end)
  294. {
  295. struct mm_struct *mm = vma->vm_mm;
  296. unsigned long addr;
  297. pte_t *ptep;
  298. struct page *page;
  299. WARN_ON(!is_vm_hugetlb_page(vma));
  300. BUG_ON((start % HPAGE_SIZE) != 0);
  301. BUG_ON((end % HPAGE_SIZE) != 0);
  302. for (addr = start; addr < end; addr += HPAGE_SIZE) {
  303. pte_t pte;
  304. ptep = huge_pte_offset(mm, addr);
  305. if (!ptep || pte_none(*ptep))
  306. continue;
  307. pte = *ptep;
  308. page = pte_page(pte);
  309. pte_clear(mm, addr, ptep);
  310. put_page(page);
  311. }
  312. add_mm_counter(mm, rss, -((end - start) >> PAGE_SHIFT));
  313. flush_tlb_pending();
  314. }
  315. int hugetlb_prefault(struct address_space *mapping, struct vm_area_struct *vma)
  316. {
  317. struct mm_struct *mm = current->mm;
  318. unsigned long addr;
  319. int ret = 0;
  320. WARN_ON(!is_vm_hugetlb_page(vma));
  321. BUG_ON((vma->vm_start % HPAGE_SIZE) != 0);
  322. BUG_ON((vma->vm_end % HPAGE_SIZE) != 0);
  323. spin_lock(&mm->page_table_lock);
  324. for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
  325. unsigned long idx;
  326. pte_t *pte = huge_pte_alloc(mm, addr);
  327. struct page *page;
  328. if (!pte) {
  329. ret = -ENOMEM;
  330. goto out;
  331. }
  332. if (! pte_none(*pte))
  333. continue;
  334. idx = ((addr - vma->vm_start) >> HPAGE_SHIFT)
  335. + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
  336. page = find_get_page(mapping, idx);
  337. if (!page) {
  338. /* charge the fs quota first */
  339. if (hugetlb_get_quota(mapping)) {
  340. ret = -ENOMEM;
  341. goto out;
  342. }
  343. page = alloc_huge_page();
  344. if (!page) {
  345. hugetlb_put_quota(mapping);
  346. ret = -ENOMEM;
  347. goto out;
  348. }
  349. ret = add_to_page_cache(page, mapping, idx, GFP_ATOMIC);
  350. if (! ret) {
  351. unlock_page(page);
  352. } else {
  353. hugetlb_put_quota(mapping);
  354. free_huge_page(page);
  355. goto out;
  356. }
  357. }
  358. set_huge_pte(mm, vma, addr, page, pte, vma->vm_flags & VM_WRITE);
  359. }
  360. out:
  361. spin_unlock(&mm->page_table_lock);
  362. return ret;
  363. }
  364. /* Because we have an exclusive hugepage region which lies within the
  365. * normal user address space, we have to take special measures to make
  366. * non-huge mmap()s evade the hugepage reserved regions. */
  367. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
  368. unsigned long len, unsigned long pgoff,
  369. unsigned long flags)
  370. {
  371. struct mm_struct *mm = current->mm;
  372. struct vm_area_struct *vma;
  373. unsigned long start_addr;
  374. if (len > TASK_SIZE)
  375. return -ENOMEM;
  376. if (addr) {
  377. addr = PAGE_ALIGN(addr);
  378. vma = find_vma(mm, addr);
  379. if (((TASK_SIZE - len) >= addr)
  380. && (!vma || (addr+len) <= vma->vm_start)
  381. && !is_hugepage_only_range(mm, addr,len))
  382. return addr;
  383. }
  384. start_addr = addr = mm->free_area_cache;
  385. full_search:
  386. vma = find_vma(mm, addr);
  387. while (TASK_SIZE - len >= addr) {
  388. BUG_ON(vma && (addr >= vma->vm_end));
  389. if (touches_hugepage_low_range(mm, addr, len)) {
  390. addr = ALIGN(addr+1, 1<<SID_SHIFT);
  391. vma = find_vma(mm, addr);
  392. continue;
  393. }
  394. if (touches_hugepage_high_range(addr, len)) {
  395. addr = TASK_HPAGE_END;
  396. vma = find_vma(mm, addr);
  397. continue;
  398. }
  399. if (!vma || addr + len <= vma->vm_start) {
  400. /*
  401. * Remember the place where we stopped the search:
  402. */
  403. mm->free_area_cache = addr + len;
  404. return addr;
  405. }
  406. addr = vma->vm_end;
  407. vma = vma->vm_next;
  408. }
  409. /* Make sure we didn't miss any holes */
  410. if (start_addr != TASK_UNMAPPED_BASE) {
  411. start_addr = addr = TASK_UNMAPPED_BASE;
  412. goto full_search;
  413. }
  414. return -ENOMEM;
  415. }
  416. /*
  417. * This mmap-allocator allocates new areas top-down from below the
  418. * stack's low limit (the base):
  419. *
  420. * Because we have an exclusive hugepage region which lies within the
  421. * normal user address space, we have to take special measures to make
  422. * non-huge mmap()s evade the hugepage reserved regions.
  423. */
  424. unsigned long
  425. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  426. const unsigned long len, const unsigned long pgoff,
  427. const unsigned long flags)
  428. {
  429. struct vm_area_struct *vma, *prev_vma;
  430. struct mm_struct *mm = current->mm;
  431. unsigned long base = mm->mmap_base, addr = addr0;
  432. int first_time = 1;
  433. /* requested length too big for entire address space */
  434. if (len > TASK_SIZE)
  435. return -ENOMEM;
  436. /* dont allow allocations above current base */
  437. if (mm->free_area_cache > base)
  438. mm->free_area_cache = base;
  439. /* requesting a specific address */
  440. if (addr) {
  441. addr = PAGE_ALIGN(addr);
  442. vma = find_vma(mm, addr);
  443. if (TASK_SIZE - len >= addr &&
  444. (!vma || addr + len <= vma->vm_start)
  445. && !is_hugepage_only_range(mm, addr,len))
  446. return addr;
  447. }
  448. try_again:
  449. /* make sure it can fit in the remaining address space */
  450. if (mm->free_area_cache < len)
  451. goto fail;
  452. /* either no address requested or cant fit in requested address hole */
  453. addr = (mm->free_area_cache - len) & PAGE_MASK;
  454. do {
  455. hugepage_recheck:
  456. if (touches_hugepage_low_range(mm, addr, len)) {
  457. addr = (addr & ((~0) << SID_SHIFT)) - len;
  458. goto hugepage_recheck;
  459. } else if (touches_hugepage_high_range(addr, len)) {
  460. addr = TASK_HPAGE_BASE - len;
  461. }
  462. /*
  463. * Lookup failure means no vma is above this address,
  464. * i.e. return with success:
  465. */
  466. if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
  467. return addr;
  468. /*
  469. * new region fits between prev_vma->vm_end and
  470. * vma->vm_start, use it:
  471. */
  472. if (addr+len <= vma->vm_start &&
  473. (!prev_vma || (addr >= prev_vma->vm_end)))
  474. /* remember the address as a hint for next time */
  475. return (mm->free_area_cache = addr);
  476. else
  477. /* pull free_area_cache down to the first hole */
  478. if (mm->free_area_cache == vma->vm_end)
  479. mm->free_area_cache = vma->vm_start;
  480. /* try just below the current vma->vm_start */
  481. addr = vma->vm_start-len;
  482. } while (len <= vma->vm_start);
  483. fail:
  484. /*
  485. * if hint left us with no space for the requested
  486. * mapping then try again:
  487. */
  488. if (first_time) {
  489. mm->free_area_cache = base;
  490. first_time = 0;
  491. goto try_again;
  492. }
  493. /*
  494. * A failed mmap() very likely causes application failure,
  495. * so fall back to the bottom-up function here. This scenario
  496. * can happen with large stack limits and large mmap()
  497. * allocations.
  498. */
  499. mm->free_area_cache = TASK_UNMAPPED_BASE;
  500. addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
  501. /*
  502. * Restore the topdown base:
  503. */
  504. mm->free_area_cache = base;
  505. return addr;
  506. }
  507. static unsigned long htlb_get_low_area(unsigned long len, u16 segmask)
  508. {
  509. unsigned long addr = 0;
  510. struct vm_area_struct *vma;
  511. vma = find_vma(current->mm, addr);
  512. while (addr + len <= 0x100000000UL) {
  513. BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
  514. if (! __within_hugepage_low_range(addr, len, segmask)) {
  515. addr = ALIGN(addr+1, 1<<SID_SHIFT);
  516. vma = find_vma(current->mm, addr);
  517. continue;
  518. }
  519. if (!vma || (addr + len) <= vma->vm_start)
  520. return addr;
  521. addr = ALIGN(vma->vm_end, HPAGE_SIZE);
  522. /* Depending on segmask this might not be a confirmed
  523. * hugepage region, so the ALIGN could have skipped
  524. * some VMAs */
  525. vma = find_vma(current->mm, addr);
  526. }
  527. return -ENOMEM;
  528. }
  529. static unsigned long htlb_get_high_area(unsigned long len)
  530. {
  531. unsigned long addr = TASK_HPAGE_BASE;
  532. struct vm_area_struct *vma;
  533. vma = find_vma(current->mm, addr);
  534. for (vma = find_vma(current->mm, addr);
  535. addr + len <= TASK_HPAGE_END;
  536. vma = vma->vm_next) {
  537. BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
  538. BUG_ON(! within_hugepage_high_range(addr, len));
  539. if (!vma || (addr + len) <= vma->vm_start)
  540. return addr;
  541. addr = ALIGN(vma->vm_end, HPAGE_SIZE);
  542. /* Because we're in a hugepage region, this alignment
  543. * should not skip us over any VMAs */
  544. }
  545. return -ENOMEM;
  546. }
  547. unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  548. unsigned long len, unsigned long pgoff,
  549. unsigned long flags)
  550. {
  551. if (len & ~HPAGE_MASK)
  552. return -EINVAL;
  553. if (!cpu_has_feature(CPU_FTR_16M_PAGE))
  554. return -EINVAL;
  555. if (test_thread_flag(TIF_32BIT)) {
  556. int lastshift = 0;
  557. u16 segmask, cursegs = current->mm->context.htlb_segs;
  558. /* First see if we can do the mapping in the existing
  559. * low hpage segments */
  560. addr = htlb_get_low_area(len, cursegs);
  561. if (addr != -ENOMEM)
  562. return addr;
  563. for (segmask = LOW_ESID_MASK(0x100000000UL-len, len);
  564. ! lastshift; segmask >>=1) {
  565. if (segmask & 1)
  566. lastshift = 1;
  567. addr = htlb_get_low_area(len, cursegs | segmask);
  568. if ((addr != -ENOMEM)
  569. && open_low_hpage_segs(current->mm, segmask) == 0)
  570. return addr;
  571. }
  572. printk(KERN_DEBUG "hugetlb_get_unmapped_area() unable to open"
  573. " enough segments\n");
  574. return -ENOMEM;
  575. } else {
  576. return htlb_get_high_area(len);
  577. }
  578. }
  579. void hugetlb_mm_free_pgd(struct mm_struct *mm)
  580. {
  581. int i;
  582. pgd_t *pgdir;
  583. spin_lock(&mm->page_table_lock);
  584. pgdir = mm->context.huge_pgdir;
  585. if (! pgdir)
  586. goto out;
  587. mm->context.huge_pgdir = NULL;
  588. /* cleanup any hugepte pages leftover */
  589. for (i = 0; i < PTRS_PER_HUGEPGD; i++) {
  590. pud_t *pud = (pud_t *)(pgdir + i);
  591. if (! pud_none(*pud)) {
  592. pte_t *pte = (pte_t *)pud_page(*pud);
  593. struct page *ptepage = virt_to_page(pte);
  594. ptepage->mapping = NULL;
  595. BUG_ON(memcmp(pte, empty_zero_page, PAGE_SIZE));
  596. kmem_cache_free(zero_cache, pte);
  597. }
  598. pud_clear(pud);
  599. }
  600. BUG_ON(memcmp(pgdir, empty_zero_page, PAGE_SIZE));
  601. kmem_cache_free(zero_cache, pgdir);
  602. out:
  603. spin_unlock(&mm->page_table_lock);
  604. }
  605. int hash_huge_page(struct mm_struct *mm, unsigned long access,
  606. unsigned long ea, unsigned long vsid, int local)
  607. {
  608. pte_t *ptep;
  609. unsigned long va, vpn;
  610. pte_t old_pte, new_pte;
  611. unsigned long hpteflags, prpn;
  612. long slot;
  613. int err = 1;
  614. spin_lock(&mm->page_table_lock);
  615. ptep = huge_pte_offset(mm, ea);
  616. /* Search the Linux page table for a match with va */
  617. va = (vsid << 28) | (ea & 0x0fffffff);
  618. vpn = va >> HPAGE_SHIFT;
  619. /*
  620. * If no pte found or not present, send the problem up to
  621. * do_page_fault
  622. */
  623. if (unlikely(!ptep || pte_none(*ptep)))
  624. goto out;
  625. /* BUG_ON(pte_bad(*ptep)); */
  626. /*
  627. * Check the user's access rights to the page. If access should be
  628. * prevented then send the problem up to do_page_fault.
  629. */
  630. if (unlikely(access & ~pte_val(*ptep)))
  631. goto out;
  632. /*
  633. * At this point, we have a pte (old_pte) which can be used to build
  634. * or update an HPTE. There are 2 cases:
  635. *
  636. * 1. There is a valid (present) pte with no associated HPTE (this is
  637. * the most common case)
  638. * 2. There is a valid (present) pte with an associated HPTE. The
  639. * current values of the pp bits in the HPTE prevent access
  640. * because we are doing software DIRTY bit management and the
  641. * page is currently not DIRTY.
  642. */
  643. old_pte = *ptep;
  644. new_pte = old_pte;
  645. hpteflags = 0x2 | (! (pte_val(new_pte) & _PAGE_RW));
  646. /* _PAGE_EXEC -> HW_NO_EXEC since it's inverted */
  647. hpteflags |= ((pte_val(new_pte) & _PAGE_EXEC) ? 0 : HW_NO_EXEC);
  648. /* Check if pte already has an hpte (case 2) */
  649. if (unlikely(pte_val(old_pte) & _PAGE_HASHPTE)) {
  650. /* There MIGHT be an HPTE for this pte */
  651. unsigned long hash, slot;
  652. hash = hpt_hash(vpn, 1);
  653. if (pte_val(old_pte) & _PAGE_SECONDARY)
  654. hash = ~hash;
  655. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  656. slot += (pte_val(old_pte) & _PAGE_GROUP_IX) >> 12;
  657. if (ppc_md.hpte_updatepp(slot, hpteflags, va, 1, local) == -1)
  658. pte_val(old_pte) &= ~_PAGE_HPTEFLAGS;
  659. }
  660. if (likely(!(pte_val(old_pte) & _PAGE_HASHPTE))) {
  661. unsigned long hash = hpt_hash(vpn, 1);
  662. unsigned long hpte_group;
  663. prpn = pte_pfn(old_pte);
  664. repeat:
  665. hpte_group = ((hash & htab_hash_mask) *
  666. HPTES_PER_GROUP) & ~0x7UL;
  667. /* Update the linux pte with the HPTE slot */
  668. pte_val(new_pte) &= ~_PAGE_HPTEFLAGS;
  669. pte_val(new_pte) |= _PAGE_HASHPTE;
  670. /* Add in WIMG bits */
  671. /* XXX We should store these in the pte */
  672. hpteflags |= _PAGE_COHERENT;
  673. slot = ppc_md.hpte_insert(hpte_group, va, prpn, 0,
  674. hpteflags, 0, 1);
  675. /* Primary is full, try the secondary */
  676. if (unlikely(slot == -1)) {
  677. pte_val(new_pte) |= _PAGE_SECONDARY;
  678. hpte_group = ((~hash & htab_hash_mask) *
  679. HPTES_PER_GROUP) & ~0x7UL;
  680. slot = ppc_md.hpte_insert(hpte_group, va, prpn,
  681. 1, hpteflags, 0, 1);
  682. if (slot == -1) {
  683. if (mftb() & 0x1)
  684. hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
  685. ppc_md.hpte_remove(hpte_group);
  686. goto repeat;
  687. }
  688. }
  689. if (unlikely(slot == -2))
  690. panic("hash_huge_page: pte_insert failed\n");
  691. pte_val(new_pte) |= (slot<<12) & _PAGE_GROUP_IX;
  692. /*
  693. * No need to use ldarx/stdcx here because all who
  694. * might be updating the pte will hold the
  695. * page_table_lock
  696. */
  697. *ptep = new_pte;
  698. }
  699. err = 0;
  700. out:
  701. spin_unlock(&mm->page_table_lock);
  702. return err;
  703. }