hugetlbpage.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * SPARC64 Huge TLB page support.
  3. *
  4. * Copyright (C) 2002, 2003, 2006 David S. Miller (davem@davemloft.net)
  5. */
  6. #include <linux/fs.h>
  7. #include <linux/mm.h>
  8. #include <linux/hugetlb.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/sysctl.h>
  11. #include <asm/mman.h>
  12. #include <asm/pgalloc.h>
  13. #include <asm/tlb.h>
  14. #include <asm/tlbflush.h>
  15. #include <asm/cacheflush.h>
  16. #include <asm/mmu_context.h>
  17. /* Slightly simplified from the non-hugepage variant because by
  18. * definition we don't have to worry about any page coloring stuff
  19. */
  20. static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *filp,
  21. unsigned long addr,
  22. unsigned long len,
  23. unsigned long pgoff,
  24. unsigned long flags)
  25. {
  26. unsigned long task_size = TASK_SIZE;
  27. struct vm_unmapped_area_info info;
  28. if (test_thread_flag(TIF_32BIT))
  29. task_size = STACK_TOP32;
  30. info.flags = 0;
  31. info.length = len;
  32. info.low_limit = TASK_UNMAPPED_BASE;
  33. info.high_limit = min(task_size, VA_EXCLUDE_START);
  34. info.align_mask = PAGE_MASK & ~HPAGE_MASK;
  35. info.align_offset = 0;
  36. addr = vm_unmapped_area(&info);
  37. if ((addr & ~PAGE_MASK) && task_size > VA_EXCLUDE_END) {
  38. VM_BUG_ON(addr != -ENOMEM);
  39. info.low_limit = VA_EXCLUDE_END;
  40. info.high_limit = task_size;
  41. addr = vm_unmapped_area(&info);
  42. }
  43. return addr;
  44. }
  45. static unsigned long
  46. hugetlb_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  47. const unsigned long len,
  48. const unsigned long pgoff,
  49. const unsigned long flags)
  50. {
  51. struct mm_struct *mm = current->mm;
  52. unsigned long addr = addr0;
  53. struct vm_unmapped_area_info info;
  54. /* This should only ever run for 32-bit processes. */
  55. BUG_ON(!test_thread_flag(TIF_32BIT));
  56. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  57. info.length = len;
  58. info.low_limit = PAGE_SIZE;
  59. info.high_limit = mm->mmap_base;
  60. info.align_mask = PAGE_MASK & ~HPAGE_MASK;
  61. info.align_offset = 0;
  62. addr = vm_unmapped_area(&info);
  63. /*
  64. * A failed mmap() very likely causes application failure,
  65. * so fall back to the bottom-up function here. This scenario
  66. * can happen with large stack limits and large mmap()
  67. * allocations.
  68. */
  69. if (addr & ~PAGE_MASK) {
  70. VM_BUG_ON(addr != -ENOMEM);
  71. info.flags = 0;
  72. info.low_limit = TASK_UNMAPPED_BASE;
  73. info.high_limit = STACK_TOP32;
  74. addr = vm_unmapped_area(&info);
  75. }
  76. return addr;
  77. }
  78. unsigned long
  79. hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  80. unsigned long len, unsigned long pgoff, unsigned long flags)
  81. {
  82. struct mm_struct *mm = current->mm;
  83. struct vm_area_struct *vma;
  84. unsigned long task_size = TASK_SIZE;
  85. if (test_thread_flag(TIF_32BIT))
  86. task_size = STACK_TOP32;
  87. if (len & ~HPAGE_MASK)
  88. return -EINVAL;
  89. if (len > task_size)
  90. return -ENOMEM;
  91. if (flags & MAP_FIXED) {
  92. if (prepare_hugepage_range(file, addr, len))
  93. return -EINVAL;
  94. return addr;
  95. }
  96. if (addr) {
  97. addr = ALIGN(addr, HPAGE_SIZE);
  98. vma = find_vma(mm, addr);
  99. if (task_size - len >= addr &&
  100. (!vma || addr + len <= vm_start_gap(vma)))
  101. return addr;
  102. }
  103. if (mm->get_unmapped_area == arch_get_unmapped_area)
  104. return hugetlb_get_unmapped_area_bottomup(file, addr, len,
  105. pgoff, flags);
  106. else
  107. return hugetlb_get_unmapped_area_topdown(file, addr, len,
  108. pgoff, flags);
  109. }
  110. pte_t *huge_pte_alloc(struct mm_struct *mm,
  111. unsigned long addr, unsigned long sz)
  112. {
  113. pgd_t *pgd;
  114. pud_t *pud;
  115. pmd_t *pmd;
  116. pte_t *pte = NULL;
  117. /* We must align the address, because our caller will run
  118. * set_huge_pte_at() on whatever we return, which writes out
  119. * all of the sub-ptes for the hugepage range. So we have
  120. * to give it the first such sub-pte.
  121. */
  122. addr &= HPAGE_MASK;
  123. pgd = pgd_offset(mm, addr);
  124. pud = pud_alloc(mm, pgd, addr);
  125. if (pud) {
  126. pmd = pmd_alloc(mm, pud, addr);
  127. if (pmd)
  128. pte = pte_alloc_map(mm, NULL, pmd, addr);
  129. }
  130. return pte;
  131. }
  132. pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
  133. {
  134. pgd_t *pgd;
  135. pud_t *pud;
  136. pmd_t *pmd;
  137. pte_t *pte = NULL;
  138. addr &= HPAGE_MASK;
  139. pgd = pgd_offset(mm, addr);
  140. if (!pgd_none(*pgd)) {
  141. pud = pud_offset(pgd, addr);
  142. if (!pud_none(*pud)) {
  143. pmd = pmd_offset(pud, addr);
  144. if (!pmd_none(*pmd))
  145. pte = pte_offset_map(pmd, addr);
  146. }
  147. }
  148. return pte;
  149. }
  150. void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
  151. pte_t *ptep, pte_t entry)
  152. {
  153. int i;
  154. pte_t orig[2];
  155. unsigned long nptes;
  156. if (!pte_present(*ptep) && pte_present(entry))
  157. mm->context.hugetlb_pte_count++;
  158. addr &= HPAGE_MASK;
  159. nptes = 1 << HUGETLB_PAGE_ORDER;
  160. orig[0] = *ptep;
  161. orig[1] = *(ptep + nptes / 2);
  162. for (i = 0; i < nptes; i++) {
  163. *ptep = entry;
  164. ptep++;
  165. addr += PAGE_SIZE;
  166. pte_val(entry) += PAGE_SIZE;
  167. }
  168. /* Issue TLB flush at REAL_HPAGE_SIZE boundaries */
  169. addr -= REAL_HPAGE_SIZE;
  170. ptep -= nptes / 2;
  171. maybe_tlb_batch_add(mm, addr, ptep, orig[1], 0);
  172. addr -= REAL_HPAGE_SIZE;
  173. ptep -= nptes / 2;
  174. maybe_tlb_batch_add(mm, addr, ptep, orig[0], 0);
  175. }
  176. pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
  177. pte_t *ptep)
  178. {
  179. pte_t entry;
  180. int i;
  181. unsigned long nptes;
  182. entry = *ptep;
  183. if (pte_present(entry))
  184. mm->context.hugetlb_pte_count--;
  185. addr &= HPAGE_MASK;
  186. nptes = 1 << HUGETLB_PAGE_ORDER;
  187. for (i = 0; i < nptes; i++) {
  188. *ptep = __pte(0UL);
  189. addr += PAGE_SIZE;
  190. ptep++;
  191. }
  192. /* Issue TLB flush at REAL_HPAGE_SIZE boundaries */
  193. addr -= REAL_HPAGE_SIZE;
  194. ptep -= nptes / 2;
  195. maybe_tlb_batch_add(mm, addr, ptep, entry, 0);
  196. addr -= REAL_HPAGE_SIZE;
  197. ptep -= nptes / 2;
  198. maybe_tlb_batch_add(mm, addr, ptep, entry, 0);
  199. return entry;
  200. }
  201. int pmd_huge(pmd_t pmd)
  202. {
  203. return 0;
  204. }
  205. int pud_huge(pud_t pud)
  206. {
  207. return 0;
  208. }