hugetlbpage.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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. 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(! in_hugepage_area((*tlb)->mm->context, addr));
  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_update (huge / !huge)
  275. */
  276. unsigned long old = pte_update(ptep, ~0UL);
  277. if (old & _PAGE_HASHPTE)
  278. hpte_update(mm, addr & HPAGE_MASK, ptep, old, 1);
  279. flush_tlb_pending();
  280. }
  281. *ptep = __pte(pte_val(pte) & ~_PAGE_HPTEFLAGS);
  282. }
  283. pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
  284. pte_t *ptep)
  285. {
  286. unsigned long old = pte_update(ptep, ~0UL);
  287. if (old & _PAGE_HASHPTE)
  288. hpte_update(mm, addr & HPAGE_MASK, ptep, old, 1);
  289. *ptep = __pte(0);
  290. return __pte(old);
  291. }
  292. struct slb_flush_info {
  293. struct mm_struct *mm;
  294. u16 newareas;
  295. };
  296. static void flush_low_segments(void *parm)
  297. {
  298. struct slb_flush_info *fi = parm;
  299. unsigned long i;
  300. BUILD_BUG_ON((sizeof(fi->newareas)*8) != NUM_LOW_AREAS);
  301. if (current->active_mm != fi->mm)
  302. return;
  303. /* Only need to do anything if this CPU is working in the same
  304. * mm as the one which has changed */
  305. /* update the paca copy of the context struct */
  306. get_paca()->context = current->active_mm->context;
  307. asm volatile("isync" : : : "memory");
  308. for (i = 0; i < NUM_LOW_AREAS; i++) {
  309. if (! (fi->newareas & (1U << i)))
  310. continue;
  311. asm volatile("slbie %0"
  312. : : "r" ((i << SID_SHIFT) | SLBIE_C));
  313. }
  314. asm volatile("isync" : : : "memory");
  315. }
  316. static void flush_high_segments(void *parm)
  317. {
  318. struct slb_flush_info *fi = parm;
  319. unsigned long i, j;
  320. BUILD_BUG_ON((sizeof(fi->newareas)*8) != NUM_HIGH_AREAS);
  321. if (current->active_mm != fi->mm)
  322. return;
  323. /* Only need to do anything if this CPU is working in the same
  324. * mm as the one which has changed */
  325. /* update the paca copy of the context struct */
  326. get_paca()->context = current->active_mm->context;
  327. asm volatile("isync" : : : "memory");
  328. for (i = 0; i < NUM_HIGH_AREAS; i++) {
  329. if (! (fi->newareas & (1U << i)))
  330. continue;
  331. for (j = 0; j < (1UL << (HTLB_AREA_SHIFT-SID_SHIFT)); j++)
  332. asm volatile("slbie %0"
  333. :: "r" (((i << HTLB_AREA_SHIFT)
  334. + (j << SID_SHIFT)) | SLBIE_C));
  335. }
  336. asm volatile("isync" : : : "memory");
  337. }
  338. static int prepare_low_area_for_htlb(struct mm_struct *mm, unsigned long area)
  339. {
  340. unsigned long start = area << SID_SHIFT;
  341. unsigned long end = (area+1) << SID_SHIFT;
  342. struct vm_area_struct *vma;
  343. BUG_ON(area >= NUM_LOW_AREAS);
  344. /* Check no VMAs are in the region */
  345. vma = find_vma(mm, start);
  346. if (vma && (vma->vm_start < end))
  347. return -EBUSY;
  348. return 0;
  349. }
  350. static int prepare_high_area_for_htlb(struct mm_struct *mm, unsigned long area)
  351. {
  352. unsigned long start = area << HTLB_AREA_SHIFT;
  353. unsigned long end = (area+1) << HTLB_AREA_SHIFT;
  354. struct vm_area_struct *vma;
  355. BUG_ON(area >= NUM_HIGH_AREAS);
  356. /* Hack, so that each addresses is controlled by exactly one
  357. * of the high or low area bitmaps, the first high area starts
  358. * at 4GB, not 0 */
  359. if (start == 0)
  360. start = 0x100000000UL;
  361. /* Check no VMAs are in the region */
  362. vma = find_vma(mm, start);
  363. if (vma && (vma->vm_start < end))
  364. return -EBUSY;
  365. return 0;
  366. }
  367. static int open_low_hpage_areas(struct mm_struct *mm, u16 newareas)
  368. {
  369. unsigned long i;
  370. struct slb_flush_info fi;
  371. BUILD_BUG_ON((sizeof(newareas)*8) != NUM_LOW_AREAS);
  372. BUILD_BUG_ON((sizeof(mm->context.low_htlb_areas)*8) != NUM_LOW_AREAS);
  373. newareas &= ~(mm->context.low_htlb_areas);
  374. if (! newareas)
  375. return 0; /* The segments we want are already open */
  376. for (i = 0; i < NUM_LOW_AREAS; i++)
  377. if ((1 << i) & newareas)
  378. if (prepare_low_area_for_htlb(mm, i) != 0)
  379. return -EBUSY;
  380. mm->context.low_htlb_areas |= newareas;
  381. /* the context change must make it to memory before the flush,
  382. * so that further SLB misses do the right thing. */
  383. mb();
  384. fi.mm = mm;
  385. fi.newareas = newareas;
  386. on_each_cpu(flush_low_segments, &fi, 0, 1);
  387. return 0;
  388. }
  389. static int open_high_hpage_areas(struct mm_struct *mm, u16 newareas)
  390. {
  391. struct slb_flush_info fi;
  392. unsigned long i;
  393. BUILD_BUG_ON((sizeof(newareas)*8) != NUM_HIGH_AREAS);
  394. BUILD_BUG_ON((sizeof(mm->context.high_htlb_areas)*8)
  395. != NUM_HIGH_AREAS);
  396. newareas &= ~(mm->context.high_htlb_areas);
  397. if (! newareas)
  398. return 0; /* The areas we want are already open */
  399. for (i = 0; i < NUM_HIGH_AREAS; i++)
  400. if ((1 << i) & newareas)
  401. if (prepare_high_area_for_htlb(mm, i) != 0)
  402. return -EBUSY;
  403. mm->context.high_htlb_areas |= newareas;
  404. /* the context change must make it to memory before the flush,
  405. * so that further SLB misses do the right thing. */
  406. mb();
  407. fi.mm = mm;
  408. fi.newareas = newareas;
  409. on_each_cpu(flush_high_segments, &fi, 0, 1);
  410. return 0;
  411. }
  412. int prepare_hugepage_range(unsigned long addr, unsigned long len, pgoff_t pgoff)
  413. {
  414. int err = 0;
  415. if (pgoff & (~HPAGE_MASK >> PAGE_SHIFT))
  416. return -EINVAL;
  417. if (len & ~HPAGE_MASK)
  418. return -EINVAL;
  419. if (addr & ~HPAGE_MASK)
  420. return -EINVAL;
  421. if (addr < 0x100000000UL)
  422. err = open_low_hpage_areas(current->mm,
  423. LOW_ESID_MASK(addr, len));
  424. if ((addr + len) > 0x100000000UL)
  425. err = open_high_hpage_areas(current->mm,
  426. HTLB_AREA_MASK(addr, len));
  427. if (err) {
  428. printk(KERN_DEBUG "prepare_hugepage_range(%lx, %lx)"
  429. " failed (lowmask: 0x%04hx, highmask: 0x%04hx)\n",
  430. addr, len,
  431. LOW_ESID_MASK(addr, len), HTLB_AREA_MASK(addr, len));
  432. return err;
  433. }
  434. return 0;
  435. }
  436. struct page *
  437. follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
  438. {
  439. pte_t *ptep;
  440. struct page *page;
  441. if (! in_hugepage_area(mm->context, address))
  442. return ERR_PTR(-EINVAL);
  443. ptep = huge_pte_offset(mm, address);
  444. page = pte_page(*ptep);
  445. if (page)
  446. page += (address % HPAGE_SIZE) / PAGE_SIZE;
  447. return page;
  448. }
  449. int pmd_huge(pmd_t pmd)
  450. {
  451. return 0;
  452. }
  453. struct page *
  454. follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  455. pmd_t *pmd, int write)
  456. {
  457. BUG();
  458. return NULL;
  459. }
  460. /* Because we have an exclusive hugepage region which lies within the
  461. * normal user address space, we have to take special measures to make
  462. * non-huge mmap()s evade the hugepage reserved regions. */
  463. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
  464. unsigned long len, unsigned long pgoff,
  465. unsigned long flags)
  466. {
  467. struct mm_struct *mm = current->mm;
  468. struct vm_area_struct *vma;
  469. unsigned long start_addr;
  470. if (len > TASK_SIZE)
  471. return -ENOMEM;
  472. if (addr) {
  473. addr = PAGE_ALIGN(addr);
  474. vma = find_vma(mm, addr);
  475. if (((TASK_SIZE - len) >= addr)
  476. && (!vma || (addr+len) <= vma->vm_start)
  477. && !is_hugepage_only_range(mm, addr,len))
  478. return addr;
  479. }
  480. if (len > mm->cached_hole_size) {
  481. start_addr = addr = mm->free_area_cache;
  482. } else {
  483. start_addr = addr = TASK_UNMAPPED_BASE;
  484. mm->cached_hole_size = 0;
  485. }
  486. full_search:
  487. vma = find_vma(mm, addr);
  488. while (TASK_SIZE - len >= addr) {
  489. BUG_ON(vma && (addr >= vma->vm_end));
  490. if (touches_hugepage_low_range(mm, addr, len)) {
  491. addr = ALIGN(addr+1, 1<<SID_SHIFT);
  492. vma = find_vma(mm, addr);
  493. continue;
  494. }
  495. if (touches_hugepage_high_range(mm, addr, len)) {
  496. addr = ALIGN(addr+1, 1UL<<HTLB_AREA_SHIFT);
  497. vma = find_vma(mm, addr);
  498. continue;
  499. }
  500. if (!vma || addr + len <= vma->vm_start) {
  501. /*
  502. * Remember the place where we stopped the search:
  503. */
  504. mm->free_area_cache = addr + len;
  505. return addr;
  506. }
  507. if (addr + mm->cached_hole_size < vma->vm_start)
  508. mm->cached_hole_size = vma->vm_start - addr;
  509. addr = vma->vm_end;
  510. vma = vma->vm_next;
  511. }
  512. /* Make sure we didn't miss any holes */
  513. if (start_addr != TASK_UNMAPPED_BASE) {
  514. start_addr = addr = TASK_UNMAPPED_BASE;
  515. mm->cached_hole_size = 0;
  516. goto full_search;
  517. }
  518. return -ENOMEM;
  519. }
  520. /*
  521. * This mmap-allocator allocates new areas top-down from below the
  522. * stack's low limit (the base):
  523. *
  524. * Because we have an exclusive hugepage region which lies within the
  525. * normal user address space, we have to take special measures to make
  526. * non-huge mmap()s evade the hugepage reserved regions.
  527. */
  528. unsigned long
  529. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  530. const unsigned long len, const unsigned long pgoff,
  531. const unsigned long flags)
  532. {
  533. struct vm_area_struct *vma, *prev_vma;
  534. struct mm_struct *mm = current->mm;
  535. unsigned long base = mm->mmap_base, addr = addr0;
  536. unsigned long largest_hole = mm->cached_hole_size;
  537. int first_time = 1;
  538. /* requested length too big for entire address space */
  539. if (len > TASK_SIZE)
  540. return -ENOMEM;
  541. /* dont allow allocations above current base */
  542. if (mm->free_area_cache > base)
  543. mm->free_area_cache = base;
  544. /* requesting a specific address */
  545. if (addr) {
  546. addr = PAGE_ALIGN(addr);
  547. vma = find_vma(mm, addr);
  548. if (TASK_SIZE - len >= addr &&
  549. (!vma || addr + len <= vma->vm_start)
  550. && !is_hugepage_only_range(mm, addr,len))
  551. return addr;
  552. }
  553. if (len <= largest_hole) {
  554. largest_hole = 0;
  555. mm->free_area_cache = base;
  556. }
  557. try_again:
  558. /* make sure it can fit in the remaining address space */
  559. if (mm->free_area_cache < len)
  560. goto fail;
  561. /* either no address requested or cant fit in requested address hole */
  562. addr = (mm->free_area_cache - len) & PAGE_MASK;
  563. do {
  564. hugepage_recheck:
  565. if (touches_hugepage_low_range(mm, addr, len)) {
  566. addr = (addr & ((~0) << SID_SHIFT)) - len;
  567. goto hugepage_recheck;
  568. } else if (touches_hugepage_high_range(mm, addr, len)) {
  569. addr = (addr & ((~0UL) << HTLB_AREA_SHIFT)) - len;
  570. goto hugepage_recheck;
  571. }
  572. /*
  573. * Lookup failure means no vma is above this address,
  574. * i.e. return with success:
  575. */
  576. if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
  577. return addr;
  578. /*
  579. * new region fits between prev_vma->vm_end and
  580. * vma->vm_start, use it:
  581. */
  582. if (addr+len <= vma->vm_start &&
  583. (!prev_vma || (addr >= prev_vma->vm_end))) {
  584. /* remember the address as a hint for next time */
  585. mm->cached_hole_size = largest_hole;
  586. return (mm->free_area_cache = addr);
  587. } else {
  588. /* pull free_area_cache down to the first hole */
  589. if (mm->free_area_cache == vma->vm_end) {
  590. mm->free_area_cache = vma->vm_start;
  591. mm->cached_hole_size = largest_hole;
  592. }
  593. }
  594. /* remember the largest hole we saw so far */
  595. if (addr + largest_hole < vma->vm_start)
  596. largest_hole = vma->vm_start - addr;
  597. /* try just below the current vma->vm_start */
  598. addr = vma->vm_start-len;
  599. } while (len <= vma->vm_start);
  600. fail:
  601. /*
  602. * if hint left us with no space for the requested
  603. * mapping then try again:
  604. */
  605. if (first_time) {
  606. mm->free_area_cache = base;
  607. largest_hole = 0;
  608. first_time = 0;
  609. goto try_again;
  610. }
  611. /*
  612. * A failed mmap() very likely causes application failure,
  613. * so fall back to the bottom-up function here. This scenario
  614. * can happen with large stack limits and large mmap()
  615. * allocations.
  616. */
  617. mm->free_area_cache = TASK_UNMAPPED_BASE;
  618. mm->cached_hole_size = ~0UL;
  619. addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
  620. /*
  621. * Restore the topdown base:
  622. */
  623. mm->free_area_cache = base;
  624. mm->cached_hole_size = ~0UL;
  625. return addr;
  626. }
  627. static int htlb_check_hinted_area(unsigned long addr, unsigned long len)
  628. {
  629. struct vm_area_struct *vma;
  630. vma = find_vma(current->mm, addr);
  631. if (!vma || ((addr + len) <= vma->vm_start))
  632. return 0;
  633. return -ENOMEM;
  634. }
  635. static unsigned long htlb_get_low_area(unsigned long len, u16 segmask)
  636. {
  637. unsigned long addr = 0;
  638. struct vm_area_struct *vma;
  639. vma = find_vma(current->mm, addr);
  640. while (addr + len <= 0x100000000UL) {
  641. BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
  642. if (! __within_hugepage_low_range(addr, len, segmask)) {
  643. addr = ALIGN(addr+1, 1<<SID_SHIFT);
  644. vma = find_vma(current->mm, addr);
  645. continue;
  646. }
  647. if (!vma || (addr + len) <= vma->vm_start)
  648. return addr;
  649. addr = ALIGN(vma->vm_end, HPAGE_SIZE);
  650. /* Depending on segmask this might not be a confirmed
  651. * hugepage region, so the ALIGN could have skipped
  652. * some VMAs */
  653. vma = find_vma(current->mm, addr);
  654. }
  655. return -ENOMEM;
  656. }
  657. static unsigned long htlb_get_high_area(unsigned long len, u16 areamask)
  658. {
  659. unsigned long addr = 0x100000000UL;
  660. struct vm_area_struct *vma;
  661. vma = find_vma(current->mm, addr);
  662. while (addr + len <= TASK_SIZE_USER64) {
  663. BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
  664. if (! __within_hugepage_high_range(addr, len, areamask)) {
  665. addr = ALIGN(addr+1, 1UL<<HTLB_AREA_SHIFT);
  666. vma = find_vma(current->mm, addr);
  667. continue;
  668. }
  669. if (!vma || (addr + len) <= vma->vm_start)
  670. return addr;
  671. addr = ALIGN(vma->vm_end, HPAGE_SIZE);
  672. /* Depending on segmask this might not be a confirmed
  673. * hugepage region, so the ALIGN could have skipped
  674. * some VMAs */
  675. vma = find_vma(current->mm, addr);
  676. }
  677. return -ENOMEM;
  678. }
  679. unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  680. unsigned long len, unsigned long pgoff,
  681. unsigned long flags)
  682. {
  683. int lastshift;
  684. u16 areamask, curareas;
  685. if (HPAGE_SHIFT == 0)
  686. return -EINVAL;
  687. if (len & ~HPAGE_MASK)
  688. return -EINVAL;
  689. if (!cpu_has_feature(CPU_FTR_16M_PAGE))
  690. return -EINVAL;
  691. /* Paranoia, caller should have dealt with this */
  692. BUG_ON((addr + len) < addr);
  693. if (test_thread_flag(TIF_32BIT)) {
  694. /* Paranoia, caller should have dealt with this */
  695. BUG_ON((addr + len) > 0x100000000UL);
  696. curareas = current->mm->context.low_htlb_areas;
  697. /* First see if we can use the hint address */
  698. if (addr && (htlb_check_hinted_area(addr, len) == 0)) {
  699. areamask = LOW_ESID_MASK(addr, len);
  700. if (open_low_hpage_areas(current->mm, areamask) == 0)
  701. return addr;
  702. }
  703. /* Next see if we can map in the existing low areas */
  704. addr = htlb_get_low_area(len, curareas);
  705. if (addr != -ENOMEM)
  706. return addr;
  707. /* Finally go looking for areas to open */
  708. lastshift = 0;
  709. for (areamask = LOW_ESID_MASK(0x100000000UL-len, len);
  710. ! lastshift; areamask >>=1) {
  711. if (areamask & 1)
  712. lastshift = 1;
  713. addr = htlb_get_low_area(len, curareas | areamask);
  714. if ((addr != -ENOMEM)
  715. && open_low_hpage_areas(current->mm, areamask) == 0)
  716. return addr;
  717. }
  718. } else {
  719. curareas = current->mm->context.high_htlb_areas;
  720. /* First see if we can use the hint address */
  721. /* We discourage 64-bit processes from doing hugepage
  722. * mappings below 4GB (must use MAP_FIXED) */
  723. if ((addr >= 0x100000000UL)
  724. && (htlb_check_hinted_area(addr, len) == 0)) {
  725. areamask = HTLB_AREA_MASK(addr, len);
  726. if (open_high_hpage_areas(current->mm, areamask) == 0)
  727. return addr;
  728. }
  729. /* Next see if we can map in the existing high areas */
  730. addr = htlb_get_high_area(len, curareas);
  731. if (addr != -ENOMEM)
  732. return addr;
  733. /* Finally go looking for areas to open */
  734. lastshift = 0;
  735. for (areamask = HTLB_AREA_MASK(TASK_SIZE_USER64-len, len);
  736. ! lastshift; areamask >>=1) {
  737. if (areamask & 1)
  738. lastshift = 1;
  739. addr = htlb_get_high_area(len, curareas | areamask);
  740. if ((addr != -ENOMEM)
  741. && open_high_hpage_areas(current->mm, areamask) == 0)
  742. return addr;
  743. }
  744. }
  745. printk(KERN_DEBUG "hugetlb_get_unmapped_area() unable to open"
  746. " enough areas\n");
  747. return -ENOMEM;
  748. }
  749. /*
  750. * Called by asm hashtable.S for doing lazy icache flush
  751. */
  752. static unsigned int hash_huge_page_do_lazy_icache(unsigned long rflags,
  753. pte_t pte, int trap)
  754. {
  755. struct page *page;
  756. int i;
  757. if (!pfn_valid(pte_pfn(pte)))
  758. return rflags;
  759. page = pte_page(pte);
  760. /* page is dirty */
  761. if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
  762. if (trap == 0x400) {
  763. for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++)
  764. __flush_dcache_icache(page_address(page+i));
  765. set_bit(PG_arch_1, &page->flags);
  766. } else {
  767. rflags |= HPTE_R_N;
  768. }
  769. }
  770. return rflags;
  771. }
  772. int hash_huge_page(struct mm_struct *mm, unsigned long access,
  773. unsigned long ea, unsigned long vsid, int local,
  774. unsigned long trap)
  775. {
  776. pte_t *ptep;
  777. unsigned long old_pte, new_pte;
  778. unsigned long va, rflags, pa;
  779. long slot;
  780. int err = 1;
  781. ptep = huge_pte_offset(mm, ea);
  782. /* Search the Linux page table for a match with va */
  783. va = (vsid << 28) | (ea & 0x0fffffff);
  784. /*
  785. * If no pte found or not present, send the problem up to
  786. * do_page_fault
  787. */
  788. if (unlikely(!ptep || pte_none(*ptep)))
  789. goto out;
  790. /*
  791. * Check the user's access rights to the page. If access should be
  792. * prevented then send the problem up to do_page_fault.
  793. */
  794. if (unlikely(access & ~pte_val(*ptep)))
  795. goto out;
  796. /*
  797. * At this point, we have a pte (old_pte) which can be used to build
  798. * or update an HPTE. There are 2 cases:
  799. *
  800. * 1. There is a valid (present) pte with no associated HPTE (this is
  801. * the most common case)
  802. * 2. There is a valid (present) pte with an associated HPTE. The
  803. * current values of the pp bits in the HPTE prevent access
  804. * because we are doing software DIRTY bit management and the
  805. * page is currently not DIRTY.
  806. */
  807. do {
  808. old_pte = pte_val(*ptep);
  809. if (old_pte & _PAGE_BUSY)
  810. goto out;
  811. new_pte = old_pte | _PAGE_BUSY |
  812. _PAGE_ACCESSED | _PAGE_HASHPTE;
  813. } while(old_pte != __cmpxchg_u64((unsigned long *)ptep,
  814. old_pte, new_pte));
  815. rflags = 0x2 | (!(new_pte & _PAGE_RW));
  816. /* _PAGE_EXEC -> HW_NO_EXEC since it's inverted */
  817. rflags |= ((new_pte & _PAGE_EXEC) ? 0 : HPTE_R_N);
  818. if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
  819. /* No CPU has hugepages but lacks no execute, so we
  820. * don't need to worry about that case */
  821. rflags = hash_huge_page_do_lazy_icache(rflags, __pte(old_pte),
  822. trap);
  823. /* Check if pte already has an hpte (case 2) */
  824. if (unlikely(old_pte & _PAGE_HASHPTE)) {
  825. /* There MIGHT be an HPTE for this pte */
  826. unsigned long hash, slot;
  827. hash = hpt_hash(va, HPAGE_SHIFT);
  828. if (old_pte & _PAGE_F_SECOND)
  829. hash = ~hash;
  830. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  831. slot += (old_pte & _PAGE_F_GIX) >> 12;
  832. if (ppc_md.hpte_updatepp(slot, rflags, va, mmu_huge_psize,
  833. local) == -1)
  834. old_pte &= ~_PAGE_HPTEFLAGS;
  835. }
  836. if (likely(!(old_pte & _PAGE_HASHPTE))) {
  837. unsigned long hash = hpt_hash(va, HPAGE_SHIFT);
  838. unsigned long hpte_group;
  839. pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
  840. repeat:
  841. hpte_group = ((hash & htab_hash_mask) *
  842. HPTES_PER_GROUP) & ~0x7UL;
  843. /* clear HPTE slot informations in new PTE */
  844. new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | _PAGE_HASHPTE;
  845. /* Add in WIMG bits */
  846. /* XXX We should store these in the pte */
  847. /* --BenH: I think they are ... */
  848. rflags |= _PAGE_COHERENT;
  849. /* Insert into the hash table, primary slot */
  850. slot = ppc_md.hpte_insert(hpte_group, va, pa, rflags, 0,
  851. mmu_huge_psize);
  852. /* Primary is full, try the secondary */
  853. if (unlikely(slot == -1)) {
  854. new_pte |= _PAGE_F_SECOND;
  855. hpte_group = ((~hash & htab_hash_mask) *
  856. HPTES_PER_GROUP) & ~0x7UL;
  857. slot = ppc_md.hpte_insert(hpte_group, va, pa, rflags,
  858. HPTE_V_SECONDARY,
  859. mmu_huge_psize);
  860. if (slot == -1) {
  861. if (mftb() & 0x1)
  862. hpte_group = ((hash & htab_hash_mask) *
  863. HPTES_PER_GROUP)&~0x7UL;
  864. ppc_md.hpte_remove(hpte_group);
  865. goto repeat;
  866. }
  867. }
  868. if (unlikely(slot == -2))
  869. panic("hash_huge_page: pte_insert failed\n");
  870. new_pte |= (slot << 12) & _PAGE_F_GIX;
  871. }
  872. /*
  873. * No need to use ldarx/stdcx here
  874. */
  875. *ptep = __pte(new_pte & ~_PAGE_BUSY);
  876. err = 0;
  877. out:
  878. return err;
  879. }
  880. static void zero_ctor(void *addr, struct kmem_cache *cache, unsigned long flags)
  881. {
  882. memset(addr, 0, kmem_cache_size(cache));
  883. }
  884. static int __init hugetlbpage_init(void)
  885. {
  886. if (!cpu_has_feature(CPU_FTR_16M_PAGE))
  887. return -ENODEV;
  888. huge_pgtable_cache = kmem_cache_create("hugepte_cache",
  889. HUGEPTE_TABLE_SIZE,
  890. HUGEPTE_TABLE_SIZE,
  891. SLAB_HWCACHE_ALIGN |
  892. SLAB_MUST_HWCACHE_ALIGN,
  893. zero_ctor, NULL);
  894. if (! huge_pgtable_cache)
  895. panic("hugetlbpage_init(): could not create hugepte cache\n");
  896. return 0;
  897. }
  898. module_init(hugetlbpage_init);