s390/kvm: support collaborative memory management
[firefly-linux-kernel-4.4.55.git] / arch / s390 / mm / pgtable.c
1 /*
2  *    Copyright IBM Corp. 2007, 2011
3  *    Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
4  */
5
6 #include <linux/sched.h>
7 #include <linux/kernel.h>
8 #include <linux/errno.h>
9 #include <linux/gfp.h>
10 #include <linux/mm.h>
11 #include <linux/swap.h>
12 #include <linux/smp.h>
13 #include <linux/highmem.h>
14 #include <linux/pagemap.h>
15 #include <linux/spinlock.h>
16 #include <linux/module.h>
17 #include <linux/quicklist.h>
18 #include <linux/rcupdate.h>
19 #include <linux/slab.h>
20 #include <linux/swapops.h>
21
22 #include <asm/pgtable.h>
23 #include <asm/pgalloc.h>
24 #include <asm/tlb.h>
25 #include <asm/tlbflush.h>
26 #include <asm/mmu_context.h>
27
28 #ifndef CONFIG_64BIT
29 #define ALLOC_ORDER     1
30 #define FRAG_MASK       0x0f
31 #else
32 #define ALLOC_ORDER     2
33 #define FRAG_MASK       0x03
34 #endif
35
36
37 unsigned long *crst_table_alloc(struct mm_struct *mm)
38 {
39         struct page *page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
40
41         if (!page)
42                 return NULL;
43         return (unsigned long *) page_to_phys(page);
44 }
45
46 void crst_table_free(struct mm_struct *mm, unsigned long *table)
47 {
48         free_pages((unsigned long) table, ALLOC_ORDER);
49 }
50
51 #ifdef CONFIG_64BIT
52 static void __crst_table_upgrade(void *arg)
53 {
54         struct mm_struct *mm = arg;
55
56         if (current->active_mm == mm)
57                 update_mm(mm, current);
58         __tlb_flush_local();
59 }
60
61 int crst_table_upgrade(struct mm_struct *mm, unsigned long limit)
62 {
63         unsigned long *table, *pgd;
64         unsigned long entry;
65         int flush;
66
67         BUG_ON(limit > (1UL << 53));
68         flush = 0;
69 repeat:
70         table = crst_table_alloc(mm);
71         if (!table)
72                 return -ENOMEM;
73         spin_lock_bh(&mm->page_table_lock);
74         if (mm->context.asce_limit < limit) {
75                 pgd = (unsigned long *) mm->pgd;
76                 if (mm->context.asce_limit <= (1UL << 31)) {
77                         entry = _REGION3_ENTRY_EMPTY;
78                         mm->context.asce_limit = 1UL << 42;
79                         mm->context.asce_bits = _ASCE_TABLE_LENGTH |
80                                                 _ASCE_USER_BITS |
81                                                 _ASCE_TYPE_REGION3;
82                 } else {
83                         entry = _REGION2_ENTRY_EMPTY;
84                         mm->context.asce_limit = 1UL << 53;
85                         mm->context.asce_bits = _ASCE_TABLE_LENGTH |
86                                                 _ASCE_USER_BITS |
87                                                 _ASCE_TYPE_REGION2;
88                 }
89                 crst_table_init(table, entry);
90                 pgd_populate(mm, (pgd_t *) table, (pud_t *) pgd);
91                 mm->pgd = (pgd_t *) table;
92                 mm->task_size = mm->context.asce_limit;
93                 table = NULL;
94                 flush = 1;
95         }
96         spin_unlock_bh(&mm->page_table_lock);
97         if (table)
98                 crst_table_free(mm, table);
99         if (mm->context.asce_limit < limit)
100                 goto repeat;
101         if (flush)
102                 on_each_cpu(__crst_table_upgrade, mm, 0);
103         return 0;
104 }
105
106 void crst_table_downgrade(struct mm_struct *mm, unsigned long limit)
107 {
108         pgd_t *pgd;
109
110         if (current->active_mm == mm)
111                 __tlb_flush_mm(mm);
112         while (mm->context.asce_limit > limit) {
113                 pgd = mm->pgd;
114                 switch (pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) {
115                 case _REGION_ENTRY_TYPE_R2:
116                         mm->context.asce_limit = 1UL << 42;
117                         mm->context.asce_bits = _ASCE_TABLE_LENGTH |
118                                                 _ASCE_USER_BITS |
119                                                 _ASCE_TYPE_REGION3;
120                         break;
121                 case _REGION_ENTRY_TYPE_R3:
122                         mm->context.asce_limit = 1UL << 31;
123                         mm->context.asce_bits = _ASCE_TABLE_LENGTH |
124                                                 _ASCE_USER_BITS |
125                                                 _ASCE_TYPE_SEGMENT;
126                         break;
127                 default:
128                         BUG();
129                 }
130                 mm->pgd = (pgd_t *) (pgd_val(*pgd) & _REGION_ENTRY_ORIGIN);
131                 mm->task_size = mm->context.asce_limit;
132                 crst_table_free(mm, (unsigned long *) pgd);
133         }
134         if (current->active_mm == mm)
135                 update_mm(mm, current);
136 }
137 #endif
138
139 #ifdef CONFIG_PGSTE
140
141 /**
142  * gmap_alloc - allocate a guest address space
143  * @mm: pointer to the parent mm_struct
144  *
145  * Returns a guest address space structure.
146  */
147 struct gmap *gmap_alloc(struct mm_struct *mm)
148 {
149         struct gmap *gmap;
150         struct page *page;
151         unsigned long *table;
152
153         gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL);
154         if (!gmap)
155                 goto out;
156         INIT_LIST_HEAD(&gmap->crst_list);
157         gmap->mm = mm;
158         page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
159         if (!page)
160                 goto out_free;
161         list_add(&page->lru, &gmap->crst_list);
162         table = (unsigned long *) page_to_phys(page);
163         crst_table_init(table, _REGION1_ENTRY_EMPTY);
164         gmap->table = table;
165         gmap->asce = _ASCE_TYPE_REGION1 | _ASCE_TABLE_LENGTH |
166                      _ASCE_USER_BITS | __pa(table);
167         list_add(&gmap->list, &mm->context.gmap_list);
168         return gmap;
169
170 out_free:
171         kfree(gmap);
172 out:
173         return NULL;
174 }
175 EXPORT_SYMBOL_GPL(gmap_alloc);
176
177 static int gmap_unlink_segment(struct gmap *gmap, unsigned long *table)
178 {
179         struct gmap_pgtable *mp;
180         struct gmap_rmap *rmap;
181         struct page *page;
182
183         if (*table & _SEGMENT_ENTRY_INVALID)
184                 return 0;
185         page = pfn_to_page(*table >> PAGE_SHIFT);
186         mp = (struct gmap_pgtable *) page->index;
187         list_for_each_entry(rmap, &mp->mapper, list) {
188                 if (rmap->entry != table)
189                         continue;
190                 list_del(&rmap->list);
191                 kfree(rmap);
192                 break;
193         }
194         *table = mp->vmaddr | _SEGMENT_ENTRY_INVALID | _SEGMENT_ENTRY_PROTECT;
195         return 1;
196 }
197
198 static void gmap_flush_tlb(struct gmap *gmap)
199 {
200         if (MACHINE_HAS_IDTE)
201                 __tlb_flush_idte((unsigned long) gmap->table |
202                                  _ASCE_TYPE_REGION1);
203         else
204                 __tlb_flush_global();
205 }
206
207 /**
208  * gmap_free - free a guest address space
209  * @gmap: pointer to the guest address space structure
210  */
211 void gmap_free(struct gmap *gmap)
212 {
213         struct page *page, *next;
214         unsigned long *table;
215         int i;
216
217
218         /* Flush tlb. */
219         if (MACHINE_HAS_IDTE)
220                 __tlb_flush_idte((unsigned long) gmap->table |
221                                  _ASCE_TYPE_REGION1);
222         else
223                 __tlb_flush_global();
224
225         /* Free all segment & region tables. */
226         down_read(&gmap->mm->mmap_sem);
227         spin_lock(&gmap->mm->page_table_lock);
228         list_for_each_entry_safe(page, next, &gmap->crst_list, lru) {
229                 table = (unsigned long *) page_to_phys(page);
230                 if ((*table & _REGION_ENTRY_TYPE_MASK) == 0)
231                         /* Remove gmap rmap structures for segment table. */
232                         for (i = 0; i < PTRS_PER_PMD; i++, table++)
233                                 gmap_unlink_segment(gmap, table);
234                 __free_pages(page, ALLOC_ORDER);
235         }
236         spin_unlock(&gmap->mm->page_table_lock);
237         up_read(&gmap->mm->mmap_sem);
238         list_del(&gmap->list);
239         kfree(gmap);
240 }
241 EXPORT_SYMBOL_GPL(gmap_free);
242
243 /**
244  * gmap_enable - switch primary space to the guest address space
245  * @gmap: pointer to the guest address space structure
246  */
247 void gmap_enable(struct gmap *gmap)
248 {
249         S390_lowcore.gmap = (unsigned long) gmap;
250 }
251 EXPORT_SYMBOL_GPL(gmap_enable);
252
253 /**
254  * gmap_disable - switch back to the standard primary address space
255  * @gmap: pointer to the guest address space structure
256  */
257 void gmap_disable(struct gmap *gmap)
258 {
259         S390_lowcore.gmap = 0UL;
260 }
261 EXPORT_SYMBOL_GPL(gmap_disable);
262
263 /*
264  * gmap_alloc_table is assumed to be called with mmap_sem held
265  */
266 static int gmap_alloc_table(struct gmap *gmap,
267                             unsigned long *table, unsigned long init)
268         __releases(&gmap->mm->page_table_lock)
269         __acquires(&gmap->mm->page_table_lock)
270 {
271         struct page *page;
272         unsigned long *new;
273
274         /* since we dont free the gmap table until gmap_free we can unlock */
275         spin_unlock(&gmap->mm->page_table_lock);
276         page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
277         spin_lock(&gmap->mm->page_table_lock);
278         if (!page)
279                 return -ENOMEM;
280         new = (unsigned long *) page_to_phys(page);
281         crst_table_init(new, init);
282         if (*table & _REGION_ENTRY_INVALID) {
283                 list_add(&page->lru, &gmap->crst_list);
284                 *table = (unsigned long) new | _REGION_ENTRY_LENGTH |
285                         (*table & _REGION_ENTRY_TYPE_MASK);
286         } else
287                 __free_pages(page, ALLOC_ORDER);
288         return 0;
289 }
290
291 /**
292  * gmap_unmap_segment - unmap segment from the guest address space
293  * @gmap: pointer to the guest address space structure
294  * @addr: address in the guest address space
295  * @len: length of the memory area to unmap
296  *
297  * Returns 0 if the unmap succeeded, -EINVAL if not.
298  */
299 int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len)
300 {
301         unsigned long *table;
302         unsigned long off;
303         int flush;
304
305         if ((to | len) & (PMD_SIZE - 1))
306                 return -EINVAL;
307         if (len == 0 || to + len < to)
308                 return -EINVAL;
309
310         flush = 0;
311         down_read(&gmap->mm->mmap_sem);
312         spin_lock(&gmap->mm->page_table_lock);
313         for (off = 0; off < len; off += PMD_SIZE) {
314                 /* Walk the guest addr space page table */
315                 table = gmap->table + (((to + off) >> 53) & 0x7ff);
316                 if (*table & _REGION_ENTRY_INVALID)
317                         goto out;
318                 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
319                 table = table + (((to + off) >> 42) & 0x7ff);
320                 if (*table & _REGION_ENTRY_INVALID)
321                         goto out;
322                 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
323                 table = table + (((to + off) >> 31) & 0x7ff);
324                 if (*table & _REGION_ENTRY_INVALID)
325                         goto out;
326                 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
327                 table = table + (((to + off) >> 20) & 0x7ff);
328
329                 /* Clear segment table entry in guest address space. */
330                 flush |= gmap_unlink_segment(gmap, table);
331                 *table = _SEGMENT_ENTRY_INVALID;
332         }
333 out:
334         spin_unlock(&gmap->mm->page_table_lock);
335         up_read(&gmap->mm->mmap_sem);
336         if (flush)
337                 gmap_flush_tlb(gmap);
338         return 0;
339 }
340 EXPORT_SYMBOL_GPL(gmap_unmap_segment);
341
342 /**
343  * gmap_mmap_segment - map a segment to the guest address space
344  * @gmap: pointer to the guest address space structure
345  * @from: source address in the parent address space
346  * @to: target address in the guest address space
347  *
348  * Returns 0 if the mmap succeeded, -EINVAL or -ENOMEM if not.
349  */
350 int gmap_map_segment(struct gmap *gmap, unsigned long from,
351                      unsigned long to, unsigned long len)
352 {
353         unsigned long *table;
354         unsigned long off;
355         int flush;
356
357         if ((from | to | len) & (PMD_SIZE - 1))
358                 return -EINVAL;
359         if (len == 0 || from + len > TASK_MAX_SIZE ||
360             from + len < from || to + len < to)
361                 return -EINVAL;
362
363         flush = 0;
364         down_read(&gmap->mm->mmap_sem);
365         spin_lock(&gmap->mm->page_table_lock);
366         for (off = 0; off < len; off += PMD_SIZE) {
367                 /* Walk the gmap address space page table */
368                 table = gmap->table + (((to + off) >> 53) & 0x7ff);
369                 if ((*table & _REGION_ENTRY_INVALID) &&
370                     gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY))
371                         goto out_unmap;
372                 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
373                 table = table + (((to + off) >> 42) & 0x7ff);
374                 if ((*table & _REGION_ENTRY_INVALID) &&
375                     gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY))
376                         goto out_unmap;
377                 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
378                 table = table + (((to + off) >> 31) & 0x7ff);
379                 if ((*table & _REGION_ENTRY_INVALID) &&
380                     gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY))
381                         goto out_unmap;
382                 table = (unsigned long *) (*table & _REGION_ENTRY_ORIGIN);
383                 table = table + (((to + off) >> 20) & 0x7ff);
384
385                 /* Store 'from' address in an invalid segment table entry. */
386                 flush |= gmap_unlink_segment(gmap, table);
387                 *table =  (from + off) | (_SEGMENT_ENTRY_INVALID |
388                                           _SEGMENT_ENTRY_PROTECT);
389         }
390         spin_unlock(&gmap->mm->page_table_lock);
391         up_read(&gmap->mm->mmap_sem);
392         if (flush)
393                 gmap_flush_tlb(gmap);
394         return 0;
395
396 out_unmap:
397         spin_unlock(&gmap->mm->page_table_lock);
398         up_read(&gmap->mm->mmap_sem);
399         gmap_unmap_segment(gmap, to, len);
400         return -ENOMEM;
401 }
402 EXPORT_SYMBOL_GPL(gmap_map_segment);
403
404 static unsigned long *gmap_table_walk(unsigned long address, struct gmap *gmap)
405 {
406         unsigned long *table;
407
408         table = gmap->table + ((address >> 53) & 0x7ff);
409         if (unlikely(*table & _REGION_ENTRY_INVALID))
410                 return ERR_PTR(-EFAULT);
411         table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
412         table = table + ((address >> 42) & 0x7ff);
413         if (unlikely(*table & _REGION_ENTRY_INVALID))
414                 return ERR_PTR(-EFAULT);
415         table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
416         table = table + ((address >> 31) & 0x7ff);
417         if (unlikely(*table & _REGION_ENTRY_INVALID))
418                 return ERR_PTR(-EFAULT);
419         table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
420         table = table + ((address >> 20) & 0x7ff);
421         return table;
422 }
423
424 /**
425  * __gmap_translate - translate a guest address to a user space address
426  * @address: guest address
427  * @gmap: pointer to guest mapping meta data structure
428  *
429  * Returns user space address which corresponds to the guest address or
430  * -EFAULT if no such mapping exists.
431  * This function does not establish potentially missing page table entries.
432  * The mmap_sem of the mm that belongs to the address space must be held
433  * when this function gets called.
434  */
435 unsigned long __gmap_translate(unsigned long address, struct gmap *gmap)
436 {
437         unsigned long *segment_ptr, vmaddr, segment;
438         struct gmap_pgtable *mp;
439         struct page *page;
440
441         current->thread.gmap_addr = address;
442         segment_ptr = gmap_table_walk(address, gmap);
443         if (IS_ERR(segment_ptr))
444                 return PTR_ERR(segment_ptr);
445         /* Convert the gmap address to an mm address. */
446         segment = *segment_ptr;
447         if (!(segment & _SEGMENT_ENTRY_INVALID)) {
448                 page = pfn_to_page(segment >> PAGE_SHIFT);
449                 mp = (struct gmap_pgtable *) page->index;
450                 return mp->vmaddr | (address & ~PMD_MASK);
451         } else if (segment & _SEGMENT_ENTRY_PROTECT) {
452                 vmaddr = segment & _SEGMENT_ENTRY_ORIGIN;
453                 return vmaddr | (address & ~PMD_MASK);
454         }
455         return -EFAULT;
456 }
457 EXPORT_SYMBOL_GPL(__gmap_translate);
458
459 /**
460  * gmap_translate - translate a guest address to a user space address
461  * @address: guest address
462  * @gmap: pointer to guest mapping meta data structure
463  *
464  * Returns user space address which corresponds to the guest address or
465  * -EFAULT if no such mapping exists.
466  * This function does not establish potentially missing page table entries.
467  */
468 unsigned long gmap_translate(unsigned long address, struct gmap *gmap)
469 {
470         unsigned long rc;
471
472         down_read(&gmap->mm->mmap_sem);
473         rc = __gmap_translate(address, gmap);
474         up_read(&gmap->mm->mmap_sem);
475         return rc;
476 }
477 EXPORT_SYMBOL_GPL(gmap_translate);
478
479 static int gmap_connect_pgtable(unsigned long address, unsigned long segment,
480                                 unsigned long *segment_ptr, struct gmap *gmap)
481 {
482         unsigned long vmaddr;
483         struct vm_area_struct *vma;
484         struct gmap_pgtable *mp;
485         struct gmap_rmap *rmap;
486         struct mm_struct *mm;
487         struct page *page;
488         pgd_t *pgd;
489         pud_t *pud;
490         pmd_t *pmd;
491
492         mm = gmap->mm;
493         vmaddr = segment & _SEGMENT_ENTRY_ORIGIN;
494         vma = find_vma(mm, vmaddr);
495         if (!vma || vma->vm_start > vmaddr)
496                 return -EFAULT;
497         /* Walk the parent mm page table */
498         pgd = pgd_offset(mm, vmaddr);
499         pud = pud_alloc(mm, pgd, vmaddr);
500         if (!pud)
501                 return -ENOMEM;
502         pmd = pmd_alloc(mm, pud, vmaddr);
503         if (!pmd)
504                 return -ENOMEM;
505         if (!pmd_present(*pmd) &&
506             __pte_alloc(mm, vma, pmd, vmaddr))
507                 return -ENOMEM;
508         /* pmd now points to a valid segment table entry. */
509         rmap = kmalloc(sizeof(*rmap), GFP_KERNEL|__GFP_REPEAT);
510         if (!rmap)
511                 return -ENOMEM;
512         /* Link gmap segment table entry location to page table. */
513         page = pmd_page(*pmd);
514         mp = (struct gmap_pgtable *) page->index;
515         rmap->gmap = gmap;
516         rmap->entry = segment_ptr;
517         rmap->vmaddr = address & PMD_MASK;
518         spin_lock(&mm->page_table_lock);
519         if (*segment_ptr == segment) {
520                 list_add(&rmap->list, &mp->mapper);
521                 /* Set gmap segment table entry to page table. */
522                 *segment_ptr = pmd_val(*pmd) & PAGE_MASK;
523                 rmap = NULL;
524         }
525         spin_unlock(&mm->page_table_lock);
526         kfree(rmap);
527         return 0;
528 }
529
530 static void gmap_disconnect_pgtable(struct mm_struct *mm, unsigned long *table)
531 {
532         struct gmap_rmap *rmap, *next;
533         struct gmap_pgtable *mp;
534         struct page *page;
535         int flush;
536
537         flush = 0;
538         spin_lock(&mm->page_table_lock);
539         page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
540         mp = (struct gmap_pgtable *) page->index;
541         list_for_each_entry_safe(rmap, next, &mp->mapper, list) {
542                 *rmap->entry = mp->vmaddr | (_SEGMENT_ENTRY_INVALID |
543                                              _SEGMENT_ENTRY_PROTECT);
544                 list_del(&rmap->list);
545                 kfree(rmap);
546                 flush = 1;
547         }
548         spin_unlock(&mm->page_table_lock);
549         if (flush)
550                 __tlb_flush_global();
551 }
552
553 /*
554  * this function is assumed to be called with mmap_sem held
555  */
556 unsigned long __gmap_fault(unsigned long address, struct gmap *gmap)
557 {
558         unsigned long *segment_ptr, segment;
559         struct gmap_pgtable *mp;
560         struct page *page;
561         int rc;
562
563         current->thread.gmap_addr = address;
564         segment_ptr = gmap_table_walk(address, gmap);
565         if (IS_ERR(segment_ptr))
566                 return -EFAULT;
567         /* Convert the gmap address to an mm address. */
568         while (1) {
569                 segment = *segment_ptr;
570                 if (!(segment & _SEGMENT_ENTRY_INVALID)) {
571                         /* Page table is present */
572                         page = pfn_to_page(segment >> PAGE_SHIFT);
573                         mp = (struct gmap_pgtable *) page->index;
574                         return mp->vmaddr | (address & ~PMD_MASK);
575                 }
576                 if (!(segment & _SEGMENT_ENTRY_PROTECT))
577                         /* Nothing mapped in the gmap address space. */
578                         break;
579                 rc = gmap_connect_pgtable(address, segment, segment_ptr, gmap);
580                 if (rc)
581                         return rc;
582         }
583         return -EFAULT;
584 }
585
586 unsigned long gmap_fault(unsigned long address, struct gmap *gmap)
587 {
588         unsigned long rc;
589
590         down_read(&gmap->mm->mmap_sem);
591         rc = __gmap_fault(address, gmap);
592         up_read(&gmap->mm->mmap_sem);
593
594         return rc;
595 }
596 EXPORT_SYMBOL_GPL(gmap_fault);
597
598 static void gmap_zap_swap_entry(swp_entry_t entry, struct mm_struct *mm)
599 {
600         if (!non_swap_entry(entry))
601                 dec_mm_counter(mm, MM_SWAPENTS);
602         else if (is_migration_entry(entry)) {
603                 struct page *page = migration_entry_to_page(entry);
604
605                 if (PageAnon(page))
606                         dec_mm_counter(mm, MM_ANONPAGES);
607                 else
608                         dec_mm_counter(mm, MM_FILEPAGES);
609         }
610         free_swap_and_cache(entry);
611 }
612
613 /**
614  * The mm->mmap_sem lock must be held
615  */
616 static void gmap_zap_unused(struct mm_struct *mm, unsigned long address)
617 {
618         unsigned long ptev, pgstev;
619         spinlock_t *ptl;
620         pgste_t pgste;
621         pte_t *ptep, pte;
622
623         ptep = get_locked_pte(mm, address, &ptl);
624         if (unlikely(!ptep))
625                 return;
626         pte = *ptep;
627         if (!pte_swap(pte))
628                 goto out_pte;
629         /* Zap unused and logically-zero pages */
630         pgste = pgste_get_lock(ptep);
631         pgstev = pgste_val(pgste);
632         ptev = pte_val(pte);
633         if (((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED) ||
634             ((pgstev & _PGSTE_GPS_ZERO) && (ptev & _PAGE_INVALID))) {
635                 gmap_zap_swap_entry(pte_to_swp_entry(pte), mm);
636                 pte_clear(mm, address, ptep);
637         }
638         pgste_set_unlock(ptep, pgste);
639 out_pte:
640         pte_unmap_unlock(*ptep, ptl);
641 }
642
643 /*
644  * this function is assumed to be called with mmap_sem held
645  */
646 void __gmap_zap(unsigned long address, struct gmap *gmap)
647 {
648         unsigned long *table, *segment_ptr;
649         unsigned long segment, pgstev, ptev;
650         struct gmap_pgtable *mp;
651         struct page *page;
652
653         segment_ptr = gmap_table_walk(address, gmap);
654         if (IS_ERR(segment_ptr))
655                 return;
656         segment = *segment_ptr;
657         if (segment & _SEGMENT_ENTRY_INVALID)
658                 return;
659         page = pfn_to_page(segment >> PAGE_SHIFT);
660         mp = (struct gmap_pgtable *) page->index;
661         address = mp->vmaddr | (address & ~PMD_MASK);
662         /* Page table is present */
663         table = (unsigned long *)(segment & _SEGMENT_ENTRY_ORIGIN);
664         table = table + ((address >> 12) & 0xff);
665         pgstev = table[PTRS_PER_PTE];
666         ptev = table[0];
667         /* quick check, checked again with locks held */
668         if (((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED) ||
669             ((pgstev & _PGSTE_GPS_ZERO) && (ptev & _PAGE_INVALID)))
670                 gmap_zap_unused(gmap->mm, address);
671 }
672 EXPORT_SYMBOL_GPL(__gmap_zap);
673
674 void gmap_discard(unsigned long from, unsigned long to, struct gmap *gmap)
675 {
676
677         unsigned long *table, address, size;
678         struct vm_area_struct *vma;
679         struct gmap_pgtable *mp;
680         struct page *page;
681
682         down_read(&gmap->mm->mmap_sem);
683         address = from;
684         while (address < to) {
685                 /* Walk the gmap address space page table */
686                 table = gmap->table + ((address >> 53) & 0x7ff);
687                 if (unlikely(*table & _REGION_ENTRY_INVALID)) {
688                         address = (address + PMD_SIZE) & PMD_MASK;
689                         continue;
690                 }
691                 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
692                 table = table + ((address >> 42) & 0x7ff);
693                 if (unlikely(*table & _REGION_ENTRY_INVALID)) {
694                         address = (address + PMD_SIZE) & PMD_MASK;
695                         continue;
696                 }
697                 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
698                 table = table + ((address >> 31) & 0x7ff);
699                 if (unlikely(*table & _REGION_ENTRY_INVALID)) {
700                         address = (address + PMD_SIZE) & PMD_MASK;
701                         continue;
702                 }
703                 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
704                 table = table + ((address >> 20) & 0x7ff);
705                 if (unlikely(*table & _SEGMENT_ENTRY_INVALID)) {
706                         address = (address + PMD_SIZE) & PMD_MASK;
707                         continue;
708                 }
709                 page = pfn_to_page(*table >> PAGE_SHIFT);
710                 mp = (struct gmap_pgtable *) page->index;
711                 vma = find_vma(gmap->mm, mp->vmaddr);
712                 size = min(to - address, PMD_SIZE - (address & ~PMD_MASK));
713                 zap_page_range(vma, mp->vmaddr | (address & ~PMD_MASK),
714                                size, NULL);
715                 address = (address + PMD_SIZE) & PMD_MASK;
716         }
717         up_read(&gmap->mm->mmap_sem);
718 }
719 EXPORT_SYMBOL_GPL(gmap_discard);
720
721 static LIST_HEAD(gmap_notifier_list);
722 static DEFINE_SPINLOCK(gmap_notifier_lock);
723
724 /**
725  * gmap_register_ipte_notifier - register a pte invalidation callback
726  * @nb: pointer to the gmap notifier block
727  */
728 void gmap_register_ipte_notifier(struct gmap_notifier *nb)
729 {
730         spin_lock(&gmap_notifier_lock);
731         list_add(&nb->list, &gmap_notifier_list);
732         spin_unlock(&gmap_notifier_lock);
733 }
734 EXPORT_SYMBOL_GPL(gmap_register_ipte_notifier);
735
736 /**
737  * gmap_unregister_ipte_notifier - remove a pte invalidation callback
738  * @nb: pointer to the gmap notifier block
739  */
740 void gmap_unregister_ipte_notifier(struct gmap_notifier *nb)
741 {
742         spin_lock(&gmap_notifier_lock);
743         list_del_init(&nb->list);
744         spin_unlock(&gmap_notifier_lock);
745 }
746 EXPORT_SYMBOL_GPL(gmap_unregister_ipte_notifier);
747
748 /**
749  * gmap_ipte_notify - mark a range of ptes for invalidation notification
750  * @gmap: pointer to guest mapping meta data structure
751  * @address: virtual address in the guest address space
752  * @len: size of area
753  *
754  * Returns 0 if for each page in the given range a gmap mapping exists and
755  * the invalidation notification could be set. If the gmap mapping is missing
756  * for one or more pages -EFAULT is returned. If no memory could be allocated
757  * -ENOMEM is returned. This function establishes missing page table entries.
758  */
759 int gmap_ipte_notify(struct gmap *gmap, unsigned long start, unsigned long len)
760 {
761         unsigned long addr;
762         spinlock_t *ptl;
763         pte_t *ptep, entry;
764         pgste_t pgste;
765         int rc = 0;
766
767         if ((start & ~PAGE_MASK) || (len & ~PAGE_MASK))
768                 return -EINVAL;
769         down_read(&gmap->mm->mmap_sem);
770         while (len) {
771                 /* Convert gmap address and connect the page tables */
772                 addr = __gmap_fault(start, gmap);
773                 if (IS_ERR_VALUE(addr)) {
774                         rc = addr;
775                         break;
776                 }
777                 /* Get the page mapped */
778                 if (fixup_user_fault(current, gmap->mm, addr, FAULT_FLAG_WRITE)) {
779                         rc = -EFAULT;
780                         break;
781                 }
782                 /* Walk the process page table, lock and get pte pointer */
783                 ptep = get_locked_pte(gmap->mm, addr, &ptl);
784                 if (unlikely(!ptep))
785                         continue;
786                 /* Set notification bit in the pgste of the pte */
787                 entry = *ptep;
788                 if ((pte_val(entry) & (_PAGE_INVALID | _PAGE_PROTECT)) == 0) {
789                         pgste = pgste_get_lock(ptep);
790                         pgste_val(pgste) |= PGSTE_IN_BIT;
791                         pgste_set_unlock(ptep, pgste);
792                         start += PAGE_SIZE;
793                         len -= PAGE_SIZE;
794                 }
795                 spin_unlock(ptl);
796         }
797         up_read(&gmap->mm->mmap_sem);
798         return rc;
799 }
800 EXPORT_SYMBOL_GPL(gmap_ipte_notify);
801
802 /**
803  * gmap_do_ipte_notify - call all invalidation callbacks for a specific pte.
804  * @mm: pointer to the process mm_struct
805  * @addr: virtual address in the process address space
806  * @pte: pointer to the page table entry
807  *
808  * This function is assumed to be called with the page table lock held
809  * for the pte to notify.
810  */
811 void gmap_do_ipte_notify(struct mm_struct *mm, unsigned long addr, pte_t *pte)
812 {
813         unsigned long segment_offset;
814         struct gmap_notifier *nb;
815         struct gmap_pgtable *mp;
816         struct gmap_rmap *rmap;
817         struct page *page;
818
819         segment_offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
820         segment_offset = segment_offset * (4096 / sizeof(pte_t));
821         page = pfn_to_page(__pa(pte) >> PAGE_SHIFT);
822         mp = (struct gmap_pgtable *) page->index;
823         spin_lock(&gmap_notifier_lock);
824         list_for_each_entry(rmap, &mp->mapper, list) {
825                 list_for_each_entry(nb, &gmap_notifier_list, list)
826                         nb->notifier_call(rmap->gmap,
827                                           rmap->vmaddr + segment_offset);
828         }
829         spin_unlock(&gmap_notifier_lock);
830 }
831
832 static inline int page_table_with_pgste(struct page *page)
833 {
834         return atomic_read(&page->_mapcount) == 0;
835 }
836
837 static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm,
838                                                     unsigned long vmaddr)
839 {
840         struct page *page;
841         unsigned long *table;
842         struct gmap_pgtable *mp;
843
844         page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
845         if (!page)
846                 return NULL;
847         mp = kmalloc(sizeof(*mp), GFP_KERNEL|__GFP_REPEAT);
848         if (!mp) {
849                 __free_page(page);
850                 return NULL;
851         }
852         if (!pgtable_page_ctor(page)) {
853                 kfree(mp);
854                 __free_page(page);
855                 return NULL;
856         }
857         mp->vmaddr = vmaddr & PMD_MASK;
858         INIT_LIST_HEAD(&mp->mapper);
859         page->index = (unsigned long) mp;
860         atomic_set(&page->_mapcount, 0);
861         table = (unsigned long *) page_to_phys(page);
862         clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
863         clear_table(table + PTRS_PER_PTE, PGSTE_HR_BIT | PGSTE_HC_BIT,
864                     PAGE_SIZE/2);
865         return table;
866 }
867
868 static inline void page_table_free_pgste(unsigned long *table)
869 {
870         struct page *page;
871         struct gmap_pgtable *mp;
872
873         page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
874         mp = (struct gmap_pgtable *) page->index;
875         BUG_ON(!list_empty(&mp->mapper));
876         pgtable_page_dtor(page);
877         atomic_set(&page->_mapcount, -1);
878         kfree(mp);
879         __free_page(page);
880 }
881
882 int set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
883                           unsigned long key, bool nq)
884 {
885         spinlock_t *ptl;
886         pgste_t old, new;
887         pte_t *ptep;
888
889         down_read(&mm->mmap_sem);
890         ptep = get_locked_pte(current->mm, addr, &ptl);
891         if (unlikely(!ptep)) {
892                 up_read(&mm->mmap_sem);
893                 return -EFAULT;
894         }
895
896         new = old = pgste_get_lock(ptep);
897         pgste_val(new) &= ~(PGSTE_GR_BIT | PGSTE_GC_BIT |
898                             PGSTE_ACC_BITS | PGSTE_FP_BIT);
899         pgste_val(new) |= (key & (_PAGE_CHANGED | _PAGE_REFERENCED)) << 48;
900         pgste_val(new) |= (key & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
901         if (!(pte_val(*ptep) & _PAGE_INVALID)) {
902                 unsigned long address, bits, skey;
903
904                 address = pte_val(*ptep) & PAGE_MASK;
905                 skey = (unsigned long) page_get_storage_key(address);
906                 bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
907                 skey = key & (_PAGE_ACC_BITS | _PAGE_FP_BIT);
908                 /* Set storage key ACC and FP */
909                 page_set_storage_key(address, skey, !nq);
910                 /* Merge host changed & referenced into pgste  */
911                 pgste_val(new) |= bits << 52;
912         }
913         /* changing the guest storage key is considered a change of the page */
914         if ((pgste_val(new) ^ pgste_val(old)) &
915             (PGSTE_ACC_BITS | PGSTE_FP_BIT | PGSTE_GR_BIT | PGSTE_GC_BIT))
916                 pgste_val(new) |= PGSTE_HC_BIT;
917
918         pgste_set_unlock(ptep, new);
919         pte_unmap_unlock(*ptep, ptl);
920         up_read(&mm->mmap_sem);
921         return 0;
922 }
923 EXPORT_SYMBOL(set_guest_storage_key);
924
925 #else /* CONFIG_PGSTE */
926
927 static inline int page_table_with_pgste(struct page *page)
928 {
929         return 0;
930 }
931
932 static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm,
933                                                     unsigned long vmaddr)
934 {
935         return NULL;
936 }
937
938 static inline void page_table_free_pgste(unsigned long *table)
939 {
940 }
941
942 static inline void gmap_disconnect_pgtable(struct mm_struct *mm,
943                                            unsigned long *table)
944 {
945 }
946
947 #endif /* CONFIG_PGSTE */
948
949 static inline unsigned int atomic_xor_bits(atomic_t *v, unsigned int bits)
950 {
951         unsigned int old, new;
952
953         do {
954                 old = atomic_read(v);
955                 new = old ^ bits;
956         } while (atomic_cmpxchg(v, old, new) != old);
957         return new;
958 }
959
960 /*
961  * page table entry allocation/free routines.
962  */
963 unsigned long *page_table_alloc(struct mm_struct *mm, unsigned long vmaddr)
964 {
965         unsigned long *uninitialized_var(table);
966         struct page *uninitialized_var(page);
967         unsigned int mask, bit;
968
969         if (mm_has_pgste(mm))
970                 return page_table_alloc_pgste(mm, vmaddr);
971         /* Allocate fragments of a 4K page as 1K/2K page table */
972         spin_lock_bh(&mm->context.list_lock);
973         mask = FRAG_MASK;
974         if (!list_empty(&mm->context.pgtable_list)) {
975                 page = list_first_entry(&mm->context.pgtable_list,
976                                         struct page, lru);
977                 table = (unsigned long *) page_to_phys(page);
978                 mask = atomic_read(&page->_mapcount);
979                 mask = mask | (mask >> 4);
980         }
981         if ((mask & FRAG_MASK) == FRAG_MASK) {
982                 spin_unlock_bh(&mm->context.list_lock);
983                 page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
984                 if (!page)
985                         return NULL;
986                 if (!pgtable_page_ctor(page)) {
987                         __free_page(page);
988                         return NULL;
989                 }
990                 atomic_set(&page->_mapcount, 1);
991                 table = (unsigned long *) page_to_phys(page);
992                 clear_table(table, _PAGE_INVALID, PAGE_SIZE);
993                 spin_lock_bh(&mm->context.list_lock);
994                 list_add(&page->lru, &mm->context.pgtable_list);
995         } else {
996                 for (bit = 1; mask & bit; bit <<= 1)
997                         table += PTRS_PER_PTE;
998                 mask = atomic_xor_bits(&page->_mapcount, bit);
999                 if ((mask & FRAG_MASK) == FRAG_MASK)
1000                         list_del(&page->lru);
1001         }
1002         spin_unlock_bh(&mm->context.list_lock);
1003         return table;
1004 }
1005
1006 void page_table_free(struct mm_struct *mm, unsigned long *table)
1007 {
1008         struct page *page;
1009         unsigned int bit, mask;
1010
1011         page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1012         if (page_table_with_pgste(page)) {
1013                 gmap_disconnect_pgtable(mm, table);
1014                 return page_table_free_pgste(table);
1015         }
1016         /* Free 1K/2K page table fragment of a 4K page */
1017         bit = 1 << ((__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t)));
1018         spin_lock_bh(&mm->context.list_lock);
1019         if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK)
1020                 list_del(&page->lru);
1021         mask = atomic_xor_bits(&page->_mapcount, bit);
1022         if (mask & FRAG_MASK)
1023                 list_add(&page->lru, &mm->context.pgtable_list);
1024         spin_unlock_bh(&mm->context.list_lock);
1025         if (mask == 0) {
1026                 pgtable_page_dtor(page);
1027                 atomic_set(&page->_mapcount, -1);
1028                 __free_page(page);
1029         }
1030 }
1031
1032 static void __page_table_free_rcu(void *table, unsigned bit)
1033 {
1034         struct page *page;
1035
1036         if (bit == FRAG_MASK)
1037                 return page_table_free_pgste(table);
1038         /* Free 1K/2K page table fragment of a 4K page */
1039         page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1040         if (atomic_xor_bits(&page->_mapcount, bit) == 0) {
1041                 pgtable_page_dtor(page);
1042                 atomic_set(&page->_mapcount, -1);
1043                 __free_page(page);
1044         }
1045 }
1046
1047 void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table)
1048 {
1049         struct mm_struct *mm;
1050         struct page *page;
1051         unsigned int bit, mask;
1052
1053         mm = tlb->mm;
1054         page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1055         if (page_table_with_pgste(page)) {
1056                 gmap_disconnect_pgtable(mm, table);
1057                 table = (unsigned long *) (__pa(table) | FRAG_MASK);
1058                 tlb_remove_table(tlb, table);
1059                 return;
1060         }
1061         bit = 1 << ((__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t)));
1062         spin_lock_bh(&mm->context.list_lock);
1063         if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK)
1064                 list_del(&page->lru);
1065         mask = atomic_xor_bits(&page->_mapcount, bit | (bit << 4));
1066         if (mask & FRAG_MASK)
1067                 list_add_tail(&page->lru, &mm->context.pgtable_list);
1068         spin_unlock_bh(&mm->context.list_lock);
1069         table = (unsigned long *) (__pa(table) | (bit << 4));
1070         tlb_remove_table(tlb, table);
1071 }
1072
1073 static void __tlb_remove_table(void *_table)
1074 {
1075         const unsigned long mask = (FRAG_MASK << 4) | FRAG_MASK;
1076         void *table = (void *)((unsigned long) _table & ~mask);
1077         unsigned type = (unsigned long) _table & mask;
1078
1079         if (type)
1080                 __page_table_free_rcu(table, type);
1081         else
1082                 free_pages((unsigned long) table, ALLOC_ORDER);
1083 }
1084
1085 static void tlb_remove_table_smp_sync(void *arg)
1086 {
1087         /* Simply deliver the interrupt */
1088 }
1089
1090 static void tlb_remove_table_one(void *table)
1091 {
1092         /*
1093          * This isn't an RCU grace period and hence the page-tables cannot be
1094          * assumed to be actually RCU-freed.
1095          *
1096          * It is however sufficient for software page-table walkers that rely
1097          * on IRQ disabling. See the comment near struct mmu_table_batch.
1098          */
1099         smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
1100         __tlb_remove_table(table);
1101 }
1102
1103 static void tlb_remove_table_rcu(struct rcu_head *head)
1104 {
1105         struct mmu_table_batch *batch;
1106         int i;
1107
1108         batch = container_of(head, struct mmu_table_batch, rcu);
1109
1110         for (i = 0; i < batch->nr; i++)
1111                 __tlb_remove_table(batch->tables[i]);
1112
1113         free_page((unsigned long)batch);
1114 }
1115
1116 void tlb_table_flush(struct mmu_gather *tlb)
1117 {
1118         struct mmu_table_batch **batch = &tlb->batch;
1119
1120         if (*batch) {
1121                 call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
1122                 *batch = NULL;
1123         }
1124 }
1125
1126 void tlb_remove_table(struct mmu_gather *tlb, void *table)
1127 {
1128         struct mmu_table_batch **batch = &tlb->batch;
1129
1130         tlb->mm->context.flush_mm = 1;
1131         if (*batch == NULL) {
1132                 *batch = (struct mmu_table_batch *)
1133                         __get_free_page(GFP_NOWAIT | __GFP_NOWARN);
1134                 if (*batch == NULL) {
1135                         __tlb_flush_mm_lazy(tlb->mm);
1136                         tlb_remove_table_one(table);
1137                         return;
1138                 }
1139                 (*batch)->nr = 0;
1140         }
1141         (*batch)->tables[(*batch)->nr++] = table;
1142         if ((*batch)->nr == MAX_TABLE_BATCH)
1143                 tlb_flush_mmu(tlb);
1144 }
1145
1146 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1147 static inline void thp_split_vma(struct vm_area_struct *vma)
1148 {
1149         unsigned long addr;
1150
1151         for (addr = vma->vm_start; addr < vma->vm_end; addr += PAGE_SIZE)
1152                 follow_page(vma, addr, FOLL_SPLIT);
1153 }
1154
1155 static inline void thp_split_mm(struct mm_struct *mm)
1156 {
1157         struct vm_area_struct *vma;
1158
1159         for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) {
1160                 thp_split_vma(vma);
1161                 vma->vm_flags &= ~VM_HUGEPAGE;
1162                 vma->vm_flags |= VM_NOHUGEPAGE;
1163         }
1164         mm->def_flags |= VM_NOHUGEPAGE;
1165 }
1166 #else
1167 static inline void thp_split_mm(struct mm_struct *mm)
1168 {
1169 }
1170 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1171
1172 static unsigned long page_table_realloc_pmd(struct mmu_gather *tlb,
1173                                 struct mm_struct *mm, pud_t *pud,
1174                                 unsigned long addr, unsigned long end)
1175 {
1176         unsigned long next, *table, *new;
1177         struct page *page;
1178         pmd_t *pmd;
1179
1180         pmd = pmd_offset(pud, addr);
1181         do {
1182                 next = pmd_addr_end(addr, end);
1183 again:
1184                 if (pmd_none_or_clear_bad(pmd))
1185                         continue;
1186                 table = (unsigned long *) pmd_deref(*pmd);
1187                 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1188                 if (page_table_with_pgste(page))
1189                         continue;
1190                 /* Allocate new page table with pgstes */
1191                 new = page_table_alloc_pgste(mm, addr);
1192                 if (!new)
1193                         return -ENOMEM;
1194
1195                 spin_lock(&mm->page_table_lock);
1196                 if (likely((unsigned long *) pmd_deref(*pmd) == table)) {
1197                         /* Nuke pmd entry pointing to the "short" page table */
1198                         pmdp_flush_lazy(mm, addr, pmd);
1199                         pmd_clear(pmd);
1200                         /* Copy ptes from old table to new table */
1201                         memcpy(new, table, PAGE_SIZE/2);
1202                         clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
1203                         /* Establish new table */
1204                         pmd_populate(mm, pmd, (pte_t *) new);
1205                         /* Free old table with rcu, there might be a walker! */
1206                         page_table_free_rcu(tlb, table);
1207                         new = NULL;
1208                 }
1209                 spin_unlock(&mm->page_table_lock);
1210                 if (new) {
1211                         page_table_free_pgste(new);
1212                         goto again;
1213                 }
1214         } while (pmd++, addr = next, addr != end);
1215
1216         return addr;
1217 }
1218
1219 static unsigned long page_table_realloc_pud(struct mmu_gather *tlb,
1220                                    struct mm_struct *mm, pgd_t *pgd,
1221                                    unsigned long addr, unsigned long end)
1222 {
1223         unsigned long next;
1224         pud_t *pud;
1225
1226         pud = pud_offset(pgd, addr);
1227         do {
1228                 next = pud_addr_end(addr, end);
1229                 if (pud_none_or_clear_bad(pud))
1230                         continue;
1231                 next = page_table_realloc_pmd(tlb, mm, pud, addr, next);
1232                 if (unlikely(IS_ERR_VALUE(next)))
1233                         return next;
1234         } while (pud++, addr = next, addr != end);
1235
1236         return addr;
1237 }
1238
1239 static unsigned long page_table_realloc(struct mmu_gather *tlb, struct mm_struct *mm,
1240                                         unsigned long addr, unsigned long end)
1241 {
1242         unsigned long next;
1243         pgd_t *pgd;
1244
1245         pgd = pgd_offset(mm, addr);
1246         do {
1247                 next = pgd_addr_end(addr, end);
1248                 if (pgd_none_or_clear_bad(pgd))
1249                         continue;
1250                 next = page_table_realloc_pud(tlb, mm, pgd, addr, next);
1251                 if (unlikely(IS_ERR_VALUE(next)))
1252                         return next;
1253         } while (pgd++, addr = next, addr != end);
1254
1255         return 0;
1256 }
1257
1258 /*
1259  * switch on pgstes for its userspace process (for kvm)
1260  */
1261 int s390_enable_sie(void)
1262 {
1263         struct task_struct *tsk = current;
1264         struct mm_struct *mm = tsk->mm;
1265         struct mmu_gather tlb;
1266
1267         /* Do we have pgstes? if yes, we are done */
1268         if (mm_has_pgste(tsk->mm))
1269                 return 0;
1270
1271         down_write(&mm->mmap_sem);
1272         /* split thp mappings and disable thp for future mappings */
1273         thp_split_mm(mm);
1274         /* Reallocate the page tables with pgstes */
1275         tlb_gather_mmu(&tlb, mm, 0, TASK_SIZE);
1276         if (!page_table_realloc(&tlb, mm, 0, TASK_SIZE))
1277                 mm->context.has_pgste = 1;
1278         tlb_finish_mmu(&tlb, 0, TASK_SIZE);
1279         up_write(&mm->mmap_sem);
1280         return mm->context.has_pgste ? 0 : -ENOMEM;
1281 }
1282 EXPORT_SYMBOL_GPL(s390_enable_sie);
1283
1284 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1285 int pmdp_clear_flush_young(struct vm_area_struct *vma, unsigned long address,
1286                            pmd_t *pmdp)
1287 {
1288         VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1289         /* No need to flush TLB
1290          * On s390 reference bits are in storage key and never in TLB */
1291         return pmdp_test_and_clear_young(vma, address, pmdp);
1292 }
1293
1294 int pmdp_set_access_flags(struct vm_area_struct *vma,
1295                           unsigned long address, pmd_t *pmdp,
1296                           pmd_t entry, int dirty)
1297 {
1298         VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1299
1300         if (pmd_same(*pmdp, entry))
1301                 return 0;
1302         pmdp_invalidate(vma, address, pmdp);
1303         set_pmd_at(vma->vm_mm, address, pmdp, entry);
1304         return 1;
1305 }
1306
1307 static void pmdp_splitting_flush_sync(void *arg)
1308 {
1309         /* Simply deliver the interrupt */
1310 }
1311
1312 void pmdp_splitting_flush(struct vm_area_struct *vma, unsigned long address,
1313                           pmd_t *pmdp)
1314 {
1315         VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1316         if (!test_and_set_bit(_SEGMENT_ENTRY_SPLIT_BIT,
1317                               (unsigned long *) pmdp)) {
1318                 /* need to serialize against gup-fast (IRQ disabled) */
1319                 smp_call_function(pmdp_splitting_flush_sync, NULL, 1);
1320         }
1321 }
1322
1323 void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
1324                                 pgtable_t pgtable)
1325 {
1326         struct list_head *lh = (struct list_head *) pgtable;
1327
1328         assert_spin_locked(&mm->page_table_lock);
1329
1330         /* FIFO */
1331         if (!pmd_huge_pte(mm, pmdp))
1332                 INIT_LIST_HEAD(lh);
1333         else
1334                 list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
1335         pmd_huge_pte(mm, pmdp) = pgtable;
1336 }
1337
1338 pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
1339 {
1340         struct list_head *lh;
1341         pgtable_t pgtable;
1342         pte_t *ptep;
1343
1344         assert_spin_locked(&mm->page_table_lock);
1345
1346         /* FIFO */
1347         pgtable = pmd_huge_pte(mm, pmdp);
1348         lh = (struct list_head *) pgtable;
1349         if (list_empty(lh))
1350                 pmd_huge_pte(mm, pmdp) = NULL;
1351         else {
1352                 pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
1353                 list_del(lh);
1354         }
1355         ptep = (pte_t *) pgtable;
1356         pte_val(*ptep) = _PAGE_INVALID;
1357         ptep++;
1358         pte_val(*ptep) = _PAGE_INVALID;
1359         return pgtable;
1360 }
1361 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */