1 // SPDX-License-Identifier: GPL-2.0
3 * Page table allocation functions
5 * Copyright IBM Corp. 2016
9 #include <linux/sysctl.h>
10 #include <linux/slab.h>
12 #include <asm/mmu_context.h>
13 #include <asm/page-states.h>
14 #include <asm/pgalloc.h>
17 #include <asm/tlbflush.h>
21 int page_table_allocate_pgste = 0;
22 EXPORT_SYMBOL(page_table_allocate_pgste);
24 static struct ctl_table page_table_sysctl[] = {
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,
36 static int __init page_table_register_sysctl(void)
38 return register_sysctl("vm", page_table_sysctl) ? 0 : -ENOMEM;
40 __initcall(page_table_register_sysctl);
42 #endif /* CONFIG_PGSTE */
44 unsigned long *crst_table_alloc(struct mm_struct *mm)
46 struct ptdesc *ptdesc = pagetable_alloc(GFP_KERNEL, CRST_ALLOC_ORDER);
51 table = ptdesc_to_virt(ptdesc);
52 __arch_set_page_dat(table, 1UL << CRST_ALLOC_ORDER);
56 void crst_table_free(struct mm_struct *mm, unsigned long *table)
60 pagetable_free(virt_to_ptdesc(table));
63 static void __crst_table_upgrade(void *arg)
65 struct mm_struct *mm = arg;
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);
75 int crst_table_upgrade(struct mm_struct *mm, unsigned long end)
77 unsigned long *pgd = NULL, *p4d = NULL, *__pgd;
78 unsigned long asce_limit = mm->context.asce_limit;
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);
83 if (end <= asce_limit)
86 if (asce_limit == _REGION2_SIZE) {
87 p4d = crst_table_alloc(mm);
90 crst_table_init(p4d, _REGION2_ENTRY_EMPTY);
92 if (end > _REGION1_SIZE) {
93 pgd = crst_table_alloc(mm);
96 crst_table_init(pgd, _REGION1_ENTRY_EMPTY);
99 spin_lock_bh(&mm->page_table_lock);
102 * This routine gets called with mmap_lock lock held and there is
103 * no reason to optimize for the case of otherwise. However, if
104 * that would ever change, the below check will let us know.
106 VM_BUG_ON(asce_limit != mm->context.asce_limit);
109 __pgd = (unsigned long *) mm->pgd;
110 p4d_populate(mm, (p4d_t *) p4d, (pud_t *) __pgd);
111 mm->pgd = (pgd_t *) p4d;
112 mm->context.asce_limit = _REGION1_SIZE;
113 mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
114 _ASCE_USER_BITS | _ASCE_TYPE_REGION2;
118 __pgd = (unsigned long *) mm->pgd;
119 pgd_populate(mm, (pgd_t *) pgd, (p4d_t *) __pgd);
120 mm->pgd = (pgd_t *) pgd;
121 mm->context.asce_limit = TASK_SIZE_MAX;
122 mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
123 _ASCE_USER_BITS | _ASCE_TYPE_REGION1;
126 spin_unlock_bh(&mm->page_table_lock);
128 on_each_cpu(__crst_table_upgrade, mm, 0);
133 crst_table_free(mm, p4d);
140 struct ptdesc *page_table_alloc_pgste(struct mm_struct *mm)
142 struct ptdesc *ptdesc;
145 ptdesc = pagetable_alloc(GFP_KERNEL, 0);
147 table = (u64 *)ptdesc_to_virt(ptdesc);
148 __arch_set_page_dat(table, 1);
149 memset64(table, _PAGE_INVALID, PTRS_PER_PTE);
150 memset64(table + PTRS_PER_PTE, 0, PTRS_PER_PTE);
155 void page_table_free_pgste(struct ptdesc *ptdesc)
157 pagetable_free(ptdesc);
160 #endif /* CONFIG_PGSTE */
162 unsigned long *page_table_alloc(struct mm_struct *mm)
164 struct ptdesc *ptdesc;
165 unsigned long *table;
167 ptdesc = pagetable_alloc(GFP_KERNEL, 0);
170 if (!pagetable_pte_ctor(ptdesc)) {
171 pagetable_free(ptdesc);
174 table = ptdesc_to_virt(ptdesc);
175 __arch_set_page_dat(table, 1);
176 /* pt_list is used by gmap only */
177 INIT_LIST_HEAD(&ptdesc->pt_list);
178 memset64((u64 *)table, _PAGE_INVALID, PTRS_PER_PTE);
179 memset64((u64 *)table + PTRS_PER_PTE, 0, PTRS_PER_PTE);
183 static void pagetable_pte_dtor_free(struct ptdesc *ptdesc)
185 pagetable_pte_dtor(ptdesc);
186 pagetable_free(ptdesc);
189 void page_table_free(struct mm_struct *mm, unsigned long *table)
191 struct ptdesc *ptdesc = virt_to_ptdesc(table);
193 pagetable_pte_dtor_free(ptdesc);
196 void __tlb_remove_table(void *table)
198 struct ptdesc *ptdesc = virt_to_ptdesc(table);
199 struct page *page = ptdesc_page(ptdesc);
201 if (compound_order(page) == CRST_ALLOC_ORDER) {
202 /* pmd, pud, or p4d */
203 pagetable_free(ptdesc);
206 pagetable_pte_dtor_free(ptdesc);
209 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
210 static void pte_free_now(struct rcu_head *head)
212 struct ptdesc *ptdesc = container_of(head, struct ptdesc, pt_rcu_head);
214 pagetable_pte_dtor_free(ptdesc);
217 void pte_free_defer(struct mm_struct *mm, pgtable_t pgtable)
219 struct ptdesc *ptdesc = virt_to_ptdesc(pgtable);
221 call_rcu(&ptdesc->pt_rcu_head, pte_free_now);
223 * THPs are not allowed for KVM guests. Warn if pgste ever reaches here.
224 * Turn to the generic pte_free_defer() version once gmap is removed.
226 WARN_ON_ONCE(mm_has_pgste(mm));
228 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
231 * Base infrastructure required to generate basic asces, region, segment,
232 * and page tables that do not make use of enhanced features like EDAT1.
235 static struct kmem_cache *base_pgt_cache;
237 static unsigned long *base_pgt_alloc(void)
239 unsigned long *table;
241 table = kmem_cache_alloc(base_pgt_cache, GFP_KERNEL);
243 memset64((u64 *)table, _PAGE_INVALID, PTRS_PER_PTE);
247 static void base_pgt_free(unsigned long *table)
249 kmem_cache_free(base_pgt_cache, table);
252 static unsigned long *base_crst_alloc(unsigned long val)
254 unsigned long *table;
255 struct ptdesc *ptdesc;
257 ptdesc = pagetable_alloc(GFP_KERNEL, CRST_ALLOC_ORDER);
260 table = ptdesc_address(ptdesc);
261 crst_table_init(table, val);
265 static void base_crst_free(unsigned long *table)
269 pagetable_free(virt_to_ptdesc(table));
272 #define BASE_ADDR_END_FUNC(NAME, SIZE) \
273 static inline unsigned long base_##NAME##_addr_end(unsigned long addr, \
276 unsigned long next = (addr + (SIZE)) & ~((SIZE) - 1); \
278 return (next - 1) < (end - 1) ? next : end; \
281 BASE_ADDR_END_FUNC(page, PAGE_SIZE)
282 BASE_ADDR_END_FUNC(segment, _SEGMENT_SIZE)
283 BASE_ADDR_END_FUNC(region3, _REGION3_SIZE)
284 BASE_ADDR_END_FUNC(region2, _REGION2_SIZE)
285 BASE_ADDR_END_FUNC(region1, _REGION1_SIZE)
287 static inline unsigned long base_lra(unsigned long address)
293 : "=d" (real) : "a" (address) : "cc");
297 static int base_page_walk(unsigned long *origin, unsigned long addr,
298 unsigned long end, int alloc)
300 unsigned long *pte, next;
305 pte += (addr & _PAGE_INDEX) >> PAGE_SHIFT;
307 next = base_page_addr_end(addr, end);
308 *pte = base_lra(addr);
309 } while (pte++, addr = next, addr < end);
313 static int base_segment_walk(unsigned long *origin, unsigned long addr,
314 unsigned long end, int alloc)
316 unsigned long *ste, next, *table;
320 ste += (addr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
322 next = base_segment_addr_end(addr, end);
323 if (*ste & _SEGMENT_ENTRY_INVALID) {
326 table = base_pgt_alloc();
329 *ste = __pa(table) | _SEGMENT_ENTRY;
331 table = __va(*ste & _SEGMENT_ENTRY_ORIGIN);
332 rc = base_page_walk(table, addr, next, alloc);
336 base_pgt_free(table);
338 } while (ste++, addr = next, addr < end);
342 static int base_region3_walk(unsigned long *origin, unsigned long addr,
343 unsigned long end, int alloc)
345 unsigned long *rtte, next, *table;
349 rtte += (addr & _REGION3_INDEX) >> _REGION3_SHIFT;
351 next = base_region3_addr_end(addr, end);
352 if (*rtte & _REGION_ENTRY_INVALID) {
355 table = base_crst_alloc(_SEGMENT_ENTRY_EMPTY);
358 *rtte = __pa(table) | _REGION3_ENTRY;
360 table = __va(*rtte & _REGION_ENTRY_ORIGIN);
361 rc = base_segment_walk(table, addr, next, alloc);
365 base_crst_free(table);
366 } while (rtte++, addr = next, addr < end);
370 static int base_region2_walk(unsigned long *origin, unsigned long addr,
371 unsigned long end, int alloc)
373 unsigned long *rste, next, *table;
377 rste += (addr & _REGION2_INDEX) >> _REGION2_SHIFT;
379 next = base_region2_addr_end(addr, end);
380 if (*rste & _REGION_ENTRY_INVALID) {
383 table = base_crst_alloc(_REGION3_ENTRY_EMPTY);
386 *rste = __pa(table) | _REGION2_ENTRY;
388 table = __va(*rste & _REGION_ENTRY_ORIGIN);
389 rc = base_region3_walk(table, addr, next, alloc);
393 base_crst_free(table);
394 } while (rste++, addr = next, addr < end);
398 static int base_region1_walk(unsigned long *origin, unsigned long addr,
399 unsigned long end, int alloc)
401 unsigned long *rfte, next, *table;
405 rfte += (addr & _REGION1_INDEX) >> _REGION1_SHIFT;
407 next = base_region1_addr_end(addr, end);
408 if (*rfte & _REGION_ENTRY_INVALID) {
411 table = base_crst_alloc(_REGION2_ENTRY_EMPTY);
414 *rfte = __pa(table) | _REGION1_ENTRY;
416 table = __va(*rfte & _REGION_ENTRY_ORIGIN);
417 rc = base_region2_walk(table, addr, next, alloc);
421 base_crst_free(table);
422 } while (rfte++, addr = next, addr < end);
427 * base_asce_free - free asce and tables returned from base_asce_alloc()
428 * @asce: asce to be freed
430 * Frees all region, segment, and page tables that were allocated with a
431 * corresponding base_asce_alloc() call.
433 void base_asce_free(unsigned long asce)
435 unsigned long *table = __va(asce & _ASCE_ORIGIN);
439 switch (asce & _ASCE_TYPE_MASK) {
440 case _ASCE_TYPE_SEGMENT:
441 base_segment_walk(table, 0, _REGION3_SIZE, 0);
443 case _ASCE_TYPE_REGION3:
444 base_region3_walk(table, 0, _REGION2_SIZE, 0);
446 case _ASCE_TYPE_REGION2:
447 base_region2_walk(table, 0, _REGION1_SIZE, 0);
449 case _ASCE_TYPE_REGION1:
450 base_region1_walk(table, 0, TASK_SIZE_MAX, 0);
453 base_crst_free(table);
456 static int base_pgt_cache_init(void)
458 static DEFINE_MUTEX(base_pgt_cache_mutex);
459 unsigned long sz = _PAGE_TABLE_SIZE;
463 mutex_lock(&base_pgt_cache_mutex);
465 base_pgt_cache = kmem_cache_create("base_pgt", sz, sz, 0, NULL);
466 mutex_unlock(&base_pgt_cache_mutex);
467 return base_pgt_cache ? 0 : -ENOMEM;
471 * base_asce_alloc - create kernel mapping without enhanced DAT features
472 * @addr: virtual start address of kernel mapping
473 * @num_pages: number of consecutive pages
475 * Generate an asce, including all required region, segment and page tables,
476 * that can be used to access the virtual kernel mapping. The difference is
477 * that the returned asce does not make use of any enhanced DAT features like
478 * e.g. large pages. This is required for some I/O functions that pass an
479 * asce, like e.g. some service call requests.
481 * Note: the returned asce may NEVER be attached to any cpu. It may only be
482 * used for I/O requests. tlb entries that might result because the
483 * asce was attached to a cpu won't be cleared.
485 unsigned long base_asce_alloc(unsigned long addr, unsigned long num_pages)
487 unsigned long asce, *table, end;
490 if (base_pgt_cache_init())
492 end = addr + num_pages * PAGE_SIZE;
493 if (end <= _REGION3_SIZE) {
494 table = base_crst_alloc(_SEGMENT_ENTRY_EMPTY);
497 rc = base_segment_walk(table, addr, end, 1);
498 asce = __pa(table) | _ASCE_TYPE_SEGMENT | _ASCE_TABLE_LENGTH;
499 } else if (end <= _REGION2_SIZE) {
500 table = base_crst_alloc(_REGION3_ENTRY_EMPTY);
503 rc = base_region3_walk(table, addr, end, 1);
504 asce = __pa(table) | _ASCE_TYPE_REGION3 | _ASCE_TABLE_LENGTH;
505 } else if (end <= _REGION1_SIZE) {
506 table = base_crst_alloc(_REGION2_ENTRY_EMPTY);
509 rc = base_region2_walk(table, addr, end, 1);
510 asce = __pa(table) | _ASCE_TYPE_REGION2 | _ASCE_TABLE_LENGTH;
512 table = base_crst_alloc(_REGION1_ENTRY_EMPTY);
515 rc = base_region1_walk(table, addr, end, 1);
516 asce = __pa(table) | _ASCE_TYPE_REGION1 | _ASCE_TABLE_LENGTH;
519 base_asce_free(asce);