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>
22 #include <asm/pgalloc.h>
26 #define GMAP_SHADOW_FAKE_TABLE 1ULL
29 * gmap_alloc - allocate and initialize a guest address space
30 * @limit: maximum address of the gmap address space
32 * Returns a guest address space structure.
34 static struct gmap *gmap_alloc(unsigned long limit)
39 unsigned long etype, atype;
41 if (limit < _REGION3_SIZE) {
42 limit = _REGION3_SIZE - 1;
43 atype = _ASCE_TYPE_SEGMENT;
44 etype = _SEGMENT_ENTRY_EMPTY;
45 } else if (limit < _REGION2_SIZE) {
46 limit = _REGION2_SIZE - 1;
47 atype = _ASCE_TYPE_REGION3;
48 etype = _REGION3_ENTRY_EMPTY;
49 } else if (limit < _REGION1_SIZE) {
50 limit = _REGION1_SIZE - 1;
51 atype = _ASCE_TYPE_REGION2;
52 etype = _REGION2_ENTRY_EMPTY;
55 atype = _ASCE_TYPE_REGION1;
56 etype = _REGION1_ENTRY_EMPTY;
58 gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL_ACCOUNT);
61 INIT_LIST_HEAD(&gmap->crst_list);
62 INIT_LIST_HEAD(&gmap->children);
63 INIT_LIST_HEAD(&gmap->pt_list);
64 INIT_RADIX_TREE(&gmap->guest_to_host, GFP_KERNEL_ACCOUNT);
65 INIT_RADIX_TREE(&gmap->host_to_guest, GFP_ATOMIC | __GFP_ACCOUNT);
66 INIT_RADIX_TREE(&gmap->host_to_rmap, GFP_ATOMIC | __GFP_ACCOUNT);
67 spin_lock_init(&gmap->guest_table_lock);
68 spin_lock_init(&gmap->shadow_lock);
69 refcount_set(&gmap->ref_count, 1);
70 page = alloc_pages(GFP_KERNEL_ACCOUNT, CRST_ALLOC_ORDER);
74 list_add(&page->lru, &gmap->crst_list);
75 table = (unsigned long *) page_to_phys(page);
76 crst_table_init(table, etype);
78 gmap->asce = atype | _ASCE_TABLE_LENGTH |
79 _ASCE_USER_BITS | __pa(table);
80 gmap->asce_end = limit;
90 * gmap_create - create a guest address space
91 * @mm: pointer to the parent mm_struct
92 * @limit: maximum size of the gmap address space
94 * Returns a guest address space structure.
96 struct gmap *gmap_create(struct mm_struct *mm, unsigned long limit)
99 unsigned long gmap_asce;
101 gmap = gmap_alloc(limit);
105 spin_lock(&mm->context.lock);
106 list_add_rcu(&gmap->list, &mm->context.gmap_list);
107 if (list_is_singular(&mm->context.gmap_list))
108 gmap_asce = gmap->asce;
111 WRITE_ONCE(mm->context.gmap_asce, gmap_asce);
112 spin_unlock(&mm->context.lock);
115 EXPORT_SYMBOL_GPL(gmap_create);
117 static void gmap_flush_tlb(struct gmap *gmap)
119 if (MACHINE_HAS_IDTE)
120 __tlb_flush_idte(gmap->asce);
122 __tlb_flush_global();
125 static void gmap_radix_tree_free(struct radix_tree_root *root)
127 struct radix_tree_iter iter;
128 unsigned long indices[16];
133 /* A radix tree is freed by deleting all of its entries */
137 radix_tree_for_each_slot(slot, root, &iter, index) {
138 indices[nr] = iter.index;
142 for (i = 0; i < nr; i++) {
144 radix_tree_delete(root, index);
149 static void gmap_rmap_radix_tree_free(struct radix_tree_root *root)
151 struct gmap_rmap *rmap, *rnext, *head;
152 struct radix_tree_iter iter;
153 unsigned long indices[16];
158 /* A radix tree is freed by deleting all of its entries */
162 radix_tree_for_each_slot(slot, root, &iter, index) {
163 indices[nr] = iter.index;
167 for (i = 0; i < nr; i++) {
169 head = radix_tree_delete(root, index);
170 gmap_for_each_rmap_safe(rmap, rnext, head)
177 * gmap_free - free a guest address space
178 * @gmap: pointer to the guest address space structure
180 * No locks required. There are no references to this gmap anymore.
182 static void gmap_free(struct gmap *gmap)
184 struct page *page, *next;
186 /* Flush tlb of all gmaps (if not already done for shadows) */
187 if (!(gmap_is_shadow(gmap) && gmap->removed))
188 gmap_flush_tlb(gmap);
189 /* Free all segment & region tables. */
190 list_for_each_entry_safe(page, next, &gmap->crst_list, lru)
191 __free_pages(page, CRST_ALLOC_ORDER);
192 gmap_radix_tree_free(&gmap->guest_to_host);
193 gmap_radix_tree_free(&gmap->host_to_guest);
195 /* Free additional data for a shadow gmap */
196 if (gmap_is_shadow(gmap)) {
197 /* Free all page tables. */
198 list_for_each_entry_safe(page, next, &gmap->pt_list, lru)
199 page_table_free_pgste(page);
200 gmap_rmap_radix_tree_free(&gmap->host_to_rmap);
201 /* Release reference to the parent */
202 gmap_put(gmap->parent);
209 * gmap_get - increase reference counter for guest address space
210 * @gmap: pointer to the guest address space structure
212 * Returns the gmap pointer
214 struct gmap *gmap_get(struct gmap *gmap)
216 refcount_inc(&gmap->ref_count);
219 EXPORT_SYMBOL_GPL(gmap_get);
222 * gmap_put - decrease reference counter for guest address space
223 * @gmap: pointer to the guest address space structure
225 * If the reference counter reaches zero the guest address space is freed.
227 void gmap_put(struct gmap *gmap)
229 if (refcount_dec_and_test(&gmap->ref_count))
232 EXPORT_SYMBOL_GPL(gmap_put);
235 * gmap_remove - remove a guest address space but do not free it yet
236 * @gmap: pointer to the guest address space structure
238 void gmap_remove(struct gmap *gmap)
240 struct gmap *sg, *next;
241 unsigned long gmap_asce;
243 /* Remove all shadow gmaps linked to this gmap */
244 if (!list_empty(&gmap->children)) {
245 spin_lock(&gmap->shadow_lock);
246 list_for_each_entry_safe(sg, next, &gmap->children, list) {
250 spin_unlock(&gmap->shadow_lock);
252 /* Remove gmap from the pre-mm list */
253 spin_lock(&gmap->mm->context.lock);
254 list_del_rcu(&gmap->list);
255 if (list_empty(&gmap->mm->context.gmap_list))
257 else if (list_is_singular(&gmap->mm->context.gmap_list))
258 gmap_asce = list_first_entry(&gmap->mm->context.gmap_list,
259 struct gmap, list)->asce;
262 WRITE_ONCE(gmap->mm->context.gmap_asce, gmap_asce);
263 spin_unlock(&gmap->mm->context.lock);
268 EXPORT_SYMBOL_GPL(gmap_remove);
271 * gmap_enable - switch primary space to the guest address space
272 * @gmap: pointer to the guest address space structure
274 void gmap_enable(struct gmap *gmap)
276 S390_lowcore.gmap = (unsigned long) gmap;
278 EXPORT_SYMBOL_GPL(gmap_enable);
281 * gmap_disable - switch back to the standard primary address space
282 * @gmap: pointer to the guest address space structure
284 void gmap_disable(struct gmap *gmap)
286 S390_lowcore.gmap = 0UL;
288 EXPORT_SYMBOL_GPL(gmap_disable);
291 * gmap_get_enabled - get a pointer to the currently enabled gmap
293 * Returns a pointer to the currently enabled gmap. 0 if none is enabled.
295 struct gmap *gmap_get_enabled(void)
297 return (struct gmap *) S390_lowcore.gmap;
299 EXPORT_SYMBOL_GPL(gmap_get_enabled);
302 * gmap_alloc_table is assumed to be called with mmap_lock held
304 static int gmap_alloc_table(struct gmap *gmap, unsigned long *table,
305 unsigned long init, unsigned long gaddr)
310 /* since we dont free the gmap table until gmap_free we can unlock */
311 page = alloc_pages(GFP_KERNEL_ACCOUNT, CRST_ALLOC_ORDER);
314 new = (unsigned long *) page_to_phys(page);
315 crst_table_init(new, init);
316 spin_lock(&gmap->guest_table_lock);
317 if (*table & _REGION_ENTRY_INVALID) {
318 list_add(&page->lru, &gmap->crst_list);
319 *table = (unsigned long) new | _REGION_ENTRY_LENGTH |
320 (*table & _REGION_ENTRY_TYPE_MASK);
324 spin_unlock(&gmap->guest_table_lock);
326 __free_pages(page, CRST_ALLOC_ORDER);
331 * __gmap_segment_gaddr - find virtual address from segment pointer
332 * @entry: pointer to a segment table entry in the guest address space
334 * Returns the virtual address in the guest address space for the segment
336 static unsigned long __gmap_segment_gaddr(unsigned long *entry)
339 unsigned long offset, mask;
341 offset = (unsigned long) entry / sizeof(unsigned long);
342 offset = (offset & (PTRS_PER_PMD - 1)) * PMD_SIZE;
343 mask = ~(PTRS_PER_PMD * sizeof(pmd_t) - 1);
344 page = virt_to_page((void *)((unsigned long) entry & mask));
345 return page->index + offset;
349 * __gmap_unlink_by_vmaddr - unlink a single segment via a host address
350 * @gmap: pointer to the guest address space structure
351 * @vmaddr: address in the host process address space
353 * Returns 1 if a TLB flush is required
355 static int __gmap_unlink_by_vmaddr(struct gmap *gmap, unsigned long vmaddr)
357 unsigned long *entry;
360 BUG_ON(gmap_is_shadow(gmap));
361 spin_lock(&gmap->guest_table_lock);
362 entry = radix_tree_delete(&gmap->host_to_guest, vmaddr >> PMD_SHIFT);
364 flush = (*entry != _SEGMENT_ENTRY_EMPTY);
365 *entry = _SEGMENT_ENTRY_EMPTY;
367 spin_unlock(&gmap->guest_table_lock);
372 * __gmap_unmap_by_gaddr - unmap a single segment via a guest address
373 * @gmap: pointer to the guest address space structure
374 * @gaddr: address in the guest address space
376 * Returns 1 if a TLB flush is required
378 static int __gmap_unmap_by_gaddr(struct gmap *gmap, unsigned long gaddr)
380 unsigned long vmaddr;
382 vmaddr = (unsigned long) radix_tree_delete(&gmap->guest_to_host,
384 return vmaddr ? __gmap_unlink_by_vmaddr(gmap, vmaddr) : 0;
388 * gmap_unmap_segment - unmap segment from the guest address space
389 * @gmap: pointer to the guest address space structure
390 * @to: address in the guest address space
391 * @len: length of the memory area to unmap
393 * Returns 0 if the unmap succeeded, -EINVAL if not.
395 int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len)
400 BUG_ON(gmap_is_shadow(gmap));
401 if ((to | len) & (PMD_SIZE - 1))
403 if (len == 0 || to + len < to)
407 mmap_write_lock(gmap->mm);
408 for (off = 0; off < len; off += PMD_SIZE)
409 flush |= __gmap_unmap_by_gaddr(gmap, to + off);
410 mmap_write_unlock(gmap->mm);
412 gmap_flush_tlb(gmap);
415 EXPORT_SYMBOL_GPL(gmap_unmap_segment);
418 * gmap_map_segment - map a segment to the guest address space
419 * @gmap: pointer to the guest address space structure
420 * @from: source address in the parent address space
421 * @to: target address in the guest address space
422 * @len: length of the memory area to map
424 * Returns 0 if the mmap succeeded, -EINVAL or -ENOMEM if not.
426 int gmap_map_segment(struct gmap *gmap, unsigned long from,
427 unsigned long to, unsigned long len)
432 BUG_ON(gmap_is_shadow(gmap));
433 if ((from | to | len) & (PMD_SIZE - 1))
435 if (len == 0 || from + len < from || to + len < to ||
436 from + len - 1 > TASK_SIZE_MAX || to + len - 1 > gmap->asce_end)
440 mmap_write_lock(gmap->mm);
441 for (off = 0; off < len; off += PMD_SIZE) {
442 /* Remove old translation */
443 flush |= __gmap_unmap_by_gaddr(gmap, to + off);
444 /* Store new translation */
445 if (radix_tree_insert(&gmap->guest_to_host,
446 (to + off) >> PMD_SHIFT,
447 (void *) from + off))
450 mmap_write_unlock(gmap->mm);
452 gmap_flush_tlb(gmap);
455 gmap_unmap_segment(gmap, to, len);
458 EXPORT_SYMBOL_GPL(gmap_map_segment);
461 * __gmap_translate - translate a guest address to a user space address
462 * @gmap: pointer to guest mapping meta data structure
463 * @gaddr: guest address
465 * Returns user space address which corresponds to the guest address or
466 * -EFAULT if no such mapping exists.
467 * This function does not establish potentially missing page table entries.
468 * The mmap_lock of the mm that belongs to the address space must be held
469 * when this function gets called.
471 * Note: Can also be called for shadow gmaps.
473 unsigned long __gmap_translate(struct gmap *gmap, unsigned long gaddr)
475 unsigned long vmaddr;
477 vmaddr = (unsigned long)
478 radix_tree_lookup(&gmap->guest_to_host, gaddr >> PMD_SHIFT);
479 /* Note: guest_to_host is empty for a shadow gmap */
480 return vmaddr ? (vmaddr | (gaddr & ~PMD_MASK)) : -EFAULT;
482 EXPORT_SYMBOL_GPL(__gmap_translate);
485 * gmap_translate - translate a guest address to a user space address
486 * @gmap: pointer to guest mapping meta data structure
487 * @gaddr: guest address
489 * Returns user space address which corresponds to the guest address or
490 * -EFAULT if no such mapping exists.
491 * This function does not establish potentially missing page table entries.
493 unsigned long gmap_translate(struct gmap *gmap, unsigned long gaddr)
497 mmap_read_lock(gmap->mm);
498 rc = __gmap_translate(gmap, gaddr);
499 mmap_read_unlock(gmap->mm);
502 EXPORT_SYMBOL_GPL(gmap_translate);
505 * gmap_unlink - disconnect a page table from the gmap shadow tables
506 * @mm: pointer to the parent mm_struct
507 * @table: pointer to the host page table
508 * @vmaddr: vm address associated with the host page table
510 void gmap_unlink(struct mm_struct *mm, unsigned long *table,
511 unsigned long vmaddr)
517 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
518 flush = __gmap_unlink_by_vmaddr(gmap, vmaddr);
520 gmap_flush_tlb(gmap);
525 static void gmap_pmdp_xchg(struct gmap *gmap, pmd_t *old, pmd_t new,
526 unsigned long gaddr);
529 * __gmap_link - set up shadow page tables to connect a host to a guest address
530 * @gmap: pointer to guest mapping meta data structure
531 * @gaddr: guest address
532 * @vmaddr: vm address
534 * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
535 * if the vm address is already mapped to a different guest segment.
536 * The mmap_lock of the mm that belongs to the address space must be held
537 * when this function gets called.
539 int __gmap_link(struct gmap *gmap, unsigned long gaddr, unsigned long vmaddr)
541 struct mm_struct *mm;
542 unsigned long *table;
551 BUG_ON(gmap_is_shadow(gmap));
552 /* Create higher level tables in the gmap page table */
554 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION1) {
555 table += (gaddr & _REGION1_INDEX) >> _REGION1_SHIFT;
556 if ((*table & _REGION_ENTRY_INVALID) &&
557 gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY,
558 gaddr & _REGION1_MASK))
560 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
562 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION2) {
563 table += (gaddr & _REGION2_INDEX) >> _REGION2_SHIFT;
564 if ((*table & _REGION_ENTRY_INVALID) &&
565 gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY,
566 gaddr & _REGION2_MASK))
568 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
570 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION3) {
571 table += (gaddr & _REGION3_INDEX) >> _REGION3_SHIFT;
572 if ((*table & _REGION_ENTRY_INVALID) &&
573 gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY,
574 gaddr & _REGION3_MASK))
576 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
578 table += (gaddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
579 /* Walk the parent mm page table */
581 pgd = pgd_offset(mm, vmaddr);
582 VM_BUG_ON(pgd_none(*pgd));
583 p4d = p4d_offset(pgd, vmaddr);
584 VM_BUG_ON(p4d_none(*p4d));
585 pud = pud_offset(p4d, vmaddr);
586 VM_BUG_ON(pud_none(*pud));
587 /* large puds cannot yet be handled */
590 pmd = pmd_offset(pud, vmaddr);
591 VM_BUG_ON(pmd_none(*pmd));
592 /* Are we allowed to use huge pages? */
593 if (pmd_large(*pmd) && !gmap->mm->context.allow_gmap_hpage_1m)
595 /* Link gmap segment table entry location to page table. */
596 rc = radix_tree_preload(GFP_KERNEL_ACCOUNT);
599 ptl = pmd_lock(mm, pmd);
600 spin_lock(&gmap->guest_table_lock);
601 if (*table == _SEGMENT_ENTRY_EMPTY) {
602 rc = radix_tree_insert(&gmap->host_to_guest,
603 vmaddr >> PMD_SHIFT, table);
605 if (pmd_large(*pmd)) {
606 *table = (pmd_val(*pmd) &
607 _SEGMENT_ENTRY_HARDWARE_BITS_LARGE)
608 | _SEGMENT_ENTRY_GMAP_UC;
610 *table = pmd_val(*pmd) &
611 _SEGMENT_ENTRY_HARDWARE_BITS;
613 } else if (*table & _SEGMENT_ENTRY_PROTECT &&
614 !(pmd_val(*pmd) & _SEGMENT_ENTRY_PROTECT)) {
615 unprot = (u64)*table;
616 unprot &= ~_SEGMENT_ENTRY_PROTECT;
617 unprot |= _SEGMENT_ENTRY_GMAP_UC;
618 gmap_pmdp_xchg(gmap, (pmd_t *)table, __pmd(unprot), gaddr);
620 spin_unlock(&gmap->guest_table_lock);
622 radix_tree_preload_end();
627 * gmap_fault - resolve a fault on a guest address
628 * @gmap: pointer to guest mapping meta data structure
629 * @gaddr: guest address
630 * @fault_flags: flags to pass down to handle_mm_fault()
632 * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
633 * if the vm address is already mapped to a different guest segment.
635 int gmap_fault(struct gmap *gmap, unsigned long gaddr,
636 unsigned int fault_flags)
638 unsigned long vmaddr;
642 mmap_read_lock(gmap->mm);
646 vmaddr = __gmap_translate(gmap, gaddr);
647 if (IS_ERR_VALUE(vmaddr)) {
651 if (fixup_user_fault(gmap->mm, vmaddr, fault_flags,
657 * In the case that fixup_user_fault unlocked the mmap_lock during
658 * faultin redo __gmap_translate to not race with a map/unmap_segment.
663 rc = __gmap_link(gmap, gaddr, vmaddr);
665 mmap_read_unlock(gmap->mm);
668 EXPORT_SYMBOL_GPL(gmap_fault);
671 * this function is assumed to be called with mmap_lock held
673 void __gmap_zap(struct gmap *gmap, unsigned long gaddr)
675 struct vm_area_struct *vma;
676 unsigned long vmaddr;
680 /* Find the vm address for the guest address */
681 vmaddr = (unsigned long) radix_tree_lookup(&gmap->guest_to_host,
684 vmaddr |= gaddr & ~PMD_MASK;
686 vma = vma_lookup(gmap->mm, vmaddr);
687 if (!vma || is_vm_hugetlb_page(vma))
690 /* Get pointer to the page table entry */
691 ptep = get_locked_pte(gmap->mm, vmaddr, &ptl);
693 ptep_zap_unused(gmap->mm, vmaddr, ptep, 0);
694 pte_unmap_unlock(ptep, ptl);
698 EXPORT_SYMBOL_GPL(__gmap_zap);
700 void gmap_discard(struct gmap *gmap, unsigned long from, unsigned long to)
702 unsigned long gaddr, vmaddr, size;
703 struct vm_area_struct *vma;
705 mmap_read_lock(gmap->mm);
706 for (gaddr = from; gaddr < to;
707 gaddr = (gaddr + PMD_SIZE) & PMD_MASK) {
708 /* Find the vm address for the guest address */
709 vmaddr = (unsigned long)
710 radix_tree_lookup(&gmap->guest_to_host,
714 vmaddr |= gaddr & ~PMD_MASK;
715 /* Find vma in the parent mm */
716 vma = find_vma(gmap->mm, vmaddr);
720 * We do not discard pages that are backed by
721 * hugetlbfs, so we don't have to refault them.
723 if (is_vm_hugetlb_page(vma))
725 size = min(to - gaddr, PMD_SIZE - (gaddr & ~PMD_MASK));
726 zap_page_range(vma, vmaddr, size);
728 mmap_read_unlock(gmap->mm);
730 EXPORT_SYMBOL_GPL(gmap_discard);
732 static LIST_HEAD(gmap_notifier_list);
733 static DEFINE_SPINLOCK(gmap_notifier_lock);
736 * gmap_register_pte_notifier - register a pte invalidation callback
737 * @nb: pointer to the gmap notifier block
739 void gmap_register_pte_notifier(struct gmap_notifier *nb)
741 spin_lock(&gmap_notifier_lock);
742 list_add_rcu(&nb->list, &gmap_notifier_list);
743 spin_unlock(&gmap_notifier_lock);
745 EXPORT_SYMBOL_GPL(gmap_register_pte_notifier);
748 * gmap_unregister_pte_notifier - remove a pte invalidation callback
749 * @nb: pointer to the gmap notifier block
751 void gmap_unregister_pte_notifier(struct gmap_notifier *nb)
753 spin_lock(&gmap_notifier_lock);
754 list_del_rcu(&nb->list);
755 spin_unlock(&gmap_notifier_lock);
758 EXPORT_SYMBOL_GPL(gmap_unregister_pte_notifier);
761 * gmap_call_notifier - call all registered invalidation callbacks
762 * @gmap: pointer to guest mapping meta data structure
763 * @start: start virtual address in the guest address space
764 * @end: end virtual address in the guest address space
766 static void gmap_call_notifier(struct gmap *gmap, unsigned long start,
769 struct gmap_notifier *nb;
771 list_for_each_entry(nb, &gmap_notifier_list, list)
772 nb->notifier_call(gmap, start, end);
776 * gmap_table_walk - walk the gmap page tables
777 * @gmap: pointer to guest mapping meta data structure
778 * @gaddr: virtual address in the guest address space
779 * @level: page table level to stop at
781 * Returns a table entry pointer for the given guest address and @level
782 * @level=0 : returns a pointer to a page table table entry (or NULL)
783 * @level=1 : returns a pointer to a segment table entry (or NULL)
784 * @level=2 : returns a pointer to a region-3 table entry (or NULL)
785 * @level=3 : returns a pointer to a region-2 table entry (or NULL)
786 * @level=4 : returns a pointer to a region-1 table entry (or NULL)
788 * Returns NULL if the gmap page tables could not be walked to the
791 * Note: Can also be called for shadow gmaps.
793 static inline unsigned long *gmap_table_walk(struct gmap *gmap,
794 unsigned long gaddr, int level)
796 const int asce_type = gmap->asce & _ASCE_TYPE_MASK;
797 unsigned long *table = gmap->table;
799 if (gmap_is_shadow(gmap) && gmap->removed)
802 if (WARN_ON_ONCE(level > (asce_type >> 2) + 1))
805 if (asce_type != _ASCE_TYPE_REGION1 &&
806 gaddr & (-1UL << (31 + (asce_type >> 2) * 11)))
810 case _ASCE_TYPE_REGION1:
811 table += (gaddr & _REGION1_INDEX) >> _REGION1_SHIFT;
814 if (*table & _REGION_ENTRY_INVALID)
816 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
818 case _ASCE_TYPE_REGION2:
819 table += (gaddr & _REGION2_INDEX) >> _REGION2_SHIFT;
822 if (*table & _REGION_ENTRY_INVALID)
824 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
826 case _ASCE_TYPE_REGION3:
827 table += (gaddr & _REGION3_INDEX) >> _REGION3_SHIFT;
830 if (*table & _REGION_ENTRY_INVALID)
832 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
834 case _ASCE_TYPE_SEGMENT:
835 table += (gaddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
838 if (*table & _REGION_ENTRY_INVALID)
840 table = (unsigned long *)(*table & _SEGMENT_ENTRY_ORIGIN);
841 table += (gaddr & _PAGE_INDEX) >> _PAGE_SHIFT;
847 * gmap_pte_op_walk - walk the gmap page table, get the page table lock
848 * and return the pte pointer
849 * @gmap: pointer to guest mapping meta data structure
850 * @gaddr: virtual address in the guest address space
851 * @ptl: pointer to the spinlock pointer
853 * Returns a pointer to the locked pte for a guest address, or NULL
855 static pte_t *gmap_pte_op_walk(struct gmap *gmap, unsigned long gaddr,
858 unsigned long *table;
860 BUG_ON(gmap_is_shadow(gmap));
861 /* Walk the gmap page table, lock and get pte pointer */
862 table = gmap_table_walk(gmap, gaddr, 1); /* get segment pointer */
863 if (!table || *table & _SEGMENT_ENTRY_INVALID)
865 return pte_alloc_map_lock(gmap->mm, (pmd_t *) table, gaddr, ptl);
869 * gmap_pte_op_fixup - force a page in and connect the gmap page table
870 * @gmap: pointer to guest mapping meta data structure
871 * @gaddr: virtual address in the guest address space
872 * @vmaddr: address in the host process address space
873 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
875 * Returns 0 if the caller can retry __gmap_translate (might fail again),
876 * -ENOMEM if out of memory and -EFAULT if anything goes wrong while fixing
877 * up or connecting the gmap page table.
879 static int gmap_pte_op_fixup(struct gmap *gmap, unsigned long gaddr,
880 unsigned long vmaddr, int prot)
882 struct mm_struct *mm = gmap->mm;
883 unsigned int fault_flags;
884 bool unlocked = false;
886 BUG_ON(gmap_is_shadow(gmap));
887 fault_flags = (prot == PROT_WRITE) ? FAULT_FLAG_WRITE : 0;
888 if (fixup_user_fault(mm, vmaddr, fault_flags, &unlocked))
891 /* lost mmap_lock, caller has to retry __gmap_translate */
893 /* Connect the page tables */
894 return __gmap_link(gmap, gaddr, vmaddr);
898 * gmap_pte_op_end - release the page table lock
899 * @ptl: pointer to the spinlock pointer
901 static void gmap_pte_op_end(spinlock_t *ptl)
908 * gmap_pmd_op_walk - walk the gmap tables, get the guest table lock
909 * and return the pmd pointer
910 * @gmap: pointer to guest mapping meta data structure
911 * @gaddr: virtual address in the guest address space
913 * Returns a pointer to the pmd for a guest address, or NULL
915 static inline pmd_t *gmap_pmd_op_walk(struct gmap *gmap, unsigned long gaddr)
919 BUG_ON(gmap_is_shadow(gmap));
920 pmdp = (pmd_t *) gmap_table_walk(gmap, gaddr, 1);
924 /* without huge pages, there is no need to take the table lock */
925 if (!gmap->mm->context.allow_gmap_hpage_1m)
926 return pmd_none(*pmdp) ? NULL : pmdp;
928 spin_lock(&gmap->guest_table_lock);
929 if (pmd_none(*pmdp)) {
930 spin_unlock(&gmap->guest_table_lock);
934 /* 4k page table entries are locked via the pte (pte_alloc_map_lock). */
935 if (!pmd_large(*pmdp))
936 spin_unlock(&gmap->guest_table_lock);
941 * gmap_pmd_op_end - release the guest_table_lock if needed
942 * @gmap: pointer to the guest mapping meta data structure
943 * @pmdp: pointer to the pmd
945 static inline void gmap_pmd_op_end(struct gmap *gmap, pmd_t *pmdp)
947 if (pmd_large(*pmdp))
948 spin_unlock(&gmap->guest_table_lock);
952 * gmap_protect_pmd - remove access rights to memory and set pmd notification bits
953 * @pmdp: pointer to the pmd to be protected
954 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
955 * @bits: notification bits to set
958 * 0 if successfully protected
959 * -EAGAIN if a fixup is needed
960 * -EINVAL if unsupported notifier bits have been specified
962 * Expected to be called with sg->mm->mmap_lock in read and
963 * guest_table_lock held.
965 static int gmap_protect_pmd(struct gmap *gmap, unsigned long gaddr,
966 pmd_t *pmdp, int prot, unsigned long bits)
968 int pmd_i = pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID;
969 int pmd_p = pmd_val(*pmdp) & _SEGMENT_ENTRY_PROTECT;
973 if ((pmd_i && (prot != PROT_NONE)) || (pmd_p && (prot == PROT_WRITE)))
976 if (prot == PROT_NONE && !pmd_i) {
977 new = set_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_INVALID));
978 gmap_pmdp_xchg(gmap, pmdp, new, gaddr);
981 if (prot == PROT_READ && !pmd_p) {
982 new = clear_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_INVALID));
983 new = set_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_PROTECT));
984 gmap_pmdp_xchg(gmap, pmdp, new, gaddr);
987 if (bits & GMAP_NOTIFY_MPROT)
988 set_pmd(pmdp, set_pmd_bit(*pmdp, __pgprot(_SEGMENT_ENTRY_GMAP_IN)));
990 /* Shadow GMAP protection needs split PMDs */
991 if (bits & GMAP_NOTIFY_SHADOW)
998 * gmap_protect_pte - remove access rights to memory and set pgste bits
999 * @gmap: pointer to guest mapping meta data structure
1000 * @gaddr: virtual address in the guest address space
1001 * @pmdp: pointer to the pmd associated with the pte
1002 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
1003 * @bits: notification bits to set
1005 * Returns 0 if successfully protected, -ENOMEM if out of memory and
1006 * -EAGAIN if a fixup is needed.
1008 * Expected to be called with sg->mm->mmap_lock in read
1010 static int gmap_protect_pte(struct gmap *gmap, unsigned long gaddr,
1011 pmd_t *pmdp, int prot, unsigned long bits)
1015 spinlock_t *ptl = NULL;
1016 unsigned long pbits = 0;
1018 if (pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID)
1021 ptep = pte_alloc_map_lock(gmap->mm, pmdp, gaddr, &ptl);
1025 pbits |= (bits & GMAP_NOTIFY_MPROT) ? PGSTE_IN_BIT : 0;
1026 pbits |= (bits & GMAP_NOTIFY_SHADOW) ? PGSTE_VSIE_BIT : 0;
1027 /* Protect and unlock. */
1028 rc = ptep_force_prot(gmap->mm, gaddr, ptep, prot, pbits);
1029 gmap_pte_op_end(ptl);
1034 * gmap_protect_range - remove access rights to memory and set pgste bits
1035 * @gmap: pointer to guest mapping meta data structure
1036 * @gaddr: virtual address in the guest address space
1037 * @len: size of area
1038 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
1039 * @bits: pgste notification bits to set
1041 * Returns 0 if successfully protected, -ENOMEM if out of memory and
1042 * -EFAULT if gaddr is invalid (or mapping for shadows is missing).
1044 * Called with sg->mm->mmap_lock in read.
1046 static int gmap_protect_range(struct gmap *gmap, unsigned long gaddr,
1047 unsigned long len, int prot, unsigned long bits)
1049 unsigned long vmaddr, dist;
1053 BUG_ON(gmap_is_shadow(gmap));
1056 pmdp = gmap_pmd_op_walk(gmap, gaddr);
1058 if (!pmd_large(*pmdp)) {
1059 rc = gmap_protect_pte(gmap, gaddr, pmdp, prot,
1066 rc = gmap_protect_pmd(gmap, gaddr, pmdp, prot,
1069 dist = HPAGE_SIZE - (gaddr & ~HPAGE_MASK);
1070 len = len < dist ? 0 : len - dist;
1071 gaddr = (gaddr & HPAGE_MASK) + HPAGE_SIZE;
1074 gmap_pmd_op_end(gmap, pmdp);
1080 /* -EAGAIN, fixup of userspace mm and gmap */
1081 vmaddr = __gmap_translate(gmap, gaddr);
1082 if (IS_ERR_VALUE(vmaddr))
1084 rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr, prot);
1093 * gmap_mprotect_notify - change access rights for a range of ptes and
1094 * call the notifier if any pte changes again
1095 * @gmap: pointer to guest mapping meta data structure
1096 * @gaddr: virtual address in the guest address space
1097 * @len: size of area
1098 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
1100 * Returns 0 if for each page in the given range a gmap mapping exists,
1101 * the new access rights could be set and the notifier could be armed.
1102 * If the gmap mapping is missing for one or more pages -EFAULT is
1103 * returned. If no memory could be allocated -ENOMEM is returned.
1104 * This function establishes missing page table entries.
1106 int gmap_mprotect_notify(struct gmap *gmap, unsigned long gaddr,
1107 unsigned long len, int prot)
1111 if ((gaddr & ~PAGE_MASK) || (len & ~PAGE_MASK) || gmap_is_shadow(gmap))
1113 if (!MACHINE_HAS_ESOP && prot == PROT_READ)
1115 mmap_read_lock(gmap->mm);
1116 rc = gmap_protect_range(gmap, gaddr, len, prot, GMAP_NOTIFY_MPROT);
1117 mmap_read_unlock(gmap->mm);
1120 EXPORT_SYMBOL_GPL(gmap_mprotect_notify);
1123 * gmap_read_table - get an unsigned long value from a guest page table using
1124 * absolute addressing, without marking the page referenced.
1125 * @gmap: pointer to guest mapping meta data structure
1126 * @gaddr: virtual address in the guest address space
1127 * @val: pointer to the unsigned long value to return
1129 * Returns 0 if the value was read, -ENOMEM if out of memory and -EFAULT
1130 * if reading using the virtual address failed. -EINVAL if called on a gmap
1133 * Called with gmap->mm->mmap_lock in read.
1135 int gmap_read_table(struct gmap *gmap, unsigned long gaddr, unsigned long *val)
1137 unsigned long address, vmaddr;
1142 if (gmap_is_shadow(gmap))
1147 ptep = gmap_pte_op_walk(gmap, gaddr, &ptl);
1150 if (pte_present(pte) && (pte_val(pte) & _PAGE_READ)) {
1151 address = pte_val(pte) & PAGE_MASK;
1152 address += gaddr & ~PAGE_MASK;
1153 *val = *(unsigned long *) address;
1154 set_pte(ptep, set_pte_bit(*ptep, __pgprot(_PAGE_YOUNG)));
1155 /* Do *NOT* clear the _PAGE_INVALID bit! */
1158 gmap_pte_op_end(ptl);
1162 vmaddr = __gmap_translate(gmap, gaddr);
1163 if (IS_ERR_VALUE(vmaddr)) {
1167 rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr, PROT_READ);
1173 EXPORT_SYMBOL_GPL(gmap_read_table);
1176 * gmap_insert_rmap - add a rmap to the host_to_rmap radix tree
1177 * @sg: pointer to the shadow guest address space structure
1178 * @vmaddr: vm address associated with the rmap
1179 * @rmap: pointer to the rmap structure
1181 * Called with the sg->guest_table_lock
1183 static inline void gmap_insert_rmap(struct gmap *sg, unsigned long vmaddr,
1184 struct gmap_rmap *rmap)
1186 struct gmap_rmap *temp;
1189 BUG_ON(!gmap_is_shadow(sg));
1190 slot = radix_tree_lookup_slot(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT);
1192 rmap->next = radix_tree_deref_slot_protected(slot,
1193 &sg->guest_table_lock);
1194 for (temp = rmap->next; temp; temp = temp->next) {
1195 if (temp->raddr == rmap->raddr) {
1200 radix_tree_replace_slot(&sg->host_to_rmap, slot, rmap);
1203 radix_tree_insert(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT,
1209 * gmap_protect_rmap - restrict access rights to memory (RO) and create an rmap
1210 * @sg: pointer to the shadow guest address space structure
1211 * @raddr: rmap address in the shadow gmap
1212 * @paddr: address in the parent guest address space
1213 * @len: length of the memory area to protect
1215 * Returns 0 if successfully protected and the rmap was created, -ENOMEM
1216 * if out of memory and -EFAULT if paddr is invalid.
1218 static int gmap_protect_rmap(struct gmap *sg, unsigned long raddr,
1219 unsigned long paddr, unsigned long len)
1221 struct gmap *parent;
1222 struct gmap_rmap *rmap;
1223 unsigned long vmaddr;
1228 BUG_ON(!gmap_is_shadow(sg));
1229 parent = sg->parent;
1231 vmaddr = __gmap_translate(parent, paddr);
1232 if (IS_ERR_VALUE(vmaddr))
1234 rmap = kzalloc(sizeof(*rmap), GFP_KERNEL_ACCOUNT);
1237 rmap->raddr = raddr;
1238 rc = radix_tree_preload(GFP_KERNEL_ACCOUNT);
1244 ptep = gmap_pte_op_walk(parent, paddr, &ptl);
1246 spin_lock(&sg->guest_table_lock);
1247 rc = ptep_force_prot(parent->mm, paddr, ptep, PROT_READ,
1250 gmap_insert_rmap(sg, vmaddr, rmap);
1251 spin_unlock(&sg->guest_table_lock);
1252 gmap_pte_op_end(ptl);
1254 radix_tree_preload_end();
1257 rc = gmap_pte_op_fixup(parent, paddr, vmaddr, PROT_READ);
1268 #define _SHADOW_RMAP_MASK 0x7
1269 #define _SHADOW_RMAP_REGION1 0x5
1270 #define _SHADOW_RMAP_REGION2 0x4
1271 #define _SHADOW_RMAP_REGION3 0x3
1272 #define _SHADOW_RMAP_SEGMENT 0x2
1273 #define _SHADOW_RMAP_PGTABLE 0x1
1276 * gmap_idte_one - invalidate a single region or segment table entry
1277 * @asce: region or segment table *origin* + table-type bits
1278 * @vaddr: virtual address to identify the table entry to flush
1280 * The invalid bit of a single region or segment table entry is set
1281 * and the associated TLB entries depending on the entry are flushed.
1282 * The table-type of the @asce identifies the portion of the @vaddr
1283 * that is used as the invalidation index.
1285 static inline void gmap_idte_one(unsigned long asce, unsigned long vaddr)
1289 : : "a" (asce), "a" (vaddr) : "cc", "memory");
1293 * gmap_unshadow_page - remove a page from a shadow page table
1294 * @sg: pointer to the shadow guest address space structure
1295 * @raddr: rmap address in the shadow guest address space
1297 * Called with the sg->guest_table_lock
1299 static void gmap_unshadow_page(struct gmap *sg, unsigned long raddr)
1301 unsigned long *table;
1303 BUG_ON(!gmap_is_shadow(sg));
1304 table = gmap_table_walk(sg, raddr, 0); /* get page table pointer */
1305 if (!table || *table & _PAGE_INVALID)
1307 gmap_call_notifier(sg, raddr, raddr + _PAGE_SIZE - 1);
1308 ptep_unshadow_pte(sg->mm, raddr, (pte_t *) table);
1312 * __gmap_unshadow_pgt - remove all entries from a shadow page table
1313 * @sg: pointer to the shadow guest address space structure
1314 * @raddr: rmap address in the shadow guest address space
1315 * @pgt: pointer to the start of a shadow page table
1317 * Called with the sg->guest_table_lock
1319 static void __gmap_unshadow_pgt(struct gmap *sg, unsigned long raddr,
1324 BUG_ON(!gmap_is_shadow(sg));
1325 for (i = 0; i < _PAGE_ENTRIES; i++, raddr += _PAGE_SIZE)
1326 pgt[i] = _PAGE_INVALID;
1330 * gmap_unshadow_pgt - remove a shadow page table from a segment entry
1331 * @sg: pointer to the shadow guest address space structure
1332 * @raddr: address in the shadow guest address space
1334 * Called with the sg->guest_table_lock
1336 static void gmap_unshadow_pgt(struct gmap *sg, unsigned long raddr)
1338 unsigned long sto, *ste, *pgt;
1341 BUG_ON(!gmap_is_shadow(sg));
1342 ste = gmap_table_walk(sg, raddr, 1); /* get segment pointer */
1343 if (!ste || !(*ste & _SEGMENT_ENTRY_ORIGIN))
1345 gmap_call_notifier(sg, raddr, raddr + _SEGMENT_SIZE - 1);
1346 sto = (unsigned long) (ste - ((raddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT));
1347 gmap_idte_one(sto | _ASCE_TYPE_SEGMENT, raddr);
1348 pgt = (unsigned long *)(*ste & _SEGMENT_ENTRY_ORIGIN);
1349 *ste = _SEGMENT_ENTRY_EMPTY;
1350 __gmap_unshadow_pgt(sg, raddr, pgt);
1351 /* Free page table */
1352 page = pfn_to_page(__pa(pgt) >> PAGE_SHIFT);
1353 list_del(&page->lru);
1354 page_table_free_pgste(page);
1358 * __gmap_unshadow_sgt - remove all entries from a shadow segment table
1359 * @sg: pointer to the shadow guest address space structure
1360 * @raddr: rmap address in the shadow guest address space
1361 * @sgt: pointer to the start of a shadow segment table
1363 * Called with the sg->guest_table_lock
1365 static void __gmap_unshadow_sgt(struct gmap *sg, unsigned long raddr,
1372 BUG_ON(!gmap_is_shadow(sg));
1373 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _SEGMENT_SIZE) {
1374 if (!(sgt[i] & _SEGMENT_ENTRY_ORIGIN))
1376 pgt = (unsigned long *)(sgt[i] & _REGION_ENTRY_ORIGIN);
1377 sgt[i] = _SEGMENT_ENTRY_EMPTY;
1378 __gmap_unshadow_pgt(sg, raddr, pgt);
1379 /* Free page table */
1380 page = pfn_to_page(__pa(pgt) >> PAGE_SHIFT);
1381 list_del(&page->lru);
1382 page_table_free_pgste(page);
1387 * gmap_unshadow_sgt - remove a shadow segment table from a region-3 entry
1388 * @sg: pointer to the shadow guest address space structure
1389 * @raddr: rmap address in the shadow guest address space
1391 * Called with the shadow->guest_table_lock
1393 static void gmap_unshadow_sgt(struct gmap *sg, unsigned long raddr)
1395 unsigned long r3o, *r3e, *sgt;
1398 BUG_ON(!gmap_is_shadow(sg));
1399 r3e = gmap_table_walk(sg, raddr, 2); /* get region-3 pointer */
1400 if (!r3e || !(*r3e & _REGION_ENTRY_ORIGIN))
1402 gmap_call_notifier(sg, raddr, raddr + _REGION3_SIZE - 1);
1403 r3o = (unsigned long) (r3e - ((raddr & _REGION3_INDEX) >> _REGION3_SHIFT));
1404 gmap_idte_one(r3o | _ASCE_TYPE_REGION3, raddr);
1405 sgt = (unsigned long *)(*r3e & _REGION_ENTRY_ORIGIN);
1406 *r3e = _REGION3_ENTRY_EMPTY;
1407 __gmap_unshadow_sgt(sg, raddr, sgt);
1408 /* Free segment table */
1409 page = pfn_to_page(__pa(sgt) >> PAGE_SHIFT);
1410 list_del(&page->lru);
1411 __free_pages(page, CRST_ALLOC_ORDER);
1415 * __gmap_unshadow_r3t - remove all entries from a shadow region-3 table
1416 * @sg: pointer to the shadow guest address space structure
1417 * @raddr: address in the shadow guest address space
1418 * @r3t: pointer to the start of a shadow region-3 table
1420 * Called with the sg->guest_table_lock
1422 static void __gmap_unshadow_r3t(struct gmap *sg, unsigned long raddr,
1429 BUG_ON(!gmap_is_shadow(sg));
1430 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION3_SIZE) {
1431 if (!(r3t[i] & _REGION_ENTRY_ORIGIN))
1433 sgt = (unsigned long *)(r3t[i] & _REGION_ENTRY_ORIGIN);
1434 r3t[i] = _REGION3_ENTRY_EMPTY;
1435 __gmap_unshadow_sgt(sg, raddr, sgt);
1436 /* Free segment table */
1437 page = pfn_to_page(__pa(sgt) >> PAGE_SHIFT);
1438 list_del(&page->lru);
1439 __free_pages(page, CRST_ALLOC_ORDER);
1444 * gmap_unshadow_r3t - remove a shadow region-3 table from a region-2 entry
1445 * @sg: pointer to the shadow guest address space structure
1446 * @raddr: rmap address in the shadow guest address space
1448 * Called with the sg->guest_table_lock
1450 static void gmap_unshadow_r3t(struct gmap *sg, unsigned long raddr)
1452 unsigned long r2o, *r2e, *r3t;
1455 BUG_ON(!gmap_is_shadow(sg));
1456 r2e = gmap_table_walk(sg, raddr, 3); /* get region-2 pointer */
1457 if (!r2e || !(*r2e & _REGION_ENTRY_ORIGIN))
1459 gmap_call_notifier(sg, raddr, raddr + _REGION2_SIZE - 1);
1460 r2o = (unsigned long) (r2e - ((raddr & _REGION2_INDEX) >> _REGION2_SHIFT));
1461 gmap_idte_one(r2o | _ASCE_TYPE_REGION2, raddr);
1462 r3t = (unsigned long *)(*r2e & _REGION_ENTRY_ORIGIN);
1463 *r2e = _REGION2_ENTRY_EMPTY;
1464 __gmap_unshadow_r3t(sg, raddr, r3t);
1465 /* Free region 3 table */
1466 page = pfn_to_page(__pa(r3t) >> PAGE_SHIFT);
1467 list_del(&page->lru);
1468 __free_pages(page, CRST_ALLOC_ORDER);
1472 * __gmap_unshadow_r2t - remove all entries from a shadow region-2 table
1473 * @sg: pointer to the shadow guest address space structure
1474 * @raddr: rmap address in the shadow guest address space
1475 * @r2t: pointer to the start of a shadow region-2 table
1477 * Called with the sg->guest_table_lock
1479 static void __gmap_unshadow_r2t(struct gmap *sg, unsigned long raddr,
1486 BUG_ON(!gmap_is_shadow(sg));
1487 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION2_SIZE) {
1488 if (!(r2t[i] & _REGION_ENTRY_ORIGIN))
1490 r3t = (unsigned long *)(r2t[i] & _REGION_ENTRY_ORIGIN);
1491 r2t[i] = _REGION2_ENTRY_EMPTY;
1492 __gmap_unshadow_r3t(sg, raddr, r3t);
1493 /* Free region 3 table */
1494 page = pfn_to_page(__pa(r3t) >> PAGE_SHIFT);
1495 list_del(&page->lru);
1496 __free_pages(page, CRST_ALLOC_ORDER);
1501 * gmap_unshadow_r2t - remove a shadow region-2 table from a region-1 entry
1502 * @sg: pointer to the shadow guest address space structure
1503 * @raddr: rmap address in the shadow guest address space
1505 * Called with the sg->guest_table_lock
1507 static void gmap_unshadow_r2t(struct gmap *sg, unsigned long raddr)
1509 unsigned long r1o, *r1e, *r2t;
1512 BUG_ON(!gmap_is_shadow(sg));
1513 r1e = gmap_table_walk(sg, raddr, 4); /* get region-1 pointer */
1514 if (!r1e || !(*r1e & _REGION_ENTRY_ORIGIN))
1516 gmap_call_notifier(sg, raddr, raddr + _REGION1_SIZE - 1);
1517 r1o = (unsigned long) (r1e - ((raddr & _REGION1_INDEX) >> _REGION1_SHIFT));
1518 gmap_idte_one(r1o | _ASCE_TYPE_REGION1, raddr);
1519 r2t = (unsigned long *)(*r1e & _REGION_ENTRY_ORIGIN);
1520 *r1e = _REGION1_ENTRY_EMPTY;
1521 __gmap_unshadow_r2t(sg, raddr, r2t);
1522 /* Free region 2 table */
1523 page = pfn_to_page(__pa(r2t) >> PAGE_SHIFT);
1524 list_del(&page->lru);
1525 __free_pages(page, CRST_ALLOC_ORDER);
1529 * __gmap_unshadow_r1t - remove all entries from a shadow region-1 table
1530 * @sg: pointer to the shadow guest address space structure
1531 * @raddr: rmap address in the shadow guest address space
1532 * @r1t: pointer to the start of a shadow region-1 table
1534 * Called with the shadow->guest_table_lock
1536 static void __gmap_unshadow_r1t(struct gmap *sg, unsigned long raddr,
1539 unsigned long asce, *r2t;
1543 BUG_ON(!gmap_is_shadow(sg));
1544 asce = (unsigned long) r1t | _ASCE_TYPE_REGION1;
1545 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION1_SIZE) {
1546 if (!(r1t[i] & _REGION_ENTRY_ORIGIN))
1548 r2t = (unsigned long *)(r1t[i] & _REGION_ENTRY_ORIGIN);
1549 __gmap_unshadow_r2t(sg, raddr, r2t);
1550 /* Clear entry and flush translation r1t -> r2t */
1551 gmap_idte_one(asce, raddr);
1552 r1t[i] = _REGION1_ENTRY_EMPTY;
1553 /* Free region 2 table */
1554 page = pfn_to_page(__pa(r2t) >> PAGE_SHIFT);
1555 list_del(&page->lru);
1556 __free_pages(page, CRST_ALLOC_ORDER);
1561 * gmap_unshadow - remove a shadow page table completely
1562 * @sg: pointer to the shadow guest address space structure
1564 * Called with sg->guest_table_lock
1566 static void gmap_unshadow(struct gmap *sg)
1568 unsigned long *table;
1570 BUG_ON(!gmap_is_shadow(sg));
1574 gmap_call_notifier(sg, 0, -1UL);
1576 table = (unsigned long *)(sg->asce & _ASCE_ORIGIN);
1577 switch (sg->asce & _ASCE_TYPE_MASK) {
1578 case _ASCE_TYPE_REGION1:
1579 __gmap_unshadow_r1t(sg, 0, table);
1581 case _ASCE_TYPE_REGION2:
1582 __gmap_unshadow_r2t(sg, 0, table);
1584 case _ASCE_TYPE_REGION3:
1585 __gmap_unshadow_r3t(sg, 0, table);
1587 case _ASCE_TYPE_SEGMENT:
1588 __gmap_unshadow_sgt(sg, 0, table);
1594 * gmap_find_shadow - find a specific asce in the list of shadow tables
1595 * @parent: pointer to the parent gmap
1596 * @asce: ASCE for which the shadow table is created
1597 * @edat_level: edat level to be used for the shadow translation
1599 * Returns the pointer to a gmap if a shadow table with the given asce is
1600 * already available, ERR_PTR(-EAGAIN) if another one is just being created,
1603 static struct gmap *gmap_find_shadow(struct gmap *parent, unsigned long asce,
1608 list_for_each_entry(sg, &parent->children, list) {
1609 if (sg->orig_asce != asce || sg->edat_level != edat_level ||
1612 if (!sg->initialized)
1613 return ERR_PTR(-EAGAIN);
1614 refcount_inc(&sg->ref_count);
1621 * gmap_shadow_valid - check if a shadow guest address space matches the
1622 * given properties and is still valid
1623 * @sg: pointer to the shadow guest address space structure
1624 * @asce: ASCE for which the shadow table is requested
1625 * @edat_level: edat level to be used for the shadow translation
1627 * Returns 1 if the gmap shadow is still valid and matches the given
1628 * properties, the caller can continue using it. Returns 0 otherwise, the
1629 * caller has to request a new shadow gmap in this case.
1632 int gmap_shadow_valid(struct gmap *sg, unsigned long asce, int edat_level)
1636 return sg->orig_asce == asce && sg->edat_level == edat_level;
1638 EXPORT_SYMBOL_GPL(gmap_shadow_valid);
1641 * gmap_shadow - create/find a shadow guest address space
1642 * @parent: pointer to the parent gmap
1643 * @asce: ASCE for which the shadow table is created
1644 * @edat_level: edat level to be used for the shadow translation
1646 * The pages of the top level page table referred by the asce parameter
1647 * will be set to read-only and marked in the PGSTEs of the kvm process.
1648 * The shadow table will be removed automatically on any change to the
1649 * PTE mapping for the source table.
1651 * Returns a guest address space structure, ERR_PTR(-ENOMEM) if out of memory,
1652 * ERR_PTR(-EAGAIN) if the caller has to retry and ERR_PTR(-EFAULT) if the
1653 * parent gmap table could not be protected.
1655 struct gmap *gmap_shadow(struct gmap *parent, unsigned long asce,
1658 struct gmap *sg, *new;
1659 unsigned long limit;
1662 BUG_ON(parent->mm->context.allow_gmap_hpage_1m);
1663 BUG_ON(gmap_is_shadow(parent));
1664 spin_lock(&parent->shadow_lock);
1665 sg = gmap_find_shadow(parent, asce, edat_level);
1666 spin_unlock(&parent->shadow_lock);
1669 /* Create a new shadow gmap */
1670 limit = -1UL >> (33 - (((asce & _ASCE_TYPE_MASK) >> 2) * 11));
1671 if (asce & _ASCE_REAL_SPACE)
1673 new = gmap_alloc(limit);
1675 return ERR_PTR(-ENOMEM);
1676 new->mm = parent->mm;
1677 new->parent = gmap_get(parent);
1678 new->orig_asce = asce;
1679 new->edat_level = edat_level;
1680 new->initialized = false;
1681 spin_lock(&parent->shadow_lock);
1682 /* Recheck if another CPU created the same shadow */
1683 sg = gmap_find_shadow(parent, asce, edat_level);
1685 spin_unlock(&parent->shadow_lock);
1689 if (asce & _ASCE_REAL_SPACE) {
1690 /* only allow one real-space gmap shadow */
1691 list_for_each_entry(sg, &parent->children, list) {
1692 if (sg->orig_asce & _ASCE_REAL_SPACE) {
1693 spin_lock(&sg->guest_table_lock);
1695 spin_unlock(&sg->guest_table_lock);
1696 list_del(&sg->list);
1702 refcount_set(&new->ref_count, 2);
1703 list_add(&new->list, &parent->children);
1704 if (asce & _ASCE_REAL_SPACE) {
1705 /* nothing to protect, return right away */
1706 new->initialized = true;
1707 spin_unlock(&parent->shadow_lock);
1710 spin_unlock(&parent->shadow_lock);
1711 /* protect after insertion, so it will get properly invalidated */
1712 mmap_read_lock(parent->mm);
1713 rc = gmap_protect_range(parent, asce & _ASCE_ORIGIN,
1714 ((asce & _ASCE_TABLE_LENGTH) + 1) * PAGE_SIZE,
1715 PROT_READ, GMAP_NOTIFY_SHADOW);
1716 mmap_read_unlock(parent->mm);
1717 spin_lock(&parent->shadow_lock);
1718 new->initialized = true;
1720 list_del(&new->list);
1724 spin_unlock(&parent->shadow_lock);
1727 EXPORT_SYMBOL_GPL(gmap_shadow);
1730 * gmap_shadow_r2t - create an empty shadow region 2 table
1731 * @sg: pointer to the shadow guest address space structure
1732 * @saddr: faulting address in the shadow gmap
1733 * @r2t: parent gmap address of the region 2 table to get shadowed
1734 * @fake: r2t references contiguous guest memory block, not a r2t
1736 * The r2t parameter specifies the address of the source table. The
1737 * four pages of the source table are made read-only in the parent gmap
1738 * address space. A write to the source table area @r2t will automatically
1739 * remove the shadow r2 table and all of its decendents.
1741 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
1742 * shadow table structure is incomplete, -ENOMEM if out of memory and
1743 * -EFAULT if an address in the parent gmap could not be resolved.
1745 * Called with sg->mm->mmap_lock in read.
1747 int gmap_shadow_r2t(struct gmap *sg, unsigned long saddr, unsigned long r2t,
1750 unsigned long raddr, origin, offset, len;
1751 unsigned long *s_r2t, *table;
1755 BUG_ON(!gmap_is_shadow(sg));
1756 /* Allocate a shadow region second table */
1757 page = alloc_pages(GFP_KERNEL_ACCOUNT, CRST_ALLOC_ORDER);
1760 page->index = r2t & _REGION_ENTRY_ORIGIN;
1762 page->index |= GMAP_SHADOW_FAKE_TABLE;
1763 s_r2t = (unsigned long *) page_to_phys(page);
1764 /* Install shadow region second table */
1765 spin_lock(&sg->guest_table_lock);
1766 table = gmap_table_walk(sg, saddr, 4); /* get region-1 pointer */
1768 rc = -EAGAIN; /* Race with unshadow */
1771 if (!(*table & _REGION_ENTRY_INVALID)) {
1772 rc = 0; /* Already established */
1774 } else if (*table & _REGION_ENTRY_ORIGIN) {
1775 rc = -EAGAIN; /* Race with shadow */
1778 crst_table_init(s_r2t, _REGION2_ENTRY_EMPTY);
1779 /* mark as invalid as long as the parent table is not protected */
1780 *table = (unsigned long) s_r2t | _REGION_ENTRY_LENGTH |
1781 _REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_INVALID;
1782 if (sg->edat_level >= 1)
1783 *table |= (r2t & _REGION_ENTRY_PROTECT);
1784 list_add(&page->lru, &sg->crst_list);
1786 /* nothing to protect for fake tables */
1787 *table &= ~_REGION_ENTRY_INVALID;
1788 spin_unlock(&sg->guest_table_lock);
1791 spin_unlock(&sg->guest_table_lock);
1792 /* Make r2t read-only in parent gmap page table */
1793 raddr = (saddr & _REGION1_MASK) | _SHADOW_RMAP_REGION1;
1794 origin = r2t & _REGION_ENTRY_ORIGIN;
1795 offset = ((r2t & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
1796 len = ((r2t & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
1797 rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
1798 spin_lock(&sg->guest_table_lock);
1800 table = gmap_table_walk(sg, saddr, 4);
1801 if (!table || (*table & _REGION_ENTRY_ORIGIN) !=
1802 (unsigned long) s_r2t)
1803 rc = -EAGAIN; /* Race with unshadow */
1805 *table &= ~_REGION_ENTRY_INVALID;
1807 gmap_unshadow_r2t(sg, raddr);
1809 spin_unlock(&sg->guest_table_lock);
1812 spin_unlock(&sg->guest_table_lock);
1813 __free_pages(page, CRST_ALLOC_ORDER);
1816 EXPORT_SYMBOL_GPL(gmap_shadow_r2t);
1819 * gmap_shadow_r3t - create a shadow region 3 table
1820 * @sg: pointer to the shadow guest address space structure
1821 * @saddr: faulting address in the shadow gmap
1822 * @r3t: parent gmap address of the region 3 table to get shadowed
1823 * @fake: r3t references contiguous guest memory block, not a r3t
1825 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
1826 * shadow table structure is incomplete, -ENOMEM if out of memory and
1827 * -EFAULT if an address in the parent gmap could not be resolved.
1829 * Called with sg->mm->mmap_lock in read.
1831 int gmap_shadow_r3t(struct gmap *sg, unsigned long saddr, unsigned long r3t,
1834 unsigned long raddr, origin, offset, len;
1835 unsigned long *s_r3t, *table;
1839 BUG_ON(!gmap_is_shadow(sg));
1840 /* Allocate a shadow region second table */
1841 page = alloc_pages(GFP_KERNEL_ACCOUNT, CRST_ALLOC_ORDER);
1844 page->index = r3t & _REGION_ENTRY_ORIGIN;
1846 page->index |= GMAP_SHADOW_FAKE_TABLE;
1847 s_r3t = (unsigned long *) page_to_phys(page);
1848 /* Install shadow region second table */
1849 spin_lock(&sg->guest_table_lock);
1850 table = gmap_table_walk(sg, saddr, 3); /* get region-2 pointer */
1852 rc = -EAGAIN; /* Race with unshadow */
1855 if (!(*table & _REGION_ENTRY_INVALID)) {
1856 rc = 0; /* Already established */
1858 } else if (*table & _REGION_ENTRY_ORIGIN) {
1859 rc = -EAGAIN; /* Race with shadow */
1862 crst_table_init(s_r3t, _REGION3_ENTRY_EMPTY);
1863 /* mark as invalid as long as the parent table is not protected */
1864 *table = (unsigned long) s_r3t | _REGION_ENTRY_LENGTH |
1865 _REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_INVALID;
1866 if (sg->edat_level >= 1)
1867 *table |= (r3t & _REGION_ENTRY_PROTECT);
1868 list_add(&page->lru, &sg->crst_list);
1870 /* nothing to protect for fake tables */
1871 *table &= ~_REGION_ENTRY_INVALID;
1872 spin_unlock(&sg->guest_table_lock);
1875 spin_unlock(&sg->guest_table_lock);
1876 /* Make r3t read-only in parent gmap page table */
1877 raddr = (saddr & _REGION2_MASK) | _SHADOW_RMAP_REGION2;
1878 origin = r3t & _REGION_ENTRY_ORIGIN;
1879 offset = ((r3t & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
1880 len = ((r3t & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
1881 rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
1882 spin_lock(&sg->guest_table_lock);
1884 table = gmap_table_walk(sg, saddr, 3);
1885 if (!table || (*table & _REGION_ENTRY_ORIGIN) !=
1886 (unsigned long) s_r3t)
1887 rc = -EAGAIN; /* Race with unshadow */
1889 *table &= ~_REGION_ENTRY_INVALID;
1891 gmap_unshadow_r3t(sg, raddr);
1893 spin_unlock(&sg->guest_table_lock);
1896 spin_unlock(&sg->guest_table_lock);
1897 __free_pages(page, CRST_ALLOC_ORDER);
1900 EXPORT_SYMBOL_GPL(gmap_shadow_r3t);
1903 * gmap_shadow_sgt - create a shadow segment table
1904 * @sg: pointer to the shadow guest address space structure
1905 * @saddr: faulting address in the shadow gmap
1906 * @sgt: parent gmap address of the segment table to get shadowed
1907 * @fake: sgt references contiguous guest memory block, not a sgt
1909 * Returns: 0 if successfully shadowed or already shadowed, -EAGAIN if the
1910 * shadow table structure is incomplete, -ENOMEM if out of memory and
1911 * -EFAULT if an address in the parent gmap could not be resolved.
1913 * Called with sg->mm->mmap_lock in read.
1915 int gmap_shadow_sgt(struct gmap *sg, unsigned long saddr, unsigned long sgt,
1918 unsigned long raddr, origin, offset, len;
1919 unsigned long *s_sgt, *table;
1923 BUG_ON(!gmap_is_shadow(sg) || (sgt & _REGION3_ENTRY_LARGE));
1924 /* Allocate a shadow segment table */
1925 page = alloc_pages(GFP_KERNEL_ACCOUNT, CRST_ALLOC_ORDER);
1928 page->index = sgt & _REGION_ENTRY_ORIGIN;
1930 page->index |= GMAP_SHADOW_FAKE_TABLE;
1931 s_sgt = (unsigned long *) page_to_phys(page);
1932 /* Install shadow region second table */
1933 spin_lock(&sg->guest_table_lock);
1934 table = gmap_table_walk(sg, saddr, 2); /* get region-3 pointer */
1936 rc = -EAGAIN; /* Race with unshadow */
1939 if (!(*table & _REGION_ENTRY_INVALID)) {
1940 rc = 0; /* Already established */
1942 } else if (*table & _REGION_ENTRY_ORIGIN) {
1943 rc = -EAGAIN; /* Race with shadow */
1946 crst_table_init(s_sgt, _SEGMENT_ENTRY_EMPTY);
1947 /* mark as invalid as long as the parent table is not protected */
1948 *table = (unsigned long) s_sgt | _REGION_ENTRY_LENGTH |
1949 _REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_INVALID;
1950 if (sg->edat_level >= 1)
1951 *table |= sgt & _REGION_ENTRY_PROTECT;
1952 list_add(&page->lru, &sg->crst_list);
1954 /* nothing to protect for fake tables */
1955 *table &= ~_REGION_ENTRY_INVALID;
1956 spin_unlock(&sg->guest_table_lock);
1959 spin_unlock(&sg->guest_table_lock);
1960 /* Make sgt read-only in parent gmap page table */
1961 raddr = (saddr & _REGION3_MASK) | _SHADOW_RMAP_REGION3;
1962 origin = sgt & _REGION_ENTRY_ORIGIN;
1963 offset = ((sgt & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
1964 len = ((sgt & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
1965 rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
1966 spin_lock(&sg->guest_table_lock);
1968 table = gmap_table_walk(sg, saddr, 2);
1969 if (!table || (*table & _REGION_ENTRY_ORIGIN) !=
1970 (unsigned long) s_sgt)
1971 rc = -EAGAIN; /* Race with unshadow */
1973 *table &= ~_REGION_ENTRY_INVALID;
1975 gmap_unshadow_sgt(sg, raddr);
1977 spin_unlock(&sg->guest_table_lock);
1980 spin_unlock(&sg->guest_table_lock);
1981 __free_pages(page, CRST_ALLOC_ORDER);
1984 EXPORT_SYMBOL_GPL(gmap_shadow_sgt);
1987 * gmap_shadow_pgt_lookup - find a shadow page table
1988 * @sg: pointer to the shadow guest address space structure
1989 * @saddr: the address in the shadow aguest address space
1990 * @pgt: parent gmap address of the page table to get shadowed
1991 * @dat_protection: if the pgtable is marked as protected by dat
1992 * @fake: pgt references contiguous guest memory block, not a pgtable
1994 * Returns 0 if the shadow page table was found and -EAGAIN if the page
1995 * table was not found.
1997 * Called with sg->mm->mmap_lock in read.
1999 int gmap_shadow_pgt_lookup(struct gmap *sg, unsigned long saddr,
2000 unsigned long *pgt, int *dat_protection,
2003 unsigned long *table;
2007 BUG_ON(!gmap_is_shadow(sg));
2008 spin_lock(&sg->guest_table_lock);
2009 table = gmap_table_walk(sg, saddr, 1); /* get segment pointer */
2010 if (table && !(*table & _SEGMENT_ENTRY_INVALID)) {
2011 /* Shadow page tables are full pages (pte+pgste) */
2012 page = pfn_to_page(*table >> PAGE_SHIFT);
2013 *pgt = page->index & ~GMAP_SHADOW_FAKE_TABLE;
2014 *dat_protection = !!(*table & _SEGMENT_ENTRY_PROTECT);
2015 *fake = !!(page->index & GMAP_SHADOW_FAKE_TABLE);
2020 spin_unlock(&sg->guest_table_lock);
2024 EXPORT_SYMBOL_GPL(gmap_shadow_pgt_lookup);
2027 * gmap_shadow_pgt - instantiate a shadow page table
2028 * @sg: pointer to the shadow guest address space structure
2029 * @saddr: faulting address in the shadow gmap
2030 * @pgt: parent gmap address of the page table to get shadowed
2031 * @fake: pgt references contiguous guest memory block, not a pgtable
2033 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
2034 * shadow table structure is incomplete, -ENOMEM if out of memory,
2035 * -EFAULT if an address in the parent gmap could not be resolved and
2037 * Called with gmap->mm->mmap_lock in read
2039 int gmap_shadow_pgt(struct gmap *sg, unsigned long saddr, unsigned long pgt,
2042 unsigned long raddr, origin;
2043 unsigned long *s_pgt, *table;
2047 BUG_ON(!gmap_is_shadow(sg) || (pgt & _SEGMENT_ENTRY_LARGE));
2048 /* Allocate a shadow page table */
2049 page = page_table_alloc_pgste(sg->mm);
2052 page->index = pgt & _SEGMENT_ENTRY_ORIGIN;
2054 page->index |= GMAP_SHADOW_FAKE_TABLE;
2055 s_pgt = (unsigned long *) page_to_phys(page);
2056 /* Install shadow page table */
2057 spin_lock(&sg->guest_table_lock);
2058 table = gmap_table_walk(sg, saddr, 1); /* get segment pointer */
2060 rc = -EAGAIN; /* Race with unshadow */
2063 if (!(*table & _SEGMENT_ENTRY_INVALID)) {
2064 rc = 0; /* Already established */
2066 } else if (*table & _SEGMENT_ENTRY_ORIGIN) {
2067 rc = -EAGAIN; /* Race with shadow */
2070 /* mark as invalid as long as the parent table is not protected */
2071 *table = (unsigned long) s_pgt | _SEGMENT_ENTRY |
2072 (pgt & _SEGMENT_ENTRY_PROTECT) | _SEGMENT_ENTRY_INVALID;
2073 list_add(&page->lru, &sg->pt_list);
2075 /* nothing to protect for fake tables */
2076 *table &= ~_SEGMENT_ENTRY_INVALID;
2077 spin_unlock(&sg->guest_table_lock);
2080 spin_unlock(&sg->guest_table_lock);
2081 /* Make pgt read-only in parent gmap page table (not the pgste) */
2082 raddr = (saddr & _SEGMENT_MASK) | _SHADOW_RMAP_SEGMENT;
2083 origin = pgt & _SEGMENT_ENTRY_ORIGIN & PAGE_MASK;
2084 rc = gmap_protect_rmap(sg, raddr, origin, PAGE_SIZE);
2085 spin_lock(&sg->guest_table_lock);
2087 table = gmap_table_walk(sg, saddr, 1);
2088 if (!table || (*table & _SEGMENT_ENTRY_ORIGIN) !=
2089 (unsigned long) s_pgt)
2090 rc = -EAGAIN; /* Race with unshadow */
2092 *table &= ~_SEGMENT_ENTRY_INVALID;
2094 gmap_unshadow_pgt(sg, raddr);
2096 spin_unlock(&sg->guest_table_lock);
2099 spin_unlock(&sg->guest_table_lock);
2100 page_table_free_pgste(page);
2104 EXPORT_SYMBOL_GPL(gmap_shadow_pgt);
2107 * gmap_shadow_page - create a shadow page mapping
2108 * @sg: pointer to the shadow guest address space structure
2109 * @saddr: faulting address in the shadow gmap
2110 * @pte: pte in parent gmap address space to get shadowed
2112 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
2113 * shadow table structure is incomplete, -ENOMEM if out of memory and
2114 * -EFAULT if an address in the parent gmap could not be resolved.
2116 * Called with sg->mm->mmap_lock in read.
2118 int gmap_shadow_page(struct gmap *sg, unsigned long saddr, pte_t pte)
2120 struct gmap *parent;
2121 struct gmap_rmap *rmap;
2122 unsigned long vmaddr, paddr;
2124 pte_t *sptep, *tptep;
2128 BUG_ON(!gmap_is_shadow(sg));
2129 parent = sg->parent;
2130 prot = (pte_val(pte) & _PAGE_PROTECT) ? PROT_READ : PROT_WRITE;
2132 rmap = kzalloc(sizeof(*rmap), GFP_KERNEL_ACCOUNT);
2135 rmap->raddr = (saddr & PAGE_MASK) | _SHADOW_RMAP_PGTABLE;
2138 paddr = pte_val(pte) & PAGE_MASK;
2139 vmaddr = __gmap_translate(parent, paddr);
2140 if (IS_ERR_VALUE(vmaddr)) {
2144 rc = radix_tree_preload(GFP_KERNEL_ACCOUNT);
2148 sptep = gmap_pte_op_walk(parent, paddr, &ptl);
2150 spin_lock(&sg->guest_table_lock);
2151 /* Get page table pointer */
2152 tptep = (pte_t *) gmap_table_walk(sg, saddr, 0);
2154 spin_unlock(&sg->guest_table_lock);
2155 gmap_pte_op_end(ptl);
2156 radix_tree_preload_end();
2159 rc = ptep_shadow_pte(sg->mm, saddr, sptep, tptep, pte);
2161 /* Success and a new mapping */
2162 gmap_insert_rmap(sg, vmaddr, rmap);
2166 gmap_pte_op_end(ptl);
2167 spin_unlock(&sg->guest_table_lock);
2169 radix_tree_preload_end();
2172 rc = gmap_pte_op_fixup(parent, paddr, vmaddr, prot);
2179 EXPORT_SYMBOL_GPL(gmap_shadow_page);
2182 * gmap_shadow_notify - handle notifications for shadow gmap
2184 * Called with sg->parent->shadow_lock.
2186 static void gmap_shadow_notify(struct gmap *sg, unsigned long vmaddr,
2187 unsigned long gaddr)
2189 struct gmap_rmap *rmap, *rnext, *head;
2190 unsigned long start, end, bits, raddr;
2192 BUG_ON(!gmap_is_shadow(sg));
2194 spin_lock(&sg->guest_table_lock);
2196 spin_unlock(&sg->guest_table_lock);
2199 /* Check for top level table */
2200 start = sg->orig_asce & _ASCE_ORIGIN;
2201 end = start + ((sg->orig_asce & _ASCE_TABLE_LENGTH) + 1) * PAGE_SIZE;
2202 if (!(sg->orig_asce & _ASCE_REAL_SPACE) && gaddr >= start &&
2204 /* The complete shadow table has to go */
2206 spin_unlock(&sg->guest_table_lock);
2207 list_del(&sg->list);
2211 /* Remove the page table tree from on specific entry */
2212 head = radix_tree_delete(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT);
2213 gmap_for_each_rmap_safe(rmap, rnext, head) {
2214 bits = rmap->raddr & _SHADOW_RMAP_MASK;
2215 raddr = rmap->raddr ^ bits;
2217 case _SHADOW_RMAP_REGION1:
2218 gmap_unshadow_r2t(sg, raddr);
2220 case _SHADOW_RMAP_REGION2:
2221 gmap_unshadow_r3t(sg, raddr);
2223 case _SHADOW_RMAP_REGION3:
2224 gmap_unshadow_sgt(sg, raddr);
2226 case _SHADOW_RMAP_SEGMENT:
2227 gmap_unshadow_pgt(sg, raddr);
2229 case _SHADOW_RMAP_PGTABLE:
2230 gmap_unshadow_page(sg, raddr);
2235 spin_unlock(&sg->guest_table_lock);
2239 * ptep_notify - call all invalidation callbacks for a specific pte.
2240 * @mm: pointer to the process mm_struct
2241 * @vmaddr: virtual address in the process address space
2242 * @pte: pointer to the page table entry
2243 * @bits: bits from the pgste that caused the notify call
2245 * This function is assumed to be called with the page table lock held
2246 * for the pte to notify.
2248 void ptep_notify(struct mm_struct *mm, unsigned long vmaddr,
2249 pte_t *pte, unsigned long bits)
2251 unsigned long offset, gaddr = 0;
2252 unsigned long *table;
2253 struct gmap *gmap, *sg, *next;
2255 offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
2256 offset = offset * (PAGE_SIZE / sizeof(pte_t));
2258 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2259 spin_lock(&gmap->guest_table_lock);
2260 table = radix_tree_lookup(&gmap->host_to_guest,
2261 vmaddr >> PMD_SHIFT);
2263 gaddr = __gmap_segment_gaddr(table) + offset;
2264 spin_unlock(&gmap->guest_table_lock);
2268 if (!list_empty(&gmap->children) && (bits & PGSTE_VSIE_BIT)) {
2269 spin_lock(&gmap->shadow_lock);
2270 list_for_each_entry_safe(sg, next,
2271 &gmap->children, list)
2272 gmap_shadow_notify(sg, vmaddr, gaddr);
2273 spin_unlock(&gmap->shadow_lock);
2275 if (bits & PGSTE_IN_BIT)
2276 gmap_call_notifier(gmap, gaddr, gaddr + PAGE_SIZE - 1);
2280 EXPORT_SYMBOL_GPL(ptep_notify);
2282 static void pmdp_notify_gmap(struct gmap *gmap, pmd_t *pmdp,
2283 unsigned long gaddr)
2285 set_pmd(pmdp, clear_pmd_bit(*pmdp, __pgprot(_SEGMENT_ENTRY_GMAP_IN)));
2286 gmap_call_notifier(gmap, gaddr, gaddr + HPAGE_SIZE - 1);
2290 * gmap_pmdp_xchg - exchange a gmap pmd with another
2291 * @gmap: pointer to the guest address space structure
2292 * @pmdp: pointer to the pmd entry
2293 * @new: replacement entry
2294 * @gaddr: the affected guest address
2296 * This function is assumed to be called with the guest_table_lock
2299 static void gmap_pmdp_xchg(struct gmap *gmap, pmd_t *pmdp, pmd_t new,
2300 unsigned long gaddr)
2302 gaddr &= HPAGE_MASK;
2303 pmdp_notify_gmap(gmap, pmdp, gaddr);
2304 new = clear_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_GMAP_IN));
2305 if (MACHINE_HAS_TLB_GUEST)
2306 __pmdp_idte(gaddr, (pmd_t *)pmdp, IDTE_GUEST_ASCE, gmap->asce,
2308 else if (MACHINE_HAS_IDTE)
2309 __pmdp_idte(gaddr, (pmd_t *)pmdp, 0, 0, IDTE_GLOBAL);
2315 static void gmap_pmdp_clear(struct mm_struct *mm, unsigned long vmaddr,
2320 unsigned long gaddr;
2323 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2324 spin_lock(&gmap->guest_table_lock);
2325 pmdp = (pmd_t *)radix_tree_delete(&gmap->host_to_guest,
2326 vmaddr >> PMD_SHIFT);
2328 gaddr = __gmap_segment_gaddr((unsigned long *)pmdp);
2329 pmdp_notify_gmap(gmap, pmdp, gaddr);
2330 WARN_ON(pmd_val(*pmdp) & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
2331 _SEGMENT_ENTRY_GMAP_UC));
2334 set_pmd(pmdp, __pmd(_SEGMENT_ENTRY_EMPTY));
2336 spin_unlock(&gmap->guest_table_lock);
2342 * gmap_pmdp_invalidate - invalidate all affected guest pmd entries without
2344 * @mm: pointer to the process mm_struct
2345 * @vmaddr: virtual address in the process address space
2347 void gmap_pmdp_invalidate(struct mm_struct *mm, unsigned long vmaddr)
2349 gmap_pmdp_clear(mm, vmaddr, 0);
2351 EXPORT_SYMBOL_GPL(gmap_pmdp_invalidate);
2354 * gmap_pmdp_csp - csp all affected guest pmd entries
2355 * @mm: pointer to the process mm_struct
2356 * @vmaddr: virtual address in the process address space
2358 void gmap_pmdp_csp(struct mm_struct *mm, unsigned long vmaddr)
2360 gmap_pmdp_clear(mm, vmaddr, 1);
2362 EXPORT_SYMBOL_GPL(gmap_pmdp_csp);
2365 * gmap_pmdp_idte_local - invalidate and clear a guest pmd entry
2366 * @mm: pointer to the process mm_struct
2367 * @vmaddr: virtual address in the process address space
2369 void gmap_pmdp_idte_local(struct mm_struct *mm, unsigned long vmaddr)
2371 unsigned long *entry, gaddr;
2376 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2377 spin_lock(&gmap->guest_table_lock);
2378 entry = radix_tree_delete(&gmap->host_to_guest,
2379 vmaddr >> PMD_SHIFT);
2381 pmdp = (pmd_t *)entry;
2382 gaddr = __gmap_segment_gaddr(entry);
2383 pmdp_notify_gmap(gmap, pmdp, gaddr);
2384 WARN_ON(*entry & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
2385 _SEGMENT_ENTRY_GMAP_UC));
2386 if (MACHINE_HAS_TLB_GUEST)
2387 __pmdp_idte(gaddr, pmdp, IDTE_GUEST_ASCE,
2388 gmap->asce, IDTE_LOCAL);
2389 else if (MACHINE_HAS_IDTE)
2390 __pmdp_idte(gaddr, pmdp, 0, 0, IDTE_LOCAL);
2391 *entry = _SEGMENT_ENTRY_EMPTY;
2393 spin_unlock(&gmap->guest_table_lock);
2397 EXPORT_SYMBOL_GPL(gmap_pmdp_idte_local);
2400 * gmap_pmdp_idte_global - invalidate and clear a guest pmd entry
2401 * @mm: pointer to the process mm_struct
2402 * @vmaddr: virtual address in the process address space
2404 void gmap_pmdp_idte_global(struct mm_struct *mm, unsigned long vmaddr)
2406 unsigned long *entry, gaddr;
2411 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2412 spin_lock(&gmap->guest_table_lock);
2413 entry = radix_tree_delete(&gmap->host_to_guest,
2414 vmaddr >> PMD_SHIFT);
2416 pmdp = (pmd_t *)entry;
2417 gaddr = __gmap_segment_gaddr(entry);
2418 pmdp_notify_gmap(gmap, pmdp, gaddr);
2419 WARN_ON(*entry & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
2420 _SEGMENT_ENTRY_GMAP_UC));
2421 if (MACHINE_HAS_TLB_GUEST)
2422 __pmdp_idte(gaddr, pmdp, IDTE_GUEST_ASCE,
2423 gmap->asce, IDTE_GLOBAL);
2424 else if (MACHINE_HAS_IDTE)
2425 __pmdp_idte(gaddr, pmdp, 0, 0, IDTE_GLOBAL);
2428 *entry = _SEGMENT_ENTRY_EMPTY;
2430 spin_unlock(&gmap->guest_table_lock);
2434 EXPORT_SYMBOL_GPL(gmap_pmdp_idte_global);
2437 * gmap_test_and_clear_dirty_pmd - test and reset segment dirty status
2438 * @gmap: pointer to guest address space
2439 * @pmdp: pointer to the pmd to be tested
2440 * @gaddr: virtual address in the guest address space
2442 * This function is assumed to be called with the guest_table_lock
2445 static bool gmap_test_and_clear_dirty_pmd(struct gmap *gmap, pmd_t *pmdp,
2446 unsigned long gaddr)
2448 if (pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID)
2451 /* Already protected memory, which did not change is clean */
2452 if (pmd_val(*pmdp) & _SEGMENT_ENTRY_PROTECT &&
2453 !(pmd_val(*pmdp) & _SEGMENT_ENTRY_GMAP_UC))
2456 /* Clear UC indication and reset protection */
2457 set_pmd(pmdp, clear_pmd_bit(*pmdp, __pgprot(_SEGMENT_ENTRY_GMAP_UC)));
2458 gmap_protect_pmd(gmap, gaddr, pmdp, PROT_READ, 0);
2463 * gmap_sync_dirty_log_pmd - set bitmap based on dirty status of segment
2464 * @gmap: pointer to guest address space
2465 * @bitmap: dirty bitmap for this pmd
2466 * @gaddr: virtual address in the guest address space
2467 * @vmaddr: virtual address in the host address space
2469 * This function is assumed to be called with the guest_table_lock
2472 void gmap_sync_dirty_log_pmd(struct gmap *gmap, unsigned long bitmap[4],
2473 unsigned long gaddr, unsigned long vmaddr)
2480 pmdp = gmap_pmd_op_walk(gmap, gaddr);
2484 if (pmd_large(*pmdp)) {
2485 if (gmap_test_and_clear_dirty_pmd(gmap, pmdp, gaddr))
2486 bitmap_fill(bitmap, _PAGE_ENTRIES);
2488 for (i = 0; i < _PAGE_ENTRIES; i++, vmaddr += PAGE_SIZE) {
2489 ptep = pte_alloc_map_lock(gmap->mm, pmdp, vmaddr, &ptl);
2492 if (ptep_test_and_clear_uc(gmap->mm, vmaddr, ptep))
2497 gmap_pmd_op_end(gmap, pmdp);
2499 EXPORT_SYMBOL_GPL(gmap_sync_dirty_log_pmd);
2501 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
2502 static int thp_split_walk_pmd_entry(pmd_t *pmd, unsigned long addr,
2503 unsigned long end, struct mm_walk *walk)
2505 struct vm_area_struct *vma = walk->vma;
2507 split_huge_pmd(vma, pmd, addr);
2511 static const struct mm_walk_ops thp_split_walk_ops = {
2512 .pmd_entry = thp_split_walk_pmd_entry,
2515 static inline void thp_split_mm(struct mm_struct *mm)
2517 struct vm_area_struct *vma;
2518 VMA_ITERATOR(vmi, mm, 0);
2520 for_each_vma(vmi, vma) {
2521 vma->vm_flags &= ~VM_HUGEPAGE;
2522 vma->vm_flags |= VM_NOHUGEPAGE;
2523 walk_page_vma(vma, &thp_split_walk_ops, NULL);
2525 mm->def_flags |= VM_NOHUGEPAGE;
2528 static inline void thp_split_mm(struct mm_struct *mm)
2531 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
2534 * Remove all empty zero pages from the mapping for lazy refaulting
2535 * - This must be called after mm->context.has_pgste is set, to avoid
2536 * future creation of zero pages
2537 * - This must be called after THP was enabled
2539 static int __zap_zero_pages(pmd_t *pmd, unsigned long start,
2540 unsigned long end, struct mm_walk *walk)
2544 for (addr = start; addr != end; addr += PAGE_SIZE) {
2548 ptep = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
2549 if (is_zero_pfn(pte_pfn(*ptep)))
2550 ptep_xchg_direct(walk->mm, addr, ptep, __pte(_PAGE_INVALID));
2551 pte_unmap_unlock(ptep, ptl);
2556 static const struct mm_walk_ops zap_zero_walk_ops = {
2557 .pmd_entry = __zap_zero_pages,
2561 * switch on pgstes for its userspace process (for kvm)
2563 int s390_enable_sie(void)
2565 struct mm_struct *mm = current->mm;
2567 /* Do we have pgstes? if yes, we are done */
2568 if (mm_has_pgste(mm))
2570 /* Fail if the page tables are 2K */
2571 if (!mm_alloc_pgste(mm))
2573 mmap_write_lock(mm);
2574 mm->context.has_pgste = 1;
2575 /* split thp mappings and disable thp for future mappings */
2577 walk_page_range(mm, 0, TASK_SIZE, &zap_zero_walk_ops, NULL);
2578 mmap_write_unlock(mm);
2581 EXPORT_SYMBOL_GPL(s390_enable_sie);
2583 int gmap_mark_unmergeable(void)
2585 struct mm_struct *mm = current->mm;
2586 struct vm_area_struct *vma;
2588 VMA_ITERATOR(vmi, mm, 0);
2590 for_each_vma(vmi, vma) {
2591 ret = ksm_madvise(vma, vma->vm_start, vma->vm_end,
2592 MADV_UNMERGEABLE, &vma->vm_flags);
2596 mm->def_flags &= ~VM_MERGEABLE;
2599 EXPORT_SYMBOL_GPL(gmap_mark_unmergeable);
2602 * Enable storage key handling from now on and initialize the storage
2603 * keys with the default key.
2605 static int __s390_enable_skey_pte(pte_t *pte, unsigned long addr,
2606 unsigned long next, struct mm_walk *walk)
2608 /* Clear storage key */
2609 ptep_zap_key(walk->mm, addr, pte);
2614 * Give a chance to schedule after setting a key to 256 pages.
2615 * We only hold the mm lock, which is a rwsem and the kvm srcu.
2618 static int __s390_enable_skey_pmd(pmd_t *pmd, unsigned long addr,
2619 unsigned long next, struct mm_walk *walk)
2625 static int __s390_enable_skey_hugetlb(pte_t *pte, unsigned long addr,
2626 unsigned long hmask, unsigned long next,
2627 struct mm_walk *walk)
2629 pmd_t *pmd = (pmd_t *)pte;
2630 unsigned long start, end;
2631 struct page *page = pmd_page(*pmd);
2634 * The write check makes sure we do not set a key on shared
2635 * memory. This is needed as the walker does not differentiate
2636 * between actual guest memory and the process executable or
2639 if (pmd_val(*pmd) & _SEGMENT_ENTRY_INVALID ||
2640 !(pmd_val(*pmd) & _SEGMENT_ENTRY_WRITE))
2643 start = pmd_val(*pmd) & HPAGE_MASK;
2644 end = start + HPAGE_SIZE - 1;
2645 __storage_key_init_range(start, end);
2646 set_bit(PG_arch_1, &page->flags);
2651 static const struct mm_walk_ops enable_skey_walk_ops = {
2652 .hugetlb_entry = __s390_enable_skey_hugetlb,
2653 .pte_entry = __s390_enable_skey_pte,
2654 .pmd_entry = __s390_enable_skey_pmd,
2657 int s390_enable_skey(void)
2659 struct mm_struct *mm = current->mm;
2662 mmap_write_lock(mm);
2663 if (mm_uses_skeys(mm))
2666 mm->context.uses_skeys = 1;
2667 rc = gmap_mark_unmergeable();
2669 mm->context.uses_skeys = 0;
2672 walk_page_range(mm, 0, TASK_SIZE, &enable_skey_walk_ops, NULL);
2675 mmap_write_unlock(mm);
2678 EXPORT_SYMBOL_GPL(s390_enable_skey);
2681 * Reset CMMA state, make all pages stable again.
2683 static int __s390_reset_cmma(pte_t *pte, unsigned long addr,
2684 unsigned long next, struct mm_walk *walk)
2686 ptep_zap_unused(walk->mm, addr, pte, 1);
2690 static const struct mm_walk_ops reset_cmma_walk_ops = {
2691 .pte_entry = __s390_reset_cmma,
2694 void s390_reset_cmma(struct mm_struct *mm)
2696 mmap_write_lock(mm);
2697 walk_page_range(mm, 0, TASK_SIZE, &reset_cmma_walk_ops, NULL);
2698 mmap_write_unlock(mm);
2700 EXPORT_SYMBOL_GPL(s390_reset_cmma);
2702 #define GATHER_GET_PAGES 32
2704 struct reset_walk_state {
2706 unsigned long count;
2707 unsigned long pfns[GATHER_GET_PAGES];
2710 static int s390_gather_pages(pte_t *ptep, unsigned long addr,
2711 unsigned long next, struct mm_walk *walk)
2713 struct reset_walk_state *p = walk->private;
2714 pte_t pte = READ_ONCE(*ptep);
2716 if (pte_present(pte)) {
2717 /* we have a reference from the mapping, take an extra one */
2718 get_page(phys_to_page(pte_val(pte)));
2719 p->pfns[p->count] = phys_to_pfn(pte_val(pte));
2723 return p->count >= GATHER_GET_PAGES;
2726 static const struct mm_walk_ops gather_pages_ops = {
2727 .pte_entry = s390_gather_pages,
2731 * Call the Destroy secure page UVC on each page in the given array of PFNs.
2732 * Each page needs to have an extra reference, which will be released here.
2734 void s390_uv_destroy_pfns(unsigned long count, unsigned long *pfns)
2738 for (i = 0; i < count; i++) {
2739 /* we always have an extra reference */
2740 uv_destroy_owned_page(pfn_to_phys(pfns[i]));
2741 /* get rid of the extra reference */
2742 put_page(pfn_to_page(pfns[i]));
2746 EXPORT_SYMBOL_GPL(s390_uv_destroy_pfns);
2749 * __s390_uv_destroy_range - Call the destroy secure page UVC on each page
2750 * in the given range of the given address space.
2751 * @mm: the mm to operate on
2752 * @start: the start of the range
2753 * @end: the end of the range
2754 * @interruptible: if not 0, stop when a fatal signal is received
2756 * Walk the given range of the given address space and call the destroy
2757 * secure page UVC on each page. Optionally exit early if a fatal signal is
2760 * Return: 0 on success, -EINTR if the function stopped before completing
2762 int __s390_uv_destroy_range(struct mm_struct *mm, unsigned long start,
2763 unsigned long end, bool interruptible)
2765 struct reset_walk_state state = { .next = start };
2771 r = walk_page_range(mm, state.next, end, &gather_pages_ops, &state);
2772 mmap_read_unlock(mm);
2774 s390_uv_destroy_pfns(state.count, state.pfns);
2775 if (interruptible && fatal_signal_pending(current))
2780 EXPORT_SYMBOL_GPL(__s390_uv_destroy_range);
2783 * s390_unlist_old_asce - Remove the topmost level of page tables from the
2784 * list of page tables of the gmap.
2785 * @gmap: the gmap whose table is to be removed
2787 * On s390x, KVM keeps a list of all pages containing the page tables of the
2788 * gmap (the CRST list). This list is used at tear down time to free all
2789 * pages that are now not needed anymore.
2791 * This function removes the topmost page of the tree (the one pointed to by
2792 * the ASCE) from the CRST list.
2794 * This means that it will not be freed when the VM is torn down, and needs
2795 * to be handled separately by the caller, unless a leak is actually
2796 * intended. Notice that this function will only remove the page from the
2797 * list, the page will still be used as a top level page table (and ASCE).
2799 void s390_unlist_old_asce(struct gmap *gmap)
2803 old = virt_to_page(gmap->table);
2804 spin_lock(&gmap->guest_table_lock);
2805 list_del(&old->lru);
2807 * Sometimes the topmost page might need to be "removed" multiple
2808 * times, for example if the VM is rebooted into secure mode several
2809 * times concurrently, or if s390_replace_asce fails after calling
2810 * s390_remove_old_asce and is attempted again later. In that case
2811 * the old asce has been removed from the list, and therefore it
2812 * will not be freed when the VM terminates, but the ASCE is still
2813 * in use and still pointed to.
2814 * A subsequent call to replace_asce will follow the pointer and try
2815 * to remove the same page from the list again.
2816 * Therefore it's necessary that the page of the ASCE has valid
2817 * pointers, so list_del can work (and do nothing) without
2818 * dereferencing stale or invalid pointers.
2820 INIT_LIST_HEAD(&old->lru);
2821 spin_unlock(&gmap->guest_table_lock);
2823 EXPORT_SYMBOL_GPL(s390_unlist_old_asce);
2826 * s390_replace_asce - Try to replace the current ASCE of a gmap with a copy
2827 * @gmap: the gmap whose ASCE needs to be replaced
2829 * If the allocation of the new top level page table fails, the ASCE is not
2831 * In any case, the old ASCE is always removed from the gmap CRST list.
2832 * Therefore the caller has to make sure to save a pointer to it
2833 * beforehand, unless a leak is actually intended.
2835 int s390_replace_asce(struct gmap *gmap)
2841 s390_unlist_old_asce(gmap);
2843 page = alloc_pages(GFP_KERNEL_ACCOUNT, CRST_ALLOC_ORDER);
2847 table = page_to_virt(page);
2848 memcpy(table, gmap->table, 1UL << (CRST_ALLOC_ORDER + PAGE_SHIFT));
2851 * The caller has to deal with the old ASCE, but here we make sure
2852 * the new one is properly added to the CRST list, so that
2853 * it will be freed when the VM is torn down.
2855 spin_lock(&gmap->guest_table_lock);
2856 list_add(&page->lru, &gmap->crst_list);
2857 spin_unlock(&gmap->guest_table_lock);
2859 /* Set new table origin while preserving existing ASCE control bits */
2860 asce = (gmap->asce & ~_ASCE_ORIGIN) | __pa(table);
2861 WRITE_ONCE(gmap->asce, asce);
2862 WRITE_ONCE(gmap->mm->context.gmap_asce, asce);
2863 WRITE_ONCE(gmap->table, table);
2867 EXPORT_SYMBOL_GPL(s390_replace_asce);