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, 0x11U << (bit + 24));
262 list_add(&page->lru, &mm->context.pgtable_list);
264 list_del(&page->lru);
265 spin_unlock_bh(&mm->context.lock);
266 mask = atomic_xor_bits(&page->_refcount, 0x10U << (bit + 24));
271 atomic_xor_bits(&page->_refcount, 3U << 24);
274 pgtable_page_dtor(page);
278 void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table,
279 unsigned long vmaddr)
281 struct mm_struct *mm;
283 unsigned int bit, mask;
286 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
287 if (mm_alloc_pgste(mm)) {
288 gmap_unlink(mm, table, vmaddr);
289 table = (unsigned long *) (__pa(table) | 3);
290 tlb_remove_table(tlb, table);
293 bit = (__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t));
294 spin_lock_bh(&mm->context.lock);
295 mask = atomic_xor_bits(&page->_refcount, 0x11U << (bit + 24));
298 list_add_tail(&page->lru, &mm->context.pgtable_list);
300 list_del(&page->lru);
301 spin_unlock_bh(&mm->context.lock);
302 table = (unsigned long *) (__pa(table) | (1U << bit));
303 tlb_remove_table(tlb, table);
306 static void __tlb_remove_table(void *_table)
308 unsigned int mask = (unsigned long) _table & 3;
309 void *table = (void *)((unsigned long) _table ^ mask);
310 struct page *page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
313 case 0: /* pmd, pud, or p4d */
314 free_pages((unsigned long) table, 2);
316 case 1: /* lower 2K of a 4K page table */
317 case 2: /* higher 2K of a 4K page table */
318 mask = atomic_xor_bits(&page->_refcount, mask << (4 + 24));
323 case 3: /* 4K page table with pgstes */
325 atomic_xor_bits(&page->_refcount, 3 << 24);
326 pgtable_page_dtor(page);
332 static void tlb_remove_table_smp_sync(void *arg)
334 /* Simply deliver the interrupt */
337 static void tlb_remove_table_one(void *table)
340 * This isn't an RCU grace period and hence the page-tables cannot be
341 * assumed to be actually RCU-freed.
343 * It is however sufficient for software page-table walkers that rely
344 * on IRQ disabling. See the comment near struct mmu_table_batch.
346 smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
347 __tlb_remove_table(table);
350 static void tlb_remove_table_rcu(struct rcu_head *head)
352 struct mmu_table_batch *batch;
355 batch = container_of(head, struct mmu_table_batch, rcu);
357 for (i = 0; i < batch->nr; i++)
358 __tlb_remove_table(batch->tables[i]);
360 free_page((unsigned long)batch);
363 void tlb_table_flush(struct mmu_gather *tlb)
365 struct mmu_table_batch **batch = &tlb->batch;
368 call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
373 void tlb_remove_table(struct mmu_gather *tlb, void *table)
375 struct mmu_table_batch **batch = &tlb->batch;
377 tlb->mm->context.flush_mm = 1;
378 if (*batch == NULL) {
379 *batch = (struct mmu_table_batch *)
380 __get_free_page(GFP_NOWAIT | __GFP_NOWARN);
381 if (*batch == NULL) {
382 __tlb_flush_mm_lazy(tlb->mm);
383 tlb_remove_table_one(table);
388 (*batch)->tables[(*batch)->nr++] = table;
389 if ((*batch)->nr == MAX_TABLE_BATCH)
394 * Base infrastructure required to generate basic asces, region, segment,
395 * and page tables that do not make use of enhanced features like EDAT1.
398 static struct kmem_cache *base_pgt_cache;
400 static unsigned long base_pgt_alloc(void)
404 table = kmem_cache_alloc(base_pgt_cache, GFP_KERNEL);
406 memset64(table, _PAGE_INVALID, PTRS_PER_PTE);
407 return (unsigned long) table;
410 static void base_pgt_free(unsigned long table)
412 kmem_cache_free(base_pgt_cache, (void *) table);
415 static unsigned long base_crst_alloc(unsigned long val)
419 table = __get_free_pages(GFP_KERNEL, CRST_ALLOC_ORDER);
421 crst_table_init((unsigned long *)table, val);
425 static void base_crst_free(unsigned long table)
427 free_pages(table, CRST_ALLOC_ORDER);
430 #define BASE_ADDR_END_FUNC(NAME, SIZE) \
431 static inline unsigned long base_##NAME##_addr_end(unsigned long addr, \
434 unsigned long next = (addr + (SIZE)) & ~((SIZE) - 1); \
436 return (next - 1) < (end - 1) ? next : end; \
439 BASE_ADDR_END_FUNC(page, _PAGE_SIZE)
440 BASE_ADDR_END_FUNC(segment, _SEGMENT_SIZE)
441 BASE_ADDR_END_FUNC(region3, _REGION3_SIZE)
442 BASE_ADDR_END_FUNC(region2, _REGION2_SIZE)
443 BASE_ADDR_END_FUNC(region1, _REGION1_SIZE)
445 static inline unsigned long base_lra(unsigned long address)
451 : "=d" (real) : "a" (address) : "cc");
455 static int base_page_walk(unsigned long origin, unsigned long addr,
456 unsigned long end, int alloc)
458 unsigned long *pte, next;
462 pte = (unsigned long *) origin;
463 pte += (addr & _PAGE_INDEX) >> _PAGE_SHIFT;
465 next = base_page_addr_end(addr, end);
466 *pte = base_lra(addr);
467 } while (pte++, addr = next, addr < end);
471 static int base_segment_walk(unsigned long origin, unsigned long addr,
472 unsigned long end, int alloc)
474 unsigned long *ste, next, table;
477 ste = (unsigned long *) origin;
478 ste += (addr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
480 next = base_segment_addr_end(addr, end);
481 if (*ste & _SEGMENT_ENTRY_INVALID) {
484 table = base_pgt_alloc();
487 *ste = table | _SEGMENT_ENTRY;
489 table = *ste & _SEGMENT_ENTRY_ORIGIN;
490 rc = base_page_walk(table, addr, next, alloc);
494 base_pgt_free(table);
496 } while (ste++, addr = next, addr < end);
500 static int base_region3_walk(unsigned long origin, unsigned long addr,
501 unsigned long end, int alloc)
503 unsigned long *rtte, next, table;
506 rtte = (unsigned long *) origin;
507 rtte += (addr & _REGION3_INDEX) >> _REGION3_SHIFT;
509 next = base_region3_addr_end(addr, end);
510 if (*rtte & _REGION_ENTRY_INVALID) {
513 table = base_crst_alloc(_SEGMENT_ENTRY_EMPTY);
516 *rtte = table | _REGION3_ENTRY;
518 table = *rtte & _REGION_ENTRY_ORIGIN;
519 rc = base_segment_walk(table, addr, next, alloc);
523 base_crst_free(table);
524 } while (rtte++, addr = next, addr < end);
528 static int base_region2_walk(unsigned long origin, unsigned long addr,
529 unsigned long end, int alloc)
531 unsigned long *rste, next, table;
534 rste = (unsigned long *) origin;
535 rste += (addr & _REGION2_INDEX) >> _REGION2_SHIFT;
537 next = base_region2_addr_end(addr, end);
538 if (*rste & _REGION_ENTRY_INVALID) {
541 table = base_crst_alloc(_REGION3_ENTRY_EMPTY);
544 *rste = table | _REGION2_ENTRY;
546 table = *rste & _REGION_ENTRY_ORIGIN;
547 rc = base_region3_walk(table, addr, next, alloc);
551 base_crst_free(table);
552 } while (rste++, addr = next, addr < end);
556 static int base_region1_walk(unsigned long origin, unsigned long addr,
557 unsigned long end, int alloc)
559 unsigned long *rfte, next, table;
562 rfte = (unsigned long *) origin;
563 rfte += (addr & _REGION1_INDEX) >> _REGION1_SHIFT;
565 next = base_region1_addr_end(addr, end);
566 if (*rfte & _REGION_ENTRY_INVALID) {
569 table = base_crst_alloc(_REGION2_ENTRY_EMPTY);
572 *rfte = table | _REGION1_ENTRY;
574 table = *rfte & _REGION_ENTRY_ORIGIN;
575 rc = base_region2_walk(table, addr, next, alloc);
579 base_crst_free(table);
580 } while (rfte++, addr = next, addr < end);
585 * base_asce_free - free asce and tables returned from base_asce_alloc()
586 * @asce: asce to be freed
588 * Frees all region, segment, and page tables that were allocated with a
589 * corresponding base_asce_alloc() call.
591 void base_asce_free(unsigned long asce)
593 unsigned long table = asce & _ASCE_ORIGIN;
597 switch (asce & _ASCE_TYPE_MASK) {
598 case _ASCE_TYPE_SEGMENT:
599 base_segment_walk(table, 0, _REGION3_SIZE, 0);
601 case _ASCE_TYPE_REGION3:
602 base_region3_walk(table, 0, _REGION2_SIZE, 0);
604 case _ASCE_TYPE_REGION2:
605 base_region2_walk(table, 0, _REGION1_SIZE, 0);
607 case _ASCE_TYPE_REGION1:
608 base_region1_walk(table, 0, -_PAGE_SIZE, 0);
611 base_crst_free(table);
614 static int base_pgt_cache_init(void)
616 static DEFINE_MUTEX(base_pgt_cache_mutex);
617 unsigned long sz = _PAGE_TABLE_SIZE;
621 mutex_lock(&base_pgt_cache_mutex);
623 base_pgt_cache = kmem_cache_create("base_pgt", sz, sz, 0, NULL);
624 mutex_unlock(&base_pgt_cache_mutex);
625 return base_pgt_cache ? 0 : -ENOMEM;
629 * base_asce_alloc - create kernel mapping without enhanced DAT features
630 * @addr: virtual start address of kernel mapping
631 * @num_pages: number of consecutive pages
633 * Generate an asce, including all required region, segment and page tables,
634 * that can be used to access the virtual kernel mapping. The difference is
635 * that the returned asce does not make use of any enhanced DAT features like
636 * e.g. large pages. This is required for some I/O functions that pass an
637 * asce, like e.g. some service call requests.
639 * Note: the returned asce may NEVER be attached to any cpu. It may only be
640 * used for I/O requests. tlb entries that might result because the
641 * asce was attached to a cpu won't be cleared.
643 unsigned long base_asce_alloc(unsigned long addr, unsigned long num_pages)
645 unsigned long asce, table, end;
648 if (base_pgt_cache_init())
650 end = addr + num_pages * PAGE_SIZE;
651 if (end <= _REGION3_SIZE) {
652 table = base_crst_alloc(_SEGMENT_ENTRY_EMPTY);
655 rc = base_segment_walk(table, addr, end, 1);
656 asce = table | _ASCE_TYPE_SEGMENT | _ASCE_TABLE_LENGTH;
657 } else if (end <= _REGION2_SIZE) {
658 table = base_crst_alloc(_REGION3_ENTRY_EMPTY);
661 rc = base_region3_walk(table, addr, end, 1);
662 asce = table | _ASCE_TYPE_REGION3 | _ASCE_TABLE_LENGTH;
663 } else if (end <= _REGION1_SIZE) {
664 table = base_crst_alloc(_REGION2_ENTRY_EMPTY);
667 rc = base_region2_walk(table, addr, end, 1);
668 asce = table | _ASCE_TYPE_REGION2 | _ASCE_TABLE_LENGTH;
670 table = base_crst_alloc(_REGION1_ENTRY_EMPTY);
673 rc = base_region1_walk(table, addr, end, 1);
674 asce = table | _ASCE_TYPE_REGION1 | _ASCE_TABLE_LENGTH;
677 base_asce_free(asce);