hugetlbpage.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. /* We must align the address, because our caller will run
  174. * set_huge_pte_at() on whatever we return, which writes out
  175. * all of the sub-ptes for the hugepage range. So we have
  176. * to give it the first such sub-pte.
  177. */
  178. addr &= HPAGE_MASK;
  179. pgd = pgd_offset(mm, addr);
  180. pud = pud_alloc(mm, pgd, addr);
  181. if (pud) {
  182. pmd = pmd_alloc(mm, pud, addr);
  183. if (pmd)
  184. pte = pte_alloc_map(mm, pmd, addr);
  185. }
  186. return pte;
  187. }
  188. pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
  189. {
  190. pgd_t *pgd;
  191. pud_t *pud;
  192. pmd_t *pmd;
  193. pte_t *pte = NULL;
  194. addr &= HPAGE_MASK;
  195. pgd = pgd_offset(mm, addr);
  196. if (!pgd_none(*pgd)) {
  197. pud = pud_offset(pgd, addr);
  198. if (!pud_none(*pud)) {
  199. pmd = pmd_offset(pud, addr);
  200. if (!pmd_none(*pmd))
  201. pte = pte_offset_map(pmd, addr);
  202. }
  203. }
  204. return pte;
  205. }
  206. void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
  207. pte_t *ptep, pte_t entry)
  208. {
  209. int i;
  210. if (!pte_present(*ptep) && pte_present(entry))
  211. mm->context.huge_pte_count++;
  212. for (i = 0; i < (1 << HUGETLB_PAGE_ORDER); i++) {
  213. set_pte_at(mm, addr, ptep, entry);
  214. ptep++;
  215. addr += PAGE_SIZE;
  216. pte_val(entry) += PAGE_SIZE;
  217. }
  218. }
  219. pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
  220. pte_t *ptep)
  221. {
  222. pte_t entry;
  223. int i;
  224. entry = *ptep;
  225. if (pte_present(entry))
  226. mm->context.huge_pte_count--;
  227. for (i = 0; i < (1 << HUGETLB_PAGE_ORDER); i++) {
  228. pte_clear(mm, addr, ptep);
  229. addr += PAGE_SIZE;
  230. ptep++;
  231. }
  232. return entry;
  233. }
  234. struct page *follow_huge_addr(struct mm_struct *mm,
  235. unsigned long address, int write)
  236. {
  237. return ERR_PTR(-EINVAL);
  238. }
  239. int pmd_huge(pmd_t pmd)
  240. {
  241. return 0;
  242. }
  243. struct page *follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  244. pmd_t *pmd, int write)
  245. {
  246. return NULL;
  247. }
  248. static void context_reload(void *__data)
  249. {
  250. struct mm_struct *mm = __data;
  251. if (mm == current->mm)
  252. load_secondary_context(mm);
  253. }
  254. void hugetlb_prefault_arch_hook(struct mm_struct *mm)
  255. {
  256. struct tsb_config *tp = &mm->context.tsb_block[MM_TSB_HUGE];
  257. if (likely(tp->tsb != NULL))
  258. return;
  259. tsb_grow(mm, MM_TSB_HUGE, 0);
  260. tsb_context_switch(mm);
  261. smp_tsb_sync(mm);
  262. /* On UltraSPARC-III+ and later, configure the second half of
  263. * the Data-TLB for huge pages.
  264. */
  265. if (tlb_type == cheetah_plus) {
  266. unsigned long ctx;
  267. spin_lock(&ctx_alloc_lock);
  268. ctx = mm->context.sparc64_ctx_val;
  269. ctx &= ~CTX_PGSZ_MASK;
  270. ctx |= CTX_PGSZ_BASE << CTX_PGSZ0_SHIFT;
  271. ctx |= CTX_PGSZ_HUGE << CTX_PGSZ1_SHIFT;
  272. if (ctx != mm->context.sparc64_ctx_val) {
  273. /* When changing the page size fields, we
  274. * must perform a context flush so that no
  275. * stale entries match. This flush must
  276. * occur with the original context register
  277. * settings.
  278. */
  279. do_flush_tlb_mm(mm);
  280. /* Reload the context register of all processors
  281. * also executing in this address space.
  282. */
  283. mm->context.sparc64_ctx_val = ctx;
  284. on_each_cpu(context_reload, mm, 0, 0);
  285. }
  286. spin_unlock(&ctx_alloc_lock);
  287. }
  288. }