blob: 71c7eff2c89f0a6489baf3edfd28bdc0e1c228ef [file] [log] [blame]
Martin Schwidefsky3610cce2007-10-22 12:52:47 +02001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * Copyright IBM Corp. 2007, 2011
Martin Schwidefsky3610cce2007-10-22 12:52:47 +02003 * 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 Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/gfp.h>
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020010#include <linux/mm.h>
11#include <linux/swap.h>
12#include <linux/smp.h>
13#include <linux/highmem.h>
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020014#include <linux/pagemap.h>
15#include <linux/spinlock.h>
16#include <linux/module.h>
17#include <linux/quicklist.h>
Martin Schwidefsky80217142010-10-25 16:10:11 +020018#include <linux/rcupdate.h>
Martin Schwidefskye5992f22011-07-24 10:48:20 +020019#include <linux/slab.h>
Konstantin Weitzb31288f2013-04-17 17:36:29 +020020#include <linux/swapops.h>
Dominik Dingel3ac8e382014-10-23 12:09:17 +020021#include <linux/ksm.h>
22#include <linux/mman.h>
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020023
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020024#include <asm/pgtable.h>
25#include <asm/pgalloc.h>
26#include <asm/tlb.h>
27#include <asm/tlbflush.h>
Martin Schwidefsky6252d702008-02-09 18:24:37 +010028#include <asm/mmu_context.h>
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020029
30#ifndef CONFIG_64BIT
31#define ALLOC_ORDER 1
Martin Schwidefsky36409f62011-06-06 14:14:41 +020032#define FRAG_MASK 0x0f
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020033#else
34#define ALLOC_ORDER 2
Martin Schwidefsky36409f62011-06-06 14:14:41 +020035#define FRAG_MASK 0x03
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020036#endif
37
Heiko Carstens239a64252009-06-12 10:26:33 +020038
Martin Schwidefsky043d0702011-05-23 10:24:23 +020039unsigned long *crst_table_alloc(struct mm_struct *mm)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020040{
41 struct page *page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
42
43 if (!page)
44 return NULL;
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020045 return (unsigned long *) page_to_phys(page);
46}
47
Martin Schwidefsky146e4b32008-02-09 18:24:35 +010048void crst_table_free(struct mm_struct *mm, unsigned long *table)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020049{
Martin Schwidefsky043d0702011-05-23 10:24:23 +020050 free_pages((unsigned long) table, ALLOC_ORDER);
Martin Schwidefsky80217142010-10-25 16:10:11 +020051}
52
Martin Schwidefsky6252d702008-02-09 18:24:37 +010053#ifdef CONFIG_64BIT
Martin Schwidefsky10607862013-10-28 14:48:30 +010054static void __crst_table_upgrade(void *arg)
55{
56 struct mm_struct *mm = arg;
57
Martin Schwidefskybeef5602014-04-14 15:11:26 +020058 if (current->active_mm == mm) {
59 clear_user_asce();
60 set_user_asce(mm);
61 }
Martin Schwidefsky10607862013-10-28 14:48:30 +010062 __tlb_flush_local();
63}
64
Martin Schwidefsky6252d702008-02-09 18:24:37 +010065int crst_table_upgrade(struct mm_struct *mm, unsigned long limit)
66{
67 unsigned long *table, *pgd;
68 unsigned long entry;
Martin Schwidefsky10607862013-10-28 14:48:30 +010069 int flush;
Martin Schwidefsky6252d702008-02-09 18:24:37 +010070
71 BUG_ON(limit > (1UL << 53));
Martin Schwidefsky10607862013-10-28 14:48:30 +010072 flush = 0;
Martin Schwidefsky6252d702008-02-09 18:24:37 +010073repeat:
Martin Schwidefsky043d0702011-05-23 10:24:23 +020074 table = crst_table_alloc(mm);
Martin Schwidefsky6252d702008-02-09 18:24:37 +010075 if (!table)
76 return -ENOMEM;
Martin Schwidefsky80217142010-10-25 16:10:11 +020077 spin_lock_bh(&mm->page_table_lock);
Martin Schwidefsky6252d702008-02-09 18:24:37 +010078 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 Schwidefskyf481bfa2009-03-18 13:27:36 +010096 mm->task_size = mm->context.asce_limit;
Martin Schwidefsky6252d702008-02-09 18:24:37 +010097 table = NULL;
Martin Schwidefsky10607862013-10-28 14:48:30 +010098 flush = 1;
Martin Schwidefsky6252d702008-02-09 18:24:37 +010099 }
Martin Schwidefsky80217142010-10-25 16:10:11 +0200100 spin_unlock_bh(&mm->page_table_lock);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100101 if (table)
102 crst_table_free(mm, table);
103 if (mm->context.asce_limit < limit)
104 goto repeat;
Martin Schwidefsky10607862013-10-28 14:48:30 +0100105 if (flush)
106 on_each_cpu(__crst_table_upgrade, mm, 0);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100107 return 0;
108}
109
110void crst_table_downgrade(struct mm_struct *mm, unsigned long limit)
111{
112 pgd_t *pgd;
113
Martin Schwidefsky02a8f3a2014-04-03 13:54:59 +0200114 if (current->active_mm == mm) {
Martin Schwidefskybeef5602014-04-14 15:11:26 +0200115 clear_user_asce();
Martin Schwidefsky10607862013-10-28 14:48:30 +0100116 __tlb_flush_mm(mm);
Martin Schwidefsky02a8f3a2014-04-03 13:54:59 +0200117 }
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100118 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 Schwidefskyf481bfa2009-03-18 13:27:36 +0100137 mm->task_size = mm->context.asce_limit;
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100138 crst_table_free(mm, (unsigned long *) pgd);
139 }
Martin Schwidefsky10607862013-10-28 14:48:30 +0100140 if (current->active_mm == mm)
Martin Schwidefskybeef5602014-04-14 15:11:26 +0200141 set_user_asce(mm);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100142}
143#endif
144
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200145#ifdef CONFIG_PGSTE
146
147/**
148 * gmap_alloc - allocate a guest address space
149 * @mm: pointer to the parent mm_struct
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200150 * @limit: maximum size of the gmap address space
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200151 *
152 * Returns a guest address space structure.
153 */
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200154struct gmap *gmap_alloc(struct mm_struct *mm, unsigned long limit)
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200155{
156 struct gmap *gmap;
157 struct page *page;
158 unsigned long *table;
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200159 unsigned long etype, atype;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200160
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200161 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 Schwidefskye5992f22011-07-24 10:48:20 +0200178 gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL);
179 if (!gmap)
180 goto out;
181 INIT_LIST_HEAD(&gmap->crst_list);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200182 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 Schwidefskye5992f22011-07-24 10:48:20 +0200185 gmap->mm = mm;
186 page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
187 if (!page)
188 goto out_free;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200189 page->index = 0;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200190 list_add(&page->lru, &gmap->crst_list);
191 table = (unsigned long *) page_to_phys(page);
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200192 crst_table_init(table, etype);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200193 gmap->table = table;
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200194 gmap->asce = atype | _ASCE_TABLE_LENGTH |
195 _ASCE_USER_BITS | __pa(table);
196 gmap->asce_end = limit;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200197 down_write(&mm->mmap_sem);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200198 list_add(&gmap->list, &mm->context.gmap_list);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200199 up_write(&mm->mmap_sem);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200200 return gmap;
201
202out_free:
203 kfree(gmap);
204out:
205 return NULL;
206}
207EXPORT_SYMBOL_GPL(gmap_alloc);
208
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200209static void gmap_flush_tlb(struct gmap *gmap)
210{
211 if (MACHINE_HAS_IDTE)
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200212 __tlb_flush_asce(gmap->mm, gmap->asce);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200213 else
214 __tlb_flush_global();
215}
216
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200217static 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 Schwidefskye5992f22011-07-24 10:48:20 +0200241/**
242 * gmap_free - free a guest address space
243 * @gmap: pointer to the guest address space structure
244 */
245void gmap_free(struct gmap *gmap)
246{
247 struct page *page, *next;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200248
249 /* Flush tlb. */
250 if (MACHINE_HAS_IDTE)
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200251 __tlb_flush_asce(gmap->mm, gmap->asce);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200252 else
253 __tlb_flush_global();
254
255 /* Free all segment & region tables. */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200256 list_for_each_entry_safe(page, next, &gmap->crst_list, lru)
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200257 __free_pages(page, ALLOC_ORDER);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200258 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 Schwidefskye5992f22011-07-24 10:48:20 +0200261 list_del(&gmap->list);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200262 up_write(&gmap->mm->mmap_sem);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200263 kfree(gmap);
264}
265EXPORT_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 */
271void gmap_enable(struct gmap *gmap)
272{
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200273 S390_lowcore.gmap = (unsigned long) gmap;
274}
275EXPORT_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 */
281void gmap_disable(struct gmap *gmap)
282{
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200283 S390_lowcore.gmap = 0UL;
284}
285EXPORT_SYMBOL_GPL(gmap_disable);
286
Carsten Ottea9162f22011-10-30 15:17:00 +0100287/*
288 * gmap_alloc_table is assumed to be called with mmap_sem held
289 */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200290static int gmap_alloc_table(struct gmap *gmap, unsigned long *table,
291 unsigned long init, unsigned long gaddr)
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200292{
293 struct page *page;
294 unsigned long *new;
295
Christian Borntraegerc86cce22011-12-27 11:25:47 +0100296 /* since we dont free the gmap table until gmap_free we can unlock */
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200297 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 Schwidefsky527e30b2014-04-30 16:04:25 +0200302 spin_lock(&gmap->mm->page_table_lock);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200303 if (*table & _REGION_ENTRY_INVALID) {
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200304 list_add(&page->lru, &gmap->crst_list);
305 *table = (unsigned long) new | _REGION_ENTRY_LENGTH |
306 (*table & _REGION_ENTRY_TYPE_MASK);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200307 page->index = gaddr;
308 page = NULL;
309 }
310 spin_unlock(&gmap->mm->page_table_lock);
311 if (page)
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200312 __free_pages(page, ALLOC_ORDER);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200313 return 0;
314}
315
316/**
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200317 * __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 */
322static 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 */
340static 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 */
362static 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 Schwidefskye5992f22011-07-24 10:48:20 +0200372 * gmap_unmap_segment - unmap segment from the guest address space
373 * @gmap: pointer to the guest address space structure
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200374 * @to: address in the guest address space
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200375 * @len: length of the memory area to unmap
376 *
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +0100377 * Returns 0 if the unmap succeeded, -EINVAL if not.
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200378 */
379int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len)
380{
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200381 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 Schwidefsky527e30b2014-04-30 16:04:25 +0200390 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 Schwidefskye5992f22011-07-24 10:48:20 +0200394 if (flush)
395 gmap_flush_tlb(gmap);
396 return 0;
397}
398EXPORT_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 Schwidefsky6e0a0432014-04-29 09:34:41 +0200405 * @len: length of the memory area to map
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200406 *
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +0100407 * Returns 0 if the mmap succeeded, -EINVAL or -ENOMEM if not.
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200408 */
409int gmap_map_segment(struct gmap *gmap, unsigned long from,
410 unsigned long to, unsigned long len)
411{
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200412 unsigned long off;
413 int flush;
414
415 if ((from | to | len) & (PMD_SIZE - 1))
416 return -EINVAL;
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200417 if (len == 0 || from + len < from || to + len < to ||
418 from + len > TASK_MAX_SIZE || to + len > gmap->asce_end)
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200419 return -EINVAL;
420
421 flush = 0;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200422 down_write(&gmap->mm->mmap_sem);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200423 for (off = 0; off < len; off += PMD_SIZE) {
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200424 /* 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 Schwidefskye5992f22011-07-24 10:48:20 +0200431 }
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200432 up_write(&gmap->mm->mmap_sem);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200433 if (flush)
434 gmap_flush_tlb(gmap);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200435 if (off >= len)
436 return 0;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200437 gmap_unmap_segment(gmap, to, len);
438 return -ENOMEM;
439}
440EXPORT_SYMBOL_GPL(gmap_map_segment);
441
Heiko Carstensc5034942012-09-10 16:14:33 +0200442/**
443 * __gmap_translate - translate a guest address to a user space address
Heiko Carstensc5034942012-09-10 16:14:33 +0200444 * @gmap: pointer to guest mapping meta data structure
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200445 * @gaddr: guest address
Heiko Carstensc5034942012-09-10 16:14:33 +0200446 *
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 Schwidefsky6e0a0432014-04-29 09:34:41 +0200453unsigned long __gmap_translate(struct gmap *gmap, unsigned long gaddr)
Heiko Carstensc5034942012-09-10 16:14:33 +0200454{
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200455 unsigned long vmaddr;
Heiko Carstensc5034942012-09-10 16:14:33 +0200456
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200457 vmaddr = (unsigned long)
458 radix_tree_lookup(&gmap->guest_to_host, gaddr >> PMD_SHIFT);
459 return vmaddr ? (vmaddr | (gaddr & ~PMD_MASK)) : -EFAULT;
Heiko Carstensc5034942012-09-10 16:14:33 +0200460}
461EXPORT_SYMBOL_GPL(__gmap_translate);
462
463/**
464 * gmap_translate - translate a guest address to a user space address
Heiko Carstensc5034942012-09-10 16:14:33 +0200465 * @gmap: pointer to guest mapping meta data structure
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200466 * @gaddr: guest address
Heiko Carstensc5034942012-09-10 16:14:33 +0200467 *
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 Schwidefsky6e0a0432014-04-29 09:34:41 +0200472unsigned long gmap_translate(struct gmap *gmap, unsigned long gaddr)
Heiko Carstensc5034942012-09-10 16:14:33 +0200473{
474 unsigned long rc;
475
476 down_read(&gmap->mm->mmap_sem);
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200477 rc = __gmap_translate(gmap, gaddr);
Heiko Carstensc5034942012-09-10 16:14:33 +0200478 up_read(&gmap->mm->mmap_sem);
479 return rc;
480}
481EXPORT_SYMBOL_GPL(gmap_translate);
482
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200483/**
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 */
489static void gmap_unlink(struct mm_struct *mm, unsigned long *table,
490 unsigned long vmaddr)
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200491{
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200492 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 */
513int __gmap_link(struct gmap *gmap, unsigned long gaddr, unsigned long vmaddr)
514{
Heiko Carstensc5034942012-09-10 16:14:33 +0200515 struct mm_struct *mm;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200516 unsigned long *table;
517 spinlock_t *ptl;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200518 pgd_t *pgd;
519 pud_t *pud;
520 pmd_t *pmd;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200521 int rc;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200522
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200523 /* Create higher level tables in the gmap page table */
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200524 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 Schwidefskyab8e5232013-04-16 13:37:46 +0200550 /* Walk the parent mm page table */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200551 mm = gmap->mm;
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200552 pgd = pgd_offset(mm, vmaddr);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200553 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 Thorlton1e1836e2014-04-07 15:37:09 -0700558 /* large pmds cannot yet be handled */
559 if (pmd_large(*pmd))
560 return -EFAULT;
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200561 /* Link gmap segment table entry location to page table. */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200562 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 Schwidefskyab8e5232013-04-16 13:37:46 +0200578}
579
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200580/**
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 Schwidefskyab8e5232013-04-16 13:37:46 +0200588 */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200589int gmap_fault(struct gmap *gmap, unsigned long gaddr,
590 unsigned int fault_flags)
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200591{
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200592 unsigned long vmaddr;
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200593 int rc;
594
Carsten Otte499069e2011-10-30 15:17:02 +0100595 down_read(&gmap->mm->mmap_sem);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200596 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);
606out_up:
Carsten Otte499069e2011-10-30 15:17:02 +0100607 up_read(&gmap->mm->mmap_sem);
Carsten Otte499069e2011-10-30 15:17:02 +0100608 return rc;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200609}
610EXPORT_SYMBOL_GPL(gmap_fault);
611
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200612static 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 Schwidefsky527e30b2014-04-30 16:04:25 +0200627/*
628 * this function is assumed to be called with mmap_sem held
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200629 */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200630void __gmap_zap(struct gmap *gmap, unsigned long gaddr)
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200631{
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200632 unsigned long vmaddr, ptev, pgstev;
633 pte_t *ptep, pte;
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200634 spinlock_t *ptl;
635 pgste_t pgste;
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200636
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200637 /* 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 Weitzb31288f2013-04-17 17:36:29 +0200645 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 Schwidefsky527e30b2014-04-30 16:04:25 +0200656 gmap_zap_swap_entry(pte_to_swp_entry(pte), gmap->mm);
657 pte_clear(gmap->mm, vmaddr, ptep);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200658 }
659 pgste_set_unlock(ptep, pgste);
660out_pte:
Dominik Dingel66e9bbd2014-10-06 16:34:44 +0200661 pte_unmap_unlock(ptep, ptl);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200662}
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200663EXPORT_SYMBOL_GPL(__gmap_zap);
664
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200665void gmap_discard(struct gmap *gmap, unsigned long from, unsigned long to)
Christian Borntraeger388186b2011-10-30 15:17:03 +0100666{
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200667 unsigned long gaddr, vmaddr, size;
Christian Borntraeger388186b2011-10-30 15:17:03 +0100668 struct vm_area_struct *vma;
Christian Borntraeger388186b2011-10-30 15:17:03 +0100669
670 down_read(&gmap->mm->mmap_sem);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200671 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 Borntraeger388186b2011-10-30 15:17:03 +0100678 continue;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200679 vmaddr |= gaddr & ~PMD_MASK;
680 /* Find vma in the parent mm */
681 vma = find_vma(gmap->mm, vmaddr);
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200682 size = min(to - gaddr, PMD_SIZE - (gaddr & ~PMD_MASK));
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200683 zap_page_range(vma, vmaddr, size, NULL);
Christian Borntraeger388186b2011-10-30 15:17:03 +0100684 }
685 up_read(&gmap->mm->mmap_sem);
686}
687EXPORT_SYMBOL_GPL(gmap_discard);
688
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200689static LIST_HEAD(gmap_notifier_list);
690static 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 */
696void 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}
702EXPORT_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 */
708void 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}
714EXPORT_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 Schwidefsky6e0a0432014-04-29 09:34:41 +0200719 * @gaddr: virtual address in the guest address space
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200720 * @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 Schwidefsky6e0a0432014-04-29 09:34:41 +0200727int gmap_ipte_notify(struct gmap *gmap, unsigned long gaddr, unsigned long len)
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200728{
729 unsigned long addr;
730 spinlock_t *ptl;
731 pte_t *ptep, entry;
732 pgste_t pgste;
733 int rc = 0;
734
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200735 if ((gaddr & ~PAGE_MASK) || (len & ~PAGE_MASK))
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200736 return -EINVAL;
737 down_read(&gmap->mm->mmap_sem);
738 while (len) {
739 /* Convert gmap address and connect the page tables */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200740 addr = __gmap_translate(gmap, gaddr);
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200741 if (IS_ERR_VALUE(addr)) {
742 rc = addr;
743 break;
744 }
745 /* Get the page mapped */
Christian Borntraegerbb4b42c2013-05-08 15:25:38 +0200746 if (fixup_user_fault(current, gmap->mm, addr, FAULT_FLAG_WRITE)) {
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200747 rc = -EFAULT;
748 break;
749 }
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200750 rc = __gmap_link(gmap, gaddr, addr);
751 if (rc)
752 break;
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200753 /* Walk the process page table, lock and get pte pointer */
754 ptep = get_locked_pte(gmap->mm, addr, &ptl);
Dominik Dingel6972cae2014-10-15 15:29:01 +0200755 VM_BUG_ON(!ptep);
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200756 /* Set notification bit in the pgste of the pte */
757 entry = *ptep;
Martin Schwidefskye5098612013-07-23 20:57:57 +0200758 if ((pte_val(entry) & (_PAGE_INVALID | _PAGE_PROTECT)) == 0) {
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200759 pgste = pgste_get_lock(ptep);
Martin Schwidefsky0d0dafc2013-05-17 14:41:33 +0200760 pgste_val(pgste) |= PGSTE_IN_BIT;
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200761 pgste_set_unlock(ptep, pgste);
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200762 gaddr += PAGE_SIZE;
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200763 len -= PAGE_SIZE;
764 }
Martin Schwidefskya697e052014-10-30 10:55:37 +0100765 pte_unmap_unlock(ptep, ptl);
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200766 }
767 up_read(&gmap->mm->mmap_sem);
768 return rc;
769}
770EXPORT_SYMBOL_GPL(gmap_ipte_notify);
771
772/**
773 * gmap_do_ipte_notify - call all invalidation callbacks for a specific pte.
774 * @mm: pointer to the process mm_struct
Martin Schwidefsky9da4e382014-04-30 14:46:26 +0200775 * @addr: virtual address in the process address space
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200776 * @pte: pointer to the page table entry
777 *
778 * This function is assumed to be called with the page table lock held
779 * for the pte to notify.
780 */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200781void gmap_do_ipte_notify(struct mm_struct *mm, unsigned long vmaddr, pte_t *pte)
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200782{
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200783 unsigned long offset, gaddr;
784 unsigned long *table;
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200785 struct gmap_notifier *nb;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200786 struct gmap *gmap;
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200787
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200788 offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
789 offset = offset * (4096 / sizeof(pte_t));
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200790 spin_lock(&gmap_notifier_lock);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200791 list_for_each_entry(gmap, &mm->context.gmap_list, list) {
792 table = radix_tree_lookup(&gmap->host_to_guest,
793 vmaddr >> PMD_SHIFT);
794 if (!table)
795 continue;
796 gaddr = __gmap_segment_gaddr(table) + offset;
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200797 list_for_each_entry(nb, &gmap_notifier_list, list)
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200798 nb->notifier_call(gmap, gaddr);
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200799 }
800 spin_unlock(&gmap_notifier_lock);
801}
Martin Schwidefsky0a61b222013-10-18 12:03:41 +0200802EXPORT_SYMBOL_GPL(gmap_do_ipte_notify);
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200803
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +0200804static inline int page_table_with_pgste(struct page *page)
805{
806 return atomic_read(&page->_mapcount) == 0;
807}
808
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200809static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm)
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200810{
811 struct page *page;
812 unsigned long *table;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200813
814 page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
815 if (!page)
816 return NULL;
Kirill A. Shutemove89cfa52013-11-14 14:31:39 -0800817 if (!pgtable_page_ctor(page)) {
Kirill A. Shutemove89cfa52013-11-14 14:31:39 -0800818 __free_page(page);
819 return NULL;
820 }
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +0200821 atomic_set(&page->_mapcount, 0);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200822 table = (unsigned long *) page_to_phys(page);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200823 clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
Martin Schwidefsky0a61b222013-10-18 12:03:41 +0200824 clear_table(table + PTRS_PER_PTE, 0, PAGE_SIZE/2);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200825 return table;
826}
827
828static inline void page_table_free_pgste(unsigned long *table)
829{
830 struct page *page;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200831
832 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
Martin Schwidefsky2320c572012-02-17 10:29:21 +0100833 pgtable_page_dtor(page);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200834 atomic_set(&page->_mapcount, -1);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200835 __free_page(page);
836}
837
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200838int set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
839 unsigned long key, bool nq)
840{
841 spinlock_t *ptl;
842 pgste_t old, new;
843 pte_t *ptep;
844
845 down_read(&mm->mmap_sem);
Christian Borntraegerab3f2852014-08-19 16:19:35 +0200846retry:
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200847 ptep = get_locked_pte(current->mm, addr, &ptl);
848 if (unlikely(!ptep)) {
849 up_read(&mm->mmap_sem);
850 return -EFAULT;
851 }
Christian Borntraegerab3f2852014-08-19 16:19:35 +0200852 if (!(pte_val(*ptep) & _PAGE_INVALID) &&
853 (pte_val(*ptep) & _PAGE_PROTECT)) {
Dominik Dingel66e9bbd2014-10-06 16:34:44 +0200854 pte_unmap_unlock(ptep, ptl);
Christian Borntraegerdc77d342014-08-27 12:20:02 +0200855 if (fixup_user_fault(current, mm, addr, FAULT_FLAG_WRITE)) {
856 up_read(&mm->mmap_sem);
857 return -EFAULT;
Christian Borntraegerab3f2852014-08-19 16:19:35 +0200858 }
Christian Borntraegerdc77d342014-08-27 12:20:02 +0200859 goto retry;
860 }
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200861
862 new = old = pgste_get_lock(ptep);
863 pgste_val(new) &= ~(PGSTE_GR_BIT | PGSTE_GC_BIT |
864 PGSTE_ACC_BITS | PGSTE_FP_BIT);
865 pgste_val(new) |= (key & (_PAGE_CHANGED | _PAGE_REFERENCED)) << 48;
866 pgste_val(new) |= (key & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
867 if (!(pte_val(*ptep) & _PAGE_INVALID)) {
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200868 unsigned long address, bits, skey;
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200869
870 address = pte_val(*ptep) & PAGE_MASK;
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200871 skey = (unsigned long) page_get_storage_key(address);
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200872 bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200873 skey = key & (_PAGE_ACC_BITS | _PAGE_FP_BIT);
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200874 /* Set storage key ACC and FP */
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200875 page_set_storage_key(address, skey, !nq);
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200876 /* Merge host changed & referenced into pgste */
877 pgste_val(new) |= bits << 52;
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200878 }
879 /* changing the guest storage key is considered a change of the page */
880 if ((pgste_val(new) ^ pgste_val(old)) &
881 (PGSTE_ACC_BITS | PGSTE_FP_BIT | PGSTE_GR_BIT | PGSTE_GC_BIT))
Martin Schwidefsky0a61b222013-10-18 12:03:41 +0200882 pgste_val(new) |= PGSTE_UC_BIT;
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200883
884 pgste_set_unlock(ptep, new);
Dominik Dingel66e9bbd2014-10-06 16:34:44 +0200885 pte_unmap_unlock(ptep, ptl);
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200886 up_read(&mm->mmap_sem);
887 return 0;
888}
889EXPORT_SYMBOL(set_guest_storage_key);
890
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200891#else /* CONFIG_PGSTE */
892
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +0200893static inline int page_table_with_pgste(struct page *page)
894{
895 return 0;
896}
897
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200898static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm)
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200899{
Jan Glauber944291d2011-08-03 16:44:18 +0200900 return NULL;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200901}
902
903static inline void page_table_free_pgste(unsigned long *table)
904{
905}
906
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200907static inline void gmap_unlink(struct mm_struct *mm, unsigned long *table,
908 unsigned long vmaddr)
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200909{
910}
911
912#endif /* CONFIG_PGSTE */
913
Martin Schwidefsky36409f62011-06-06 14:14:41 +0200914static inline unsigned int atomic_xor_bits(atomic_t *v, unsigned int bits)
915{
916 unsigned int old, new;
917
918 do {
919 old = atomic_read(v);
920 new = old ^ bits;
921 } while (atomic_cmpxchg(v, old, new) != old);
922 return new;
923}
924
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200925/*
926 * page table entry allocation/free routines.
927 */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200928unsigned long *page_table_alloc(struct mm_struct *mm)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200929{
Heiko Carstens41459d32012-09-14 11:09:52 +0200930 unsigned long *uninitialized_var(table);
931 struct page *uninitialized_var(page);
Martin Schwidefsky36409f62011-06-06 14:14:41 +0200932 unsigned int mask, bit;
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200933
Martin Schwidefsky36409f62011-06-06 14:14:41 +0200934 if (mm_has_pgste(mm))
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200935 return page_table_alloc_pgste(mm);
Martin Schwidefsky36409f62011-06-06 14:14:41 +0200936 /* Allocate fragments of a 4K page as 1K/2K page table */
Martin Schwidefsky80217142010-10-25 16:10:11 +0200937 spin_lock_bh(&mm->context.list_lock);
Martin Schwidefsky36409f62011-06-06 14:14:41 +0200938 mask = FRAG_MASK;
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100939 if (!list_empty(&mm->context.pgtable_list)) {
940 page = list_first_entry(&mm->context.pgtable_list,
941 struct page, lru);
Martin Schwidefsky36409f62011-06-06 14:14:41 +0200942 table = (unsigned long *) page_to_phys(page);
943 mask = atomic_read(&page->_mapcount);
944 mask = mask | (mask >> 4);
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200945 }
Martin Schwidefsky36409f62011-06-06 14:14:41 +0200946 if ((mask & FRAG_MASK) == FRAG_MASK) {
Martin Schwidefsky80217142010-10-25 16:10:11 +0200947 spin_unlock_bh(&mm->context.list_lock);
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100948 page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
949 if (!page)
950 return NULL;
Kirill A. Shutemove89cfa52013-11-14 14:31:39 -0800951 if (!pgtable_page_ctor(page)) {
952 __free_page(page);
953 return NULL;
954 }
Martin Schwidefsky36409f62011-06-06 14:14:41 +0200955 atomic_set(&page->_mapcount, 1);
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100956 table = (unsigned long *) page_to_phys(page);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200957 clear_table(table, _PAGE_INVALID, PAGE_SIZE);
Martin Schwidefsky80217142010-10-25 16:10:11 +0200958 spin_lock_bh(&mm->context.list_lock);
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100959 list_add(&page->lru, &mm->context.pgtable_list);
Martin Schwidefsky36409f62011-06-06 14:14:41 +0200960 } else {
961 for (bit = 1; mask & bit; bit <<= 1)
962 table += PTRS_PER_PTE;
963 mask = atomic_xor_bits(&page->_mapcount, bit);
964 if ((mask & FRAG_MASK) == FRAG_MASK)
965 list_del(&page->lru);
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100966 }
Martin Schwidefsky80217142010-10-25 16:10:11 +0200967 spin_unlock_bh(&mm->context.list_lock);
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200968 return table;
969}
970
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100971void page_table_free(struct mm_struct *mm, unsigned long *table)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200972{
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100973 struct page *page;
Martin Schwidefsky36409f62011-06-06 14:14:41 +0200974 unsigned int bit, mask;
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200975
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +0200976 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200977 if (page_table_with_pgste(page))
Martin Schwidefsky36409f62011-06-06 14:14:41 +0200978 return page_table_free_pgste(table);
Martin Schwidefsky36409f62011-06-06 14:14:41 +0200979 /* Free 1K/2K page table fragment of a 4K page */
Martin Schwidefsky36409f62011-06-06 14:14:41 +0200980 bit = 1 << ((__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t)));
Martin Schwidefsky80217142010-10-25 16:10:11 +0200981 spin_lock_bh(&mm->context.list_lock);
Martin Schwidefsky36409f62011-06-06 14:14:41 +0200982 if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK)
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100983 list_del(&page->lru);
Martin Schwidefsky36409f62011-06-06 14:14:41 +0200984 mask = atomic_xor_bits(&page->_mapcount, bit);
985 if (mask & FRAG_MASK)
986 list_add(&page->lru, &mm->context.pgtable_list);
Martin Schwidefsky80217142010-10-25 16:10:11 +0200987 spin_unlock_bh(&mm->context.list_lock);
Martin Schwidefsky36409f62011-06-06 14:14:41 +0200988 if (mask == 0) {
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100989 pgtable_page_dtor(page);
Martin Schwidefsky36409f62011-06-06 14:14:41 +0200990 atomic_set(&page->_mapcount, -1);
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100991 __free_page(page);
992 }
993}
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200994
Martin Schwidefsky36409f62011-06-06 14:14:41 +0200995static void __page_table_free_rcu(void *table, unsigned bit)
996{
997 struct page *page;
998
Martin Schwidefsky36409f62011-06-06 14:14:41 +0200999 if (bit == FRAG_MASK)
1000 return page_table_free_pgste(table);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001001 /* Free 1K/2K page table fragment of a 4K page */
1002 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1003 if (atomic_xor_bits(&page->_mapcount, bit) == 0) {
1004 pgtable_page_dtor(page);
1005 atomic_set(&page->_mapcount, -1);
1006 __free_page(page);
Martin Schwidefsky80217142010-10-25 16:10:11 +02001007 }
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001008}
1009
Martin Schwidefsky527e30b2014-04-30 16:04:25 +02001010void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table,
1011 unsigned long vmaddr)
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001012{
1013 struct mm_struct *mm;
1014 struct page *page;
1015 unsigned int bit, mask;
1016
1017 mm = tlb->mm;
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001018 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1019 if (page_table_with_pgste(page)) {
Martin Schwidefsky527e30b2014-04-30 16:04:25 +02001020 gmap_unlink(mm, table, vmaddr);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001021 table = (unsigned long *) (__pa(table) | FRAG_MASK);
1022 tlb_remove_table(tlb, table);
1023 return;
Martin Schwidefsky80217142010-10-25 16:10:11 +02001024 }
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001025 bit = 1 << ((__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t)));
Martin Schwidefsky80217142010-10-25 16:10:11 +02001026 spin_lock_bh(&mm->context.list_lock);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001027 if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK)
1028 list_del(&page->lru);
1029 mask = atomic_xor_bits(&page->_mapcount, bit | (bit << 4));
1030 if (mask & FRAG_MASK)
1031 list_add_tail(&page->lru, &mm->context.pgtable_list);
Martin Schwidefsky80217142010-10-25 16:10:11 +02001032 spin_unlock_bh(&mm->context.list_lock);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001033 table = (unsigned long *) (__pa(table) | (bit << 4));
1034 tlb_remove_table(tlb, table);
Martin Schwidefsky80217142010-10-25 16:10:11 +02001035}
1036
Heiko Carstens63df41d62013-09-06 19:10:48 +02001037static void __tlb_remove_table(void *_table)
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001038{
Martin Schwidefskye73b7ff2011-10-30 15:16:08 +01001039 const unsigned long mask = (FRAG_MASK << 4) | FRAG_MASK;
1040 void *table = (void *)((unsigned long) _table & ~mask);
1041 unsigned type = (unsigned long) _table & mask;
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001042
1043 if (type)
1044 __page_table_free_rcu(table, type);
1045 else
1046 free_pages((unsigned long) table, ALLOC_ORDER);
1047}
1048
Martin Schwidefskycd94154cc2012-04-11 14:28:07 +02001049static void tlb_remove_table_smp_sync(void *arg)
1050{
1051 /* Simply deliver the interrupt */
1052}
1053
1054static void tlb_remove_table_one(void *table)
1055{
1056 /*
1057 * This isn't an RCU grace period and hence the page-tables cannot be
1058 * assumed to be actually RCU-freed.
1059 *
1060 * It is however sufficient for software page-table walkers that rely
1061 * on IRQ disabling. See the comment near struct mmu_table_batch.
1062 */
1063 smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
1064 __tlb_remove_table(table);
1065}
1066
1067static void tlb_remove_table_rcu(struct rcu_head *head)
1068{
1069 struct mmu_table_batch *batch;
1070 int i;
1071
1072 batch = container_of(head, struct mmu_table_batch, rcu);
1073
1074 for (i = 0; i < batch->nr; i++)
1075 __tlb_remove_table(batch->tables[i]);
1076
1077 free_page((unsigned long)batch);
1078}
1079
1080void tlb_table_flush(struct mmu_gather *tlb)
1081{
1082 struct mmu_table_batch **batch = &tlb->batch;
1083
1084 if (*batch) {
Martin Schwidefskycd94154cc2012-04-11 14:28:07 +02001085 call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
1086 *batch = NULL;
1087 }
1088}
1089
1090void tlb_remove_table(struct mmu_gather *tlb, void *table)
1091{
1092 struct mmu_table_batch **batch = &tlb->batch;
1093
Martin Schwidefsky5c474a12013-08-16 13:31:40 +02001094 tlb->mm->context.flush_mm = 1;
Martin Schwidefskycd94154cc2012-04-11 14:28:07 +02001095 if (*batch == NULL) {
1096 *batch = (struct mmu_table_batch *)
1097 __get_free_page(GFP_NOWAIT | __GFP_NOWARN);
1098 if (*batch == NULL) {
Martin Schwidefsky5c474a12013-08-16 13:31:40 +02001099 __tlb_flush_mm_lazy(tlb->mm);
Martin Schwidefskycd94154cc2012-04-11 14:28:07 +02001100 tlb_remove_table_one(table);
1101 return;
1102 }
1103 (*batch)->nr = 0;
1104 }
1105 (*batch)->tables[(*batch)->nr++] = table;
1106 if ((*batch)->nr == MAX_TABLE_BATCH)
Martin Schwidefsky5c474a12013-08-16 13:31:40 +02001107 tlb_flush_mmu(tlb);
Martin Schwidefskycd94154cc2012-04-11 14:28:07 +02001108}
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001109
Gerald Schaefer274023d2012-10-08 16:30:21 -07001110#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001111static inline void thp_split_vma(struct vm_area_struct *vma)
Gerald Schaefer274023d2012-10-08 16:30:21 -07001112{
1113 unsigned long addr;
Gerald Schaefer274023d2012-10-08 16:30:21 -07001114
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001115 for (addr = vma->vm_start; addr < vma->vm_end; addr += PAGE_SIZE)
1116 follow_page(vma, addr, FOLL_SPLIT);
Gerald Schaefer274023d2012-10-08 16:30:21 -07001117}
1118
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001119static inline void thp_split_mm(struct mm_struct *mm)
Gerald Schaefer274023d2012-10-08 16:30:21 -07001120{
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001121 struct vm_area_struct *vma;
Gerald Schaefer274023d2012-10-08 16:30:21 -07001122
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001123 for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) {
Gerald Schaefer274023d2012-10-08 16:30:21 -07001124 thp_split_vma(vma);
1125 vma->vm_flags &= ~VM_HUGEPAGE;
1126 vma->vm_flags |= VM_NOHUGEPAGE;
Gerald Schaefer274023d2012-10-08 16:30:21 -07001127 }
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001128 mm->def_flags |= VM_NOHUGEPAGE;
1129}
1130#else
1131static inline void thp_split_mm(struct mm_struct *mm)
1132{
Gerald Schaefer274023d2012-10-08 16:30:21 -07001133}
1134#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1135
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001136static unsigned long page_table_realloc_pmd(struct mmu_gather *tlb,
1137 struct mm_struct *mm, pud_t *pud,
1138 unsigned long addr, unsigned long end)
1139{
1140 unsigned long next, *table, *new;
1141 struct page *page;
Christian Borntraeger55e42832014-07-25 14:23:29 +02001142 spinlock_t *ptl;
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001143 pmd_t *pmd;
1144
1145 pmd = pmd_offset(pud, addr);
1146 do {
1147 next = pmd_addr_end(addr, end);
1148again:
1149 if (pmd_none_or_clear_bad(pmd))
1150 continue;
1151 table = (unsigned long *) pmd_deref(*pmd);
1152 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1153 if (page_table_with_pgste(page))
1154 continue;
1155 /* Allocate new page table with pgstes */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +02001156 new = page_table_alloc_pgste(mm);
Dominik Dingelbe39f192013-10-31 10:01:16 +01001157 if (!new)
1158 return -ENOMEM;
1159
Christian Borntraeger55e42832014-07-25 14:23:29 +02001160 ptl = pmd_lock(mm, pmd);
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001161 if (likely((unsigned long *) pmd_deref(*pmd) == table)) {
1162 /* Nuke pmd entry pointing to the "short" page table */
1163 pmdp_flush_lazy(mm, addr, pmd);
1164 pmd_clear(pmd);
1165 /* Copy ptes from old table to new table */
1166 memcpy(new, table, PAGE_SIZE/2);
1167 clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
1168 /* Establish new table */
1169 pmd_populate(mm, pmd, (pte_t *) new);
1170 /* Free old table with rcu, there might be a walker! */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +02001171 page_table_free_rcu(tlb, table, addr);
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001172 new = NULL;
1173 }
Christian Borntraeger55e42832014-07-25 14:23:29 +02001174 spin_unlock(ptl);
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001175 if (new) {
1176 page_table_free_pgste(new);
1177 goto again;
1178 }
1179 } while (pmd++, addr = next, addr != end);
1180
1181 return addr;
1182}
1183
1184static unsigned long page_table_realloc_pud(struct mmu_gather *tlb,
1185 struct mm_struct *mm, pgd_t *pgd,
1186 unsigned long addr, unsigned long end)
1187{
1188 unsigned long next;
1189 pud_t *pud;
1190
1191 pud = pud_offset(pgd, addr);
1192 do {
1193 next = pud_addr_end(addr, end);
1194 if (pud_none_or_clear_bad(pud))
1195 continue;
1196 next = page_table_realloc_pmd(tlb, mm, pud, addr, next);
Dominik Dingelbe39f192013-10-31 10:01:16 +01001197 if (unlikely(IS_ERR_VALUE(next)))
1198 return next;
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001199 } while (pud++, addr = next, addr != end);
1200
1201 return addr;
1202}
1203
Dominik Dingelbe39f192013-10-31 10:01:16 +01001204static unsigned long page_table_realloc(struct mmu_gather *tlb, struct mm_struct *mm,
1205 unsigned long addr, unsigned long end)
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001206{
1207 unsigned long next;
1208 pgd_t *pgd;
1209
1210 pgd = pgd_offset(mm, addr);
1211 do {
1212 next = pgd_addr_end(addr, end);
1213 if (pgd_none_or_clear_bad(pgd))
1214 continue;
1215 next = page_table_realloc_pud(tlb, mm, pgd, addr, next);
Dominik Dingelbe39f192013-10-31 10:01:16 +01001216 if (unlikely(IS_ERR_VALUE(next)))
1217 return next;
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001218 } while (pgd++, addr = next, addr != end);
Dominik Dingelbe39f192013-10-31 10:01:16 +01001219
1220 return 0;
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001221}
1222
Carsten Otte402b0862008-03-25 18:47:10 +01001223/*
1224 * switch on pgstes for its userspace process (for kvm)
1225 */
1226int s390_enable_sie(void)
1227{
1228 struct task_struct *tsk = current;
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001229 struct mm_struct *mm = tsk->mm;
1230 struct mmu_gather tlb;
Carsten Otte402b0862008-03-25 18:47:10 +01001231
Christian Borntraeger74b6b522008-05-21 13:37:29 +02001232 /* Do we have pgstes? if yes, we are done */
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001233 if (mm_has_pgste(tsk->mm))
Christian Borntraeger74b6b522008-05-21 13:37:29 +02001234 return 0;
Carsten Otte402b0862008-03-25 18:47:10 +01001235
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001236 down_write(&mm->mmap_sem);
Gerald Schaefer274023d2012-10-08 16:30:21 -07001237 /* split thp mappings and disable thp for future mappings */
1238 thp_split_mm(mm);
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001239 /* Reallocate the page tables with pgstes */
Linus Torvaldsae7a8352013-09-04 18:15:06 -07001240 tlb_gather_mmu(&tlb, mm, 0, TASK_SIZE);
Dominik Dingelbe39f192013-10-31 10:01:16 +01001241 if (!page_table_realloc(&tlb, mm, 0, TASK_SIZE))
1242 mm->context.has_pgste = 1;
Linus Torvaldsae7a8352013-09-04 18:15:06 -07001243 tlb_finish_mmu(&tlb, 0, TASK_SIZE);
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001244 up_write(&mm->mmap_sem);
1245 return mm->context.has_pgste ? 0 : -ENOMEM;
Carsten Otte402b0862008-03-25 18:47:10 +01001246}
1247EXPORT_SYMBOL_GPL(s390_enable_sie);
Hans-Joachim Picht7db11a32009-06-16 10:30:26 +02001248
Dominik Dingel934bc132014-01-14 18:10:17 +01001249/*
1250 * Enable storage key handling from now on and initialize the storage
1251 * keys with the default key.
1252 */
Dominik Dingela13cff32014-10-23 12:07:14 +02001253static int __s390_enable_skey(pte_t *pte, unsigned long addr,
1254 unsigned long next, struct mm_walk *walk)
1255{
1256 unsigned long ptev;
1257 pgste_t pgste;
1258
1259 pgste = pgste_get_lock(pte);
Dominik Dingel2faee8f2014-10-23 12:08:38 +02001260 /*
1261 * Remove all zero page mappings,
1262 * after establishing a policy to forbid zero page mappings
1263 * following faults for that page will get fresh anonymous pages
1264 */
1265 if (is_zero_pfn(pte_pfn(*pte))) {
1266 ptep_flush_direct(walk->mm, addr, pte);
1267 pte_val(*pte) = _PAGE_INVALID;
1268 }
Dominik Dingela13cff32014-10-23 12:07:14 +02001269 /* Clear storage key */
1270 pgste_val(pgste) &= ~(PGSTE_ACC_BITS | PGSTE_FP_BIT |
1271 PGSTE_GR_BIT | PGSTE_GC_BIT);
1272 ptev = pte_val(*pte);
1273 if (!(ptev & _PAGE_INVALID) && (ptev & _PAGE_WRITE))
1274 page_set_storage_key(ptev & PAGE_MASK, PAGE_DEFAULT_KEY, 1);
1275 pgste_set_unlock(pte, pgste);
1276 return 0;
1277}
1278
Dominik Dingel3ac8e382014-10-23 12:09:17 +02001279int s390_enable_skey(void)
Dominik Dingel934bc132014-01-14 18:10:17 +01001280{
Dominik Dingela13cff32014-10-23 12:07:14 +02001281 struct mm_walk walk = { .pte_entry = __s390_enable_skey };
1282 struct mm_struct *mm = current->mm;
Dominik Dingel3ac8e382014-10-23 12:09:17 +02001283 struct vm_area_struct *vma;
1284 int rc = 0;
Dominik Dingela13cff32014-10-23 12:07:14 +02001285
1286 down_write(&mm->mmap_sem);
1287 if (mm_use_skey(mm))
1288 goto out_up;
Dominik Dingel2faee8f2014-10-23 12:08:38 +02001289
1290 mm->context.use_skey = 1;
Dominik Dingel3ac8e382014-10-23 12:09:17 +02001291 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1292 if (ksm_madvise(vma, vma->vm_start, vma->vm_end,
1293 MADV_UNMERGEABLE, &vma->vm_flags)) {
1294 mm->context.use_skey = 0;
1295 rc = -ENOMEM;
1296 goto out_up;
1297 }
1298 }
1299 mm->def_flags &= ~VM_MERGEABLE;
Dominik Dingel2faee8f2014-10-23 12:08:38 +02001300
Dominik Dingela13cff32014-10-23 12:07:14 +02001301 walk.mm = mm;
1302 walk_page_range(0, TASK_SIZE, &walk);
Dominik Dingela13cff32014-10-23 12:07:14 +02001303
1304out_up:
1305 up_write(&mm->mmap_sem);
Dominik Dingel3ac8e382014-10-23 12:09:17 +02001306 return rc;
Dominik Dingel934bc132014-01-14 18:10:17 +01001307}
1308EXPORT_SYMBOL_GPL(s390_enable_skey);
1309
Dominik Dingela0bf4f12014-03-24 14:27:58 +01001310/*
Dominik Dingela13cff32014-10-23 12:07:14 +02001311 * Reset CMMA state, make all pages stable again.
1312 */
1313static int __s390_reset_cmma(pte_t *pte, unsigned long addr,
1314 unsigned long next, struct mm_walk *walk)
1315{
1316 pgste_t pgste;
1317
1318 pgste = pgste_get_lock(pte);
1319 pgste_val(pgste) &= ~_PGSTE_GPS_USAGE_MASK;
1320 pgste_set_unlock(pte, pgste);
1321 return 0;
1322}
1323
1324void s390_reset_cmma(struct mm_struct *mm)
1325{
1326 struct mm_walk walk = { .pte_entry = __s390_reset_cmma };
1327
1328 down_write(&mm->mmap_sem);
1329 walk.mm = mm;
1330 walk_page_range(0, TASK_SIZE, &walk);
1331 up_write(&mm->mmap_sem);
1332}
1333EXPORT_SYMBOL_GPL(s390_reset_cmma);
1334
1335/*
Dominik Dingela0bf4f12014-03-24 14:27:58 +01001336 * Test and reset if a guest page is dirty
1337 */
1338bool gmap_test_and_clear_dirty(unsigned long address, struct gmap *gmap)
1339{
1340 pte_t *pte;
1341 spinlock_t *ptl;
1342 bool dirty = false;
1343
1344 pte = get_locked_pte(gmap->mm, address, &ptl);
1345 if (unlikely(!pte))
1346 return false;
1347
1348 if (ptep_test_and_clear_user_dirty(gmap->mm, address, pte))
1349 dirty = true;
1350
1351 spin_unlock(ptl);
1352 return dirty;
1353}
1354EXPORT_SYMBOL_GPL(gmap_test_and_clear_dirty);
1355
Gerald Schaefer75077af2012-10-08 16:30:15 -07001356#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001357int pmdp_clear_flush_young(struct vm_area_struct *vma, unsigned long address,
1358 pmd_t *pmdp)
1359{
1360 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1361 /* No need to flush TLB
1362 * On s390 reference bits are in storage key and never in TLB */
1363 return pmdp_test_and_clear_young(vma, address, pmdp);
1364}
1365
1366int pmdp_set_access_flags(struct vm_area_struct *vma,
1367 unsigned long address, pmd_t *pmdp,
1368 pmd_t entry, int dirty)
1369{
1370 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1371
Martin Schwidefsky152125b2014-07-24 11:03:41 +02001372 entry = pmd_mkyoung(entry);
1373 if (dirty)
1374 entry = pmd_mkdirty(entry);
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001375 if (pmd_same(*pmdp, entry))
1376 return 0;
1377 pmdp_invalidate(vma, address, pmdp);
1378 set_pmd_at(vma->vm_mm, address, pmdp, entry);
1379 return 1;
1380}
1381
Gerald Schaefer75077af2012-10-08 16:30:15 -07001382static void pmdp_splitting_flush_sync(void *arg)
1383{
1384 /* Simply deliver the interrupt */
1385}
1386
1387void pmdp_splitting_flush(struct vm_area_struct *vma, unsigned long address,
1388 pmd_t *pmdp)
1389{
1390 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1391 if (!test_and_set_bit(_SEGMENT_ENTRY_SPLIT_BIT,
1392 (unsigned long *) pmdp)) {
1393 /* need to serialize against gup-fast (IRQ disabled) */
1394 smp_call_function(pmdp_splitting_flush_sync, NULL, 1);
1395 }
1396}
Gerald Schaefer9501d092012-10-08 16:30:18 -07001397
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001398void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
1399 pgtable_t pgtable)
Gerald Schaefer9501d092012-10-08 16:30:18 -07001400{
1401 struct list_head *lh = (struct list_head *) pgtable;
1402
Martin Schwidefskyec66ad62014-02-12 14:16:18 +01001403 assert_spin_locked(pmd_lockptr(mm, pmdp));
Gerald Schaefer9501d092012-10-08 16:30:18 -07001404
1405 /* FIFO */
Kirill A. Shutemovc389a252013-11-14 14:30:59 -08001406 if (!pmd_huge_pte(mm, pmdp))
Gerald Schaefer9501d092012-10-08 16:30:18 -07001407 INIT_LIST_HEAD(lh);
1408 else
Kirill A. Shutemovc389a252013-11-14 14:30:59 -08001409 list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
1410 pmd_huge_pte(mm, pmdp) = pgtable;
Gerald Schaefer9501d092012-10-08 16:30:18 -07001411}
1412
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001413pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
Gerald Schaefer9501d092012-10-08 16:30:18 -07001414{
1415 struct list_head *lh;
1416 pgtable_t pgtable;
1417 pte_t *ptep;
1418
Martin Schwidefskyec66ad62014-02-12 14:16:18 +01001419 assert_spin_locked(pmd_lockptr(mm, pmdp));
Gerald Schaefer9501d092012-10-08 16:30:18 -07001420
1421 /* FIFO */
Kirill A. Shutemovc389a252013-11-14 14:30:59 -08001422 pgtable = pmd_huge_pte(mm, pmdp);
Gerald Schaefer9501d092012-10-08 16:30:18 -07001423 lh = (struct list_head *) pgtable;
1424 if (list_empty(lh))
Kirill A. Shutemovc389a252013-11-14 14:30:59 -08001425 pmd_huge_pte(mm, pmdp) = NULL;
Gerald Schaefer9501d092012-10-08 16:30:18 -07001426 else {
Kirill A. Shutemovc389a252013-11-14 14:30:59 -08001427 pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
Gerald Schaefer9501d092012-10-08 16:30:18 -07001428 list_del(lh);
1429 }
1430 ptep = (pte_t *) pgtable;
Martin Schwidefskye5098612013-07-23 20:57:57 +02001431 pte_val(*ptep) = _PAGE_INVALID;
Gerald Schaefer9501d092012-10-08 16:30:18 -07001432 ptep++;
Martin Schwidefskye5098612013-07-23 20:57:57 +02001433 pte_val(*ptep) = _PAGE_INVALID;
Gerald Schaefer9501d092012-10-08 16:30:18 -07001434 return pgtable;
1435}
Gerald Schaefer75077af2012-10-08 16:30:15 -07001436#endif /* CONFIG_TRANSPARENT_HUGEPAGE */