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