hugetlbpage.c 8.1 KB

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