blob: 90943bf3e5eecd1c2ee072448aec7ff83d2b9aa1 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// 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/pgalloc.h>
14#include <asm/gmap.h>
15#include <asm/tlb.h>
16#include <asm/tlbflush.h>
17
18#ifdef CONFIG_PGSTE
19
20int page_table_allocate_pgste = 0;
21EXPORT_SYMBOL(page_table_allocate_pgste);
22
23static struct ctl_table page_table_sysctl[] = {
24 {
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,
31 .extra2 = SYSCTL_ONE,
32 },
33 { }
34};
35
36static struct ctl_table page_table_sysctl_dir[] = {
37 {
38 .procname = "vm",
39 .maxlen = 0,
40 .mode = 0555,
41 .child = page_table_sysctl,
42 },
43 { }
44};
45
46static int __init page_table_register_sysctl(void)
47{
48 return register_sysctl_table(page_table_sysctl_dir) ? 0 : -ENOMEM;
49}
50__initcall(page_table_register_sysctl);
51
52#endif /* CONFIG_PGSTE */
53
54unsigned long *crst_table_alloc(struct mm_struct *mm)
55{
56 struct page *page = alloc_pages(GFP_KERNEL, 2);
57
58 if (!page)
59 return NULL;
60 arch_set_page_dat(page, 2);
61 return (unsigned long *) page_to_phys(page);
62}
63
64void crst_table_free(struct mm_struct *mm, unsigned long *table)
65{
66 free_pages((unsigned long) table, 2);
67}
68
69static void __crst_table_upgrade(void *arg)
70{
71 struct mm_struct *mm = arg;
72
73 /* we must 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 if (current->thread.mm_segment == USER_DS) {
77 __ctl_load(S390_lowcore.user_asce, 1, 1);
78 /* Mark user-ASCE present in CR1 */
79 clear_cpu_flag(CIF_ASCE_PRIMARY);
80 }
81 if (current->thread.mm_segment == USER_DS_SACF) {
82 __ctl_load(S390_lowcore.user_asce, 7, 7);
83 /* enable_sacf_uaccess does all or nothing */
84 WARN_ON(!test_cpu_flag(CIF_ASCE_SECONDARY));
85 }
86 }
87 __tlb_flush_local();
88}
89
90int crst_table_upgrade(struct mm_struct *mm, unsigned long end)
91{
92 unsigned long *table, *pgd;
93 int rc, notify;
94
95 /* upgrade should only happen from 3 to 4, 3 to 5, or 4 to 5 levels */
96 VM_BUG_ON(mm->context.asce_limit < _REGION2_SIZE);
97 rc = 0;
98 notify = 0;
99 while (mm->context.asce_limit < end) {
100 table = crst_table_alloc(mm);
101 if (!table) {
102 rc = -ENOMEM;
103 break;
104 }
105 spin_lock_bh(&mm->page_table_lock);
106 pgd = (unsigned long *) mm->pgd;
107 if (mm->context.asce_limit == _REGION2_SIZE) {
108 crst_table_init(table, _REGION2_ENTRY_EMPTY);
109 p4d_populate(mm, (p4d_t *) table, (pud_t *) pgd);
110 mm->pgd = (pgd_t *) table;
111 mm->context.asce_limit = _REGION1_SIZE;
112 mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
113 _ASCE_USER_BITS | _ASCE_TYPE_REGION2;
114 mm_inc_nr_puds(mm);
115 } else {
116 crst_table_init(table, _REGION1_ENTRY_EMPTY);
117 pgd_populate(mm, (pgd_t *) table, (p4d_t *) pgd);
118 mm->pgd = (pgd_t *) table;
119 mm->context.asce_limit = -PAGE_SIZE;
120 mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
121 _ASCE_USER_BITS | _ASCE_TYPE_REGION1;
122 }
123 notify = 1;
124 spin_unlock_bh(&mm->page_table_lock);
125 }
126 if (notify)
127 on_each_cpu(__crst_table_upgrade, mm, 0);
128 return rc;
129}
130
131void crst_table_downgrade(struct mm_struct *mm)
132{
133 pgd_t *pgd;
134
135 /* downgrade should only happen from 3 to 2 levels (compat only) */
136 VM_BUG_ON(mm->context.asce_limit != _REGION2_SIZE);
137
138 if (current->active_mm == mm) {
139 clear_user_asce();
140 __tlb_flush_mm(mm);
141 }
142
143 pgd = mm->pgd;
144 mm_dec_nr_pmds(mm);
145 mm->pgd = (pgd_t *) (pgd_val(*pgd) & _REGION_ENTRY_ORIGIN);
146 mm->context.asce_limit = _REGION3_SIZE;
147 mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
148 _ASCE_USER_BITS | _ASCE_TYPE_SEGMENT;
149 crst_table_free(mm, (unsigned long *) pgd);
150
151 if (current->active_mm == mm)
152 set_user_asce(mm);
153}
154
155static inline unsigned int atomic_xor_bits(atomic_t *v, unsigned int bits)
156{
157 unsigned int old, new;
158
159 do {
160 old = atomic_read(v);
161 new = old ^ bits;
162 } while (atomic_cmpxchg(v, old, new) != old);
163 return new;
164}
165
166#ifdef CONFIG_PGSTE
167
168struct page *page_table_alloc_pgste(struct mm_struct *mm)
169{
170 struct page *page;
171 u64 *table;
172
173 page = alloc_page(GFP_KERNEL);
174 if (page) {
175 table = (u64 *)page_to_phys(page);
176 memset64(table, _PAGE_INVALID, PTRS_PER_PTE);
177 memset64(table + PTRS_PER_PTE, 0, PTRS_PER_PTE);
178 }
179 return page;
180}
181
182void page_table_free_pgste(struct page *page)
183{
184 __free_page(page);
185}
186
187#endif /* CONFIG_PGSTE */
188
189/*
190 * page table entry allocation/free routines.
191 */
192unsigned long *page_table_alloc(struct mm_struct *mm)
193{
194 unsigned long *table;
195 struct page *page;
196 unsigned int mask, bit;
197
198 /* Try to get a fragment of a 4K page as a 2K page table */
199 if (!mm_alloc_pgste(mm)) {
200 table = NULL;
201 spin_lock_bh(&mm->context.lock);
202 if (!list_empty(&mm->context.pgtable_list)) {
203 page = list_first_entry(&mm->context.pgtable_list,
204 struct page, lru);
205 mask = atomic_read(&page->_refcount) >> 24;
206 mask = (mask | (mask >> 4)) & 3;
207 if (mask != 3) {
208 table = (unsigned long *) page_to_phys(page);
209 bit = mask & 1; /* =1 -> second 2K */
210 if (bit)
211 table += PTRS_PER_PTE;
212 atomic_xor_bits(&page->_refcount,
213 1U << (bit + 24));
214 list_del(&page->lru);
215 }
216 }
217 spin_unlock_bh(&mm->context.lock);
218 if (table)
219 return table;
220 }
221 /* Allocate a fresh page */
222 page = alloc_page(GFP_KERNEL);
223 if (!page)
224 return NULL;
225 if (!pgtable_pte_page_ctor(page)) {
226 __free_page(page);
227 return NULL;
228 }
229 arch_set_page_dat(page, 0);
230 /* Initialize page table */
231 table = (unsigned long *) page_to_phys(page);
232 if (mm_alloc_pgste(mm)) {
233 /* Return 4K page table with PGSTEs */
234 atomic_xor_bits(&page->_refcount, 3 << 24);
235 memset64((u64 *)table, _PAGE_INVALID, PTRS_PER_PTE);
236 memset64((u64 *)table + PTRS_PER_PTE, 0, PTRS_PER_PTE);
237 } else {
238 /* Return the first 2K fragment of the page */
239 atomic_xor_bits(&page->_refcount, 1 << 24);
240 memset64((u64 *)table, _PAGE_INVALID, 2 * PTRS_PER_PTE);
241 spin_lock_bh(&mm->context.lock);
242 list_add(&page->lru, &mm->context.pgtable_list);
243 spin_unlock_bh(&mm->context.lock);
244 }
245 return table;
246}
247
248void page_table_free(struct mm_struct *mm, unsigned long *table)
249{
250 struct page *page;
251 unsigned int bit, mask;
252
253 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
254 if (!mm_alloc_pgste(mm)) {
255 /* Free 2K page table fragment of a 4K page */
256 bit = (__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t));
257 spin_lock_bh(&mm->context.lock);
258 mask = atomic_xor_bits(&page->_refcount, 0x11U << (bit + 24));
259 mask >>= 24;
260 if (mask & 3)
261 list_add(&page->lru, &mm->context.pgtable_list);
262 else
263 list_del(&page->lru);
264 spin_unlock_bh(&mm->context.lock);
265 mask = atomic_xor_bits(&page->_refcount, 0x10U << (bit + 24));
266 mask >>= 24;
267 if (mask != 0)
268 return;
269 } else {
270 atomic_xor_bits(&page->_refcount, 3U << 24);
271 }
272
273 pgtable_pte_page_dtor(page);
274 __free_page(page);
275}
276
277void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table,
278 unsigned long vmaddr)
279{
280 struct mm_struct *mm;
281 struct page *page;
282 unsigned int bit, mask;
283
284 mm = tlb->mm;
285 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
286 if (mm_alloc_pgste(mm)) {
287 gmap_unlink(mm, table, vmaddr);
288 table = (unsigned long *) (__pa(table) | 3);
289 tlb_remove_table(tlb, table);
290 return;
291 }
292 bit = (__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t));
293 spin_lock_bh(&mm->context.lock);
294 mask = atomic_xor_bits(&page->_refcount, 0x11U << (bit + 24));
295 mask >>= 24;
296 if (mask & 3)
297 list_add_tail(&page->lru, &mm->context.pgtable_list);
298 else
299 list_del(&page->lru);
300 spin_unlock_bh(&mm->context.lock);
301 table = (unsigned long *) (__pa(table) | (1U << bit));
302 tlb_remove_table(tlb, table);
303}
304
305void __tlb_remove_table(void *_table)
306{
307 unsigned int mask = (unsigned long) _table & 3;
308 void *table = (void *)((unsigned long) _table ^ mask);
309 struct page *page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
310
311 switch (mask) {
312 case 0: /* pmd, pud, or p4d */
313 free_pages((unsigned long) table, 2);
314 break;
315 case 1: /* lower 2K of a 4K page table */
316 case 2: /* higher 2K of a 4K page table */
317 mask = atomic_xor_bits(&page->_refcount, mask << (4 + 24));
318 mask >>= 24;
319 if (mask != 0)
320 break;
321 /* fallthrough */
322 case 3: /* 4K page table with pgstes */
323 if (mask & 3)
324 atomic_xor_bits(&page->_refcount, 3 << 24);
325 pgtable_pte_page_dtor(page);
326 __free_page(page);
327 break;
328 }
329}
330
331/*
332 * Base infrastructure required to generate basic asces, region, segment,
333 * and page tables that do not make use of enhanced features like EDAT1.
334 */
335
336static struct kmem_cache *base_pgt_cache;
337
338static unsigned long base_pgt_alloc(void)
339{
340 u64 *table;
341
342 table = kmem_cache_alloc(base_pgt_cache, GFP_KERNEL);
343 if (table)
344 memset64(table, _PAGE_INVALID, PTRS_PER_PTE);
345 return (unsigned long) table;
346}
347
348static void base_pgt_free(unsigned long table)
349{
350 kmem_cache_free(base_pgt_cache, (void *) table);
351}
352
353static unsigned long base_crst_alloc(unsigned long val)
354{
355 unsigned long table;
356
357 table = __get_free_pages(GFP_KERNEL, CRST_ALLOC_ORDER);
358 if (table)
359 crst_table_init((unsigned long *)table, val);
360 return table;
361}
362
363static void base_crst_free(unsigned long table)
364{
365 free_pages(table, CRST_ALLOC_ORDER);
366}
367
368#define BASE_ADDR_END_FUNC(NAME, SIZE) \
369static inline unsigned long base_##NAME##_addr_end(unsigned long addr, \
370 unsigned long end) \
371{ \
372 unsigned long next = (addr + (SIZE)) & ~((SIZE) - 1); \
373 \
374 return (next - 1) < (end - 1) ? next : end; \
375}
376
377BASE_ADDR_END_FUNC(page, _PAGE_SIZE)
378BASE_ADDR_END_FUNC(segment, _SEGMENT_SIZE)
379BASE_ADDR_END_FUNC(region3, _REGION3_SIZE)
380BASE_ADDR_END_FUNC(region2, _REGION2_SIZE)
381BASE_ADDR_END_FUNC(region1, _REGION1_SIZE)
382
383static inline unsigned long base_lra(unsigned long address)
384{
385 unsigned long real;
386
387 asm volatile(
388 " lra %0,0(%1)\n"
389 : "=d" (real) : "a" (address) : "cc");
390 return real;
391}
392
393static int base_page_walk(unsigned long origin, unsigned long addr,
394 unsigned long end, int alloc)
395{
396 unsigned long *pte, next;
397
398 if (!alloc)
399 return 0;
400 pte = (unsigned long *) origin;
401 pte += (addr & _PAGE_INDEX) >> _PAGE_SHIFT;
402 do {
403 next = base_page_addr_end(addr, end);
404 *pte = base_lra(addr);
405 } while (pte++, addr = next, addr < end);
406 return 0;
407}
408
409static int base_segment_walk(unsigned long origin, unsigned long addr,
410 unsigned long end, int alloc)
411{
412 unsigned long *ste, next, table;
413 int rc;
414
415 ste = (unsigned long *) origin;
416 ste += (addr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
417 do {
418 next = base_segment_addr_end(addr, end);
419 if (*ste & _SEGMENT_ENTRY_INVALID) {
420 if (!alloc)
421 continue;
422 table = base_pgt_alloc();
423 if (!table)
424 return -ENOMEM;
425 *ste = table | _SEGMENT_ENTRY;
426 }
427 table = *ste & _SEGMENT_ENTRY_ORIGIN;
428 rc = base_page_walk(table, addr, next, alloc);
429 if (rc)
430 return rc;
431 if (!alloc)
432 base_pgt_free(table);
433 cond_resched();
434 } while (ste++, addr = next, addr < end);
435 return 0;
436}
437
438static int base_region3_walk(unsigned long origin, unsigned long addr,
439 unsigned long end, int alloc)
440{
441 unsigned long *rtte, next, table;
442 int rc;
443
444 rtte = (unsigned long *) origin;
445 rtte += (addr & _REGION3_INDEX) >> _REGION3_SHIFT;
446 do {
447 next = base_region3_addr_end(addr, end);
448 if (*rtte & _REGION_ENTRY_INVALID) {
449 if (!alloc)
450 continue;
451 table = base_crst_alloc(_SEGMENT_ENTRY_EMPTY);
452 if (!table)
453 return -ENOMEM;
454 *rtte = table | _REGION3_ENTRY;
455 }
456 table = *rtte & _REGION_ENTRY_ORIGIN;
457 rc = base_segment_walk(table, addr, next, alloc);
458 if (rc)
459 return rc;
460 if (!alloc)
461 base_crst_free(table);
462 } while (rtte++, addr = next, addr < end);
463 return 0;
464}
465
466static int base_region2_walk(unsigned long origin, unsigned long addr,
467 unsigned long end, int alloc)
468{
469 unsigned long *rste, next, table;
470 int rc;
471
472 rste = (unsigned long *) origin;
473 rste += (addr & _REGION2_INDEX) >> _REGION2_SHIFT;
474 do {
475 next = base_region2_addr_end(addr, end);
476 if (*rste & _REGION_ENTRY_INVALID) {
477 if (!alloc)
478 continue;
479 table = base_crst_alloc(_REGION3_ENTRY_EMPTY);
480 if (!table)
481 return -ENOMEM;
482 *rste = table | _REGION2_ENTRY;
483 }
484 table = *rste & _REGION_ENTRY_ORIGIN;
485 rc = base_region3_walk(table, addr, next, alloc);
486 if (rc)
487 return rc;
488 if (!alloc)
489 base_crst_free(table);
490 } while (rste++, addr = next, addr < end);
491 return 0;
492}
493
494static int base_region1_walk(unsigned long origin, unsigned long addr,
495 unsigned long end, int alloc)
496{
497 unsigned long *rfte, next, table;
498 int rc;
499
500 rfte = (unsigned long *) origin;
501 rfte += (addr & _REGION1_INDEX) >> _REGION1_SHIFT;
502 do {
503 next = base_region1_addr_end(addr, end);
504 if (*rfte & _REGION_ENTRY_INVALID) {
505 if (!alloc)
506 continue;
507 table = base_crst_alloc(_REGION2_ENTRY_EMPTY);
508 if (!table)
509 return -ENOMEM;
510 *rfte = table | _REGION1_ENTRY;
511 }
512 table = *rfte & _REGION_ENTRY_ORIGIN;
513 rc = base_region2_walk(table, addr, next, alloc);
514 if (rc)
515 return rc;
516 if (!alloc)
517 base_crst_free(table);
518 } while (rfte++, addr = next, addr < end);
519 return 0;
520}
521
522/**
523 * base_asce_free - free asce and tables returned from base_asce_alloc()
524 * @asce: asce to be freed
525 *
526 * Frees all region, segment, and page tables that were allocated with a
527 * corresponding base_asce_alloc() call.
528 */
529void base_asce_free(unsigned long asce)
530{
531 unsigned long table = asce & _ASCE_ORIGIN;
532
533 if (!asce)
534 return;
535 switch (asce & _ASCE_TYPE_MASK) {
536 case _ASCE_TYPE_SEGMENT:
537 base_segment_walk(table, 0, _REGION3_SIZE, 0);
538 break;
539 case _ASCE_TYPE_REGION3:
540 base_region3_walk(table, 0, _REGION2_SIZE, 0);
541 break;
542 case _ASCE_TYPE_REGION2:
543 base_region2_walk(table, 0, _REGION1_SIZE, 0);
544 break;
545 case _ASCE_TYPE_REGION1:
546 base_region1_walk(table, 0, -_PAGE_SIZE, 0);
547 break;
548 }
549 base_crst_free(table);
550}
551
552static int base_pgt_cache_init(void)
553{
554 static DEFINE_MUTEX(base_pgt_cache_mutex);
555 unsigned long sz = _PAGE_TABLE_SIZE;
556
557 if (base_pgt_cache)
558 return 0;
559 mutex_lock(&base_pgt_cache_mutex);
560 if (!base_pgt_cache)
561 base_pgt_cache = kmem_cache_create("base_pgt", sz, sz, 0, NULL);
562 mutex_unlock(&base_pgt_cache_mutex);
563 return base_pgt_cache ? 0 : -ENOMEM;
564}
565
566/**
567 * base_asce_alloc - create kernel mapping without enhanced DAT features
568 * @addr: virtual start address of kernel mapping
569 * @num_pages: number of consecutive pages
570 *
571 * Generate an asce, including all required region, segment and page tables,
572 * that can be used to access the virtual kernel mapping. The difference is
573 * that the returned asce does not make use of any enhanced DAT features like
574 * e.g. large pages. This is required for some I/O functions that pass an
575 * asce, like e.g. some service call requests.
576 *
577 * Note: the returned asce may NEVER be attached to any cpu. It may only be
578 * used for I/O requests. tlb entries that might result because the
579 * asce was attached to a cpu won't be cleared.
580 */
581unsigned long base_asce_alloc(unsigned long addr, unsigned long num_pages)
582{
583 unsigned long asce, table, end;
584 int rc;
585
586 if (base_pgt_cache_init())
587 return 0;
588 end = addr + num_pages * PAGE_SIZE;
589 if (end <= _REGION3_SIZE) {
590 table = base_crst_alloc(_SEGMENT_ENTRY_EMPTY);
591 if (!table)
592 return 0;
593 rc = base_segment_walk(table, addr, end, 1);
594 asce = table | _ASCE_TYPE_SEGMENT | _ASCE_TABLE_LENGTH;
595 } else if (end <= _REGION2_SIZE) {
596 table = base_crst_alloc(_REGION3_ENTRY_EMPTY);
597 if (!table)
598 return 0;
599 rc = base_region3_walk(table, addr, end, 1);
600 asce = table | _ASCE_TYPE_REGION3 | _ASCE_TABLE_LENGTH;
601 } else if (end <= _REGION1_SIZE) {
602 table = base_crst_alloc(_REGION2_ENTRY_EMPTY);
603 if (!table)
604 return 0;
605 rc = base_region2_walk(table, addr, end, 1);
606 asce = table | _ASCE_TYPE_REGION2 | _ASCE_TABLE_LENGTH;
607 } else {
608 table = base_crst_alloc(_REGION1_ENTRY_EMPTY);
609 if (!table)
610 return 0;
611 rc = base_region1_walk(table, addr, end, 1);
612 asce = table | _ASCE_TYPE_REGION1 | _ASCE_TABLE_LENGTH;
613 }
614 if (rc) {
615 base_asce_free(asce);
616 asce = 0;
617 }
618 return asce;
619}