hugetlbpage.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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 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(! in_hugepage_area(mm->context, addr));
  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(! in_hugepage_area(mm->context, addr));
  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. static void free_hugepte_range(struct mmu_gather *tlb, hugepd_t *hpdp)
  121. {
  122. pte_t *hugepte = hugepd_page(*hpdp);
  123. hpdp->pd = 0;
  124. tlb->need_flush = 1;
  125. pgtable_free_tlb(tlb, pgtable_free_cache(hugepte, HUGEPTE_CACHE_NUM,
  126. PGF_CACHENUM_MASK));
  127. }
  128. #ifdef CONFIG_PPC_64K_PAGES
  129. static void hugetlb_free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
  130. unsigned long addr, unsigned long end,
  131. unsigned long floor, unsigned long ceiling)
  132. {
  133. pmd_t *pmd;
  134. unsigned long next;
  135. unsigned long start;
  136. start = addr;
  137. pmd = pmd_offset(pud, addr);
  138. do {
  139. next = pmd_addr_end(addr, end);
  140. if (pmd_none(*pmd))
  141. continue;
  142. free_hugepte_range(tlb, (hugepd_t *)pmd);
  143. } while (pmd++, addr = next, addr != end);
  144. start &= PUD_MASK;
  145. if (start < floor)
  146. return;
  147. if (ceiling) {
  148. ceiling &= PUD_MASK;
  149. if (!ceiling)
  150. return;
  151. }
  152. if (end - 1 > ceiling - 1)
  153. return;
  154. pmd = pmd_offset(pud, start);
  155. pud_clear(pud);
  156. pmd_free_tlb(tlb, pmd);
  157. }
  158. #endif
  159. static void hugetlb_free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
  160. unsigned long addr, unsigned long end,
  161. unsigned long floor, unsigned long ceiling)
  162. {
  163. pud_t *pud;
  164. unsigned long next;
  165. unsigned long start;
  166. start = addr;
  167. pud = pud_offset(pgd, addr);
  168. do {
  169. next = pud_addr_end(addr, end);
  170. #ifdef CONFIG_PPC_64K_PAGES
  171. if (pud_none_or_clear_bad(pud))
  172. continue;
  173. hugetlb_free_pmd_range(tlb, pud, addr, next, floor, ceiling);
  174. #else
  175. if (pud_none(*pud))
  176. continue;
  177. free_hugepte_range(tlb, (hugepd_t *)pud);
  178. #endif
  179. } while (pud++, addr = next, addr != end);
  180. start &= PGDIR_MASK;
  181. if (start < floor)
  182. return;
  183. if (ceiling) {
  184. ceiling &= PGDIR_MASK;
  185. if (!ceiling)
  186. return;
  187. }
  188. if (end - 1 > ceiling - 1)
  189. return;
  190. pud = pud_offset(pgd, start);
  191. pgd_clear(pgd);
  192. pud_free_tlb(tlb, pud);
  193. }
  194. /*
  195. * This function frees user-level page tables of a process.
  196. *
  197. * Must be called with pagetable lock held.
  198. */
  199. void hugetlb_free_pgd_range(struct mmu_gather **tlb,
  200. unsigned long addr, unsigned long end,
  201. unsigned long floor, unsigned long ceiling)
  202. {
  203. pgd_t *pgd;
  204. unsigned long next;
  205. unsigned long start;
  206. /*
  207. * Comments below take from the normal free_pgd_range(). They
  208. * apply here too. The tests against HUGEPD_MASK below are
  209. * essential, because we *don't* test for this at the bottom
  210. * level. Without them we'll attempt to free a hugepte table
  211. * when we unmap just part of it, even if there are other
  212. * active mappings using it.
  213. *
  214. * The next few lines have given us lots of grief...
  215. *
  216. * Why are we testing HUGEPD* at this top level? Because
  217. * often there will be no work to do at all, and we'd prefer
  218. * not to go all the way down to the bottom just to discover
  219. * that.
  220. *
  221. * Why all these "- 1"s? Because 0 represents both the bottom
  222. * of the address space and the top of it (using -1 for the
  223. * top wouldn't help much: the masks would do the wrong thing).
  224. * The rule is that addr 0 and floor 0 refer to the bottom of
  225. * the address space, but end 0 and ceiling 0 refer to the top
  226. * Comparisons need to use "end - 1" and "ceiling - 1" (though
  227. * that end 0 case should be mythical).
  228. *
  229. * Wherever addr is brought up or ceiling brought down, we
  230. * must be careful to reject "the opposite 0" before it
  231. * confuses the subsequent tests. But what about where end is
  232. * brought down by HUGEPD_SIZE below? no, end can't go down to
  233. * 0 there.
  234. *
  235. * Whereas we round start (addr) and ceiling down, by different
  236. * masks at different levels, in order to test whether a table
  237. * now has no other vmas using it, so can be freed, we don't
  238. * bother to round floor or end up - the tests don't need that.
  239. */
  240. addr &= HUGEPD_MASK;
  241. if (addr < floor) {
  242. addr += HUGEPD_SIZE;
  243. if (!addr)
  244. return;
  245. }
  246. if (ceiling) {
  247. ceiling &= HUGEPD_MASK;
  248. if (!ceiling)
  249. return;
  250. }
  251. if (end - 1 > ceiling - 1)
  252. end -= HUGEPD_SIZE;
  253. if (addr > end - 1)
  254. return;
  255. start = addr;
  256. pgd = pgd_offset((*tlb)->mm, addr);
  257. do {
  258. BUG_ON(! in_hugepage_area((*tlb)->mm->context, addr));
  259. next = pgd_addr_end(addr, end);
  260. if (pgd_none_or_clear_bad(pgd))
  261. continue;
  262. hugetlb_free_pud_range(*tlb, pgd, addr, next, floor, ceiling);
  263. } while (pgd++, addr = next, addr != end);
  264. }
  265. void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
  266. pte_t *ptep, pte_t pte)
  267. {
  268. if (pte_present(*ptep)) {
  269. /* We open-code pte_clear because we need to pass the right
  270. * argument to hpte_update (huge / !huge)
  271. */
  272. unsigned long old = pte_update(ptep, ~0UL);
  273. if (old & _PAGE_HASHPTE)
  274. hpte_update(mm, addr & HPAGE_MASK, ptep, old, 1);
  275. flush_tlb_pending();
  276. }
  277. *ptep = __pte(pte_val(pte) & ~_PAGE_HPTEFLAGS);
  278. }
  279. pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
  280. pte_t *ptep)
  281. {
  282. unsigned long old = pte_update(ptep, ~0UL);
  283. if (old & _PAGE_HASHPTE)
  284. hpte_update(mm, addr & HPAGE_MASK, ptep, old, 1);
  285. *ptep = __pte(0);
  286. return __pte(old);
  287. }
  288. struct slb_flush_info {
  289. struct mm_struct *mm;
  290. u16 newareas;
  291. };
  292. static void flush_low_segments(void *parm)
  293. {
  294. struct slb_flush_info *fi = parm;
  295. unsigned long i;
  296. BUILD_BUG_ON((sizeof(fi->newareas)*8) != NUM_LOW_AREAS);
  297. if (current->active_mm != fi->mm)
  298. return;
  299. /* Only need to do anything if this CPU is working in the same
  300. * mm as the one which has changed */
  301. /* update the paca copy of the context struct */
  302. get_paca()->context = current->active_mm->context;
  303. asm volatile("isync" : : : "memory");
  304. for (i = 0; i < NUM_LOW_AREAS; i++) {
  305. if (! (fi->newareas & (1U << i)))
  306. continue;
  307. asm volatile("slbie %0"
  308. : : "r" ((i << SID_SHIFT) | SLBIE_C));
  309. }
  310. asm volatile("isync" : : : "memory");
  311. }
  312. static void flush_high_segments(void *parm)
  313. {
  314. struct slb_flush_info *fi = parm;
  315. unsigned long i, j;
  316. BUILD_BUG_ON((sizeof(fi->newareas)*8) != NUM_HIGH_AREAS);
  317. if (current->active_mm != fi->mm)
  318. return;
  319. /* Only need to do anything if this CPU is working in the same
  320. * mm as the one which has changed */
  321. /* update the paca copy of the context struct */
  322. get_paca()->context = current->active_mm->context;
  323. asm volatile("isync" : : : "memory");
  324. for (i = 0; i < NUM_HIGH_AREAS; i++) {
  325. if (! (fi->newareas & (1U << i)))
  326. continue;
  327. for (j = 0; j < (1UL << (HTLB_AREA_SHIFT-SID_SHIFT)); j++)
  328. asm volatile("slbie %0"
  329. :: "r" (((i << HTLB_AREA_SHIFT)
  330. + (j << SID_SHIFT)) | SLBIE_C));
  331. }
  332. asm volatile("isync" : : : "memory");
  333. }
  334. static int prepare_low_area_for_htlb(struct mm_struct *mm, unsigned long area)
  335. {
  336. unsigned long start = area << SID_SHIFT;
  337. unsigned long end = (area+1) << SID_SHIFT;
  338. struct vm_area_struct *vma;
  339. BUG_ON(area >= NUM_LOW_AREAS);
  340. /* Check no VMAs are in the region */
  341. vma = find_vma(mm, start);
  342. if (vma && (vma->vm_start < end))
  343. return -EBUSY;
  344. return 0;
  345. }
  346. static int prepare_high_area_for_htlb(struct mm_struct *mm, unsigned long area)
  347. {
  348. unsigned long start = area << HTLB_AREA_SHIFT;
  349. unsigned long end = (area+1) << HTLB_AREA_SHIFT;
  350. struct vm_area_struct *vma;
  351. BUG_ON(area >= NUM_HIGH_AREAS);
  352. /* Hack, so that each addresses is controlled by exactly one
  353. * of the high or low area bitmaps, the first high area starts
  354. * at 4GB, not 0 */
  355. if (start == 0)
  356. start = 0x100000000UL;
  357. /* Check no VMAs are in the region */
  358. vma = find_vma(mm, start);
  359. if (vma && (vma->vm_start < end))
  360. return -EBUSY;
  361. return 0;
  362. }
  363. static int open_low_hpage_areas(struct mm_struct *mm, u16 newareas)
  364. {
  365. unsigned long i;
  366. struct slb_flush_info fi;
  367. BUILD_BUG_ON((sizeof(newareas)*8) != NUM_LOW_AREAS);
  368. BUILD_BUG_ON((sizeof(mm->context.low_htlb_areas)*8) != NUM_LOW_AREAS);
  369. newareas &= ~(mm->context.low_htlb_areas);
  370. if (! newareas)
  371. return 0; /* The segments we want are already open */
  372. for (i = 0; i < NUM_LOW_AREAS; i++)
  373. if ((1 << i) & newareas)
  374. if (prepare_low_area_for_htlb(mm, i) != 0)
  375. return -EBUSY;
  376. mm->context.low_htlb_areas |= newareas;
  377. /* the context change must make it to memory before the flush,
  378. * so that further SLB misses do the right thing. */
  379. mb();
  380. fi.mm = mm;
  381. fi.newareas = newareas;
  382. on_each_cpu(flush_low_segments, &fi, 0, 1);
  383. return 0;
  384. }
  385. static int open_high_hpage_areas(struct mm_struct *mm, u16 newareas)
  386. {
  387. struct slb_flush_info fi;
  388. unsigned long i;
  389. BUILD_BUG_ON((sizeof(newareas)*8) != NUM_HIGH_AREAS);
  390. BUILD_BUG_ON((sizeof(mm->context.high_htlb_areas)*8)
  391. != NUM_HIGH_AREAS);
  392. newareas &= ~(mm->context.high_htlb_areas);
  393. if (! newareas)
  394. return 0; /* The areas we want are already open */
  395. for (i = 0; i < NUM_HIGH_AREAS; i++)
  396. if ((1 << i) & newareas)
  397. if (prepare_high_area_for_htlb(mm, i) != 0)
  398. return -EBUSY;
  399. mm->context.high_htlb_areas |= newareas;
  400. /* the context change must make it to memory before the flush,
  401. * so that further SLB misses do the right thing. */
  402. mb();
  403. fi.mm = mm;
  404. fi.newareas = newareas;
  405. on_each_cpu(flush_high_segments, &fi, 0, 1);
  406. return 0;
  407. }
  408. int prepare_hugepage_range(unsigned long addr, unsigned long len, pgoff_t pgoff)
  409. {
  410. int err = 0;
  411. if (pgoff & (~HPAGE_MASK >> PAGE_SHIFT))
  412. return -EINVAL;
  413. if (len & ~HPAGE_MASK)
  414. return -EINVAL;
  415. if (addr & ~HPAGE_MASK)
  416. return -EINVAL;
  417. if (addr < 0x100000000UL)
  418. err = open_low_hpage_areas(current->mm,
  419. LOW_ESID_MASK(addr, len));
  420. if ((addr + len) > 0x100000000UL)
  421. err = open_high_hpage_areas(current->mm,
  422. HTLB_AREA_MASK(addr, len));
  423. if (err) {
  424. printk(KERN_DEBUG "prepare_hugepage_range(%lx, %lx)"
  425. " failed (lowmask: 0x%04hx, highmask: 0x%04hx)\n",
  426. addr, len,
  427. LOW_ESID_MASK(addr, len), HTLB_AREA_MASK(addr, len));
  428. return err;
  429. }
  430. return 0;
  431. }
  432. struct page *
  433. follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
  434. {
  435. pte_t *ptep;
  436. struct page *page;
  437. if (! in_hugepage_area(mm->context, address))
  438. return ERR_PTR(-EINVAL);
  439. ptep = huge_pte_offset(mm, address);
  440. page = pte_page(*ptep);
  441. if (page)
  442. page += (address % HPAGE_SIZE) / PAGE_SIZE;
  443. return page;
  444. }
  445. int pmd_huge(pmd_t pmd)
  446. {
  447. return 0;
  448. }
  449. struct page *
  450. follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  451. pmd_t *pmd, int write)
  452. {
  453. BUG();
  454. return NULL;
  455. }
  456. /* Because we have an exclusive hugepage region which lies within the
  457. * normal user address space, we have to take special measures to make
  458. * non-huge mmap()s evade the hugepage reserved regions. */
  459. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
  460. unsigned long len, unsigned long pgoff,
  461. unsigned long flags)
  462. {
  463. struct mm_struct *mm = current->mm;
  464. struct vm_area_struct *vma;
  465. unsigned long start_addr;
  466. if (len > TASK_SIZE)
  467. return -ENOMEM;
  468. if (addr) {
  469. addr = PAGE_ALIGN(addr);
  470. vma = find_vma(mm, addr);
  471. if (((TASK_SIZE - len) >= addr)
  472. && (!vma || (addr+len) <= vma->vm_start)
  473. && !is_hugepage_only_range(mm, addr,len))
  474. return addr;
  475. }
  476. if (len > mm->cached_hole_size) {
  477. start_addr = addr = mm->free_area_cache;
  478. } else {
  479. start_addr = addr = TASK_UNMAPPED_BASE;
  480. mm->cached_hole_size = 0;
  481. }
  482. full_search:
  483. vma = find_vma(mm, addr);
  484. while (TASK_SIZE - len >= addr) {
  485. BUG_ON(vma && (addr >= vma->vm_end));
  486. if (touches_hugepage_low_range(mm, addr, len)) {
  487. addr = ALIGN(addr+1, 1<<SID_SHIFT);
  488. vma = find_vma(mm, addr);
  489. continue;
  490. }
  491. if (touches_hugepage_high_range(mm, addr, len)) {
  492. addr = ALIGN(addr+1, 1UL<<HTLB_AREA_SHIFT);
  493. vma = find_vma(mm, addr);
  494. continue;
  495. }
  496. if (!vma || addr + len <= vma->vm_start) {
  497. /*
  498. * Remember the place where we stopped the search:
  499. */
  500. mm->free_area_cache = addr + len;
  501. return addr;
  502. }
  503. if (addr + mm->cached_hole_size < vma->vm_start)
  504. mm->cached_hole_size = vma->vm_start - addr;
  505. addr = vma->vm_end;
  506. vma = vma->vm_next;
  507. }
  508. /* Make sure we didn't miss any holes */
  509. if (start_addr != TASK_UNMAPPED_BASE) {
  510. start_addr = addr = TASK_UNMAPPED_BASE;
  511. mm->cached_hole_size = 0;
  512. goto full_search;
  513. }
  514. return -ENOMEM;
  515. }
  516. /*
  517. * This mmap-allocator allocates new areas top-down from below the
  518. * stack's low limit (the base):
  519. *
  520. * Because we have an exclusive hugepage region which lies within the
  521. * normal user address space, we have to take special measures to make
  522. * non-huge mmap()s evade the hugepage reserved regions.
  523. */
  524. unsigned long
  525. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  526. const unsigned long len, const unsigned long pgoff,
  527. const unsigned long flags)
  528. {
  529. struct vm_area_struct *vma, *prev_vma;
  530. struct mm_struct *mm = current->mm;
  531. unsigned long base = mm->mmap_base, addr = addr0;
  532. unsigned long largest_hole = mm->cached_hole_size;
  533. int first_time = 1;
  534. /* requested length too big for entire address space */
  535. if (len > TASK_SIZE)
  536. return -ENOMEM;
  537. /* dont allow allocations above current base */
  538. if (mm->free_area_cache > base)
  539. mm->free_area_cache = base;
  540. /* requesting a specific address */
  541. if (addr) {
  542. addr = PAGE_ALIGN(addr);
  543. vma = find_vma(mm, addr);
  544. if (TASK_SIZE - len >= addr &&
  545. (!vma || addr + len <= vma->vm_start)
  546. && !is_hugepage_only_range(mm, addr,len))
  547. return addr;
  548. }
  549. if (len <= largest_hole) {
  550. largest_hole = 0;
  551. mm->free_area_cache = base;
  552. }
  553. try_again:
  554. /* make sure it can fit in the remaining address space */
  555. if (mm->free_area_cache < len)
  556. goto fail;
  557. /* either no address requested or cant fit in requested address hole */
  558. addr = (mm->free_area_cache - len) & PAGE_MASK;
  559. do {
  560. hugepage_recheck:
  561. if (touches_hugepage_low_range(mm, addr, len)) {
  562. addr = (addr & ((~0) << SID_SHIFT)) - len;
  563. goto hugepage_recheck;
  564. } else if (touches_hugepage_high_range(mm, addr, len)) {
  565. addr = (addr & ((~0UL) << HTLB_AREA_SHIFT)) - len;
  566. goto hugepage_recheck;
  567. }
  568. /*
  569. * Lookup failure means no vma is above this address,
  570. * i.e. return with success:
  571. */
  572. if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
  573. return addr;
  574. /*
  575. * new region fits between prev_vma->vm_end and
  576. * vma->vm_start, use it:
  577. */
  578. if (addr+len <= vma->vm_start &&
  579. (!prev_vma || (addr >= prev_vma->vm_end))) {
  580. /* remember the address as a hint for next time */
  581. mm->cached_hole_size = largest_hole;
  582. return (mm->free_area_cache = addr);
  583. } else {
  584. /* pull free_area_cache down to the first hole */
  585. if (mm->free_area_cache == vma->vm_end) {
  586. mm->free_area_cache = vma->vm_start;
  587. mm->cached_hole_size = largest_hole;
  588. }
  589. }
  590. /* remember the largest hole we saw so far */
  591. if (addr + largest_hole < vma->vm_start)
  592. largest_hole = vma->vm_start - addr;
  593. /* try just below the current vma->vm_start */
  594. addr = vma->vm_start-len;
  595. } while (len <= vma->vm_start);
  596. fail:
  597. /*
  598. * if hint left us with no space for the requested
  599. * mapping then try again:
  600. */
  601. if (first_time) {
  602. mm->free_area_cache = base;
  603. largest_hole = 0;
  604. first_time = 0;
  605. goto try_again;
  606. }
  607. /*
  608. * A failed mmap() very likely causes application failure,
  609. * so fall back to the bottom-up function here. This scenario
  610. * can happen with large stack limits and large mmap()
  611. * allocations.
  612. */
  613. mm->free_area_cache = TASK_UNMAPPED_BASE;
  614. mm->cached_hole_size = ~0UL;
  615. addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
  616. /*
  617. * Restore the topdown base:
  618. */
  619. mm->free_area_cache = base;
  620. mm->cached_hole_size = ~0UL;
  621. return addr;
  622. }
  623. static int htlb_check_hinted_area(unsigned long addr, unsigned long len)
  624. {
  625. struct vm_area_struct *vma;
  626. vma = find_vma(current->mm, addr);
  627. if (!vma || ((addr + len) <= vma->vm_start))
  628. return 0;
  629. return -ENOMEM;
  630. }
  631. static unsigned long htlb_get_low_area(unsigned long len, u16 segmask)
  632. {
  633. unsigned long addr = 0;
  634. struct vm_area_struct *vma;
  635. vma = find_vma(current->mm, addr);
  636. while (addr + len <= 0x100000000UL) {
  637. BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
  638. if (! __within_hugepage_low_range(addr, len, segmask)) {
  639. addr = ALIGN(addr+1, 1<<SID_SHIFT);
  640. vma = find_vma(current->mm, addr);
  641. continue;
  642. }
  643. if (!vma || (addr + len) <= vma->vm_start)
  644. return addr;
  645. addr = ALIGN(vma->vm_end, HPAGE_SIZE);
  646. /* Depending on segmask this might not be a confirmed
  647. * hugepage region, so the ALIGN could have skipped
  648. * some VMAs */
  649. vma = find_vma(current->mm, addr);
  650. }
  651. return -ENOMEM;
  652. }
  653. static unsigned long htlb_get_high_area(unsigned long len, u16 areamask)
  654. {
  655. unsigned long addr = 0x100000000UL;
  656. struct vm_area_struct *vma;
  657. vma = find_vma(current->mm, addr);
  658. while (addr + len <= TASK_SIZE_USER64) {
  659. BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
  660. if (! __within_hugepage_high_range(addr, len, areamask)) {
  661. addr = ALIGN(addr+1, 1UL<<HTLB_AREA_SHIFT);
  662. vma = find_vma(current->mm, addr);
  663. continue;
  664. }
  665. if (!vma || (addr + len) <= vma->vm_start)
  666. return addr;
  667. addr = ALIGN(vma->vm_end, HPAGE_SIZE);
  668. /* Depending on segmask this might not be a confirmed
  669. * hugepage region, so the ALIGN could have skipped
  670. * some VMAs */
  671. vma = find_vma(current->mm, addr);
  672. }
  673. return -ENOMEM;
  674. }
  675. unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  676. unsigned long len, unsigned long pgoff,
  677. unsigned long flags)
  678. {
  679. int lastshift;
  680. u16 areamask, curareas;
  681. if (HPAGE_SHIFT == 0)
  682. return -EINVAL;
  683. if (len & ~HPAGE_MASK)
  684. return -EINVAL;
  685. if (!cpu_has_feature(CPU_FTR_16M_PAGE))
  686. return -EINVAL;
  687. /* Paranoia, caller should have dealt with this */
  688. BUG_ON((addr + len) < addr);
  689. if (test_thread_flag(TIF_32BIT)) {
  690. /* Paranoia, caller should have dealt with this */
  691. BUG_ON((addr + len) > 0x100000000UL);
  692. curareas = current->mm->context.low_htlb_areas;
  693. /* First see if we can use the hint address */
  694. if (addr && (htlb_check_hinted_area(addr, len) == 0)) {
  695. areamask = LOW_ESID_MASK(addr, len);
  696. if (open_low_hpage_areas(current->mm, areamask) == 0)
  697. return addr;
  698. }
  699. /* Next see if we can map in the existing low areas */
  700. addr = htlb_get_low_area(len, curareas);
  701. if (addr != -ENOMEM)
  702. return addr;
  703. /* Finally go looking for areas to open */
  704. lastshift = 0;
  705. for (areamask = LOW_ESID_MASK(0x100000000UL-len, len);
  706. ! lastshift; areamask >>=1) {
  707. if (areamask & 1)
  708. lastshift = 1;
  709. addr = htlb_get_low_area(len, curareas | areamask);
  710. if ((addr != -ENOMEM)
  711. && open_low_hpage_areas(current->mm, areamask) == 0)
  712. return addr;
  713. }
  714. } else {
  715. curareas = current->mm->context.high_htlb_areas;
  716. /* First see if we can use the hint address */
  717. /* We discourage 64-bit processes from doing hugepage
  718. * mappings below 4GB (must use MAP_FIXED) */
  719. if ((addr >= 0x100000000UL)
  720. && (htlb_check_hinted_area(addr, len) == 0)) {
  721. areamask = HTLB_AREA_MASK(addr, len);
  722. if (open_high_hpage_areas(current->mm, areamask) == 0)
  723. return addr;
  724. }
  725. /* Next see if we can map in the existing high areas */
  726. addr = htlb_get_high_area(len, curareas);
  727. if (addr != -ENOMEM)
  728. return addr;
  729. /* Finally go looking for areas to open */
  730. lastshift = 0;
  731. for (areamask = HTLB_AREA_MASK(TASK_SIZE_USER64-len, len);
  732. ! lastshift; areamask >>=1) {
  733. if (areamask & 1)
  734. lastshift = 1;
  735. addr = htlb_get_high_area(len, curareas | areamask);
  736. if ((addr != -ENOMEM)
  737. && open_high_hpage_areas(current->mm, areamask) == 0)
  738. return addr;
  739. }
  740. }
  741. printk(KERN_DEBUG "hugetlb_get_unmapped_area() unable to open"
  742. " enough areas\n");
  743. return -ENOMEM;
  744. }
  745. /*
  746. * Called by asm hashtable.S for doing lazy icache flush
  747. */
  748. static unsigned int hash_huge_page_do_lazy_icache(unsigned long rflags,
  749. pte_t pte, int trap)
  750. {
  751. struct page *page;
  752. int i;
  753. if (!pfn_valid(pte_pfn(pte)))
  754. return rflags;
  755. page = pte_page(pte);
  756. /* page is dirty */
  757. if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
  758. if (trap == 0x400) {
  759. for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++)
  760. __flush_dcache_icache(page_address(page+i));
  761. set_bit(PG_arch_1, &page->flags);
  762. } else {
  763. rflags |= HPTE_R_N;
  764. }
  765. }
  766. return rflags;
  767. }
  768. int hash_huge_page(struct mm_struct *mm, unsigned long access,
  769. unsigned long ea, unsigned long vsid, int local,
  770. unsigned long trap)
  771. {
  772. pte_t *ptep;
  773. unsigned long old_pte, new_pte;
  774. unsigned long va, rflags, pa;
  775. long slot;
  776. int err = 1;
  777. ptep = huge_pte_offset(mm, ea);
  778. /* Search the Linux page table for a match with va */
  779. va = (vsid << 28) | (ea & 0x0fffffff);
  780. /*
  781. * If no pte found or not present, send the problem up to
  782. * do_page_fault
  783. */
  784. if (unlikely(!ptep || pte_none(*ptep)))
  785. goto out;
  786. /*
  787. * Check the user's access rights to the page. If access should be
  788. * prevented then send the problem up to do_page_fault.
  789. */
  790. if (unlikely(access & ~pte_val(*ptep)))
  791. goto out;
  792. /*
  793. * At this point, we have a pte (old_pte) which can be used to build
  794. * or update an HPTE. There are 2 cases:
  795. *
  796. * 1. There is a valid (present) pte with no associated HPTE (this is
  797. * the most common case)
  798. * 2. There is a valid (present) pte with an associated HPTE. The
  799. * current values of the pp bits in the HPTE prevent access
  800. * because we are doing software DIRTY bit management and the
  801. * page is currently not DIRTY.
  802. */
  803. do {
  804. old_pte = pte_val(*ptep);
  805. if (old_pte & _PAGE_BUSY)
  806. goto out;
  807. new_pte = old_pte | _PAGE_BUSY |
  808. _PAGE_ACCESSED | _PAGE_HASHPTE;
  809. } while(old_pte != __cmpxchg_u64((unsigned long *)ptep,
  810. old_pte, new_pte));
  811. rflags = 0x2 | (!(new_pte & _PAGE_RW));
  812. /* _PAGE_EXEC -> HW_NO_EXEC since it's inverted */
  813. rflags |= ((new_pte & _PAGE_EXEC) ? 0 : HPTE_R_N);
  814. if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
  815. /* No CPU has hugepages but lacks no execute, so we
  816. * don't need to worry about that case */
  817. rflags = hash_huge_page_do_lazy_icache(rflags, __pte(old_pte),
  818. trap);
  819. /* Check if pte already has an hpte (case 2) */
  820. if (unlikely(old_pte & _PAGE_HASHPTE)) {
  821. /* There MIGHT be an HPTE for this pte */
  822. unsigned long hash, slot;
  823. hash = hpt_hash(va, HPAGE_SHIFT);
  824. if (old_pte & _PAGE_F_SECOND)
  825. hash = ~hash;
  826. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  827. slot += (old_pte & _PAGE_F_GIX) >> 12;
  828. if (ppc_md.hpte_updatepp(slot, rflags, va, mmu_huge_psize,
  829. local) == -1)
  830. old_pte &= ~_PAGE_HPTEFLAGS;
  831. }
  832. if (likely(!(old_pte & _PAGE_HASHPTE))) {
  833. unsigned long hash = hpt_hash(va, HPAGE_SHIFT);
  834. unsigned long hpte_group;
  835. pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
  836. repeat:
  837. hpte_group = ((hash & htab_hash_mask) *
  838. HPTES_PER_GROUP) & ~0x7UL;
  839. /* clear HPTE slot informations in new PTE */
  840. new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | _PAGE_HASHPTE;
  841. /* Add in WIMG bits */
  842. /* XXX We should store these in the pte */
  843. /* --BenH: I think they are ... */
  844. rflags |= _PAGE_COHERENT;
  845. /* Insert into the hash table, primary slot */
  846. slot = ppc_md.hpte_insert(hpte_group, va, pa, rflags, 0,
  847. mmu_huge_psize);
  848. /* Primary is full, try the secondary */
  849. if (unlikely(slot == -1)) {
  850. new_pte |= _PAGE_F_SECOND;
  851. hpte_group = ((~hash & htab_hash_mask) *
  852. HPTES_PER_GROUP) & ~0x7UL;
  853. slot = ppc_md.hpte_insert(hpte_group, va, pa, rflags,
  854. HPTE_V_SECONDARY,
  855. mmu_huge_psize);
  856. if (slot == -1) {
  857. if (mftb() & 0x1)
  858. hpte_group = ((hash & htab_hash_mask) *
  859. HPTES_PER_GROUP)&~0x7UL;
  860. ppc_md.hpte_remove(hpte_group);
  861. goto repeat;
  862. }
  863. }
  864. if (unlikely(slot == -2))
  865. panic("hash_huge_page: pte_insert failed\n");
  866. new_pte |= (slot << 12) & _PAGE_F_GIX;
  867. }
  868. /*
  869. * No need to use ldarx/stdcx here
  870. */
  871. *ptep = __pte(new_pte & ~_PAGE_BUSY);
  872. err = 0;
  873. out:
  874. return err;
  875. }
  876. static void zero_ctor(void *addr, kmem_cache_t *cache, unsigned long flags)
  877. {
  878. memset(addr, 0, kmem_cache_size(cache));
  879. }
  880. static int __init hugetlbpage_init(void)
  881. {
  882. if (!cpu_has_feature(CPU_FTR_16M_PAGE))
  883. return -ENODEV;
  884. huge_pgtable_cache = kmem_cache_create("hugepte_cache",
  885. HUGEPTE_TABLE_SIZE,
  886. HUGEPTE_TABLE_SIZE,
  887. SLAB_HWCACHE_ALIGN |
  888. SLAB_MUST_HWCACHE_ALIGN,
  889. zero_ctor, NULL);
  890. if (! huge_pgtable_cache)
  891. panic("hugetlbpage_init(): could not create hugepte cache\n");
  892. return 0;
  893. }
  894. module_init(hugetlbpage_init);