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 int page_table_allocate_pgste
= 0;
21 EXPORT_SYMBOL(page_table_allocate_pgste
);
23 static struct ctl_table page_table_sysctl
[] = {
25 .procname
= "allocate_pgste",
26 .data
= &page_table_allocate_pgste
,
27 .maxlen
= sizeof(int),
28 .mode
= S_IRUGO
| S_IWUSR
,
29 .proc_handler
= proc_dointvec_minmax
,
30 .extra1
= SYSCTL_ZERO
,
36 static struct ctl_table page_table_sysctl_dir
[] = {
41 .child
= page_table_sysctl
,
46 static int __init
page_table_register_sysctl(void)
48 return register_sysctl_table(page_table_sysctl_dir
) ? 0 : -ENOMEM
;
50 __initcall(page_table_register_sysctl
);
52 #endif /* CONFIG_PGSTE */
54 unsigned long *crst_table_alloc(struct mm_struct
*mm
)
56 struct page
*page
= alloc_pages(GFP_KERNEL
, 2);
60 arch_set_page_dat(page
, 2);
61 return (unsigned long *) page_to_phys(page
);
64 void crst_table_free(struct mm_struct
*mm
, unsigned long *table
)
66 free_pages((unsigned long) table
, 2);
69 static void __crst_table_upgrade(void *arg
)
71 struct mm_struct
*mm
= arg
;
73 /* change all active ASCEs to avoid the creation of new TLBs */
74 if (current
->active_mm
== mm
) {
75 S390_lowcore
.user_asce
= mm
->context
.asce
;
76 __ctl_load(S390_lowcore
.user_asce
, 7, 7);
81 int crst_table_upgrade(struct mm_struct
*mm
, unsigned long end
)
83 unsigned long *pgd
= NULL
, *p4d
= NULL
, *__pgd
;
84 unsigned long asce_limit
= mm
->context
.asce_limit
;
86 /* upgrade should only happen from 3 to 4, 3 to 5, or 4 to 5 levels */
87 VM_BUG_ON(asce_limit
< _REGION2_SIZE
);
89 if (end
<= asce_limit
)
92 if (asce_limit
== _REGION2_SIZE
) {
93 p4d
= crst_table_alloc(mm
);
96 crst_table_init(p4d
, _REGION2_ENTRY_EMPTY
);
98 if (end
> _REGION1_SIZE
) {
99 pgd
= crst_table_alloc(mm
);
102 crst_table_init(pgd
, _REGION1_ENTRY_EMPTY
);
105 spin_lock_bh(&mm
->page_table_lock
);
108 * This routine gets called with mmap_lock lock held and there is
109 * no reason to optimize for the case of otherwise. However, if
110 * that would ever change, the below check will let us know.
112 VM_BUG_ON(asce_limit
!= mm
->context
.asce_limit
);
115 __pgd
= (unsigned long *) mm
->pgd
;
116 p4d_populate(mm
, (p4d_t
*) p4d
, (pud_t
*) __pgd
);
117 mm
->pgd
= (pgd_t
*) p4d
;
118 mm
->context
.asce_limit
= _REGION1_SIZE
;
119 mm
->context
.asce
= __pa(mm
->pgd
) | _ASCE_TABLE_LENGTH
|
120 _ASCE_USER_BITS
| _ASCE_TYPE_REGION2
;
124 __pgd
= (unsigned long *) mm
->pgd
;
125 pgd_populate(mm
, (pgd_t
*) pgd
, (p4d_t
*) __pgd
);
126 mm
->pgd
= (pgd_t
*) pgd
;
127 mm
->context
.asce_limit
= TASK_SIZE_MAX
;
128 mm
->context
.asce
= __pa(mm
->pgd
) | _ASCE_TABLE_LENGTH
|
129 _ASCE_USER_BITS
| _ASCE_TYPE_REGION1
;
132 spin_unlock_bh(&mm
->page_table_lock
);
134 on_each_cpu(__crst_table_upgrade
, mm
, 0);
139 crst_table_free(mm
, p4d
);
144 static inline unsigned int atomic_xor_bits(atomic_t
*v
, unsigned int bits
)
146 unsigned int old
, new;
149 old
= atomic_read(v
);
151 } while (atomic_cmpxchg(v
, old
, new) != old
);
157 struct page
*page_table_alloc_pgste(struct mm_struct
*mm
)
162 page
= alloc_page(GFP_KERNEL
);
164 table
= (u64
*)page_to_phys(page
);
165 memset64(table
, _PAGE_INVALID
, PTRS_PER_PTE
);
166 memset64(table
+ PTRS_PER_PTE
, 0, PTRS_PER_PTE
);
171 void page_table_free_pgste(struct page
*page
)
176 #endif /* CONFIG_PGSTE */
179 * page table entry allocation/free routines.
181 unsigned long *page_table_alloc(struct mm_struct
*mm
)
183 unsigned long *table
;
185 unsigned int mask
, bit
;
187 /* Try to get a fragment of a 4K page as a 2K page table */
188 if (!mm_alloc_pgste(mm
)) {
190 spin_lock_bh(&mm
->context
.lock
);
191 if (!list_empty(&mm
->context
.pgtable_list
)) {
192 page
= list_first_entry(&mm
->context
.pgtable_list
,
194 mask
= atomic_read(&page
->_refcount
) >> 24;
195 mask
= (mask
| (mask
>> 4)) & 3;
197 table
= (unsigned long *) page_to_phys(page
);
198 bit
= mask
& 1; /* =1 -> second 2K */
200 table
+= PTRS_PER_PTE
;
201 atomic_xor_bits(&page
->_refcount
,
203 list_del(&page
->lru
);
206 spin_unlock_bh(&mm
->context
.lock
);
210 /* Allocate a fresh page */
211 page
= alloc_page(GFP_KERNEL
);
214 if (!pgtable_pte_page_ctor(page
)) {
218 arch_set_page_dat(page
, 0);
219 /* Initialize page table */
220 table
= (unsigned long *) page_to_phys(page
);
221 if (mm_alloc_pgste(mm
)) {
222 /* Return 4K page table with PGSTEs */
223 atomic_xor_bits(&page
->_refcount
, 3 << 24);
224 memset64((u64
*)table
, _PAGE_INVALID
, PTRS_PER_PTE
);
225 memset64((u64
*)table
+ PTRS_PER_PTE
, 0, PTRS_PER_PTE
);
227 /* Return the first 2K fragment of the page */
228 atomic_xor_bits(&page
->_refcount
, 1 << 24);
229 memset64((u64
*)table
, _PAGE_INVALID
, 2 * PTRS_PER_PTE
);
230 spin_lock_bh(&mm
->context
.lock
);
231 list_add(&page
->lru
, &mm
->context
.pgtable_list
);
232 spin_unlock_bh(&mm
->context
.lock
);
237 void page_table_free(struct mm_struct
*mm
, unsigned long *table
)
240 unsigned int bit
, mask
;
242 page
= pfn_to_page(__pa(table
) >> PAGE_SHIFT
);
243 if (!mm_alloc_pgste(mm
)) {
244 /* Free 2K page table fragment of a 4K page */
245 bit
= (__pa(table
) & ~PAGE_MASK
)/(PTRS_PER_PTE
*sizeof(pte_t
));
246 spin_lock_bh(&mm
->context
.lock
);
247 mask
= atomic_xor_bits(&page
->_refcount
, 1U << (bit
+ 24));
250 list_add(&page
->lru
, &mm
->context
.pgtable_list
);
252 list_del(&page
->lru
);
253 spin_unlock_bh(&mm
->context
.lock
);
257 atomic_xor_bits(&page
->_refcount
, 3U << 24);
260 pgtable_pte_page_dtor(page
);
264 void page_table_free_rcu(struct mmu_gather
*tlb
, unsigned long *table
,
265 unsigned long vmaddr
)
267 struct mm_struct
*mm
;
269 unsigned int bit
, mask
;
272 page
= pfn_to_page(__pa(table
) >> PAGE_SHIFT
);
273 if (mm_alloc_pgste(mm
)) {
274 gmap_unlink(mm
, table
, vmaddr
);
275 table
= (unsigned long *) (__pa(table
) | 3);
276 tlb_remove_table(tlb
, table
);
279 bit
= (__pa(table
) & ~PAGE_MASK
) / (PTRS_PER_PTE
*sizeof(pte_t
));
280 spin_lock_bh(&mm
->context
.lock
);
281 mask
= atomic_xor_bits(&page
->_refcount
, 0x11U
<< (bit
+ 24));
284 list_add_tail(&page
->lru
, &mm
->context
.pgtable_list
);
286 list_del(&page
->lru
);
287 spin_unlock_bh(&mm
->context
.lock
);
288 table
= (unsigned long *) (__pa(table
) | (1U << bit
));
289 tlb_remove_table(tlb
, table
);
292 void __tlb_remove_table(void *_table
)
294 unsigned int mask
= (unsigned long) _table
& 3;
295 void *table
= (void *)((unsigned long) _table
^ mask
);
296 struct page
*page
= pfn_to_page(__pa(table
) >> PAGE_SHIFT
);
299 case 0: /* pmd, pud, or p4d */
300 free_pages((unsigned long) table
, 2);
302 case 1: /* lower 2K of a 4K page table */
303 case 2: /* higher 2K of a 4K page table */
304 mask
= atomic_xor_bits(&page
->_refcount
, mask
<< (4 + 24));
309 case 3: /* 4K page table with pgstes */
311 atomic_xor_bits(&page
->_refcount
, 3 << 24);
312 pgtable_pte_page_dtor(page
);
319 * Base infrastructure required to generate basic asces, region, segment,
320 * and page tables that do not make use of enhanced features like EDAT1.
323 static struct kmem_cache
*base_pgt_cache
;
325 static unsigned long base_pgt_alloc(void)
329 table
= kmem_cache_alloc(base_pgt_cache
, GFP_KERNEL
);
331 memset64(table
, _PAGE_INVALID
, PTRS_PER_PTE
);
332 return (unsigned long) table
;
335 static void base_pgt_free(unsigned long table
)
337 kmem_cache_free(base_pgt_cache
, (void *) table
);
340 static unsigned long base_crst_alloc(unsigned long val
)
344 table
= __get_free_pages(GFP_KERNEL
, CRST_ALLOC_ORDER
);
346 crst_table_init((unsigned long *)table
, val
);
350 static void base_crst_free(unsigned long table
)
352 free_pages(table
, CRST_ALLOC_ORDER
);
355 #define BASE_ADDR_END_FUNC(NAME, SIZE) \
356 static inline unsigned long base_##NAME##_addr_end(unsigned long addr, \
359 unsigned long next = (addr + (SIZE)) & ~((SIZE) - 1); \
361 return (next - 1) < (end - 1) ? next : end; \
364 BASE_ADDR_END_FUNC(page
, _PAGE_SIZE
)
365 BASE_ADDR_END_FUNC(segment
, _SEGMENT_SIZE
)
366 BASE_ADDR_END_FUNC(region3
, _REGION3_SIZE
)
367 BASE_ADDR_END_FUNC(region2
, _REGION2_SIZE
)
368 BASE_ADDR_END_FUNC(region1
, _REGION1_SIZE
)
370 static inline unsigned long base_lra(unsigned long address
)
376 : "=d" (real
) : "a" (address
) : "cc");
380 static int base_page_walk(unsigned long origin
, unsigned long addr
,
381 unsigned long end
, int alloc
)
383 unsigned long *pte
, next
;
387 pte
= (unsigned long *) origin
;
388 pte
+= (addr
& _PAGE_INDEX
) >> _PAGE_SHIFT
;
390 next
= base_page_addr_end(addr
, end
);
391 *pte
= base_lra(addr
);
392 } while (pte
++, addr
= next
, addr
< end
);
396 static int base_segment_walk(unsigned long origin
, unsigned long addr
,
397 unsigned long end
, int alloc
)
399 unsigned long *ste
, next
, table
;
402 ste
= (unsigned long *) origin
;
403 ste
+= (addr
& _SEGMENT_INDEX
) >> _SEGMENT_SHIFT
;
405 next
= base_segment_addr_end(addr
, end
);
406 if (*ste
& _SEGMENT_ENTRY_INVALID
) {
409 table
= base_pgt_alloc();
412 *ste
= table
| _SEGMENT_ENTRY
;
414 table
= *ste
& _SEGMENT_ENTRY_ORIGIN
;
415 rc
= base_page_walk(table
, addr
, next
, alloc
);
419 base_pgt_free(table
);
421 } while (ste
++, addr
= next
, addr
< end
);
425 static int base_region3_walk(unsigned long origin
, unsigned long addr
,
426 unsigned long end
, int alloc
)
428 unsigned long *rtte
, next
, table
;
431 rtte
= (unsigned long *) origin
;
432 rtte
+= (addr
& _REGION3_INDEX
) >> _REGION3_SHIFT
;
434 next
= base_region3_addr_end(addr
, end
);
435 if (*rtte
& _REGION_ENTRY_INVALID
) {
438 table
= base_crst_alloc(_SEGMENT_ENTRY_EMPTY
);
441 *rtte
= table
| _REGION3_ENTRY
;
443 table
= *rtte
& _REGION_ENTRY_ORIGIN
;
444 rc
= base_segment_walk(table
, addr
, next
, alloc
);
448 base_crst_free(table
);
449 } while (rtte
++, addr
= next
, addr
< end
);
453 static int base_region2_walk(unsigned long origin
, unsigned long addr
,
454 unsigned long end
, int alloc
)
456 unsigned long *rste
, next
, table
;
459 rste
= (unsigned long *) origin
;
460 rste
+= (addr
& _REGION2_INDEX
) >> _REGION2_SHIFT
;
462 next
= base_region2_addr_end(addr
, end
);
463 if (*rste
& _REGION_ENTRY_INVALID
) {
466 table
= base_crst_alloc(_REGION3_ENTRY_EMPTY
);
469 *rste
= table
| _REGION2_ENTRY
;
471 table
= *rste
& _REGION_ENTRY_ORIGIN
;
472 rc
= base_region3_walk(table
, addr
, next
, alloc
);
476 base_crst_free(table
);
477 } while (rste
++, addr
= next
, addr
< end
);
481 static int base_region1_walk(unsigned long origin
, unsigned long addr
,
482 unsigned long end
, int alloc
)
484 unsigned long *rfte
, next
, table
;
487 rfte
= (unsigned long *) origin
;
488 rfte
+= (addr
& _REGION1_INDEX
) >> _REGION1_SHIFT
;
490 next
= base_region1_addr_end(addr
, end
);
491 if (*rfte
& _REGION_ENTRY_INVALID
) {
494 table
= base_crst_alloc(_REGION2_ENTRY_EMPTY
);
497 *rfte
= table
| _REGION1_ENTRY
;
499 table
= *rfte
& _REGION_ENTRY_ORIGIN
;
500 rc
= base_region2_walk(table
, addr
, next
, alloc
);
504 base_crst_free(table
);
505 } while (rfte
++, addr
= next
, addr
< end
);
510 * base_asce_free - free asce and tables returned from base_asce_alloc()
511 * @asce: asce to be freed
513 * Frees all region, segment, and page tables that were allocated with a
514 * corresponding base_asce_alloc() call.
516 void base_asce_free(unsigned long asce
)
518 unsigned long table
= asce
& _ASCE_ORIGIN
;
522 switch (asce
& _ASCE_TYPE_MASK
) {
523 case _ASCE_TYPE_SEGMENT
:
524 base_segment_walk(table
, 0, _REGION3_SIZE
, 0);
526 case _ASCE_TYPE_REGION3
:
527 base_region3_walk(table
, 0, _REGION2_SIZE
, 0);
529 case _ASCE_TYPE_REGION2
:
530 base_region2_walk(table
, 0, _REGION1_SIZE
, 0);
532 case _ASCE_TYPE_REGION1
:
533 base_region1_walk(table
, 0, TASK_SIZE_MAX
, 0);
536 base_crst_free(table
);
539 static int base_pgt_cache_init(void)
541 static DEFINE_MUTEX(base_pgt_cache_mutex
);
542 unsigned long sz
= _PAGE_TABLE_SIZE
;
546 mutex_lock(&base_pgt_cache_mutex
);
548 base_pgt_cache
= kmem_cache_create("base_pgt", sz
, sz
, 0, NULL
);
549 mutex_unlock(&base_pgt_cache_mutex
);
550 return base_pgt_cache
? 0 : -ENOMEM
;
554 * base_asce_alloc - create kernel mapping without enhanced DAT features
555 * @addr: virtual start address of kernel mapping
556 * @num_pages: number of consecutive pages
558 * Generate an asce, including all required region, segment and page tables,
559 * that can be used to access the virtual kernel mapping. The difference is
560 * that the returned asce does not make use of any enhanced DAT features like
561 * e.g. large pages. This is required for some I/O functions that pass an
562 * asce, like e.g. some service call requests.
564 * Note: the returned asce may NEVER be attached to any cpu. It may only be
565 * used for I/O requests. tlb entries that might result because the
566 * asce was attached to a cpu won't be cleared.
568 unsigned long base_asce_alloc(unsigned long addr
, unsigned long num_pages
)
570 unsigned long asce
, table
, end
;
573 if (base_pgt_cache_init())
575 end
= addr
+ num_pages
* PAGE_SIZE
;
576 if (end
<= _REGION3_SIZE
) {
577 table
= base_crst_alloc(_SEGMENT_ENTRY_EMPTY
);
580 rc
= base_segment_walk(table
, addr
, end
, 1);
581 asce
= table
| _ASCE_TYPE_SEGMENT
| _ASCE_TABLE_LENGTH
;
582 } else if (end
<= _REGION2_SIZE
) {
583 table
= base_crst_alloc(_REGION3_ENTRY_EMPTY
);
586 rc
= base_region3_walk(table
, addr
, end
, 1);
587 asce
= table
| _ASCE_TYPE_REGION3
| _ASCE_TABLE_LENGTH
;
588 } else if (end
<= _REGION1_SIZE
) {
589 table
= base_crst_alloc(_REGION2_ENTRY_EMPTY
);
592 rc
= base_region2_walk(table
, addr
, end
, 1);
593 asce
= table
| _ASCE_TYPE_REGION2
| _ASCE_TABLE_LENGTH
;
595 table
= base_crst_alloc(_REGION1_ENTRY_EMPTY
);
598 rc
= base_region1_walk(table
, addr
, end
, 1);
599 asce
= table
| _ASCE_TYPE_REGION1
| _ASCE_TABLE_LENGTH
;
602 base_asce_free(asce
);