blob: 0986d426a413bcb292ffb3452a2d8f66b32f800d [file] [log] [blame]
Chris Metcalf867e3592010-05-28 23:09:12 -04001/*
2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
12 * more details.
13 *
14 * TILE Huge TLB Page Support for Kernel.
15 * Taken from i386 hugetlb implementation:
16 * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
17 */
18
19#include <linux/init.h>
20#include <linux/fs.h>
21#include <linux/mm.h>
Ingo Molnar01042602017-02-08 18:51:31 +010022#include <linux/sched/mm.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040023#include <linux/hugetlb.h>
24#include <linux/pagemap.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040025#include <linux/slab.h>
26#include <linux/err.h>
27#include <linux/sysctl.h>
28#include <linux/mman.h>
29#include <asm/tlb.h>
30#include <asm/tlbflush.h>
Chris Metcalf621b1952012-04-01 14:04:21 -040031#include <asm/setup.h>
32
33#ifdef CONFIG_HUGETLB_SUPER_PAGES
34
35/*
36 * Provide an additional huge page size (in addition to the regular default
37 * huge page size) if no "hugepagesz" arguments are specified.
38 * Note that it must be smaller than the default huge page size so
39 * that it's possible to allocate them on demand from the buddy allocator.
40 * You can change this to 64K (on a 16K build), 256K, 1M, or 4M,
41 * or not define it at all.
42 */
43#define ADDITIONAL_HUGE_SIZE (1024 * 1024UL)
44
45/* "Extra" page-size multipliers, one per level of the page table. */
46int huge_shift[HUGE_SHIFT_ENTRIES] = {
47#ifdef ADDITIONAL_HUGE_SIZE
48#define ADDITIONAL_HUGE_SHIFT __builtin_ctzl(ADDITIONAL_HUGE_SIZE / PAGE_SIZE)
49 [HUGE_SHIFT_PAGE] = ADDITIONAL_HUGE_SHIFT
50#endif
51};
52
Chris Metcalf621b1952012-04-01 14:04:21 -040053#endif
Chris Metcalf867e3592010-05-28 23:09:12 -040054
55pte_t *huge_pte_alloc(struct mm_struct *mm,
56 unsigned long addr, unsigned long sz)
57{
58 pgd_t *pgd;
59 pud_t *pud;
Chris Metcalf867e3592010-05-28 23:09:12 -040060
Chris Metcalf621b1952012-04-01 14:04:21 -040061 addr &= -sz; /* Mask off any low bits in the address. */
Chris Metcalf867e3592010-05-28 23:09:12 -040062
63 pgd = pgd_offset(mm, addr);
64 pud = pud_alloc(mm, pgd, addr);
Chris Metcalf867e3592010-05-28 23:09:12 -040065
Chris Metcalf621b1952012-04-01 14:04:21 -040066#ifdef CONFIG_HUGETLB_SUPER_PAGES
67 if (sz >= PGDIR_SIZE) {
68 BUG_ON(sz != PGDIR_SIZE &&
69 sz != PGDIR_SIZE << huge_shift[HUGE_SHIFT_PGDIR]);
70 return (pte_t *)pud;
71 } else {
72 pmd_t *pmd = pmd_alloc(mm, pud, addr);
73 if (sz >= PMD_SIZE) {
74 BUG_ON(sz != PMD_SIZE &&
75 sz != (PMD_SIZE << huge_shift[HUGE_SHIFT_PMD]));
76 return (pte_t *)pmd;
77 }
78 else {
79 if (sz != PAGE_SIZE << huge_shift[HUGE_SHIFT_PAGE])
80 panic("Unexpected page size %#lx\n", sz);
Kirill A. Shutemov3ed3a4f2016-03-17 14:19:11 -070081 return pte_alloc_map(mm, pmd, addr);
Chris Metcalf621b1952012-04-01 14:04:21 -040082 }
83 }
84#else
85 BUG_ON(sz != PMD_SIZE);
86 return (pte_t *) pmd_alloc(mm, pud, addr);
87#endif
88}
89
90static pte_t *get_pte(pte_t *base, int index, int level)
91{
92 pte_t *ptep = base + index;
93#ifdef CONFIG_HUGETLB_SUPER_PAGES
94 if (!pte_present(*ptep) && huge_shift[level] != 0) {
95 unsigned long mask = -1UL << huge_shift[level];
96 pte_t *super_ptep = base + (index & mask);
97 pte_t pte = *super_ptep;
98 if (pte_present(pte) && pte_super(pte))
99 ptep = super_ptep;
100 }
101#endif
102 return ptep;
Chris Metcalf867e3592010-05-28 23:09:12 -0400103}
104
Punit Agrawal7868a202017-07-06 15:39:42 -0700105pte_t *huge_pte_offset(struct mm_struct *mm,
106 unsigned long addr, unsigned long sz)
Chris Metcalf867e3592010-05-28 23:09:12 -0400107{
108 pgd_t *pgd;
109 pud_t *pud;
Chris Metcalf621b1952012-04-01 14:04:21 -0400110 pmd_t *pmd;
111#ifdef CONFIG_HUGETLB_SUPER_PAGES
112 pte_t *pte;
113#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400114
Chris Metcalf621b1952012-04-01 14:04:21 -0400115 /* Get the top-level page table entry. */
116 pgd = (pgd_t *)get_pte((pte_t *)mm->pgd, pgd_index(addr), 0);
Chris Metcalf867e3592010-05-28 23:09:12 -0400117
Chris Metcalf621b1952012-04-01 14:04:21 -0400118 /* We don't have four levels. */
119 pud = pud_offset(pgd, addr);
120#ifndef __PAGETABLE_PUD_FOLDED
121# error support fourth page table level
122#endif
Chris Metcalfa0bd12d2013-08-07 11:00:45 -0400123 if (!pud_present(*pud))
124 return NULL;
Chris Metcalf867e3592010-05-28 23:09:12 -0400125
Chris Metcalf621b1952012-04-01 14:04:21 -0400126 /* Check for an L0 huge PTE, if we have three levels. */
127#ifndef __PAGETABLE_PMD_FOLDED
128 if (pud_huge(*pud))
129 return (pte_t *)pud;
Chris Metcalf867e3592010-05-28 23:09:12 -0400130
Chris Metcalf621b1952012-04-01 14:04:21 -0400131 pmd = (pmd_t *)get_pte((pte_t *)pud_page_vaddr(*pud),
132 pmd_index(addr), 1);
133 if (!pmd_present(*pmd))
134 return NULL;
135#else
136 pmd = pmd_offset(pud, addr);
137#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400138
Chris Metcalf621b1952012-04-01 14:04:21 -0400139 /* Check for an L1 huge PTE. */
140 if (pmd_huge(*pmd))
141 return (pte_t *)pmd;
Chris Metcalf867e3592010-05-28 23:09:12 -0400142
Chris Metcalf621b1952012-04-01 14:04:21 -0400143#ifdef CONFIG_HUGETLB_SUPER_PAGES
144 /* Check for an L2 huge PTE. */
145 pte = get_pte((pte_t *)pmd_page_vaddr(*pmd), pte_index(addr), 2);
146 if (!pte_present(*pte))
147 return NULL;
148 if (pte_super(*pte))
149 return pte;
150#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400151
Chris Metcalf867e3592010-05-28 23:09:12 -0400152 return NULL;
153}
154
Chris Metcalf867e3592010-05-28 23:09:12 -0400155int pmd_huge(pmd_t pmd)
156{
157 return !!(pmd_val(pmd) & _PAGE_HUGE_PAGE);
158}
159
160int pud_huge(pud_t pud)
161{
162 return !!(pud_val(pud) & _PAGE_HUGE_PAGE);
163}
164
Chris Metcalf867e3592010-05-28 23:09:12 -0400165#ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
166static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
167 unsigned long addr, unsigned long len,
168 unsigned long pgoff, unsigned long flags)
169{
170 struct hstate *h = hstate_file(file);
Michel Lespinassedd529592012-12-11 16:02:17 -0800171 struct vm_unmapped_area_info info;
Chris Metcalf867e3592010-05-28 23:09:12 -0400172
Michel Lespinassedd529592012-12-11 16:02:17 -0800173 info.flags = 0;
174 info.length = len;
175 info.low_limit = TASK_UNMAPPED_BASE;
176 info.high_limit = TASK_SIZE;
177 info.align_mask = PAGE_MASK & ~huge_page_mask(h);
178 info.align_offset = 0;
179 return vm_unmapped_area(&info);
Chris Metcalf867e3592010-05-28 23:09:12 -0400180}
181
182static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
183 unsigned long addr0, unsigned long len,
184 unsigned long pgoff, unsigned long flags)
185{
186 struct hstate *h = hstate_file(file);
Michel Lespinassedd529592012-12-11 16:02:17 -0800187 struct vm_unmapped_area_info info;
188 unsigned long addr;
Chris Metcalf867e3592010-05-28 23:09:12 -0400189
Michel Lespinassedd529592012-12-11 16:02:17 -0800190 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
191 info.length = len;
192 info.low_limit = PAGE_SIZE;
193 info.high_limit = current->mm->mmap_base;
194 info.align_mask = PAGE_MASK & ~huge_page_mask(h);
195 info.align_offset = 0;
196 addr = vm_unmapped_area(&info);
Chris Metcalf867e3592010-05-28 23:09:12 -0400197
Chris Metcalf867e3592010-05-28 23:09:12 -0400198 /*
199 * A failed mmap() very likely causes application failure,
200 * so fall back to the bottom-up function here. This scenario
201 * can happen with large stack limits and large mmap()
202 * allocations.
203 */
Michel Lespinassedd529592012-12-11 16:02:17 -0800204 if (addr & ~PAGE_MASK) {
205 VM_BUG_ON(addr != -ENOMEM);
206 info.flags = 0;
207 info.low_limit = TASK_UNMAPPED_BASE;
208 info.high_limit = TASK_SIZE;
209 addr = vm_unmapped_area(&info);
210 }
Chris Metcalf867e3592010-05-28 23:09:12 -0400211
212 return addr;
213}
214
215unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
216 unsigned long len, unsigned long pgoff, unsigned long flags)
217{
218 struct hstate *h = hstate_file(file);
219 struct mm_struct *mm = current->mm;
220 struct vm_area_struct *vma;
221
222 if (len & ~huge_page_mask(h))
223 return -EINVAL;
224 if (len > TASK_SIZE)
225 return -ENOMEM;
226
227 if (flags & MAP_FIXED) {
228 if (prepare_hugepage_range(file, addr, len))
229 return -EINVAL;
230 return addr;
231 }
232
233 if (addr) {
234 addr = ALIGN(addr, huge_page_size(h));
235 vma = find_vma(mm, addr);
236 if (TASK_SIZE - len >= addr &&
Hugh Dickins1be71072017-06-19 04:03:24 -0700237 (!vma || addr + len <= vm_start_gap(vma)))
Chris Metcalf867e3592010-05-28 23:09:12 -0400238 return addr;
239 }
240 if (current->mm->get_unmapped_area == arch_get_unmapped_area)
241 return hugetlb_get_unmapped_area_bottomup(file, addr, len,
242 pgoff, flags);
243 else
244 return hugetlb_get_unmapped_area_topdown(file, addr, len,
245 pgoff, flags);
246}
Chris Metcalf621b1952012-04-01 14:04:21 -0400247#endif /* HAVE_ARCH_HUGETLB_UNMAPPED_AREA */
248
249#ifdef CONFIG_HUGETLB_SUPER_PAGES
250static __init int __setup_hugepagesz(unsigned long ps)
251{
252 int log_ps = __builtin_ctzl(ps);
253 int level, base_shift;
254
255 if ((1UL << log_ps) != ps || (log_ps & 1) != 0) {
Joe Perchesf4743672014-10-31 10:50:46 -0700256 pr_warn("Not enabling %ld byte huge pages; must be a power of four\n",
257 ps);
Chris Metcalf621b1952012-04-01 14:04:21 -0400258 return -EINVAL;
259 }
260
261 if (ps > 64*1024*1024*1024UL) {
Joe Perchesf4743672014-10-31 10:50:46 -0700262 pr_warn("Not enabling %ld MB huge pages; largest legal value is 64 GB\n",
263 ps >> 20);
Chris Metcalf621b1952012-04-01 14:04:21 -0400264 return -EINVAL;
265 } else if (ps >= PUD_SIZE) {
266 static long hv_jpage_size;
267 if (hv_jpage_size == 0)
268 hv_jpage_size = hv_sysconf(HV_SYSCONF_PAGE_SIZE_JUMBO);
269 if (hv_jpage_size != PUD_SIZE) {
Joe Perchesf4743672014-10-31 10:50:46 -0700270 pr_warn("Not enabling >= %ld MB huge pages: hypervisor reports size %ld\n",
Chris Metcalf621b1952012-04-01 14:04:21 -0400271 PUD_SIZE >> 20, hv_jpage_size);
272 return -EINVAL;
273 }
274 level = 0;
275 base_shift = PUD_SHIFT;
276 } else if (ps >= PMD_SIZE) {
277 level = 1;
278 base_shift = PMD_SHIFT;
279 } else if (ps > PAGE_SIZE) {
280 level = 2;
281 base_shift = PAGE_SHIFT;
282 } else {
283 pr_err("hugepagesz: huge page size %ld too small\n", ps);
284 return -EINVAL;
285 }
286
287 if (log_ps != base_shift) {
288 int shift_val = log_ps - base_shift;
289 if (huge_shift[level] != 0) {
290 int old_shift = base_shift + huge_shift[level];
Joe Perchesf4743672014-10-31 10:50:46 -0700291 pr_warn("Not enabling %ld MB huge pages; already have size %ld MB\n",
Chris Metcalf621b1952012-04-01 14:04:21 -0400292 ps >> 20, (1UL << old_shift) >> 20);
293 return -EINVAL;
294 }
295 if (hv_set_pte_super_shift(level, shift_val) != 0) {
Joe Perchesf4743672014-10-31 10:50:46 -0700296 pr_warn("Not enabling %ld MB huge pages; no hypervisor support\n",
297 ps >> 20);
Chris Metcalf621b1952012-04-01 14:04:21 -0400298 return -EINVAL;
299 }
300 printk(KERN_DEBUG "Enabled %ld MB huge pages\n", ps >> 20);
301 huge_shift[level] = shift_val;
302 }
303
304 hugetlb_add_hstate(log_ps - PAGE_SHIFT);
305
306 return 0;
307}
308
309static bool saw_hugepagesz;
Chris Metcalf867e3592010-05-28 23:09:12 -0400310
311static __init int setup_hugepagesz(char *opt)
312{
Vaishali Thakkarb3d424f2016-05-19 17:11:17 -0700313 int rc;
314
Chris Metcalf621b1952012-04-01 14:04:21 -0400315 if (!saw_hugepagesz) {
316 saw_hugepagesz = true;
317 memset(huge_shift, 0, sizeof(huge_shift));
Chris Metcalf867e3592010-05-28 23:09:12 -0400318 }
Vaishali Thakkarb3d424f2016-05-19 17:11:17 -0700319 rc = __setup_hugepagesz(memparse(opt, NULL));
320 if (rc)
321 hugetlb_bad_size();
322 return rc;
Chris Metcalf867e3592010-05-28 23:09:12 -0400323}
324__setup("hugepagesz=", setup_hugepagesz);
325
Chris Metcalf621b1952012-04-01 14:04:21 -0400326#ifdef ADDITIONAL_HUGE_SIZE
327/*
328 * Provide an additional huge page size if no "hugepagesz" args are given.
329 * In that case, all the cores have properly set up their hv super_shift
330 * already, but we need to notify the hugetlb code to enable the
331 * new huge page size from the Linux point of view.
332 */
333static __init int add_default_hugepagesz(void)
334{
335 if (!saw_hugepagesz) {
336 BUILD_BUG_ON(ADDITIONAL_HUGE_SIZE >= PMD_SIZE ||
337 ADDITIONAL_HUGE_SIZE <= PAGE_SIZE);
338 BUILD_BUG_ON((PAGE_SIZE << ADDITIONAL_HUGE_SHIFT) !=
339 ADDITIONAL_HUGE_SIZE);
340 BUILD_BUG_ON(ADDITIONAL_HUGE_SHIFT & 1);
341 hugetlb_add_hstate(ADDITIONAL_HUGE_SHIFT);
342 }
343 return 0;
344}
345arch_initcall(add_default_hugepagesz);
346#endif
347
348#endif /* CONFIG_HUGETLB_SUPER_PAGES */