2 * Copyright IBM Corp. 2007, 2011
3 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
6 #include <linux/sched.h>
7 #include <linux/kernel.h>
8 #include <linux/errno.h>
11 #include <linux/swap.h>
12 #include <linux/smp.h>
13 #include <linux/highmem.h>
14 #include <linux/pagemap.h>
15 #include <linux/spinlock.h>
16 #include <linux/module.h>
17 #include <linux/quicklist.h>
18 #include <linux/rcupdate.h>
19 #include <linux/slab.h>
20 #include <linux/swapops.h>
22 #include <asm/pgtable.h>
23 #include <asm/pgalloc.h>
25 #include <asm/tlbflush.h>
26 #include <asm/mmu_context.h>
30 #define FRAG_MASK 0x0f
33 #define FRAG_MASK 0x03
37 unsigned long *crst_table_alloc(struct mm_struct *mm)
39 struct page *page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
43 return (unsigned long *) page_to_phys(page);
46 void crst_table_free(struct mm_struct *mm, unsigned long *table)
48 free_pages((unsigned long) table, ALLOC_ORDER);
52 static void __crst_table_upgrade(void *arg)
54 struct mm_struct *mm = arg;
56 if (current->active_mm == mm) {
63 int crst_table_upgrade(struct mm_struct *mm, unsigned long limit)
65 unsigned long *table, *pgd;
69 BUG_ON(limit > (1UL << 53));
72 table = crst_table_alloc(mm);
75 spin_lock_bh(&mm->page_table_lock);
76 if (mm->context.asce_limit < limit) {
77 pgd = (unsigned long *) mm->pgd;
78 if (mm->context.asce_limit <= (1UL << 31)) {
79 entry = _REGION3_ENTRY_EMPTY;
80 mm->context.asce_limit = 1UL << 42;
81 mm->context.asce_bits = _ASCE_TABLE_LENGTH |
85 entry = _REGION2_ENTRY_EMPTY;
86 mm->context.asce_limit = 1UL << 53;
87 mm->context.asce_bits = _ASCE_TABLE_LENGTH |
91 crst_table_init(table, entry);
92 pgd_populate(mm, (pgd_t *) table, (pud_t *) pgd);
93 mm->pgd = (pgd_t *) table;
94 mm->task_size = mm->context.asce_limit;
98 spin_unlock_bh(&mm->page_table_lock);
100 crst_table_free(mm, table);
101 if (mm->context.asce_limit < limit)
104 on_each_cpu(__crst_table_upgrade, mm, 0);
108 void crst_table_downgrade(struct mm_struct *mm, unsigned long limit)
112 if (current->active_mm == mm) {
116 while (mm->context.asce_limit > limit) {
118 switch (pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) {
119 case _REGION_ENTRY_TYPE_R2:
120 mm->context.asce_limit = 1UL << 42;
121 mm->context.asce_bits = _ASCE_TABLE_LENGTH |
125 case _REGION_ENTRY_TYPE_R3:
126 mm->context.asce_limit = 1UL << 31;
127 mm->context.asce_bits = _ASCE_TABLE_LENGTH |
134 mm->pgd = (pgd_t *) (pgd_val(*pgd) & _REGION_ENTRY_ORIGIN);
135 mm->task_size = mm->context.asce_limit;
136 crst_table_free(mm, (unsigned long *) pgd);
138 if (current->active_mm == mm)
146 * gmap_alloc - allocate a guest address space
147 * @mm: pointer to the parent mm_struct
149 * Returns a guest address space structure.
151 struct gmap *gmap_alloc(struct mm_struct *mm)
155 unsigned long *table;
157 gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL);
160 INIT_LIST_HEAD(&gmap->crst_list);
162 page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
165 list_add(&page->lru, &gmap->crst_list);
166 table = (unsigned long *) page_to_phys(page);
167 crst_table_init(table, _REGION1_ENTRY_EMPTY);
169 gmap->asce = _ASCE_TYPE_REGION1 | _ASCE_TABLE_LENGTH |
170 _ASCE_USER_BITS | __pa(table);
171 list_add(&gmap->list, &mm->context.gmap_list);
179 EXPORT_SYMBOL_GPL(gmap_alloc);
181 static int gmap_unlink_segment(struct gmap *gmap, unsigned long *table)
183 struct gmap_pgtable *mp;
184 struct gmap_rmap *rmap;
187 if (*table & _SEGMENT_ENTRY_INVALID)
189 page = pfn_to_page(*table >> PAGE_SHIFT);
190 mp = (struct gmap_pgtable *) page->index;
191 list_for_each_entry(rmap, &mp->mapper, list) {
192 if (rmap->entry != table)
194 list_del(&rmap->list);
198 *table = mp->vmaddr | _SEGMENT_ENTRY_INVALID | _SEGMENT_ENTRY_PROTECT;
202 static void gmap_flush_tlb(struct gmap *gmap)
204 if (MACHINE_HAS_IDTE)
205 __tlb_flush_asce(gmap->mm, (unsigned long) gmap->table |
208 __tlb_flush_global();
212 * gmap_free - free a guest address space
213 * @gmap: pointer to the guest address space structure
215 void gmap_free(struct gmap *gmap)
217 struct page *page, *next;
218 unsigned long *table;
223 if (MACHINE_HAS_IDTE)
224 __tlb_flush_asce(gmap->mm, (unsigned long) gmap->table |
227 __tlb_flush_global();
229 /* Free all segment & region tables. */
230 down_read(&gmap->mm->mmap_sem);
231 spin_lock(&gmap->mm->page_table_lock);
232 list_for_each_entry_safe(page, next, &gmap->crst_list, lru) {
233 table = (unsigned long *) page_to_phys(page);
234 if ((*table & _REGION_ENTRY_TYPE_MASK) == 0)
235 /* Remove gmap rmap structures for segment table. */
236 for (i = 0; i < PTRS_PER_PMD; i++, table++)
237 gmap_unlink_segment(gmap, table);
238 __free_pages(page, ALLOC_ORDER);
240 spin_unlock(&gmap->mm->page_table_lock);
241 up_read(&gmap->mm->mmap_sem);
242 list_del(&gmap->list);
245 EXPORT_SYMBOL_GPL(gmap_free);
248 * gmap_enable - switch primary space to the guest address space
249 * @gmap: pointer to the guest address space structure
251 void gmap_enable(struct gmap *gmap)
253 S390_lowcore.gmap = (unsigned long) gmap;
255 EXPORT_SYMBOL_GPL(gmap_enable);
258 * gmap_disable - switch back to the standard primary address space
259 * @gmap: pointer to the guest address space structure
261 void gmap_disable(struct gmap *gmap)
263 S390_lowcore.gmap = 0UL;
265 EXPORT_SYMBOL_GPL(gmap_disable);
268 * gmap_alloc_table is assumed to be called with mmap_sem held
270 static int gmap_alloc_table(struct gmap *gmap,
271 unsigned long *table, unsigned long init)
272 __releases(&gmap->mm->page_table_lock)
273 __acquires(&gmap->mm->page_table_lock)
278 /* since we dont free the gmap table until gmap_free we can unlock */
279 spin_unlock(&gmap->mm->page_table_lock);
280 page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
281 spin_lock(&gmap->mm->page_table_lock);
284 new = (unsigned long *) page_to_phys(page);
285 crst_table_init(new, init);
286 if (*table & _REGION_ENTRY_INVALID) {
287 list_add(&page->lru, &gmap->crst_list);
288 *table = (unsigned long) new | _REGION_ENTRY_LENGTH |
289 (*table & _REGION_ENTRY_TYPE_MASK);
291 __free_pages(page, ALLOC_ORDER);
296 * gmap_unmap_segment - unmap segment from the guest address space
297 * @gmap: pointer to the guest address space structure
298 * @addr: address in the guest address space
299 * @len: length of the memory area to unmap
301 * Returns 0 if the unmap succeeded, -EINVAL if not.
303 int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len)
305 unsigned long *table;
309 if ((to | len) & (PMD_SIZE - 1))
311 if (len == 0 || to + len < to)
315 down_read(&gmap->mm->mmap_sem);
316 spin_lock(&gmap->mm->page_table_lock);
317 for (off = 0; off < len; off += PMD_SIZE) {
318 /* Walk the guest addr space page table */
319 table = gmap->table + (((to + off) >> 53) & 0x7ff);
320 if (*table & _REGION_ENTRY_INVALID)
322 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
323 table = table + (((to + off) >> 42) & 0x7ff);
324 if (*table & _REGION_ENTRY_INVALID)
326 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
327 table = table + (((to + off) >> 31) & 0x7ff);
328 if (*table & _REGION_ENTRY_INVALID)
330 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
331 table = table + (((to + off) >> 20) & 0x7ff);
333 /* Clear segment table entry in guest address space. */
334 flush |= gmap_unlink_segment(gmap, table);
335 *table = _SEGMENT_ENTRY_INVALID;
338 spin_unlock(&gmap->mm->page_table_lock);
339 up_read(&gmap->mm->mmap_sem);
341 gmap_flush_tlb(gmap);
344 EXPORT_SYMBOL_GPL(gmap_unmap_segment);
347 * gmap_mmap_segment - map a segment to the guest address space
348 * @gmap: pointer to the guest address space structure
349 * @from: source address in the parent address space
350 * @to: target address in the guest address space
352 * Returns 0 if the mmap succeeded, -EINVAL or -ENOMEM if not.
354 int gmap_map_segment(struct gmap *gmap, unsigned long from,
355 unsigned long to, unsigned long len)
357 unsigned long *table;
361 if ((from | to | len) & (PMD_SIZE - 1))
363 if (len == 0 || from + len > TASK_MAX_SIZE ||
364 from + len < from || to + len < to)
368 down_read(&gmap->mm->mmap_sem);
369 spin_lock(&gmap->mm->page_table_lock);
370 for (off = 0; off < len; off += PMD_SIZE) {
371 /* Walk the gmap address space page table */
372 table = gmap->table + (((to + off) >> 53) & 0x7ff);
373 if ((*table & _REGION_ENTRY_INVALID) &&
374 gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY))
376 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
377 table = table + (((to + off) >> 42) & 0x7ff);
378 if ((*table & _REGION_ENTRY_INVALID) &&
379 gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY))
381 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
382 table = table + (((to + off) >> 31) & 0x7ff);
383 if ((*table & _REGION_ENTRY_INVALID) &&
384 gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY))
386 table = (unsigned long *) (*table & _REGION_ENTRY_ORIGIN);
387 table = table + (((to + off) >> 20) & 0x7ff);
389 /* Store 'from' address in an invalid segment table entry. */
390 flush |= gmap_unlink_segment(gmap, table);
391 *table = (from + off) | (_SEGMENT_ENTRY_INVALID |
392 _SEGMENT_ENTRY_PROTECT);
394 spin_unlock(&gmap->mm->page_table_lock);
395 up_read(&gmap->mm->mmap_sem);
397 gmap_flush_tlb(gmap);
401 spin_unlock(&gmap->mm->page_table_lock);
402 up_read(&gmap->mm->mmap_sem);
403 gmap_unmap_segment(gmap, to, len);
406 EXPORT_SYMBOL_GPL(gmap_map_segment);
408 static unsigned long *gmap_table_walk(unsigned long address, struct gmap *gmap)
410 unsigned long *table;
412 table = gmap->table + ((address >> 53) & 0x7ff);
413 if (unlikely(*table & _REGION_ENTRY_INVALID))
414 return ERR_PTR(-EFAULT);
415 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
416 table = table + ((address >> 42) & 0x7ff);
417 if (unlikely(*table & _REGION_ENTRY_INVALID))
418 return ERR_PTR(-EFAULT);
419 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
420 table = table + ((address >> 31) & 0x7ff);
421 if (unlikely(*table & _REGION_ENTRY_INVALID))
422 return ERR_PTR(-EFAULT);
423 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
424 table = table + ((address >> 20) & 0x7ff);
429 * __gmap_translate - translate a guest address to a user space address
430 * @address: guest address
431 * @gmap: pointer to guest mapping meta data structure
433 * Returns user space address which corresponds to the guest address or
434 * -EFAULT if no such mapping exists.
435 * This function does not establish potentially missing page table entries.
436 * The mmap_sem of the mm that belongs to the address space must be held
437 * when this function gets called.
439 unsigned long __gmap_translate(unsigned long address, struct gmap *gmap)
441 unsigned long *segment_ptr, vmaddr, segment;
442 struct gmap_pgtable *mp;
445 current->thread.gmap_addr = address;
446 segment_ptr = gmap_table_walk(address, gmap);
447 if (IS_ERR(segment_ptr))
448 return PTR_ERR(segment_ptr);
449 /* Convert the gmap address to an mm address. */
450 segment = *segment_ptr;
451 if (!(segment & _SEGMENT_ENTRY_INVALID)) {
452 page = pfn_to_page(segment >> PAGE_SHIFT);
453 mp = (struct gmap_pgtable *) page->index;
454 return mp->vmaddr | (address & ~PMD_MASK);
455 } else if (segment & _SEGMENT_ENTRY_PROTECT) {
456 vmaddr = segment & _SEGMENT_ENTRY_ORIGIN;
457 return vmaddr | (address & ~PMD_MASK);
461 EXPORT_SYMBOL_GPL(__gmap_translate);
464 * gmap_translate - translate a guest address to a user space address
465 * @address: guest address
466 * @gmap: pointer to guest mapping meta data structure
468 * Returns user space address which corresponds to the guest address or
469 * -EFAULT if no such mapping exists.
470 * This function does not establish potentially missing page table entries.
472 unsigned long gmap_translate(unsigned long address, struct gmap *gmap)
476 down_read(&gmap->mm->mmap_sem);
477 rc = __gmap_translate(address, gmap);
478 up_read(&gmap->mm->mmap_sem);
481 EXPORT_SYMBOL_GPL(gmap_translate);
483 static int gmap_connect_pgtable(unsigned long address, unsigned long segment,
484 unsigned long *segment_ptr, struct gmap *gmap)
486 unsigned long vmaddr;
487 struct vm_area_struct *vma;
488 struct gmap_pgtable *mp;
489 struct gmap_rmap *rmap;
490 struct mm_struct *mm;
497 vmaddr = segment & _SEGMENT_ENTRY_ORIGIN;
498 vma = find_vma(mm, vmaddr);
499 if (!vma || vma->vm_start > vmaddr)
501 /* Walk the parent mm page table */
502 pgd = pgd_offset(mm, vmaddr);
503 pud = pud_alloc(mm, pgd, vmaddr);
506 pmd = pmd_alloc(mm, pud, vmaddr);
509 if (!pmd_present(*pmd) &&
510 __pte_alloc(mm, vma, pmd, vmaddr))
512 /* large pmds cannot yet be handled */
515 /* pmd now points to a valid segment table entry. */
516 rmap = kmalloc(sizeof(*rmap), GFP_KERNEL|__GFP_REPEAT);
519 /* Link gmap segment table entry location to page table. */
520 page = pmd_page(*pmd);
521 mp = (struct gmap_pgtable *) page->index;
523 rmap->entry = segment_ptr;
524 rmap->vmaddr = address & PMD_MASK;
525 spin_lock(&mm->page_table_lock);
526 if (*segment_ptr == segment) {
527 list_add(&rmap->list, &mp->mapper);
528 /* Set gmap segment table entry to page table. */
529 *segment_ptr = pmd_val(*pmd) & PAGE_MASK;
532 spin_unlock(&mm->page_table_lock);
537 static void gmap_disconnect_pgtable(struct mm_struct *mm, unsigned long *table)
539 struct gmap_rmap *rmap, *next;
540 struct gmap_pgtable *mp;
545 spin_lock(&mm->page_table_lock);
546 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
547 mp = (struct gmap_pgtable *) page->index;
548 list_for_each_entry_safe(rmap, next, &mp->mapper, list) {
549 *rmap->entry = mp->vmaddr | (_SEGMENT_ENTRY_INVALID |
550 _SEGMENT_ENTRY_PROTECT);
551 list_del(&rmap->list);
555 spin_unlock(&mm->page_table_lock);
557 __tlb_flush_global();
561 * this function is assumed to be called with mmap_sem held
563 unsigned long __gmap_fault(unsigned long address, struct gmap *gmap)
565 unsigned long *segment_ptr, segment;
566 struct gmap_pgtable *mp;
570 current->thread.gmap_addr = address;
571 segment_ptr = gmap_table_walk(address, gmap);
572 if (IS_ERR(segment_ptr))
574 /* Convert the gmap address to an mm address. */
576 segment = *segment_ptr;
577 if (!(segment & _SEGMENT_ENTRY_INVALID)) {
578 /* Page table is present */
579 page = pfn_to_page(segment >> PAGE_SHIFT);
580 mp = (struct gmap_pgtable *) page->index;
581 return mp->vmaddr | (address & ~PMD_MASK);
583 if (!(segment & _SEGMENT_ENTRY_PROTECT))
584 /* Nothing mapped in the gmap address space. */
586 rc = gmap_connect_pgtable(address, segment, segment_ptr, gmap);
593 unsigned long gmap_fault(unsigned long address, struct gmap *gmap)
597 down_read(&gmap->mm->mmap_sem);
598 rc = __gmap_fault(address, gmap);
599 up_read(&gmap->mm->mmap_sem);
603 EXPORT_SYMBOL_GPL(gmap_fault);
605 static void gmap_zap_swap_entry(swp_entry_t entry, struct mm_struct *mm)
607 if (!non_swap_entry(entry))
608 dec_mm_counter(mm, MM_SWAPENTS);
609 else if (is_migration_entry(entry)) {
610 struct page *page = migration_entry_to_page(entry);
613 dec_mm_counter(mm, MM_ANONPAGES);
615 dec_mm_counter(mm, MM_FILEPAGES);
617 free_swap_and_cache(entry);
621 * The mm->mmap_sem lock must be held
623 static void gmap_zap_unused(struct mm_struct *mm, unsigned long address)
625 unsigned long ptev, pgstev;
630 ptep = get_locked_pte(mm, address, &ptl);
636 /* Zap unused and logically-zero pages */
637 pgste = pgste_get_lock(ptep);
638 pgstev = pgste_val(pgste);
640 if (((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED) ||
641 ((pgstev & _PGSTE_GPS_ZERO) && (ptev & _PAGE_INVALID))) {
642 gmap_zap_swap_entry(pte_to_swp_entry(pte), mm);
643 pte_clear(mm, address, ptep);
645 pgste_set_unlock(ptep, pgste);
647 pte_unmap_unlock(*ptep, ptl);
651 * this function is assumed to be called with mmap_sem held
653 void __gmap_zap(unsigned long address, struct gmap *gmap)
655 unsigned long *table, *segment_ptr;
656 unsigned long segment, pgstev, ptev;
657 struct gmap_pgtable *mp;
660 segment_ptr = gmap_table_walk(address, gmap);
661 if (IS_ERR(segment_ptr))
663 segment = *segment_ptr;
664 if (segment & _SEGMENT_ENTRY_INVALID)
666 page = pfn_to_page(segment >> PAGE_SHIFT);
667 mp = (struct gmap_pgtable *) page->index;
668 address = mp->vmaddr | (address & ~PMD_MASK);
669 /* Page table is present */
670 table = (unsigned long *)(segment & _SEGMENT_ENTRY_ORIGIN);
671 table = table + ((address >> 12) & 0xff);
672 pgstev = table[PTRS_PER_PTE];
674 /* quick check, checked again with locks held */
675 if (((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED) ||
676 ((pgstev & _PGSTE_GPS_ZERO) && (ptev & _PAGE_INVALID)))
677 gmap_zap_unused(gmap->mm, address);
679 EXPORT_SYMBOL_GPL(__gmap_zap);
681 void gmap_discard(unsigned long from, unsigned long to, struct gmap *gmap)
684 unsigned long *table, address, size;
685 struct vm_area_struct *vma;
686 struct gmap_pgtable *mp;
689 down_read(&gmap->mm->mmap_sem);
691 while (address < to) {
692 /* Walk the gmap address space page table */
693 table = gmap->table + ((address >> 53) & 0x7ff);
694 if (unlikely(*table & _REGION_ENTRY_INVALID)) {
695 address = (address + PMD_SIZE) & PMD_MASK;
698 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
699 table = table + ((address >> 42) & 0x7ff);
700 if (unlikely(*table & _REGION_ENTRY_INVALID)) {
701 address = (address + PMD_SIZE) & PMD_MASK;
704 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
705 table = table + ((address >> 31) & 0x7ff);
706 if (unlikely(*table & _REGION_ENTRY_INVALID)) {
707 address = (address + PMD_SIZE) & PMD_MASK;
710 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
711 table = table + ((address >> 20) & 0x7ff);
712 if (unlikely(*table & _SEGMENT_ENTRY_INVALID)) {
713 address = (address + PMD_SIZE) & PMD_MASK;
716 page = pfn_to_page(*table >> PAGE_SHIFT);
717 mp = (struct gmap_pgtable *) page->index;
718 vma = find_vma(gmap->mm, mp->vmaddr);
719 size = min(to - address, PMD_SIZE - (address & ~PMD_MASK));
720 zap_page_range(vma, mp->vmaddr | (address & ~PMD_MASK),
722 address = (address + PMD_SIZE) & PMD_MASK;
724 up_read(&gmap->mm->mmap_sem);
726 EXPORT_SYMBOL_GPL(gmap_discard);
728 static LIST_HEAD(gmap_notifier_list);
729 static DEFINE_SPINLOCK(gmap_notifier_lock);
732 * gmap_register_ipte_notifier - register a pte invalidation callback
733 * @nb: pointer to the gmap notifier block
735 void gmap_register_ipte_notifier(struct gmap_notifier *nb)
737 spin_lock(&gmap_notifier_lock);
738 list_add(&nb->list, &gmap_notifier_list);
739 spin_unlock(&gmap_notifier_lock);
741 EXPORT_SYMBOL_GPL(gmap_register_ipte_notifier);
744 * gmap_unregister_ipte_notifier - remove a pte invalidation callback
745 * @nb: pointer to the gmap notifier block
747 void gmap_unregister_ipte_notifier(struct gmap_notifier *nb)
749 spin_lock(&gmap_notifier_lock);
750 list_del_init(&nb->list);
751 spin_unlock(&gmap_notifier_lock);
753 EXPORT_SYMBOL_GPL(gmap_unregister_ipte_notifier);
756 * gmap_ipte_notify - mark a range of ptes for invalidation notification
757 * @gmap: pointer to guest mapping meta data structure
758 * @start: virtual address in the guest address space
761 * Returns 0 if for each page in the given range a gmap mapping exists and
762 * the invalidation notification could be set. If the gmap mapping is missing
763 * for one or more pages -EFAULT is returned. If no memory could be allocated
764 * -ENOMEM is returned. This function establishes missing page table entries.
766 int gmap_ipte_notify(struct gmap *gmap, unsigned long start, unsigned long len)
774 if ((start & ~PAGE_MASK) || (len & ~PAGE_MASK))
776 down_read(&gmap->mm->mmap_sem);
778 /* Convert gmap address and connect the page tables */
779 addr = __gmap_fault(start, gmap);
780 if (IS_ERR_VALUE(addr)) {
784 /* Get the page mapped */
785 if (fixup_user_fault(current, gmap->mm, addr, FAULT_FLAG_WRITE)) {
789 /* Walk the process page table, lock and get pte pointer */
790 ptep = get_locked_pte(gmap->mm, addr, &ptl);
793 /* Set notification bit in the pgste of the pte */
795 if ((pte_val(entry) & (_PAGE_INVALID | _PAGE_PROTECT)) == 0) {
796 pgste = pgste_get_lock(ptep);
797 pgste_val(pgste) |= PGSTE_IN_BIT;
798 pgste_set_unlock(ptep, pgste);
804 up_read(&gmap->mm->mmap_sem);
807 EXPORT_SYMBOL_GPL(gmap_ipte_notify);
810 * gmap_do_ipte_notify - call all invalidation callbacks for a specific pte.
811 * @mm: pointer to the process mm_struct
812 * @addr: virtual address in the process address space
813 * @pte: pointer to the page table entry
815 * This function is assumed to be called with the page table lock held
816 * for the pte to notify.
818 void gmap_do_ipte_notify(struct mm_struct *mm, unsigned long addr, pte_t *pte)
820 unsigned long segment_offset;
821 struct gmap_notifier *nb;
822 struct gmap_pgtable *mp;
823 struct gmap_rmap *rmap;
826 segment_offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
827 segment_offset = segment_offset * (4096 / sizeof(pte_t));
828 page = pfn_to_page(__pa(pte) >> PAGE_SHIFT);
829 mp = (struct gmap_pgtable *) page->index;
830 spin_lock(&gmap_notifier_lock);
831 list_for_each_entry(rmap, &mp->mapper, list) {
832 list_for_each_entry(nb, &gmap_notifier_list, list)
833 nb->notifier_call(rmap->gmap,
834 rmap->vmaddr + segment_offset);
836 spin_unlock(&gmap_notifier_lock);
838 EXPORT_SYMBOL_GPL(gmap_do_ipte_notify);
840 static inline int page_table_with_pgste(struct page *page)
842 return atomic_read(&page->_mapcount) == 0;
845 static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm,
846 unsigned long vmaddr)
849 unsigned long *table;
850 struct gmap_pgtable *mp;
852 page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
855 mp = kmalloc(sizeof(*mp), GFP_KERNEL|__GFP_REPEAT);
860 if (!pgtable_page_ctor(page)) {
865 mp->vmaddr = vmaddr & PMD_MASK;
866 INIT_LIST_HEAD(&mp->mapper);
867 page->index = (unsigned long) mp;
868 atomic_set(&page->_mapcount, 0);
869 table = (unsigned long *) page_to_phys(page);
870 clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
871 clear_table(table + PTRS_PER_PTE, 0, PAGE_SIZE/2);
875 static inline void page_table_free_pgste(unsigned long *table)
878 struct gmap_pgtable *mp;
880 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
881 mp = (struct gmap_pgtable *) page->index;
882 BUG_ON(!list_empty(&mp->mapper));
883 pgtable_page_dtor(page);
884 atomic_set(&page->_mapcount, -1);
889 static inline unsigned long page_table_reset_pte(struct mm_struct *mm, pmd_t *pmd,
890 unsigned long addr, unsigned long end, bool init_skey)
892 pte_t *start_pte, *pte;
896 start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
899 pgste = pgste_get_lock(pte);
900 pgste_val(pgste) &= ~_PGSTE_GPS_USAGE_MASK;
902 unsigned long address;
904 pgste_val(pgste) &= ~(PGSTE_ACC_BITS | PGSTE_FP_BIT |
905 PGSTE_GR_BIT | PGSTE_GC_BIT);
907 /* skip invalid and not writable pages */
908 if (pte_val(*pte) & _PAGE_INVALID ||
909 !(pte_val(*pte) & _PAGE_WRITE)) {
910 pgste_set_unlock(pte, pgste);
914 address = pte_val(*pte) & PAGE_MASK;
915 page_set_storage_key(address, PAGE_DEFAULT_KEY, 1);
917 pgste_set_unlock(pte, pgste);
918 } while (pte++, addr += PAGE_SIZE, addr != end);
919 pte_unmap_unlock(start_pte, ptl);
924 static inline unsigned long page_table_reset_pmd(struct mm_struct *mm, pud_t *pud,
925 unsigned long addr, unsigned long end, bool init_skey)
930 pmd = pmd_offset(pud, addr);
932 next = pmd_addr_end(addr, end);
933 if (pmd_none_or_clear_bad(pmd))
935 next = page_table_reset_pte(mm, pmd, addr, next, init_skey);
936 } while (pmd++, addr = next, addr != end);
941 static inline unsigned long page_table_reset_pud(struct mm_struct *mm, pgd_t *pgd,
942 unsigned long addr, unsigned long end, bool init_skey)
947 pud = pud_offset(pgd, addr);
949 next = pud_addr_end(addr, end);
950 if (pud_none_or_clear_bad(pud))
952 next = page_table_reset_pmd(mm, pud, addr, next, init_skey);
953 } while (pud++, addr = next, addr != end);
958 void page_table_reset_pgste(struct mm_struct *mm, unsigned long start,
959 unsigned long end, bool init_skey)
961 unsigned long addr, next;
964 down_write(&mm->mmap_sem);
965 if (init_skey && mm_use_skey(mm))
968 pgd = pgd_offset(mm, addr);
970 next = pgd_addr_end(addr, end);
971 if (pgd_none_or_clear_bad(pgd))
973 next = page_table_reset_pud(mm, pgd, addr, next, init_skey);
974 } while (pgd++, addr = next, addr != end);
976 current->mm->context.use_skey = 1;
978 up_write(&mm->mmap_sem);
980 EXPORT_SYMBOL(page_table_reset_pgste);
982 int set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
983 unsigned long key, bool nq)
989 down_read(&mm->mmap_sem);
991 ptep = get_locked_pte(current->mm, addr, &ptl);
992 if (unlikely(!ptep)) {
993 up_read(&mm->mmap_sem);
996 if (!(pte_val(*ptep) & _PAGE_INVALID) &&
997 (pte_val(*ptep) & _PAGE_PROTECT)) {
998 pte_unmap_unlock(*ptep, ptl);
999 if (fixup_user_fault(current, mm, addr, FAULT_FLAG_WRITE)) {
1000 up_read(&mm->mmap_sem);
1006 new = old = pgste_get_lock(ptep);
1007 pgste_val(new) &= ~(PGSTE_GR_BIT | PGSTE_GC_BIT |
1008 PGSTE_ACC_BITS | PGSTE_FP_BIT);
1009 pgste_val(new) |= (key & (_PAGE_CHANGED | _PAGE_REFERENCED)) << 48;
1010 pgste_val(new) |= (key & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
1011 if (!(pte_val(*ptep) & _PAGE_INVALID)) {
1012 unsigned long address, bits, skey;
1014 address = pte_val(*ptep) & PAGE_MASK;
1015 skey = (unsigned long) page_get_storage_key(address);
1016 bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
1017 skey = key & (_PAGE_ACC_BITS | _PAGE_FP_BIT);
1018 /* Set storage key ACC and FP */
1019 page_set_storage_key(address, skey, !nq);
1020 /* Merge host changed & referenced into pgste */
1021 pgste_val(new) |= bits << 52;
1023 /* changing the guest storage key is considered a change of the page */
1024 if ((pgste_val(new) ^ pgste_val(old)) &
1025 (PGSTE_ACC_BITS | PGSTE_FP_BIT | PGSTE_GR_BIT | PGSTE_GC_BIT))
1026 pgste_val(new) |= PGSTE_UC_BIT;
1028 pgste_set_unlock(ptep, new);
1029 pte_unmap_unlock(*ptep, ptl);
1030 up_read(&mm->mmap_sem);
1033 EXPORT_SYMBOL(set_guest_storage_key);
1035 #else /* CONFIG_PGSTE */
1037 static inline int page_table_with_pgste(struct page *page)
1042 static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm,
1043 unsigned long vmaddr)
1048 void page_table_reset_pgste(struct mm_struct *mm, unsigned long start,
1049 unsigned long end, bool init_skey)
1053 static inline void page_table_free_pgste(unsigned long *table)
1057 static inline void gmap_disconnect_pgtable(struct mm_struct *mm,
1058 unsigned long *table)
1062 #endif /* CONFIG_PGSTE */
1064 static inline unsigned int atomic_xor_bits(atomic_t *v, unsigned int bits)
1066 unsigned int old, new;
1069 old = atomic_read(v);
1071 } while (atomic_cmpxchg(v, old, new) != old);
1076 * page table entry allocation/free routines.
1078 unsigned long *page_table_alloc(struct mm_struct *mm, unsigned long vmaddr)
1080 unsigned long *uninitialized_var(table);
1081 struct page *uninitialized_var(page);
1082 unsigned int mask, bit;
1084 if (mm_has_pgste(mm))
1085 return page_table_alloc_pgste(mm, vmaddr);
1086 /* Allocate fragments of a 4K page as 1K/2K page table */
1087 spin_lock_bh(&mm->context.list_lock);
1089 if (!list_empty(&mm->context.pgtable_list)) {
1090 page = list_first_entry(&mm->context.pgtable_list,
1092 table = (unsigned long *) page_to_phys(page);
1093 mask = atomic_read(&page->_mapcount);
1094 mask = mask | (mask >> 4);
1096 if ((mask & FRAG_MASK) == FRAG_MASK) {
1097 spin_unlock_bh(&mm->context.list_lock);
1098 page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
1101 if (!pgtable_page_ctor(page)) {
1105 atomic_set(&page->_mapcount, 1);
1106 table = (unsigned long *) page_to_phys(page);
1107 clear_table(table, _PAGE_INVALID, PAGE_SIZE);
1108 spin_lock_bh(&mm->context.list_lock);
1109 list_add(&page->lru, &mm->context.pgtable_list);
1111 for (bit = 1; mask & bit; bit <<= 1)
1112 table += PTRS_PER_PTE;
1113 mask = atomic_xor_bits(&page->_mapcount, bit);
1114 if ((mask & FRAG_MASK) == FRAG_MASK)
1115 list_del(&page->lru);
1117 spin_unlock_bh(&mm->context.list_lock);
1121 void page_table_free(struct mm_struct *mm, unsigned long *table)
1124 unsigned int bit, mask;
1126 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1127 if (page_table_with_pgste(page)) {
1128 gmap_disconnect_pgtable(mm, table);
1129 return page_table_free_pgste(table);
1131 /* Free 1K/2K page table fragment of a 4K page */
1132 bit = 1 << ((__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t)));
1133 spin_lock_bh(&mm->context.list_lock);
1134 if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK)
1135 list_del(&page->lru);
1136 mask = atomic_xor_bits(&page->_mapcount, bit);
1137 if (mask & FRAG_MASK)
1138 list_add(&page->lru, &mm->context.pgtable_list);
1139 spin_unlock_bh(&mm->context.list_lock);
1141 pgtable_page_dtor(page);
1142 atomic_set(&page->_mapcount, -1);
1147 static void __page_table_free_rcu(void *table, unsigned bit)
1151 if (bit == FRAG_MASK)
1152 return page_table_free_pgste(table);
1153 /* Free 1K/2K page table fragment of a 4K page */
1154 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1155 if (atomic_xor_bits(&page->_mapcount, bit) == 0) {
1156 pgtable_page_dtor(page);
1157 atomic_set(&page->_mapcount, -1);
1162 void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table)
1164 struct mm_struct *mm;
1166 unsigned int bit, mask;
1169 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1170 if (page_table_with_pgste(page)) {
1171 gmap_disconnect_pgtable(mm, table);
1172 table = (unsigned long *) (__pa(table) | FRAG_MASK);
1173 tlb_remove_table(tlb, table);
1176 bit = 1 << ((__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t)));
1177 spin_lock_bh(&mm->context.list_lock);
1178 if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK)
1179 list_del(&page->lru);
1180 mask = atomic_xor_bits(&page->_mapcount, bit | (bit << 4));
1181 if (mask & FRAG_MASK)
1182 list_add_tail(&page->lru, &mm->context.pgtable_list);
1183 spin_unlock_bh(&mm->context.list_lock);
1184 table = (unsigned long *) (__pa(table) | (bit << 4));
1185 tlb_remove_table(tlb, table);
1188 static void __tlb_remove_table(void *_table)
1190 const unsigned long mask = (FRAG_MASK << 4) | FRAG_MASK;
1191 void *table = (void *)((unsigned long) _table & ~mask);
1192 unsigned type = (unsigned long) _table & mask;
1195 __page_table_free_rcu(table, type);
1197 free_pages((unsigned long) table, ALLOC_ORDER);
1200 static void tlb_remove_table_smp_sync(void *arg)
1202 /* Simply deliver the interrupt */
1205 static void tlb_remove_table_one(void *table)
1208 * This isn't an RCU grace period and hence the page-tables cannot be
1209 * assumed to be actually RCU-freed.
1211 * It is however sufficient for software page-table walkers that rely
1212 * on IRQ disabling. See the comment near struct mmu_table_batch.
1214 smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
1215 __tlb_remove_table(table);
1218 static void tlb_remove_table_rcu(struct rcu_head *head)
1220 struct mmu_table_batch *batch;
1223 batch = container_of(head, struct mmu_table_batch, rcu);
1225 for (i = 0; i < batch->nr; i++)
1226 __tlb_remove_table(batch->tables[i]);
1228 free_page((unsigned long)batch);
1231 void tlb_table_flush(struct mmu_gather *tlb)
1233 struct mmu_table_batch **batch = &tlb->batch;
1236 call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
1241 void tlb_remove_table(struct mmu_gather *tlb, void *table)
1243 struct mmu_table_batch **batch = &tlb->batch;
1245 tlb->mm->context.flush_mm = 1;
1246 if (*batch == NULL) {
1247 *batch = (struct mmu_table_batch *)
1248 __get_free_page(GFP_NOWAIT | __GFP_NOWARN);
1249 if (*batch == NULL) {
1250 __tlb_flush_mm_lazy(tlb->mm);
1251 tlb_remove_table_one(table);
1256 (*batch)->tables[(*batch)->nr++] = table;
1257 if ((*batch)->nr == MAX_TABLE_BATCH)
1261 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1262 static inline void thp_split_vma(struct vm_area_struct *vma)
1266 for (addr = vma->vm_start; addr < vma->vm_end; addr += PAGE_SIZE)
1267 follow_page(vma, addr, FOLL_SPLIT);
1270 static inline void thp_split_mm(struct mm_struct *mm)
1272 struct vm_area_struct *vma;
1274 for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) {
1276 vma->vm_flags &= ~VM_HUGEPAGE;
1277 vma->vm_flags |= VM_NOHUGEPAGE;
1279 mm->def_flags |= VM_NOHUGEPAGE;
1282 static inline void thp_split_mm(struct mm_struct *mm)
1285 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1287 static unsigned long page_table_realloc_pmd(struct mmu_gather *tlb,
1288 struct mm_struct *mm, pud_t *pud,
1289 unsigned long addr, unsigned long end)
1291 unsigned long next, *table, *new;
1296 pmd = pmd_offset(pud, addr);
1298 next = pmd_addr_end(addr, end);
1300 if (pmd_none_or_clear_bad(pmd))
1302 table = (unsigned long *) pmd_deref(*pmd);
1303 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1304 if (page_table_with_pgste(page))
1306 /* Allocate new page table with pgstes */
1307 new = page_table_alloc_pgste(mm, addr);
1311 ptl = pmd_lock(mm, pmd);
1312 if (likely((unsigned long *) pmd_deref(*pmd) == table)) {
1313 /* Nuke pmd entry pointing to the "short" page table */
1314 pmdp_flush_lazy(mm, addr, pmd);
1316 /* Copy ptes from old table to new table */
1317 memcpy(new, table, PAGE_SIZE/2);
1318 clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
1319 /* Establish new table */
1320 pmd_populate(mm, pmd, (pte_t *) new);
1321 /* Free old table with rcu, there might be a walker! */
1322 page_table_free_rcu(tlb, table);
1327 page_table_free_pgste(new);
1330 } while (pmd++, addr = next, addr != end);
1335 static unsigned long page_table_realloc_pud(struct mmu_gather *tlb,
1336 struct mm_struct *mm, pgd_t *pgd,
1337 unsigned long addr, unsigned long end)
1342 pud = pud_offset(pgd, addr);
1344 next = pud_addr_end(addr, end);
1345 if (pud_none_or_clear_bad(pud))
1347 next = page_table_realloc_pmd(tlb, mm, pud, addr, next);
1348 if (unlikely(IS_ERR_VALUE(next)))
1350 } while (pud++, addr = next, addr != end);
1355 static unsigned long page_table_realloc(struct mmu_gather *tlb, struct mm_struct *mm,
1356 unsigned long addr, unsigned long end)
1361 pgd = pgd_offset(mm, addr);
1363 next = pgd_addr_end(addr, end);
1364 if (pgd_none_or_clear_bad(pgd))
1366 next = page_table_realloc_pud(tlb, mm, pgd, addr, next);
1367 if (unlikely(IS_ERR_VALUE(next)))
1369 } while (pgd++, addr = next, addr != end);
1375 * switch on pgstes for its userspace process (for kvm)
1377 int s390_enable_sie(void)
1379 struct task_struct *tsk = current;
1380 struct mm_struct *mm = tsk->mm;
1381 struct mmu_gather tlb;
1383 /* Do we have pgstes? if yes, we are done */
1384 if (mm_has_pgste(tsk->mm))
1387 down_write(&mm->mmap_sem);
1388 /* split thp mappings and disable thp for future mappings */
1390 /* Reallocate the page tables with pgstes */
1391 tlb_gather_mmu(&tlb, mm, 0, TASK_SIZE);
1392 if (!page_table_realloc(&tlb, mm, 0, TASK_SIZE))
1393 mm->context.has_pgste = 1;
1394 tlb_finish_mmu(&tlb, 0, TASK_SIZE);
1395 up_write(&mm->mmap_sem);
1396 return mm->context.has_pgste ? 0 : -ENOMEM;
1398 EXPORT_SYMBOL_GPL(s390_enable_sie);
1401 * Enable storage key handling from now on and initialize the storage
1402 * keys with the default key.
1404 void s390_enable_skey(void)
1406 page_table_reset_pgste(current->mm, 0, TASK_SIZE, true);
1408 EXPORT_SYMBOL_GPL(s390_enable_skey);
1411 * Test and reset if a guest page is dirty
1413 bool gmap_test_and_clear_dirty(unsigned long address, struct gmap *gmap)
1419 pte = get_locked_pte(gmap->mm, address, &ptl);
1423 if (ptep_test_and_clear_user_dirty(gmap->mm, address, pte))
1429 EXPORT_SYMBOL_GPL(gmap_test_and_clear_dirty);
1431 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1432 int pmdp_clear_flush_young(struct vm_area_struct *vma, unsigned long address,
1435 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1436 /* No need to flush TLB
1437 * On s390 reference bits are in storage key and never in TLB */
1438 return pmdp_test_and_clear_young(vma, address, pmdp);
1441 int pmdp_set_access_flags(struct vm_area_struct *vma,
1442 unsigned long address, pmd_t *pmdp,
1443 pmd_t entry, int dirty)
1445 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1447 entry = pmd_mkyoung(entry);
1449 entry = pmd_mkdirty(entry);
1450 if (pmd_same(*pmdp, entry))
1452 pmdp_invalidate(vma, address, pmdp);
1453 set_pmd_at(vma->vm_mm, address, pmdp, entry);
1457 static void pmdp_splitting_flush_sync(void *arg)
1459 /* Simply deliver the interrupt */
1462 void pmdp_splitting_flush(struct vm_area_struct *vma, unsigned long address,
1465 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1466 if (!test_and_set_bit(_SEGMENT_ENTRY_SPLIT_BIT,
1467 (unsigned long *) pmdp)) {
1468 /* need to serialize against gup-fast (IRQ disabled) */
1469 smp_call_function(pmdp_splitting_flush_sync, NULL, 1);
1473 void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
1476 struct list_head *lh = (struct list_head *) pgtable;
1478 assert_spin_locked(pmd_lockptr(mm, pmdp));
1481 if (!pmd_huge_pte(mm, pmdp))
1484 list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
1485 pmd_huge_pte(mm, pmdp) = pgtable;
1488 pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
1490 struct list_head *lh;
1494 assert_spin_locked(pmd_lockptr(mm, pmdp));
1497 pgtable = pmd_huge_pte(mm, pmdp);
1498 lh = (struct list_head *) pgtable;
1500 pmd_huge_pte(mm, pmdp) = NULL;
1502 pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
1505 ptep = (pte_t *) pgtable;
1506 pte_val(*ptep) = _PAGE_INVALID;
1508 pte_val(*ptep) = _PAGE_INVALID;
1511 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */