2 * Copyright IBM Corp. 2007, 2011
3 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
6 #include <linux/sched.h>
7 #include <linux/kernel.h>
8 #include <linux/errno.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>
21 #include <asm/pgtable.h>
22 #include <asm/pgalloc.h>
24 #include <asm/tlbflush.h>
25 #include <asm/mmu_context.h>
27 unsigned long *crst_table_alloc(struct mm_struct *mm)
29 struct page *page = alloc_pages(GFP_KERNEL, 2);
33 return (unsigned long *) page_to_phys(page);
36 void crst_table_free(struct mm_struct *mm, unsigned long *table)
38 free_pages((unsigned long) table, 2);
41 static void __crst_table_upgrade(void *arg)
43 struct mm_struct *mm = arg;
45 if (current->active_mm == mm) {
52 int crst_table_upgrade(struct mm_struct *mm)
54 unsigned long *table, *pgd;
56 /* upgrade should only happen from 3 to 4 levels */
57 BUG_ON(mm->context.asce_limit != (1UL << 42));
59 table = crst_table_alloc(mm);
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);
74 on_each_cpu(__crst_table_upgrade, mm, 0);
78 void crst_table_downgrade(struct mm_struct *mm)
82 /* downgrade should only happen from 3 to 2 levels (compat only) */
83 BUG_ON(mm->context.asce_limit != (1UL << 42));
85 if (current->active_mm == mm) {
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);
98 if (current->active_mm == mm)
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
109 * Returns a guest address space structure.
111 struct gmap *gmap_alloc(struct mm_struct *mm, unsigned long limit)
115 unsigned long *table;
116 unsigned long etype, atype;
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;
132 atype = _ASCE_TYPE_REGION1;
133 etype = _REGION1_ENTRY_EMPTY;
135 gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL);
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);
143 page = alloc_pages(GFP_KERNEL, 2);
147 list_add(&page->lru, &gmap->crst_list);
148 table = (unsigned long *) page_to_phys(page);
149 crst_table_init(table, etype);
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);
164 EXPORT_SYMBOL_GPL(gmap_alloc);
166 static void gmap_flush_tlb(struct gmap *gmap)
168 if (MACHINE_HAS_IDTE)
169 __tlb_flush_idte(gmap->asce);
171 __tlb_flush_global();
174 static void gmap_radix_tree_free(struct radix_tree_root *root)
176 struct radix_tree_iter iter;
177 unsigned long indices[16];
182 /* A radix tree is freed by deleting all of its entries */
186 radix_tree_for_each_slot(slot, root, &iter, index) {
187 indices[nr] = iter.index;
191 for (i = 0; i < nr; i++) {
193 radix_tree_delete(root, index);
199 * gmap_free - free a guest address space
200 * @gmap: pointer to the guest address space structure
202 void gmap_free(struct gmap *gmap)
204 struct page *page, *next;
207 if (MACHINE_HAS_IDTE)
208 __tlb_flush_idte(gmap->asce);
210 __tlb_flush_global();
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);
222 EXPORT_SYMBOL_GPL(gmap_free);
225 * gmap_enable - switch primary space to the guest address space
226 * @gmap: pointer to the guest address space structure
228 void gmap_enable(struct gmap *gmap)
230 S390_lowcore.gmap = (unsigned long) gmap;
232 EXPORT_SYMBOL_GPL(gmap_enable);
235 * gmap_disable - switch back to the standard primary address space
236 * @gmap: pointer to the guest address space structure
238 void gmap_disable(struct gmap *gmap)
240 S390_lowcore.gmap = 0UL;
242 EXPORT_SYMBOL_GPL(gmap_disable);
245 * gmap_alloc_table is assumed to be called with mmap_sem held
247 static int gmap_alloc_table(struct gmap *gmap, unsigned long *table,
248 unsigned long init, unsigned long gaddr)
253 /* since we dont free the gmap table until gmap_free we can unlock */
254 page = alloc_pages(GFP_KERNEL, 2);
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);
267 spin_unlock(&gmap->mm->page_table_lock);
269 __free_pages(page, 2);
274 * __gmap_segment_gaddr - find virtual address from segment pointer
275 * @entry: pointer to a segment table entry in the guest address space
277 * Returns the virtual address in the guest address space for the segment
279 static unsigned long __gmap_segment_gaddr(unsigned long *entry)
282 unsigned long offset, mask;
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;
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
296 * Returns 1 if a TLB flush is required
298 static int __gmap_unlink_by_vmaddr(struct gmap *gmap, unsigned long vmaddr)
300 unsigned long *entry;
303 spin_lock(&gmap->guest_table_lock);
304 entry = radix_tree_delete(&gmap->host_to_guest, vmaddr >> PMD_SHIFT);
306 flush = (*entry != _SEGMENT_ENTRY_INVALID);
307 *entry = _SEGMENT_ENTRY_INVALID;
309 spin_unlock(&gmap->guest_table_lock);
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
318 * Returns 1 if a TLB flush is required
320 static int __gmap_unmap_by_gaddr(struct gmap *gmap, unsigned long gaddr)
322 unsigned long vmaddr;
324 vmaddr = (unsigned long) radix_tree_delete(&gmap->guest_to_host,
326 return vmaddr ? __gmap_unlink_by_vmaddr(gmap, vmaddr) : 0;
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
335 * Returns 0 if the unmap succeeded, -EINVAL if not.
337 int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len)
342 if ((to | len) & (PMD_SIZE - 1))
344 if (len == 0 || to + len < to)
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);
353 gmap_flush_tlb(gmap);
356 EXPORT_SYMBOL_GPL(gmap_unmap_segment);
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
365 * Returns 0 if the mmap succeeded, -EINVAL or -ENOMEM if not.
367 int gmap_map_segment(struct gmap *gmap, unsigned long from,
368 unsigned long to, unsigned long len)
373 if ((from | to | len) & (PMD_SIZE - 1))
375 if (len == 0 || from + len < from || to + len < to ||
376 from + len > TASK_MAX_SIZE || to + len > gmap->asce_end)
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))
390 up_write(&gmap->mm->mmap_sem);
392 gmap_flush_tlb(gmap);
395 gmap_unmap_segment(gmap, to, len);
398 EXPORT_SYMBOL_GPL(gmap_map_segment);
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
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.
411 unsigned long __gmap_translate(struct gmap *gmap, unsigned long gaddr)
413 unsigned long vmaddr;
415 vmaddr = (unsigned long)
416 radix_tree_lookup(&gmap->guest_to_host, gaddr >> PMD_SHIFT);
417 return vmaddr ? (vmaddr | (gaddr & ~PMD_MASK)) : -EFAULT;
419 EXPORT_SYMBOL_GPL(__gmap_translate);
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
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.
430 unsigned long gmap_translate(struct gmap *gmap, unsigned long gaddr)
434 down_read(&gmap->mm->mmap_sem);
435 rc = __gmap_translate(gmap, gaddr);
436 up_read(&gmap->mm->mmap_sem);
439 EXPORT_SYMBOL_GPL(gmap_translate);
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
447 static void gmap_unlink(struct mm_struct *mm, unsigned long *table,
448 unsigned long vmaddr)
453 list_for_each_entry(gmap, &mm->context.gmap_list, list) {
454 flush = __gmap_unlink_by_vmaddr(gmap, vmaddr);
456 gmap_flush_tlb(gmap);
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
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.
471 int __gmap_link(struct gmap *gmap, unsigned long gaddr, unsigned long vmaddr)
473 struct mm_struct *mm;
474 unsigned long *table;
481 /* Create higher level tables in the gmap page 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))
489 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
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))
497 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
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))
505 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
507 table += (gaddr >> 20) & 0x7ff;
508 /* Walk the parent mm page table */
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 */
519 /* Link gmap segment table entry location to page table. */
520 rc = radix_tree_preload(GFP_KERNEL);
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);
529 *table = pmd_val(*pmd);
532 spin_unlock(&gmap->guest_table_lock);
534 radix_tree_preload_end();
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()
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.
547 int gmap_fault(struct gmap *gmap, unsigned long gaddr,
548 unsigned int fault_flags)
550 unsigned long vmaddr;
553 down_read(&gmap->mm->mmap_sem);
554 vmaddr = __gmap_translate(gmap, gaddr);
555 if (IS_ERR_VALUE(vmaddr)) {
559 if (fixup_user_fault(current, gmap->mm, vmaddr, fault_flags)) {
563 rc = __gmap_link(gmap, gaddr, vmaddr);
565 up_read(&gmap->mm->mmap_sem);
568 EXPORT_SYMBOL_GPL(gmap_fault);
570 static void gmap_zap_swap_entry(swp_entry_t entry, struct mm_struct *mm)
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);
578 dec_mm_counter(mm, MM_ANONPAGES);
580 dec_mm_counter(mm, MM_FILEPAGES);
582 free_swap_and_cache(entry);
586 * this function is assumed to be called with mmap_sem held
588 void __gmap_zap(struct gmap *gmap, unsigned long gaddr)
590 unsigned long vmaddr, ptev, pgstev;
595 /* Find the vm address for the guest address */
596 vmaddr = (unsigned long) radix_tree_lookup(&gmap->guest_to_host,
600 vmaddr |= gaddr & ~PMD_MASK;
601 /* Get pointer to the page table entry */
602 ptep = get_locked_pte(gmap->mm, vmaddr, &ptl);
608 /* Zap unused and logically-zero pages */
609 pgste = pgste_get_lock(ptep);
610 pgstev = pgste_val(pgste);
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);
617 pgste_set_unlock(ptep, pgste);
619 pte_unmap_unlock(ptep, ptl);
621 EXPORT_SYMBOL_GPL(__gmap_zap);
623 void gmap_discard(struct gmap *gmap, unsigned long from, unsigned long to)
625 unsigned long gaddr, vmaddr, size;
626 struct vm_area_struct *vma;
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,
637 vmaddr |= gaddr & ~PMD_MASK;
638 /* Find vma in the parent mm */
639 vma = find_vma(gmap->mm, vmaddr);
642 size = min(to - gaddr, PMD_SIZE - (gaddr & ~PMD_MASK));
643 zap_page_range(vma, vmaddr, size, NULL);
645 up_read(&gmap->mm->mmap_sem);
647 EXPORT_SYMBOL_GPL(gmap_discard);
649 static LIST_HEAD(gmap_notifier_list);
650 static DEFINE_SPINLOCK(gmap_notifier_lock);
653 * gmap_register_ipte_notifier - register a pte invalidation callback
654 * @nb: pointer to the gmap notifier block
656 void gmap_register_ipte_notifier(struct gmap_notifier *nb)
658 spin_lock(&gmap_notifier_lock);
659 list_add(&nb->list, &gmap_notifier_list);
660 spin_unlock(&gmap_notifier_lock);
662 EXPORT_SYMBOL_GPL(gmap_register_ipte_notifier);
665 * gmap_unregister_ipte_notifier - remove a pte invalidation callback
666 * @nb: pointer to the gmap notifier block
668 void gmap_unregister_ipte_notifier(struct gmap_notifier *nb)
670 spin_lock(&gmap_notifier_lock);
671 list_del_init(&nb->list);
672 spin_unlock(&gmap_notifier_lock);
674 EXPORT_SYMBOL_GPL(gmap_unregister_ipte_notifier);
677 * gmap_ipte_notify - mark a range of ptes for invalidation notification
678 * @gmap: pointer to guest mapping meta data structure
679 * @gaddr: virtual address in the guest address space
682 * Returns 0 if for each page in the given range a gmap mapping exists and
683 * the invalidation notification could be set. If the gmap mapping is missing
684 * for one or more pages -EFAULT is returned. If no memory could be allocated
685 * -ENOMEM is returned. This function establishes missing page table entries.
687 int gmap_ipte_notify(struct gmap *gmap, unsigned long gaddr, unsigned long len)
695 if ((gaddr & ~PAGE_MASK) || (len & ~PAGE_MASK))
697 down_read(&gmap->mm->mmap_sem);
699 /* Convert gmap address and connect the page tables */
700 addr = __gmap_translate(gmap, gaddr);
701 if (IS_ERR_VALUE(addr)) {
705 /* Get the page mapped */
706 if (fixup_user_fault(current, gmap->mm, addr, FAULT_FLAG_WRITE)) {
710 rc = __gmap_link(gmap, gaddr, addr);
713 /* Walk the process page table, lock and get pte pointer */
714 ptep = get_locked_pte(gmap->mm, addr, &ptl);
716 /* Set notification bit in the pgste of the pte */
718 if ((pte_val(entry) & (_PAGE_INVALID | _PAGE_PROTECT)) == 0) {
719 pgste = pgste_get_lock(ptep);
720 pgste_val(pgste) |= PGSTE_IN_BIT;
721 pgste_set_unlock(ptep, pgste);
725 pte_unmap_unlock(ptep, ptl);
727 up_read(&gmap->mm->mmap_sem);
730 EXPORT_SYMBOL_GPL(gmap_ipte_notify);
733 * gmap_do_ipte_notify - call all invalidation callbacks for a specific pte.
734 * @mm: pointer to the process mm_struct
735 * @addr: virtual address in the process address space
736 * @pte: pointer to the page table entry
738 * This function is assumed to be called with the page table lock held
739 * for the pte to notify.
741 void gmap_do_ipte_notify(struct mm_struct *mm, unsigned long vmaddr, pte_t *pte)
743 unsigned long offset, gaddr;
744 unsigned long *table;
745 struct gmap_notifier *nb;
748 offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
749 offset = offset * (4096 / sizeof(pte_t));
750 spin_lock(&gmap_notifier_lock);
751 list_for_each_entry(gmap, &mm->context.gmap_list, list) {
752 table = radix_tree_lookup(&gmap->host_to_guest,
753 vmaddr >> PMD_SHIFT);
756 gaddr = __gmap_segment_gaddr(table) + offset;
757 list_for_each_entry(nb, &gmap_notifier_list, list)
758 nb->notifier_call(gmap, gaddr);
760 spin_unlock(&gmap_notifier_lock);
762 EXPORT_SYMBOL_GPL(gmap_do_ipte_notify);
764 int set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
765 unsigned long key, bool nq)
771 down_read(&mm->mmap_sem);
773 ptep = get_locked_pte(mm, addr, &ptl);
774 if (unlikely(!ptep)) {
775 up_read(&mm->mmap_sem);
778 if (!(pte_val(*ptep) & _PAGE_INVALID) &&
779 (pte_val(*ptep) & _PAGE_PROTECT)) {
780 pte_unmap_unlock(ptep, ptl);
781 if (fixup_user_fault(current, mm, addr, FAULT_FLAG_WRITE)) {
782 up_read(&mm->mmap_sem);
788 new = old = pgste_get_lock(ptep);
789 pgste_val(new) &= ~(PGSTE_GR_BIT | PGSTE_GC_BIT |
790 PGSTE_ACC_BITS | PGSTE_FP_BIT);
791 pgste_val(new) |= (key & (_PAGE_CHANGED | _PAGE_REFERENCED)) << 48;
792 pgste_val(new) |= (key & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
793 if (!(pte_val(*ptep) & _PAGE_INVALID)) {
794 unsigned long address, bits, skey;
796 address = pte_val(*ptep) & PAGE_MASK;
797 skey = (unsigned long) page_get_storage_key(address);
798 bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
799 skey = key & (_PAGE_ACC_BITS | _PAGE_FP_BIT);
800 /* Set storage key ACC and FP */
801 page_set_storage_key(address, skey, !nq);
802 /* Merge host changed & referenced into pgste */
803 pgste_val(new) |= bits << 52;
805 /* changing the guest storage key is considered a change of the page */
806 if ((pgste_val(new) ^ pgste_val(old)) &
807 (PGSTE_ACC_BITS | PGSTE_FP_BIT | PGSTE_GR_BIT | PGSTE_GC_BIT))
808 pgste_val(new) |= PGSTE_UC_BIT;
810 pgste_set_unlock(ptep, new);
811 pte_unmap_unlock(ptep, ptl);
812 up_read(&mm->mmap_sem);
815 EXPORT_SYMBOL(set_guest_storage_key);
817 unsigned long get_guest_storage_key(struct mm_struct *mm, unsigned long addr)
823 unsigned long key = 0;
825 down_read(&mm->mmap_sem);
826 ptep = get_locked_pte(mm, addr, &ptl);
827 if (unlikely(!ptep)) {
828 up_read(&mm->mmap_sem);
831 pgste = pgste_get_lock(ptep);
833 if (pte_val(*ptep) & _PAGE_INVALID) {
834 key |= (pgste_val(pgste) & PGSTE_ACC_BITS) >> 56;
835 key |= (pgste_val(pgste) & PGSTE_FP_BIT) >> 56;
836 key |= (pgste_val(pgste) & PGSTE_GR_BIT) >> 48;
837 key |= (pgste_val(pgste) & PGSTE_GC_BIT) >> 48;
839 physaddr = pte_val(*ptep) & PAGE_MASK;
840 key = page_get_storage_key(physaddr);
842 /* Reflect guest's logical view, not physical */
843 if (pgste_val(pgste) & PGSTE_GR_BIT)
844 key |= _PAGE_REFERENCED;
845 if (pgste_val(pgste) & PGSTE_GC_BIT)
846 key |= _PAGE_CHANGED;
849 pgste_set_unlock(ptep, pgste);
850 pte_unmap_unlock(ptep, ptl);
851 up_read(&mm->mmap_sem);
854 EXPORT_SYMBOL(get_guest_storage_key);
856 static int page_table_allocate_pgste_min = 0;
857 static int page_table_allocate_pgste_max = 1;
858 int page_table_allocate_pgste = 0;
859 EXPORT_SYMBOL(page_table_allocate_pgste);
861 static struct ctl_table page_table_sysctl[] = {
863 .procname = "allocate_pgste",
864 .data = &page_table_allocate_pgste,
865 .maxlen = sizeof(int),
866 .mode = S_IRUGO | S_IWUSR,
867 .proc_handler = proc_dointvec,
868 .extra1 = &page_table_allocate_pgste_min,
869 .extra2 = &page_table_allocate_pgste_max,
874 static struct ctl_table page_table_sysctl_dir[] = {
879 .child = page_table_sysctl,
884 static int __init page_table_register_sysctl(void)
886 return register_sysctl_table(page_table_sysctl_dir) ? 0 : -ENOMEM;
888 __initcall(page_table_register_sysctl);
890 #else /* CONFIG_PGSTE */
892 static inline void gmap_unlink(struct mm_struct *mm, unsigned long *table,
893 unsigned long vmaddr)
897 #endif /* CONFIG_PGSTE */
899 static inline unsigned int atomic_xor_bits(atomic_t *v, unsigned int bits)
901 unsigned int old, new;
904 old = atomic_read(v);
906 } while (atomic_cmpxchg(v, old, new) != old);
911 * page table entry allocation/free routines.
913 unsigned long *page_table_alloc(struct mm_struct *mm)
915 unsigned long *table;
917 unsigned int mask, bit;
919 /* Try to get a fragment of a 4K page as a 2K page table */
920 if (!mm_alloc_pgste(mm)) {
922 spin_lock_bh(&mm->context.list_lock);
923 if (!list_empty(&mm->context.pgtable_list)) {
924 page = list_first_entry(&mm->context.pgtable_list,
926 mask = atomic_read(&page->_mapcount);
927 mask = (mask | (mask >> 4)) & 3;
929 table = (unsigned long *) page_to_phys(page);
930 bit = mask & 1; /* =1 -> second 2K */
932 table += PTRS_PER_PTE;
933 atomic_xor_bits(&page->_mapcount, 1U << bit);
934 list_del(&page->lru);
937 spin_unlock_bh(&mm->context.list_lock);
941 /* Allocate a fresh page */
942 page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
945 if (!pgtable_page_ctor(page)) {
949 /* Initialize page table */
950 table = (unsigned long *) page_to_phys(page);
951 if (mm_alloc_pgste(mm)) {
952 /* Return 4K page table with PGSTEs */
953 atomic_set(&page->_mapcount, 3);
954 clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
955 clear_table(table + PTRS_PER_PTE, 0, PAGE_SIZE/2);
957 /* Return the first 2K fragment of the page */
958 atomic_set(&page->_mapcount, 1);
959 clear_table(table, _PAGE_INVALID, PAGE_SIZE);
960 spin_lock_bh(&mm->context.list_lock);
961 list_add(&page->lru, &mm->context.pgtable_list);
962 spin_unlock_bh(&mm->context.list_lock);
967 void page_table_free(struct mm_struct *mm, unsigned long *table)
970 unsigned int bit, mask;
972 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
973 if (!mm_alloc_pgste(mm)) {
974 /* Free 2K page table fragment of a 4K page */
975 bit = (__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t));
976 spin_lock_bh(&mm->context.list_lock);
977 mask = atomic_xor_bits(&page->_mapcount, 1U << bit);
979 list_add(&page->lru, &mm->context.pgtable_list);
981 list_del(&page->lru);
982 spin_unlock_bh(&mm->context.list_lock);
987 pgtable_page_dtor(page);
988 atomic_set(&page->_mapcount, -1);
992 void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table,
993 unsigned long vmaddr)
995 struct mm_struct *mm;
997 unsigned int bit, mask;
1000 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1001 if (mm_alloc_pgste(mm)) {
1002 gmap_unlink(mm, table, vmaddr);
1003 table = (unsigned long *) (__pa(table) | 3);
1004 tlb_remove_table(tlb, table);
1007 bit = (__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t));
1008 spin_lock_bh(&mm->context.list_lock);
1009 mask = atomic_xor_bits(&page->_mapcount, 0x11U << bit);
1011 list_add_tail(&page->lru, &mm->context.pgtable_list);
1013 list_del(&page->lru);
1014 spin_unlock_bh(&mm->context.list_lock);
1015 table = (unsigned long *) (__pa(table) | (1U << bit));
1016 tlb_remove_table(tlb, table);
1019 static void __tlb_remove_table(void *_table)
1021 unsigned int mask = (unsigned long) _table & 3;
1022 void *table = (void *)((unsigned long) _table ^ mask);
1023 struct page *page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1026 case 0: /* pmd or pud */
1027 free_pages((unsigned long) table, 2);
1029 case 1: /* lower 2K of a 4K page table */
1030 case 2: /* higher 2K of a 4K page table */
1031 if (atomic_xor_bits(&page->_mapcount, mask << 4) != 0)
1034 case 3: /* 4K page table with pgstes */
1035 pgtable_page_dtor(page);
1036 atomic_set(&page->_mapcount, -1);
1042 static void tlb_remove_table_smp_sync(void *arg)
1044 /* Simply deliver the interrupt */
1047 static void tlb_remove_table_one(void *table)
1050 * This isn't an RCU grace period and hence the page-tables cannot be
1051 * assumed to be actually RCU-freed.
1053 * It is however sufficient for software page-table walkers that rely
1054 * on IRQ disabling. See the comment near struct mmu_table_batch.
1056 smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
1057 __tlb_remove_table(table);
1060 static void tlb_remove_table_rcu(struct rcu_head *head)
1062 struct mmu_table_batch *batch;
1065 batch = container_of(head, struct mmu_table_batch, rcu);
1067 for (i = 0; i < batch->nr; i++)
1068 __tlb_remove_table(batch->tables[i]);
1070 free_page((unsigned long)batch);
1073 void tlb_table_flush(struct mmu_gather *tlb)
1075 struct mmu_table_batch **batch = &tlb->batch;
1078 call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
1083 void tlb_remove_table(struct mmu_gather *tlb, void *table)
1085 struct mmu_table_batch **batch = &tlb->batch;
1087 tlb->mm->context.flush_mm = 1;
1088 if (*batch == NULL) {
1089 *batch = (struct mmu_table_batch *)
1090 __get_free_page(GFP_NOWAIT | __GFP_NOWARN);
1091 if (*batch == NULL) {
1092 __tlb_flush_mm_lazy(tlb->mm);
1093 tlb_remove_table_one(table);
1098 (*batch)->tables[(*batch)->nr++] = table;
1099 if ((*batch)->nr == MAX_TABLE_BATCH)
1103 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1104 static inline void thp_split_vma(struct vm_area_struct *vma)
1108 for (addr = vma->vm_start; addr < vma->vm_end; addr += PAGE_SIZE)
1109 follow_page(vma, addr, FOLL_SPLIT);
1112 static inline void thp_split_mm(struct mm_struct *mm)
1114 struct vm_area_struct *vma;
1116 for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) {
1118 vma->vm_flags &= ~VM_HUGEPAGE;
1119 vma->vm_flags |= VM_NOHUGEPAGE;
1121 mm->def_flags |= VM_NOHUGEPAGE;
1124 static inline void thp_split_mm(struct mm_struct *mm)
1127 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1130 * switch on pgstes for its userspace process (for kvm)
1132 int s390_enable_sie(void)
1134 struct mm_struct *mm = current->mm;
1136 /* Do we have pgstes? if yes, we are done */
1137 if (mm_has_pgste(mm))
1139 /* Fail if the page tables are 2K */
1140 if (!mm_alloc_pgste(mm))
1142 down_write(&mm->mmap_sem);
1143 mm->context.has_pgste = 1;
1144 /* split thp mappings and disable thp for future mappings */
1146 up_write(&mm->mmap_sem);
1149 EXPORT_SYMBOL_GPL(s390_enable_sie);
1152 * Enable storage key handling from now on and initialize the storage
1153 * keys with the default key.
1155 static int __s390_enable_skey(pte_t *pte, unsigned long addr,
1156 unsigned long next, struct mm_walk *walk)
1161 pgste = pgste_get_lock(pte);
1163 * Remove all zero page mappings,
1164 * after establishing a policy to forbid zero page mappings
1165 * following faults for that page will get fresh anonymous pages
1167 if (is_zero_pfn(pte_pfn(*pte))) {
1168 ptep_flush_direct(walk->mm, addr, pte);
1169 pte_val(*pte) = _PAGE_INVALID;
1171 /* Clear storage key */
1172 pgste_val(pgste) &= ~(PGSTE_ACC_BITS | PGSTE_FP_BIT |
1173 PGSTE_GR_BIT | PGSTE_GC_BIT);
1174 ptev = pte_val(*pte);
1175 if (!(ptev & _PAGE_INVALID) && (ptev & _PAGE_WRITE))
1176 page_set_storage_key(ptev & PAGE_MASK, PAGE_DEFAULT_KEY, 1);
1177 pgste_set_unlock(pte, pgste);
1181 int s390_enable_skey(void)
1183 struct mm_walk walk = { .pte_entry = __s390_enable_skey };
1184 struct mm_struct *mm = current->mm;
1185 struct vm_area_struct *vma;
1188 down_write(&mm->mmap_sem);
1189 if (mm_use_skey(mm))
1192 mm->context.use_skey = 1;
1193 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1194 if (ksm_madvise(vma, vma->vm_start, vma->vm_end,
1195 MADV_UNMERGEABLE, &vma->vm_flags)) {
1196 mm->context.use_skey = 0;
1201 mm->def_flags &= ~VM_MERGEABLE;
1204 walk_page_range(0, TASK_SIZE, &walk);
1207 up_write(&mm->mmap_sem);
1210 EXPORT_SYMBOL_GPL(s390_enable_skey);
1213 * Reset CMMA state, make all pages stable again.
1215 static int __s390_reset_cmma(pte_t *pte, unsigned long addr,
1216 unsigned long next, struct mm_walk *walk)
1220 pgste = pgste_get_lock(pte);
1221 pgste_val(pgste) &= ~_PGSTE_GPS_USAGE_MASK;
1222 pgste_set_unlock(pte, pgste);
1226 void s390_reset_cmma(struct mm_struct *mm)
1228 struct mm_walk walk = { .pte_entry = __s390_reset_cmma };
1230 down_write(&mm->mmap_sem);
1232 walk_page_range(0, TASK_SIZE, &walk);
1233 up_write(&mm->mmap_sem);
1235 EXPORT_SYMBOL_GPL(s390_reset_cmma);
1238 * Test and reset if a guest page is dirty
1240 bool gmap_test_and_clear_dirty(unsigned long address, struct gmap *gmap)
1249 pgd = pgd_offset(gmap->mm, address);
1250 pud = pud_alloc(gmap->mm, pgd, address);
1253 pmd = pmd_alloc(gmap->mm, pud, address);
1256 /* We can't run guests backed by huge pages, but userspace can
1257 * still set them up and then try to migrate them without any
1258 * migration support.
1260 if (pmd_large(*pmd))
1263 pte = pte_alloc_map_lock(gmap->mm, pmd, address, &ptl);
1267 if (ptep_test_and_clear_user_dirty(gmap->mm, address, pte))
1273 EXPORT_SYMBOL_GPL(gmap_test_and_clear_dirty);
1275 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1276 int pmdp_clear_flush_young(struct vm_area_struct *vma, unsigned long address,
1279 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1280 /* No need to flush TLB
1281 * On s390 reference bits are in storage key and never in TLB */
1282 return pmdp_test_and_clear_young(vma, address, pmdp);
1285 int pmdp_set_access_flags(struct vm_area_struct *vma,
1286 unsigned long address, pmd_t *pmdp,
1287 pmd_t entry, int dirty)
1289 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1291 entry = pmd_mkyoung(entry);
1293 entry = pmd_mkdirty(entry);
1294 if (pmd_same(*pmdp, entry))
1296 pmdp_invalidate(vma, address, pmdp);
1297 set_pmd_at(vma->vm_mm, address, pmdp, entry);
1301 static void pmdp_splitting_flush_sync(void *arg)
1303 /* Simply deliver the interrupt */
1306 void pmdp_splitting_flush(struct vm_area_struct *vma, unsigned long address,
1309 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1310 if (!test_and_set_bit(_SEGMENT_ENTRY_SPLIT_BIT,
1311 (unsigned long *) pmdp)) {
1312 /* need to serialize against gup-fast (IRQ disabled) */
1313 smp_call_function(pmdp_splitting_flush_sync, NULL, 1);
1317 void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
1320 struct list_head *lh = (struct list_head *) pgtable;
1322 assert_spin_locked(pmd_lockptr(mm, pmdp));
1325 if (!pmd_huge_pte(mm, pmdp))
1328 list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
1329 pmd_huge_pte(mm, pmdp) = pgtable;
1332 pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
1334 struct list_head *lh;
1338 assert_spin_locked(pmd_lockptr(mm, pmdp));
1341 pgtable = pmd_huge_pte(mm, pmdp);
1342 lh = (struct list_head *) pgtable;
1344 pmd_huge_pte(mm, pmdp) = NULL;
1346 pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
1349 ptep = (pte_t *) pgtable;
1350 pte_val(*ptep) = _PAGE_INVALID;
1352 pte_val(*ptep) = _PAGE_INVALID;
1355 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */