1 // SPDX-License-Identifier: GPL-2.0
3 * IA-32 Huge TLB Page Support for Kernel.
5 * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
8 #include <linux/init.h>
11 #include <linux/sched/mm.h>
12 #include <linux/hugetlb.h>
13 #include <linux/pagemap.h>
14 #include <linux/err.h>
15 #include <linux/sysctl.h>
16 #include <linux/compat.h>
19 #include <asm/tlbflush.h>
23 * pmd_huge() returns 1 if @pmd is hugetlb related entry, that is normal
24 * hugetlb entry or non-present (migration or hwpoisoned) hugetlb entry.
25 * Otherwise, returns 0.
27 int pmd_huge(pmd_t pmd)
29 return !pmd_none(pmd) &&
30 (pmd_val(pmd) & (_PAGE_PRESENT|_PAGE_PSE)) != _PAGE_PRESENT;
34 * pud_huge() returns 1 if @pud is hugetlb related entry, that is normal
35 * hugetlb entry or non-present (migration or hwpoisoned) hugetlb entry.
36 * Otherwise, returns 0.
38 int pud_huge(pud_t pud)
40 return !pud_none(pud) &&
41 (pud_val(pud) & (_PAGE_PRESENT|_PAGE_PSE)) != _PAGE_PRESENT;
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)
49 struct hstate *h = hstate_file(file);
50 struct vm_unmapped_area_info info;
54 info.low_limit = get_mmap_base(1);
57 * If hint address is above DEFAULT_MAP_WINDOW, look for unmapped area
58 * in the full address space.
60 info.high_limit = in_32bit_syscall() ?
61 task_size_32bit() : task_size_64bit(addr > DEFAULT_MAP_WINDOW);
63 info.align_mask = PAGE_MASK & ~huge_page_mask(h);
64 info.align_offset = 0;
65 return vm_unmapped_area(&info);
68 static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
69 unsigned long addr, unsigned long len,
70 unsigned long pgoff, unsigned long flags)
72 struct hstate *h = hstate_file(file);
73 struct vm_unmapped_area_info info;
75 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
77 info.low_limit = PAGE_SIZE;
78 info.high_limit = get_mmap_base(0);
81 * If hint address is above DEFAULT_MAP_WINDOW, look for unmapped area
82 * in the full address space.
84 if (addr > DEFAULT_MAP_WINDOW && !in_32bit_syscall())
85 info.high_limit += TASK_SIZE_MAX - DEFAULT_MAP_WINDOW;
87 info.align_mask = PAGE_MASK & ~huge_page_mask(h);
88 info.align_offset = 0;
89 addr = vm_unmapped_area(&info);
92 * A failed mmap() very likely causes application failure,
93 * so fall back to the bottom-up function here. This scenario
94 * can happen with large stack limits and large mmap()
97 if (addr & ~PAGE_MASK) {
98 VM_BUG_ON(addr != -ENOMEM);
100 info.low_limit = TASK_UNMAPPED_BASE;
101 info.high_limit = TASK_SIZE_LOW;
102 addr = vm_unmapped_area(&info);
109 hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
110 unsigned long len, unsigned long pgoff, unsigned long flags)
112 struct hstate *h = hstate_file(file);
113 struct mm_struct *mm = current->mm;
114 struct vm_area_struct *vma;
116 if (len & ~huge_page_mask(h))
122 /* No address checking. See comment at mmap_address_hint_valid() */
123 if (flags & MAP_FIXED) {
124 if (prepare_hugepage_range(file, addr, len))
130 addr &= huge_page_mask(h);
131 if (!mmap_address_hint_valid(addr, len))
132 goto get_unmapped_area;
134 vma = find_vma(mm, addr);
135 if (!vma || addr + len <= vm_start_gap(vma))
140 if (mm->get_unmapped_area == arch_get_unmapped_area)
141 return hugetlb_get_unmapped_area_bottomup(file, addr, len,
144 return hugetlb_get_unmapped_area_topdown(file, addr, len,
147 #endif /* CONFIG_HUGETLB_PAGE */
150 bool __init arch_hugetlb_valid_size(unsigned long size)
152 if (size == PMD_SIZE)
154 else if (size == PUD_SIZE && boot_cpu_has(X86_FEATURE_GBPAGES))
160 #ifdef CONFIG_CONTIG_ALLOC
161 static __init int gigantic_pages_init(void)
163 /* With compaction or CMA we can allocate gigantic pages at runtime */
164 if (boot_cpu_has(X86_FEATURE_GBPAGES))
165 hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
168 arch_initcall(gigantic_pages_init);