hugetlbpage.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #else
  45. /*
  46. * pmd_huge() returns 1 if @pmd is hugetlb related entry, that is normal
  47. * hugetlb entry or non-present (migration or hwpoisoned) hugetlb entry.
  48. * Otherwise, returns 0.
  49. */
  50. int pmd_huge(pmd_t pmd)
  51. {
  52. return !pmd_none(pmd) &&
  53. (pmd_val(pmd) & (_PAGE_PRESENT|_PAGE_PSE)) != _PAGE_PRESENT;
  54. }
  55. int pud_huge(pud_t pud)
  56. {
  57. return !!(pud_val(pud) & _PAGE_PSE);
  58. }
  59. #endif
  60. #ifdef CONFIG_HUGETLB_PAGE
  61. static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
  62. unsigned long addr, unsigned long len,
  63. unsigned long pgoff, unsigned long flags)
  64. {
  65. struct hstate *h = hstate_file(file);
  66. struct vm_unmapped_area_info info;
  67. info.flags = 0;
  68. info.length = len;
  69. info.low_limit = current->mm->mmap_legacy_base;
  70. info.high_limit = TASK_SIZE;
  71. info.align_mask = PAGE_MASK & ~huge_page_mask(h);
  72. info.align_offset = 0;
  73. return vm_unmapped_area(&info);
  74. }
  75. static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
  76. unsigned long addr0, 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. unsigned long addr;
  82. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  83. info.length = len;
  84. info.low_limit = PAGE_SIZE;
  85. info.high_limit = current->mm->mmap_base;
  86. info.align_mask = PAGE_MASK & ~huge_page_mask(h);
  87. info.align_offset = 0;
  88. addr = vm_unmapped_area(&info);
  89. /*
  90. * A failed mmap() very likely causes application failure,
  91. * so fall back to the bottom-up function here. This scenario
  92. * can happen with large stack limits and large mmap()
  93. * allocations.
  94. */
  95. if (addr & ~PAGE_MASK) {
  96. VM_BUG_ON(addr != -ENOMEM);
  97. info.flags = 0;
  98. info.low_limit = TASK_UNMAPPED_BASE;
  99. info.high_limit = TASK_SIZE;
  100. addr = vm_unmapped_area(&info);
  101. }
  102. return addr;
  103. }
  104. unsigned long
  105. hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  106. unsigned long len, unsigned long pgoff, unsigned long flags)
  107. {
  108. struct hstate *h = hstate_file(file);
  109. struct mm_struct *mm = current->mm;
  110. struct vm_area_struct *vma;
  111. if (len & ~huge_page_mask(h))
  112. return -EINVAL;
  113. if (len > TASK_SIZE)
  114. return -ENOMEM;
  115. if (flags & MAP_FIXED) {
  116. if (prepare_hugepage_range(file, addr, len))
  117. return -EINVAL;
  118. return addr;
  119. }
  120. if (addr) {
  121. addr = ALIGN(addr, huge_page_size(h));
  122. vma = find_vma(mm, addr);
  123. if (TASK_SIZE - len >= addr &&
  124. (!vma || addr + len <= vm_start_gap(vma)))
  125. return addr;
  126. }
  127. if (mm->get_unmapped_area == arch_get_unmapped_area)
  128. return hugetlb_get_unmapped_area_bottomup(file, addr, len,
  129. pgoff, flags);
  130. else
  131. return hugetlb_get_unmapped_area_topdown(file, addr, len,
  132. pgoff, flags);
  133. }
  134. #endif /* CONFIG_HUGETLB_PAGE */
  135. #ifdef CONFIG_X86_64
  136. static __init int setup_hugepagesz(char *opt)
  137. {
  138. unsigned long ps = memparse(opt, &opt);
  139. if (ps == PMD_SIZE) {
  140. hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
  141. } else if (ps == PUD_SIZE && boot_cpu_has(X86_FEATURE_GBPAGES)) {
  142. hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
  143. } else {
  144. hugetlb_bad_size();
  145. printk(KERN_ERR "hugepagesz: Unsupported page size %lu M\n",
  146. ps >> 20);
  147. return 0;
  148. }
  149. return 1;
  150. }
  151. __setup("hugepagesz=", setup_hugepagesz);
  152. #if (defined(CONFIG_MEMORY_ISOLATION) && defined(CONFIG_COMPACTION)) || defined(CONFIG_CMA)
  153. static __init int gigantic_pages_init(void)
  154. {
  155. /* With compaction or CMA we can allocate gigantic pages at runtime */
  156. if (boot_cpu_has(X86_FEATURE_GBPAGES) && !size_to_hstate(1UL << PUD_SHIFT))
  157. hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
  158. return 0;
  159. }
  160. arch_initcall(gigantic_pages_init);
  161. #endif
  162. #endif