2 * KVM guest address space mapping code
4 * Copyright IBM Corp. 2007, 2016
5 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
8 #include <linux/kernel.h>
10 #include <linux/swap.h>
11 #include <linux/smp.h>
12 #include <linux/spinlock.h>
13 #include <linux/slab.h>
14 #include <linux/swapops.h>
15 #include <linux/ksm.h>
16 #include <linux/mman.h>
18 #include <asm/pgtable.h>
19 #include <asm/pgalloc.h>
24 * gmap_alloc - allocate a guest address space
25 * @mm: pointer to the parent mm_struct
26 * @limit: maximum address of the gmap address space
28 * Returns a guest address space structure.
30 struct gmap *gmap_alloc(struct mm_struct *mm, unsigned long limit)
35 unsigned long etype, atype;
37 if (limit < (1UL << 31)) {
38 limit = (1UL << 31) - 1;
39 atype = _ASCE_TYPE_SEGMENT;
40 etype = _SEGMENT_ENTRY_EMPTY;
41 } else if (limit < (1UL << 42)) {
42 limit = (1UL << 42) - 1;
43 atype = _ASCE_TYPE_REGION3;
44 etype = _REGION3_ENTRY_EMPTY;
45 } else if (limit < (1UL << 53)) {
46 limit = (1UL << 53) - 1;
47 atype = _ASCE_TYPE_REGION2;
48 etype = _REGION2_ENTRY_EMPTY;
51 atype = _ASCE_TYPE_REGION1;
52 etype = _REGION1_ENTRY_EMPTY;
54 gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL);
57 INIT_LIST_HEAD(&gmap->crst_list);
58 INIT_RADIX_TREE(&gmap->guest_to_host, GFP_KERNEL);
59 INIT_RADIX_TREE(&gmap->host_to_guest, GFP_ATOMIC);
60 spin_lock_init(&gmap->guest_table_lock);
62 page = alloc_pages(GFP_KERNEL, 2);
66 list_add(&page->lru, &gmap->crst_list);
67 table = (unsigned long *) page_to_phys(page);
68 crst_table_init(table, etype);
70 gmap->asce = atype | _ASCE_TABLE_LENGTH |
71 _ASCE_USER_BITS | __pa(table);
72 gmap->asce_end = limit;
73 spin_lock(&mm->context.gmap_lock);
74 list_add_rcu(&gmap->list, &mm->context.gmap_list);
75 spin_unlock(&mm->context.gmap_lock);
83 EXPORT_SYMBOL_GPL(gmap_alloc);
85 static void gmap_flush_tlb(struct gmap *gmap)
88 __tlb_flush_asce(gmap->mm, gmap->asce);
93 static void gmap_radix_tree_free(struct radix_tree_root *root)
95 struct radix_tree_iter iter;
96 unsigned long indices[16];
101 /* A radix tree is freed by deleting all of its entries */
105 radix_tree_for_each_slot(slot, root, &iter, index) {
106 indices[nr] = iter.index;
110 for (i = 0; i < nr; i++) {
112 radix_tree_delete(root, index);
118 * gmap_free - free a guest address space
119 * @gmap: pointer to the guest address space structure
121 void gmap_free(struct gmap *gmap)
123 struct page *page, *next;
126 if (MACHINE_HAS_IDTE)
127 __tlb_flush_asce(gmap->mm, gmap->asce);
129 __tlb_flush_global();
131 spin_lock(&gmap->mm->context.gmap_lock);
132 list_del_rcu(&gmap->list);
133 spin_unlock(&gmap->mm->context.gmap_lock);
136 /* Free all segment & region tables. */
137 list_for_each_entry_safe(page, next, &gmap->crst_list, lru)
138 __free_pages(page, 2);
139 gmap_radix_tree_free(&gmap->guest_to_host);
140 gmap_radix_tree_free(&gmap->host_to_guest);
143 EXPORT_SYMBOL_GPL(gmap_free);
146 * gmap_enable - switch primary space to the guest address space
147 * @gmap: pointer to the guest address space structure
149 void gmap_enable(struct gmap *gmap)
151 S390_lowcore.gmap = (unsigned long) gmap;
153 EXPORT_SYMBOL_GPL(gmap_enable);
156 * gmap_disable - switch back to the standard primary address space
157 * @gmap: pointer to the guest address space structure
159 void gmap_disable(struct gmap *gmap)
161 S390_lowcore.gmap = 0UL;
163 EXPORT_SYMBOL_GPL(gmap_disable);
166 * gmap_alloc_table is assumed to be called with mmap_sem held
168 static int gmap_alloc_table(struct gmap *gmap, unsigned long *table,
169 unsigned long init, unsigned long gaddr)
174 /* since we dont free the gmap table until gmap_free we can unlock */
175 page = alloc_pages(GFP_KERNEL, 2);
178 new = (unsigned long *) page_to_phys(page);
179 crst_table_init(new, init);
180 spin_lock(&gmap->mm->page_table_lock);
181 if (*table & _REGION_ENTRY_INVALID) {
182 list_add(&page->lru, &gmap->crst_list);
183 *table = (unsigned long) new | _REGION_ENTRY_LENGTH |
184 (*table & _REGION_ENTRY_TYPE_MASK);
188 spin_unlock(&gmap->mm->page_table_lock);
190 __free_pages(page, 2);
195 * __gmap_segment_gaddr - find virtual address from segment pointer
196 * @entry: pointer to a segment table entry in the guest address space
198 * Returns the virtual address in the guest address space for the segment
200 static unsigned long __gmap_segment_gaddr(unsigned long *entry)
203 unsigned long offset, mask;
205 offset = (unsigned long) entry / sizeof(unsigned long);
206 offset = (offset & (PTRS_PER_PMD - 1)) * PMD_SIZE;
207 mask = ~(PTRS_PER_PMD * sizeof(pmd_t) - 1);
208 page = virt_to_page((void *)((unsigned long) entry & mask));
209 return page->index + offset;
213 * __gmap_unlink_by_vmaddr - unlink a single segment via a host address
214 * @gmap: pointer to the guest address space structure
215 * @vmaddr: address in the host process address space
217 * Returns 1 if a TLB flush is required
219 static int __gmap_unlink_by_vmaddr(struct gmap *gmap, unsigned long vmaddr)
221 unsigned long *entry;
224 spin_lock(&gmap->guest_table_lock);
225 entry = radix_tree_delete(&gmap->host_to_guest, vmaddr >> PMD_SHIFT);
227 flush = (*entry != _SEGMENT_ENTRY_INVALID);
228 *entry = _SEGMENT_ENTRY_INVALID;
230 spin_unlock(&gmap->guest_table_lock);
235 * __gmap_unmap_by_gaddr - unmap a single segment via a guest address
236 * @gmap: pointer to the guest address space structure
237 * @gaddr: address in the guest address space
239 * Returns 1 if a TLB flush is required
241 static int __gmap_unmap_by_gaddr(struct gmap *gmap, unsigned long gaddr)
243 unsigned long vmaddr;
245 vmaddr = (unsigned long) radix_tree_delete(&gmap->guest_to_host,
247 return vmaddr ? __gmap_unlink_by_vmaddr(gmap, vmaddr) : 0;
251 * gmap_unmap_segment - unmap segment from the guest address space
252 * @gmap: pointer to the guest address space structure
253 * @to: address in the guest address space
254 * @len: length of the memory area to unmap
256 * Returns 0 if the unmap succeeded, -EINVAL if not.
258 int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len)
263 if ((to | len) & (PMD_SIZE - 1))
265 if (len == 0 || to + len < to)
269 down_write(&gmap->mm->mmap_sem);
270 for (off = 0; off < len; off += PMD_SIZE)
271 flush |= __gmap_unmap_by_gaddr(gmap, to + off);
272 up_write(&gmap->mm->mmap_sem);
274 gmap_flush_tlb(gmap);
277 EXPORT_SYMBOL_GPL(gmap_unmap_segment);
280 * gmap_map_segment - map a segment to the guest address space
281 * @gmap: pointer to the guest address space structure
282 * @from: source address in the parent address space
283 * @to: target address in the guest address space
284 * @len: length of the memory area to map
286 * Returns 0 if the mmap succeeded, -EINVAL or -ENOMEM if not.
288 int gmap_map_segment(struct gmap *gmap, unsigned long from,
289 unsigned long to, unsigned long len)
294 if ((from | to | len) & (PMD_SIZE - 1))
296 if (len == 0 || from + len < from || to + len < to ||
297 from + len - 1 > TASK_MAX_SIZE || to + len - 1 > gmap->asce_end)
301 down_write(&gmap->mm->mmap_sem);
302 for (off = 0; off < len; off += PMD_SIZE) {
303 /* Remove old translation */
304 flush |= __gmap_unmap_by_gaddr(gmap, to + off);
305 /* Store new translation */
306 if (radix_tree_insert(&gmap->guest_to_host,
307 (to + off) >> PMD_SHIFT,
308 (void *) from + off))
311 up_write(&gmap->mm->mmap_sem);
313 gmap_flush_tlb(gmap);
316 gmap_unmap_segment(gmap, to, len);
319 EXPORT_SYMBOL_GPL(gmap_map_segment);
322 * __gmap_translate - translate a guest address to a user space address
323 * @gmap: pointer to guest mapping meta data structure
324 * @gaddr: guest address
326 * Returns user space address which corresponds to the guest address or
327 * -EFAULT if no such mapping exists.
328 * This function does not establish potentially missing page table entries.
329 * The mmap_sem of the mm that belongs to the address space must be held
330 * when this function gets called.
332 unsigned long __gmap_translate(struct gmap *gmap, unsigned long gaddr)
334 unsigned long vmaddr;
336 vmaddr = (unsigned long)
337 radix_tree_lookup(&gmap->guest_to_host, gaddr >> PMD_SHIFT);
338 return vmaddr ? (vmaddr | (gaddr & ~PMD_MASK)) : -EFAULT;
340 EXPORT_SYMBOL_GPL(__gmap_translate);
343 * gmap_translate - translate a guest address to a user space address
344 * @gmap: pointer to guest mapping meta data structure
345 * @gaddr: guest address
347 * Returns user space address which corresponds to the guest address or
348 * -EFAULT if no such mapping exists.
349 * This function does not establish potentially missing page table entries.
351 unsigned long gmap_translate(struct gmap *gmap, unsigned long gaddr)
355 down_read(&gmap->mm->mmap_sem);
356 rc = __gmap_translate(gmap, gaddr);
357 up_read(&gmap->mm->mmap_sem);
360 EXPORT_SYMBOL_GPL(gmap_translate);
363 * gmap_unlink - disconnect a page table from the gmap shadow tables
364 * @gmap: pointer to guest mapping meta data structure
365 * @table: pointer to the host page table
366 * @vmaddr: vm address associated with the host page table
368 void gmap_unlink(struct mm_struct *mm, unsigned long *table,
369 unsigned long vmaddr)
375 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
376 flush = __gmap_unlink_by_vmaddr(gmap, vmaddr);
378 gmap_flush_tlb(gmap);
384 * gmap_link - set up shadow page tables to connect a host to a guest address
385 * @gmap: pointer to guest mapping meta data structure
386 * @gaddr: guest address
387 * @vmaddr: vm address
389 * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
390 * if the vm address is already mapped to a different guest segment.
391 * The mmap_sem of the mm that belongs to the address space must be held
392 * when this function gets called.
394 int __gmap_link(struct gmap *gmap, unsigned long gaddr, unsigned long vmaddr)
396 struct mm_struct *mm;
397 unsigned long *table;
404 /* Create higher level tables in the gmap page table */
406 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION1) {
407 table += (gaddr >> 53) & 0x7ff;
408 if ((*table & _REGION_ENTRY_INVALID) &&
409 gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY,
410 gaddr & 0xffe0000000000000UL))
412 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
414 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION2) {
415 table += (gaddr >> 42) & 0x7ff;
416 if ((*table & _REGION_ENTRY_INVALID) &&
417 gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY,
418 gaddr & 0xfffffc0000000000UL))
420 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
422 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION3) {
423 table += (gaddr >> 31) & 0x7ff;
424 if ((*table & _REGION_ENTRY_INVALID) &&
425 gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY,
426 gaddr & 0xffffffff80000000UL))
428 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
430 table += (gaddr >> 20) & 0x7ff;
431 /* Walk the parent mm page table */
433 pgd = pgd_offset(mm, vmaddr);
434 VM_BUG_ON(pgd_none(*pgd));
435 pud = pud_offset(pgd, vmaddr);
436 VM_BUG_ON(pud_none(*pud));
437 pmd = pmd_offset(pud, vmaddr);
438 VM_BUG_ON(pmd_none(*pmd));
439 /* large pmds cannot yet be handled */
442 /* Link gmap segment table entry location to page table. */
443 rc = radix_tree_preload(GFP_KERNEL);
446 ptl = pmd_lock(mm, pmd);
447 spin_lock(&gmap->guest_table_lock);
448 if (*table == _SEGMENT_ENTRY_INVALID) {
449 rc = radix_tree_insert(&gmap->host_to_guest,
450 vmaddr >> PMD_SHIFT, table);
452 *table = pmd_val(*pmd);
455 spin_unlock(&gmap->guest_table_lock);
457 radix_tree_preload_end();
462 * gmap_fault - resolve a fault on a guest address
463 * @gmap: pointer to guest mapping meta data structure
464 * @gaddr: guest address
465 * @fault_flags: flags to pass down to handle_mm_fault()
467 * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
468 * if the vm address is already mapped to a different guest segment.
470 int gmap_fault(struct gmap *gmap, unsigned long gaddr,
471 unsigned int fault_flags)
473 unsigned long vmaddr;
477 down_read(&gmap->mm->mmap_sem);
481 vmaddr = __gmap_translate(gmap, gaddr);
482 if (IS_ERR_VALUE(vmaddr)) {
486 if (fixup_user_fault(current, gmap->mm, vmaddr, fault_flags,
492 * In the case that fixup_user_fault unlocked the mmap_sem during
493 * faultin redo __gmap_translate to not race with a map/unmap_segment.
498 rc = __gmap_link(gmap, gaddr, vmaddr);
500 up_read(&gmap->mm->mmap_sem);
503 EXPORT_SYMBOL_GPL(gmap_fault);
506 * this function is assumed to be called with mmap_sem held
508 void __gmap_zap(struct gmap *gmap, unsigned long gaddr)
510 unsigned long vmaddr;
514 /* Find the vm address for the guest address */
515 vmaddr = (unsigned long) radix_tree_lookup(&gmap->guest_to_host,
518 vmaddr |= gaddr & ~PMD_MASK;
519 /* Get pointer to the page table entry */
520 ptep = get_locked_pte(gmap->mm, vmaddr, &ptl);
522 ptep_zap_unused(gmap->mm, vmaddr, ptep, 0);
523 pte_unmap_unlock(ptep, ptl);
526 EXPORT_SYMBOL_GPL(__gmap_zap);
528 void gmap_discard(struct gmap *gmap, unsigned long from, unsigned long to)
530 unsigned long gaddr, vmaddr, size;
531 struct vm_area_struct *vma;
533 down_read(&gmap->mm->mmap_sem);
534 for (gaddr = from; gaddr < to;
535 gaddr = (gaddr + PMD_SIZE) & PMD_MASK) {
536 /* Find the vm address for the guest address */
537 vmaddr = (unsigned long)
538 radix_tree_lookup(&gmap->guest_to_host,
542 vmaddr |= gaddr & ~PMD_MASK;
543 /* Find vma in the parent mm */
544 vma = find_vma(gmap->mm, vmaddr);
545 size = min(to - gaddr, PMD_SIZE - (gaddr & ~PMD_MASK));
546 zap_page_range(vma, vmaddr, size, NULL);
548 up_read(&gmap->mm->mmap_sem);
550 EXPORT_SYMBOL_GPL(gmap_discard);
552 static LIST_HEAD(gmap_notifier_list);
553 static DEFINE_SPINLOCK(gmap_notifier_lock);
556 * gmap_register_pte_notifier - register a pte invalidation callback
557 * @nb: pointer to the gmap notifier block
559 void gmap_register_pte_notifier(struct gmap_notifier *nb)
561 spin_lock(&gmap_notifier_lock);
562 list_add_rcu(&nb->list, &gmap_notifier_list);
563 spin_unlock(&gmap_notifier_lock);
565 EXPORT_SYMBOL_GPL(gmap_register_pte_notifier);
568 * gmap_unregister_pte_notifier - remove a pte invalidation callback
569 * @nb: pointer to the gmap notifier block
571 void gmap_unregister_pte_notifier(struct gmap_notifier *nb)
573 spin_lock(&gmap_notifier_lock);
574 list_del_rcu(&nb->list);
575 spin_unlock(&gmap_notifier_lock);
578 EXPORT_SYMBOL_GPL(gmap_unregister_pte_notifier);
581 * gmap_call_notifier - call all registered invalidation callbacks
582 * @gmap: pointer to guest mapping meta data structure
583 * @start: start virtual address in the guest address space
584 * @end: end virtual address in the guest address space
586 static void gmap_call_notifier(struct gmap *gmap, unsigned long start,
589 struct gmap_notifier *nb;
591 list_for_each_entry(nb, &gmap_notifier_list, list)
592 nb->notifier_call(gmap, start, end);
596 * gmap_table_walk - walk the gmap page tables
597 * @gmap: pointer to guest mapping meta data structure
598 * @gaddr: virtual address in the guest address space
600 * Returns a table pointer for the given guest address.
602 static inline unsigned long *gmap_table_walk(struct gmap *gmap,
605 unsigned long *table;
608 switch (gmap->asce & _ASCE_TYPE_MASK) {
609 case _ASCE_TYPE_REGION1:
610 table += (gaddr >> 53) & 0x7ff;
611 if (*table & _REGION_ENTRY_INVALID)
613 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
615 case _ASCE_TYPE_REGION2:
616 table += (gaddr >> 42) & 0x7ff;
617 if (*table & _REGION_ENTRY_INVALID)
619 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
621 case _ASCE_TYPE_REGION3:
622 table += (gaddr >> 31) & 0x7ff;
623 if (*table & _REGION_ENTRY_INVALID)
625 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
627 case _ASCE_TYPE_SEGMENT:
628 table += (gaddr >> 20) & 0x7ff;
634 * gmap_pte_op_walk - walk the gmap page table, get the page table lock
635 * and return the pte pointer
636 * @gmap: pointer to guest mapping meta data structure
637 * @gaddr: virtual address in the guest address space
638 * @ptl: pointer to the spinlock pointer
640 * Returns a pointer to the locked pte for a guest address, or NULL
642 static pte_t *gmap_pte_op_walk(struct gmap *gmap, unsigned long gaddr,
645 unsigned long *table;
647 /* Walk the gmap page table, lock and get pte pointer */
648 table = gmap_table_walk(gmap, gaddr);
649 if (!table || *table & _SEGMENT_ENTRY_INVALID)
651 return pte_alloc_map_lock(gmap->mm, (pmd_t *) table, gaddr, ptl);
655 * gmap_pte_op_fixup - force a page in and connect the gmap page table
656 * @gmap: pointer to guest mapping meta data structure
657 * @gaddr: virtual address in the guest address space
658 * @vmaddr: address in the host process address space
660 * Returns 0 if the caller can retry __gmap_translate (might fail again),
661 * -ENOMEM if out of memory and -EFAULT if anything goes wrong while fixing
662 * up or connecting the gmap page table.
664 static int gmap_pte_op_fixup(struct gmap *gmap, unsigned long gaddr,
665 unsigned long vmaddr)
667 struct mm_struct *mm = gmap->mm;
668 bool unlocked = false;
670 if (fixup_user_fault(current, mm, vmaddr, FAULT_FLAG_WRITE, &unlocked))
673 /* lost mmap_sem, caller has to retry __gmap_translate */
675 /* Connect the page tables */
676 return __gmap_link(gmap, gaddr, vmaddr);
680 * gmap_pte_op_end - release the page table lock
681 * @ptl: pointer to the spinlock pointer
683 static void gmap_pte_op_end(spinlock_t *ptl)
689 * gmap_mprotect_notify - change access rights for a range of ptes and
690 * call the notifier if any pte changes again
691 * @gmap: pointer to guest mapping meta data structure
692 * @gaddr: virtual address in the guest address space
694 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
696 * Returns 0 if for each page in the given range a gmap mapping exists,
697 * the new access rights could be set and the notifier could be armed.
698 * If the gmap mapping is missing for one or more pages -EFAULT is
699 * returned. If no memory could be allocated -ENOMEM is returned.
700 * This function establishes missing page table entries.
702 int gmap_mprotect_notify(struct gmap *gmap, unsigned long gaddr,
703 unsigned long len, int prot)
705 unsigned long vmaddr;
710 if ((gaddr & ~PAGE_MASK) || (len & ~PAGE_MASK))
712 if (!MACHINE_HAS_ESOP && prot == PROT_READ)
714 down_read(&gmap->mm->mmap_sem);
717 ptep = gmap_pte_op_walk(gmap, gaddr, &ptl);
719 rc = ptep_force_prot(gmap->mm, gaddr, ptep, prot);
720 gmap_pte_op_end(ptl);
723 vmaddr = __gmap_translate(gmap, gaddr);
724 if (IS_ERR_VALUE(vmaddr)) {
728 rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr);
736 up_read(&gmap->mm->mmap_sem);
739 EXPORT_SYMBOL_GPL(gmap_mprotect_notify);
742 * ptep_notify - call all invalidation callbacks for a specific pte.
743 * @mm: pointer to the process mm_struct
744 * @addr: virtual address in the process address space
745 * @pte: pointer to the page table entry
747 * This function is assumed to be called with the page table lock held
748 * for the pte to notify.
750 void ptep_notify(struct mm_struct *mm, unsigned long vmaddr, pte_t *pte)
752 unsigned long offset, gaddr;
753 unsigned long *table;
756 offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
757 offset = offset * (4096 / sizeof(pte_t));
759 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
760 spin_lock(&gmap->guest_table_lock);
761 table = radix_tree_lookup(&gmap->host_to_guest,
762 vmaddr >> PMD_SHIFT);
764 gaddr = __gmap_segment_gaddr(table) + offset;
765 spin_unlock(&gmap->guest_table_lock);
767 gmap_call_notifier(gmap, gaddr, gaddr + PAGE_SIZE - 1);
771 EXPORT_SYMBOL_GPL(ptep_notify);
773 static inline void thp_split_mm(struct mm_struct *mm)
775 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
776 struct vm_area_struct *vma;
779 for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) {
780 for (addr = vma->vm_start;
783 follow_page(vma, addr, FOLL_SPLIT);
784 vma->vm_flags &= ~VM_HUGEPAGE;
785 vma->vm_flags |= VM_NOHUGEPAGE;
787 mm->def_flags |= VM_NOHUGEPAGE;
792 * switch on pgstes for its userspace process (for kvm)
794 int s390_enable_sie(void)
796 struct mm_struct *mm = current->mm;
798 /* Do we have pgstes? if yes, we are done */
799 if (mm_has_pgste(mm))
801 /* Fail if the page tables are 2K */
802 if (!mm_alloc_pgste(mm))
804 down_write(&mm->mmap_sem);
805 mm->context.has_pgste = 1;
806 /* split thp mappings and disable thp for future mappings */
808 up_write(&mm->mmap_sem);
811 EXPORT_SYMBOL_GPL(s390_enable_sie);
814 * Enable storage key handling from now on and initialize the storage
815 * keys with the default key.
817 static int __s390_enable_skey(pte_t *pte, unsigned long addr,
818 unsigned long next, struct mm_walk *walk)
821 * Remove all zero page mappings,
822 * after establishing a policy to forbid zero page mappings
823 * following faults for that page will get fresh anonymous pages
825 if (is_zero_pfn(pte_pfn(*pte)))
826 ptep_xchg_direct(walk->mm, addr, pte, __pte(_PAGE_INVALID));
827 /* Clear storage key */
828 ptep_zap_key(walk->mm, addr, pte);
832 int s390_enable_skey(void)
834 struct mm_walk walk = { .pte_entry = __s390_enable_skey };
835 struct mm_struct *mm = current->mm;
836 struct vm_area_struct *vma;
839 down_write(&mm->mmap_sem);
843 mm->context.use_skey = 1;
844 for (vma = mm->mmap; vma; vma = vma->vm_next) {
845 if (ksm_madvise(vma, vma->vm_start, vma->vm_end,
846 MADV_UNMERGEABLE, &vma->vm_flags)) {
847 mm->context.use_skey = 0;
852 mm->def_flags &= ~VM_MERGEABLE;
855 walk_page_range(0, TASK_SIZE, &walk);
858 up_write(&mm->mmap_sem);
861 EXPORT_SYMBOL_GPL(s390_enable_skey);
864 * Reset CMMA state, make all pages stable again.
866 static int __s390_reset_cmma(pte_t *pte, unsigned long addr,
867 unsigned long next, struct mm_walk *walk)
869 ptep_zap_unused(walk->mm, addr, pte, 1);
873 void s390_reset_cmma(struct mm_struct *mm)
875 struct mm_walk walk = { .pte_entry = __s390_reset_cmma };
877 down_write(&mm->mmap_sem);
879 walk_page_range(0, TASK_SIZE, &walk);
880 up_write(&mm->mmap_sem);
882 EXPORT_SYMBOL_GPL(s390_reset_cmma);