hugetlbpage.c 6.7 KB

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