hugetlbpage.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #ifdef CONFIG_HUGETLB_PAGE
  74. static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
  75. unsigned long addr, unsigned long len,
  76. unsigned long pgoff, unsigned long flags)
  77. {
  78. struct hstate *h = hstate_file(file);
  79. struct vm_unmapped_area_info info;
  80. info.flags = 0;
  81. info.length = len;
  82. info.low_limit = current->mm->mmap_legacy_base;
  83. info.high_limit = TASK_SIZE;
  84. info.align_mask = PAGE_MASK & ~huge_page_mask(h);
  85. info.align_offset = 0;
  86. return vm_unmapped_area(&info);
  87. }
  88. static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
  89. unsigned long addr0, unsigned long len,
  90. unsigned long pgoff, unsigned long flags)
  91. {
  92. struct hstate *h = hstate_file(file);
  93. struct vm_unmapped_area_info info;
  94. unsigned long addr;
  95. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  96. info.length = len;
  97. info.low_limit = PAGE_SIZE;
  98. info.high_limit = current->mm->mmap_base;
  99. info.align_mask = PAGE_MASK & ~huge_page_mask(h);
  100. info.align_offset = 0;
  101. addr = vm_unmapped_area(&info);
  102. /*
  103. * A failed mmap() very likely causes application failure,
  104. * so fall back to the bottom-up function here. This scenario
  105. * can happen with large stack limits and large mmap()
  106. * allocations.
  107. */
  108. if (addr & ~PAGE_MASK) {
  109. VM_BUG_ON(addr != -ENOMEM);
  110. info.flags = 0;
  111. info.low_limit = TASK_UNMAPPED_BASE;
  112. info.high_limit = TASK_SIZE;
  113. addr = vm_unmapped_area(&info);
  114. }
  115. return addr;
  116. }
  117. unsigned long
  118. hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  119. unsigned long len, unsigned long pgoff, unsigned long flags)
  120. {
  121. struct hstate *h = hstate_file(file);
  122. struct mm_struct *mm = current->mm;
  123. struct vm_area_struct *vma;
  124. if (len & ~huge_page_mask(h))
  125. return -EINVAL;
  126. if (len > TASK_SIZE)
  127. return -ENOMEM;
  128. if (flags & MAP_FIXED) {
  129. if (prepare_hugepage_range(file, addr, len))
  130. return -EINVAL;
  131. return addr;
  132. }
  133. if (addr) {
  134. addr = ALIGN(addr, huge_page_size(h));
  135. vma = find_vma(mm, addr);
  136. if (TASK_SIZE - len >= addr &&
  137. (!vma || addr + len <= vma->vm_start))
  138. return addr;
  139. }
  140. if (mm->get_unmapped_area == arch_get_unmapped_area)
  141. return hugetlb_get_unmapped_area_bottomup(file, addr, len,
  142. pgoff, flags);
  143. else
  144. return hugetlb_get_unmapped_area_topdown(file, addr, len,
  145. pgoff, flags);
  146. }
  147. #endif /* CONFIG_HUGETLB_PAGE */
  148. #ifdef CONFIG_X86_64
  149. static __init int setup_hugepagesz(char *opt)
  150. {
  151. unsigned long ps = memparse(opt, &opt);
  152. if (ps == PMD_SIZE) {
  153. hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
  154. } else if (ps == PUD_SIZE && cpu_has_gbpages) {
  155. hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
  156. } else {
  157. printk(KERN_ERR "hugepagesz: Unsupported page size %lu M\n",
  158. ps >> 20);
  159. return 0;
  160. }
  161. return 1;
  162. }
  163. __setup("hugepagesz=", setup_hugepagesz);
  164. #endif