hugetlbpage.c 7.8 KB

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