hugetlbpage.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. start_addr = mm->free_area_cache;
  119. full_search:
  120. addr = ALIGN(start_addr, HPAGE_SIZE);
  121. for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
  122. /* At this point: (!vma || addr < vma->vm_end). */
  123. if (TASK_SIZE - len < addr) {
  124. /*
  125. * Start a new search - just in case we missed
  126. * some holes.
  127. */
  128. if (start_addr != TASK_UNMAPPED_BASE) {
  129. start_addr = TASK_UNMAPPED_BASE;
  130. goto full_search;
  131. }
  132. return -ENOMEM;
  133. }
  134. if (!vma || addr + len <= vma->vm_start) {
  135. mm->free_area_cache = addr + len;
  136. return addr;
  137. }
  138. addr = ALIGN(vma->vm_end, HPAGE_SIZE);
  139. }
  140. }
  141. static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
  142. unsigned long addr0, unsigned long len,
  143. unsigned long pgoff, unsigned long flags)
  144. {
  145. struct mm_struct *mm = current->mm;
  146. struct vm_area_struct *vma, *prev_vma;
  147. unsigned long base = mm->mmap_base, addr = addr0;
  148. int first_time = 1;
  149. /* don't allow allocations above current base */
  150. if (mm->free_area_cache > base)
  151. mm->free_area_cache = base;
  152. try_again:
  153. /* make sure it can fit in the remaining address space */
  154. if (mm->free_area_cache < len)
  155. goto fail;
  156. /* either no address requested or cant fit in requested address hole */
  157. addr = (mm->free_area_cache - len) & HPAGE_MASK;
  158. do {
  159. /*
  160. * Lookup failure means no vma is above this address,
  161. * i.e. return with success:
  162. */
  163. if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
  164. return addr;
  165. /*
  166. * new region fits between prev_vma->vm_end and
  167. * vma->vm_start, use it:
  168. */
  169. if (addr + len <= vma->vm_start &&
  170. (!prev_vma || (addr >= prev_vma->vm_end)))
  171. /* remember the address as a hint for next time */
  172. return (mm->free_area_cache = addr);
  173. else
  174. /* pull free_area_cache down to the first hole */
  175. if (mm->free_area_cache == vma->vm_end)
  176. mm->free_area_cache = vma->vm_start;
  177. /* try just below the current vma->vm_start */
  178. addr = (vma->vm_start - len) & HPAGE_MASK;
  179. } while (len <= vma->vm_start);
  180. fail:
  181. /*
  182. * if hint left us with no space for the requested
  183. * mapping then try again:
  184. */
  185. if (first_time) {
  186. mm->free_area_cache = base;
  187. first_time = 0;
  188. goto try_again;
  189. }
  190. /*
  191. * A failed mmap() very likely causes application failure,
  192. * so fall back to the bottom-up function here. This scenario
  193. * can happen with large stack limits and large mmap()
  194. * allocations.
  195. */
  196. mm->free_area_cache = TASK_UNMAPPED_BASE;
  197. addr = hugetlb_get_unmapped_area_bottomup(file, addr0,
  198. len, pgoff, flags);
  199. /*
  200. * Restore the topdown base:
  201. */
  202. mm->free_area_cache = base;
  203. return addr;
  204. }
  205. unsigned long
  206. hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  207. unsigned long len, unsigned long pgoff, unsigned long flags)
  208. {
  209. struct mm_struct *mm = current->mm;
  210. struct vm_area_struct *vma;
  211. if (len & ~HPAGE_MASK)
  212. return -EINVAL;
  213. if (len > TASK_SIZE)
  214. return -ENOMEM;
  215. if (addr) {
  216. addr = ALIGN(addr, HPAGE_SIZE);
  217. vma = find_vma(mm, addr);
  218. if (TASK_SIZE - len >= addr &&
  219. (!vma || addr + len <= vma->vm_start))
  220. return addr;
  221. }
  222. if (mm->get_unmapped_area == arch_get_unmapped_area)
  223. return hugetlb_get_unmapped_area_bottomup(file, addr, len,
  224. pgoff, flags);
  225. else
  226. return hugetlb_get_unmapped_area_topdown(file, addr, len,
  227. pgoff, flags);
  228. }
  229. #endif /*HAVE_ARCH_HUGETLB_UNMAPPED_AREA*/