hugetlbpage.c 6.4 KB

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