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