hugetlbpage.c 6.6 KB

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