1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * IBM System z Huge TLB Page Support for Kernel.
4 *
5 * Copyright IBM Corp. 2007,2020
6 * Author(s): Gerald Schaefer <gerald.schaefer@de.ibm.com>
7 */
8
9 #define KMSG_COMPONENT "hugetlb"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
12 #include <linux/mm.h>
13 #include <linux/hugetlb.h>
14 #include <linux/mman.h>
15 #include <linux/sched/mm.h>
16 #include <linux/security.h>
17
18 /*
19 * If the bit selected by single-bit bitmask "a" is set within "x", move
20 * it to the position indicated by single-bit bitmask "b".
21 */
22 #define move_set_bit(x, a, b) (((x) & (a)) >> ilog2(a) << ilog2(b))
23
__pte_to_rste(pte_t pte)24 static inline unsigned long __pte_to_rste(pte_t pte)
25 {
26 unsigned long rste;
27
28 /*
29 * Convert encoding pte bits pmd / pud bits
30 * lIR.uswrdy.p dy..R...I...wr
31 * empty 010.000000.0 -> 00..0...1...00
32 * prot-none, clean, old 111.000000.1 -> 00..1...1...00
33 * prot-none, clean, young 111.000001.1 -> 01..1...1...00
34 * prot-none, dirty, old 111.000010.1 -> 10..1...1...00
35 * prot-none, dirty, young 111.000011.1 -> 11..1...1...00
36 * read-only, clean, old 111.000100.1 -> 00..1...1...01
37 * read-only, clean, young 101.000101.1 -> 01..1...0...01
38 * read-only, dirty, old 111.000110.1 -> 10..1...1...01
39 * read-only, dirty, young 101.000111.1 -> 11..1...0...01
40 * read-write, clean, old 111.001100.1 -> 00..1...1...11
41 * read-write, clean, young 101.001101.1 -> 01..1...0...11
42 * read-write, dirty, old 110.001110.1 -> 10..0...1...11
43 * read-write, dirty, young 100.001111.1 -> 11..0...0...11
44 * HW-bits: R read-only, I invalid
45 * SW-bits: p present, y young, d dirty, r read, w write, s special,
46 * u unused, l large
47 */
48 if (pte_present(pte)) {
49 rste = pte_val(pte) & PAGE_MASK;
50 rste |= move_set_bit(pte_val(pte), _PAGE_READ,
51 _SEGMENT_ENTRY_READ);
52 rste |= move_set_bit(pte_val(pte), _PAGE_WRITE,
53 _SEGMENT_ENTRY_WRITE);
54 rste |= move_set_bit(pte_val(pte), _PAGE_INVALID,
55 _SEGMENT_ENTRY_INVALID);
56 rste |= move_set_bit(pte_val(pte), _PAGE_PROTECT,
57 _SEGMENT_ENTRY_PROTECT);
58 rste |= move_set_bit(pte_val(pte), _PAGE_DIRTY,
59 _SEGMENT_ENTRY_DIRTY);
60 rste |= move_set_bit(pte_val(pte), _PAGE_YOUNG,
61 _SEGMENT_ENTRY_YOUNG);
62 #ifdef CONFIG_MEM_SOFT_DIRTY
63 rste |= move_set_bit(pte_val(pte), _PAGE_SOFT_DIRTY,
64 _SEGMENT_ENTRY_SOFT_DIRTY);
65 #endif
66 rste |= move_set_bit(pte_val(pte), _PAGE_NOEXEC,
67 _SEGMENT_ENTRY_NOEXEC);
68 } else
69 rste = _SEGMENT_ENTRY_EMPTY;
70 return rste;
71 }
72
__rste_to_pte(unsigned long rste)73 static inline pte_t __rste_to_pte(unsigned long rste)
74 {
75 int present;
76 pte_t pte;
77
78 if ((rste & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
79 present = pud_present(__pud(rste));
80 else
81 present = pmd_present(__pmd(rste));
82
83 /*
84 * Convert encoding pmd / pud bits pte bits
85 * dy..R...I...wr lIR.uswrdy.p
86 * empty 00..0...1...00 -> 010.000000.0
87 * prot-none, clean, old 00..1...1...00 -> 111.000000.1
88 * prot-none, clean, young 01..1...1...00 -> 111.000001.1
89 * prot-none, dirty, old 10..1...1...00 -> 111.000010.1
90 * prot-none, dirty, young 11..1...1...00 -> 111.000011.1
91 * read-only, clean, old 00..1...1...01 -> 111.000100.1
92 * read-only, clean, young 01..1...0...01 -> 101.000101.1
93 * read-only, dirty, old 10..1...1...01 -> 111.000110.1
94 * read-only, dirty, young 11..1...0...01 -> 101.000111.1
95 * read-write, clean, old 00..1...1...11 -> 111.001100.1
96 * read-write, clean, young 01..1...0...11 -> 101.001101.1
97 * read-write, dirty, old 10..0...1...11 -> 110.001110.1
98 * read-write, dirty, young 11..0...0...11 -> 100.001111.1
99 * HW-bits: R read-only, I invalid
100 * SW-bits: p present, y young, d dirty, r read, w write, s special,
101 * u unused, l large
102 */
103 if (present) {
104 pte_val(pte) = rste & _SEGMENT_ENTRY_ORIGIN_LARGE;
105 pte_val(pte) |= _PAGE_LARGE | _PAGE_PRESENT;
106 pte_val(pte) |= move_set_bit(rste, _SEGMENT_ENTRY_READ,
107 _PAGE_READ);
108 pte_val(pte) |= move_set_bit(rste, _SEGMENT_ENTRY_WRITE,
109 _PAGE_WRITE);
110 pte_val(pte) |= move_set_bit(rste, _SEGMENT_ENTRY_INVALID,
111 _PAGE_INVALID);
112 pte_val(pte) |= move_set_bit(rste, _SEGMENT_ENTRY_PROTECT,
113 _PAGE_PROTECT);
114 pte_val(pte) |= move_set_bit(rste, _SEGMENT_ENTRY_DIRTY,
115 _PAGE_DIRTY);
116 pte_val(pte) |= move_set_bit(rste, _SEGMENT_ENTRY_YOUNG,
117 _PAGE_YOUNG);
118 #ifdef CONFIG_MEM_SOFT_DIRTY
119 pte_val(pte) |= move_set_bit(rste, _SEGMENT_ENTRY_SOFT_DIRTY,
120 _PAGE_DIRTY);
121 #endif
122 pte_val(pte) |= move_set_bit(rste, _SEGMENT_ENTRY_NOEXEC,
123 _PAGE_NOEXEC);
124 } else
125 pte_val(pte) = _PAGE_INVALID;
126 return pte;
127 }
128
set_huge_pte_at(struct mm_struct * mm,unsigned long addr,pte_t * ptep,pte_t pte)129 void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
130 pte_t *ptep, pte_t pte)
131 {
132 unsigned long rste;
133
134 rste = __pte_to_rste(pte);
135 if (!MACHINE_HAS_NX)
136 rste &= ~_SEGMENT_ENTRY_NOEXEC;
137
138 /* Set correct table type for 2G hugepages */
139 if ((pte_val(*ptep) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
140 rste |= _REGION_ENTRY_TYPE_R3 | _REGION3_ENTRY_LARGE;
141 else
142 rste |= _SEGMENT_ENTRY_LARGE;
143 pte_val(*ptep) = rste;
144 }
145
huge_ptep_get(pte_t * ptep)146 pte_t huge_ptep_get(pte_t *ptep)
147 {
148 return __rste_to_pte(pte_val(*ptep));
149 }
150
huge_ptep_get_and_clear(struct mm_struct * mm,unsigned long addr,pte_t * ptep)151 pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
152 unsigned long addr, pte_t *ptep)
153 {
154 pte_t pte = huge_ptep_get(ptep);
155 pmd_t *pmdp = (pmd_t *) ptep;
156 pud_t *pudp = (pud_t *) ptep;
157
158 if ((pte_val(*ptep) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
159 pudp_xchg_direct(mm, addr, pudp, __pud(_REGION3_ENTRY_EMPTY));
160 else
161 pmdp_xchg_direct(mm, addr, pmdp, __pmd(_SEGMENT_ENTRY_EMPTY));
162 return pte;
163 }
164
huge_pte_alloc(struct mm_struct * mm,unsigned long addr,unsigned long sz)165 pte_t *huge_pte_alloc(struct mm_struct *mm,
166 unsigned long addr, unsigned long sz)
167 {
168 pgd_t *pgdp;
169 p4d_t *p4dp;
170 pud_t *pudp;
171 pmd_t *pmdp = NULL;
172
173 pgdp = pgd_offset(mm, addr);
174 p4dp = p4d_alloc(mm, pgdp, addr);
175 if (p4dp) {
176 pudp = pud_alloc(mm, p4dp, addr);
177 if (pudp) {
178 if (sz == PUD_SIZE)
179 return (pte_t *) pudp;
180 else if (sz == PMD_SIZE)
181 pmdp = pmd_alloc(mm, pudp, addr);
182 }
183 }
184 return (pte_t *) pmdp;
185 }
186
huge_pte_offset(struct mm_struct * mm,unsigned long addr,unsigned long sz)187 pte_t *huge_pte_offset(struct mm_struct *mm,
188 unsigned long addr, unsigned long sz)
189 {
190 pgd_t *pgdp;
191 p4d_t *p4dp;
192 pud_t *pudp;
193 pmd_t *pmdp = NULL;
194
195 pgdp = pgd_offset(mm, addr);
196 if (pgd_present(*pgdp)) {
197 p4dp = p4d_offset(pgdp, addr);
198 if (p4d_present(*p4dp)) {
199 pudp = pud_offset(p4dp, addr);
200 if (pud_present(*pudp)) {
201 if (pud_large(*pudp))
202 return (pte_t *) pudp;
203 pmdp = pmd_offset(pudp, addr);
204 }
205 }
206 }
207 return (pte_t *) pmdp;
208 }
209
pmd_huge(pmd_t pmd)210 int pmd_huge(pmd_t pmd)
211 {
212 return pmd_large(pmd);
213 }
214
pud_huge(pud_t pud)215 int pud_huge(pud_t pud)
216 {
217 return pud_large(pud);
218 }
219
220 struct page *
follow_huge_pud(struct mm_struct * mm,unsigned long address,pud_t * pud,int flags)221 follow_huge_pud(struct mm_struct *mm, unsigned long address,
222 pud_t *pud, int flags)
223 {
224 if (flags & FOLL_GET)
225 return NULL;
226
227 return pud_page(*pud) + ((address & ~PUD_MASK) >> PAGE_SHIFT);
228 }
229
setup_hugepagesz(char * opt)230 static __init int setup_hugepagesz(char *opt)
231 {
232 unsigned long size;
233 char *string = opt;
234
235 size = memparse(opt, &opt);
236 if (MACHINE_HAS_EDAT1 && size == PMD_SIZE) {
237 hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
238 } else if (MACHINE_HAS_EDAT2 && size == PUD_SIZE) {
239 hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
240 } else {
241 hugetlb_bad_size();
242 pr_err("hugepagesz= specifies an unsupported page size %s\n",
243 string);
244 return 0;
245 }
246 return 1;
247 }
248 __setup("hugepagesz=", setup_hugepagesz);
249
hugetlb_get_unmapped_area_bottomup(struct file * file,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)250 static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
251 unsigned long addr, unsigned long len,
252 unsigned long pgoff, unsigned long flags)
253 {
254 struct hstate *h = hstate_file(file);
255 struct vm_unmapped_area_info info;
256
257 info.flags = 0;
258 info.length = len;
259 info.low_limit = current->mm->mmap_base;
260 info.high_limit = TASK_SIZE;
261 info.align_mask = PAGE_MASK & ~huge_page_mask(h);
262 info.align_offset = 0;
263 return vm_unmapped_area(&info);
264 }
265
hugetlb_get_unmapped_area_topdown(struct file * file,unsigned long addr0,unsigned long len,unsigned long pgoff,unsigned long flags)266 static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
267 unsigned long addr0, unsigned long len,
268 unsigned long pgoff, unsigned long flags)
269 {
270 struct hstate *h = hstate_file(file);
271 struct vm_unmapped_area_info info;
272 unsigned long addr;
273
274 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
275 info.length = len;
276 info.low_limit = max(PAGE_SIZE, mmap_min_addr);
277 info.high_limit = current->mm->mmap_base;
278 info.align_mask = PAGE_MASK & ~huge_page_mask(h);
279 info.align_offset = 0;
280 addr = vm_unmapped_area(&info);
281
282 /*
283 * A failed mmap() very likely causes application failure,
284 * so fall back to the bottom-up function here. This scenario
285 * can happen with large stack limits and large mmap()
286 * allocations.
287 */
288 if (addr & ~PAGE_MASK) {
289 VM_BUG_ON(addr != -ENOMEM);
290 info.flags = 0;
291 info.low_limit = TASK_UNMAPPED_BASE;
292 info.high_limit = TASK_SIZE;
293 addr = vm_unmapped_area(&info);
294 }
295
296 return addr;
297 }
298
hugetlb_get_unmapped_area(struct file * file,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)299 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
300 unsigned long len, unsigned long pgoff, unsigned long flags)
301 {
302 struct hstate *h = hstate_file(file);
303 struct mm_struct *mm = current->mm;
304 struct vm_area_struct *vma;
305 int rc;
306
307 if (len & ~huge_page_mask(h))
308 return -EINVAL;
309 if (len > TASK_SIZE - mmap_min_addr)
310 return -ENOMEM;
311
312 if (flags & MAP_FIXED) {
313 if (prepare_hugepage_range(file, addr, len))
314 return -EINVAL;
315 goto check_asce_limit;
316 }
317
318 if (addr) {
319 addr = ALIGN(addr, huge_page_size(h));
320 vma = find_vma(mm, addr);
321 if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
322 (!vma || addr + len <= vm_start_gap(vma)))
323 goto check_asce_limit;
324 }
325
326 if (mm->get_unmapped_area == arch_get_unmapped_area)
327 addr = hugetlb_get_unmapped_area_bottomup(file, addr, len,
328 pgoff, flags);
329 else
330 addr = hugetlb_get_unmapped_area_topdown(file, addr, len,
331 pgoff, flags);
332 if (addr & ~PAGE_MASK)
333 return addr;
334
335 check_asce_limit:
336 if (addr + len > current->mm->context.asce_limit &&
337 addr + len <= TASK_SIZE) {
338 rc = crst_table_upgrade(mm, addr + len);
339 if (rc)
340 return (unsigned long) rc;
341 }
342 return addr;
343 }
344