hugetlbpage.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * IA-32 Huge TLB Page Support for Kernel.
  4. *
  5. * Copyright (C) 2002, Rohit Seth <[email protected]>
  6. */
  7. #include <linux/init.h>
  8. #include <linux/fs.h>
  9. #include <linux/mm.h>
  10. #include <linux/sched/mm.h>
  11. #include <linux/hugetlb.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/err.h>
  14. #include <linux/sysctl.h>
  15. #include <linux/compat.h>
  16. #include <asm/mman.h>
  17. #include <asm/tlb.h>
  18. #include <asm/tlbflush.h>
  19. #include <asm/elf.h>
  20. /*
  21. * pmd_huge() returns 1 if @pmd is hugetlb related entry, that is normal
  22. * hugetlb entry or non-present (migration or hwpoisoned) hugetlb entry.
  23. * Otherwise, returns 0.
  24. */
  25. int pmd_huge(pmd_t pmd)
  26. {
  27. return !pmd_none(pmd) &&
  28. (pmd_val(pmd) & (_PAGE_PRESENT|_PAGE_PSE)) != _PAGE_PRESENT;
  29. }
  30. /*
  31. * pud_huge() returns 1 if @pud is hugetlb related entry, that is normal
  32. * hugetlb entry or non-present (migration or hwpoisoned) hugetlb entry.
  33. * Otherwise, returns 0.
  34. */
  35. int pud_huge(pud_t pud)
  36. {
  37. #if CONFIG_PGTABLE_LEVELS > 2
  38. return !pud_none(pud) &&
  39. (pud_val(pud) & (_PAGE_PRESENT|_PAGE_PSE)) != _PAGE_PRESENT;
  40. #else
  41. return 0;
  42. #endif
  43. }
  44. #ifdef CONFIG_HUGETLB_PAGE
  45. static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
  46. unsigned long addr, unsigned long len,
  47. unsigned long pgoff, unsigned long flags)
  48. {
  49. struct hstate *h = hstate_file(file);
  50. struct vm_unmapped_area_info info;
  51. info.flags = 0;
  52. info.length = len;
  53. info.low_limit = get_mmap_base(1);
  54. /*
  55. * If hint address is above DEFAULT_MAP_WINDOW, look for unmapped area
  56. * in the full address space.
  57. */
  58. info.high_limit = in_32bit_syscall() ?
  59. task_size_32bit() : task_size_64bit(addr > DEFAULT_MAP_WINDOW);
  60. info.align_mask = PAGE_MASK & ~huge_page_mask(h);
  61. info.align_offset = 0;
  62. return vm_unmapped_area(&info);
  63. }
  64. static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
  65. unsigned long addr, unsigned long len,
  66. unsigned long pgoff, unsigned long flags)
  67. {
  68. struct hstate *h = hstate_file(file);
  69. struct vm_unmapped_area_info info;
  70. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  71. info.length = len;
  72. info.low_limit = PAGE_SIZE;
  73. info.high_limit = get_mmap_base(0);
  74. /*
  75. * If hint address is above DEFAULT_MAP_WINDOW, look for unmapped area
  76. * in the full address space.
  77. */
  78. if (addr > DEFAULT_MAP_WINDOW && !in_32bit_syscall())
  79. info.high_limit += TASK_SIZE_MAX - DEFAULT_MAP_WINDOW;
  80. info.align_mask = PAGE_MASK & ~huge_page_mask(h);
  81. info.align_offset = 0;
  82. addr = vm_unmapped_area(&info);
  83. /*
  84. * A failed mmap() very likely causes application failure,
  85. * so fall back to the bottom-up function here. This scenario
  86. * can happen with large stack limits and large mmap()
  87. * allocations.
  88. */
  89. if (addr & ~PAGE_MASK) {
  90. VM_BUG_ON(addr != -ENOMEM);
  91. info.flags = 0;
  92. info.low_limit = TASK_UNMAPPED_BASE;
  93. info.high_limit = TASK_SIZE_LOW;
  94. addr = vm_unmapped_area(&info);
  95. }
  96. return addr;
  97. }
  98. unsigned long
  99. hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  100. unsigned long len, unsigned long pgoff, unsigned long flags)
  101. {
  102. struct hstate *h = hstate_file(file);
  103. struct mm_struct *mm = current->mm;
  104. struct vm_area_struct *vma;
  105. if (len & ~huge_page_mask(h))
  106. return -EINVAL;
  107. if (len > TASK_SIZE)
  108. return -ENOMEM;
  109. /* No address checking. See comment at mmap_address_hint_valid() */
  110. if (flags & MAP_FIXED) {
  111. if (prepare_hugepage_range(file, addr, len))
  112. return -EINVAL;
  113. return addr;
  114. }
  115. if (addr) {
  116. addr &= huge_page_mask(h);
  117. if (!mmap_address_hint_valid(addr, len))
  118. goto get_unmapped_area;
  119. vma = find_vma(mm, addr);
  120. if (!vma || addr + len <= vm_start_gap(vma))
  121. return addr;
  122. }
  123. get_unmapped_area:
  124. if (mm->get_unmapped_area == arch_get_unmapped_area)
  125. return hugetlb_get_unmapped_area_bottomup(file, addr, len,
  126. pgoff, flags);
  127. else
  128. return hugetlb_get_unmapped_area_topdown(file, addr, len,
  129. pgoff, flags);
  130. }
  131. #endif /* CONFIG_HUGETLB_PAGE */
  132. #ifdef CONFIG_X86_64
  133. bool __init arch_hugetlb_valid_size(unsigned long size)
  134. {
  135. if (size == PMD_SIZE)
  136. return true;
  137. else if (size == PUD_SIZE && boot_cpu_has(X86_FEATURE_GBPAGES))
  138. return true;
  139. else
  140. return false;
  141. }
  142. #ifdef CONFIG_CONTIG_ALLOC
  143. static __init int gigantic_pages_init(void)
  144. {
  145. /* With compaction or CMA we can allocate gigantic pages at runtime */
  146. if (boot_cpu_has(X86_FEATURE_GBPAGES))
  147. hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
  148. return 0;
  149. }
  150. arch_initcall(gigantic_pages_init);
  151. #endif
  152. #endif