Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs
[sfrench/cifs-2.6.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         /* large pmds cannot yet be handled */
509         if (pmd_large(*pmd))
510                 return -EFAULT;
511         /* pmd now points to a valid segment table entry. */
512         rmap = kmalloc(sizeof(*rmap), GFP_KERNEL|__GFP_REPEAT);
513         if (!rmap)
514                 return -ENOMEM;
515         /* Link gmap segment table entry location to page table. */
516         page = pmd_page(*pmd);
517         mp = (struct gmap_pgtable *) page->index;
518         rmap->gmap = gmap;
519         rmap->entry = segment_ptr;
520         rmap->vmaddr = address & PMD_MASK;
521         spin_lock(&mm->page_table_lock);
522         if (*segment_ptr == segment) {
523                 list_add(&rmap->list, &mp->mapper);
524                 /* Set gmap segment table entry to page table. */
525                 *segment_ptr = pmd_val(*pmd) & PAGE_MASK;
526                 rmap = NULL;
527         }
528         spin_unlock(&mm->page_table_lock);
529         kfree(rmap);
530         return 0;
531 }
532
533 static void gmap_disconnect_pgtable(struct mm_struct *mm, unsigned long *table)
534 {
535         struct gmap_rmap *rmap, *next;
536         struct gmap_pgtable *mp;
537         struct page *page;
538         int flush;
539
540         flush = 0;
541         spin_lock(&mm->page_table_lock);
542         page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
543         mp = (struct gmap_pgtable *) page->index;
544         list_for_each_entry_safe(rmap, next, &mp->mapper, list) {
545                 *rmap->entry = mp->vmaddr | (_SEGMENT_ENTRY_INVALID |
546                                              _SEGMENT_ENTRY_PROTECT);
547                 list_del(&rmap->list);
548                 kfree(rmap);
549                 flush = 1;
550         }
551         spin_unlock(&mm->page_table_lock);
552         if (flush)
553                 __tlb_flush_global();
554 }
555
556 /*
557  * this function is assumed to be called with mmap_sem held
558  */
559 unsigned long __gmap_fault(unsigned long address, struct gmap *gmap)
560 {
561         unsigned long *segment_ptr, segment;
562         struct gmap_pgtable *mp;
563         struct page *page;
564         int rc;
565
566         current->thread.gmap_addr = address;
567         segment_ptr = gmap_table_walk(address, gmap);
568         if (IS_ERR(segment_ptr))
569                 return -EFAULT;
570         /* Convert the gmap address to an mm address. */
571         while (1) {
572                 segment = *segment_ptr;
573                 if (!(segment & _SEGMENT_ENTRY_INVALID)) {
574                         /* Page table is present */
575                         page = pfn_to_page(segment >> PAGE_SHIFT);
576                         mp = (struct gmap_pgtable *) page->index;
577                         return mp->vmaddr | (address & ~PMD_MASK);
578                 }
579                 if (!(segment & _SEGMENT_ENTRY_PROTECT))
580                         /* Nothing mapped in the gmap address space. */
581                         break;
582                 rc = gmap_connect_pgtable(address, segment, segment_ptr, gmap);
583                 if (rc)
584                         return rc;
585         }
586         return -EFAULT;
587 }
588
589 unsigned long gmap_fault(unsigned long address, struct gmap *gmap)
590 {
591         unsigned long rc;
592
593         down_read(&gmap->mm->mmap_sem);
594         rc = __gmap_fault(address, gmap);
595         up_read(&gmap->mm->mmap_sem);
596
597         return rc;
598 }
599 EXPORT_SYMBOL_GPL(gmap_fault);
600
601 static void gmap_zap_swap_entry(swp_entry_t entry, struct mm_struct *mm)
602 {
603         if (!non_swap_entry(entry))
604                 dec_mm_counter(mm, MM_SWAPENTS);
605         else if (is_migration_entry(entry)) {
606                 struct page *page = migration_entry_to_page(entry);
607
608                 if (PageAnon(page))
609                         dec_mm_counter(mm, MM_ANONPAGES);
610                 else
611                         dec_mm_counter(mm, MM_FILEPAGES);
612         }
613         free_swap_and_cache(entry);
614 }
615
616 /**
617  * The mm->mmap_sem lock must be held
618  */
619 static void gmap_zap_unused(struct mm_struct *mm, unsigned long address)
620 {
621         unsigned long ptev, pgstev;
622         spinlock_t *ptl;
623         pgste_t pgste;
624         pte_t *ptep, pte;
625
626         ptep = get_locked_pte(mm, address, &ptl);
627         if (unlikely(!ptep))
628                 return;
629         pte = *ptep;
630         if (!pte_swap(pte))
631                 goto out_pte;
632         /* Zap unused and logically-zero pages */
633         pgste = pgste_get_lock(ptep);
634         pgstev = pgste_val(pgste);
635         ptev = pte_val(pte);
636         if (((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED) ||
637             ((pgstev & _PGSTE_GPS_ZERO) && (ptev & _PAGE_INVALID))) {
638                 gmap_zap_swap_entry(pte_to_swp_entry(pte), mm);
639                 pte_clear(mm, address, ptep);
640         }
641         pgste_set_unlock(ptep, pgste);
642 out_pte:
643         pte_unmap_unlock(*ptep, ptl);
644 }
645
646 /*
647  * this function is assumed to be called with mmap_sem held
648  */
649 void __gmap_zap(unsigned long address, struct gmap *gmap)
650 {
651         unsigned long *table, *segment_ptr;
652         unsigned long segment, pgstev, ptev;
653         struct gmap_pgtable *mp;
654         struct page *page;
655
656         segment_ptr = gmap_table_walk(address, gmap);
657         if (IS_ERR(segment_ptr))
658                 return;
659         segment = *segment_ptr;
660         if (segment & _SEGMENT_ENTRY_INVALID)
661                 return;
662         page = pfn_to_page(segment >> PAGE_SHIFT);
663         mp = (struct gmap_pgtable *) page->index;
664         address = mp->vmaddr | (address & ~PMD_MASK);
665         /* Page table is present */
666         table = (unsigned long *)(segment & _SEGMENT_ENTRY_ORIGIN);
667         table = table + ((address >> 12) & 0xff);
668         pgstev = table[PTRS_PER_PTE];
669         ptev = table[0];
670         /* quick check, checked again with locks held */
671         if (((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED) ||
672             ((pgstev & _PGSTE_GPS_ZERO) && (ptev & _PAGE_INVALID)))
673                 gmap_zap_unused(gmap->mm, address);
674 }
675 EXPORT_SYMBOL_GPL(__gmap_zap);
676
677 void gmap_discard(unsigned long from, unsigned long to, struct gmap *gmap)
678 {
679
680         unsigned long *table, address, size;
681         struct vm_area_struct *vma;
682         struct gmap_pgtable *mp;
683         struct page *page;
684
685         down_read(&gmap->mm->mmap_sem);
686         address = from;
687         while (address < to) {
688                 /* Walk the gmap address space page table */
689                 table = gmap->table + ((address >> 53) & 0x7ff);
690                 if (unlikely(*table & _REGION_ENTRY_INVALID)) {
691                         address = (address + PMD_SIZE) & PMD_MASK;
692                         continue;
693                 }
694                 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
695                 table = table + ((address >> 42) & 0x7ff);
696                 if (unlikely(*table & _REGION_ENTRY_INVALID)) {
697                         address = (address + PMD_SIZE) & PMD_MASK;
698                         continue;
699                 }
700                 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
701                 table = table + ((address >> 31) & 0x7ff);
702                 if (unlikely(*table & _REGION_ENTRY_INVALID)) {
703                         address = (address + PMD_SIZE) & PMD_MASK;
704                         continue;
705                 }
706                 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
707                 table = table + ((address >> 20) & 0x7ff);
708                 if (unlikely(*table & _SEGMENT_ENTRY_INVALID)) {
709                         address = (address + PMD_SIZE) & PMD_MASK;
710                         continue;
711                 }
712                 page = pfn_to_page(*table >> PAGE_SHIFT);
713                 mp = (struct gmap_pgtable *) page->index;
714                 vma = find_vma(gmap->mm, mp->vmaddr);
715                 size = min(to - address, PMD_SIZE - (address & ~PMD_MASK));
716                 zap_page_range(vma, mp->vmaddr | (address & ~PMD_MASK),
717                                size, NULL);
718                 address = (address + PMD_SIZE) & PMD_MASK;
719         }
720         up_read(&gmap->mm->mmap_sem);
721 }
722 EXPORT_SYMBOL_GPL(gmap_discard);
723
724 static LIST_HEAD(gmap_notifier_list);
725 static DEFINE_SPINLOCK(gmap_notifier_lock);
726
727 /**
728  * gmap_register_ipte_notifier - register a pte invalidation callback
729  * @nb: pointer to the gmap notifier block
730  */
731 void gmap_register_ipte_notifier(struct gmap_notifier *nb)
732 {
733         spin_lock(&gmap_notifier_lock);
734         list_add(&nb->list, &gmap_notifier_list);
735         spin_unlock(&gmap_notifier_lock);
736 }
737 EXPORT_SYMBOL_GPL(gmap_register_ipte_notifier);
738
739 /**
740  * gmap_unregister_ipte_notifier - remove a pte invalidation callback
741  * @nb: pointer to the gmap notifier block
742  */
743 void gmap_unregister_ipte_notifier(struct gmap_notifier *nb)
744 {
745         spin_lock(&gmap_notifier_lock);
746         list_del_init(&nb->list);
747         spin_unlock(&gmap_notifier_lock);
748 }
749 EXPORT_SYMBOL_GPL(gmap_unregister_ipte_notifier);
750
751 /**
752  * gmap_ipte_notify - mark a range of ptes for invalidation notification
753  * @gmap: pointer to guest mapping meta data structure
754  * @start: virtual address in the guest address space
755  * @len: size of area
756  *
757  * Returns 0 if for each page in the given range a gmap mapping exists and
758  * the invalidation notification could be set. If the gmap mapping is missing
759  * for one or more pages -EFAULT is returned. If no memory could be allocated
760  * -ENOMEM is returned. This function establishes missing page table entries.
761  */
762 int gmap_ipte_notify(struct gmap *gmap, unsigned long start, unsigned long len)
763 {
764         unsigned long addr;
765         spinlock_t *ptl;
766         pte_t *ptep, entry;
767         pgste_t pgste;
768         int rc = 0;
769
770         if ((start & ~PAGE_MASK) || (len & ~PAGE_MASK))
771                 return -EINVAL;
772         down_read(&gmap->mm->mmap_sem);
773         while (len) {
774                 /* Convert gmap address and connect the page tables */
775                 addr = __gmap_fault(start, gmap);
776                 if (IS_ERR_VALUE(addr)) {
777                         rc = addr;
778                         break;
779                 }
780                 /* Get the page mapped */
781                 if (fixup_user_fault(current, gmap->mm, addr, FAULT_FLAG_WRITE)) {
782                         rc = -EFAULT;
783                         break;
784                 }
785                 /* Walk the process page table, lock and get pte pointer */
786                 ptep = get_locked_pte(gmap->mm, addr, &ptl);
787                 if (unlikely(!ptep))
788                         continue;
789                 /* Set notification bit in the pgste of the pte */
790                 entry = *ptep;
791                 if ((pte_val(entry) & (_PAGE_INVALID | _PAGE_PROTECT)) == 0) {
792                         pgste = pgste_get_lock(ptep);
793                         pgste_val(pgste) |= PGSTE_IN_BIT;
794                         pgste_set_unlock(ptep, pgste);
795                         start += PAGE_SIZE;
796                         len -= PAGE_SIZE;
797                 }
798                 spin_unlock(ptl);
799         }
800         up_read(&gmap->mm->mmap_sem);
801         return rc;
802 }
803 EXPORT_SYMBOL_GPL(gmap_ipte_notify);
804
805 /**
806  * gmap_do_ipte_notify - call all invalidation callbacks for a specific pte.
807  * @mm: pointer to the process mm_struct
808  * @pte: pointer to the page table entry
809  *
810  * This function is assumed to be called with the page table lock held
811  * for the pte to notify.
812  */
813 void gmap_do_ipte_notify(struct mm_struct *mm, pte_t *pte)
814 {
815         unsigned long segment_offset;
816         struct gmap_notifier *nb;
817         struct gmap_pgtable *mp;
818         struct gmap_rmap *rmap;
819         struct page *page;
820
821         segment_offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
822         segment_offset = segment_offset * (4096 / sizeof(pte_t));
823         page = pfn_to_page(__pa(pte) >> PAGE_SHIFT);
824         mp = (struct gmap_pgtable *) page->index;
825         spin_lock(&gmap_notifier_lock);
826         list_for_each_entry(rmap, &mp->mapper, list) {
827                 list_for_each_entry(nb, &gmap_notifier_list, list)
828                         nb->notifier_call(rmap->gmap,
829                                           rmap->vmaddr + segment_offset);
830         }
831         spin_unlock(&gmap_notifier_lock);
832 }
833
834 static inline int page_table_with_pgste(struct page *page)
835 {
836         return atomic_read(&page->_mapcount) == 0;
837 }
838
839 static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm,
840                                                     unsigned long vmaddr)
841 {
842         struct page *page;
843         unsigned long *table;
844         struct gmap_pgtable *mp;
845
846         page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
847         if (!page)
848                 return NULL;
849         mp = kmalloc(sizeof(*mp), GFP_KERNEL|__GFP_REPEAT);
850         if (!mp) {
851                 __free_page(page);
852                 return NULL;
853         }
854         if (!pgtable_page_ctor(page)) {
855                 kfree(mp);
856                 __free_page(page);
857                 return NULL;
858         }
859         mp->vmaddr = vmaddr & PMD_MASK;
860         INIT_LIST_HEAD(&mp->mapper);
861         page->index = (unsigned long) mp;
862         atomic_set(&page->_mapcount, 0);
863         table = (unsigned long *) page_to_phys(page);
864         clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
865         clear_table(table + PTRS_PER_PTE, PGSTE_HR_BIT | PGSTE_HC_BIT,
866                     PAGE_SIZE/2);
867         return table;
868 }
869
870 static inline void page_table_free_pgste(unsigned long *table)
871 {
872         struct page *page;
873         struct gmap_pgtable *mp;
874
875         page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
876         mp = (struct gmap_pgtable *) page->index;
877         BUG_ON(!list_empty(&mp->mapper));
878         pgtable_page_dtor(page);
879         atomic_set(&page->_mapcount, -1);
880         kfree(mp);
881         __free_page(page);
882 }
883
884 static inline unsigned long page_table_reset_pte(struct mm_struct *mm,
885                         pmd_t *pmd, unsigned long addr, unsigned long end)
886 {
887         pte_t *start_pte, *pte;
888         spinlock_t *ptl;
889         pgste_t pgste;
890
891         start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
892         pte = start_pte;
893         do {
894                 pgste = pgste_get_lock(pte);
895                 pgste_val(pgste) &= ~_PGSTE_GPS_USAGE_MASK;
896                 pgste_set_unlock(pte, pgste);
897         } while (pte++, addr += PAGE_SIZE, addr != end);
898         pte_unmap_unlock(start_pte, ptl);
899
900         return addr;
901 }
902
903 static inline unsigned long page_table_reset_pmd(struct mm_struct *mm,
904                         pud_t *pud, unsigned long addr, unsigned long end)
905 {
906         unsigned long next;
907         pmd_t *pmd;
908
909         pmd = pmd_offset(pud, addr);
910         do {
911                 next = pmd_addr_end(addr, end);
912                 if (pmd_none_or_clear_bad(pmd))
913                         continue;
914                 next = page_table_reset_pte(mm, pmd, addr, next);
915         } while (pmd++, addr = next, addr != end);
916
917         return addr;
918 }
919
920 static inline unsigned long page_table_reset_pud(struct mm_struct *mm,
921                         pgd_t *pgd, unsigned long addr, unsigned long end)
922 {
923         unsigned long next;
924         pud_t *pud;
925
926         pud = pud_offset(pgd, addr);
927         do {
928                 next = pud_addr_end(addr, end);
929                 if (pud_none_or_clear_bad(pud))
930                         continue;
931                 next = page_table_reset_pmd(mm, pud, addr, next);
932         } while (pud++, addr = next, addr != end);
933
934         return addr;
935 }
936
937 void page_table_reset_pgste(struct mm_struct *mm,
938                         unsigned long start, unsigned long end)
939 {
940         unsigned long addr, next;
941         pgd_t *pgd;
942
943         addr = start;
944         down_read(&mm->mmap_sem);
945         pgd = pgd_offset(mm, addr);
946         do {
947                 next = pgd_addr_end(addr, end);
948                 if (pgd_none_or_clear_bad(pgd))
949                         continue;
950                 next = page_table_reset_pud(mm, pgd, addr, next);
951         } while (pgd++, addr = next, addr != end);
952         up_read(&mm->mmap_sem);
953 }
954 EXPORT_SYMBOL(page_table_reset_pgste);
955
956 int set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
957                           unsigned long key, bool nq)
958 {
959         spinlock_t *ptl;
960         pgste_t old, new;
961         pte_t *ptep;
962
963         down_read(&mm->mmap_sem);
964         ptep = get_locked_pte(current->mm, addr, &ptl);
965         if (unlikely(!ptep)) {
966                 up_read(&mm->mmap_sem);
967                 return -EFAULT;
968         }
969
970         new = old = pgste_get_lock(ptep);
971         pgste_val(new) &= ~(PGSTE_GR_BIT | PGSTE_GC_BIT |
972                             PGSTE_ACC_BITS | PGSTE_FP_BIT);
973         pgste_val(new) |= (key & (_PAGE_CHANGED | _PAGE_REFERENCED)) << 48;
974         pgste_val(new) |= (key & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
975         if (!(pte_val(*ptep) & _PAGE_INVALID)) {
976                 unsigned long address, bits, skey;
977
978                 address = pte_val(*ptep) & PAGE_MASK;
979                 skey = (unsigned long) page_get_storage_key(address);
980                 bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
981                 skey = key & (_PAGE_ACC_BITS | _PAGE_FP_BIT);
982                 /* Set storage key ACC and FP */
983                 page_set_storage_key(address, skey, !nq);
984                 /* Merge host changed & referenced into pgste  */
985                 pgste_val(new) |= bits << 52;
986         }
987         /* changing the guest storage key is considered a change of the page */
988         if ((pgste_val(new) ^ pgste_val(old)) &
989             (PGSTE_ACC_BITS | PGSTE_FP_BIT | PGSTE_GR_BIT | PGSTE_GC_BIT))
990                 pgste_val(new) |= PGSTE_HC_BIT;
991
992         pgste_set_unlock(ptep, new);
993         pte_unmap_unlock(*ptep, ptl);
994         up_read(&mm->mmap_sem);
995         return 0;
996 }
997 EXPORT_SYMBOL(set_guest_storage_key);
998
999 #else /* CONFIG_PGSTE */
1000
1001 static inline int page_table_with_pgste(struct page *page)
1002 {
1003         return 0;
1004 }
1005
1006 static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm,
1007                                                     unsigned long vmaddr)
1008 {
1009         return NULL;
1010 }
1011
1012 static inline void page_table_free_pgste(unsigned long *table)
1013 {
1014 }
1015
1016 static inline void gmap_disconnect_pgtable(struct mm_struct *mm,
1017                                            unsigned long *table)
1018 {
1019 }
1020
1021 #endif /* CONFIG_PGSTE */
1022
1023 static inline unsigned int atomic_xor_bits(atomic_t *v, unsigned int bits)
1024 {
1025         unsigned int old, new;
1026
1027         do {
1028                 old = atomic_read(v);
1029                 new = old ^ bits;
1030         } while (atomic_cmpxchg(v, old, new) != old);
1031         return new;
1032 }
1033
1034 /*
1035  * page table entry allocation/free routines.
1036  */
1037 unsigned long *page_table_alloc(struct mm_struct *mm, unsigned long vmaddr)
1038 {
1039         unsigned long *uninitialized_var(table);
1040         struct page *uninitialized_var(page);
1041         unsigned int mask, bit;
1042
1043         if (mm_has_pgste(mm))
1044                 return page_table_alloc_pgste(mm, vmaddr);
1045         /* Allocate fragments of a 4K page as 1K/2K page table */
1046         spin_lock_bh(&mm->context.list_lock);
1047         mask = FRAG_MASK;
1048         if (!list_empty(&mm->context.pgtable_list)) {
1049                 page = list_first_entry(&mm->context.pgtable_list,
1050                                         struct page, lru);
1051                 table = (unsigned long *) page_to_phys(page);
1052                 mask = atomic_read(&page->_mapcount);
1053                 mask = mask | (mask >> 4);
1054         }
1055         if ((mask & FRAG_MASK) == FRAG_MASK) {
1056                 spin_unlock_bh(&mm->context.list_lock);
1057                 page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
1058                 if (!page)
1059                         return NULL;
1060                 if (!pgtable_page_ctor(page)) {
1061                         __free_page(page);
1062                         return NULL;
1063                 }
1064                 atomic_set(&page->_mapcount, 1);
1065                 table = (unsigned long *) page_to_phys(page);
1066                 clear_table(table, _PAGE_INVALID, PAGE_SIZE);
1067                 spin_lock_bh(&mm->context.list_lock);
1068                 list_add(&page->lru, &mm->context.pgtable_list);
1069         } else {
1070                 for (bit = 1; mask & bit; bit <<= 1)
1071                         table += PTRS_PER_PTE;
1072                 mask = atomic_xor_bits(&page->_mapcount, bit);
1073                 if ((mask & FRAG_MASK) == FRAG_MASK)
1074                         list_del(&page->lru);
1075         }
1076         spin_unlock_bh(&mm->context.list_lock);
1077         return table;
1078 }
1079
1080 void page_table_free(struct mm_struct *mm, unsigned long *table)
1081 {
1082         struct page *page;
1083         unsigned int bit, mask;
1084
1085         page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1086         if (page_table_with_pgste(page)) {
1087                 gmap_disconnect_pgtable(mm, table);
1088                 return page_table_free_pgste(table);
1089         }
1090         /* Free 1K/2K page table fragment of a 4K page */
1091         bit = 1 << ((__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t)));
1092         spin_lock_bh(&mm->context.list_lock);
1093         if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK)
1094                 list_del(&page->lru);
1095         mask = atomic_xor_bits(&page->_mapcount, bit);
1096         if (mask & FRAG_MASK)
1097                 list_add(&page->lru, &mm->context.pgtable_list);
1098         spin_unlock_bh(&mm->context.list_lock);
1099         if (mask == 0) {
1100                 pgtable_page_dtor(page);
1101                 atomic_set(&page->_mapcount, -1);
1102                 __free_page(page);
1103         }
1104 }
1105
1106 static void __page_table_free_rcu(void *table, unsigned bit)
1107 {
1108         struct page *page;
1109
1110         if (bit == FRAG_MASK)
1111                 return page_table_free_pgste(table);
1112         /* Free 1K/2K page table fragment of a 4K page */
1113         page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1114         if (atomic_xor_bits(&page->_mapcount, bit) == 0) {
1115                 pgtable_page_dtor(page);
1116                 atomic_set(&page->_mapcount, -1);
1117                 __free_page(page);
1118         }
1119 }
1120
1121 void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table)
1122 {
1123         struct mm_struct *mm;
1124         struct page *page;
1125         unsigned int bit, mask;
1126
1127         mm = tlb->mm;
1128         page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1129         if (page_table_with_pgste(page)) {
1130                 gmap_disconnect_pgtable(mm, table);
1131                 table = (unsigned long *) (__pa(table) | FRAG_MASK);
1132                 tlb_remove_table(tlb, table);
1133                 return;
1134         }
1135         bit = 1 << ((__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t)));
1136         spin_lock_bh(&mm->context.list_lock);
1137         if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK)
1138                 list_del(&page->lru);
1139         mask = atomic_xor_bits(&page->_mapcount, bit | (bit << 4));
1140         if (mask & FRAG_MASK)
1141                 list_add_tail(&page->lru, &mm->context.pgtable_list);
1142         spin_unlock_bh(&mm->context.list_lock);
1143         table = (unsigned long *) (__pa(table) | (bit << 4));
1144         tlb_remove_table(tlb, table);
1145 }
1146
1147 static void __tlb_remove_table(void *_table)
1148 {
1149         const unsigned long mask = (FRAG_MASK << 4) | FRAG_MASK;
1150         void *table = (void *)((unsigned long) _table & ~mask);
1151         unsigned type = (unsigned long) _table & mask;
1152
1153         if (type)
1154                 __page_table_free_rcu(table, type);
1155         else
1156                 free_pages((unsigned long) table, ALLOC_ORDER);
1157 }
1158
1159 static void tlb_remove_table_smp_sync(void *arg)
1160 {
1161         /* Simply deliver the interrupt */
1162 }
1163
1164 static void tlb_remove_table_one(void *table)
1165 {
1166         /*
1167          * This isn't an RCU grace period and hence the page-tables cannot be
1168          * assumed to be actually RCU-freed.
1169          *
1170          * It is however sufficient for software page-table walkers that rely
1171          * on IRQ disabling. See the comment near struct mmu_table_batch.
1172          */
1173         smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
1174         __tlb_remove_table(table);
1175 }
1176
1177 static void tlb_remove_table_rcu(struct rcu_head *head)
1178 {
1179         struct mmu_table_batch *batch;
1180         int i;
1181
1182         batch = container_of(head, struct mmu_table_batch, rcu);
1183
1184         for (i = 0; i < batch->nr; i++)
1185                 __tlb_remove_table(batch->tables[i]);
1186
1187         free_page((unsigned long)batch);
1188 }
1189
1190 void tlb_table_flush(struct mmu_gather *tlb)
1191 {
1192         struct mmu_table_batch **batch = &tlb->batch;
1193
1194         if (*batch) {
1195                 call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
1196                 *batch = NULL;
1197         }
1198 }
1199
1200 void tlb_remove_table(struct mmu_gather *tlb, void *table)
1201 {
1202         struct mmu_table_batch **batch = &tlb->batch;
1203
1204         tlb->mm->context.flush_mm = 1;
1205         if (*batch == NULL) {
1206                 *batch = (struct mmu_table_batch *)
1207                         __get_free_page(GFP_NOWAIT | __GFP_NOWARN);
1208                 if (*batch == NULL) {
1209                         __tlb_flush_mm_lazy(tlb->mm);
1210                         tlb_remove_table_one(table);
1211                         return;
1212                 }
1213                 (*batch)->nr = 0;
1214         }
1215         (*batch)->tables[(*batch)->nr++] = table;
1216         if ((*batch)->nr == MAX_TABLE_BATCH)
1217                 tlb_flush_mmu(tlb);
1218 }
1219
1220 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1221 static inline void thp_split_vma(struct vm_area_struct *vma)
1222 {
1223         unsigned long addr;
1224
1225         for (addr = vma->vm_start; addr < vma->vm_end; addr += PAGE_SIZE)
1226                 follow_page(vma, addr, FOLL_SPLIT);
1227 }
1228
1229 static inline void thp_split_mm(struct mm_struct *mm)
1230 {
1231         struct vm_area_struct *vma;
1232
1233         for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) {
1234                 thp_split_vma(vma);
1235                 vma->vm_flags &= ~VM_HUGEPAGE;
1236                 vma->vm_flags |= VM_NOHUGEPAGE;
1237         }
1238         mm->def_flags |= VM_NOHUGEPAGE;
1239 }
1240 #else
1241 static inline void thp_split_mm(struct mm_struct *mm)
1242 {
1243 }
1244 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1245
1246 static unsigned long page_table_realloc_pmd(struct mmu_gather *tlb,
1247                                 struct mm_struct *mm, pud_t *pud,
1248                                 unsigned long addr, unsigned long end)
1249 {
1250         unsigned long next, *table, *new;
1251         struct page *page;
1252         pmd_t *pmd;
1253
1254         pmd = pmd_offset(pud, addr);
1255         do {
1256                 next = pmd_addr_end(addr, end);
1257 again:
1258                 if (pmd_none_or_clear_bad(pmd))
1259                         continue;
1260                 table = (unsigned long *) pmd_deref(*pmd);
1261                 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1262                 if (page_table_with_pgste(page))
1263                         continue;
1264                 /* Allocate new page table with pgstes */
1265                 new = page_table_alloc_pgste(mm, addr);
1266                 if (!new)
1267                         return -ENOMEM;
1268
1269                 spin_lock(&mm->page_table_lock);
1270                 if (likely((unsigned long *) pmd_deref(*pmd) == table)) {
1271                         /* Nuke pmd entry pointing to the "short" page table */
1272                         pmdp_flush_lazy(mm, addr, pmd);
1273                         pmd_clear(pmd);
1274                         /* Copy ptes from old table to new table */
1275                         memcpy(new, table, PAGE_SIZE/2);
1276                         clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
1277                         /* Establish new table */
1278                         pmd_populate(mm, pmd, (pte_t *) new);
1279                         /* Free old table with rcu, there might be a walker! */
1280                         page_table_free_rcu(tlb, table);
1281                         new = NULL;
1282                 }
1283                 spin_unlock(&mm->page_table_lock);
1284                 if (new) {
1285                         page_table_free_pgste(new);
1286                         goto again;
1287                 }
1288         } while (pmd++, addr = next, addr != end);
1289
1290         return addr;
1291 }
1292
1293 static unsigned long page_table_realloc_pud(struct mmu_gather *tlb,
1294                                    struct mm_struct *mm, pgd_t *pgd,
1295                                    unsigned long addr, unsigned long end)
1296 {
1297         unsigned long next;
1298         pud_t *pud;
1299
1300         pud = pud_offset(pgd, addr);
1301         do {
1302                 next = pud_addr_end(addr, end);
1303                 if (pud_none_or_clear_bad(pud))
1304                         continue;
1305                 next = page_table_realloc_pmd(tlb, mm, pud, addr, next);
1306                 if (unlikely(IS_ERR_VALUE(next)))
1307                         return next;
1308         } while (pud++, addr = next, addr != end);
1309
1310         return addr;
1311 }
1312
1313 static unsigned long page_table_realloc(struct mmu_gather *tlb, struct mm_struct *mm,
1314                                         unsigned long addr, unsigned long end)
1315 {
1316         unsigned long next;
1317         pgd_t *pgd;
1318
1319         pgd = pgd_offset(mm, addr);
1320         do {
1321                 next = pgd_addr_end(addr, end);
1322                 if (pgd_none_or_clear_bad(pgd))
1323                         continue;
1324                 next = page_table_realloc_pud(tlb, mm, pgd, addr, next);
1325                 if (unlikely(IS_ERR_VALUE(next)))
1326                         return next;
1327         } while (pgd++, addr = next, addr != end);
1328
1329         return 0;
1330 }
1331
1332 /*
1333  * switch on pgstes for its userspace process (for kvm)
1334  */
1335 int s390_enable_sie(void)
1336 {
1337         struct task_struct *tsk = current;
1338         struct mm_struct *mm = tsk->mm;
1339         struct mmu_gather tlb;
1340
1341         /* Do we have pgstes? if yes, we are done */
1342         if (mm_has_pgste(tsk->mm))
1343                 return 0;
1344
1345         down_write(&mm->mmap_sem);
1346         /* split thp mappings and disable thp for future mappings */
1347         thp_split_mm(mm);
1348         /* Reallocate the page tables with pgstes */
1349         tlb_gather_mmu(&tlb, mm, 0, TASK_SIZE);
1350         if (!page_table_realloc(&tlb, mm, 0, TASK_SIZE))
1351                 mm->context.has_pgste = 1;
1352         tlb_finish_mmu(&tlb, 0, TASK_SIZE);
1353         up_write(&mm->mmap_sem);
1354         return mm->context.has_pgste ? 0 : -ENOMEM;
1355 }
1356 EXPORT_SYMBOL_GPL(s390_enable_sie);
1357
1358 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1359 int pmdp_clear_flush_young(struct vm_area_struct *vma, unsigned long address,
1360                            pmd_t *pmdp)
1361 {
1362         VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1363         /* No need to flush TLB
1364          * On s390 reference bits are in storage key and never in TLB */
1365         return pmdp_test_and_clear_young(vma, address, pmdp);
1366 }
1367
1368 int pmdp_set_access_flags(struct vm_area_struct *vma,
1369                           unsigned long address, pmd_t *pmdp,
1370                           pmd_t entry, int dirty)
1371 {
1372         VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1373
1374         if (pmd_same(*pmdp, entry))
1375                 return 0;
1376         pmdp_invalidate(vma, address, pmdp);
1377         set_pmd_at(vma->vm_mm, address, pmdp, entry);
1378         return 1;
1379 }
1380
1381 static void pmdp_splitting_flush_sync(void *arg)
1382 {
1383         /* Simply deliver the interrupt */
1384 }
1385
1386 void pmdp_splitting_flush(struct vm_area_struct *vma, unsigned long address,
1387                           pmd_t *pmdp)
1388 {
1389         VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1390         if (!test_and_set_bit(_SEGMENT_ENTRY_SPLIT_BIT,
1391                               (unsigned long *) pmdp)) {
1392                 /* need to serialize against gup-fast (IRQ disabled) */
1393                 smp_call_function(pmdp_splitting_flush_sync, NULL, 1);
1394         }
1395 }
1396
1397 void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
1398                                 pgtable_t pgtable)
1399 {
1400         struct list_head *lh = (struct list_head *) pgtable;
1401
1402         assert_spin_locked(pmd_lockptr(mm, pmdp));
1403
1404         /* FIFO */
1405         if (!pmd_huge_pte(mm, pmdp))
1406                 INIT_LIST_HEAD(lh);
1407         else
1408                 list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
1409         pmd_huge_pte(mm, pmdp) = pgtable;
1410 }
1411
1412 pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
1413 {
1414         struct list_head *lh;
1415         pgtable_t pgtable;
1416         pte_t *ptep;
1417
1418         assert_spin_locked(pmd_lockptr(mm, pmdp));
1419
1420         /* FIFO */
1421         pgtable = pmd_huge_pte(mm, pmdp);
1422         lh = (struct list_head *) pgtable;
1423         if (list_empty(lh))
1424                 pmd_huge_pte(mm, pmdp) = NULL;
1425         else {
1426                 pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
1427                 list_del(lh);
1428         }
1429         ptep = (pte_t *) pgtable;
1430         pte_val(*ptep) = _PAGE_INVALID;
1431         ptep++;
1432         pte_val(*ptep) = _PAGE_INVALID;
1433         return pgtable;
1434 }
1435 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */