blob: a0d023cb4292153a1e11e3826a2a33dfe0b59f72 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * IA-32 Huge TLB Page Support for Kernel.
4 *
5 * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
6 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/init.h>
9#include <linux/fs.h>
10#include <linux/mm.h>
Ingo Molnar01042602017-02-08 18:51:31 +010011#include <linux/sched/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/hugetlb.h>
13#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/err.h>
15#include <linux/sysctl.h>
Dmitry Safonove13b73d2017-03-14 14:41:26 +030016#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/mman.h>
18#include <asm/tlb.h>
19#include <asm/tlbflush.h>
Dmitry Safonove13b73d2017-03-14 14:41:26 +030020#include <asm/elf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#if 0 /* This is just for testing */
23struct page *
24follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
25{
26 unsigned long start = address;
27 int length = 1;
28 int nr;
29 struct page *page;
30 struct vm_area_struct *vma;
31
32 vma = find_vma(mm, addr);
33 if (!vma || !is_vm_hugetlb_page(vma))
34 return ERR_PTR(-EINVAL);
35
Punit Agrawal7868a202017-07-06 15:39:42 -070036 pte = huge_pte_offset(mm, address, vma_mmu_pagesize(vma));
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38 /* hugetlb should be locked, and hence, prefaulted */
39 WARN_ON(!pte || pte_none(*pte));
40
41 page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
42
Christoph Lameter25e59882008-03-26 21:03:04 -070043 WARN_ON(!PageHead(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45 return page;
46}
47
48int pmd_huge(pmd_t pmd)
49{
50 return 0;
51}
52
Andi Kleenceb86872008-07-23 21:27:50 -070053int pud_huge(pud_t pud)
54{
55 return 0;
56}
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#else
59
Naoya Horiguchicbef8472015-02-11 15:25:19 -080060/*
61 * pmd_huge() returns 1 if @pmd is hugetlb related entry, that is normal
62 * hugetlb entry or non-present (migration or hwpoisoned) hugetlb entry.
63 * Otherwise, returns 0.
64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065int pmd_huge(pmd_t pmd)
66{
Naoya Horiguchicbef8472015-02-11 15:25:19 -080067 return !pmd_none(pmd) &&
68 (pmd_val(pmd) & (_PAGE_PRESENT|_PAGE_PSE)) != _PAGE_PRESENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
Andi Kleenceb86872008-07-23 21:27:50 -070071int pud_huge(pud_t pud)
72{
Andi Kleen39c11e62008-07-23 21:27:50 -070073 return !!(pud_val(pud) & _PAGE_PSE);
Andi Kleenceb86872008-07-23 21:27:50 -070074}
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#endif
76
Kirill A. Shutemovfd8526a2013-11-19 15:17:50 +020077#ifdef CONFIG_HUGETLB_PAGE
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
79 unsigned long addr, unsigned long len,
80 unsigned long pgoff, unsigned long flags)
81{
Andi Kleen39c11e62008-07-23 21:27:50 -070082 struct hstate *h = hstate_file(file);
Michel Lespinassecdc17342012-12-11 16:02:02 -080083 struct vm_unmapped_area_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Michel Lespinassecdc17342012-12-11 16:02:02 -080085 info.flags = 0;
86 info.length = len;
Dmitry Safonove13b73d2017-03-14 14:41:26 +030087 info.low_limit = get_mmap_base(1);
Kirill A. Shutemovb569bab2017-07-17 01:59:52 +030088
89 /*
90 * If hint address is above DEFAULT_MAP_WINDOW, look for unmapped area
91 * in the full address space.
92 */
Dmitry Safonova8464462018-10-12 14:42:52 +010093 info.high_limit = in_32bit_syscall() ?
Kirill A. Shutemovb569bab2017-07-17 01:59:52 +030094 task_size_32bit() : task_size_64bit(addr > DEFAULT_MAP_WINDOW);
95
Michel Lespinassecdc17342012-12-11 16:02:02 -080096 info.align_mask = PAGE_MASK & ~huge_page_mask(h);
97 info.align_offset = 0;
98 return vm_unmapped_area(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
Kirill A. Shutemovb569bab2017-07-17 01:59:52 +0300102 unsigned long addr, unsigned long len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 unsigned long pgoff, unsigned long flags)
104{
Andi Kleen39c11e62008-07-23 21:27:50 -0700105 struct hstate *h = hstate_file(file);
Michel Lespinassecdc17342012-12-11 16:02:02 -0800106 struct vm_unmapped_area_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Michel Lespinassecdc17342012-12-11 16:02:02 -0800108 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
109 info.length = len;
110 info.low_limit = PAGE_SIZE;
Dmitry Safonove13b73d2017-03-14 14:41:26 +0300111 info.high_limit = get_mmap_base(0);
Kirill A. Shutemovb569bab2017-07-17 01:59:52 +0300112
113 /*
114 * If hint address is above DEFAULT_MAP_WINDOW, look for unmapped area
115 * in the full address space.
116 */
Dmitry Safonova8464462018-10-12 14:42:52 +0100117 if (addr > DEFAULT_MAP_WINDOW && !in_32bit_syscall())
Kirill A. Shutemovb569bab2017-07-17 01:59:52 +0300118 info.high_limit += TASK_SIZE_MAX - DEFAULT_MAP_WINDOW;
119
Michel Lespinassecdc17342012-12-11 16:02:02 -0800120 info.align_mask = PAGE_MASK & ~huge_page_mask(h);
121 info.align_offset = 0;
122 addr = vm_unmapped_area(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 /*
125 * A failed mmap() very likely causes application failure,
126 * so fall back to the bottom-up function here. This scenario
127 * can happen with large stack limits and large mmap()
128 * allocations.
129 */
Michel Lespinassecdc17342012-12-11 16:02:02 -0800130 if (addr & ~PAGE_MASK) {
131 VM_BUG_ON(addr != -ENOMEM);
132 info.flags = 0;
133 info.low_limit = TASK_UNMAPPED_BASE;
Kirill A. Shutemovb569bab2017-07-17 01:59:52 +0300134 info.high_limit = TASK_SIZE_LOW;
Michel Lespinassecdc17342012-12-11 16:02:02 -0800135 addr = vm_unmapped_area(&info);
136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 return addr;
139}
140
141unsigned long
142hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
143 unsigned long len, unsigned long pgoff, unsigned long flags)
144{
Andi Kleen39c11e62008-07-23 21:27:50 -0700145 struct hstate *h = hstate_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct mm_struct *mm = current->mm;
147 struct vm_area_struct *vma;
148
Andi Kleen39c11e62008-07-23 21:27:50 -0700149 if (len & ~huge_page_mask(h))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return -EINVAL;
Kirill A. Shutemov44b04912017-07-17 01:59:51 +0300151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (len > TASK_SIZE)
153 return -ENOMEM;
154
Kirill A. Shutemov1e0f25d2017-11-15 17:36:06 +0300155 /* No address checking. See comment at mmap_address_hint_valid() */
Benjamin Herrenschmidt5a8130f2007-05-06 14:50:08 -0700156 if (flags & MAP_FIXED) {
Andi Kleena5516432008-07-23 21:27:41 -0700157 if (prepare_hugepage_range(file, addr, len))
Benjamin Herrenschmidt5a8130f2007-05-06 14:50:08 -0700158 return -EINVAL;
159 return addr;
160 }
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (addr) {
Kirill A. Shutemov1e0f25d2017-11-15 17:36:06 +0300163 addr &= huge_page_mask(h);
164 if (!mmap_address_hint_valid(addr, len))
165 goto get_unmapped_area;
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 vma = find_vma(mm, addr);
Kirill A. Shutemov1e0f25d2017-11-15 17:36:06 +0300168 if (!vma || addr + len <= vm_start_gap(vma))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return addr;
170 }
Kirill A. Shutemov1e0f25d2017-11-15 17:36:06 +0300171
172get_unmapped_area:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (mm->get_unmapped_area == arch_get_unmapped_area)
174 return hugetlb_get_unmapped_area_bottomup(file, addr, len,
175 pgoff, flags);
176 else
177 return hugetlb_get_unmapped_area_topdown(file, addr, len,
178 pgoff, flags);
179}
Kirill A. Shutemovfd8526a2013-11-19 15:17:50 +0200180#endif /* CONFIG_HUGETLB_PAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Andi Kleenb4718e62008-07-23 21:27:51 -0700182#ifdef CONFIG_X86_64
Mike Kravetzae94da82020-06-03 16:00:34 -0700183bool __init arch_hugetlb_valid_size(unsigned long size)
184{
185 if (size == PMD_SIZE)
186 return true;
187 else if (size == PUD_SIZE && boot_cpu_has(X86_FEATURE_GBPAGES))
188 return true;
189 else
190 return false;
191}
192
Alexandre Ghiti8df995f2019-05-13 17:19:00 -0700193#ifdef CONFIG_CONTIG_ALLOC
Kirill A. Shutemovece84b32015-02-10 14:08:19 -0800194static __init int gigantic_pages_init(void)
195{
Vlastimil Babka080fe202016-02-05 15:36:41 -0800196 /* With compaction or CMA we can allocate gigantic pages at runtime */
Mike Kravetz38237832020-06-03 16:00:42 -0700197 if (boot_cpu_has(X86_FEATURE_GBPAGES))
Kirill A. Shutemovece84b32015-02-10 14:08:19 -0800198 hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
199 return 0;
200}
201arch_initcall(gigantic_pages_init);
202#endif
Andi Kleenb4718e62008-07-23 21:27:51 -0700203#endif