Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 1 | /* |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 2 | * Copyright IBM Corp. 2007, 2011 |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 3 | * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com> |
| 4 | */ |
| 5 | |
| 6 | #include <linux/sched.h> |
| 7 | #include <linux/kernel.h> |
| 8 | #include <linux/errno.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/gfp.h> |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 10 | #include <linux/mm.h> |
| 11 | #include <linux/swap.h> |
| 12 | #include <linux/smp.h> |
| 13 | #include <linux/highmem.h> |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 14 | #include <linux/pagemap.h> |
| 15 | #include <linux/spinlock.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/quicklist.h> |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 18 | #include <linux/rcupdate.h> |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 19 | #include <linux/slab.h> |
Konstantin Weitz | b31288f | 2013-04-17 17:36:29 +0200 | [diff] [blame] | 20 | #include <linux/swapops.h> |
Dominik Dingel | 3ac8e38 | 2014-10-23 12:09:17 +0200 | [diff] [blame^] | 21 | #include <linux/ksm.h> |
| 22 | #include <linux/mman.h> |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 23 | |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 24 | #include <asm/pgtable.h> |
| 25 | #include <asm/pgalloc.h> |
| 26 | #include <asm/tlb.h> |
| 27 | #include <asm/tlbflush.h> |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 28 | #include <asm/mmu_context.h> |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 29 | |
| 30 | #ifndef CONFIG_64BIT |
| 31 | #define ALLOC_ORDER 1 |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 32 | #define FRAG_MASK 0x0f |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 33 | #else |
| 34 | #define ALLOC_ORDER 2 |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 35 | #define FRAG_MASK 0x03 |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 36 | #endif |
| 37 | |
Heiko Carstens | 239a6425 | 2009-06-12 10:26:33 +0200 | [diff] [blame] | 38 | |
Martin Schwidefsky | 043d070 | 2011-05-23 10:24:23 +0200 | [diff] [blame] | 39 | unsigned long *crst_table_alloc(struct mm_struct *mm) |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 40 | { |
| 41 | struct page *page = alloc_pages(GFP_KERNEL, ALLOC_ORDER); |
| 42 | |
| 43 | if (!page) |
| 44 | return NULL; |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 45 | return (unsigned long *) page_to_phys(page); |
| 46 | } |
| 47 | |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 48 | void crst_table_free(struct mm_struct *mm, unsigned long *table) |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 49 | { |
Martin Schwidefsky | 043d070 | 2011-05-23 10:24:23 +0200 | [diff] [blame] | 50 | free_pages((unsigned long) table, ALLOC_ORDER); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 51 | } |
| 52 | |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 53 | #ifdef CONFIG_64BIT |
Martin Schwidefsky | 1060786 | 2013-10-28 14:48:30 +0100 | [diff] [blame] | 54 | static void __crst_table_upgrade(void *arg) |
| 55 | { |
| 56 | struct mm_struct *mm = arg; |
| 57 | |
Martin Schwidefsky | beef560 | 2014-04-14 15:11:26 +0200 | [diff] [blame] | 58 | if (current->active_mm == mm) { |
| 59 | clear_user_asce(); |
| 60 | set_user_asce(mm); |
| 61 | } |
Martin Schwidefsky | 1060786 | 2013-10-28 14:48:30 +0100 | [diff] [blame] | 62 | __tlb_flush_local(); |
| 63 | } |
| 64 | |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 65 | int crst_table_upgrade(struct mm_struct *mm, unsigned long limit) |
| 66 | { |
| 67 | unsigned long *table, *pgd; |
| 68 | unsigned long entry; |
Martin Schwidefsky | 1060786 | 2013-10-28 14:48:30 +0100 | [diff] [blame] | 69 | int flush; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 70 | |
| 71 | BUG_ON(limit > (1UL << 53)); |
Martin Schwidefsky | 1060786 | 2013-10-28 14:48:30 +0100 | [diff] [blame] | 72 | flush = 0; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 73 | repeat: |
Martin Schwidefsky | 043d070 | 2011-05-23 10:24:23 +0200 | [diff] [blame] | 74 | table = crst_table_alloc(mm); |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 75 | if (!table) |
| 76 | return -ENOMEM; |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 77 | spin_lock_bh(&mm->page_table_lock); |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 78 | if (mm->context.asce_limit < limit) { |
| 79 | pgd = (unsigned long *) mm->pgd; |
| 80 | if (mm->context.asce_limit <= (1UL << 31)) { |
| 81 | entry = _REGION3_ENTRY_EMPTY; |
| 82 | mm->context.asce_limit = 1UL << 42; |
| 83 | mm->context.asce_bits = _ASCE_TABLE_LENGTH | |
| 84 | _ASCE_USER_BITS | |
| 85 | _ASCE_TYPE_REGION3; |
| 86 | } else { |
| 87 | entry = _REGION2_ENTRY_EMPTY; |
| 88 | mm->context.asce_limit = 1UL << 53; |
| 89 | mm->context.asce_bits = _ASCE_TABLE_LENGTH | |
| 90 | _ASCE_USER_BITS | |
| 91 | _ASCE_TYPE_REGION2; |
| 92 | } |
| 93 | crst_table_init(table, entry); |
| 94 | pgd_populate(mm, (pgd_t *) table, (pud_t *) pgd); |
| 95 | mm->pgd = (pgd_t *) table; |
Martin Schwidefsky | f481bfa | 2009-03-18 13:27:36 +0100 | [diff] [blame] | 96 | mm->task_size = mm->context.asce_limit; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 97 | table = NULL; |
Martin Schwidefsky | 1060786 | 2013-10-28 14:48:30 +0100 | [diff] [blame] | 98 | flush = 1; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 99 | } |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 100 | spin_unlock_bh(&mm->page_table_lock); |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 101 | if (table) |
| 102 | crst_table_free(mm, table); |
| 103 | if (mm->context.asce_limit < limit) |
| 104 | goto repeat; |
Martin Schwidefsky | 1060786 | 2013-10-28 14:48:30 +0100 | [diff] [blame] | 105 | if (flush) |
| 106 | on_each_cpu(__crst_table_upgrade, mm, 0); |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | void crst_table_downgrade(struct mm_struct *mm, unsigned long limit) |
| 111 | { |
| 112 | pgd_t *pgd; |
| 113 | |
Martin Schwidefsky | 02a8f3a | 2014-04-03 13:54:59 +0200 | [diff] [blame] | 114 | if (current->active_mm == mm) { |
Martin Schwidefsky | beef560 | 2014-04-14 15:11:26 +0200 | [diff] [blame] | 115 | clear_user_asce(); |
Martin Schwidefsky | 1060786 | 2013-10-28 14:48:30 +0100 | [diff] [blame] | 116 | __tlb_flush_mm(mm); |
Martin Schwidefsky | 02a8f3a | 2014-04-03 13:54:59 +0200 | [diff] [blame] | 117 | } |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 118 | while (mm->context.asce_limit > limit) { |
| 119 | pgd = mm->pgd; |
| 120 | switch (pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) { |
| 121 | case _REGION_ENTRY_TYPE_R2: |
| 122 | mm->context.asce_limit = 1UL << 42; |
| 123 | mm->context.asce_bits = _ASCE_TABLE_LENGTH | |
| 124 | _ASCE_USER_BITS | |
| 125 | _ASCE_TYPE_REGION3; |
| 126 | break; |
| 127 | case _REGION_ENTRY_TYPE_R3: |
| 128 | mm->context.asce_limit = 1UL << 31; |
| 129 | mm->context.asce_bits = _ASCE_TABLE_LENGTH | |
| 130 | _ASCE_USER_BITS | |
| 131 | _ASCE_TYPE_SEGMENT; |
| 132 | break; |
| 133 | default: |
| 134 | BUG(); |
| 135 | } |
| 136 | mm->pgd = (pgd_t *) (pgd_val(*pgd) & _REGION_ENTRY_ORIGIN); |
Martin Schwidefsky | f481bfa | 2009-03-18 13:27:36 +0100 | [diff] [blame] | 137 | mm->task_size = mm->context.asce_limit; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 138 | crst_table_free(mm, (unsigned long *) pgd); |
| 139 | } |
Martin Schwidefsky | 1060786 | 2013-10-28 14:48:30 +0100 | [diff] [blame] | 140 | if (current->active_mm == mm) |
Martin Schwidefsky | beef560 | 2014-04-14 15:11:26 +0200 | [diff] [blame] | 141 | set_user_asce(mm); |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 142 | } |
| 143 | #endif |
| 144 | |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 145 | #ifdef CONFIG_PGSTE |
| 146 | |
| 147 | /** |
| 148 | * gmap_alloc - allocate a guest address space |
| 149 | * @mm: pointer to the parent mm_struct |
Martin Schwidefsky | c6c956b | 2014-07-01 14:36:04 +0200 | [diff] [blame] | 150 | * @limit: maximum size of the gmap address space |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 151 | * |
| 152 | * Returns a guest address space structure. |
| 153 | */ |
Martin Schwidefsky | c6c956b | 2014-07-01 14:36:04 +0200 | [diff] [blame] | 154 | struct gmap *gmap_alloc(struct mm_struct *mm, unsigned long limit) |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 155 | { |
| 156 | struct gmap *gmap; |
| 157 | struct page *page; |
| 158 | unsigned long *table; |
Martin Schwidefsky | c6c956b | 2014-07-01 14:36:04 +0200 | [diff] [blame] | 159 | unsigned long etype, atype; |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 160 | |
Martin Schwidefsky | c6c956b | 2014-07-01 14:36:04 +0200 | [diff] [blame] | 161 | if (limit < (1UL << 31)) { |
| 162 | limit = (1UL << 31) - 1; |
| 163 | atype = _ASCE_TYPE_SEGMENT; |
| 164 | etype = _SEGMENT_ENTRY_EMPTY; |
| 165 | } else if (limit < (1UL << 42)) { |
| 166 | limit = (1UL << 42) - 1; |
| 167 | atype = _ASCE_TYPE_REGION3; |
| 168 | etype = _REGION3_ENTRY_EMPTY; |
| 169 | } else if (limit < (1UL << 53)) { |
| 170 | limit = (1UL << 53) - 1; |
| 171 | atype = _ASCE_TYPE_REGION2; |
| 172 | etype = _REGION2_ENTRY_EMPTY; |
| 173 | } else { |
| 174 | limit = -1UL; |
| 175 | atype = _ASCE_TYPE_REGION1; |
| 176 | etype = _REGION1_ENTRY_EMPTY; |
| 177 | } |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 178 | gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL); |
| 179 | if (!gmap) |
| 180 | goto out; |
| 181 | INIT_LIST_HEAD(&gmap->crst_list); |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 182 | INIT_RADIX_TREE(&gmap->guest_to_host, GFP_KERNEL); |
| 183 | INIT_RADIX_TREE(&gmap->host_to_guest, GFP_ATOMIC); |
| 184 | spin_lock_init(&gmap->guest_table_lock); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 185 | gmap->mm = mm; |
| 186 | page = alloc_pages(GFP_KERNEL, ALLOC_ORDER); |
| 187 | if (!page) |
| 188 | goto out_free; |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 189 | page->index = 0; |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 190 | list_add(&page->lru, &gmap->crst_list); |
| 191 | table = (unsigned long *) page_to_phys(page); |
Martin Schwidefsky | c6c956b | 2014-07-01 14:36:04 +0200 | [diff] [blame] | 192 | crst_table_init(table, etype); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 193 | gmap->table = table; |
Martin Schwidefsky | c6c956b | 2014-07-01 14:36:04 +0200 | [diff] [blame] | 194 | gmap->asce = atype | _ASCE_TABLE_LENGTH | |
| 195 | _ASCE_USER_BITS | __pa(table); |
| 196 | gmap->asce_end = limit; |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 197 | down_write(&mm->mmap_sem); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 198 | list_add(&gmap->list, &mm->context.gmap_list); |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 199 | up_write(&mm->mmap_sem); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 200 | return gmap; |
| 201 | |
| 202 | out_free: |
| 203 | kfree(gmap); |
| 204 | out: |
| 205 | return NULL; |
| 206 | } |
| 207 | EXPORT_SYMBOL_GPL(gmap_alloc); |
| 208 | |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 209 | static void gmap_flush_tlb(struct gmap *gmap) |
| 210 | { |
| 211 | if (MACHINE_HAS_IDTE) |
Martin Schwidefsky | c6c956b | 2014-07-01 14:36:04 +0200 | [diff] [blame] | 212 | __tlb_flush_asce(gmap->mm, gmap->asce); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 213 | else |
| 214 | __tlb_flush_global(); |
| 215 | } |
| 216 | |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 217 | static void gmap_radix_tree_free(struct radix_tree_root *root) |
| 218 | { |
| 219 | struct radix_tree_iter iter; |
| 220 | unsigned long indices[16]; |
| 221 | unsigned long index; |
| 222 | void **slot; |
| 223 | int i, nr; |
| 224 | |
| 225 | /* A radix tree is freed by deleting all of its entries */ |
| 226 | index = 0; |
| 227 | do { |
| 228 | nr = 0; |
| 229 | radix_tree_for_each_slot(slot, root, &iter, index) { |
| 230 | indices[nr] = iter.index; |
| 231 | if (++nr == 16) |
| 232 | break; |
| 233 | } |
| 234 | for (i = 0; i < nr; i++) { |
| 235 | index = indices[i]; |
| 236 | radix_tree_delete(root, index); |
| 237 | } |
| 238 | } while (nr > 0); |
| 239 | } |
| 240 | |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 241 | /** |
| 242 | * gmap_free - free a guest address space |
| 243 | * @gmap: pointer to the guest address space structure |
| 244 | */ |
| 245 | void gmap_free(struct gmap *gmap) |
| 246 | { |
| 247 | struct page *page, *next; |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 248 | |
| 249 | /* Flush tlb. */ |
| 250 | if (MACHINE_HAS_IDTE) |
Martin Schwidefsky | c6c956b | 2014-07-01 14:36:04 +0200 | [diff] [blame] | 251 | __tlb_flush_asce(gmap->mm, gmap->asce); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 252 | else |
| 253 | __tlb_flush_global(); |
| 254 | |
| 255 | /* Free all segment & region tables. */ |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 256 | list_for_each_entry_safe(page, next, &gmap->crst_list, lru) |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 257 | __free_pages(page, ALLOC_ORDER); |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 258 | gmap_radix_tree_free(&gmap->guest_to_host); |
| 259 | gmap_radix_tree_free(&gmap->host_to_guest); |
| 260 | down_write(&gmap->mm->mmap_sem); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 261 | list_del(&gmap->list); |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 262 | up_write(&gmap->mm->mmap_sem); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 263 | kfree(gmap); |
| 264 | } |
| 265 | EXPORT_SYMBOL_GPL(gmap_free); |
| 266 | |
| 267 | /** |
| 268 | * gmap_enable - switch primary space to the guest address space |
| 269 | * @gmap: pointer to the guest address space structure |
| 270 | */ |
| 271 | void gmap_enable(struct gmap *gmap) |
| 272 | { |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 273 | S390_lowcore.gmap = (unsigned long) gmap; |
| 274 | } |
| 275 | EXPORT_SYMBOL_GPL(gmap_enable); |
| 276 | |
| 277 | /** |
| 278 | * gmap_disable - switch back to the standard primary address space |
| 279 | * @gmap: pointer to the guest address space structure |
| 280 | */ |
| 281 | void gmap_disable(struct gmap *gmap) |
| 282 | { |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 283 | S390_lowcore.gmap = 0UL; |
| 284 | } |
| 285 | EXPORT_SYMBOL_GPL(gmap_disable); |
| 286 | |
Carsten Otte | a9162f23 | 2011-10-30 15:17:00 +0100 | [diff] [blame] | 287 | /* |
| 288 | * gmap_alloc_table is assumed to be called with mmap_sem held |
| 289 | */ |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 290 | static int gmap_alloc_table(struct gmap *gmap, unsigned long *table, |
| 291 | unsigned long init, unsigned long gaddr) |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 292 | { |
| 293 | struct page *page; |
| 294 | unsigned long *new; |
| 295 | |
Christian Borntraeger | c86cce2 | 2011-12-27 11:25:47 +0100 | [diff] [blame] | 296 | /* since we dont free the gmap table until gmap_free we can unlock */ |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 297 | page = alloc_pages(GFP_KERNEL, ALLOC_ORDER); |
| 298 | if (!page) |
| 299 | return -ENOMEM; |
| 300 | new = (unsigned long *) page_to_phys(page); |
| 301 | crst_table_init(new, init); |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 302 | spin_lock(&gmap->mm->page_table_lock); |
Martin Schwidefsky | e509861 | 2013-07-23 20:57:57 +0200 | [diff] [blame] | 303 | if (*table & _REGION_ENTRY_INVALID) { |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 304 | list_add(&page->lru, &gmap->crst_list); |
| 305 | *table = (unsigned long) new | _REGION_ENTRY_LENGTH | |
| 306 | (*table & _REGION_ENTRY_TYPE_MASK); |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 307 | page->index = gaddr; |
| 308 | page = NULL; |
| 309 | } |
| 310 | spin_unlock(&gmap->mm->page_table_lock); |
| 311 | if (page) |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 312 | __free_pages(page, ALLOC_ORDER); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | /** |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 317 | * __gmap_segment_gaddr - find virtual address from segment pointer |
| 318 | * @entry: pointer to a segment table entry in the guest address space |
| 319 | * |
| 320 | * Returns the virtual address in the guest address space for the segment |
| 321 | */ |
| 322 | static unsigned long __gmap_segment_gaddr(unsigned long *entry) |
| 323 | { |
| 324 | struct page *page; |
| 325 | unsigned long offset; |
| 326 | |
| 327 | offset = (unsigned long) entry / sizeof(unsigned long); |
| 328 | offset = (offset & (PTRS_PER_PMD - 1)) * PMD_SIZE; |
| 329 | page = pmd_to_page((pmd_t *) entry); |
| 330 | return page->index + offset; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * __gmap_unlink_by_vmaddr - unlink a single segment via a host address |
| 335 | * @gmap: pointer to the guest address space structure |
| 336 | * @vmaddr: address in the host process address space |
| 337 | * |
| 338 | * Returns 1 if a TLB flush is required |
| 339 | */ |
| 340 | static int __gmap_unlink_by_vmaddr(struct gmap *gmap, unsigned long vmaddr) |
| 341 | { |
| 342 | unsigned long *entry; |
| 343 | int flush = 0; |
| 344 | |
| 345 | spin_lock(&gmap->guest_table_lock); |
| 346 | entry = radix_tree_delete(&gmap->host_to_guest, vmaddr >> PMD_SHIFT); |
| 347 | if (entry) { |
| 348 | flush = (*entry != _SEGMENT_ENTRY_INVALID); |
| 349 | *entry = _SEGMENT_ENTRY_INVALID; |
| 350 | } |
| 351 | spin_unlock(&gmap->guest_table_lock); |
| 352 | return flush; |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * __gmap_unmap_by_gaddr - unmap a single segment via a guest address |
| 357 | * @gmap: pointer to the guest address space structure |
| 358 | * @gaddr: address in the guest address space |
| 359 | * |
| 360 | * Returns 1 if a TLB flush is required |
| 361 | */ |
| 362 | static int __gmap_unmap_by_gaddr(struct gmap *gmap, unsigned long gaddr) |
| 363 | { |
| 364 | unsigned long vmaddr; |
| 365 | |
| 366 | vmaddr = (unsigned long) radix_tree_delete(&gmap->guest_to_host, |
| 367 | gaddr >> PMD_SHIFT); |
| 368 | return vmaddr ? __gmap_unlink_by_vmaddr(gmap, vmaddr) : 0; |
| 369 | } |
| 370 | |
| 371 | /** |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 372 | * gmap_unmap_segment - unmap segment from the guest address space |
| 373 | * @gmap: pointer to the guest address space structure |
Martin Schwidefsky | 6e0a043 | 2014-04-29 09:34:41 +0200 | [diff] [blame] | 374 | * @to: address in the guest address space |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 375 | * @len: length of the memory area to unmap |
| 376 | * |
Hendrik Brueckner | b4a9601 | 2013-12-13 12:53:42 +0100 | [diff] [blame] | 377 | * Returns 0 if the unmap succeeded, -EINVAL if not. |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 378 | */ |
| 379 | int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len) |
| 380 | { |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 381 | unsigned long off; |
| 382 | int flush; |
| 383 | |
| 384 | if ((to | len) & (PMD_SIZE - 1)) |
| 385 | return -EINVAL; |
| 386 | if (len == 0 || to + len < to) |
| 387 | return -EINVAL; |
| 388 | |
| 389 | flush = 0; |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 390 | down_write(&gmap->mm->mmap_sem); |
| 391 | for (off = 0; off < len; off += PMD_SIZE) |
| 392 | flush |= __gmap_unmap_by_gaddr(gmap, to + off); |
| 393 | up_write(&gmap->mm->mmap_sem); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 394 | if (flush) |
| 395 | gmap_flush_tlb(gmap); |
| 396 | return 0; |
| 397 | } |
| 398 | EXPORT_SYMBOL_GPL(gmap_unmap_segment); |
| 399 | |
| 400 | /** |
| 401 | * gmap_mmap_segment - map a segment to the guest address space |
| 402 | * @gmap: pointer to the guest address space structure |
| 403 | * @from: source address in the parent address space |
| 404 | * @to: target address in the guest address space |
Martin Schwidefsky | 6e0a043 | 2014-04-29 09:34:41 +0200 | [diff] [blame] | 405 | * @len: length of the memory area to map |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 406 | * |
Hendrik Brueckner | b4a9601 | 2013-12-13 12:53:42 +0100 | [diff] [blame] | 407 | * Returns 0 if the mmap succeeded, -EINVAL or -ENOMEM if not. |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 408 | */ |
| 409 | int gmap_map_segment(struct gmap *gmap, unsigned long from, |
| 410 | unsigned long to, unsigned long len) |
| 411 | { |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 412 | unsigned long off; |
| 413 | int flush; |
| 414 | |
| 415 | if ((from | to | len) & (PMD_SIZE - 1)) |
| 416 | return -EINVAL; |
Martin Schwidefsky | c6c956b | 2014-07-01 14:36:04 +0200 | [diff] [blame] | 417 | if (len == 0 || from + len < from || to + len < to || |
| 418 | from + len > TASK_MAX_SIZE || to + len > gmap->asce_end) |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 419 | return -EINVAL; |
| 420 | |
| 421 | flush = 0; |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 422 | down_write(&gmap->mm->mmap_sem); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 423 | for (off = 0; off < len; off += PMD_SIZE) { |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 424 | /* Remove old translation */ |
| 425 | flush |= __gmap_unmap_by_gaddr(gmap, to + off); |
| 426 | /* Store new translation */ |
| 427 | if (radix_tree_insert(&gmap->guest_to_host, |
| 428 | (to + off) >> PMD_SHIFT, |
| 429 | (void *) from + off)) |
| 430 | break; |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 431 | } |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 432 | up_write(&gmap->mm->mmap_sem); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 433 | if (flush) |
| 434 | gmap_flush_tlb(gmap); |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 435 | if (off >= len) |
| 436 | return 0; |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 437 | gmap_unmap_segment(gmap, to, len); |
| 438 | return -ENOMEM; |
| 439 | } |
| 440 | EXPORT_SYMBOL_GPL(gmap_map_segment); |
| 441 | |
Heiko Carstens | c503494 | 2012-09-10 16:14:33 +0200 | [diff] [blame] | 442 | /** |
| 443 | * __gmap_translate - translate a guest address to a user space address |
Heiko Carstens | c503494 | 2012-09-10 16:14:33 +0200 | [diff] [blame] | 444 | * @gmap: pointer to guest mapping meta data structure |
Martin Schwidefsky | 6e0a043 | 2014-04-29 09:34:41 +0200 | [diff] [blame] | 445 | * @gaddr: guest address |
Heiko Carstens | c503494 | 2012-09-10 16:14:33 +0200 | [diff] [blame] | 446 | * |
| 447 | * Returns user space address which corresponds to the guest address or |
| 448 | * -EFAULT if no such mapping exists. |
| 449 | * This function does not establish potentially missing page table entries. |
| 450 | * The mmap_sem of the mm that belongs to the address space must be held |
| 451 | * when this function gets called. |
| 452 | */ |
Martin Schwidefsky | 6e0a043 | 2014-04-29 09:34:41 +0200 | [diff] [blame] | 453 | unsigned long __gmap_translate(struct gmap *gmap, unsigned long gaddr) |
Heiko Carstens | c503494 | 2012-09-10 16:14:33 +0200 | [diff] [blame] | 454 | { |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 455 | unsigned long vmaddr; |
Heiko Carstens | c503494 | 2012-09-10 16:14:33 +0200 | [diff] [blame] | 456 | |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 457 | vmaddr = (unsigned long) |
| 458 | radix_tree_lookup(&gmap->guest_to_host, gaddr >> PMD_SHIFT); |
| 459 | return vmaddr ? (vmaddr | (gaddr & ~PMD_MASK)) : -EFAULT; |
Heiko Carstens | c503494 | 2012-09-10 16:14:33 +0200 | [diff] [blame] | 460 | } |
| 461 | EXPORT_SYMBOL_GPL(__gmap_translate); |
| 462 | |
| 463 | /** |
| 464 | * gmap_translate - translate a guest address to a user space address |
Heiko Carstens | c503494 | 2012-09-10 16:14:33 +0200 | [diff] [blame] | 465 | * @gmap: pointer to guest mapping meta data structure |
Martin Schwidefsky | 6e0a043 | 2014-04-29 09:34:41 +0200 | [diff] [blame] | 466 | * @gaddr: guest address |
Heiko Carstens | c503494 | 2012-09-10 16:14:33 +0200 | [diff] [blame] | 467 | * |
| 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. |
| 471 | */ |
Martin Schwidefsky | 6e0a043 | 2014-04-29 09:34:41 +0200 | [diff] [blame] | 472 | unsigned long gmap_translate(struct gmap *gmap, unsigned long gaddr) |
Heiko Carstens | c503494 | 2012-09-10 16:14:33 +0200 | [diff] [blame] | 473 | { |
| 474 | unsigned long rc; |
| 475 | |
| 476 | down_read(&gmap->mm->mmap_sem); |
Martin Schwidefsky | 6e0a043 | 2014-04-29 09:34:41 +0200 | [diff] [blame] | 477 | rc = __gmap_translate(gmap, gaddr); |
Heiko Carstens | c503494 | 2012-09-10 16:14:33 +0200 | [diff] [blame] | 478 | up_read(&gmap->mm->mmap_sem); |
| 479 | return rc; |
| 480 | } |
| 481 | EXPORT_SYMBOL_GPL(gmap_translate); |
| 482 | |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 483 | /** |
| 484 | * gmap_unlink - disconnect a page table from the gmap shadow tables |
| 485 | * @gmap: pointer to guest mapping meta data structure |
| 486 | * @table: pointer to the host page table |
| 487 | * @vmaddr: vm address associated with the host page table |
| 488 | */ |
| 489 | static void gmap_unlink(struct mm_struct *mm, unsigned long *table, |
| 490 | unsigned long vmaddr) |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 491 | { |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 492 | struct gmap *gmap; |
| 493 | int flush; |
| 494 | |
| 495 | list_for_each_entry(gmap, &mm->context.gmap_list, list) { |
| 496 | flush = __gmap_unlink_by_vmaddr(gmap, vmaddr); |
| 497 | if (flush) |
| 498 | gmap_flush_tlb(gmap); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * gmap_link - set up shadow page tables to connect a host to a guest address |
| 504 | * @gmap: pointer to guest mapping meta data structure |
| 505 | * @gaddr: guest address |
| 506 | * @vmaddr: vm address |
| 507 | * |
| 508 | * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT |
| 509 | * if the vm address is already mapped to a different guest segment. |
| 510 | * The mmap_sem of the mm that belongs to the address space must be held |
| 511 | * when this function gets called. |
| 512 | */ |
| 513 | int __gmap_link(struct gmap *gmap, unsigned long gaddr, unsigned long vmaddr) |
| 514 | { |
Heiko Carstens | c503494 | 2012-09-10 16:14:33 +0200 | [diff] [blame] | 515 | struct mm_struct *mm; |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 516 | unsigned long *table; |
| 517 | spinlock_t *ptl; |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 518 | pgd_t *pgd; |
| 519 | pud_t *pud; |
| 520 | pmd_t *pmd; |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 521 | int rc; |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 522 | |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 523 | /* Create higher level tables in the gmap page table */ |
Martin Schwidefsky | c6c956b | 2014-07-01 14:36:04 +0200 | [diff] [blame] | 524 | table = gmap->table; |
| 525 | if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION1) { |
| 526 | table += (gaddr >> 53) & 0x7ff; |
| 527 | if ((*table & _REGION_ENTRY_INVALID) && |
| 528 | gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY, |
| 529 | gaddr & 0xffe0000000000000)) |
| 530 | return -ENOMEM; |
| 531 | table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN); |
| 532 | } |
| 533 | if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION2) { |
| 534 | table += (gaddr >> 42) & 0x7ff; |
| 535 | if ((*table & _REGION_ENTRY_INVALID) && |
| 536 | gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY, |
| 537 | gaddr & 0xfffffc0000000000)) |
| 538 | return -ENOMEM; |
| 539 | table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN); |
| 540 | } |
| 541 | if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION3) { |
| 542 | table += (gaddr >> 31) & 0x7ff; |
| 543 | if ((*table & _REGION_ENTRY_INVALID) && |
| 544 | gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY, |
| 545 | gaddr & 0xffffffff80000000)) |
| 546 | return -ENOMEM; |
| 547 | table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN); |
| 548 | } |
| 549 | table += (gaddr >> 20) & 0x7ff; |
Martin Schwidefsky | ab8e523 | 2013-04-16 13:37:46 +0200 | [diff] [blame] | 550 | /* Walk the parent mm page table */ |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 551 | mm = gmap->mm; |
Martin Schwidefsky | ab8e523 | 2013-04-16 13:37:46 +0200 | [diff] [blame] | 552 | pgd = pgd_offset(mm, vmaddr); |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 553 | VM_BUG_ON(pgd_none(*pgd)); |
| 554 | pud = pud_offset(pgd, vmaddr); |
| 555 | VM_BUG_ON(pud_none(*pud)); |
| 556 | pmd = pmd_offset(pud, vmaddr); |
| 557 | VM_BUG_ON(pmd_none(*pmd)); |
Alex Thorlton | 1e1836e | 2014-04-07 15:37:09 -0700 | [diff] [blame] | 558 | /* large pmds cannot yet be handled */ |
| 559 | if (pmd_large(*pmd)) |
| 560 | return -EFAULT; |
Martin Schwidefsky | ab8e523 | 2013-04-16 13:37:46 +0200 | [diff] [blame] | 561 | /* Link gmap segment table entry location to page table. */ |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 562 | rc = radix_tree_preload(GFP_KERNEL); |
| 563 | if (rc) |
| 564 | return rc; |
| 565 | ptl = pmd_lock(mm, pmd); |
| 566 | spin_lock(&gmap->guest_table_lock); |
| 567 | if (*table == _SEGMENT_ENTRY_INVALID) { |
| 568 | rc = radix_tree_insert(&gmap->host_to_guest, |
| 569 | vmaddr >> PMD_SHIFT, table); |
| 570 | if (!rc) |
| 571 | *table = pmd_val(*pmd); |
| 572 | } else |
| 573 | rc = 0; |
| 574 | spin_unlock(&gmap->guest_table_lock); |
| 575 | spin_unlock(ptl); |
| 576 | radix_tree_preload_end(); |
| 577 | return rc; |
Martin Schwidefsky | ab8e523 | 2013-04-16 13:37:46 +0200 | [diff] [blame] | 578 | } |
| 579 | |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 580 | /** |
| 581 | * gmap_fault - resolve a fault on a guest address |
| 582 | * @gmap: pointer to guest mapping meta data structure |
| 583 | * @gaddr: guest address |
| 584 | * @fault_flags: flags to pass down to handle_mm_fault() |
| 585 | * |
| 586 | * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT |
| 587 | * if the vm address is already mapped to a different guest segment. |
Martin Schwidefsky | ab8e523 | 2013-04-16 13:37:46 +0200 | [diff] [blame] | 588 | */ |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 589 | int gmap_fault(struct gmap *gmap, unsigned long gaddr, |
| 590 | unsigned int fault_flags) |
Martin Schwidefsky | ab8e523 | 2013-04-16 13:37:46 +0200 | [diff] [blame] | 591 | { |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 592 | unsigned long vmaddr; |
Martin Schwidefsky | ab8e523 | 2013-04-16 13:37:46 +0200 | [diff] [blame] | 593 | int rc; |
| 594 | |
Carsten Otte | 499069e | 2011-10-30 15:17:02 +0100 | [diff] [blame] | 595 | down_read(&gmap->mm->mmap_sem); |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 596 | vmaddr = __gmap_translate(gmap, gaddr); |
| 597 | if (IS_ERR_VALUE(vmaddr)) { |
| 598 | rc = vmaddr; |
| 599 | goto out_up; |
| 600 | } |
| 601 | if (fixup_user_fault(current, gmap->mm, vmaddr, fault_flags)) { |
| 602 | rc = -EFAULT; |
| 603 | goto out_up; |
| 604 | } |
| 605 | rc = __gmap_link(gmap, gaddr, vmaddr); |
| 606 | out_up: |
Carsten Otte | 499069e | 2011-10-30 15:17:02 +0100 | [diff] [blame] | 607 | up_read(&gmap->mm->mmap_sem); |
Carsten Otte | 499069e | 2011-10-30 15:17:02 +0100 | [diff] [blame] | 608 | return rc; |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 609 | } |
| 610 | EXPORT_SYMBOL_GPL(gmap_fault); |
| 611 | |
Konstantin Weitz | b31288f | 2013-04-17 17:36:29 +0200 | [diff] [blame] | 612 | static void gmap_zap_swap_entry(swp_entry_t entry, struct mm_struct *mm) |
| 613 | { |
| 614 | if (!non_swap_entry(entry)) |
| 615 | dec_mm_counter(mm, MM_SWAPENTS); |
| 616 | else if (is_migration_entry(entry)) { |
| 617 | struct page *page = migration_entry_to_page(entry); |
| 618 | |
| 619 | if (PageAnon(page)) |
| 620 | dec_mm_counter(mm, MM_ANONPAGES); |
| 621 | else |
| 622 | dec_mm_counter(mm, MM_FILEPAGES); |
| 623 | } |
| 624 | free_swap_and_cache(entry); |
| 625 | } |
| 626 | |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 627 | /* |
| 628 | * this function is assumed to be called with mmap_sem held |
Konstantin Weitz | b31288f | 2013-04-17 17:36:29 +0200 | [diff] [blame] | 629 | */ |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 630 | void __gmap_zap(struct gmap *gmap, unsigned long gaddr) |
Konstantin Weitz | b31288f | 2013-04-17 17:36:29 +0200 | [diff] [blame] | 631 | { |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 632 | unsigned long vmaddr, ptev, pgstev; |
| 633 | pte_t *ptep, pte; |
Konstantin Weitz | b31288f | 2013-04-17 17:36:29 +0200 | [diff] [blame] | 634 | spinlock_t *ptl; |
| 635 | pgste_t pgste; |
Konstantin Weitz | b31288f | 2013-04-17 17:36:29 +0200 | [diff] [blame] | 636 | |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 637 | /* Find the vm address for the guest address */ |
| 638 | vmaddr = (unsigned long) radix_tree_lookup(&gmap->guest_to_host, |
| 639 | gaddr >> PMD_SHIFT); |
| 640 | if (!vmaddr) |
| 641 | return; |
| 642 | vmaddr |= gaddr & ~PMD_MASK; |
| 643 | /* Get pointer to the page table entry */ |
| 644 | ptep = get_locked_pte(gmap->mm, vmaddr, &ptl); |
Konstantin Weitz | b31288f | 2013-04-17 17:36:29 +0200 | [diff] [blame] | 645 | if (unlikely(!ptep)) |
| 646 | return; |
| 647 | pte = *ptep; |
| 648 | if (!pte_swap(pte)) |
| 649 | goto out_pte; |
| 650 | /* Zap unused and logically-zero pages */ |
| 651 | pgste = pgste_get_lock(ptep); |
| 652 | pgstev = pgste_val(pgste); |
| 653 | ptev = pte_val(pte); |
| 654 | if (((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED) || |
| 655 | ((pgstev & _PGSTE_GPS_ZERO) && (ptev & _PAGE_INVALID))) { |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 656 | gmap_zap_swap_entry(pte_to_swp_entry(pte), gmap->mm); |
| 657 | pte_clear(gmap->mm, vmaddr, ptep); |
Konstantin Weitz | b31288f | 2013-04-17 17:36:29 +0200 | [diff] [blame] | 658 | } |
| 659 | pgste_set_unlock(ptep, pgste); |
| 660 | out_pte: |
Dominik Dingel | 66e9bbd | 2014-10-06 16:34:44 +0200 | [diff] [blame] | 661 | pte_unmap_unlock(ptep, ptl); |
Konstantin Weitz | b31288f | 2013-04-17 17:36:29 +0200 | [diff] [blame] | 662 | } |
Konstantin Weitz | b31288f | 2013-04-17 17:36:29 +0200 | [diff] [blame] | 663 | EXPORT_SYMBOL_GPL(__gmap_zap); |
| 664 | |
Martin Schwidefsky | 6e0a043 | 2014-04-29 09:34:41 +0200 | [diff] [blame] | 665 | void gmap_discard(struct gmap *gmap, unsigned long from, unsigned long to) |
Christian Borntraeger | 388186b | 2011-10-30 15:17:03 +0100 | [diff] [blame] | 666 | { |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 667 | unsigned long gaddr, vmaddr, size; |
Christian Borntraeger | 388186b | 2011-10-30 15:17:03 +0100 | [diff] [blame] | 668 | struct vm_area_struct *vma; |
Christian Borntraeger | 388186b | 2011-10-30 15:17:03 +0100 | [diff] [blame] | 669 | |
| 670 | down_read(&gmap->mm->mmap_sem); |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 671 | for (gaddr = from; gaddr < to; |
| 672 | gaddr = (gaddr + PMD_SIZE) & PMD_MASK) { |
| 673 | /* Find the vm address for the guest address */ |
| 674 | vmaddr = (unsigned long) |
| 675 | radix_tree_lookup(&gmap->guest_to_host, |
| 676 | gaddr >> PMD_SHIFT); |
| 677 | if (!vmaddr) |
Christian Borntraeger | 388186b | 2011-10-30 15:17:03 +0100 | [diff] [blame] | 678 | continue; |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 679 | vmaddr |= gaddr & ~PMD_MASK; |
| 680 | /* Find vma in the parent mm */ |
| 681 | vma = find_vma(gmap->mm, vmaddr); |
Martin Schwidefsky | 6e0a043 | 2014-04-29 09:34:41 +0200 | [diff] [blame] | 682 | size = min(to - gaddr, PMD_SIZE - (gaddr & ~PMD_MASK)); |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 683 | zap_page_range(vma, vmaddr, size, NULL); |
Christian Borntraeger | 388186b | 2011-10-30 15:17:03 +0100 | [diff] [blame] | 684 | } |
| 685 | up_read(&gmap->mm->mmap_sem); |
| 686 | } |
| 687 | EXPORT_SYMBOL_GPL(gmap_discard); |
| 688 | |
Martin Schwidefsky | d338363 | 2013-04-17 10:53:39 +0200 | [diff] [blame] | 689 | static LIST_HEAD(gmap_notifier_list); |
| 690 | static DEFINE_SPINLOCK(gmap_notifier_lock); |
| 691 | |
| 692 | /** |
| 693 | * gmap_register_ipte_notifier - register a pte invalidation callback |
| 694 | * @nb: pointer to the gmap notifier block |
| 695 | */ |
| 696 | void gmap_register_ipte_notifier(struct gmap_notifier *nb) |
| 697 | { |
| 698 | spin_lock(&gmap_notifier_lock); |
| 699 | list_add(&nb->list, &gmap_notifier_list); |
| 700 | spin_unlock(&gmap_notifier_lock); |
| 701 | } |
| 702 | EXPORT_SYMBOL_GPL(gmap_register_ipte_notifier); |
| 703 | |
| 704 | /** |
| 705 | * gmap_unregister_ipte_notifier - remove a pte invalidation callback |
| 706 | * @nb: pointer to the gmap notifier block |
| 707 | */ |
| 708 | void gmap_unregister_ipte_notifier(struct gmap_notifier *nb) |
| 709 | { |
| 710 | spin_lock(&gmap_notifier_lock); |
| 711 | list_del_init(&nb->list); |
| 712 | spin_unlock(&gmap_notifier_lock); |
| 713 | } |
| 714 | EXPORT_SYMBOL_GPL(gmap_unregister_ipte_notifier); |
| 715 | |
| 716 | /** |
| 717 | * gmap_ipte_notify - mark a range of ptes for invalidation notification |
| 718 | * @gmap: pointer to guest mapping meta data structure |
Martin Schwidefsky | 6e0a043 | 2014-04-29 09:34:41 +0200 | [diff] [blame] | 719 | * @gaddr: virtual address in the guest address space |
Martin Schwidefsky | d338363 | 2013-04-17 10:53:39 +0200 | [diff] [blame] | 720 | * @len: size of area |
| 721 | * |
| 722 | * Returns 0 if for each page in the given range a gmap mapping exists and |
| 723 | * the invalidation notification could be set. If the gmap mapping is missing |
| 724 | * for one or more pages -EFAULT is returned. If no memory could be allocated |
| 725 | * -ENOMEM is returned. This function establishes missing page table entries. |
| 726 | */ |
Martin Schwidefsky | 6e0a043 | 2014-04-29 09:34:41 +0200 | [diff] [blame] | 727 | int gmap_ipte_notify(struct gmap *gmap, unsigned long gaddr, unsigned long len) |
Martin Schwidefsky | d338363 | 2013-04-17 10:53:39 +0200 | [diff] [blame] | 728 | { |
| 729 | unsigned long addr; |
| 730 | spinlock_t *ptl; |
| 731 | pte_t *ptep, entry; |
| 732 | pgste_t pgste; |
| 733 | int rc = 0; |
| 734 | |
Martin Schwidefsky | 6e0a043 | 2014-04-29 09:34:41 +0200 | [diff] [blame] | 735 | if ((gaddr & ~PAGE_MASK) || (len & ~PAGE_MASK)) |
Martin Schwidefsky | d338363 | 2013-04-17 10:53:39 +0200 | [diff] [blame] | 736 | return -EINVAL; |
| 737 | down_read(&gmap->mm->mmap_sem); |
| 738 | while (len) { |
| 739 | /* Convert gmap address and connect the page tables */ |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 740 | addr = __gmap_translate(gmap, gaddr); |
Martin Schwidefsky | d338363 | 2013-04-17 10:53:39 +0200 | [diff] [blame] | 741 | if (IS_ERR_VALUE(addr)) { |
| 742 | rc = addr; |
| 743 | break; |
| 744 | } |
| 745 | /* Get the page mapped */ |
Christian Borntraeger | bb4b42c | 2013-05-08 15:25:38 +0200 | [diff] [blame] | 746 | if (fixup_user_fault(current, gmap->mm, addr, FAULT_FLAG_WRITE)) { |
Martin Schwidefsky | d338363 | 2013-04-17 10:53:39 +0200 | [diff] [blame] | 747 | rc = -EFAULT; |
| 748 | break; |
| 749 | } |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 750 | rc = __gmap_link(gmap, gaddr, addr); |
| 751 | if (rc) |
| 752 | break; |
Martin Schwidefsky | d338363 | 2013-04-17 10:53:39 +0200 | [diff] [blame] | 753 | /* Walk the process page table, lock and get pte pointer */ |
| 754 | ptep = get_locked_pte(gmap->mm, addr, &ptl); |
| 755 | if (unlikely(!ptep)) |
| 756 | continue; |
| 757 | /* Set notification bit in the pgste of the pte */ |
| 758 | entry = *ptep; |
Martin Schwidefsky | e509861 | 2013-07-23 20:57:57 +0200 | [diff] [blame] | 759 | if ((pte_val(entry) & (_PAGE_INVALID | _PAGE_PROTECT)) == 0) { |
Martin Schwidefsky | d338363 | 2013-04-17 10:53:39 +0200 | [diff] [blame] | 760 | pgste = pgste_get_lock(ptep); |
Martin Schwidefsky | 0d0dafc | 2013-05-17 14:41:33 +0200 | [diff] [blame] | 761 | pgste_val(pgste) |= PGSTE_IN_BIT; |
Martin Schwidefsky | d338363 | 2013-04-17 10:53:39 +0200 | [diff] [blame] | 762 | pgste_set_unlock(ptep, pgste); |
Martin Schwidefsky | 6e0a043 | 2014-04-29 09:34:41 +0200 | [diff] [blame] | 763 | gaddr += PAGE_SIZE; |
Martin Schwidefsky | d338363 | 2013-04-17 10:53:39 +0200 | [diff] [blame] | 764 | len -= PAGE_SIZE; |
| 765 | } |
| 766 | spin_unlock(ptl); |
| 767 | } |
| 768 | up_read(&gmap->mm->mmap_sem); |
| 769 | return rc; |
| 770 | } |
| 771 | EXPORT_SYMBOL_GPL(gmap_ipte_notify); |
| 772 | |
| 773 | /** |
| 774 | * gmap_do_ipte_notify - call all invalidation callbacks for a specific pte. |
| 775 | * @mm: pointer to the process mm_struct |
Martin Schwidefsky | 9da4e38 | 2014-04-30 14:46:26 +0200 | [diff] [blame] | 776 | * @addr: virtual address in the process address space |
Martin Schwidefsky | d338363 | 2013-04-17 10:53:39 +0200 | [diff] [blame] | 777 | * @pte: pointer to the page table entry |
| 778 | * |
| 779 | * This function is assumed to be called with the page table lock held |
| 780 | * for the pte to notify. |
| 781 | */ |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 782 | void gmap_do_ipte_notify(struct mm_struct *mm, unsigned long vmaddr, pte_t *pte) |
Martin Schwidefsky | d338363 | 2013-04-17 10:53:39 +0200 | [diff] [blame] | 783 | { |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 784 | unsigned long offset, gaddr; |
| 785 | unsigned long *table; |
Martin Schwidefsky | d338363 | 2013-04-17 10:53:39 +0200 | [diff] [blame] | 786 | struct gmap_notifier *nb; |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 787 | struct gmap *gmap; |
Martin Schwidefsky | d338363 | 2013-04-17 10:53:39 +0200 | [diff] [blame] | 788 | |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 789 | offset = ((unsigned long) pte) & (255 * sizeof(pte_t)); |
| 790 | offset = offset * (4096 / sizeof(pte_t)); |
Martin Schwidefsky | d338363 | 2013-04-17 10:53:39 +0200 | [diff] [blame] | 791 | spin_lock(&gmap_notifier_lock); |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 792 | list_for_each_entry(gmap, &mm->context.gmap_list, list) { |
| 793 | table = radix_tree_lookup(&gmap->host_to_guest, |
| 794 | vmaddr >> PMD_SHIFT); |
| 795 | if (!table) |
| 796 | continue; |
| 797 | gaddr = __gmap_segment_gaddr(table) + offset; |
Martin Schwidefsky | d338363 | 2013-04-17 10:53:39 +0200 | [diff] [blame] | 798 | list_for_each_entry(nb, &gmap_notifier_list, list) |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 799 | nb->notifier_call(gmap, gaddr); |
Martin Schwidefsky | d338363 | 2013-04-17 10:53:39 +0200 | [diff] [blame] | 800 | } |
| 801 | spin_unlock(&gmap_notifier_lock); |
| 802 | } |
Martin Schwidefsky | 0a61b22 | 2013-10-18 12:03:41 +0200 | [diff] [blame] | 803 | EXPORT_SYMBOL_GPL(gmap_do_ipte_notify); |
Martin Schwidefsky | d338363 | 2013-04-17 10:53:39 +0200 | [diff] [blame] | 804 | |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame] | 805 | static inline int page_table_with_pgste(struct page *page) |
| 806 | { |
| 807 | return atomic_read(&page->_mapcount) == 0; |
| 808 | } |
| 809 | |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 810 | static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm) |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 811 | { |
| 812 | struct page *page; |
| 813 | unsigned long *table; |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 814 | |
| 815 | page = alloc_page(GFP_KERNEL|__GFP_REPEAT); |
| 816 | if (!page) |
| 817 | return NULL; |
Kirill A. Shutemov | e89cfa5 | 2013-11-14 14:31:39 -0800 | [diff] [blame] | 818 | if (!pgtable_page_ctor(page)) { |
Kirill A. Shutemov | e89cfa5 | 2013-11-14 14:31:39 -0800 | [diff] [blame] | 819 | __free_page(page); |
| 820 | return NULL; |
| 821 | } |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame] | 822 | atomic_set(&page->_mapcount, 0); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 823 | table = (unsigned long *) page_to_phys(page); |
Martin Schwidefsky | e509861 | 2013-07-23 20:57:57 +0200 | [diff] [blame] | 824 | clear_table(table, _PAGE_INVALID, PAGE_SIZE/2); |
Martin Schwidefsky | 0a61b22 | 2013-10-18 12:03:41 +0200 | [diff] [blame] | 825 | clear_table(table + PTRS_PER_PTE, 0, PAGE_SIZE/2); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 826 | return table; |
| 827 | } |
| 828 | |
| 829 | static inline void page_table_free_pgste(unsigned long *table) |
| 830 | { |
| 831 | struct page *page; |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 832 | |
| 833 | page = pfn_to_page(__pa(table) >> PAGE_SHIFT); |
Martin Schwidefsky | 2320c57 | 2012-02-17 10:29:21 +0100 | [diff] [blame] | 834 | pgtable_page_dtor(page); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 835 | atomic_set(&page->_mapcount, -1); |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 836 | __free_page(page); |
| 837 | } |
| 838 | |
Christian Borntraeger | 24d5dd0 | 2013-05-27 10:42:04 +0200 | [diff] [blame] | 839 | int set_guest_storage_key(struct mm_struct *mm, unsigned long addr, |
| 840 | unsigned long key, bool nq) |
| 841 | { |
| 842 | spinlock_t *ptl; |
| 843 | pgste_t old, new; |
| 844 | pte_t *ptep; |
| 845 | |
| 846 | down_read(&mm->mmap_sem); |
Christian Borntraeger | ab3f285 | 2014-08-19 16:19:35 +0200 | [diff] [blame] | 847 | retry: |
Christian Borntraeger | 24d5dd0 | 2013-05-27 10:42:04 +0200 | [diff] [blame] | 848 | ptep = get_locked_pte(current->mm, addr, &ptl); |
| 849 | if (unlikely(!ptep)) { |
| 850 | up_read(&mm->mmap_sem); |
| 851 | return -EFAULT; |
| 852 | } |
Christian Borntraeger | ab3f285 | 2014-08-19 16:19:35 +0200 | [diff] [blame] | 853 | if (!(pte_val(*ptep) & _PAGE_INVALID) && |
| 854 | (pte_val(*ptep) & _PAGE_PROTECT)) { |
Dominik Dingel | 66e9bbd | 2014-10-06 16:34:44 +0200 | [diff] [blame] | 855 | pte_unmap_unlock(ptep, ptl); |
Christian Borntraeger | dc77d34 | 2014-08-27 12:20:02 +0200 | [diff] [blame] | 856 | if (fixup_user_fault(current, mm, addr, FAULT_FLAG_WRITE)) { |
| 857 | up_read(&mm->mmap_sem); |
| 858 | return -EFAULT; |
Christian Borntraeger | ab3f285 | 2014-08-19 16:19:35 +0200 | [diff] [blame] | 859 | } |
Christian Borntraeger | dc77d34 | 2014-08-27 12:20:02 +0200 | [diff] [blame] | 860 | goto retry; |
| 861 | } |
Christian Borntraeger | 24d5dd0 | 2013-05-27 10:42:04 +0200 | [diff] [blame] | 862 | |
| 863 | new = old = pgste_get_lock(ptep); |
| 864 | pgste_val(new) &= ~(PGSTE_GR_BIT | PGSTE_GC_BIT | |
| 865 | PGSTE_ACC_BITS | PGSTE_FP_BIT); |
| 866 | pgste_val(new) |= (key & (_PAGE_CHANGED | _PAGE_REFERENCED)) << 48; |
| 867 | pgste_val(new) |= (key & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56; |
| 868 | if (!(pte_val(*ptep) & _PAGE_INVALID)) { |
Martin Schwidefsky | 0944fe3 | 2013-07-23 22:11:42 +0200 | [diff] [blame] | 869 | unsigned long address, bits, skey; |
Christian Borntraeger | 24d5dd0 | 2013-05-27 10:42:04 +0200 | [diff] [blame] | 870 | |
| 871 | address = pte_val(*ptep) & PAGE_MASK; |
Martin Schwidefsky | 0944fe3 | 2013-07-23 22:11:42 +0200 | [diff] [blame] | 872 | skey = (unsigned long) page_get_storage_key(address); |
Christian Borntraeger | 24d5dd0 | 2013-05-27 10:42:04 +0200 | [diff] [blame] | 873 | bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED); |
Martin Schwidefsky | 0944fe3 | 2013-07-23 22:11:42 +0200 | [diff] [blame] | 874 | skey = key & (_PAGE_ACC_BITS | _PAGE_FP_BIT); |
Christian Borntraeger | 24d5dd0 | 2013-05-27 10:42:04 +0200 | [diff] [blame] | 875 | /* Set storage key ACC and FP */ |
Martin Schwidefsky | 0944fe3 | 2013-07-23 22:11:42 +0200 | [diff] [blame] | 876 | page_set_storage_key(address, skey, !nq); |
Christian Borntraeger | 24d5dd0 | 2013-05-27 10:42:04 +0200 | [diff] [blame] | 877 | /* Merge host changed & referenced into pgste */ |
| 878 | pgste_val(new) |= bits << 52; |
Christian Borntraeger | 24d5dd0 | 2013-05-27 10:42:04 +0200 | [diff] [blame] | 879 | } |
| 880 | /* changing the guest storage key is considered a change of the page */ |
| 881 | if ((pgste_val(new) ^ pgste_val(old)) & |
| 882 | (PGSTE_ACC_BITS | PGSTE_FP_BIT | PGSTE_GR_BIT | PGSTE_GC_BIT)) |
Martin Schwidefsky | 0a61b22 | 2013-10-18 12:03:41 +0200 | [diff] [blame] | 883 | pgste_val(new) |= PGSTE_UC_BIT; |
Christian Borntraeger | 24d5dd0 | 2013-05-27 10:42:04 +0200 | [diff] [blame] | 884 | |
| 885 | pgste_set_unlock(ptep, new); |
Dominik Dingel | 66e9bbd | 2014-10-06 16:34:44 +0200 | [diff] [blame] | 886 | pte_unmap_unlock(ptep, ptl); |
Christian Borntraeger | 24d5dd0 | 2013-05-27 10:42:04 +0200 | [diff] [blame] | 887 | up_read(&mm->mmap_sem); |
| 888 | return 0; |
| 889 | } |
| 890 | EXPORT_SYMBOL(set_guest_storage_key); |
| 891 | |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 892 | #else /* CONFIG_PGSTE */ |
| 893 | |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame] | 894 | static inline int page_table_with_pgste(struct page *page) |
| 895 | { |
| 896 | return 0; |
| 897 | } |
| 898 | |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 899 | static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm) |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 900 | { |
Jan Glauber | 944291d | 2011-08-03 16:44:18 +0200 | [diff] [blame] | 901 | return NULL; |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | static inline void page_table_free_pgste(unsigned long *table) |
| 905 | { |
| 906 | } |
| 907 | |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 908 | static inline void gmap_unlink(struct mm_struct *mm, unsigned long *table, |
| 909 | unsigned long vmaddr) |
Martin Schwidefsky | e5992f2 | 2011-07-24 10:48:20 +0200 | [diff] [blame] | 910 | { |
| 911 | } |
| 912 | |
| 913 | #endif /* CONFIG_PGSTE */ |
| 914 | |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 915 | static inline unsigned int atomic_xor_bits(atomic_t *v, unsigned int bits) |
| 916 | { |
| 917 | unsigned int old, new; |
| 918 | |
| 919 | do { |
| 920 | old = atomic_read(v); |
| 921 | new = old ^ bits; |
| 922 | } while (atomic_cmpxchg(v, old, new) != old); |
| 923 | return new; |
| 924 | } |
| 925 | |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 926 | /* |
| 927 | * page table entry allocation/free routines. |
| 928 | */ |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 929 | unsigned long *page_table_alloc(struct mm_struct *mm) |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 930 | { |
Heiko Carstens | 41459d3 | 2012-09-14 11:09:52 +0200 | [diff] [blame] | 931 | unsigned long *uninitialized_var(table); |
| 932 | struct page *uninitialized_var(page); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 933 | unsigned int mask, bit; |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 934 | |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 935 | if (mm_has_pgste(mm)) |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 936 | return page_table_alloc_pgste(mm); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 937 | /* Allocate fragments of a 4K page as 1K/2K page table */ |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 938 | spin_lock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 939 | mask = FRAG_MASK; |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 940 | if (!list_empty(&mm->context.pgtable_list)) { |
| 941 | page = list_first_entry(&mm->context.pgtable_list, |
| 942 | struct page, lru); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 943 | table = (unsigned long *) page_to_phys(page); |
| 944 | mask = atomic_read(&page->_mapcount); |
| 945 | mask = mask | (mask >> 4); |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 946 | } |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 947 | if ((mask & FRAG_MASK) == FRAG_MASK) { |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 948 | spin_unlock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 949 | page = alloc_page(GFP_KERNEL|__GFP_REPEAT); |
| 950 | if (!page) |
| 951 | return NULL; |
Kirill A. Shutemov | e89cfa5 | 2013-11-14 14:31:39 -0800 | [diff] [blame] | 952 | if (!pgtable_page_ctor(page)) { |
| 953 | __free_page(page); |
| 954 | return NULL; |
| 955 | } |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 956 | atomic_set(&page->_mapcount, 1); |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 957 | table = (unsigned long *) page_to_phys(page); |
Martin Schwidefsky | e509861 | 2013-07-23 20:57:57 +0200 | [diff] [blame] | 958 | clear_table(table, _PAGE_INVALID, PAGE_SIZE); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 959 | spin_lock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 960 | list_add(&page->lru, &mm->context.pgtable_list); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 961 | } else { |
| 962 | for (bit = 1; mask & bit; bit <<= 1) |
| 963 | table += PTRS_PER_PTE; |
| 964 | mask = atomic_xor_bits(&page->_mapcount, bit); |
| 965 | if ((mask & FRAG_MASK) == FRAG_MASK) |
| 966 | list_del(&page->lru); |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 967 | } |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 968 | spin_unlock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 969 | return table; |
| 970 | } |
| 971 | |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 972 | void page_table_free(struct mm_struct *mm, unsigned long *table) |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 973 | { |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 974 | struct page *page; |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 975 | unsigned int bit, mask; |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 976 | |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame] | 977 | page = pfn_to_page(__pa(table) >> PAGE_SHIFT); |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 978 | if (page_table_with_pgste(page)) |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 979 | return page_table_free_pgste(table); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 980 | /* Free 1K/2K page table fragment of a 4K page */ |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 981 | bit = 1 << ((__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t))); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 982 | spin_lock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 983 | if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK) |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 984 | list_del(&page->lru); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 985 | mask = atomic_xor_bits(&page->_mapcount, bit); |
| 986 | if (mask & FRAG_MASK) |
| 987 | list_add(&page->lru, &mm->context.pgtable_list); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 988 | spin_unlock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 989 | if (mask == 0) { |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 990 | pgtable_page_dtor(page); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 991 | atomic_set(&page->_mapcount, -1); |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 992 | __free_page(page); |
| 993 | } |
| 994 | } |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 995 | |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 996 | static void __page_table_free_rcu(void *table, unsigned bit) |
| 997 | { |
| 998 | struct page *page; |
| 999 | |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 1000 | if (bit == FRAG_MASK) |
| 1001 | return page_table_free_pgste(table); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 1002 | /* Free 1K/2K page table fragment of a 4K page */ |
| 1003 | page = pfn_to_page(__pa(table) >> PAGE_SHIFT); |
| 1004 | if (atomic_xor_bits(&page->_mapcount, bit) == 0) { |
| 1005 | pgtable_page_dtor(page); |
| 1006 | atomic_set(&page->_mapcount, -1); |
| 1007 | __free_page(page); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 1008 | } |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 1009 | } |
| 1010 | |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 1011 | void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table, |
| 1012 | unsigned long vmaddr) |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 1013 | { |
| 1014 | struct mm_struct *mm; |
| 1015 | struct page *page; |
| 1016 | unsigned int bit, mask; |
| 1017 | |
| 1018 | mm = tlb->mm; |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame] | 1019 | page = pfn_to_page(__pa(table) >> PAGE_SHIFT); |
| 1020 | if (page_table_with_pgste(page)) { |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 1021 | gmap_unlink(mm, table, vmaddr); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 1022 | table = (unsigned long *) (__pa(table) | FRAG_MASK); |
| 1023 | tlb_remove_table(tlb, table); |
| 1024 | return; |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 1025 | } |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 1026 | bit = 1 << ((__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t))); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 1027 | spin_lock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 1028 | if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK) |
| 1029 | list_del(&page->lru); |
| 1030 | mask = atomic_xor_bits(&page->_mapcount, bit | (bit << 4)); |
| 1031 | if (mask & FRAG_MASK) |
| 1032 | list_add_tail(&page->lru, &mm->context.pgtable_list); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 1033 | spin_unlock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 1034 | table = (unsigned long *) (__pa(table) | (bit << 4)); |
| 1035 | tlb_remove_table(tlb, table); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 1036 | } |
| 1037 | |
Heiko Carstens | 63df41d6 | 2013-09-06 19:10:48 +0200 | [diff] [blame] | 1038 | static void __tlb_remove_table(void *_table) |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 1039 | { |
Martin Schwidefsky | e73b7ff | 2011-10-30 15:16:08 +0100 | [diff] [blame] | 1040 | const unsigned long mask = (FRAG_MASK << 4) | FRAG_MASK; |
| 1041 | void *table = (void *)((unsigned long) _table & ~mask); |
| 1042 | unsigned type = (unsigned long) _table & mask; |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 1043 | |
| 1044 | if (type) |
| 1045 | __page_table_free_rcu(table, type); |
| 1046 | else |
| 1047 | free_pages((unsigned long) table, ALLOC_ORDER); |
| 1048 | } |
| 1049 | |
Martin Schwidefsky | cd94154cc | 2012-04-11 14:28:07 +0200 | [diff] [blame] | 1050 | static void tlb_remove_table_smp_sync(void *arg) |
| 1051 | { |
| 1052 | /* Simply deliver the interrupt */ |
| 1053 | } |
| 1054 | |
| 1055 | static void tlb_remove_table_one(void *table) |
| 1056 | { |
| 1057 | /* |
| 1058 | * This isn't an RCU grace period and hence the page-tables cannot be |
| 1059 | * assumed to be actually RCU-freed. |
| 1060 | * |
| 1061 | * It is however sufficient for software page-table walkers that rely |
| 1062 | * on IRQ disabling. See the comment near struct mmu_table_batch. |
| 1063 | */ |
| 1064 | smp_call_function(tlb_remove_table_smp_sync, NULL, 1); |
| 1065 | __tlb_remove_table(table); |
| 1066 | } |
| 1067 | |
| 1068 | static void tlb_remove_table_rcu(struct rcu_head *head) |
| 1069 | { |
| 1070 | struct mmu_table_batch *batch; |
| 1071 | int i; |
| 1072 | |
| 1073 | batch = container_of(head, struct mmu_table_batch, rcu); |
| 1074 | |
| 1075 | for (i = 0; i < batch->nr; i++) |
| 1076 | __tlb_remove_table(batch->tables[i]); |
| 1077 | |
| 1078 | free_page((unsigned long)batch); |
| 1079 | } |
| 1080 | |
| 1081 | void tlb_table_flush(struct mmu_gather *tlb) |
| 1082 | { |
| 1083 | struct mmu_table_batch **batch = &tlb->batch; |
| 1084 | |
| 1085 | if (*batch) { |
Martin Schwidefsky | cd94154cc | 2012-04-11 14:28:07 +0200 | [diff] [blame] | 1086 | call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu); |
| 1087 | *batch = NULL; |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | void tlb_remove_table(struct mmu_gather *tlb, void *table) |
| 1092 | { |
| 1093 | struct mmu_table_batch **batch = &tlb->batch; |
| 1094 | |
Martin Schwidefsky | 5c474a1 | 2013-08-16 13:31:40 +0200 | [diff] [blame] | 1095 | tlb->mm->context.flush_mm = 1; |
Martin Schwidefsky | cd94154cc | 2012-04-11 14:28:07 +0200 | [diff] [blame] | 1096 | if (*batch == NULL) { |
| 1097 | *batch = (struct mmu_table_batch *) |
| 1098 | __get_free_page(GFP_NOWAIT | __GFP_NOWARN); |
| 1099 | if (*batch == NULL) { |
Martin Schwidefsky | 5c474a1 | 2013-08-16 13:31:40 +0200 | [diff] [blame] | 1100 | __tlb_flush_mm_lazy(tlb->mm); |
Martin Schwidefsky | cd94154cc | 2012-04-11 14:28:07 +0200 | [diff] [blame] | 1101 | tlb_remove_table_one(table); |
| 1102 | return; |
| 1103 | } |
| 1104 | (*batch)->nr = 0; |
| 1105 | } |
| 1106 | (*batch)->tables[(*batch)->nr++] = table; |
| 1107 | if ((*batch)->nr == MAX_TABLE_BATCH) |
Martin Schwidefsky | 5c474a1 | 2013-08-16 13:31:40 +0200 | [diff] [blame] | 1108 | tlb_flush_mmu(tlb); |
Martin Schwidefsky | cd94154cc | 2012-04-11 14:28:07 +0200 | [diff] [blame] | 1109 | } |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 1110 | |
Gerald Schaefer | 274023d | 2012-10-08 16:30:21 -0700 | [diff] [blame] | 1111 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame] | 1112 | static inline void thp_split_vma(struct vm_area_struct *vma) |
Gerald Schaefer | 274023d | 2012-10-08 16:30:21 -0700 | [diff] [blame] | 1113 | { |
| 1114 | unsigned long addr; |
Gerald Schaefer | 274023d | 2012-10-08 16:30:21 -0700 | [diff] [blame] | 1115 | |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame] | 1116 | for (addr = vma->vm_start; addr < vma->vm_end; addr += PAGE_SIZE) |
| 1117 | follow_page(vma, addr, FOLL_SPLIT); |
Gerald Schaefer | 274023d | 2012-10-08 16:30:21 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame] | 1120 | static inline void thp_split_mm(struct mm_struct *mm) |
Gerald Schaefer | 274023d | 2012-10-08 16:30:21 -0700 | [diff] [blame] | 1121 | { |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame] | 1122 | struct vm_area_struct *vma; |
Gerald Schaefer | 274023d | 2012-10-08 16:30:21 -0700 | [diff] [blame] | 1123 | |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame] | 1124 | for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) { |
Gerald Schaefer | 274023d | 2012-10-08 16:30:21 -0700 | [diff] [blame] | 1125 | thp_split_vma(vma); |
| 1126 | vma->vm_flags &= ~VM_HUGEPAGE; |
| 1127 | vma->vm_flags |= VM_NOHUGEPAGE; |
Gerald Schaefer | 274023d | 2012-10-08 16:30:21 -0700 | [diff] [blame] | 1128 | } |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame] | 1129 | mm->def_flags |= VM_NOHUGEPAGE; |
| 1130 | } |
| 1131 | #else |
| 1132 | static inline void thp_split_mm(struct mm_struct *mm) |
| 1133 | { |
Gerald Schaefer | 274023d | 2012-10-08 16:30:21 -0700 | [diff] [blame] | 1134 | } |
| 1135 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
| 1136 | |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame] | 1137 | static unsigned long page_table_realloc_pmd(struct mmu_gather *tlb, |
| 1138 | struct mm_struct *mm, pud_t *pud, |
| 1139 | unsigned long addr, unsigned long end) |
| 1140 | { |
| 1141 | unsigned long next, *table, *new; |
| 1142 | struct page *page; |
Christian Borntraeger | 55e4283 | 2014-07-25 14:23:29 +0200 | [diff] [blame] | 1143 | spinlock_t *ptl; |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame] | 1144 | pmd_t *pmd; |
| 1145 | |
| 1146 | pmd = pmd_offset(pud, addr); |
| 1147 | do { |
| 1148 | next = pmd_addr_end(addr, end); |
| 1149 | again: |
| 1150 | if (pmd_none_or_clear_bad(pmd)) |
| 1151 | continue; |
| 1152 | table = (unsigned long *) pmd_deref(*pmd); |
| 1153 | page = pfn_to_page(__pa(table) >> PAGE_SHIFT); |
| 1154 | if (page_table_with_pgste(page)) |
| 1155 | continue; |
| 1156 | /* Allocate new page table with pgstes */ |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 1157 | new = page_table_alloc_pgste(mm); |
Dominik Dingel | be39f19 | 2013-10-31 10:01:16 +0100 | [diff] [blame] | 1158 | if (!new) |
| 1159 | return -ENOMEM; |
| 1160 | |
Christian Borntraeger | 55e4283 | 2014-07-25 14:23:29 +0200 | [diff] [blame] | 1161 | ptl = pmd_lock(mm, pmd); |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame] | 1162 | if (likely((unsigned long *) pmd_deref(*pmd) == table)) { |
| 1163 | /* Nuke pmd entry pointing to the "short" page table */ |
| 1164 | pmdp_flush_lazy(mm, addr, pmd); |
| 1165 | pmd_clear(pmd); |
| 1166 | /* Copy ptes from old table to new table */ |
| 1167 | memcpy(new, table, PAGE_SIZE/2); |
| 1168 | clear_table(table, _PAGE_INVALID, PAGE_SIZE/2); |
| 1169 | /* Establish new table */ |
| 1170 | pmd_populate(mm, pmd, (pte_t *) new); |
| 1171 | /* Free old table with rcu, there might be a walker! */ |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 1172 | page_table_free_rcu(tlb, table, addr); |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame] | 1173 | new = NULL; |
| 1174 | } |
Christian Borntraeger | 55e4283 | 2014-07-25 14:23:29 +0200 | [diff] [blame] | 1175 | spin_unlock(ptl); |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame] | 1176 | if (new) { |
| 1177 | page_table_free_pgste(new); |
| 1178 | goto again; |
| 1179 | } |
| 1180 | } while (pmd++, addr = next, addr != end); |
| 1181 | |
| 1182 | return addr; |
| 1183 | } |
| 1184 | |
| 1185 | static unsigned long page_table_realloc_pud(struct mmu_gather *tlb, |
| 1186 | struct mm_struct *mm, pgd_t *pgd, |
| 1187 | unsigned long addr, unsigned long end) |
| 1188 | { |
| 1189 | unsigned long next; |
| 1190 | pud_t *pud; |
| 1191 | |
| 1192 | pud = pud_offset(pgd, addr); |
| 1193 | do { |
| 1194 | next = pud_addr_end(addr, end); |
| 1195 | if (pud_none_or_clear_bad(pud)) |
| 1196 | continue; |
| 1197 | next = page_table_realloc_pmd(tlb, mm, pud, addr, next); |
Dominik Dingel | be39f19 | 2013-10-31 10:01:16 +0100 | [diff] [blame] | 1198 | if (unlikely(IS_ERR_VALUE(next))) |
| 1199 | return next; |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame] | 1200 | } while (pud++, addr = next, addr != end); |
| 1201 | |
| 1202 | return addr; |
| 1203 | } |
| 1204 | |
Dominik Dingel | be39f19 | 2013-10-31 10:01:16 +0100 | [diff] [blame] | 1205 | static unsigned long page_table_realloc(struct mmu_gather *tlb, struct mm_struct *mm, |
| 1206 | unsigned long addr, unsigned long end) |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame] | 1207 | { |
| 1208 | unsigned long next; |
| 1209 | pgd_t *pgd; |
| 1210 | |
| 1211 | pgd = pgd_offset(mm, addr); |
| 1212 | do { |
| 1213 | next = pgd_addr_end(addr, end); |
| 1214 | if (pgd_none_or_clear_bad(pgd)) |
| 1215 | continue; |
| 1216 | next = page_table_realloc_pud(tlb, mm, pgd, addr, next); |
Dominik Dingel | be39f19 | 2013-10-31 10:01:16 +0100 | [diff] [blame] | 1217 | if (unlikely(IS_ERR_VALUE(next))) |
| 1218 | return next; |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame] | 1219 | } while (pgd++, addr = next, addr != end); |
Dominik Dingel | be39f19 | 2013-10-31 10:01:16 +0100 | [diff] [blame] | 1220 | |
| 1221 | return 0; |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame] | 1222 | } |
| 1223 | |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 1224 | /* |
| 1225 | * switch on pgstes for its userspace process (for kvm) |
| 1226 | */ |
| 1227 | int s390_enable_sie(void) |
| 1228 | { |
| 1229 | struct task_struct *tsk = current; |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame] | 1230 | struct mm_struct *mm = tsk->mm; |
| 1231 | struct mmu_gather tlb; |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 1232 | |
Christian Borntraeger | 74b6b52 | 2008-05-21 13:37:29 +0200 | [diff] [blame] | 1233 | /* Do we have pgstes? if yes, we are done */ |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 1234 | if (mm_has_pgste(tsk->mm)) |
Christian Borntraeger | 74b6b52 | 2008-05-21 13:37:29 +0200 | [diff] [blame] | 1235 | return 0; |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 1236 | |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame] | 1237 | down_write(&mm->mmap_sem); |
Gerald Schaefer | 274023d | 2012-10-08 16:30:21 -0700 | [diff] [blame] | 1238 | /* split thp mappings and disable thp for future mappings */ |
| 1239 | thp_split_mm(mm); |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame] | 1240 | /* Reallocate the page tables with pgstes */ |
Linus Torvalds | ae7a835 | 2013-09-04 18:15:06 -0700 | [diff] [blame] | 1241 | tlb_gather_mmu(&tlb, mm, 0, TASK_SIZE); |
Dominik Dingel | be39f19 | 2013-10-31 10:01:16 +0100 | [diff] [blame] | 1242 | if (!page_table_realloc(&tlb, mm, 0, TASK_SIZE)) |
| 1243 | mm->context.has_pgste = 1; |
Linus Torvalds | ae7a835 | 2013-09-04 18:15:06 -0700 | [diff] [blame] | 1244 | tlb_finish_mmu(&tlb, 0, TASK_SIZE); |
Martin Schwidefsky | 3eabaee | 2013-07-26 15:04:02 +0200 | [diff] [blame] | 1245 | up_write(&mm->mmap_sem); |
| 1246 | return mm->context.has_pgste ? 0 : -ENOMEM; |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 1247 | } |
| 1248 | EXPORT_SYMBOL_GPL(s390_enable_sie); |
Hans-Joachim Picht | 7db11a3 | 2009-06-16 10:30:26 +0200 | [diff] [blame] | 1249 | |
Dominik Dingel | 934bc13 | 2014-01-14 18:10:17 +0100 | [diff] [blame] | 1250 | /* |
| 1251 | * Enable storage key handling from now on and initialize the storage |
| 1252 | * keys with the default key. |
| 1253 | */ |
Dominik Dingel | a13cff3 | 2014-10-23 12:07:14 +0200 | [diff] [blame] | 1254 | static int __s390_enable_skey(pte_t *pte, unsigned long addr, |
| 1255 | unsigned long next, struct mm_walk *walk) |
| 1256 | { |
| 1257 | unsigned long ptev; |
| 1258 | pgste_t pgste; |
| 1259 | |
| 1260 | pgste = pgste_get_lock(pte); |
Dominik Dingel | 2faee8f | 2014-10-23 12:08:38 +0200 | [diff] [blame] | 1261 | /* |
| 1262 | * Remove all zero page mappings, |
| 1263 | * after establishing a policy to forbid zero page mappings |
| 1264 | * following faults for that page will get fresh anonymous pages |
| 1265 | */ |
| 1266 | if (is_zero_pfn(pte_pfn(*pte))) { |
| 1267 | ptep_flush_direct(walk->mm, addr, pte); |
| 1268 | pte_val(*pte) = _PAGE_INVALID; |
| 1269 | } |
Dominik Dingel | a13cff3 | 2014-10-23 12:07:14 +0200 | [diff] [blame] | 1270 | /* Clear storage key */ |
| 1271 | pgste_val(pgste) &= ~(PGSTE_ACC_BITS | PGSTE_FP_BIT | |
| 1272 | PGSTE_GR_BIT | PGSTE_GC_BIT); |
| 1273 | ptev = pte_val(*pte); |
| 1274 | if (!(ptev & _PAGE_INVALID) && (ptev & _PAGE_WRITE)) |
| 1275 | page_set_storage_key(ptev & PAGE_MASK, PAGE_DEFAULT_KEY, 1); |
| 1276 | pgste_set_unlock(pte, pgste); |
| 1277 | return 0; |
| 1278 | } |
| 1279 | |
Dominik Dingel | 3ac8e38 | 2014-10-23 12:09:17 +0200 | [diff] [blame^] | 1280 | int s390_enable_skey(void) |
Dominik Dingel | 934bc13 | 2014-01-14 18:10:17 +0100 | [diff] [blame] | 1281 | { |
Dominik Dingel | a13cff3 | 2014-10-23 12:07:14 +0200 | [diff] [blame] | 1282 | struct mm_walk walk = { .pte_entry = __s390_enable_skey }; |
| 1283 | struct mm_struct *mm = current->mm; |
Dominik Dingel | 3ac8e38 | 2014-10-23 12:09:17 +0200 | [diff] [blame^] | 1284 | struct vm_area_struct *vma; |
| 1285 | int rc = 0; |
Dominik Dingel | a13cff3 | 2014-10-23 12:07:14 +0200 | [diff] [blame] | 1286 | |
| 1287 | down_write(&mm->mmap_sem); |
| 1288 | if (mm_use_skey(mm)) |
| 1289 | goto out_up; |
Dominik Dingel | 2faee8f | 2014-10-23 12:08:38 +0200 | [diff] [blame] | 1290 | |
| 1291 | mm->context.use_skey = 1; |
Dominik Dingel | 3ac8e38 | 2014-10-23 12:09:17 +0200 | [diff] [blame^] | 1292 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 1293 | if (ksm_madvise(vma, vma->vm_start, vma->vm_end, |
| 1294 | MADV_UNMERGEABLE, &vma->vm_flags)) { |
| 1295 | mm->context.use_skey = 0; |
| 1296 | rc = -ENOMEM; |
| 1297 | goto out_up; |
| 1298 | } |
| 1299 | } |
| 1300 | mm->def_flags &= ~VM_MERGEABLE; |
Dominik Dingel | 2faee8f | 2014-10-23 12:08:38 +0200 | [diff] [blame] | 1301 | |
Dominik Dingel | a13cff3 | 2014-10-23 12:07:14 +0200 | [diff] [blame] | 1302 | walk.mm = mm; |
| 1303 | walk_page_range(0, TASK_SIZE, &walk); |
Dominik Dingel | a13cff3 | 2014-10-23 12:07:14 +0200 | [diff] [blame] | 1304 | |
| 1305 | out_up: |
| 1306 | up_write(&mm->mmap_sem); |
Dominik Dingel | 3ac8e38 | 2014-10-23 12:09:17 +0200 | [diff] [blame^] | 1307 | return rc; |
Dominik Dingel | 934bc13 | 2014-01-14 18:10:17 +0100 | [diff] [blame] | 1308 | } |
| 1309 | EXPORT_SYMBOL_GPL(s390_enable_skey); |
| 1310 | |
Dominik Dingel | a0bf4f1 | 2014-03-24 14:27:58 +0100 | [diff] [blame] | 1311 | /* |
Dominik Dingel | a13cff3 | 2014-10-23 12:07:14 +0200 | [diff] [blame] | 1312 | * Reset CMMA state, make all pages stable again. |
| 1313 | */ |
| 1314 | static int __s390_reset_cmma(pte_t *pte, unsigned long addr, |
| 1315 | unsigned long next, struct mm_walk *walk) |
| 1316 | { |
| 1317 | pgste_t pgste; |
| 1318 | |
| 1319 | pgste = pgste_get_lock(pte); |
| 1320 | pgste_val(pgste) &= ~_PGSTE_GPS_USAGE_MASK; |
| 1321 | pgste_set_unlock(pte, pgste); |
| 1322 | return 0; |
| 1323 | } |
| 1324 | |
| 1325 | void s390_reset_cmma(struct mm_struct *mm) |
| 1326 | { |
| 1327 | struct mm_walk walk = { .pte_entry = __s390_reset_cmma }; |
| 1328 | |
| 1329 | down_write(&mm->mmap_sem); |
| 1330 | walk.mm = mm; |
| 1331 | walk_page_range(0, TASK_SIZE, &walk); |
| 1332 | up_write(&mm->mmap_sem); |
| 1333 | } |
| 1334 | EXPORT_SYMBOL_GPL(s390_reset_cmma); |
| 1335 | |
| 1336 | /* |
Dominik Dingel | a0bf4f1 | 2014-03-24 14:27:58 +0100 | [diff] [blame] | 1337 | * Test and reset if a guest page is dirty |
| 1338 | */ |
| 1339 | bool gmap_test_and_clear_dirty(unsigned long address, struct gmap *gmap) |
| 1340 | { |
| 1341 | pte_t *pte; |
| 1342 | spinlock_t *ptl; |
| 1343 | bool dirty = false; |
| 1344 | |
| 1345 | pte = get_locked_pte(gmap->mm, address, &ptl); |
| 1346 | if (unlikely(!pte)) |
| 1347 | return false; |
| 1348 | |
| 1349 | if (ptep_test_and_clear_user_dirty(gmap->mm, address, pte)) |
| 1350 | dirty = true; |
| 1351 | |
| 1352 | spin_unlock(ptl); |
| 1353 | return dirty; |
| 1354 | } |
| 1355 | EXPORT_SYMBOL_GPL(gmap_test_and_clear_dirty); |
| 1356 | |
Gerald Schaefer | 75077af | 2012-10-08 16:30:15 -0700 | [diff] [blame] | 1357 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Gerald Schaefer | 1ae1c1d | 2012-10-08 16:30:24 -0700 | [diff] [blame] | 1358 | int pmdp_clear_flush_young(struct vm_area_struct *vma, unsigned long address, |
| 1359 | pmd_t *pmdp) |
| 1360 | { |
| 1361 | VM_BUG_ON(address & ~HPAGE_PMD_MASK); |
| 1362 | /* No need to flush TLB |
| 1363 | * On s390 reference bits are in storage key and never in TLB */ |
| 1364 | return pmdp_test_and_clear_young(vma, address, pmdp); |
| 1365 | } |
| 1366 | |
| 1367 | int pmdp_set_access_flags(struct vm_area_struct *vma, |
| 1368 | unsigned long address, pmd_t *pmdp, |
| 1369 | pmd_t entry, int dirty) |
| 1370 | { |
| 1371 | VM_BUG_ON(address & ~HPAGE_PMD_MASK); |
| 1372 | |
Martin Schwidefsky | 152125b | 2014-07-24 11:03:41 +0200 | [diff] [blame] | 1373 | entry = pmd_mkyoung(entry); |
| 1374 | if (dirty) |
| 1375 | entry = pmd_mkdirty(entry); |
Gerald Schaefer | 1ae1c1d | 2012-10-08 16:30:24 -0700 | [diff] [blame] | 1376 | if (pmd_same(*pmdp, entry)) |
| 1377 | return 0; |
| 1378 | pmdp_invalidate(vma, address, pmdp); |
| 1379 | set_pmd_at(vma->vm_mm, address, pmdp, entry); |
| 1380 | return 1; |
| 1381 | } |
| 1382 | |
Gerald Schaefer | 75077af | 2012-10-08 16:30:15 -0700 | [diff] [blame] | 1383 | static void pmdp_splitting_flush_sync(void *arg) |
| 1384 | { |
| 1385 | /* Simply deliver the interrupt */ |
| 1386 | } |
| 1387 | |
| 1388 | void pmdp_splitting_flush(struct vm_area_struct *vma, unsigned long address, |
| 1389 | pmd_t *pmdp) |
| 1390 | { |
| 1391 | VM_BUG_ON(address & ~HPAGE_PMD_MASK); |
| 1392 | if (!test_and_set_bit(_SEGMENT_ENTRY_SPLIT_BIT, |
| 1393 | (unsigned long *) pmdp)) { |
| 1394 | /* need to serialize against gup-fast (IRQ disabled) */ |
| 1395 | smp_call_function(pmdp_splitting_flush_sync, NULL, 1); |
| 1396 | } |
| 1397 | } |
Gerald Schaefer | 9501d09 | 2012-10-08 16:30:18 -0700 | [diff] [blame] | 1398 | |
Aneesh Kumar K.V | 6b0b50b | 2013-06-05 17:14:02 -0700 | [diff] [blame] | 1399 | void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp, |
| 1400 | pgtable_t pgtable) |
Gerald Schaefer | 9501d09 | 2012-10-08 16:30:18 -0700 | [diff] [blame] | 1401 | { |
| 1402 | struct list_head *lh = (struct list_head *) pgtable; |
| 1403 | |
Martin Schwidefsky | ec66ad6 | 2014-02-12 14:16:18 +0100 | [diff] [blame] | 1404 | assert_spin_locked(pmd_lockptr(mm, pmdp)); |
Gerald Schaefer | 9501d09 | 2012-10-08 16:30:18 -0700 | [diff] [blame] | 1405 | |
| 1406 | /* FIFO */ |
Kirill A. Shutemov | c389a25 | 2013-11-14 14:30:59 -0800 | [diff] [blame] | 1407 | if (!pmd_huge_pte(mm, pmdp)) |
Gerald Schaefer | 9501d09 | 2012-10-08 16:30:18 -0700 | [diff] [blame] | 1408 | INIT_LIST_HEAD(lh); |
| 1409 | else |
Kirill A. Shutemov | c389a25 | 2013-11-14 14:30:59 -0800 | [diff] [blame] | 1410 | list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp)); |
| 1411 | pmd_huge_pte(mm, pmdp) = pgtable; |
Gerald Schaefer | 9501d09 | 2012-10-08 16:30:18 -0700 | [diff] [blame] | 1412 | } |
| 1413 | |
Aneesh Kumar K.V | 6b0b50b | 2013-06-05 17:14:02 -0700 | [diff] [blame] | 1414 | pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp) |
Gerald Schaefer | 9501d09 | 2012-10-08 16:30:18 -0700 | [diff] [blame] | 1415 | { |
| 1416 | struct list_head *lh; |
| 1417 | pgtable_t pgtable; |
| 1418 | pte_t *ptep; |
| 1419 | |
Martin Schwidefsky | ec66ad6 | 2014-02-12 14:16:18 +0100 | [diff] [blame] | 1420 | assert_spin_locked(pmd_lockptr(mm, pmdp)); |
Gerald Schaefer | 9501d09 | 2012-10-08 16:30:18 -0700 | [diff] [blame] | 1421 | |
| 1422 | /* FIFO */ |
Kirill A. Shutemov | c389a25 | 2013-11-14 14:30:59 -0800 | [diff] [blame] | 1423 | pgtable = pmd_huge_pte(mm, pmdp); |
Gerald Schaefer | 9501d09 | 2012-10-08 16:30:18 -0700 | [diff] [blame] | 1424 | lh = (struct list_head *) pgtable; |
| 1425 | if (list_empty(lh)) |
Kirill A. Shutemov | c389a25 | 2013-11-14 14:30:59 -0800 | [diff] [blame] | 1426 | pmd_huge_pte(mm, pmdp) = NULL; |
Gerald Schaefer | 9501d09 | 2012-10-08 16:30:18 -0700 | [diff] [blame] | 1427 | else { |
Kirill A. Shutemov | c389a25 | 2013-11-14 14:30:59 -0800 | [diff] [blame] | 1428 | pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next; |
Gerald Schaefer | 9501d09 | 2012-10-08 16:30:18 -0700 | [diff] [blame] | 1429 | list_del(lh); |
| 1430 | } |
| 1431 | ptep = (pte_t *) pgtable; |
Martin Schwidefsky | e509861 | 2013-07-23 20:57:57 +0200 | [diff] [blame] | 1432 | pte_val(*ptep) = _PAGE_INVALID; |
Gerald Schaefer | 9501d09 | 2012-10-08 16:30:18 -0700 | [diff] [blame] | 1433 | ptep++; |
Martin Schwidefsky | e509861 | 2013-07-23 20:57:57 +0200 | [diff] [blame] | 1434 | pte_val(*ptep) = _PAGE_INVALID; |
Gerald Schaefer | 9501d09 | 2012-10-08 16:30:18 -0700 | [diff] [blame] | 1435 | return pgtable; |
| 1436 | } |
Gerald Schaefer | 75077af | 2012-10-08 16:30:15 -0700 | [diff] [blame] | 1437 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |