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