hugetlbpage.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * IA-32 Huge TLB Page Support for Kernel.
  3. *
  4. * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
  5. */
  6. #include <linux/config.h>
  7. #include <linux/init.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/err.h>
  15. #include <linux/sysctl.h>
  16. #include <asm/mman.h>
  17. #include <asm/tlb.h>
  18. #include <asm/tlbflush.h>
  19. pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr)
  20. {
  21. pgd_t *pgd;
  22. pud_t *pud;
  23. pte_t *pte = NULL;
  24. pgd = pgd_offset(mm, addr);
  25. pud = pud_alloc(mm, pgd, addr);
  26. if (pud)
  27. pte = (pte_t *) pmd_alloc(mm, pud, addr);
  28. BUG_ON(pte && !pte_none(*pte) && !pte_huge(*pte));
  29. return pte;
  30. }
  31. pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
  32. {
  33. pgd_t *pgd;
  34. pud_t *pud;
  35. pmd_t *pmd = NULL;
  36. pgd = pgd_offset(mm, addr);
  37. if (pgd_present(*pgd)) {
  38. pud = pud_offset(pgd, addr);
  39. if (pud_present(*pud))
  40. pmd = pmd_offset(pud, addr);
  41. }
  42. return (pte_t *) pmd;
  43. }
  44. #if 0 /* This is just for testing */
  45. struct page *
  46. follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
  47. {
  48. unsigned long start = address;
  49. int length = 1;
  50. int nr;
  51. struct page *page;
  52. struct vm_area_struct *vma;
  53. vma = find_vma(mm, addr);
  54. if (!vma || !is_vm_hugetlb_page(vma))
  55. return ERR_PTR(-EINVAL);
  56. pte = huge_pte_offset(mm, address);
  57. /* hugetlb should be locked, and hence, prefaulted */
  58. WARN_ON(!pte || pte_none(*pte));
  59. page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
  60. WARN_ON(!PageCompound(page));
  61. return page;
  62. }
  63. int pmd_huge(pmd_t pmd)
  64. {
  65. return 0;
  66. }
  67. struct page *
  68. follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  69. pmd_t *pmd, int write)
  70. {
  71. return NULL;
  72. }
  73. #else
  74. struct page *
  75. follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
  76. {
  77. return ERR_PTR(-EINVAL);
  78. }
  79. int pmd_huge(pmd_t pmd)
  80. {
  81. return !!(pmd_val(pmd) & _PAGE_PSE);
  82. }
  83. struct page *
  84. follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  85. pmd_t *pmd, int write)
  86. {
  87. struct page *page;
  88. page = pte_page(*(pte_t *)pmd);
  89. if (page)
  90. page += ((address & ~HPAGE_MASK) >> PAGE_SHIFT);
  91. return page;
  92. }
  93. #endif
  94. /* x86_64 also uses this file */
  95. #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
  96. static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
  97. unsigned long addr, unsigned long len,
  98. unsigned long pgoff, unsigned long flags)
  99. {
  100. struct mm_struct *mm = current->mm;
  101. struct vm_area_struct *vma;
  102. unsigned long start_addr;
  103. if (len > mm->cached_hole_size) {
  104. start_addr = mm->free_area_cache;
  105. } else {
  106. start_addr = TASK_UNMAPPED_BASE;
  107. mm->cached_hole_size = 0;
  108. }
  109. full_search:
  110. addr = ALIGN(start_addr, HPAGE_SIZE);
  111. for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
  112. /* At this point: (!vma || addr < vma->vm_end). */
  113. if (TASK_SIZE - len < addr) {
  114. /*
  115. * Start a new search - just in case we missed
  116. * some holes.
  117. */
  118. if (start_addr != TASK_UNMAPPED_BASE) {
  119. start_addr = TASK_UNMAPPED_BASE;
  120. mm->cached_hole_size = 0;
  121. goto full_search;
  122. }
  123. return -ENOMEM;
  124. }
  125. if (!vma || addr + len <= vma->vm_start) {
  126. mm->free_area_cache = addr + len;
  127. return addr;
  128. }
  129. if (addr + mm->cached_hole_size < vma->vm_start)
  130. mm->cached_hole_size = vma->vm_start - addr;
  131. addr = ALIGN(vma->vm_end, HPAGE_SIZE);
  132. }
  133. }
  134. static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
  135. unsigned long addr0, unsigned long len,
  136. unsigned long pgoff, unsigned long flags)
  137. {
  138. struct mm_struct *mm = current->mm;
  139. struct vm_area_struct *vma, *prev_vma;
  140. unsigned long base = mm->mmap_base, addr = addr0;
  141. unsigned long largest_hole = mm->cached_hole_size;
  142. int first_time = 1;
  143. /* don't allow allocations above current base */
  144. if (mm->free_area_cache > base)
  145. mm->free_area_cache = base;
  146. if (len <= largest_hole) {
  147. largest_hole = 0;
  148. mm->free_area_cache = base;
  149. }
  150. try_again:
  151. /* make sure it can fit in the remaining address space */
  152. if (mm->free_area_cache < len)
  153. goto fail;
  154. /* either no address requested or cant fit in requested address hole */
  155. addr = (mm->free_area_cache - len) & HPAGE_MASK;
  156. do {
  157. /*
  158. * Lookup failure means no vma is above this address,
  159. * i.e. return with success:
  160. */
  161. if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
  162. return addr;
  163. /*
  164. * new region fits between prev_vma->vm_end and
  165. * vma->vm_start, use it:
  166. */
  167. if (addr + len <= vma->vm_start &&
  168. (!prev_vma || (addr >= prev_vma->vm_end))) {
  169. /* remember the address as a hint for next time */
  170. mm->cached_hole_size = largest_hole;
  171. return (mm->free_area_cache = addr);
  172. } else {
  173. /* pull free_area_cache down to the first hole */
  174. if (mm->free_area_cache == vma->vm_end) {
  175. mm->free_area_cache = vma->vm_start;
  176. mm->cached_hole_size = largest_hole;
  177. }
  178. }
  179. /* remember the largest hole we saw so far */
  180. if (addr + largest_hole < vma->vm_start)
  181. largest_hole = vma->vm_start - addr;
  182. /* try just below the current vma->vm_start */
  183. addr = (vma->vm_start - len) & HPAGE_MASK;
  184. } while (len <= vma->vm_start);
  185. fail:
  186. /*
  187. * if hint left us with no space for the requested
  188. * mapping then try again:
  189. */
  190. if (first_time) {
  191. mm->free_area_cache = base;
  192. largest_hole = 0;
  193. first_time = 0;
  194. goto try_again;
  195. }
  196. /*
  197. * A failed mmap() very likely causes application failure,
  198. * so fall back to the bottom-up function here. This scenario
  199. * can happen with large stack limits and large mmap()
  200. * allocations.
  201. */
  202. mm->free_area_cache = TASK_UNMAPPED_BASE;
  203. mm->cached_hole_size = ~0UL;
  204. addr = hugetlb_get_unmapped_area_bottomup(file, addr0,
  205. len, pgoff, flags);
  206. /*
  207. * Restore the topdown base:
  208. */
  209. mm->free_area_cache = base;
  210. mm->cached_hole_size = ~0UL;
  211. return addr;
  212. }
  213. unsigned long
  214. hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  215. unsigned long len, unsigned long pgoff, unsigned long flags)
  216. {
  217. struct mm_struct *mm = current->mm;
  218. struct vm_area_struct *vma;
  219. if (len & ~HPAGE_MASK)
  220. return -EINVAL;
  221. if (len > TASK_SIZE)
  222. return -ENOMEM;
  223. if (addr) {
  224. addr = ALIGN(addr, HPAGE_SIZE);
  225. vma = find_vma(mm, addr);
  226. if (TASK_SIZE - len >= addr &&
  227. (!vma || addr + len <= vma->vm_start))
  228. return addr;
  229. }
  230. if (mm->get_unmapped_area == arch_get_unmapped_area)
  231. return hugetlb_get_unmapped_area_bottomup(file, addr, len,
  232. pgoff, flags);
  233. else
  234. return hugetlb_get_unmapped_area_topdown(file, addr, len,
  235. pgoff, flags);
  236. }
  237. #endif /*HAVE_ARCH_HUGETLB_UNMAPPED_AREA*/