1 // SPDX-License-Identifier: GPL-2.0
3 * Page table allocation functions
5 * Copyright IBM Corp. 2016
6 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
9 #include <linux/sysctl.h>
10 #include <linux/slab.h>
12 #include <asm/mmu_context.h>
13 #include <asm/pgalloc.h>
16 #include <asm/tlbflush.h>
20 static int page_table_allocate_pgste_min = 0;
21 static int page_table_allocate_pgste_max = 1;
22 int page_table_allocate_pgste = 0;
23 EXPORT_SYMBOL(page_table_allocate_pgste);
25 static struct ctl_table page_table_sysctl[] = {
27 .procname = "allocate_pgste",
28 .data = &page_table_allocate_pgste,
29 .maxlen = sizeof(int),
30 .mode = S_IRUGO | S_IWUSR,
31 .proc_handler = proc_dointvec_minmax,
32 .extra1 = &page_table_allocate_pgste_min,
33 .extra2 = &page_table_allocate_pgste_max,
38 static struct ctl_table page_table_sysctl_dir[] = {
43 .child = page_table_sysctl,
48 static int __init page_table_register_sysctl(void)
50 return register_sysctl_table(page_table_sysctl_dir) ? 0 : -ENOMEM;
52 __initcall(page_table_register_sysctl);
54 #endif /* CONFIG_PGSTE */
56 unsigned long *crst_table_alloc(struct mm_struct *mm)
58 struct page *page = alloc_pages(GFP_KERNEL, 2);
62 arch_set_page_dat(page, 2);
63 return (unsigned long *) page_to_phys(page);
66 void crst_table_free(struct mm_struct *mm, unsigned long *table)
68 free_pages((unsigned long) table, 2);
71 static void __crst_table_upgrade(void *arg)
73 struct mm_struct *mm = arg;
75 /* we must change all active ASCEs to avoid the creation of new TLBs */
76 if (current->active_mm == mm) {
77 S390_lowcore.user_asce = mm->context.asce;
78 if (current->thread.mm_segment == USER_DS) {
79 __ctl_load(S390_lowcore.user_asce, 1, 1);
80 /* Mark user-ASCE present in CR1 */
81 clear_cpu_flag(CIF_ASCE_PRIMARY);
83 if (current->thread.mm_segment == USER_DS_SACF) {
84 __ctl_load(S390_lowcore.user_asce, 7, 7);
85 /* enable_sacf_uaccess does all or nothing */
86 WARN_ON(!test_cpu_flag(CIF_ASCE_SECONDARY));
92 int crst_table_upgrade(struct mm_struct *mm, unsigned long end)
94 unsigned long *table, *pgd;
97 /* upgrade should only happen from 3 to 4, 3 to 5, or 4 to 5 levels */
98 VM_BUG_ON(mm->context.asce_limit < _REGION2_SIZE);
101 while (mm->context.asce_limit < end) {
102 table = crst_table_alloc(mm);
107 spin_lock_bh(&mm->page_table_lock);
108 pgd = (unsigned long *) mm->pgd;
109 if (mm->context.asce_limit == _REGION2_SIZE) {
110 crst_table_init(table, _REGION2_ENTRY_EMPTY);
111 p4d_populate(mm, (p4d_t *) table, (pud_t *) pgd);
112 mm->pgd = (pgd_t *) table;
113 mm->context.asce_limit = _REGION1_SIZE;
114 mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
115 _ASCE_USER_BITS | _ASCE_TYPE_REGION2;
118 crst_table_init(table, _REGION1_ENTRY_EMPTY);
119 pgd_populate(mm, (pgd_t *) table, (p4d_t *) pgd);
120 mm->pgd = (pgd_t *) table;
121 mm->context.asce_limit = -PAGE_SIZE;
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);
129 on_each_cpu(__crst_table_upgrade, mm, 0);
133 void crst_table_downgrade(struct mm_struct *mm)
137 /* downgrade should only happen from 3 to 2 levels (compat only) */
138 VM_BUG_ON(mm->context.asce_limit != _REGION2_SIZE);
140 if (current->active_mm == mm) {
146 mm->pgd = (pgd_t *) (pgd_val(*pgd) & _REGION_ENTRY_ORIGIN);
147 mm->context.asce_limit = _REGION3_SIZE;
148 mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
149 _ASCE_USER_BITS | _ASCE_TYPE_SEGMENT;
150 crst_table_free(mm, (unsigned long *) pgd);
152 if (current->active_mm == mm)
156 static inline unsigned int atomic_xor_bits(atomic_t *v, unsigned int bits)
158 unsigned int old, new;
161 old = atomic_read(v);
163 } while (atomic_cmpxchg(v, old, new) != old);
169 struct page *page_table_alloc_pgste(struct mm_struct *mm)
174 page = alloc_page(GFP_KERNEL);
176 table = (u64 *)page_to_phys(page);
177 memset64(table, _PAGE_INVALID, PTRS_PER_PTE);
178 memset64(table + PTRS_PER_PTE, 0, PTRS_PER_PTE);
183 void page_table_free_pgste(struct page *page)
188 #endif /* CONFIG_PGSTE */
191 * page table entry allocation/free routines.
193 unsigned long *page_table_alloc(struct mm_struct *mm)
195 unsigned long *table;
197 unsigned int mask, bit;
199 /* Try to get a fragment of a 4K page as a 2K page table */
200 if (!mm_alloc_pgste(mm)) {
202 spin_lock_bh(&mm->context.lock);
203 if (!list_empty(&mm->context.pgtable_list)) {
204 page = list_first_entry(&mm->context.pgtable_list,
206 mask = atomic_read(&page->_refcount) >> 24;
207 mask = (mask | (mask >> 4)) & 3;
209 table = (unsigned long *) page_to_phys(page);
210 bit = mask & 1; /* =1 -> second 2K */
212 table += PTRS_PER_PTE;
213 atomic_xor_bits(&page->_refcount,
215 list_del(&page->lru);
218 spin_unlock_bh(&mm->context.lock);
222 /* Allocate a fresh page */
223 page = alloc_page(GFP_KERNEL);
226 if (!pgtable_page_ctor(page)) {
230 arch_set_page_dat(page, 0);
231 /* Initialize page table */
232 table = (unsigned long *) page_to_phys(page);
233 if (mm_alloc_pgste(mm)) {
234 /* Return 4K page table with PGSTEs */
235 atomic_xor_bits(&page->_refcount, 3 << 24);
236 memset64((u64 *)table, _PAGE_INVALID, PTRS_PER_PTE);
237 memset64((u64 *)table + PTRS_PER_PTE, 0, PTRS_PER_PTE);
239 /* Return the first 2K fragment of the page */
240 atomic_xor_bits(&page->_refcount, 1 << 24);
241 memset64((u64 *)table, _PAGE_INVALID, 2 * PTRS_PER_PTE);
242 spin_lock_bh(&mm->context.lock);
243 list_add(&page->lru, &mm->context.pgtable_list);
244 spin_unlock_bh(&mm->context.lock);
249 void page_table_free(struct mm_struct *mm, unsigned long *table)
252 unsigned int bit, mask;
254 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
255 if (!mm_alloc_pgste(mm)) {
256 /* Free 2K page table fragment of a 4K page */
257 bit = (__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t));
258 spin_lock_bh(&mm->context.lock);
259 mask = atomic_xor_bits(&page->_refcount, 1U << (bit + 24));
262 list_add(&page->lru, &mm->context.pgtable_list);
264 list_del(&page->lru);
265 spin_unlock_bh(&mm->context.lock);
269 atomic_xor_bits(&page->_refcount, 3U << 24);
272 pgtable_page_dtor(page);
276 void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table,
277 unsigned long vmaddr)
279 struct mm_struct *mm;
281 unsigned int bit, mask;
284 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
285 if (mm_alloc_pgste(mm)) {
286 gmap_unlink(mm, table, vmaddr);
287 table = (unsigned long *) (__pa(table) | 3);
288 tlb_remove_table(tlb, table);
291 bit = (__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t));
292 spin_lock_bh(&mm->context.lock);
293 mask = atomic_xor_bits(&page->_refcount, 0x11U << (bit + 24));
296 list_add_tail(&page->lru, &mm->context.pgtable_list);
298 list_del(&page->lru);
299 spin_unlock_bh(&mm->context.lock);
300 table = (unsigned long *) (__pa(table) | (1U << bit));
301 tlb_remove_table(tlb, table);
304 static void __tlb_remove_table(void *_table)
306 unsigned int mask = (unsigned long) _table & 3;
307 void *table = (void *)((unsigned long) _table ^ mask);
308 struct page *page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
311 case 0: /* pmd, pud, or p4d */
312 free_pages((unsigned long) table, 2);
314 case 1: /* lower 2K of a 4K page table */
315 case 2: /* higher 2K of a 4K page table */
316 mask = atomic_xor_bits(&page->_refcount, mask << (4 + 24));
321 case 3: /* 4K page table with pgstes */
323 atomic_xor_bits(&page->_refcount, 3 << 24);
324 pgtable_page_dtor(page);
330 static void tlb_remove_table_smp_sync(void *arg)
332 /* Simply deliver the interrupt */
335 static void tlb_remove_table_one(void *table)
338 * This isn't an RCU grace period and hence the page-tables cannot be
339 * assumed to be actually RCU-freed.
341 * It is however sufficient for software page-table walkers that rely
342 * on IRQ disabling. See the comment near struct mmu_table_batch.
344 smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
345 __tlb_remove_table(table);
348 static void tlb_remove_table_rcu(struct rcu_head *head)
350 struct mmu_table_batch *batch;
353 batch = container_of(head, struct mmu_table_batch, rcu);
355 for (i = 0; i < batch->nr; i++)
356 __tlb_remove_table(batch->tables[i]);
358 free_page((unsigned long)batch);
361 void tlb_table_flush(struct mmu_gather *tlb)
363 struct mmu_table_batch **batch = &tlb->batch;
366 call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
371 void tlb_remove_table(struct mmu_gather *tlb, void *table)
373 struct mmu_table_batch **batch = &tlb->batch;
375 tlb->mm->context.flush_mm = 1;
376 if (*batch == NULL) {
377 *batch = (struct mmu_table_batch *)
378 __get_free_page(GFP_NOWAIT | __GFP_NOWARN);
379 if (*batch == NULL) {
380 __tlb_flush_mm_lazy(tlb->mm);
381 tlb_remove_table_one(table);
386 (*batch)->tables[(*batch)->nr++] = table;
387 if ((*batch)->nr == MAX_TABLE_BATCH)
392 * Base infrastructure required to generate basic asces, region, segment,
393 * and page tables that do not make use of enhanced features like EDAT1.
396 static struct kmem_cache *base_pgt_cache;
398 static unsigned long base_pgt_alloc(void)
402 table = kmem_cache_alloc(base_pgt_cache, GFP_KERNEL);
404 memset64(table, _PAGE_INVALID, PTRS_PER_PTE);
405 return (unsigned long) table;
408 static void base_pgt_free(unsigned long table)
410 kmem_cache_free(base_pgt_cache, (void *) table);
413 static unsigned long base_crst_alloc(unsigned long val)
417 table = __get_free_pages(GFP_KERNEL, CRST_ALLOC_ORDER);
419 crst_table_init((unsigned long *)table, val);
423 static void base_crst_free(unsigned long table)
425 free_pages(table, CRST_ALLOC_ORDER);
428 #define BASE_ADDR_END_FUNC(NAME, SIZE) \
429 static inline unsigned long base_##NAME##_addr_end(unsigned long addr, \
432 unsigned long next = (addr + (SIZE)) & ~((SIZE) - 1); \
434 return (next - 1) < (end - 1) ? next : end; \
437 BASE_ADDR_END_FUNC(page, _PAGE_SIZE)
438 BASE_ADDR_END_FUNC(segment, _SEGMENT_SIZE)
439 BASE_ADDR_END_FUNC(region3, _REGION3_SIZE)
440 BASE_ADDR_END_FUNC(region2, _REGION2_SIZE)
441 BASE_ADDR_END_FUNC(region1, _REGION1_SIZE)
443 static inline unsigned long base_lra(unsigned long address)
449 : "=d" (real) : "a" (address) : "cc");
453 static int base_page_walk(unsigned long origin, unsigned long addr,
454 unsigned long end, int alloc)
456 unsigned long *pte, next;
460 pte = (unsigned long *) origin;
461 pte += (addr & _PAGE_INDEX) >> _PAGE_SHIFT;
463 next = base_page_addr_end(addr, end);
464 *pte = base_lra(addr);
465 } while (pte++, addr = next, addr < end);
469 static int base_segment_walk(unsigned long origin, unsigned long addr,
470 unsigned long end, int alloc)
472 unsigned long *ste, next, table;
475 ste = (unsigned long *) origin;
476 ste += (addr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
478 next = base_segment_addr_end(addr, end);
479 if (*ste & _SEGMENT_ENTRY_INVALID) {
482 table = base_pgt_alloc();
485 *ste = table | _SEGMENT_ENTRY;
487 table = *ste & _SEGMENT_ENTRY_ORIGIN;
488 rc = base_page_walk(table, addr, next, alloc);
492 base_pgt_free(table);
494 } while (ste++, addr = next, addr < end);
498 static int base_region3_walk(unsigned long origin, unsigned long addr,
499 unsigned long end, int alloc)
501 unsigned long *rtte, next, table;
504 rtte = (unsigned long *) origin;
505 rtte += (addr & _REGION3_INDEX) >> _REGION3_SHIFT;
507 next = base_region3_addr_end(addr, end);
508 if (*rtte & _REGION_ENTRY_INVALID) {
511 table = base_crst_alloc(_SEGMENT_ENTRY_EMPTY);
514 *rtte = table | _REGION3_ENTRY;
516 table = *rtte & _REGION_ENTRY_ORIGIN;
517 rc = base_segment_walk(table, addr, next, alloc);
521 base_crst_free(table);
522 } while (rtte++, addr = next, addr < end);
526 static int base_region2_walk(unsigned long origin, unsigned long addr,
527 unsigned long end, int alloc)
529 unsigned long *rste, next, table;
532 rste = (unsigned long *) origin;
533 rste += (addr & _REGION2_INDEX) >> _REGION2_SHIFT;
535 next = base_region2_addr_end(addr, end);
536 if (*rste & _REGION_ENTRY_INVALID) {
539 table = base_crst_alloc(_REGION3_ENTRY_EMPTY);
542 *rste = table | _REGION2_ENTRY;
544 table = *rste & _REGION_ENTRY_ORIGIN;
545 rc = base_region3_walk(table, addr, next, alloc);
549 base_crst_free(table);
550 } while (rste++, addr = next, addr < end);
554 static int base_region1_walk(unsigned long origin, unsigned long addr,
555 unsigned long end, int alloc)
557 unsigned long *rfte, next, table;
560 rfte = (unsigned long *) origin;
561 rfte += (addr & _REGION1_INDEX) >> _REGION1_SHIFT;
563 next = base_region1_addr_end(addr, end);
564 if (*rfte & _REGION_ENTRY_INVALID) {
567 table = base_crst_alloc(_REGION2_ENTRY_EMPTY);
570 *rfte = table | _REGION1_ENTRY;
572 table = *rfte & _REGION_ENTRY_ORIGIN;
573 rc = base_region2_walk(table, addr, next, alloc);
577 base_crst_free(table);
578 } while (rfte++, addr = next, addr < end);
583 * base_asce_free - free asce and tables returned from base_asce_alloc()
584 * @asce: asce to be freed
586 * Frees all region, segment, and page tables that were allocated with a
587 * corresponding base_asce_alloc() call.
589 void base_asce_free(unsigned long asce)
591 unsigned long table = asce & _ASCE_ORIGIN;
595 switch (asce & _ASCE_TYPE_MASK) {
596 case _ASCE_TYPE_SEGMENT:
597 base_segment_walk(table, 0, _REGION3_SIZE, 0);
599 case _ASCE_TYPE_REGION3:
600 base_region3_walk(table, 0, _REGION2_SIZE, 0);
602 case _ASCE_TYPE_REGION2:
603 base_region2_walk(table, 0, _REGION1_SIZE, 0);
605 case _ASCE_TYPE_REGION1:
606 base_region1_walk(table, 0, -_PAGE_SIZE, 0);
609 base_crst_free(table);
612 static int base_pgt_cache_init(void)
614 static DEFINE_MUTEX(base_pgt_cache_mutex);
615 unsigned long sz = _PAGE_TABLE_SIZE;
619 mutex_lock(&base_pgt_cache_mutex);
621 base_pgt_cache = kmem_cache_create("base_pgt", sz, sz, 0, NULL);
622 mutex_unlock(&base_pgt_cache_mutex);
623 return base_pgt_cache ? 0 : -ENOMEM;
627 * base_asce_alloc - create kernel mapping without enhanced DAT features
628 * @addr: virtual start address of kernel mapping
629 * @num_pages: number of consecutive pages
631 * Generate an asce, including all required region, segment and page tables,
632 * that can be used to access the virtual kernel mapping. The difference is
633 * that the returned asce does not make use of any enhanced DAT features like
634 * e.g. large pages. This is required for some I/O functions that pass an
635 * asce, like e.g. some service call requests.
637 * Note: the returned asce may NEVER be attached to any cpu. It may only be
638 * used for I/O requests. tlb entries that might result because the
639 * asce was attached to a cpu won't be cleared.
641 unsigned long base_asce_alloc(unsigned long addr, unsigned long num_pages)
643 unsigned long asce, table, end;
646 if (base_pgt_cache_init())
648 end = addr + num_pages * PAGE_SIZE;
649 if (end <= _REGION3_SIZE) {
650 table = base_crst_alloc(_SEGMENT_ENTRY_EMPTY);
653 rc = base_segment_walk(table, addr, end, 1);
654 asce = table | _ASCE_TYPE_SEGMENT | _ASCE_TABLE_LENGTH;
655 } else if (end <= _REGION2_SIZE) {
656 table = base_crst_alloc(_REGION3_ENTRY_EMPTY);
659 rc = base_region3_walk(table, addr, end, 1);
660 asce = table | _ASCE_TYPE_REGION3 | _ASCE_TABLE_LENGTH;
661 } else if (end <= _REGION1_SIZE) {
662 table = base_crst_alloc(_REGION2_ENTRY_EMPTY);
665 rc = base_region2_walk(table, addr, end, 1);
666 asce = table | _ASCE_TYPE_REGION2 | _ASCE_TABLE_LENGTH;
668 table = base_crst_alloc(_REGION1_ENTRY_EMPTY);
671 rc = base_region1_walk(table, addr, end, 1);
672 asce = table | _ASCE_TYPE_REGION1 | _ASCE_TABLE_LENGTH;
675 base_asce_free(asce);