hugetlbpage.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * SPARC64 Huge TLB page support.
  3. *
  4. * Copyright (C) 2002, 2003, 2006 David S. Miller (davem@davemloft.net)
  5. */
  6. #include <linux/init.h>
  7. #include <linux/module.h>
  8. #include <linux/fs.h>
  9. #include <linux/mm.h>
  10. #include <linux/hugetlb.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/sysctl.h>
  13. #include <asm/mman.h>
  14. #include <asm/pgalloc.h>
  15. #include <asm/tlb.h>
  16. #include <asm/tlbflush.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/mmu_context.h>
  19. /* Slightly simplified from the non-hugepage variant because by
  20. * definition we don't have to worry about any page coloring stuff
  21. */
  22. #define VA_EXCLUDE_START (0x0000080000000000UL - (1UL << 32UL))
  23. #define VA_EXCLUDE_END (0xfffff80000000000UL + (1UL << 32UL))
  24. static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *filp,
  25. unsigned long addr,
  26. unsigned long len,
  27. unsigned long pgoff,
  28. unsigned long flags)
  29. {
  30. struct mm_struct *mm = current->mm;
  31. struct vm_area_struct * vma;
  32. unsigned long task_size = TASK_SIZE;
  33. unsigned long start_addr;
  34. if (test_thread_flag(TIF_32BIT))
  35. task_size = STACK_TOP32;
  36. if (unlikely(len >= VA_EXCLUDE_START))
  37. return -ENOMEM;
  38. if (len > mm->cached_hole_size) {
  39. start_addr = addr = mm->free_area_cache;
  40. } else {
  41. start_addr = addr = TASK_UNMAPPED_BASE;
  42. mm->cached_hole_size = 0;
  43. }
  44. task_size -= len;
  45. full_search:
  46. addr = ALIGN(addr, HPAGE_SIZE);
  47. for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
  48. /* At this point: (!vma || addr < vma->vm_end). */
  49. if (addr < VA_EXCLUDE_START &&
  50. (addr + len) >= VA_EXCLUDE_START) {
  51. addr = VA_EXCLUDE_END;
  52. vma = find_vma(mm, VA_EXCLUDE_END);
  53. }
  54. if (unlikely(task_size < addr)) {
  55. if (start_addr != TASK_UNMAPPED_BASE) {
  56. start_addr = addr = TASK_UNMAPPED_BASE;
  57. mm->cached_hole_size = 0;
  58. goto full_search;
  59. }
  60. return -ENOMEM;
  61. }
  62. if (likely(!vma || addr + len <= vma->vm_start)) {
  63. /*
  64. * Remember the place where we stopped the search:
  65. */
  66. mm->free_area_cache = addr + len;
  67. return addr;
  68. }
  69. if (addr + mm->cached_hole_size < vma->vm_start)
  70. mm->cached_hole_size = vma->vm_start - addr;
  71. addr = ALIGN(vma->vm_end, HPAGE_SIZE);
  72. }
  73. }
  74. static unsigned long
  75. hugetlb_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  76. const unsigned long len,
  77. const unsigned long pgoff,
  78. const unsigned long flags)
  79. {
  80. struct vm_area_struct *vma;
  81. struct mm_struct *mm = current->mm;
  82. unsigned long addr = addr0;
  83. /* This should only ever run for 32-bit processes. */
  84. BUG_ON(!test_thread_flag(TIF_32BIT));
  85. /* check if free_area_cache is useful for us */
  86. if (len <= mm->cached_hole_size) {
  87. mm->cached_hole_size = 0;
  88. mm->free_area_cache = mm->mmap_base;
  89. }
  90. /* either no address requested or can't fit in requested address hole */
  91. addr = mm->free_area_cache & HPAGE_MASK;
  92. /* make sure it can fit in the remaining address space */
  93. if (likely(addr > len)) {
  94. vma = find_vma(mm, addr-len);
  95. if (!vma || addr <= vma->vm_start) {
  96. /* remember the address as a hint for next time */
  97. return (mm->free_area_cache = addr-len);
  98. }
  99. }
  100. if (unlikely(mm->mmap_base < len))
  101. goto bottomup;
  102. addr = (mm->mmap_base-len) & HPAGE_MASK;
  103. do {
  104. /*
  105. * Lookup failure means no vma is above this address,
  106. * else if new region fits below vma->vm_start,
  107. * return with success:
  108. */
  109. vma = find_vma(mm, addr);
  110. if (likely(!vma || addr+len <= vma->vm_start)) {
  111. /* remember the address as a hint for next time */
  112. return (mm->free_area_cache = addr);
  113. }
  114. /* remember the largest hole we saw so far */
  115. if (addr + mm->cached_hole_size < vma->vm_start)
  116. mm->cached_hole_size = vma->vm_start - addr;
  117. /* try just below the current vma->vm_start */
  118. addr = (vma->vm_start-len) & HPAGE_MASK;
  119. } while (likely(len < vma->vm_start));
  120. bottomup:
  121. /*
  122. * A failed mmap() very likely causes application failure,
  123. * so fall back to the bottom-up function here. This scenario
  124. * can happen with large stack limits and large mmap()
  125. * allocations.
  126. */
  127. mm->cached_hole_size = ~0UL;
  128. mm->free_area_cache = TASK_UNMAPPED_BASE;
  129. addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
  130. /*
  131. * Restore the topdown base:
  132. */
  133. mm->free_area_cache = mm->mmap_base;
  134. mm->cached_hole_size = ~0UL;
  135. return addr;
  136. }
  137. unsigned long
  138. hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  139. unsigned long len, unsigned long pgoff, unsigned long flags)
  140. {
  141. struct mm_struct *mm = current->mm;
  142. struct vm_area_struct *vma;
  143. unsigned long task_size = TASK_SIZE;
  144. if (test_thread_flag(TIF_32BIT))
  145. task_size = STACK_TOP32;
  146. if (len & ~HPAGE_MASK)
  147. return -EINVAL;
  148. if (len > task_size)
  149. return -ENOMEM;
  150. if (flags & MAP_FIXED) {
  151. if (prepare_hugepage_range(file, addr, len))
  152. return -EINVAL;
  153. return addr;
  154. }
  155. if (addr) {
  156. addr = ALIGN(addr, HPAGE_SIZE);
  157. vma = find_vma(mm, addr);
  158. if (task_size - len >= addr &&
  159. (!vma || addr + len <= vma->vm_start))
  160. return addr;
  161. }
  162. if (mm->get_unmapped_area == arch_get_unmapped_area)
  163. return hugetlb_get_unmapped_area_bottomup(file, addr, len,
  164. pgoff, flags);
  165. else
  166. return hugetlb_get_unmapped_area_topdown(file, addr, len,
  167. pgoff, flags);
  168. }
  169. pte_t *huge_pte_alloc(struct mm_struct *mm,
  170. unsigned long addr, unsigned long sz)
  171. {
  172. pgd_t *pgd;
  173. pud_t *pud;
  174. pmd_t *pmd;
  175. pte_t *pte = NULL;
  176. /* We must align the address, because our caller will run
  177. * set_huge_pte_at() on whatever we return, which writes out
  178. * all of the sub-ptes for the hugepage range. So we have
  179. * to give it the first such sub-pte.
  180. */
  181. addr &= HPAGE_MASK;
  182. pgd = pgd_offset(mm, addr);
  183. pud = pud_alloc(mm, pgd, addr);
  184. if (pud) {
  185. pmd = pmd_alloc(mm, pud, addr);
  186. if (pmd)
  187. pte = pte_alloc_map(mm, NULL, pmd, addr);
  188. }
  189. return pte;
  190. }
  191. pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
  192. {
  193. pgd_t *pgd;
  194. pud_t *pud;
  195. pmd_t *pmd;
  196. pte_t *pte = NULL;
  197. addr &= HPAGE_MASK;
  198. pgd = pgd_offset(mm, addr);
  199. if (!pgd_none(*pgd)) {
  200. pud = pud_offset(pgd, addr);
  201. if (!pud_none(*pud)) {
  202. pmd = pmd_offset(pud, addr);
  203. if (!pmd_none(*pmd))
  204. pte = pte_offset_map(pmd, addr);
  205. }
  206. }
  207. return pte;
  208. }
  209. int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
  210. {
  211. return 0;
  212. }
  213. void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
  214. pte_t *ptep, pte_t entry)
  215. {
  216. int i;
  217. if (!pte_present(*ptep) && pte_present(entry))
  218. mm->context.huge_pte_count++;
  219. addr &= HPAGE_MASK;
  220. for (i = 0; i < (1 << HUGETLB_PAGE_ORDER); i++) {
  221. set_pte_at(mm, addr, ptep, entry);
  222. ptep++;
  223. addr += PAGE_SIZE;
  224. pte_val(entry) += PAGE_SIZE;
  225. }
  226. }
  227. pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
  228. pte_t *ptep)
  229. {
  230. pte_t entry;
  231. int i;
  232. entry = *ptep;
  233. if (pte_present(entry))
  234. mm->context.huge_pte_count--;
  235. addr &= HPAGE_MASK;
  236. for (i = 0; i < (1 << HUGETLB_PAGE_ORDER); i++) {
  237. pte_clear(mm, addr, ptep);
  238. addr += PAGE_SIZE;
  239. ptep++;
  240. }
  241. return entry;
  242. }
  243. struct page *follow_huge_addr(struct mm_struct *mm,
  244. unsigned long address, int write)
  245. {
  246. return ERR_PTR(-EINVAL);
  247. }
  248. int pmd_huge(pmd_t pmd)
  249. {
  250. return 0;
  251. }
  252. int pud_huge(pud_t pud)
  253. {
  254. return 0;
  255. }
  256. struct page *follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  257. pmd_t *pmd, int write)
  258. {
  259. return NULL;
  260. }
  261. static void context_reload(void *__data)
  262. {
  263. struct mm_struct *mm = __data;
  264. if (mm == current->mm)
  265. load_secondary_context(mm);
  266. }
  267. void hugetlb_prefault_arch_hook(struct mm_struct *mm)
  268. {
  269. struct tsb_config *tp = &mm->context.tsb_block[MM_TSB_HUGE];
  270. if (likely(tp->tsb != NULL))
  271. return;
  272. tsb_grow(mm, MM_TSB_HUGE, 0);
  273. tsb_context_switch(mm);
  274. smp_tsb_sync(mm);
  275. /* On UltraSPARC-III+ and later, configure the second half of
  276. * the Data-TLB for huge pages.
  277. */
  278. if (tlb_type == cheetah_plus) {
  279. unsigned long ctx;
  280. spin_lock(&ctx_alloc_lock);
  281. ctx = mm->context.sparc64_ctx_val;
  282. ctx &= ~CTX_PGSZ_MASK;
  283. ctx |= CTX_PGSZ_BASE << CTX_PGSZ0_SHIFT;
  284. ctx |= CTX_PGSZ_HUGE << CTX_PGSZ1_SHIFT;
  285. if (ctx != mm->context.sparc64_ctx_val) {
  286. /* When changing the page size fields, we
  287. * must perform a context flush so that no
  288. * stale entries match. This flush must
  289. * occur with the original context register
  290. * settings.
  291. */
  292. do_flush_tlb_mm(mm);
  293. /* Reload the context register of all processors
  294. * also executing in this address space.
  295. */
  296. mm->context.sparc64_ctx_val = ctx;
  297. on_each_cpu(context_reload, mm, 0);
  298. }
  299. spin_unlock(&ctx_alloc_lock);
  300. }
  301. }