blob: b33f66110ca9401418f7d657b951d8bfb84d99c3 [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>
Martin Schwidefsky0b46e0a2015-04-15 13:23:26 +020021#include <linux/sysctl.h>
Dominik Dingel3ac8e382014-10-23 12:09:17 +020022#include <linux/ksm.h>
23#include <linux/mman.h>
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020024
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020025#include <asm/pgtable.h>
26#include <asm/pgalloc.h>
27#include <asm/tlb.h>
28#include <asm/tlbflush.h>
Martin Schwidefsky6252d702008-02-09 18:24:37 +010029#include <asm/mmu_context.h>
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020030
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020031#define ALLOC_ORDER 2
Martin Schwidefsky36409f62011-06-06 14:14:41 +020032#define FRAG_MASK 0x03
Heiko Carstens239a64252009-06-12 10:26:33 +020033
Martin Schwidefsky043d0702011-05-23 10:24:23 +020034unsigned long *crst_table_alloc(struct mm_struct *mm)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020035{
36 struct page *page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
37
38 if (!page)
39 return NULL;
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020040 return (unsigned long *) page_to_phys(page);
41}
42
Martin Schwidefsky146e4b32008-02-09 18:24:35 +010043void crst_table_free(struct mm_struct *mm, unsigned long *table)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020044{
Martin Schwidefsky043d0702011-05-23 10:24:23 +020045 free_pages((unsigned long) table, ALLOC_ORDER);
Martin Schwidefsky80217142010-10-25 16:10:11 +020046}
47
Martin Schwidefsky10607862013-10-28 14:48:30 +010048static void __crst_table_upgrade(void *arg)
49{
50 struct mm_struct *mm = arg;
51
Martin Schwidefskybeef5602014-04-14 15:11:26 +020052 if (current->active_mm == mm) {
53 clear_user_asce();
54 set_user_asce(mm);
55 }
Martin Schwidefsky10607862013-10-28 14:48:30 +010056 __tlb_flush_local();
57}
58
Martin Schwidefsky6252d702008-02-09 18:24:37 +010059int crst_table_upgrade(struct mm_struct *mm, unsigned long limit)
60{
61 unsigned long *table, *pgd;
62 unsigned long entry;
Martin Schwidefsky10607862013-10-28 14:48:30 +010063 int flush;
Martin Schwidefsky6252d702008-02-09 18:24:37 +010064
65 BUG_ON(limit > (1UL << 53));
Martin Schwidefsky10607862013-10-28 14:48:30 +010066 flush = 0;
Martin Schwidefsky6252d702008-02-09 18:24:37 +010067repeat:
Martin Schwidefsky043d0702011-05-23 10:24:23 +020068 table = crst_table_alloc(mm);
Martin Schwidefsky6252d702008-02-09 18:24:37 +010069 if (!table)
70 return -ENOMEM;
Martin Schwidefsky80217142010-10-25 16:10:11 +020071 spin_lock_bh(&mm->page_table_lock);
Martin Schwidefsky6252d702008-02-09 18:24:37 +010072 if (mm->context.asce_limit < limit) {
73 pgd = (unsigned long *) mm->pgd;
74 if (mm->context.asce_limit <= (1UL << 31)) {
75 entry = _REGION3_ENTRY_EMPTY;
76 mm->context.asce_limit = 1UL << 42;
77 mm->context.asce_bits = _ASCE_TABLE_LENGTH |
78 _ASCE_USER_BITS |
79 _ASCE_TYPE_REGION3;
80 } else {
81 entry = _REGION2_ENTRY_EMPTY;
82 mm->context.asce_limit = 1UL << 53;
83 mm->context.asce_bits = _ASCE_TABLE_LENGTH |
84 _ASCE_USER_BITS |
85 _ASCE_TYPE_REGION2;
86 }
87 crst_table_init(table, entry);
88 pgd_populate(mm, (pgd_t *) table, (pud_t *) pgd);
89 mm->pgd = (pgd_t *) table;
Martin Schwidefskyf481bfa2009-03-18 13:27:36 +010090 mm->task_size = mm->context.asce_limit;
Martin Schwidefsky6252d702008-02-09 18:24:37 +010091 table = NULL;
Martin Schwidefsky10607862013-10-28 14:48:30 +010092 flush = 1;
Martin Schwidefsky6252d702008-02-09 18:24:37 +010093 }
Martin Schwidefsky80217142010-10-25 16:10:11 +020094 spin_unlock_bh(&mm->page_table_lock);
Martin Schwidefsky6252d702008-02-09 18:24:37 +010095 if (table)
96 crst_table_free(mm, table);
97 if (mm->context.asce_limit < limit)
98 goto repeat;
Martin Schwidefsky10607862013-10-28 14:48:30 +010099 if (flush)
100 on_each_cpu(__crst_table_upgrade, mm, 0);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100101 return 0;
102}
103
104void crst_table_downgrade(struct mm_struct *mm, unsigned long limit)
105{
106 pgd_t *pgd;
107
Martin Schwidefsky02a8f3a2014-04-03 13:54:59 +0200108 if (current->active_mm == mm) {
Martin Schwidefskybeef5602014-04-14 15:11:26 +0200109 clear_user_asce();
Martin Schwidefsky10607862013-10-28 14:48:30 +0100110 __tlb_flush_mm(mm);
Martin Schwidefsky02a8f3a2014-04-03 13:54:59 +0200111 }
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100112 while (mm->context.asce_limit > limit) {
113 pgd = mm->pgd;
114 switch (pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) {
115 case _REGION_ENTRY_TYPE_R2:
116 mm->context.asce_limit = 1UL << 42;
117 mm->context.asce_bits = _ASCE_TABLE_LENGTH |
118 _ASCE_USER_BITS |
119 _ASCE_TYPE_REGION3;
120 break;
121 case _REGION_ENTRY_TYPE_R3:
122 mm->context.asce_limit = 1UL << 31;
123 mm->context.asce_bits = _ASCE_TABLE_LENGTH |
124 _ASCE_USER_BITS |
125 _ASCE_TYPE_SEGMENT;
126 break;
127 default:
128 BUG();
129 }
130 mm->pgd = (pgd_t *) (pgd_val(*pgd) & _REGION_ENTRY_ORIGIN);
Martin Schwidefskyf481bfa2009-03-18 13:27:36 +0100131 mm->task_size = mm->context.asce_limit;
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100132 crst_table_free(mm, (unsigned long *) pgd);
133 }
Martin Schwidefsky10607862013-10-28 14:48:30 +0100134 if (current->active_mm == mm)
Martin Schwidefskybeef5602014-04-14 15:11:26 +0200135 set_user_asce(mm);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100136}
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100137
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200138#ifdef CONFIG_PGSTE
139
140/**
141 * gmap_alloc - allocate a guest address space
142 * @mm: pointer to the parent mm_struct
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200143 * @limit: maximum size of the gmap address space
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200144 *
145 * Returns a guest address space structure.
146 */
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200147struct gmap *gmap_alloc(struct mm_struct *mm, unsigned long limit)
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200148{
149 struct gmap *gmap;
150 struct page *page;
151 unsigned long *table;
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200152 unsigned long etype, atype;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200153
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200154 if (limit < (1UL << 31)) {
155 limit = (1UL << 31) - 1;
156 atype = _ASCE_TYPE_SEGMENT;
157 etype = _SEGMENT_ENTRY_EMPTY;
158 } else if (limit < (1UL << 42)) {
159 limit = (1UL << 42) - 1;
160 atype = _ASCE_TYPE_REGION3;
161 etype = _REGION3_ENTRY_EMPTY;
162 } else if (limit < (1UL << 53)) {
163 limit = (1UL << 53) - 1;
164 atype = _ASCE_TYPE_REGION2;
165 etype = _REGION2_ENTRY_EMPTY;
166 } else {
167 limit = -1UL;
168 atype = _ASCE_TYPE_REGION1;
169 etype = _REGION1_ENTRY_EMPTY;
170 }
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200171 gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL);
172 if (!gmap)
173 goto out;
174 INIT_LIST_HEAD(&gmap->crst_list);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200175 INIT_RADIX_TREE(&gmap->guest_to_host, GFP_KERNEL);
176 INIT_RADIX_TREE(&gmap->host_to_guest, GFP_ATOMIC);
177 spin_lock_init(&gmap->guest_table_lock);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200178 gmap->mm = mm;
179 page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
180 if (!page)
181 goto out_free;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200182 page->index = 0;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200183 list_add(&page->lru, &gmap->crst_list);
184 table = (unsigned long *) page_to_phys(page);
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200185 crst_table_init(table, etype);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200186 gmap->table = table;
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200187 gmap->asce = atype | _ASCE_TABLE_LENGTH |
188 _ASCE_USER_BITS | __pa(table);
189 gmap->asce_end = limit;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200190 down_write(&mm->mmap_sem);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200191 list_add(&gmap->list, &mm->context.gmap_list);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200192 up_write(&mm->mmap_sem);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200193 return gmap;
194
195out_free:
196 kfree(gmap);
197out:
198 return NULL;
199}
200EXPORT_SYMBOL_GPL(gmap_alloc);
201
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200202static void gmap_flush_tlb(struct gmap *gmap)
203{
204 if (MACHINE_HAS_IDTE)
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200205 __tlb_flush_asce(gmap->mm, gmap->asce);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200206 else
207 __tlb_flush_global();
208}
209
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200210static void gmap_radix_tree_free(struct radix_tree_root *root)
211{
212 struct radix_tree_iter iter;
213 unsigned long indices[16];
214 unsigned long index;
215 void **slot;
216 int i, nr;
217
218 /* A radix tree is freed by deleting all of its entries */
219 index = 0;
220 do {
221 nr = 0;
222 radix_tree_for_each_slot(slot, root, &iter, index) {
223 indices[nr] = iter.index;
224 if (++nr == 16)
225 break;
226 }
227 for (i = 0; i < nr; i++) {
228 index = indices[i];
229 radix_tree_delete(root, index);
230 }
231 } while (nr > 0);
232}
233
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200234/**
235 * gmap_free - free a guest address space
236 * @gmap: pointer to the guest address space structure
237 */
238void gmap_free(struct gmap *gmap)
239{
240 struct page *page, *next;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200241
242 /* Flush tlb. */
243 if (MACHINE_HAS_IDTE)
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200244 __tlb_flush_asce(gmap->mm, gmap->asce);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200245 else
246 __tlb_flush_global();
247
248 /* Free all segment & region tables. */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200249 list_for_each_entry_safe(page, next, &gmap->crst_list, lru)
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200250 __free_pages(page, ALLOC_ORDER);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200251 gmap_radix_tree_free(&gmap->guest_to_host);
252 gmap_radix_tree_free(&gmap->host_to_guest);
253 down_write(&gmap->mm->mmap_sem);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200254 list_del(&gmap->list);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200255 up_write(&gmap->mm->mmap_sem);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200256 kfree(gmap);
257}
258EXPORT_SYMBOL_GPL(gmap_free);
259
260/**
261 * gmap_enable - switch primary space to the guest address space
262 * @gmap: pointer to the guest address space structure
263 */
264void gmap_enable(struct gmap *gmap)
265{
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200266 S390_lowcore.gmap = (unsigned long) gmap;
267}
268EXPORT_SYMBOL_GPL(gmap_enable);
269
270/**
271 * gmap_disable - switch back to the standard primary address space
272 * @gmap: pointer to the guest address space structure
273 */
274void gmap_disable(struct gmap *gmap)
275{
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200276 S390_lowcore.gmap = 0UL;
277}
278EXPORT_SYMBOL_GPL(gmap_disable);
279
Carsten Ottea9162f22011-10-30 15:17:00 +0100280/*
281 * gmap_alloc_table is assumed to be called with mmap_sem held
282 */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200283static int gmap_alloc_table(struct gmap *gmap, unsigned long *table,
284 unsigned long init, unsigned long gaddr)
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200285{
286 struct page *page;
287 unsigned long *new;
288
Christian Borntraegerc86cce22011-12-27 11:25:47 +0100289 /* since we dont free the gmap table until gmap_free we can unlock */
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200290 page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
291 if (!page)
292 return -ENOMEM;
293 new = (unsigned long *) page_to_phys(page);
294 crst_table_init(new, init);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200295 spin_lock(&gmap->mm->page_table_lock);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200296 if (*table & _REGION_ENTRY_INVALID) {
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200297 list_add(&page->lru, &gmap->crst_list);
298 *table = (unsigned long) new | _REGION_ENTRY_LENGTH |
299 (*table & _REGION_ENTRY_TYPE_MASK);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200300 page->index = gaddr;
301 page = NULL;
302 }
303 spin_unlock(&gmap->mm->page_table_lock);
304 if (page)
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200305 __free_pages(page, ALLOC_ORDER);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200306 return 0;
307}
308
309/**
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200310 * __gmap_segment_gaddr - find virtual address from segment pointer
311 * @entry: pointer to a segment table entry in the guest address space
312 *
313 * Returns the virtual address in the guest address space for the segment
314 */
315static unsigned long __gmap_segment_gaddr(unsigned long *entry)
316{
317 struct page *page;
Martin Schwidefskyfbc89c92015-01-07 11:00:02 +0100318 unsigned long offset, mask;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200319
320 offset = (unsigned long) entry / sizeof(unsigned long);
321 offset = (offset & (PTRS_PER_PMD - 1)) * PMD_SIZE;
Martin Schwidefskyfbc89c92015-01-07 11:00:02 +0100322 mask = ~(PTRS_PER_PMD * sizeof(pmd_t) - 1);
323 page = virt_to_page((void *)((unsigned long) entry & mask));
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200324 return page->index + offset;
325}
326
327/**
328 * __gmap_unlink_by_vmaddr - unlink a single segment via a host address
329 * @gmap: pointer to the guest address space structure
330 * @vmaddr: address in the host process address space
331 *
332 * Returns 1 if a TLB flush is required
333 */
334static int __gmap_unlink_by_vmaddr(struct gmap *gmap, unsigned long vmaddr)
335{
336 unsigned long *entry;
337 int flush = 0;
338
339 spin_lock(&gmap->guest_table_lock);
340 entry = radix_tree_delete(&gmap->host_to_guest, vmaddr >> PMD_SHIFT);
341 if (entry) {
342 flush = (*entry != _SEGMENT_ENTRY_INVALID);
343 *entry = _SEGMENT_ENTRY_INVALID;
344 }
345 spin_unlock(&gmap->guest_table_lock);
346 return flush;
347}
348
349/**
350 * __gmap_unmap_by_gaddr - unmap a single segment via a guest address
351 * @gmap: pointer to the guest address space structure
352 * @gaddr: address in the guest address space
353 *
354 * Returns 1 if a TLB flush is required
355 */
356static int __gmap_unmap_by_gaddr(struct gmap *gmap, unsigned long gaddr)
357{
358 unsigned long vmaddr;
359
360 vmaddr = (unsigned long) radix_tree_delete(&gmap->guest_to_host,
361 gaddr >> PMD_SHIFT);
362 return vmaddr ? __gmap_unlink_by_vmaddr(gmap, vmaddr) : 0;
363}
364
365/**
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200366 * gmap_unmap_segment - unmap segment from the guest address space
367 * @gmap: pointer to the guest address space structure
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200368 * @to: address in the guest address space
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200369 * @len: length of the memory area to unmap
370 *
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +0100371 * Returns 0 if the unmap succeeded, -EINVAL if not.
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200372 */
373int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len)
374{
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200375 unsigned long off;
376 int flush;
377
378 if ((to | len) & (PMD_SIZE - 1))
379 return -EINVAL;
380 if (len == 0 || to + len < to)
381 return -EINVAL;
382
383 flush = 0;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200384 down_write(&gmap->mm->mmap_sem);
385 for (off = 0; off < len; off += PMD_SIZE)
386 flush |= __gmap_unmap_by_gaddr(gmap, to + off);
387 up_write(&gmap->mm->mmap_sem);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200388 if (flush)
389 gmap_flush_tlb(gmap);
390 return 0;
391}
392EXPORT_SYMBOL_GPL(gmap_unmap_segment);
393
394/**
395 * gmap_mmap_segment - map a segment to the guest address space
396 * @gmap: pointer to the guest address space structure
397 * @from: source address in the parent address space
398 * @to: target address in the guest address space
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200399 * @len: length of the memory area to map
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200400 *
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +0100401 * Returns 0 if the mmap succeeded, -EINVAL or -ENOMEM if not.
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200402 */
403int gmap_map_segment(struct gmap *gmap, unsigned long from,
404 unsigned long to, unsigned long len)
405{
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200406 unsigned long off;
407 int flush;
408
409 if ((from | to | len) & (PMD_SIZE - 1))
410 return -EINVAL;
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200411 if (len == 0 || from + len < from || to + len < to ||
412 from + len > TASK_MAX_SIZE || to + len > gmap->asce_end)
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200413 return -EINVAL;
414
415 flush = 0;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200416 down_write(&gmap->mm->mmap_sem);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200417 for (off = 0; off < len; off += PMD_SIZE) {
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200418 /* Remove old translation */
419 flush |= __gmap_unmap_by_gaddr(gmap, to + off);
420 /* Store new translation */
421 if (radix_tree_insert(&gmap->guest_to_host,
422 (to + off) >> PMD_SHIFT,
423 (void *) from + off))
424 break;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200425 }
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200426 up_write(&gmap->mm->mmap_sem);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200427 if (flush)
428 gmap_flush_tlb(gmap);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200429 if (off >= len)
430 return 0;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200431 gmap_unmap_segment(gmap, to, len);
432 return -ENOMEM;
433}
434EXPORT_SYMBOL_GPL(gmap_map_segment);
435
Heiko Carstensc5034942012-09-10 16:14:33 +0200436/**
437 * __gmap_translate - translate a guest address to a user space address
Heiko Carstensc5034942012-09-10 16:14:33 +0200438 * @gmap: pointer to guest mapping meta data structure
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200439 * @gaddr: guest address
Heiko Carstensc5034942012-09-10 16:14:33 +0200440 *
441 * Returns user space address which corresponds to the guest address or
442 * -EFAULT if no such mapping exists.
443 * This function does not establish potentially missing page table entries.
444 * The mmap_sem of the mm that belongs to the address space must be held
445 * when this function gets called.
446 */
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200447unsigned long __gmap_translate(struct gmap *gmap, unsigned long gaddr)
Heiko Carstensc5034942012-09-10 16:14:33 +0200448{
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200449 unsigned long vmaddr;
Heiko Carstensc5034942012-09-10 16:14:33 +0200450
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200451 vmaddr = (unsigned long)
452 radix_tree_lookup(&gmap->guest_to_host, gaddr >> PMD_SHIFT);
453 return vmaddr ? (vmaddr | (gaddr & ~PMD_MASK)) : -EFAULT;
Heiko Carstensc5034942012-09-10 16:14:33 +0200454}
455EXPORT_SYMBOL_GPL(__gmap_translate);
456
457/**
458 * gmap_translate - translate a guest address to a user space address
Heiko Carstensc5034942012-09-10 16:14:33 +0200459 * @gmap: pointer to guest mapping meta data structure
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200460 * @gaddr: guest address
Heiko Carstensc5034942012-09-10 16:14:33 +0200461 *
462 * Returns user space address which corresponds to the guest address or
463 * -EFAULT if no such mapping exists.
464 * This function does not establish potentially missing page table entries.
465 */
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200466unsigned long gmap_translate(struct gmap *gmap, unsigned long gaddr)
Heiko Carstensc5034942012-09-10 16:14:33 +0200467{
468 unsigned long rc;
469
470 down_read(&gmap->mm->mmap_sem);
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200471 rc = __gmap_translate(gmap, gaddr);
Heiko Carstensc5034942012-09-10 16:14:33 +0200472 up_read(&gmap->mm->mmap_sem);
473 return rc;
474}
475EXPORT_SYMBOL_GPL(gmap_translate);
476
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200477/**
478 * gmap_unlink - disconnect a page table from the gmap shadow tables
479 * @gmap: pointer to guest mapping meta data structure
480 * @table: pointer to the host page table
481 * @vmaddr: vm address associated with the host page table
482 */
483static void gmap_unlink(struct mm_struct *mm, unsigned long *table,
484 unsigned long vmaddr)
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200485{
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200486 struct gmap *gmap;
487 int flush;
488
489 list_for_each_entry(gmap, &mm->context.gmap_list, list) {
490 flush = __gmap_unlink_by_vmaddr(gmap, vmaddr);
491 if (flush)
492 gmap_flush_tlb(gmap);
493 }
494}
495
496/**
497 * gmap_link - set up shadow page tables to connect a host to a guest address
498 * @gmap: pointer to guest mapping meta data structure
499 * @gaddr: guest address
500 * @vmaddr: vm address
501 *
502 * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
503 * if the vm address is already mapped to a different guest segment.
504 * The mmap_sem of the mm that belongs to the address space must be held
505 * when this function gets called.
506 */
507int __gmap_link(struct gmap *gmap, unsigned long gaddr, unsigned long vmaddr)
508{
Heiko Carstensc5034942012-09-10 16:14:33 +0200509 struct mm_struct *mm;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200510 unsigned long *table;
511 spinlock_t *ptl;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200512 pgd_t *pgd;
513 pud_t *pud;
514 pmd_t *pmd;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200515 int rc;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200516
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200517 /* Create higher level tables in the gmap page table */
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200518 table = gmap->table;
519 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION1) {
520 table += (gaddr >> 53) & 0x7ff;
521 if ((*table & _REGION_ENTRY_INVALID) &&
522 gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY,
Heiko Carstens925dfc02014-12-12 13:04:21 +0100523 gaddr & 0xffe0000000000000UL))
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200524 return -ENOMEM;
525 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
526 }
527 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION2) {
528 table += (gaddr >> 42) & 0x7ff;
529 if ((*table & _REGION_ENTRY_INVALID) &&
530 gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY,
Heiko Carstens925dfc02014-12-12 13:04:21 +0100531 gaddr & 0xfffffc0000000000UL))
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200532 return -ENOMEM;
533 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
534 }
535 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION3) {
536 table += (gaddr >> 31) & 0x7ff;
537 if ((*table & _REGION_ENTRY_INVALID) &&
538 gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY,
Heiko Carstens925dfc02014-12-12 13:04:21 +0100539 gaddr & 0xffffffff80000000UL))
Martin Schwidefskyc6c956b2014-07-01 14:36:04 +0200540 return -ENOMEM;
541 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
542 }
543 table += (gaddr >> 20) & 0x7ff;
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200544 /* Walk the parent mm page table */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200545 mm = gmap->mm;
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200546 pgd = pgd_offset(mm, vmaddr);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200547 VM_BUG_ON(pgd_none(*pgd));
548 pud = pud_offset(pgd, vmaddr);
549 VM_BUG_ON(pud_none(*pud));
550 pmd = pmd_offset(pud, vmaddr);
551 VM_BUG_ON(pmd_none(*pmd));
Alex Thorlton1e1836e2014-04-07 15:37:09 -0700552 /* large pmds cannot yet be handled */
553 if (pmd_large(*pmd))
554 return -EFAULT;
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200555 /* Link gmap segment table entry location to page table. */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200556 rc = radix_tree_preload(GFP_KERNEL);
557 if (rc)
558 return rc;
559 ptl = pmd_lock(mm, pmd);
560 spin_lock(&gmap->guest_table_lock);
561 if (*table == _SEGMENT_ENTRY_INVALID) {
562 rc = radix_tree_insert(&gmap->host_to_guest,
563 vmaddr >> PMD_SHIFT, table);
564 if (!rc)
565 *table = pmd_val(*pmd);
566 } else
567 rc = 0;
568 spin_unlock(&gmap->guest_table_lock);
569 spin_unlock(ptl);
570 radix_tree_preload_end();
571 return rc;
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200572}
573
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200574/**
575 * gmap_fault - resolve a fault on a guest address
576 * @gmap: pointer to guest mapping meta data structure
577 * @gaddr: guest address
578 * @fault_flags: flags to pass down to handle_mm_fault()
579 *
580 * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
581 * if the vm address is already mapped to a different guest segment.
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200582 */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200583int gmap_fault(struct gmap *gmap, unsigned long gaddr,
584 unsigned int fault_flags)
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200585{
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200586 unsigned long vmaddr;
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200587 int rc;
588
Carsten Otte499069e2011-10-30 15:17:02 +0100589 down_read(&gmap->mm->mmap_sem);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200590 vmaddr = __gmap_translate(gmap, gaddr);
591 if (IS_ERR_VALUE(vmaddr)) {
592 rc = vmaddr;
593 goto out_up;
594 }
595 if (fixup_user_fault(current, gmap->mm, vmaddr, fault_flags)) {
596 rc = -EFAULT;
597 goto out_up;
598 }
599 rc = __gmap_link(gmap, gaddr, vmaddr);
600out_up:
Carsten Otte499069e2011-10-30 15:17:02 +0100601 up_read(&gmap->mm->mmap_sem);
Carsten Otte499069e2011-10-30 15:17:02 +0100602 return rc;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200603}
604EXPORT_SYMBOL_GPL(gmap_fault);
605
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200606static void gmap_zap_swap_entry(swp_entry_t entry, struct mm_struct *mm)
607{
608 if (!non_swap_entry(entry))
609 dec_mm_counter(mm, MM_SWAPENTS);
610 else if (is_migration_entry(entry)) {
611 struct page *page = migration_entry_to_page(entry);
612
613 if (PageAnon(page))
614 dec_mm_counter(mm, MM_ANONPAGES);
615 else
616 dec_mm_counter(mm, MM_FILEPAGES);
617 }
618 free_swap_and_cache(entry);
619}
620
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200621/*
622 * this function is assumed to be called with mmap_sem held
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200623 */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200624void __gmap_zap(struct gmap *gmap, unsigned long gaddr)
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200625{
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200626 unsigned long vmaddr, ptev, pgstev;
627 pte_t *ptep, pte;
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200628 spinlock_t *ptl;
629 pgste_t pgste;
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200630
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200631 /* Find the vm address for the guest address */
632 vmaddr = (unsigned long) radix_tree_lookup(&gmap->guest_to_host,
633 gaddr >> PMD_SHIFT);
634 if (!vmaddr)
635 return;
636 vmaddr |= gaddr & ~PMD_MASK;
637 /* Get pointer to the page table entry */
638 ptep = get_locked_pte(gmap->mm, vmaddr, &ptl);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200639 if (unlikely(!ptep))
640 return;
641 pte = *ptep;
642 if (!pte_swap(pte))
643 goto out_pte;
644 /* Zap unused and logically-zero pages */
645 pgste = pgste_get_lock(ptep);
646 pgstev = pgste_val(pgste);
647 ptev = pte_val(pte);
648 if (((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED) ||
649 ((pgstev & _PGSTE_GPS_ZERO) && (ptev & _PAGE_INVALID))) {
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200650 gmap_zap_swap_entry(pte_to_swp_entry(pte), gmap->mm);
651 pte_clear(gmap->mm, vmaddr, ptep);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200652 }
653 pgste_set_unlock(ptep, pgste);
654out_pte:
Dominik Dingel66e9bbd2014-10-06 16:34:44 +0200655 pte_unmap_unlock(ptep, ptl);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200656}
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200657EXPORT_SYMBOL_GPL(__gmap_zap);
658
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200659void gmap_discard(struct gmap *gmap, unsigned long from, unsigned long to)
Christian Borntraeger388186b2011-10-30 15:17:03 +0100660{
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200661 unsigned long gaddr, vmaddr, size;
Christian Borntraeger388186b2011-10-30 15:17:03 +0100662 struct vm_area_struct *vma;
Christian Borntraeger388186b2011-10-30 15:17:03 +0100663
664 down_read(&gmap->mm->mmap_sem);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200665 for (gaddr = from; gaddr < to;
666 gaddr = (gaddr + PMD_SIZE) & PMD_MASK) {
667 /* Find the vm address for the guest address */
668 vmaddr = (unsigned long)
669 radix_tree_lookup(&gmap->guest_to_host,
670 gaddr >> PMD_SHIFT);
671 if (!vmaddr)
Christian Borntraeger388186b2011-10-30 15:17:03 +0100672 continue;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200673 vmaddr |= gaddr & ~PMD_MASK;
674 /* Find vma in the parent mm */
675 vma = find_vma(gmap->mm, vmaddr);
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200676 size = min(to - gaddr, PMD_SIZE - (gaddr & ~PMD_MASK));
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200677 zap_page_range(vma, vmaddr, size, NULL);
Christian Borntraeger388186b2011-10-30 15:17:03 +0100678 }
679 up_read(&gmap->mm->mmap_sem);
680}
681EXPORT_SYMBOL_GPL(gmap_discard);
682
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200683static LIST_HEAD(gmap_notifier_list);
684static DEFINE_SPINLOCK(gmap_notifier_lock);
685
686/**
687 * gmap_register_ipte_notifier - register a pte invalidation callback
688 * @nb: pointer to the gmap notifier block
689 */
690void gmap_register_ipte_notifier(struct gmap_notifier *nb)
691{
692 spin_lock(&gmap_notifier_lock);
693 list_add(&nb->list, &gmap_notifier_list);
694 spin_unlock(&gmap_notifier_lock);
695}
696EXPORT_SYMBOL_GPL(gmap_register_ipte_notifier);
697
698/**
699 * gmap_unregister_ipte_notifier - remove a pte invalidation callback
700 * @nb: pointer to the gmap notifier block
701 */
702void gmap_unregister_ipte_notifier(struct gmap_notifier *nb)
703{
704 spin_lock(&gmap_notifier_lock);
705 list_del_init(&nb->list);
706 spin_unlock(&gmap_notifier_lock);
707}
708EXPORT_SYMBOL_GPL(gmap_unregister_ipte_notifier);
709
710/**
711 * gmap_ipte_notify - mark a range of ptes for invalidation notification
712 * @gmap: pointer to guest mapping meta data structure
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200713 * @gaddr: virtual address in the guest address space
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200714 * @len: size of area
715 *
716 * Returns 0 if for each page in the given range a gmap mapping exists and
717 * the invalidation notification could be set. If the gmap mapping is missing
718 * for one or more pages -EFAULT is returned. If no memory could be allocated
719 * -ENOMEM is returned. This function establishes missing page table entries.
720 */
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200721int gmap_ipte_notify(struct gmap *gmap, unsigned long gaddr, unsigned long len)
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200722{
723 unsigned long addr;
724 spinlock_t *ptl;
725 pte_t *ptep, entry;
726 pgste_t pgste;
727 int rc = 0;
728
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200729 if ((gaddr & ~PAGE_MASK) || (len & ~PAGE_MASK))
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200730 return -EINVAL;
731 down_read(&gmap->mm->mmap_sem);
732 while (len) {
733 /* Convert gmap address and connect the page tables */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200734 addr = __gmap_translate(gmap, gaddr);
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200735 if (IS_ERR_VALUE(addr)) {
736 rc = addr;
737 break;
738 }
739 /* Get the page mapped */
Christian Borntraegerbb4b42c2013-05-08 15:25:38 +0200740 if (fixup_user_fault(current, gmap->mm, addr, FAULT_FLAG_WRITE)) {
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200741 rc = -EFAULT;
742 break;
743 }
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200744 rc = __gmap_link(gmap, gaddr, addr);
745 if (rc)
746 break;
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200747 /* Walk the process page table, lock and get pte pointer */
748 ptep = get_locked_pte(gmap->mm, addr, &ptl);
Dominik Dingel6972cae2014-10-15 15:29:01 +0200749 VM_BUG_ON(!ptep);
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200750 /* Set notification bit in the pgste of the pte */
751 entry = *ptep;
Martin Schwidefskye5098612013-07-23 20:57:57 +0200752 if ((pte_val(entry) & (_PAGE_INVALID | _PAGE_PROTECT)) == 0) {
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200753 pgste = pgste_get_lock(ptep);
Martin Schwidefsky0d0dafc2013-05-17 14:41:33 +0200754 pgste_val(pgste) |= PGSTE_IN_BIT;
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200755 pgste_set_unlock(ptep, pgste);
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200756 gaddr += PAGE_SIZE;
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200757 len -= PAGE_SIZE;
758 }
Martin Schwidefskya697e052014-10-30 10:55:37 +0100759 pte_unmap_unlock(ptep, ptl);
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200760 }
761 up_read(&gmap->mm->mmap_sem);
762 return rc;
763}
764EXPORT_SYMBOL_GPL(gmap_ipte_notify);
765
766/**
767 * gmap_do_ipte_notify - call all invalidation callbacks for a specific pte.
768 * @mm: pointer to the process mm_struct
Martin Schwidefsky9da4e382014-04-30 14:46:26 +0200769 * @addr: virtual address in the process address space
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200770 * @pte: pointer to the page table entry
771 *
772 * This function is assumed to be called with the page table lock held
773 * for the pte to notify.
774 */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200775void gmap_do_ipte_notify(struct mm_struct *mm, unsigned long vmaddr, pte_t *pte)
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200776{
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200777 unsigned long offset, gaddr;
778 unsigned long *table;
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200779 struct gmap_notifier *nb;
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200780 struct gmap *gmap;
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200781
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200782 offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
783 offset = offset * (4096 / sizeof(pte_t));
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200784 spin_lock(&gmap_notifier_lock);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200785 list_for_each_entry(gmap, &mm->context.gmap_list, list) {
786 table = radix_tree_lookup(&gmap->host_to_guest,
787 vmaddr >> PMD_SHIFT);
788 if (!table)
789 continue;
790 gaddr = __gmap_segment_gaddr(table) + offset;
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200791 list_for_each_entry(nb, &gmap_notifier_list, list)
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200792 nb->notifier_call(gmap, gaddr);
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200793 }
794 spin_unlock(&gmap_notifier_lock);
795}
Martin Schwidefsky0a61b222013-10-18 12:03:41 +0200796EXPORT_SYMBOL_GPL(gmap_do_ipte_notify);
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200797
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +0200798static inline int page_table_with_pgste(struct page *page)
799{
800 return atomic_read(&page->_mapcount) == 0;
801}
802
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200803static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm)
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200804{
805 struct page *page;
806 unsigned long *table;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200807
808 page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
809 if (!page)
810 return NULL;
Kirill A. Shutemove89cfa52013-11-14 14:31:39 -0800811 if (!pgtable_page_ctor(page)) {
Kirill A. Shutemove89cfa52013-11-14 14:31:39 -0800812 __free_page(page);
813 return NULL;
814 }
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +0200815 atomic_set(&page->_mapcount, 0);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200816 table = (unsigned long *) page_to_phys(page);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200817 clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
Martin Schwidefsky0a61b222013-10-18 12:03:41 +0200818 clear_table(table + PTRS_PER_PTE, 0, PAGE_SIZE/2);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200819 return table;
820}
821
822static inline void page_table_free_pgste(unsigned long *table)
823{
824 struct page *page;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200825
826 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
Martin Schwidefsky2320c572012-02-17 10:29:21 +0100827 pgtable_page_dtor(page);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200828 atomic_set(&page->_mapcount, -1);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200829 __free_page(page);
830}
831
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200832int set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
833 unsigned long key, bool nq)
834{
835 spinlock_t *ptl;
836 pgste_t old, new;
837 pte_t *ptep;
838
839 down_read(&mm->mmap_sem);
Christian Borntraegerab3f2852014-08-19 16:19:35 +0200840retry:
Jason J. Herneedeb69e2014-10-07 13:31:37 -0400841 ptep = get_locked_pte(mm, addr, &ptl);
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200842 if (unlikely(!ptep)) {
843 up_read(&mm->mmap_sem);
844 return -EFAULT;
845 }
Christian Borntraegerab3f2852014-08-19 16:19:35 +0200846 if (!(pte_val(*ptep) & _PAGE_INVALID) &&
847 (pte_val(*ptep) & _PAGE_PROTECT)) {
Dominik Dingel66e9bbd2014-10-06 16:34:44 +0200848 pte_unmap_unlock(ptep, ptl);
Christian Borntraegerdc77d342014-08-27 12:20:02 +0200849 if (fixup_user_fault(current, mm, addr, FAULT_FLAG_WRITE)) {
850 up_read(&mm->mmap_sem);
851 return -EFAULT;
Christian Borntraegerab3f2852014-08-19 16:19:35 +0200852 }
Christian Borntraegerdc77d342014-08-27 12:20:02 +0200853 goto retry;
854 }
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200855
856 new = old = pgste_get_lock(ptep);
857 pgste_val(new) &= ~(PGSTE_GR_BIT | PGSTE_GC_BIT |
858 PGSTE_ACC_BITS | PGSTE_FP_BIT);
859 pgste_val(new) |= (key & (_PAGE_CHANGED | _PAGE_REFERENCED)) << 48;
860 pgste_val(new) |= (key & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
861 if (!(pte_val(*ptep) & _PAGE_INVALID)) {
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200862 unsigned long address, bits, skey;
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200863
864 address = pte_val(*ptep) & PAGE_MASK;
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200865 skey = (unsigned long) page_get_storage_key(address);
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200866 bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200867 skey = key & (_PAGE_ACC_BITS | _PAGE_FP_BIT);
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200868 /* Set storage key ACC and FP */
Martin Schwidefsky0944fe32013-07-23 22:11:42 +0200869 page_set_storage_key(address, skey, !nq);
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200870 /* Merge host changed & referenced into pgste */
871 pgste_val(new) |= bits << 52;
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200872 }
873 /* changing the guest storage key is considered a change of the page */
874 if ((pgste_val(new) ^ pgste_val(old)) &
875 (PGSTE_ACC_BITS | PGSTE_FP_BIT | PGSTE_GR_BIT | PGSTE_GC_BIT))
Martin Schwidefsky0a61b222013-10-18 12:03:41 +0200876 pgste_val(new) |= PGSTE_UC_BIT;
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200877
878 pgste_set_unlock(ptep, new);
Dominik Dingel66e9bbd2014-10-06 16:34:44 +0200879 pte_unmap_unlock(ptep, ptl);
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200880 up_read(&mm->mmap_sem);
881 return 0;
882}
883EXPORT_SYMBOL(set_guest_storage_key);
884
Jason J. Herne9fcf93b2014-09-23 09:18:57 -0400885unsigned long get_guest_storage_key(struct mm_struct *mm, unsigned long addr)
886{
887 spinlock_t *ptl;
888 pgste_t pgste;
889 pte_t *ptep;
890 uint64_t physaddr;
891 unsigned long key = 0;
892
893 down_read(&mm->mmap_sem);
894 ptep = get_locked_pte(mm, addr, &ptl);
895 if (unlikely(!ptep)) {
896 up_read(&mm->mmap_sem);
897 return -EFAULT;
898 }
899 pgste = pgste_get_lock(ptep);
900
901 if (pte_val(*ptep) & _PAGE_INVALID) {
902 key |= (pgste_val(pgste) & PGSTE_ACC_BITS) >> 56;
903 key |= (pgste_val(pgste) & PGSTE_FP_BIT) >> 56;
904 key |= (pgste_val(pgste) & PGSTE_GR_BIT) >> 48;
905 key |= (pgste_val(pgste) & PGSTE_GC_BIT) >> 48;
906 } else {
907 physaddr = pte_val(*ptep) & PAGE_MASK;
908 key = page_get_storage_key(physaddr);
909
910 /* Reflect guest's logical view, not physical */
911 if (pgste_val(pgste) & PGSTE_GR_BIT)
912 key |= _PAGE_REFERENCED;
913 if (pgste_val(pgste) & PGSTE_GC_BIT)
914 key |= _PAGE_CHANGED;
915 }
916
917 pgste_set_unlock(ptep, pgste);
918 pte_unmap_unlock(ptep, ptl);
919 up_read(&mm->mmap_sem);
920 return key;
921}
922EXPORT_SYMBOL(get_guest_storage_key);
923
Martin Schwidefsky0b46e0a2015-04-15 13:23:26 +0200924static int page_table_allocate_pgste_min = 0;
925static int page_table_allocate_pgste_max = 1;
926int page_table_allocate_pgste = 0;
927EXPORT_SYMBOL(page_table_allocate_pgste);
928
929static struct ctl_table page_table_sysctl[] = {
930 {
931 .procname = "allocate_pgste",
932 .data = &page_table_allocate_pgste,
933 .maxlen = sizeof(int),
934 .mode = S_IRUGO | S_IWUSR,
935 .proc_handler = proc_dointvec,
936 .extra1 = &page_table_allocate_pgste_min,
937 .extra2 = &page_table_allocate_pgste_max,
938 },
939 { }
940};
941
942static struct ctl_table page_table_sysctl_dir[] = {
943 {
944 .procname = "vm",
945 .maxlen = 0,
946 .mode = 0555,
947 .child = page_table_sysctl,
948 },
949 { }
950};
951
952static int __init page_table_register_sysctl(void)
953{
954 return register_sysctl_table(page_table_sysctl_dir) ? 0 : -ENOMEM;
955}
956__initcall(page_table_register_sysctl);
957
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200958#else /* CONFIG_PGSTE */
959
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +0200960static inline int page_table_with_pgste(struct page *page)
961{
962 return 0;
963}
964
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200965static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm)
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200966{
Jan Glauber944291d2011-08-03 16:44:18 +0200967 return NULL;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200968}
969
970static inline void page_table_free_pgste(unsigned long *table)
971{
972}
973
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200974static inline void gmap_unlink(struct mm_struct *mm, unsigned long *table,
975 unsigned long vmaddr)
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200976{
977}
978
979#endif /* CONFIG_PGSTE */
980
Martin Schwidefsky36409f62011-06-06 14:14:41 +0200981static inline unsigned int atomic_xor_bits(atomic_t *v, unsigned int bits)
982{
983 unsigned int old, new;
984
985 do {
986 old = atomic_read(v);
987 new = old ^ bits;
988 } while (atomic_cmpxchg(v, old, new) != old);
989 return new;
990}
991
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200992/*
993 * page table entry allocation/free routines.
994 */
Martin Schwidefsky527e30b2014-04-30 16:04:25 +0200995unsigned long *page_table_alloc(struct mm_struct *mm)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200996{
Heiko Carstens41459d32012-09-14 11:09:52 +0200997 unsigned long *uninitialized_var(table);
998 struct page *uninitialized_var(page);
Martin Schwidefsky36409f62011-06-06 14:14:41 +0200999 unsigned int mask, bit;
Martin Schwidefsky3610cce2007-10-22 12:52:47 +02001000
Martin Schwidefsky0b46e0a2015-04-15 13:23:26 +02001001 if (mm_alloc_pgste(mm))
Martin Schwidefsky527e30b2014-04-30 16:04:25 +02001002 return page_table_alloc_pgste(mm);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001003 /* Allocate fragments of a 4K page as 1K/2K page table */
Martin Schwidefsky80217142010-10-25 16:10:11 +02001004 spin_lock_bh(&mm->context.list_lock);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001005 mask = FRAG_MASK;
Martin Schwidefsky146e4b32008-02-09 18:24:35 +01001006 if (!list_empty(&mm->context.pgtable_list)) {
1007 page = list_first_entry(&mm->context.pgtable_list,
1008 struct page, lru);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001009 table = (unsigned long *) page_to_phys(page);
1010 mask = atomic_read(&page->_mapcount);
1011 mask = mask | (mask >> 4);
Martin Schwidefsky3610cce2007-10-22 12:52:47 +02001012 }
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001013 if ((mask & FRAG_MASK) == FRAG_MASK) {
Martin Schwidefsky80217142010-10-25 16:10:11 +02001014 spin_unlock_bh(&mm->context.list_lock);
Martin Schwidefsky146e4b32008-02-09 18:24:35 +01001015 page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
1016 if (!page)
1017 return NULL;
Kirill A. Shutemove89cfa52013-11-14 14:31:39 -08001018 if (!pgtable_page_ctor(page)) {
1019 __free_page(page);
1020 return NULL;
1021 }
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001022 atomic_set(&page->_mapcount, 1);
Martin Schwidefsky146e4b32008-02-09 18:24:35 +01001023 table = (unsigned long *) page_to_phys(page);
Martin Schwidefskye5098612013-07-23 20:57:57 +02001024 clear_table(table, _PAGE_INVALID, PAGE_SIZE);
Martin Schwidefsky80217142010-10-25 16:10:11 +02001025 spin_lock_bh(&mm->context.list_lock);
Martin Schwidefsky146e4b32008-02-09 18:24:35 +01001026 list_add(&page->lru, &mm->context.pgtable_list);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001027 } else {
1028 for (bit = 1; mask & bit; bit <<= 1)
1029 table += PTRS_PER_PTE;
1030 mask = atomic_xor_bits(&page->_mapcount, bit);
1031 if ((mask & FRAG_MASK) == FRAG_MASK)
1032 list_del(&page->lru);
Martin Schwidefsky146e4b32008-02-09 18:24:35 +01001033 }
Martin Schwidefsky80217142010-10-25 16:10:11 +02001034 spin_unlock_bh(&mm->context.list_lock);
Martin Schwidefsky3610cce2007-10-22 12:52:47 +02001035 return table;
1036}
1037
Martin Schwidefsky146e4b32008-02-09 18:24:35 +01001038void page_table_free(struct mm_struct *mm, unsigned long *table)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +02001039{
Martin Schwidefsky146e4b32008-02-09 18:24:35 +01001040 struct page *page;
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001041 unsigned int bit, mask;
Martin Schwidefsky3610cce2007-10-22 12:52:47 +02001042
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001043 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
Martin Schwidefsky527e30b2014-04-30 16:04:25 +02001044 if (page_table_with_pgste(page))
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001045 return page_table_free_pgste(table);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001046 /* Free 1K/2K page table fragment of a 4K page */
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001047 bit = 1 << ((__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t)));
Martin Schwidefsky80217142010-10-25 16:10:11 +02001048 spin_lock_bh(&mm->context.list_lock);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001049 if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK)
Martin Schwidefsky146e4b32008-02-09 18:24:35 +01001050 list_del(&page->lru);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001051 mask = atomic_xor_bits(&page->_mapcount, bit);
1052 if (mask & FRAG_MASK)
1053 list_add(&page->lru, &mm->context.pgtable_list);
Martin Schwidefsky80217142010-10-25 16:10:11 +02001054 spin_unlock_bh(&mm->context.list_lock);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001055 if (mask == 0) {
Martin Schwidefsky146e4b32008-02-09 18:24:35 +01001056 pgtable_page_dtor(page);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001057 atomic_set(&page->_mapcount, -1);
Martin Schwidefsky146e4b32008-02-09 18:24:35 +01001058 __free_page(page);
1059 }
1060}
Martin Schwidefsky3610cce2007-10-22 12:52:47 +02001061
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001062static void __page_table_free_rcu(void *table, unsigned bit)
1063{
1064 struct page *page;
1065
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001066 if (bit == FRAG_MASK)
1067 return page_table_free_pgste(table);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001068 /* Free 1K/2K page table fragment of a 4K page */
1069 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1070 if (atomic_xor_bits(&page->_mapcount, bit) == 0) {
1071 pgtable_page_dtor(page);
1072 atomic_set(&page->_mapcount, -1);
1073 __free_page(page);
Martin Schwidefsky80217142010-10-25 16:10:11 +02001074 }
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001075}
1076
Martin Schwidefsky527e30b2014-04-30 16:04:25 +02001077void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table,
1078 unsigned long vmaddr)
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001079{
1080 struct mm_struct *mm;
1081 struct page *page;
1082 unsigned int bit, mask;
1083
1084 mm = tlb->mm;
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001085 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1086 if (page_table_with_pgste(page)) {
Martin Schwidefsky527e30b2014-04-30 16:04:25 +02001087 gmap_unlink(mm, table, vmaddr);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001088 table = (unsigned long *) (__pa(table) | FRAG_MASK);
1089 tlb_remove_table(tlb, table);
1090 return;
Martin Schwidefsky80217142010-10-25 16:10:11 +02001091 }
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001092 bit = 1 << ((__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t)));
Martin Schwidefsky80217142010-10-25 16:10:11 +02001093 spin_lock_bh(&mm->context.list_lock);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001094 if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK)
1095 list_del(&page->lru);
1096 mask = atomic_xor_bits(&page->_mapcount, bit | (bit << 4));
1097 if (mask & FRAG_MASK)
1098 list_add_tail(&page->lru, &mm->context.pgtable_list);
Martin Schwidefsky80217142010-10-25 16:10:11 +02001099 spin_unlock_bh(&mm->context.list_lock);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001100 table = (unsigned long *) (__pa(table) | (bit << 4));
1101 tlb_remove_table(tlb, table);
Martin Schwidefsky80217142010-10-25 16:10:11 +02001102}
1103
Heiko Carstens63df41d62013-09-06 19:10:48 +02001104static void __tlb_remove_table(void *_table)
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001105{
Martin Schwidefskye73b7ff2011-10-30 15:16:08 +01001106 const unsigned long mask = (FRAG_MASK << 4) | FRAG_MASK;
1107 void *table = (void *)((unsigned long) _table & ~mask);
1108 unsigned type = (unsigned long) _table & mask;
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001109
1110 if (type)
1111 __page_table_free_rcu(table, type);
1112 else
1113 free_pages((unsigned long) table, ALLOC_ORDER);
1114}
1115
Martin Schwidefskycd94154cc2012-04-11 14:28:07 +02001116static void tlb_remove_table_smp_sync(void *arg)
1117{
1118 /* Simply deliver the interrupt */
1119}
1120
1121static void tlb_remove_table_one(void *table)
1122{
1123 /*
1124 * This isn't an RCU grace period and hence the page-tables cannot be
1125 * assumed to be actually RCU-freed.
1126 *
1127 * It is however sufficient for software page-table walkers that rely
1128 * on IRQ disabling. See the comment near struct mmu_table_batch.
1129 */
1130 smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
1131 __tlb_remove_table(table);
1132}
1133
1134static void tlb_remove_table_rcu(struct rcu_head *head)
1135{
1136 struct mmu_table_batch *batch;
1137 int i;
1138
1139 batch = container_of(head, struct mmu_table_batch, rcu);
1140
1141 for (i = 0; i < batch->nr; i++)
1142 __tlb_remove_table(batch->tables[i]);
1143
1144 free_page((unsigned long)batch);
1145}
1146
1147void tlb_table_flush(struct mmu_gather *tlb)
1148{
1149 struct mmu_table_batch **batch = &tlb->batch;
1150
1151 if (*batch) {
Martin Schwidefskycd94154cc2012-04-11 14:28:07 +02001152 call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
1153 *batch = NULL;
1154 }
1155}
1156
1157void tlb_remove_table(struct mmu_gather *tlb, void *table)
1158{
1159 struct mmu_table_batch **batch = &tlb->batch;
1160
Martin Schwidefsky5c474a12013-08-16 13:31:40 +02001161 tlb->mm->context.flush_mm = 1;
Martin Schwidefskycd94154cc2012-04-11 14:28:07 +02001162 if (*batch == NULL) {
1163 *batch = (struct mmu_table_batch *)
1164 __get_free_page(GFP_NOWAIT | __GFP_NOWARN);
1165 if (*batch == NULL) {
Martin Schwidefsky5c474a12013-08-16 13:31:40 +02001166 __tlb_flush_mm_lazy(tlb->mm);
Martin Schwidefskycd94154cc2012-04-11 14:28:07 +02001167 tlb_remove_table_one(table);
1168 return;
1169 }
1170 (*batch)->nr = 0;
1171 }
1172 (*batch)->tables[(*batch)->nr++] = table;
1173 if ((*batch)->nr == MAX_TABLE_BATCH)
Martin Schwidefsky5c474a12013-08-16 13:31:40 +02001174 tlb_flush_mmu(tlb);
Martin Schwidefskycd94154cc2012-04-11 14:28:07 +02001175}
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001176
Gerald Schaefer274023d2012-10-08 16:30:21 -07001177#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001178static inline void thp_split_vma(struct vm_area_struct *vma)
Gerald Schaefer274023d2012-10-08 16:30:21 -07001179{
1180 unsigned long addr;
Gerald Schaefer274023d2012-10-08 16:30:21 -07001181
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001182 for (addr = vma->vm_start; addr < vma->vm_end; addr += PAGE_SIZE)
1183 follow_page(vma, addr, FOLL_SPLIT);
Gerald Schaefer274023d2012-10-08 16:30:21 -07001184}
1185
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001186static inline void thp_split_mm(struct mm_struct *mm)
Gerald Schaefer274023d2012-10-08 16:30:21 -07001187{
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001188 struct vm_area_struct *vma;
Gerald Schaefer274023d2012-10-08 16:30:21 -07001189
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001190 for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) {
Gerald Schaefer274023d2012-10-08 16:30:21 -07001191 thp_split_vma(vma);
1192 vma->vm_flags &= ~VM_HUGEPAGE;
1193 vma->vm_flags |= VM_NOHUGEPAGE;
Gerald Schaefer274023d2012-10-08 16:30:21 -07001194 }
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001195 mm->def_flags |= VM_NOHUGEPAGE;
1196}
1197#else
1198static inline void thp_split_mm(struct mm_struct *mm)
1199{
Gerald Schaefer274023d2012-10-08 16:30:21 -07001200}
1201#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1202
Carsten Otte402b0862008-03-25 18:47:10 +01001203/*
1204 * switch on pgstes for its userspace process (for kvm)
1205 */
1206int s390_enable_sie(void)
1207{
Martin Schwidefsky0b46e0a2015-04-15 13:23:26 +02001208 struct mm_struct *mm = current->mm;
Carsten Otte402b0862008-03-25 18:47:10 +01001209
Christian Borntraeger74b6b522008-05-21 13:37:29 +02001210 /* Do we have pgstes? if yes, we are done */
Martin Schwidefsky0b46e0a2015-04-15 13:23:26 +02001211 if (mm_has_pgste(mm))
Christian Borntraeger74b6b522008-05-21 13:37:29 +02001212 return 0;
Martin Schwidefsky0b46e0a2015-04-15 13:23:26 +02001213 /* Fail if the page tables are 2K */
1214 if (!mm_alloc_pgste(mm))
1215 return -EINVAL;
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001216 down_write(&mm->mmap_sem);
Martin Schwidefsky0b46e0a2015-04-15 13:23:26 +02001217 mm->context.has_pgste = 1;
Gerald Schaefer274023d2012-10-08 16:30:21 -07001218 /* split thp mappings and disable thp for future mappings */
1219 thp_split_mm(mm);
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001220 up_write(&mm->mmap_sem);
Martin Schwidefsky0b46e0a2015-04-15 13:23:26 +02001221 return 0;
Carsten Otte402b0862008-03-25 18:47:10 +01001222}
1223EXPORT_SYMBOL_GPL(s390_enable_sie);
Hans-Joachim Picht7db11a32009-06-16 10:30:26 +02001224
Dominik Dingel934bc132014-01-14 18:10:17 +01001225/*
1226 * Enable storage key handling from now on and initialize the storage
1227 * keys with the default key.
1228 */
Dominik Dingela13cff32014-10-23 12:07:14 +02001229static int __s390_enable_skey(pte_t *pte, unsigned long addr,
1230 unsigned long next, struct mm_walk *walk)
1231{
1232 unsigned long ptev;
1233 pgste_t pgste;
1234
1235 pgste = pgste_get_lock(pte);
Dominik Dingel2faee8f2014-10-23 12:08:38 +02001236 /*
1237 * Remove all zero page mappings,
1238 * after establishing a policy to forbid zero page mappings
1239 * following faults for that page will get fresh anonymous pages
1240 */
1241 if (is_zero_pfn(pte_pfn(*pte))) {
1242 ptep_flush_direct(walk->mm, addr, pte);
1243 pte_val(*pte) = _PAGE_INVALID;
1244 }
Dominik Dingela13cff32014-10-23 12:07:14 +02001245 /* Clear storage key */
1246 pgste_val(pgste) &= ~(PGSTE_ACC_BITS | PGSTE_FP_BIT |
1247 PGSTE_GR_BIT | PGSTE_GC_BIT);
1248 ptev = pte_val(*pte);
1249 if (!(ptev & _PAGE_INVALID) && (ptev & _PAGE_WRITE))
1250 page_set_storage_key(ptev & PAGE_MASK, PAGE_DEFAULT_KEY, 1);
1251 pgste_set_unlock(pte, pgste);
1252 return 0;
1253}
1254
Dominik Dingel3ac8e382014-10-23 12:09:17 +02001255int s390_enable_skey(void)
Dominik Dingel934bc132014-01-14 18:10:17 +01001256{
Dominik Dingela13cff32014-10-23 12:07:14 +02001257 struct mm_walk walk = { .pte_entry = __s390_enable_skey };
1258 struct mm_struct *mm = current->mm;
Dominik Dingel3ac8e382014-10-23 12:09:17 +02001259 struct vm_area_struct *vma;
1260 int rc = 0;
Dominik Dingela13cff32014-10-23 12:07:14 +02001261
1262 down_write(&mm->mmap_sem);
1263 if (mm_use_skey(mm))
1264 goto out_up;
Dominik Dingel2faee8f2014-10-23 12:08:38 +02001265
1266 mm->context.use_skey = 1;
Dominik Dingel3ac8e382014-10-23 12:09:17 +02001267 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1268 if (ksm_madvise(vma, vma->vm_start, vma->vm_end,
1269 MADV_UNMERGEABLE, &vma->vm_flags)) {
1270 mm->context.use_skey = 0;
1271 rc = -ENOMEM;
1272 goto out_up;
1273 }
1274 }
1275 mm->def_flags &= ~VM_MERGEABLE;
Dominik Dingel2faee8f2014-10-23 12:08:38 +02001276
Dominik Dingela13cff32014-10-23 12:07:14 +02001277 walk.mm = mm;
1278 walk_page_range(0, TASK_SIZE, &walk);
Dominik Dingela13cff32014-10-23 12:07:14 +02001279
1280out_up:
1281 up_write(&mm->mmap_sem);
Dominik Dingel3ac8e382014-10-23 12:09:17 +02001282 return rc;
Dominik Dingel934bc132014-01-14 18:10:17 +01001283}
1284EXPORT_SYMBOL_GPL(s390_enable_skey);
1285
Dominik Dingela0bf4f12014-03-24 14:27:58 +01001286/*
Dominik Dingela13cff32014-10-23 12:07:14 +02001287 * Reset CMMA state, make all pages stable again.
1288 */
1289static 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
1300void 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}
1309EXPORT_SYMBOL_GPL(s390_reset_cmma);
1310
1311/*
Dominik Dingela0bf4f12014-03-24 14:27:58 +01001312 * Test and reset if a guest page is dirty
1313 */
1314bool 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}
1330EXPORT_SYMBOL_GPL(gmap_test_and_clear_dirty);
1331
Gerald Schaefer75077af2012-10-08 16:30:15 -07001332#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001333int 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
1342int 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 Schwidefsky152125b2014-07-24 11:03:41 +02001348 entry = pmd_mkyoung(entry);
1349 if (dirty)
1350 entry = pmd_mkdirty(entry);
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001351 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 Schaefer75077af2012-10-08 16:30:15 -07001358static void pmdp_splitting_flush_sync(void *arg)
1359{
1360 /* Simply deliver the interrupt */
1361}
1362
1363void 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 Schaefer9501d092012-10-08 16:30:18 -07001373
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001374void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
1375 pgtable_t pgtable)
Gerald Schaefer9501d092012-10-08 16:30:18 -07001376{
1377 struct list_head *lh = (struct list_head *) pgtable;
1378
Martin Schwidefskyec66ad62014-02-12 14:16:18 +01001379 assert_spin_locked(pmd_lockptr(mm, pmdp));
Gerald Schaefer9501d092012-10-08 16:30:18 -07001380
1381 /* FIFO */
Kirill A. Shutemovc389a252013-11-14 14:30:59 -08001382 if (!pmd_huge_pte(mm, pmdp))
Gerald Schaefer9501d092012-10-08 16:30:18 -07001383 INIT_LIST_HEAD(lh);
1384 else
Kirill A. Shutemovc389a252013-11-14 14:30:59 -08001385 list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
1386 pmd_huge_pte(mm, pmdp) = pgtable;
Gerald Schaefer9501d092012-10-08 16:30:18 -07001387}
1388
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001389pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
Gerald Schaefer9501d092012-10-08 16:30:18 -07001390{
1391 struct list_head *lh;
1392 pgtable_t pgtable;
1393 pte_t *ptep;
1394
Martin Schwidefskyec66ad62014-02-12 14:16:18 +01001395 assert_spin_locked(pmd_lockptr(mm, pmdp));
Gerald Schaefer9501d092012-10-08 16:30:18 -07001396
1397 /* FIFO */
Kirill A. Shutemovc389a252013-11-14 14:30:59 -08001398 pgtable = pmd_huge_pte(mm, pmdp);
Gerald Schaefer9501d092012-10-08 16:30:18 -07001399 lh = (struct list_head *) pgtable;
1400 if (list_empty(lh))
Kirill A. Shutemovc389a252013-11-14 14:30:59 -08001401 pmd_huge_pte(mm, pmdp) = NULL;
Gerald Schaefer9501d092012-10-08 16:30:18 -07001402 else {
Kirill A. Shutemovc389a252013-11-14 14:30:59 -08001403 pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
Gerald Schaefer9501d092012-10-08 16:30:18 -07001404 list_del(lh);
1405 }
1406 ptep = (pte_t *) pgtable;
Martin Schwidefskye5098612013-07-23 20:57:57 +02001407 pte_val(*ptep) = _PAGE_INVALID;
Gerald Schaefer9501d092012-10-08 16:30:18 -07001408 ptep++;
Martin Schwidefskye5098612013-07-23 20:57:57 +02001409 pte_val(*ptep) = _PAGE_INVALID;
Gerald Schaefer9501d092012-10-08 16:30:18 -07001410 return pgtable;
1411}
Gerald Schaefer75077af2012-10-08 16:30:15 -07001412#endif /* CONFIG_TRANSPARENT_HUGEPAGE */