hugetlbpage.c 27 KB

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