hugetlbpage.c 8.3 KB

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