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)
57 update_mm(mm, current);
61 int crst_table_upgrade(struct mm_struct *mm, unsigned long limit)
63 unsigned long *table, *pgd;
67 BUG_ON(limit > (1UL << 53));
70 table = crst_table_alloc(mm);
73 spin_lock_bh(&mm->page_table_lock);
74 if (mm->context.asce_limit < limit) {
75 pgd = (unsigned long *) mm->pgd;
76 if (mm->context.asce_limit <= (1UL << 31)) {
77 entry = _REGION3_ENTRY_EMPTY;
78 mm->context.asce_limit = 1UL << 42;
79 mm->context.asce_bits = _ASCE_TABLE_LENGTH |
83 entry = _REGION2_ENTRY_EMPTY;
84 mm->context.asce_limit = 1UL << 53;
85 mm->context.asce_bits = _ASCE_TABLE_LENGTH |
89 crst_table_init(table, entry);
90 pgd_populate(mm, (pgd_t *) table, (pud_t *) pgd);
91 mm->pgd = (pgd_t *) table;
92 mm->task_size = mm->context.asce_limit;
96 spin_unlock_bh(&mm->page_table_lock);
98 crst_table_free(mm, table);
99 if (mm->context.asce_limit < limit)
102 on_each_cpu(__crst_table_upgrade, mm, 0);
106 void crst_table_downgrade(struct mm_struct *mm, unsigned long limit)
110 if (current->active_mm == mm)
112 while (mm->context.asce_limit > limit) {
114 switch (pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) {
115 case _REGION_ENTRY_TYPE_R2:
116 mm->context.asce_limit = 1UL << 42;
117 mm->context.asce_bits = _ASCE_TABLE_LENGTH |
121 case _REGION_ENTRY_TYPE_R3:
122 mm->context.asce_limit = 1UL << 31;
123 mm->context.asce_bits = _ASCE_TABLE_LENGTH |
130 mm->pgd = (pgd_t *) (pgd_val(*pgd) & _REGION_ENTRY_ORIGIN);
131 mm->task_size = mm->context.asce_limit;
132 crst_table_free(mm, (unsigned long *) pgd);
134 if (current->active_mm == mm)
135 update_mm(mm, current);
142 * gmap_alloc - allocate a guest address space
143 * @mm: pointer to the parent mm_struct
145 * Returns a guest address space structure.
147 struct gmap *gmap_alloc(struct mm_struct *mm)
151 unsigned long *table;
153 gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL);
156 INIT_LIST_HEAD(&gmap->crst_list);
158 page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
161 list_add(&page->lru, &gmap->crst_list);
162 table = (unsigned long *) page_to_phys(page);
163 crst_table_init(table, _REGION1_ENTRY_EMPTY);
165 gmap->asce = _ASCE_TYPE_REGION1 | _ASCE_TABLE_LENGTH |
166 _ASCE_USER_BITS | __pa(table);
167 list_add(&gmap->list, &mm->context.gmap_list);
175 EXPORT_SYMBOL_GPL(gmap_alloc);
177 static int gmap_unlink_segment(struct gmap *gmap, unsigned long *table)
179 struct gmap_pgtable *mp;
180 struct gmap_rmap *rmap;
183 if (*table & _SEGMENT_ENTRY_INVALID)
185 page = pfn_to_page(*table >> PAGE_SHIFT);
186 mp = (struct gmap_pgtable *) page->index;
187 list_for_each_entry(rmap, &mp->mapper, list) {
188 if (rmap->entry != table)
190 list_del(&rmap->list);
194 *table = mp->vmaddr | _SEGMENT_ENTRY_INVALID | _SEGMENT_ENTRY_PROTECT;
198 static void gmap_flush_tlb(struct gmap *gmap)
200 if (MACHINE_HAS_IDTE)
201 __tlb_flush_idte((unsigned long) gmap->table |
204 __tlb_flush_global();
208 * gmap_free - free a guest address space
209 * @gmap: pointer to the guest address space structure
211 void gmap_free(struct gmap *gmap)
213 struct page *page, *next;
214 unsigned long *table;
219 if (MACHINE_HAS_IDTE)
220 __tlb_flush_idte((unsigned long) gmap->table |
223 __tlb_flush_global();
225 /* Free all segment & region tables. */
226 down_read(&gmap->mm->mmap_sem);
227 spin_lock(&gmap->mm->page_table_lock);
228 list_for_each_entry_safe(page, next, &gmap->crst_list, lru) {
229 table = (unsigned long *) page_to_phys(page);
230 if ((*table & _REGION_ENTRY_TYPE_MASK) == 0)
231 /* Remove gmap rmap structures for segment table. */
232 for (i = 0; i < PTRS_PER_PMD; i++, table++)
233 gmap_unlink_segment(gmap, table);
234 __free_pages(page, ALLOC_ORDER);
236 spin_unlock(&gmap->mm->page_table_lock);
237 up_read(&gmap->mm->mmap_sem);
238 list_del(&gmap->list);
241 EXPORT_SYMBOL_GPL(gmap_free);
244 * gmap_enable - switch primary space to the guest address space
245 * @gmap: pointer to the guest address space structure
247 void gmap_enable(struct gmap *gmap)
249 S390_lowcore.gmap = (unsigned long) gmap;
251 EXPORT_SYMBOL_GPL(gmap_enable);
254 * gmap_disable - switch back to the standard primary address space
255 * @gmap: pointer to the guest address space structure
257 void gmap_disable(struct gmap *gmap)
259 S390_lowcore.gmap = 0UL;
261 EXPORT_SYMBOL_GPL(gmap_disable);
264 * gmap_alloc_table is assumed to be called with mmap_sem held
266 static int gmap_alloc_table(struct gmap *gmap,
267 unsigned long *table, unsigned long init)
268 __releases(&gmap->mm->page_table_lock)
269 __acquires(&gmap->mm->page_table_lock)
274 /* since we dont free the gmap table until gmap_free we can unlock */
275 spin_unlock(&gmap->mm->page_table_lock);
276 page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
277 spin_lock(&gmap->mm->page_table_lock);
280 new = (unsigned long *) page_to_phys(page);
281 crst_table_init(new, init);
282 if (*table & _REGION_ENTRY_INVALID) {
283 list_add(&page->lru, &gmap->crst_list);
284 *table = (unsigned long) new | _REGION_ENTRY_LENGTH |
285 (*table & _REGION_ENTRY_TYPE_MASK);
287 __free_pages(page, ALLOC_ORDER);
292 * gmap_unmap_segment - unmap segment from the guest address space
293 * @gmap: pointer to the guest address space structure
294 * @addr: address in the guest address space
295 * @len: length of the memory area to unmap
297 * Returns 0 if the unmap succeeded, -EINVAL if not.
299 int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len)
301 unsigned long *table;
305 if ((to | len) & (PMD_SIZE - 1))
307 if (len == 0 || to + len < to)
311 down_read(&gmap->mm->mmap_sem);
312 spin_lock(&gmap->mm->page_table_lock);
313 for (off = 0; off < len; off += PMD_SIZE) {
314 /* Walk the guest addr space page table */
315 table = gmap->table + (((to + off) >> 53) & 0x7ff);
316 if (*table & _REGION_ENTRY_INVALID)
318 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
319 table = table + (((to + off) >> 42) & 0x7ff);
320 if (*table & _REGION_ENTRY_INVALID)
322 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
323 table = table + (((to + off) >> 31) & 0x7ff);
324 if (*table & _REGION_ENTRY_INVALID)
326 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
327 table = table + (((to + off) >> 20) & 0x7ff);
329 /* Clear segment table entry in guest address space. */
330 flush |= gmap_unlink_segment(gmap, table);
331 *table = _SEGMENT_ENTRY_INVALID;
334 spin_unlock(&gmap->mm->page_table_lock);
335 up_read(&gmap->mm->mmap_sem);
337 gmap_flush_tlb(gmap);
340 EXPORT_SYMBOL_GPL(gmap_unmap_segment);
343 * gmap_mmap_segment - map a segment to the guest address space
344 * @gmap: pointer to the guest address space structure
345 * @from: source address in the parent address space
346 * @to: target address in the guest address space
348 * Returns 0 if the mmap succeeded, -EINVAL or -ENOMEM if not.
350 int gmap_map_segment(struct gmap *gmap, unsigned long from,
351 unsigned long to, unsigned long len)
353 unsigned long *table;
357 if ((from | to | len) & (PMD_SIZE - 1))
359 if (len == 0 || from + len > TASK_MAX_SIZE ||
360 from + len < from || to + len < to)
364 down_read(&gmap->mm->mmap_sem);
365 spin_lock(&gmap->mm->page_table_lock);
366 for (off = 0; off < len; off += PMD_SIZE) {
367 /* Walk the gmap address space page table */
368 table = gmap->table + (((to + off) >> 53) & 0x7ff);
369 if ((*table & _REGION_ENTRY_INVALID) &&
370 gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY))
372 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
373 table = table + (((to + off) >> 42) & 0x7ff);
374 if ((*table & _REGION_ENTRY_INVALID) &&
375 gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY))
377 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
378 table = table + (((to + off) >> 31) & 0x7ff);
379 if ((*table & _REGION_ENTRY_INVALID) &&
380 gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY))
382 table = (unsigned long *) (*table & _REGION_ENTRY_ORIGIN);
383 table = table + (((to + off) >> 20) & 0x7ff);
385 /* Store 'from' address in an invalid segment table entry. */
386 flush |= gmap_unlink_segment(gmap, table);
387 *table = (from + off) | (_SEGMENT_ENTRY_INVALID |
388 _SEGMENT_ENTRY_PROTECT);
390 spin_unlock(&gmap->mm->page_table_lock);
391 up_read(&gmap->mm->mmap_sem);
393 gmap_flush_tlb(gmap);
397 spin_unlock(&gmap->mm->page_table_lock);
398 up_read(&gmap->mm->mmap_sem);
399 gmap_unmap_segment(gmap, to, len);
402 EXPORT_SYMBOL_GPL(gmap_map_segment);
404 static unsigned long *gmap_table_walk(unsigned long address, struct gmap *gmap)
406 unsigned long *table;
408 table = gmap->table + ((address >> 53) & 0x7ff);
409 if (unlikely(*table & _REGION_ENTRY_INVALID))
410 return ERR_PTR(-EFAULT);
411 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
412 table = table + ((address >> 42) & 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 >> 31) & 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 >> 20) & 0x7ff);
425 * __gmap_translate - translate a guest address to a user space address
426 * @address: guest address
427 * @gmap: pointer to guest mapping meta data structure
429 * Returns user space address which corresponds to the guest address or
430 * -EFAULT if no such mapping exists.
431 * This function does not establish potentially missing page table entries.
432 * The mmap_sem of the mm that belongs to the address space must be held
433 * when this function gets called.
435 unsigned long __gmap_translate(unsigned long address, struct gmap *gmap)
437 unsigned long *segment_ptr, vmaddr, segment;
438 struct gmap_pgtable *mp;
441 current->thread.gmap_addr = address;
442 segment_ptr = gmap_table_walk(address, gmap);
443 if (IS_ERR(segment_ptr))
444 return PTR_ERR(segment_ptr);
445 /* Convert the gmap address to an mm address. */
446 segment = *segment_ptr;
447 if (!(segment & _SEGMENT_ENTRY_INVALID)) {
448 page = pfn_to_page(segment >> PAGE_SHIFT);
449 mp = (struct gmap_pgtable *) page->index;
450 return mp->vmaddr | (address & ~PMD_MASK);
451 } else if (segment & _SEGMENT_ENTRY_PROTECT) {
452 vmaddr = segment & _SEGMENT_ENTRY_ORIGIN;
453 return vmaddr | (address & ~PMD_MASK);
457 EXPORT_SYMBOL_GPL(__gmap_translate);
460 * gmap_translate - translate a guest address to a user space address
461 * @address: guest address
462 * @gmap: pointer to guest mapping meta data structure
464 * Returns user space address which corresponds to the guest address or
465 * -EFAULT if no such mapping exists.
466 * This function does not establish potentially missing page table entries.
468 unsigned long gmap_translate(unsigned long address, struct gmap *gmap)
472 down_read(&gmap->mm->mmap_sem);
473 rc = __gmap_translate(address, gmap);
474 up_read(&gmap->mm->mmap_sem);
477 EXPORT_SYMBOL_GPL(gmap_translate);
479 static int gmap_connect_pgtable(unsigned long address, unsigned long segment,
480 unsigned long *segment_ptr, struct gmap *gmap)
482 unsigned long vmaddr;
483 struct vm_area_struct *vma;
484 struct gmap_pgtable *mp;
485 struct gmap_rmap *rmap;
486 struct mm_struct *mm;
493 vmaddr = segment & _SEGMENT_ENTRY_ORIGIN;
494 vma = find_vma(mm, vmaddr);
495 if (!vma || vma->vm_start > vmaddr)
497 /* Walk the parent mm page table */
498 pgd = pgd_offset(mm, vmaddr);
499 pud = pud_alloc(mm, pgd, vmaddr);
502 pmd = pmd_alloc(mm, pud, vmaddr);
505 if (!pmd_present(*pmd) &&
506 __pte_alloc(mm, vma, pmd, vmaddr))
508 /* pmd now points to a valid segment table entry. */
509 rmap = kmalloc(sizeof(*rmap), GFP_KERNEL|__GFP_REPEAT);
512 /* Link gmap segment table entry location to page table. */
513 page = pmd_page(*pmd);
514 mp = (struct gmap_pgtable *) page->index;
516 rmap->entry = segment_ptr;
517 rmap->vmaddr = address & PMD_MASK;
518 spin_lock(&mm->page_table_lock);
519 if (*segment_ptr == segment) {
520 list_add(&rmap->list, &mp->mapper);
521 /* Set gmap segment table entry to page table. */
522 *segment_ptr = pmd_val(*pmd) & PAGE_MASK;
525 spin_unlock(&mm->page_table_lock);
530 static void gmap_disconnect_pgtable(struct mm_struct *mm, unsigned long *table)
532 struct gmap_rmap *rmap, *next;
533 struct gmap_pgtable *mp;
538 spin_lock(&mm->page_table_lock);
539 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
540 mp = (struct gmap_pgtable *) page->index;
541 list_for_each_entry_safe(rmap, next, &mp->mapper, list) {
542 *rmap->entry = mp->vmaddr | (_SEGMENT_ENTRY_INVALID |
543 _SEGMENT_ENTRY_PROTECT);
544 list_del(&rmap->list);
548 spin_unlock(&mm->page_table_lock);
550 __tlb_flush_global();
554 * this function is assumed to be called with mmap_sem held
556 unsigned long __gmap_fault(unsigned long address, struct gmap *gmap)
558 unsigned long *segment_ptr, segment;
559 struct gmap_pgtable *mp;
563 current->thread.gmap_addr = address;
564 segment_ptr = gmap_table_walk(address, gmap);
565 if (IS_ERR(segment_ptr))
567 /* Convert the gmap address to an mm address. */
569 segment = *segment_ptr;
570 if (!(segment & _SEGMENT_ENTRY_INVALID)) {
571 /* Page table is present */
572 page = pfn_to_page(segment >> PAGE_SHIFT);
573 mp = (struct gmap_pgtable *) page->index;
574 return mp->vmaddr | (address & ~PMD_MASK);
576 if (!(segment & _SEGMENT_ENTRY_PROTECT))
577 /* Nothing mapped in the gmap address space. */
579 rc = gmap_connect_pgtable(address, segment, segment_ptr, gmap);
586 unsigned long gmap_fault(unsigned long address, struct gmap *gmap)
590 down_read(&gmap->mm->mmap_sem);
591 rc = __gmap_fault(address, gmap);
592 up_read(&gmap->mm->mmap_sem);
596 EXPORT_SYMBOL_GPL(gmap_fault);
598 static void gmap_zap_swap_entry(swp_entry_t entry, struct mm_struct *mm)
600 if (!non_swap_entry(entry))
601 dec_mm_counter(mm, MM_SWAPENTS);
602 else if (is_migration_entry(entry)) {
603 struct page *page = migration_entry_to_page(entry);
606 dec_mm_counter(mm, MM_ANONPAGES);
608 dec_mm_counter(mm, MM_FILEPAGES);
610 free_swap_and_cache(entry);
614 * The mm->mmap_sem lock must be held
616 static void gmap_zap_unused(struct mm_struct *mm, unsigned long address)
618 unsigned long ptev, pgstev;
623 ptep = get_locked_pte(mm, address, &ptl);
629 /* Zap unused and logically-zero pages */
630 pgste = pgste_get_lock(ptep);
631 pgstev = pgste_val(pgste);
633 if (((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED) ||
634 ((pgstev & _PGSTE_GPS_ZERO) && (ptev & _PAGE_INVALID))) {
635 gmap_zap_swap_entry(pte_to_swp_entry(pte), mm);
636 pte_clear(mm, address, ptep);
638 pgste_set_unlock(ptep, pgste);
640 pte_unmap_unlock(*ptep, ptl);
644 * this function is assumed to be called with mmap_sem held
646 void __gmap_zap(unsigned long address, struct gmap *gmap)
648 unsigned long *table, *segment_ptr;
649 unsigned long segment, pgstev, ptev;
650 struct gmap_pgtable *mp;
653 segment_ptr = gmap_table_walk(address, gmap);
654 if (IS_ERR(segment_ptr))
656 segment = *segment_ptr;
657 if (segment & _SEGMENT_ENTRY_INVALID)
659 page = pfn_to_page(segment >> PAGE_SHIFT);
660 mp = (struct gmap_pgtable *) page->index;
661 address = mp->vmaddr | (address & ~PMD_MASK);
662 /* Page table is present */
663 table = (unsigned long *)(segment & _SEGMENT_ENTRY_ORIGIN);
664 table = table + ((address >> 12) & 0xff);
665 pgstev = table[PTRS_PER_PTE];
667 /* quick check, checked again with locks held */
668 if (((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED) ||
669 ((pgstev & _PGSTE_GPS_ZERO) && (ptev & _PAGE_INVALID)))
670 gmap_zap_unused(gmap->mm, address);
672 EXPORT_SYMBOL_GPL(__gmap_zap);
674 void gmap_discard(unsigned long from, unsigned long to, struct gmap *gmap)
677 unsigned long *table, address, size;
678 struct vm_area_struct *vma;
679 struct gmap_pgtable *mp;
682 down_read(&gmap->mm->mmap_sem);
684 while (address < to) {
685 /* Walk the gmap address space page table */
686 table = gmap->table + ((address >> 53) & 0x7ff);
687 if (unlikely(*table & _REGION_ENTRY_INVALID)) {
688 address = (address + PMD_SIZE) & PMD_MASK;
691 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
692 table = table + ((address >> 42) & 0x7ff);
693 if (unlikely(*table & _REGION_ENTRY_INVALID)) {
694 address = (address + PMD_SIZE) & PMD_MASK;
697 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
698 table = table + ((address >> 31) & 0x7ff);
699 if (unlikely(*table & _REGION_ENTRY_INVALID)) {
700 address = (address + PMD_SIZE) & PMD_MASK;
703 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
704 table = table + ((address >> 20) & 0x7ff);
705 if (unlikely(*table & _SEGMENT_ENTRY_INVALID)) {
706 address = (address + PMD_SIZE) & PMD_MASK;
709 page = pfn_to_page(*table >> PAGE_SHIFT);
710 mp = (struct gmap_pgtable *) page->index;
711 vma = find_vma(gmap->mm, mp->vmaddr);
712 size = min(to - address, PMD_SIZE - (address & ~PMD_MASK));
713 zap_page_range(vma, mp->vmaddr | (address & ~PMD_MASK),
715 address = (address + PMD_SIZE) & PMD_MASK;
717 up_read(&gmap->mm->mmap_sem);
719 EXPORT_SYMBOL_GPL(gmap_discard);
721 static LIST_HEAD(gmap_notifier_list);
722 static DEFINE_SPINLOCK(gmap_notifier_lock);
725 * gmap_register_ipte_notifier - register a pte invalidation callback
726 * @nb: pointer to the gmap notifier block
728 void gmap_register_ipte_notifier(struct gmap_notifier *nb)
730 spin_lock(&gmap_notifier_lock);
731 list_add(&nb->list, &gmap_notifier_list);
732 spin_unlock(&gmap_notifier_lock);
734 EXPORT_SYMBOL_GPL(gmap_register_ipte_notifier);
737 * gmap_unregister_ipte_notifier - remove a pte invalidation callback
738 * @nb: pointer to the gmap notifier block
740 void gmap_unregister_ipte_notifier(struct gmap_notifier *nb)
742 spin_lock(&gmap_notifier_lock);
743 list_del_init(&nb->list);
744 spin_unlock(&gmap_notifier_lock);
746 EXPORT_SYMBOL_GPL(gmap_unregister_ipte_notifier);
749 * gmap_ipte_notify - mark a range of ptes for invalidation notification
750 * @gmap: pointer to guest mapping meta data structure
751 * @start: virtual address in the guest address space
754 * Returns 0 if for each page in the given range a gmap mapping exists and
755 * the invalidation notification could be set. If the gmap mapping is missing
756 * for one or more pages -EFAULT is returned. If no memory could be allocated
757 * -ENOMEM is returned. This function establishes missing page table entries.
759 int gmap_ipte_notify(struct gmap *gmap, unsigned long start, unsigned long len)
767 if ((start & ~PAGE_MASK) || (len & ~PAGE_MASK))
769 down_read(&gmap->mm->mmap_sem);
771 /* Convert gmap address and connect the page tables */
772 addr = __gmap_fault(start, gmap);
773 if (IS_ERR_VALUE(addr)) {
777 /* Get the page mapped */
778 if (fixup_user_fault(current, gmap->mm, addr, FAULT_FLAG_WRITE)) {
782 /* Walk the process page table, lock and get pte pointer */
783 ptep = get_locked_pte(gmap->mm, addr, &ptl);
786 /* Set notification bit in the pgste of the pte */
788 if ((pte_val(entry) & (_PAGE_INVALID | _PAGE_PROTECT)) == 0) {
789 pgste = pgste_get_lock(ptep);
790 pgste_val(pgste) |= PGSTE_IN_BIT;
791 pgste_set_unlock(ptep, pgste);
797 up_read(&gmap->mm->mmap_sem);
800 EXPORT_SYMBOL_GPL(gmap_ipte_notify);
803 * gmap_do_ipte_notify - call all invalidation callbacks for a specific pte.
804 * @mm: pointer to the process mm_struct
805 * @pte: pointer to the page table entry
807 * This function is assumed to be called with the page table lock held
808 * for the pte to notify.
810 void gmap_do_ipte_notify(struct mm_struct *mm, pte_t *pte)
812 unsigned long segment_offset;
813 struct gmap_notifier *nb;
814 struct gmap_pgtable *mp;
815 struct gmap_rmap *rmap;
818 segment_offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
819 segment_offset = segment_offset * (4096 / sizeof(pte_t));
820 page = pfn_to_page(__pa(pte) >> PAGE_SHIFT);
821 mp = (struct gmap_pgtable *) page->index;
822 spin_lock(&gmap_notifier_lock);
823 list_for_each_entry(rmap, &mp->mapper, list) {
824 list_for_each_entry(nb, &gmap_notifier_list, list)
825 nb->notifier_call(rmap->gmap,
826 rmap->vmaddr + segment_offset);
828 spin_unlock(&gmap_notifier_lock);
831 static inline int page_table_with_pgste(struct page *page)
833 return atomic_read(&page->_mapcount) == 0;
836 static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm,
837 unsigned long vmaddr)
840 unsigned long *table;
841 struct gmap_pgtable *mp;
843 page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
846 mp = kmalloc(sizeof(*mp), GFP_KERNEL|__GFP_REPEAT);
851 if (!pgtable_page_ctor(page)) {
856 mp->vmaddr = vmaddr & PMD_MASK;
857 INIT_LIST_HEAD(&mp->mapper);
858 page->index = (unsigned long) mp;
859 atomic_set(&page->_mapcount, 0);
860 table = (unsigned long *) page_to_phys(page);
861 clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
862 clear_table(table + PTRS_PER_PTE, PGSTE_HR_BIT | PGSTE_HC_BIT,
867 static inline void page_table_free_pgste(unsigned long *table)
870 struct gmap_pgtable *mp;
872 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
873 mp = (struct gmap_pgtable *) page->index;
874 BUG_ON(!list_empty(&mp->mapper));
875 pgtable_page_dtor(page);
876 atomic_set(&page->_mapcount, -1);
881 static inline unsigned long page_table_reset_pte(struct mm_struct *mm,
882 pmd_t *pmd, unsigned long addr, unsigned long end)
884 pte_t *start_pte, *pte;
888 start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
891 pgste = pgste_get_lock(pte);
892 pgste_val(pgste) &= ~_PGSTE_GPS_USAGE_MASK;
893 pgste_set_unlock(pte, pgste);
894 } while (pte++, addr += PAGE_SIZE, addr != end);
895 pte_unmap_unlock(start_pte, ptl);
900 static inline unsigned long page_table_reset_pmd(struct mm_struct *mm,
901 pud_t *pud, unsigned long addr, unsigned long end)
906 pmd = pmd_offset(pud, addr);
908 next = pmd_addr_end(addr, end);
909 if (pmd_none_or_clear_bad(pmd))
911 next = page_table_reset_pte(mm, pmd, addr, next);
912 } while (pmd++, addr = next, addr != end);
917 static inline unsigned long page_table_reset_pud(struct mm_struct *mm,
918 pgd_t *pgd, unsigned long addr, unsigned long end)
923 pud = pud_offset(pgd, addr);
925 next = pud_addr_end(addr, end);
926 if (pud_none_or_clear_bad(pud))
928 next = page_table_reset_pmd(mm, pud, addr, next);
929 } while (pud++, addr = next, addr != end);
934 void page_table_reset_pgste(struct mm_struct *mm,
935 unsigned long start, unsigned long end)
937 unsigned long addr, next;
941 down_read(&mm->mmap_sem);
942 pgd = pgd_offset(mm, addr);
944 next = pgd_addr_end(addr, end);
945 if (pgd_none_or_clear_bad(pgd))
947 next = page_table_reset_pud(mm, pgd, addr, next);
948 } while (pgd++, addr = next, addr != end);
949 up_read(&mm->mmap_sem);
951 EXPORT_SYMBOL(page_table_reset_pgste);
953 int set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
954 unsigned long key, bool nq)
960 down_read(&mm->mmap_sem);
961 ptep = get_locked_pte(current->mm, addr, &ptl);
962 if (unlikely(!ptep)) {
963 up_read(&mm->mmap_sem);
967 new = old = pgste_get_lock(ptep);
968 pgste_val(new) &= ~(PGSTE_GR_BIT | PGSTE_GC_BIT |
969 PGSTE_ACC_BITS | PGSTE_FP_BIT);
970 pgste_val(new) |= (key & (_PAGE_CHANGED | _PAGE_REFERENCED)) << 48;
971 pgste_val(new) |= (key & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
972 if (!(pte_val(*ptep) & _PAGE_INVALID)) {
973 unsigned long address, bits, skey;
975 address = pte_val(*ptep) & PAGE_MASK;
976 skey = (unsigned long) page_get_storage_key(address);
977 bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
978 skey = key & (_PAGE_ACC_BITS | _PAGE_FP_BIT);
979 /* Set storage key ACC and FP */
980 page_set_storage_key(address, skey, !nq);
981 /* Merge host changed & referenced into pgste */
982 pgste_val(new) |= bits << 52;
984 /* changing the guest storage key is considered a change of the page */
985 if ((pgste_val(new) ^ pgste_val(old)) &
986 (PGSTE_ACC_BITS | PGSTE_FP_BIT | PGSTE_GR_BIT | PGSTE_GC_BIT))
987 pgste_val(new) |= PGSTE_HC_BIT;
989 pgste_set_unlock(ptep, new);
990 pte_unmap_unlock(*ptep, ptl);
991 up_read(&mm->mmap_sem);
994 EXPORT_SYMBOL(set_guest_storage_key);
996 #else /* CONFIG_PGSTE */
998 static inline int page_table_with_pgste(struct page *page)
1003 static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm,
1004 unsigned long vmaddr)
1009 static inline void page_table_free_pgste(unsigned long *table)
1013 static inline void gmap_disconnect_pgtable(struct mm_struct *mm,
1014 unsigned long *table)
1018 #endif /* CONFIG_PGSTE */
1020 static inline unsigned int atomic_xor_bits(atomic_t *v, unsigned int bits)
1022 unsigned int old, new;
1025 old = atomic_read(v);
1027 } while (atomic_cmpxchg(v, old, new) != old);
1032 * page table entry allocation/free routines.
1034 unsigned long *page_table_alloc(struct mm_struct *mm, unsigned long vmaddr)
1036 unsigned long *uninitialized_var(table);
1037 struct page *uninitialized_var(page);
1038 unsigned int mask, bit;
1040 if (mm_has_pgste(mm))
1041 return page_table_alloc_pgste(mm, vmaddr);
1042 /* Allocate fragments of a 4K page as 1K/2K page table */
1043 spin_lock_bh(&mm->context.list_lock);
1045 if (!list_empty(&mm->context.pgtable_list)) {
1046 page = list_first_entry(&mm->context.pgtable_list,
1048 table = (unsigned long *) page_to_phys(page);
1049 mask = atomic_read(&page->_mapcount);
1050 mask = mask | (mask >> 4);
1052 if ((mask & FRAG_MASK) == FRAG_MASK) {
1053 spin_unlock_bh(&mm->context.list_lock);
1054 page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
1057 if (!pgtable_page_ctor(page)) {
1061 atomic_set(&page->_mapcount, 1);
1062 table = (unsigned long *) page_to_phys(page);
1063 clear_table(table, _PAGE_INVALID, PAGE_SIZE);
1064 spin_lock_bh(&mm->context.list_lock);
1065 list_add(&page->lru, &mm->context.pgtable_list);
1067 for (bit = 1; mask & bit; bit <<= 1)
1068 table += PTRS_PER_PTE;
1069 mask = atomic_xor_bits(&page->_mapcount, bit);
1070 if ((mask & FRAG_MASK) == FRAG_MASK)
1071 list_del(&page->lru);
1073 spin_unlock_bh(&mm->context.list_lock);
1077 void page_table_free(struct mm_struct *mm, unsigned long *table)
1080 unsigned int bit, mask;
1082 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1083 if (page_table_with_pgste(page)) {
1084 gmap_disconnect_pgtable(mm, table);
1085 return page_table_free_pgste(table);
1087 /* Free 1K/2K page table fragment of a 4K page */
1088 bit = 1 << ((__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t)));
1089 spin_lock_bh(&mm->context.list_lock);
1090 if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK)
1091 list_del(&page->lru);
1092 mask = atomic_xor_bits(&page->_mapcount, bit);
1093 if (mask & FRAG_MASK)
1094 list_add(&page->lru, &mm->context.pgtable_list);
1095 spin_unlock_bh(&mm->context.list_lock);
1097 pgtable_page_dtor(page);
1098 atomic_set(&page->_mapcount, -1);
1103 static void __page_table_free_rcu(void *table, unsigned bit)
1107 if (bit == FRAG_MASK)
1108 return page_table_free_pgste(table);
1109 /* Free 1K/2K page table fragment of a 4K page */
1110 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1111 if (atomic_xor_bits(&page->_mapcount, bit) == 0) {
1112 pgtable_page_dtor(page);
1113 atomic_set(&page->_mapcount, -1);
1118 void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table)
1120 struct mm_struct *mm;
1122 unsigned int bit, mask;
1125 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1126 if (page_table_with_pgste(page)) {
1127 gmap_disconnect_pgtable(mm, table);
1128 table = (unsigned long *) (__pa(table) | FRAG_MASK);
1129 tlb_remove_table(tlb, table);
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 | (bit << 4));
1137 if (mask & FRAG_MASK)
1138 list_add_tail(&page->lru, &mm->context.pgtable_list);
1139 spin_unlock_bh(&mm->context.list_lock);
1140 table = (unsigned long *) (__pa(table) | (bit << 4));
1141 tlb_remove_table(tlb, table);
1144 static void __tlb_remove_table(void *_table)
1146 const unsigned long mask = (FRAG_MASK << 4) | FRAG_MASK;
1147 void *table = (void *)((unsigned long) _table & ~mask);
1148 unsigned type = (unsigned long) _table & mask;
1151 __page_table_free_rcu(table, type);
1153 free_pages((unsigned long) table, ALLOC_ORDER);
1156 static void tlb_remove_table_smp_sync(void *arg)
1158 /* Simply deliver the interrupt */
1161 static void tlb_remove_table_one(void *table)
1164 * This isn't an RCU grace period and hence the page-tables cannot be
1165 * assumed to be actually RCU-freed.
1167 * It is however sufficient for software page-table walkers that rely
1168 * on IRQ disabling. See the comment near struct mmu_table_batch.
1170 smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
1171 __tlb_remove_table(table);
1174 static void tlb_remove_table_rcu(struct rcu_head *head)
1176 struct mmu_table_batch *batch;
1179 batch = container_of(head, struct mmu_table_batch, rcu);
1181 for (i = 0; i < batch->nr; i++)
1182 __tlb_remove_table(batch->tables[i]);
1184 free_page((unsigned long)batch);
1187 void tlb_table_flush(struct mmu_gather *tlb)
1189 struct mmu_table_batch **batch = &tlb->batch;
1192 call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
1197 void tlb_remove_table(struct mmu_gather *tlb, void *table)
1199 struct mmu_table_batch **batch = &tlb->batch;
1201 tlb->mm->context.flush_mm = 1;
1202 if (*batch == NULL) {
1203 *batch = (struct mmu_table_batch *)
1204 __get_free_page(GFP_NOWAIT | __GFP_NOWARN);
1205 if (*batch == NULL) {
1206 __tlb_flush_mm_lazy(tlb->mm);
1207 tlb_remove_table_one(table);
1212 (*batch)->tables[(*batch)->nr++] = table;
1213 if ((*batch)->nr == MAX_TABLE_BATCH)
1217 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1218 static inline void thp_split_vma(struct vm_area_struct *vma)
1222 for (addr = vma->vm_start; addr < vma->vm_end; addr += PAGE_SIZE)
1223 follow_page(vma, addr, FOLL_SPLIT);
1226 static inline void thp_split_mm(struct mm_struct *mm)
1228 struct vm_area_struct *vma;
1230 for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) {
1232 vma->vm_flags &= ~VM_HUGEPAGE;
1233 vma->vm_flags |= VM_NOHUGEPAGE;
1235 mm->def_flags |= VM_NOHUGEPAGE;
1238 static inline void thp_split_mm(struct mm_struct *mm)
1241 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1243 static unsigned long page_table_realloc_pmd(struct mmu_gather *tlb,
1244 struct mm_struct *mm, pud_t *pud,
1245 unsigned long addr, unsigned long end)
1247 unsigned long next, *table, *new;
1251 pmd = pmd_offset(pud, addr);
1253 next = pmd_addr_end(addr, end);
1255 if (pmd_none_or_clear_bad(pmd))
1257 table = (unsigned long *) pmd_deref(*pmd);
1258 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1259 if (page_table_with_pgste(page))
1261 /* Allocate new page table with pgstes */
1262 new = page_table_alloc_pgste(mm, addr);
1266 spin_lock(&mm->page_table_lock);
1267 if (likely((unsigned long *) pmd_deref(*pmd) == table)) {
1268 /* Nuke pmd entry pointing to the "short" page table */
1269 pmdp_flush_lazy(mm, addr, pmd);
1271 /* Copy ptes from old table to new table */
1272 memcpy(new, table, PAGE_SIZE/2);
1273 clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
1274 /* Establish new table */
1275 pmd_populate(mm, pmd, (pte_t *) new);
1276 /* Free old table with rcu, there might be a walker! */
1277 page_table_free_rcu(tlb, table);
1280 spin_unlock(&mm->page_table_lock);
1282 page_table_free_pgste(new);
1285 } while (pmd++, addr = next, addr != end);
1290 static unsigned long page_table_realloc_pud(struct mmu_gather *tlb,
1291 struct mm_struct *mm, pgd_t *pgd,
1292 unsigned long addr, unsigned long end)
1297 pud = pud_offset(pgd, addr);
1299 next = pud_addr_end(addr, end);
1300 if (pud_none_or_clear_bad(pud))
1302 next = page_table_realloc_pmd(tlb, mm, pud, addr, next);
1303 if (unlikely(IS_ERR_VALUE(next)))
1305 } while (pud++, addr = next, addr != end);
1310 static unsigned long page_table_realloc(struct mmu_gather *tlb, struct mm_struct *mm,
1311 unsigned long addr, unsigned long end)
1316 pgd = pgd_offset(mm, addr);
1318 next = pgd_addr_end(addr, end);
1319 if (pgd_none_or_clear_bad(pgd))
1321 next = page_table_realloc_pud(tlb, mm, pgd, addr, next);
1322 if (unlikely(IS_ERR_VALUE(next)))
1324 } while (pgd++, addr = next, addr != end);
1330 * switch on pgstes for its userspace process (for kvm)
1332 int s390_enable_sie(void)
1334 struct task_struct *tsk = current;
1335 struct mm_struct *mm = tsk->mm;
1336 struct mmu_gather tlb;
1338 /* Do we have pgstes? if yes, we are done */
1339 if (mm_has_pgste(tsk->mm))
1342 down_write(&mm->mmap_sem);
1343 /* split thp mappings and disable thp for future mappings */
1345 /* Reallocate the page tables with pgstes */
1346 tlb_gather_mmu(&tlb, mm, 0, TASK_SIZE);
1347 if (!page_table_realloc(&tlb, mm, 0, TASK_SIZE))
1348 mm->context.has_pgste = 1;
1349 tlb_finish_mmu(&tlb, 0, TASK_SIZE);
1350 up_write(&mm->mmap_sem);
1351 return mm->context.has_pgste ? 0 : -ENOMEM;
1353 EXPORT_SYMBOL_GPL(s390_enable_sie);
1355 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1356 int pmdp_clear_flush_young(struct vm_area_struct *vma, unsigned long address,
1359 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1360 /* No need to flush TLB
1361 * On s390 reference bits are in storage key and never in TLB */
1362 return pmdp_test_and_clear_young(vma, address, pmdp);
1365 int pmdp_set_access_flags(struct vm_area_struct *vma,
1366 unsigned long address, pmd_t *pmdp,
1367 pmd_t entry, int dirty)
1369 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1371 if (pmd_same(*pmdp, entry))
1373 pmdp_invalidate(vma, address, pmdp);
1374 set_pmd_at(vma->vm_mm, address, pmdp, entry);
1378 static void pmdp_splitting_flush_sync(void *arg)
1380 /* Simply deliver the interrupt */
1383 void pmdp_splitting_flush(struct vm_area_struct *vma, unsigned long address,
1386 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1387 if (!test_and_set_bit(_SEGMENT_ENTRY_SPLIT_BIT,
1388 (unsigned long *) pmdp)) {
1389 /* need to serialize against gup-fast (IRQ disabled) */
1390 smp_call_function(pmdp_splitting_flush_sync, NULL, 1);
1394 void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
1397 struct list_head *lh = (struct list_head *) pgtable;
1399 assert_spin_locked(pmd_lockptr(mm, pmdp));
1402 if (!pmd_huge_pte(mm, pmdp))
1405 list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
1406 pmd_huge_pte(mm, pmdp) = pgtable;
1409 pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
1411 struct list_head *lh;
1415 assert_spin_locked(pmd_lockptr(mm, pmdp));
1418 pgtable = pmd_huge_pte(mm, pmdp);
1419 lh = (struct list_head *) pgtable;
1421 pmd_huge_pte(mm, pmdp) = NULL;
1423 pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
1426 ptep = (pte_t *) pgtable;
1427 pte_val(*ptep) = _PAGE_INVALID;
1429 pte_val(*ptep) = _PAGE_INVALID;
1432 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */