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