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