Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Page table allocation functions
4 *
5 * Copyright IBM Corp. 2016
6 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
7 */
8
9#include <linux/sysctl.h>
10#include <linux/slab.h>
11#include <linux/mm.h>
12#include <asm/mmu_context.h>
13#include <asm/page-states.h>
14#include <asm/pgalloc.h>
15#include <asm/gmap.h>
16#include <asm/tlb.h>
17#include <asm/tlbflush.h>
18
19#ifdef CONFIG_PGSTE
20
21int page_table_allocate_pgste = 0;
22EXPORT_SYMBOL(page_table_allocate_pgste);
23
24static const struct ctl_table page_table_sysctl[] = {
25 {
26 .procname = "allocate_pgste",
27 .data = &page_table_allocate_pgste,
28 .maxlen = sizeof(int),
29 .mode = S_IRUGO | S_IWUSR,
30 .proc_handler = proc_dointvec_minmax,
31 .extra1 = SYSCTL_ZERO,
32 .extra2 = SYSCTL_ONE,
33 },
34};
35
36static int __init page_table_register_sysctl(void)
37{
38 return register_sysctl("vm", page_table_sysctl) ? 0 : -ENOMEM;
39}
40__initcall(page_table_register_sysctl);
41
42#endif /* CONFIG_PGSTE */
43
44unsigned long *crst_table_alloc(struct mm_struct *mm)
45{
46 struct ptdesc *ptdesc = pagetable_alloc(GFP_KERNEL, CRST_ALLOC_ORDER);
47 unsigned long *table;
48
49 if (!ptdesc)
50 return NULL;
51 table = ptdesc_to_virt(ptdesc);
52 __arch_set_page_dat(table, 1UL << CRST_ALLOC_ORDER);
53 return table;
54}
55
56void crst_table_free(struct mm_struct *mm, unsigned long *table)
57{
58 if (!table)
59 return;
60 pagetable_free(virt_to_ptdesc(table));
61}
62
63static void __crst_table_upgrade(void *arg)
64{
65 struct mm_struct *mm = arg;
66
67 /* change all active ASCEs to avoid the creation of new TLBs */
68 if (current->active_mm == mm) {
69 get_lowcore()->user_asce.val = mm->context.asce;
70 local_ctl_load(7, &get_lowcore()->user_asce);
71 }
72 __tlb_flush_local();
73}
74
75int crst_table_upgrade(struct mm_struct *mm, unsigned long end)
76{
77 unsigned long *pgd = NULL, *p4d = NULL, *__pgd;
78 unsigned long asce_limit = mm->context.asce_limit;
79
80 /* upgrade should only happen from 3 to 4, 3 to 5, or 4 to 5 levels */
81 VM_BUG_ON(asce_limit < _REGION2_SIZE);
82
83 if (end <= asce_limit)
84 return 0;
85
86 if (asce_limit == _REGION2_SIZE) {
87 p4d = crst_table_alloc(mm);
88 if (unlikely(!p4d))
89 goto err_p4d;
90 crst_table_init(p4d, _REGION2_ENTRY_EMPTY);
91 pagetable_p4d_ctor(virt_to_ptdesc(p4d));
92 }
93 if (end > _REGION1_SIZE) {
94 pgd = crst_table_alloc(mm);
95 if (unlikely(!pgd))
96 goto err_pgd;
97 crst_table_init(pgd, _REGION1_ENTRY_EMPTY);
98 pagetable_pgd_ctor(virt_to_ptdesc(pgd));
99 }
100
101 spin_lock_bh(&mm->page_table_lock);
102
103 /*
104 * This routine gets called with mmap_lock lock held and there is
105 * no reason to optimize for the case of otherwise. However, if
106 * that would ever change, the below check will let us know.
107 */
108 VM_BUG_ON(asce_limit != mm->context.asce_limit);
109
110 if (p4d) {
111 __pgd = (unsigned long *) mm->pgd;
112 p4d_populate(mm, (p4d_t *) p4d, (pud_t *) __pgd);
113 mm->pgd = (pgd_t *) p4d;
114 mm->context.asce_limit = _REGION1_SIZE;
115 mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
116 _ASCE_USER_BITS | _ASCE_TYPE_REGION2;
117 mm_inc_nr_puds(mm);
118 }
119 if (pgd) {
120 __pgd = (unsigned long *) mm->pgd;
121 pgd_populate(mm, (pgd_t *) pgd, (p4d_t *) __pgd);
122 mm->pgd = (pgd_t *) pgd;
123 mm->context.asce_limit = TASK_SIZE_MAX;
124 mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
125 _ASCE_USER_BITS | _ASCE_TYPE_REGION1;
126 }
127
128 spin_unlock_bh(&mm->page_table_lock);
129
130 on_each_cpu(__crst_table_upgrade, mm, 0);
131
132 return 0;
133
134err_pgd:
135 pagetable_dtor(virt_to_ptdesc(p4d));
136 crst_table_free(mm, p4d);
137err_p4d:
138 return -ENOMEM;
139}
140
141#ifdef CONFIG_PGSTE
142
143struct ptdesc *page_table_alloc_pgste(struct mm_struct *mm)
144{
145 struct ptdesc *ptdesc;
146 u64 *table;
147
148 ptdesc = pagetable_alloc(GFP_KERNEL, 0);
149 if (ptdesc) {
150 table = (u64 *)ptdesc_to_virt(ptdesc);
151 __arch_set_page_dat(table, 1);
152 memset64(table, _PAGE_INVALID, PTRS_PER_PTE);
153 memset64(table + PTRS_PER_PTE, 0, PTRS_PER_PTE);
154 }
155 return ptdesc;
156}
157
158void page_table_free_pgste(struct ptdesc *ptdesc)
159{
160 pagetable_free(ptdesc);
161}
162
163#endif /* CONFIG_PGSTE */
164
165unsigned long *page_table_alloc(struct mm_struct *mm)
166{
167 struct ptdesc *ptdesc;
168 unsigned long *table;
169
170 ptdesc = pagetable_alloc(GFP_KERNEL, 0);
171 if (!ptdesc)
172 return NULL;
173 if (!pagetable_pte_ctor(ptdesc)) {
174 pagetable_free(ptdesc);
175 return NULL;
176 }
177 table = ptdesc_to_virt(ptdesc);
178 __arch_set_page_dat(table, 1);
179 memset64((u64 *)table, _PAGE_INVALID, PTRS_PER_PTE);
180 memset64((u64 *)table + PTRS_PER_PTE, 0, PTRS_PER_PTE);
181 return table;
182}
183
184void page_table_free(struct mm_struct *mm, unsigned long *table)
185{
186 struct ptdesc *ptdesc = virt_to_ptdesc(table);
187
188 pagetable_dtor_free(ptdesc);
189}
190
191#ifdef CONFIG_TRANSPARENT_HUGEPAGE
192static void pte_free_now(struct rcu_head *head)
193{
194 struct ptdesc *ptdesc = container_of(head, struct ptdesc, pt_rcu_head);
195
196 pagetable_dtor_free(ptdesc);
197}
198
199void pte_free_defer(struct mm_struct *mm, pgtable_t pgtable)
200{
201 struct ptdesc *ptdesc = virt_to_ptdesc(pgtable);
202
203 call_rcu(&ptdesc->pt_rcu_head, pte_free_now);
204 /*
205 * THPs are not allowed for KVM guests. Warn if pgste ever reaches here.
206 * Turn to the generic pte_free_defer() version once gmap is removed.
207 */
208 WARN_ON_ONCE(mm_has_pgste(mm));
209}
210#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
211
212/*
213 * Base infrastructure required to generate basic asces, region, segment,
214 * and page tables that do not make use of enhanced features like EDAT1.
215 */
216
217static struct kmem_cache *base_pgt_cache;
218
219static unsigned long *base_pgt_alloc(void)
220{
221 unsigned long *table;
222
223 table = kmem_cache_alloc(base_pgt_cache, GFP_KERNEL);
224 if (table)
225 memset64((u64 *)table, _PAGE_INVALID, PTRS_PER_PTE);
226 return table;
227}
228
229static void base_pgt_free(unsigned long *table)
230{
231 kmem_cache_free(base_pgt_cache, table);
232}
233
234static unsigned long *base_crst_alloc(unsigned long val)
235{
236 unsigned long *table;
237 struct ptdesc *ptdesc;
238
239 ptdesc = pagetable_alloc(GFP_KERNEL, CRST_ALLOC_ORDER);
240 if (!ptdesc)
241 return NULL;
242 table = ptdesc_address(ptdesc);
243 crst_table_init(table, val);
244 return table;
245}
246
247static void base_crst_free(unsigned long *table)
248{
249 if (!table)
250 return;
251 pagetable_free(virt_to_ptdesc(table));
252}
253
254#define BASE_ADDR_END_FUNC(NAME, SIZE) \
255static inline unsigned long base_##NAME##_addr_end(unsigned long addr, \
256 unsigned long end) \
257{ \
258 unsigned long next = (addr + (SIZE)) & ~((SIZE) - 1); \
259 \
260 return (next - 1) < (end - 1) ? next : end; \
261}
262
263BASE_ADDR_END_FUNC(page, PAGE_SIZE)
264BASE_ADDR_END_FUNC(segment, _SEGMENT_SIZE)
265BASE_ADDR_END_FUNC(region3, _REGION3_SIZE)
266BASE_ADDR_END_FUNC(region2, _REGION2_SIZE)
267BASE_ADDR_END_FUNC(region1, _REGION1_SIZE)
268
269static inline unsigned long base_lra(unsigned long address)
270{
271 unsigned long real;
272
273 asm volatile(
274 " lra %0,0(%1)\n"
275 : "=d" (real) : "a" (address) : "cc");
276 return real;
277}
278
279static int base_page_walk(unsigned long *origin, unsigned long addr,
280 unsigned long end, int alloc)
281{
282 unsigned long *pte, next;
283
284 if (!alloc)
285 return 0;
286 pte = origin;
287 pte += (addr & _PAGE_INDEX) >> PAGE_SHIFT;
288 do {
289 next = base_page_addr_end(addr, end);
290 *pte = base_lra(addr);
291 } while (pte++, addr = next, addr < end);
292 return 0;
293}
294
295static int base_segment_walk(unsigned long *origin, unsigned long addr,
296 unsigned long end, int alloc)
297{
298 unsigned long *ste, next, *table;
299 int rc;
300
301 ste = origin;
302 ste += (addr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
303 do {
304 next = base_segment_addr_end(addr, end);
305 if (*ste & _SEGMENT_ENTRY_INVALID) {
306 if (!alloc)
307 continue;
308 table = base_pgt_alloc();
309 if (!table)
310 return -ENOMEM;
311 *ste = __pa(table) | _SEGMENT_ENTRY;
312 }
313 table = __va(*ste & _SEGMENT_ENTRY_ORIGIN);
314 rc = base_page_walk(table, addr, next, alloc);
315 if (rc)
316 return rc;
317 if (!alloc)
318 base_pgt_free(table);
319 cond_resched();
320 } while (ste++, addr = next, addr < end);
321 return 0;
322}
323
324static int base_region3_walk(unsigned long *origin, unsigned long addr,
325 unsigned long end, int alloc)
326{
327 unsigned long *rtte, next, *table;
328 int rc;
329
330 rtte = origin;
331 rtte += (addr & _REGION3_INDEX) >> _REGION3_SHIFT;
332 do {
333 next = base_region3_addr_end(addr, end);
334 if (*rtte & _REGION_ENTRY_INVALID) {
335 if (!alloc)
336 continue;
337 table = base_crst_alloc(_SEGMENT_ENTRY_EMPTY);
338 if (!table)
339 return -ENOMEM;
340 *rtte = __pa(table) | _REGION3_ENTRY;
341 }
342 table = __va(*rtte & _REGION_ENTRY_ORIGIN);
343 rc = base_segment_walk(table, addr, next, alloc);
344 if (rc)
345 return rc;
346 if (!alloc)
347 base_crst_free(table);
348 } while (rtte++, addr = next, addr < end);
349 return 0;
350}
351
352static int base_region2_walk(unsigned long *origin, unsigned long addr,
353 unsigned long end, int alloc)
354{
355 unsigned long *rste, next, *table;
356 int rc;
357
358 rste = origin;
359 rste += (addr & _REGION2_INDEX) >> _REGION2_SHIFT;
360 do {
361 next = base_region2_addr_end(addr, end);
362 if (*rste & _REGION_ENTRY_INVALID) {
363 if (!alloc)
364 continue;
365 table = base_crst_alloc(_REGION3_ENTRY_EMPTY);
366 if (!table)
367 return -ENOMEM;
368 *rste = __pa(table) | _REGION2_ENTRY;
369 }
370 table = __va(*rste & _REGION_ENTRY_ORIGIN);
371 rc = base_region3_walk(table, addr, next, alloc);
372 if (rc)
373 return rc;
374 if (!alloc)
375 base_crst_free(table);
376 } while (rste++, addr = next, addr < end);
377 return 0;
378}
379
380static int base_region1_walk(unsigned long *origin, unsigned long addr,
381 unsigned long end, int alloc)
382{
383 unsigned long *rfte, next, *table;
384 int rc;
385
386 rfte = origin;
387 rfte += (addr & _REGION1_INDEX) >> _REGION1_SHIFT;
388 do {
389 next = base_region1_addr_end(addr, end);
390 if (*rfte & _REGION_ENTRY_INVALID) {
391 if (!alloc)
392 continue;
393 table = base_crst_alloc(_REGION2_ENTRY_EMPTY);
394 if (!table)
395 return -ENOMEM;
396 *rfte = __pa(table) | _REGION1_ENTRY;
397 }
398 table = __va(*rfte & _REGION_ENTRY_ORIGIN);
399 rc = base_region2_walk(table, addr, next, alloc);
400 if (rc)
401 return rc;
402 if (!alloc)
403 base_crst_free(table);
404 } while (rfte++, addr = next, addr < end);
405 return 0;
406}
407
408/**
409 * base_asce_free - free asce and tables returned from base_asce_alloc()
410 * @asce: asce to be freed
411 *
412 * Frees all region, segment, and page tables that were allocated with a
413 * corresponding base_asce_alloc() call.
414 */
415void base_asce_free(unsigned long asce)
416{
417 unsigned long *table = __va(asce & _ASCE_ORIGIN);
418
419 if (!asce)
420 return;
421 switch (asce & _ASCE_TYPE_MASK) {
422 case _ASCE_TYPE_SEGMENT:
423 base_segment_walk(table, 0, _REGION3_SIZE, 0);
424 break;
425 case _ASCE_TYPE_REGION3:
426 base_region3_walk(table, 0, _REGION2_SIZE, 0);
427 break;
428 case _ASCE_TYPE_REGION2:
429 base_region2_walk(table, 0, _REGION1_SIZE, 0);
430 break;
431 case _ASCE_TYPE_REGION1:
432 base_region1_walk(table, 0, TASK_SIZE_MAX, 0);
433 break;
434 }
435 base_crst_free(table);
436}
437
438static int base_pgt_cache_init(void)
439{
440 static DEFINE_MUTEX(base_pgt_cache_mutex);
441 unsigned long sz = _PAGE_TABLE_SIZE;
442
443 if (base_pgt_cache)
444 return 0;
445 mutex_lock(&base_pgt_cache_mutex);
446 if (!base_pgt_cache)
447 base_pgt_cache = kmem_cache_create("base_pgt", sz, sz, 0, NULL);
448 mutex_unlock(&base_pgt_cache_mutex);
449 return base_pgt_cache ? 0 : -ENOMEM;
450}
451
452/**
453 * base_asce_alloc - create kernel mapping without enhanced DAT features
454 * @addr: virtual start address of kernel mapping
455 * @num_pages: number of consecutive pages
456 *
457 * Generate an asce, including all required region, segment and page tables,
458 * that can be used to access the virtual kernel mapping. The difference is
459 * that the returned asce does not make use of any enhanced DAT features like
460 * e.g. large pages. This is required for some I/O functions that pass an
461 * asce, like e.g. some service call requests.
462 *
463 * Note: the returned asce may NEVER be attached to any cpu. It may only be
464 * used for I/O requests. tlb entries that might result because the
465 * asce was attached to a cpu won't be cleared.
466 */
467unsigned long base_asce_alloc(unsigned long addr, unsigned long num_pages)
468{
469 unsigned long asce, *table, end;
470 int rc;
471
472 if (base_pgt_cache_init())
473 return 0;
474 end = addr + num_pages * PAGE_SIZE;
475 if (end <= _REGION3_SIZE) {
476 table = base_crst_alloc(_SEGMENT_ENTRY_EMPTY);
477 if (!table)
478 return 0;
479 rc = base_segment_walk(table, addr, end, 1);
480 asce = __pa(table) | _ASCE_TYPE_SEGMENT | _ASCE_TABLE_LENGTH;
481 } else if (end <= _REGION2_SIZE) {
482 table = base_crst_alloc(_REGION3_ENTRY_EMPTY);
483 if (!table)
484 return 0;
485 rc = base_region3_walk(table, addr, end, 1);
486 asce = __pa(table) | _ASCE_TYPE_REGION3 | _ASCE_TABLE_LENGTH;
487 } else if (end <= _REGION1_SIZE) {
488 table = base_crst_alloc(_REGION2_ENTRY_EMPTY);
489 if (!table)
490 return 0;
491 rc = base_region2_walk(table, addr, end, 1);
492 asce = __pa(table) | _ASCE_TYPE_REGION2 | _ASCE_TABLE_LENGTH;
493 } else {
494 table = base_crst_alloc(_REGION1_ENTRY_EMPTY);
495 if (!table)
496 return 0;
497 rc = base_region1_walk(table, addr, end, 1);
498 asce = __pa(table) | _ASCE_TYPE_REGION1 | _ASCE_TABLE_LENGTH;
499 }
500 if (rc) {
501 base_asce_free(asce);
502 asce = 0;
503 }
504 return asce;
505}