1 // SPDX-License-Identifier: GPL-2.0
3 * KVM guest address space mapping code
5 * Copyright IBM Corp. 2007, 2020
6 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
7 * David Hildenbrand <david@redhat.com>
8 * Janosch Frank <frankja@linux.vnet.ibm.com>
11 #include <linux/kernel.h>
12 #include <linux/pagewalk.h>
13 #include <linux/swap.h>
14 #include <linux/smp.h>
15 #include <linux/spinlock.h>
16 #include <linux/slab.h>
17 #include <linux/swapops.h>
18 #include <linux/ksm.h>
19 #include <linux/mman.h>
20 #include <linux/pgtable.h>
21 #include <asm/page-states.h>
22 #include <asm/pgalloc.h>
27 #define GMAP_SHADOW_FAKE_TABLE 1ULL
29 static struct page *gmap_alloc_crst(void)
33 page = alloc_pages(GFP_KERNEL_ACCOUNT, CRST_ALLOC_ORDER);
36 __arch_set_page_dat(page_to_virt(page), 1UL << CRST_ALLOC_ORDER);
41 * gmap_alloc - allocate and initialize a guest address space
42 * @limit: maximum address of the gmap address space
44 * Returns a guest address space structure.
46 static struct gmap *gmap_alloc(unsigned long limit)
51 unsigned long etype, atype;
53 if (limit < _REGION3_SIZE) {
54 limit = _REGION3_SIZE - 1;
55 atype = _ASCE_TYPE_SEGMENT;
56 etype = _SEGMENT_ENTRY_EMPTY;
57 } else if (limit < _REGION2_SIZE) {
58 limit = _REGION2_SIZE - 1;
59 atype = _ASCE_TYPE_REGION3;
60 etype = _REGION3_ENTRY_EMPTY;
61 } else if (limit < _REGION1_SIZE) {
62 limit = _REGION1_SIZE - 1;
63 atype = _ASCE_TYPE_REGION2;
64 etype = _REGION2_ENTRY_EMPTY;
67 atype = _ASCE_TYPE_REGION1;
68 etype = _REGION1_ENTRY_EMPTY;
70 gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL_ACCOUNT);
73 INIT_LIST_HEAD(&gmap->crst_list);
74 INIT_LIST_HEAD(&gmap->children);
75 INIT_LIST_HEAD(&gmap->pt_list);
76 INIT_RADIX_TREE(&gmap->guest_to_host, GFP_KERNEL_ACCOUNT);
77 INIT_RADIX_TREE(&gmap->host_to_guest, GFP_ATOMIC | __GFP_ACCOUNT);
78 INIT_RADIX_TREE(&gmap->host_to_rmap, GFP_ATOMIC | __GFP_ACCOUNT);
79 spin_lock_init(&gmap->guest_table_lock);
80 spin_lock_init(&gmap->shadow_lock);
81 refcount_set(&gmap->ref_count, 1);
82 page = gmap_alloc_crst();
86 list_add(&page->lru, &gmap->crst_list);
87 table = page_to_virt(page);
88 crst_table_init(table, etype);
90 gmap->asce = atype | _ASCE_TABLE_LENGTH |
91 _ASCE_USER_BITS | __pa(table);
92 gmap->asce_end = limit;
102 * gmap_create - create a guest address space
103 * @mm: pointer to the parent mm_struct
104 * @limit: maximum size of the gmap address space
106 * Returns a guest address space structure.
108 struct gmap *gmap_create(struct mm_struct *mm, unsigned long limit)
111 unsigned long gmap_asce;
113 gmap = gmap_alloc(limit);
117 spin_lock(&mm->context.lock);
118 list_add_rcu(&gmap->list, &mm->context.gmap_list);
119 if (list_is_singular(&mm->context.gmap_list))
120 gmap_asce = gmap->asce;
123 WRITE_ONCE(mm->context.gmap_asce, gmap_asce);
124 spin_unlock(&mm->context.lock);
127 EXPORT_SYMBOL_GPL(gmap_create);
129 static void gmap_flush_tlb(struct gmap *gmap)
131 if (MACHINE_HAS_IDTE)
132 __tlb_flush_idte(gmap->asce);
134 __tlb_flush_global();
137 static void gmap_radix_tree_free(struct radix_tree_root *root)
139 struct radix_tree_iter iter;
140 unsigned long indices[16];
145 /* A radix tree is freed by deleting all of its entries */
149 radix_tree_for_each_slot(slot, root, &iter, index) {
150 indices[nr] = iter.index;
154 for (i = 0; i < nr; i++) {
156 radix_tree_delete(root, index);
161 static void gmap_rmap_radix_tree_free(struct radix_tree_root *root)
163 struct gmap_rmap *rmap, *rnext, *head;
164 struct radix_tree_iter iter;
165 unsigned long indices[16];
170 /* A radix tree is freed by deleting all of its entries */
174 radix_tree_for_each_slot(slot, root, &iter, index) {
175 indices[nr] = iter.index;
179 for (i = 0; i < nr; i++) {
181 head = radix_tree_delete(root, index);
182 gmap_for_each_rmap_safe(rmap, rnext, head)
189 * gmap_free - free a guest address space
190 * @gmap: pointer to the guest address space structure
192 * No locks required. There are no references to this gmap anymore.
194 static void gmap_free(struct gmap *gmap)
196 struct page *page, *next;
198 /* Flush tlb of all gmaps (if not already done for shadows) */
199 if (!(gmap_is_shadow(gmap) && gmap->removed))
200 gmap_flush_tlb(gmap);
201 /* Free all segment & region tables. */
202 list_for_each_entry_safe(page, next, &gmap->crst_list, lru)
203 __free_pages(page, CRST_ALLOC_ORDER);
204 gmap_radix_tree_free(&gmap->guest_to_host);
205 gmap_radix_tree_free(&gmap->host_to_guest);
207 /* Free additional data for a shadow gmap */
208 if (gmap_is_shadow(gmap)) {
209 struct ptdesc *ptdesc, *n;
211 /* Free all page tables. */
212 list_for_each_entry_safe(ptdesc, n, &gmap->pt_list, pt_list)
213 page_table_free_pgste(ptdesc);
214 gmap_rmap_radix_tree_free(&gmap->host_to_rmap);
215 /* Release reference to the parent */
216 gmap_put(gmap->parent);
223 * gmap_get - increase reference counter for guest address space
224 * @gmap: pointer to the guest address space structure
226 * Returns the gmap pointer
228 struct gmap *gmap_get(struct gmap *gmap)
230 refcount_inc(&gmap->ref_count);
233 EXPORT_SYMBOL_GPL(gmap_get);
236 * gmap_put - decrease reference counter for guest address space
237 * @gmap: pointer to the guest address space structure
239 * If the reference counter reaches zero the guest address space is freed.
241 void gmap_put(struct gmap *gmap)
243 if (refcount_dec_and_test(&gmap->ref_count))
246 EXPORT_SYMBOL_GPL(gmap_put);
249 * gmap_remove - remove a guest address space but do not free it yet
250 * @gmap: pointer to the guest address space structure
252 void gmap_remove(struct gmap *gmap)
254 struct gmap *sg, *next;
255 unsigned long gmap_asce;
257 /* Remove all shadow gmaps linked to this gmap */
258 if (!list_empty(&gmap->children)) {
259 spin_lock(&gmap->shadow_lock);
260 list_for_each_entry_safe(sg, next, &gmap->children, list) {
264 spin_unlock(&gmap->shadow_lock);
266 /* Remove gmap from the pre-mm list */
267 spin_lock(&gmap->mm->context.lock);
268 list_del_rcu(&gmap->list);
269 if (list_empty(&gmap->mm->context.gmap_list))
271 else if (list_is_singular(&gmap->mm->context.gmap_list))
272 gmap_asce = list_first_entry(&gmap->mm->context.gmap_list,
273 struct gmap, list)->asce;
276 WRITE_ONCE(gmap->mm->context.gmap_asce, gmap_asce);
277 spin_unlock(&gmap->mm->context.lock);
282 EXPORT_SYMBOL_GPL(gmap_remove);
285 * gmap_enable - switch primary space to the guest address space
286 * @gmap: pointer to the guest address space structure
288 void gmap_enable(struct gmap *gmap)
290 get_lowcore()->gmap = (unsigned long)gmap;
292 EXPORT_SYMBOL_GPL(gmap_enable);
295 * gmap_disable - switch back to the standard primary address space
296 * @gmap: pointer to the guest address space structure
298 void gmap_disable(struct gmap *gmap)
300 get_lowcore()->gmap = 0UL;
302 EXPORT_SYMBOL_GPL(gmap_disable);
305 * gmap_get_enabled - get a pointer to the currently enabled gmap
307 * Returns a pointer to the currently enabled gmap. 0 if none is enabled.
309 struct gmap *gmap_get_enabled(void)
311 return (struct gmap *)get_lowcore()->gmap;
313 EXPORT_SYMBOL_GPL(gmap_get_enabled);
316 * gmap_alloc_table is assumed to be called with mmap_lock held
318 static int gmap_alloc_table(struct gmap *gmap, unsigned long *table,
319 unsigned long init, unsigned long gaddr)
324 /* since we dont free the gmap table until gmap_free we can unlock */
325 page = gmap_alloc_crst();
328 new = page_to_virt(page);
329 crst_table_init(new, init);
330 spin_lock(&gmap->guest_table_lock);
331 if (*table & _REGION_ENTRY_INVALID) {
332 list_add(&page->lru, &gmap->crst_list);
333 *table = __pa(new) | _REGION_ENTRY_LENGTH |
334 (*table & _REGION_ENTRY_TYPE_MASK);
338 spin_unlock(&gmap->guest_table_lock);
340 __free_pages(page, CRST_ALLOC_ORDER);
345 * __gmap_segment_gaddr - find virtual address from segment pointer
346 * @entry: pointer to a segment table entry in the guest address space
348 * Returns the virtual address in the guest address space for the segment
350 static unsigned long __gmap_segment_gaddr(unsigned long *entry)
353 unsigned long offset;
355 offset = (unsigned long) entry / sizeof(unsigned long);
356 offset = (offset & (PTRS_PER_PMD - 1)) * PMD_SIZE;
357 page = pmd_pgtable_page((pmd_t *) entry);
358 return page->index + offset;
362 * __gmap_unlink_by_vmaddr - unlink a single segment via a host address
363 * @gmap: pointer to the guest address space structure
364 * @vmaddr: address in the host process address space
366 * Returns 1 if a TLB flush is required
368 static int __gmap_unlink_by_vmaddr(struct gmap *gmap, unsigned long vmaddr)
370 unsigned long *entry;
373 BUG_ON(gmap_is_shadow(gmap));
374 spin_lock(&gmap->guest_table_lock);
375 entry = radix_tree_delete(&gmap->host_to_guest, vmaddr >> PMD_SHIFT);
377 flush = (*entry != _SEGMENT_ENTRY_EMPTY);
378 *entry = _SEGMENT_ENTRY_EMPTY;
380 spin_unlock(&gmap->guest_table_lock);
385 * __gmap_unmap_by_gaddr - unmap a single segment via a guest address
386 * @gmap: pointer to the guest address space structure
387 * @gaddr: address in the guest address space
389 * Returns 1 if a TLB flush is required
391 static int __gmap_unmap_by_gaddr(struct gmap *gmap, unsigned long gaddr)
393 unsigned long vmaddr;
395 vmaddr = (unsigned long) radix_tree_delete(&gmap->guest_to_host,
397 return vmaddr ? __gmap_unlink_by_vmaddr(gmap, vmaddr) : 0;
401 * gmap_unmap_segment - unmap segment from the guest address space
402 * @gmap: pointer to the guest address space structure
403 * @to: address in the guest address space
404 * @len: length of the memory area to unmap
406 * Returns 0 if the unmap succeeded, -EINVAL if not.
408 int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len)
413 BUG_ON(gmap_is_shadow(gmap));
414 if ((to | len) & (PMD_SIZE - 1))
416 if (len == 0 || to + len < to)
420 mmap_write_lock(gmap->mm);
421 for (off = 0; off < len; off += PMD_SIZE)
422 flush |= __gmap_unmap_by_gaddr(gmap, to + off);
423 mmap_write_unlock(gmap->mm);
425 gmap_flush_tlb(gmap);
428 EXPORT_SYMBOL_GPL(gmap_unmap_segment);
431 * gmap_map_segment - map a segment to the guest address space
432 * @gmap: pointer to the guest address space structure
433 * @from: source address in the parent address space
434 * @to: target address in the guest address space
435 * @len: length of the memory area to map
437 * Returns 0 if the mmap succeeded, -EINVAL or -ENOMEM if not.
439 int gmap_map_segment(struct gmap *gmap, unsigned long from,
440 unsigned long to, unsigned long len)
445 BUG_ON(gmap_is_shadow(gmap));
446 if ((from | to | len) & (PMD_SIZE - 1))
448 if (len == 0 || from + len < from || to + len < to ||
449 from + len - 1 > TASK_SIZE_MAX || to + len - 1 > gmap->asce_end)
453 mmap_write_lock(gmap->mm);
454 for (off = 0; off < len; off += PMD_SIZE) {
455 /* Remove old translation */
456 flush |= __gmap_unmap_by_gaddr(gmap, to + off);
457 /* Store new translation */
458 if (radix_tree_insert(&gmap->guest_to_host,
459 (to + off) >> PMD_SHIFT,
460 (void *) from + off))
463 mmap_write_unlock(gmap->mm);
465 gmap_flush_tlb(gmap);
468 gmap_unmap_segment(gmap, to, len);
471 EXPORT_SYMBOL_GPL(gmap_map_segment);
474 * __gmap_translate - translate a guest address to a user space address
475 * @gmap: pointer to guest mapping meta data structure
476 * @gaddr: guest address
478 * Returns user space address which corresponds to the guest address or
479 * -EFAULT if no such mapping exists.
480 * This function does not establish potentially missing page table entries.
481 * The mmap_lock of the mm that belongs to the address space must be held
482 * when this function gets called.
484 * Note: Can also be called for shadow gmaps.
486 unsigned long __gmap_translate(struct gmap *gmap, unsigned long gaddr)
488 unsigned long vmaddr;
490 vmaddr = (unsigned long)
491 radix_tree_lookup(&gmap->guest_to_host, gaddr >> PMD_SHIFT);
492 /* Note: guest_to_host is empty for a shadow gmap */
493 return vmaddr ? (vmaddr | (gaddr & ~PMD_MASK)) : -EFAULT;
495 EXPORT_SYMBOL_GPL(__gmap_translate);
498 * gmap_translate - translate a guest address to a user space address
499 * @gmap: pointer to guest mapping meta data structure
500 * @gaddr: guest address
502 * Returns user space address which corresponds to the guest address or
503 * -EFAULT if no such mapping exists.
504 * This function does not establish potentially missing page table entries.
506 unsigned long gmap_translate(struct gmap *gmap, unsigned long gaddr)
510 mmap_read_lock(gmap->mm);
511 rc = __gmap_translate(gmap, gaddr);
512 mmap_read_unlock(gmap->mm);
515 EXPORT_SYMBOL_GPL(gmap_translate);
518 * gmap_unlink - disconnect a page table from the gmap shadow tables
519 * @mm: pointer to the parent mm_struct
520 * @table: pointer to the host page table
521 * @vmaddr: vm address associated with the host page table
523 void gmap_unlink(struct mm_struct *mm, unsigned long *table,
524 unsigned long vmaddr)
530 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
531 flush = __gmap_unlink_by_vmaddr(gmap, vmaddr);
533 gmap_flush_tlb(gmap);
538 static void gmap_pmdp_xchg(struct gmap *gmap, pmd_t *old, pmd_t new,
539 unsigned long gaddr);
542 * __gmap_link - set up shadow page tables to connect a host to a guest address
543 * @gmap: pointer to guest mapping meta data structure
544 * @gaddr: guest address
545 * @vmaddr: vm address
547 * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
548 * if the vm address is already mapped to a different guest segment.
549 * The mmap_lock of the mm that belongs to the address space must be held
550 * when this function gets called.
552 int __gmap_link(struct gmap *gmap, unsigned long gaddr, unsigned long vmaddr)
554 struct mm_struct *mm;
555 unsigned long *table;
564 BUG_ON(gmap_is_shadow(gmap));
565 /* Create higher level tables in the gmap page table */
567 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION1) {
568 table += (gaddr & _REGION1_INDEX) >> _REGION1_SHIFT;
569 if ((*table & _REGION_ENTRY_INVALID) &&
570 gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY,
571 gaddr & _REGION1_MASK))
573 table = __va(*table & _REGION_ENTRY_ORIGIN);
575 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION2) {
576 table += (gaddr & _REGION2_INDEX) >> _REGION2_SHIFT;
577 if ((*table & _REGION_ENTRY_INVALID) &&
578 gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY,
579 gaddr & _REGION2_MASK))
581 table = __va(*table & _REGION_ENTRY_ORIGIN);
583 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION3) {
584 table += (gaddr & _REGION3_INDEX) >> _REGION3_SHIFT;
585 if ((*table & _REGION_ENTRY_INVALID) &&
586 gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY,
587 gaddr & _REGION3_MASK))
589 table = __va(*table & _REGION_ENTRY_ORIGIN);
591 table += (gaddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
592 /* Walk the parent mm page table */
594 pgd = pgd_offset(mm, vmaddr);
595 VM_BUG_ON(pgd_none(*pgd));
596 p4d = p4d_offset(pgd, vmaddr);
597 VM_BUG_ON(p4d_none(*p4d));
598 pud = pud_offset(p4d, vmaddr);
599 VM_BUG_ON(pud_none(*pud));
600 /* large puds cannot yet be handled */
603 pmd = pmd_offset(pud, vmaddr);
604 VM_BUG_ON(pmd_none(*pmd));
605 /* Are we allowed to use huge pages? */
606 if (pmd_leaf(*pmd) && !gmap->mm->context.allow_gmap_hpage_1m)
608 /* Link gmap segment table entry location to page table. */
609 rc = radix_tree_preload(GFP_KERNEL_ACCOUNT);
612 ptl = pmd_lock(mm, pmd);
613 spin_lock(&gmap->guest_table_lock);
614 if (*table == _SEGMENT_ENTRY_EMPTY) {
615 rc = radix_tree_insert(&gmap->host_to_guest,
616 vmaddr >> PMD_SHIFT, table);
618 if (pmd_leaf(*pmd)) {
619 *table = (pmd_val(*pmd) &
620 _SEGMENT_ENTRY_HARDWARE_BITS_LARGE)
621 | _SEGMENT_ENTRY_GMAP_UC;
623 *table = pmd_val(*pmd) &
624 _SEGMENT_ENTRY_HARDWARE_BITS;
626 } else if (*table & _SEGMENT_ENTRY_PROTECT &&
627 !(pmd_val(*pmd) & _SEGMENT_ENTRY_PROTECT)) {
628 unprot = (u64)*table;
629 unprot &= ~_SEGMENT_ENTRY_PROTECT;
630 unprot |= _SEGMENT_ENTRY_GMAP_UC;
631 gmap_pmdp_xchg(gmap, (pmd_t *)table, __pmd(unprot), gaddr);
633 spin_unlock(&gmap->guest_table_lock);
635 radix_tree_preload_end();
640 * gmap_fault - resolve a fault on a guest address
641 * @gmap: pointer to guest mapping meta data structure
642 * @gaddr: guest address
643 * @fault_flags: flags to pass down to handle_mm_fault()
645 * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
646 * if the vm address is already mapped to a different guest segment.
648 int gmap_fault(struct gmap *gmap, unsigned long gaddr,
649 unsigned int fault_flags)
651 unsigned long vmaddr;
655 mmap_read_lock(gmap->mm);
659 vmaddr = __gmap_translate(gmap, gaddr);
660 if (IS_ERR_VALUE(vmaddr)) {
664 if (fixup_user_fault(gmap->mm, vmaddr, fault_flags,
670 * In the case that fixup_user_fault unlocked the mmap_lock during
671 * faultin redo __gmap_translate to not race with a map/unmap_segment.
676 rc = __gmap_link(gmap, gaddr, vmaddr);
678 mmap_read_unlock(gmap->mm);
681 EXPORT_SYMBOL_GPL(gmap_fault);
684 * this function is assumed to be called with mmap_lock held
686 void __gmap_zap(struct gmap *gmap, unsigned long gaddr)
688 struct vm_area_struct *vma;
689 unsigned long vmaddr;
693 /* Find the vm address for the guest address */
694 vmaddr = (unsigned long) radix_tree_lookup(&gmap->guest_to_host,
697 vmaddr |= gaddr & ~PMD_MASK;
699 vma = vma_lookup(gmap->mm, vmaddr);
700 if (!vma || is_vm_hugetlb_page(vma))
703 /* Get pointer to the page table entry */
704 ptep = get_locked_pte(gmap->mm, vmaddr, &ptl);
706 ptep_zap_unused(gmap->mm, vmaddr, ptep, 0);
707 pte_unmap_unlock(ptep, ptl);
711 EXPORT_SYMBOL_GPL(__gmap_zap);
713 void gmap_discard(struct gmap *gmap, unsigned long from, unsigned long to)
715 unsigned long gaddr, vmaddr, size;
716 struct vm_area_struct *vma;
718 mmap_read_lock(gmap->mm);
719 for (gaddr = from; gaddr < to;
720 gaddr = (gaddr + PMD_SIZE) & PMD_MASK) {
721 /* Find the vm address for the guest address */
722 vmaddr = (unsigned long)
723 radix_tree_lookup(&gmap->guest_to_host,
727 vmaddr |= gaddr & ~PMD_MASK;
728 /* Find vma in the parent mm */
729 vma = find_vma(gmap->mm, vmaddr);
733 * We do not discard pages that are backed by
734 * hugetlbfs, so we don't have to refault them.
736 if (is_vm_hugetlb_page(vma))
738 size = min(to - gaddr, PMD_SIZE - (gaddr & ~PMD_MASK));
739 zap_page_range_single(vma, vmaddr, size, NULL);
741 mmap_read_unlock(gmap->mm);
743 EXPORT_SYMBOL_GPL(gmap_discard);
745 static LIST_HEAD(gmap_notifier_list);
746 static DEFINE_SPINLOCK(gmap_notifier_lock);
749 * gmap_register_pte_notifier - register a pte invalidation callback
750 * @nb: pointer to the gmap notifier block
752 void gmap_register_pte_notifier(struct gmap_notifier *nb)
754 spin_lock(&gmap_notifier_lock);
755 list_add_rcu(&nb->list, &gmap_notifier_list);
756 spin_unlock(&gmap_notifier_lock);
758 EXPORT_SYMBOL_GPL(gmap_register_pte_notifier);
761 * gmap_unregister_pte_notifier - remove a pte invalidation callback
762 * @nb: pointer to the gmap notifier block
764 void gmap_unregister_pte_notifier(struct gmap_notifier *nb)
766 spin_lock(&gmap_notifier_lock);
767 list_del_rcu(&nb->list);
768 spin_unlock(&gmap_notifier_lock);
771 EXPORT_SYMBOL_GPL(gmap_unregister_pte_notifier);
774 * gmap_call_notifier - call all registered invalidation callbacks
775 * @gmap: pointer to guest mapping meta data structure
776 * @start: start virtual address in the guest address space
777 * @end: end virtual address in the guest address space
779 static void gmap_call_notifier(struct gmap *gmap, unsigned long start,
782 struct gmap_notifier *nb;
784 list_for_each_entry(nb, &gmap_notifier_list, list)
785 nb->notifier_call(gmap, start, end);
789 * gmap_table_walk - walk the gmap page tables
790 * @gmap: pointer to guest mapping meta data structure
791 * @gaddr: virtual address in the guest address space
792 * @level: page table level to stop at
794 * Returns a table entry pointer for the given guest address and @level
795 * @level=0 : returns a pointer to a page table table entry (or NULL)
796 * @level=1 : returns a pointer to a segment table entry (or NULL)
797 * @level=2 : returns a pointer to a region-3 table entry (or NULL)
798 * @level=3 : returns a pointer to a region-2 table entry (or NULL)
799 * @level=4 : returns a pointer to a region-1 table entry (or NULL)
801 * Returns NULL if the gmap page tables could not be walked to the
804 * Note: Can also be called for shadow gmaps.
806 static inline unsigned long *gmap_table_walk(struct gmap *gmap,
807 unsigned long gaddr, int level)
809 const int asce_type = gmap->asce & _ASCE_TYPE_MASK;
810 unsigned long *table = gmap->table;
812 if (gmap_is_shadow(gmap) && gmap->removed)
815 if (WARN_ON_ONCE(level > (asce_type >> 2) + 1))
818 if (asce_type != _ASCE_TYPE_REGION1 &&
819 gaddr & (-1UL << (31 + (asce_type >> 2) * 11)))
823 case _ASCE_TYPE_REGION1:
824 table += (gaddr & _REGION1_INDEX) >> _REGION1_SHIFT;
827 if (*table & _REGION_ENTRY_INVALID)
829 table = __va(*table & _REGION_ENTRY_ORIGIN);
831 case _ASCE_TYPE_REGION2:
832 table += (gaddr & _REGION2_INDEX) >> _REGION2_SHIFT;
835 if (*table & _REGION_ENTRY_INVALID)
837 table = __va(*table & _REGION_ENTRY_ORIGIN);
839 case _ASCE_TYPE_REGION3:
840 table += (gaddr & _REGION3_INDEX) >> _REGION3_SHIFT;
843 if (*table & _REGION_ENTRY_INVALID)
845 table = __va(*table & _REGION_ENTRY_ORIGIN);
847 case _ASCE_TYPE_SEGMENT:
848 table += (gaddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
851 if (*table & _REGION_ENTRY_INVALID)
853 table = __va(*table & _SEGMENT_ENTRY_ORIGIN);
854 table += (gaddr & _PAGE_INDEX) >> _PAGE_SHIFT;
860 * gmap_pte_op_walk - walk the gmap page table, get the page table lock
861 * and return the pte pointer
862 * @gmap: pointer to guest mapping meta data structure
863 * @gaddr: virtual address in the guest address space
864 * @ptl: pointer to the spinlock pointer
866 * Returns a pointer to the locked pte for a guest address, or NULL
868 static pte_t *gmap_pte_op_walk(struct gmap *gmap, unsigned long gaddr,
871 unsigned long *table;
873 BUG_ON(gmap_is_shadow(gmap));
874 /* Walk the gmap page table, lock and get pte pointer */
875 table = gmap_table_walk(gmap, gaddr, 1); /* get segment pointer */
876 if (!table || *table & _SEGMENT_ENTRY_INVALID)
878 return pte_alloc_map_lock(gmap->mm, (pmd_t *) table, gaddr, ptl);
882 * gmap_pte_op_fixup - force a page in and connect the gmap page table
883 * @gmap: pointer to guest mapping meta data structure
884 * @gaddr: virtual address in the guest address space
885 * @vmaddr: address in the host process address space
886 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
888 * Returns 0 if the caller can retry __gmap_translate (might fail again),
889 * -ENOMEM if out of memory and -EFAULT if anything goes wrong while fixing
890 * up or connecting the gmap page table.
892 static int gmap_pte_op_fixup(struct gmap *gmap, unsigned long gaddr,
893 unsigned long vmaddr, int prot)
895 struct mm_struct *mm = gmap->mm;
896 unsigned int fault_flags;
897 bool unlocked = false;
899 BUG_ON(gmap_is_shadow(gmap));
900 fault_flags = (prot == PROT_WRITE) ? FAULT_FLAG_WRITE : 0;
901 if (fixup_user_fault(mm, vmaddr, fault_flags, &unlocked))
904 /* lost mmap_lock, caller has to retry __gmap_translate */
906 /* Connect the page tables */
907 return __gmap_link(gmap, gaddr, vmaddr);
911 * gmap_pte_op_end - release the page table lock
912 * @ptep: pointer to the locked pte
913 * @ptl: pointer to the page table spinlock
915 static void gmap_pte_op_end(pte_t *ptep, spinlock_t *ptl)
917 pte_unmap_unlock(ptep, ptl);
921 * gmap_pmd_op_walk - walk the gmap tables, get the guest table lock
922 * and return the pmd pointer
923 * @gmap: pointer to guest mapping meta data structure
924 * @gaddr: virtual address in the guest address space
926 * Returns a pointer to the pmd for a guest address, or NULL
928 static inline pmd_t *gmap_pmd_op_walk(struct gmap *gmap, unsigned long gaddr)
932 BUG_ON(gmap_is_shadow(gmap));
933 pmdp = (pmd_t *) gmap_table_walk(gmap, gaddr, 1);
937 /* without huge pages, there is no need to take the table lock */
938 if (!gmap->mm->context.allow_gmap_hpage_1m)
939 return pmd_none(*pmdp) ? NULL : pmdp;
941 spin_lock(&gmap->guest_table_lock);
942 if (pmd_none(*pmdp)) {
943 spin_unlock(&gmap->guest_table_lock);
947 /* 4k page table entries are locked via the pte (pte_alloc_map_lock). */
948 if (!pmd_leaf(*pmdp))
949 spin_unlock(&gmap->guest_table_lock);
954 * gmap_pmd_op_end - release the guest_table_lock if needed
955 * @gmap: pointer to the guest mapping meta data structure
956 * @pmdp: pointer to the pmd
958 static inline void gmap_pmd_op_end(struct gmap *gmap, pmd_t *pmdp)
961 spin_unlock(&gmap->guest_table_lock);
965 * gmap_protect_pmd - remove access rights to memory and set pmd notification bits
966 * @pmdp: pointer to the pmd to be protected
967 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
968 * @bits: notification bits to set
971 * 0 if successfully protected
972 * -EAGAIN if a fixup is needed
973 * -EINVAL if unsupported notifier bits have been specified
975 * Expected to be called with sg->mm->mmap_lock in read and
976 * guest_table_lock held.
978 static int gmap_protect_pmd(struct gmap *gmap, unsigned long gaddr,
979 pmd_t *pmdp, int prot, unsigned long bits)
981 int pmd_i = pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID;
982 int pmd_p = pmd_val(*pmdp) & _SEGMENT_ENTRY_PROTECT;
986 if ((pmd_i && (prot != PROT_NONE)) || (pmd_p && (prot == PROT_WRITE)))
989 if (prot == PROT_NONE && !pmd_i) {
990 new = set_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_INVALID));
991 gmap_pmdp_xchg(gmap, pmdp, new, gaddr);
994 if (prot == PROT_READ && !pmd_p) {
995 new = clear_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_INVALID));
996 new = set_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_PROTECT));
997 gmap_pmdp_xchg(gmap, pmdp, new, gaddr);
1000 if (bits & GMAP_NOTIFY_MPROT)
1001 set_pmd(pmdp, set_pmd_bit(*pmdp, __pgprot(_SEGMENT_ENTRY_GMAP_IN)));
1003 /* Shadow GMAP protection needs split PMDs */
1004 if (bits & GMAP_NOTIFY_SHADOW)
1011 * gmap_protect_pte - remove access rights to memory and set pgste bits
1012 * @gmap: pointer to guest mapping meta data structure
1013 * @gaddr: virtual address in the guest address space
1014 * @pmdp: pointer to the pmd associated with the pte
1015 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
1016 * @bits: notification bits to set
1018 * Returns 0 if successfully protected, -ENOMEM if out of memory and
1019 * -EAGAIN if a fixup is needed.
1021 * Expected to be called with sg->mm->mmap_lock in read
1023 static int gmap_protect_pte(struct gmap *gmap, unsigned long gaddr,
1024 pmd_t *pmdp, int prot, unsigned long bits)
1029 unsigned long pbits = 0;
1031 if (pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID)
1034 ptep = pte_alloc_map_lock(gmap->mm, pmdp, gaddr, &ptl);
1038 pbits |= (bits & GMAP_NOTIFY_MPROT) ? PGSTE_IN_BIT : 0;
1039 pbits |= (bits & GMAP_NOTIFY_SHADOW) ? PGSTE_VSIE_BIT : 0;
1040 /* Protect and unlock. */
1041 rc = ptep_force_prot(gmap->mm, gaddr, ptep, prot, pbits);
1042 gmap_pte_op_end(ptep, ptl);
1047 * gmap_protect_range - remove access rights to memory and set pgste bits
1048 * @gmap: pointer to guest mapping meta data structure
1049 * @gaddr: virtual address in the guest address space
1050 * @len: size of area
1051 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
1052 * @bits: pgste notification bits to set
1054 * Returns 0 if successfully protected, -ENOMEM if out of memory and
1055 * -EFAULT if gaddr is invalid (or mapping for shadows is missing).
1057 * Called with sg->mm->mmap_lock in read.
1059 static int gmap_protect_range(struct gmap *gmap, unsigned long gaddr,
1060 unsigned long len, int prot, unsigned long bits)
1062 unsigned long vmaddr, dist;
1066 BUG_ON(gmap_is_shadow(gmap));
1069 pmdp = gmap_pmd_op_walk(gmap, gaddr);
1071 if (!pmd_leaf(*pmdp)) {
1072 rc = gmap_protect_pte(gmap, gaddr, pmdp, prot,
1079 rc = gmap_protect_pmd(gmap, gaddr, pmdp, prot,
1082 dist = HPAGE_SIZE - (gaddr & ~HPAGE_MASK);
1083 len = len < dist ? 0 : len - dist;
1084 gaddr = (gaddr & HPAGE_MASK) + HPAGE_SIZE;
1087 gmap_pmd_op_end(gmap, pmdp);
1093 /* -EAGAIN, fixup of userspace mm and gmap */
1094 vmaddr = __gmap_translate(gmap, gaddr);
1095 if (IS_ERR_VALUE(vmaddr))
1097 rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr, prot);
1106 * gmap_mprotect_notify - change access rights for a range of ptes and
1107 * call the notifier if any pte changes again
1108 * @gmap: pointer to guest mapping meta data structure
1109 * @gaddr: virtual address in the guest address space
1110 * @len: size of area
1111 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
1113 * Returns 0 if for each page in the given range a gmap mapping exists,
1114 * the new access rights could be set and the notifier could be armed.
1115 * If the gmap mapping is missing for one or more pages -EFAULT is
1116 * returned. If no memory could be allocated -ENOMEM is returned.
1117 * This function establishes missing page table entries.
1119 int gmap_mprotect_notify(struct gmap *gmap, unsigned long gaddr,
1120 unsigned long len, int prot)
1124 if ((gaddr & ~PAGE_MASK) || (len & ~PAGE_MASK) || gmap_is_shadow(gmap))
1126 if (!MACHINE_HAS_ESOP && prot == PROT_READ)
1128 mmap_read_lock(gmap->mm);
1129 rc = gmap_protect_range(gmap, gaddr, len, prot, GMAP_NOTIFY_MPROT);
1130 mmap_read_unlock(gmap->mm);
1133 EXPORT_SYMBOL_GPL(gmap_mprotect_notify);
1136 * gmap_read_table - get an unsigned long value from a guest page table using
1137 * absolute addressing, without marking the page referenced.
1138 * @gmap: pointer to guest mapping meta data structure
1139 * @gaddr: virtual address in the guest address space
1140 * @val: pointer to the unsigned long value to return
1142 * Returns 0 if the value was read, -ENOMEM if out of memory and -EFAULT
1143 * if reading using the virtual address failed. -EINVAL if called on a gmap
1146 * Called with gmap->mm->mmap_lock in read.
1148 int gmap_read_table(struct gmap *gmap, unsigned long gaddr, unsigned long *val)
1150 unsigned long address, vmaddr;
1155 if (gmap_is_shadow(gmap))
1160 ptep = gmap_pte_op_walk(gmap, gaddr, &ptl);
1163 if (pte_present(pte) && (pte_val(pte) & _PAGE_READ)) {
1164 address = pte_val(pte) & PAGE_MASK;
1165 address += gaddr & ~PAGE_MASK;
1166 *val = *(unsigned long *)__va(address);
1167 set_pte(ptep, set_pte_bit(*ptep, __pgprot(_PAGE_YOUNG)));
1168 /* Do *NOT* clear the _PAGE_INVALID bit! */
1171 gmap_pte_op_end(ptep, ptl);
1175 vmaddr = __gmap_translate(gmap, gaddr);
1176 if (IS_ERR_VALUE(vmaddr)) {
1180 rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr, PROT_READ);
1186 EXPORT_SYMBOL_GPL(gmap_read_table);
1189 * gmap_insert_rmap - add a rmap to the host_to_rmap radix tree
1190 * @sg: pointer to the shadow guest address space structure
1191 * @vmaddr: vm address associated with the rmap
1192 * @rmap: pointer to the rmap structure
1194 * Called with the sg->guest_table_lock
1196 static inline void gmap_insert_rmap(struct gmap *sg, unsigned long vmaddr,
1197 struct gmap_rmap *rmap)
1199 struct gmap_rmap *temp;
1202 BUG_ON(!gmap_is_shadow(sg));
1203 slot = radix_tree_lookup_slot(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT);
1205 rmap->next = radix_tree_deref_slot_protected(slot,
1206 &sg->guest_table_lock);
1207 for (temp = rmap->next; temp; temp = temp->next) {
1208 if (temp->raddr == rmap->raddr) {
1213 radix_tree_replace_slot(&sg->host_to_rmap, slot, rmap);
1216 radix_tree_insert(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT,
1222 * gmap_protect_rmap - restrict access rights to memory (RO) and create an rmap
1223 * @sg: pointer to the shadow guest address space structure
1224 * @raddr: rmap address in the shadow gmap
1225 * @paddr: address in the parent guest address space
1226 * @len: length of the memory area to protect
1228 * Returns 0 if successfully protected and the rmap was created, -ENOMEM
1229 * if out of memory and -EFAULT if paddr is invalid.
1231 static int gmap_protect_rmap(struct gmap *sg, unsigned long raddr,
1232 unsigned long paddr, unsigned long len)
1234 struct gmap *parent;
1235 struct gmap_rmap *rmap;
1236 unsigned long vmaddr;
1241 BUG_ON(!gmap_is_shadow(sg));
1242 parent = sg->parent;
1244 vmaddr = __gmap_translate(parent, paddr);
1245 if (IS_ERR_VALUE(vmaddr))
1247 rmap = kzalloc(sizeof(*rmap), GFP_KERNEL_ACCOUNT);
1250 rmap->raddr = raddr;
1251 rc = radix_tree_preload(GFP_KERNEL_ACCOUNT);
1257 ptep = gmap_pte_op_walk(parent, paddr, &ptl);
1259 spin_lock(&sg->guest_table_lock);
1260 rc = ptep_force_prot(parent->mm, paddr, ptep, PROT_READ,
1263 gmap_insert_rmap(sg, vmaddr, rmap);
1264 spin_unlock(&sg->guest_table_lock);
1265 gmap_pte_op_end(ptep, ptl);
1267 radix_tree_preload_end();
1270 rc = gmap_pte_op_fixup(parent, paddr, vmaddr, PROT_READ);
1281 #define _SHADOW_RMAP_MASK 0x7
1282 #define _SHADOW_RMAP_REGION1 0x5
1283 #define _SHADOW_RMAP_REGION2 0x4
1284 #define _SHADOW_RMAP_REGION3 0x3
1285 #define _SHADOW_RMAP_SEGMENT 0x2
1286 #define _SHADOW_RMAP_PGTABLE 0x1
1289 * gmap_idte_one - invalidate a single region or segment table entry
1290 * @asce: region or segment table *origin* + table-type bits
1291 * @vaddr: virtual address to identify the table entry to flush
1293 * The invalid bit of a single region or segment table entry is set
1294 * and the associated TLB entries depending on the entry are flushed.
1295 * The table-type of the @asce identifies the portion of the @vaddr
1296 * that is used as the invalidation index.
1298 static inline void gmap_idte_one(unsigned long asce, unsigned long vaddr)
1302 : : "a" (asce), "a" (vaddr) : "cc", "memory");
1306 * gmap_unshadow_page - remove a page from a shadow page table
1307 * @sg: pointer to the shadow guest address space structure
1308 * @raddr: rmap address in the shadow guest address space
1310 * Called with the sg->guest_table_lock
1312 static void gmap_unshadow_page(struct gmap *sg, unsigned long raddr)
1314 unsigned long *table;
1316 BUG_ON(!gmap_is_shadow(sg));
1317 table = gmap_table_walk(sg, raddr, 0); /* get page table pointer */
1318 if (!table || *table & _PAGE_INVALID)
1320 gmap_call_notifier(sg, raddr, raddr + _PAGE_SIZE - 1);
1321 ptep_unshadow_pte(sg->mm, raddr, (pte_t *) table);
1325 * __gmap_unshadow_pgt - remove all entries from a shadow page table
1326 * @sg: pointer to the shadow guest address space structure
1327 * @raddr: rmap address in the shadow guest address space
1328 * @pgt: pointer to the start of a shadow page table
1330 * Called with the sg->guest_table_lock
1332 static void __gmap_unshadow_pgt(struct gmap *sg, unsigned long raddr,
1337 BUG_ON(!gmap_is_shadow(sg));
1338 for (i = 0; i < _PAGE_ENTRIES; i++, raddr += _PAGE_SIZE)
1339 pgt[i] = _PAGE_INVALID;
1343 * gmap_unshadow_pgt - remove a shadow page table from a segment entry
1344 * @sg: pointer to the shadow guest address space structure
1345 * @raddr: address in the shadow guest address space
1347 * Called with the sg->guest_table_lock
1349 static void gmap_unshadow_pgt(struct gmap *sg, unsigned long raddr)
1352 phys_addr_t sto, pgt;
1353 struct ptdesc *ptdesc;
1355 BUG_ON(!gmap_is_shadow(sg));
1356 ste = gmap_table_walk(sg, raddr, 1); /* get segment pointer */
1357 if (!ste || !(*ste & _SEGMENT_ENTRY_ORIGIN))
1359 gmap_call_notifier(sg, raddr, raddr + _SEGMENT_SIZE - 1);
1360 sto = __pa(ste - ((raddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT));
1361 gmap_idte_one(sto | _ASCE_TYPE_SEGMENT, raddr);
1362 pgt = *ste & _SEGMENT_ENTRY_ORIGIN;
1363 *ste = _SEGMENT_ENTRY_EMPTY;
1364 __gmap_unshadow_pgt(sg, raddr, __va(pgt));
1365 /* Free page table */
1366 ptdesc = page_ptdesc(phys_to_page(pgt));
1367 list_del(&ptdesc->pt_list);
1368 page_table_free_pgste(ptdesc);
1372 * __gmap_unshadow_sgt - remove all entries from a shadow segment table
1373 * @sg: pointer to the shadow guest address space structure
1374 * @raddr: rmap address in the shadow guest address space
1375 * @sgt: pointer to the start of a shadow segment table
1377 * Called with the sg->guest_table_lock
1379 static void __gmap_unshadow_sgt(struct gmap *sg, unsigned long raddr,
1382 struct ptdesc *ptdesc;
1386 BUG_ON(!gmap_is_shadow(sg));
1387 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _SEGMENT_SIZE) {
1388 if (!(sgt[i] & _SEGMENT_ENTRY_ORIGIN))
1390 pgt = sgt[i] & _REGION_ENTRY_ORIGIN;
1391 sgt[i] = _SEGMENT_ENTRY_EMPTY;
1392 __gmap_unshadow_pgt(sg, raddr, __va(pgt));
1393 /* Free page table */
1394 ptdesc = page_ptdesc(phys_to_page(pgt));
1395 list_del(&ptdesc->pt_list);
1396 page_table_free_pgste(ptdesc);
1401 * gmap_unshadow_sgt - remove a shadow segment table from a region-3 entry
1402 * @sg: pointer to the shadow guest address space structure
1403 * @raddr: rmap address in the shadow guest address space
1405 * Called with the shadow->guest_table_lock
1407 static void gmap_unshadow_sgt(struct gmap *sg, unsigned long raddr)
1409 unsigned long r3o, *r3e;
1413 BUG_ON(!gmap_is_shadow(sg));
1414 r3e = gmap_table_walk(sg, raddr, 2); /* get region-3 pointer */
1415 if (!r3e || !(*r3e & _REGION_ENTRY_ORIGIN))
1417 gmap_call_notifier(sg, raddr, raddr + _REGION3_SIZE - 1);
1418 r3o = (unsigned long) (r3e - ((raddr & _REGION3_INDEX) >> _REGION3_SHIFT));
1419 gmap_idte_one(__pa(r3o) | _ASCE_TYPE_REGION3, raddr);
1420 sgt = *r3e & _REGION_ENTRY_ORIGIN;
1421 *r3e = _REGION3_ENTRY_EMPTY;
1422 __gmap_unshadow_sgt(sg, raddr, __va(sgt));
1423 /* Free segment table */
1424 page = phys_to_page(sgt);
1425 list_del(&page->lru);
1426 __free_pages(page, CRST_ALLOC_ORDER);
1430 * __gmap_unshadow_r3t - remove all entries from a shadow region-3 table
1431 * @sg: pointer to the shadow guest address space structure
1432 * @raddr: address in the shadow guest address space
1433 * @r3t: pointer to the start of a shadow region-3 table
1435 * Called with the sg->guest_table_lock
1437 static void __gmap_unshadow_r3t(struct gmap *sg, unsigned long raddr,
1444 BUG_ON(!gmap_is_shadow(sg));
1445 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION3_SIZE) {
1446 if (!(r3t[i] & _REGION_ENTRY_ORIGIN))
1448 sgt = r3t[i] & _REGION_ENTRY_ORIGIN;
1449 r3t[i] = _REGION3_ENTRY_EMPTY;
1450 __gmap_unshadow_sgt(sg, raddr, __va(sgt));
1451 /* Free segment table */
1452 page = phys_to_page(sgt);
1453 list_del(&page->lru);
1454 __free_pages(page, CRST_ALLOC_ORDER);
1459 * gmap_unshadow_r3t - remove a shadow region-3 table from a region-2 entry
1460 * @sg: pointer to the shadow guest address space structure
1461 * @raddr: rmap address in the shadow guest address space
1463 * Called with the sg->guest_table_lock
1465 static void gmap_unshadow_r3t(struct gmap *sg, unsigned long raddr)
1467 unsigned long r2o, *r2e;
1471 BUG_ON(!gmap_is_shadow(sg));
1472 r2e = gmap_table_walk(sg, raddr, 3); /* get region-2 pointer */
1473 if (!r2e || !(*r2e & _REGION_ENTRY_ORIGIN))
1475 gmap_call_notifier(sg, raddr, raddr + _REGION2_SIZE - 1);
1476 r2o = (unsigned long) (r2e - ((raddr & _REGION2_INDEX) >> _REGION2_SHIFT));
1477 gmap_idte_one(__pa(r2o) | _ASCE_TYPE_REGION2, raddr);
1478 r3t = *r2e & _REGION_ENTRY_ORIGIN;
1479 *r2e = _REGION2_ENTRY_EMPTY;
1480 __gmap_unshadow_r3t(sg, raddr, __va(r3t));
1481 /* Free region 3 table */
1482 page = phys_to_page(r3t);
1483 list_del(&page->lru);
1484 __free_pages(page, CRST_ALLOC_ORDER);
1488 * __gmap_unshadow_r2t - remove all entries from a shadow region-2 table
1489 * @sg: pointer to the shadow guest address space structure
1490 * @raddr: rmap address in the shadow guest address space
1491 * @r2t: pointer to the start of a shadow region-2 table
1493 * Called with the sg->guest_table_lock
1495 static void __gmap_unshadow_r2t(struct gmap *sg, unsigned long raddr,
1502 BUG_ON(!gmap_is_shadow(sg));
1503 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION2_SIZE) {
1504 if (!(r2t[i] & _REGION_ENTRY_ORIGIN))
1506 r3t = r2t[i] & _REGION_ENTRY_ORIGIN;
1507 r2t[i] = _REGION2_ENTRY_EMPTY;
1508 __gmap_unshadow_r3t(sg, raddr, __va(r3t));
1509 /* Free region 3 table */
1510 page = phys_to_page(r3t);
1511 list_del(&page->lru);
1512 __free_pages(page, CRST_ALLOC_ORDER);
1517 * gmap_unshadow_r2t - remove a shadow region-2 table from a region-1 entry
1518 * @sg: pointer to the shadow guest address space structure
1519 * @raddr: rmap address in the shadow guest address space
1521 * Called with the sg->guest_table_lock
1523 static void gmap_unshadow_r2t(struct gmap *sg, unsigned long raddr)
1525 unsigned long r1o, *r1e;
1529 BUG_ON(!gmap_is_shadow(sg));
1530 r1e = gmap_table_walk(sg, raddr, 4); /* get region-1 pointer */
1531 if (!r1e || !(*r1e & _REGION_ENTRY_ORIGIN))
1533 gmap_call_notifier(sg, raddr, raddr + _REGION1_SIZE - 1);
1534 r1o = (unsigned long) (r1e - ((raddr & _REGION1_INDEX) >> _REGION1_SHIFT));
1535 gmap_idte_one(__pa(r1o) | _ASCE_TYPE_REGION1, raddr);
1536 r2t = *r1e & _REGION_ENTRY_ORIGIN;
1537 *r1e = _REGION1_ENTRY_EMPTY;
1538 __gmap_unshadow_r2t(sg, raddr, __va(r2t));
1539 /* Free region 2 table */
1540 page = phys_to_page(r2t);
1541 list_del(&page->lru);
1542 __free_pages(page, CRST_ALLOC_ORDER);
1546 * __gmap_unshadow_r1t - remove all entries from a shadow region-1 table
1547 * @sg: pointer to the shadow guest address space structure
1548 * @raddr: rmap address in the shadow guest address space
1549 * @r1t: pointer to the start of a shadow region-1 table
1551 * Called with the shadow->guest_table_lock
1553 static void __gmap_unshadow_r1t(struct gmap *sg, unsigned long raddr,
1561 BUG_ON(!gmap_is_shadow(sg));
1562 asce = __pa(r1t) | _ASCE_TYPE_REGION1;
1563 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION1_SIZE) {
1564 if (!(r1t[i] & _REGION_ENTRY_ORIGIN))
1566 r2t = r1t[i] & _REGION_ENTRY_ORIGIN;
1567 __gmap_unshadow_r2t(sg, raddr, __va(r2t));
1568 /* Clear entry and flush translation r1t -> r2t */
1569 gmap_idte_one(asce, raddr);
1570 r1t[i] = _REGION1_ENTRY_EMPTY;
1571 /* Free region 2 table */
1572 page = phys_to_page(r2t);
1573 list_del(&page->lru);
1574 __free_pages(page, CRST_ALLOC_ORDER);
1579 * gmap_unshadow - remove a shadow page table completely
1580 * @sg: pointer to the shadow guest address space structure
1582 * Called with sg->guest_table_lock
1584 static void gmap_unshadow(struct gmap *sg)
1586 unsigned long *table;
1588 BUG_ON(!gmap_is_shadow(sg));
1592 gmap_call_notifier(sg, 0, -1UL);
1594 table = __va(sg->asce & _ASCE_ORIGIN);
1595 switch (sg->asce & _ASCE_TYPE_MASK) {
1596 case _ASCE_TYPE_REGION1:
1597 __gmap_unshadow_r1t(sg, 0, table);
1599 case _ASCE_TYPE_REGION2:
1600 __gmap_unshadow_r2t(sg, 0, table);
1602 case _ASCE_TYPE_REGION3:
1603 __gmap_unshadow_r3t(sg, 0, table);
1605 case _ASCE_TYPE_SEGMENT:
1606 __gmap_unshadow_sgt(sg, 0, table);
1612 * gmap_find_shadow - find a specific asce in the list of shadow tables
1613 * @parent: pointer to the parent gmap
1614 * @asce: ASCE for which the shadow table is created
1615 * @edat_level: edat level to be used for the shadow translation
1617 * Returns the pointer to a gmap if a shadow table with the given asce is
1618 * already available, ERR_PTR(-EAGAIN) if another one is just being created,
1621 static struct gmap *gmap_find_shadow(struct gmap *parent, unsigned long asce,
1626 list_for_each_entry(sg, &parent->children, list) {
1627 if (sg->orig_asce != asce || sg->edat_level != edat_level ||
1630 if (!sg->initialized)
1631 return ERR_PTR(-EAGAIN);
1632 refcount_inc(&sg->ref_count);
1639 * gmap_shadow_valid - check if a shadow guest address space matches the
1640 * given properties and is still valid
1641 * @sg: pointer to the shadow guest address space structure
1642 * @asce: ASCE for which the shadow table is requested
1643 * @edat_level: edat level to be used for the shadow translation
1645 * Returns 1 if the gmap shadow is still valid and matches the given
1646 * properties, the caller can continue using it. Returns 0 otherwise, the
1647 * caller has to request a new shadow gmap in this case.
1650 int gmap_shadow_valid(struct gmap *sg, unsigned long asce, int edat_level)
1654 return sg->orig_asce == asce && sg->edat_level == edat_level;
1656 EXPORT_SYMBOL_GPL(gmap_shadow_valid);
1659 * gmap_shadow - create/find a shadow guest address space
1660 * @parent: pointer to the parent gmap
1661 * @asce: ASCE for which the shadow table is created
1662 * @edat_level: edat level to be used for the shadow translation
1664 * The pages of the top level page table referred by the asce parameter
1665 * will be set to read-only and marked in the PGSTEs of the kvm process.
1666 * The shadow table will be removed automatically on any change to the
1667 * PTE mapping for the source table.
1669 * Returns a guest address space structure, ERR_PTR(-ENOMEM) if out of memory,
1670 * ERR_PTR(-EAGAIN) if the caller has to retry and ERR_PTR(-EFAULT) if the
1671 * parent gmap table could not be protected.
1673 struct gmap *gmap_shadow(struct gmap *parent, unsigned long asce,
1676 struct gmap *sg, *new;
1677 unsigned long limit;
1680 BUG_ON(parent->mm->context.allow_gmap_hpage_1m);
1681 BUG_ON(gmap_is_shadow(parent));
1682 spin_lock(&parent->shadow_lock);
1683 sg = gmap_find_shadow(parent, asce, edat_level);
1684 spin_unlock(&parent->shadow_lock);
1687 /* Create a new shadow gmap */
1688 limit = -1UL >> (33 - (((asce & _ASCE_TYPE_MASK) >> 2) * 11));
1689 if (asce & _ASCE_REAL_SPACE)
1691 new = gmap_alloc(limit);
1693 return ERR_PTR(-ENOMEM);
1694 new->mm = parent->mm;
1695 new->parent = gmap_get(parent);
1696 new->private = parent->private;
1697 new->orig_asce = asce;
1698 new->edat_level = edat_level;
1699 new->initialized = false;
1700 spin_lock(&parent->shadow_lock);
1701 /* Recheck if another CPU created the same shadow */
1702 sg = gmap_find_shadow(parent, asce, edat_level);
1704 spin_unlock(&parent->shadow_lock);
1708 if (asce & _ASCE_REAL_SPACE) {
1709 /* only allow one real-space gmap shadow */
1710 list_for_each_entry(sg, &parent->children, list) {
1711 if (sg->orig_asce & _ASCE_REAL_SPACE) {
1712 spin_lock(&sg->guest_table_lock);
1714 spin_unlock(&sg->guest_table_lock);
1715 list_del(&sg->list);
1721 refcount_set(&new->ref_count, 2);
1722 list_add(&new->list, &parent->children);
1723 if (asce & _ASCE_REAL_SPACE) {
1724 /* nothing to protect, return right away */
1725 new->initialized = true;
1726 spin_unlock(&parent->shadow_lock);
1729 spin_unlock(&parent->shadow_lock);
1730 /* protect after insertion, so it will get properly invalidated */
1731 mmap_read_lock(parent->mm);
1732 rc = gmap_protect_range(parent, asce & _ASCE_ORIGIN,
1733 ((asce & _ASCE_TABLE_LENGTH) + 1) * PAGE_SIZE,
1734 PROT_READ, GMAP_NOTIFY_SHADOW);
1735 mmap_read_unlock(parent->mm);
1736 spin_lock(&parent->shadow_lock);
1737 new->initialized = true;
1739 list_del(&new->list);
1743 spin_unlock(&parent->shadow_lock);
1746 EXPORT_SYMBOL_GPL(gmap_shadow);
1749 * gmap_shadow_r2t - create an empty shadow region 2 table
1750 * @sg: pointer to the shadow guest address space structure
1751 * @saddr: faulting address in the shadow gmap
1752 * @r2t: parent gmap address of the region 2 table to get shadowed
1753 * @fake: r2t references contiguous guest memory block, not a r2t
1755 * The r2t parameter specifies the address of the source table. The
1756 * four pages of the source table are made read-only in the parent gmap
1757 * address space. A write to the source table area @r2t will automatically
1758 * remove the shadow r2 table and all of its descendants.
1760 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
1761 * shadow table structure is incomplete, -ENOMEM if out of memory and
1762 * -EFAULT if an address in the parent gmap could not be resolved.
1764 * Called with sg->mm->mmap_lock in read.
1766 int gmap_shadow_r2t(struct gmap *sg, unsigned long saddr, unsigned long r2t,
1769 unsigned long raddr, origin, offset, len;
1770 unsigned long *table;
1775 BUG_ON(!gmap_is_shadow(sg));
1776 /* Allocate a shadow region second table */
1777 page = gmap_alloc_crst();
1780 page->index = r2t & _REGION_ENTRY_ORIGIN;
1782 page->index |= GMAP_SHADOW_FAKE_TABLE;
1783 s_r2t = page_to_phys(page);
1784 /* Install shadow region second table */
1785 spin_lock(&sg->guest_table_lock);
1786 table = gmap_table_walk(sg, saddr, 4); /* get region-1 pointer */
1788 rc = -EAGAIN; /* Race with unshadow */
1791 if (!(*table & _REGION_ENTRY_INVALID)) {
1792 rc = 0; /* Already established */
1794 } else if (*table & _REGION_ENTRY_ORIGIN) {
1795 rc = -EAGAIN; /* Race with shadow */
1798 crst_table_init(__va(s_r2t), _REGION2_ENTRY_EMPTY);
1799 /* mark as invalid as long as the parent table is not protected */
1800 *table = s_r2t | _REGION_ENTRY_LENGTH |
1801 _REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_INVALID;
1802 if (sg->edat_level >= 1)
1803 *table |= (r2t & _REGION_ENTRY_PROTECT);
1804 list_add(&page->lru, &sg->crst_list);
1806 /* nothing to protect for fake tables */
1807 *table &= ~_REGION_ENTRY_INVALID;
1808 spin_unlock(&sg->guest_table_lock);
1811 spin_unlock(&sg->guest_table_lock);
1812 /* Make r2t read-only in parent gmap page table */
1813 raddr = (saddr & _REGION1_MASK) | _SHADOW_RMAP_REGION1;
1814 origin = r2t & _REGION_ENTRY_ORIGIN;
1815 offset = ((r2t & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
1816 len = ((r2t & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
1817 rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
1818 spin_lock(&sg->guest_table_lock);
1820 table = gmap_table_walk(sg, saddr, 4);
1821 if (!table || (*table & _REGION_ENTRY_ORIGIN) != s_r2t)
1822 rc = -EAGAIN; /* Race with unshadow */
1824 *table &= ~_REGION_ENTRY_INVALID;
1826 gmap_unshadow_r2t(sg, raddr);
1828 spin_unlock(&sg->guest_table_lock);
1831 spin_unlock(&sg->guest_table_lock);
1832 __free_pages(page, CRST_ALLOC_ORDER);
1835 EXPORT_SYMBOL_GPL(gmap_shadow_r2t);
1838 * gmap_shadow_r3t - create a shadow region 3 table
1839 * @sg: pointer to the shadow guest address space structure
1840 * @saddr: faulting address in the shadow gmap
1841 * @r3t: parent gmap address of the region 3 table to get shadowed
1842 * @fake: r3t references contiguous guest memory block, not a r3t
1844 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
1845 * shadow table structure is incomplete, -ENOMEM if out of memory and
1846 * -EFAULT if an address in the parent gmap could not be resolved.
1848 * Called with sg->mm->mmap_lock in read.
1850 int gmap_shadow_r3t(struct gmap *sg, unsigned long saddr, unsigned long r3t,
1853 unsigned long raddr, origin, offset, len;
1854 unsigned long *table;
1859 BUG_ON(!gmap_is_shadow(sg));
1860 /* Allocate a shadow region second table */
1861 page = gmap_alloc_crst();
1864 page->index = r3t & _REGION_ENTRY_ORIGIN;
1866 page->index |= GMAP_SHADOW_FAKE_TABLE;
1867 s_r3t = page_to_phys(page);
1868 /* Install shadow region second table */
1869 spin_lock(&sg->guest_table_lock);
1870 table = gmap_table_walk(sg, saddr, 3); /* get region-2 pointer */
1872 rc = -EAGAIN; /* Race with unshadow */
1875 if (!(*table & _REGION_ENTRY_INVALID)) {
1876 rc = 0; /* Already established */
1878 } else if (*table & _REGION_ENTRY_ORIGIN) {
1879 rc = -EAGAIN; /* Race with shadow */
1882 crst_table_init(__va(s_r3t), _REGION3_ENTRY_EMPTY);
1883 /* mark as invalid as long as the parent table is not protected */
1884 *table = s_r3t | _REGION_ENTRY_LENGTH |
1885 _REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_INVALID;
1886 if (sg->edat_level >= 1)
1887 *table |= (r3t & _REGION_ENTRY_PROTECT);
1888 list_add(&page->lru, &sg->crst_list);
1890 /* nothing to protect for fake tables */
1891 *table &= ~_REGION_ENTRY_INVALID;
1892 spin_unlock(&sg->guest_table_lock);
1895 spin_unlock(&sg->guest_table_lock);
1896 /* Make r3t read-only in parent gmap page table */
1897 raddr = (saddr & _REGION2_MASK) | _SHADOW_RMAP_REGION2;
1898 origin = r3t & _REGION_ENTRY_ORIGIN;
1899 offset = ((r3t & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
1900 len = ((r3t & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
1901 rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
1902 spin_lock(&sg->guest_table_lock);
1904 table = gmap_table_walk(sg, saddr, 3);
1905 if (!table || (*table & _REGION_ENTRY_ORIGIN) != s_r3t)
1906 rc = -EAGAIN; /* Race with unshadow */
1908 *table &= ~_REGION_ENTRY_INVALID;
1910 gmap_unshadow_r3t(sg, raddr);
1912 spin_unlock(&sg->guest_table_lock);
1915 spin_unlock(&sg->guest_table_lock);
1916 __free_pages(page, CRST_ALLOC_ORDER);
1919 EXPORT_SYMBOL_GPL(gmap_shadow_r3t);
1922 * gmap_shadow_sgt - create a shadow segment table
1923 * @sg: pointer to the shadow guest address space structure
1924 * @saddr: faulting address in the shadow gmap
1925 * @sgt: parent gmap address of the segment table to get shadowed
1926 * @fake: sgt references contiguous guest memory block, not a sgt
1928 * Returns: 0 if successfully shadowed or already shadowed, -EAGAIN if the
1929 * shadow table structure is incomplete, -ENOMEM if out of memory and
1930 * -EFAULT if an address in the parent gmap could not be resolved.
1932 * Called with sg->mm->mmap_lock in read.
1934 int gmap_shadow_sgt(struct gmap *sg, unsigned long saddr, unsigned long sgt,
1937 unsigned long raddr, origin, offset, len;
1938 unsigned long *table;
1943 BUG_ON(!gmap_is_shadow(sg) || (sgt & _REGION3_ENTRY_LARGE));
1944 /* Allocate a shadow segment table */
1945 page = gmap_alloc_crst();
1948 page->index = sgt & _REGION_ENTRY_ORIGIN;
1950 page->index |= GMAP_SHADOW_FAKE_TABLE;
1951 s_sgt = page_to_phys(page);
1952 /* Install shadow region second table */
1953 spin_lock(&sg->guest_table_lock);
1954 table = gmap_table_walk(sg, saddr, 2); /* get region-3 pointer */
1956 rc = -EAGAIN; /* Race with unshadow */
1959 if (!(*table & _REGION_ENTRY_INVALID)) {
1960 rc = 0; /* Already established */
1962 } else if (*table & _REGION_ENTRY_ORIGIN) {
1963 rc = -EAGAIN; /* Race with shadow */
1966 crst_table_init(__va(s_sgt), _SEGMENT_ENTRY_EMPTY);
1967 /* mark as invalid as long as the parent table is not protected */
1968 *table = s_sgt | _REGION_ENTRY_LENGTH |
1969 _REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_INVALID;
1970 if (sg->edat_level >= 1)
1971 *table |= sgt & _REGION_ENTRY_PROTECT;
1972 list_add(&page->lru, &sg->crst_list);
1974 /* nothing to protect for fake tables */
1975 *table &= ~_REGION_ENTRY_INVALID;
1976 spin_unlock(&sg->guest_table_lock);
1979 spin_unlock(&sg->guest_table_lock);
1980 /* Make sgt read-only in parent gmap page table */
1981 raddr = (saddr & _REGION3_MASK) | _SHADOW_RMAP_REGION3;
1982 origin = sgt & _REGION_ENTRY_ORIGIN;
1983 offset = ((sgt & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
1984 len = ((sgt & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
1985 rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
1986 spin_lock(&sg->guest_table_lock);
1988 table = gmap_table_walk(sg, saddr, 2);
1989 if (!table || (*table & _REGION_ENTRY_ORIGIN) != s_sgt)
1990 rc = -EAGAIN; /* Race with unshadow */
1992 *table &= ~_REGION_ENTRY_INVALID;
1994 gmap_unshadow_sgt(sg, raddr);
1996 spin_unlock(&sg->guest_table_lock);
1999 spin_unlock(&sg->guest_table_lock);
2000 __free_pages(page, CRST_ALLOC_ORDER);
2003 EXPORT_SYMBOL_GPL(gmap_shadow_sgt);
2006 * gmap_shadow_pgt_lookup - find a shadow page table
2007 * @sg: pointer to the shadow guest address space structure
2008 * @saddr: the address in the shadow aguest address space
2009 * @pgt: parent gmap address of the page table to get shadowed
2010 * @dat_protection: if the pgtable is marked as protected by dat
2011 * @fake: pgt references contiguous guest memory block, not a pgtable
2013 * Returns 0 if the shadow page table was found and -EAGAIN if the page
2014 * table was not found.
2016 * Called with sg->mm->mmap_lock in read.
2018 int gmap_shadow_pgt_lookup(struct gmap *sg, unsigned long saddr,
2019 unsigned long *pgt, int *dat_protection,
2022 unsigned long *table;
2026 BUG_ON(!gmap_is_shadow(sg));
2027 spin_lock(&sg->guest_table_lock);
2028 table = gmap_table_walk(sg, saddr, 1); /* get segment pointer */
2029 if (table && !(*table & _SEGMENT_ENTRY_INVALID)) {
2030 /* Shadow page tables are full pages (pte+pgste) */
2031 page = pfn_to_page(*table >> PAGE_SHIFT);
2032 *pgt = page->index & ~GMAP_SHADOW_FAKE_TABLE;
2033 *dat_protection = !!(*table & _SEGMENT_ENTRY_PROTECT);
2034 *fake = !!(page->index & GMAP_SHADOW_FAKE_TABLE);
2039 spin_unlock(&sg->guest_table_lock);
2043 EXPORT_SYMBOL_GPL(gmap_shadow_pgt_lookup);
2046 * gmap_shadow_pgt - instantiate a shadow page table
2047 * @sg: pointer to the shadow guest address space structure
2048 * @saddr: faulting address in the shadow gmap
2049 * @pgt: parent gmap address of the page table to get shadowed
2050 * @fake: pgt references contiguous guest memory block, not a pgtable
2052 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
2053 * shadow table structure is incomplete, -ENOMEM if out of memory,
2054 * -EFAULT if an address in the parent gmap could not be resolved and
2056 * Called with gmap->mm->mmap_lock in read
2058 int gmap_shadow_pgt(struct gmap *sg, unsigned long saddr, unsigned long pgt,
2061 unsigned long raddr, origin;
2062 unsigned long *table;
2063 struct ptdesc *ptdesc;
2067 BUG_ON(!gmap_is_shadow(sg) || (pgt & _SEGMENT_ENTRY_LARGE));
2068 /* Allocate a shadow page table */
2069 ptdesc = page_table_alloc_pgste(sg->mm);
2072 ptdesc->pt_index = pgt & _SEGMENT_ENTRY_ORIGIN;
2074 ptdesc->pt_index |= GMAP_SHADOW_FAKE_TABLE;
2075 s_pgt = page_to_phys(ptdesc_page(ptdesc));
2076 /* Install shadow page table */
2077 spin_lock(&sg->guest_table_lock);
2078 table = gmap_table_walk(sg, saddr, 1); /* get segment pointer */
2080 rc = -EAGAIN; /* Race with unshadow */
2083 if (!(*table & _SEGMENT_ENTRY_INVALID)) {
2084 rc = 0; /* Already established */
2086 } else if (*table & _SEGMENT_ENTRY_ORIGIN) {
2087 rc = -EAGAIN; /* Race with shadow */
2090 /* mark as invalid as long as the parent table is not protected */
2091 *table = (unsigned long) s_pgt | _SEGMENT_ENTRY |
2092 (pgt & _SEGMENT_ENTRY_PROTECT) | _SEGMENT_ENTRY_INVALID;
2093 list_add(&ptdesc->pt_list, &sg->pt_list);
2095 /* nothing to protect for fake tables */
2096 *table &= ~_SEGMENT_ENTRY_INVALID;
2097 spin_unlock(&sg->guest_table_lock);
2100 spin_unlock(&sg->guest_table_lock);
2101 /* Make pgt read-only in parent gmap page table (not the pgste) */
2102 raddr = (saddr & _SEGMENT_MASK) | _SHADOW_RMAP_SEGMENT;
2103 origin = pgt & _SEGMENT_ENTRY_ORIGIN & PAGE_MASK;
2104 rc = gmap_protect_rmap(sg, raddr, origin, PAGE_SIZE);
2105 spin_lock(&sg->guest_table_lock);
2107 table = gmap_table_walk(sg, saddr, 1);
2108 if (!table || (*table & _SEGMENT_ENTRY_ORIGIN) != s_pgt)
2109 rc = -EAGAIN; /* Race with unshadow */
2111 *table &= ~_SEGMENT_ENTRY_INVALID;
2113 gmap_unshadow_pgt(sg, raddr);
2115 spin_unlock(&sg->guest_table_lock);
2118 spin_unlock(&sg->guest_table_lock);
2119 page_table_free_pgste(ptdesc);
2123 EXPORT_SYMBOL_GPL(gmap_shadow_pgt);
2126 * gmap_shadow_page - create a shadow page mapping
2127 * @sg: pointer to the shadow guest address space structure
2128 * @saddr: faulting address in the shadow gmap
2129 * @pte: pte in parent gmap address space to get shadowed
2131 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
2132 * shadow table structure is incomplete, -ENOMEM if out of memory and
2133 * -EFAULT if an address in the parent gmap could not be resolved.
2135 * Called with sg->mm->mmap_lock in read.
2137 int gmap_shadow_page(struct gmap *sg, unsigned long saddr, pte_t pte)
2139 struct gmap *parent;
2140 struct gmap_rmap *rmap;
2141 unsigned long vmaddr, paddr;
2143 pte_t *sptep, *tptep;
2147 BUG_ON(!gmap_is_shadow(sg));
2148 parent = sg->parent;
2149 prot = (pte_val(pte) & _PAGE_PROTECT) ? PROT_READ : PROT_WRITE;
2151 rmap = kzalloc(sizeof(*rmap), GFP_KERNEL_ACCOUNT);
2154 rmap->raddr = (saddr & PAGE_MASK) | _SHADOW_RMAP_PGTABLE;
2157 paddr = pte_val(pte) & PAGE_MASK;
2158 vmaddr = __gmap_translate(parent, paddr);
2159 if (IS_ERR_VALUE(vmaddr)) {
2163 rc = radix_tree_preload(GFP_KERNEL_ACCOUNT);
2167 sptep = gmap_pte_op_walk(parent, paddr, &ptl);
2169 spin_lock(&sg->guest_table_lock);
2170 /* Get page table pointer */
2171 tptep = (pte_t *) gmap_table_walk(sg, saddr, 0);
2173 spin_unlock(&sg->guest_table_lock);
2174 gmap_pte_op_end(sptep, ptl);
2175 radix_tree_preload_end();
2178 rc = ptep_shadow_pte(sg->mm, saddr, sptep, tptep, pte);
2180 /* Success and a new mapping */
2181 gmap_insert_rmap(sg, vmaddr, rmap);
2185 gmap_pte_op_end(sptep, ptl);
2186 spin_unlock(&sg->guest_table_lock);
2188 radix_tree_preload_end();
2191 rc = gmap_pte_op_fixup(parent, paddr, vmaddr, prot);
2198 EXPORT_SYMBOL_GPL(gmap_shadow_page);
2201 * gmap_shadow_notify - handle notifications for shadow gmap
2203 * Called with sg->parent->shadow_lock.
2205 static void gmap_shadow_notify(struct gmap *sg, unsigned long vmaddr,
2206 unsigned long gaddr)
2208 struct gmap_rmap *rmap, *rnext, *head;
2209 unsigned long start, end, bits, raddr;
2211 BUG_ON(!gmap_is_shadow(sg));
2213 spin_lock(&sg->guest_table_lock);
2215 spin_unlock(&sg->guest_table_lock);
2218 /* Check for top level table */
2219 start = sg->orig_asce & _ASCE_ORIGIN;
2220 end = start + ((sg->orig_asce & _ASCE_TABLE_LENGTH) + 1) * PAGE_SIZE;
2221 if (!(sg->orig_asce & _ASCE_REAL_SPACE) && gaddr >= start &&
2223 /* The complete shadow table has to go */
2225 spin_unlock(&sg->guest_table_lock);
2226 list_del(&sg->list);
2230 /* Remove the page table tree from on specific entry */
2231 head = radix_tree_delete(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT);
2232 gmap_for_each_rmap_safe(rmap, rnext, head) {
2233 bits = rmap->raddr & _SHADOW_RMAP_MASK;
2234 raddr = rmap->raddr ^ bits;
2236 case _SHADOW_RMAP_REGION1:
2237 gmap_unshadow_r2t(sg, raddr);
2239 case _SHADOW_RMAP_REGION2:
2240 gmap_unshadow_r3t(sg, raddr);
2242 case _SHADOW_RMAP_REGION3:
2243 gmap_unshadow_sgt(sg, raddr);
2245 case _SHADOW_RMAP_SEGMENT:
2246 gmap_unshadow_pgt(sg, raddr);
2248 case _SHADOW_RMAP_PGTABLE:
2249 gmap_unshadow_page(sg, raddr);
2254 spin_unlock(&sg->guest_table_lock);
2258 * ptep_notify - call all invalidation callbacks for a specific pte.
2259 * @mm: pointer to the process mm_struct
2260 * @vmaddr: virtual address in the process address space
2261 * @pte: pointer to the page table entry
2262 * @bits: bits from the pgste that caused the notify call
2264 * This function is assumed to be called with the page table lock held
2265 * for the pte to notify.
2267 void ptep_notify(struct mm_struct *mm, unsigned long vmaddr,
2268 pte_t *pte, unsigned long bits)
2270 unsigned long offset, gaddr = 0;
2271 unsigned long *table;
2272 struct gmap *gmap, *sg, *next;
2274 offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
2275 offset = offset * (PAGE_SIZE / sizeof(pte_t));
2277 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2278 spin_lock(&gmap->guest_table_lock);
2279 table = radix_tree_lookup(&gmap->host_to_guest,
2280 vmaddr >> PMD_SHIFT);
2282 gaddr = __gmap_segment_gaddr(table) + offset;
2283 spin_unlock(&gmap->guest_table_lock);
2287 if (!list_empty(&gmap->children) && (bits & PGSTE_VSIE_BIT)) {
2288 spin_lock(&gmap->shadow_lock);
2289 list_for_each_entry_safe(sg, next,
2290 &gmap->children, list)
2291 gmap_shadow_notify(sg, vmaddr, gaddr);
2292 spin_unlock(&gmap->shadow_lock);
2294 if (bits & PGSTE_IN_BIT)
2295 gmap_call_notifier(gmap, gaddr, gaddr + PAGE_SIZE - 1);
2299 EXPORT_SYMBOL_GPL(ptep_notify);
2301 static void pmdp_notify_gmap(struct gmap *gmap, pmd_t *pmdp,
2302 unsigned long gaddr)
2304 set_pmd(pmdp, clear_pmd_bit(*pmdp, __pgprot(_SEGMENT_ENTRY_GMAP_IN)));
2305 gmap_call_notifier(gmap, gaddr, gaddr + HPAGE_SIZE - 1);
2309 * gmap_pmdp_xchg - exchange a gmap pmd with another
2310 * @gmap: pointer to the guest address space structure
2311 * @pmdp: pointer to the pmd entry
2312 * @new: replacement entry
2313 * @gaddr: the affected guest address
2315 * This function is assumed to be called with the guest_table_lock
2318 static void gmap_pmdp_xchg(struct gmap *gmap, pmd_t *pmdp, pmd_t new,
2319 unsigned long gaddr)
2321 gaddr &= HPAGE_MASK;
2322 pmdp_notify_gmap(gmap, pmdp, gaddr);
2323 new = clear_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_GMAP_IN));
2324 if (MACHINE_HAS_TLB_GUEST)
2325 __pmdp_idte(gaddr, (pmd_t *)pmdp, IDTE_GUEST_ASCE, gmap->asce,
2327 else if (MACHINE_HAS_IDTE)
2328 __pmdp_idte(gaddr, (pmd_t *)pmdp, 0, 0, IDTE_GLOBAL);
2334 static void gmap_pmdp_clear(struct mm_struct *mm, unsigned long vmaddr,
2339 unsigned long gaddr;
2342 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2343 spin_lock(&gmap->guest_table_lock);
2344 pmdp = (pmd_t *)radix_tree_delete(&gmap->host_to_guest,
2345 vmaddr >> PMD_SHIFT);
2347 gaddr = __gmap_segment_gaddr((unsigned long *)pmdp);
2348 pmdp_notify_gmap(gmap, pmdp, gaddr);
2349 WARN_ON(pmd_val(*pmdp) & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
2350 _SEGMENT_ENTRY_GMAP_UC));
2353 set_pmd(pmdp, __pmd(_SEGMENT_ENTRY_EMPTY));
2355 spin_unlock(&gmap->guest_table_lock);
2361 * gmap_pmdp_invalidate - invalidate all affected guest pmd entries without
2363 * @mm: pointer to the process mm_struct
2364 * @vmaddr: virtual address in the process address space
2366 void gmap_pmdp_invalidate(struct mm_struct *mm, unsigned long vmaddr)
2368 gmap_pmdp_clear(mm, vmaddr, 0);
2370 EXPORT_SYMBOL_GPL(gmap_pmdp_invalidate);
2373 * gmap_pmdp_csp - csp all affected guest pmd entries
2374 * @mm: pointer to the process mm_struct
2375 * @vmaddr: virtual address in the process address space
2377 void gmap_pmdp_csp(struct mm_struct *mm, unsigned long vmaddr)
2379 gmap_pmdp_clear(mm, vmaddr, 1);
2381 EXPORT_SYMBOL_GPL(gmap_pmdp_csp);
2384 * gmap_pmdp_idte_local - invalidate and clear a guest pmd entry
2385 * @mm: pointer to the process mm_struct
2386 * @vmaddr: virtual address in the process address space
2388 void gmap_pmdp_idte_local(struct mm_struct *mm, unsigned long vmaddr)
2390 unsigned long *entry, gaddr;
2395 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2396 spin_lock(&gmap->guest_table_lock);
2397 entry = radix_tree_delete(&gmap->host_to_guest,
2398 vmaddr >> PMD_SHIFT);
2400 pmdp = (pmd_t *)entry;
2401 gaddr = __gmap_segment_gaddr(entry);
2402 pmdp_notify_gmap(gmap, pmdp, gaddr);
2403 WARN_ON(*entry & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
2404 _SEGMENT_ENTRY_GMAP_UC));
2405 if (MACHINE_HAS_TLB_GUEST)
2406 __pmdp_idte(gaddr, pmdp, IDTE_GUEST_ASCE,
2407 gmap->asce, IDTE_LOCAL);
2408 else if (MACHINE_HAS_IDTE)
2409 __pmdp_idte(gaddr, pmdp, 0, 0, IDTE_LOCAL);
2410 *entry = _SEGMENT_ENTRY_EMPTY;
2412 spin_unlock(&gmap->guest_table_lock);
2416 EXPORT_SYMBOL_GPL(gmap_pmdp_idte_local);
2419 * gmap_pmdp_idte_global - invalidate and clear a guest pmd entry
2420 * @mm: pointer to the process mm_struct
2421 * @vmaddr: virtual address in the process address space
2423 void gmap_pmdp_idte_global(struct mm_struct *mm, unsigned long vmaddr)
2425 unsigned long *entry, gaddr;
2430 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2431 spin_lock(&gmap->guest_table_lock);
2432 entry = radix_tree_delete(&gmap->host_to_guest,
2433 vmaddr >> PMD_SHIFT);
2435 pmdp = (pmd_t *)entry;
2436 gaddr = __gmap_segment_gaddr(entry);
2437 pmdp_notify_gmap(gmap, pmdp, gaddr);
2438 WARN_ON(*entry & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
2439 _SEGMENT_ENTRY_GMAP_UC));
2440 if (MACHINE_HAS_TLB_GUEST)
2441 __pmdp_idte(gaddr, pmdp, IDTE_GUEST_ASCE,
2442 gmap->asce, IDTE_GLOBAL);
2443 else if (MACHINE_HAS_IDTE)
2444 __pmdp_idte(gaddr, pmdp, 0, 0, IDTE_GLOBAL);
2447 *entry = _SEGMENT_ENTRY_EMPTY;
2449 spin_unlock(&gmap->guest_table_lock);
2453 EXPORT_SYMBOL_GPL(gmap_pmdp_idte_global);
2456 * gmap_test_and_clear_dirty_pmd - test and reset segment dirty status
2457 * @gmap: pointer to guest address space
2458 * @pmdp: pointer to the pmd to be tested
2459 * @gaddr: virtual address in the guest address space
2461 * This function is assumed to be called with the guest_table_lock
2464 static bool gmap_test_and_clear_dirty_pmd(struct gmap *gmap, pmd_t *pmdp,
2465 unsigned long gaddr)
2467 if (pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID)
2470 /* Already protected memory, which did not change is clean */
2471 if (pmd_val(*pmdp) & _SEGMENT_ENTRY_PROTECT &&
2472 !(pmd_val(*pmdp) & _SEGMENT_ENTRY_GMAP_UC))
2475 /* Clear UC indication and reset protection */
2476 set_pmd(pmdp, clear_pmd_bit(*pmdp, __pgprot(_SEGMENT_ENTRY_GMAP_UC)));
2477 gmap_protect_pmd(gmap, gaddr, pmdp, PROT_READ, 0);
2482 * gmap_sync_dirty_log_pmd - set bitmap based on dirty status of segment
2483 * @gmap: pointer to guest address space
2484 * @bitmap: dirty bitmap for this pmd
2485 * @gaddr: virtual address in the guest address space
2486 * @vmaddr: virtual address in the host address space
2488 * This function is assumed to be called with the guest_table_lock
2491 void gmap_sync_dirty_log_pmd(struct gmap *gmap, unsigned long bitmap[4],
2492 unsigned long gaddr, unsigned long vmaddr)
2499 pmdp = gmap_pmd_op_walk(gmap, gaddr);
2503 if (pmd_leaf(*pmdp)) {
2504 if (gmap_test_and_clear_dirty_pmd(gmap, pmdp, gaddr))
2505 bitmap_fill(bitmap, _PAGE_ENTRIES);
2507 for (i = 0; i < _PAGE_ENTRIES; i++, vmaddr += PAGE_SIZE) {
2508 ptep = pte_alloc_map_lock(gmap->mm, pmdp, vmaddr, &ptl);
2511 if (ptep_test_and_clear_uc(gmap->mm, vmaddr, ptep))
2513 pte_unmap_unlock(ptep, ptl);
2516 gmap_pmd_op_end(gmap, pmdp);
2518 EXPORT_SYMBOL_GPL(gmap_sync_dirty_log_pmd);
2520 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
2521 static int thp_split_walk_pmd_entry(pmd_t *pmd, unsigned long addr,
2522 unsigned long end, struct mm_walk *walk)
2524 struct vm_area_struct *vma = walk->vma;
2526 split_huge_pmd(vma, pmd, addr);
2530 static const struct mm_walk_ops thp_split_walk_ops = {
2531 .pmd_entry = thp_split_walk_pmd_entry,
2532 .walk_lock = PGWALK_WRLOCK_VERIFY,
2535 static inline void thp_split_mm(struct mm_struct *mm)
2537 struct vm_area_struct *vma;
2538 VMA_ITERATOR(vmi, mm, 0);
2540 for_each_vma(vmi, vma) {
2541 vm_flags_mod(vma, VM_NOHUGEPAGE, VM_HUGEPAGE);
2542 walk_page_vma(vma, &thp_split_walk_ops, NULL);
2544 mm->def_flags |= VM_NOHUGEPAGE;
2547 static inline void thp_split_mm(struct mm_struct *mm)
2550 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
2553 * switch on pgstes for its userspace process (for kvm)
2555 int s390_enable_sie(void)
2557 struct mm_struct *mm = current->mm;
2559 /* Do we have pgstes? if yes, we are done */
2560 if (mm_has_pgste(mm))
2562 /* Fail if the page tables are 2K */
2563 if (!mm_alloc_pgste(mm))
2565 mmap_write_lock(mm);
2566 mm->context.has_pgste = 1;
2567 /* split thp mappings and disable thp for future mappings */
2569 mmap_write_unlock(mm);
2572 EXPORT_SYMBOL_GPL(s390_enable_sie);
2574 static int find_zeropage_pte_entry(pte_t *pte, unsigned long addr,
2575 unsigned long end, struct mm_walk *walk)
2577 unsigned long *found_addr = walk->private;
2579 /* Return 1 of the page is a zeropage. */
2580 if (is_zero_pfn(pte_pfn(*pte))) {
2582 * Shared zeropage in e.g., a FS DAX mapping? We cannot do the
2583 * right thing and likely don't care: FAULT_FLAG_UNSHARE
2584 * currently only works in COW mappings, which is also where
2585 * mm_forbids_zeropage() is checked.
2587 if (!is_cow_mapping(walk->vma->vm_flags))
2596 static const struct mm_walk_ops find_zeropage_ops = {
2597 .pte_entry = find_zeropage_pte_entry,
2598 .walk_lock = PGWALK_WRLOCK,
2602 * Unshare all shared zeropages, replacing them by anonymous pages. Note that
2603 * we cannot simply zap all shared zeropages, because this could later
2604 * trigger unexpected userfaultfd missing events.
2606 * This must be called after mm->context.allow_cow_sharing was
2607 * set to 0, to avoid future mappings of shared zeropages.
2609 * mm contracts with s390, that even if mm were to remove a page table,
2610 * and racing with walk_page_range_vma() calling pte_offset_map_lock()
2611 * would fail, it will never insert a page table containing empty zero
2612 * pages once mm_forbids_zeropage(mm) i.e.
2613 * mm->context.allow_cow_sharing is set to 0.
2615 static int __s390_unshare_zeropages(struct mm_struct *mm)
2617 struct vm_area_struct *vma;
2618 VMA_ITERATOR(vmi, mm, 0);
2623 for_each_vma(vmi, vma) {
2625 * We could only look at COW mappings, but it's more future
2626 * proof to catch unexpected zeropages in other mappings and
2629 if ((vma->vm_flags & VM_PFNMAP) || is_vm_hugetlb_page(vma))
2631 addr = vma->vm_start;
2634 rc = walk_page_range_vma(vma, addr, vma->vm_end,
2635 &find_zeropage_ops, &addr);
2641 /* addr was updated by find_zeropage_pte_entry() */
2642 fault = handle_mm_fault(vma, addr,
2643 FAULT_FLAG_UNSHARE | FAULT_FLAG_REMOTE,
2645 if (fault & VM_FAULT_OOM)
2648 * See break_ksm(): even after handle_mm_fault() returned 0, we
2649 * must start the lookup from the current address, because
2650 * handle_mm_fault() may back out if there's any difficulty.
2652 * VM_FAULT_SIGBUS and VM_FAULT_SIGSEGV are unexpected but
2653 * maybe they could trigger in the future on concurrent
2654 * truncation. In that case, the shared zeropage would be gone
2655 * and we can simply retry and make progress.
2664 static int __s390_disable_cow_sharing(struct mm_struct *mm)
2668 if (!mm->context.allow_cow_sharing)
2671 mm->context.allow_cow_sharing = 0;
2673 /* Replace all shared zeropages by anonymous pages. */
2674 rc = __s390_unshare_zeropages(mm);
2676 * Make sure to disable KSM (if enabled for the whole process or
2677 * individual VMAs). Note that nothing currently hinders user space
2678 * from re-enabling it.
2681 rc = ksm_disable(mm);
2683 mm->context.allow_cow_sharing = 1;
2688 * Disable most COW-sharing of memory pages for the whole process:
2689 * (1) Disable KSM and unmerge/unshare any KSM pages.
2690 * (2) Disallow shared zeropages and unshare any zerpages that are mapped.
2692 * Not that we currently don't bother with COW-shared pages that are shared
2693 * with parent/child processes due to fork().
2695 int s390_disable_cow_sharing(void)
2699 mmap_write_lock(current->mm);
2700 rc = __s390_disable_cow_sharing(current->mm);
2701 mmap_write_unlock(current->mm);
2704 EXPORT_SYMBOL_GPL(s390_disable_cow_sharing);
2707 * Enable storage key handling from now on and initialize the storage
2708 * keys with the default key.
2710 static int __s390_enable_skey_pte(pte_t *pte, unsigned long addr,
2711 unsigned long next, struct mm_walk *walk)
2713 /* Clear storage key */
2714 ptep_zap_key(walk->mm, addr, pte);
2719 * Give a chance to schedule after setting a key to 256 pages.
2720 * We only hold the mm lock, which is a rwsem and the kvm srcu.
2723 static int __s390_enable_skey_pmd(pmd_t *pmd, unsigned long addr,
2724 unsigned long next, struct mm_walk *walk)
2730 static int __s390_enable_skey_hugetlb(pte_t *pte, unsigned long addr,
2731 unsigned long hmask, unsigned long next,
2732 struct mm_walk *walk)
2734 pmd_t *pmd = (pmd_t *)pte;
2735 unsigned long start, end;
2736 struct folio *folio = page_folio(pmd_page(*pmd));
2739 * The write check makes sure we do not set a key on shared
2740 * memory. This is needed as the walker does not differentiate
2741 * between actual guest memory and the process executable or
2744 if (pmd_val(*pmd) & _SEGMENT_ENTRY_INVALID ||
2745 !(pmd_val(*pmd) & _SEGMENT_ENTRY_WRITE))
2748 start = pmd_val(*pmd) & HPAGE_MASK;
2749 end = start + HPAGE_SIZE;
2750 __storage_key_init_range(start, end);
2751 set_bit(PG_arch_1, &folio->flags);
2756 static const struct mm_walk_ops enable_skey_walk_ops = {
2757 .hugetlb_entry = __s390_enable_skey_hugetlb,
2758 .pte_entry = __s390_enable_skey_pte,
2759 .pmd_entry = __s390_enable_skey_pmd,
2760 .walk_lock = PGWALK_WRLOCK,
2763 int s390_enable_skey(void)
2765 struct mm_struct *mm = current->mm;
2768 mmap_write_lock(mm);
2769 if (mm_uses_skeys(mm))
2772 mm->context.uses_skeys = 1;
2773 rc = __s390_disable_cow_sharing(mm);
2775 mm->context.uses_skeys = 0;
2778 walk_page_range(mm, 0, TASK_SIZE, &enable_skey_walk_ops, NULL);
2781 mmap_write_unlock(mm);
2784 EXPORT_SYMBOL_GPL(s390_enable_skey);
2787 * Reset CMMA state, make all pages stable again.
2789 static int __s390_reset_cmma(pte_t *pte, unsigned long addr,
2790 unsigned long next, struct mm_walk *walk)
2792 ptep_zap_unused(walk->mm, addr, pte, 1);
2796 static const struct mm_walk_ops reset_cmma_walk_ops = {
2797 .pte_entry = __s390_reset_cmma,
2798 .walk_lock = PGWALK_WRLOCK,
2801 void s390_reset_cmma(struct mm_struct *mm)
2803 mmap_write_lock(mm);
2804 walk_page_range(mm, 0, TASK_SIZE, &reset_cmma_walk_ops, NULL);
2805 mmap_write_unlock(mm);
2807 EXPORT_SYMBOL_GPL(s390_reset_cmma);
2809 #define GATHER_GET_PAGES 32
2811 struct reset_walk_state {
2813 unsigned long count;
2814 unsigned long pfns[GATHER_GET_PAGES];
2817 static int s390_gather_pages(pte_t *ptep, unsigned long addr,
2818 unsigned long next, struct mm_walk *walk)
2820 struct reset_walk_state *p = walk->private;
2821 pte_t pte = READ_ONCE(*ptep);
2823 if (pte_present(pte)) {
2824 /* we have a reference from the mapping, take an extra one */
2825 get_page(phys_to_page(pte_val(pte)));
2826 p->pfns[p->count] = phys_to_pfn(pte_val(pte));
2830 return p->count >= GATHER_GET_PAGES;
2833 static const struct mm_walk_ops gather_pages_ops = {
2834 .pte_entry = s390_gather_pages,
2835 .walk_lock = PGWALK_RDLOCK,
2839 * Call the Destroy secure page UVC on each page in the given array of PFNs.
2840 * Each page needs to have an extra reference, which will be released here.
2842 void s390_uv_destroy_pfns(unsigned long count, unsigned long *pfns)
2844 struct folio *folio;
2847 for (i = 0; i < count; i++) {
2848 folio = pfn_folio(pfns[i]);
2849 /* we always have an extra reference */
2850 uv_destroy_folio(folio);
2851 /* get rid of the extra reference */
2856 EXPORT_SYMBOL_GPL(s390_uv_destroy_pfns);
2859 * __s390_uv_destroy_range - Call the destroy secure page UVC on each page
2860 * in the given range of the given address space.
2861 * @mm: the mm to operate on
2862 * @start: the start of the range
2863 * @end: the end of the range
2864 * @interruptible: if not 0, stop when a fatal signal is received
2866 * Walk the given range of the given address space and call the destroy
2867 * secure page UVC on each page. Optionally exit early if a fatal signal is
2870 * Return: 0 on success, -EINTR if the function stopped before completing
2872 int __s390_uv_destroy_range(struct mm_struct *mm, unsigned long start,
2873 unsigned long end, bool interruptible)
2875 struct reset_walk_state state = { .next = start };
2881 r = walk_page_range(mm, state.next, end, &gather_pages_ops, &state);
2882 mmap_read_unlock(mm);
2884 s390_uv_destroy_pfns(state.count, state.pfns);
2885 if (interruptible && fatal_signal_pending(current))
2890 EXPORT_SYMBOL_GPL(__s390_uv_destroy_range);
2893 * s390_unlist_old_asce - Remove the topmost level of page tables from the
2894 * list of page tables of the gmap.
2895 * @gmap: the gmap whose table is to be removed
2897 * On s390x, KVM keeps a list of all pages containing the page tables of the
2898 * gmap (the CRST list). This list is used at tear down time to free all
2899 * pages that are now not needed anymore.
2901 * This function removes the topmost page of the tree (the one pointed to by
2902 * the ASCE) from the CRST list.
2904 * This means that it will not be freed when the VM is torn down, and needs
2905 * to be handled separately by the caller, unless a leak is actually
2906 * intended. Notice that this function will only remove the page from the
2907 * list, the page will still be used as a top level page table (and ASCE).
2909 void s390_unlist_old_asce(struct gmap *gmap)
2913 old = virt_to_page(gmap->table);
2914 spin_lock(&gmap->guest_table_lock);
2915 list_del(&old->lru);
2917 * Sometimes the topmost page might need to be "removed" multiple
2918 * times, for example if the VM is rebooted into secure mode several
2919 * times concurrently, or if s390_replace_asce fails after calling
2920 * s390_remove_old_asce and is attempted again later. In that case
2921 * the old asce has been removed from the list, and therefore it
2922 * will not be freed when the VM terminates, but the ASCE is still
2923 * in use and still pointed to.
2924 * A subsequent call to replace_asce will follow the pointer and try
2925 * to remove the same page from the list again.
2926 * Therefore it's necessary that the page of the ASCE has valid
2927 * pointers, so list_del can work (and do nothing) without
2928 * dereferencing stale or invalid pointers.
2930 INIT_LIST_HEAD(&old->lru);
2931 spin_unlock(&gmap->guest_table_lock);
2933 EXPORT_SYMBOL_GPL(s390_unlist_old_asce);
2936 * s390_replace_asce - Try to replace the current ASCE of a gmap with a copy
2937 * @gmap: the gmap whose ASCE needs to be replaced
2939 * If the ASCE is a SEGMENT type then this function will return -EINVAL,
2940 * otherwise the pointers in the host_to_guest radix tree will keep pointing
2941 * to the wrong pages, causing use-after-free and memory corruption.
2942 * If the allocation of the new top level page table fails, the ASCE is not
2944 * In any case, the old ASCE is always removed from the gmap CRST list.
2945 * Therefore the caller has to make sure to save a pointer to it
2946 * beforehand, unless a leak is actually intended.
2948 int s390_replace_asce(struct gmap *gmap)
2954 s390_unlist_old_asce(gmap);
2956 /* Replacing segment type ASCEs would cause serious issues */
2957 if ((gmap->asce & _ASCE_TYPE_MASK) == _ASCE_TYPE_SEGMENT)
2960 page = gmap_alloc_crst();
2964 table = page_to_virt(page);
2965 memcpy(table, gmap->table, 1UL << (CRST_ALLOC_ORDER + PAGE_SHIFT));
2968 * The caller has to deal with the old ASCE, but here we make sure
2969 * the new one is properly added to the CRST list, so that
2970 * it will be freed when the VM is torn down.
2972 spin_lock(&gmap->guest_table_lock);
2973 list_add(&page->lru, &gmap->crst_list);
2974 spin_unlock(&gmap->guest_table_lock);
2976 /* Set new table origin while preserving existing ASCE control bits */
2977 asce = (gmap->asce & ~_ASCE_ORIGIN) | __pa(table);
2978 WRITE_ONCE(gmap->asce, asce);
2979 WRITE_ONCE(gmap->mm->context.gmap_asce, asce);
2980 WRITE_ONCE(gmap->table, table);
2984 EXPORT_SYMBOL_GPL(s390_replace_asce);