hugetlbpage.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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/err.h>
  12. #include <linux/sysctl.h>
  13. #include <asm/mman.h>
  14. #include <asm/tlb.h>
  15. #include <asm/tlbflush.h>
  16. #include <asm/pgalloc.h>
  17. #if 0 /* This is just for testing */
  18. struct page *
  19. follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
  20. {
  21. unsigned long start = address;
  22. int length = 1;
  23. int nr;
  24. struct page *page;
  25. struct vm_area_struct *vma;
  26. vma = find_vma(mm, addr);
  27. if (!vma || !is_vm_hugetlb_page(vma))
  28. return ERR_PTR(-EINVAL);
  29. pte = huge_pte_offset(mm, address);
  30. /* hugetlb should be locked, and hence, prefaulted */
  31. WARN_ON(!pte || pte_none(*pte));
  32. page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
  33. WARN_ON(!PageHead(page));
  34. return page;
  35. }
  36. int pmd_huge(pmd_t pmd)
  37. {
  38. return 0;
  39. }
  40. int pud_huge(pud_t pud)
  41. {
  42. return 0;
  43. }
  44. struct page *
  45. follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  46. pmd_t *pmd, int write)
  47. {
  48. return NULL;
  49. }
  50. int pmd_huge_support(void)
  51. {
  52. return 0;
  53. }
  54. #else
  55. struct page *
  56. follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
  57. {
  58. return ERR_PTR(-EINVAL);
  59. }
  60. int pmd_huge(pmd_t pmd)
  61. {
  62. return !!(pmd_val(pmd) & _PAGE_PSE);
  63. }
  64. int pud_huge(pud_t pud)
  65. {
  66. return !!(pud_val(pud) & _PAGE_PSE);
  67. }
  68. int pmd_huge_support(void)
  69. {
  70. return 1;
  71. }
  72. #endif
  73. /* x86_64 also uses this file */
  74. #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
  75. static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
  76. unsigned long addr, unsigned long len,
  77. unsigned long pgoff, unsigned long flags)
  78. {
  79. struct hstate *h = hstate_file(file);
  80. struct vm_unmapped_area_info info;
  81. info.flags = 0;
  82. info.length = len;
  83. info.low_limit = TASK_UNMAPPED_BASE;
  84. info.high_limit = TASK_SIZE;
  85. info.align_mask = PAGE_MASK & ~huge_page_mask(h);
  86. info.align_offset = 0;
  87. return vm_unmapped_area(&info);
  88. }
  89. static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
  90. unsigned long addr0, unsigned long len,
  91. unsigned long pgoff, unsigned long flags)
  92. {
  93. struct hstate *h = hstate_file(file);
  94. struct vm_unmapped_area_info info;
  95. unsigned long addr;
  96. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  97. info.length = len;
  98. info.low_limit = PAGE_SIZE;
  99. info.high_limit = current->mm->mmap_base;
  100. info.align_mask = PAGE_MASK & ~huge_page_mask(h);
  101. info.align_offset = 0;
  102. addr = vm_unmapped_area(&info);
  103. /*
  104. * A failed mmap() very likely causes application failure,
  105. * so fall back to the bottom-up function here. This scenario
  106. * can happen with large stack limits and large mmap()
  107. * allocations.
  108. */
  109. if (addr & ~PAGE_MASK) {
  110. VM_BUG_ON(addr != -ENOMEM);
  111. info.flags = 0;
  112. info.low_limit = TASK_UNMAPPED_BASE;
  113. info.high_limit = TASK_SIZE;
  114. addr = vm_unmapped_area(&info);
  115. }
  116. return addr;
  117. }
  118. unsigned long
  119. hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  120. unsigned long len, unsigned long pgoff, unsigned long flags)
  121. {
  122. struct hstate *h = hstate_file(file);
  123. struct mm_struct *mm = current->mm;
  124. struct vm_area_struct *vma;
  125. if (len & ~huge_page_mask(h))
  126. return -EINVAL;
  127. if (len > TASK_SIZE)
  128. return -ENOMEM;
  129. if (flags & MAP_FIXED) {
  130. if (prepare_hugepage_range(file, addr, len))
  131. return -EINVAL;
  132. return addr;
  133. }
  134. if (addr) {
  135. addr = ALIGN(addr, huge_page_size(h));
  136. vma = find_vma(mm, addr);
  137. if (TASK_SIZE - len >= addr &&
  138. (!vma || addr + len <= vma->vm_start))
  139. return addr;
  140. }
  141. if (mm->get_unmapped_area == arch_get_unmapped_area)
  142. return hugetlb_get_unmapped_area_bottomup(file, addr, len,
  143. pgoff, flags);
  144. else
  145. return hugetlb_get_unmapped_area_topdown(file, addr, len,
  146. pgoff, flags);
  147. }
  148. #endif /*HAVE_ARCH_HUGETLB_UNMAPPED_AREA*/
  149. #ifdef CONFIG_X86_64
  150. static __init int setup_hugepagesz(char *opt)
  151. {
  152. unsigned long ps = memparse(opt, &opt);
  153. if (ps == PMD_SIZE) {
  154. hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
  155. } else if (ps == PUD_SIZE && cpu_has_gbpages) {
  156. hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
  157. } else {
  158. printk(KERN_ERR "hugepagesz: Unsupported page size %lu M\n",
  159. ps >> 20);
  160. return 0;
  161. }
  162. return 1;
  163. }
  164. __setup("hugepagesz=", setup_hugepagesz);
  165. #endif