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